diff options
| author | gingerBill <bill@gingerbill.org> | 2020-06-10 14:53:35 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2020-06-10 14:53:35 +0100 |
| commit | 99944f3b02dd8aef610bdc19e31a790939e29cdd (patch) | |
| tree | 6ef3f793c9c959434c09c6981e4eb5d185bf62f0 /src/llvm_backend.cpp | |
| parent | a9295d33ab285cfd4d430f998d8273ebb2649f3a (diff) | |
Improve behaviour of `return` with named results to aid with `defer` statements
Diffstat (limited to 'src/llvm_backend.cpp')
| -rw-r--r-- | src/llvm_backend.cpp | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 09d7c78de..7b1da7a50 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -2434,7 +2434,6 @@ void lb_begin_procedure_body(lbProcedure *p) { if (p->type->Proc.has_named_results) { GB_ASSERT(p->type->Proc.result_count > 0); TypeTuple *results = &p->type->Proc.results->Tuple; - LLVMValueRef return_ptr = LLVMGetParam(p->value, 0); isize result_index = 0; @@ -4192,6 +4191,15 @@ void lb_build_stmt(lbProcedure *p, Ast *node) { res = lb_build_expr(p, rs->results[0]); res = lb_emit_conv(p, res, e->type); } + if (p->type->Proc.has_named_results) { + // NOTE(bill): store the named values before returning + if (e->token.string != "") { + lbValue *found = map_get(&p->module->values, hash_entity(e)); + GB_ASSERT(found != nullptr); + lb_emit_store(p, *found, res); + } + } + } else { auto results = array_make<lbValue>(heap_allocator(), 0, return_count); @@ -4221,6 +4229,23 @@ void lb_build_stmt(lbProcedure *p, Ast *node) { GB_ASSERT(results.count == return_count); + if (p->type->Proc.has_named_results) { + // NOTE(bill): store the named values before returning + for_array(i, p->type->Proc.results->Tuple.variables) { + Entity *e = p->type->Proc.results->Tuple.variables[i]; + if (e->kind != Entity_Variable) { + continue; + } + + if (e->token.string == "") { + continue; + } + lbValue *found = map_get(&p->module->values, hash_entity(e)); + GB_ASSERT(found != nullptr); + lb_emit_store(p, *found, results[i]); + } + } + Type *ret_type = p->type->Proc.results; // NOTE(bill): Doesn't need to be zero because it will be initialized in the loops res = lb_add_local_generated(p, ret_type, false).addr; |