Skip to content

Commit c68ef9a

Browse files
cknittclaude
andcommitted
Emit untagged checks directly instead of through Dynamic_checks
The check AST in Ast_untagged_variants only encoded the int-tag test of tagged variants and the instance checks. Js_exp_make now builds both directly (is_int_tag from the literal list, is_instance_of), and matching_facts no longer carries has_null, has_undefined and has_other_literal, which that test derived. A class is never a switch key, so block_type_name no longer pretends to emit one. No output changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 9c89641 commit c68ef9a

7 files changed

Lines changed: 43 additions & 118 deletions

File tree

‎compiler/core/js_exp_make.ml‎

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,45 +1395,44 @@ let class_constructor (instance : Variant_runtime.Instance.t) =
13951395
| [] -> assert false
13961396
| global :: properties -> List.fold_left dot (js_global global) properties
13971397

1398-
(* The [typeof] string an untagged payload answers to. *)
1399-
let block_type_name = function
1400-
| Variant_runtime.InstanceType i -> class_constructor i
1401-
| block_type -> (
1402-
match Variant_runtime.typeof_name block_type with
1403-
| Some name -> str name
1404-
| None ->
1405-
(* An unknown payload has no check: it is the only payload case *)
1406-
assert false)
1398+
(* The [typeof] string an untagged payload answers to, as a [switch] key.
1399+
Instances are told apart by [is_instance_of], an unknown payload is not
1400+
checked *)
1401+
let block_type_name block_type =
1402+
match Variant_runtime.typeof_name block_type with
1403+
| Some name -> str name
1404+
| None -> assert false
14071405

14081406
let tag_type = function
14091407
| Variant_runtime.Literal d -> literal_tag d
14101408
| Payload_shape b -> block_type_name b
14111409

1412-
let rec emit_check (check : t Ast_untagged_variants.Dynamic_checks.t) =
1413-
match check with
1414-
| TagType t -> tag_type t
1415-
| BinOp (op, x, y) ->
1416-
let op =
1417-
match op with
1418-
| EqEqEq -> Js_op.EqEqEq
1419-
| NotEqEq -> NotEqEq
1420-
| And -> And
1421-
| Or -> Or
1422-
in
1423-
bin op (emit_check x) (emit_check y)
1424-
| TypeOf x -> typeof (emit_check x)
1425-
| IsInstanceOf (Array, x) -> is_array (emit_check x)
1426-
| IsInstanceOf (instance, x) ->
1427-
instanceof (emit_check x) (class_constructor instance)
1428-
| Not x -> not (emit_check x)
1429-
| Expr x -> x
1430-
1431-
let is_int_tag ?has_null_undefined_other e =
1432-
let check =
1433-
Ast_untagged_variants.Dynamic_checks.is_int_tag ?has_null_undefined_other
1434-
(Expr e)
1410+
let is_instance_of (instance : Variant_runtime.Instance.t) e =
1411+
match instance with
1412+
| Array -> is_array e
1413+
| Class _ -> instanceof e (class_constructor instance)
1414+
1415+
(* Whether a value of a tagged variant is a constant rather than a block,
1416+
given the literals of its constants *)
1417+
let is_int_tag ~(literal_tags : Variant_runtime.literal_tag list) e =
1418+
let has_null = List.mem Variant_runtime.Null literal_tags in
1419+
let has_undefined = List.mem Variant_runtime.Undefined literal_tags in
1420+
let has_other =
1421+
List.exists
1422+
(function
1423+
| Variant_runtime.Null | Undefined -> false
1424+
| String _ | Int _ | Float _ | BigInt _ | Bool _ -> true)
1425+
literal_tags
14351426
in
1436-
emit_check check
1427+
let is_null = bin EqEqEq e nil in
1428+
let is_undefined = bin EqEqEq e undefined in
1429+
let not_an_object = bin NotEqEq (typeof e) (str "object") in
1430+
match (has_null, has_undefined, has_other) with
1431+
| true, false, false -> is_null
1432+
| true, true, false -> bin Or is_null is_undefined
1433+
| false, true, false -> is_undefined
1434+
| true, _, true -> bin Or is_null not_an_object
1435+
| false, _, _ -> not_an_object
14371436

14381437
let tag ?comment ?(name = Js_dump_lit.tag) e : t =
14391438
{expression_desc = Caml_block_tag (e, name); comment; source_loc = None}

