Skip to content

Commit 33cdd63

Browse files
committed
continue clean up
1 parent f777673 commit 33cdd63

26 files changed

+563
-600
lines changed

jscomp/core/lam_dispatch_primitive.ml

+135-147
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,52 @@ let translate loc (prim_name : string)
5454
| None ->
5555
if prim_name.[0] = '?' then
5656
(String.sub prim_name 1 (String.length prim_name - 1))
57-
else prim_name
57+
else
58+
if Ext_string.starts_with prim_name "caml_" then
59+
String.sub prim_name 5 (String.length prim_name - 5)
60+
else assert false
61+
(* prim_name *)
5862
| Some x -> x in
5963
E.runtime_call m name args in
6064
begin match prim_name with
61-
| "?int_of_float"
62-
->
65+
| "caml_notequal" ->
6366
begin match args with
64-
| [e] -> E.to_int32 e
65-
| _ -> assert false
67+
| [a1;b1] when
68+
E.for_sure_js_null_undefined a1
69+
|| E.for_sure_js_null_undefined b1
70+
->
71+
E.neq_null_undefined_boolean a1 b1
72+
(* FIXME address_equal *)
73+
| _ ->
74+
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison ;
75+
call Js_runtime_modules.obj_runtime
76+
end
77+
| "caml_equal" ->
78+
begin match args with
79+
| [a1;b1] when
80+
E.for_sure_js_null_undefined a1 || E.for_sure_js_null_undefined b1
81+
->
82+
E.eq_null_undefined_boolean a1 b1
83+
(* FIXME address_equal *)
84+
| _ ->
85+
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison ;
86+
call Js_runtime_modules.obj_runtime
6687
end
88+
89+
| "caml_min"
90+
| "caml_max"
91+
| "caml_compare"
92+
| "caml_greaterequal"
93+
| "caml_greaterthan"
94+
| "caml_lessequal"
95+
| "caml_lessthan"
96+
97+
| "caml_equal_null"
98+
| "caml_equal_undefined"
99+
| "caml_equal_nullable"
100+
->
101+
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison ;
102+
call Js_runtime_modules.obj_runtime
67103
(* generated by the compiler, not user facing *)
68104
| "caml_bytes_greaterthan"
69105
| "caml_bytes_greaterequal"
@@ -72,50 +108,7 @@ let translate loc (prim_name : string)
72108
| "caml_bytes_compare"
73109
| "caml_bytes_equal"
74110
->
75-
call Js_runtime_modules.bytes ~name:prim_name
76-
| "?int64_succ" ->
77-
E.runtime_call Js_runtime_modules.int64 "succ" args
78-
| "?int64_to_string" ->
79-
E.runtime_call Js_runtime_modules.int64 "to_string" args
80-
| "caml_int64_equal_null"
81-
-> Js_long.equal_null args
82-
| "caml_int64_equal_undefined"
83-
-> Js_long.equal_undefined args
84-
| "caml_int64_equal_nullable"
85-
-> Js_long.equal_nullable args
86-
87-
| "?int64_to_float"
88-
-> Js_long.to_float args
89-
| "?int64_of_float"
90-
-> Js_long.of_float args
91-
| "caml_int64_compare"
92-
-> Js_long.compare args
93-
| "?int64_bits_of_float"
94-
-> Js_long.bits_of_float args
95-
| "?int64_float_of_bits"
96-
-> Js_long.float_of_bits args
97-
| "caml_int64_min"
98-
-> Js_long.min args
99-
| "caml_int64_max"
100-
-> Js_long.max args
101-
| "?int_float_of_bits"
102-
| "?int_bits_of_float"
103-
104-
| "?modf_float"
105-
| "?ldexp_float"
106-
| "?frexp_float"
107-
| "?copysign_float"
108-
| "?expm1_float"
109-
| "?hypot_float"
110-
->
111-
call Js_runtime_modules.float
112-
| "?fmod_float"
113-
(* float module like js number module *)
114-
->
115-
begin match args with
116-
| [e0;e1] -> E.float_mod e0 e1
117-
| _ -> assert false
118-
end
111+
call Js_runtime_modules.bytes
119112

