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 "llvm/Support/Host.h"
std::string Arguments::input_fname;
std::string Arguments::output_fname;
bool Arguments::wimport;
std::map<std::string, bool> Arguments::flags;
std::map<std::string, std::string> 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<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");
TCLAP::ValueArg<std::string> march_arg("", "march", "Architecture to compile for.", false, "native", "string");
TCLAP::ValueArg<std::string> mcpu_arg("", "mcpu", "CPU to compile for.", false, "generic", "string");
TCLAP::ValueArg<std::string> 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<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
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)

View File

@ -1,19 +1,14 @@
#pragma once
#include "sapphirepch.h"
#include <llvm/ADT/Triple.h>
#include <map>
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<std::string, bool> flags;
static std::map<std::string, std::string> values;
static llvm::Triple TargetTriple;
private:

View File

@ -18,7 +18,7 @@ IRBuilder::IRBuilder()
{
context = globalContext;
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()
@ -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;

View File

@ -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 = Lexer::make_lexer(fname);
std::unique_ptr<Lexer> 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<ProgramNode> 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"]);
}