Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions compiler/src/codegen/compcore.re
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ let equal_mod = "GRAIN$MODULE$runtime/equal";
let equal_ident = Ident.create_persistent("equal");
let equal_closure_ident = Ident.create_persistent("GRAIN$EXPORT$equal");

/* List concat */
let collection_concat_mod = "GRAIN$MODULE$runtime/concat";
let list_concat_ident = Ident.create_persistent("listConcat");
let list_concat_closure_ident =
Ident.create_persistent("GRAIN$EXPORT$listConcat");
let array_concat_ident = Ident.create_persistent("arrayConcat");
let array_concat_closure_ident =
Ident.create_persistent("GRAIN$EXPORT$arrayConcat");

/* JS-runner support */
let console_mod = "console";
let tracepoint_ident = Ident.create_persistent("tracepoint");

let required_global_imports = [
{
mimp_id: reloc_base,
Expand Down Expand Up @@ -174,6 +187,24 @@ let grain_runtime_imports = [
mimp_setup: MSetupNone,
mimp_used: false,
},
{
mimp_id: list_concat_closure_ident,
mimp_mod: collection_concat_mod,
mimp_name: Ident.name(list_concat_closure_ident),
mimp_type: MGlobalImport(Types.Unmanaged(WasmI32), true),
mimp_kind: MImportWasm,
mimp_setup: MSetupNone,
mimp_used: false,
},
{
mimp_id: array_concat_closure_ident,
mimp_mod: collection_concat_mod,
mimp_name: Ident.name(array_concat_closure_ident),
mimp_type: MGlobalImport(Types.Unmanaged(WasmI32), true),
mimp_kind: MImportWasm,
mimp_setup: MSetupNone,
mimp_used: false,
},
];

let runtime_global_imports =
Expand Down Expand Up @@ -248,6 +279,24 @@ let grain_function_imports = [
mimp_setup: MSetupNone,
mimp_used: false,
},
{
mimp_id: list_concat_ident,
mimp_mod: collection_concat_mod,
mimp_name: Ident.name(list_concat_ident),
mimp_type: MFuncImport([Types.Managed, Types.Managed], [Types.Managed]),
mimp_kind: MImportWasm,
mimp_setup: MSetupNone,
mimp_used: false,
},
{
mimp_id: array_concat_ident,
mimp_mod: collection_concat_mod,
mimp_name: Ident.name(array_concat_ident),
mimp_type: MFuncImport([Types.Managed, Types.Managed], [Types.Managed]),
mimp_kind: MImportWasm,
mimp_setup: MSetupNone,
mimp_used: false,
},
];

let runtime_function_imports =
Expand Down Expand Up @@ -404,6 +453,44 @@ let call_equal = (wasm_mod, env, args) =>
],
Type.int32,
);
let call_list_concat = (wasm_mod, env, args) => {
let args = [
Expression.Global_get.make(
wasm_mod,
get_wasm_imported_name(
collection_concat_mod,
list_concat_closure_ident,
),
Type.int32,
),
...args,
];
Expression.Call.make(
wasm_mod,
get_wasm_imported_name(collection_concat_mod, list_concat_ident),
args,
Type.int32,
);
};
let call_array_concat = (wasm_mod, env, args) => {
let args = [
Expression.Global_get.make(
wasm_mod,
get_wasm_imported_name(
collection_concat_mod,
array_concat_closure_ident,
),
Type.int32,
),
...args,
];
Expression.Call.make(
wasm_mod,
get_wasm_imported_name(collection_concat_mod, array_concat_ident),
args,
Type.int32,
);
};

/** Untags the number */

Expand Down Expand Up @@ -2929,6 +3016,14 @@ and compile_instr = (wasm_mod, env, instr) =>
compile_record_op(wasm_mod, env, record, record_op)
| MClosureOp(closure_op, closure) =>
compile_closure_op(wasm_mod, env, closure, closure_op)
| MCollectionConcat(t, collections) =>
let collections_arr = allocate_array(wasm_mod, env, collections);
let concat =
switch (t) {
| TExpListConcat => call_list_concat
| TExpArrayConcat => call_array_concat
};
concat(wasm_mod, env, [collections_arr]);
| MPrim0(p0) => compile_prim0(wasm_mod, env, p0)
| MPrim1(p1, arg) => compile_prim1(wasm_mod, env, p1, arg, instr.instr_loc)
| MPrim2(p2, arg1, arg2) => compile_prim2(wasm_mod, env, p2, arg1, arg2)
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/codegen/garbage_collection.re
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ let instr_produces_value = instr =>
| MBoxOp(_)
| MArrayOp(_)
| MAdtOp(_)
| MRecordOp(_) => true
| MRecordOp(_)
| MCollectionConcat(_) => true
| MClosureOp(MClosureSetPtr(_), _) => false
| MStore(_) => false
| MSet(_) => true
Expand Down Expand Up @@ -177,6 +178,7 @@ let rec analyze_usage = instrs => {
};
process_imm(imm);
| MClosureOp(closure_op, imm) => process_imm(imm)
| MCollectionConcat(t, collections) => List.iter(process_imm, collections)
| MStore(binds) =>
List.iter(
((bind, instr)) => {
Expand Down Expand Up @@ -502,6 +504,8 @@ let rec apply_gc = (~level, ~loop_context, ~implicit_return=false, instrs) => {
MRecordOp(record_op, handle_imm(~non_gc_instr=true, imm));
| MClosureOp(closure_op, imm) =>
MClosureOp(closure_op, handle_imm(~non_gc_instr=true, imm))
| MCollectionConcat(t, collections) =>
MCollectionConcat(t, List.map(handle_imm, collections))
| MStore(binds) =>
MStore(
List.map(
Expand Down
1 change: 1 addition & 0 deletions compiler/src/codegen/mashtree.re
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ and instr_desc =
| MAdtOp(adt_op, immediate)
| MRecordOp(record_op, immediate)
| MClosureOp(closure_op, immediate)
| MCollectionConcat(Typedtree.collection_concat_type, list(immediate))
| MStore(list((binding, instr))) /* Items in the same list have their backpatching delayed until the end of that list */
| MSet(binding, instr)
| MDrop(instr) /* Ignore the result of an expression. Used for sequences. */
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/codegen/transl_anf.re
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,8 @@ let rec compile_comp = (~id=?, env, c) => {
MRecordSet(idx, compile_imm(env, arg)),
compile_imm(env, record),
)
| CCollectionConcat(t, lists) =>
MCollectionConcat(t, List.map(compile_imm(env), lists))
| CLambda(name, args, body, closure_status) =>
let (body, return_type) = body;
let body = (body, [return_type]);
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/formatting/debug.re
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ let debug_expression = (expr: Parsetree.expression) => {
print_loc("PExpRecordGet", expr.pexp_loc)
| PExpRecordSet(expression, {txt, _}, expression2) =>
print_loc("PExpRecordSet", expr.pexp_loc)
| PExpCollectionConcat(t, collections) =>
print_loc("PExpCollectionConcat", expr.pexp_loc)
| PExpMatch(expression, match_branches) =>
print_loc("PExpMatch", expr.pexp_loc)
| PExpPrim0(prim0) => print_loc("PExpPrim0", expr.pexp_loc)
Expand Down
Loading