Skip to content

Commit 709ed97

Browse files
committed
Add TypeOf as a kind of heap_type (which, given a function id returns its type)
1 parent b9185e2 commit 709ed97

File tree

10 files changed

+81
-27
lines changed

10 files changed

+81
-27
lines changed

src/concrete/concrete_ref.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ let pp fmt = function
2222
| Extern _ -> pf fmt "externref"
2323
| Func _ -> pf fmt "funcref"
2424

25-
let null = function Text.Func_ht -> Func None | Extern_ht -> Extern None
25+
(* TODO: Is this the same as Symbolic_ref.null? *)
26+
let null = function
27+
| Text.Func_ht -> Func None
28+
| Extern_ht -> Extern None
29+
| TypeOf _ -> (* TODO: Func None? *) assert false
2630

2731
let func (f : Kind.func) = Func (Some f)
2832

src/ir/binary_encoder.ml

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,26 @@ let write_char_indice buf c idx =
6969
Buffer.add_char buf c;
7070
write_indice buf idx
7171

72-
let write_reftype buf ht =
73-
match ht with
74-
| Text.Func_ht -> Buffer.add_char buf '\x70'
75-
| Extern_ht -> Buffer.add_char buf '\x6F'
72+
let write_reftype buf nullable ht =
73+
match nullable with
74+
| Text.Null -> begin
75+
match ht with
76+
| Text.Extern_ht -> Buffer.add_char buf '\x6F'
77+
| Func_ht -> Buffer.add_char buf '\x70'
78+
| TypeOf (Raw id) -> write_char_indice buf '\x63' id
79+
| TypeOf _ -> assert false
80+
end
81+
| No_null -> begin
82+
Buffer.add_char buf '\x64';
83+
match ht with
84+
| Text.Func_ht -> Buffer.add_char buf '\x70'
85+
| Extern_ht -> Buffer.add_char buf '\x6F'
86+
| TypeOf (Raw id) -> write_indice buf id
87+
| TypeOf _ -> assert false
88+
end
89+
(* TODO: TypeOf (Text id) Unreachable because there are no text ids in binary
90+
format, the proper way to do it is by redefining ref_type and heap_type for
91+
the binary format but it requires lots of changes. *)
7692

7793
let get_char_valtype = function
7894
| Text.Num_type I32 -> '\x7F'
@@ -159,12 +175,12 @@ let write_memory_import buf
159175
write_limits buf limits
160176

161177
let write_table_import buf
162-
({ modul_name; name; typ = limits, (_nullable, heaptype); _ } :
178+
({ modul_name; name; typ = limits, (nullable, heaptype); _ } :
163179
Text.Table.Type.t Origin.imported ) =
164180
write_string buf modul_name;
165181
write_string buf name;
166182
Buffer.add_char buf '\x01';
167-
write_reftype buf heaptype;
183+
write_reftype buf nullable heaptype;
168184
write_limits buf limits
169185

170186
let write_func_import buf
@@ -441,7 +457,7 @@ let rec write_instr buf instr =
441457
| I64_extend32_s -> add_char '\xC4'
442458
| Ref_null rt ->
443459
add_char '\xD0';
444-
write_reftype buf rt
460+
write_reftype buf Text.Null rt
445461
| Ref_is_null -> add_char '\xD1'
446462
| Ref_func idx -> write_char_indice buf '\xD2' idx
447463
| I_trunc_sat_f (S32, S32, S) -> write_fc buf 0
@@ -509,16 +525,16 @@ and write_expr buf expr ~end_op_code =
509525
let end_op_code = Option.value end_op_code ~default:'\x0B' in
510526
Buffer.add_char buf end_op_code
511527

512-
let write_table buf { Table.typ = limits, (_nullable, heaptype); init; _ } =
528+
let write_table buf { Table.typ = limits, (nullable, heaptype); init; _ } =
513529
match init with
514530
| Some e ->
515531
Buffer.add_char buf '\x40';
516532
Buffer.add_char buf '\x00';
517-
write_reftype buf heaptype;
533+
write_reftype buf nullable heaptype;
518534
write_limits buf limits;
519535
write_expr buf e ~end_op_code:None
520536
| None ->
521-
write_reftype buf heaptype;
537+
write_reftype buf nullable heaptype;
522538
write_limits buf limits
523539

524540
let write_export buf cid ({ name; id } : Binary.Export.t) =
@@ -559,7 +575,7 @@ let write_locals buf locals =
559575
Buffer.add_char buf char )
560576
compressed
561577

