PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
Compiled and JIT VU code leave a branch with a branch in its delay slot to the interpreter
Samir Nuric24341661e28
diff --git a/recomp/vurecomp.cpp b/recomp/vurecomp.cppindex 400aa4e..2a4e957 100644--- a/recomp/vurecomp.cpp+++ b/recomp/vurecomp.cpp@@ -491,6 +491,7 @@ std::string generate(const Program &p, std::string &name) {const std::uint32_t lo = word(p.code, pc), hi = word(p.code, pc + 4);Lower l;if (!(hi & 0x80000000u)) l = lower(lo, pc);+ if (ps2::vu::branch_in_delay_slot(p.code.data(), kMask, pc)) l.supported = false;for (const std::uint32_t latency : {7u, 13u})replace_all(l.code, std::format("fl.end_q(r, qb, {}u)", latency), std::format("fl.end_q(r, qb, {}u)", ps2::vu::q_delay(p.code.data(), kMask, pc, latency)));any_status = any_status || l.reads_status;@@ -652,6 +653,7 @@ int main(int argc, char **argv) {std::fprintf(stderr, "%s: key mismatch, skipped\n", e.path().filename().c_str());continue;}+ if (ps2::vu::branch_in_delay_slot(p.code.data(), kMask, entry)) continue;programs.push_back(std::move(p));}std::set<std::uint64_t> keys;@@ -668,6 +670,7 @@ int main(int argc, char **argv) {}for (std::uint32_t target : targets) {const std::uint64_t key = ps2::vu::program_key(programs[i].code.data(), ps2::vu::reachable(programs[i].code.data(), kMask, target), target);+ if (ps2::vu::branch_in_delay_slot(programs[i].code.data(), kMask, target)) continue;if (keys.insert(key).second) programs.push_back(Program{key, target, programs[i].code});}}diff --git a/runtime/include/ps2/vu_ops.hpp b/runtime/include/ps2/vu_ops.hppindex da92434..8bf7c7c 100644--- a/runtime/include/ps2/vu_ops.hpp+++ b/runtime/include/ps2/vu_ops.hpp@@ -40,6 +40,14 @@ inline bool is_branch(std::uint32_t lo) {const std::uint32_t op = lower_major(lo);return op == 0x20 || op == 0x21 || op == 0x24 || op == 0x25 || (op >= 0x28 && op <= 0x2F && op != 0x2A && op != 0x2B);}+inline bool branch_in_delay_slot(const std::uint8_t *code, std::uint32_t mask, std::uint32_t pc) {+ std::uint32_t lo, hi, next_lo, next_hi;+ std::memcpy(&lo, code + (pc & mask), 4);+ std::memcpy(&hi, code + ((pc + 4u) & mask), 4);+ std::memcpy(&next_lo, code + ((pc + 8u) & mask), 4);+ std::memcpy(&next_hi, code + ((pc + 12u) & mask), 4);+ return !(hi & 0x80000000u) && is_branch(lo) && !(next_hi & 0x80000000u) && is_branch(next_lo);+}inline bool is_jump_register(std::uint32_t lo) { return lower_major(lo) == 0x24 || lower_major(lo) == 0x25; }inline bool is_unconditional(std::uint32_t lo) { return lower_major(lo) == 0x20 || lower_major(lo) == 0x21; }inline bool links(std::uint32_t lo) { return lower_major(lo) == 0x21 || lower_major(lo) == 0x25; }diff --git a/runtime/src/vu_jit.cpp b/runtime/src/vu_jit.cppindex 635db6c..cd5a883 100644--- a/runtime/src/vu_jit.cpp+++ b/runtime/src/vu_jit.cpp@@ -1855,11 +1855,12 @@ void Translator::prologue() {bool Translator::run(std::vector<std::uint8_t> &out) {pcs_ = reachable(code_, kMask, entry_);- if (pcs_.empty()) return false;+ if (pcs_.empty() || branch_in_delay_slot(code_, kMask, entry_)) return false;in_.insert(pcs_.begin(), pcs_.end());for (std::uint32_t pc : pcs_) {LowerInfo l;if (!(word(pc + 4) & 0x80000000u)) l = analyze_lower(word(pc), pc);+ if (branch_in_delay_slot(code_, kMask, pc)) l.supported = false;any_status_ = any_status_ || l.reads_status;any_flags_ = any_flags_ || l.reads_flags;lowers_[pc] = l;