From 417c0addcafe8bfd3d9305ab7d2fac207ad54700 Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Sat, 12 Oct 2019 02:41:50 +0100 Subject: Implement recursive definition support Enables definition of a recursive function through defining a base case using an `if` statement. Previously, doing this would produce compilable code, but running the product executable would cause a stack overflow, since the executable would attempt to evaluate both sides of the branch. Making the `if` statement a built-in part of the language is a bit crude, but is simple and works. Maybe, if time allows, this will be revisited. --- basic.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ simple.int | 3 ++- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/basic.c b/basic.c index c82bab3..29ce672 100644 --- a/basic.c +++ b/basic.c @@ -95,6 +95,7 @@ void print_sexpr(sexpr *se) { } LLVMModuleRef module; +LLVMValueRef function; LLVMBuilderRef builder; LLVMTypeRef print_int_t; @@ -216,6 +217,8 @@ LLVMValueRef codegen_definition(sexpr *se) { LLVMBasicBlockRef func_b = LLVMAppendBasicBlock(func_p, "entry"); LLVMPositionBuilderAtEnd(builder, func_b); + function = func_p; + scope_add_entry(sc, func_id, func_p, func_t); scope_push_layer(&sc); @@ -305,6 +308,56 @@ LLVMValueRef codegen_invocation(sexpr *se) { return res; } +LLVMValueRef codegen_conditional(sexpr *se) { + assert(se); + assert(se->type == BRANCH); + + sexpr *cond_ast = se->contents.n.l; + assert(cond_ast); + + se = se->contents.n.r; + assert(se); + assert(se->type == BRANCH); + + sexpr *then_ast = se->contents.n.l; + assert(then_ast); + + se = se->contents.n.r; + assert(se); + assert(se->type == BRANCH); + + sexpr *else_ast = se->contents.n.l; + assert(else_ast); + + assert(!se->contents.n.r); + + LLVMBasicBlockRef then_b = LLVMAppendBasicBlock(function, "then"); + LLVMBasicBlockRef else_b = LLVMAppendBasicBlock(function, "else"); + LLVMBasicBlockRef done_b = LLVMAppendBasicBlock(function, "done"); + + LLVMValueRef llvm_zero_v = LLVMConstInt(llvm_int_t, 0, 0); + LLVMValueRef cond_res = _codegen(cond_ast); + LLVMValueRef cond_v = + LLVMBuildICmp(builder, LLVMIntNE, cond_res, llvm_zero_v, "neq_0"); + LLVMBuildCondBr(builder, cond_v, then_b, else_b); + + LLVMPositionBuilderAtEnd(builder, then_b); + LLVMValueRef then_v = _codegen(then_ast); + LLVMBuildBr(builder, done_b); + + LLVMPositionBuilderAtEnd(builder, else_b); + LLVMValueRef else_v = _codegen(else_ast); + LLVMBuildBr(builder, done_b); + + LLVMPositionBuilderAtEnd(builder, done_b); + LLVMValueRef phi_p = LLVMBuildPhi(builder, llvm_int_t, "if_res"); + LLVMBasicBlockRef phi_bs[] = { then_b, else_b }; + LLVMValueRef phi_vs[] = { then_v, else_v }; + LLVMAddIncoming(phi_p, phi_vs, phi_bs, 2); + + return phi_p; +} + LLVMValueRef codegen_branch(sexpr *se) { debug("codegenning branch @%p\n", (void *) se); @@ -318,6 +371,8 @@ LLVMValueRef codegen_branch(sexpr *se) { if (l->type == ID) { if (!strcmp(l->contents.s, "def")) { res = codegen_definition(r); + } else if (!strcmp(l->contents.s, "if")) { + res = codegen_conditional(r); } else { res = codegen_invocation(se); } @@ -371,12 +426,6 @@ void prologue() { print_str_t = LLVMFunctionType(LLVMInt32Type(), print_str_params, 1, 0); print_str_p = LLVMAddFunction(module, "debug_str", print_str_t); scope_add_entry(sc, "debug_str", print_str_p, print_str_t); - - /* add int_if */ - LLVMTypeRef int_if_params[] = { llvm_int_t, llvm_int_t, llvm_int_t }; - LLVMTypeRef int_if_t = LLVMFunctionType(llvm_int_t, int_if_params, 3, 0); - LLVMValueRef int_if_p = LLVMAddFunction(module, "int_if", int_if_t); - scope_add_entry(sc, "int_if", int_if_p, int_if_t); } void epilogue() { diff --git a/simple.int b/simple.int index ad6d789..fe34eab 100644 --- a/simple.int +++ b/simple.int @@ -1,2 +1,3 @@ +[def (INT fact) [(INT n)] [if n [* n [fact [- n 1]]] 1]] [def (INT double) [(INT x)] [* 2 x]] -[def (INT main) [] [debug_int [+ 3 [double 4]]]] +[def (INT main) [] [debug_int [fact 10]]] -- cgit v1.2.1