diff --git a/src/Arguments.cpp b/src/Arguments.cpp index 5b3d34c..42afc31 100644 --- a/src/Arguments.cpp +++ b/src/Arguments.cpp @@ -4,12 +4,9 @@ #include "utils.h" #include "llvm/Support/Host.h" -std::string Arguments::input_fname; -std::string Arguments::output_fname; -bool Arguments::wimport; +std::map Arguments::flags; +std::map Arguments::values; llvm::Triple Arguments::TargetTriple; -std::string Arguments::cpu; -bool Arguments::emit_llvm; 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::UnlabeledValueArg input_fname_arg("file", "Input file.", true, "program.sp", "string"); +#define CreateValueArgument(name, value_short, value_long, help, default_value) \ + TCLAP::ValueArg name##_Argument(value_short, value_long, help, false, default_value, "string"); \ + command_line.add(name##_Argument) - TCLAP::ValueArg output_fname_arg("o", "output", "Output file.", false, "output.o", "string"); - TCLAP::ValueArg march_arg("", "march", "Architecture to compile for.", false, "native", "string"); - TCLAP::ValueArg mcpu_arg("", "mcpu", "CPU to compile for.", false, "generic", "string"); - TCLAP::ValueArg msystem_arg("", "msystem", "Operating System to compile for.", false, "native", - "string"); +#define CreateFlagArgument(name, value_short, value_long, help) \ + TCLAP::SwitchArg name##_FlagArgument(value_short, value_long, help); \ + command_line.add(name##_FlagArgument) - 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 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 - TCLAP::SwitchArg mprofile_arg("", "mprofile", "Show execution times for functions."); + CreateFlagArgument(mprofile, "", "mprofile", "Show execution times for functions."); #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); - input_fname = input_fname_arg.getValue(); - output_fname = output_fname_arg.getValue(); - wimport = wimport_arg.getValue(); - emit_llvm = emit_llvm_arg.getValue(); + SaveValueArgument(output); + SaveValueArgument(input); + SaveValueArgument(march); + SaveValueArgument(msystem); + SaveValueArgument(mcpu); + SaveFlagArgument(emit_llvm); - cpu = mcpu_arg.getValue(); - - setTriple(march_arg.getValue(), msystem_arg.getValue()); + setTriple(values["march"], values["msystem"]); #ifndef NO_BENCHMARKING - if (mprofile_arg.getValue()) __benchmark_impl::enable(); + SaveFlagArgument(mprofile); + if (flags["mprofile"]) __benchmark_impl::enable(); #endif } catch (TCLAP::ArgException& e) diff --git a/src/Arguments.h b/src/Arguments.h index fd8d2cc..e44052f 100644 --- a/src/Arguments.h +++ b/src/Arguments.h @@ -1,19 +1,14 @@ #pragma once #include "sapphirepch.h" #include +#include struct Arguments { static void parse(int argc, char** argv); - static std::string input_fname; - static std::string output_fname; - - static bool wimport; - static bool emit_llvm; - - static std::string cpu; - + static std::map flags; + static std::map values; static llvm::Triple TargetTriple; private: diff --git a/src/IRBuilder.cpp b/src/IRBuilder.cpp index 083f390..45e7b7f 100644 --- a/src/IRBuilder.cpp +++ b/src/IRBuilder.cpp @@ -18,7 +18,7 @@ IRBuilder::IRBuilder() { context = globalContext; builder = std::unique_ptr>(new llvm::IRBuilder<>(*context)); - module = std::make_unique(Arguments::input_fname, *context); + module = std::make_unique(Arguments::values["input"], *context); } llvm::IRBuilder<>* IRBuilder::getBuilder() @@ -38,7 +38,7 @@ void IRBuilder::resolveToLLVMIR(std::string_view path) 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); @@ -70,13 +70,13 @@ void IRBuilder::resolveToObjectFile(std::string_view path) Error::throw_error_without_location(err); } std::string CPU; - if (Arguments::cpu == "native") + if (Arguments::values["mcpu"] == "native") { CPU = llvm::sys::getHostCPUName(); Error::throw_warning_without_location("Using host CPU: " + CPU); } else - CPU = Arguments::cpu; + CPU = Arguments::values["mcpu"]; auto Features = ""; llvm::TargetOptions opt; @@ -90,7 +90,7 @@ void IRBuilder::resolveToObjectFile(std::string_view path) 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; diff --git a/src/sapphire.cpp b/src/sapphire.cpp index 659353f..c7368ef 100644 --- a/src/sapphire.cpp +++ b/src/sapphire.cpp @@ -11,20 +11,20 @@ int main(int argc, char** argv) { Arguments::parse(argc, argv); - std::string fname = Arguments::input_fname; - std::string contents = FileIO::read_all(fname); + std::string filename = Arguments::values["input"]; + std::string contents = FileIO::read_all(filename); - std::unique_ptr lexer = Lexer::make_lexer(fname); + std::unique_ptr lexer = Lexer::make_lexer(filename); - TokenStream result; + TokenStream tokens; { benchmark("Lexing"); - result = lexer->lex(contents); + tokens = lexer->lex(contents); } initGlobalContext(); - auto parser = Parser::new_parser(result); + auto parser = Parser::new_parser(tokens); std::shared_ptr ast; { @@ -39,7 +39,7 @@ int main(int argc, char** argv) builder.create_program(ast); } - if (Arguments::emit_llvm) builder.resolveToLLVMIR(Arguments::output_fname); + if (Arguments::flags["emit_llvm"]) builder.resolveToLLVMIR(Arguments::values["output"]); else - builder.resolveToObjectFile(Arguments::output_fname); + builder.resolveToObjectFile(Arguments::values["output"]); }