‎compiler/core/js_exp_make.mli‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ val assign : ?comment:string -> t -> t -> t
164164
val literal_tag : Variant_runtime.literal_tag -> t
165165
val tag_type : Variant_runtime.tag_type -> t
166166

167-
val emit_check : t Ast_untagged_variants.Dynamic_checks.t -> t
167+
val is_instance_of : Variant_runtime.Instance.t -> t -> t
168+
(** [Array.isArray(e)] or [e instanceof C] *)
168169

169170
val triple_equal : ?comment:string -> t -> t -> t
170171
(* TODO: reduce [triple_equal] use *)
@@ -179,7 +180,7 @@ val eq_null_undefined_boolean : ?comment:string -> t -> t -> t
179180

180181
val neq_null_undefined_boolean : ?comment:string -> t -> t -> t
181182

182-
val is_int_tag : ?has_null_undefined_other:bool * bool * bool -> t -> t
183+
val is_int_tag : literal_tags:Variant_runtime.literal_tag list -> t -> t
183184

184185
val is_type_object : t -> t
185186

‎compiler/core/lam_compile.ml‎

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,9 @@ let tag_of_switch_key : _ -> Variant_runtime.tag_type option = function
186186
Some (Payload_shape block_type)
187187

188188
let dispatch_info = function
189-
| Lambda.Switch_direct -> (Js_dump_lit.tag, [], [], (false, false, false))
190-
| Switch_variant
191-
{
192-
Variant_runtime.tag_name;
193-
block_types;
194-
literal_tags;
195-
has_null;
196-
has_undefined;
197-
has_other_literal;
198-
} ->
199-
( Option.value tag_name ~default:Js_dump_lit.tag,
200-
block_types,
201-
literal_tags,
202-
(has_null, has_undefined, has_other_literal) )
189+
| Lambda.Switch_direct -> (Js_dump_lit.tag, [], [])
190+
| Switch_variant {Variant_runtime.tag_name; block_types; literal_tags} ->
191+
(Option.value tag_name ~default:Js_dump_lit.tag, block_types, literal_tags)
203192

204193
let no_effects_const = lazy true
205194
(* let has_effects_const = lazy false *)
@@ -762,9 +751,7 @@ let compile output_prefix =
762751
let sw_blocks_default =
763752
default_action ~saturated:sw_blocks_full sw_failaction
764753
in
765-
let tag_name, block_cases, literal_cases, has_null_undefined_other =
766-
dispatch_info sw_dispatch
767-
in
754+
let tag_name, block_cases, literal_cases = dispatch_info sw_dispatch in
768755
let untagged = block_cases <> [] in
769756
let compile_whole (cxt : Lam_compile_context.t) =
770757
match
@@ -947,7 +934,7 @@ let compile output_prefix =
947934
else
948935
[
949936
S.if_
950-
(E.is_int_tag ~has_null_undefined_other e)
937+
(E.is_int_tag ~literal_tags:literal_cases e)
951938
(compile_cases ~cxt ~switch_exp:e ~block_cases
952939
~default:sw_num_default sw_consts)
953940
~else_:
@@ -1051,7 +1038,7 @@ let compile output_prefix =
10511038
| Variant_runtime.Instance.Class _ -> true
10521039
| Array -> false
10531040
in
1054-
let check_instance i = E.emit_check (IsInstanceOf (i, Expr e)) in
1041+
let check_instance i = E.is_instance_of i e in
10551042
let typeof_is name = E.string_equal (E.typeof e) (E.str name) in
10561043
(* [null], when it did not go to the default before *)
10571044
let is_object =

‎compiler/ml/ast_untagged_variants.ml‎

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -562,43 +562,3 @@ let check_tag_field_conflicts (cstrs : Types.constructor_declaration list) =
562562
fields
563563
| _ -> ())
564564
cstrs
565-
566-
module Dynamic_checks = struct
567-
type op = EqEqEq | NotEqEq | Or | And
568-
type 'a t =
569-
| BinOp of op * 'a t * 'a t
570-
| TagType of tag_type
571-
| TypeOf of 'a t
572-
| IsInstanceOf of Instance.t * 'a t
573-
| Not of 'a t
574-
| Expr of 'a
575-
576-
let bin op x y = BinOp (op, x, y)
577-
let tag_type t = TagType t
578-
let typeof x = TypeOf x
579-
let nil = Literal Null |> tag_type
580-
let undefined = Literal Undefined |> tag_type
581-
let object_ = Payload_shape ObjectType |> tag_type
582-
583-
let ( == ) x y = bin EqEqEq x y
584-
let ( != ) x y = bin NotEqEq x y
585-
let ( ||| ) x y = bin Or x y
586-
587-
let is_int_tag ?(has_null_undefined_other = (false, false, false)) (e : _ t) :
588-
_ t =
589-
let has_null, has_undefined, has_other = has_null_undefined_other in
590-
if has_null && has_undefined = false && has_other = false then
591-
(* null *)
592-
bin EqEqEq e nil
593-
else if has_null && has_undefined && has_other = false then
594-
(* null + undefined *)
595-
e == nil ||| e == undefined
596-
else if has_null = false && has_undefined && has_other = false then
597-
(* undefined *)
598-
e == undefined
599-
else if has_null then
600-
(* (null + undefined + other) || (null + other) *)
601-
e == nil ||| typeof e != object_
602-
else (* (undefiled + other) || other *)
603-
typeof e != object_
604-
end

