Skip to content

Commit

Permalink
refactor module definitions;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Nov 7, 2024
1 parent aea44d9 commit 8472064
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 28 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions src/calculator/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <gtkmm.h>
#include "Compiler.h"
#include "std/StandardLibrary.h"
#include "compiler/Compiler.h"
#include "ui/Window.h"

/*
Expand All @@ -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<unlogic::Window>(argc, argv);
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace unlogic
Compiler::global_init_complete_.wait(false);

// Establish context for build
llvm::LLVMContext ctx;
auto ctx = std::make_unique<llvm::LLVMContext>();

// Build parser
auto parser = bf::SLRParser<ParserGrammarType>::Build(unlogic::tokenizer, unlogic::unlogic_program);
Expand All @@ -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);
Expand Down Expand Up @@ -121,11 +121,11 @@ namespace unlogic

auto body = std::get<std::unique_ptr<Node>>(*parser->Parse(program_text));

Program program(ctx, std::move(body));
Program program(*ctx.get(), std::move(body));

auto module = std::make_unique<llvm::Module>(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));
Expand Down
16 changes: 8 additions & 8 deletions src/compiler/Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ namespace unlogic
{
std::string name;
llvm::LLVMContext &ctx;
llvm::Module module;
std::unique_ptr<llvm::Module> module;
std::vector<LibraryFunction> 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<llvm::Module>(name, ctx)) {}


Library() = delete;
Library(Library const&) = delete;
};

class LibraryDefinition
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace unlogic
std::unique_ptr<Node> body_;

public:
llvm::Module Build()
std::unique_ptr<llvm::Module> Build()
{
llvm::Module module("unlogic", this->llvm_ctx_);
auto module = std::make_unique<llvm::Module>("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,
};
Expand Down
11 changes: 8 additions & 3 deletions src/compiler/transformer/IRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand All @@ -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())
{
Expand Down Expand Up @@ -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);
Expand All @@ -111,7 +116,7 @@ void unlogic::IRGenerator::Visit(const unlogic::FunctionDefinitionNode *node)
// Generate function information
std::vector<llvm::Type*> 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())
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/transformer/IRGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace unlogic
struct IRGenerationContext
{
llvm::LLVMContext &llvm_ctx;
llvm::Module &module;
std::unique_ptr<llvm::Module> module;
llvm::IRBuilder<> &builder;
Scope &scope;
};
Expand All @@ -51,6 +51,7 @@ namespace unlogic
std::stack<llvm::Value *> 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;
Expand Down
2 changes: 1 addition & 1 deletion src/graphic/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <cmath>
#include <glm/vec2.hpp>
#include <glm/ext/matrix_float4x4.hpp>
#include "Compiler.h"
#include "compiler/Compiler.h"
#include "graphic/ugl/Drawable.h"
#include "graphic/ugl/shapes/Line.h"

Expand Down
2 changes: 1 addition & 1 deletion src/graphic/ugl/shapes/Line.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <string>
#include <glm/vec2.hpp>
#include <glm/vec4.hpp>
#include "Compiler.h"
#include "compiler/Compiler.h"
#include "graphic/ugl/Drawable.h"

namespace unlogic
Expand Down
12 changes: 12 additions & 0 deletions src/parser/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace unlogic
{
struct NumericLiteralNode;
struct StringLiteralNode;
struct VariableNode;
struct CallNode;
struct AdditionNode;
Expand All @@ -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;
Expand Down Expand Up @@ -65,6 +67,16 @@ namespace unlogic
NumericLiteralNode(double value) : Literal(value) {}
};

struct StringLiteralNode : public Node, public Literal<std::string>
{
void Accept(INodeVisitor &visitor) override
{
visitor.Visit(this);
}

StringLiteralNode(std::string value) : Literal(std::move(value)) {}
};

struct VariableNode : public Node
{
std::string identifier_;
Expand Down

0 comments on commit 8472064

Please sign in to comment.