Skip to content

WASM_X64: Support if else #1390

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 3 commits into from
Jan 2, 2023
Merged

Conversation

ubaidsk
Copy link
Collaborator

@ubaidsk ubaidsk commented Jan 2, 2023

This PR adds support for if-else statements in the wasm_x64 backend.

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Jan 2, 2023

I initially attempted to support the idea of using unique integers shared at #1264 (comment).

For example using:

void visit_If() {
        unique_idx++;
        std::string label = std::to_string(unique_idx);
        m_a.asm_pop_r64(X64Reg::rax); // now `rax` contains the logical value (true = 1, false = 0) of the if condition
        m_a.asm_cmp_r64_imm8(X64Reg::rax, 1);
        m_a.asm_je_label(".then_" + label);
        m_a.asm_jmp_label(".else_" + label);
        m_a.add_label(".then_" + label);
        {
            decode_instructions();
        }
        m_a.add_label(".endif_" + label);
        unique_idx--;
    }

    void visit_Else() {
        std::string label = std::to_string(unique_idx);
        m_a.asm_jmp_label(".endif_" + label);
        m_a.add_label(".else_" + label);
    }

But it happens that the if-else statements within the same scope have the same ids as follows:

if { ---- index 0
    if { --- index 1
        if { --- index 2

        } else { --- index 2

        }        
    } else { --- index 1

    }
   
    if { -- index 1
   
    } else { --- index 1
    
    }
} 

Thus, I used the stack approach similar to the x86 backend.

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Jan 2, 2023

Thank you for the approval. I am adding it to auto-merge as it seems approved.

@ubaidsk ubaidsk enabled auto-merge January 2, 2023 04:00
@ubaidsk ubaidsk merged commit b47f2e8 into lcompilers:main Jan 2, 2023
@ubaidsk ubaidsk deleted the wasm_x64_if_else branch January 2, 2023 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants