Commit Graph

37 Commits

Author SHA1 Message Date
Matthew Stanley
6f9649c7e7 decompressed: per-variant synthetic link identities for pattern fragments
Path 2 of the pattern-fragment dispatch architecture: each variant of
a [[input.decompressed_section_pattern]] now gets a unique link-time
ram_addr from a synthetic vram pool (0xC0000000+, KSEG2/KSEG3 — unused
by N64 software so it can't collide with engine-resident sections like
RSP at 0xA4000000+).

Why: when multiple variants share a single canonical link bucket
(e.g. all stadium_models pattern variants at 0x8FF00000), runtime
fragment-vaddr resolution via gFragments[id] is single-pointer and
ambiguous when more than one variant is host-resident at the same
time. Per-variant synthetic ram_addrs make each variant's RELOC_HI16
/ RELOC_LO16 emit produce a unique 0xCXXXXXXX literal at runtime,
giving variant-internal references unambiguous identity without
depending on caller PC, host stack walks, or data-context tracking.

Implementation:

- add_decompressed_section accepts an override_link_ram_addr param.
  The bytes-encoded `vram` (= canonical link bucket) is passed to
  parse_fragment_relocs and discover_function_bounds (so jump tables
  resolve correctly against the body's encoded references), while
  section.ram_addr is set to the override. The two roles of vram are
  cleanly separated.

- New original_pattern_id field on Section. Populated for synthetic-
  link variants with the original game-side fragment id derived from
  the pattern's canonical bucket (e.g. 0xEF for stadium_models).
  Lets the runtime candidate filter know which game id should
  include this synthetic section as a candidate, eliminating cross-
  pattern hash-collision misregistration.

- main.cpp emit: section_load_table now writes original_pattern_id
  into the SectionTableEntry initializer.

- decompressed.cpp pattern loop: every unique variant now gets
  synthetic ram_addr = 0xC0000000 + variant_idx * 0x100000 (1 MB
  stride, ~286 KB largest observed variant). For Stadium's 279
  unique variants the pool occupies 0xC0000000..0xCDB00000, well
  within the runtime-side 512-bucket capacity.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 21:47:45 -07:00
Matthew Stanley
5f2ae6e4f7 recomp: emit content_hash on pattern-synthesized sections (Shape A, build side)
Adds Section::content_hash, populates it on pattern-synthesized
sections with FNV-1a-64 of the first 0x100 bytes of the decompressed
body, and emits it into recomp_overlays.inl's SectionTableEntry. The
runtime side hashes the same window over the bytes Stadium loads at
fragment_ptr and looks up the matching section by hash.

Build-time and runtime use:
  - SAME hash algorithm: FNV-1a-64
  - SAME window: 0x100 bytes (95% uniqueness across Stadium's 282
    distinct fragment bodies; falls back to first-candidate on the
    residual ~5%)
  - SAME byte source: pre-relocation decompressed bytes (link-time
    form, before Stadium's R_MIPS_32 patches run)

Section table emit gains the .content_hash field; non-pattern sections
get hash=0, runtime-side condition `sec.content_hash != 0` filters
them out of the candidate set.

Pairs with the runtime-side change in
lib/N64ModernRuntime/librecomp/src/overlays.cpp.

Activation in PokemonStadiumRecomp's game.toml is gated on a
follow-up: pattern-synthesized impl bodies currently get a basic
forward-CFG-walked size which produces invalid C for fragments with
internal jump tables (data interpreted as code). Future fix: emit
pattern-section impl bodies as runtime-dispatched stubs instead of
trying to statically recompile each body. Until then, fragment78
stays declared as a single static [[input.decompressed_section]];
the engine's pattern infrastructure is in place, ready to be flipped
on once the impl-body emit is reshaped.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 21:47:44 -07:00
Matthew Stanley
5fe2c734a5 recomp: SectionAbsolute guard + reloc filter + runtime-fragment decl
Three engine fixes uncovered by Stadium fragment dispatch:

1. recompilation.cpp: SectionAbsolute guard in print_func_call_by_address.
   Stadium's .fragmentN sections JAL into SHN_ABS symbols (e.g.
   osGbpakReadWrite); resolve_jal indexed context.sections[] at 65534
   and segfaulted on first dispatch. Skip reloc resolution when
   reloc_section >= context.sections.size().

2. main.cpp (overlay table emit): filter unsupported MIPS reloc types
   before indexing reloc_names[]. Stadium's .rel.fragmentN includes
   R_MIPS_PC16 (type 10) which the recompiler doesn't model; the OOB
   read embedded a NUL byte in the .type field and broke the C compile.

3. main.cpp: bounds-check inversion in the static-funcs scan
   (read section_funcs[size] before checking i < size). Latent bug
   exposed by .fragment1's larger CreateStatic surface.

4. recomp.h: forward-declare recomp_register_runtime_fragment so funcs
   files can call it from inlined hook text generated by
   [[patches.hook]] on Memmap_RelocateFragment.

(NOTE: original local commit de76241 also added a recomp_unhandled_*
forward-decl family; those declarations are dropped from this PR — they
violate the no-stubs principle and depend on a runtime API not yet in
upstream N64ModernRuntime.)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 21:47:09 -07:00
Wiseguy
2b6f05688d
Add mod merger tool (#168) 2025-12-31 22:17:44 -05:00
Wiseguy
afc2ff93a5
Implement mdebug parsing for static symbols in IDO elfs (#155)
Co-authored-by: Tharo <17233964+Thar0@users.noreply.github.com>
2025-09-06 18:44:18 -04:00
Wiseguy
c1a6dc93bf
Implement fixed address and globally loaded sections in mods, respect section index in mod self-section relocs (#150)
Some checks failed
validate / ${{ matrix.os }} (${{ (matrix.os == 'macos-14' || matrix.os == 'blaze/ubuntu-22.04') && 'arm64' || 'x64' }}, ${{ matrix.type }}) (blaze/ubuntu-22.04, Debug) (push) Has been cancelled
validate / ${{ matrix.os }} (${{ (matrix.os == 'macos-14' || matrix.os == 'blaze/ubuntu-22.04') && 'arm64' || 'x64' }}, ${{ matrix.type }}) (blaze/ubuntu-22.04, Release) (push) Has been cancelled
validate / ${{ matrix.os }} (${{ (matrix.os == 'macos-14' || matrix.os == 'blaze/ubuntu-22.04') && 'arm64' || 'x64' }}, ${{ matrix.type }}) (macos-13, Debug) (push) Has been cancelled
validate / ${{ matrix.os }} (${{ (matrix.os == 'macos-14' || matrix.os == 'blaze/ubuntu-22.04') && 'arm64' || 'x64' }}, ${{ matrix.type }}) (macos-13, Release) (push) Has been cancelled
validate / ${{ matrix.os }} (${{ (matrix.os == 'macos-14' || matrix.os == 'blaze/ubuntu-22.04') && 'arm64' || 'x64' }}, ${{ matrix.type }}) (macos-14, Debug) (push) Has been cancelled
validate / ${{ matrix.os }} (${{ (matrix.os == 'macos-14' || matrix.os == 'blaze/ubuntu-22.04') && 'arm64' || 'x64' }}, ${{ matrix.type }}) (macos-14, Release) (push) Has been cancelled
validate / ${{ matrix.os }} (${{ (matrix.os == 'macos-14' || matrix.os == 'blaze/ubuntu-22.04') && 'arm64' || 'x64' }}, ${{ matrix.type }}) (ubuntu-latest, Debug) (push) Has been cancelled
validate / ${{ matrix.os }} (${{ (matrix.os == 'macos-14' || matrix.os == 'blaze/ubuntu-22.04') && 'arm64' || 'x64' }}, ${{ matrix.type }}) (ubuntu-latest, Release) (push) Has been cancelled
validate / ${{ matrix.os }} (${{ (matrix.os == 'macos-14' || matrix.os == 'blaze/ubuntu-22.04') && 'arm64' || 'x64' }}, ${{ matrix.type }}) (windows-latest, Debug) (push) Has been cancelled
validate / ${{ matrix.os }} (${{ (matrix.os == 'macos-14' || matrix.os == 'blaze/ubuntu-22.04') && 'arm64' || 'x64' }}, ${{ matrix.type }}) (windows-latest, Release) (push) Has been cancelled
2025-07-19 03:39:41 -04:00
Wiseguy
3531bc0317
Optional dependencies for mod tool and add dependency name vector in recompiler context (#147) 2025-07-07 01:52:18 -04:00
Wiseguy
8781eb44ac
Add a mechanism to provide original section indices for jump table regeneration (#130) 2025-02-11 22:36:33 -05:00
Wiseguy
2af6f2d161
Implement shim function generation (#128) 2025-01-30 23:48:20 -05:00
Wiseguy
b2d07ecd5a
Renamed mod manifest to mod.json, added display_name, description, short_description fields (#125) 2025-01-26 21:57:00 -05:00
Wiseguy
38df8e3ddc
Mod function hooking (#124)
* Add function hooks to mod symbol format

* Add function sizes to section function tables

* Add support for function hooks in live generator

* Add an option to the context to force function lookup for all non-relocated function calls

* Include relocs in overlay data

* Include R_MIPS_26 relocs in symbol file dumping/parsing

* Add manual patch symbols (syms.ld) to the output overlay file and relocs

* Fix which relocs were being emitted for patch sections

* Fix sign extension issue with mfc1, add TODO for banker's rounding
2025-01-26 21:52:46 -05:00
Ethan Lafrenais
36b5d9ae33
PIC Jump Table Support (#120)
* Support for $gp relative jump table calls
2025-01-16 00:40:50 -05:00
Ethan Lafrenais
53ffee96fd
Add ldl, ldr, sdl, sdr implementations (#119) 2025-01-12 22:43:46 -05:00
LittleCube
49bf144b0d
Add TRACE_RETURN (#117) 2025-01-04 22:10:29 -05:00
Wiseguy
66062a06e9
Implement live recompiler (#114)
This commit implements the "live recompiler", which is another backend for the recompiler that generates platform-specific assembly at runtime. This is still static recompilation as opposed to dynamic recompilation, as it still requires information about the binary to recompile and leverages the same static analysis that the C recompiler uses. However, similarly to dynamic recompilation it's aimed at recompiling binaries at runtime, mainly for modding purposes.

The live recompiler leverages a library called sljit to generate platform-specific code. This library provides an API that's implemented on several platforms, including the main targets of this component: x86_64 and ARM64.

Performance is expected to be slower than the C recompiler, but should still be plenty fast enough for running large amounts of recompiled code without an issue. Considering these ROMs can often be run through an interpreter and still hit their full speed, performance should not be a concern for running native code even if it's less optimal than the C recompiler's codegen.

As mentioned earlier, the main use of the live recompiler will be for loading mods in the N64Recomp runtime. This makes it so that modders don't need to ship platform-specific binaries for their mods, and allows fixing bugs with recompilation down the line without requiring modders to update their binaries.

This PR also includes a utility for testing the live recompiler. It accepts binaries in a custom format which contain the instructions, input data, and target data. Documentation for the test format as well as most of the tests that were used to validate the live recompiler can be found here. The few remaining tests were hacked together binaries that I put together very hastily, so they need to be cleaned up and will probably be uploaded at a later date. The only test in that suite that doesn't currently succeed is the div test, due to unknown behavior when the two operands aren't properly sign extended to 64 bits. This has no bearing on practical usage, since the inputs will always be sign extended as expected.
2024-12-31 16:11:40 -05:00
LittleCube
17438755a1
Implement nrm filename toml input, renaming list, trace mode, and context dumping flag (#111)
* implement nrm filename toml input

* change name of mod toml setting to 'mod_filename'

* add renaming and re mode

* fix --dump-context arg, fix entrypoint detection

* refactor re_mode to function_trace_mode

* adjust trace mode to use a general TRACE_ENTRY() macro

* fix some renaming and trace mode comments, revert no toml entrypoint code, add TODO to broken block

* fix arg2 check and usage string
2024-12-24 02:10:26 -05:00
Wiseguy
cc71b31b09
Modding Support PR 2 (Finished mod tool base feature set and improvements for use in N64ModernRuntime) (#93)
* Remove reference context from parse_mod_symbols argument

* Add support for special dependency names (self and base recomp), fix non-compliant offline mod recompiler output

* Fix export names not being set on functions when parsing mod syms, add missing returns to mod parsing

* Switch offline mod recompilation to use a base global event index instead of per-event global indices

* Add support for creating events in normal recompilation

* Output recomp API version in offline mod recompiler

* Removed dependency version from mod symbols (moved to manifest)

* Added mod manifest generation to mod tool

* Implement mod file creation in Windows

* Fixed some error prints not using stderr

* Implement mod file creation on posix systems

* De-hardcode symbol file path for offline mod recompiler

* Fix duplicate import symbols issue and prevent emitting unused imports
2024-09-09 22:49:57 -04:00
Wiseguy
5b17bf8bb5
Modding Support PR 1 (Instruction tables, modding support, mod symbol format, library conversion) (#89)
* Initial implementation of binary operation table

* Initial implementation of unary operation table

* More binary op types, moved binary expression string generation into separate function

* Added and implemented conditional branch instruction table

* Fixed likely swap on bgezal, fixed extra indent branch close and missing
indent on branch statement

* Add operands for other uses of float registers

* Added CHECK_FR generation to binary operation processing, moved float comparison instructions to binary op table

* Finished moving float arithmetic instructions to operation tables

* Added store instruction operation table

* Created Generator interface, separated operation types and tables and C generation code into new files

* Fix mov.d using the wrong input operand

* Move recompiler core logic into a core library and make the existing CLI consume the core library

* Removed unnecessary config input to recompilation functions

* Moved parts of recomp_port.h into new internal headers in src folder

* Changed recomp port naming to N64Recomp

* Remove some unused code and document which Context fields are actually required for recompilation

* Implement mod symbol parsing

* Restructure mod symbols to make replacements global instead of per-section

* Refactor elf parsing into static Context method for reusability

* Move elf parsing into a separate library

* WIP elf to mod tool, currently working without relocations or API exports/imports

* Make mod tool emit relocs and patch binary for non-relocatable symbol references as needed

* Implemented writing import and exports in the mod tool

* Add dependencies to the mod symbol format, finish exporting and importing of mod symbols

* Add first pass offline mod recompiler (generates C from mods that can be compiled and linked into a dynamic library)

* Add strict mode and ability to generate exports for normal recompilation (for patches)

* Move mod context fields into base context, move import symbols into separate vector, misc cleanup

* Some cleanup by making some Context members private

* Add events (from dependencies and exported) and callbacks to the mod symbol format and add support to them in elf parsing

* Add runtime-driven fields to offline mod recompiler, fix event symbol relocs using the wrong section in the mod tool

* Move file header writing outside of function recompilation

* Allow cross-section relocations, encode exact target section in mod relocations, add way to tag reference symbol relocations

* Add local symbol addresses array to offline mod recompiler output and rename original one to reference section addresses

* Add more comments to the offline mod recompiler's output

* Fix handling of section load addresses to match objcopy behavior, added event parsing to dependency tomls, minor cleanup

* Fixed incorrect size used for finding section segments

* Add missing includes for libstdc++

* Rework callbacks and imports to use the section name for identifying the dependency instead of relying on per-dependency tomls
2024-08-26 23:06:34 -04:00
Wiseguy
f8d439aeee
Add option to output multiple functions per file, defaults to 50 (#88) 2024-08-15 00:17:09 -04:00
Mr-Wiseguy
4161ef68cc Made recompilation header include configurable 2024-08-15 00:00:25 -04:00
Wiseguy
ba4aede49c
Add symbol reference file mechanism for elf recompilation (#82)
* Consolidate context dumping toggle into a single bool, begin work on data symbol context dumping
* Added data symbol context dumping
* Fix mthi/mtlo implementation
* Add option to control unpaired LO16 warnings
2024-07-02 21:42:22 -04:00
Gilles Siberlin
6eb7d5bd3e
Implement hook insertion (#73)
* Implement function hook insertion

* Fix recompiled code indentation

* Add _matherr to renamed_funcs

* Replace after_vram by before_vram

* Emit dummy value if relocatable_sections_ordered is empty
2024-05-31 23:31:50 -04:00
Mr-Wiseguy
e0e52d1fc3
Symbol file toml update (#52)
* Symbol input file mechanism

* Migration to new toml lib

---------

Co-authored-by: dcvz <david@dcvz.io>
2024-05-16 22:33:08 -04:00
David Chavez
706e7c5069
Add Initializers for Structs - Fix issue with Apple Clang (#31)
Fixes #30 also adds CI
2024-05-13 20:55:43 -04:00
Mr-Wiseguy
50d55bd171 Added manual sections input option, fixed bug with multiplications and added mthi/lo instructions 2024-04-20 20:00:29 -04:00
Mr-Wiseguy
be275c198a Added single-file mode and absolute symbol options (for patch recompilation) 2023-11-12 14:50:50 -05:00
Mr-Wiseguy
d249363fe5 Misc upgrades including mips3 float mode support, skip overwriting existing files if they're identical to the current recompiled output 2023-10-29 20:53:17 -04:00
Mr-Wiseguy
302dd091c2 Implement application of single-instruction patches 2023-03-24 20:28:36 -04:00
Mr-Wiseguy
9949813018 Implemented parsing of instruction patches in config file 2023-03-24 19:22:30 -04:00
Mr-Wiseguy
7df3e28c76 Implemented function stubbing 2023-03-24 18:04:21 -04:00
Mr-Wiseguy
fba0085946 Added toml11 and implemented initial config file parsing, replaces command-line arg inputs 2023-03-24 17:11:17 -04:00
Mr-Wiseguy
602be9c2c6 Implemented relocatable overlays (OoT runs) 2023-02-04 00:14:03 -05:00
Mr-Wiseguy
c6de2b6189 WIP overlay support and some libultra function implementations for other games 2023-01-12 23:39:49 -05:00
Mr-Wiseguy
5d9ea96abc Added temp for switch case operand, fixed compilation issues in output 2022-11-15 19:55:48 -05:00
Mr-Wiseguy
2300a4b6c9 Implemented register state tracking to identify jump tables for jr instructions 2022-11-15 19:55:48 -05:00
Mr-Wiseguy
84fd433dcc Implemented jal function lookup 2022-11-15 19:55:48 -05:00
Mr-Wiseguy
8a0f0da0cc Implemented initial set of instructions and ignored functions 2022-11-15 19:55:48 -05:00