From 6d1be9527e10a8519c2635c0c218d08fb1a35a57 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 31 May 2020 14:37:33 -0700 Subject: [PATCH] Start working on a lifted version of a definition. --- 12/CMakeLists.txt | 1 + 12/global_scope.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 12/global_scope.hpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 12/global_scope.cpp create mode 100644 12/global_scope.hpp diff --git a/12/CMakeLists.txt b/12/CMakeLists.txt index c3fb2ef..8a3f3c1 100644 --- a/12/CMakeLists.txt +++ b/12/CMakeLists.txt @@ -32,6 +32,7 @@ add_executable(compiler binop.cpp binop.hpp instruction.cpp instruction.hpp graph.cpp graph.hpp + global_scope.cpp global_scope.hpp ${BISON_parser_OUTPUTS} ${FLEX_scanner_OUTPUTS} main.cpp diff --git a/12/global_scope.cpp b/12/global_scope.cpp new file mode 100644 index 0000000..b0ae9c0 --- /dev/null +++ b/12/global_scope.cpp @@ -0,0 +1,43 @@ +#include "global_scope.hpp" +#include "ast.hpp" + +void global_definition::compile() { + env_ptr new_env = env_ptr(new env_offset(0, nullptr)); + for(auto it = params.rbegin(); it != params.rend(); it++) { + new_env = env_ptr(new env_var(*it, new_env)); + } + body->compile(new_env, instructions); + instructions.push_back(instruction_ptr(new instruction_update(params.size()))); + instructions.push_back(instruction_ptr(new instruction_pop(params.size()))); +} + +void global_definition::declare_llvm(llvm_context& ctx) { + generated_function = ctx.create_custom_function(name, params.size()); +} + +void global_definition::generate_llvm(llvm_context& ctx) { + ctx.builder.SetInsertPoint(&generated_function->getEntryBlock()); + for(auto& instruction : instructions) { + instruction->gen_llvm(ctx, generated_function); + } + ctx.builder.CreateRetVoid(); +} + +global_definition_ptr& global_scope::add_definition(std::string n, std::vector ps, ast_ptr b) { + auto occurence_it = occurence_count.find(n); + int occurence = 0; + if(occurence_it != occurence_count.end()) { + occurence = occurence_it->second + 1; + } + occurence_count[n] = occurence; + + std::string final_name = n; + if (occurence != 0) { + final_name += "_"; + final_name += std::to_string(occurence); + } + + definitions.push_back(global_definition_ptr( + new global_definition(final_name, std::move(ps), std::move(b)))); + return *definitions.rbegin(); +} diff --git a/12/global_scope.hpp b/12/global_scope.hpp new file mode 100644 index 0000000..28350fb --- /dev/null +++ b/12/global_scope.hpp @@ -0,0 +1,34 @@ +#pragma once +#include +#include +#include +#include +#include "instruction.hpp" + +struct ast; +using ast_ptr = std::unique_ptr; + +struct global_definition { + std::string name; + std::vector params; + ast_ptr body; + + std::vector instructions; + llvm::Function* generated_function; + + global_definition(std::string n, std::vector ps, ast_ptr b) + : name(std::move(n)), params(std::move(ps)), body(std::move(b)) {} + + void compile(); + void declare_llvm(llvm_context& ctx); + void generate_llvm(llvm_context& ctx); +}; + +using global_definition_ptr = std::unique_ptr; + +struct global_scope { + std::map occurence_count; + std::vector definitions; + + global_definition_ptr& add_definition(std::string n, std::vector ps, ast_ptr b); +};