From 84720649ba34646343c45b709e66b559aa65812d Mon Sep 17 00:00:00 2001 From: Nathan Seymour Date: Thu, 7 Nov 2024 10:29:37 -0600 Subject: [PATCH] refactor module definitions; --- CMakeLists.txt | 5 ++--- src/calculator/main.cpp | 4 +--- src/compiler/Compiler.h | 10 +++++----- src/compiler/Library.h | 16 ++++++++-------- src/compiler/Program.h | 6 +++--- src/compiler/transformer/IRGenerator.cpp | 11 ++++++++--- src/compiler/transformer/IRGenerator.h | 3 ++- src/graphic/Graph.h | 2 +- src/graphic/ugl/shapes/Line.h | 2 +- src/parser/Node.h | 12 ++++++++++++ 10 files changed, 43 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fa2741..7ca7d28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,6 @@ endif() # Unlogic add_library(unlogic STATIC - src/parser/Node.cpp src/parser/Node.h src/compiler/Compiler.cpp src/compiler/Compiler.h @@ -58,8 +57,8 @@ add_library(unlogic STATIC src/parser/Parser.h src/compiler/Program.cpp src/compiler/Program.h - src/compiler/IRGenerator.cpp - src/compiler/IRGenerator.h + src/compiler/transformer/IRGenerator.cpp + src/compiler/transformer/IRGenerator.h ) target_link_libraries(unlogic PUBLIC ${llvm_libs} buffalo) target_include_directories(unlogic PUBLIC ${LLVM_INCLUDE_DIRS} src) diff --git a/src/calculator/main.cpp b/src/calculator/main.cpp index a3afc2c..c53fb09 100644 --- a/src/calculator/main.cpp +++ b/src/calculator/main.cpp @@ -1,6 +1,5 @@ #include -#include "Compiler.h" -#include "std/StandardLibrary.h" +#include "compiler/Compiler.h" #include "ui/Window.h" /* @@ -14,7 +13,6 @@ int main(int argc, char *argv[]) { unlogic::Compiler::InitializeGlobalCompilerRuntime(); - unlogic::Compiler::RegisterGlobalLibrary(unlogic::stdlib); auto app = Gtk::Application::create("global.seymour.unlogic"); return app->make_window_and_run(argc, argv); diff --git a/src/compiler/Compiler.h b/src/compiler/Compiler.h index e0e9b64..1cb1e9c 100644 --- a/src/compiler/Compiler.h +++ b/src/compiler/Compiler.h @@ -75,7 +75,7 @@ namespace unlogic Compiler::global_init_complete_.wait(false); // Establish context for build - llvm::LLVMContext ctx; + auto ctx = std::make_unique(); // Build parser auto parser = bf::SLRParser::Build(unlogic::tokenizer, unlogic::unlogic_program); @@ -89,7 +89,7 @@ namespace unlogic for(auto &library_definition : this->default_libraries_) { - Library library = library_definition.Build(ctx); + Library library = library_definition.Build(*ctx.get()); // Create dylib auto dylib = jit->createJITDylib(library.name); @@ -121,11 +121,11 @@ namespace unlogic auto body = std::get>(*parser->Parse(program_text)); - Program program(ctx, std::move(body)); + Program program(*ctx.get(), std::move(body)); - auto module = std::make_unique(std::move(program.Build())); + auto module = std::move(program.Build()); - llvm::orc::ThreadSafeModule tsm(std::move(module), std::move(ctx.llvm_ctx)); + llvm::orc::ThreadSafeModule tsm(std::move(module), std::move(ctx)); jit->addIRModule(std::move(tsm)); return Executable(std::move(jit)); diff --git a/src/compiler/Library.h b/src/compiler/Library.h index 5bd7fde..d3a243f 100644 --- a/src/compiler/Library.h +++ b/src/compiler/Library.h @@ -22,31 +22,31 @@ namespace unlogic { std::string name; llvm::LLVMContext &ctx; - llvm::Module module; + std::unique_ptr module; std::vector functions; void AddFunction(char const *function_name, llvm::FunctionType *type, void *native_function) { - llvm::Function *function = llvm::Function::Create(type, llvm::Function::ExternalLinkage, function_name, this->module); + llvm::Function *function = llvm::Function::Create(type, llvm::Function::ExternalLinkage, function_name, *this->module); llvm::orc::ExecutorSymbolDef symbol = { llvm::orc::ExecutorAddr::fromPtr(native_function), llvm::JITSymbolFlags::Callable, }; - this->functions.emplace_back({ - .symbol = std::move(symbol), - .function = function, - }); + LibraryFunction lib_function { + .symbol = symbol, + .function = function, + }; + this->functions.push_back(lib_function); } Library(std::string name, llvm::LLVMContext &ctx) : name(std::move(name)), ctx(ctx), - module(name, ctx) {} + module(std::make_unique(name, ctx)) {} Library() = delete; - Library(Library const&) = delete; }; class LibraryDefinition diff --git a/src/compiler/Program.h b/src/compiler/Program.h index a7c6192..5abc492 100644 --- a/src/compiler/Program.h +++ b/src/compiler/Program.h @@ -14,15 +14,15 @@ namespace unlogic std::unique_ptr body_; public: - llvm::Module Build() + std::unique_ptr Build() { - llvm::Module module("unlogic", this->llvm_ctx_); + auto module = std::make_unique("unlogic", this->llvm_ctx_); // Create IR generation context Scope program_scope; IRGenerationContext ctx = { .llvm_ctx = this->llvm_ctx_, - .module = module, + .module = std::move(module), .builder = this->builder_, .scope = program_scope, }; diff --git a/src/compiler/transformer/IRGenerator.cpp b/src/compiler/transformer/IRGenerator.cpp index 5832e2c..3de641f 100644 --- a/src/compiler/transformer/IRGenerator.cpp +++ b/src/compiler/transformer/IRGenerator.cpp @@ -6,6 +6,11 @@ void unlogic::IRGenerator::Visit(const unlogic::NumericLiteralNode *node) this->values.push(value); } +void unlogic::IRGenerator::Visit(const unlogic::StringLiteralNode *node) +{ + +} + void unlogic::IRGenerator::Visit(const unlogic::VariableNode *node) { llvm::Value *value = *this->ctx.scope.Lookup(node->identifier_); @@ -14,7 +19,7 @@ void unlogic::IRGenerator::Visit(const unlogic::VariableNode *node) void unlogic::IRGenerator::Visit(const unlogic::CallNode *node) { - llvm::Function *function = ctx.module.getFunction(node->function_name_); + llvm::Function *function = ctx.module->getFunction(node->function_name_); if(function->arg_size() < node->arguments_.size()) { @@ -100,7 +105,7 @@ void unlogic::IRGenerator::Visit(const unlogic::PotentiationNode *node) llvm::Value *rhs = this->values.top(); this->values.pop(); - llvm::Function *pow = ctx.module.getFunction("pow"); + llvm::Function *pow = ctx.module->getFunction("pow"); llvm::Value *value = ctx.builder.CreateCall(pow, {lhs, rhs}, "powtmp"); this->values.push(value); @@ -111,7 +116,7 @@ void unlogic::IRGenerator::Visit(const unlogic::FunctionDefinitionNode *node) // Generate function information std::vector argument_types(node->args_.size(), llvm::Type::getDoubleTy(ctx.llvm_ctx)); llvm::FunctionType *function_type = llvm::FunctionType::get(llvm::Type::getDoubleTy(ctx.llvm_ctx), argument_types, false); - llvm::Function *function = llvm::Function::Create(function_type, llvm::Function::ExternalLinkage, node->name_, ctx.module); + llvm::Function *function = llvm::Function::Create(function_type, llvm::Function::ExternalLinkage, node->name_, *ctx.module); unsigned idx = 0; for (auto &arg : function->args()) diff --git a/src/compiler/transformer/IRGenerator.h b/src/compiler/transformer/IRGenerator.h index 02a2c56..7a7f2dc 100644 --- a/src/compiler/transformer/IRGenerator.h +++ b/src/compiler/transformer/IRGenerator.h @@ -40,7 +40,7 @@ namespace unlogic struct IRGenerationContext { llvm::LLVMContext &llvm_ctx; - llvm::Module &module; + std::unique_ptr module; llvm::IRBuilder<> &builder; Scope &scope; }; @@ -51,6 +51,7 @@ namespace unlogic std::stack values; void Visit(const NumericLiteralNode *node) override; + void Visit(const StringLiteralNode *node) override; void Visit(const DivisionNode *node) override; void Visit(const ScopedBlockNode *node) override; void Visit(const VariableNode *node) override; diff --git a/src/graphic/Graph.h b/src/graphic/Graph.h index f5044af..1a36cc3 100644 --- a/src/graphic/Graph.h +++ b/src/graphic/Graph.h @@ -8,7 +8,7 @@ #include #include #include -#include "Compiler.h" +#include "compiler/Compiler.h" #include "graphic/ugl/Drawable.h" #include "graphic/ugl/shapes/Line.h" diff --git a/src/graphic/ugl/shapes/Line.h b/src/graphic/ugl/shapes/Line.h index c14429f..314ba44 100644 --- a/src/graphic/ugl/shapes/Line.h +++ b/src/graphic/ugl/shapes/Line.h @@ -5,7 +5,7 @@ #include #include #include -#include "Compiler.h" +#include "compiler/Compiler.h" #include "graphic/ugl/Drawable.h" namespace unlogic diff --git a/src/parser/Node.h b/src/parser/Node.h index 2c95f22..3b870a8 100644 --- a/src/parser/Node.h +++ b/src/parser/Node.h @@ -16,6 +16,7 @@ namespace unlogic { struct NumericLiteralNode; + struct StringLiteralNode; struct VariableNode; struct CallNode; struct AdditionNode; @@ -29,6 +30,7 @@ namespace unlogic struct INodeVisitor { virtual void Visit(NumericLiteralNode const *node) = 0; + virtual void Visit(StringLiteralNode const *node) = 0; virtual void Visit(VariableNode const *node) = 0; virtual void Visit(CallNode const *node) = 0; virtual void Visit(AdditionNode const *node) = 0; @@ -65,6 +67,16 @@ namespace unlogic NumericLiteralNode(double value) : Literal(value) {} }; + struct StringLiteralNode : public Node, public Literal + { + void Accept(INodeVisitor &visitor) override + { + visitor.Visit(this); + } + + StringLiteralNode(std::string value) : Literal(std::move(value)) {} + }; + struct VariableNode : public Node { std::string identifier_;