diff --git a/code/compiler/09/instruction.cpp b/code/compiler/09/instruction.cpp index 23f4258..79c8a2f 100644 --- a/code/compiler/09/instruction.cpp +++ b/code/compiler/09/instruction.cpp @@ -155,7 +155,7 @@ void instruction_eval::print(int indent, std::ostream& to) const { } void instruction_eval::gen_llvm(llvm_context& ctx, Function* f) const { - ctx.create_push(f, ctx.create_eval(ctx.create_pop(f))); + ctx.create_unwind(f); } void instruction_alloc::print(int indent, std::ostream& to) const { diff --git a/code/compiler/09/llvm_context.cpp b/code/compiler/09/llvm_context.cpp index b2982f9..18ce6e4 100644 --- a/code/compiler/09/llvm_context.cpp +++ b/code/compiler/09/llvm_context.cpp @@ -145,6 +145,12 @@ void llvm_context::create_functions() { "eval", &module ); + functions["unwind"] = Function::Create( + FunctionType::get(void_type, { stack_ptr_type }, false), + Function::LinkageTypes::ExternalLinkage, + "unwind", + &module + ); } ConstantInt* llvm_context::create_i8(int8_t i) { @@ -199,6 +205,11 @@ Value* llvm_context::create_eval(Value* e) { return builder.CreateCall(eval_f, { e }); } +void llvm_context::create_unwind(Function* f) { + auto unwind_f = functions.at("unwind"); + builder.CreateCall(unwind_f, { f->args().begin() }); +} + Value* llvm_context::unwrap_num(Value* v) { auto num_ptr_type = PointerType::getUnqual(struct_types.at("node_num")); auto cast = builder.CreatePointerCast(v, num_ptr_type); diff --git a/code/compiler/09/llvm_context.hpp b/code/compiler/09/llvm_context.hpp index ca75dca..7c9a6a6 100644 --- a/code/compiler/09/llvm_context.hpp +++ b/code/compiler/09/llvm_context.hpp @@ -52,6 +52,7 @@ struct llvm_context { void create_alloc(llvm::Function*, llvm::Value*); llvm::Value* create_eval(llvm::Value*); + void create_unwind(llvm::Function*); llvm::Value* unwrap_num(llvm::Value*); llvm::Value* create_num(llvm::Value*);