Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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% | |
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions config/GC_2_0p3/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion include/compiler/Alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/backend/Scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/backend/TOC.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/driver/mwcceppc.rc
Original file line number Diff line number Diff line change
@@ -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
Expand Down
46 changes: 40 additions & 6 deletions src/compiler/optimizer/Alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand All @@ -983,6 +984,7 @@ static void gather_instruction_alias(PCode* pcode, UInt32* myvec) {
}
}
}
#endif

aliases_idx++;
}
Expand All @@ -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 {
Expand Down Expand Up @@ -1254,19 +1256,27 @@ 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;

if (pcode->flags & fIsWrite) {
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) &&
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/optimizer/CodeMotion.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/optimizer/IroPropagate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/optimizer/ValueNumbering.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/driver/CmdLineBuildDate.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down