diff --git a/integration_tests/loop_02.py b/integration_tests/loop_02.py index f54754e9da..bfd7eca86c 100644 --- a/integration_tests/loop_02.py +++ b/integration_tests/loop_02.py @@ -75,6 +75,18 @@ def test_loop_03(): i += 1 assert k == 826 + i = 0 + if i == 0: + while i < 10: + j = 0 + if j == 0: + while j < 10: + k += 1 + if i == 9: + break + j += 1 + i += 1 + assert k == 917 def verify(): test_loop_01() diff --git a/src/libasr/codegen/wasm_to_x86.cpp b/src/libasr/codegen/wasm_to_x86.cpp index 9db9d2b4b6..1aaa00ffa2 100644 --- a/src/libasr/codegen/wasm_to_x86.cpp +++ b/src/libasr/codegen/wasm_to_x86.cpp @@ -34,6 +34,7 @@ class X86Visitor : public WASMDecoder, uint32_t cur_func_idx; std::vector if_unique_id; std::vector loop_unique_id; + uint32_t cur_nesting_length; int32_t last_vis_i32_const, last_last_vis_i32_const; std::unordered_map loc_to_str; @@ -43,6 +44,7 @@ class X86Visitor : public WASMDecoder, BaseWASMVisitor(code, 0U /* temporary offset */), m_a(m_a) { wasm_bytes.from_pointer_n(code.data(), code.size()); + cur_nesting_length = 0; } void visit_Unreachable() {} @@ -132,7 +134,8 @@ class X86Visitor : public WASMDecoder, void visit_Br(uint32_t label_index) { // Branch is used to jump to the `loop.head` or `loop.end`. - if (if_unique_id.size() - loop_unique_id.size() == label_index - 1) { + if (loop_unique_id.size() + if_unique_id.size() - cur_nesting_length + == label_index + 1) { // cycle/continue or loop.end m_a.asm_jmp_label(".loop.head_" + loop_unique_id.back()); } else { @@ -142,6 +145,8 @@ class X86Visitor : public WASMDecoder, } void visit_Loop() { + uint32_t prev_nesting_length = cur_nesting_length; + cur_nesting_length = loop_unique_id.size() + if_unique_id.size(); loop_unique_id.push_back(std::to_string(offset)); /* The loop statement starts with `loop.head`. The `loop.body` and @@ -162,6 +167,7 @@ class X86Visitor : public WASMDecoder, // end m_a.add_label(".loop.end_" + loop_unique_id.back()); loop_unique_id.pop_back(); + cur_nesting_length = prev_nesting_length; } void visit_If() {