Skip to content

Commit

Permalink
fix not being able to return values from non int functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dosisod committed Dec 7, 2020
1 parent aa96da1 commit 5e0c942
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion skull/llvm/flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

extern Scope *SCOPE;
extern LLVMValueRef CURRENT_FUNC;
extern LLVMValueRef MAIN_FUNC;
extern LLVMBuilderRef BUILDER;

bool node_to_llvm_ir(AstNode *);
Expand All @@ -31,7 +32,7 @@ void llvm_make_return(AstNode *node) {
SCOPE_FIND_VAR(found_var, node->token->next, var_name);
free(var_name);

if (found_var->type != &TYPE_INT) {
if (CURRENT_FUNC == MAIN_FUNC && found_var->type != &TYPE_INT) {
PANIC("returning non-int variable \"%s\" from main\n", { .var = found_var });
}

Expand Down
12 changes: 12 additions & 0 deletions test/sh/function/_non_int_return.sk.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; ModuleID = './test/sh/function/non_int_return.sk'
source_filename = "./test/sh/function/non_int_return.sk"

define i64 @main() {
entry:
ret i64 0
}

define private double @func() {
entry:
ret double 3.140000e+00
}
4 changes: 4 additions & 0 deletions test/sh/function/non_int_return.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
func() float {
ret := 3.14
return ret
}
1 change: 1 addition & 0 deletions test/sh/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ test "function" "func_with_scope.sk"
test "function" "multi_var_func.sk"
test "function" "nested_if.sk"
test "function" "exported.sk"
test "function" "non_int_return.sk"

test "flow" "if_empty_body.sk"
test "flow" "if_false.sk"
Expand Down

0 comments on commit 5e0c942

Please sign in to comment.