-
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathcomp_utils.re
More file actions
311 lines (296 loc) · 9.04 KB
/
comp_utils.re
File metadata and controls
311 lines (296 loc) · 9.04 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
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
open Mashtree;
open Binaryen;
open Grain_typed;
open Grain_utils;
let grain_main = "_gmain";
let grain_start = "_start";
let grain_env_name = "_genv";
let grain_global_function_table = "tbl";
// TODO(#): Use a more descriptive name once we get fixes into Binaryen
let grain_memory = "0";
let wasm_type =
fun
| Types.Managed
| Types.Unmanaged(WasmI32) => Type.int32
| Types.Unmanaged(WasmI64) => Type.int64
| Types.Unmanaged(WasmF32) => Type.float32
| Types.Unmanaged(WasmF64) => Type.float64;
let encoded_int32 = n => n * 2 + 1;
let const_int32 = n => Literal.int32(Int32.of_int(n));
let const_int64 = n => Literal.int64(Int64.of_int(n));
let const_float32 = n => Literal.float32(n);
let const_float64 = n => Literal.float64(n);
/* These are like the above 'const' functions, but take inputs
of the underlying types instead */
let wrap_int32 = n => Literal.int32(n);
let wrap_int64 = n => Literal.int64(n);
let wrap_float32 = n => Literal.float32(n);
let wrap_float64 = n => Literal.float64(n);
let grain_number_max = 0x3fffffff;
let grain_number_min = (-0x3fffffff); // 0xC0000001
type int_type =
| Int8Type
| Int16Type
| Uint8Type
| Uint16Type;
/** Constant compilation */
let rec compile_const = (c): Literal.t => {
let identity: 'a. 'a => 'a = x => x;
let conv_int32 = n => Int32.(add(mul(2l, n), 1l));
let conv_int64 = n => Int64.(add(mul(2L, n), 1L));
let conv_uint32 = n => Int32.(add(mul(2l, n), 1l));
let conv_uint64 = n => Int64.(add(mul(2L, n), 1L));
let conv_float32 = identity;
let conv_float64 = identity;
let conv_char = char => {
let uchar = List.hd @@ Utf8.decodeUtf8String(char);
let uchar_int: int = Utf8__Uchar.toInt(uchar);
Int32.of_int(uchar_int lsl 8 lor 0b010);
};
let conv_short_int = (int, int_type) => {
let tag =
switch (int_type) {
| Int8Type => 1l
| Int16Type => 2l
| Uint8Type => 3l
| Uint16Type => 4l
};
let (<<) = Int32.shift_left;
let (||) = Int32.logor;
let shifted_tag = tag << 3;
int << 8 || shifted_tag || 0b010l;
};
switch (c) {
| MConstLiteral(MConstLiteral(_) as c) => compile_const(c)
| MConstI8(n) => Literal.int32(conv_short_int(n, Int8Type))
| MConstI16(n) => Literal.int32(conv_short_int(n, Int16Type))
| MConstI32(n) => Literal.int32(conv_int32(n))
| MConstI64(n) => Literal.int64(conv_int64(n))
| MConstU8(n) => Literal.int32(conv_short_int(n, Uint8Type))
| MConstU16(n) => Literal.int32(conv_short_int(n, Uint16Type))
| MConstU32(n) => Literal.int32(conv_uint32(n))
| MConstU64(n) => Literal.int64(conv_uint64(n))
| MConstF32(n) => Literal.float32(conv_float32(Int64.float_of_bits(n)))
| MConstF64(n) => Literal.float64(conv_float64(Int64.float_of_bits(n)))
| MConstChar(c) => Literal.int32(conv_char(c))
| MConstLiteral(MConstI8(n))
| MConstLiteral(MConstI16(n))
| MConstLiteral(MConstI32(n)) => Literal.int32(n)
| MConstLiteral(MConstI64(n)) => Literal.int64(n)
| MConstLiteral(MConstU8(n))
| MConstLiteral(MConstU16(n))
| MConstLiteral(MConstU32(n)) => Literal.int32(n)
| MConstLiteral(MConstU64(n)) => Literal.int64(n)
| MConstLiteral(MConstF32(n)) => Literal.float32(Int64.float_of_bits(n))
| MConstLiteral(MConstF64(n)) => Literal.float64(Int64.float_of_bits(n))
| MConstLiteral(MConstChar(c)) => Literal.int32(conv_char(c))
};
};
/* Translate constants to WASM */
let const_true = () => compile_const(const_true);
let const_false = () => compile_const(const_false);
let const_void = () => compile_const(const_void);
/* WebAssembly helpers */
/* These instructions get helpers due to their verbosity */
let store = (~ty=Type.int32, ~align=?, ~offset=0, ~sz=?, wasm_mod, ptr, arg) => {
let sz =
Option.value(
~default=
switch (ty) {
| a when a === Type.int32 || a === Type.float32 => 4
| a when a === Type.int64 || a === Type.float64 => 8
| _ => failwith("sizing not defined for this type")
},
sz,
);
let align = Option.value(~default=sz, align);
Expression.Store.make(
wasm_mod,
sz,
offset,
align,
ptr,
arg,
ty,
grain_memory,
);
};
let load =
(~ty=Type.int32, ~align=?, ~offset=0, ~sz=?, ~signed=false, wasm_mod, ptr) => {
let sz =
Option.value(
~default=
switch (ty) {
| a when a === Type.int32 || a === Type.float32 => 4
| a when a === Type.int64 || a === Type.float64 => 8
| _ => failwith("sizing not defined for this type")
},
sz,
);
let align = Option.value(~default=sz, align);
Expression.Load.make(
~signed,
wasm_mod,
sz,
offset,
align,
ty,
ptr,
grain_memory,
);
};
let type_of_repr = repr => {
Types.(
switch (repr) {
| WasmI32 => Type.int32
| WasmI64 => Type.int64
| WasmF32 => Type.float32
| WasmF64 => Type.float64
}
);
};
let write_universal_exports =
(wasm_mod, {Cmi_format.cmi_sign}, exports, resolve) => {
open Types;
open Type_utils;
let export_map = Hashtbl.create(128);
List.iter(
e => {
switch (e) {
| WasmGlobalExport({ex_global_name, ex_global_internal_name}) =>
Hashtbl.add(
export_map,
ex_global_name,
resolve(ex_global_internal_name),
)
// All functions have an associated global
| WasmFunctionExport(_) => ()
}
},
exports,
);
List.iter(
item => {
switch (item) {
| TSigValue(id, {val_repr: ReprFunction(args, rets, direct)}) =>
let name = Ident.name(id);
let internal_name = Hashtbl.find(export_map, name);
let get_closure = () =>
Expression.Global_get.make(wasm_mod, internal_name, Type.int32);
let arguments =
List.mapi(
(i, arg) =>
Expression.Local_get.make(wasm_mod, i, type_of_repr(arg)),
args,
);
let arguments = [get_closure(), ...arguments];
let call_result_types =
Type.create(
Array.of_list(
List.map(type_of_repr, rets == [] ? [WasmI32] : rets),
),
);
let function_call =
switch (direct) {
| Direct({name}) =>
Expression.Call.make(
wasm_mod,
internal_name,
arguments,
call_result_types,
)
| Indirect =>
let call_arg_types =
Type.create(
Array.of_list(List.map(type_of_repr, [WasmI32, ...args])),
);
let func_ptr =
Expression.Load.make(
wasm_mod,
4,
8,
2,
Type.int32,
get_closure(),
grain_memory,
);
Expression.Call_indirect.make(
wasm_mod,
grain_global_function_table,
func_ptr,
arguments,
call_arg_types,
call_result_types,
);
| Unknown => failwith("Impossible: Unknown function call type")
};
let function_body =
switch (rets) {
| [] => Expression.Drop.make(wasm_mod, function_call)
| _ => function_call
};
let function_body =
Expression.Block.make(
wasm_mod,
"closure_incref",
[
Expression.If.make(
wasm_mod,
Expression.Binary.make(
wasm_mod,
Op.ne_int32,
get_closure(),
Expression.Const.make(wasm_mod, Literal.int32(0l)),
),
store(
wasm_mod,
Expression.Binary.make(
wasm_mod,
Op.sub_int32,
get_closure(),
Expression.Const.make(wasm_mod, Literal.int32(8l)),
),
Expression.Binary.make(
wasm_mod,
Op.add_int32,
load(
wasm_mod,
Expression.Binary.make(
wasm_mod,
Op.sub_int32,
get_closure(),
Expression.Const.make(wasm_mod, Literal.int32(8l)),
),
),
Expression.Const.make(wasm_mod, Literal.int32(1l)),
),
),
Expression.Null.make(),
),
function_body,
],
);
let arg_types =
Type.create(Array.of_list(List.map(type_of_repr, args)));
let result_types =
Type.create(Array.of_list(List.map(type_of_repr, rets)));
ignore @@
Function.add_function(
wasm_mod,
name,
arg_types,
result_types,
[||],
function_body,
);
ignore @@ Export.add_function_export(wasm_mod, name, name);
| TSigValue(_)
| TSigType(_)
| TSigTypeExt(_)
| TSigModule(_)
| TSigModType(_) => ()
}
},
cmi_sign,
);
};