120113
| "caml_string_equal"
121114
->
@@ -154,20 +147,19 @@ let translate loc (prim_name : string)
154147
E.string_comp Ge e0 e1
155148
| _ -> assert false
156149
end
157-
| "?string_repeat"
158-
->
159-
begin match args with
160-
| [ n ; {expression_desc = Number (Int {i})} ] ->
161-
let str = (String.make 1 (Char.chr (Int32.to_int i))) in
162-
begin match n.expression_desc with
163-
| Number (Int {i = 1l}) -> E.str str
164-
| _ ->
165-
E.call (E.dot (E.str str) "repeat") [n]
166-
~info:Js_call_info.builtin_runtime_call
167-
end
168-
| _ ->
169-
E.runtime_call Js_runtime_modules.string "make" args
170-
end
150+
151+
| "caml_int64_equal_null"
152+
-> Js_long.equal_null args
153+
| "caml_int64_equal_undefined"
154+
-> Js_long.equal_undefined args
155+
| "caml_int64_equal_nullable"
156+
-> Js_long.equal_nullable args
157+
| "caml_int64_min"
158+
-> Js_long.min args
159+
| "caml_int64_max"
160+
-> Js_long.max args
161+
| "caml_int64_compare"
162+
-> Js_long.compare args
171163
| "caml_string_greaterthan"
172164
->
173165
begin match args with
@@ -253,68 +245,41 @@ let translate loc (prim_name : string)
253245
-> E.string_comp EqEqEq e0 e1
254246
| _ -> assert false
255247
end
256-
| "?create_bytes"
257-
->
258-
(* Bytes.create *)
259-
(* Note that for invalid range, JS raise an Exception RangeError,
260-
here in OCaml it's [Invalid_argument], we have to preserve this semantics.
261-
Also, it's creating a [bytes] which is a js array actually.
262-
*)
263-
begin match args with
264-
| [{expression_desc = Number (Int {i; _}); _}]
265-
when i < 8l
266-
->
267-
(*Invariants: assuming bytes are [int array]*)
268-
E.array NA
269-
(if i = 0l then []
270-
else
271-
Ext_list.init
272-
(Int32.to_int i)
273-
(fun _ -> E.zero_int_literal)
274-
)
275-
| _ ->
276-
E.runtime_call Js_runtime_modules.bytes
277-
"create" args
278-
end
279248
| "caml_bool_compare" ->
280249
begin match args with
281250
| [{expression_desc = Bool a} ; {expression_desc = Bool b} ]
282251
->
283252
let c = compare (a : bool) b in
284253
E.int (if c = 0 then 0l else if c > 0 then 1l else -1l)
285254
| _ ->
286-
call Js_runtime_modules.caml_primitive ~name:"bool_compare"
255+
call Js_runtime_modules.caml_primitive
287256
end
288257
| "caml_int_compare"
289258
-> E.runtime_call Js_runtime_modules.caml_primitive
290259
"int_compare" args
291260
| "caml_float_compare" ->
292-
call Js_runtime_modules.caml_primitive ~name:"float_compare"
261+
call Js_runtime_modules.caml_primitive
293262
| "caml_string_compare"
294263
->
295-
call Js_runtime_modules.caml_primitive ~name:"string_compare"
264+
call Js_runtime_modules.caml_primitive
296265
| "caml_bool_min"
297266
| "caml_int_min"
298267
| "caml_float_min"
299268
| "caml_string_min"
300-
301-
302-
303269
->
304270
begin match args with
305271
| [a;b] ->
306272
if Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b then
307273
E.econd (E.js_comp Clt a b) a b
308274
else
309-
call Js_runtime_modules.caml_primitive ~name:prim_name
275+
call Js_runtime_modules.caml_primitive
310276
| _ -> assert false
311277
end
312278
| "caml_bool_max"
313279
| "caml_int_max"
314280
| "caml_float_max"
315281
| "caml_string_max"
316282

317-
318283
->
319284
begin match args with
320285
| [a;b] ->
@@ -325,6 +290,82 @@ let translate loc (prim_name : string)
325290
| _ -> assert false
326291
end
327292

293+
294+
(* customized primitives *)
295+
| "?int_of_float"
296+
->
297+
begin match args with
298+
| [e] -> E.to_int32 e
299+
| _ -> assert false
300+
end
301+
| "?int64_succ" ->
302+
E.runtime_call Js_runtime_modules.int64 "succ" args
303+
| "?int64_to_string" ->
304+
E.runtime_call Js_runtime_modules.int64 "to_string" args
305+
| "?int64_to_float"
306+
-> Js_long.to_float args
307+
| "?int64_of_float"
308+
-> Js_long.of_float args
309+
| "?int64_bits_of_float"
310+
-> Js_long.bits_of_float args
311+
| "?int64_float_of_bits"
312+
-> Js_long.float_of_bits args
313+
| "?int_float_of_bits"
314+
| "?int_bits_of_float"
315+
316+
| "?modf_float"
317+
| "?ldexp_float"
318+
| "?frexp_float"
319+
| "?copysign_float"
320+
| "?expm1_float"
321+
| "?hypot_float"
322+
->
323+
call Js_runtime_modules.float
324+
| "?fmod_float"
325+
(* float module like js number module *)
326+
->
327+
begin match args with
328+
| [e0;e1] -> E.float_mod e0 e1
329+
| _ -> assert false
330+
end
331+
332+
| "?string_repeat"
333+
->
334+
begin match args with
335+
| [ n ; {expression_desc = Number (Int {i})} ] ->
336+
let str = (String.make 1 (Char.chr (Int32.to_int i))) in
337+
begin match n.expression_desc with
338+
| Number (Int {i = 1l}) -> E.str str
339+
| _ ->
340+
E.call (E.dot (E.str str) "repeat") [n]
341+
~info:Js_call_info.builtin_runtime_call
342+
end
343+
| _ ->
344+
E.runtime_call Js_runtime_modules.string "make" args
345+
end
346+
| "?create_bytes"
347+
->
348+
(* Bytes.create *)
349+
(* Note that for invalid range, JS raise an Exception RangeError,
350+
here in OCaml it's [Invalid_argument], we have to preserve this semantics.
351+
Also, it's creating a [bytes] which is a js array actually.
352+
*)
353+
begin match args with
354+
| [{expression_desc = Number (Int {i; _}); _}]
355+
when i < 8l
356+
->
357+
(*Invariants: assuming bytes are [int array]*)
358+
E.array NA
359+
(if i = 0l then []
360+
else
361+
Ext_list.init
362+
(Int32.to_int i)
363+
(fun _ -> E.zero_int_literal)
364+
)
365+
| _ ->
366+
E.runtime_call Js_runtime_modules.bytes
367+
"create" args
368+
end
328369
(** Note we captured [exception/extension] creation in the early pass, this primitive is
329370
like normal one to set the identifier *)
330371
| "?exn_slot_name"
@@ -374,22 +415,6 @@ let translate loc (prim_name : string)
374415
| "?make_vect"
375416
-> E.runtime_call Js_runtime_modules.array "make" args
376417

