bloglang/13/main.cpp

214 lines
7.2 KiB
C++
Raw Normal View History

2020-09-08 18:38:05 -07:00
#include "ast.hpp"
#include <iostream>
#include "binop.hpp"
#include "definition.hpp"
#include "graph.hpp"
#include "instruction.hpp"
#include "llvm_context.hpp"
#include "parser.hpp"
#include "error.hpp"
#include "type.hpp"
#include "parse_driver.hpp"
2020-09-10 14:50:03 -07:00
#include "type_env.hpp"
2020-09-08 18:38:05 -07:00
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Target/TargetMachine.h"
2020-09-09 12:21:50 -07:00
void yy::parser::error(const yy::location& loc, const std::string& msg) {
2020-09-08 18:38:05 -07:00
std::cout << "An error occured: " << msg << std::endl;
}
2020-09-10 00:54:35 -07:00
void prelude_types(definition_group& defs, type_env_ptr env) {
2020-09-09 18:08:38 -07:00
type_ptr int_type = type_ptr(new type_internal("Int"));
2020-09-08 18:38:05 -07:00
env->bind_type("Int", int_type);
type_ptr int_type_app = type_ptr(new type_app(int_type));
2020-09-10 00:54:35 -07:00
type_ptr bool_type = type_ptr(new type_internal("Bool"));
env->bind_type("Bool", bool_type);
type_ptr bool_type_app = type_ptr(new type_app(bool_type));
2020-09-08 18:38:05 -07:00
type_ptr binop_type = type_ptr(new type_arr(
int_type_app,
type_ptr(new type_arr(int_type_app, int_type_app))));
type_ptr cmp_type = type_ptr(new type_arr(
int_type_app,
type_ptr(new type_arr(int_type_app, bool_type_app))));
2020-09-10 14:50:03 -07:00
constexpr binop number_ops[] = { PLUS, MINUS, TIMES, DIVIDE, MODULO };
constexpr binop cmp_ops[] = { EQUALS, LESS_EQUALS };
for(auto& op : number_ops) {
env->bind(op_name(op), binop_type, visibility::global);
env->set_mangled_name(op_name(op), op_action(op));
2020-09-10 14:50:03 -07:00
}
for(auto& op : cmp_ops) {
env->bind(op_name(op), cmp_type, visibility::global);
env->set_mangled_name(op_name(op), op_action(op));
2020-09-10 14:50:03 -07:00
}
2020-09-08 18:38:05 -07:00
2020-09-10 00:54:35 -07:00
env->bind("True", bool_type_app, visibility::global);
env->bind("False", bool_type_app, visibility::global);
}
void typecheck_program(
definition_group& defs,
type_mgr& mgr, type_env_ptr& env) {
prelude_types(defs, env);
2020-09-08 18:38:05 -07:00
std::set<std::string> free;
defs.find_free(free);
defs.typecheck(mgr, env);
#ifdef DEBUG_OUT
2020-09-08 18:38:05 -07:00
for(auto& pair : defs.env->names) {
std::cout << pair.first << ": ";
pair.second.type->print(mgr, std::cout);
std::cout << std::endl;
}
#endif
2020-09-08 18:38:05 -07:00
}
global_scope translate_program(definition_group& group) {
global_scope scope;
for(auto& data : group.defs_data) {
data.second->into_globals(scope);
}
for(auto& defn : group.defs_defn) {
auto& function = defn.second->into_global(scope);
function.body->env->parent->set_mangled_name(defn.first, function.name);
}
return scope;
}
void output_llvm(llvm_context& ctx, const std::string& filename) {
std::string targetTriple = llvm::sys::getDefaultTargetTriple();
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmParser();
llvm::InitializeNativeTargetAsmPrinter();
std::string error;
const llvm::Target* target =
llvm::TargetRegistry::lookupTarget(targetTriple, error);
if (!target) {
std::cerr << error << std::endl;
} else {
std::string cpu = "generic";
std::string features = "";
llvm::TargetOptions options;
std::unique_ptr<llvm::TargetMachine> targetMachine(
target->createTargetMachine(targetTriple, cpu, features,
options, llvm::Optional<llvm::Reloc::Model>()));
2020-09-08 18:38:05 -07:00
ctx.module.setDataLayout(targetMachine->createDataLayout());
ctx.module.setTargetTriple(targetTriple);
std::error_code ec;
llvm::raw_fd_ostream file(filename, ec, llvm::sys::fs::F_None);
if (ec) {
throw std::runtime_error("failed to open object file for writing");
2020-09-08 18:38:05 -07:00
} else {
llvm::CodeGenFileType type = llvm::CGFT_ObjectFile;
llvm::legacy::PassManager pm;
if (targetMachine->addPassesToEmitFile(pm, file, NULL, type)) {
throw std::runtime_error("failed to add passes to pass manager");
2020-09-08 18:38:05 -07:00
} else {
pm.run(ctx.module);
file.close();
}
}
}
}
2020-09-10 00:54:35 -07:00
void gen_llvm_internal_op(llvm_context& ctx, binop op) {
auto new_function = ctx.create_custom_function(op_action(op), 2);
std::vector<instruction_ptr> instructions;
instructions.push_back(instruction_ptr(new instruction_push(1)));
instructions.push_back(instruction_ptr(new instruction_eval()));
instructions.push_back(instruction_ptr(new instruction_push(1)));
instructions.push_back(instruction_ptr(new instruction_eval()));
instructions.push_back(instruction_ptr(new instruction_binop(op)));
instructions.push_back(instruction_ptr(new instruction_update(2)));
instructions.push_back(instruction_ptr(new instruction_pop(2)));
ctx.builder.SetInsertPoint(&new_function->getEntryBlock());
for(auto& instruction : instructions) {
instruction->gen_llvm(ctx, new_function);
}
ctx.builder.CreateRetVoid();
}
void gen_llvm_boolean_constructor(llvm_context& ctx, const std::string& s, bool b) {
auto new_function = ctx.create_custom_function(s, 0);
std::vector<instruction_ptr> instructions;
instructions.push_back(instruction_ptr(new instruction_pushint(b)));
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();
}
2020-09-08 18:38:05 -07:00
void gen_llvm(global_scope& scope) {
llvm_context ctx;
gen_llvm_internal_op(ctx, PLUS);
gen_llvm_internal_op(ctx, MINUS);
gen_llvm_internal_op(ctx, TIMES);
gen_llvm_internal_op(ctx, DIVIDE);
gen_llvm_internal_op(ctx, MODULO);
gen_llvm_internal_op(ctx, EQUALS);
gen_llvm_internal_op(ctx, LESS_EQUALS);
2020-09-10 00:54:35 -07:00
gen_llvm_boolean_constructor(ctx, "True", true);
gen_llvm_boolean_constructor(ctx, "False", false);
2020-09-08 18:38:05 -07:00
scope.generate_llvm(ctx);
#ifdef DEBUG_OUT
2020-09-08 18:38:05 -07:00
ctx.module.print(llvm::outs(), nullptr);
#endif
2020-09-08 18:38:05 -07:00
output_llvm(ctx, "program.o");
}
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "please enter a file to compile." << std::endl;
}
parse_driver driver(argv[1]);
if(!driver.run_parse()) {
std::cerr << "failed to open file " << argv[1] << std::endl;
exit(1);
}
2020-09-08 18:38:05 -07:00
type_mgr mgr;
type_env_ptr env(new type_env);
#ifdef DEBUG_OUT
for(auto& def_defn : driver.global_defs.defs_defn) {
2020-09-08 18:38:05 -07:00
std::cout << def_defn.second->name;
for(auto& param : def_defn.second->params) std::cout << " " << param;
std::cout << ":" << std::endl;
def_defn.second->body->print(1, std::cout);
2020-09-09 15:08:43 -07:00
std::cout << std::endl;
2020-09-08 18:38:05 -07:00
}
#endif
2020-09-08 18:38:05 -07:00
try {
typecheck_program(driver.global_defs, mgr, env);
global_scope scope = translate_program(driver.global_defs);
2020-09-08 18:38:05 -07:00
scope.compile();
gen_llvm(scope);
} catch(unification_error& err) {
err.pretty_print(std::cerr, driver, mgr);
2020-09-08 18:38:05 -07:00
} catch(type_error& err) {
err.pretty_print(std::cerr, driver);
} catch(std::runtime_error& err) {
std::cerr << err.what() << std::endl;
2020-09-08 18:38:05 -07:00
}
}