bugfix: nested do-blocks with indirect return statments
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 18 Apr 2023 01:08:18 +0000 (20:08 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 18 Apr 2023 01:08:18 +0000 (20:08 -0500)
compiler/src/wasm_emit.c
core/container/iter.onyx

index 7d2e8bcef40bbccbb294c75efd0db186b60ecf79..445b9f2f6b36c068bc3c303f8bdd6829be47f8e3 100644 (file)
@@ -3769,8 +3769,18 @@ EMIT_FUNC(return, AstReturn* ret) {
     AstLocal* result_destination = NULL;
     i64 jump_label = get_structured_jump_label(mod, Jump_Type_Return, ret->count + 1);
 
+    //
+    // If this is return statement if an inner return of a `do` block,
+    // we have to get the result destination out of the return location stack.
+    // This can be computed as the -ret->count element.
+    //
+    //      2 | return from second do
+    //      1 | return from first do
+    //      0 | return from function
+    //
     if (bh_arr_length(mod->return_location_stack) > 0 && jump_label >= 0) {
-        result_destination = bh_arr_last(mod->return_location_stack);
+        i32 len = bh_arr_length(mod->return_location_stack);
+        result_destination = mod->return_location_stack[len - ret->count - 1];
     }
 
     // If we have an expression to return, we see if it should be placed on the linear memory stack, or the WASM stack.
index d76dcd9dd66dfcecab2a314dc854944fd2653833..4c8b0c8461ebcf3ea63efb93671bc0f820c319c0 100644 (file)
@@ -31,6 +31,7 @@ use core.intrinsics.types {type_is_struct}
     count :: count;
     some :: some;
     every :: every;
+    collect :: to_array;
 }