From 5a0b183198ae6eb4a79da1e1ff88645e0cac4e28 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 26 Aug 2022 16:02:07 +0200 Subject: [PATCH] Check for entry point before code generation --- src/Arguments.cpp | 4 ++++ src/IRBuilder.cpp | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Arguments.cpp b/src/Arguments.cpp index 42afc31..ec4a3a0 100644 --- a/src/Arguments.cpp +++ b/src/Arguments.cpp @@ -33,8 +33,10 @@ void Arguments::parse(int argc, char** argv) CreateValueArgument(march, "", "march", "Architecture to compile for.", "native"); CreateValueArgument(mcpu, "", "mcpu", "CPU to compile for.", "generic"); 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(omit_entry, "", "omit-entry", "Do not check for an entry point."); #ifndef NO_BENCHMARKING CreateFlagArgument(mprofile, "", "mprofile", "Show execution times for functions."); @@ -46,7 +48,9 @@ void Arguments::parse(int argc, char** argv) SaveValueArgument(march); SaveValueArgument(msystem); SaveValueArgument(mcpu); + SaveValueArgument(entry); SaveFlagArgument(emit_llvm); + SaveFlagArgument(omit_entry); setTriple(values["march"], values["msystem"]); diff --git a/src/IRBuilder.cpp b/src/IRBuilder.cpp index 45e7b7f..d0df9ec 100644 --- a/src/IRBuilder.cpp +++ b/src/IRBuilder.cpp @@ -1,6 +1,7 @@ #include "IRBuilder.h" #include "Arguments.h" #include "Error.h" +#include "FormatString/FormatString.hpp" #include "GlobalContext.h" #include "llvm/IR/Function.h" #include "llvm/IR/LegacyPassManager.h" @@ -29,6 +30,14 @@ llvm::IRBuilder<>* IRBuilder::getBuilder() void IRBuilder::create_program(std::shared_ptr program) { program->walk([&](std::shared_ptr 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) @@ -38,7 +47,8 @@ void IRBuilder::resolveToLLVMIR(std::string_view path) 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);