diff --git a/Makefile b/Makefile index b5169384..810d1451 100644 --- a/Makefile +++ b/Makefile @@ -163,36 +163,19 @@ build/libv86-debug.js: $(CLOSURE) src/*.js lib/*.js src/browser/*.js .PHONY: instruction_tables instruction_tables: $(INSTRUCTION_TABLES) -# Back target file up if it exists; if not, create empty back up file -BACKUP_EXISTING_TABLE_CMD=([ -e $@ ] && cp $@ $@.bak) || touch $@.bak -GEN_DIFF_FROM_BACKUP_CMD=git diff --no-index $@.bak $@ > $@.diff || true - -# All of the following generate a specific instruction table and a diff compared to the existing table build/jit.c: $(JIT_DEPENDENCIES) - $(BACKUP_EXISTING_TABLE_CMD) ./gen/generate_jit.js --output-dir build/ --table jit - $(GEN_DIFF_FROM_BACKUP_CMD) build/jit0f_16.c: $(JIT_DEPENDENCIES) - $(BACKUP_EXISTING_TABLE_CMD) ./gen/generate_jit.js --output-dir build/ --table jit0f_16 - $(GEN_DIFF_FROM_BACKUP_CMD) build/jit0f_32.c: $(JIT_DEPENDENCIES) - $(BACKUP_EXISTING_TABLE_CMD) ./gen/generate_jit.js --output-dir build/ --table jit0f_32 - $(GEN_DIFF_FROM_BACKUP_CMD) build/interpreter.c: $(INTERPRETER_DEPENDENCIES) - $(BACKUP_EXISTING_TABLE_CMD) ./gen/generate_interpreter.js --output-dir build/ --table interpreter - $(GEN_DIFF_FROM_BACKUP_CMD) build/interpreter0f_16.c: $(INTERPRETER_DEPENDENCIES) - $(BACKUP_EXISTING_TABLE_CMD) ./gen/generate_interpreter.js --output-dir build/ --table interpreter0f_16 - $(GEN_DIFF_FROM_BACKUP_CMD) build/interpreter0f_32.c: $(INTERPRETER_DEPENDENCIES) - $(BACKUP_EXISTING_TABLE_CMD) ./gen/generate_interpreter.js --output-dir build/ --table interpreter0f_32 - $(GEN_DIFF_FROM_BACKUP_CMD) build/v86.wasm: src/native/*.c src/native/*.h src/native/codegen/*.c src/native/codegen/*.h src/native/profiler/* src/native/call-indirect.ll $(INSTRUCTION_TABLES) diff --git a/gen/generate_interpreter.js b/gen/generate_interpreter.js index 2e4da9fe..399a8b0a 100755 --- a/gen/generate_interpreter.js +++ b/gen/generate_interpreter.js @@ -5,7 +5,7 @@ const fs = require("fs"); const path = require("path"); const encodings = require("./x86_table"); const c_ast = require("./c_ast"); -const { hex, get_switch_value, get_switch_exist } = require("./util"); +const { hex, get_switch_value, get_switch_exist, finalize_table } = require("./util"); const OUT_DIR = get_switch_value("--output-dir") || path.join(__dirname, "..", "build"); @@ -408,8 +408,9 @@ function gen_table() }; if(to_generate.interpreter) { - fs.writeFileSync( - path.join(OUT_DIR, "interpreter.c"), + finalize_table( + OUT_DIR, + "interpreter", c_ast.print_syntax_tree([table]).join("\n") + "\n" ); } @@ -474,16 +475,18 @@ function gen_table() if(to_generate.interpreter0f_16) { - fs.writeFileSync( - path.join(OUT_DIR, "interpreter0f_16.c"), + finalize_table( + OUT_DIR, + "interpreter0f_16", c_ast.print_syntax_tree([table0f_16]).join("\n") + "\n" ); } if(to_generate.interpreter0f_32) { - fs.writeFileSync( - path.join(OUT_DIR, "interpreter0f_32.c"), + finalize_table( + OUT_DIR, + "interpreter0f_32", c_ast.print_syntax_tree([table0f_32]).join("\n") + "\n" ); } diff --git a/gen/generate_jit.js b/gen/generate_jit.js index 7db879d1..fc8a9da1 100755 --- a/gen/generate_jit.js +++ b/gen/generate_jit.js @@ -5,7 +5,7 @@ const fs = require("fs"); const path = require("path"); const encodings = require("./x86_table"); const c_ast = require("./c_ast"); -const { hex, get_switch_value, get_switch_exist } = require("./util"); +const { hex, get_switch_value, get_switch_exist, finalize_table } = require("./util"); const OUT_DIR = get_switch_value("--output-dir") || path.join(__dirname, "..", "build"); @@ -509,8 +509,9 @@ function gen_table() if(to_generate.jit) { - fs.writeFileSync( - path.join(OUT_DIR, "jit.c"), + finalize_table( + OUT_DIR, + "jit", c_ast.print_syntax_tree([table]).join("\n") + "\n" ); } @@ -575,16 +576,18 @@ function gen_table() if(to_generate.jit0f_16) { - fs.writeFileSync( - path.join(OUT_DIR, "jit0f_16.c"), + finalize_table( + OUT_DIR, + "jit0f_16", c_ast.print_syntax_tree([table0f_16]).join("\n") + "\n" ); } if(to_generate.jit0f_32) { - fs.writeFileSync( - path.join(OUT_DIR, "jit0f_32.c"), + finalize_table( + OUT_DIR, + "jit0f_32", c_ast.print_syntax_tree([table0f_32]).join("\n") + "\n" ); } diff --git a/gen/util.js b/gen/util.js index 43ec5e16..e8cdf892 100644 --- a/gen/util.js +++ b/gen/util.js @@ -2,7 +2,11 @@ const assert = require("assert"); const fs = require("fs"); +const path = require("path"); const process = require("process"); +const child_process = require("child_process"); + +const CYAN_FMT = "\x1b[36m%s\x1b[0m"; function hex(n, pad) { @@ -29,8 +33,41 @@ function get_switch_exist(arg_switch) return process.argv.includes(arg_switch); } +function create_backup_file(src, dest) +{ + try + { + fs.copyFileSync(src, dest); + } + catch(e) + { + if(e.code !== "ENOENT") throw e; + fs.writeFileSync(dest, ""); + } +} + +function create_diff_file(in1, in2, out) +{ + const diff = child_process.spawnSync("git", ["diff", "--no-index", in1, in2]).stdout; + fs.writeFileSync(out, diff); +} + +function finalize_table(out_dir, name, contents) +{ + const file_path = path.join(out_dir, `${name}.c`); + const backup_file_path = path.join(out_dir, `${name}.c.bak`); + const diff_file_path = path.join(out_dir, `${name}.c.diff`); + + create_backup_file(file_path, backup_file_path); + fs.writeFileSync(file_path, contents); + create_diff_file(backup_file_path, file_path, diff_file_path); + + console.log(CYAN_FMT, `[+] Wrote table ${name}. Remember to check ${diff_file_path}`); +} + module.exports = { hex, get_switch_value, get_switch_exist, + finalize_table, };