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
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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% | |

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.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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions config/GC_2_5a/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion include/compiler/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/backend/CMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/backend/MachineSimulationAltiVec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/driver/mwcceppc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/frontend/CParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
21 changes: 21 additions & 0 deletions src/compiler/optimizer/Alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -1050,16 +1050,28 @@ 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;
if (canidate_reaches_use(
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) {
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/driver/CmdLineBuildDate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down