Skip to content

Commit

Permalink
refactoring;
Browse files Browse the repository at this point in the history
  • Loading branch information
NateSeymour committed Nov 7, 2024
1 parent 8472064 commit d2ea9fe
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 105 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ add_library(unlogic STATIC
src/Error.h
src/parser/Parser.cpp
src/parser/Parser.h
src/compiler/Program.cpp
src/compiler/Program.h
src/compiler/transformer/IRGenerator.cpp
src/compiler/transformer/IRGenerator.h
)
Expand All @@ -79,6 +77,7 @@ add_executable(unlogic-calculator
src/graphic/ugl/Canvas.h
src/graphic/ugl/VertexBuffer.cpp
src/graphic/ugl/VertexBuffer.h
src/calculator/main.h
)
target_link_libraries(unlogic-calculator PUBLIC unlogic glm::glm ${GTKMM_LIBRARIES})
target_link_directories(unlogic-calculator PUBLIC ${GTKMM_LIBRARY_DIRS})
Expand Down
20 changes: 18 additions & 2 deletions src/calculator/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <thread>
#include <gtkmm.h>
#include "main.h"
#include "compiler/Compiler.h"
#include "ui/Window.h"

Expand All @@ -10,10 +12,24 @@
* - Dispatchers for synchronization
*/

int main(int argc, char *argv[])
void init_compiler_runtime()
{
unlogic::Compiler::InitializeGlobalCompilerRuntime();

unlogic::compiler_runtime_avail = true;
unlogic::compiler_runtime_avail.notify_all();
}

int main(int argc, char *argv[])
{
std::cout << "[MAIN] Initializing compiler runtime..." << std::endl;
std::thread init_thread(init_compiler_runtime);

std::cout << "[MAIN] Creating application..." << std::endl;
auto app = Gtk::Application::create("global.seymour.unlogic");
return app->make_window_and_run<unlogic::Window>(argc, argv);
int ret = app->make_window_and_run<unlogic::Window>(argc, argv);

init_thread.join();

return ret;
}
11 changes: 11 additions & 0 deletions src/calculator/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef UNLOGIC_MAIN_H
#define UNLOGIC_MAIN_H

#include <atomic>

namespace unlogic
{
std::atomic<bool> compiler_runtime_avail = false;
}

#endif //UNLOGIC_MAIN_H
20 changes: 4 additions & 16 deletions src/calculator/ui/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace unlogic
class Window : public Gtk::Window
{
protected:
Glib::Dispatcher event_compilation_finished;

Gtk::Paned divider_;

std::shared_ptr<SourceBuffer> source_buffer_;
Expand Down Expand Up @@ -48,18 +50,10 @@ namespace unlogic

void on_source_buffer_changed()
{
/*
auto source_text = this->source_buffer_->get_text();
auto program = parser.Parse(source_text.c_str());
if(program)
{
Executable executable = Compiler::CompileProgram(std::get<Program>(*program));
executable();
}
*/
}

void CreateUI()
public:
Window()
{
this->set_title("Unlogic");
this->set_default_size(1000, 500);
Expand Down Expand Up @@ -93,12 +87,6 @@ namespace unlogic
this->canvas_.signal_realize().connect(sigc::mem_fun(*this, &Window::on_renderer_realize));
this->canvas_.signal_render().connect(sigc::mem_fun(*this, &Window::on_renderer_render), false);
}

public:
Window()
{
this->CreateUI();
}
};
}

Expand Down
2 changes: 0 additions & 2 deletions src/compiler/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
#include "Compiler.h"

using namespace unlogic;

std::atomic<bool> Compiler::global_init_complete_ = false;
36 changes: 22 additions & 14 deletions src/compiler/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
#include "parser/Node.h"
#include "parser/Parser.h"
#include "Library.h"
#include "Program.h"
#include "std/StandardLibrary.h"
#include "transformer/IRGenerator.h"

namespace unlogic
{
class Executable
class Program
{
friend class Compiler;

void(*function_)() = nullptr;
std::unique_ptr<llvm::orc::LLJIT> jit_;

protected:
explicit Executable(std::unique_ptr<llvm::orc::LLJIT> jit) : jit_(std::move(jit))
explicit Program(std::unique_ptr<llvm::orc::LLJIT> jit) : jit_(std::move(jit))
{
auto function_ea = this->jit_->lookup("__entry");
if(auto e = function_ea.takeError())
Expand All @@ -51,12 +51,11 @@ namespace unlogic
return this->function_();
}

Executable() = delete;
Program() = delete;
};

class Compiler
{
static std::atomic<bool> global_init_complete_;
std::vector<LibraryDefinition> default_libraries_;

public:
Expand All @@ -65,15 +64,10 @@ namespace unlogic
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();

Compiler::global_init_complete_ = true;
Compiler::global_init_complete_.notify_all();
}

Executable Compile(std::string_view program_text)
Program Compile(std::string_view program_text)
{
Compiler::global_init_complete_.wait(false);

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

Expand Down Expand Up @@ -119,16 +113,30 @@ namespace unlogic
main.addToLinkOrder(*dylib);
}

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

Program program(*ctx.get(), std::move(body));
// Compile program
auto module = std::make_unique<llvm::Module>("unlogic", *ctx.get());

// Create IR generation context
Scope program_scope;
IRGenerationContext ir_ctx = {
.llvm_ctx = *ctx.get(),
.module = std::move(module),
.scope = program_scope,
};

// IR Generator
IRGenerator generator(ir_ctx);

auto module = std::move(program.Build());
// Build program
body->Accept(generator);

llvm::orc::ThreadSafeModule tsm(std::move(module), std::move(ctx));
jit->addIRModule(std::move(tsm));

return Executable(std::move(jit));
return Program(std::move(jit));
}

