Skip to content

Commit d0eee4a

Browse files
committed
Consistent ordering
1 parent e434c34 commit d0eee4a

File tree

16 files changed

+500
-519
lines changed

16 files changed

+500
-519
lines changed

interpreter/binary/decode.ml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -286,23 +286,23 @@ let limits uN s =
286286
let max = opt uN has_max s in
287287
at, {min; max}
288288

289-
let tabletype s =
290-
let t = reftype s in
291-
let at, lim = limits u64 s in
292-
TableT (at, lim, t)
293-
294-
let memorytype s =
295-
let at, lim = limits u64 s in
296-
MemoryT (at, lim)
289+
let tagtype s =
290+
zero s;
291+
TagT (typeuse idx s)
297292

298293
let globaltype s =
299294
let t = valtype s in
300295
let mut = mutability s in
301296
GlobalT (mut, t)
302297

303-
let tagtype s =
304-
zero s;
305-
TagT (typeuse idx s)
298+
let memorytype s =
299+
let at, lim = limits u64 s in
300+
MemoryT (at, lim)
301+
302+
let tabletype s =
303+
let t = reftype s in
304+
let at, lim = limits u64 s in
305+
TableT (at, lim, t)
306306

307307
let externtype s =
308308
match byte s with
@@ -1052,13 +1052,13 @@ let id s =
10521052
| _ -> error s (pos s) "malformed section id"
10531053
) bo
10541054

1055-
let section_with_size tag f default s =
1055+
let section_with_size kind f default s =
10561056
match id s with
1057-
| Some tag' when tag' = tag -> skip 1 s; sized f s
1057+
| Some kind' when kind' = kind -> skip 1 s; sized f s
10581058
| _ -> default
10591059

1060-
let section tag f default s =
1061-
section_with_size tag (fun _ -> f) default s
1060+
let section kind f default s =
1061+
section_with_size kind (fun _ -> f) default s
10621062

10631063

10641064
(* Type section *)

interpreter/binary/encode.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,17 @@ struct
195195
let flags = flag (max <> None) 0 + flag (at = I64AT) 2 in
196196
byte flags; u64 min; opt u64 max
197197

198-
let tabletype = function
199-
| TableT (at, lim, t) -> reftype t; limits at lim
200-
201-
let memorytype = function
202-
| MemoryT (at, lim) -> limits at lim
198+
let tagtype = function
199+
| TagT ut -> u32 0x00l; typeuse u32 ut
203200

204201
let globaltype = function
205202
| GlobalT (mut, t) -> valtype t; mutability mut
206203

207-
let tagtype = function
208-
| TagT ut -> u32 0x00l; typeuse u32 ut
204+
let memorytype = function
205+
| MemoryT (at, lim) -> limits at lim
206+
207+
let tabletype = function
208+
| TableT (at, lim, t) -> reftype t; limits at lim
209209

210210
let externtype = function
211211
| ExternFuncT ut -> byte 0x00; typeuse u32 ut

interpreter/custom/custom.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ type section_kind =
77
| Func
88
| Table
99
| Memory
10+
| Tag
1011
| Global
1112
| Export
1213
| Start
1314
| Elem
1415
| DataCount
1516
| Code
1617
| Data
17-
| Tag
1818

1919
type place =
2020
| Before of section_kind

interpreter/custom/handler_custom.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ and parse_annot m annot =
5151
let place, items'' = parse_place_opt items' in
5252
let content, items''' = parse_content items'' in
5353
parse_end items''';
54-
let Ast.{types; tags; globals; tables; memories; funcs; start;
55-
elems; datas; imports; exports} = m.it in
54+
let Ast.{types; tags; globals; memories; tables; funcs; start;
55+
datas; elems; imports; exports} = m.it in
5656
let outside x =
5757
if annot.at.left >= x.at.left && annot.at.right <= x.at.right then
5858
parse_error annot.at "misplaced @custom annotation"
5959
in
6060
List.iter outside types;
6161
List.iter outside tags;
6262
List.iter outside globals;
63-
List.iter outside tables;
6463
List.iter outside memories;
64+
List.iter outside tables;
6565
List.iter outside funcs;
6666
List.iter outside (Option.to_list start);
6767
List.iter outside elems;
@@ -97,11 +97,11 @@ and parse_direction at = function
9797

