Start working on a lifted version of a definition.

This commit is contained in:
Danila Fedorin 2020-05-31 14:37:33 -07:00
parent 7f8dae74ac
commit 08c8aca144
3 changed files with 78 additions and 0 deletions

View File

@ -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

View File

@ -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<std::string> 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();
}

View File

@ -0,0 +1,34 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <llvm/IR/Function.h>
#include "instruction.hpp"
struct ast;
using ast_ptr = std::unique_ptr<ast>;
struct global_definition {
std::string name;
std::vector<std::string> params;
ast_ptr body;
std::vector<instruction_ptr> instructions;
llvm::Function* generated_function;
global_definition(std::string n, std::vector<std::string> 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<global_definition>;
struct global_scope {
std::map<std::string, int> occurence_count;
std::vector<global_definition_ptr> definitions;
global_definition_ptr& add_definition(std::string n, std::vector<std::string> ps, ast_ptr b);
};