-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathValue.v3
More file actions
233 lines (224 loc) · 7.35 KB
/
Copy pathValue.v3
File metadata and controls
233 lines (224 loc) · 7.35 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
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
// Copyright 2020 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
// WebAssembly program values.
type Value {
case Ref(val: Object);
case I31(val: u31);
case I32(val: u32);
case I64(val: u64);
case F32(bits: u32);
case F64(bits: u64);
case V128(low: u64, high: u64);
case Cont(val: Continuation);
}
// Categorization of values into storage kinds.
enum ValueKind(code: byte) {
I32(BpTypeCode.I32.code),
I64(BpTypeCode.I64.code),
F32(BpTypeCode.F32.code),
F64(BpTypeCode.F64.code),
V128(BpTypeCode.V128.code),
REF(BpTypeCode.REF.code),
REF_U64(BpTypeCode.CONTREF.code),
}
// Superclass of all objects referred to by Value.Ref, including external refs.
class Object extends Exportable { }
// Wasm objects that are allocated on the Virgil heap.
class WasmObject extends Object {
// def decl() -> HeapTypeDecl; TODO: JVM bridge method is broken
}
class WasmStruct extends WasmObject {
def decl() -> StructDecl;
def getField(index: u31) -> Value;
def setField(index: u31, v: Value);
}
class WasmArray extends WasmObject {
def decl() -> ArrayDecl;
def getLength() -> u32;
def getElem(index: u32) -> Value;
def setElem(index: u32, v: Value);
def fill(dst_offset: u64, size: u64, val: Value) -> TrapReason;
def copy(dst_offset: u64, src: WasmArray, src_offset: u64, size: u64) -> TrapReason;
def copyInto(dst_offset: u64, src: Range<Value>) -> TrapReason;
def copyOutOf(dst: Range<Value>, src_offset: u64) -> TrapReason;
def sendBytes<R>(offset: u64, size: u64, f: Range<byte> -> R) -> MaybeTrap<R>;
def checkRange(offset: u64, size: u64) -> TrapReason {
var length = getLength();
if (offset > length || size > (length - offset)) return TrapReason.ARRAY_OOB;
return TrapReason.NONE;
}
}
// Utilities associated with values.
component Values {
def I32_1 = Value.I32(1);
def I32_0 = Value.I32(0);
def I64_0 = Value.I64(0);
def F32_0 = Value.F32(0);
def F64_0 = Value.F64(0);
def V128_0 = Value.V128(0, 0);
def F32_minus_0 = Value.F32(0x8000_0000);
def F64_minus_0 = Value.F64(0x8000_0000_0000_0000);
def F32_nan = Value.F32(0x7fc0_0000);
def F64_nan = Value.F64(0x7ff8_0000_0000_0000);
def F32_infinity = Value.F32(0x7f80_0000);
def F64_infinity = Value.F64(0x7ff0_0000_0000_0000);
def F32_minus_infinity = Value.F32(0xff80_0000);
def F64_minus_infinity = Value.F64(0xfff0_0000_0000_0000);
def FUNCREF_NULL = Value.Ref(null);
def REF_NULL = FUNCREF_NULL;
def CONT_NULL = Value.Cont(Continuations.NULL);
def NONE = Array<Value>.new(0);
def NO_SUPERS = Array<HeapTypeDecl>.new(0);
def render(v: Value, buf: StringBuilder) -> StringBuilder {
match (v) {
Ref(val) => match (val) {
x: HostObject => buf.put1("<externref %q>", x.render);
x: WasmFunction => buf.put1("<funcref: #%d>", x.decl.func_index);
x: WasmStruct => {
var id = if(x.decl == null, -1, x.decl().heaptype_index);
buf.put1("<ref struct #%d>", id);
}
x: WasmArray => {
var id = if(x.decl == null, -1, x.decl().heaptype_index);
buf.put1("<ref array #%d>", id);
}
x: Object => x.render(buf);
null => buf.puts("<ref null>");
}
I31(val) => buf.put1("i31:%d", u32.view(val));
I32(val) => buf.put1("%d", val);
I64(val) => buf.put1("%duL", val);
F32(val) => buf.put1("f32:%x", val);
F64(val) => buf.put1("f64:%x", val);
V128(low, high) => buf.puts("v128:").putx_64(high).putc('_').putx_64(low);
Cont(val) => buf.put1("<continuation %q>", val.render);
}
return buf;
}
def renderVals(buf: StringBuilder, av: Range<Value>) -> StringBuilder {
buf.putc('(');
Trace.renderCspRange(buf, av, Values.render);
buf.putc(')');
return buf;
}
def isNull(v: Value) -> bool {
match (v) {
Ref(val) => return val == null;
Cont(cont) => return cont == Continuations.NULL;
_ => return false;
}
}
def default(t: ValueType) -> Value {
var v: Value;
match (t) {
BOTTOM => v = REF_NULL; // TODO: no default for bottom
I32 => v = I32_0;
I64 => v = I64_0;
F32 => v = F32_0;
F64 => v = F64_0;
V128 => v = V128_0;
Host,
Ref => v = REF_NULL;
}
return v;
}
// Unboxing utilities.
def unbox_i(v: Value) => i32.view(Value.I32.!(v).val);
def unbox_u(v: Value) => Value.I32.!(v).val;
def unbox_u8(v: Value) => u8.view(Value.I32.!(v).val);
def unbox_u16(v: Value) => u16.view(Value.I32.!(v).val);
def unbox_fu32(v: Value) => Value.F32.!(v).bits;
def unbox_du64(v: Value) => Value.F64.!(v).bits;
def unbox_f(v: Value) => float.view(Value.F32.!(v).bits);
def unbox_d(v: Value) => double.view(Value.F64.!(v).bits);
def unbox_l(v: Value) => i64.view(Value.I64.!(v).val);
def unbox_w(v: Value) => Value.I64.!(v).val;
def unbox_w8(v: Value) => u8.view(Value.I64.!(v).val);
def unbox_w16(v: Value) => u16.view(Value.I64.!(v).val);
def unbox_w32(v: Value) => u32.view(Value.I64.!(v).val);
def unbox_s(v: Value) -> (u64, u64) {
var b = Value.V128.!(v);
return (b.low, b.high);
}
def box_b(b: bool) => Value.I32(if(b, 1, 0));
// Reflective unboxing.
def unbox<T>(v: Value) -> T {
match (Type<T>()) {
x: Type<int> => return T.!(unbox_i(v));
x: Type<u32> => return T.!(unbox_u(v));
x: Type<long> => return T.!(unbox_l(v));
x: Type<u64> => return T.!(unbox_w(v));
x: Type<float> => return T.!(unbox_f(v));
x: Type<double> => return T.!(unbox_d(v));
x: Type<(u64, u64)> => return T.!(unbox_s(v));
x: Type<Object> => return T.!(Value.Ref.!(v).val);
_ => ;
}
match (v) {
Ref(val) => return T.!(val);
I31(val) => return T.!(val);
I32(val) => return T.!(val);
I64(val) => return T.!(val);
F32(bits) => return T.!(float.view(bits));
F64(bits) => return T.!(double.view(bits));
V128(low, high) => return T.!((low, high));
Cont(val) => return T.!(val);
}
}
// Boxing utilities.
def box_i(v: i32) => Value.I32(u32.view(v));
def box_u(v: u32) => Value.I32(v);
def box_l(v: i64) => Value.I64(u64.view(v));
def box_f(v: float) => Value.F32(u32.view(v));
def box_d(v: double) => Value.F64(u64.view(v));
def box_fu32(v: u32) => Value.F32(v);
def box_du64(v: u64) => Value.F64(v);
def box_w(v: u64) => Value.I64(v);
def box_s(x: u64, y: u64) => Value.V128(x, y);
// Reflective boxing.
def box<T>(v: T) -> Value {
match (Type<T>()) {
x: Type<int> => return box_i(int.!(v));
x: Type<u32> => return box_u(u32.!(v));
x: Type<long> => return box_l(long.!(v));
x: Type<u64> => return box_w(u64.!(v));
x: Type<float> => return box_f(float.!(v));
x: Type<double> => return box_d(double.!(v));
x: Type<(u64, u64)> => return box_s(Type<(u64, u64)>().cast(v));
x: Type<Continuation> => return Value.Cont(Continuation.!(v));
x: Type<Object> => return Value.Ref(Object.!(v));
_ => ;
}
match (v) {
x: i32 => return box_i(x);
x: u32 => return box_u(x);
x: i64 => return box_l(x);
x: u64 => return box_w(x);
x: Object => return Value.Ref(x);
_ => System.error("BoxError", "no matching boxing operation for Virgil value");
}
return Values.REF_NULL;
}
def toFuncAndId(val: Value) -> (Function, int) {
match (val) {
Ref(val) => match (val) {
x: Function => return (x, Canon.sigId(x.sig));
}
_ => ;
}
return (null, -1);
}
def kind(val: Value) -> ValueKind {
match (val) {
I32 => return ValueKind.I32;
I64 => return ValueKind.I64;
F32 => return ValueKind.F32;
F64 => return ValueKind.F64;
V128 => return ValueKind.V128;
_ => return ValueKind.REF;
}
}
}
private type Type<T>() #unboxed {
def cast<B>(v: B) => T.!(v);
}