Skip to content

WASM_X86: Initial support for print_str - #1265

Merged
ubaidsk merged 3 commits into
lcompilers:mainfrom
ubaidsk:wasm_x86_print_str
Nov 11, 2022
Merged

WASM_X86: Initial support for print_str#1265
ubaidsk merged 3 commits into
lcompilers:mainfrom
ubaidsk:wasm_x86_print_str

Conversation

@ubaidsk

@ubaidsk ubaidsk commented Nov 4, 2022

Copy link
Copy Markdown
Collaborator

It seems to work, but the approach seems hackish.


Example:

def Main0():
    x: i32
    x = (2+3)*5
    print(x)
    print("Hi")
    print("Hello")
    print((5-2) * 7)
    print("Bye")

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 
25
Hi
Hello
21
Bye
(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 
25
Hi
Hello
21
Bye

Note: Currently only single parameter is supported for print by both x86 and wasm_x86 backends.

@czgdp1807

Copy link
Copy Markdown
Collaborator

Why is it Main0 and not, main0?

@Thirumalai-Shaktivel

Copy link
Copy Markdown
Collaborator

In the x86 backend, we get the following:

$ lpython examples/expr2.py  --backend x86 && ./a.out 
code generation error: Subroutine code not generated for 'main0'


Note: if any of the above error or warning messages are not clear or are lacking
context please report it to us (we consider that a bug that needs to be fixed).

@czgdp1807

Copy link
Copy Markdown
Collaborator

Please generate functions after performing topological sort on them. Here's how it is done in C backend,

// Topologically sort all global functions
// and then define them in the right order
std::vector<std::string> global_func_order = ASRUtils::determine_function_definition_order(x.m_global_scope);

// Process global functions
size_t i;
for (i = 0; i < global_func_order.size(); i++) {
ASR::symbol_t* sym = x.m_global_scope->get_symbol(global_func_order[i]);
// Ignore external symbols because they are already defined by the loop above.
if( !sym || ASR::is_a<ASR::ExternalSymbol_t>(*sym) ) {
continue ;
}
visit_symbol(*sym);
unit_src += src;
}

@ubaidsk

ubaidsk commented Nov 8, 2022

Copy link
Copy Markdown
Collaborator Author

Please generate functions after performing topological sort on them. Here's how it is done in C backend,

Thank you for sharing. This would be very helpful. I think we can also use it in the wasm backend. I will submit a PR with these changes. Thank you again.

@ubaidsk

ubaidsk commented Nov 8, 2022

Copy link
Copy Markdown
Collaborator Author

Why is it Main0 and not, main0?

There were functions like _lpython_main_program() and _lcompilers_main_program() which get defined before the main0() (I think that happens because the ascii value of m (109) is greater the _ (95)) If we rename main0() to Main0() then it gets defined before the _lpython_main_program() and _lcompilers_main_program(), since I guess/think ascii of M (77) is less than _ (95).

@certik certik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine for now.

@ubaidsk

ubaidsk commented Nov 11, 2022

Copy link
Copy Markdown
Collaborator Author

I rebased this on top of latest main. I am adding it to auto-merge as it seems to be approved.

@ubaidsk
ubaidsk enabled auto-merge November 11, 2022 05:47
@ubaidsk
ubaidsk merged commit b55d10b into lcompilers:main Nov 11, 2022
@ubaidsk
ubaidsk deleted the wasm_x86_print_str branch December 23, 2022 21:47
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.

4 participants