-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTyp.lean
More file actions
143 lines (104 loc) · 3.81 KB
/
Copy pathTyp.lean
File metadata and controls
143 lines (104 loc) · 3.81 KB
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
/- Encoding of defintion WASM's type defintion:
https://webassembly.github.io/spec/core/binary/types.html
-/
import Wasm.Vec
import Wasm.Binary.Opcode
import Wasm.Syntax.Typ
namespace Wasm.Binary.Typ
open Wasm.Syntax.Typ
def Num.toOpcode : Syntax.Typ.Num → Byte
| .i32 => 0x7F
| .i64 => 0x7E
| .f32 => 0x7D
| .f64 => 0x7C
def Num.ofOpcode : Bytecode Syntax.Typ.Num := do
match ← Bytecode.readByte with
| 0x7F => return .i32
| 0x7E => return .i64
| 0x7D => return .f32
| 0x7C => return .f64
| _ => Bytecode.errMsg "Parsing numtype."
instance : Opcode Syntax.Typ.Num := ⟨Byte.toSeq ∘ Num.toOpcode, Num.ofOpcode⟩
def Vec.toOpcode : Syntax.Typ.Vec → UInt8
| .v128 => 0x7B
def Vec.ofOpcode : Bytecode Syntax.Typ.Vec := do
match ← Bytecode.readByte with
| 0x7B => return .v128
| _ => Bytecode.errMsg "Parsing vectype."
instance : Opcode Syntax.Typ.Vec := ⟨Byte.toSeq ∘ Vec.toOpcode, Vec.ofOpcode⟩
def Ref.toOpcode : Ref → UInt8
| .func => 0x70
| .extern => 0x6F
def Ref.ofOpcode : Bytecode Ref := do
match ← Bytecode.readByte with
| 0x70 => return .func
| 0x6F => return .extern
| _ => Bytecode.errMsg "Parsing vectype."
instance : Opcode Ref := ⟨Byte.toSeq ∘ Ref.toOpcode, Ref.ofOpcode⟩
def Val.toOpcode : Val → UInt8
| .num v => Num.toOpcode v
| .vec v => Vec.toOpcode v
| .ref v => Ref.toOpcode v
def Val.ofOpcode : Bytecode Val :=
Bytecode.err_log "Parsing valtype." do
(return Val.num (← Num.ofOpcode))
<|> (return Val.vec (← Vec.ofOpcode))
<|> (return Val.ref (← Ref.ofOpcode))
instance : Opcode Val := ⟨Byte.toSeq ∘ Val.toOpcode, Val.ofOpcode⟩
def Result.toOpcode (res : Result) : ByteSeq := Wasm.Binary.Vec.toOpcode res
def Result.ofOpcode : Bytecode Result := Wasm.Binary.Vec.ofOpcode
instance : Opcode Result := ⟨Result.toOpcode, Result.ofOpcode⟩
nonrec def Func.toOpcode (func : Func) : ByteSeq :=
0x60 :: toOpcode func.args ++ toOpcode func.result
def Func.ofOpcode : Bytecode Func :=
Bytecode.err_log "Parsing function type." do
match ← Bytecode.readByte with
| 0x60 =>
let args ← Wasm.Binary.Vec.ofOpcode
let res ← Wasm.Binary.Vec.ofOpcode
return ⟨args, res⟩
| _ => Bytecode.err
instance : Opcode Func := ⟨Func.toOpcode, Func.ofOpcode⟩
nonrec def Limit.toOpcode (lim : Limit) : ByteSeq :=
match lim.max with
| .none => 0x00 :: toOpcode lim.min
| .some m => 0x01 :: toOpcode lim.min ++ toOpcode m
nonrec def Limit.ofOpcode : Bytecode Limit :=
Bytecode.err_log "Parsing limit." do
match ← Bytecode.readByte with
| 0x00 =>
let min ← ofOpcode
return ⟨min, .none⟩
| 0x01 =>
let min ← ofOpcode
let max ← ofOpcode
return ⟨min, .some max⟩
| _ => Bytecode.err
instance : Opcode Limit := ⟨Limit.toOpcode, Limit.ofOpcode⟩
instance : Opcode Mem := ⟨Limit.toOpcode, Limit.ofOpcode⟩
nonrec def Table.toOpcode (tab : Table) : ByteSeq :=
toOpcode tab.2 ++ toOpcode tab.1
nonrec def Table.ofOpcode : Bytecode Table :=
Bytecode.err_log "Parsing table type." do
let ref ← Ref.ofOpcode
let lim ← Limit.ofOpcode
return ⟨lim, ref⟩
instance : Opcode Table := ⟨Table.toOpcode, Table.ofOpcode⟩
def Mut.toOpcode : Mut → Byte
| .const => 0x00
| .var => 0x01
def Mut.ofOpcode : Bytecode Mut := do
match ← Bytecode.readByte with
| 0x00 => return .const
| 0x01 => return .var
| _ => Bytecode.errMsg "Parsing mutablility."
instance : Opcode Mut := ⟨Byte.toSeq ∘ Mut.toOpcode, Mut.ofOpcode⟩
nonrec def Global.toOpcode (g : Global) : ByteSeq :=
toOpcode g.2 ++ toOpcode g.1
nonrec def Global.ofOpcode : Bytecode Global :=
Bytecode.err_log "Parsing global type." do
let val ← ofOpcode
let m ← ofOpcode
return (m, val)
instance : Opcode Global := ⟨Global.toOpcode, Global.ofOpcode⟩
-- todo Extern