Compiler() = default;
Expand Down
1 change: 0 additions & 1 deletion src/compiler/Program.cpp

This file was deleted.

51 changes: 0 additions & 51 deletions src/compiler/Program.h

This file was deleted.

25 changes: 22 additions & 3 deletions src/compiler/std/StandardLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,39 @@
// Created by nathan on 6/18/24.
//

#include <iostream>
#include <cmath>
#include "StandardLibrary.h"

using namespace unlogic;

extern "C"
{
double unlogic_pow(double base, double exponent) { return std::pow(base, exponent); }
double unlogic_std_pow(double base, double exponent) { return std::pow(base, exponent); }

void unlogic_std_log(char const *message)
{
std::cout << message << std::endl;
}
}

LibraryDefinition unlogic::stdlib("stdlib", [](unlogic::Library &lib) {
std::array<llvm::Type*, 2> pow_args = { llvm::Type::getDoubleTy(lib.ctx), llvm::Type::getDoubleTy(lib.ctx) };
// pow
std::array<llvm::Type*, 2> pow_args = {
llvm::Type::getDoubleTy(lib.ctx),
llvm::Type::getDoubleTy(lib.ctx),
};

llvm::FunctionType *pow_type = llvm::FunctionType::get(llvm::Type::getDoubleTy(lib.ctx), pow_args, false);
lib.AddFunction("pow", pow_type, (void*)unlogic_pow);
lib.AddFunction("pow", pow_type, (void *) unlogic_std_pow);

// log
std::array<llvm::Type*, 1> log_args = {
llvm::Type::getInt8PtrTy(lib.ctx),
};

llvm::FunctionType *log_type = llvm::FunctionType::get(llvm::Type::getVoidTy(lib.ctx), log_args, false);
lib.AddFunction("log", log_type, (void *) unlogic_std_log);
});

/*
Expand Down
17 changes: 8 additions & 9 deletions src/compiler/transformer/IRGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void unlogic::IRGenerator::Visit(const unlogic::CallNode *node)
this->values.pop();
}

llvm::Value *value = ctx.builder.CreateCall(function, argument_values, "calltmp");
llvm::Value *value = this->builder.CreateCall(function, argument_values, "calltmp");
this->values.push(value);
}

Expand All @@ -49,7 +49,7 @@ void unlogic::IRGenerator::Visit(const unlogic::AdditionNode *node)
llvm::Value *rhs = this->values.top();
this->values.pop();

llvm::Value *value = ctx.builder.CreateFAdd(lhs, rhs, "addtmp");
llvm::Value *value = this->builder.CreateFAdd(lhs, rhs, "addtmp");
this->values.push(value);
}

Expand All @@ -63,7 +63,7 @@ void unlogic::IRGenerator::Visit(const unlogic::SubtractionNode *node)
llvm::Value *rhs = this->values.top();
this->values.pop();

llvm::Value *value = ctx.builder.CreateFSub(lhs, rhs, "subtmp");
llvm::Value *value = this->builder.CreateFSub(lhs, rhs, "subtmp");
this->values.push(value);
}

Expand All @@ -77,7 +77,7 @@ void unlogic::IRGenerator::Visit(const unlogic::MultiplicationNode *node)
llvm::Value *rhs = this->values.top();
this->values.pop();

llvm::Value *value = ctx.builder.CreateFMul(lhs, rhs, "multmp");
llvm::Value *value = this->builder.CreateFMul(lhs, rhs, "multmp");
this->values.push(value);
}

Expand All @@ -91,7 +91,7 @@ void unlogic::IRGenerator::Visit(const unlogic::DivisionNode *node)
llvm::Value *rhs = this->values.top();
this->values.pop();

llvm::Value *value = ctx.builder.CreateFDiv(lhs, rhs, "divtmp");
llvm::Value *value = this->builder.CreateFDiv(lhs, rhs, "divtmp");
this->values.push(value);
}

Expand All @@ -107,7 +107,7 @@ void unlogic::IRGenerator::Visit(const unlogic::PotentiationNode *node)

llvm::Function *pow = ctx.module->getFunction("pow");

llvm::Value *value = ctx.builder.CreateCall(pow, {lhs, rhs}, "powtmp");
llvm::Value *value = this->builder.CreateCall(pow, {lhs, rhs}, "powtmp");
this->values.push(value);
}

Expand All @@ -126,7 +126,7 @@ void unlogic::IRGenerator::Visit(const unlogic::FunctionDefinitionNode *node)

// Generate function body
llvm::BasicBlock *block = llvm::BasicBlock::Create(ctx.llvm_ctx, node->name_, function);
ctx.builder.SetInsertPoint(block);
this->builder.SetInsertPoint(block);

ctx.scope.PushLayer();
for(auto &arg : function->args())
Expand All @@ -138,7 +138,7 @@ void unlogic::IRGenerator::Visit(const unlogic::FunctionDefinitionNode *node)
llvm::Value* return_value = this->values.top();
this->values.pop();

ctx.builder.CreateRet(return_value);
this->builder.CreateRet(return_value);

ctx.scope.PopLayer();

Expand All @@ -149,5 +149,4 @@ void unlogic::IRGenerator::Visit(const unlogic::FunctionDefinitionNode *node)

void unlogic::IRGenerator::Visit(const unlogic::ScopedBlockNode *node)
{

}
Loading

0 comments on commit d2ea9fe

Please sign in to comment.