Source file ast.ml
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
type ('desc, 'info) annotated = ('desc, 'info) Wax_utils.Ast.annotated = {
desc : 'desc;
info : 'info;
}
type location = Wax_utils.Ast.location = {
loc_start : Lexing.position;
loc_end : Lexing.position;
}
let no_loc = Wax_utils.Ast.no_loc
module Annot = Wax_utils.Ast
type ident = (string, location) annotated
include Wax_wasm.Ast.Make_types (struct
type idx = ident
type 'a annotated_array = (ident * 'a, location) annotated array
type 'a opt_annotated_array = (ident option * 'a, location) annotated array
end)
let splice_field_name = ".."
let is_splice_field (f : (ident * fieldtype, location) annotated) =
String.equal (fst f.desc).desc splice_field_name
let splice_field loc : (ident * fieldtype, location) annotated =
{
desc =
( { desc = splice_field_name; info = loc },
{ mut = false; typ = Value I32 } );
info = loc;
}
* The located [(name, thing)] pairs the type family carries: a function
parameter (its name optional), a struct field, a rec-group member. Named
accessors rather than [fst]/[snd] over [.desc], because [desc] on a node whose
type is not already known resolves to the instruction record's field of that
name (see {!Annot}). Their declared return types also let a chained read —
[(field_name f).desc] — resolve on its own. *)
let param_name (p : (ident option * valtype, location) annotated) = fst p.desc
let param_type (p : (ident option * valtype, location) annotated) = snd p.desc
let field_name (f : (ident * fieldtype, location) annotated) = fst f.desc
let field_type (f : (ident * fieldtype, location) annotated) = snd f.desc
let member_name (m : (ident * subtype, location) annotated) = fst m.desc
let member_type (m : (ident * subtype, location) annotated) = snd m.desc
type signage = Wax_wasm.Ast.signage = Signed | Unsigned
type unop = Neg | Pos | Not
type binop =
| Add
| Sub
| Mul
| Div of signage option
| Rem of signage
| And
| Or
| Xor
| Shl
| Shr of signage
| Eq
| Ne
| Lt of signage option
| Gt of signage option
| Le of signage option
| Ge of signage option
type label = ident
type casttype =
| Valtype of valtype
| Functype of { nullable : bool; sign : functype }
| Signedtype of {
typ : [ `I32 | `I64 | `F32 | `F64 ];
signage : signage;
strict : bool;
}
| Ascribed of valtype
(** The parenthesized type ascription [(e : t)]: a static assertion that
[e]'s type is a subtype of [t], with result type [t]. Never lowers to
an instruction, and — unlike a cast — an ascribed bare hole claims no
pending value: it GROUNDS a value of type [t] off the polymorphic
floor. *)
let format_signed_type typ signage strict =
Printf.sprintf "%s_%s%s"
(match typ with
| `I32 -> "i32"
| `I64 -> "i64"
| `F32 -> "f32"
| `F64 -> "f64")
(match signage with Signed -> "s" | Unsigned -> "u")
(if strict then "_strict" else "")
type catch =
| Catch of ident * label
| CatchRef of ident * label
| CatchAll of label
| CatchAllRef of label
type on_clause = OnLabel of ident * label | OnSwitch of ident
type match_pattern = MatchCast of ident option * reftype | MatchNull
type 'info instr_desc =
| Block of {
label : label option;
typ : functype;
block : ('info instr list, location) annotated;
}
| Loop of {
label : label option;
typ : functype;
block : ('info instr list, location) annotated;
}
| While of {
label : label option;
cond : 'info instr;
step : 'info instr option;
block : ('info instr list, location) annotated;
}
| If of {
label : label option;
typ : functype;
cond : 'info instr;
if_block : ('info instr list, location) annotated;
else_block : ('info instr list, location) annotated option;
}
| TryTable of {
label : label option;
typ : functype;
catches : catch list;
block : ('info instr list, location) annotated;
}
| Try of {
label : label option;
typ : functype;
block : ('info instr list, location) annotated;
catches : (ident * ('info instr list, location) annotated) list;
catch_all : ('info instr list, location) annotated option;
}
| TryCatch of {
label : label option;
typ : functype;
block : ('info instr list, location) annotated;
arms : 'info trycatch_arm list;
}
| Unreachable
| Nop
| Hole
| Null
| Get of ident
| Path of ident * ident
| Set of ident * (binop, location) annotated option * 'info instr
| Tee of ident * 'info instr
| Call of 'info instr * 'info instr list
| TailCall of 'info instr * 'info instr list
| Labelled of ident * 'info instr
| Char of Uchar.t
| String of ident option * string
| Int of string
| Float of string
| Cast of 'info instr * casttype
| CastDesc of 'info instr * bool * 'info instr
| Test of 'info instr * reftype
| NonNull of 'info instr
| Struct of ident option * (ident * 'info instr option) list
| StructDefault of ident option
| StructDesc of 'info instr * (ident * 'info instr option) list
| StructDefaultDesc of 'info instr
| StructGet of 'info instr * ident
| GetDescriptor of 'info instr
| StructSet of 'info instr * ident * 'info instr
| Array of ident option * 'info instr * 'info instr
| ArrayDefault of ident option * 'info instr
| ArrayFixed of ident option * 'info instr list
| ArraySegment of ident option * ident * 'info instr * 'info instr
| ArrayGet of 'info instr * 'info instr
| ArraySet of 'info instr * 'info instr * 'info instr
| BinOp of (binop, location) annotated * 'info instr * 'info instr
| UnOp of (unop, location) annotated * 'info instr
| Let of (ident option * valtype option) list * 'info instr option
| Br of label * 'info instr option
| Br_if of label * 'info instr
| Br_table of label list * 'info instr
| Dispatch of {
index : 'info instr;
cases : label list;
default : label;
arms : (label * ('info instr list, location) annotated) list;
}
| Match of {
scrutinee : 'info instr;
arms : (match_pattern * ('info instr list, location) annotated) list;
default : ('info instr list, location) annotated;
}
| Br_on_null of label * 'info instr
| Br_on_non_null of label * 'info instr
| Br_on_cast of label * reftype * 'info instr
| Br_on_cast_fail of label * reftype * 'info instr
| Br_on_cast_desc_eq of
label * bool * 'info instr * 'info instr
| Br_on_cast_desc_eq_fail of
label * bool * 'info instr * 'info instr
| Throw of ident * 'info instr list
| ThrowRef of 'info instr
| ContNew of ident * 'info instr
| ContBind of ident * ident * 'info instr list
| Suspend of ident * 'info instr list
| Resume of ident * on_clause list * 'info instr list
| ResumeThrow of ident * ident * on_clause list * 'info instr list
| ResumeThrowRef of ident * on_clause list * 'info instr list
| Switch of ident * ident * 'info instr list
| On of 'info instr * on_clause list
| Return of 'info instr option
| Sequence of 'info instr list
| Select of 'info instr * 'info instr * 'info instr
| If_annotation of {
cond : Wax_wasm.Ast.cond;
then_body : ('info instr list, location) annotated;
else_body : ('info instr list, location) annotated option;
}
and expectation = Unset | Contextual | Recorded of valtype
and 'info instr = {
desc : 'info instr_desc;
info : 'info;
hints : ident Wax_wasm.Hints.t;
expected : expectation;
}
and 'info trycatch_arm = {
arm_tag : ident option;
arm_ref : bool;
arm_types : valtype array;
arm_body : ('info instr list, location) annotated;
}
let no_loc_instr desc : location instr =
{
desc;
info = Wax_utils.Ast.dummy_loc;
hints = Wax_wasm.Hints.none;
expected = Unset;
}
type attribute = {
attr_name : string;
attr_value : location instr option;
attr_guard : (Wax_wasm.Ast.cond, location) annotated option;
attr_span : location;
}
type attributes = attribute list
type import_kind =
| Import_func of { typ : ident option; sign : functype option; exact : bool }
| Import_global of { mut : bool; typ : valtype }
| Import_tag of { typ : ident option; sign : functype option }
| Import_memory of {
address_type : [ `I32 | `I64 ];
limits : (Wax_utils.Uint64.t * Wax_utils.Uint64.t option) option;
page_size_log2 : int option;
shared : bool;
}
| Import_table of {
address_type : [ `I32 | `I64 ];
reftype : reftype;
limits : (Wax_utils.Uint64.t * Wax_utils.Uint64.t option) option;
}
type import_decl = { id : ident; kind : import_kind; attributes : attributes }
type data_elem =
| Data_string of string
| Data_run of storagetype * (string, location) annotated list
| Data_v128 of (Wax_utils.V128.t, location) annotated list
type 'info memdata = {
data_name : ident option;
offset : 'info instr;
init : data_elem list;
}
type 'info datamode = Passive | Active of ident * 'info instr
type 'info elemmode = EPassive | EActive of ident * 'info instr
type 'info modulefield =
| Type of rectype
| Func of {
name : ident;
typ : ident option;
sign : functype option;
body : label option * 'info instr list;
attributes : attributes;
}
| Global of {
name : ident;
mut : bool;
typ : valtype option;
def : 'info instr;
attributes : attributes;
}
| Tag of {
name : ident;
typ : ident option;
sign : functype option;
attributes : attributes;
}
| Memory of {
name : ident;
address_type : [ `I32 | `I64 ];
limits : (Wax_utils.Uint64.t * Wax_utils.Uint64.t option) option;
page_size_log2 : int option;
shared : bool;
data : 'info memdata list;
attributes : attributes;
}
| Data of {
name : ident option;
mode : 'info datamode;
init : data_elem list;
attributes : attributes;
}
| Table of {
name : ident;
address_type : [ `I32 | `I64 ];
reftype : reftype;
limits : (Wax_utils.Uint64.t * Wax_utils.Uint64.t option) option;
init : 'info instr option;
attributes : attributes;
}
| Elem of {
name : ident;
reftype : reftype;
mode : 'info elemmode;
init : 'info instr list;
attributes : attributes;
}
| Import of {
module_ : (string, location) annotated;
decl : (import_decl, location) annotated;
}
| Import_group of {
module_ : (string, location) annotated;
decls : (import_decl, location) annotated list;
}
| Module_annotation of attributes
| Conditional of {
cond : Wax_wasm.Ast.cond;
then_fields :
(('info modulefield, location) annotated list, location) annotated;
else_fields :
(('info modulefield, location) annotated list, location) annotated
option;
}
type 'info module_ = ('info modulefield, location) annotated list