Skip to content

Commit ebf8b44

Browse files
Merge pull request #1299 from Thirumalai-Shaktivel/i_1298
Fixes #1298
2 parents 7feb501 + f137f1f commit ebf8b44

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

integration_tests/loop_02.py

+12
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ def test_loop_03():
7575
i += 1
7676
assert k == 826
7777

78+
i = 0
79+
if i == 0:
80+
while i < 10:
81+
j = 0
82+
if j == 0:
83+
while j < 10:
84+
k += 1
85+
if i == 9:
86+
break
87+
j += 1
88+
i += 1
89+
assert k == 917
7890

7991
def verify():
8092
test_loop_01()

src/libasr/codegen/wasm_to_x86.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class X86Visitor : public WASMDecoder<X86Visitor>,
3434
uint32_t cur_func_idx;
3535
std::vector<std::string> if_unique_id;
3636
std::vector<std::string> loop_unique_id;
37+
uint32_t cur_nesting_length;
3738
int32_t last_vis_i32_const, last_last_vis_i32_const;
3839
std::unordered_map<int32_t, std::string> loc_to_str;
3940

@@ -43,6 +44,7 @@ class X86Visitor : public WASMDecoder<X86Visitor>,
4344
BaseWASMVisitor(code, 0U /* temporary offset */),
4445
m_a(m_a) {
4546
wasm_bytes.from_pointer_n(code.data(), code.size());
47+
cur_nesting_length = 0;
4648
}
4749

4850
void visit_Unreachable() {}
@@ -132,7 +134,8 @@ class X86Visitor : public WASMDecoder<X86Visitor>,
132134

133135
void visit_Br(uint32_t label_index) {
134136
// Branch is used to jump to the `loop.head` or `loop.end`.
135-
if (if_unique_id.size() - loop_unique_id.size() == label_index - 1) {
137+
if (loop_unique_id.size() + if_unique_id.size() - cur_nesting_length
138+
== label_index + 1) {
136139
// cycle/continue or loop.end
137140
m_a.asm_jmp_label(".loop.head_" + loop_unique_id.back());
138141
} else {
@@ -142,6 +145,8 @@ class X86Visitor : public WASMDecoder<X86Visitor>,
142145
}
143146

144147
void visit_Loop() {
148+
uint32_t prev_nesting_length = cur_nesting_length;
149+
cur_nesting_length = loop_unique_id.size() + if_unique_id.size();
145150
loop_unique_id.push_back(std::to_string(offset));
146151
/*
147152
The loop statement starts with `loop.head`. The `loop.body` and
@@ -162,6 +167,7 @@ class X86Visitor : public WASMDecoder<X86Visitor>,
162167
// end
163168
m_a.add_label(".loop.end_" + loop_unique_id.back());
164169
loop_unique_id.pop_back();
170+
cur_nesting_length = prev_nesting_length;
165171
}
166172

167173
void visit_If() {

0 commit comments

Comments
 (0)