mirror of https://github.com/zeldaret/tp.git
Use compiler 1.2.5n instead of frank (#372)
This commit is contained in:
parent
0b12611fea
commit
ae83c59703
4
Makefile
4
Makefile
|
@ -71,8 +71,7 @@ AS := $(DEVKITPPC)/bin/powerpc-eabi-as
|
|||
OBJCOPY := $(DEVKITPPC)/bin/powerpc-eabi-objcopy
|
||||
STRIP := $(DEVKITPPC)/bin/powerpc-eabi-strip
|
||||
CC := $(WINE) tools/mwcc_compiler/$(MWCC_VERSION)/mwcceppc_modded.exe
|
||||
DOLPHIN_LIB_CC := $(WINE) tools/mwcc_compiler/1.2.5/mwcceppc.exe
|
||||
FRANK_CC := $(WINE) tools/mwcc_compiler/1.2.5e/mwcceppc.exe
|
||||
DOLPHIN_LIB_CC := $(WINE) tools/mwcc_compiler/1.2.5n/mwcceppc.exe
|
||||
LD := $(WINE_LD) tools/mwcc_compiler/$(MWCC_VERSION)/mwldeppc.exe
|
||||
ELF2DOL := $(BUILD_PATH)/elf2dol
|
||||
YAZ0 := $(BUILD_PATH)/yaz0.so
|
||||
|
@ -80,7 +79,6 @@ PYTHON := python3
|
|||
ICONV := iconv
|
||||
DOXYGEN := doxygen
|
||||
MAKEREL := tools/makerel.py
|
||||
FRANK := tools/frank.py
|
||||
IMAGENAME := gz2e01.iso
|
||||
|
||||
# Options
|
||||
|
|
|
@ -36,11 +36,8 @@ $(BUILD_DIR)/libs/dolphin/ai/%.o: libs/dolphin/ai/%.c $(BUILD_DIR)/libs/dolphin/
|
|||
@mkdir -p $(@D)
|
||||
@echo building... $<
|
||||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(FRANK_CC) $(LIBAI_A_CFLAGS) -c -o $(@:.o=.po) $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBAI_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $(@:.o=.po) $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBAI_A_D_FILES := $(LIBAI_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -37,11 +37,8 @@ $(BUILD_DIR)/libs/dolphin/ar/%.o: libs/dolphin/ar/%.c $(BUILD_DIR)/libs/dolphin/
|
|||
@mkdir -p $(@D)
|
||||
@echo building... $<
|
||||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(FRANK_CC) $(LIBAR_A_CFLAGS) -c -o $(@:.o=.po) $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBAR_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $(@:.o=.po) $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBAR_A_D_FILES := $(LIBAR_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -29,8 +29,6 @@ $(BUILD_DIR)/libs/dolphin/base/%.o: libs/dolphin/base/%.c $(BUILD_DIR)/libs/dolp
|
|||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(CFLAGS) $(LIBBASE_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $@ $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBBASE_A_D_FILES := $(LIBBASE_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -64,11 +64,8 @@ $(BUILD_DIR)/libs/dolphin/card/%.o: libs/dolphin/card/%.c $(BUILD_DIR)/libs/dolp
|
|||
@mkdir -p $(@D)
|
||||
@echo building... $<
|
||||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(FRANK_CC) $(LIBCARD_A_CFLAGS) -c -o $(@:.o=.po) $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBCARD_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $(@:.o=.po) $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBCARD_A_D_FILES := $(LIBCARD_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -37,8 +37,6 @@ $(BUILD_DIR)/libs/dolphin/db/%.o: libs/dolphin/db/%.c $(BUILD_DIR)/libs/dolphin/
|
|||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBDB_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $@ $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBDB_A_D_FILES := $(LIBDB_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -41,8 +41,6 @@ $(BUILD_DIR)/libs/dolphin/dsp/%.o: libs/dolphin/dsp/%.c $(BUILD_DIR)/libs/dolphi
|
|||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBDSP_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $@ $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBDSP_A_D_FILES := $(LIBDSP_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -50,11 +50,8 @@ $(BUILD_DIR)/libs/dolphin/dvd/%.o: libs/dolphin/dvd/%.c $(BUILD_DIR)/libs/dolphi
|
|||
@mkdir -p $(@D)
|
||||
@echo building... $<
|
||||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(FRANK_CC) $(LIBDVD_A_CFLAGS) -c -o $(@:.o=.po) $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBDVD_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $(@:.o=.po) $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBDVD_A_D_FILES := $(LIBDVD_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -39,11 +39,8 @@ $(BUILD_DIR)/libs/dolphin/exi/%.o: libs/dolphin/exi/%.c $(BUILD_DIR)/libs/dolphi
|
|||
@mkdir -p $(@D)
|
||||
@echo building... $<
|
||||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(FRANK_CC) $(LIBEXI_A_CFLAGS) -c -o $(@:.o=.po) $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBEXI_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $(@:.o=.po) $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBEXI_A_D_FILES := $(LIBEXI_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -39,8 +39,6 @@ $(BUILD_DIR)/libs/dolphin/gd/%.o: libs/dolphin/gd/%.c $(BUILD_DIR)/libs/dolphin/
|
|||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBGD_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $@ $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBGD_A_D_FILES := $(LIBGD_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -57,8 +57,6 @@ $(BUILD_DIR)/libs/dolphin/gx/%.o: libs/dolphin/gx/%.c $(BUILD_DIR)/libs/dolphin/
|
|||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(CFLAGS) $(LIBGX_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $@ $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBGX_A_D_FILES := $(LIBGX_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -38,8 +38,6 @@ $(BUILD_DIR)/libs/dolphin/mtx/%.o: libs/dolphin/mtx/%.c $(BUILD_DIR)/libs/dolphi
|
|||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(CFLAGS) $(LIBMTX_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $@ $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBMTX_A_D_FILES := $(LIBMTX_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -80,11 +80,8 @@ $(BUILD_DIR)/libs/dolphin/os/%.o: libs/dolphin/os/%.c $(BUILD_DIR)/libs/dolphin/
|
|||
@mkdir -p $(@D)
|
||||
@echo building... $<
|
||||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(FRANK_CC) $(LIBOS_A_CFLAGS) -c -o $(@:.o=.po) $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBOS_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $(@:.o=.po) $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBOS_A_D_FILES := $(LIBOS_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -37,11 +37,8 @@ $(BUILD_DIR)/libs/dolphin/pad/%.o: libs/dolphin/pad/%.c $(BUILD_DIR)/libs/dolphi
|
|||
@mkdir -p $(@D)
|
||||
@echo building... $<
|
||||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(FRANK_CC) $(LIBPAD_A_CFLAGS) -c -o $(@:.o=.po) $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBPAD_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $(@:.o=.po) $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBPAD_A_D_FILES := $(LIBPAD_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -38,11 +38,8 @@ $(BUILD_DIR)/libs/dolphin/si/%.o: libs/dolphin/si/%.c $(BUILD_DIR)/libs/dolphin/
|
|||
@mkdir -p $(@D)
|
||||
@echo building... $<
|
||||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(FRANK_CC) $(LIBSI_A_CFLAGS) -c -o $(@:.o=.po) $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBSI_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $(@:.o=.po) $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBSI_A_D_FILES := $(LIBSI_A_O_FILES:.o=.d)
|
||||
|
|
|
@ -36,11 +36,8 @@ $(BUILD_DIR)/libs/dolphin/vi/%.o: libs/dolphin/vi/%.c $(BUILD_DIR)/libs/dolphin/
|
|||
@mkdir -p $(@D)
|
||||
@echo building... $<
|
||||
@$(ICONV) -f UTF-8 -t CP932 < $< > $(basename $@).c
|
||||
@$(FRANK_CC) $(LIBVI_A_CFLAGS) -c -o $(@:.o=.po) $(basename $@).c
|
||||
@$(DOLPHIN_LIB_CC) $(LIBVI_A_CFLAGS) $(DEPFLAGS) -c -o $(dir $@) $(basename $@).c
|
||||
@if [ -z '$(DISABLE_DEPS)' ]; then tools/transform-dep.py '$(basename $@).d' '$(basename $@).d'; touch -c $@; fi
|
||||
@echo Frank is fixing $@
|
||||
@$(PYTHON) $(FRANK) $@ $(@:.o=.po) $@
|
||||
|
||||
ifndef DISABLE_DEPS
|
||||
LIBVI_A_D_FILES := $(LIBVI_A_O_FILES:.o=.d)
|
||||
|
|
214
tools/frank.py
214
tools/frank.py
|
@ -1,214 +0,0 @@
|
|||
#! /usr/bin/env python3
|
||||
|
||||
# Written by Ethan Roseman (ethteck)
|
||||
# MIT License
|
||||
# Copyright 2021
|
||||
|
||||
# Modified by EpochFlame
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
# Byte sequence that marks code size
|
||||
CODESIZE_MAGIC = b"\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x34"
|
||||
BLR_BYTE_SEQ = b"\x4E\x80\x00\x20"
|
||||
MTLR_BYTE_SEQ = b"\x7C\x08\x03\xA6"
|
||||
PROFILE_EXTRA_BYTES = b"\x48\x00\x00\x01\x60\x00\x00\x00"
|
||||
|
||||
LWZ_BYTE = b"\x80"
|
||||
|
||||
# Byte sequence array for branches to link register
|
||||
BLR_BYTE_SEQ_ARRAY = [BLR_BYTE_SEQ,
|
||||
b"\x4D\x80\x00\x20", b"\x4D\x80\x00\x21", b"\x4C\x81\x00\x20", b"\x4C\x81\x00\x21",
|
||||
b"\x4D\x82\x00\x20", b"\x4D\x82\x00\x21", b"\x4C\x80\x00\x20", b"\x4C\x80\x00\x21",
|
||||
b"\x4D\x81\x00\x20", b"\x4D\x81\x00\x21", b"\x4C\x80\x00\x20", b"\x4C\x80\x00\x21",
|
||||
b"\x4C\x82\x00\x20", b"\x4C\x82\x00\x21", b"\x4C\x81\x00\x20", b"\x4C\x81\x00\x21",
|
||||
b"\x4D\x83\x00\x20", b"\x4D\x83\x00\x21", b"\x4C\x83\x00\x20", b"\x4C\x83\x00\x21",
|
||||
b"\x4D\x83\x00\x20", b"\x4D\x83\x00\x21", b"\x4C\x83\x00\x20", b"\x4C\x83\x00\x21"]
|
||||
|
||||
# Example invocation: ./frank.py vanilla.o profile.o output.o
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("vanilla", help="Path to the vanilla object", type=argparse.FileType('rb'))
|
||||
parser.add_argument("profile", help="Path to the profile object", type=argparse.FileType('rb'))
|
||||
parser.add_argument("target", help="Path to the target object (to write)")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Read contents into bytearrays and close files
|
||||
vanilla_bytes = args.vanilla.read()
|
||||
args.vanilla.close()
|
||||
|
||||
# If the file contains no code, the codesize magic will not be found.
|
||||
# The vanilla object requires no modification.
|
||||
code_size_magic_idx = vanilla_bytes.find(CODESIZE_MAGIC)
|
||||
if code_size_magic_idx == -1:
|
||||
with open(args.target, "wb") as f:
|
||||
f.write(vanilla_bytes)
|
||||
sys.exit(0)
|
||||
|
||||
profile_bytes = args.profile.read()
|
||||
args.profile.close()
|
||||
|
||||
# Peephole rescheduling
|
||||
#
|
||||
# This is the pattern we will detect:
|
||||
# (A) lwz <--. .--> (A) li
|
||||
# (B) li <---\-' bl
|
||||
# \ nop
|
||||
# '---> (B) lwz
|
||||
#
|
||||
# If the profiled schedule swaps the
|
||||
# instructions around the bl/nop, we
|
||||
# instead use the vanilla schedule.
|
||||
#
|
||||
idx = 8
|
||||
shift = 0 # difference between vanilla and profile code, due to bl/nops
|
||||
while idx < len(profile_bytes) - 16:
|
||||
# Find next epilogue
|
||||
epi_pos = profile_bytes.find(PROFILE_EXTRA_BYTES, idx)
|
||||
if epi_pos == -1:
|
||||
break # break while loop when no targets remain
|
||||
if epi_pos % 4 != 0: # check 4-byte alignment
|
||||
idx += 4
|
||||
continue
|
||||
|
||||
v_pos = epi_pos - shift
|
||||
shift += 8
|
||||
|
||||
vanilla_inst_a = vanilla_bytes[v_pos-4:v_pos]
|
||||
vanilla_inst_b = vanilla_bytes[v_pos:v_pos+4]
|
||||
vanilla_inst_c = vanilla_bytes[v_pos+4:v_pos+8]
|
||||
profile_inst_a = profile_bytes[epi_pos-4:epi_pos]
|
||||
profile_inst_b = profile_bytes[epi_pos+8:epi_pos+12]
|
||||
profile_inst_c = profile_bytes[epi_pos+12:epi_pos+16]
|
||||
|
||||
opcode_a = vanilla_inst_a[0] >> 2
|
||||
opcode_b = vanilla_inst_b[0] >> 2
|
||||
opcode_c = vanilla_inst_c[0] >> 2
|
||||
|
||||
LWZ = 0x80 >> 2
|
||||
LFS = 0xC0 >> 2
|
||||
ADDI = 0x38 >> 2
|
||||
LI = ADDI # an LI instruction is just an ADDI with RA=0
|
||||
LMW = 0xB8 >> 2
|
||||
FDIVS = 0xEC >> 2
|
||||
|
||||
if opcode_a in [LWZ, LMW] and vanilla_inst_a[2] == 0x00 and \
|
||||
opcode_b in [LI, LFS, FDIVS] and \
|
||||
vanilla_inst_a == profile_inst_b and \
|
||||
vanilla_inst_b == profile_inst_a and \
|
||||
vanilla_inst_c == profile_inst_c and \
|
||||
opcode_c != ADDI: # <- don't reorder if at the very end of the epilogue
|
||||
|
||||
# Swap instructions (A) and (B)
|
||||
profile_bytes = profile_bytes[:epi_pos-4] \
|
||||
+ vanilla_inst_a \
|
||||
+ PROFILE_EXTRA_BYTES \
|
||||
+ vanilla_inst_b \
|
||||
+ profile_bytes[epi_pos+12:]
|
||||
|
||||
# Similar reordering for lwz/lmw, except both insns follow the bl/nop
|
||||
elif opcode_b == LWZ and \
|
||||
opcode_c == LMW and \
|
||||
vanilla_inst_b == profile_inst_c and \
|
||||
vanilla_inst_c == profile_inst_b:
|
||||
|
||||
profile_bytes = profile_bytes[:epi_pos+8] \
|
||||
+ vanilla_inst_b \
|
||||
+ vanilla_inst_c \
|
||||
+ profile_bytes[epi_pos+16:]
|
||||
|
||||
idx = epi_pos + 8
|
||||
|
||||
# Remove byte sequence
|
||||
stripped_bytes = profile_bytes.replace(PROFILE_EXTRA_BYTES, b"")
|
||||
|
||||
# Find end of code sections in vanilla and stripped bytes
|
||||
code_size_offset = code_size_magic_idx + len(CODESIZE_MAGIC)
|
||||
code_size_bytes = vanilla_bytes[code_size_offset:code_size_offset+4]
|
||||
code_size = int.from_bytes(code_size_bytes, byteorder='big')
|
||||
|
||||
eoc_offset = 0x34 + code_size
|
||||
|
||||
# Break if the eoc is not found
|
||||
assert(eoc_offset != len(vanilla_bytes))
|
||||
|
||||
# Replace 0x34 - eoc in vanilla with bytes from stripped
|
||||
final_bytes = vanilla_bytes[:0x34] + stripped_bytes[0x34:eoc_offset] + vanilla_bytes[eoc_offset:]
|
||||
|
||||
# Fix branches to link register
|
||||
for seq in BLR_BYTE_SEQ_ARRAY:
|
||||
idx = 0
|
||||
|
||||
while idx < len(vanilla_bytes):
|
||||
found_pos = vanilla_bytes.find(seq, idx)
|
||||
if found_pos == -1:
|
||||
break # break while loop when no targets remain
|
||||
if found_pos % 4 != 0: # check 4-byte alignment
|
||||
idx += 4
|
||||
continue
|
||||
final_bytes = final_bytes[:found_pos] + vanilla_bytes[found_pos:found_pos+4] + final_bytes[found_pos+4:]
|
||||
idx = found_pos + len(seq)
|
||||
|
||||
# Reunify mtlr/blr instructions, shifting intermediary instructions up
|
||||
idx = 0
|
||||
|
||||
while idx < len(final_bytes):
|
||||
# Find mtlr position
|
||||
mtlr_found_pos = final_bytes.find(MTLR_BYTE_SEQ, idx)
|
||||
if mtlr_found_pos == -1:
|
||||
break # break while loop when no targets remain
|
||||
if mtlr_found_pos % 4 != 0: # check 4-byte alignment
|
||||
idx += 4
|
||||
continue
|
||||
# Find paired blr position
|
||||
blr_found_pos = final_bytes.find(BLR_BYTE_SEQ, mtlr_found_pos)
|
||||
if blr_found_pos == -1:
|
||||
break # break while loop when no targets remain
|
||||
if blr_found_pos % 4 != 0: # check 4-byte alignment
|
||||
idx += 4
|
||||
continue
|
||||
if mtlr_found_pos + 4 == blr_found_pos:
|
||||
idx += 4
|
||||
continue # continue if mtlr is followed directly by blr
|
||||
|
||||
final_bytes = final_bytes[:mtlr_found_pos] + final_bytes[mtlr_found_pos+4:blr_found_pos] + final_bytes[mtlr_found_pos:mtlr_found_pos+4] + final_bytes[blr_found_pos:]
|
||||
idx = mtlr_found_pos + len(MTLR_BYTE_SEQ)
|
||||
|
||||
# Reorder lmw/lwz/lfd instructions, if needed (@Altafen)
|
||||
# Specifically, if this sequence shows up in the stripped profiler code: "LMW, LWZ, LFD*"
|
||||
# And this sequence shows up in the vanilla code: "LWZ, LFD*, LMW"
|
||||
# (LFD* = any number of LFDs, including zero)
|
||||
# If all bytes match between the two (except for the reordering), then use the vanilla ordering.
|
||||
# This could be written to anchor around the "BL, NOP" instructions in unstripped profiler code,
|
||||
# or to check for the presence of "ADDI, MTLR, BLR" soon after.
|
||||
# This also could be written to decode the operands of each instruction to make sure the reorder is harmless.
|
||||
# Neither of these safeguards are necessary at the moment.
|
||||
LWZ = 32
|
||||
LMW = 46
|
||||
LFD = 50
|
||||
idx = 0
|
||||
while idx+4 < len(final_bytes):
|
||||
if final_bytes[idx] >> 2 == LMW and final_bytes[idx+4] >> 2 == LWZ and vanilla_bytes[idx] >> 2 == LWZ:
|
||||
start_idx = idx
|
||||
lmw_bytes = final_bytes[idx:idx+4]
|
||||
lwz_bytes = final_bytes[idx+4:idx+8]
|
||||
if vanilla_bytes[idx:idx+4] != lwz_bytes:
|
||||
idx += 4
|
||||
continue
|
||||
lfd_bytes = b""
|
||||
idx += 4
|
||||
while vanilla_bytes[idx] >> 2 == LFD:
|
||||
lfd_bytes += vanilla_bytes[idx:idx+4]
|
||||
idx += 4
|
||||
if vanilla_bytes[idx:idx+4] != lmw_bytes:
|
||||
continue
|
||||
if final_bytes[start_idx+8:start_idx+8+len(lfd_bytes)] != lfd_bytes:
|
||||
continue
|
||||
idx += 4
|
||||
final_bytes = final_bytes[:start_idx] + lwz_bytes + lfd_bytes + lmw_bytes + final_bytes[idx:]
|
||||
continue
|
||||
idx += 4
|
||||
|
||||
with open(args.target, "wb") as f:
|
||||
f.write(final_bytes)
|
15
tools/tp.py
15
tools/tp.py
|
@ -139,7 +139,8 @@ def expected_copy(debug: bool, build_path: Path, expected_path: Path):
|
|||
default="native",
|
||||
required=False,
|
||||
)
|
||||
def setup(debug: bool, game_path: Path, tools_path: Path, yaz0_encoder: str):
|
||||
@click.option("--force-download/--no-force-download")
|
||||
def setup(debug: bool, game_path: Path, tools_path: Path, yaz0_encoder: str, force_download: bool):
|
||||
"""Setup project"""
|
||||
|
||||
if debug:
|
||||
|
@ -162,9 +163,11 @@ def setup(debug: bool, game_path: Path, tools_path: Path, yaz0_encoder: str):
|
|||
CONSOLE.print(text)
|
||||
|
||||
compilers = tools_path.joinpath("mwcc_compiler")
|
||||
if force_download:
|
||||
shutil.rmtree(compilers)
|
||||
if not compilers.exists() or not compilers.is_dir():
|
||||
os.mkdir(compilers)
|
||||
r = requests.get('https://cdn.discordapp.com/attachments/727918646525165659/917185027656286218/GC_WII_COMPILERS.zip')
|
||||
r = requests.get('https://cdn.discordapp.com/attachments/727918646525165659/1129759991696457728/GC_WII_COMPILERS.zip')
|
||||
z = zipfile.ZipFile(io.BytesIO(r.content))
|
||||
z.extractall(compilers)
|
||||
gc_path = compilers.joinpath("GC")
|
||||
|
@ -201,11 +204,11 @@ def setup(debug: bool, game_path: Path, tools_path: Path, yaz0_encoder: str):
|
|||
)
|
||||
sys.exit(1)
|
||||
|
||||
c125e = compilers.joinpath("1.2.5e")
|
||||
if not c125e.exists() or not c125e.is_dir():
|
||||
c125n = compilers.joinpath("1.2.5n")
|
||||
if not c125n.exists() or not c125n.is_dir():
|
||||
LOG.error(
|
||||
(
|
||||
f"Unable to find patched MWCC compiler version 1.2.5e: missing directory '{c125e}'\n"
|
||||
f"Unable to find patched MWCC compiler version 1.2.5n: missing directory '{c125n}'\n"
|
||||
f"Check the README for instructions on how to obtain the compilers"
|
||||
)
|
||||
)
|
||||
|
@ -233,7 +236,7 @@ def setup(debug: bool, game_path: Path, tools_path: Path, yaz0_encoder: str):
|
|||
|
||||
copy_lmgr326b(c27)
|
||||
copy_lmgr326b(c125)
|
||||
copy_lmgr326b(c125e)
|
||||
copy_lmgr326b(c125n)
|
||||
|
||||
c27_mwcceppc = c27.joinpath("mwcceppc.exe")
|
||||
if not c27_mwcceppc.exists() or not c27_mwcceppc.is_file():
|
||||
|
|
Loading…
Reference in New Issue