aorecomp

Anarchy Online classic in the browser

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


tools/client/share_guest_bodies.py (2634B)


      1 """Share identical lifted bodies while retaining their guest entry points."""
      2 import collections
      3 import pathlib
      4 import re
      5 import sys
      6 
      7 
      8 def share_bodies(source):
      9     functions = list(re.finditer(r'^void sub_([0-9A-F]+)\(void\) \{\n', source, re.M))
     10     if not functions or re.search(r'^\s*#', source[functions[0].end():], re.M):
     11         return source
     12 
     13     # Compare complete bodies, changing only the entry address and entry jump.
     14     groups = collections.defaultdict(list)
     15     for index, function in enumerate(functions):
     16         end = functions[index + 1].start() if index + 1 < len(functions) else len(source)
     17         body = source[function.end():end].rstrip()
     18         if not body.endswith('}') or re.search(r'\b(static|entry_address|__\w+)\b', body):
     19             continue
     20         enter = '    RECOMP_ENTER(0x' + function[1] + 'u);\n'
     21         if body.count(enter) != 1:
     22             continue
     23         before, after = body.split(enter)
     24         jump = re.match(r'\n    goto (L_[0-9A-F]+);\n', after)
     25         target = jump[1] if jump else None
     26         if jump:
     27             after = '\n' + after[jump.end():]
     28         groups[before, after].append((function, end, target))
     29 
     30     # Keep wrappers callable through the original dispatch table and direct calls.
     31     replacements = []
     32     for (before, after), group in groups.items():
     33         if len(group) < 2:
     34             continue
     35         name = 'shared_body_' + group[0][0][1]
     36         helper = '\n'.join('void sub_' + function[1] + '(void);' for function, end, target in group)
     37         helper += '\n\n/* Dispatch equivalent guest entry points through one lifted body. */\n'
     38         helper += 'static __attribute__((noinline)) void ' + name + '(uint32_t entry_address) {\n'
     39         helper += before + '    RECOMP_ENTER(entry_address);\n'
     40         helper += '    switch (entry_address) {\n'
     41         for function, end, target in group:
     42             if target:
     43                 helper += '    case 0x' + function[1] + 'u: goto ' + target + ';\n'
     44         helper += '    default: break;\n    }\n' + after + '\n\n'
     45         for index, (function, end, target) in enumerate(group):
     46             wrapper = 'void sub_' + function[1] + '(void) {\n'
     47             wrapper += '    ' + name + '(0x' + function[1] + 'u);\n}\n\n'
     48             replacements.append((function.start(), end, (helper if index == 0 else '') + wrapper))
     49 
     50     for start, end, replacement in sorted(replacements, reverse=True):
     51         source = source[:start] + replacement + source[end:]
     52     return source
     53 
     54 
     55 if __name__ == '__main__':
     56     source, output = map(pathlib.Path, sys.argv[1:])
     57     output.write_text(share_bodies(source.read_text()))