commit a8f84bd63de454dfbae58714fc90bdcab3657450
parent 1158386ac41b26e51bb90e3ec1a39e2fadc03aa8
Author: anon <anon@example.com>
Date: Sun, 18 Oct 2026 02:39:55 +0000
Resolve fixed guest calls in portable client builds
Diffstat:
7 files changed, 204 insertions(+), 1 deletion(-)
diff --git a/src/Makefile.linux b/src/Makefile.linux
@@ -8,6 +8,12 @@ VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/so
OBJECTS := $(patsubst %.c,build/linux/objects/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(RUNTIME_SOURCES) $(VENDOR_SOURCES))
.DEFAULT_GOAL := all
+include Makefile.portable
+
+$(patsubst %.c,build/linux/objects/%.o,$(RESOLVED_CALL_SOURCES)): build/linux/objects/%.o: build/portable/%.c
+ @mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c $< -o $@
+
.PHONY: all objects test
all: build/linux/game
@@ -237,6 +243,8 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/terrain-tile
./build/linux/terrain-normal
./build/linux/terrain-occlusion
+ ./build/linux/resolved-calls
+ python3 ../tools/client/test_resolve_guest_calls.py
./build/linux/shadow-contact
./build/linux/network-queue
./build/linux/vertex-upload
@@ -572,3 +580,9 @@ build/linux/terrain-occlusion: tests/terrain_occlusion.c game/bbr.dll/lifted-006
$(CC) $(CPPFLAGS) $(CFLAGS) -DRECOMP_YIELD_EVERY=16 $(filter %.c,$^) -lm -o $@
test: build/linux/terrain-occlusion
+
+build/linux/resolved-calls: build/portable/tests/resolved_calls.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h
+ @mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@
+
+test: build/linux/resolved-calls
diff --git a/src/Makefile.portable b/src/Makefile.portable
@@ -0,0 +1,26 @@
+# Resolve fixed guest targets only in full portable builds. Standalone fixtures
+# and Windows retain dispatch through their own overrides and import bridges.
+RESOLVED_CALL_SOURCES := \
+ game/qqr.dll/lifted-0005.c \
+ game/llr.dll/lifted-0025.c \
+ game/llr.dll/lifted-0023.c \
+ game/llr.dll/lifted-0021.c \
+ game/llr.dll/lifted-0022.c \
+ game/kkr.dll/lifted-0033.c \
+ game/kkr.dll/lifted-0081.c \
+ game/kkr.dll/lifted-0073.c \
+ game/bbr.dll/lifted-0064.c \
+ game/bbr.dll/lifted-0012.c \
+ game/bbr.dll/lifted-0032.c \
+ game/bbr.dll/lifted-0061.c \
+ game/bbr.dll/lifted-0055.c \
+ game/bbr.dll/lifted-0013.c \
+ game/bbr.dll/lifted-0057.c
+
+$(addprefix build/portable/,$(RESOLVED_CALL_SOURCES)): build/portable/%.c: %.c game/dispatch.c ../tools/client/resolve_guest_calls.py
+ @mkdir -p $(@D)
+ python3 ../tools/client/resolve_guest_calls.py $< game/dispatch.c $@
+
+build/portable/tests/resolved_calls.c: tests/resolved_calls.c ../tools/client/resolve_guest_calls.py
+ @mkdir -p $(@D)
+ python3 ../tools/client/resolve_guest_calls.py $< $< $@
diff --git a/src/Makefile.wasm b/src/Makefile.wasm
@@ -10,6 +10,15 @@ VENDOR_SOURCES := $(wildcard vendor/msvcp60/source/*.c) $(wildcard vendor/ace/so
OBJECTS := $(patsubst %.c,build/wasm/objects/%.o,$(GAME_SOURCES) $(DATA_SOURCES) $(VENDOR_SOURCES) $(RUNTIME_SOURCES))
.DEFAULT_GOAL := all
+include Makefile.portable
+
+build/wasm/resolved-calls.js: build/portable/tests/resolved_calls.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h
+ @mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -o $@
+
+$(patsubst %.c,build/wasm/objects/%.o,$(RESOLVED_CALL_SOURCES)): build/wasm/objects/%.o: build/portable/%.c
+ @mkdir -p $(@D)
+ $(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c $< -o $@
build/wasm/terrain-occlusion.js: tests/terrain_occlusion.c game/bbr.dll/lifted-0064.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h
@mkdir -p $(@D)
diff --git a/src/tests/resolved_calls.c b/src/tests/resolved_calls.c
@@ -0,0 +1,79 @@
+#define RECOMP_GENERATED_CODE
+#include "recomp_types.h"
+#include "guest_memory.h"
+#include <assert.h>
+
+void sub_10002000(void);
+const recomp_dispatch_entry_t test_dispatch[] = {{0x10002000u, sub_10002000}};
+static unsigned lookups;
+
+#ifdef RECOMP_TRACE
+void recomp_trace_enter(uint32_t address) { assert(address == 0x10002000); }
+#endif
+
+/* The callee observes the same guest boundary as a dispatched function. */
+void sub_10002000(void)
+{
+ uint32_t _flag_k = FK_NONE, _flag_a = 0, _flag_b = 0, _cf = 0;
+ RECOMP_REGS_LOCALS
+ RECOMP_ENTER(0x10002000);
+ assert(MEM32(esp) == RECOMP_RETADDR);
+ assert(_flag_a == 7 && _flag_b == 9 && _cf == 1);
+ assert(eax == 41 && ecx == 17 && g_st[0] == 3.5);
+ eax = 42;
+ ecx = 18;
+ g_st[0] = 4.5;
+ _flag_a = 11;
+ esp += 4;
+ RECOMP_FLAGS_OUT();
+ RECOMP_REGS_OUT();
+}
+
+recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; }
+recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
+recomp_func_t recomp_lookup(uint32_t address)
+{
+ assert(address == 0x10002000);
+ lookups++;
+ return sub_10002000;
+}
+
+static void check_call(int computed)
+{
+ uint32_t _flag_k = FK_NONE, _flag_a = 7, _flag_b = 9, _cf = 1;
+ RECOMP_REGS_LOCALS
+ esp = 0x5000ff00;
+ eax = 41;
+ ecx = 17;
+ g_st[0] = 3.5;
+ g_cur_func = 0x10001000;
+ g_icall_count = 0;
+ g_icall_trace_idx = 0;
+ lookups = 0;
+
+ if (computed) {
+ uint32_t target = 0x10002000;
+ RECOMP_ICALL(target);
+ } else {
+ RECOMP_ICALL(0x10002000u);
+ }
+
+ assert(esp == 0x5000ff00 && g_cur_func == 0x10001000);
+ assert(eax == 42 && ecx == 18 && g_st[0] == 4.5);
+ assert(g_flag_a == 11 && g_flag_b == 9 && g_flag_cf == 1);
+ assert(g_icall_count == 1 && lookups == (unsigned)computed);
+#ifdef RECOMP_TRACE
+ assert(g_icall_trace_idx == 1);
+ assert(g_icall_trace[0] == 0x10002000 && g_icall_from[0] == 0x10001000);
+#else
+ assert(g_icall_trace_idx == 0);
+#endif
+}
+
+int main(void)
+{
+ assert(guest_map("resolved call test", 0x50000000, 0x10000));
+ check_call(0);
+ check_call(1);
+ puts("resolved calls: guest state, diagnostics and computed dispatch passed");
+}
diff --git a/tools/client/publish_wasm.py b/tools/client/publish_wasm.py
@@ -24,7 +24,8 @@ with (source / 'game.wasm').open('rb') as input_file:
# Record the exact inputs and published bytes, including uncommitted source edits.
inputs = subprocess.check_output(
- ['git', 'ls-files', 'src', 'tools/client/build_wasm_dependencies.py'], cwd=root
+ ['git', 'ls-files', 'src', 'tools/client/build_wasm_dependencies.py',
+ 'tools/client/resolve_guest_calls.py'], cwd=root
).decode().splitlines()
digest = hashlib.sha256()
for name in inputs:
diff --git a/tools/client/resolve_guest_calls.py b/tools/client/resolve_guest_calls.py
@@ -0,0 +1,38 @@
+"""Resolve literal calls in selected full-client portable compiler inputs."""
+import pathlib
+import re
+import sys
+
+
+def resolve_calls(source, dispatch):
+ # Only the immutable game dispatch table establishes a direct target.
+ known = set(re.findall(r'\{0x([0-9A-F]+)u, sub_\1\}', dispatch))
+ calls = re.compile(r'(?m)^([ \t]*)RECOMP_ICALL\(0x([0-9A-F]+)u\);')
+ targets = sorted({match[2] for match in calls.finditer(source)} & known)
+ if not targets:
+ return source
+
+ # Retain diagnostics and the existing guest call boundary implementation.
+ declarations = '\n'.join('void sub_' + address + '(void);' for address in targets)
+ macro = r'''
+#define RECOMP_RESOLVED_CALL(function, address) do { \
+ RECOMP_RECORD_ICALL(address); \
+ g_icall_count++; \
+ RECOMP_CALL(function); \
+} while (0)
+'''
+ source = source.replace('#include "recomp_types.h"',
+ '#include "recomp_types.h"\n' + declarations + '\n' + macro, 1)
+
+ def replace_call(match):
+ if match[2] not in known:
+ return match[0]
+ return (match[1] + 'RECOMP_RESOLVED_CALL(sub_' + match[2] +
+ ', 0x' + match[2] + 'u);')
+
+ return calls.sub(replace_call, source)
+
+
+if __name__ == '__main__':
+ source, dispatch, output = map(pathlib.Path, sys.argv[1:])
+ output.write_text(resolve_calls(source.read_text(), dispatch.read_text()))
diff --git a/tools/client/test_resolve_guest_calls.py b/tools/client/test_resolve_guest_calls.py
@@ -0,0 +1,36 @@
+import unittest
+
+import resolve_guest_calls
+
+
+class ResolveGuestCallsTest(unittest.TestCase):
+ def test_only_known_literal_calls_are_resolved(self):
+ source = '''#include "recomp_types.h"
+ RECOMP_ICALL(0x12345678u);
+ RECOMP_ICALL(0x87654321u);
+ RECOMP_ICALL(eax);
+ RECOMP_ITAIL(0x12345678u);
+'''
+ dispatch = '''void sub_12345678(void);
+void sub_87654321(void);
+ {0x12345678u, sub_12345678},
+'''
+
+ result = resolve_guest_calls.resolve_calls(source, dispatch)
+
+ self.assertIn('void sub_12345678(void);', result)
+ self.assertIn(' RECOMP_RESOLVED_CALL(sub_12345678, 0x12345678u);', result)
+ self.assertIn(' RECOMP_ICALL(0x87654321u);', result)
+ self.assertIn(' RECOMP_ICALL(eax);', result)
+ self.assertIn(' RECOMP_ITAIL(0x12345678u);', result)
+
+ def test_source_without_known_calls_is_unchanged(self):
+ source = ' RECOMP_ICALL(0x12345678u);\n'
+
+ result = resolve_guest_calls.resolve_calls(source, '')
+
+ self.assertEqual(result, source)
+
+
+if __name__ == '__main__':
+ unittest.main()