tools/client/resolve_guest_calls.py (1376B)
1 """Resolve literal calls in selected full-client portable compiler inputs.""" 2 import pathlib 3 import re 4 import sys 5 6 7 def resolve_calls(source, dispatch): 8 # Only the immutable game dispatch table establishes a direct target. 9 known = set(re.findall(r'\{0x([0-9A-F]+)u, sub_\1\}', dispatch)) 10 calls = re.compile(r'(?m)^([ \t]*)RECOMP_ICALL\(0x([0-9A-F]+)u\);') 11 targets = sorted({match[2] for match in calls.finditer(source)} & known) 12 if not targets: 13 return source 14 15 # Retain diagnostics and the existing guest call boundary implementation. 16 declarations = '\n'.join('void sub_' + address + '(void);' for address in targets) 17 macro = r''' 18 #define RECOMP_RESOLVED_CALL(function, address) do { \ 19 RECOMP_RECORD_ICALL(address); \ 20 g_icall_count++; \ 21 RECOMP_CALL(function); \ 22 } while (0) 23 ''' 24 source = source.replace('#include "recomp_types.h"', 25 '#include "recomp_types.h"\n' + declarations + '\n' + macro, 1) 26 27 def replace_call(match): 28 if match[2] not in known: 29 return match[0] 30 return (match[1] + 'RECOMP_RESOLVED_CALL(sub_' + match[2] + 31 ', 0x' + match[2] + 'u);') 32 33 return calls.sub(replace_call, source) 34 35 36 if __name__ == '__main__': 37 source, dispatch, output = map(pathlib.Path, sys.argv[1:]) 38 output.write_text(resolve_calls(source.read_text(), dispatch.read_text()))