Update Arguments

This commit is contained in:
apio 2022-08-26 15:55:45 +02:00
parent bbf31e9688
commit cb5f41a7c8
4 changed files with 46 additions and 59 deletions

View File

@ -4,12 +4,9 @@
#include "utils.h" #include "utils.h"
#include "llvm/Support/Host.h" #include "llvm/Support/Host.h"
std::string Arguments::input_fname; std::map<std::string, bool> Arguments::flags;
std::string Arguments::output_fname; std::map<std::string, std::string> Arguments::values;
bool Arguments::wimport;
llvm::Triple Arguments::TargetTriple; llvm::Triple Arguments::TargetTriple;
std::string Arguments::cpu;
bool Arguments::emit_llvm;
void Arguments::parse(int argc, char** argv) void Arguments::parse(int argc, char** argv)
{ {
@ -18,49 +15,44 @@ void Arguments::parse(int argc, char** argv)
{ {
TCLAP::CmdLine command_line("The Sapphire compiler.", ' ', "0.1"); TCLAP::CmdLine command_line("The Sapphire compiler.", ' ', "0.1");
TCLAP::UnlabeledValueArg<std::string> input_fname_arg("file", "Input file.", true, "program.sp", "string"); #define CreateValueArgument(name, value_short, value_long, help, default_value) \
TCLAP::ValueArg<std::string> name##_Argument(value_short, value_long, help, false, default_value, "string"); \
command_line.add(name##_Argument)
TCLAP::ValueArg<std::string> output_fname_arg("o", "output", "Output file.", false, "output.o", "string"); #define CreateFlagArgument(name, value_short, value_long, help) \
TCLAP::ValueArg<std::string> march_arg("", "march", "Architecture to compile for.", false, "native", "string"); TCLAP::SwitchArg name##_FlagArgument(value_short, value_long, help); \
TCLAP::ValueArg<std::string> mcpu_arg("", "mcpu", "CPU to compile for.", false, "generic", "string"); command_line.add(name##_FlagArgument)
TCLAP::ValueArg<std::string> msystem_arg("", "msystem", "Operating System to compile for.", false, "native",
"string");
TCLAP::SwitchArg emit_llvm_arg("", "emit-llvm", "Emit LLVM IR instead of an object file."); #define SaveValueArgument(name) Arguments::values[#name] = name##_Argument.getValue()
#define SaveFlagArgument(name) Arguments::flags[#name] = name##_FlagArgument.getValue()
TCLAP::UnlabeledValueArg<std::string> input_Argument("file", "Input file.", true, "program.sp", "string");
command_line.add(input_Argument);
CreateValueArgument(output, "o", "output", "Output file.", "output.o");
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");
CreateFlagArgument(emit_llvm, "", "emit-llvm", "Emit LLVM IR instead of an object file.");
#ifndef NO_BENCHMARKING #ifndef NO_BENCHMARKING
TCLAP::SwitchArg mprofile_arg("", "mprofile", "Show execution times for functions."); CreateFlagArgument(mprofile, "", "mprofile", "Show execution times for functions.");
#endif #endif
TCLAP::SwitchArg wimport_arg("", "wimport", "Show a warning when trying to import an already imported file.");
command_line.add(wimport_arg);
command_line.add(emit_llvm_arg);
#ifndef NO_BENCHMARKING
command_line.add(mprofile_arg);
#endif
command_line.add(input_fname_arg);
command_line.add(output_fname_arg);
command_line.add(march_arg);
command_line.add(mcpu_arg);
command_line.add(msystem_arg);
command_line.parse(argc, argv); command_line.parse(argc, argv);
input_fname = input_fname_arg.getValue(); SaveValueArgument(output);
output_fname = output_fname_arg.getValue(); SaveValueArgument(input);
wimport = wimport_arg.getValue(); SaveValueArgument(march);
emit_llvm = emit_llvm_arg.getValue(); SaveValueArgument(msystem);
SaveValueArgument(mcpu);
SaveFlagArgument(emit_llvm);
cpu = mcpu_arg.getValue(); setTriple(values["march"], values["msystem"]);
setTriple(march_arg.getValue(), msystem_arg.getValue());
#ifndef NO_BENCHMARKING #ifndef NO_BENCHMARKING
if (mprofile_arg.getValue()) __benchmark_impl::enable(); SaveFlagArgument(mprofile);
if (flags["mprofile"]) __benchmark_impl::enable();
#endif #endif
} }
catch (TCLAP::ArgException& e) catch (TCLAP::ArgException& e)

View File

@ -1,19 +1,14 @@
#pragma once #pragma once
#include "sapphirepch.h" #include "sapphirepch.h"
#include <llvm/ADT/Triple.h> #include <llvm/ADT/Triple.h>
#include <map>
struct Arguments struct Arguments
{ {
static void parse(int argc, char** argv); static void parse(int argc, char** argv);
static std::string input_fname; static std::map<std::string, bool> flags;
static std::string output_fname; static std::map<std::string, std::string> values;
static bool wimport;
static bool emit_llvm;
static std::string cpu;
static llvm::Triple TargetTriple; static llvm::Triple TargetTriple;
private: private:

View File

@ -18,7 +18,7 @@ IRBuilder::IRBuilder()
{ {
context = globalContext; context = globalContext;
builder = std::unique_ptr<llvm::IRBuilder<>>(new llvm::IRBuilder<>(*context)); builder = std::unique_ptr<llvm::IRBuilder<>>(new llvm::IRBuilder<>(*context));
module = std::make_unique<llvm::Module>(Arguments::input_fname, *context); module = std::make_unique<llvm::Module>(Arguments::values["input"], *context);
} }
llvm::IRBuilder<>* IRBuilder::getBuilder() llvm::IRBuilder<>* IRBuilder::getBuilder()
@ -38,7 +38,7 @@ void IRBuilder::resolveToLLVMIR(std::string_view path)
if (EC) if (EC)
{ {
Error::throw_error_without_location("Could not open file " + Arguments::output_fname + " :" + EC.message()); Error::throw_error_without_location("Could not open file " + Arguments::values["output"] + " :" + EC.message());
} }
module->print(dest, nullptr); module->print(dest, nullptr);
@ -70,13 +70,13 @@ void IRBuilder::resolveToObjectFile(std::string_view path)
Error::throw_error_without_location(err); Error::throw_error_without_location(err);
} }
std::string CPU; std::string CPU;
if (Arguments::cpu == "native") if (Arguments::values["mcpu"] == "native")
{ {
CPU = llvm::sys::getHostCPUName(); CPU = llvm::sys::getHostCPUName();
Error::throw_warning_without_location("Using host CPU: " + CPU); Error::throw_warning_without_location("Using host CPU: " + CPU);
} }
else else
CPU = Arguments::cpu; CPU = Arguments::values["mcpu"];
auto Features = ""; auto Features = "";
llvm::TargetOptions opt; llvm::TargetOptions opt;
@ -90,7 +90,7 @@ void IRBuilder::resolveToObjectFile(std::string_view path)
if (EC) if (EC)
{ {
Error::throw_error_without_location("Could not open file " + Arguments::output_fname + " :" + EC.message()); Error::throw_error_without_location("Could not open file " + Arguments::values["output"] + " :" + EC.message());
} }
llvm::legacy::PassManager pass; llvm::legacy::PassManager pass;

View File

@ -11,20 +11,20 @@ int main(int argc, char** argv)
{ {
Arguments::parse(argc, argv); Arguments::parse(argc, argv);
std::string fname = Arguments::input_fname; std::string filename = Arguments::values["input"];
std::string contents = FileIO::read_all(fname); std::string contents = FileIO::read_all(filename);
std::unique_ptr<Lexer> lexer = Lexer::make_lexer(fname); std::unique_ptr<Lexer> lexer = Lexer::make_lexer(filename);
TokenStream result; TokenStream tokens;
{ {
benchmark("Lexing"); benchmark("Lexing");
result = lexer->lex(contents); tokens = lexer->lex(contents);
} }
initGlobalContext(); initGlobalContext();
auto parser = Parser::new_parser(result); auto parser = Parser::new_parser(tokens);
std::shared_ptr<ProgramNode> ast; std::shared_ptr<ProgramNode> ast;
{ {
@ -39,7 +39,7 @@ int main(int argc, char** argv)
builder.create_program(ast); builder.create_program(ast);
} }
if (Arguments::emit_llvm) builder.resolveToLLVMIR(Arguments::output_fname); if (Arguments::flags["emit_llvm"]) builder.resolveToLLVMIR(Arguments::values["output"]);
else else
builder.resolveToObjectFile(Arguments::output_fname); builder.resolveToObjectFile(Arguments::values["output"]);
} }