blob: 8be89c65d27f1a08fa16bb86a7b3a2cf7dc1ce52 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
{-# OPTIONS --safe --without-K #-}
module Wasm.Data.Instruction where
open import Data.Fin using (Fin)
open import Data.List using (List; []; _∷_; _++_; length; lookup)
open import Data.List.Membership.Propositional using (_∈_)
open import Data.List.Relation.Unary.All using (All)
open import Data.Maybe using (Maybe; just)
open import Data.Nat using (ℕ; suc; _>_)
open import Relation.Binary.PropositionalEquality using (_≡_)
open import Wasm.Type
open import Wasm.Value
infixr 4 _∙_ _∙₁_ _∙₂_
record Context : Set where
field
funcs : List FuncType
tables : List TableType
mems : List MemType
globals : List GlobalType
elems : List RefType
datas : ℕ
locals : List ValType
labels : List (List ValType)
return : Maybe (List ValType)
refs : List (Fin (length funcs))
data Signedness : Set where
signed : Signedness
unsigned : Signedness
module i32 where
data Unary : Set where
clz : Unary
ctz : Unary
popcnt : Unary
extend8-s : Unary
extend16-s : Unary
data Binary : Set where
add : Binary
sub : Binary
mul : Binary
div : Signedness → Binary
rem : Signedness → Binary
and : Binary
or : Binary
xor : Binary
shl : Binary
shr : Signedness → Binary
rotl : Binary
rotr : Binary
data Test : Set where
eqz : Test
data Rel : Set where
eq : Rel
ne : Rel
lt : Signedness → Rel
gt : Signedness → Rel
le : Signedness → Rel
ge : Signedness → Rel
data Convert : NumType → Set where
wrap : Convert i64
trunc-f32 : Signedness → Convert f32
trunc-f64 : Signedness → Convert f64
trunc-sat-f32 : Signedness → Convert f32
trunc-sat-f64 : Signedness → Convert f64
reinterpret : Convert f32
module i64 where
data Unary : Set where
clz : Unary
ctz : Unary
popcnt : Unary
extend8-s : Unary
extend16-s : Unary
extend32-s : Unary
data Binary : Set where
add : Binary
sub : Binary
mul : Binary
div : Signedness → Binary
rem : Signedness → Binary
and : Binary
or : Binary
xor : Binary
shl : Binary
shr : Signedness → Binary
rotl : Binary
rotr : Binary
data Test : Set where
eqz : Test
data Rel : Set where
eq : Rel
ne : Rel
lt : Signedness → Rel
gt : Signedness → Rel
le : Signedness → Rel
ge : Signedness → Rel
data Convert : NumType → Set where
extend : Convert i32
trunc-f32 : Signedness → Convert f32
trunc-f64 : Signedness → Convert f64
trunc-sat-f32 : Signedness → Convert f32
trunc-sat-f64 : Signedness → Convert f64
reinterpret : Convert f64
module f32 where
data Unary : Set where
abs : Unary
neg : Unary
sqrt : Unary
ceil : Unary
floor : Unary
trunc : Unary
nearest : Unary
data Binary : Set where
add : Binary
sub : Binary
mul : Binary
div : Binary
min : Binary
max : Binary
copysign : Binary
data Rel : Set where
eq : Rel
ne : Rel
lt : Rel
gt : Rel
le : Rel
ge : Rel
data Convert : NumType → Set where
demote : Convert f64
convert-i32 : Convert i32
convert-i64 : Convert i64
reinterpret : Convert i32
module f64 where
data Unary : Set where
abs : Unary
neg : Unary
sqrt : Unary
ceil : Unary
floor : Unary
trunc : Unary
nearest : Unary
data Binary : Set where
add : Binary
sub : Binary
mul : Binary
div : Binary
min : Binary
max : Binary
copysign : Binary
data Rel : Set where
eq : Rel
ne : Rel
lt : Rel
gt : Rel
le : Rel
ge : Rel
data Convert : NumType → Set where
promote : Convert f32
convert-i32 : Convert i32
convert-i64 : Convert i64
reinterpret : Convert i64
record MemArg (t : ℕ): Set where
field
offset : I32
align : Fin (suc t)
data BlockType : Context → Set where
idx : ∀ {C} → FuncType → BlockType C
val : ∀ {C} → ValType → BlockType C
nothing : ∀ {C} → BlockType C
blockTypeAsFunc : ∀ {C} → BlockType C → FuncType
blockTypeAsFunc {C} (idx τ) = τ
blockTypeAsFunc (val x) = [] ⟶ x ∷ []
blockTypeAsFunc nothing = [] ⟶ []
data Instruction (C : Context) : FuncType → Set
data Sequence (C : Context) : FuncType → Set
data Instruction C where
---- Numeric instructions
-- const
i32-const : (c : I32) → Instruction C ([] ⟶ num i32 ∷ [])
i64-const : (c : I64) → Instruction C ([] ⟶ num i64 ∷ [])
f32-const : (c : F32) → Instruction C ([] ⟶ num f32 ∷ [])
f64-const : (c : F64) → Instruction C ([] ⟶ num f64 ∷ [])
-- unop
i32-unop : (op : i32.Unary) → Instruction C (num i32 ∷ [] ⟶ num i32 ∷ [])
i64-unop : (op : i64.Unary) → Instruction C (num i64 ∷ [] ⟶ num i64 ∷ [])
f32-unop : (op : f32.Unary) → Instruction C (num f32 ∷ [] ⟶ num f32 ∷ [])
f64-unop : (op : f64.Unary) → Instruction C (num f64 ∷ [] ⟶ num f64 ∷ [])
-- binop
i32-binop : (op : i32.Binary) → Instruction C (num i32 ∷ num i32 ∷ [] ⟶ num i32 ∷ [])
i64-binop : (op : i64.Binary) → Instruction C (num i64 ∷ num i64 ∷ [] ⟶ num i64 ∷ [])
f32-binop : (op : f32.Binary) → Instruction C (num f32 ∷ num f32 ∷ [] ⟶ num f32 ∷ [])
f64-binop : (op : f64.Binary) → Instruction C (num f64 ∷ num f64 ∷ [] ⟶ num f64 ∷ [])
-- testop
i32-testop : (op : i32.Test) → Instruction C (num i32 ∷ [] ⟶ num i32 ∷ [])
i64-testop : (op : i64.Test) → Instruction C (num i64 ∷ [] ⟶ num i32 ∷ [])
-- relop
i32-relop : (op : i32.Rel) → Instruction C (num i32 ∷ num i32 ∷ [] ⟶ num i32 ∷ [])
i64-relop : (op : i64.Rel) → Instruction C (num i64 ∷ num i64 ∷ [] ⟶ num i32 ∷ [])
f32-relop : (op : f32.Rel) → Instruction C (num f32 ∷ num f32 ∷ [] ⟶ num i32 ∷ [])
f64-relop : (op : f64.Rel) → Instruction C (num f64 ∷ num f64 ∷ [] ⟶ num i32 ∷ [])
-- cvtop
i32-cvtop : ∀ {τ} → (op : i32.Convert τ) → Instruction C (num τ ∷ [] ⟶ num i32 ∷ [])
i64-cvtop : ∀ {τ} → (op : i64.Convert τ) → Instruction C (num τ ∷ [] ⟶ num i64 ∷ [])
f32-cvtop : ∀ {τ} → (op : f32.Convert τ) → Instruction C (num τ ∷ [] ⟶ num f32 ∷ [])
f64-cvtop : ∀ {τ} → (op : f64.Convert τ) → Instruction C (num τ ∷ [] ⟶ num f64 ∷ [])
---- Reference instructions
ref-null : ∀ τ → Instruction C ([] ⟶ ref τ ∷ [])
ref-is-null : ∀ {τ} → Instruction C (ref τ ∷ [] ⟶ num i32 ∷ [])
ref-func : ∀ {x} → (x∈refs : x ∈ Context.refs C) → Instruction C ([] ⟶ ref funcref ∷ [])
---- Parametric instructions
drop : ∀ {τ} → Instruction C (τ ∷ [] ⟶ [])
select : ∀ {τ} → Instruction C (num i32 ∷ τ ∷ τ ∷ [] ⟶ τ ∷ [])
---- Variable instructions
local-get : ∀ x → Instruction C ([] ⟶ lookup (Context.locals C) x ∷ [])
local-set : ∀ x → Instruction C (lookup (Context.locals C) x ∷ [] ⟶ [])
local-tee : ∀ x → Instruction C (lookup (Context.locals C) x ∷ [] ⟶ lookup (Context.locals C) x ∷ [])
global-get : ∀ x → Instruction C ([] ⟶ GlobalType.type (lookup (Context.globals C) x) ∷ [])
global-set : ∀ x → let g = lookup (Context.globals C) x in GlobalType.mut g ≡ var → Instruction C (GlobalType.type g ∷ [] ⟶ [])
---- Table instructions
table-get : ∀ x → Instruction C (num i32 ∷ [] ⟶ ref (TableType.type (lookup (Context.tables C) x)) ∷ [])
table-set : ∀ x → Instruction C (ref (TableType.type (lookup (Context.tables C) x)) ∷ num i32 ∷ [] ⟶ [])
table-size : ∀ (x : Fin (length (Context.tables C))) → Instruction C ([] ⟶ num i32 ∷ [])
table-grow : ∀ x → Instruction C (num i32 ∷ ref (TableType.type (lookup (Context.tables C) x)) ∷ [] ⟶ num i32 ∷ [])
table-fill : ∀ x → Instruction C (num i32 ∷ ref (TableType.type (lookup (Context.tables C) x)) ∷ num i32 ∷ [] ⟶ [])
table-copy : ∀ x y → TableType.type (lookup (Context.tables C) x) ≡ TableType.type (lookup (Context.tables C) y) → Instruction C (num i32 ∷ num i32 ∷ num i32 ∷ [] ⟶ [])
table-init : ∀ x y → TableType.type (lookup (Context.tables C) x) ≡ lookup (Context.elems C) y → Instruction C (num i32 ∷ num i32 ∷ num i32 ∷ [] ⟶ [])
elem-drop : ∀ (x : Fin (length (Context.elems C))) → Instruction C ([] ⟶ [])
---- Memory instructions
-- load
i32-load : ∀ (m : MemArg 2) → length (Context.mems C) > 0 → Instruction C (num i32 ∷ [] ⟶ num i32 ∷ [])
i64-load : ∀ (m : MemArg 3) → length (Context.mems C) > 0 → Instruction C (num i32 ∷ [] ⟶ num i64 ∷ [])
f32-load : ∀ (m : MemArg 2) → length (Context.mems C) > 0 → Instruction C (num i32 ∷ [] ⟶ num f32 ∷ [])
f64-load : ∀ (m : MemArg 3) → length (Context.mems C) > 0 → Instruction C (num i32 ∷ [] ⟶ num f64 ∷ [])
-- loadN
i32-load8 : ∀ (m : MemArg 0) → length (Context.mems C) > 0 → Signedness → Instruction C (num i32 ∷ [] ⟶ num i32 ∷ [])
i32-load16 : ∀ (m : MemArg 1) → length (Context.mems C) > 0 → Signedness → Instruction C (num i32 ∷ [] ⟶ num i32 ∷ [])
i64-load8 : ∀ (m : MemArg 0) → length (Context.mems C) > 0 → Signedness → Instruction C (num i32 ∷ [] ⟶ num i64 ∷ [])
i64-load16 : ∀ (m : MemArg 1) → length (Context.mems C) > 0 → Signedness → Instruction C (num i32 ∷ [] ⟶ num i64 ∷ [])
i64-load32 : ∀ (m : MemArg 2) → length (Context.mems C) > 0 → Signedness → Instruction C (num i32 ∷ [] ⟶ num i64 ∷ [])
-- store
i32-store : ∀ (m : MemArg 2) → length (Context.mems C) > 0 → Instruction C (num i32 ∷ num i32 ∷ [] ⟶ [])
i64-store : ∀ (m : MemArg 3) → length (Context.mems C) > 0 → Instruction C (num i64 ∷ num i32 ∷ [] ⟶ [])
f32-store : ∀ (m : MemArg 2) → length (Context.mems C) > 0 → Instruction C (num f32 ∷ num i32 ∷ [] ⟶ [])
f64-store : ∀ (m : MemArg 3) → length (Context.mems C) > 0 → Instruction C (num f64 ∷ num i32 ∷ [] ⟶ [])
-- storeN
i32-store8 : ∀ (m : MemArg 0) → length (Context.mems C) > 0 → Signedness → Instruction C (num i32 ∷ num i32 ∷ [] ⟶ [])
i32-store16 : ∀ (m : MemArg 1) → length (Context.mems C) > 0 → Signedness → Instruction C (num i32 ∷ num i32 ∷ [] ⟶ [])
i64-store8 : ∀ (m : MemArg 0) → length (Context.mems C) > 0 → Signedness → Instruction C (num i64 ∷ num i32 ∷ [] ⟶ [])
i64-store16 : ∀ (m : MemArg 1) → length (Context.mems C) > 0 → Signedness → Instruction C (num i64 ∷ num i32 ∷ [] ⟶ [])
i64-store32 : ∀ (m : MemArg 2) → length (Context.mems C) > 0 → Signedness → Instruction C (num i64 ∷ num i32 ∷ [] ⟶ [])
-- other
memory-size : length (Context.mems C) > 0 → Instruction C ([] ⟶ num i32 ∷ [])
memory-grow : length (Context.mems C) > 0 → Instruction C (num i32 ∷ [] ⟶ num i32 ∷ [])
memory-fill : length (Context.mems C) > 0 → Instruction C (num i32 ∷ num i32 ∷ num i32 ∷ [] ⟶ [])
memory-copy : length (Context.mems C) > 0 → Instruction C (num i32 ∷ num i32 ∷ num i32 ∷ [] ⟶ [])
memory-init : (x : Fin (Context.datas C)) → length (Context.mems C) > 0 → Instruction C (num i32 ∷ num i32 ∷ num i32 ∷ [] ⟶ [])
data-drop : (x : Fin (Context.datas C)) → Instruction C ([] ⟶ [])
---- Control instructions
block : (bt : BlockType C) → let τ = blockTypeAsFunc bt in
Sequence (record C {labels = FuncType.to τ ∷ Context.labels C}) τ → Instruction C τ
loop : (bt : BlockType C) → let τ = blockTypeAsFunc bt in
Sequence (record C {labels = FuncType.from τ ∷ Context.labels C}) τ → Instruction C τ
if : (bt : BlockType C) → let τ = blockTypeAsFunc bt in
(then else : Sequence (record C {labels = FuncType.to τ ∷ Context.labels C}) τ) → Instruction C (num i32 ∷ FuncType.from τ ⟶ FuncType.to τ)
br_if : ∀ l → let τ = lookup (Context.labels C) l in Instruction C (num i32 ∷ τ ⟶ τ)
call : ∀ x → Instruction C (lookup (Context.funcs C) x)
call_indirect : ∀ x τ → TableType.type (lookup (Context.tables C) x) ≡ funcref → Instruction C (num i32 ∷ FuncType.from τ ⟶ FuncType.to τ)
data Sequence C where
nop : ∀ {τ} → Sequence C (τ ⟶ τ)
unreachable : ∀ {τ} → Sequence C τ
br : ∀ {τ₁ τ₂} l → Sequence C (lookup (Context.labels C) l ++ τ₁ ⟶ τ₂)
br_table : ∀ {τ₁ τ₂} ls l → let τ = lookup (Context.labels C) l in All (λ l → lookup (Context.labels C) l ≡ τ) ls → Sequence C (τ ++ τ₁ ⟶ τ₂)
return : ∀ {τ τ₁ τ₂} → Context.return C ≡ just τ → Sequence C (τ ++ τ₁ ⟶ τ₂)
_∙_ : ∀ {τ₀ τ₁ τ₂ τ₃} → Instruction C (τ₁ ⟶ τ₂) → Sequence C (τ₂ ++ τ₀ ⟶ τ₃) → Sequence C (τ₁ ++ τ₀ ⟶ τ₃)
Expression : Context → List ValType → Set
Expression C τ = Sequence C ([] ⟶ τ)
data InstructionRef {C} : ∀ {τ} → Instruction C τ → Fin (length (Context.funcs C)) → Set
data SequenceRef {C} : ∀ {τ} → Sequence C τ → Fin (length (Context.funcs C)) → Set
data InstructionRef {C} where
---- Direct
ref-func : ∀ {x} x∈refs → InstructionRef (ref-func {C} {x} x∈refs) x
call : ∀ x → InstructionRef (call {C} x) x
---- Structural
block : ∀ {x} bt {i} → SequenceRef i x → InstructionRef (block {C} bt i) x
loop : ∀ {x} bt {i} → SequenceRef i x → InstructionRef (loop {C} bt i) x
if₁ : ∀ {x} bt {i₁} → SequenceRef i₁ x → ∀ i₂ → InstructionRef (if {C} bt i₁ i₂) x
if₂ : ∀ {x} bt i₁ {i₂} → SequenceRef i₂ x → InstructionRef (if {C} bt i₁ i₂) x
data SequenceRef {C} where
_∙₁_ : ∀ {x τ₀ τ₁ τ₂ τ₃ i} → InstructionRef i x → ∀ i* → SequenceRef (_∙_ {C} {τ₀} {τ₁} {τ₂} {τ₃} i i*) x
_∙₂_ : ∀ {x τ₀ τ₁ τ₂ τ₃} i {i*} → SequenceRef i* x → SequenceRef (_∙_ {C} {τ₀} {τ₁} {τ₂} {τ₃} i i*) x
data InstructionConstant {C} : ∀ {τ} → Instruction C τ → Set
data SequenceConstant {C} : ∀ {τ} → Sequence C τ → Set
data InstructionConstant {C} where
---- Direct
-- const
i32-const : ∀ c → InstructionConstant (i32-const c)
i64-const : ∀ c → InstructionConstant (i64-const c)
f32-const : ∀ c → InstructionConstant (f32-const c)
f64-const : ∀ c → InstructionConstant (f64-const c)
-- refs
ref-null : ∀ τ → InstructionConstant (ref-null τ)
ref-func : ∀ {x} x∈refs → InstructionConstant (ref-func {x = x} x∈refs)
-- global
global-get : ∀ {x} → GlobalType.mut (lookup (Context.globals C) x) ≡ const → InstructionConstant (global-get x)
data SequenceConstant {C} where
nop : ∀ {τ} → SequenceConstant (nop {C} {τ})
_∙_ : ∀ {τ₀ τ₁ τ₂ τ₃ i i*} → InstructionConstant i → SequenceConstant i* → SequenceConstant (_∙_ {C} {τ₀} {τ₁} {τ₂} {τ₃} i i*)
|