Separate definitions in compiler series
This commit is contained in:
@@ -3,35 +3,33 @@
|
||||
#include "ast.hpp"
|
||||
#include "instruction.hpp"
|
||||
#include "llvm_context.hpp"
|
||||
#include "type_env.hpp"
|
||||
#include <llvm/IR/DerivedTypes.h>
|
||||
#include <llvm/IR/Function.h>
|
||||
#include <llvm/IR/Type.h>
|
||||
|
||||
void definition_defn::typecheck_first(type_mgr& mgr, type_env& env) {
|
||||
void definition_defn::find_free(type_mgr& mgr, type_env_ptr& env) {
|
||||
this->env = env;
|
||||
|
||||
var_env = type_scope(env);
|
||||
return_type = mgr.new_type();
|
||||
type_ptr full_type = return_type;
|
||||
full_type = return_type;
|
||||
|
||||
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));
|
||||
param_types.push_back(param_type);
|
||||
var_env->bind(*it, param_type);
|
||||
}
|
||||
|
||||
env.bind(name, full_type);
|
||||
body->find_free(mgr, var_env, free_variables);
|
||||
}
|
||||
|
||||
void definition_defn::typecheck_second(type_mgr& mgr, const type_env& env) const {
|
||||
type_env new_env = env.scope();
|
||||
auto param_it = params.begin();
|
||||
auto type_it = param_types.rbegin();
|
||||
void definition_defn::insert_types(type_mgr& mgr) {
|
||||
env->bind(name, full_type);
|
||||
}
|
||||
|
||||
while(param_it != params.end() && type_it != param_types.rend()) {
|
||||
new_env.bind(*param_it, *type_it);
|
||||
param_it++;
|
||||
type_it++;
|
||||
}
|
||||
|
||||
type_ptr body_type = body->typecheck(mgr, new_env);
|
||||
void definition_defn::typecheck(type_mgr& mgr) {
|
||||
type_ptr body_type = body->typecheck(mgr);
|
||||
mgr.unify(return_type, body_type);
|
||||
}
|
||||
|
||||
@@ -44,11 +42,12 @@ void definition_defn::compile() {
|
||||
instructions.push_back(instruction_ptr(new instruction_update(params.size())));
|
||||
instructions.push_back(instruction_ptr(new instruction_pop(params.size())));
|
||||
}
|
||||
void definition_defn::gen_llvm_first(llvm_context& ctx) {
|
||||
|
||||
void definition_defn::declare_llvm(llvm_context& ctx) {
|
||||
generated_function = ctx.create_custom_function(name, params.size());
|
||||
}
|
||||
|
||||
void definition_defn::gen_llvm_second(llvm_context& ctx) {
|
||||
void definition_defn::generate_llvm(llvm_context& ctx) {
|
||||
ctx.builder.SetInsertPoint(&generated_function->getEntryBlock());
|
||||
for(auto& instruction : instructions) {
|
||||
instruction->gen_llvm(ctx, generated_function);
|
||||
@@ -56,7 +55,11 @@ void definition_defn::gen_llvm_second(llvm_context& ctx) {
|
||||
ctx.builder.CreateRetVoid();
|
||||
}
|
||||
|
||||
void definition_data::typecheck_first(type_mgr& mgr, type_env& env) {
|
||||
void definition_data::insert_types(type_mgr& mgr, type_env_ptr& env) {
|
||||
this->env = env;
|
||||
}
|
||||
|
||||
void definition_data::insert_constructors() const {
|
||||
type_data* this_type = new type_data(name);
|
||||
type_ptr return_type = type_ptr(this_type);
|
||||
int next_tag = 0;
|
||||
@@ -71,19 +74,11 @@ void definition_data::typecheck_first(type_mgr& mgr, type_env& env) {
|
||||
full_type = type_ptr(new type_arr(type, full_type));
|
||||
}
|
||||
|
||||
env.bind(constructor->name, full_type);
|
||||
env->bind(constructor->name, full_type);
|
||||
}
|
||||
}
|
||||
|
||||
void definition_data::typecheck_second(type_mgr& mgr, const type_env& env) const {
|
||||
// Nothing
|
||||
}
|
||||
|
||||
void definition_data::compile() {
|
||||
|
||||
}
|
||||
|
||||
void definition_data::gen_llvm_first(llvm_context& ctx) {
|
||||
void definition_data::generate_llvm(llvm_context& ctx) {
|
||||
for(auto& constructor : constructors) {
|
||||
auto new_function =
|
||||
ctx.create_custom_function(constructor->name, constructor->types.size());
|
||||
@@ -99,7 +94,3 @@ void definition_data::gen_llvm_first(llvm_context& ctx) {
|
||||
ctx.builder.CreateRetVoid();
|
||||
}
|
||||
}
|
||||
|
||||
void definition_data::gen_llvm_second(llvm_context& ctx) {
|
||||
// Nothing
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user