mirror of
https://github.com/open-goal/jak-project
synced 2026-08-16 05:09:45 -04:00
compiler speed improvement (#1547)
This commit is contained in:
@@ -123,49 +123,37 @@ Val* Compiler::compile_asm_text_file(const goos::Object& form, const goos::Objec
|
||||
Val* Compiler::compile_asm_file(const goos::Object& form, const goos::Object& rest, Env* env) {
|
||||
(void)env;
|
||||
int i = 0;
|
||||
std::string filename;
|
||||
std::string disasm_filename = "";
|
||||
bool load = false;
|
||||
bool color = false;
|
||||
bool write = false;
|
||||
bool no_code = false;
|
||||
bool disassemble = false;
|
||||
bool no_time_prints = false;
|
||||
CompilationOptions options;
|
||||
bool no_throw = false;
|
||||
|
||||
std::vector<std::pair<std::string, double>> timing;
|
||||
Timer total_timer;
|
||||
|
||||
// parse arguments
|
||||
bool last_was_disasm = false;
|
||||
for_each_in_list(rest, [&](const goos::Object& o) {
|
||||
if (last_was_disasm) {
|
||||
last_was_disasm = false;
|
||||
if (o.type == goos::ObjectType::STRING) {
|
||||
disasm_filename = as_string(o);
|
||||
options.disassembly_output_file = as_string(o);
|
||||
i++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (i == 0) {
|
||||
filename = as_string(o);
|
||||
options.filename = as_string(o);
|
||||
} else {
|
||||
auto setting = symbol_string(o);
|
||||
if (setting == ":load") {
|
||||
load = true;
|
||||
options.load = true;
|
||||
} else if (setting == ":color") {
|
||||
color = true;
|
||||
options.color = true;
|
||||
} else if (setting == ":write") {
|
||||
write = true;
|
||||
options.write = true;
|
||||
} else if (setting == ":no-code") {
|
||||
no_code = true;
|
||||
options.no_code = true;
|
||||
} else if (setting == ":no-throw") {
|
||||
no_throw = true;
|
||||
} else if (setting == ":disassemble") {
|
||||
disassemble = true;
|
||||
options.disassemble = true;
|
||||
last_was_disasm = true;
|
||||
} else if (setting == ":no-time-prints") {
|
||||
no_time_prints = true;
|
||||
} else {
|
||||
throw_compiler_error(form, "The option {} was not recognized for asm-file.", setting);
|
||||
}
|
||||
@@ -173,92 +161,8 @@ Val* Compiler::compile_asm_file(const goos::Object& form, const goos::Object& re
|
||||
i++;
|
||||
});
|
||||
|
||||
// READ
|
||||
Timer reader_timer;
|
||||
try {
|
||||
auto code = m_goos.reader.read_from_file({filename});
|
||||
timing.emplace_back("read", reader_timer.getMs());
|
||||
|
||||
Timer compile_timer;
|
||||
std::string obj_file_name = filename;
|
||||
|
||||
// Extract object name from file name.
|
||||
for (int idx = int(filename.size()) - 1; idx-- > 0;) {
|
||||
if (filename.at(idx) == '\\' || filename.at(idx) == '/') {
|
||||
obj_file_name = filename.substr(idx + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
obj_file_name = obj_file_name.substr(0, obj_file_name.find_last_of('.'));
|
||||
|
||||
// COMPILE
|
||||
auto obj_file = compile_object_file(obj_file_name, code, !no_code);
|
||||
timing.emplace_back("compile", compile_timer.getMs());
|
||||
|
||||
if (color) {
|
||||
// register allocation
|
||||
Timer color_timer;
|
||||
color_object_file(obj_file);
|
||||
timing.emplace_back("color", color_timer.getMs());
|
||||
|
||||
// code/object file generation
|
||||
Timer codegen_timer;
|
||||
std::vector<u8> data;
|
||||
std::string disasm;
|
||||
if (disassemble) {
|
||||
codegen_and_disassemble_object_file(obj_file, &data, &disasm);
|
||||
if (disasm_filename == "") {
|
||||
printf("%s\n", disasm.c_str());
|
||||
} else {
|
||||
file_util::write_text_file(disasm_filename, disasm);
|
||||
}
|
||||
} else {
|
||||
data = codegen_object_file(obj_file);
|
||||
}
|
||||
timing.emplace_back("codegen", codegen_timer.getMs());
|
||||
|
||||
// send to target
|
||||
if (load) {
|
||||
if (m_listener.is_connected()) {
|
||||
m_listener.send_code(data, obj_file_name);
|
||||
} else {
|
||||
printf("WARNING - couldn't load because listener isn't connected\n"); // todo log warn
|
||||
}
|
||||
}
|
||||
|
||||
// save file
|
||||
if (write) {
|
||||
auto path = file_util::get_file_path({"out", "obj", obj_file_name + ".o"});
|
||||
file_util::create_dir_if_needed_for_file(path);
|
||||
file_util::write_binary_file(path, (void*)data.data(), data.size());
|
||||
}
|
||||
} else {
|
||||
if (load) {
|
||||
printf("WARNING - couldn't load because coloring is not enabled\n");
|
||||
}
|
||||
|
||||
if (write) {
|
||||
printf("WARNING - couldn't write because coloring is not enabled\n");
|
||||
}
|
||||
|
||||
if (disassemble) {
|
||||
printf("WARNING - couldn't disassemble because coloring is not enabled\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (m_settings.print_timing) {
|
||||
printf("F: %36s ", obj_file_name.c_str());
|
||||
timing.emplace_back("total", total_timer.getMs());
|
||||
for (auto& e : timing) {
|
||||
printf(" %12s %4.0f", e.first.c_str(), e.second);
|
||||
}
|
||||
printf("\n");
|
||||
} else {
|
||||
auto total_time = total_timer.getMs();
|
||||
if (total_time > 10.0 && color && !no_time_prints) {
|
||||
fmt::print("[ASM-FILE] {} took {:.2f} ms\n", obj_file_name, total_time);
|
||||
}
|
||||
}
|
||||
asm_file(options);
|
||||
} catch (std::runtime_error& e) {
|
||||
if (!no_throw) {
|
||||
throw_compiler_error(form, "Error while compiling file: {}", e.what());
|
||||
|
||||
Reference in New Issue
Block a user