Skip to content

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

C++

Entry points the windows call in the program, and no stale unit files

Riccardo Giorato6246b0e9638c
diff --git a/recomp/codegen.cpp b/recomp/codegen.cpp
index 4cd8fe5..aff715d 100644
--- a/recomp/codegen.cpp
+++ b/recomp/codegen.cpp
@@ -7,6 +7,7 @@
#include <iterator>
#include <map>
#include <optional>
+#include <regex>
#include <set>
#include <sstream>
#include <stdexcept>
@@ -791,10 +792,12 @@ void generate(const std::vector<Translation> &translations, const CodegenOptions
header << "#pragma once\n\n#include \"ps2/generated.hpp\"\n\nnamespace gen {\n";
table << "#include \"units.hpp\"\n\nnamespace gen {\n\n";
std::size_t written = 0, main_units = 0, module_count = 0;
+ std::vector<std::string> unit_names;
for (const Translation &t : translations) {
const std::vector<UnitRow> rows = Generator(t, options).run();
written += rows.size();
for (const UnitRow &r : rows) {
+ unit_names.push_back(r.function);
header << "void " << r.function << "(ps2::Cpu &c, std::uint32_t entry, std::uint32_t depth);\n";
header << "extern const std::uint16_t " << r.table << "[];\n";
}
@@ -819,6 +822,15 @@ void generate(const std::vector<Translation> &translations, const CodegenOptions
table << " }\n";
write_if_changed(fs::path(options.output) / "units.hpp", header.str());
write_if_changed(fs::path(options.output) / "table.cpp", table.str());
+ // Units of an earlier translation that this one no longer has (a module
+ // regrouped under a new name, a smaller image) would still be built.
+ std::set<std::string> kept;
+ for (const std::string &name : unit_names) kept.insert(name + ".cpp");
+ static const std::regex unit_file("(m_.*_)?unit_[0-9A-F]{4}\\.cpp");
+ for (const auto &entry : fs::directory_iterator(options.output)) {
+ const std::string file = entry.path().filename().string();
+ if (std::regex_match(file, unit_file) && !kept.contains(file)) fs::remove(entry.path());
+ }
std::printf("%zu units written to %s\n", written, options.output.c_str());
}
diff --git a/recomp/main.cpp b/recomp/main.cpp
index 311b673..76deea6 100644
--- a/recomp/main.cpp
+++ b/recomp/main.cpp
@@ -216,6 +216,7 @@ int main(int argc, char **argv) {
std::map<std::string, std::set<std::uint32_t>> window_offsets;
std::map<std::uint32_t, std::vector<std::pair<std::string, std::vector<std::pair<std::uint32_t, std::uint32_t>>>>> code_signatures;
std::set<std::string> representatives;
+ std::set<std::uint32_t> called_out;
for (const Snr2Module &o : overlays) {
if (!window_seeds.count(o.base)) window_seeds[o.base] = window_entries(image, program, o.base, o.size);
if (const auto it = entries.modules.find(o.name); it != entries.modules.end())
@@ -255,6 +256,21 @@ int main(int argc, char **argv) {
overlay_code += code_size(programs.back());
translations.push_back({&images.back(), &programs.back(), nullptr, "m_" + o.name + "_", false, o.name, o.size,
checks_for_window(o, programs.back()), true, o.base});
+ for (std::uint32_t pc = o.base; pc < o.base + o.size; pc += 4) {
+ if (!programs.back().is_code(pc)) continue;
+ const Insn i = decode(*images.back().word(pc), pc);
+ const Flow f = flow(i.op);
+ const std::uint32_t t = i.jump_target();
+ if ((f == Flow::Jump || f == Flow::Call) && t >= program.base && t < program.end && !program.entries.count(t)) called_out.insert(t);
+ }
+ }
+ // Window code jumps into the program at places the program may reach
+ // only through pointers, if at all: entry points of it all the same.
+ if (!called_out.empty()) {
+ std::vector<std::uint32_t> seeds = entries.main;
+ seeds.insert(seeds.end(), called_out.begin(), called_out.end());
+ program = analyse(image, seeds, dynamic_sites(imports));
+ std::printf("%zu entry points of the program called from code windows\n", called_out.size());
}
if (!overlays.empty()) {
std::size_t called_entries = 0;