Skip to content

Commit b41feb7

Browse files
authored
feat(compiler)!: Supply primitive types via the compiler (#1667)
1 parent c40df12 commit b41feb7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+777
-731
lines changed

compiler/src/codegen/compcore.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,7 @@ let compile_prim0 = (wasm_mod, env, p0): Expression.t => {
23792379
allocate_number_uninitialized(wasm_mod, env, BoxedRational)
23802380
| AllocateUint32 => allocate_uint_uninitialized(wasm_mod, env, true)
23812381
| AllocateUint64 => allocate_uint_uninitialized(wasm_mod, env, false)
2382+
| WasmMemorySize => Expression.Memory_size.make(wasm_mod)
23822383
| Unreachable => Expression.Unreachable.make(wasm_mod)
23832384
};
23842385
};
@@ -2665,7 +2666,6 @@ let compile_primn = (wasm_mod, env: codegen_env, p, args): Expression.t => {
26652666
compile_wasm_store(~sz, ~ty=Type.int64, wasm_mod, env, args)
26662667
| WasmStoreF32 => compile_wasm_store(~ty=Type.float32, wasm_mod, env, args)
26672668
| WasmStoreF64 => compile_wasm_store(~ty=Type.float64, wasm_mod, env, args)
2668-
| WasmMemorySize => Expression.Memory_size.make(wasm_mod)
26692669
| WasmMemoryCopy =>
26702670
Expression.Block.make(
26712671
wasm_mod,

compiler/src/codegen/mashtree.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ type prim0 =
179179
| AllocateFloat32
180180
| AllocateFloat64
181181
| AllocateRational
182+
| WasmMemorySize
182183
| Unreachable;
183184

184185
type prim1 =
@@ -281,7 +282,6 @@ type primn =
281282
| WasmStoreF64
282283
| WasmMemoryCopy
283284
| WasmMemoryFill
284-
| WasmMemorySize
285285
| WasmMemoryCompare;
286286

287287
[@deriving sexp]

compiler/src/formatting/format.re

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4703,13 +4703,13 @@ let print_foreign_value_description =
47034703
]);
47044704
};
47054705

