From 424fc1ed0ee122ebb554c4c794c6903dc8193c21 Mon Sep 17 00:00:00 2001 From: Arthur Axel 'fREW' Schmidt Date: Tue, 10 Nov 2026 07:01:54 -0800 Subject: [PATCH] Add GC_2_0p3: 2.0p2 plus FFCC Japan's alias rules A derived version like GC_2_0p2 and GC_2_5a: GameCube 2.0p2 (BFBB's model) plus four rules measured on the Japanese game code of the Final Fantasy Crystal Chronicles decompilation: - gather_instruction_alias keeps an access's own alias when other reaching definitions of its base register have none; - may_alias_scheduling orders a worst_case store before a load of a stack-passed parameter; - is_pointer_like_load treats const small-data globals like the others; - is_pointer_like_load treats a load of part of a small literal like a load of a whole one. Every other version builds byte-identically. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QmcpERTQ1WaRZ73F12eUis --- README.md | 59 +++++++++++++++++++++++++ config/GC_2_0p3/config.json | 4 ++ include/compiler/Alias.h | 2 +- include/version.h | 7 +-- src/compiler/backend/Scheduler.c | 6 +-- src/compiler/backend/TOC.c | 2 +- src/compiler/driver/mwcceppc.rc | 2 +- src/compiler/optimizer/Alias.c | 46 ++++++++++++++++--- src/compiler/optimizer/CodeMotion.c | 2 +- src/compiler/optimizer/IroPropagate.c | 2 +- src/compiler/optimizer/ValueNumbering.c | 4 +- src/driver/CmdLineBuildDate.c | 2 +- 12 files changed, 118 insertions(+), 20 deletions(-) create mode 100644 config/GC_2_0p3/config.json diff --git a/README.md b/README.md index f6f114e..c393cc6 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,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.0p3 | `GC_2_0p3` | 2.4.7 build 92 | 4019 / 4068 | 2853 / 4071 (70.1%) | 54.3% | GameCube 2.0 patched for FFCC | | GameCube 2.5 | `GC_2_5` | 2.4.7 build 105 | 4026 / 4076 | 2860 / 4079 (70.1%) | 54.3% | | | 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.0p3 is not an official binary either; it is 2.0p2 plus the rules seen when compiling the Japanese game code of the + [*Final Fantasy Crystal Chronicles*](https://github.com/zcanann/FFCC-Decomp) decompilation. See + [below](#final-fantasy-crystal-chronicles-japan-20p3) for more information. ## Getting started @@ -162,6 +166,61 @@ been: - `snow_life` should not be `const`; - `NPAR_TubeSpiralMagic`'s colours should be `static const` locals. +### Final Fantasy Crystal Chronicles Japan (2.0p3) + +The Japanese release of *Final Fantasy Crystal Chronicles* (August 2003) was built with a 2.0 that no release +matches either, and it behaves much like BFBB's: 2.0, 2.5, 2.6 and 2.7 each lose more than 600 of the FFCC +decompilation's 3081 Japanese game functions, while BFBB's 2.0p2 gains 10 and loses 18 against the compiler FFCC has +used so far (`2.0p1k`, a chain of binary patches of 2.0p1). 2.0p3 is 2.0p2 with the rules FFCC's Japanese code needs +on top: + +- the alias analysis keeps an access's own alias when other reaching definitions of its base register have none (2.0 + makes such an access `worst_case`); +- the scheduler orders a `worst_case` store, such as the prologue's register saves, before a load of a parameter + passed on the stack; +- the scheduler orders loads from small data like loads through a pointer even when the global is `const`; +- the scheduler orders loads from part of a small literal (a local aggregate's initialiser) like loads of a whole one. + +The first two are rules FFCC's binary patches had (`ksa` and `pwcp`). Its other three (`e3ns`, `c3a` and `dsl`) are +already covered by 2.0p2's rules for floating-point literals and small-data stores: they never change an answer on +top of 2.0p2. The last two are new. + +An example of the first rule, from `pppSRandCV`: after picking `u8* target = (off == -1) ? gPppDefaultValueBuffer : +work + off`, every byte store through `target` makes 2.0p2 reload the int-to-float bias constant and keeps the loads +of `target[1..3]` below it; the retail game loads them all up front and the bias once: + +``` +2.0p2 2.0p3, retail +stb r0,0(r3) stb r0,0(r3) +lbz r0,9(r30) lbz r0,9(r30) +lfd f3,@bias lfs f1,4(r31) +extsb r0,r0 extsb r0,r0 +lfs f1,4(r31) xoris r0,r0,32768 +xoris r0,r0,32768 +lbz r4,1(r3) +``` + +Functions matching retail, over every FFCC game unit built for Japan (3081 functions; FFCC's sound library, built +with the same compiler in all three regions, gives byte-identical objects with 2.0p1g, 2.0p2 and 2.0p3): + +| Compiler | Japan | vs. 2.0p1k | +| --------------- | -------- | ----------- | +| 2.0 | 2380 | +0 / -622 | +| 2.0p1 | 2766 | +0 / -236 | +| 2.0p1g (BFBB) | 2959 | +0 / -43 | +| 2.0p1k (FFCC) | 3002 | | +| 2.0p2 | 2994 | +10 / -18 | +| **2.0p3** | **3014** | **+12 / -0** | + +Each rule, added to 2.0p2 in turn: +13 / -0, +5 / -0, +1 / -0 and +1 / -0. 265 of the 274 Japanese objects are +byte-identical to 2.0p1k's; the other nine differ only in the functions 2.0p3 matches and 2.0p1k does not, and in +three that come closer. No 2.0 behaviour switched to 2.5's on top of 2.0p3 (each `#if VERSION` in turn) gains a +function. The two new rules each rest on one function (`CMenuPcs::SetWorldParam` and `CChara::CModel::DrawFur`) and +change nothing else. + +Only FFCC has been measured, and BFBB has not been rebuilt with 2.0p3. To use 2.0p3 in FFCC, put +`build/GC_2_0p3/clang/mwcceppc.exe` in place of the project's Japanese game compiler. + ## 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_0p3/config.json b/config/GC_2_0p3/config.json new file mode 100644 index 0000000..390e43c --- /dev/null +++ b/config/GC_2_0p3/config.json @@ -0,0 +1,4 @@ +{ + "derived_from": "GC_2_0", + "description": "FFCC Japan's compiler: 2.0p2 plus the alias rules FFCC's Japanese game code shows; no original, built from the sources alone" +} diff --git a/include/compiler/Alias.h b/include/compiler/Alias.h index 50fef22..ae4f06e 100644 --- a/include/compiler/Alias.h +++ b/include/compiler/Alias.h @@ -54,7 +54,7 @@ extern Alias* make_distinct_virtual_alias(Object* obj); #endif extern void gather_alias_info(void); extern Boolean may_alias(PCode* a, PCode* b); -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 extern Boolean is_direct_small_data_access(PCode* pcode); extern Boolean may_alias_scheduling(PCode* a, PCode* b); extern void update_small_data_values(PCode* pcode); diff --git a/include/version.h b/include/version.h index 45ad0a5..6afc519 100644 --- a/include/version.h +++ b/include/version.h @@ -3,9 +3,10 @@ #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_0P3 2 +#define VERSION_GC_2_5 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/Scheduler.c b/src/compiler/backend/Scheduler.c index 524e350..8c8dc39 100644 --- a/src/compiler/backend/Scheduler.c +++ b/src/compiler/backend/Scheduler.c @@ -246,7 +246,7 @@ static void serialize_load(DGNode* node) { DGNodeList* list; for (list = memory_defs; list; list = list->next) { -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 if (may_alias_scheduling(node->instr, list->node->instr)) { #else if (may_alias(node->instr, list->node->instr)) { @@ -262,7 +262,7 @@ static void serialize_store(DGNode* node) { DGNodeList* list; for (list = memory_uses; list; list = list->next) { -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 if (may_alias_scheduling(node->instr, list->node->instr)) { #else if (may_alias(node->instr, list->node->instr)) { @@ -271,7 +271,7 @@ static void serialize_store(DGNode* node) { } } for (list = memory_defs; list; list = list->next) { -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 if (may_alias_scheduling(node->instr, list->node->instr)) { #else if (may_alias(node->instr, list->node->instr)) { diff --git a/src/compiler/backend/TOC.c b/src/compiler/backend/TOC.c index f5cf210..7bfbc13 100644 --- a/src/compiler/backend/TOC.c +++ b/src/compiler/backend/TOC.c @@ -285,7 +285,7 @@ Object* createfloatconstant(Type* type, Float* data) { obj->u.data.info = NULL; obj->u.data.linkname = obj->name; obj->sclass = TK_STATIC; -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 obj->qual = Q_INLINE_DATA; #else obj->qual = Q_CONST | Q_INLINE_DATA; diff --git a/src/compiler/driver/mwcceppc.rc b/src/compiler/driver/mwcceppc.rc index 32a8374..48f3fb7 100644 --- a/src/compiler/driver/mwcceppc.rc +++ b/src/compiler/driver/mwcceppc.rc @@ -1,6 +1,6 @@ #include "version.h" -#if VERSION <= VERSION_GC_2_0P2 +#if VERSION <= VERSION_GC_2_0P3 #define BUILD 92 #define BUILD_STRING L"2.4.7.92\0" #elif VERSION == VERSION_GC_2_5 diff --git a/src/compiler/optimizer/Alias.c b/src/compiler/optimizer/Alias.c index c47f375..bcf2c8a 100644 --- a/src/compiler/optimizer/Alias.c +++ b/src/compiler/optimizer/Alias.c @@ -77,7 +77,7 @@ Alias* make_distinct_virtual_alias(Object* obj, SInt32 offset, SInt32 size) { object->name = CParser_GetUniqueName(); object->flags = obj->flags; object->qual = obj->qual; -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 return make_alias(object, 0x7FFFFF, 1); #else alias = make_alias_set(); @@ -367,7 +367,7 @@ static inline Boolean aliases_overlap(Alias* a, Alias* b) { ); } -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 Boolean is_direct_small_data_access(PCode* pcode) { Alias* alias; @@ -936,7 +936,7 @@ static void gather_instruction_alias(PCode* pcode, UInt32* myvec) { RegUseOrDef* list; PCode* defpcode; -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 if ((pcode->flags & fIsWrite) && is_direct_small_data_access(pcode) && !is_safe_const(pcode->alias->object)) { @@ -972,6 +972,7 @@ static void gather_instruction_alias(PCode* pcode, UInt32* myvec) { } alias_array[aliases_idx] = pcode->alias; +#if VERSION != VERSION_GC_2_0P3 for (list = reg_Defs[op->arg][op->data.reg.reg]; list; list = list->next) { if (bitvectorgetbit(list->id, myvec)) { defpcode = Defs[list->id].pcode; @@ -983,6 +984,7 @@ static void gather_instruction_alias(PCode* pcode, UInt32* myvec) { } } } +#endif aliases_idx++; } @@ -998,7 +1000,7 @@ static void gather_instruction_alias(PCode* pcode, UInt32* myvec) { CError_ASSERT(1250, alias_array[1] == pcode->alias); } } else if (alias_array[1] != worst_case_ptr) { -#if VERSION != VERSION_GC_2_0P2 +#if VERSION != VERSION_GC_2_0P2 && VERSION != VERSION_GC_2_0P3 alias_choice = worst_case_ptr; #endif } else { @@ -1254,7 +1256,7 @@ Boolean may_alias(PCode* a, PCode* b) { return may_alias_alias(a->alias, b->alias); } -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 static inline Boolean is_pointer_like_load(PCode* pcode) { Alias* alias; @@ -1262,11 +1264,19 @@ static inline Boolean is_pointer_like_load(PCode* pcode) { return FALSE; } if (is_direct_small_data_access(pcode)) { +#if VERSION == VERSION_GC_2_0P3 + return TRUE; +#else return !is_safe_const(pcode->alias->object); +#endif } alias = pcode->alias; return alias && +#if VERSION == VERSION_GC_2_0P3 + alias->type != AliasType_Set && +#else alias->type == AliasType_Object && +#endif alias->object->datatype != DLOCAL && alias->object->name->name[0] == '@' && !IS_TYPE_FLOAT(alias->object->type) && @@ -1295,11 +1305,35 @@ static inline Boolean is_direct_small_data_store(PCode* pcode) { (int)pcode->args[2].arg == RefType_Addr16; } +#if VERSION == VERSION_GC_2_0P3 +static inline Boolean is_stack_parameter_load(PCode* pcode) { + Alias* alias; + + alias = pcode->alias; + return !(pcode->flags & fIsWrite) && + alias && + alias->type != AliasType_Set && + alias->object->datatype == DLOCAL && + alias->object->u.var.info && + (alias->object->u.var.info->flags & VarInfoFlag_ParamArea); +} + +static inline Boolean is_worst_case_store(PCode* pcode) { + return (pcode->flags & fIsWrite) && pcode->alias == worst_case_ptr; +} +#endif + Boolean may_alias_scheduling(PCode* a, PCode* b) { if ((is_small_data_load(a) && is_direct_small_data_store(b)) || (is_small_data_load(b) && is_direct_small_data_store(a))) { return TRUE; } +#if VERSION == VERSION_GC_2_0P3 + if ((is_stack_parameter_load(a) && is_worst_case_store(b)) || + (is_stack_parameter_load(b) && is_worst_case_store(a))) { + return TRUE; + } +#endif return may_alias_alias( is_pointer_like_load(a) ? worst_case_ptr : a->alias, is_pointer_like_load(b) ? worst_case_ptr : b->alias @@ -1424,7 +1458,7 @@ void update_all_alias_values(void) { } } -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 void update_small_data_values(PCode* pcode) { Alias* alias; diff --git a/src/compiler/optimizer/CodeMotion.c b/src/compiler/optimizer/CodeMotion.c index b82737c..ee02b0b 100644 --- a/src/compiler/optimizer/CodeMotion.c +++ b/src/compiler/optimizer/CodeMotion.c @@ -985,7 +985,7 @@ static void moveinvariantsfromloop(Loop* loop) { nextInstr = instr->nextPCode; if (!(instr->flags & fIsBranch) && instr->argCount) { if (!(instr->flags & (fIsCall | fIsPtrOp | fIsVolatile | fSideEffects)) && -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 !((instr->flags & fIsRead) && is_direct_small_data_access(instr)) && #endif isloopinvariant(instr, loop, myvec, FALSE, FALSE) && diff --git a/src/compiler/optimizer/IroPropagate.c b/src/compiler/optimizer/IroPropagate.c index 05530a0..94e3e0b 100644 --- a/src/compiler/optimizer/IroPropagate.c +++ b/src/compiler/optimizer/IroPropagate.c @@ -207,7 +207,7 @@ static Boolean IsPropagatable(IROLinear* linear) { !is_volatile_object(srcObj) && IRO_TypesEqual(obj->type, linear->rtype)) { if (!Inline_IsObjectData(linear->u.diadic.right->u.monadic->u.node->data.objref)) { -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 if (obj->datatype == DLOCAL && obj->u.var.info && obj->u.var.info->noregister) { return FALSE; } diff --git a/src/compiler/optimizer/ValueNumbering.c b/src/compiler/optimizer/ValueNumbering.c index c629687..4b4e6b1 100644 --- a/src/compiler/optimizer/ValueNumbering.c +++ b/src/compiler/optimizer/ValueNumbering.c @@ -511,7 +511,7 @@ static void simpleload(PCode* pcode) { static void simplestore(PCode* pcode) { update_alias_value(pcode->alias, pcode); -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 update_small_data_values(pcode); #endif killregisters(pcode); @@ -539,7 +539,7 @@ static void pointerload(PCode* pcode) { static void pointerstore(PCode* pcode) { update_alias_value(pcode->alias, NULL); -#if VERSION == VERSION_GC_2_0P2 +#if VERSION == VERSION_GC_2_0P2 || VERSION == VERSION_GC_2_0P3 update_small_data_values(pcode); #endif killregisters(pcode); diff --git a/src/driver/CmdLineBuildDate.c b/src/driver/CmdLineBuildDate.c index d320e37..1465604 100644 --- a/src/driver/CmdLineBuildDate.c +++ b/src/driver/CmdLineBuildDate.c @@ -1,7 +1,7 @@ #include "driver/CmdLineBuildDate.h" #include "version.h" -#if VERSION <= VERSION_GC_2_0P2 +#if VERSION <= VERSION_GC_2_0P3 const char* CMDLINE_BUILD_TIME = "15:14:48"; const char* CMDLINE_BUILD_DATE = "Sep 16 2002"; #elif VERSION == VERSION_GC_2_5