From 28e81cc9bdd3be3dd02bd64277b86edd75d88b16 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 27 Jan 2020 20:29:01 -0800 Subject: [PATCH] Update the compiler to leave the stack clean --- 09/definition.cpp | 13 +++++++++---- 09/main.cpp | 2 ++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/09/definition.cpp b/09/definition.cpp index fe116c8..9c100b0 100644 --- a/09/definition.cpp +++ b/09/definition.cpp @@ -1,6 +1,7 @@ #include "definition.hpp" #include "error.hpp" #include "ast.hpp" +#include "instruction.hpp" #include "llvm_context.hpp" #include #include @@ -102,11 +103,15 @@ 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()); + std::vector 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.create_pack(new_function, - ctx.create_size(constructor->types.size()), - ctx.create_i8(constructor->tag) - ); + for (auto& instruction : instructions) { + instruction->gen_llvm(ctx, new_function); + } ctx.builder.CreateRetVoid(); } } diff --git a/09/main.cpp b/09/main.cpp index 3f50c32..2b01f61 100644 --- a/09/main.cpp +++ b/09/main.cpp @@ -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_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);