2019-11-05 10:40:51 -08:00
|
|
|
#include "definition.hpp"
|
2019-11-02 16:38:11 -07:00
|
|
|
#include "error.hpp"
|
2019-11-05 10:40:51 -08:00
|
|
|
#include "ast.hpp"
|
2019-11-06 12:52:42 -08:00
|
|
|
#include "llvm_context.hpp"
|
|
|
|
#include <llvm/IR/DerivedTypes.h>
|
|
|
|
#include <llvm/IR/Function.h>
|
|
|
|
#include <llvm/IR/Type.h>
|
2019-11-02 16:38:11 -07:00
|
|
|
|
|
|
|
void definition_defn::typecheck_first(type_mgr& mgr, type_env& env) {
|
|
|
|
return_type = mgr.new_type();
|
|
|
|
type_ptr 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
env.bind(name, full_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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_common(mgr, new_env);
|
|
|
|
mgr.unify(return_type, body_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void definition_defn::resolve(const type_mgr& mgr) {
|
|
|
|
type_var* var;
|
|
|
|
body->resolve_common(mgr);
|
|
|
|
|
|
|
|
return_type = mgr.resolve(return_type, var);
|
|
|
|
if(var) throw type_error("ambiguously typed program");
|
|
|
|
for(auto& param_type : param_types) {
|
|
|
|
param_type = mgr.resolve(param_type, var);
|
|
|
|
if(var) throw type_error("ambiguously typed program");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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())));
|
|
|
|
}
|
2019-11-06 12:52:42 -08:00
|
|
|
void definition_defn::gen_llvm_first(llvm_context& ctx) {
|
|
|
|
generated_function = ctx.create_custom_function(name, params.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void definition_defn::gen_llvm_second(llvm_context& ctx) {
|
|
|
|
ctx.builder.SetInsertPoint(&generated_function->getEntryBlock());
|
|
|
|
for(auto& instruction : instructions) {
|
|
|
|
instruction->gen_llvm(ctx, generated_function);
|
|
|
|
}
|
|
|
|
ctx.create_popn(generated_function, ctx.create_size(params.size()));
|
|
|
|
ctx.builder.CreateRetVoid();
|
|
|
|
}
|
2019-11-02 16:38:11 -07:00
|
|
|
|
|
|
|
void definition_data::typecheck_first(type_mgr& mgr, type_env& env) {
|
|
|
|
type_data* this_type = new type_data(name);
|
|
|
|
type_ptr return_type = type_ptr(this_type);
|
|
|
|
int next_tag = 0;
|
|
|
|
|
|
|
|
for(auto& constructor : constructors) {
|
2019-11-06 12:52:42 -08:00
|
|
|
constructor->tag = next_tag;
|
2019-11-02 16:38:11 -07:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
env.bind(constructor->name, full_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void definition_data::typecheck_second(type_mgr& mgr, const type_env& env) const {
|
|
|
|
// Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
void definition_data::resolve(const type_mgr& mgr) {
|
|
|
|
// Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
void definition_data::compile() {
|
|
|
|
|
|
|
|
}
|
2019-11-06 12:52:42 -08:00
|
|
|
|
|
|
|
void definition_data::gen_llvm_first(llvm_context& ctx) {
|
|
|
|
for(auto& constructor : constructors) {
|
|
|
|
auto new_function =
|
|
|
|
ctx.create_custom_function(constructor->name, constructor->types.size());
|
|
|
|
ctx.builder.SetInsertPoint(&new_function->getEntryBlock());
|
|
|
|
ctx.create_pack(new_function,
|
|
|
|
ctx.create_size(constructor->types.size()),
|
|
|
|
ctx.create_i8(constructor->tag)
|
|
|
|
);
|
|
|
|
ctx.builder.CreateRetVoid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void definition_data::gen_llvm_second(llvm_context& ctx) {
|
|
|
|
// Nothing
|
|
|
|
}
|