Skip to content

Commit 23a3ca9

Browse files
committed
migrate caml_{type}_compare/min/max primitives
1 parent 4ee4603 commit 23a3ca9

File tree

131 files changed

+1290
-1348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+1290
-1348
lines changed

jscomp/core/j.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ and vident = Id of ident | Qualified of module_id * string option
6666
pattern match we can ignore the first one for simplicity
6767
for example
6868
{[
69-
Qualified (_, Runtime, Some "caml_int_compare")
69+
Qualified (_, Runtime, Some "compare")
7070
]}
7171
*)
7272

jscomp/core/js_exp_make.ml

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -936,37 +936,13 @@ let rec int_comp (cmp : Lam_compat.comparison) ?comment (e0 : t) (e1 : t) =
936936
Call
937937
( {
938938
expression_desc =
939-
Var (Qualified ({ kind = Runtime }, Some "int_compare"));
939+
Var (Qualified ({ kind = Runtime }, Some "compare"));
940940
_;
941941
},
942942
[ l; r ],
943943
_ ),
944944
Number (Int { i = 0l }) ) ->
945945
int_comp cmp l r (* = 0 > 0 < 0 *)
946-
| ( Ceq,
947-
Call
948-
( ({
949-
expression_desc =
950-
Var
951-
(Qualified
952-
(({ id = _; kind = Runtime } as iid), Some "compare"));
953-
_;
954-
} as fn),
955-
([ _; _ ] as args),
956-
call_info ),
957-
Number (Int { i = 0l }) ) ->
958-
(* This is now generalized for runtime modules
959-
`RuntimeModule.compare x y = 0 ` -->
960-
`RuntimeModule.equal x y`
961-
*)
962-
{
963-
e0 with
964-
expression_desc =
965-
Call
966-
( { fn with expression_desc = Var (Qualified (iid, Some "equal")) },
967-
args,
968-
call_info );
969-
}
970946
| Ceq, Optional_block _, Undefined _ | Ceq, Undefined _, Optional_block _ ->
971947
false_
972948
| Ceq, _, _ -> int_equal e0 e1

jscomp/core/lam_analysis.ml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,25 @@ let rec no_side_effects (lam : Lam.t) : bool =
6464
| Pfield _ | Pval_from_option | Pval_from_option_not_nest
6565
(* NOP The compiler already [t option] is the same as t *)
6666
| Pduprecord
67-
(* Boolean operations *)
67+
(* bool primitives *)
6868
| Psequand | Psequor | Pnot
69-
(* Integer operations *)
69+
| Pboolorder | Pboolmin | Pboolmax
70+
(* int primitives *)
7071
| Pnegint | Paddint | Psubint | Pmulint | Pandint | Porint | Pxorint
7172
| Plslint | Plsrint | Pasrint | Pintcomp _
72-
(* Float operations *)
73+
| Pintorder | Pintmin | Pintmax
74+
(* float primitives *)
7375
| Pintoffloat | Pfloatofint | Pnegfloat
74-
(* | Pabsfloat *)
75-
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat | Pfloatcomp _ | Pjscomp _
76+
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat
77+
| Pfloatcomp _ | Pjscomp _ | Pfloatorder | Pfloatmin | Pfloatmax
78+
(* bigint primitives *)
7679
| Pnegbigint | Paddbigint | Psubbigint | Pmulbigint | Ppowbigint
7780
| Pandbigint | Porbigint | Pxorbigint | Plslbigint | Pasrbigint
78-
| Pbigintcomp _
79-
(* String operations *)
81+
| Pbigintcomp _ | Pbigintorder | Pbigintmin | Pbigintmax
82+
(* string primitives *)
8083
| Pstringlength | Pstringrefu | Pstringrefs
84+
| Pstringorder | Pstringmin | Pstringmax
85+
(* array primitives *)
8186
| Pmakearray | Parraylength | Parrayrefu | Parrayrefs
8287
(* Test if the argument is a block or an immediate integer *)
8388
| Pisint | Pis_poly_var_block

