diff --git a/compiler/src/main.cpp b/compiler/src/main.cpp index 27d5169..05ed636 100644 --- a/compiler/src/main.cpp +++ b/compiler/src/main.cpp @@ -105,10 +105,13 @@ int main(int argc, char** argv) { // 参数收集 std::string machine_path; + std::string out_path; bool disasm_mode = false; for (int i = 2; i + 1 < argc; ++i) { if (std::strcmp(argv[i], "--machine") == 0) { machine_path = argv[i + 1]; + } else if (std::strcmp(argv[i], "-o") == 0) { + out_path = argv[i + 1]; } } for (int i = 1; i < argc; ++i) { @@ -117,38 +120,32 @@ int main(int argc, char** argv) { } } - // 机器定义(编译与反汇编都需要;加载/校验失败报错退出) - if (machine_path.empty()) { - std::fprintf(stderr, "error: missing --machine \n"); - usage(); - return 1; - } - compiler::MachineConfig cfg; - std::string err; - if (!cfg.load(machine_path, &err)) { - std::fprintf(stderr, "error: %s\n", err.c_str()); - return 1; - } - + // --disasm:需要机器定义(反汇编按 machine.toml 的 format 输出) if (disasm_mode) { + if (machine_path.empty()) { + std::fprintf(stderr, "error: missing --machine \n"); + usage(); + return 1; + } + compiler::MachineConfig cfg; + std::string err; + if (!cfg.load(machine_path, &err)) { + std::fprintf(stderr, "error: %s\n", err.c_str()); + return 1; + } return dump_image(argv[1], cfg); } const std::string toml_path = argv[1]; - std::string out_path; - for (int i = 2; i + 1 < argc; ++i) { - if (std::strcmp(argv[i], "-o") == 0) { - out_path = argv[i + 1]; - } - } - compiler::Project proj; + std::string err; if (!compiler::parse_project(toml_path, &proj, &err)) { std::fprintf(stderr, "error: %s\n", err.c_str()); return 1; } const std::vector files = compiler::compile_files(proj); + // 打印模式(无 -o):不需要机器定义 if (out_path.empty()) { uint64_t hash = 0; if (!compiler::compute_project_hash(proj, &hash, &err)) { @@ -164,6 +161,18 @@ int main(int argc, char** argv) { return 0; } + // 编译路径:机器定义必填(编码/类型/FB 布局来自 machine.toml) + if (machine_path.empty()) { + std::fprintf(stderr, "error: missing --machine \n"); + usage(); + return 1; + } + compiler::MachineConfig cfg; + if (!cfg.load(machine_path, &err)) { + std::fprintf(stderr, "error: %s\n", err.c_str()); + return 1; + } + // 完整管线:读 .st → 链接 → 类型检查 → 寄存器码 std::vector units; for (const std::string& f : files) {