#include #include #include #include #include #include "frontend/AntlrDriver.h" #include "frontend/SyntaxTreePrinter.h" #if !COMPILER_PARSE_ONLY #include "ir/IR.h" #include "ir/passes/PassManager.h" #include "irgen/IRGen.h" #include "mir/MIR.h" #include "sem/Sema.h" #endif #include "utils/CLI.h" #include "utils/Log.h" int main(int argc, char** argv) { try { auto opts = ParseCLI(argc, argv); if (opts.show_help) { PrintHelp(std::cout); return 0; } auto antlr = ParseFileWithAntlr(opts.input); bool need_blank_line = false; if (opts.emit_parse_tree) { PrintSyntaxTree(antlr.tree, antlr.parser.get(), std::cout); need_blank_line = true; } #if !COMPILER_PARSE_ONLY auto* comp_unit = dynamic_cast(antlr.tree); if (!comp_unit) { throw std::runtime_error( FormatError("main", "语法树根节点不是 compUnit")); } auto sema = RunSema(*comp_unit); auto module = GenerateIR(*comp_unit, sema); // 执行优化(如果启用) if (opts.optimize) { ir::PassManager pass_manager; pass_manager.RunScalarOptimizationPasses(module.get()); } // 汇编输出到文件或标准输出 if (opts.emit_asm) { auto machine_module = mir::LowerModuleToMIR(*module); mir::RunRegAlloc(*machine_module); mir::RunFrameLowering(*machine_module); mir::RunPeephole(*machine_module); std::ostringstream asm_ss; mir::PrintAsm(*machine_module, asm_ss); if (!opts.output.empty()) { // 写入指定文件(-o 参数) std::ofstream ofs(opts.output); if (!ofs) { throw std::runtime_error(FormatError("main", std::string("无法打开输出文件: ") + opts.output)); } ofs << asm_ss.str(); ofs.close(); } else { // 输出到标准输出 if (need_blank_line) { std::cout << "\n"; } std::cout << asm_ss.str(); } } if (opts.emit_ir) { ir::IRPrinter printer; if (need_blank_line) { std::cout << "\n"; } printer.Print(*module, std::cout); need_blank_line = true; } #else if (opts.emit_ir || opts.emit_asm) { throw std::runtime_error( FormatError("main", "当前为 parse-only 构建;IR/汇编输出已禁用")); } #endif } catch (const std::exception& ex) { PrintException(std::cerr, ex); return 1; } return 0; }