From e36672d5f48ed1514b2a2846e24c52c98920b2ff Mon Sep 17 00:00:00 2001 From: Arthur Axel 'fREW' Schmidt Date: Mon, 9 Nov 2026 17:01:24 -0800 Subject: [PATCH] Add GC 2.5a: 2.5 with FFCC's store alias rule Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QmcpERTQ1WaRZ73F12eUis --- README.md | 55 +++++++++++++++++++ config/GC_2_5a/config.json | 4 ++ include/compiler/types.h | 2 +- include/version.h | 5 +- src/compiler/backend/CMachine.c | 8 +-- .../backend/MachineSimulationAltiVec.c | 6 +- src/compiler/driver/mwcceppc.rc | 2 +- src/compiler/frontend/CParser.c | 2 +- src/compiler/optimizer/Alias.c | 21 +++++++ src/driver/CmdLineBuildDate.c | 2 +- 10 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 config/GC_2_5a/config.json diff --git a/README.md b/README.md index f6f114e..3323d22 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ A decompilation of the Metrowerks CodeWarrior toolchain. Currently supported ver | GameCube 2.0 | `GC_2_0` | 2.4.7 build 92 | 4016 / 4065 | 2850 / 4068 (70.1%) | 54.3% | | | GameCube 2.0p2 | `GC_2_0p2` | 2.4.7 build 92 | 4019 / 4068 | 2853 / 4071 (70.1%) | 54.3% | GameCube 2.0 patched for BFBB | | GameCube 2.5 | `GC_2_5` | 2.4.7 build 105 | 4026 / 4076 | 2860 / 4079 (70.1%) | 54.3% | | +| GameCube 2.5a | `GC_2_5a` | 2.4.7 build 105 | 4026 / 4076 | 2860 / 4079 (70.1%) | 54.3% | GameCube 2.5 patched for FFCC | | GameCube 2.6 | `GC_2_6` | 2.4.7 build 107 | 4027 / 4077 | 2860 / 4080 (70.1%) | 54.4% | | | GameCube 2.7 | `GC_2_7` | 2.4.7 build 108 | 4033 / 4084 | 2848 / 4087 (69.7%) | 54.2% | | @@ -21,6 +22,9 @@ A decompilation of the Metrowerks CodeWarrior toolchain. Currently supported ver - 2.0p2 is not an official binary; it models the observed side effects of compiling the [*Battle for Bikini Bottom*](https://github.com/bfbbdecomp/bfbb) decompilation against GameCube 2.0 since it seems to have been compiled with a unique patched compiler. See [below](#battle-for-bikini-bottom-20p2) for more information. +- 2.5a is not an official binary either; it models the one difference from 2.5 seen when compiling the PAL and USA game + code of the [*Final Fantasy Crystal Chronicles*](https://github.com/zcanann/FFCC-Decomp) decompilation. See + [below](#final-fantasy-crystal-chronicles-25a) for more information. ## Getting started @@ -162,6 +166,57 @@ been: - `snow_life` should not be `const`; - `NPAR_TubeSpiralMagic`'s colours should be `static const` locals. +### Final Fantasy Crystal Chronicles (2.5a) + +*Final Fantasy Crystal Chronicles*' PAL and USA game code was built with a 2.5 that no release matches. 2.5, 2.6 and +2.7 produce byte-identical objects for all of the FFCC decompilation's PAL (269) and USA (268) game units, and 2.0 only +loses functions. 2.5a is 2.5 with one rule of its own, in alias analysis (`operand_alias`): + +- a store through a pointer with several reaching definitions, some with an alias and some without, keeps the alias of + the ones that have one when that alias is a single static object (whole or part, not a local), instead of making the + store `worst_case`. Loads, and stores into locals, are unchanged. + +FFCC's `pppRand*` randomizers pick a buffer and then store each channel through it, reading a scale through another +pointer each time: + +```c +s32* target = (in->sourceOffset == -1) ? (s32*)gPppDefaultValueBuffer : (s32*)(work + in->sourceOffset); +target[0] += randint(in->blend[0], *valuePtr); +target[1] += randint(in->blend[1], *valuePtr); +``` + +2.5 reloads `*valuePtr` after each store through `target`; the retail game, and 2.5a, load it once and keep it in a +register (`pppRandIV`): + +``` +2.5 2.5a, retail +stw r0,0(r4) stw r0,0(r4) +lwz r0,12(r31) lwz r0,12(r31) +lfs f1,0(r5) lwz r3,4(r4) +xoris r0,r0,32768 xoris r0,r0,32768 +lwz r3,4(r4) +``` + +Functions matching retail, over every FFCC game unit built with 2.5 (3121 functions in PAL, 3116 in USA): + +| Compiler | PAL | PAL, `pppRand*` as above | USA | USA, `pppRand*` as above | +| -------- | ------- | ------------------------ | ------- | ------------------------ | +| 2.5 | 3066 | 3063 | 3061 | 3058 | +| **2.5a** | 3066 | **3075** | 3061 | **3070** | + +FFCC's current source works around 2.5 by reading the scale into a local once, which comes close but leaves nine of +these functions inexact. On that source, 2.5a's objects are byte-identical to 2.5's. With the `pppRand*` source above, +which is also the source the Japanese release matches, 2.5a matches nine more functions than 2.5 does on the current +source, and loses none. Broader forms of the rule lose functions (PAL, current source): + +- for loads and stores alike: 18 functions lost and none gained (with the `pppRand*` source above, 15 lost and none + gained: the loads of `target` move above the stores); +- for every store, including into locals: `CMenuPcs::loadData` lost (a store into a local array); +- for stores to statics only (2.5a): nothing lost. + +Only FFCC has been measured. To use 2.5a in FFCC, put `build/GC_2_5a/clang/mwcceppc.exe` in place of the project's +2.5 compiler for its game code. + ## Contributing - Format C with `clang-format -i`, and Python with `ruff format` and `ruff check` (the venv's pinned versions); CI diff --git a/config/GC_2_5a/config.json b/config/GC_2_5a/config.json new file mode 100644 index 0000000..8b2503b --- /dev/null +++ b/config/GC_2_5a/config.json @@ -0,0 +1,4 @@ +{ + "derived_from": "GC_2_5", + "description": "FFCC's compiler: 2.5 where a store through a pointer that may be one static object keeps that object's alias; no original, built from the sources alone" +} diff --git a/include/compiler/types.h b/include/compiler/types.h index 3d371b1..de2d931 100644 --- a/include/compiler/types.h +++ b/include/compiler/types.h @@ -279,7 +279,7 @@ struct TypeBitfield { char offset; char bitlength; char suppressAlignment; -#if VERSION == VERSION_GC_2_5 +#if VERSION == VERSION_GC_2_5 || VERSION == VERSION_GC_2_5A char declsize; char x0e; #endif diff --git a/include/version.h b/include/version.h index 45ad0a5..ddbabed 100644 --- a/include/version.h +++ b/include/version.h @@ -4,8 +4,9 @@ #define VERSION_GC_2_0 0 #define VERSION_GC_2_0P2 1 #define VERSION_GC_2_5 2 -#define VERSION_GC_2_6 3 -#define VERSION_GC_2_7 4 +#define VERSION_GC_2_5A 3 +#define VERSION_GC_2_6 4 +#define VERSION_GC_2_7 5 #ifndef VERSION #define VERSION VERSION_GC_2_6 diff --git a/src/compiler/backend/CMachine.c b/src/compiler/backend/CMachine.c index f7828a4..c206bf5 100644 --- a/src/compiler/backend/CMachine.c +++ b/src/compiler/backend/CMachine.c @@ -134,7 +134,7 @@ static UInt8 cmach_structalign; static short cmach_curbfsize; static short cmach_curbfbasesize; static int cmach_curbfoffset; -#if VERSION == VERSION_GC_2_5 +#if VERSION == VERSION_GC_2_5 || VERSION == VERSION_GC_2_5A static int cmach_curbfintegral; #endif @@ -1338,7 +1338,7 @@ void CMach_StructLayoutInitOffset(SInt32 offset) { if (!copts.old_style_bitfields) { cmach_curbfbasesize = 0; cmach_curbfoffset = 0; -#if VERSION == VERSION_GC_2_5 +#if VERSION == VERSION_GC_2_5 || VERSION == VERSION_GC_2_5A cmach_curbfintegral = 0; #endif } @@ -1505,7 +1505,7 @@ static SInt32 CMach_StructLayoutPackedBitfield( return CMach_StructLayoutNewBitfieldUnit(tbitfield, basesize); } -#if VERSION == VERSION_GC_2_5 +#if VERSION == VERSION_GC_2_5 || VERSION == VERSION_GC_2_5A SInt32 CMach_StructLayoutBitfield(TypeBitfield* tbitfield, UInt32 qual) { SInt16 align; SInt16 padding_at_start; @@ -2247,7 +2247,7 @@ void CMach_ReInitRuntimeObjects(void) { #undef CError_LINE_SHIFT #define CError_LINE_SHIFT -68 #endif -#if VERSION == VERSION_GC_2_5 +#if VERSION == VERSION_GC_2_5 || VERSION == VERSION_GC_2_5A #undef CError_LINE_SHIFT #define CError_LINE_SHIFT 42 #endif diff --git a/src/compiler/backend/MachineSimulationAltiVec.c b/src/compiler/backend/MachineSimulationAltiVec.c index 7036cd2..8ab20b1 100644 --- a/src/compiler/backend/MachineSimulationAltiVec.c +++ b/src/compiler/backend/MachineSimulationAltiVec.c @@ -576,7 +576,7 @@ static struct { 0, CFX2, 0, 0, 0, 1, 0, 0, 0, #if VERSION < VERSION_GC_2_5 0, BPU, 0, 80, -114, 87, 0, 112, -115, -#elif VERSION == VERSION_GC_2_5 +#elif VERSION <= VERSION_GC_2_5A 0, BPU, 0, 16, -107, 87, 0, 48, -108, #elif VERSION == VERSION_GC_2_6 0, BPU, 0, -128, -111, 87, 0, -96, -112, @@ -585,7 +585,7 @@ static struct { #endif #if VERSION < VERSION_GC_2_5 87, 96, -117, 87, 0, -32, -118, 87, 0, -#elif VERSION == VERSION_GC_2_5 +#elif VERSION <= VERSION_GC_2_5A 87, 32, -110, 87, 0, -96, -111, 87, 0, #elif VERSION == VERSION_GC_2_6 87, 144, -114, 87, 0, 16, -114, 87, 0, @@ -594,7 +594,7 @@ static struct { #endif #if VERSION < VERSION_GC_2_5 -31504, 87, 0, -48, -124, 87, 0, -80, -124, -#elif VERSION == VERSION_GC_2_5 +#elif VERSION <= VERSION_GC_2_5A -29776, 87, 0, -112, -117, 87, 0, 112, -117, #elif VERSION == VERSION_GC_2_6 -30688, 87, 0, 0, -120, 87, 0, -32, -121, diff --git a/src/compiler/driver/mwcceppc.rc b/src/compiler/driver/mwcceppc.rc index 32a8374..914ce64 100644 --- a/src/compiler/driver/mwcceppc.rc +++ b/src/compiler/driver/mwcceppc.rc @@ -3,7 +3,7 @@ #if VERSION <= VERSION_GC_2_0P2 #define BUILD 92 #define BUILD_STRING L"2.4.7.92\0" -#elif VERSION == VERSION_GC_2_5 +#elif VERSION <= VERSION_GC_2_5A #define BUILD 105 #define BUILD_STRING L"2.4.7.105\0" #elif VERSION == VERSION_GC_2_6 diff --git a/src/compiler/frontend/CParser.c b/src/compiler/frontend/CParser.c index c1cacc5..20c5b6b 100644 --- a/src/compiler/frontend/CParser.c +++ b/src/compiler/frontend/CParser.c @@ -131,7 +131,7 @@ typedef struct OriginalObject { #if VERSION < VERSION_GC_2_5 #define ORIGINAL(object, v2_0, v2_5, v2_6, v2_7) {&(object), sizeof(object), v2_0} -#elif VERSION == VERSION_GC_2_5 +#elif VERSION <= VERSION_GC_2_5A #define ORIGINAL(object, v2_0, v2_5, v2_6, v2_7) {&(object), sizeof(object), v2_5} #elif VERSION == VERSION_GC_2_6 #define ORIGINAL(object, v2_0, v2_5, v2_6, v2_7) {&(object), sizeof(object), v2_6} diff --git a/src/compiler/optimizer/Alias.c b/src/compiler/optimizer/Alias.c index c47f375..500aac0 100644 --- a/src/compiler/optimizer/Alias.c +++ b/src/compiler/optimizer/Alias.c @@ -1050,9 +1050,15 @@ static Alias* operand_alias(PCode* pcode, int index, UInt32* myvec) { Alias* result; int count; PCodeArg* op; +#if VERSION == VERSION_GC_2_5A + Boolean skipped; +#endif result = NULL; count = 0; +#if VERSION == VERSION_GC_2_5A + skipped = FALSE; +#endif op = &pcode->args[index]; for (list = reg_Defs[op->arg][op->data.reg.reg]; list; list = list->next) { defpcode = Defs[list->id].pcode; @@ -1060,6 +1066,12 @@ static Alias* operand_alias(PCode* pcode, int index, UInt32* myvec) { defpcode, list->id, op->data.reg.reg, pcode, myvec, RegClass_GPR )) { alias = address_def_alias(defpcode); +#if VERSION == VERSION_GC_2_5A + if (!alias && (pcode->flags & fIsWrite)) { + skipped = TRUE; + continue; + } +#endif count++; if (alias) { if (count == 1) { @@ -1075,6 +1087,15 @@ static Alias* operand_alias(PCode* pcode, int index, UInt32* myvec) { } } +#if VERSION == VERSION_GC_2_5A + if (skipped && + result && + !((result->type == AliasType_Object || result->type == AliasType_ObjectPart) && + result->object->datatype != DLOCAL)) { + add_alias_member(worst_case_ptr, result); + result = worst_case_ptr; + } +#endif return result; } diff --git a/src/driver/CmdLineBuildDate.c b/src/driver/CmdLineBuildDate.c index d320e37..4bcf563 100644 --- a/src/driver/CmdLineBuildDate.c +++ b/src/driver/CmdLineBuildDate.c @@ -4,7 +4,7 @@ #if VERSION <= VERSION_GC_2_0P2 const char* CMDLINE_BUILD_TIME = "15:14:48"; const char* CMDLINE_BUILD_DATE = "Sep 16 2002"; -#elif VERSION == VERSION_GC_2_5 +#elif VERSION <= VERSION_GC_2_5A const char* CMDLINE_BUILD_TIME = "14:21:02"; const char* CMDLINE_BUILD_DATE = "Feb 20 2003"; #elif VERSION == VERSION_GC_2_6