4706-
let print_primitive_value_description =
4706+
let print_primitive_description =
47074707
(
47084708
~original_source: array(string),
47094709
~comments: list(Parsetree.comment),
4710-
vd: Parsetree.value_description,
4710+
pd: Parsetree.primitive_description,
47114711
) => {
4712-
let ident = vd.pval_name.txt;
4712+
let ident = pd.pprim_ident.txt;
47134713

47144714
let fixed_ident =
47154715
if (infixop(ident) || prefixop(ident)) {
@@ -4720,14 +4720,11 @@ let print_primitive_value_description =
47204720

47214721
Doc.concat([
47224722
fixed_ident,
4723-
Doc.text(":"),
4724-
Doc.space,
4725-
print_type(~original_source, ~comments, vd.pval_type),
47264723
Doc.space,
47274724
Doc.equal,
47284725
Doc.space,
47294726
Doc.text("\""),
4730-
Doc.join(~sep=Doc.text(","), List.map(p => Doc.text(p), vd.pval_prim)),
4727+
Doc.text(pd.pprim_name.txt),
47314728
Doc.text("\""),
47324729
]);
47334730
};
@@ -4758,7 +4755,7 @@ let rec toplevel_print =
47584755
value_description,
47594756
),
47604757
]);
4761-
| PTopPrimitive(provide_flag, value_description) =>
4758+
| PTopPrimitive(provide_flag, primitive_description) =>
47624759
let provide =
47634760
switch (provide_flag) {
47644761
| NotProvided => Doc.nil
@@ -4768,10 +4765,10 @@ let rec toplevel_print =
47684765
Doc.concat([
47694766
provide,
47704767
Doc.text("primitive "),
4771-
print_primitive_value_description(
4768+
print_primitive_description(
47724769
~original_source,
47734770
~comments,
4774-
value_description,
4771+
primitive_description,
47754772
),
47764773
]);
47774774
| PTopData(data_declarations) =>

compiler/src/middle_end/analyze_purity.re

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module PurityArg: Anf_iterator.IterArgument = {
4747
AllocateRational,
4848
) =>
4949
true
50-
| CPrim0(Unreachable) => false
50+
| CPrim0(WasmMemorySize | Unreachable) => false
5151
| CPrim1(
5252
AllocateArray | AllocateTuple | AllocateBytes | AllocateString |
5353
BuiltinId |
@@ -98,7 +98,6 @@ module PurityArg: Anf_iterator.IterArgument = {
9898
WasmStoreI32(_) | WasmStoreI64(_) | WasmStoreF32 | WasmStoreF64 |
9999
WasmMemoryCopy |
100100
WasmMemoryFill |
101-
WasmMemorySize |
102101
WasmMemoryCompare,
103102
_,
104103
) =>

compiler/src/middle_end/anftree.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type prim0 =
165165
| AllocateFloat32
166166
| AllocateFloat64
167167
| AllocateRational
168+
| WasmMemorySize
168169
| Unreachable;
169170

170171
type prim1 =
@@ -267,7 +268,6 @@ type primn =
267268
| WasmStoreF64
268269
| WasmMemoryCopy
269270
| WasmMemoryFill
270-
| WasmMemorySize
271271
| WasmMemoryCompare;
272272

273273
let (prim0_of_sexp, sexp_of_prim0) = (

compiler/src/middle_end/anftree.rei

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ type prim0 =
166166
| AllocateFloat32
167167
| AllocateFloat64
168168
| AllocateRational
169+
| WasmMemorySize
169170
| Unreachable;
170171

171172
type prim1 =
@@ -268,7 +269,6 @@ type primn =
268269
| WasmStoreF64
269270
| WasmMemoryCopy
270271
| WasmMemoryFill
271-
| WasmMemorySize
272272
| WasmMemoryCompare;
273273

274274
/** Immediate expressions (requiring no computation) */

compiler/src/parsing/ast_helper.re

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,12 @@ module Toplevel = {
450450
mk(~loc, ~attributes?, PTopProvide(e));
451451
};
452452

453+
module PrimitiveDescription = {
454+
let mk = (~loc, ~ident, ~name, ()) => {
455+
{pprim_ident: ident, pprim_name: name, pprim_loc: loc};
456+
};
457+
};
458+
453459
module ValueDescription = {
454460
let mk = (~loc=?, ~mod_, ~name, ~alias, ~typ, ~prim, ()) => {
455461
let loc = Option.value(~default=Location.dummy_loc, loc);

compiler/src/parsing/ast_helper.rei

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ module Toplevel: {
265265
~loc: loc=?,
266266
~attributes: attributes=?,
267267
provide_flag,
268-
value_description
268+
primitive_description
269269
) =>
270270
toplevel_stmt;
271271
let data:
@@ -295,6 +295,10 @@ module Toplevel: {
295295
toplevel_stmt;
296296
};
297297

298+
module PrimitiveDescription: {
299+
let mk: (~loc: loc, ~ident: str, ~name: str, unit) => primitive_description;
300+
};
301+
298302
module ValueDescription: {
299303
let mk:
300304
(

compiler/src/parsing/ast_mapper.re

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type mapper = {
1515
provide: (mapper, list(provide_item)) => list(provide_item),
1616
value_binding: (mapper, value_binding) => value_binding,
1717
match_branch: (mapper, match_branch) => match_branch,
18+
primitive_description:
19+
(mapper, primitive_description) => primitive_description,
1820
value_description: (mapper, value_description) => value_description,
1921
grain_exception: (mapper, type_exception) => type_exception,
2022
toplevel: (mapper, toplevel_stmt) => toplevel_stmt,
@@ -390,6 +392,15 @@ module Pr = {
390392
};
391393
};
392394

395+
module PD = {
396+
let map = (sub, {pprim_ident: ident, pprim_name: name, pprim_loc: loc}) => {
397+
let pprim_loc = sub.location(sub, loc);
398+
let pprim_ident = map_loc(sub, ident);
399+
let pprim_name = map_loc(sub, name);
400+
{pprim_ident, pprim_name, pprim_loc};
401+
};
402+
};
403+
393404
module VD = {
394405
let map = (sub, {pval_mod: vmod, pval_name: vname, pval_loc: loc} as d) => {
395406
let pval_loc = sub.location(sub, loc);
@@ -415,7 +426,12 @@ module TL = {
415426
| PTopForeign(e, d) =>
416427
Toplevel.foreign(~loc, ~attributes, e, sub.value_description(sub, d))
417428
| PTopPrimitive(e, d) =>
418-
Toplevel.primitive(~loc, ~attributes, e, sub.value_description(sub, d))
429+
Toplevel.primitive(
430+
~loc,
431+
~attributes,
432+
e,
433+
sub.primitive_description(sub, d),
434+
)
419435
| PTopData(dd) =>
420436
Toplevel.data(
421437
~loc,
@@ -465,6 +481,7 @@ let default_mapper = {
465481
provide: Pr.map,
466482
value_binding: V.map,
467483
match_branch: MB.map,
484+
primitive_description: PD.map,
468485
value_description: VD.map,
469486
grain_exception: Exc.map,
470487
toplevel: TL.map,

compiler/src/parsing/ast_mapper.rei

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type mapper = {
3535
provide: (mapper, list(provide_item)) => list(provide_item),
3636
value_binding: (mapper, value_binding) => value_binding,
3737
match_branch: (mapper, match_branch) => match_branch,
38+
primitive_description:
39+
(mapper, primitive_description) => primitive_description,
3840
value_description: (mapper, value_description) => value_description,
3941
grain_exception: (mapper, type_exception) => type_exception,
4042
toplevel: (mapper, toplevel_stmt) => toplevel_stmt,

0 commit comments

Comments
 (0)