Skip to content

codegen: emit lookupFunction dispatch for external jump-table targets#111

Closed
jlsandri wants to merge 1 commit intoran-j:mainfrom
jlsandri:pr/codegen-external-jump-table-lookup
Closed

codegen: emit lookupFunction dispatch for external jump-table targets#111
jlsandri wants to merge 1 commit intoran-j:mainfrom
jlsandri:pr/codegen-external-jump-table-lookup

Conversation

@jlsandri
Copy link
Copy Markdown

@jlsandri jlsandri commented Apr 6, 2026

Problem

When a [[jump_tables.table]] entry in the TOML config has targets that live outside the enclosing function's boundary, the previous codegen silently filtered them out. Those external targets would then be unreachable from the recompiled jump, which at runtime manifests as a wrong branch destination (or a no-op, depending on the dispatch path).

Fix

  • Teach analysis to distinguish internal jump-table targets (which still use goto-to-label dispatch inside the function body) from external ones.
  • Emit a runtime lookupFunction(0x…) dispatch call for the external targets, matching the fallback path already used for unresolved static J/JAL sites (per the README's "unresolved static J/JAL" behaviour).
  • Add an externalJumpTableTargets field to AnalysisResult so the two categories can be separated during codegen instead of being decided in the emitter.

Scope

  • ps2xRecomp/include/ps2recomp/code_generator.h: +4 / -0
  • ps2xRecomp/src/lib/code_generator.cpp: +46 / -9

Rationale

The recompiler already has a principled fallback for unresolved static calls (runtime->lookupFunction(...)), so routing external jump-table targets through the same mechanism fits the existing model and makes the previously-filtered targets actually reachable at runtime.

When a [[jump_tables.table]] entry has targets outside the function
boundary, emit runtime lookupFunction dispatch calls instead of
filtering them out. Internal targets still use goto labels.

Added externalJumpTableTargets to AnalysisResult to distinguish
internal gotos from external calls during codegen.
@jlsandri jlsandri force-pushed the pr/codegen-external-jump-table-lookup branch from 91708d4 to bf97d21 Compare April 6, 2026 08:56
@jlsandri
Copy link
Copy Markdown
Author

jlsandri commented Apr 6, 2026

Closing as part of a batch cleanup after #107 landed. The runtime ecosystem refactor in #107 substantially reworked the files this PR touched, and I would like to re-audit the underlying fix against the new code structure before putting it back in front of you. If the fix is still needed after that re-audit, I will re-open as a focused PR rebased onto current main. Thanks for your patience.

@jlsandri jlsandri closed this Apr 6, 2026
@jlsandri jlsandri deleted the pr/codegen-external-jump-table-lookup branch April 6, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant