@@ -49,14 +49,30 @@ let transl_extension_constructor env path ext =
49
49
50
50
(* Translation of primitives *)
51
51
52
- (*
53
- type sargs = (Asttypes.arg_label * Parsetree.expression) list
54
-
55
- let translate_unified_application (env : Env.t) (prim : Primitive.description)
56
- (sargs : sargs) : Lambda.primitive option =
57
- (* TODO *)
58
- None
59
- *)
52
+ let translate_unified_ops (prim : Primitive.description ) (env : Env.t )
53
+ (lhs_type : type_expr ) : Lambda.primitive option =
54
+ (* lhs_type is already unified in type-level *)
55
+ let entry = Hashtbl. find_opt Unified_ops. index_by_name prim.prim_name in
56
+ match entry with
57
+ | Some {specialization} -> (
58
+ match specialization with
59
+ | {int }
60
+ when is_base_type env lhs_type Predef. path_int
61
+ || is_base_type env lhs_type Predef. path_char
62
+ || maybe_pointer_type env lhs_type = Immediate ->
63
+ Some int
64
+ | {float = Some float } when is_base_type env lhs_type Predef. path_float ->
65
+ Some float
66
+ | {bigint = Some bigint} when is_base_type env lhs_type Predef. path_bigint
67
+ ->
68
+ Some bigint
69
+ | {string = Some string } when is_base_type env lhs_type Predef. path_string
70
+ ->
71
+ Some string
72
+ | {bool = Some bool } when is_base_type env lhs_type Predef. path_bool ->
73
+ Some bool
74
+ | {int} -> Some int )
75
+ | _ -> None
60
76
61
77
type specialized = {
62
78
objcomp : Lambda .primitive ;
@@ -403,12 +419,21 @@ let specialize_comparison
403
419
raise Not_found if primitive is unknown *)
404
420
405
421
let specialize_primitive p env ty (* ~has_constant_constructor *) =
406
- try
407
- let table = Hashtbl. find comparisons_table p.prim_name in
408
- match is_function_type env ty with
409
- | Some (lhs , _rhs ) -> specialize_comparison table env lhs
410
- | None -> table.objcomp
411
- with Not_found -> find_primitive p.prim_name
422
+ let fn_expr = is_function_type env ty in
423
+ let unified =
424
+ match fn_expr with
425
+ | Some (lhs , _ ) -> translate_unified_ops p env lhs
426
+ | None -> None
427
+ in
428
+ match unified with
429
+ | Some primitive -> primitive
430
+ | None -> (
431
+ try
432
+ let table = Hashtbl. find comparisons_table p.prim_name in
433
+ match fn_expr with
434
+ | Some (lhs , _rhs ) -> specialize_comparison table env lhs
435
+ | None -> table.objcomp
436
+ with Not_found -> find_primitive p.prim_name)
412
437
413
438
(* Eta-expand a primitive *)
414
439
@@ -467,32 +492,44 @@ let transl_primitive loc p env ty =
467
492
468
493
let transl_primitive_application loc prim env ty args =
469
494
let prim_name = prim.prim_name in
470
- try
495
+ let unified =
471
496
match args with
472
- | [arg1; _]
473
- when is_base_type env arg1.exp_type Predef. path_bool
474
- && Hashtbl. mem comparisons_table prim_name ->
475
- (Hashtbl. find comparisons_table prim_name).boolcomp
476
- | _ ->
477
- let has_constant_constructor =
478
- match args with
479
- | [_; {exp_desc = Texp_construct (_, {cstr_tag = Cstr_constant _}, _)}]
480
- | [{exp_desc = Texp_construct (_, {cstr_tag = Cstr_constant _}, _)}; _]
481
- | [_; {exp_desc = Texp_variant (_, None )}]
482
- | [{exp_desc = Texp_variant (_, None )}; _] ->
483
- true
484
- | _ -> false
485
- in
486
- if has_constant_constructor then
487
- match Hashtbl. find_opt comparisons_table prim_name with
488
- | Some table when table.simplify_constant_constructor -> table.intcomp
489
- | Some _ | None -> specialize_primitive prim env ty
490
- (* ~has_constant_constructor*)
491
- else specialize_primitive prim env ty
492
- with Not_found ->
493
- if String. length prim_name > 0 && prim_name.[0 ] = '%' then
494
- raise (Error (loc, Unknown_builtin_primitive prim_name));
495
- Pccall prim
497
+ | [arg1] | [arg1; _] -> translate_unified_ops prim env arg1.exp_type
498
+ | _ -> None
499
+ in
500
+ match unified with
501
+ | Some primitive -> primitive
502
+ | None -> (
503
+ try
504
+ match args with
505
+ | [arg1; _]
506
+ when is_base_type env arg1.exp_type Predef. path_bool
507
+ && Hashtbl. mem comparisons_table prim_name ->
508
+ (Hashtbl. find comparisons_table prim_name).boolcomp
509
+ | _ ->
510
+ let has_constant_constructor =
511
+ match args with
512
+ | [
513
+ _; {exp_desc = Texp_construct (_, {cstr_tag = Cstr_constant _}, _)};
514
+ ]
515
+ | [
516
+ {exp_desc = Texp_construct (_, {cstr_tag = Cstr_constant _}, _)}; _;
517
+ ]
518
+ | [_; {exp_desc = Texp_variant (_, None )}]
519
+ | [{exp_desc = Texp_variant (_, None )}; _] ->
520
+ true
521
+ | _ -> false
522
+ in
523
+ if has_constant_constructor then
524
+ match Hashtbl. find_opt comparisons_table prim_name with
525
+ | Some table when table.simplify_constant_constructor -> table.intcomp
526
+ | Some _ | None -> specialize_primitive prim env ty
527
+ (* ~has_constant_constructor*)
528
+ else specialize_primitive prim env ty
529
+ with Not_found ->
530
+ if String. length prim_name > 0 && prim_name.[0 ] = '%' then
531
+ raise (Error (loc, Unknown_builtin_primitive prim_name));
532
+ Pccall prim)
496
533
497
534
(* To propagate structured constants *)
498
535
0 commit comments