-
Notifications
You must be signed in to change notification settings - Fork 176
Implement Loop statement #1290
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
Merged
Merged
Implement Loop statement #1290
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1c79a6b
[WASM_x86] Add description to `if` and `compare` visitors
Thirumalai-Shaktivel 580c1f3
[WASM_x86] Implement Loop statement
Thirumalai-Shaktivel bc64ef4
[WASM_x86] Use different variable to store loop_unique_id
Thirumalai-Shaktivel 5f38adc
[WASM_x86] Implement Break and Continue statements
Thirumalai-Shaktivel b7faf79
Add loop tests and enable wasm_x86 tests in CMakeLists
Thirumalai-Shaktivel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| from ltypes import i32 | ||
| def test_loop_01(): | ||
| i: i32 = 0 | ||
| j: i32 = 0 | ||
|
|
||
| while False: | ||
| assert False | ||
|
|
||
| while i < 0: | ||
| assert False | ||
|
|
||
| while i < 10: | ||
| i += 1 | ||
| assert i == 10 | ||
|
|
||
| while i < 20: | ||
| while i < 15: | ||
| i += 1 | ||
| i += 1 | ||
| assert i == 20 | ||
|
|
||
| for i in range(5): | ||
| assert i == j | ||
| j += 1 | ||
|
|
||
| def test_loop_02(): | ||
| i: i32 = 0 | ||
| j: i32 = 0 | ||
|
|
||
| j = 0 | ||
| for i in range(10, 0, -1): | ||
| j = j + i | ||
| assert j == 55 | ||
|
|
||
| for i in range(5): | ||
| if i == 3: | ||
| break | ||
| assert i == 3 | ||
|
|
||
| j = 0 | ||
| for i in range(5): | ||
| if i == 3: | ||
| continue | ||
| j += 1 | ||
| assert j == 4 | ||
|
|
||
| def test_loop_03(): | ||
| i: i32 = 0 | ||
| j: i32 = 0 | ||
| k: i32 = 0 | ||
| while i < 10: | ||
| j = 0 | ||
| while j < 10: | ||
| k += 1 | ||
| if i == 0: | ||
| if j == 3: | ||
| continue | ||
| else: | ||
| j += 1 | ||
| j += 1 | ||
| i += 1 | ||
| assert k == 95 | ||
|
|
||
| i = 0; j = 0; k = 0 | ||
| while i < 10: | ||
| j = 0 | ||
| while j < 10: | ||
| k += i + j | ||
| if i == 5: | ||
| if j == 4: | ||
| break | ||
| else: | ||
| j += 1 | ||
| j += 1 | ||
| i += 1 | ||
| assert k == 826 | ||
|
|
||
|
|
||
| def verify(): | ||
| test_loop_01() | ||
| test_loop_02() | ||
| test_loop_03() | ||
|
|
||
| verify() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,8 @@ class X86Visitor : public WASMDecoder<X86Visitor>, | |
| public: | ||
| X86Assembler &m_a; | ||
| uint32_t cur_func_idx; | ||
| std::vector<std::string> unique_id; | ||
| std::vector<std::string> if_unique_id; | ||
| std::vector<std::string> loop_unique_id; | ||
| int32_t last_vis_i32_const, last_last_vis_i32_const; | ||
| std::unordered_map<int32_t, std::string> loc_to_str; | ||
|
|
||
|
|
@@ -129,23 +130,59 @@ class X86Visitor : public WASMDecoder<X86Visitor>, | |
|
|
||
| void visit_EmtpyBlockType() {} | ||
|
|
||
| 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) { | ||
| // cycle/continue or loop.end | ||
| m_a.asm_jmp_label(".loop.head_" + loop_unique_id.back()); | ||
| } else { | ||
| // exit/break | ||
| m_a.asm_jmp_label(".loop.end_" + loop_unique_id.back()); | ||
| } | ||
| } | ||
|
|
||
| void visit_Loop() { | ||
| loop_unique_id.push_back(std::to_string(offset)); | ||
| /* | ||
| The loop statement starts with `loop.head`. The `loop.body` and | ||
| `loop.branch` are enclosed within the `if.block`. If the condition | ||
| fails, the loop is exited through `else.block`. | ||
| .head | ||
| .If | ||
| # Statements | ||
| .Br | ||
| .Else | ||
| .endIf | ||
| .end | ||
| */ | ||
|
Comment on lines
+146
to
+157
Collaborator
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. Great note, Thirumalai !. It explains the |
||
| m_a.add_label(".loop.head_" + loop_unique_id.back()); | ||
| { | ||
| decode_instructions(); | ||
| } | ||
| // end | ||
| m_a.add_label(".loop.end_" + loop_unique_id.back()); | ||
| loop_unique_id.pop_back(); | ||
| } | ||
|
|
||
| void visit_If() { | ||
| unique_id.push_back(std::to_string(offset)); | ||
| if_unique_id.push_back(std::to_string(offset)); | ||
| // `eax` contains the logical value (true = 1, false = 0) | ||
| // of the if condition | ||
| m_a.asm_pop_r32(X86Reg::eax); | ||
| m_a.asm_cmp_r32_imm8(LFortran::X86Reg::eax, 1); | ||
| m_a.asm_je_label(".then_" + unique_id.back()); | ||
| m_a.asm_jmp_label(".else_" + unique_id.back()); | ||
| m_a.add_label(".then_" + unique_id.back()); | ||
| m_a.asm_je_label(".then_" + if_unique_id.back()); | ||
| m_a.asm_jmp_label(".else_" + if_unique_id.back()); | ||
| m_a.add_label(".then_" + if_unique_id.back()); | ||
| { | ||
| decode_instructions(); | ||
| } | ||
| m_a.add_label(".endif_" + unique_id.back()); | ||
| unique_id.pop_back(); | ||
| m_a.add_label(".endif_" + if_unique_id.back()); | ||
| if_unique_id.pop_back(); | ||
| } | ||
|
|
||
| void visit_Else() { | ||
| m_a.asm_jmp_label(".endif_" + unique_id.back()); | ||
| m_a.add_label(".else_" + unique_id.back()); | ||
| m_a.asm_jmp_label(".endif_" + if_unique_id.back()); | ||
| m_a.add_label(".else_" + if_unique_id.back()); | ||
| } | ||
|
|
||
| void visit_LocalGet(uint32_t localidx) { | ||
|
|
@@ -225,6 +262,7 @@ class X86Visitor : public WASMDecoder<X86Visitor>, | |
| std::string label = std::to_string(offset); | ||
| m_a.asm_pop_r32(X86Reg::ebx); | ||
| m_a.asm_pop_r32(X86Reg::eax); | ||
| // `eax` and `ebx` contain the left and right operands, respectively | ||
| m_a.asm_cmp_r32_r32(X86Reg::eax, X86Reg::ebx); | ||
| if (compare_op == "Eq") { | ||
| m_a.asm_je_label(".compare_1" + label); | ||
|
|
@@ -241,6 +279,8 @@ class X86Visitor : public WASMDecoder<X86Visitor>, | |
| } else { | ||
| throw CodeGenError("Comparison operator not implemented"); | ||
| } | ||
| // if the `compare` condition in `true`, jump to compare_1 | ||
| // and assign `1` else assign `0` | ||
| m_a.asm_push_imm8(0); | ||
| m_a.asm_jmp_label(".compare.end_" + label); | ||
| m_a.add_label(".compare_1" + label); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This
if-elsecondition inside thevisit_Br()is not clear to me. @Thirumalai-Shaktivel, please, could you possibly share how thisif-elseworks and chooses whether to iterate the loop or end it?Uh oh!
There was an error while loading. Please reload this page.
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.
Consider this example:
Output printed using:
We have 2 loops and 2 if's. But in the backend, we get 2 loops and 4 ifs, right?
The first br comes from the continue, if we subtract 4 if's with 2 loops: we get the actual 2 if's. the label_index is 3, we do 3 - 1 = 2: jump to head and So on...
But if the
breakis encountered thelabel_indexis 2, So we jump to the end of the loopAny doubts, please ask
Uh oh!
There was an error while loading. Please reload this page.
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.
Every time, the loop is iterated using the
Br. Also, I didn't analyze how the loop is being exited. I will look into it soon.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.
I see, the Loop is exited through the else block:
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.
I have a doubt, here the jump and label
.endif_304is useless? is it ok? @Shaikh-UbaidUh oh!
There was an error while loading. Please reload this page.
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.
@Shaikh-Ubaid, were you able to understand or get the point?
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.
I think for the moment that should be fine.
Yup, thank you so much for the explanation, Thirumalai.
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.
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).Uh oh!
There was an error while loading. Please reload this page.
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.
+1; I agree with this.
Also, I didn't test the loop within the if statement.
There you go, you've found the actual bug in the loop statement.
Thank you for testing this. I will make this a new issue. Let's work on this and fix it.