From ff280a110ae347d5c6d6050994f2bb092fd1c991 Mon Sep 17 00:00:00 2001 From: Spotandjake <40705786+spotandjake@users.noreply.github.com> Date: Sun, 28 Jul 2024 13:08:52 -0400 Subject: [PATCH] chore(compiler): Make `locs` on `anf_helper` non optional (#1942) Co-authored-by: Oscar Spencer --- compiler/src/middle_end/anf_helper.re | 208 +++++++++----------- compiler/src/middle_end/anf_helper.rei | 93 +++++---- compiler/src/middle_end/linearize.re | 165 ++++++++++++---- compiler/src/middle_end/matchcomp.re | 106 +++++++--- compiler/test/suites/optimizations.re | 259 ++++++++++++++++++++----- 5 files changed, 554 insertions(+), 277 deletions(-) diff --git a/compiler/src/middle_end/anf_helper.re b/compiler/src/middle_end/anf_helper.re index 07915e29d9..73eeac51f8 100644 --- a/compiler/src/middle_end/anf_helper.re +++ b/compiler/src/middle_end/anf_helper.re @@ -9,223 +9,193 @@ type env = Env.t; type ident = Ident.t; type attributes = Typedtree.attributes; -let default_loc = Location.dummy_loc; let default_env = Env.empty; let default_attributes = []; let default_allocation_type = Managed; -let or_default_loc = Option.value(~default=default_loc); let or_default_env = Option.value(~default=default_env); let or_default_attributes = Option.value(~default=default_attributes); let or_default_allocation_type = Option.value(~default=default_allocation_type); module Imm = { - let mk = (~loc=?, ~env=?, d) => { + let mk = (~loc, ~env=?, d) => { imm_desc: d, - imm_loc: or_default_loc(loc), + imm_loc: loc, imm_env: or_default_env(env), imm_analyses: ref([]), }; - let id = (~loc=?, ~env=?, id) => mk(~loc?, ~env?, ImmId(id)); - let const = (~loc=?, ~env=?, const) => mk(~loc?, ~env?, ImmConst(const)); - let trap = (~loc=?, ~env=?, ()) => mk(~loc?, ~env?, ImmTrap); + let id = (~loc, ~env=?, id) => mk(~loc, ~env?, ImmId(id)); + let const = (~loc, ~env=?, const) => mk(~loc, ~env?, ImmConst(const)); + let trap = (~loc, ~env=?, ()) => mk(~loc, ~env?, ImmTrap); }; module Comp = { - let mk = (~loc=?, ~attributes=?, ~allocation_type=?, ~env=?, d) => { + let mk = (~loc, ~attributes=?, ~allocation_type=?, ~env=?, d) => { comp_desc: d, - comp_loc: or_default_loc(loc), + comp_loc: loc, comp_env: or_default_env(env), comp_attributes: or_default_attributes(attributes), comp_allocation_type: or_default_allocation_type(allocation_type), comp_analyses: ref([]), }; - let imm = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, imm) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CImmExpr(imm)); - let number = (~loc=?, ~attributes=?, ~env=?, i) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CNumber(i)); - let int32 = (~loc=?, ~attributes=?, ~env=?, i) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CInt32(i)); - let int64 = (~loc=?, ~attributes=?, ~env=?, i) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CInt64(i)); - let uint32 = (~loc=?, ~attributes=?, ~env=?, i) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CUint32(i)); - let uint64 = (~loc=?, ~attributes=?, ~env=?, i) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CUint64(i)); - let float32 = (~loc=?, ~attributes=?, ~env=?, i) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CFloat32(i)); - let float64 = (~loc=?, ~attributes=?, ~env=?, i) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CFloat64(i)); - let prim0 = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, p0) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CPrim0(p0)); - let prim1 = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, p1, a) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CPrim1(p1, a)); - let prim2 = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, p2, a1, a2) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CPrim2(p2, a1, a2)); - let primn = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, p, args) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CPrimN(p, args)); - let box_assign = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, a1, a2) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CBoxAssign(a1, a2)); - let local_assign = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, a1, a2) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CLocalAssign(a1, a2)); - let assign = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, a1, a2) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CAssign(a1, a2)); - let tuple = (~loc=?, ~attributes=?, ~env=?, elts) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CTuple(elts)); - let array = (~loc=?, ~attributes=?, ~env=?, elts) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CArray(elts)); - let array_get = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, arr, i) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CArrayGet(arr, i)); - let array_set = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, arr, i, a) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CArraySet(arr, i, a)); - let record = (~loc=?, ~attributes=?, ~env=?, type_hash, ttag, elts) => + let imm = (~loc, ~attributes=?, ~allocation_type, ~env=?, imm) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CImmExpr(imm)); + let number = (~loc, ~attributes=?, ~env=?, i) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CNumber(i)); + let int32 = (~loc, ~attributes=?, ~env=?, i) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CInt32(i)); + let int64 = (~loc, ~attributes=?, ~env=?, i) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CInt64(i)); + let uint32 = (~loc, ~attributes=?, ~env=?, i) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CUint32(i)); + let uint64 = (~loc, ~attributes=?, ~env=?, i) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CUint64(i)); + let float32 = (~loc, ~attributes=?, ~env=?, i) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CFloat32(i)); + let float64 = (~loc, ~attributes=?, ~env=?, i) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CFloat64(i)); + let prim0 = (~loc, ~attributes=?, ~allocation_type, ~env=?, p0) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CPrim0(p0)); + let prim1 = (~loc, ~attributes=?, ~allocation_type, ~env=?, p1, a) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CPrim1(p1, a)); + let prim2 = (~loc, ~attributes=?, ~allocation_type, ~env=?, p2, a1, a2) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CPrim2(p2, a1, a2)); + let primn = (~loc, ~attributes=?, ~allocation_type, ~env=?, p, args) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CPrimN(p, args)); + let box_assign = (~loc, ~attributes=?, ~allocation_type, ~env=?, a1, a2) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CBoxAssign(a1, a2)); + let local_assign = (~loc, ~attributes=?, ~allocation_type, ~env=?, a1, a2) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CLocalAssign(a1, a2)); + let assign = (~loc, ~attributes=?, ~allocation_type, ~env=?, a1, a2) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CAssign(a1, a2)); + let tuple = (~loc, ~attributes=?, ~env=?, elts) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CTuple(elts)); + let array = (~loc, ~attributes=?, ~env=?, elts) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CArray(elts)); + let array_get = (~loc, ~attributes=?, ~allocation_type, ~env=?, arr, i) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CArrayGet(arr, i)); + let array_set = (~loc, ~attributes=?, ~allocation_type, ~env=?, arr, i, a) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CArraySet(arr, i, a)); + let record = (~loc, ~attributes=?, ~env=?, type_hash, ttag, elts) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type=Managed, ~env?, CRecord(type_hash, ttag, elts), ); - let adt = (~loc=?, ~attributes=?, ~env=?, type_hash, ttag, vtag, elts) => + let adt = (~loc, ~attributes=?, ~env=?, type_hash, ttag, vtag, elts) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type=Managed, ~env?, CAdt(type_hash, ttag, vtag, elts), ); - let tuple_get = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, idx, tup) => - mk( - ~loc?, - ~attributes?, - ~allocation_type, - ~env?, - CGetTupleItem(idx, tup), - ); + let tuple_get = (~loc, ~attributes=?, ~allocation_type, ~env=?, idx, tup) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CGetTupleItem(idx, tup)); let tuple_set = - (~loc=?, ~attributes=?, ~allocation_type, ~env=?, idx, tup, value) => + (~loc, ~attributes=?, ~allocation_type, ~env=?, idx, tup, value) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type, ~env?, CSetTupleItem(idx, tup, value), ); - let adt_get = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, idx, value) => - mk( - ~loc?, - ~attributes?, - ~allocation_type, - ~env?, - CGetAdtItem(idx, value), - ); - let adt_get_tag = (~loc=?, ~attributes=?, ~env=?, value) => + let adt_get = (~loc, ~attributes=?, ~allocation_type, ~env=?, idx, value) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CGetAdtItem(idx, value)); + let adt_get_tag = (~loc, ~attributes=?, ~env=?, value) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type=Unmanaged(WasmI32), ~env?, CGetAdtTag(value), ); let record_get = - (~loc=?, ~attributes=?, ~allocation_type, ~env=?, idx, record) => + (~loc, ~attributes=?, ~allocation_type, ~env=?, idx, record) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type, ~env?, CGetRecordItem(idx, record), ); let record_set = - (~loc=?, ~attributes=?, ~allocation_type, ~env=?, idx, record, arg) => + (~loc, ~attributes=?, ~allocation_type, ~env=?, idx, record, arg) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type, ~env?, CSetRecordItem(idx, record, arg), ); - let if_ = (~loc=?, ~attributes=?, ~allocation_type, ~env=?, cond, tru, fals) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CIf(cond, tru, fals)); - let for_ = (~loc=?, ~attributes=?, ~env=?, cond, inc, body) => + let if_ = (~loc, ~attributes=?, ~allocation_type, ~env=?, cond, tru, fals) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CIf(cond, tru, fals)); + let for_ = (~loc, ~attributes=?, ~env=?, cond, inc, body) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type=Unmanaged(WasmI32), ~env?, CFor(cond, inc, body), ); - let continue = (~loc=?, ~attributes=?, ~env=?, ()) => + let continue = (~loc, ~attributes=?, ~env=?, ()) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type=Unmanaged(WasmI32), ~env?, CContinue, ); - let break = (~loc=?, ~attributes=?, ~env=?, ()) => + let break = (~loc, ~attributes=?, ~env=?, ()) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type=Unmanaged(WasmI32), ~env?, CBreak, ); - let return = (~loc=?, ~attributes=?, ~env=?, ret) => + let return = (~loc, ~attributes=?, ~env=?, ret) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type=Unmanaged(WasmI32), ~env?, CReturn(ret), ); let switch_ = - ( - ~loc=?, - ~attributes=?, - ~allocation_type, - ~env=?, - arg, - branches, - partial, - ) => + (~loc, ~attributes=?, ~allocation_type, ~env=?, arg, branches, partial) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type, ~env?, CSwitch(arg, branches, partial), ); let app = - ( - ~loc=?, - ~attributes=?, - ~allocation_type, - ~env=?, - ~tail=false, - func, - args, - ) => - mk(~loc?, ~attributes?, ~allocation_type, ~env?, CApp(func, args, tail)); - let lambda = (~loc=?, ~attributes=?, ~env=?, ~name=?, args, body) => + (~loc, ~attributes=?, ~allocation_type, ~env=?, ~tail=false, func, args) => + mk(~loc, ~attributes?, ~allocation_type, ~env?, CApp(func, args, tail)); + let lambda = (~loc, ~attributes=?, ~env=?, ~name=?, args, body) => mk( - ~loc?, + ~loc, ~attributes?, ~allocation_type=Managed, ~env?, CLambda(name, args, body, Uncomputed), ); - let bytes = (~loc=?, ~attributes=?, ~env=?, b) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CBytes(b)); - let string = (~loc=?, ~attributes=?, ~env=?, s) => - mk(~loc?, ~attributes?, ~allocation_type=Managed, ~env?, CString(s)); + let bytes = (~loc, ~attributes=?, ~env=?, b) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CBytes(b)); + let string = (~loc, ~attributes=?, ~env=?, s) => + mk(~loc, ~attributes?, ~allocation_type=Managed, ~env?, CString(s)); }; module AExp = { - let mk = (~loc=?, ~env=?, ~alloc_type, d) => { + let mk = (~loc, ~env=?, ~alloc_type, d) => { anf_desc: d, - anf_loc: or_default_loc(loc), + anf_loc: loc, anf_env: or_default_env(env), anf_analyses: ref([]), anf_allocation_type: alloc_type, @@ -240,7 +210,7 @@ module AExp = { let let_ = ( - ~loc=?, + ~loc, ~env=?, ~global=Nonglobal, ~mut_flag=Immutable, @@ -249,15 +219,15 @@ module AExp = { body, ) => mk( - ~loc?, + ~loc, ~env?, ~alloc_type=alloc_type(body), AELet(global, rec_flag, mut_flag, binds, body), ); - let seq = (~loc=?, ~env=?, hd, tl) => - mk(~loc?, ~env?, ~alloc_type=alloc_type(tl), AESeq(hd, tl)); - let comp = (~loc=?, ~env=?, e) => - mk(~loc?, ~env?, ~alloc_type=e.comp_allocation_type, AEComp(e)); + let seq = (~loc, ~env=?, hd, tl) => + mk(~loc, ~env?, ~alloc_type=alloc_type(tl), AESeq(hd, tl)); + let comp = (~loc, ~env=?, e) => + mk(~loc, ~env?, ~alloc_type=e.comp_allocation_type, AEComp(e)); }; module IncludeDeclaration = { diff --git a/compiler/src/middle_end/anf_helper.rei b/compiler/src/middle_end/anf_helper.rei index 6287bd7be5..d8e947a16a 100644 --- a/compiler/src/middle_end/anf_helper.rei +++ b/compiler/src/middle_end/anf_helper.rei @@ -10,16 +10,16 @@ type ident = Ident.t; type attributes = Typedtree.attributes; module Imm: { - let mk: (~loc: loc=?, ~env: env=?, imm_expression_desc) => imm_expression; - let id: (~loc: loc=?, ~env: env=?, ident) => imm_expression; - let const: (~loc: loc=?, ~env: env=?, constant) => imm_expression; - let trap: (~loc: loc=?, ~env: env=?, unit) => imm_expression; + let mk: (~loc: loc, ~env: env=?, imm_expression_desc) => imm_expression; + let id: (~loc: loc, ~env: env=?, ident) => imm_expression; + let const: (~loc: loc, ~env: env=?, constant) => imm_expression; + let trap: (~loc: loc, ~env: env=?, unit) => imm_expression; }; module Comp: { let mk: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type=?, ~env: env=?, @@ -28,7 +28,7 @@ module Comp: { comp_expression; let imm: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -37,33 +37,33 @@ module Comp: { comp_expression; let number: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~env: env=?, Asttypes.number_type ) => comp_expression; let int32: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, int32) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, int32) => comp_expression; let int64: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, int64) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, int64) => comp_expression; let uint32: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, int32) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, int32) => comp_expression; let uint64: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, int64) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, int64) => comp_expression; let float32: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, float) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, float) => comp_expression; let float64: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, float) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, float) => comp_expression; let prim0: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -72,7 +72,7 @@ module Comp: { comp_expression; let prim1: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -82,7 +82,7 @@ module Comp: { comp_expression; let prim2: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -93,7 +93,7 @@ module Comp: { comp_expression; let primn: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -103,7 +103,7 @@ module Comp: { comp_expression; let box_assign: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -113,7 +113,7 @@ module Comp: { comp_expression; let local_assign: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -123,7 +123,7 @@ module Comp: { comp_expression; let assign: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -133,7 +133,7 @@ module Comp: { comp_expression; let tuple: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~env: env=?, list(imm_expression) @@ -141,7 +141,7 @@ module Comp: { comp_expression; let array: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~env: env=?, list(imm_expression) @@ -149,7 +149,7 @@ module Comp: { comp_expression; let array_get: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -159,7 +159,7 @@ module Comp: { comp_expression; let array_set: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -170,7 +170,7 @@ module Comp: { comp_expression; let record: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~env: env=?, imm_expression, @@ -180,7 +180,7 @@ module Comp: { comp_expression; let adt: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~env: env=?, imm_expression, @@ -191,7 +191,7 @@ module Comp: { comp_expression; let tuple_get: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -201,7 +201,7 @@ module Comp: { comp_expression; let tuple_set: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -212,7 +212,7 @@ module Comp: { comp_expression; let adt_get: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -221,11 +221,11 @@ module Comp: { ) => comp_expression; let adt_get_tag: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, imm_expression) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, imm_expression) => comp_expression; let record_get: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -235,7 +235,7 @@ module Comp: { comp_expression; let record_set: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -246,7 +246,7 @@ module Comp: { comp_expression; let if_: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -257,7 +257,7 @@ module Comp: { comp_expression; let for_: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~env: env=?, option(anf_expression), @@ -266,14 +266,14 @@ module Comp: { ) => comp_expression; let continue: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, unit) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, unit) => comp_expression; let break: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, unit) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, unit) => comp_expression; let return: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~env: env=?, option(imm_expression) @@ -281,7 +281,7 @@ module Comp: { comp_expression; let switch_: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -292,7 +292,7 @@ module Comp: { comp_expression; let app: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~allocation_type: allocation_type, ~env: env=?, @@ -303,7 +303,7 @@ module Comp: { comp_expression; let lambda: ( - ~loc: loc=?, + ~loc: loc, ~attributes: attributes=?, ~env: env=?, ~name: string=?, @@ -312,17 +312,17 @@ module Comp: { ) => comp_expression; let bytes: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, bytes) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, bytes) => comp_expression; let string: - (~loc: loc=?, ~attributes: attributes=?, ~env: env=?, string) => + (~loc: loc, ~attributes: attributes=?, ~env: env=?, string) => comp_expression; }; module AExp: { let mk: ( - ~loc: loc=?, + ~loc: loc, ~env: env=?, ~alloc_type: allocation_type, anf_expression_desc @@ -330,7 +330,7 @@ module AExp: { anf_expression; let let_: ( - ~loc: loc=?, + ~loc: loc, ~env: env=?, ~global: global_flag=?, ~mut_flag: mut_flag=?, @@ -340,9 +340,8 @@ module AExp: { ) => anf_expression; let seq: - (~loc: loc=?, ~env: env=?, comp_expression, anf_expression) => - anf_expression; - let comp: (~loc: loc=?, ~env: env=?, comp_expression) => anf_expression; + (~loc: loc, ~env: env=?, comp_expression, anf_expression) => anf_expression; + let comp: (~loc: loc, ~env: env=?, comp_expression) => anf_expression; }; module IncludeDeclaration: { diff --git a/compiler/src/middle_end/linearize.re b/compiler/src/middle_end/linearize.re index 7900f4bb87..0f5b8b1c17 100644 --- a/compiler/src/middle_end/linearize.re +++ b/compiler/src/middle_end/linearize.re @@ -156,20 +156,45 @@ let lookup_symbol = (~env, ~allocation_type, ~repr, path) => { let convert_bind = (body, bind) => switch (bind) { - | BSeq(exp) => AExp.seq(exp, body) + | BSeq(exp) => AExp.seq(~loc=exp.comp_loc, exp, body) | BLet(name, exp, global) => - AExp.let_(~global, Nonrecursive, [(name, exp)], body) + AExp.let_(~loc=exp.comp_loc, ~global, Nonrecursive, [(name, exp)], body) | BLetMut(name, exp, global) => - AExp.let_(~mut_flag=Mutable, ~global, Nonrecursive, [(name, exp)], body) - | BLetRec(names, global) => AExp.let_(~global, Recursive, names, body) + AExp.let_( + ~loc=exp.comp_loc, + ~mut_flag=Mutable, + ~global, + Nonrecursive, + [(name, exp)], + body, + ) + | BLetRec(names, global) => + AExp.let_( + ~loc=snd(List.hd(names)).comp_loc, + ~global, + Recursive, + names, + body, + ) | BLetRecMut(names, global) => - AExp.let_(~mut_flag=Mutable, ~global, Recursive, names, body) + AExp.let_( + ~loc=snd(List.hd(names)).comp_loc, + ~mut_flag=Mutable, + ~global, + Recursive, + names, + body, + ) }; let convert_binds = anf_binds => { let void_comp = - Comp.imm(~allocation_type=Unmanaged(WasmI32), Imm.const(Const_void)); - let void = AExp.comp(void_comp); + Comp.imm( + ~loc=Location.dummy_loc, + ~allocation_type=Unmanaged(WasmI32), + Imm.const(~loc=Location.dummy_loc, Const_void), + ); + let void = AExp.comp(~loc=Location.dummy_loc, void_comp); let (last_bind, top_binds) = switch (anf_binds) { | [bind, ...rest] => (bind, rest) @@ -177,20 +202,41 @@ let convert_binds = anf_binds => { }; let ans = switch (last_bind) { - | BSeq(exp) => AExp.comp(exp) + | BSeq(exp) => AExp.comp(~loc=exp.comp_loc, exp) | BLet(name, exp, global) => - AExp.let_(~global, Nonrecursive, [(name, exp)], void) + AExp.let_( + ~loc=exp.comp_loc, + ~global, + Nonrecursive, + [(name, exp)], + void, + ) | BLetMut(name, exp, global) => AExp.let_( + ~loc=exp.comp_loc, ~mut_flag=Mutable, ~global, Nonrecursive, [(name, exp)], void, ) - | BLetRec(names, global) => AExp.let_(~global, Recursive, names, void) + | BLetRec(names, global) => + AExp.let_( + ~loc=snd(List.hd(names)).comp_loc, + ~global, + Recursive, + names, + void, + ) | BLetRecMut(names, global) => - AExp.let_(~mut_flag=Mutable, ~global, Recursive, names, void) + AExp.let_( + ~loc=snd(List.hd(names)).comp_loc, + ~mut_flag=Mutable, + ~global, + Recursive, + names, + void, + ) }; List.fold_left(convert_bind, ans, top_binds); }; @@ -230,45 +276,77 @@ let transl_const = switch (c) { | Const_number(n) => Right( - with_bind("number", tmp => [BLet(tmp, Comp.number(n), Nonglobal)]), + with_bind("number", tmp => + [BLet(tmp, Comp.number(~loc, n), Nonglobal)] + ), ) | Const_bigint(data) => Right( with_bind("number", tmp => - [BLet(tmp, Comp.number(Const_number_bigint(data)), Nonglobal)] + [ + BLet(tmp, Comp.number(~loc, Const_number_bigint(data)), Nonglobal), + ] ), ) | Const_rational(data) => Right( with_bind("rational", tmp => - [BLet(tmp, Comp.number(Const_number_rational(data)), Nonglobal)] + [ + BLet( + tmp, + Comp.number(~loc, Const_number_rational(data)), + Nonglobal, + ), + ] ), ) | Const_int32(i) => - Right(with_bind("int32", tmp => [BLet(tmp, Comp.int32(i), Nonglobal)])) + Right( + with_bind("int32", tmp => + [BLet(tmp, Comp.int32(~loc, i), Nonglobal)] + ), + ) | Const_int64(i) => - Right(with_bind("int64", tmp => [BLet(tmp, Comp.int64(i), Nonglobal)])) + Right( + with_bind("int64", tmp => + [BLet(tmp, Comp.int64(~loc, i), Nonglobal)] + ), + ) | Const_uint32(i) => Right( - with_bind("uint32", tmp => [BLet(tmp, Comp.uint32(i), Nonglobal)]), + with_bind("uint32", tmp => + [BLet(tmp, Comp.uint32(~loc, i), Nonglobal)] + ), ) | Const_uint64(i) => Right( - with_bind("uint64", tmp => [BLet(tmp, Comp.uint64(i), Nonglobal)]), + with_bind("uint64", tmp => + [BLet(tmp, Comp.uint64(~loc, i), Nonglobal)] + ), ) | Const_float64(i) => Right( - with_bind("float64", tmp => [BLet(tmp, Comp.float64(i), Nonglobal)]), + with_bind("float64", tmp => + [BLet(tmp, Comp.float64(~loc, i), Nonglobal)] + ), ) | Const_float32(i) => Right( - with_bind("float32", tmp => [BLet(tmp, Comp.float32(i), Nonglobal)]), + with_bind("float32", tmp => + [BLet(tmp, Comp.float32(~loc, i), Nonglobal)] + ), ) | Const_bytes(b) => - Right(with_bind("bytes", tmp => [BLet(tmp, Comp.bytes(b), Nonglobal)])) + Right( + with_bind("bytes", tmp => + [BLet(tmp, Comp.bytes(~loc, b), Nonglobal)] + ), + ) | Const_string(s) => - Right(with_bind("str", tmp => [BLet(tmp, Comp.string(s), Nonglobal)])) - | _ => Left(Imm.const(c)) + Right( + with_bind("str", tmp => [BLet(tmp, Comp.string(~loc, s), Nonglobal)]), + ) + | _ => Left(Imm.const(~loc, c)) }; }; @@ -549,11 +627,11 @@ let rec transl_imm = ], ); | TExpContinue => ( - Imm.const(Const_void), + Imm.const(~loc, Const_void), [BSeq(Comp.continue(~loc, ~env, ()))], ) | TExpBreak => ( - Imm.const(Const_void), + Imm.const(~loc, Const_void), [BSeq(Comp.break(~loc, ~env, ()))], ) | TExpReturn(Some({exp_desc: TExpApp(_)} as return)) => @@ -567,7 +645,7 @@ let rec transl_imm = | None => (None, []) }; ( - Imm.const(Const_void), + Imm.const(~loc, Const_void), value_setup @ [BSeq(Comp.return(~loc, ~env, value_imm))], ); | TExpApp( @@ -599,7 +677,7 @@ let rec transl_imm = ); if (tail) { ( - Imm.const(Const_void), + Imm.const(~loc, Const_void), setup @ [BSeq(Comp.return(~loc, ~env, Some(imm)))], ); } else { @@ -637,7 +715,7 @@ let rec transl_imm = ), ], ); - | TExpBlock([]) => (Imm.const(Const_void), []) + | TExpBlock([]) => (Imm.const(~loc, Const_void), []) | TExpBlock([stmt]) => transl_imm(stmt) | TExpBlock(stmts) => let stmts = List.rev_map(transl_comp_expression, stmts); @@ -653,7 +731,7 @@ let rec transl_imm = stmts, ); (Imm.id(~loc, ~env, tmp), setup); - | TExpLet(Nonrecursive, _, []) => (Imm.const(Const_void), []) + | TExpLet(Nonrecursive, _, []) => (Imm.const(~loc, Const_void), []) | TExpLet( Nonrecursive, mut_flag, @@ -684,6 +762,7 @@ let rec transl_imm = let (exp_ans, exp_setup) = transl_imm(vb_expr); let boxed = Comp.prim1( + ~loc=Location.dummy_loc, ~allocation_type=get_allocation_type(pat_env, pat_type), BoxBind, exp_ans, @@ -747,7 +826,7 @@ let rec transl_imm = BLetRec(List.combine(names, new_binds), Nonglobal), BLet( tmp, - Comp.imm(~allocation_type, Imm.const(Const_void)), + Comp.imm(~loc, ~allocation_type, Imm.const(~loc, Const_void)), Nonglobal, ), ], @@ -1057,6 +1136,7 @@ and transl_comp_expression = switch (Hashtbl.find_opt(Builtin_types.builtin_idents, builtin)) { | Some(id) => ( Comp.imm( + ~loc, ~allocation_type, Imm.const( ~loc, @@ -1119,9 +1199,11 @@ and transl_comp_expression = ~allocation_type, arg_imm, AExp.comp( + ~loc, Comp.imm( + ~loc, ~allocation_type=Unmanaged(WasmI32), - Imm.const(Const_void), + Imm.const(~loc, Const_void), ), ), throw_error, @@ -1199,7 +1281,10 @@ and transl_comp_expression = ), cond_setup, ); - | TExpBlock([]) => (Comp.imm(~allocation_type, Imm.const(Const_void)), []) + | TExpBlock([]) => ( + Comp.imm(~loc, ~allocation_type, Imm.const(~loc, Const_void)), + [], + ) | TExpBlock(stmts) => let stmts = List.rev_map(transl_comp_expression, stmts); // stmts is non-empty, so this cannot fail @@ -1214,7 +1299,7 @@ and transl_comp_expression = ); (last_ans, setup); | TExpLet(Nonrecursive, _, []) => ( - Comp.imm(~allocation_type, Imm.const(Const_void)), + Comp.imm(~loc, ~allocation_type, Imm.const(~loc, Const_void)), [], ) @@ -1242,6 +1327,7 @@ and transl_comp_expression = let (imm, imm_setup) = transl_imm(vb_expr); ( Comp.prim1( + ~loc=Location.dummy_loc, ~allocation_type= get_allocation_type(vb_expr.exp_env, vb_expr.exp_type), BoxBind, @@ -1307,7 +1393,7 @@ and transl_comp_expression = binds, ); ( - Comp.imm(~allocation_type, Imm.const(Const_void)), + Comp.imm(~loc, ~allocation_type, Imm.const(~loc, Const_void)), List.concat(new_setup) @ [BLetRec(List.combine(names, new_binds), Nonglobal)], ); @@ -1348,6 +1434,7 @@ and transl_comp_expression = BLet( id, Comp.imm( + ~loc, ~allocation_type= get_allocation_type(arg.pat_env, arg.pat_type), Imm.id(~loc, ~env, tmp), @@ -1431,7 +1518,13 @@ and transl_comp_expression = func_setup @ List.concat(new_setup), ); ( - Comp.imm(~attributes, ~allocation_type, ~env, Imm.trap(~loc, ~env, ())), + Comp.imm( + ~loc, + ~attributes, + ~allocation_type, + ~env, + Imm.trap(~loc, ~env, ()), + ), ans_setup @ [BSeq(ans)], ); | TExpApp( @@ -1643,7 +1736,7 @@ let rec transl_anf_statement = ~mut_flag, ~global=Global, vb_pat, - Imm.id(tmp), + Imm.id(~loc=Location.dummy_loc, tmp), ); (Some(exp_setup @ setup @ rest_setup), rest_imp); | TTopLet(Recursive, mut_flag, binds) => diff --git a/compiler/src/middle_end/matchcomp.re b/compiler/src/middle_end/matchcomp.re index f5c2ba1355..c1275ad5bb 100644 --- a/compiler/src/middle_end/matchcomp.re +++ b/compiler/src/middle_end/matchcomp.re @@ -729,21 +729,28 @@ module MatchTreeCompiler = { (bind, body) => switch (bind) { | BLet(name, exp, global) => - AExp.let_(~global, Nonrecursive, [(name, exp)], body) + AExp.let_( + ~loc=exp.comp_loc, + ~global, + Nonrecursive, + [(name, exp)], + body, + ) | BLetMut(name, exp, global) => AExp.let_( + ~loc=exp.comp_loc, ~global, ~mut_flag=Mutable, Nonrecursive, [(name, exp)], body, ) - | BSeq(exp) => AExp.seq(exp, body) + | BSeq(exp) => AExp.seq(~loc=exp.comp_loc, exp, body) | _ => failwith("match_comp: compile_tree_help: unsupported binding type") }, setup, - AExp.comp(ans), + AExp.comp(~loc=ans.comp_loc, ans), ); }; @@ -753,19 +760,21 @@ module MatchTreeCompiler = { if (mut_boxing) { BSeq( Comp.assign( + ~loc=Location.dummy_loc, ~env, ~allocation_type=Managed, - Imm.id(name), - Imm.id(value), + Imm.id(~loc=Location.dummy_loc, name), + Imm.id(~loc=Location.dummy_loc, value), ), ); } else { BSeq( Comp.local_assign( + ~loc=Location.dummy_loc, ~env, ~allocation_type=Managed, name, - Imm.id(value), + Imm.id(~loc=Location.dummy_loc, value), ), ); }, @@ -780,14 +789,16 @@ module MatchTreeCompiler = { let assign = id => if (mut_boxing) { Comp.assign( + ~loc=Location.dummy_loc, ~env, ~allocation_type= get_allocation_type(pat.pat_env, pat.pat_type), - Imm.id(id), + Imm.id(~loc=Location.dummy_loc, id), value, ); } else { Comp.local_assign( + ~loc=Location.dummy_loc, ~env, ~allocation_type= get_allocation_type(pat.pat_env, pat.pat_type), @@ -833,8 +844,12 @@ module MatchTreeCompiler = { let env = expr.imm_env; ( Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), - Imm.const(Const_number(Const_number_int(Int64.of_int(i)))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(Int64.of_int(i))), + ), ), get_bindings(~mut_boxing, env, patterns, values, aliases), ); @@ -880,6 +895,7 @@ module MatchTreeCompiler = { ); ( Comp.if_( + ~loc=Location.dummy_loc, ~allocation_type=true_comp.comp_allocation_type, cond, fold_tree(true_setup, true_comp), @@ -919,10 +935,14 @@ module MatchTreeCompiler = { let (const, const_setup) = switch (helpConst(const)) { | Left(imm) => (imm, []) - | Right((name, binds)) => (Imm.id(name), binds) + | Right((name, binds)) => ( + Imm.id(~loc=Location.dummy_loc, name), + binds, + ) }; let cond = Comp.prim2( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), equality_op, cur_value, @@ -957,21 +977,30 @@ module MatchTreeCompiler = { let alias_binding = BLet( alias, - Comp.imm(~allocation_type=Managed, cur_value), + Comp.imm( + ~loc=Location.dummy_loc, + ~allocation_type=Managed, + cur_value, + ), Nonglobal, ); ( Comp.if_( + ~loc=Location.dummy_loc, ~allocation_type=true_comp.comp_allocation_type, - Imm.id(cond_id), + Imm.id(~loc=Location.dummy_loc, cond_id), fold_tree(true_setup, true_comp), fold_tree(false_setup, false_comp), ), [alias_binding, ...cond_setup], ); | Fail => ( - Comp.imm(~allocation_type=Unmanaged(WasmI32), Imm.trap()), + Comp.imm( + ~loc=Location.dummy_loc, + ~allocation_type=Unmanaged(WasmI32), + Imm.trap(~loc=Location.dummy_loc, ()), + ), [], ) | Explode(matrix_type, alias, rest) => @@ -992,6 +1021,7 @@ module MatchTreeCompiler = { BLet( id, Comp.adt_get( + ~loc=Location.dummy_loc, ~allocation_type=Managed, Int32.of_int(idx), cur_value, @@ -1010,6 +1040,7 @@ module MatchTreeCompiler = { BLet( id, Comp.tuple_get( + ~loc=Location.dummy_loc, ~allocation_type=Managed, Int32.of_int(idx), cur_value, @@ -1026,8 +1057,10 @@ module MatchTreeCompiler = { BLet( id, Comp.array_get( + ~loc=Location.dummy_loc, ~allocation_type=Managed, Imm.const( + ~loc=Location.dummy_loc, Const_number(Const_number_int(Int64.of_int(idx))), ), cur_value, @@ -1045,6 +1078,7 @@ module MatchTreeCompiler = { BLet( id, Comp.record_get( + ~loc=Location.dummy_loc, ~allocation_type=Managed, Int32.of_int(label_pos), cur_value, @@ -1061,7 +1095,7 @@ module MatchTreeCompiler = { let new_values = List.map( fun - | BLet(id, _, _) => Imm.id(id) + | BLet(id, _, _) => Imm.id(~loc=Location.dummy_loc, id) | _ => failwith( "Impossible: matchcomp: compile_tree_help: binding was not BLet", @@ -1074,7 +1108,11 @@ module MatchTreeCompiler = { let bindings = [ BLet( alias, - Comp.imm(~allocation_type=Managed, cur_value), + Comp.imm( + ~loc=Location.dummy_loc, + ~allocation_type=Managed, + cur_value, + ), Nonglobal, ), ...bindings, @@ -1124,12 +1162,15 @@ module MatchTreeCompiler = { helpConst, ); let value_constr_name = Ident.create("match_constructor"); - let value_constr_id = Imm.id(value_constr_name); + let value_constr_id = + Imm.id(~loc=Location.dummy_loc, value_constr_name); let value_constr = switch (switch_type) { - | ConstructorSwitch => Comp.adt_get_tag(cur_value) + | ConstructorSwitch => + Comp.adt_get_tag(~loc=Location.dummy_loc, cur_value) | ArraySwitch => Comp.prim1( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), ArrayLength, cur_value, @@ -1141,17 +1182,19 @@ module MatchTreeCompiler = { List.fold_left( ((body_ans, body_setup), (tag, tree)) => { let cmp_id_name = Ident.create("match_cmp_constructors"); - let cmp_id = Imm.id(cmp_id_name); + let cmp_id = Imm.id(~loc=Location.dummy_loc, cmp_id_name); /* If the constructor has the correct tag, execute this branch. Otherwise continue. */ let setup = [ BLet( cmp_id_name, Comp.prim2( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), Is, value_constr_id, Imm.const( + ~loc=Location.dummy_loc, Const_number(Const_number_int(Int64.of_int(tag))), ), ), @@ -1171,6 +1214,7 @@ module MatchTreeCompiler = { ); let ans = Comp.if_( + ~loc=Location.dummy_loc, ~allocation_type=tree_ans.comp_allocation_type, cmp_id, fold_tree(tree_setup, tree_ans), @@ -1199,19 +1243,32 @@ module MatchTreeCompiler = { let dummy_value = switch (allocation_type) { | Managed - | Unmanaged(WasmI32) => Imm.const(Const_wasmi32(0l)) - | Unmanaged(WasmI64) => Imm.const(Const_wasmi64(0L)) - | Unmanaged(WasmF32) => Imm.const(Const_wasmf32(0.)) - | Unmanaged(WasmF64) => Imm.const(Const_wasmf64(0.)) + | Unmanaged(WasmI32) => + Imm.const(~loc=Location.dummy_loc, Const_wasmi32(0l)) + | Unmanaged(WasmI64) => + Imm.const(~loc=Location.dummy_loc, Const_wasmi64(0L)) + | Unmanaged(WasmF32) => + Imm.const(~loc=Location.dummy_loc, Const_wasmf32(0.)) + | Unmanaged(WasmF64) => + Imm.const(~loc=Location.dummy_loc, Const_wasmf64(0.)) }; if (mut_boxing) { BLetMut( id, - Comp.prim1(~allocation_type=Managed, BoxBind, dummy_value), + Comp.prim1( + ~loc=Location.dummy_loc, + ~allocation_type=Managed, + BoxBind, + dummy_value, + ), Nonglobal, ); } else { - BLetMut(id, Comp.imm(~allocation_type, dummy_value), global); + BLetMut( + id, + Comp.imm(~loc=Location.dummy_loc, ~allocation_type, dummy_value), + global, + ); }; }; switch (pat.pat_desc) { @@ -1266,8 +1323,9 @@ module MatchTreeCompiler = { ); ( Comp.switch_( + ~loc=Location.dummy_loc, ~allocation_type, - Imm.id(jmp_name), + Imm.id(~loc=Location.dummy_loc, jmp_name), switch_branches, partial, ), diff --git a/compiler/test/suites/optimizations.re b/compiler/test/suites/optimizations.re index fec048a60b..291ecb5fc7 100644 --- a/compiler/test/suites/optimizations.re +++ b/compiler/test/suites/optimizations.re @@ -3,6 +3,7 @@ open Grain_tests.Runner; open Grain_middle_end.Anftree; open Grain_middle_end.Anf_helper; open Grain_utils; +open Grain_parsing; describe("optimizations", ({test, testSkip}) => { let test_or_skip = @@ -67,9 +68,14 @@ describe("optimizations", ({test, testSkip}) => { "test_dead_branch_elimination_1.gr", "{ if (true) {4} else {5} }", AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Managed, - Imm.const(Const_number(Const_number_int(4L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(4L)), + ), ), ), ); @@ -77,9 +83,14 @@ describe("optimizations", ({test, testSkip}) => { "test_dead_branch_elimination_2.gr", "{ if (false) {4} else {5} }", AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Managed, - Imm.const(Const_number(Const_number_int(5L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(5L)), + ), ), ), ); @@ -87,9 +98,14 @@ describe("optimizations", ({test, testSkip}) => { "test_dead_branch_elimination_3.gr", "{ let x = true; if (x) {4} else {5} }", AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Managed, - Imm.const(Const_number(Const_number_int(4L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(4L)), + ), ), ), ); @@ -97,9 +113,14 @@ describe("optimizations", ({test, testSkip}) => { "test_dead_branch_elimination_4.gr", "{let x = if (true) {4} else {5}; x}", AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Managed, - Imm.const(Const_number(Const_number_int(4L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(4L)), + ), ), ), ); @@ -112,9 +133,14 @@ describe("optimizations", ({test, testSkip}) => { "test_const_propagation.gr", "{\n let x = 4;\n let y = x;\n x}", AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Managed, - Imm.const(Const_number(Const_number_int(4L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(4L)), + ), ), ), ); @@ -126,13 +152,20 @@ describe("optimizations", ({test, testSkip}) => { open Grain_typed; let x = Ident.create("lambda_arg"); AExp.comp( + ~loc=Location.dummy_loc, Comp.lambda( + ~loc=Location.dummy_loc, [(x, Managed)], ( AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Managed, - Imm.const(Const_number(Const_number_int(4L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(4L)), + ), ), ), Managed, @@ -147,13 +180,24 @@ describe("optimizations", ({test, testSkip}) => { "{\n let x = 5;\n let y = 12;\n let z = y;\n {\n let y = x;\n x\n }\n x + y}", Grain_typed.( AExp.comp( + ~loc=Location.dummy_loc, Comp.app( + ~loc=Location.dummy_loc, ~tail=true, ~allocation_type=Managed, - (Imm.id(Ident.create("+")), ([Managed, Managed], Managed)), + ( + Imm.id(~loc=Location.dummy_loc, Ident.create("+")), + ([Managed, Managed], Managed), + ), [ - Imm.const(Const_number(Const_number_int(5L))), - Imm.const(Const_number(Const_number_int(12L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(5L)), + ), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(12L)), + ), ], ), ) @@ -166,13 +210,22 @@ describe("optimizations", ({test, testSkip}) => { open Grain_typed; let arg = Ident.create("lambda_arg"); AExp.comp( - Comp.lambda([(arg, Managed)]) @@ + ~loc=Location.dummy_loc, + Comp.lambda(~loc=Location.dummy_loc, [(arg, Managed)]) @@ ( - AExp.comp @@ - Comp.tuple([ - Imm.id(arg), - Imm.const(Const_number(Const_number_int(1L))), - ]), + AExp.comp( + ~loc=Location.dummy_loc, + Comp.tuple( + ~loc=Location.dummy_loc, + [ + Imm.id(~loc=Location.dummy_loc, arg), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(1L)), + ), + ], + ), + ), Managed, ), ); @@ -185,13 +238,20 @@ describe("optimizations", ({test, testSkip}) => { open Grain_typed; let x = Ident.create("lambda_arg"); AExp.comp( + ~loc=Location.dummy_loc, Comp.lambda( + ~loc=Location.dummy_loc, [(x, Managed)], ( AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Managed, - Imm.const(Const_number(Const_number_int(1L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(1L)), + ), ), ), Managed, @@ -208,33 +268,45 @@ describe("optimizations", ({test, testSkip}) => { let foo = Ident.create("foo"); let x = Ident.create("x"); AExp.let_( + ~loc=Location.dummy_loc, Nonrecursive, ~global=Global, [ ( foo, Comp.lambda( + ~loc=Location.dummy_loc, ~name="foo", [], ( AExp.let_( + ~loc=Location.dummy_loc, Nonrecursive, ~mut_flag=Mutable, [ ( x, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Managed, - Imm.const(Const_number(Const_number_int(5L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(5L)), + ), ), ), ], ) @@ AExp.comp( + ~loc=Location.dummy_loc, Comp.local_assign( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), x, - Imm.const(Const_number(Const_number_int(6L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(6L)), + ), ), ), Unmanaged(WasmI32), @@ -243,9 +315,11 @@ describe("optimizations", ({test, testSkip}) => { ), ], AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), - Imm.const(Const_void), + Imm.const(~loc=Location.dummy_loc, Const_void), ), ), ); @@ -260,41 +334,52 @@ describe("optimizations", ({test, testSkip}) => { let bar = Ident.create("bar"); let foo = Ident.create("foo"); AExp.let_( + ~loc=Location.dummy_loc, Nonrecursive, ~global=Global, [ ( bar, Comp.lambda( + ~loc=Location.dummy_loc, ~name="bar", [], ( AExp.let_( + ~loc=Location.dummy_loc, Nonrecursive, [ ( x, Comp.prim1( + ~loc=Location.dummy_loc, ~allocation_type=Managed, BoxBind, - Imm.const(Const_number(Const_number_int(5L))), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(5L)), + ), ), ), ], ) @@ AExp.let_( + ~loc=Location.dummy_loc, Nonrecursive, [ ( foo, Comp.lambda( + ~loc=Location.dummy_loc, [], ( AExp.comp( + ~loc=Location.dummy_loc, Comp.prim1( + ~loc=Location.dummy_loc, ~allocation_type=Managed, UnboxBind, - Imm.id(x), + Imm.id(~loc=Location.dummy_loc, x), ), ), Managed, @@ -304,10 +389,12 @@ describe("optimizations", ({test, testSkip}) => { ], ) @@ AExp.comp( + ~loc=Location.dummy_loc, Comp.app( + ~loc=Location.dummy_loc, ~tail=true, ~allocation_type=Managed, - (Imm.id(foo), ([], Managed)), + (Imm.id(~loc=Location.dummy_loc, foo), ([], Managed)), [], ), ), @@ -317,9 +404,11 @@ describe("optimizations", ({test, testSkip}) => { ), ], AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), - Imm.const(Const_void), + Imm.const(~loc=Location.dummy_loc, Const_void), ), ), ); @@ -336,37 +425,57 @@ describe("optimizations", ({test, testSkip}) => { let arg = Ident.create("lambda_arg"); let app = Ident.create("app"); AExp.let_( + ~loc=Location.dummy_loc, Nonrecursive, [ ( foo, - Comp.lambda([(arg, Managed)]) @@ + Comp.lambda(~loc=Location.dummy_loc, [(arg, Managed)]) @@ ( - AExp.comp @@ Comp.imm(~allocation_type=Managed) @@ Imm.id(arg), + AExp.comp(~loc=Location.dummy_loc) @@ + Comp.imm(~loc=Location.dummy_loc, ~allocation_type=Managed) @@ + Imm.id(~loc=Location.dummy_loc, arg), Managed, ), ), ], ) @@ AExp.let_( + ~loc=Location.dummy_loc, Nonrecursive, [ ( app, Comp.app( + ~loc=Location.dummy_loc, ~allocation_type=Managed, - (Imm.id(foo), ([Managed], Managed)), - [Imm.const(Const_number(Const_number_int(3L)))], + (Imm.id(~loc=Location.dummy_loc, foo), ([Managed], Managed)), + [ + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(3L)), + ), + ], ), ), ], ) @@ - AExp.comp @@ + AExp.comp(~loc=Location.dummy_loc) @@ Comp.app( + ~loc=Location.dummy_loc, ~allocation_type=Managed, ~tail=true, - (Imm.id(plus), ([Managed, Managed], Managed)), - [Imm.id(app), Imm.const(Const_number(Const_number_int(5L)))], + ( + Imm.id(~loc=Location.dummy_loc, plus), + ([Managed, Managed], Managed), + ), + [ + Imm.id(~loc=Location.dummy_loc, app), + Imm.const( + ~loc=Location.dummy_loc, + Const_number(Const_number_int(5L)), + ), + ], ); }, ); @@ -425,12 +534,14 @@ describe("optimizations", ({test, testSkip}) => { let arg = Ident.create("lambda_arg"); let app = Ident.create("app"); AExp.let_( + ~loc=Location.dummy_loc, ~global=Global, Nonrecursive, [ ( foo, Comp.lambda( + ~loc=Location.dummy_loc, ~name=Ident.name(foo), ~attributes=[ Grain_parsing.Location.mknoloc(Typedtree.Disable_gc), @@ -438,23 +549,39 @@ describe("optimizations", ({test, testSkip}) => { [(arg, Managed), (arg, Managed), (arg, Managed)], ( AExp.let_( + ~loc=Location.dummy_loc, Nonrecursive, [ ( app, Comp.app( + ~loc=Location.dummy_loc, ~allocation_type=Managed, - (Imm.id(plus), ([Managed, Managed], Managed)), - [Imm.id(arg), Imm.id(arg)], + ( + Imm.id(~loc=Location.dummy_loc, plus), + ([Managed, Managed], Managed), + ), + [ + Imm.id(~loc=Location.dummy_loc, arg), + Imm.id(~loc=Location.dummy_loc, arg), + ], ), ), ], AExp.comp( + ~loc=Location.dummy_loc, Comp.app( + ~loc=Location.dummy_loc, ~allocation_type=Managed, ~tail=true, - (Imm.id(plus), ([Managed, Managed], Managed)), - [Imm.id(app), Imm.id(arg)], + ( + Imm.id(~loc=Location.dummy_loc, plus), + ([Managed, Managed], Managed), + ), + [ + Imm.id(~loc=Location.dummy_loc, app), + Imm.id(~loc=Location.dummy_loc, arg), + ], ), ), ), @@ -465,9 +592,11 @@ describe("optimizations", ({test, testSkip}) => { ], ) @@ AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), - Imm.const(Const_void), + Imm.const(~loc=Location.dummy_loc, Const_void), ), ); }, @@ -491,12 +620,14 @@ describe("optimizations", ({test, testSkip}) => { let fill = Ident.create("fill"); let copy = Ident.create("copy"); AExp.let_( + ~loc=Location.dummy_loc, ~global=Global, Nonrecursive, [ ( foo, Comp.lambda( + ~loc=Location.dummy_loc, ~name=Ident.name(foo), ~attributes=[ Grain_parsing.Location.mknoloc(Typedtree.Disable_gc), @@ -504,10 +635,12 @@ describe("optimizations", ({test, testSkip}) => { [], ( AExp.seq( + ~loc=Location.dummy_loc, Comp.app( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), ( - Imm.id(fill), + Imm.id(~loc=Location.dummy_loc, fill), ( [ Unmanaged(WasmI32), @@ -518,17 +651,19 @@ describe("optimizations", ({test, testSkip}) => { ), ), [ - Imm.const(Const_wasmi32(0l)), - Imm.const(Const_wasmi32(0l)), - Imm.const(Const_wasmi32(0l)), + Imm.const(~loc=Location.dummy_loc, Const_wasmi32(0l)), + Imm.const(~loc=Location.dummy_loc, Const_wasmi32(0l)), + Imm.const(~loc=Location.dummy_loc, Const_wasmi32(0l)), ], ), AExp.comp( + ~loc=Location.dummy_loc, Comp.app( + ~loc=Location.dummy_loc, ~tail=true, ~allocation_type=Unmanaged(WasmI32), ( - Imm.id(copy), + Imm.id(~loc=Location.dummy_loc, copy), ( [ Unmanaged(WasmI32), @@ -539,9 +674,15 @@ describe("optimizations", ({test, testSkip}) => { ), ), [ - Imm.const(Const_wasmi32(0l)), - Imm.const(Const_wasmi32(0l)), - Imm.const(Const_wasmi32(0l)), + Imm.const(~loc=Location.dummy_loc, Const_wasmi32(0l)), + Imm.const( + ~loc=Location.dummy_loc, + Const_wasmi32(0l), + ), + Imm.const( + ~loc=Location.dummy_loc, + Const_wasmi32(0l), + ), ], ), ), @@ -553,9 +694,11 @@ describe("optimizations", ({test, testSkip}) => { ], ) @@ AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), - Imm.const(Const_void), + Imm.const(~loc=Location.dummy_loc, Const_void), ), ); }, @@ -575,12 +718,14 @@ describe("optimizations", ({test, testSkip}) => { open Grain_typed; let foo = Ident.create("foo"); AExp.let_( + ~loc=Location.dummy_loc, ~global=Global, Nonrecursive, [ ( foo, Comp.lambda( + ~loc=Location.dummy_loc, ~name=Ident.name(foo), ~attributes=[ Grain_parsing.Location.mknoloc(Typedtree.Disable_gc), @@ -588,23 +733,33 @@ describe("optimizations", ({test, testSkip}) => { [], ( AExp.seq( + ~loc=Location.dummy_loc, Comp.primn( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), WasmMemoryFill, [ - Imm.const(Const_wasmi32(0l)), - Imm.const(Const_wasmi32(0l)), - Imm.const(Const_wasmi32(0l)), + Imm.const(~loc=Location.dummy_loc, Const_wasmi32(0l)), + Imm.const(~loc=Location.dummy_loc, Const_wasmi32(0l)), + Imm.const(~loc=Location.dummy_loc, Const_wasmi32(0l)), ], ), AExp.comp( + ~loc=Location.dummy_loc, Comp.primn( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), WasmMemoryCopy, [ - Imm.const(Const_wasmi32(0l)), - Imm.const(Const_wasmi32(0l)), - Imm.const(Const_wasmi32(0l)), + Imm.const(~loc=Location.dummy_loc, Const_wasmi32(0l)), + Imm.const( + ~loc=Location.dummy_loc, + Const_wasmi32(0l), + ), + Imm.const( + ~loc=Location.dummy_loc, + Const_wasmi32(0l), + ), ], ), ), @@ -616,9 +771,11 @@ describe("optimizations", ({test, testSkip}) => { ], ) @@ AExp.comp( + ~loc=Location.dummy_loc, Comp.imm( + ~loc=Location.dummy_loc, ~allocation_type=Unmanaged(WasmI32), - Imm.const(Const_void), + Imm.const(~loc=Location.dummy_loc, Const_void), ), ); },