562-
let write_element buf ({ typ = _, ht; init; mode; _ } : Elem.t) =
578+
let write_element buf ({ typ = nullable, ht; init; mode; _ } : Elem.t) =
563579
let write_init buf init =
564580
let is_ref_func = ref true in
565581
encode_vector_list buf init (fun buf expr ->
@@ -581,7 +597,7 @@ let write_element buf ({ typ = _, ht; init; mode; _ } : Elem.t) =
581597
end
582598
else begin
583599
write_u32_of_int buf 5;
584-
write_reftype buf ht;
600+
write_reftype buf nullable ht;
585601
Buffer.add_buffer buf elem_buf
586602
end
587603
| Declarative ->
@@ -594,7 +610,7 @@ let write_element buf ({ typ = _, ht; init; mode; _ } : Elem.t) =
594610
end
595611
else begin
596612
write_u32_of_int buf 7;
597-
write_reftype buf ht;
613+
write_reftype buf nullable ht;
598614
Buffer.add_buffer buf elem_buf
599615
end
600616
| Active (Some 0, expr) ->
@@ -617,7 +633,7 @@ let write_element buf ({ typ = _, ht; init; mode; _ } : Elem.t) =
617633
write_u32_of_int buf 6;
618634
write_indice buf i;
619635
write_expr buf expr ~end_op_code:None;
620-
write_reftype buf ht;
636+
write_reftype buf nullable ht;
621637
Buffer.add_buffer buf elem_buf
622638
end
623639
| _ -> assert false

src/ir/text.ml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ let pp_indice_not0 fmt = function
2727

2828
let pp_indice_opt fmt = function None -> () | Some i -> pp_indice fmt i
2929

30+
let compare_indice id1 id2 =
31+
match (id1, id2) with
32+
| Text s1, Text s2 -> String.compare s1 s2
33+
| Raw i1, Raw i2 -> Int.compare i1 i2
34+
| Text _, Raw _ -> 1
35+
| Raw _, Text _ -> -1
36+
3037
type nonrec num_type =
3138
| I32
3239
| I64
@@ -240,23 +247,32 @@ let pp_limits fmt { min; max } =
240247
(** Types *)
241248

242249
type heap_type =
250+
| TypeOf of indice
251+
(* abs_heap_type *)
243252
| Func_ht
244253
| Extern_ht
245254

246255
let pp_heap_type fmt = function
256+
| TypeOf id -> pf fmt "%a" pp_indice id
247257
| Func_ht -> pf fmt "func"
248258
| Extern_ht -> pf fmt "extern"
249259

250260
let heap_type_eq t1 t2 =
251261
(* TODO: this is wrong *)
252262
match (t1, t2) with
253263
| Func_ht, Func_ht | Extern_ht, Extern_ht -> true
264+
| TypeOf id1, TypeOf id2 -> compare_indice id1 id2 = 0
254265
| _, _ -> false
255266

256267
let compare_heap_type t1 t2 =
257268
(* TODO: this is wrong *)
258-
let to_int = function Func_ht -> 0 | Extern_ht -> 1 in
259-
Int.compare (to_int t1) (to_int t2)
269+
match (t1, t2) with
270+
| Func_ht, Func_ht | Extern_ht, Extern_ht -> 0
271+
| TypeOf id1, TypeOf id2 -> compare_indice id1 id2
272+
| TypeOf _, _ -> 1
273+
| _, TypeOf _ -> -1
274+
| Extern_ht, _ -> 1
275+
| _, Extern_ht -> -1
260276

261277
type nonrec ref_type = nullable * heap_type
262278

src/ir/text.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ val pp_indice : Format.formatter -> indice -> unit
1818

1919
val pp_indice_opt : Format.formatter -> indice option -> unit
2020

21+
val compare_indice : indice -> indice -> int
22+
2123
type nonrec num_type =
2224
| I32
2325
| I64
@@ -160,6 +162,8 @@ val pp_limits : Format.formatter -> limits -> unit
160162
(** Types *)
161163

162164
type heap_type =
165+
| TypeOf of indice
166+
(* abs_heap_type *)
163167
| Func_ht
164168
| Extern_ht
165169

src/ir/wast.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,12 @@ type result =
5353
| Result_extern_ref
5454
| Result_func_ref
5555

56-
let pp_result fmt = function
57-
| Result_const c -> pf fmt "(%a)" pp_result_const c
58-
| Result_func_ref | Result_extern_ref -> assert false
59-
6056
let pp_result_bis fmt = function
6157
| Result_const c -> pf fmt "%a" pp_result_const c
62-
| Result_extern_ref | Result_func_ref -> assert false
58+
| Result_extern_ref -> pf fmt "ref.extern"
59+
| Result_func_ref -> pf fmt "ref.func"
60+
61+
let pp_result fmt r = Fmt.pf fmt "(%a)" pp_result_bis r
6362

6463
let pp_results fmt r = list ~sep:sp pp_result_bis fmt r
6564

src/owi.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ module Text : sig
230230
(** Types *)
231231

232232
type heap_type =
233+
| TypeOf of indice
234+
(* abs_heap_type *)
233235
| Func_ht
234236
| Extern_ht
235237

src/parser/text_parser.mly

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ let null_opt ==
133133
let heap_type ==
134134
| FUNC; { Func_ht }
135135
| EXTERN; { Extern_ht }
136+
| ~ = indice; <TypeOf>
136137

137138
let ref_type ==
138139
| LPAR; REF; ~ = null_opt; ~ = heap_type; RPAR; <>

src/script/script.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ let compare_result_const result (const : Concrete_value.t) =
112112
| Result_const (Nan_arith S64), F64 f ->
113113
let pos_nan = Float64.to_bits Float64.pos_nan in
114114
Int64.eq (Int64.logand (Float64.to_bits f) pos_nan) pos_nan
115+
| Result_func_ref, Ref (Func (Some (Wasm _))) ->
116+
(* TODO: FIX! This is probably unsound! *)
117+
true
115118
| Result_const (Nan_arith _), _
116119
| Result_const (Nan_canon _), _
117120
| Result_const (Literal (Const_I32 _)), _
@@ -121,8 +124,10 @@ let compare_result_const result (const : Concrete_value.t) =
121124
| Result_const (Literal (Const_null _)), _
122125
| Result_const (Literal (Const_host _)), _ ->
123126
false
124-
| _ ->
125-
Log.err (fun m -> m "TODO: unimplemented Script.compare_result_const");
127+
| _, _ ->
128+
Log.err (fun m ->
129+
m "TODO: unimplemented Script.compare_result_const %a %a" Wast.pp_result
130+
result Concrete_value.pp const );
126131
assert false
127132

128133
let value_of_const : Wast.const -> Concrete_value.t = function

src/symbolic/symbolic_ref.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ let pp fmt = function
2222
| Extern _ -> pf fmt "externref"
2323
| Func _ -> pf fmt "funcref"
2424

25-
let null = function Text.Func_ht -> Func None | Extern_ht -> Extern None
25+
let null = function
26+
| Text.Func_ht -> Func None
27+
| Extern_ht -> Extern None
28+
| TypeOf _ -> assert false
29+
(* TODO: Should this be Func None? *)
2630

2731
let func (f : Kind.func) = Func (Some f)
2832

src/validate/binary_validate.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,11 @@ end = struct
168168
match (required, got) with
169169
| Text.Func_ht, Text.Func_ht -> true
170170
| Extern_ht, Extern_ht -> true
171-
| Func_ht, Extern_ht -> false
172-
| Extern_ht, Func_ht -> false
171+
| TypeOf id1, TypeOf id2 ->
172+
(* TODO: This should probably check the types of id1 and id2,
173+
not the ids themselves *)
174+
Text.compare_indice id1 id2 = 0
175+
| _ -> false
173176

174177
let match_types required got =
175178
match (required, got) with

0 commit comments

Comments
 (0)