Revome the old clang driver approach for code generation

This commit is contained in:
Sameer Rahmani 2021-11-03 18:27:20 +00:00
parent 69e8782c67
commit 0895a09db0
2 changed files with 119 additions and 163 deletions

View File

@ -45,12 +45,6 @@ target_link_libraries(serenec
LLVMOption LLVMOption
lldDriver lldDriver
lldELF lldELF
clangDriver
clangBasic
clangdSupport
clangFrontend
clangEdit
clangLex
) )
target_include_directories(serenec PRIVATE ${PROJECT_BINARY_DIR}) target_include_directories(serenec PRIVATE ${PROJECT_BINARY_DIR})

View File

@ -212,184 +212,146 @@ int dumpAsObject(Namespace &ns) {
"/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib64/crtn.o"); "/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib64/crtn.o");
lld::elf::link(args, false, llvm::outs(), llvm::errs()); lld::elf::link(args, false, llvm::outs(), llvm::errs());
}
return 0;
};
// llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> opts = int main(int argc, char *argv[]) {
// new clang::DiagnosticOptions; initCompiler();
// clang::DiagnosticsEngine diags( registerSereneCLOptions();
// new clang::DiagnosticIDs, opts,
// new clang::TextDiagnosticPrinter(llvm::errs(), opts.get()));
// clang::driver::Driver d("clang", ctx.targetTriple, diags, cl::ParseCommandLineOptions(argc, argv, banner);
// "Serene compiler");
// std::vector<const char *> args = {"serenec"};
// args.push_back(destObjFilePath.c_str()); auto ctx = makeSereneContext();
// args.push_back("-o"); auto userNS = makeNamespace(*ctx, "user", llvm::None);
// args.push_back(destFile.c_str());
// d.setCheckInputsExist(true); applySereneCLOptions(*ctx);
// std::unique_ptr<clang::driver::Compilation> compilation; // TODO: handle the outputDir by not forcing it. it should be
// compilation.reset(d.BuildCompilation(args)); // default to the current working dir
if (outputDir == "-") {
llvm::errs() << "Error: The build directory is not set. Did you forget to "
"use '-b'?\n";
return 1;
}
// if (!compilation) { switch (emitAction) {
// llvm::errs() << "can't create the compilation!\n";
// return 1;
// }
// llvm::SmallVector<std::pair<int, const clang::driver::Command *>> case Action::RunJIT: {
// failCommand; // TODO: Replace it by a proper jit configuration
ctx->setOperationPhase(CompilationPhase::NoOptimization);
// d.ExecuteCompilation(*compilation, failCommand); break;
// if (failCommand.empty()) {
// llvm::outs() << "Done!\n";
// } else {
// llvm::errs() << "Linking failed!\n";
// failCommand.front().second->Print(llvm::errs(), "\n", false);
// }
// }
return 0;
}; };
int main(int argc, char *argv[]) { // Just print out the raw AST
initCompiler(); case Action::DumpAST: {
registerSereneCLOptions(); ctx->setOperationPhase(CompilationPhase::Parse);
break;
};
cl::ParseCommandLineOptions(argc, argv, banner); case Action::DumpSemantic: {
ctx->setOperationPhase(CompilationPhase::Analysis);
break;
};
auto ctx = makeSereneContext(); case Action::DumpSLIR: {
auto userNS = makeNamespace(*ctx, "user", llvm::None); ctx->setOperationPhase(CompilationPhase::SLIR);
break;
}
applySereneCLOptions(*ctx); case Action::DumpMLIR: {
ctx->setOperationPhase(CompilationPhase::MLIR);
break;
}
// TODO: handle the outputDir by not forcing it. it should be case Action::DumpLIR: {
// default to the current working dir ctx->setOperationPhase(CompilationPhase::LIR);
if (outputDir == "-") { break;
llvm::errs() }
<< "Error: The build directory is not set. Did you forget to "
"use '-b'?\n";
return 1;
}
switch (emitAction) { case Action::DumpIR: {
ctx->setOperationPhase(CompilationPhase::IR);
break;
}
case Action::RunJIT: { case Action::CompileToObject: {
// TODO: Replace it by a proper jit configuration ctx->setOperationPhase(CompilationPhase::NoOptimization);
ctx->setOperationPhase(CompilationPhase::NoOptimization); break;
break; }
};
// Just print out the raw AST case Action::Compile: {
case Action::DumpAST: { ctx->setOperationPhase(CompilationPhase::NoOptimization);
ctx->setOperationPhase(CompilationPhase::Parse); break;
break; }
};
case Action::DumpSemantic: { default: {
ctx->setOperationPhase(CompilationPhase::Analysis); llvm::errs() << "No action specified. TODO: Print out help here\n";
break; return 1;
}; }
}
case Action::DumpSLIR: { auto runLoc = reader::LocationRange::UnknownLocation(inputNS);
ctx->setOperationPhase(CompilationPhase::SLIR); auto maybeNS = ctx->sourceManager.readNamespace(*ctx, inputNS, runLoc);
break;
}
case Action::DumpMLIR: { if (!maybeNS) {
ctx->setOperationPhase(CompilationPhase::MLIR); throwErrors(*ctx, maybeNS.getError());
break; return (int)std::errc::no_such_file_or_directory;
} }
case Action::DumpLIR: { auto ns = maybeNS.getValue();
ctx->setOperationPhase(CompilationPhase::LIR);
break;
}
case Action::DumpIR: { ctx->insertNS(ns);
ctx->setOperationPhase(CompilationPhase::IR);
break;
}
case Action::CompileToObject: {
ctx->setOperationPhase(CompilationPhase::NoOptimization);
break;
}
case Action::Compile: {
ctx->setOperationPhase(CompilationPhase::NoOptimization);
break;
}
default: {
llvm::errs() << "No action specified. TODO: Print out help here\n";
return 1;
}
}
auto runLoc = reader::LocationRange::UnknownLocation(inputNS);
auto maybeNS = ctx->sourceManager.readNamespace(*ctx, inputNS, runLoc);
if (!maybeNS) {
throwErrors(*ctx, maybeNS.getError());
return (int)std::errc::no_such_file_or_directory;
}
auto ns = maybeNS.getValue();
ctx->insertNS(ns);
switch (emitAction) {
case Action::DumpAST:
case Action::DumpSemantic: {
auto ast = ns->getTree();
llvm::outs() << exprs::astToString(&ast) << "\n";
return 0;
}
case Action::DumpSLIR:
case Action::DumpMLIR:
case Action::DumpLIR: {
ns->dump();
break;
};
case Action::DumpIR: {
auto maybeModule = ns->compileToLLVM();
if (!maybeModule) {
llvm::errs() << "Failed to generate the IR.\n";
return 1;
}
maybeModule.getValue()->dump();
break;
};
case Action::RunJIT: {
auto maybeJIT = JIT::make(*ns);
if (!maybeJIT) {
// TODO: panic in here: "Couldn't creat the JIT!"
return -1;
}
auto jit = std::move(maybeJIT.getValue());
if (jit->invoke("main")) {
llvm::errs() << "Faild to invoke the 'main' function.\n";
return 1;
}
llvm::outs() << "Done!";
break;
};
case Action::Compile:
case Action::CompileToObject: {
return dumpAsObject(*ns);
};
default: {
llvm::errs() << "Action is not supported yet!\n";
};
}
switch (emitAction) {
case Action::DumpAST:
case Action::DumpSemantic: {
auto ast = ns->getTree();
llvm::outs() << exprs::astToString(&ast) << "\n";
return 0; return 0;
} }
case Action::DumpSLIR:
case Action::DumpMLIR:
case Action::DumpLIR: {
ns->dump();
break;
};
case Action::DumpIR: {
auto maybeModule = ns->compileToLLVM();
if (!maybeModule) {
llvm::errs() << "Failed to generate the IR.\n";
return 1;
}
maybeModule.getValue()->dump();
break;
};
case Action::RunJIT: {
auto maybeJIT = JIT::make(*ns);
if (!maybeJIT) {
// TODO: panic in here: "Couldn't creat the JIT!"
return -1;
}
auto jit = std::move(maybeJIT.getValue());
if (jit->invoke("main")) {
llvm::errs() << "Faild to invoke the 'main' function.\n";
return 1;
}
llvm::outs() << "Done!";
break;
};
case Action::Compile:
case Action::CompileToObject: {
return dumpAsObject(*ns);
};
default: {
llvm::errs() << "Action is not supported yet!\n";
};
}
return 0;
}