9898
and parse_section at = function
9999
| {it = Atom "type"; _} :: items -> Type, items
100-
| {it = Atom "tag"; _} :: items -> Tag, items
101100
| {it = Atom "import"; _} :: items -> Import, items
102101
| {it = Atom "func"; _} :: items -> Func, items
103102
| {it = Atom "table"; _} :: items -> Table, items
104103
| {it = Atom "memory"; _} :: items -> Memory, items
104+
| {it = Atom "tag"; _} :: items -> Tag, items
105105
| {it = Atom "global"; _} :: items -> Global, items
106106
| {it = Atom "export"; _} :: items -> Export, items
107107
| {it = Atom "start"; _} :: items -> Start, items
@@ -146,11 +146,11 @@ and arrange_place = function
146146
and arrange_sec = function
147147
| Custom -> assert false
148148
| Type -> "type"
149-
| Tag -> "tag"
150149
| Import -> "import"
151150
| Func -> "func"
152151
| Table -> "table"
153152
| Memory -> "memory"
153+
| Tag -> "tag"
154154
| Global -> "global"
155155
| Export -> "export"
156156
| Start -> "start"

interpreter/custom/handler_name.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,18 @@ let locate_type bs x name at (ty : type_) =
333333
let locate_module bs name at (m : module_) =
334334
if not (is_contained at m.at) then
335335
parse_error at "misplaced @name annotation";
336-
let {types; tags; globals; tables; memories; funcs; start;
337-
elems; datas; imports; exports} = m.it in
336+
let {types; tags; globals; memories; tables; funcs; start;
337+
datas; elems; imports; exports} = m.it in
338338
let ats =
339339
List.map (fun p -> p.at) types @
340340
List.map (fun p -> p.at) tags @
341341
List.map (fun p -> p.at) globals @
342-
List.map (fun p -> p.at) tables @
343342
List.map (fun p -> p.at) memories @
343+
List.map (fun p -> p.at) tables @
344344
List.map (fun p -> p.at) funcs @
345345
List.map (fun p -> p.at) (Option.to_list start) @
346-
List.map (fun p -> p.at) elems @
347346
List.map (fun p -> p.at) datas @
347+
List.map (fun p -> p.at) elems @
348348
List.map (fun p -> p.at) imports @
349349
List.map (fun p -> p.at) exports |> List.sort compare
350350
in

interpreter/exec/eval.ml

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,19 @@ let lookup category list x =
101101
Crash.error x.at ("undefined " ^ category ^ " " ^ Int32.to_string x.it)
102102

103103
let type_ (inst : moduleinst) x = lookup "type" inst.types x
104-
let func (inst : moduleinst) x = lookup "function" inst.funcs x
105-
let table (inst : moduleinst) x = lookup "table" inst.tables x
106-
let memory (inst : moduleinst) x = lookup "memory" inst.memories x
107104
let tag (inst : moduleinst) x = lookup "tag" inst.tags x
108105
let global (inst : moduleinst) x = lookup "global" inst.globals x
109-
let elem (inst : moduleinst) x = lookup "element segment" inst.elems x
106+
let memory (inst : moduleinst) x = lookup "memory" inst.memories x
107+
let table (inst : moduleinst) x = lookup "table" inst.tables x
108+
let func (inst : moduleinst) x = lookup "function" inst.funcs x
110109
let data (inst : moduleinst) x = lookup "data segment" inst.datas x
110+
let elem (inst : moduleinst) x = lookup "element segment" inst.elems x
111111
let local (frame : frame) x = lookup "local" frame.locals x
112112

113113
let comp_type (inst : moduleinst) x = expand_deftype (type_ inst x)
114-
let func_type (inst : moduleinst) x = functype_of_comptype (comp_type inst x)
115114
let struct_type (inst : moduleinst) x = structtype_of_comptype (comp_type inst x)
116115
let array_type (inst : moduleinst) x = arraytype_of_comptype (comp_type inst x)
116+
let func_type (inst : moduleinst) x = functype_of_comptype (comp_type inst x)
117117

118118
let subst_of (inst : moduleinst) = function
119119
| Idx x when x < Lib.List32.length inst.types ->
@@ -1190,16 +1190,11 @@ let init_import (inst : moduleinst) (ex : extern) (im : import) : moduleinst =
11901190
"expected " ^ Types.string_of_externtype xt ^
11911191
", got " ^ Types.string_of_externtype xt');
11921192
match ex with
1193-
| ExternFunc func -> {inst with funcs = inst.funcs @ [func]}
1194-
| ExternTable tab -> {inst with tables = inst.tables @ [tab]}
1195-
| ExternMemory mem -> {inst with memories = inst.memories @ [mem]}
1196-
| ExternGlobal glob -> {inst with globals = inst.globals @ [glob]}
11971193
| ExternTag tag -> {inst with tags = inst.tags @ [tag]}
1198-
1199-
let init_func (inst : moduleinst) (f : func) : moduleinst =
1200-
let Func (x, _, _) = f.it in
1201-
let func = Func.alloc (type_ inst x) (Lib.Promise.make ()) f in
1202-
{inst with funcs = inst.funcs @ [func]}
1194+
| ExternGlobal glob -> {inst with globals = inst.globals @ [glob]}
1195+
| ExternMemory mem -> {inst with memories = inst.memories @ [mem]}
1196+
| ExternTable tab -> {inst with tables = inst.tables @ [tab]}
1197+
| ExternFunc func -> {inst with funcs = inst.funcs @ [func]}
12031198