jscomp/core/lam_compile_primitive.ml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,103 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
304304
match args with
305305
| [ e; e1 ] -> Js_of_lam_string.ref_string e e1
306306
| _ -> assert false)
307+
| Pboolorder -> (
308+
match args with
309+
| [ { expression_desc = Bool a }; { expression_desc = Bool b } ] ->
310+
let c = compare (a : bool) b in
311+
E.int (if c = 0 then 0l else if c > 0 then 1l else -1l)
312+
| [ a; b ] -> E.runtime_call Js_runtime_modules.bool "compare" args
313+
| _ -> assert false)
314+
| Pboolmin -> (
315+
match args with
316+
| [ { expression_desc = Bool _ } as a; { expression_desc = Bool _ } as b ] ->
317+
if
318+
Js_analyzer.is_okay_to_duplicate a
319+
&& Js_analyzer.is_okay_to_duplicate b
320+
then E.econd (E.js_comp Clt a b) a b
321+
else E.runtime_call Js_runtime_modules.bool "min" args
322+
| [ a; b ] -> E.runtime_call Js_runtime_modules.bool "min" args
323+
| _ -> assert false)
324+
| Pboolmax -> (
325+
match args with
326+
| [ { expression_desc = Bool _ } as a; { expression_desc = Bool _ } as b ]
327+
when Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b ->
328+
E.econd (E.js_comp Cgt a b) a b
329+
| [ a; b ] -> E.runtime_call Js_runtime_modules.bool "max" args
330+
| _ -> assert false)
331+
| Pintorder -> (
332+
match args with
333+
| [ a; b ] -> E.runtime_call Js_runtime_modules.int "compare" args
334+
| _ -> assert false)
335+
| Pintmin -> (
336+
match args with
337+
| [ { expression_desc = Number (Int _) } as a; { expression_desc = Number (Int _) } as b ]
338+
when Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b ->
339+
E.econd (E.js_comp Clt a b) a b
340+
| [ a; b ] -> E.runtime_call Js_runtime_modules.int "min" args
341+
| _ -> assert false)
342+
| Pintmax -> (
343+
match args with
344+
| [ { expression_desc = Number (Int _) } as a; { expression_desc = Number (Int _) } as b ]
345+
when Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b ->
346+
E.econd (E.js_comp Cgt a b) a b
347+
| [ a; b ] -> E.runtime_call Js_runtime_modules.int "max" args
348+
| _ -> assert false)
349+
| Pfloatorder -> (
350+
match args with
351+
| [ a; b ] as args ->
352+
E.runtime_call Js_runtime_modules.float "compare" args
353+
| _ -> assert false)
354+
| Pfloatmin -> (
355+
match args with
356+
| [ { expression_desc = Number (Float _) } as a; { expression_desc = Number (Float _) } as b ]
357+
when Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b ->
358+
E.econd (E.js_comp Clt a b) a b
359+
| [ a; b ] -> E.runtime_call Js_runtime_modules.float "min" args
360+
| _ -> assert false)
361+
| Pfloatmax -> (
362+
match args with
363+
| [ { expression_desc = Number (Float _) } as a; { expression_desc = Number (Float _) } as b ]
364+
when Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b ->
365+
E.econd (E.js_comp Cgt a b) a b
366+
| [ a; b ] -> E.runtime_call Js_runtime_modules.float "max" args
367+
| _ -> assert false)
368+
| Pbigintorder -> (
369+
match args with
370+
| [ a; b ] -> E.runtime_call Js_runtime_modules.bigint "compare" args
371+
| _ -> assert false)
372+
| Pbigintmin -> (
373+
match args with
374+
| [ { expression_desc = Number (BigInt _) } as a; { expression_desc = Number (BigInt _) } as b ]
375+
when Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b ->
376+
E.econd (E.js_comp Clt a b) a b
377+
| [ a; b ] -> E.runtime_call Js_runtime_modules.bigint "min" args
378+
| _ -> assert false)
379+
| Pbigintmax -> (
380+
match args with
381+
| [ { expression_desc = Number (Float _) } as a; { expression_desc = Number (Float _) } as b ]
382+
when Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b ->
383+
E.econd (E.js_comp Cgt a b) a b
384+
| [ a; b ] -> E.runtime_call Js_runtime_modules.bigint "max" args
385+
| _ -> assert false)
386+
| Pstringorder -> (
387+
match args with
388+
| [ a; b ] -> E.runtime_call Js_runtime_modules.string "compare" args
389+
| _ -> assert false)
390+
| Pstringmin -> (
391+
match args with
392+
| [ { expression_desc = Str _ } as a; { expression_desc = Str _ } as b ]
393+
when Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b ->
394+
E.econd (E.js_comp Clt a b) a b
395+
| [a; b] -> E.runtime_call Js_runtime_modules.string "min" args
396+
| _ -> assert false)
397+
| Pstringmax -> (
398+
match args with
399+
| [ { expression_desc = Str _ } as a; { expression_desc = Str _ } as b ]
400+
when Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b ->
401+
E.econd (E.js_comp Cgt a b) a b
402+
| [a; b] -> E.runtime_call Js_runtime_modules.string "max" args
403+
| _ -> assert false)
307404
(* only when Lapply -> expand = true*)
308405
| Praise -> assert false (* handled before here *)
309406
(* Runtime encoding relevant *)

