-
Notifications
You must be signed in to change notification settings - Fork 171
Implement if-else in wasm_to_x86 backend #1264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
19309b8
2aea75b
03fbb06
8fb624b
4b09191
bfa5887
4093db1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
def Test_if_01(): | ||
if True: | ||
print(1) | ||
|
||
if False: | ||
print(0) | ||
|
||
if 1 < 0: | ||
print(0) | ||
else: | ||
print(1) | ||
|
||
if 1 > 0: | ||
print(1) | ||
else: | ||
print(0) | ||
|
||
if 1 < 0: | ||
print(1) | ||
elif 1 > 0: | ||
print(1) | ||
else: | ||
print(0) | ||
|
||
def Test_if_02(): | ||
if True: | ||
print(1) | ||
if True: | ||
print(2) | ||
if False: | ||
print(3) | ||
elif True: | ||
print(4) | ||
else: | ||
print(5) | ||
else: | ||
print(6) | ||
|
||
if True: | ||
print(7) | ||
if False: | ||
print(8) | ||
if False: | ||
print(9) | ||
else: | ||
print(10) | ||
else: | ||
if True: | ||
print(11) | ||
else: | ||
print(12) | ||
print(13) | ||
print(14) | ||
|
||
def Verify(): | ||
Test_if_01() | ||
Test_if_02() | ||
|
||
Verify() |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,7 @@ class X86Visitor : public WASMDecoder<X86Visitor>, | |||||||
public: | ||||||||
X86Assembler &m_a; | ||||||||
uint32_t cur_func_idx; | ||||||||
std::vector<uint32_t> unique_id; | ||||||||
|
||||||||
X86Visitor(X86Assembler &m_a, Allocator &al, | ||||||||
diag::Diagnostics &diagonostics, Vec<uint8_t> &code) | ||||||||
|
@@ -68,6 +69,27 @@ class X86Visitor : public WASMDecoder<X86Visitor>, | |||||||
} | ||||||||
} | ||||||||
|
||||||||
void visit_EmtpyBlockType() {} | ||||||||
|
||||||||
void visit_If() { | ||||||||
unique_id.push_back(offset); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
m_a.asm_pop_r32(X86Reg::eax); | ||||||||
m_a.asm_cmp_r32_imm8(LFortran::X86Reg::eax, 1); | ||||||||
m_a.asm_je_label(".then_" + std::to_string(unique_id.back())); | ||||||||
m_a.asm_jmp_label(".else_" + std::to_string(unique_id.back())); | ||||||||
m_a.add_label(".then_" + std::to_string(unique_id.back())); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For later (but probably quite soon): we should not be using strings for labels, but just unique integers. The unique integer can just be a counter in this visitor, just an integer, see above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (we might need to have a stack of unique ids...) |
||||||||
{ | ||||||||
decode_instructions(); | ||||||||
} | ||||||||
m_a.add_label(".endif_" + std::to_string(unique_id.back())); | ||||||||
unique_id.pop_back(); | ||||||||
} | ||||||||
|
||||||||
void visit_Else() { | ||||||||
m_a.asm_jmp_label(".endif_" + std::to_string(unique_id.back())); | ||||||||
m_a.add_label(".else_" + std::to_string(unique_id.back())); | ||||||||
} | ||||||||
|
||||||||
void visit_LocalGet(uint32_t localidx) { | ||||||||
X86Reg base = X86Reg::ebp; | ||||||||
int no_of_params = | ||||||||
|
@@ -230,6 +252,9 @@ Result<int> wasm_to_x86(Vec<uint8_t> &wasm_bytes, Allocator &al, | |||||||
.count(); | ||||||||
} | ||||||||
|
||||||||
//! Helpful for debugging | ||||||||
// std::cout << x86_visitor.m_a.get_asm() << std::endl; | ||||||||
|
||||||||
if (time_report) { | ||||||||
std::cout << "Codegen Time report:" << std::endl; | ||||||||
std::cout << "Decode wasm: " << std::setw(5) << time_decode_wasm | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Later: