Get awful LLVM code generation working.

This commit is contained in:
2019-06-12 01:38:27 -07:00
parent e579078f73
commit 37f5e53a59
10 changed files with 643 additions and 17 deletions

View File

@@ -86,4 +86,102 @@ namespace lily {
os << "jump";
return os;
}
void instruction_slide::gen_llvm(llvm_context& ctx) {
llvm::Value* stack = ctx.get_current_function()->arg_begin();
builder.CreateCall(stack_slide_func, { stack, get_int32_constant(amount) });
}
void instruction_alloc::gen_llvm(llvm_context& ctx) {
llvm::Value* stack = ctx.get_current_function()->arg_begin();
builder.CreateCall(stack_alloc_func, { stack, get_int32_constant(amount) });
}
void instruction_pop::gen_llvm(llvm_context& ctx) {
llvm::Value* stack = ctx.get_current_function()->arg_begin();
builder.CreateCall(stack_popn_func, { stack, get_int32_constant(amount) });
}
void instruction_unwind::gen_llvm(llvm_context& ctx) {
}
void instruction_push_global::gen_llvm(llvm_context& ctx) {
llvm::Value* stack = ctx.get_current_function()->arg_begin();
llvm::Value* new_node = builder.CreateCall(malloc_node_global_func,
{ get_int8_constant(2), ctx.get_supercombinator(name) }, "temp");
// TODO get arity
builder.CreateCall(stack_push_func, { stack, new_node });
}
void instruction_push_int::gen_llvm(llvm_context& ctx) {
llvm::Value* stack = ctx.get_current_function()->arg_begin();
llvm::Value* new_node = builder.CreateCall(malloc_node_num_func,
{ get_int32_constant(value) });
builder.CreateCall(stack_push_func, { stack, new_node });
}
void instruction_push_str::gen_llvm(llvm_context& ctx) {
}
void instruction_push::gen_llvm(llvm_context& ctx) {
llvm::Value* stack = ctx.get_current_function()->arg_begin();
llvm::Value* peeked = builder.CreateCall(stack_peek_func, { stack, get_int32_constant(offset) }, "temp");
builder.CreateCall(stack_push_func, { stack, peeked });
}
void instruction_mkapp::gen_llvm(llvm_context& ctx) {
llvm::Value* stack = ctx.get_current_function()->arg_begin();
llvm::Value* func = builder.CreateCall(stack_pop_func, { stack }, "temp");
llvm::Value* param = builder.CreateCall(stack_pop_func, { stack }, "temp");
llvm::Value* app_node = builder.CreateCall(malloc_node_app_func, { func, param }, "temp");
builder.CreateCall(stack_push_func, { stack, app_node });
}
void instruction_eval::gen_llvm(llvm_context& ctx) {
// ??
}
void instruction_op::gen_llvm(llvm_context& ctx) {
llvm::Value* stack = ctx.get_current_function()->arg_begin();
llvm::Value* param1 = builder.CreateCall(stack_pop_func, { stack }, "temp");
llvm::Value* param2 = builder.CreateCall(stack_pop_func, { stack }, "temp");
llvm::Value* param1_node_num = builder.CreatePointerCast(param1, llvm::PointerType::getUnqual(node_num_type), "temp");
llvm::Value* param2_node_num = builder.CreatePointerCast(param2, llvm::PointerType::getUnqual(node_num_type), "temp");
llvm::Value* param1_intptr = builder.CreateGEP(param1_node_num, { get_int32_constant(0), get_int32_constant(1) }, "temp");
llvm::Value* param2_intptr = builder.CreateGEP(param2_node_num, { get_int32_constant(0), get_int32_constant(1) }, "temp");
llvm::Value* param1_int = builder.CreateLoad(llvm::IntegerType::get(context, 32), param1_intptr, "temp");
llvm::Value* param2_int = builder.CreateLoad(llvm::IntegerType::get(context, 32), param2_intptr, "temp");
llvm::Value* op_result;
switch(op) {
case binop::add: op_result = builder.CreateAdd(param2_int, param1_int, "temp"); break;
case binop::subtract: op_result = builder.CreateSub(param2_int, param1_int, "temp"); break;
case binop::times: op_result = builder.CreateMul(param2_int, param1_int, "temp"); break;
case binop::divide: op_result = builder.CreateSDiv(param2_int, param1_int, "temp"); break;
}
llvm::Value* new_node = builder.CreateCall(malloc_node_num_func, { op_result }, "temp");
builder.CreateCall(stack_push_func, { stack, new_node });
}
void instruction_cond::gen_llvm(llvm_context& ctx) {
}
void instruction_update::gen_llvm(llvm_context& ctx) {
llvm::Value* stack = ctx.get_current_function()->arg_begin();
builder.CreateCall(stack_update_func, { stack, get_int32_constant(offset) });
}
void instruction_pack::gen_llvm(llvm_context& ctx) {
}
void instruction_split::gen_llvm(llvm_context& ctx) {
}
void instruction_jump::gen_llvm(llvm_context& ctx) {
}
}