Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

fix: dispatch fallthroughs that have no generated label

Sennett Lau0a39d8270a8f
diff --git a/recomp/codegen.cpp b/recomp/codegen.cpp
index aff715d..47079a1 100644
--- a/recomp/codegen.cpp
+++ b/recomp/codegen.cpp
@@ -234,7 +234,7 @@ private:
falls = fl != Flow::Jump && fl != Flow::JumpRegister;
skip = true;
if (falls && (labels_.count(a + 4) || !in_unit(a + 4))) {
- if (in_unit(a + 8)) line("goto " + label(a + 8) + ";");
+ if (in_unit(a + 8) && p_.is_code(a + 8)) line("goto " + label(a + 8) + ";");
else exit_to(a + 8);
falls = false;
}
diff --git a/tests/README.md b/tests/README.md
new file mode 100644
index 0000000..11629b4
--- /dev/null
+++ b/tests/README.md
@@ -0,0 +1,16 @@
+# Recompiler regressions
+
+These tests construct small ELF files from synthetic instructions and need no
+game data. Build the recompiler and run them from the repository root:
+
+```sh
+cmake -S . -B build/tools -DCMAKE_BUILD_TYPE=Release
+cmake --build build/tools --target eerecomp
+python3 tests/analysis_tailcall.py
+python3 tests/elf_bss.py
+python3 tests/codegen_fallthrough.py
+```
+
+Each script accepts `--recompiler /path/to/eerecomp` to check another build.
+The fallthrough test also uses `clang++` or `g++` from `PATH` to check that the
+generated C++ compiles.
diff --git a/tests/codegen_fallthrough.py b/tests/codegen_fallthrough.py
new file mode 100644
index 0000000..a2d3b57
--- /dev/null
+++ b/tests/codegen_fallthrough.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+"""Regression: a separately callable delay slot followed by untranslated bytes.
+
+The compiler must dispatch an untranslated fallthrough instead of emitting a
+goto to a label that does not exist. No game data is used by this fixture.
+"""
+from pathlib import Path
+import argparse
+import shutil
+import struct
+import subprocess
+import tempfile
+
+
+root = Path(__file__).resolve().parents[1]
+parser = argparse.ArgumentParser(description=__doc__)
+parser.add_argument('--recompiler', type=Path, default=root / 'build/tools/eerecomp')
+args = parser.parse_args()
+compiler = shutil.which('clang++') or shutil.which('g++')
+if not compiler:
+ raise SystemExit('C++ compiler required')
+with tempfile.TemporaryDirectory(prefix='ps2-codegen-test-') as temp:
+ work = Path(temp)
+ data = bytearray(0x114)
+ data[:7] = b'\x7fELF\x01\x01\x01'
+ struct.pack_into('<HHIIIIIHHHHHH', data, 16, 2, 8, 1, 0x100000, 52, 0, 0, 52, 32, 1, 40, 0, 0)
+ struct.pack_into('<8I', data, 52, 1, 0x100, 0x100000, 0, 20, 20, 5, 4)
+ # beq zero,zero,target; nop; invalid; invalid; target: break
+ struct.pack_into('<5I', data, 0x100, 0x10000003, 0, 1, 1, 0x0d)
+ (work / 'input.elf').write_bytes(data)
+ (work / 'entries.txt').write_text('100004\n')
+ result = subprocess.run([str(args.recompiler.resolve()), str(work / 'input.elf'), '--entries',
+ str(work / 'entries.txt'), '--out', str(work / 'generated')], check=True, capture_output=True, text=True)
+ assert 'code 3 instructions' in result.stdout, result.stdout
+ subprocess.run([compiler, '-std=c++20', '-fsyntax-only', '-I' + str(root / 'runtime/include'),
+ '-I' + str(work / 'generated'), *map(str, (work / 'generated').glob('*.cpp'))], check=True)
+print('Untranslated fallthrough regression passed')