It seems this condition (if_unique_id.size() - loop_unique_id.size() == label_index - 1) might not be perfect to determine to continue or end the loop. Consider the following example where we have a loop inside an if construct:
def main0():
x: i32
x = (2+3)*5
j: i32 = 0
if True:
while x > 0:
j += 2
x -= 1
print(x)
print(j)
main0()
Output:
(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ lpython examples/expr2.py
0
50
(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ lpython examples/expr2.py --backend wasm_x86 -o loop
(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ ./loop
24
2
(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$
The loop in this case just executes once and then exits. This is because in this case (when visit_Br() is visited) the if count would be 2 and loop count would be 1. The label_index would be 1. The condition would be 2 - 1 == 1 - 1, that is 1 == 0, which is not true and hence the loop exits.
I think we might need to simulate how wasm works in x86. We might need to use same label indexes (or same label index space) for if's and loop's (just like wasm does).
Originally posted by @Shaikh-Ubaid in #1290 (comment)
It seems this condition
(if_unique_id.size() - loop_unique_id.size() == label_index - 1)might not be perfect to determine tocontinueorendtheloop. Consider the following example where we have aloopinside anifconstruct:Output:
The
loopin this case just executes once and then exits. This is because in this case (whenvisit_Br()is visited) theifcount would be2andloopcount would be1. Thelabel_indexwould be1. The condition would be2 - 1 == 1 - 1, that is1 == 0, which is nottrueand hence theloopexits.I think we might need to simulate how
wasmworks inx86. We might need to use same label indexes (or same label index space) forif's andloop's (just likewasmdoes).Originally posted by @Shaikh-Ubaid in #1290 (comment)