Skip to content

Commit db86964

Browse files
committed
Wasm AST: add heap types 'struct', 'array' and 'none'
1 parent dfbb093 commit db86964

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

compiler/lib-wasm/code_generation.ml

+26-5
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,40 @@ let heap_type_sub (ty : W.heap_type) (ty' : W.heap_type) st =
161161
match ty, ty' with
162162
| Func, Func
163163
| Extern, Extern
164-
| (Any | Eq | I31 | Type _), Any
165-
| (Eq | I31 | Type _), Eq
166-
| I31, I31 -> true, st
164+
| (Any | Eq | Struct | Array | I31 | None_ | Type _), Any
165+
| (Eq | Struct | Array | I31 | None_ | Type _), Eq
166+
| (None_ | Struct), Struct -> true, st
167+
| (None_ | Array), Array -> true, st
168+
| (None_ | I31), I31 -> true, st
169+
| None_, None_ -> true, st
170+
| Type t, Struct ->
171+
( (let type_field = Hashtbl.find st.context.types t in
172+
match type_field.typ with
173+
| Struct _ -> true
174+
| Array _ | Func _ -> false)
175+
, st )
176+
| Type t, Array ->
177+
( (let type_field = Hashtbl.find st.context.types t in
178+
match type_field.typ with
179+
| Array _ -> true
180+
| Struct _ | Func _ -> false)
181+
, st )
167182
| Type t, Type t' -> type_index_sub t t' st
183+
| None_, Type t ->
184+
( (let type_field = Hashtbl.find st.context.types t in
185+
match type_field.typ with
186+
| Struct _ | Array _ -> true
187+
| Func _ -> false)
188+
, st )
168189
(* Func and Extern are only in suptyping relation with themselves *)
169190
| Func, _
170191
| _, Func
171192
| Extern, _
172193
| _, Extern
173194
(* Any has no supertype *)
174195
| Any, _
175-
(* I31, struct and arrays have no subtype (of a different kind) *)
176-
| _, (I31 | Type _) -> false, st
196+
(* I31, struct, array and none have no other subtype *)
197+
| _, (I31 | Type _ | Struct | Array | None_) -> false, st
177198

178199
let register_global name ?exported_name ?(constant = false) typ init st =
179200
st.context.other_fields <-

compiler/lib-wasm/gc_target.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ module Value = struct
479479
| W.RefI31 _ -> (
480480
match typ.typ with
481481
| W.I31 | Eq | Any -> return (W.Const (I32 1l))
482-
| Type _ | Func | Extern -> return (W.Const (I32 0l)))
482+
| Struct | Array | Type _ | None_ | Func | Extern -> return (W.Const (I32 0l)))
483483
| GlobalGet nm -> (
484484
let* init = get_global nm in
485485
match init with

compiler/lib-wasm/wasm_ast.ml

+3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ type heap_type =
2727
| Extern
2828
| Any
2929
| Eq
30+
| Struct
31+
| Array
3032
| I31
33+
| None_
3134
| Type of var
3235

3336
type ref_type =

compiler/lib-wasm/wasm_output.ml

+4
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,17 @@ end = struct
172172
res
173173

174174
(****)
175+
175176
let output_heaptype type_names ch typ =
176177
match (typ : heap_type) with
178+
| None_ -> output_byte ch 0x71
177179
| Func -> output_byte ch 0x70
178180
| Extern -> output_byte ch 0x6F
179181
| Any -> output_byte ch 0x6E
180182
| Eq -> output_byte ch 0x6D
181183
| I31 -> output_byte ch 0x6C
184+
| Struct -> output_byte ch 0x6B
185+
| Array -> output_byte ch 0x6A
182186
| Type nm -> output_sint ch (Hashtbl.find type_names nm)
183187

184188
let output_valtype type_names ch (typ : value_type) =

compiler/lib-wasm/wat_output.ml

+3
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ let heap_type st (ty : heap_type) =
150150
| Extern -> Atom "extern"
151151
| Any -> Atom "any"
152152
| Eq -> Atom "eq"
153+
| Struct -> Atom "struct"
154+
| Array -> Atom "array"
153155
| I31 -> Atom "i31"
156+
| None_ -> Atom "none"
154157
| Type t -> index st.type_names t
155158

156159
let ref_type st { nullable; typ } =

0 commit comments

Comments
 (0)