Skip to content

Commit

Permalink
refactor: use inline record for J.Caml_block
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Oct 27, 2024
1 parent 746338e commit e617acb
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 38 deletions.
7 changes: 6 additions & 1 deletion jscomp/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ and expression_desc =
| Array of expression list * mutable_flag
| Optional_block of expression * bool
(* [true] means [identity] *)
| Caml_block of expression list * mutable_flag * expression * tag_info
| Caml_block of {
fields : expression list;
mutable_flag : mutable_flag;
tag : expression;
tag_info : tag_info;
}
(* The third argument is [tag] , forth is [tag_info] *)
(* | Caml_uninitialized_obj of expression * expression *)
(* [tag] and [size] tailed for [Obj.new_block] *)
Expand Down
7 changes: 4 additions & 3 deletions jscomp/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
no_side_effect a && no_side_effect b
| Is_null_or_undefined b -> no_side_effect b
| Str (b, _) -> b
| Array (xs, _mutable_flag) | Caml_block (xs, _mutable_flag, _, _) ->
| Array (xs, _mutable_flag)
| Caml_block { fields = xs; mutable_flag = _mutable_flag; _ } ->
(* create [immutable] block,
does not really mean that this opreation itself is [pure].
Expand Down Expand Up @@ -206,9 +207,9 @@ let rec eq_expression ({ expression_desc = x0; _ } : J.expression)
match y0 with
| Optional_block (a1, b1) -> b0 = b1 && eq_expression a0 a1
| _ -> false)
| Caml_block (ls0, flag0, tag0, _) -> (
| Caml_block { fields = ls0; mutable_flag = flag0; tag = tag0; _ } -> (
match y0 with
| Caml_block (ls1, flag1, tag1, _) ->
| Caml_block { fields = ls1; mutable_flag = flag1; tag = tag1; _ } ->
eq_expression_list ls0 ls1 && flag0 = flag1 && eq_expression tag0 tag1
| _ -> false)
| Length _ | Char_of_int _ | Char_to_int _ | Is_null_or_undefined _
Expand Down
42 changes: 28 additions & 14 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,12 @@ let exp_need_paren (e : J.expression) =
| Raw_js_code { code_info = Exp _; _ }
| Fun _
| Caml_block
( _,
_,
_,
( Blk_record _ | Blk_module _ | Blk_poly_var | Blk_extension
| Blk_record_ext _ | Blk_record_inlined _ | Blk_constructor _ ) )
{
tag_info =
( Blk_record _ | Blk_module _ | Blk_poly_var | Blk_extension
| Blk_record_ext _ | Blk_record_inlined _ | Blk_constructor _ );
_;
}
| Object _ ->
true
| Raw_js_code { code_info = Stmt _; _ }
Expand Down Expand Up @@ -760,18 +761,18 @@ and expression_desc cxt ~(level : int) x : cxt =
else
E.runtime_call ~module_name:Js_runtime_modules.option ~fn_name:"some"
[ e ])
| Caml_block (el, _, _, Blk_module fields) ->
| Caml_block { fields = el; tag_info = Blk_module fields; _ } ->
expression_desc cxt ~level
(Object
(List.map_combine fields el (fun x -> Js_op.Lit (Ident.convert x))))
(*name convention of Record is slight different from modules*)
| Caml_block (el, mutable_flag, _, Blk_record fields) ->
| Caml_block { fields = el; mutable_flag; tag_info = Blk_record fields; _ } ->
if block_has_all_int_fields fields then
expression_desc cxt ~level (Array (el, mutable_flag))
else
expression_desc cxt ~level
(Object (List.map_combine_array fields el (fun i -> Js_op.Lit i)))
| Caml_block (el, _, _, Blk_poly_var) -> (
| Caml_block { fields = el; tag_info = Blk_poly_var; _ } -> (
match el with
| [ { expression_desc = Str (_, name); _ }; value ] ->
expression_desc cxt ~level
Expand All @@ -781,9 +782,11 @@ and expression_desc cxt ~(level : int) x : cxt =
(Lit L.polyvar_value, value);
])
| _ -> assert false)
| Caml_block (el, _, _, ((Blk_extension | Blk_record_ext _) as ext)) ->
| Caml_block
{ fields = el; tag_info = (Blk_extension | Blk_record_ext _) as ext; _ }
->
expression cxt ~level (exn_block_as_obj ~stack:false el ext)
| Caml_block (el, _, tag, Blk_record_inlined p) ->
| Caml_block { fields = el; tag; tag_info = Blk_record_inlined p; _ } ->
let objs =
let tails =
List.map_combine_array_append p.fields el
Expand All @@ -804,7 +807,7 @@ and expression_desc cxt ~(level : int) x : cxt =
:: tails
in
expression_desc cxt ~level (Object objs)
| Caml_block (el, _, tag, Blk_constructor p) ->
| Caml_block { fields = el; tag; tag_info = Blk_constructor p; _ } ->
let is_cons = Js_op_util.is_cons p.name in
let objs =
let tails =
Expand Down Expand Up @@ -833,8 +836,14 @@ and expression_desc cxt ~(level : int) x : cxt =
:: tails
in
expression_desc cxt ~level (Object objs)
| Caml_block (_, _, _, (Blk_module_export | Blk_na _)) -> assert false
| Caml_block (el, mutable_flag, _tag, (Blk_tuple | Blk_class | Blk_array)) ->
| Caml_block { tag_info = Blk_module_export | Blk_na _; _ } -> assert false
| Caml_block
{
fields = el;
mutable_flag;
tag_info = Blk_tuple | Blk_class | Blk_array;
_;
} ->
expression_desc cxt ~level (Array (el, mutable_flag))
| Caml_block_tag e ->
group cxt 1 (fun _ ->
Expand Down Expand Up @@ -1214,7 +1223,12 @@ and statement_desc top cxt (s : J.statement_desc) : cxt =
| Throw e ->
let e =
match e.expression_desc with
| Caml_block (el, _, _, ((Blk_extension | Blk_record_ext _) as ext)) ->
| Caml_block
{
fields = el;
tag_info = (Blk_extension | Blk_record_ext _) as ext;
_;
} ->
{
e with
expression_desc =
Expand Down
19 changes: 10 additions & 9 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,16 @@ let dot ?loc ?comment (e0 : t) (e1 : string) : t =
let module_access (e : t) (name : string) (pos : int32) =
let name = Ident.convert name in
match e.expression_desc with
| Caml_block (l, _, _, _) when no_side_effect e -> (
| Caml_block { fields = l; _ } when no_side_effect e -> (
match List.nth_opt l (Int32.to_int pos) with
| Some x -> x
| None -> make_expression (Static_index (e, name, Some pos)))
| _ -> make_expression (Static_index (e, name, Some pos))

let make_block ?loc ?comment (tag : t) (tag_info : J.tag_info) (es : t list)
let make_block ?loc ?comment (tag : t) (tag_info : J.tag_info) (fields : t list)
(mutable_flag : J.mutable_flag) : t =
make_expression ?loc ?comment (Caml_block (es, mutable_flag, tag, tag_info))
make_expression ?loc ?comment
(Caml_block { fields; mutable_flag; tag; tag_info })

(* ATTENTION: this is relevant to how we encode string, boolean *)
let typeof ?loc ?comment (e : t) : t =
Expand Down Expand Up @@ -327,7 +328,7 @@ let array_index ?loc ?comment (e0 : t) (e1 : t) : t =
let array_index_by_int ?loc ?comment (e : t) (pos : int32) : t =
match e.expression_desc with
| Array (l, _) (* Float i -- should not appear here *)
| Caml_block (l, _, _, _)
| Caml_block { fields = l; _ }
when no_side_effect e -> (
match List.nth_opt l (Int32.to_int pos) with
| Some x -> x
Expand All @@ -337,7 +338,7 @@ let array_index_by_int ?loc ?comment (e : t) (pos : int32) : t =
let record_access (e : t) (name : string) (pos : int32) =
match e.expression_desc with
| Array (l, _) (* Float i -- should not appear here *)
| Caml_block (l, _, _, _)
| Caml_block { fields = l; _ }
when no_side_effect e -> (
match List.nth_opt l (Int32.to_int pos) with
| Some x -> x
Expand All @@ -364,20 +365,20 @@ let cons_access (e : t) (pos : int32) =

let poly_var_tag_access (e : t) =
match e.expression_desc with
| Caml_block (l, _, _, _) when no_side_effect e -> (
| Caml_block { fields = l; _ } when no_side_effect e -> (
match l with x :: _ -> x | [] -> assert false)
| _ -> make_expression (Static_index (e, Js_dump_lit.polyvar_hash, Some 0l))

let poly_var_value_access (e : t) =
match e.expression_desc with
| Caml_block (l, _, _, _) when no_side_effect e -> (
| Caml_block { fields = l; _ } when no_side_effect e -> (
match l with _ :: v :: _ -> v | _ -> assert false)
| _ -> make_expression (Static_index (e, Js_dump_lit.polyvar_value, Some 1l))

let extension_access (e : t) ?name (pos : int32) : t =
match e.expression_desc with
| Array (l, _) (* Float i -- should not appear here *)
| Caml_block (l, _, _, _)
| Caml_block { fields = l; _ }
when no_side_effect e -> (
match List.nth_opt l (Int32.to_int pos) with
| Some x -> x
Expand Down Expand Up @@ -461,7 +462,7 @@ let extension_assign (e : t) (pos : int32) name (value : t) =
let array_length ?loc ?comment (e : t) : t =
match e.expression_desc with
(* TODO: use array instead? *)
| (Array (l, _) | Caml_block (l, _, _, _)) when no_side_effect e ->
| (Array (l, _) | Caml_block { fields = l; _ }) when no_side_effect e ->
int ?comment (Int32.of_int (List.length l))
| _ -> make_expression ?loc ?comment (Length (e, Array))

Expand Down
9 changes: 7 additions & 2 deletions jscomp/core/js_of_lam_variant.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ let eval (arg : J.expression) (dispatches : (string * string) list) : E.t =
let eval_as_event (arg : J.expression)
(dispatches : (string * string) list option) =
match arg.expression_desc with
| Caml_block ([ { expression_desc = Str (_, s); _ }; cb ], _, _, Blk_poly_var)
| Caml_block
{
fields = [ { expression_desc = Str (_, s); _ }; cb ];
tag_info = Blk_poly_var;
_;
}
when Js_analyzer.no_side_effect_expression cb ->
let v =
match dispatches with
Expand Down Expand Up @@ -124,5 +129,5 @@ let eval_as_int (arg : J.expression) (dispatches : (string * int) list) : E.t =

let eval_as_unwrap (arg : J.expression) : E.t =
match arg.expression_desc with
| Caml_block ([ { expression_desc = Number _; _ }; cb ], _, _, _) -> cb
| Caml_block { fields = [ { expression_desc = Number _; _ }; cb ]; _ } -> cb
| _ -> E.poly_var_value_access arg
6 changes: 1 addition & 5 deletions jscomp/core/js_pass_flatten.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ let flatten_map =
(List.rev_map
~f:(fun x -> self.statement self x)
(Js_analyzer.rev_flatten_seq v))
| Exp
{
expression_desc = Caml_block (args, _mutable_flag, _tag, _tag_info);
_;
} ->
| Exp { expression_desc = Caml_block { fields = args; _ }; _ } ->
S.block
(List.map ~f:(fun arg -> self.statement self (S.exp arg)) args)
| Exp { expression_desc = Cond (a, b, c); comment; _ } ->
Expand Down
23 changes: 19 additions & 4 deletions jscomp/core/js_pass_flatten_and_mark_dead.ml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ let subst_map (substitution : J.expression Ident.Hash.t) =
({
expression_desc =
Caml_block
((_ :: _ :: _ as ls), Immutable, tag, tag_info);
{
fields = _ :: _ :: _ as ls;
mutable_flag = Immutable;
tag;
tag_info;
};
_;
} as block);
_;
Expand Down Expand Up @@ -232,7 +237,13 @@ let subst_map (substitution : J.expression Ident.Hash.t) =
{
block with
expression_desc =
Caml_block (List.rev e, Immutable, tag, tag_info);
Caml_block
{
fields = List.rev e;
mutable_flag = Immutable;
tag;
tag_info;
};
}
in
let () = add_substitue substitution ident e in
Expand Down Expand Up @@ -262,8 +273,12 @@ let subst_map (substitution : J.expression Ident.Hash.t) =
{ expression_desc = Number (Int { i; _ }); _ } )
| Static_index ({ expression_desc = Var (Id id); _ }, _, Some i) -> (
match Ident.Hash.find_opt substitution id with
| Some { expression_desc = Caml_block (ls, Immutable, _, _); _ }
-> (
| Some
{
expression_desc =
Caml_block { fields = ls; mutable_flag = Immutable; _ };
_;
} -> (
(* user program can be wrong, we should not
turn a runtime crash into compile time crash : )
*)
Expand Down

0 comments on commit e617acb

Please sign in to comment.