12041199
let init_tag (inst : moduleinst) (tag : tag) : moduleinst =
12051200
let Tag tt = tag.it in
@@ -1214,6 +1209,12 @@ let init_global (inst : moduleinst) (glob : global) : moduleinst =
12141209
let glob = Global.alloc gt' v in
12151210
{inst with globals = inst.globals @ [glob]}
12161211

1212+
let init_memory (inst : moduleinst) (mem : memory) : moduleinst =
1213+
let Memory mt = mem.it in
1214+
let mt' = subst_memorytype (subst_of inst) mt in
1215+
let mem = Memory.alloc mt' in
1216+
{inst with memories = inst.memories @ [mem]}
1217+
12171218
let init_table (inst : moduleinst) (tab : table) : moduleinst =
12181219
let Table (tt, c) = tab.it in
12191220
let tt' = subst_tabletype (subst_of inst) tt in
@@ -1225,31 +1226,30 @@ let init_table (inst : moduleinst) (tab : table) : moduleinst =
12251226
let tab = Table.alloc tt' r in
12261227
{inst with tables = inst.tables @ [tab]}
12271228

1228-
let init_memory (inst : moduleinst) (mem : memory) : moduleinst =
1229-
let Memory mt = mem.it in
1230-
let mt' = subst_memorytype (subst_of inst) mt in
1231-
let mem = Memory.alloc mt' in
1232-
{inst with memories = inst.memories @ [mem]}
1233-
1234-
let init_elem (inst : moduleinst) (elem : elem) : moduleinst =
1235-
let Elem (rt, cs, _emode) = elem.it in
1236-
let elem = Elem.alloc (List.map (fun c -> as_ref (eval_const inst c)) cs) in
1237-
{inst with elems = inst.elems @ [elem]}
1229+
let init_func (inst : moduleinst) (f : func) : moduleinst =
1230+
let Func (x, _, _) = f.it in
1231+
let func = Func.alloc (type_ inst x) (Lib.Promise.make ()) f in
1232+
{inst with funcs = inst.funcs @ [func]}
12381233

12391234
let init_data (inst : moduleinst) (data : data) : moduleinst =
12401235
let Data (bs, _dmode) = data.it in
12411236
let data = Data.alloc bs in
12421237
{inst with datas = inst.datas @ [data]}
12431238

1239+
let init_elem (inst : moduleinst) (elem : elem) : moduleinst =
1240+
let Elem (rt, cs, _emode) = elem.it in
1241+
let elem = Elem.alloc (List.map (fun c -> as_ref (eval_const inst c)) cs) in
1242+
{inst with elems = inst.elems @ [elem]}
1243+
12441244
let init_export (inst : moduleinst) (ex : export) : moduleinst =
12451245
let Export (name, xx) = ex.it in
12461246
let ext =
12471247
match xx.it with
1248-
| FuncX x -> ExternFunc (func inst x)
1249-
| TableX x -> ExternTable (table inst x)
1250-
| MemoryX x -> ExternMemory (memory inst x)
1251-
| GlobalX x -> ExternGlobal (global inst x)
12521248
| TagX x -> ExternTag (tag inst x)
1249+
| GlobalX x -> ExternGlobal (global inst x)
1250+
| MemoryX x -> ExternMemory (memory inst x)
1251+
| TableX x -> ExternTable (table inst x)
1252+
| FuncX x -> ExternFunc (func inst x)
12531253
in
12541254
{inst with exports = inst.exports @ [(name, ext)]}
12551255