‎compiler/ml/variant_runtime.ml‎

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ type matching_facts = {
129129
payload constructor only receives values that are not literals, and
130130
[S("a")] matches as [A]: constructor identity is not observable at
131131
runtime for such values. *)
132-
has_null: bool;
133-
has_undefined: bool;
134-
has_other_literal: bool;
135132
}
136133
(** Declaration-level facts needed when lowering a constructor match. This is
137134
not an occurrence-specific matching plan: it contains no arms, actions,
@@ -197,18 +194,9 @@ let compute_matching_facts ~tag_name (constructors : constructor_case array) :
197194
matching_facts =
198195
let block_types = ref [] in
199196
let literal_tags = ref [] in
200-
let has_null = ref false in
201-
let has_undefined = ref false in
202-
let has_other_literal = ref false in
203197
Array.iter
204198
(function
205-
| Constant {literal} -> (
206-
literal_tags := literal :: !literal_tags;
207-
match literal with
208-
| Null -> has_null := true
209-
| Undefined -> has_undefined := true
210-
| String _ | Int _ | Float _ | BigInt _ | Bool _ ->
211-
has_other_literal := true)
199+
| Constant {literal} -> literal_tags := literal :: !literal_tags
212200
| Block (Untagged {block_type}) ->
213201
block_types := block_type :: !block_types
214202
| Block (Tagged _) -> ())
@@ -217,9 +205,6 @@ let compute_matching_facts ~tag_name (constructors : constructor_case array) :
217205
tag_name;
218206
block_types = List.rev !block_types;
219207
literal_tags = List.rev !literal_tags;
220-
has_null = !has_null;
221-
has_undefined = !has_undefined;
222-
has_other_literal = !has_other_literal;
223208
}
224209

225210
let make_layout ~(configuration : configuration) constructors =

‎compiler/ml/variant_runtime.mli‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ type matching_facts = {
9494
tag_name: string option;
9595
block_types: block_type list;
9696
literal_tags: literal_tag list;
97-
has_null: bool;
98-
has_undefined: bool;
99-
has_other_literal: bool;
10097
}
10198

10299
type layout

‎tests/ounit_tests/ounit_string_literal_tests.ml‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,14 +774,10 @@ let suites =
774774
| _ ->
775775
OUnit.assert_failure
776776
"expected Array.isArray simplification to remain active");
777-
let open Ast_untagged_variants.Dynamic_checks in
778777
let date =
779778
Variant_runtime.Instance.Class {name = "Date"; ancestors = []}
780779
in
781-
assert_js_global ~expected:"Date"
782-
(Js_exp_make.emit_check
783-
(TagType (Variant_runtime.Payload_shape (InstanceType date))));
784-
match Js_exp_make.emit_check (IsInstanceOf (date, Expr value)) with
780+
match Js_exp_make.is_instance_of date value with
785781
| {expression_desc = Bin (InstanceOf, argument, constructor)} ->
786782
OUnit.assert_bool "expected the original argument"
787783
(Js_analyzer.eq_expression value argument);

0 commit comments

Comments
 (0)