Skip to content

Commit 9b1fff8

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 5af3b88 commit 9b1fff8

7 files changed

Lines changed: 44 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
@@ -187,20 +187,9 @@ let tag_of_switch_key = function
187187
Some (Variant_runtime.to_matchable_tag tag)
188188

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

205194
let no_effects_const = lazy true
206195
(* let has_effects_const = lazy false *)
@@ -765,9 +754,7 @@ let compile output_prefix =
765754
let sw_blocks_default =
766755
default_action ~saturated:sw_blocks_full sw_failaction
767756
in
768-
let tag_name, block_cases, literal_cases, has_null_undefined_other =
769-
dispatch_info sw_dispatch
770-
in
757+
let tag_name, block_cases, literal_cases = dispatch_info sw_dispatch in
771758
let untagged = block_cases <> [] in
772759
let compile_whole (cxt : Lam_compile_context.t) =
773760
match
@@ -952,7 +939,7 @@ let compile output_prefix =
952939
else
953940
[
954941
S.if_
955-
(E.is_int_tag ~has_null_undefined_other e)
942+
(E.is_int_tag ~literal_tags:literal_cases e)
956943
(compile_cases ~cxt ~switch_exp:e ~block_cases
957944
~default:sw_num_default sw_consts)
958945
~else_:
@@ -1056,7 +1043,7 @@ let compile output_prefix =
10561043
| Variant_runtime.Instance.Class _ -> true
10571044
| Array -> false
10581045
in
1059-
let check_instance i = E.emit_check (IsInstanceOf (i, Expr e)) in
1046+
let check_instance i = E.is_instance_of i e in
10601047
let typeof_is name = E.string_equal (E.typeof e) (E.str name) in
10611048
(* [null], when it did not go to the default before *)
10621049
let is_object =

‎compiler/ml/ast_untagged_variants.ml‎

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

‎compiler/ml/variant_runtime.ml‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,6 @@ type matching_facts = {
135135
payload constructor only receives values that are not literals, and
136136
[S("a")] matches as [A]: constructor identity is not observable at
137137
runtime for such values. *)
138-
has_null: bool;
139-
has_undefined: bool;
140-
has_other_literal: bool;
141138
}
142139
(** Declaration-level facts needed when lowering a constructor match. This is
143140
not an occurrence-specific matching plan: it contains no arms, actions,
@@ -203,24 +200,16 @@ let compute_matching_facts ~tag_name (constructors : constructor_case array) :
203200
matching_facts =
204201
let block_types = ref [] in
205202
let literal_tags = ref [] in
206-
let has_null = ref false in
207-
let has_undefined = ref false in
208-
let has_other_literal = ref false in
209203
Array.iter
210204
(function
211-
| Constant {name; literal} -> (
205+
| Constant {name; literal} ->
212206
(* Without an [@as], a nullary constructor is its own name. *)
213207
let literal =
214208
match literal with
215209
| Some literal -> literal
216210
| None -> String name
217211
in
218-
literal_tags := literal :: !literal_tags;
219-
match literal with
220-
| Null -> has_null := true
221-
| Undefined -> has_undefined := true
222-
| String _ | Int _ | Float _ | BigInt _ | Bool _ ->
223-
has_other_literal := true)
212+
literal_tags := literal :: !literal_tags
224213
| Block (Untagged {block_type}) ->
225214
block_types := block_type :: !block_types
226215
| Block (Tagged _) -> ())
@@ -229,9 +218,6 @@ let compute_matching_facts ~tag_name (constructors : constructor_case array) :
229218
tag_name;
230219
block_types = List.rev !block_types;
231220
literal_tags = List.rev !literal_tags;
232-
has_null = !has_null;
233-
has_undefined = !has_undefined;
234-
has_other_literal = !has_other_literal;
235221
}
236222

237223
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
@@ -101,9 +101,6 @@ type matching_facts = {
101101
tag_name: string option;
102102
block_types: block_type list;
103103
literal_tags: literal_tag list;
104-
has_null: bool;
105-
has_undefined: bool;
106-
has_other_literal: bool;
107104
}
108105

109106
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)