Skip to content

Wasm_x86: support functions return value #1244

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

Conversation

ubaidsk
Copy link
Collaborator

@ubaidsk ubaidsk commented Oct 30, 2022

This PR adds support for return values of function calls.

Example:

def A_Sqr(x: i32) -> i32:
    return x * x

def ComputeCircleArea(radius: i32) -> i32:
    pi: i32
    pi = 3
    return pi * A_Sqr(radius)

def Main0():
    print(ComputeCircleArea(5))

Main0()

Output:

(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ lpython examples/expr2.py --backend x86
(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ ./a.out 
75
(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ lpython examples/expr2.py --backend wasm_x86
(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ ./a.out 
75(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ 

Only the latest/recent two commits are part of this PR. The rest of the commits are part of #1223. I will rebase this PR once #1223 is merged.

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Oct 30, 2022

In the above example, the function Sqr() is named as A_Sqr() so that it gets defined before the function ComputeCircleArea() which calls it.

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Oct 30, 2022

Another example, with (unnecessary) local variables inside A_Sqr(). The unnecessary local variables testify that the return value is being properly/appropriately handled.

def A_Sqr(x: i32) -> i32:
    y: i32
    z: i32
    y = 2
    z = 5
    x = y + z
    return x * x

def ComputeCircleArea(radius: i32) -> i32:
    pi: i32
    pi = 3
    return pi * A_Sqr(radius)

def Main0():
    print(ComputeCircleArea(5))

Main0()

Output:

(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ lpython examples/expr2.py --backend x86
(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ ./a.out 
147
(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ lpython examples/expr2.py --backend wasm_x86
(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ ./a.out 
147(lp) ubaid@ubaid-Lenovo-ideapad-330-15ARR:~/OpenSource/lpython$ 

@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Oct 30, 2022

This is ready. Please possibly review and please share feedback.

@ubaidsk ubaidsk requested a review from certik October 30, 2022 11:36
@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Nov 2, 2022

It seems, we can combine the X86Generator and X86Visitor classes. I created a demo PR for the same here ubaidsk#1. Please possibly review and share if we shall combine the two classes.

@certik certik force-pushed the wasm_x86_support_functions_return_value branch from 73902d5 to 9b13170 Compare November 2, 2022 17:36
@certik certik enabled auto-merge November 2, 2022 17:37
@certik certik merged commit a112710 into lcompilers:main Nov 2, 2022
@ubaidsk
Copy link
Collaborator Author

ubaidsk commented Nov 2, 2022

It seems, we can combine the X86Generator and X86Visitor classes. I created a demo PR for the same here ubaidsk#1. Please possibly review and share if we shall combine the two classes.

I submitted the above change as a regular PR at #1257.

@ubaidsk ubaidsk deleted the wasm_x86_support_functions_return_value branch November 3, 2022 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants