Skip to content

Commit 0507cf1

Browse files
committed
Restrict order of func_fields; more tests (#291)
Fixes #290 by imposing a strict order on param, result, and local declarations.
1 parent 040e28e commit 0507cf1

8 files changed

+127
-34
lines changed

ml-proto/host/parser.mly

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,25 +282,28 @@ expr_list :
282282
/* Functions */
283283
284284
func_fields :
285-
| expr_list
286-
{ empty_type,
287-
fun c -> let c' = anon_label c in
288-
{ftype = -1 @@ at(); locals = []; body = $1 c'} }
285+
| func_body { $1 }
286+
| LPAR RESULT VALUE_TYPE RPAR func_body
287+
{ if (fst $5).out <> None then error (at ()) "multiple return types";
288+
{(fst $5) with out = Some $3},
289+
fun c -> (snd $5) c }
289290
| LPAR PARAM value_type_list RPAR func_fields
290291
{ {(fst $5) with ins = $3 @ (fst $5).ins},
291292
fun c -> anon_locals c $3; (snd $5) c }
292293
| LPAR PARAM bind_var VALUE_TYPE RPAR func_fields /* Sugar */
293294
{ {(fst $6) with ins = $4 :: (fst $6).ins},
294295
fun c -> bind_local c $3; (snd $6) c }
295-
| LPAR RESULT VALUE_TYPE RPAR func_fields
296-
{ if (fst $5).out <> None then error (at ()) "multiple return types";
297-
{(fst $5) with out = Some $3},
298-
fun c -> (snd $5) c }
299-
| LPAR LOCAL value_type_list RPAR func_fields
296+
;
297+
func_body :
298+
| expr_list
299+
{ empty_type,
300+
fun c -> let c' = anon_label c in
301+
{ftype = -1 @@ at(); locals = []; body = $1 c'} }
302+
| LPAR LOCAL value_type_list RPAR func_body
300303
{ fst $5,
301304
fun c -> anon_locals c $3; let f = (snd $5) c in
302305
{f with locals = $3 @ f.locals} }
303-
| LPAR LOCAL bind_var VALUE_TYPE RPAR func_fields /* Sugar */
306+
| LPAR LOCAL bind_var VALUE_TYPE RPAR func_body /* Sugar */
304307
{ fst $6,
305308
fun c -> bind_local c $3; let f = (snd $6) c in
306309
{f with locals = $4 :: f.locals} }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(module (func (nop) (local i32)))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(module (func (local i32) (param i32)))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(module (func (local i32) (result i32) (get_local 0)))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(module (func (nop) (param i32)))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(module (func (nop) (result i32)))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(module (func (result i32) (param i32) (get_local 0)))

ml-proto/test/functions.wast

Lines changed: 108 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,128 @@
11
(module
2-
(func $empty)
3-
(export "empty" $empty)
2+
(func "empty")
43

5-
(func $result-nop (nop))
6-
(export "result-nop" $result-nop)
4+
(func "value-nop" (nop))
5+
(func "value-drop" (i32.const 1))
6+
(func "value-block-nop" (block (i32.const 1) (nop)))
7+
(func "value-block-drop" (block (nop) (i32.const 1)))
78

8-
(func $result-drop (i32.const 1))
9-
(export "result-drop" $result-drop)
9+
(func "return" (return))
10+
(func "return-nop" (return (nop)))
11+
(func "return-drop" (return (i32.const 1)))
12+
(func "return-block-nop" (return (block (i32.const 1) (nop))))
13+
(func "return-block-drop" (return (block (nop) (i32.const 1))))
1014

11-
(func $result-block-nop (block (i32.const 1) (nop)))
12-
(export "result-block-nop" $result-block-nop)
15+
(func "param" (param i32) (i32.eqz (get_local 0)))
16+
(func "params" (param i32 f64)
17+
(i32.eqz (get_local 0)) (f64.neg (get_local 1))
18+
)
19+
(func "param-param" (param i32) (param f64)
20+
(i32.eqz (get_local 0)) (f64.neg (get_local 1))
21+
)
22+
(func "params-params" (param i32 f32) (param i32) (param f64 i64)
23+
(i32.eqz (get_local 0)) (f32.neg (get_local 1)) (i32.eqz (get_local 2))
24+
(f64.neg (get_local 3)) (i64.eqz (get_local 4))
25+
)
1326

14-
(func $result-block-drop (block (nop) (i32.const 1)))
15-
(export "result-block-drop" $result-block-drop)
27+
(func "result" (result i32) (i32.const 1))
1628

17-
(func $return (return))
18-
(export "return" $return)
29+
(func "local" (local i32) (i32.eqz (get_local 0)))
30+
(func "locals" (local i32 f64 i64)
31+
(i32.eqz (get_local 0)) (f64.neg (get_local 1)) (i64.eqz (get_local 2))
32+
)
33+
(func "local-local" (local i32) (local f64)
34+
(i32.eqz (get_local 0)) (f64.neg (get_local 1))
35+
)
36+
(func "locals-locals" (local i32 f32) (local i32) (local f64 i64)
37+
(i32.eqz (get_local 0)) (f32.neg (get_local 1)) (i32.eqz (get_local 2))
38+
(f64.neg (get_local 3)) (i64.eqz (get_local 4))
39+
)
1940

20-
(func $return-nop (return (nop)))
21-
(export "return-nop" $return-nop)
41+
(func "param-result" (param i32) (result i32) (i32.eqz (get_local 0)))
42+
(func "params-result" (param i32 f64) (result i32)
43+
(f64.neg (get_local 1)) (i32.eqz (get_local 0))
44+
)
45+
(func "param-param-result" (param i32) (param f64) (result i32)
46+
(f64.neg (get_local 1)) (i32.eqz (get_local 0))
47+
)
2248

23-
(func $return-drop (return (i32.const 1)))
24-
(export "return-drop" $return-drop)
49+
(func "result-local" (result i32) (local i32) (i32.eqz (get_local 0)))
50+
(func "result-locals" (result i32) (local i32 f64)
51+
(f64.neg (get_local 1)) (i32.eqz (get_local 0))
52+
)
53+
(func "result-local-local" (result i32) (local i32) (local f64)
54+
(f64.neg (get_local 1)) (i32.eqz (get_local 0))
55+
)
2556

26-
(func $return-block-nop (return (block (i32.const 1) (nop))))
27-
(export "return-block-nop" $return-block-nop)
57+
(func "param-local" (param i32) (local f64)
58+
(i32.eqz (get_local 0)) (f64.neg (get_local 1))
59+
)
60+
(func "params-local" (param i32 i64) (local f64)
61+
(i32.eqz (get_local 0)) (i64.eqz (get_local 1)) (f64.neg (get_local 2))
62+
)
63+
(func "param-locals" (param i32) (local i64 f64)
64+
(i32.eqz (get_local 0)) (i64.eqz (get_local 1)) (f64.neg (get_local 2))
65+
)
66+
(func "params-locals" (param f32 i32) (local i64 f64)
67+
(f32.neg (get_local 0)) (i32.eqz (get_local 1))
68+
(i64.eqz (get_local 2)) (f64.neg (get_local 3))
69+
)
70+
(func "param-params-locals-local" (param f32) (param i32 f32) (local i64 f64) (local i32)
71+
(f32.neg (get_local 0)) (i32.eqz (get_local 1)) (f32.neg (get_local 2))
72+
(i64.eqz (get_local 3)) (f64.neg (get_local 4)) (i32.eqz (get_local 5))
73+
)
2874

29-
(func $return-block-drop (return (block (nop) (i32.const 1))))
30-
(export "return-block-drop" $return-block-drop)
75+
(func "param-result-local" (param i32) (result f64) (local f64)
76+
(i32.eqz (get_local 0)) (f64.neg (get_local 1))
77+
)
78+
(func "params-params-result-locals-locals"
79+
(param i32 i64) (param f32 f64) (result i32)
80+
(local i32 f64) (local i64 f32 i32)
81+
(i32.eqz (get_local 0)) (i64.eqz (get_local 1)) (f32.neg (get_local 2))
82+
(f64.neg (get_local 3)) (i32.eqz (get_local 4)) (f64.neg (get_local 5))
83+
(i64.eqz (get_local 6)) (f32.neg (get_local 7)) (i32.eqz (get_local 8))
84+
)
3185
)
3286

3387
(assert_return (invoke "empty"))
34-
(assert_return (invoke "result-nop"))
35-
(assert_return (invoke "result-drop"))
36-
(assert_return (invoke "result-block-nop"))
37-
(assert_return (invoke "result-block-drop"))
88+
89+
(assert_return (invoke "value-nop"))
90+
(assert_return (invoke "value-drop"))
91+
(assert_return (invoke "value-block-nop"))
92+
(assert_return (invoke "value-block-drop"))
3893

3994
(assert_return (invoke "return"))
4095
(assert_return (invoke "return-nop"))
4196
(assert_return (invoke "return-drop"))
4297
(assert_return (invoke "return-block-nop"))
4398
(assert_return (invoke "return-block-drop"))
4499

100+
(assert_return (invoke "param" (i32.const 1)))
101+
(assert_return (invoke "params" (i32.const 1) (f64.const 0)))
102+
(assert_return (invoke "param-param" (i32.const 1) (f64.const 0)))
103+
(assert_return (invoke "params-params" (i32.const 1) (f32.const 0) (i32.const 1) (f64.const 0) (i64.const 1)))
104+
105+
(assert_return (invoke "result") (i32.const 1))
106+
107+
(assert_return (invoke "local"))
108+
(assert_return (invoke "locals"))
109+
(assert_return (invoke "local-local"))
110+
(assert_return (invoke "locals-locals"))
111+
112+
(assert_return (invoke "param-result" (i32.const 1)) (i32.const 0))
113+
(assert_return (invoke "params-result" (i32.const 1) (f64.const 0)) (i32.const 0))
114+
(assert_return (invoke "param-param-result" (i32.const 1) (f64.const 0)) (i32.const 0))
115+
116+
(assert_return (invoke "result-local") (i32.const 1))
117+
(assert_return (invoke "result-locals" ) (i32.const 1))
118+
(assert_return (invoke "result-local-local") (i32.const 1))
119+
120+
(assert_return (invoke "param-local" (i32.const 1)))
121+
(assert_return (invoke "params-local" (i32.const 1) (i64.const 0)))
122+
(assert_return (invoke "param-locals" (i32.const 1)))
123+
(assert_return (invoke "params-locals" (f32.const 1) (i32.const 0)))
124+
(assert_return (invoke "param-params-locals-local" (f32.const 1) (i32.const 0) (f32.const 1)))
125+
126+
(assert_return (invoke "param-result-local" (i32.const 1)) (f64.const -0))
127+
(assert_return (invoke "params-params-result-locals-locals" (i32.const 1) (i64.const 1) (f32.const 0) (f64.const 1)) (i32.const 1))
128+

0 commit comments

Comments
 (0)