Update the compiler to leave the stack clean

This commit is contained in:
Danila Fedorin 2020-01-27 20:29:01 -08:00
parent 0bc9ae320b
commit 28e81cc9bd
2 changed files with 11 additions and 4 deletions

View File

@ -1,6 +1,7 @@
#include "definition.hpp" #include "definition.hpp"
#include "error.hpp" #include "error.hpp"
#include "ast.hpp" #include "ast.hpp"
#include "instruction.hpp"
#include "llvm_context.hpp" #include "llvm_context.hpp"
#include <llvm/IR/DerivedTypes.h> #include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Function.h> #include <llvm/IR/Function.h>
@ -102,11 +103,15 @@ void definition_data::gen_llvm_first(llvm_context& ctx) {
for(auto& constructor : constructors) { for(auto& constructor : constructors) {
auto new_function = auto new_function =
ctx.create_custom_function(constructor->name, constructor->types.size()); 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()); ctx.builder.SetInsertPoint(&new_function->getEntryBlock());
ctx.create_pack(new_function, for (auto& instruction : instructions) {
ctx.create_size(constructor->types.size()), instruction->gen_llvm(ctx, new_function);
ctx.create_i8(constructor->tag) }
);
ctx.builder.CreateRetVoid(); ctx.builder.CreateRetVoid();
} }
} }

View File

@ -75,6 +75,8 @@ void gen_llvm_internal_op(llvm_context& ctx, binop op) {
instructions.push_back(instruction_ptr(new instruction_push(1))); instructions.push_back(instruction_ptr(new instruction_push(1)));
instructions.push_back(instruction_ptr(new instruction_eval())); instructions.push_back(instruction_ptr(new instruction_eval()));
instructions.push_back(instruction_ptr(new instruction_binop(op))); 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()); ctx.builder.SetInsertPoint(&new_function->getEntryBlock());
for(auto& instruction : instructions) { for(auto& instruction : instructions) {
instruction->gen_llvm(ctx, new_function); instruction->gen_llvm(ctx, new_function);