diff --git a/core/init.sp b/core/init.sp new file mode 100644 index 0000000..ae802df --- /dev/null +++ b/core/init.sp @@ -0,0 +1,16 @@ +import core/flow; + +namespace init { + + i32 _libcore_Argc; + i8** _libcore_Argv; + + @__libcore_call_init (i32 _Argc, i8** _Argv) { + _libcore_Argc = _Argc; + _libcore_Argv = _Argv; + + main(); + + flow.exit(0); + } +} \ No newline at end of file diff --git a/src/Arguments.cpp b/src/Arguments.cpp index 009b81f..cbbab35 100644 --- a/src/Arguments.cpp +++ b/src/Arguments.cpp @@ -1,19 +1,25 @@ #include "Arguments.h" #include "Error.h" #include "tclap/CmdLine.h" +#include "llvm/Support/Host.h" std::string Arguments::input_fname; std::string Arguments::output_fname; bool Arguments::wimport; +llvm::Triple Arguments::TargetTriple; +std::string Arguments::cpu; void Arguments::parse(int argc, char **argv) { try { TCLAP::CmdLine command_line("The Sapphire compiler.",' ',"0.1"); - TCLAP::UnlabeledValueArg input_fname_arg("file","Input file.",true,"test.sp","filename"); + TCLAP::UnlabeledValueArg input_fname_arg("file","Input file.",true,"test.sp","string"); - TCLAP::ValueArg output_fname_arg("o","output","Output file.",false,"sp-output","output"); + TCLAP::ValueArg output_fname_arg("o","output","Output file.",false,"sp-output","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"); TCLAP::SwitchArg wimport_arg("","wimport","Show a warning when trying to import an already imported file."); @@ -22,13 +28,40 @@ void Arguments::parse(int argc, char **argv) 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(); + cpu = mcpu_arg.getValue(); + + setTriple(march_arg.getValue(),msystem_arg.getValue()); + } catch (TCLAP::ArgException &e) { Error::throw_error_without_location(e.error()); } } + +void Arguments::setTriple(const std::string& arch, const std::string& system) +{ + std::string triple = llvm::sys::getDefaultTargetTriple(); + llvm::Triple tgTriple(triple); + + if(arch != "native") + { + tgTriple.setArchName(arch); + } + if(system != "native") + { + tgTriple.setOSAndEnvironmentName(system); + } + + tgTriple.setVendor(llvm::Triple::VendorType::UnknownVendor); // let's leave it like that + + TargetTriple = tgTriple; +} diff --git a/src/Arguments.h b/src/Arguments.h index bdd3922..28bdf43 100644 --- a/src/Arguments.h +++ b/src/Arguments.h @@ -1,5 +1,6 @@ #pragma once #include +#include struct Arguments { @@ -9,4 +10,10 @@ struct Arguments static std::string output_fname; static bool wimport; + + static std::string cpu; + + static llvm::Triple TargetTriple; +private: + static void setTriple(const std::string& arch, const std::string& system); }; diff --git a/src/Lexer.cpp b/src/Lexer.cpp index ac47d2d..122878a 100644 --- a/src/Lexer.cpp +++ b/src/Lexer.cpp @@ -233,6 +233,7 @@ Token Lexer::create_identifier() if (identifier == "syscall3") return Token::make_with_line({TT_Syscall3,{prev_line,prev_column,loc.fname}},current_line_text); if (identifier == "syscall4") return Token::make_with_line({TT_Syscall4,{prev_line,prev_column,loc.fname}},current_line_text); if (identifier == "syscall5") return Token::make_with_line({TT_Syscall5,{prev_line,prev_column,loc.fname}},current_line_text); + if( identifier == "compmacro" ) return Token::make_with_line({TT_CompilerMacro,{prev_line,prev_column,loc.fname}},current_line_text); return Token::make_with_line({TT_Identifier,identifier,{prev_line,prev_column,loc.fname}},current_line_text); } } @@ -251,6 +252,7 @@ Token Lexer::create_identifier() if (identifier == "syscall3") return Token::make_with_line({TT_Syscall3,{prev_line,prev_column,loc.fname}},current_line_text); if (identifier == "syscall4") return Token::make_with_line({TT_Syscall4,{prev_line,prev_column,loc.fname}},current_line_text); if (identifier == "syscall5") return Token::make_with_line({TT_Syscall5,{prev_line,prev_column,loc.fname}},current_line_text); + if( identifier == "compmacro" ) return Token::make_with_line({TT_CompilerMacro,{prev_line,prev_column,loc.fname}},current_line_text); return Token::make_with_line({TT_Identifier,identifier,{prev_line,prev_column,loc.fname}},current_line_text); } diff --git a/src/Parser.h b/src/Parser.h index 595b8b7..9a23f0e 100644 --- a/src/Parser.h +++ b/src/Parser.h @@ -1,4 +1,7 @@ #pragma once +#include +#include "Lexer.h" +#include "AST/ASTNode.h" class Parser { @@ -7,4 +10,7 @@ private: public: Parser(/* args */); ~Parser(); + + static std::shared_ptr new_parser(const TokenStream& tokens); + std::shared_ptr parse(); }; diff --git a/src/Token.cpp b/src/Token.cpp index ae9171a..34f7251 100644 --- a/src/Token.cpp +++ b/src/Token.cpp @@ -42,7 +42,8 @@ const std::string token_strings[] = { "TT_SYSCALL2", "TT_SYSCALL3", "TT_SYSCALL4", - "TT_SYSCALL5" + "TT_SYSCALL5", + "TT_COMPILERMACRO" }; Token::Token(const TokenType& type) @@ -186,6 +187,8 @@ std::string Token::to_string() const return "SYSCALL4 " + details; case TT_Syscall5: return "SYSCALL5 " + details; + case TT_CompilerMacro: + return "COMPMACRO " + details; } return ""; } diff --git a/src/Token.h b/src/Token.h index 4b6a1e2..ea7d57f 100644 --- a/src/Token.h +++ b/src/Token.h @@ -43,7 +43,8 @@ enum TokenType TT_Syscall2, TT_Syscall3, TT_Syscall4, - TT_Syscall5 + TT_Syscall5, + TT_CompilerMacro }; extern const std::string token_strings[]; diff --git a/src/sapphire.cpp b/src/sapphire.cpp index 95a3dd7..a9452e5 100644 --- a/src/sapphire.cpp +++ b/src/sapphire.cpp @@ -22,4 +22,6 @@ int main(int argc, char** argv) } std::cout << "Output filename: " << Arguments::output_fname << std::endl; + + std::cout << "Output target triple: " << Arguments::TargetTriple.getTriple() << std::endl; }