@@ -1308,18 +1308,18 @@ let init (m : module_) (exts : extern list) : moduleinst =
13081308
empty_moduleinst
13091309
|> init_list init_type m.it.types
13101310
|> init_list2 init_import exts m.it.imports
1311+
|> init_list init_tag m.it.tags
13111312
|> init_list init_func m.it.funcs
13121313
|> init_list init_global m.it.globals
13131314
|> init_list init_table m.it.tables
13141315
|> init_list init_memory m.it.memories
1315-
|> init_list init_tag m.it.tags
1316-
|> init_list init_elem m.it.elems
13171316
|> init_list init_data m.it.datas
1317+
|> init_list init_elem m.it.elems
13181318
|> init_list init_export m.it.exports
13191319
in
13201320
List.iter (init_funcinst inst) inst.funcs;
1321-
let es_elem = List.concat (Lib.List32.mapi run_elem m.it.elems) in
13221321
let es_data = List.concat (Lib.List32.mapi run_data m.it.datas) in
1322+
let es_elem = List.concat (Lib.List32.mapi run_elem m.it.elems) in
13231323
let es_start = Lib.Option.get (Lib.Option.map run_start m.it.start) [] in
13241324
ignore (eval (config inst [] (List.map plain (es_elem @ es_data @ es_start))));
13251325
inst

interpreter/runtime/instance.ml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@ open Types
33
type moduleinst =
44
{
55
types : typeinst list;
6-
funcs : funcinst list;
7-
tables : tableinst list;
8-
memories : memoryinst list;
9-
globals : globalinst list;
106
tags : taginst list;
11-
elems : eleminst list;
7+
globals : globalinst list;
8+
memories : memoryinst list;
9+
tables : tableinst list;
10+
funcs : funcinst list;
1211
datas : datainst list;
12+
elems : eleminst list;
1313
exports : exportinst list;
1414
}
1515

1616
and typeinst = deftype
17-
and funcinst = moduleinst Lib.Promise.t Func.t
18-
and tableinst = Table.t
19-
and memoryinst = Memory.t
20-
and globalinst = Global.t
2117
and taginst = Tag.t
22-
and eleminst = Elem.t
18+
and globalinst = Global.t
19+
and memoryinst = Memory.t
20+
and tableinst = Table.t
21+
and funcinst = moduleinst Lib.Promise.t Func.t
2322
and datainst = Data.t
23+
and eleminst = Elem.t
2424
and exportinst = Ast.name * extern
2525

2626
and extern =
27-
| ExternFunc of funcinst
28-
| ExternTable of tableinst
29-
| ExternMemory of memoryinst
30-
| ExternGlobal of globalinst
3127
| ExternTag of taginst
28+
| ExternGlobal of globalinst
29+
| ExternMemory of memoryinst
30+
| ExternTable of tableinst
31+
| ExternFunc of funcinst
3232

3333

3434
(* Reference types *)
@@ -57,25 +57,25 @@ let () =
5757

5858
(* Projections *)
5959

60-
let funcinst_of_extern = function ExternFunc f -> f | _ -> failwith "funcinst_of_extern"
61-
let tableinst_of_extern = function ExternTable t -> t | _ -> failwith "tableinst_of_extern"
62-
let memoryinst_of_extern = function ExternMemory m -> m | _ -> failwith "memoryinst_of_extern"
63-
let globalinst_of_extern = function ExternGlobal g -> g | _ -> failwith "globalinst_of_extern"
6460
let taginst_of_extern = function ExternTag t -> t | _ -> failwith "taginst_of_extern"
61+
let globalinst_of_extern = function ExternGlobal g -> g | _ -> failwith "globalinst_of_extern"
62+
let memoryinst_of_extern = function ExternMemory m -> m | _ -> failwith "memoryinst_of_extern"
63+
let tableinst_of_extern = function ExternTable t -> t | _ -> failwith "tableinst_of_extern"
64+
let funcinst_of_extern = function ExternFunc f -> f | _ -> failwith "funcinst_of_extern"
6565

6666

6767
(* Auxiliary functions *)
6868

6969
let empty_moduleinst =
70-
{ types = []; funcs = []; tables = []; memories = []; globals = [];
71-
tags = []; elems = []; datas = []; exports = [] }
70+
{ types = []; tags = []; globals = []; memories = []; tables = []; funcs = [];
71+
datas = []; elems = []; exports = [] }
7272

7373
let externtype_of c = function
74-
| ExternFunc func -> ExternFuncT (Def (Func.type_of func))
75-
| ExternTable tab -> ExternTableT (Table.type_of tab)
76-
| ExternMemory mem -> ExternMemoryT (Memory.type_of mem)
77-
| ExternGlobal glob -> ExternGlobalT (Global.type_of glob)
7874
| ExternTag tag -> ExternTagT (Tag.type_of tag)
75+
| ExternGlobal glob -> ExternGlobalT (Global.type_of glob)
76+
| ExternMemory mem -> ExternMemoryT (Memory.type_of mem)
77+
| ExternTable tab -> ExternTableT (Table.type_of tab)
78+
| ExternFunc func -> ExternFuncT (Def (Func.type_of func))
7979

8080
let export inst name =
8181
try Some (List.assoc name inst.exports) with Not_found -> None

0 commit comments

Comments
 (0)