PS2 games in the browser: static recompilation to WebAssembly with a native runtime
C++
recomp: probes, and only rewrite output that changed
Samir Nuri0665124c0a3a
diff --git a/recomp/codegen.cpp b/recomp/codegen.cppindex f4ac916..8254154 100644--- a/recomp/codegen.cpp+++ b/recomp/codegen.cpp@@ -4,6 +4,7 @@#include <filesystem>#include <format>#include <fstream>+#include <iterator>#include <map>#include <set>#include <sstream>@@ -55,6 +56,15 @@ private:};std::string unit_name(std::uint32_t u) { return std::format("unit_{:04X}", u); }++void write_if_changed(const fs::path &path, const std::string &text) {+ std::ifstream in(path, std::ios::binary);+ if (in) {+ std::string old((std::istreambuf_iterator<char>(in)), {});+ if (old == text) return;+ }+ std::ofstream(path, std::ios::binary) << text;+}std::string table_name(std::uint32_t u) { return std::format("entries_{:04X}", u); }class Generator {@@ -119,7 +129,7 @@ private:bool in_unit(std::uint32_t a) const { return a >= base_ && a < base_ + layout_.size(); }void write_header() {- std::ofstream out(fs::path(options_.output) / "units.hpp");+ std::ostringstream out;out << "#pragma once\n\n#include \"ps2/generated.hpp\"\n\nnamespace gen {\n";for (std::uint32_t u = 0; u < layout_.count(); ++u) {if (!has_code_[u]) continue;@@ -127,10 +137,11 @@ private:out << "extern const std::uint16_t " << table_name(u) << "[];\n";}out << "}\n";+ write_if_changed(fs::path(options_.output) / "units.hpp", out.str());}void write_table() {- std::ofstream out(fs::path(options_.output) / "table.cpp");+ std::ostringstream out;out << "#include \"units.hpp\"\n\nnamespace gen {\n\nconst ps2::UnitInfo kUnits[] = {\n";std::size_t n = 0;for (std::uint32_t u = 0; u < layout_.count(); ++u) {@@ -139,6 +150,7 @@ private:++n;}out << "};\n\n}\n\nvoid register_generated_code() { ps2::register_units(gen::kUnits, " << n << "u); }\n";+ write_if_changed(fs::path(options_.output) / "table.cpp", out.str());}void write_unit(std::uint32_t u) {@@ -187,6 +199,7 @@ private:flush();out_ << label(a) << ":\n";}+ if (options_.probes.count(a)) line(std::format("probe(c, {});", hex(a)));const Insn i = at(a);const Flow fl = flow(i.op);if (fl == Flow::None) {@@ -215,8 +228,7 @@ private:}if (falls) exit_to(end);out_ << "}\n\n}\n";- std::ofstream file(fs::path(options_.output) / std::format("{}.cpp", unit_name(u)));- file << out_.str();+ write_if_changed(fs::path(options_.output) / std::format("{}.cpp", unit_name(u)), out_.str());}void line(const std::string &text) {diff --git a/recomp/codegen.hpp b/recomp/codegen.hppindex ffdffed..865e442 100644--- a/recomp/codegen.hpp+++ b/recomp/codegen.hpp@@ -4,6 +4,7 @@#include "elf.hpp"#include <cstdint>+#include <set>#include <string>namespace recomp {@@ -11,6 +12,7 @@ namespace recomp {struct CodegenOptions {std::string output;std::uint32_t unit_bits = 14;+ std::set<std::uint32_t> probes;};void generate(const Image &image, Program &program, const CodegenOptions &options);diff --git a/recomp/main.cpp b/recomp/main.cppindex 0b44b59..9dac6bb 100644--- a/recomp/main.cpp+++ b/recomp/main.cpp@@ -25,7 +25,7 @@ std::vector<std::uint32_t> read_entries(const std::string &path) {}int usage() {- std::fprintf(stderr, "usage: eerecomp <elf> [--entries file] [--out dir] [--stats]\n");+ std::fprintf(stderr, "usage: eerecomp <elf> [--entries file] [--probes file] [--out dir] [--stats]\n");return 2;}@@ -33,13 +33,14 @@ int usage() {int main(int argc, char **argv) {if (argc < 2) return usage();- std::string elf_path = argv[1], entries_path, out;+ std::string elf_path = argv[1], entries_path, out, probes_path;bool stats = false;for (int i = 2; i < argc; ++i) {const std::string arg = argv[i];if (arg == "--entries" && i + 1 < argc) entries_path = argv[++i];else if (arg == "--stats") stats = true;else if (arg == "--out" && i + 1 < argc) out = argv[++i];+ else if (arg == "--probes" && i + 1 < argc) probes_path = argv[++i];else return usage();}@@ -57,7 +58,13 @@ int main(int argc, char **argv) {code, code * 4 / 1024, program.functions.size(), program.entries.size(), program.rejected.size());if (stats)for (const auto &[op, count] : histogram) std::printf("%10zu %s\n", count, name(op));- if (!out.empty()) generate(image, program, CodegenOptions{out});+ if (!out.empty()) {+ CodegenOptions options;+ options.output = out;+ if (!probes_path.empty())+ for (std::uint32_t a : read_entries(probes_path)) options.probes.insert(a);+ generate(image, program, options);+ }} catch (const std::exception &error) {std::fprintf(stderr, "eerecomp: %s\n", error.what());return 1;diff --git a/runtime/include/ps2/generated.hpp b/runtime/include/ps2/generated.hppindex 3b2c58f..584ba9d 100644--- a/runtime/include/ps2/generated.hpp+++ b/runtime/include/ps2/generated.hpp@@ -32,6 +32,7 @@ void syscall(Cpu &c, std::uint32_t pc);void breakpoint(Cpu &c, std::uint32_t pc);void trap(Cpu &c, std::uint32_t pc);void stop(Cpu &c, std::uint32_t pc, const char *what);+void probe(Cpu &c, std::uint32_t pc);std::uint32_t mfc0(Cpu &c, std::uint32_t reg, std::uint32_t sel);void mtc0(Cpu &c, std::uint32_t reg, std::uint32_t value);diff --git a/runtime/src/dispatch.cpp b/runtime/src/dispatch.cppindex a5b91b2..2123d76 100644--- a/runtime/src/dispatch.cpp+++ b/runtime/src/dispatch.cpp@@ -81,6 +81,11 @@ void stop(Cpu &c, std::uint32_t pc, const char *what) {c.pc = pc | kUnwind;}+void probe(Cpu &c, std::uint32_t pc) {+ std::fprintf(stderr, "[probe] 0x%08X a0 %08X a1 %08X a2 %08X a3 %08X ra %08X v0 %08X\n", pc, c.r[4].w[0], c.r[5].w[0], c.r[6].w[0],+ c.r[7].w[0], c.r[31].w[0], c.r[2].w[0]);+}+void breakpoint(Cpu &c, std::uint32_t pc) {std::fprintf(stderr, "[ee] break at 0x%08X (ra 0x%08X)\n", pc, c.r[31].w[0]);}