Skip to content

Commit ea3d9b6

Browse files
committed
simplify binary module type
1 parent 45d5f4d commit ea3d9b6

File tree

14 files changed

+351
-388
lines changed

14 files changed

+351
-388
lines changed

example/c/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ int main(void) {
367367
```
368368
369369
```sh
370-
$ owi c --e-acsl primes.c
370+
$ owi c --e-acsl primes.c -w1
371371
Assert failure: false
372372
Model:
373373
(model

src/ast/binary.ml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,27 @@ type elem =
5050

5151
type modul =
5252
{ id : string option
53-
; types : binary rec_type Named.t
54-
; global : (global, binary global_type) Runtime.t Named.t
55-
; table : (binary table, binary table_type) Runtime.t Named.t
56-
; mem : (mem, limits) Runtime.t Named.t
57-
; func : (binary func, binary block_type) Runtime.t Named.t
53+
; types : binary rec_type array
54+
; global : (global, binary global_type) Runtime.t array
55+
; table : (binary table, binary table_type) Runtime.t array
56+
; mem : (mem, limits) Runtime.t array
57+
; func : (binary func, binary block_type) Runtime.t array
5858
(* TODO: switch to func_type *)
59-
; elem : elem Named.t
60-
; data : data Named.t
59+
; elem : elem array
60+
; data : data array
6161
; exports : exports
6262
; start : int option
6363
}
6464

6565
let empty_modul =
6666
{ id = None
67-
; types = Named.empty
68-
; global = Named.empty
69-
; table = Named.empty
70-
; mem = Named.empty
71-
; func = Named.empty
72-
; elem = Named.empty
73-
; data = Named.empty
67+
; types = [||]
68+
; global = [||]
69+
; table = [||]
70+
; mem = [||]
71+
; func = [||]
72+
; elem = [||]
73+
; data = [||]
7474
; exports = { global = []; mem = []; table = []; func = [] }
7575
; start = None
7676
}

src/ast/binary_encoder.ml

Lines changed: 68 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,21 @@ let write_valtype buf vt =
8989
let c = get_char_valtype vt in
9090
Buffer.add_char buf c
9191

92-
let encode_vector buf datas encode_func =
92+
let encode_vector length iter buf datas encode_func =
9393
let vector_buf = Buffer.create 16 in
94-
let len = List.length datas in
95-
List.iter (encode_func vector_buf) datas;
94+
let len = length datas in
95+
iter (encode_func vector_buf) datas;
9696
write_u32_of_int buf len;
9797
Buffer.add_buffer buf vector_buf
9898

99+
let encode_vector_list buf datas encode_func =
100+
encode_vector List.length List.iter buf datas encode_func
101+
102+
let encode_vector_array buf datas encode_func =
103+
encode_vector Array.length Array.iter buf datas encode_func
104+
99105
let write_resulttype buf (rt : _ result_type) =
100-
encode_vector buf rt write_valtype
106+
encode_vector_list buf rt write_valtype
101107

102108
let write_paramtype buf (pt : _ param_type) =
103109
let vt = List.map snd pt in
@@ -111,11 +117,10 @@ let write_block_type buf (typ : binary block_type option) =
111117
match typ with
112118
| None | Some (Bt_raw (None, ([], []))) -> Buffer.add_char buf '\x40'
113119
| Some (Bt_raw (None, ([], [ vt ]))) -> write_valtype buf vt
114-
| Some (Bt_raw (None, (pt, _))) ->
115-
write_paramtype buf pt
116-
(* TODO: memo
117-
will this pattern matching be enough with the use of the new modul.types field?
118-
*)
120+
| Some (Bt_raw (None, (pt, _))) -> write_paramtype buf pt
121+
(* TODO: memo
122+
will this pattern matching be enough with the use of the new modul.types field?
123+
*)
119124
| _ -> assert false (* TODO: same, new pattern matching cases ? *)
120125

121126
let write_block_type_idx buf (typ : binary block_type) =
@@ -200,9 +205,8 @@ let rec write_instr buf instr =
200205
| Br idx -> write_char_indice buf '\x0C' idx
201206
| Br_if idx -> write_char_indice buf '\x0D' idx
202207
| Br_table (idxs, idx) ->
203-
let idxs = Array.to_list idxs in
204208
add_char '\x0E';
205-
encode_vector buf idxs write_indice;
209+
encode_vector_array buf idxs write_indice;
206210
write_indice buf idx
207211
| Return -> add_char '\x0F'
208212
| Call idx -> write_char_indice buf '\x10' idx
@@ -540,12 +544,11 @@ let write_locals buf locals =
540544
let write_element buf ({ typ = _, ht; init; mode; _ } : elem) =
541545
let write_init buf init =
542546
let is_ref_func = ref true in
543-
encode_vector buf init (fun buf expr ->
544-
match expr with
545-
| [ Ref_func idx ] -> write_indice buf idx
546-
| expr ->
547-
write_expr buf expr ~end_op_code:None;
548-
is_ref_func := false );
547+
encode_vector_list buf init (fun buf -> function
548+
| [ Ref_func idx ] -> write_indice buf idx
549+
| expr ->
550+
write_expr buf expr ~end_op_code:None;
551+
is_ref_func := false );
549552
!is_ref_func
550553
in
551554
match mode with
@@ -626,24 +629,21 @@ let encode_section buf id encode_func data =
626629
end
627630

628631
(* type: section 1 *)
629-
let encode_types buf (rec_types : binary rec_type Named.t) =
630-
encode_vector buf rec_types.values
631-
(fun buf (typ : binary rec_type Indexed.t) ->
632-
let typ = Indexed.get typ in
632+
let encode_types buf rec_types =
633+
encode_vector_array buf rec_types (fun buf -> function
634+
| [] -> assert false
635+
| _ :: _ :: _ ->
636+
(* TODO rec types *)
637+
assert false
638+
| [ typ ] -> (
633639
match typ with
634-
| [] -> assert false
635-
| _ :: _ :: _ ->
636-
(* TODO rec types *)
637-
assert false
638-
| [ typ ] -> (
639-
match typ with
640-
| _name, (Final, _idx, Def_func_t (pt, rt)) ->
641-
Buffer.add_char buf '\x60';
642-
write_paramtype buf pt;
643-
write_resulttype buf rt
644-
| _ ->
645-
(* TODO non final types and other type declarations *)
646-
assert false ) )
640+
| _name, (Final, _idx, Def_func_t (pt, rt)) ->
641+
Buffer.add_char buf '\x60';
642+
write_paramtype buf pt;
643+
write_resulttype buf rt
644+
| _ ->
645+
(* TODO non final types and other type declarations *)
646+
assert false ) )
647647

648648
(* import: section 2 *)
649649
let encode_imports buf (funcs, tables, memories, globals) =
@@ -662,20 +662,18 @@ let encode_imports buf (funcs, tables, memories, globals) =
662662
(* function: section 3 *)
663663
let encode_functions buf (funcs : binary func list) =
664664
let idx = ref 0 in
665-
encode_vector buf funcs (fun buf func ->
665+
encode_vector_list buf funcs (fun buf func ->
666666
write_block_type_idx buf func.type_f;
667667
incr idx )
668668

669669
(* table: section 4 *)
670-
let encode_tables buf tables = encode_vector buf tables write_table
670+
let encode_tables buf tables = encode_vector_list buf tables write_table
671671

672672
(* memory: section 5 *)
673-
let encode_memories buf memories = encode_vector buf memories write_memory
673+
let encode_memories buf memories = encode_vector_list buf memories write_memory
674674

675675
(* global: section 6 *)
676-
let encode_globals buf globals =
677-
let globals = List.rev globals in
678-
encode_vector buf globals write_global
676+
let encode_globals buf globals = encode_vector_list buf globals write_global
679677

680678
(* export: section 7 *)
681679
let encode_exports buf ({ global; mem; table; func } : exports) =
@@ -699,74 +697,66 @@ let encode_start buf int_opt =
699697
match int_opt with None -> () | Some funcidx -> write_u32_of_int buf funcidx
700698

701699
(* element: section 9 *)
702-
let encode_elements buf { Named.values = elems; _ } =
703-
encode_vector buf elems (fun buf elem ->
704-
let elem = Indexed.get elem in
705-
write_element buf elem )
700+
let encode_elements buf elems = encode_vector_array buf elems write_element
706701

707702
(* datacount: section 12 *)
708-
let encode_datacount buf { Named.values = datas; _ } =
709-
let len = List.length datas in
703+
let encode_datacount buf datas =
704+
let len = Array.length datas in
710705
write_u32_of_int buf len
711706

712707
(* code: section 10 *)
713708
let encode_codes buf funcs =
714-
encode_vector buf funcs (fun buf { locals; body; _ } ->
709+
encode_vector_list buf funcs (fun buf { locals; body; _ } ->
715710
let code_buf = Buffer.create 16 in
716711
write_locals code_buf locals;
717712
write_expr code_buf body ~end_op_code:None;
718713
write_u32_of_int buf (Buffer.length code_buf);
719714
Buffer.add_buffer buf code_buf )
720715

721716
(* data: section 11 *)
722-
let encode_datas buf { Named.values = datas; _ } =
723-
encode_vector buf datas (fun buf data ->
724-
let data = Indexed.get data in
725-
write_data buf data )
717+
let encode_datas buf datas = encode_vector_array buf datas write_data
726718

727-
let keep_local { Named.values; _ } =
719+
let keep_local values =
728720
List.filter_map
729-
(fun data ->
730-
match Indexed.get data with
731-
| Runtime.Local data -> Some data
732-
| Runtime.Imported _data -> None )
733-
(List.rev values)
721+
(function Runtime.Local data -> Some data | Runtime.Imported _data -> None)
722+
(Array.to_list values)
734723

735-
let keep_imported { Named.values; _ } =
724+
let keep_imported values =
736725
List.filter_map
737-
(fun data ->
738-
match Indexed.get data with
739-
| Runtime.Local _data -> None
740-
| Runtime.Imported data -> Some data )
741-
(List.rev values)
726+
(function Runtime.Local _data -> None | Runtime.Imported data -> Some data)
727+
(Array.to_list values)
742728

743-
let encode (modul : Binary.modul) =
729+
let encode
730+
({ func; table; global; exports; start; data; mem; types; elem; _ } :
731+
Binary.modul ) =
744732
let buf = Buffer.create 256 in
745-
let local_funcs = keep_local modul.func in
746-
let local_tables = keep_local modul.table in
747-
let local_memories = keep_local modul.mem in
748-
let local_globales = keep_local modul.global in
749-
let imported_funcs = keep_imported modul.func in
750-
let imported_tables = keep_imported modul.table in
751-
let imported_memories = keep_imported modul.mem in
752-
let imported_globals = keep_imported modul.global in
733+
734+
let local_funcs = keep_local func in
735+
let local_tables = keep_local table in
736+
let local_memories = keep_local mem in
737+
let local_globales = keep_local global in
738+
let imported_funcs = keep_imported func in
739+
let imported_tables = keep_imported table in
740+
let imported_memories = keep_imported mem in
741+
let imported_globals = keep_imported global in
742+
753743
Buffer.add_string buf "\x00\x61\x73\x6d";
754744
(* magic *)
755745
Buffer.add_string buf "\x01\x00\x00\x00";
756746
(* version *)
757-
encode_section buf '\x01' encode_types modul.types;
747+
encode_section buf '\x01' encode_types types;
758748
encode_section buf '\x02' encode_imports
759749
(imported_funcs, imported_tables, imported_memories, imported_globals);
760750
encode_section buf '\x03' encode_functions local_funcs;
761751
encode_section buf '\x04' encode_tables local_tables;
762752
encode_section buf '\x05' encode_memories local_memories;
763753
encode_section buf '\x06' encode_globals local_globales;
764-
encode_section buf '\x07' encode_exports modul.exports;
765-
encode_section buf '\x08' encode_start modul.start;
766-
encode_section buf '\x09' encode_elements modul.elem;
767-
encode_section buf '\x0C' encode_datacount modul.data;
754+
encode_section buf '\x07' encode_exports exports;
755+
encode_section buf '\x08' encode_start start;
756+
encode_section buf '\x09' encode_elements elem;
757+
encode_section buf '\x0C' encode_datacount data;
768758
encode_section buf '\x0A' encode_codes local_funcs;
769-
encode_section buf '\x0B' encode_datas modul.data;
759+
encode_section buf '\x0B' encode_datas data;
770760
Buffer.contents buf
771761

772762
let write_file filename content =

0 commit comments

Comments
 (0)