Check for entry point before code generation

This commit is contained in:
apio 2022-08-26 16:02:07 +02:00
parent cb5f41a7c8
commit 5a0b183198
2 changed files with 15 additions and 1 deletions

View File

@ -33,8 +33,10 @@ void Arguments::parse(int argc, char** argv)
CreateValueArgument(march, "", "march", "Architecture to compile for.", "native"); CreateValueArgument(march, "", "march", "Architecture to compile for.", "native");
CreateValueArgument(mcpu, "", "mcpu", "CPU to compile for.", "generic"); CreateValueArgument(mcpu, "", "mcpu", "CPU to compile for.", "generic");
CreateValueArgument(msystem, "", "msystem", "Operating System to compile for.", "native"); CreateValueArgument(msystem, "", "msystem", "Operating System to compile for.", "native");
CreateValueArgument(entry, "e", "entry", "Name of entry function.", "main");
CreateFlagArgument(emit_llvm, "", "emit-llvm", "Emit LLVM IR instead of an object file."); CreateFlagArgument(emit_llvm, "", "emit-llvm", "Emit LLVM IR instead of an object file.");
CreateFlagArgument(omit_entry, "", "omit-entry", "Do not check for an entry point.");
#ifndef NO_BENCHMARKING #ifndef NO_BENCHMARKING
CreateFlagArgument(mprofile, "", "mprofile", "Show execution times for functions."); CreateFlagArgument(mprofile, "", "mprofile", "Show execution times for functions.");
@ -46,7 +48,9 @@ void Arguments::parse(int argc, char** argv)
SaveValueArgument(march); SaveValueArgument(march);
SaveValueArgument(msystem); SaveValueArgument(msystem);
SaveValueArgument(mcpu); SaveValueArgument(mcpu);
SaveValueArgument(entry);
SaveFlagArgument(emit_llvm); SaveFlagArgument(emit_llvm);
SaveFlagArgument(omit_entry);
setTriple(values["march"], values["msystem"]); setTriple(values["march"], values["msystem"]);

View File

@ -1,6 +1,7 @@
#include "IRBuilder.h" #include "IRBuilder.h"
#include "Arguments.h" #include "Arguments.h"
#include "Error.h" #include "Error.h"
#include "FormatString/FormatString.hpp"
#include "GlobalContext.h" #include "GlobalContext.h"
#include "llvm/IR/Function.h" #include "llvm/IR/Function.h"
#include "llvm/IR/LegacyPassManager.h" #include "llvm/IR/LegacyPassManager.h"
@ -29,6 +30,14 @@ llvm::IRBuilder<>* IRBuilder::getBuilder()
void IRBuilder::create_program(std::shared_ptr<ProgramNode> program) void IRBuilder::create_program(std::shared_ptr<ProgramNode> program)
{ {
program->walk([&](std::shared_ptr<TopLevelNode> node) { node->codegen(this, module.get()); }); program->walk([&](std::shared_ptr<TopLevelNode> node) { node->codegen(this, module.get()); });
if (!Arguments::flags["omit_entry"])
{
if (!module->getFunction(Arguments::values["entry"]))
{
Error::throw_error_without_location(format_string("Missing entry point: %s", Arguments::values["entry"]));
}
}
} }
void IRBuilder::resolveToLLVMIR(std::string_view path) void IRBuilder::resolveToLLVMIR(std::string_view path)
@ -38,7 +47,8 @@ void IRBuilder::resolveToLLVMIR(std::string_view path)
if (EC) if (EC)
{ {
Error::throw_error_without_location("Could not open file " + Arguments::values["output"] + " :" + EC.message()); Error::throw_error_without_location(
format_string("Could not open file %s: %s", Arguments::values["output"], EC.message()));
} }
module->print(dest, nullptr); module->print(dest, nullptr);