jscomp/core/lam_convert.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
221221
| Psequand -> prim ~primitive:Psequand ~args loc
222222
| Psequor -> prim ~primitive:Psequor ~args loc
223223
| Pnot -> prim ~primitive:Pnot ~args loc
224+
| Pboolorder -> prim ~primitive:Pboolorder ~args loc
225+
| Pboolmin -> prim ~primitive:Pboolmin ~args loc
226+
| Pboolmax -> prim ~primitive:Pboolmax ~args loc
224227
| Pnegint -> prim ~primitive:Pnegint ~args loc
225228
| Paddint -> prim ~primitive:Paddint ~args loc
226229
| Psubint -> prim ~primitive:Psubint ~args loc
@@ -233,8 +236,14 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
233236
| Plslint -> prim ~primitive:Plslint ~args loc
234237
| Plsrint -> prim ~primitive:Plsrint ~args loc
235238
| Pasrint -> prim ~primitive:Pasrint ~args loc
239+
| Pintorder -> prim ~primitive:Pintorder ~args loc
240+
| Pintmin -> prim ~primitive:Pintmin ~args loc
241+
| Pintmax -> prim ~primitive:Pintmax ~args loc
236242
| Pstringlength -> prim ~primitive:Pstringlength ~args loc
237243
| Pstringrefu -> prim ~primitive:Pstringrefu ~args loc
244+
| Pstringorder -> prim ~primitive:Pstringorder ~args loc
245+
| Pstringmin -> prim ~primitive:Pstringmin ~args loc
246+
| Pstringmax -> prim ~primitive:Pstringmax ~args loc
238247
| Pabsfloat -> assert false
239248
| Pstringrefs -> prim ~primitive:Pstringrefs ~args loc
240249
| Pisint -> prim ~primitive:Pisint ~args loc
@@ -250,6 +259,9 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
250259
| Psubfloat -> prim ~primitive:Psubfloat ~args loc
251260
| Pmulfloat -> prim ~primitive:Pmulfloat ~args loc
252261
| Pdivfloat -> prim ~primitive:Pdivfloat ~args loc
262+
| Pfloatorder -> prim ~primitive:Pfloatorder ~args loc
263+
| Pfloatmin -> prim ~primitive:Pfloatmin ~args loc
264+
| Pfloatmax -> prim ~primitive:Pfloatmax ~args loc
253265
| Pnegbigint -> prim ~primitive:Pnegbigint ~args loc
254266
| Paddbigint -> prim ~primitive:Paddbigint ~args loc
255267
| Psubbigint -> prim ~primitive:Psubbigint ~args loc
@@ -263,6 +275,9 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
263275
| Plslbigint -> prim ~primitive:Plslbigint ~args loc
264276
| Pasrbigint -> prim ~primitive:Pasrbigint ~args loc
265277
| Pbigintcomp x -> prim ~primitive:(Pbigintcomp x) ~args loc
278+
| Pbigintorder -> prim ~primitive:Pbigintorder ~args loc
279+
| Pbigintmin -> prim ~primitive:Pbigintorder ~args loc
280+
| Pbigintmax -> prim ~primitive:Pbigintorder ~args loc
266281
| Pintcomp x -> prim ~primitive:(Pintcomp x) ~args loc
267282
| Poffsetint x -> prim ~primitive:(Poffsetint x) ~args loc
268283
| Poffsetref x -> prim ~primitive:(Poffsetref x) ~args loc

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -122,35 +122,6 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
122122
match args with
123123
| [ e0; e1 ] -> E.string_comp EqEqEq e0 e1
124124
| _ -> assert false)
125-
| "caml_bool_compare" -> (
126-
match args with
127-
| [ { expression_desc = Bool a }; { expression_desc = Bool b } ] ->
128-
let c = compare (a : bool) b in
129-
E.int (if c = 0 then 0l else if c > 0 then 1l else -1l)
130-
| _ -> call Js_runtime_modules.caml_primitive)
131-
| "caml_int_compare" ->
132-
E.runtime_call Js_runtime_modules.caml_primitive "int_compare" args
133-
| "caml_float_compare" -> call Js_runtime_modules.caml_primitive
134-
| "caml_bigint_compare" -> call Js_runtime_modules.caml_primitive
135-
| "caml_string_compare" -> call Js_runtime_modules.caml_primitive
136-
| "caml_bool_min" | "caml_int_min" | "caml_float_min" | "caml_bigint_min" | "caml_string_min" -> (
137-
match args with
138-
| [ a; b ] ->
139-
if
140-
Js_analyzer.is_okay_to_duplicate a
141-
&& Js_analyzer.is_okay_to_duplicate b
142-
then E.econd (E.js_comp Clt a b) a b
143-
else call Js_runtime_modules.caml_primitive
144-
| _ -> assert false)
145-
| "caml_bool_max" | "caml_int_max" | "caml_float_max" | "caml_bigint_max" | "caml_string_max" -> (
146-
match args with
147-
| [ a; b ] ->
148-
if
149-
Js_analyzer.is_okay_to_duplicate a
150-
&& Js_analyzer.is_okay_to_duplicate b
151-
then E.econd (E.js_comp Cgt a b) a b
152-
else call Js_runtime_modules.caml_primitive
153-
| _ -> assert false)
154125
(******************************************************************************)
155126
(************************* customized primitives ******************************)
156127
(******************************************************************************)
@@ -180,14 +151,6 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
180151
| "?hash_mix_string" | "?hash_mix_int" | "?hash_final_mix" ->
181152
call Js_runtime_modules.hash_primitive
182153
| "?hash" -> call Js_runtime_modules.hash
183-
| "?bigint_div" -> (
184-
match args with
185-
| [ e1; e2 ] -> E.bigint_div e1 e2 ~checked:false
186-
| _ -> assert false)
187-
| "?bigint_mod" -> (
188-
match args with
189-
| [ e1; e2 ] -> E.bigint_mod e1 e2 ~checked:false
190-
| _ -> assert false)
191154
| "?await" -> (
192155
match args with
193156
| [e] -> {e with expression_desc = Await e}

0 commit comments

Comments
 (0)