377-
| "caml_array_dup" ->
378-
begin match args with
379-
| [a]
380-
->
381-
begin match a.expression_desc with
382-
| Array _
383-
| Caml_block _ -> a
384-
(* here we created a temporary block
385-
and copied it
386-
and discarded it immediately
387-
This could be canceled
388-
*)
389-
| _ -> E.runtime_call Js_runtime_modules.array "dup" args
390-
end
391-
| _ -> assert false
392-
end
393418

394419
| "?format_float"
395420
| "?hexstring_of_float"
@@ -404,44 +429,7 @@ let translate loc (prim_name : string)
404429
->
405430
call Js_runtime_modules.obj_runtime
406431

407-
| "caml_notequal" ->
408-
begin match args with
409-
| [a1;b1] when
410-
E.for_sure_js_null_undefined a1
411-
|| E.for_sure_js_null_undefined b1
412-
->
413-
E.neq_null_undefined_boolean a1 b1
414-
(* FIXME address_equal *)
415-
| _ ->
416-
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison ;
417-
call Js_runtime_modules.obj_runtime ~name:"notequal"
418-
end
419-
| "caml_equal" ->
420-
begin match args with
421-
| [a1;b1] when
422-
E.for_sure_js_null_undefined a1 || E.for_sure_js_null_undefined b1
423-
->
424-
E.eq_null_undefined_boolean a1 b1
425-
(* FIXME address_equal *)
426-
| _ ->
427-
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison ;
428-
call Js_runtime_modules.obj_runtime ~name:"equal"
429-
end
430-
431-
| "caml_min"
432-
| "caml_max"
433-
| "caml_compare"
434-
| "caml_greaterequal"
435-
| "caml_greaterthan"
436-
| "caml_lessequal"
437-
| "caml_lessthan"
438432

439-
| "caml_equal_null"
440-
| "caml_equal_undefined"
441-
| "caml_equal_nullable"
442-
->
443-
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison ;
444-
call Js_runtime_modules.obj_runtime ~name:(String.sub prim_name 5 (String.length prim_name - 5))
445433
| "?obj_tag" ->
446434
(* Note that in ocaml, [int] has tag [1000] and [string] has tag [252]
447435
also now we need do nullary check

jscomp/runtime/caml.ml

+8-8
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,22 @@ let string_compare (s1 : string) (s2 : string) : int =
5151
type 'a selector = 'a -> 'a -> 'a
5252

5353
(* could be replaced by [Math.min], but it seems those built-ins are slower *)
54-
let caml_bool_min (x : bool) y : bool =
54+
let bool_min (x : bool) y : bool =
5555
if x then y else x
56-
let caml_int_min (x : int) (y : int) : int =
56+
let int_min (x : int) (y : int) : int =
5757
if x < y then x else y
58-
let caml_float_min (x : float) y =
58+
let float_min (x : float) y =
5959
if x < y then x else y
60-
let caml_string_min (x : string) y =
60+
let string_min (x : string) y =
6161
if x < y then x else y
6262

63-
let caml_bool_max (x : bool) y : bool =
63+
let bool_max (x : bool) y : bool =
6464
if x then x else y
65-
let caml_int_max (x : int) (y : int) : int =
65+
let int_max (x : int) (y : int) : int =
6666
if x > y then x else y
67-
let caml_float_max (x : float) y =
67+
let float_max (x : float) y =
6868
if x > y then x else y
69-
let caml_string_max (x : string) y =
69+
let string_max (x : string) y =
7070
if x > y then x else y
7171
type i64 = Caml_int64_extern.t
7272
let i64_eq ( x : i64) (y : i64) =

0 commit comments

Comments
 (0)