aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 11fe72be9ca6091650e3e6d67ff04584bd173f66
parent 21ff1d8f9213698b5226c1c9768fa9badd10633c
Author: anon <anon@example.com>
Date:   Sun, 18 Oct 2026 09:48:31 +0000

Reduce RTTI and object directory lookup overhead

Diffstat:
Msrc/Makefile.linux | 7+++++++
Msrc/Makefile.wasm | 4++++
Msrc/game/llr.dll/lifted-0008.c | 105+++++++++++++++++++++++++++----------------------------------------------------
Msrc/runtime/guest_rtti.c | 50++++++++++++++++++++++++++++++++------------------
Asrc/tests/dynel_lookup.c | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/tests/rtti.c | 26++++++++++++++++++++++++--
6 files changed, 188 insertions(+), 89 deletions(-)

diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -196,6 +196,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/memory-status ./build/linux/system-time ./build/linux/rtti + ./build/linux/dynel-lookup ./build/linux/module-snapshot ./build/linux/fonts build/linux/font-metrics.bin build/linux/font-pixels.bin ./build/linux/module-filename @@ -314,6 +315,12 @@ build/linux/rtti: tests/rtti.c runtime/guest_rtti.c runtime/guest_memory.c runti test: build/linux/rtti +build/linux/dynel-lookup: tests/dynel_lookup.c game/llr.dll/lifted-0008.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ + +test: build/linux/dynel-lookup + build/linux/module-snapshot: tests/module_snapshot.c runtime/linux_snapshot.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/linux_snapshot.h runtime/guest_memory.h @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -pthread -lm -o $@ diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -147,6 +147,10 @@ build/wasm/animation-initialization.js: tests/animation_initialization.c game/bb @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ +build/wasm/dynel-lookup.js: tests/dynel_lookup.c game/llr.dll/lifted-0008.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ + build/wasm/rtti.js: tests/rtti.c runtime/guest_rtti.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_rtti.h runtime/guest_memory.h @mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ diff --git a/src/game/llr.dll/lifted-0008.c b/src/game/llr.dll/lifted-0008.c @@ -3767,78 +3767,45 @@ L_38017D50: RECOMP_REGS_OUT(); } -void sub_38017D70(void) { - int _fpu_cmp = 0; - uint32_t _cf = 0; - int _df = 1; - uint32_t _flag_k = FK_NONE; - uint32_t _flag_a = 0, _flag_b = 0; - uint32_t _itail_tgt = 0; +void sub_38017D70(void) +{ + uint32_t _cf = 0, _flag_k = FK_NONE, _flag_a = 0, _flag_b = 0; RECOMP_REGS_LOCALS RECOMP_ENTER(0x38017D70u); -L_38017D70: - PUSH32(esp, ebp); /* 0x38017D70: push ebp */ - ebp = esp; /* 0x38017D71: mov ebp, esp */ - PUSH32(esp, ecx); /* 0x38017D73: push ecx */ - PUSH32(esp, esi); /* 0x38017D74: push esi */ - esi = MEM32(0x3806C110); /* 0x38017D75: mov esi, dword ptr [0x3806c110] */ - PUSH32(esp, edi); /* 0x38017D7B: push edi */ - edi = MEM32(ebp + 0x8); /* 0x38017D7C: mov edi, dword ptr [ebp + 8] */ - PUSH32(esp, edi); /* 0x38017D7F: push edi */ - ecx = esi; /* 0x38017D80: mov ecx, esi */ - RECOMP_ICALL(0x38040680u); /* 0x38017D82: call 0x38040680 */ /* not lifted */ - RECOMP_FLAGS_IN(); - /* cmp eax, MEM32(esi + 0x4) */ /* 0x38017D87: cmp eax, dword ptr [esi + 4] */ - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(MEM32(esi + 0x4)); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - _flag_k = FK_CMP; - MEM32(ebp + 0x8) = eax; /* 0x38017D8A: mov dword ptr [ebp + 8], eax */ - if (CMP_EQ(_flag_a, _flag_b)) goto L_38017DA3; /* 0x38017D8D: je 0x38017da3 */ -L_38017D8F: - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(0xCu); - _cf = (uint32_t)((uint32_t)(_flag_a + _flag_b) < (uint32_t)_flag_a); - eax = eax + 0xCu; /* 0x38017D8F: add eax, 0xc */ - _flag_k = FK_ADD; - PUSH32(esp, eax); /* 0x38017D92: push eax */ - ecx = edi; /* 0x38017D93: mov ecx, edi */ - RECOMP_ICALL(0x380179F0u); /* 0x38017D95: call 0x380179f0 */ /* not lifted */ - RECOMP_FLAGS_IN(); - /* test LO8(eax), LO8(eax) */ /* 0x38017D9A: test al, al */ - _flag_a = (uint32_t)(LO8(eax)) << 24; _flag_b = (uint32_t)(LO8(eax)) << 24; - _cf = 0; - _flag_k = FK_NARROW(FK_TEST, 24); - if (TEST_NZ(_flag_a, _flag_b)) goto L_38017DA3; /* 0x38017D9C: jne 0x38017da3 */ -L_38017D9E: - eax = ebp + 0x8; /* 0x38017D9E: lea eax, [ebp + 8] */ - goto L_38017DAC; /* 0x38017DA1: jmp 0x38017dac */ -L_38017DA3: - eax = MEM32(esi + 0x4); /* 0x38017DA3: mov eax, dword ptr [esi + 4] */ - MEM32(ebp + (-0x4)) = eax; /* 0x38017DA6: mov dword ptr [ebp - 4], eax */ - eax = ebp + (-0x4); /* 0x38017DA9: lea eax, [ebp - 4] */ -L_38017DAC: - ecx = MEM32(0x3806C110); /* 0x38017DAC: mov ecx, dword ptr [0x3806c110] */ - eax = MEM32(eax); /* 0x38017DB2: mov eax, dword ptr [eax] */ - /* cmp eax, MEM32(ecx + 0x4) */ /* 0x38017DB4: cmp eax, dword ptr [ecx + 4] */ - _flag_a = (uint32_t)(eax); _flag_b = (uint32_t)(MEM32(ecx + 0x4)); - _cf = (uint32_t)CMP_B(_flag_a, _flag_b); - _flag_k = FK_CMP; - edi = POP32_VAL(esp); /* 0x38017DB7: pop edi */ - esi = POP32_VAL(esp); /* 0x38017DB8: pop esi */ - if (CMP_NE(_flag_a, _flag_b)) goto L_38017DC1; /* 0x38017DB9: jne 0x38017dc1 */ -L_38017DBB: - _flag_a = (uint32_t)(0); _flag_b = (uint32_t)(0); - eax = 0; /* 0x38017DBB: xor eax, eax */ - _cf = 0; - _flag_k = FK_TEST; - esp = ebp; /* 0x38017DBD: mov esp, ebp */ - ebp = POP32_VAL(esp); /* 0x38017DBF: pop ebp */ - RECOMP_FLAGS_OUT(); esp += 4; { RECOMP_REGS_OUT(); return; } /* 0x38017DC0: ret */ -L_38017DC1: - eax = MEM32(eax + 0x14); /* 0x38017DC1: mov eax, dword ptr [eax + 0x14] */ - esp = ebp; /* 0x38017DC4: mov esp, ebp */ - ebp = POP32_VAL(esp); /* 0x38017DC6: pop ebp */ - RECOMP_FLAGS_OUT(); esp += 4; { RECOMP_REGS_OUT(); return; } /* 0x38017DC7: ret */ + /* n3Dynel_t::GetDynel uses a unique-key directory. Compare signed types, + * then unsigned instance IDs, and stop as soon as the identity matches. */ + uint32_t key = MEM32(esp + 4); + int32_t type = MEM32(key); + uint32_t instance = MEM32(key + 4); + ecx = MEM32(0x3806C110); + uint32_t header = MEM32(ecx + 4), nil = MEM32(ecx + 8); + uint32_t node = MEM32(header + 4), lower = header; + while (node != nil) { + int32_t node_type = MEM32(node + 12); + uint32_t node_instance = MEM32(node + 16); + if (node_type < type || (node_type == type && node_instance < instance)) { + node = MEM32(node + 8); + } else { + lower = node; + if (node_type == type && node_instance == instance) break; + node = MEM32(node); + } + RECOMP_BACKEDGE(); + } + + /* Preserve the guest function's live stack, volatile registers and flags. */ + MEM32(esp + 4) = lower; + edx = lower == header ? nil : MEM32(lower + 12); + eax = 0; + if (lower != header && (int32_t)edx == type && MEM32(lower + 16) == instance) { + eax = MEM32(lower + 20); + _flag_k = FK_CMP; _flag_a = lower; _flag_b = header; _cf = lower < header; + } else { + _flag_k = FK_TEST; + } + esp += 4; + RECOMP_FLAGS_OUT(); RECOMP_REGS_OUT(); } diff --git a/src/runtime/guest_rtti.c b/src/runtime/guest_rtti.c @@ -1,5 +1,18 @@ #include "guest_rtti.h" +/* MSVC x86 RTTI records contain guest pointers, never host pointers. */ +struct rtti_locator { + uint32_t signature, offset, construction_displacement, type, hierarchy; +}; +struct rtti_hierarchy { + uint32_t signature, attributes, count, bases; +}; +struct rtti_base { + uint32_t type, descendants, offset; + int32_t vbptr; + uint32_t vbslot, attributes; +}; + static int same_type(uint32_t left, uint32_t right) { return left == right || !strcmp((const char *)ADDR(left + 8), (const char *)ADDR(right + 8)); @@ -12,12 +25,12 @@ static void type_info_equal(void) g_esp += 8; /* type_info::operator== is thiscall. */ } -static uint32_t base_address(uint32_t object, uint32_t descriptor) +static uint32_t base_address(uint32_t object, const struct rtti_base *descriptor) { - uint32_t result = object + MEM32(descriptor + 8); - int32_t vbptr = MEM32(descriptor + 12); + uint32_t result = object + descriptor->offset; + int32_t vbptr = descriptor->vbptr; if (vbptr != -1) - result += vbptr + MEM32(MEM32(object + vbptr) + MEM32(descriptor + 16)); + result += vbptr + MEM32(MEM32(object + vbptr) + descriptor->vbslot); return result; } @@ -31,12 +44,13 @@ static void dynamic_cast(void) if (!input) return; /* The x86 locator precedes the vtable and contains guest absolute pointers. */ - uint32_t locator = MEM32(MEM32(input) - 4); - uint32_t object = input - MEM32(locator + 4); - if (MEM32(locator + 8)) object -= MEM32(input - MEM32(locator + 8)); - uint32_t hierarchy = MEM32(locator + 16); - uint32_t count = MEM32(hierarchy + 8), bases = MEM32(hierarchy + 12); - if (MEM32(locator) || MEM32(hierarchy)) { + const struct rtti_locator *locator = (const void *)ADDR(MEM32(MEM32(input) - 4)); + uint32_t object = input - locator->offset; + if (locator->construction_displacement) object -= MEM32(input - locator->construction_displacement); + const struct rtti_hierarchy *hierarchy = (const void *)ADDR(locator->hierarchy); + uint32_t count = hierarchy->count; + const uint32_t *bases = (const void *)ADDR(hierarchy->bases); + if (locator->signature || hierarchy->signature) { fprintf(stderr, "Unsupported guest RTTI metadata version\n"); abort(); } @@ -47,24 +61,24 @@ static void dynamic_cast(void) uint32_t source_index = count; int down_ambiguous = 0, cross_ambiguous = 0; for (uint32_t i = count; i-- > 0;) { - uint32_t descriptor = MEM32(bases + i * 4); - if (MEM32(descriptor + 20) & ~2u) { + const struct rtti_base *descriptor = (const void *)ADDR(bases[i]); + if (descriptor->attributes & ~2u) { /* An unrelated private base does not invalidate a public cast. */ - if (!(MEM32(descriptor + 20) & ~0x1fu) && - !same_type(MEM32(descriptor), source) && !same_type(MEM32(descriptor), target)) continue; + if (!(descriptor->attributes & ~0x1fu) && + !same_type(descriptor->type, source) && !same_type(descriptor->type, target)) continue; fprintf(stderr, "Non-public guest RTTI bases are not implemented (attributes %x)\n", - MEM32(descriptor + 20)); + descriptor->attributes); abort(); } - if (same_type(MEM32(descriptor), source) && base_address(object, descriptor) == input - vf_delta) + if (same_type(descriptor->type, source) && base_address(object, descriptor) == input - vf_delta) source_index = i; - if (!same_type(MEM32(descriptor), target)) continue; + if (!same_type(descriptor->type, target)) continue; uint32_t address = base_address(object, descriptor); if (crosscast && crosscast != address) cross_ambiguous = 1; crosscast = address; /* In reverse preorder, the nearest matching source is enough to * decide whether this target's subtree contains the source subobject. */ - if (source_index < count && source_index <= i + MEM32(descriptor + 4)) { + if (source_index < count && source_index <= i + descriptor->descendants) { if (downcast && downcast != address) down_ambiguous = 1; downcast = address; } diff --git a/src/tests/dynel_lookup.c b/src/tests/dynel_lookup.c @@ -0,0 +1,85 @@ +#include "recomp_types.h" +#include <assert.h> + +void sub_38017D70(void); +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) { abort(); } + +static uint32_t lookup(uint32_t type, uint32_t instance) +{ + g_esp = 0x5000ff00; + MEM32(g_esp + 4) = 0x50000300; + MEM32(0x50000300) = type; + MEM32(0x50000304) = instance; + g_ebx = 123; g_ebp = 234; g_esi = 345; g_edi = 456; + sub_38017D70(); + assert(g_esp == 0x5000ff04); + assert(g_ebx == 123 && g_ebp == 234 && g_esi == 345 && g_edi == 456); + assert(g_ecx == 0x50000000); + return g_eax; +} + +static void exact_identity_uses_signed_type_and_unsigned_instance(void) +{ + const uint32_t nodes[][6] = { + {0x50000120, 0, 0x50000180, 50000, 0x80000000, 10}, + {0x50000140, 0, 0x50000160, 0xffffffff, 0xffffffff, 20}, + {0x50000200, 0, 0x50000200, 0x80000000, 0, 30}, + {0x50000200, 0, 0x50000200, 50000, 0, 40}, + {0x500001a0, 0, 0x500001c0, 0x7fffffff, 0, 50}, + {0x50000200, 0, 0x50000200, 50000, 0xffffffff, 60}, + {0x50000200, 0, 0x50000200, 0x7fffffff, 0xffffffff, 70}, + }; + for (unsigned i = 0; i < 7; ++i) + memcpy((void *)ADDR(0x50000100 + i * 32), nodes[i], sizeof(nodes[i])); + MEM32(0x50000224) = 0x50000100; + + uint32_t root = lookup(50000, 0x80000000); + uint32_t negative = lookup(0xffffffff, 0xffffffff); + uint32_t minimum = lookup(0x80000000, 0); + uint32_t first_instance = lookup(50000, 0); + uint32_t maximum_type = lookup(0x7fffffff, 0); + uint32_t last_instance = lookup(50000, 0xffffffff); + uint32_t maximum = lookup(0x7fffffff, 0xffffffff); + + assert(root == 10 && negative == 20 && minimum == 30); + assert(first_instance == 40 && maximum_type == 50); + assert(last_instance == 60 && maximum == 70); + assert(lookup(0x80000000, 1) == 0); + assert(lookup(0, 0) == 0); + assert(lookup(50000, 0x7fffffff) == 0); + assert(lookup(50000, 0x80000001) == 0); + assert(lookup(0x7fffffff, 1) == 0); +} + +static void lookup_observes_directory_clear_and_reuse(void) +{ + MEM32(0x50000224) = 0x50000200; + + uint32_t empty = lookup(50000, 0x80000000); + MEM32(0x50000120) = 0x50000200; + MEM32(0x50000128) = 0x50000200; + MEM32(0x5000012c) = 50000; + MEM32(0x50000130) = 0x80000000; + MEM32(0x50000134) = 99; + MEM32(0x50000224) = 0x50000120; + uint32_t replacement = lookup(50000, 0x80000000); + + assert(empty == 0); + assert(replacement == 99); + assert(lookup(0xffffffff, 0xffffffff) == 0); +} + +int main(void) +{ + assert(guest_map("dynel directory", 0x50000000, 0x10000)); + assert(guest_map("dynel directory pointer", 0x38060000, 0x10000)); + MEM32(0x3806C110) = 0x50000000; + MEM32(0x50000004) = 0x50000220; + MEM32(0x50000008) = 0x50000200; + exact_identity_uses_signed_type_and_unsigned_instance(); + lookup_observes_directory_clear_and_reuse(); + puts("Dynel lookup: signed types, unsigned instances, misses, clear/reuse and ABI passed"); + return 0; +} diff --git a/src/tests/rtti.c b/src/tests/rtti.c @@ -214,14 +214,35 @@ static void unrelated_private_base_allows_public_vehicle_casts(void) assert(cast(0x50007000, 0, 10, 4, 0) == 0); } +static void metadata_records_span_guest_pages(void) +{ + for (unsigned record = 0; record < 5; ++record) { + layout(3, 3, 32); + base(0, 0, 2, 0, -1, 0); + base(1, 1, 1, 0, -1, 0); + base(2, 2, 0, 0, 8, 4); + MEM32(0x50007008) = 0x50008000; + MEM32(0x50008004) = 24; + const uint32_t source[] = {0x50005000, 0x50004000, 0x50003000, 0x50002000, 0x50002040}; + const uint32_t size[] = {20, 16, 12, 24, 24}; + const uint32_t pointer[] = {0x50006000, 0x50005010, 0x5000400c, 0x50003000, 0x50003008}; + memcpy((void *)ADDR(0x5001fff8), (void *)ADDR(source[record]), size[record]); + MEM32(pointer[record]) = 0x5001fff8; + + uint32_t result = cast(0x50007020, 0, 2, 1, 0); + + assert(result == 0x50007000); + } +} + int main(void) { #ifdef _WIN32 - assert(VirtualAlloc((void *)0x50000000, 0x10000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == (void *)0x50000000); + assert(VirtualAlloc((void *)0x50000000, 0x30000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == (void *)0x50000000); native_cast = (void *)GetProcAddress(LoadLibraryA("msvcrt.dll"), "__RTDynamicCast"); assert(native_cast); #else - assert(guest_map("RTTI fixture", 0x50000000, 0x10000)); + assert(guest_map("RTTI fixture", 0x50000000, 0x30000)); #endif type(0, ".?AVRDBCATMesh_t@@"); type(1, ".?AVRDBObject_t@@"); @@ -237,6 +258,7 @@ int main(void) shared_virtual_source_matches_both_target_branches(); type_info_equality_uses_names(); unrelated_private_base_allows_public_vehicle_casts(); + metadata_records_span_guest_pages(); puts("RTTI: public multiple/virtual inheritance, unrelated private bases, repeated bases, cross-DLL names, type_info equality, null/failure and ABI passed"); return 0; }