@@ -54,16 +54,52 @@ let translate loc (prim_name : string)
54
54
| None ->
55
55
if prim_name.[0 ] = '?' then
56
56
(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 *)
58
62
| Some x -> x in
59
63
E. runtime_call m name args in
60
64
begin match prim_name with
61
- | " ?int_of_float"
62
- ->
65
+ | "caml_notequal" ->
63
66
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
66
87
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
67
103
(* generated by the compiler, not user facing *)
68
104
| " caml_bytes_greaterthan"
69
105
| " caml_bytes_greaterequal"
@@ -72,50 +108,7 @@ let translate loc (prim_name : string)
72
108
| " caml_bytes_compare"
73
109
| " caml_bytes_equal"
74
110
->
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
119
112
120
113
| " caml_string_equal"
121
114
->
@@ -154,20 +147,19 @@ let translate loc (prim_name : string)
154
147
E. string_comp Ge e0 e1
155
148
| _ -> assert false
156
149
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
171
163
| " caml_string_greaterthan"
172
164
->
173
165
begin match args with
@@ -253,68 +245,41 @@ let translate loc (prim_name : string)
253
245
-> E. string_comp EqEqEq e0 e1
254
246
| _ -> assert false
255
247
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
279
248
| "caml_bool_compare" ->
280
249
begin match args with
281
250
| [{expression_desc = Bool a} ; {expression_desc = Bool b} ]
282
251
->
283
252
let c = compare (a : bool ) b in
284
253
E. int (if c = 0 then 0l else if c > 0 then 1l else - 1l )
285
254
| _ ->
286
- call Js_runtime_modules. caml_primitive ~name: " bool_compare "
255
+ call Js_runtime_modules. caml_primitive
287
256
end
288
257
| " caml_int_compare"
289
258
-> E. runtime_call Js_runtime_modules. caml_primitive
290
259
" int_compare" args
291
260
| "caml_float_compare" ->
292
- call Js_runtime_modules. caml_primitive ~name: " float_compare "
261
+ call Js_runtime_modules. caml_primitive
293
262
| " caml_string_compare"
294
263
->
295
- call Js_runtime_modules. caml_primitive ~name: " string_compare "
264
+ call Js_runtime_modules. caml_primitive
296
265
| " caml_bool_min"
297
266
| " caml_int_min"
298
267
| " caml_float_min"
299
268
| " caml_string_min"
300
-
301
-
302
-
303
269
->
304
270
begin match args with
305
271
| [a;b] ->
306
272
if Js_analyzer. is_okay_to_duplicate a && Js_analyzer. is_okay_to_duplicate b then
307
273
E. econd (E. js_comp Clt a b) a b
308
274
else
309
- call Js_runtime_modules. caml_primitive ~name: prim_name
275
+ call Js_runtime_modules. caml_primitive
310
276
| _ -> assert false
311
277
end
312
278
| " caml_bool_max"
313
279
| " caml_int_max"
314
280
| " caml_float_max"
315
281
| " caml_string_max"
316
282
317
-
318
283
->
319
284
begin match args with
320
285
| [a;b] ->
@@ -325,6 +290,82 @@ let translate loc (prim_name : string)
325
290
| _ -> assert false
326
291
end
327
292
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
328
369
(* * Note we captured [exception/extension] creation in the early pass, this primitive is
329
370
like normal one to set the identifier *)
330
371
| " ?exn_slot_name"
@@ -374,22 +415,6 @@ let translate loc (prim_name : string)
374
415
| " ?make_vect"
375
416
-> E. runtime_call Js_runtime_modules. array " make" args
376
417
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
393
418
394
419
| " ?format_float"
395
420
| " ?hexstring_of_float"
@@ -404,44 +429,7 @@ let translate loc (prim_name : string)
404
429
->
405
430
call Js_runtime_modules. obj_runtime
406
431
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"
438
432
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 ))
445
433
| "?obj_tag" ->
446
434
(* Note that in ocaml, [int] has tag [1000] and [string] has tag [252]
447
435
also now we need do nullary check
0 commit comments