Repository navigation
WASI: Initial Support - #1497
Conversation
|
This is currently a WIP (Work in progress). At the moment, #1461 (comment) seems to work. (lp) lpython$ cat examples/expr2.py
from sys import exit
def main0():
a: i32 = 67
exit(a)
main0()
(lp) lpython$ lpython examples/expr2.py --backend wasm -o tmp
(lp) lpython$ wasmtime tmp
(lp) lpython$ echo $?
67
(lp) lpython$ |
|
Beautiful. I think this approach will force us to "clean up" our code generation to make it "standardized". |
565b13a to
2f4f4c6
Compare
|
Current Output: (lp) lpython$ cat examples/expr2.py
def main0():
print("hi", "1234", "jldakfjs!jdlkasf#9u843*)(")
print("abcdefgh1234")
print(-1234)
print(6579352)
print("hello")
main0()
(lp) lpython$ lpython examples/expr2.py --backend wasm -o tmp
(lp) lpython$ wasmtime tmp
hi, 1234, jldakfjs!jdlkasf#9u843*)(
abcdefgh1234
-####
#######
hello
(lp) lpython$
|
| avail_mem_loc += str.length(); | ||
| no_of_data_segments++; | ||
|
|
||
| avail_mem_loc = (int(ceil(avail_mem_loc / 4.0)) << 2); /* align mem location to 4 bytes */ |
There was a problem hiding this comment.
Line 1812: It seems that fd_write works only when its iov vector (it stores the start location of the string and the size of the string) is aligned by 4. It doesn't seem to print any output when the location of the iov vector is not a multiple of 4.
We previously had memory aligned by single byte. It seems we might need the memory alignment as 4 bytes in future.
|
doubts/concerns:
|
|
Printing integers seems to work: (lp) lpython$ cat examples/expr2.py
def main0():
print("hi", "1234", "jldakfjs!jdlkasf#9u843*)(")
print("abcdefgh1234")
print(-1234)
print(6579352, -123, "hi", 45897, "bye", 0, -1, 1, -10, 10, "done.")
print("hello")
print(2, -2, 10, -10, 1000, -10000)
main0()
(lp) lpython$ lpython examples/expr2.py --backend wasm -o tmp
(lp) lpython$ wasmtime tmp
hi, 1234, jldakfjs!jdlkasf#9u843*)(
abcdefgh1234
-1234
6579352, -123, hi, 45897, bye, 0, -1, 1, -10, 10, done.
hello
2, -2, 10, -10, 1000, -10000
(lp) lpython$ |
|
Great job. I think this is maintainable for now. I think we might later need to add a class that handles the WASM as a format, and exposes the API using capabilities like "add a function" and it internally takes care of the details. That way one cannot make a mistake. Regarding: void emit_if_else(std::function<void()> test_cond, std::function<void()> if_block, std::function<void()> else_block) {
test_cond();
wasm::emit_b8(m_code_section, m_al, 0x04); // emit if start
wasm::emit_b8(m_code_section, m_al, 0x40); // empty block type
nesting_level++;
if_block();
wasm::emit_b8(m_code_section, m_al, 0x05); // starting of else
else_block();
nesting_level--;
wasm::emit_expr_end(m_code_section, m_al); // emit if end
}This can probably later be moved to the |
| (data (;14;) (i32.const 184) " | ||
| ") |
There was a problem hiding this comment.
newline character (which is needed/printed by print after printing all arguments) is being shown as above in the generated WAT.
There was a problem hiding this comment.
This has been fixed. The newline character is now escaped and is printed in its byte form (\0a) as follows:
(data (;3;) (i32.const 24) "\0a ")
|
This might still fail at the CI. Currently:
|
|
This looks good, just the WASM->x64 needs to be fixed so that the CI passes and we can merge this. |
|
Now, printing partially works in |
|
Nice, great progress! |
|
It works now! WASM:Binary: (lp) lpython$ lpython integration_tests/print_05.py --backend wasm -o tmp
(lp) lpython$ wasmtime tmp
hi 1234 jldakfjs!jdlkasf#9u843*)(
abcdefgh1234
-1234
6579352 -123 hi 45897 bye 0 -1 1 -10 10 done.
hello
2 -2 10 -10 1000 -10000
3.14000000
-1000.10000000
-1000.00099999
abcdefgh1234
0 0.000000000 abcd 0 efghijk 0.000000000 1^3 -1.000000000 pqrswxyz 1.000000000 mno 2.000000000 p -5.09999999 -5 zz
pi = 3.14000000WAT Text: (lp) lpython$ lpython integration_tests/print_05.py --show-wat > main.wat
(lp) lpython$ wasmtime main.wat
hi 1234 jldakfjs!jdlkasf#9u843*)(
abcdefgh1234
-1234
6579352 -123 hi 45897 bye 0 -1 1 -10 10 done.
hello
2 -2 10 -10 1000 -10000
3.14000000
-1000.10000000
-1000.00099999
abcdefgh1234
0 0.000000000 abcd 0 efghijk 0.000000000 1^3 -1.000000000 pqrswxyz 1.000000000 mno 2.000000000 p -5.09999999 -5 zz
pi = 3.14000000ASM:Binary: (wasm_asm) lpython$ lpython integration_tests/print_05.py --backend wasm_x64 -o tmp > main.asm
(wasm_asm) lpython$ ./tmp
hi 1234 jldakfjs!jdlkasf#9u843*)(
abcdefgh1234
-1234
6579352 -123 hi 45897 bye 0 -1 1 -10 10 done.
hello
2 -2 10 -10 1000 -10000
3.14000000
-1000.10000000
-1000.00099999
abcdefgh1234
0 0.000000000 abcd 0 efghijk 0.000000000 1^3 -1.000000000 pqrswxyz 1.000000000 mno 2.000000000 p -5.09999999 -5 zz
pi = 3.14000000ASM Text: (wasm_asm) lpython$ nasm -fbin main.asm && chmod +x main && ./main
hi 1234 jldakfjs!jdlkasf#9u843*)(
abcdefgh1234
-1234
6579352 -123 hi 45897 bye 0 -1 1 -10 10 done.
hello
2 -2 10 -10 1000 -10000
3.14000000
-1000.10000000
-1000.00099999
abcdefgh1234
0 0.000000000 abcd 0 efghijk 0.000000000 1^3 -1.000000000 pqrswxyz 1.000000000 mno 2.000000000 p -5.09999999 -5 zz
pi = 3.14000000 |
|
I disabled testing for the |
|
The CI seems to pass. I will structure the commits and push here. |
2. WASI: Support importing and calling proc_exit()
|
I reviewed all the changes I think this is beautiful, everything seems to work. Is the Will it be difficult to update the online GUI to work with these changes? |
PS: If the lpython/src/libasr/codegen/wasm_to_x86.cpp Line 348 in 00ba081 |
It could present some challenges (and would thus be a great opportunity to learn and grow). We need to see if there are any I am currently concerned about making the |
|
I would get wasm_x86 working, not fully, but in some limited capacity, so that we can keep testing it and ensuring it works (in its limited capacity). The way I would do it is implement WASM 64bit integers in wasm_x86 (you have to use pair of registers or operate on memory in pairs of 32bit operations), then you can just use the existing WASM code. That can be done later. For now, why don't you get some simple tests that only print strings (but not numbers) working again? That way we have at least something. Or if that is too complicated, then at least returning zero / nonzero return value. Then we can merge this PR and implement the rest later. Regarding the web UI, I would implement the functions that we use ourselves. That way the implementation will be small and clean. Regarding accessing memory that you can modify in x64 assembly: I think it's called "heap", and you have to request the memory from the system. In C you would call |
Sure, Sir. Using pairs of
Got it. Thank you for the guidance. |
|
If it is fine, shall we tackle the WebUI and the assembly (heap) memory later? |
CodeGenError() prints 'Unknown Exception', where as AssemblerError() prints the error message
720e1d1 to
9d2751a
Compare
|
I think this looks good, let's see if tests pass. |
|
This PR is ready.
Sure. |
|
The There is an integer overflow when printing the value of lpython/integration_tests/print_03.py Lines 6 to 10 in 00ba081 On my system locally, it just prints the (lp) lpython$ lpython integration_tests/print_03.py --backend wasm_x86 -o tmp
(lp) lpython$ ./tmp
-25
100
2147483647
-
Hi
Hello
21
Bye |
20dde5f to
46a0788
Compare
|
I am sorry about #1497 (comment). I see it was the What shall we do about TEST: Slightly modify test to support it in wasm_x86? |
|
This is ready. |
certik
left a comment
There was a problem hiding this comment.
I think this is good to go in!
I reviewed most of it, I don't see any major issue now. Great job, this is excellent progress.
|
Thank you for the approval and merging! |
This PR implements initial support of
WASIin thewasmandwasm_x64backends.