|
| 1 | +open Source |
| 2 | +open Ast |
| 3 | + |
| 4 | +module Set = Set.Make(Int32) |
| 5 | + |
| 6 | +type t = |
| 7 | +{ |
| 8 | + types : Set.t; |
| 9 | + globals : Set.t; |
| 10 | + tables : Set.t; |
| 11 | + memories : Set.t; |
| 12 | + funcs : Set.t; |
| 13 | + elems : Set.t; |
| 14 | + datas : Set.t; |
| 15 | + locals : Set.t; |
| 16 | + labels : Set.t; |
| 17 | +} |
| 18 | + |
| 19 | +let empty : t = |
| 20 | +{ |
| 21 | + types = Set.empty; |
| 22 | + globals = Set.empty; |
| 23 | + tables = Set.empty; |
| 24 | + memories = Set.empty; |
| 25 | + funcs = Set.empty; |
| 26 | + elems = Set.empty; |
| 27 | + datas = Set.empty; |
| 28 | + locals = Set.empty; |
| 29 | + labels = Set.empty; |
| 30 | +} |
| 31 | + |
| 32 | +let union (s1 : t) (s2 : t) : t = |
| 33 | +{ |
| 34 | + types = Set.union s1.types s2.types; |
| 35 | + globals = Set.union s1.globals s2.globals; |
| 36 | + tables = Set.union s1.tables s2.tables; |
| 37 | + memories = Set.union s1.memories s2.memories; |
| 38 | + funcs = Set.union s1.funcs s2.funcs; |
| 39 | + elems = Set.union s1.elems s2.elems; |
| 40 | + datas = Set.union s1.datas s2.datas; |
| 41 | + locals = Set.union s1.locals s2.locals; |
| 42 | + labels = Set.union s1.labels s2.labels; |
| 43 | +} |
| 44 | + |
| 45 | +let types s = {empty with types = s} |
| 46 | +let globals s = {empty with globals = s} |
| 47 | +let tables s = {empty with tables = s} |
| 48 | +let memories s = {empty with memories = s} |
| 49 | +let funcs s = {empty with funcs = s} |
| 50 | +let elems s = {empty with elems = s} |
| 51 | +let datas s = {empty with datas = s} |
| 52 | +let locals s = {empty with locals = s} |
| 53 | +let labels s = {empty with labels = s} |
| 54 | + |
| 55 | +let var x = Set.singleton x.it |
| 56 | +let zero = Set.singleton 0l |
| 57 | +let shift s = Set.map (Int32.add (-1l)) (Set.remove 0l s) |
| 58 | + |
| 59 | +let (++) = union |
| 60 | +let list free xs = List.fold_left union empty (List.map free xs) |
| 61 | + |
| 62 | +let rec instr (e : instr) = |
| 63 | + match e.it with |
| 64 | + | Unreachable | Nop | Drop | Select -> empty |
| 65 | + | Const _ | Test _ | Compare _ | Unary _ | Binary _ | Convert _ -> empty |
| 66 | + | Block (_, es) | Loop (_, es) -> block es |
| 67 | + | If (_, es1, es2) -> block es1 ++ block es2 |
| 68 | + | Br x | BrIf x -> labels (var x) |
| 69 | + | BrTable (xs, x) -> list (fun x -> labels (var x)) (x::xs) |
| 70 | + | Return -> empty |
| 71 | + | Call x -> funcs (var x) |
| 72 | + | CallIndirect x -> types (var x) ++ tables zero |
| 73 | + | LocalGet x | LocalSet x | LocalTee x -> locals (var x) |
| 74 | + | GlobalGet x | GlobalSet x -> globals (var x) |
| 75 | + | Load _ | Store _ | MemorySize | MemoryGrow | MemoryCopy | MemoryFill -> |
| 76 | + memories zero |
| 77 | + | MemoryInit x -> memories zero ++ datas (var x) |
| 78 | + | TableCopy -> tables zero |
| 79 | + | TableInit x -> tables zero ++ elems (var x) |
| 80 | + | DataDrop x -> datas (var x) |
| 81 | + | ElemDrop x -> elems (var x) |
| 82 | + |
| 83 | +and block (es : instr list) = |
| 84 | + let free = list instr es in {free with labels = shift free.labels} |
| 85 | + |
| 86 | +let const (c : const) = block c.it |
| 87 | + |
| 88 | +let global (g : global) = const g.it.value |
| 89 | +let func (f : func) = {(block f.it.body) with locals = Set.empty} |
| 90 | +let table (t : table) = empty |
| 91 | +let memory (m : memory) = empty |
| 92 | + |
| 93 | +let elem (e : elem) = |
| 94 | + match e.it with |
| 95 | + | Null -> empty |
| 96 | + | Func x -> funcs (var x) |
| 97 | + |
| 98 | +let table_segment (s : table_segment) = |
| 99 | + match s.it with |
| 100 | + | Active {index; offset; init} -> |
| 101 | + tables (var index) ++ const offset ++ list elem init |
| 102 | + | Passive {etype; data} -> list elem data |
| 103 | + |
| 104 | +let memory_segment (s : memory_segment) = |
| 105 | + match s.it with |
| 106 | + | Active {index; offset; init} -> memories (var index) ++ const offset |
| 107 | + | Passive {etype; data} -> empty |
| 108 | + |
| 109 | +let type_ (t : type_) = empty |
| 110 | + |
| 111 | +let export_desc (d : export_desc) = |
| 112 | + match d.it with |
| 113 | + | FuncExport x -> funcs (var x) |
| 114 | + | TableExport x -> tables (var x) |
| 115 | + | MemoryExport x -> memories (var x) |
| 116 | + | GlobalExport x -> globals (var x) |
| 117 | + |
| 118 | +let import_desc (d : import_desc) = |
| 119 | + match d.it with |
| 120 | + | FuncImport x -> types (var x) |
| 121 | + | TableImport tt -> empty |
| 122 | + | MemoryImport mt -> empty |
| 123 | + | GlobalImport gt -> empty |
| 124 | + |
| 125 | +let export (e : export) = export_desc e.it.edesc |
| 126 | +let import (i : import) = import_desc i.it.idesc |
| 127 | + |
| 128 | +let start (s : var option) = |
| 129 | + funcs (Lib.Option.get (Lib.Option.map var s) Set.empty) |
| 130 | + |
| 131 | +let module_ (m : module_) = |
| 132 | + list type_ m.it.types ++ |
| 133 | + list global m.it.globals ++ |
| 134 | + list table m.it.tables ++ |
| 135 | + list memory m.it.memories ++ |
| 136 | + list func m.it.funcs ++ |
| 137 | + start m.it.start ++ |
| 138 | + list table_segment m.it.elems ++ |
| 139 | + list memory_segment m.it.datas ++ |
| 140 | + list import m.it.imports ++ |
| 141 | + list export m.it.exports |
0 commit comments