You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.3 KiB

#include <exception>
#include <iostream>
#include <stdexcept>
#include "frontend/AntlrDriver.h"
#include "frontend/SyntaxTreePrinter.h"
#if !COMPILER_PARSE_ONLY
#include "ir/IR.h"
#include "ir/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<SysYParser::CompUnitContext*>(antlr.tree);
if (!comp_unit) {
throw std::runtime_error(FormatError("main", "syntax tree root is not compUnit"));
}
auto sema = RunSema(*comp_unit);
std::unique_ptr<ir::Module> asm_module;
if (opts.emit_asm) {
asm_module = GenerateIR(*comp_unit, sema);
ir::RunIRPassPipeline(*asm_module);
}
if (opts.emit_ir) {
std::unique_ptr<ir::Module> ir_module;
if (opts.emit_asm) {
ir_module = GenerateIR(*comp_unit, sema);
} else {
ir_module = GenerateIR(*comp_unit, sema);
}
ir::RunIRPassPipeline(*ir_module);
if (need_blank_line) {
std::cout << "\n";
}
ir::IRPrinter printer;
printer.Print(*ir_module, std::cout);
need_blank_line = true;
}
if (opts.emit_asm) {
auto machine_module = mir::LowerToMIR(*asm_module);
mir::RunMIRPreRegAllocPassPipeline(*machine_module);
mir::RunRegAlloc(*machine_module);
mir::RunMIRPostRegAllocPassPipeline(*machine_module);
mir::RunFrameLowering(*machine_module);
if (need_blank_line) {
std::cout << "\n";
}
mir::PrintAsm(*machine_module, std::cout);
}
#else
if (opts.emit_ir || opts.emit_asm) {
throw std::runtime_error(
FormatError("main", "IR/asm emission is unavailable in parse-only builds"));
}
#endif
} catch (const std::exception& ex) {
PrintException(std::cerr, ex);
return 1;
}
return 0;
}