Skip to content

Commit 88cb648

Browse files
authored
Merge pull request #1454 from Shaikh-Ubaid/fix_wat_decoding
WASMDecoder: Replace while loop with individual decode intructions
2 parents 6b34882 + 76fc4c9 commit 88cb648

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

src/libasr/codegen/wasm_decoder.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,17 @@ class WASMDecoder {
262262
}
263263

264264
data_segments.p[i].insts_start_index = offset;
265-
while (read_b8(wasm_bytes, offset) != 0x0B)
266-
;
265+
266+
// read i32.const
267+
if (read_b8(wasm_bytes, offset) != 0x41) {
268+
throw CodeGenError("DecodeDataSection: Invalid byte for i32.const");
269+
}
270+
// read the integer (memory location)
271+
read_i32(wasm_bytes, offset);
272+
// read expr end
273+
if (read_b8(wasm_bytes, offset) != 0x0B) {
274+
throw CodeGenError("DecodeDataSection: Invalid byte for expr end");
275+
}
267276

268277
uint32_t text_size = read_u32(wasm_bytes, offset);
269278
data_segments.p[i].text.resize(

tests/print_str.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def printCompOperations():
2+
print("x < y")
3+
print("x !< y")
4+
print("x > y")
5+
print("x !> y")
6+
print("x == y")
7+
print("x != y")
8+
9+
10+
def main0():
11+
printCompOperations()
12+
13+
main0()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "wat-print_str-385e953",
3+
"cmd": "lpython --no-color --show-wat {infile}",
4+
"infile": "tests/print_str.py",
5+
"infile_hash": "56d2a86f8e1cca05ff1749d1c4cdaa79f99b6f92d3ce60870ee900d7",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "wat-print_str-385e953.stdout",
9+
"stdout_hash": "a3680583488bc169e7993ffda63e9da9e7aa8987cb4422d31c4ab021",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
(module
2+
(type (;0;) (func (param i32) (result)))
3+
(type (;1;) (func (param i64) (result)))
4+
(type (;2;) (func (param f32) (result)))
5+
(type (;3;) (func (param f64) (result)))
6+
(type (;4;) (func (param i32 i32) (result)))
7+
(type (;5;) (func (param) (result)))
8+
(type (;6;) (func (param i32) (result)))
9+
(type (;7;) (func (param) (result)))
10+
(type (;8;) (func (param) (result)))
11+
(type (;9;) (func (param) (result)))
12+
(type (;10;) (func (param) (result)))
13+
(import "js" "print_i32" (func (;0;) (type 0)))
14+
(import "js" "print_i64" (func (;1;) (type 1)))
15+
(import "js" "print_f32" (func (;2;) (type 2)))
16+
(import "js" "print_f64" (func (;3;) (type 3)))
17+
(import "js" "print_str" (func (;4;) (type 4)))
18+
(import "js" "flush_buf" (func (;5;) (type 5)))
19+
(import "js" "set_exit_code" (func (;6;) (type 6)))
20+
(import "js" "memory" (memory (;0;) 100 100))
21+
(func $7 (type 7) (param) (result)
22+
(local)
23+
call 8
24+
return
25+
)
26+
(func $8 (type 8) (param) (result)
27+
(local)
28+
call 9
29+
return
30+
)
31+
(func $9 (type 9) (param) (result)
32+
(local)
33+
i32.const 0
34+
i32.const 5
35+
call 4
36+
call 5
37+
i32.const 5
38+
i32.const 6
39+
call 4
40+
call 5
41+
i32.const 11
42+
i32.const 5
43+
call 4
44+
call 5
45+
i32.const 16
46+
i32.const 6
47+
call 4
48+
call 5
49+
i32.const 22
50+
i32.const 6
51+
call 4
52+
call 5
53+
i32.const 28
54+
i32.const 6
55+
call 4
56+
call 5
57+
return
58+
)
59+
(func $10 (type 10) (param) (result)
60+
(local)
61+
call 7
62+
i32.const 0
63+
call 6
64+
return
65+
)
66+
(export "_lpython_main_program" (func $7))
67+
(export "main0" (func $8))
68+
(export "printCompOperations" (func $9))
69+
(export "_lcompilers_main" (func $10))
70+
(data (;0;) (i32.const 0) "x < y")
71+
(data (;1;) (i32.const 5) "x !< y")
72+
(data (;2;) (i32.const 11) "x > y")
73+
(data (;3;) (i32.const 16) "x !> y")
74+
(data (;4;) (i32.const 22) "x == y")
75+
(data (;5;) (i32.const 28) "x != y")
76+
)

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ c = true
286286
filename = "ltypes1.py"
287287
llvm = true
288288

289+
[[test]]
290+
filename = "print_str.py"
291+
wat = true
292+
289293
[[test]]
290294
filename = "../integration_tests/test_builtin.py"
291295
asr = true

0 commit comments

Comments
 (0)