Skip to content

Commit 580c1f3

Browse files
[WASM_x86] Implement Loop statement
1 parent 1c79a6b commit 580c1f3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/libasr/codegen/asr_to_wasm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,7 @@ class ASRToWASMVisitor : public ASR::BaseVisitor<ASRToWASMVisitor> {
20322032
m_code_section, m_al,
20332033
nesting_level -
20342034
cur_loop_nesting_level); // emit_branch and label the loop
2035+
wasm::emit_b8(m_code_section, m_al, 0x05); // starting of else
20352036
wasm::emit_expr_end(m_code_section, m_al); // end if
20362037

20372038
nesting_level--;

src/libasr/codegen/wasm_to_x86.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,34 @@ class X86Visitor : public WASMDecoder<X86Visitor>,
129129

130130
void visit_EmtpyBlockType() {}
131131

132+
void visit_Br(uint32_t /*label_index*/) {
133+
// Branch is used to jump to the `loop.head`.
134+
m_a.asm_jmp_label(".loop.head_" + unique_id.rbegin()[1]);
135+
}
136+
137+
void visit_Loop() {
138+
unique_id.push_back(std::to_string(offset));
139+
/*
140+
The loop statement starts with `loop.head`. The `loop.body` and
141+
`loop.branch` are enclosed within the `if.block`. If the condition
142+
fails, the loop is exited through `else.block`.
143+
.head
144+
.If
145+
# Statements
146+
.Br
147+
.Else
148+
.endIf
149+
.end
150+
*/
151+
m_a.add_label(".loop.head_" + unique_id.back());
152+
{
153+
decode_instructions();
154+
}
155+
// end
156+
m_a.add_label(".loop.end_" + unique_id.back());
157+
unique_id.pop_back();
158+
}
159+
132160
void visit_If() {
133161
unique_id.push_back(std::to_string(offset));
134162
// `eax` contains the logical value (true = 1, false = 0)

0 commit comments

Comments
 (0)