blob: e932df82f007fcdeb1bdd43aca27eb0e614df066 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
;; Test that we can roundtrip struct and array types
(module
;; Structs
(type $struct.A (struct
i32
(field f32)
(field $named f64)
))
;; identical to $struct.A, so will be canonicalized with it, but field names
;; are different
(type $struct.A.prime (struct
i32
(field f32)
(field $othername f64)
))
(type $struct.B (struct
(field i8)
(field (mut i16))
(field (ref $struct.A))
(field (mut (ref $struct.A)))
))
(type $struct.C (struct
(field $named-mut (mut f32))
))
;; Arrays
(type $vector (array (mut f64)))
(type $matrix (array (mut (ref null $vector))))
(type $bytes (array (mut i8)))
(type $words (array (mut i32)))
;; RTT
(type $parent (struct))
(type $child (struct i32))
(type $grandchild (struct i32 i64))
(global $rttparent (rtt 0 $parent) (rtt.canon $parent))
(global $rttchild (rtt 1 $child) (rtt.sub $child (global.get $rttparent)))
(global $rttgrandchild (rtt 2 $grandchild) (rtt.sub $grandchild (global.get $rttchild)))
(type $nested-child-struct (struct (field (mut (ref $child)))))
(type $nested-child-array (array (mut (ref $child))))
(func $structs (param $x (ref $struct.A)) (result (ref $struct.B))
(local $tA (ref null $struct.A))
(local $tB (ref null $struct.B))
(local $tc (ref null $struct.C))
(local $tv (ref null $vector))
(local $tm (ref null $matrix))
(drop
(local.get $x)
)
(drop
(struct.get $struct.A 0 (local.get $x))
)
(drop
(struct.get $struct.A 1 (local.get $x))
)
(drop
(struct.get $struct.A 2 (local.get $x))
)
(drop
(struct.get $struct.A $named (local.get $x))
)
(drop
(struct.get $struct.A.prime $othername (ref.null $struct.A.prime))
)
(drop
(struct.get_u $struct.B 0 (local.get $tB))
)
(drop
(struct.get_s $struct.B 0 (local.get $tB))
)
;; immutable fields allow subtyping.
(drop
(struct.get $child 0 (ref.null $grandchild))
)
(drop
(ref.null $struct.A)
)
(drop
(block (result (ref null $struct.A))
(local.get $x)
)
)
(drop
(if (result (ref null $struct.A))
(i32.const 1)
(local.get $x)
(local.get $x)
)
)
(drop
(loop (result (ref null $struct.A))
(local.get $x)
)
)
(drop
(select (result (ref null $struct.A))
(local.get $x)
(local.get $x)
(i32.const 1)
)
)
(struct.set $struct.C 0
(ref.null $struct.C)
(f32.const 100)
)
;; values may be subtypes
(struct.set $nested-child-struct 0
(ref.null $nested-child-struct)
(ref.as_non_null
(ref.null $grandchild)
)
)
(drop
(struct.new_default_with_rtt $struct.A
(rtt.canon $struct.A)
)
)
(drop
(struct.new_with_rtt $struct.A
(i32.const 1)
(f32.const 2.345)
(f64.const 3.14159)
(rtt.canon $struct.A)
)
)
(unreachable)
)
(func $arrays (param $x (ref $vector)) (result (ref $matrix))
(local $tv (ref null $vector))
(local $tm (ref null $matrix))
(local $tb (ref null $bytes))
(local $tw (ref null $words))
(drop
(array.new_with_rtt $vector
(f64.const 3.14159)
(i32.const 3)
(rtt.canon $vector)
)
)
(drop
(array.new_default_with_rtt $matrix
(i32.const 10)
(rtt.canon $matrix)
)
)
(drop
(array.get $vector
(local.get $x)
(i32.const 2)
)
)
(array.set $vector
(local.get $x)
(i32.const 2)
(f64.const 2.18281828)
)
;; values may be subtypes
(array.set $nested-child-array
(ref.null $nested-child-array)
(i32.const 3)
(ref.as_non_null
(ref.null $grandchild)
)
)
(drop
(array.len $vector
(local.get $x)
)
)
(drop
(array.get $words
(local.get $tw)
(i32.const 1)
)
)
(drop
(array.get_u $bytes
(local.get $tb)
(i32.const 2)
)
)
(drop
(array.get_s $bytes
(local.get $tb)
(i32.const 3)
)
)
(unreachable)
)
;; RTT types as parameters
(func $rtt-param-with-depth (param $rtt (rtt 1 $parent)))
(func $rtt-param-without-depth (param $rtt (rtt $parent)))
(func $rtt-operations
(local $temp.A (ref null $struct.A))
(drop
(ref.test (ref.null $struct.A) (rtt.canon $struct.B))
)
(drop
(ref.cast (ref.null $struct.A) (rtt.canon $struct.B))
)
(drop
(block $out (result (ref $struct.B))
;; set the value to a local with type $struct.A, showing that the value
;; flowing out has the right type
(local.set $temp.A
(br_on_cast $out (ref.null $struct.A) (rtt.canon $struct.B))
)
;; an untaken br_on_cast, with unreachable rtt - so we cannot use the
;; RTT in binaryen IR to find the cast type.
(br_on_cast $out (ref.null $struct.A) (unreachable))
(unreachable)
)
)
)
(func $ref.is_X (param $x anyref)
(if (ref.is_func (local.get $x)) (unreachable))
(if (ref.is_data (local.get $x)) (unreachable))
(if (ref.is_i31 (local.get $x)) (unreachable))
)
(func $ref.as_X (param $x anyref)
(drop (ref.as_non_null (local.get $x)))
(drop (ref.as_func (local.get $x)))
(drop (ref.as_data (local.get $x)))
(drop (ref.as_i31 (local.get $x)))
)
(func $br_on_X (param $x anyref)
(local $y anyref)
(local $z (ref null any))
(block $null
(local.set $z
(br_on_null $null (local.get $x))
)
)
(drop
(block $func (result funcref)
(local.set $y
(br_on_func $func (local.get $x))
)
(ref.null func)
)
)
(drop
(block $data (result (ref null data))
(local.set $y
(br_on_data $data (local.get $x))
)
(ref.null data)
)
)
(drop
(block $i31 (result (ref null i31))
(local.set $y
(br_on_i31 $i31 (local.get $x))
)
(ref.null i31)
)
)
)
(func $unreachables
(drop
(struct.get $struct.A 0 (unreachable))
)
(drop
(struct.set $struct.A 0 (unreachable) (unreachable))
)
)
)
|