2020-03-10 20:58:26 -07:00
|
|
|
#include "definition.hpp"
|
|
|
|
#include "error.hpp"
|
|
|
|
#include "ast.hpp"
|
|
|
|
#include "instruction.hpp"
|
|
|
|
#include "llvm_context.hpp"
|
2020-03-24 21:08:02 -07:00
|
|
|
#include "type_env.hpp"
|
2020-03-10 20:58:26 -07:00
|
|
|
#include <llvm/IR/DerivedTypes.h>
|
|
|
|
#include <llvm/IR/Function.h>
|
|
|
|
#include <llvm/IR/Type.h>
|
|
|
|
|
2020-03-24 21:08:02 -07:00
|
|
|
void definition_defn::find_free(type_mgr& mgr, type_env_ptr& env) {
|
|
|
|
this->env = env;
|
|
|
|
|
|
|
|
var_env = type_scope(env);
|
2020-03-10 20:58:26 -07:00
|
|
|
return_type = mgr.new_type();
|
2020-03-24 21:08:02 -07:00
|
|
|
full_type = return_type;
|
2020-03-10 20:58:26 -07:00
|
|
|
|
|
|
|
for(auto it = params.rbegin(); it != params.rend(); it++) {
|
|
|
|
type_ptr param_type = mgr.new_type();
|
|
|
|
full_type = type_ptr(new type_arr(param_type, full_type));
|
2020-03-24 21:08:02 -07:00
|
|
|
var_env->bind(*it, param_type);
|
2020-03-10 20:58:26 -07:00
|
|
|
}
|
|
|
|
|
2020-03-24 21:08:02 -07:00
|
|
|
body->find_free(mgr, var_env, free_variables);
|
2020-03-10 20:58:26 -07:00
|
|
|
}
|
|
|
|
|
2020-03-24 21:08:02 -07:00
|
|
|
void definition_defn::insert_types(type_mgr& mgr) {
|
|
|
|
env->bind(name, full_type);
|
|
|
|
}
|
2020-03-10 20:58:26 -07:00
|
|
|
|
2020-03-24 21:08:02 -07:00
|
|
|
void definition_defn::typecheck(type_mgr& mgr) {
|
|
|
|
type_ptr body_type = body->typecheck(mgr);
|
2020-03-10 20:58:26 -07:00
|
|
|
mgr.unify(return_type, body_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void definition_defn::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())));
|
|
|
|
}
|
2020-03-24 21:08:02 -07:00
|
|
|
|
|
|
|
void definition_defn::declare_llvm(llvm_context& ctx) {
|
2020-03-10 20:58:26 -07:00
|
|
|
generated_function = ctx.create_custom_function(name, params.size());
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:08:02 -07:00
|
|
|
void definition_defn::generate_llvm(llvm_context& ctx) {
|
2020-03-10 20:58:26 -07:00
|
|
|
ctx.builder.SetInsertPoint(&generated_function->getEntryBlock());
|
|
|
|
for(auto& instruction : instructions) {
|
|
|
|
instruction->gen_llvm(ctx, generated_function);
|
|
|
|
}
|
|
|
|
ctx.builder.CreateRetVoid();
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:08:02 -07:00
|
|
|
void definition_data::insert_types(type_mgr& mgr, type_env_ptr& env) {
|
|
|
|
this->env = env;
|
|
|
|
}
|
|
|
|
|
|
|
|
void definition_data::insert_constructors() const {
|
2020-03-10 20:58:26 -07:00
|
|
|
type_data* this_type = new type_data(name);
|
|
|
|
type_ptr return_type = type_ptr(this_type);
|
|
|
|
int next_tag = 0;
|
|
|
|
|
|
|
|
for(auto& constructor : constructors) {
|
|
|
|
constructor->tag = next_tag;
|
|
|
|
this_type->constructors[constructor->name] = { next_tag++ };
|
|
|
|
|
|
|
|
type_ptr full_type = return_type;
|
|
|
|
for(auto it = constructor->types.rbegin(); it != constructor->types.rend(); it++) {
|
|
|
|
type_ptr type = type_ptr(new type_base(*it));
|
|
|
|
full_type = type_ptr(new type_arr(type, full_type));
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:08:02 -07:00
|
|
|
env->bind(constructor->name, full_type);
|
2020-03-10 20:58:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:08:02 -07:00
|
|
|
void definition_data::generate_llvm(llvm_context& ctx) {
|
2020-03-10 20:58:26 -07:00
|
|
|
for(auto& constructor : constructors) {
|
|
|
|
auto new_function =
|
|
|
|
ctx.create_custom_function(constructor->name, constructor->types.size());
|
|
|
|
std::vector<instruction_ptr> instructions;
|
|
|
|
instructions.push_back(instruction_ptr(
|
|
|
|
new instruction_pack(constructor->tag, constructor->types.size())
|
|
|
|
));
|
|
|
|
instructions.push_back(instruction_ptr(new instruction_update(0)));
|
|
|
|
ctx.builder.SetInsertPoint(&new_function->getEntryBlock());
|
|
|
|
for (auto& instruction : instructions) {
|
|
|
|
instruction->gen_llvm(ctx, new_function);
|
|
|
|
}
|
|
|
|
ctx.builder.CreateRetVoid();
|
|
|
|
}
|
|
|
|
}
|