@@ -68,6 +68,7 @@ module Generate (Target : Target_sig.S) = struct
68
68
type repr =
69
69
| Value
70
70
| Float
71
+ | Int
71
72
| Int32
72
73
| Nativeint
73
74
| Int64
@@ -76,24 +77,23 @@ module Generate (Target : Target_sig.S) = struct
76
77
match r with
77
78
| Value -> Type. value
78
79
| Float -> F64
79
- | Int32 -> I32
80
- | Nativeint -> I32
80
+ | Int | Int32 | Nativeint -> I32
81
81
| Int64 -> I64
82
82
83
83
let specialized_primitive_type (_ , params , result ) =
84
84
{ W. params = List. map ~f: repr_type params; result = [ repr_type result ] }
85
85
86
86
let box_value r e =
87
87
match r with
88
- | Value -> e
88
+ | Value | Int -> e
89
89
| Float -> Memory. box_float e
90
90
| Int32 -> Memory. box_int32 e
91
91
| Nativeint -> Memory. box_nativeint e
92
92
| Int64 -> Memory. box_int64 e
93
93
94
94
let unbox_value r e =
95
95
match r with
96
- | Value -> e
96
+ | Value | Int -> e
97
97
| Float -> Memory. unbox_float e
98
98
| Int32 -> Memory. unbox_int32 e
99
99
| Nativeint -> Memory. unbox_nativeint e
@@ -106,9 +106,9 @@ module Generate (Target : Target_sig.S) = struct
106
106
[ " caml_int32_bswap" , (`Pure , [ Int32 ], Int32 )
107
107
; " caml_nativeint_bswap" , (`Pure , [ Nativeint ], Nativeint )
108
108
; " caml_int64_bswap" , (`Pure , [ Int64 ], Int64 )
109
- ; " caml_int32_compare" , (`Pure , [ Int32 ; Int32 ], Value )
110
- ; " caml_nativeint_compare" , (`Pure , [ Nativeint ; Nativeint ], Value )
111
- ; " caml_int64_compare" , (`Pure , [ Int64 ; Int64 ], Value )
109
+ ; " caml_int32_compare" , (`Pure , [ Int32 ; Int32 ], Int )
110
+ ; " caml_nativeint_compare" , (`Pure , [ Nativeint ; Nativeint ], Int )
111
+ ; " caml_int64_compare" , (`Pure , [ Int64 ; Int64 ], Int )
112
112
; " caml_string_get32" , (`Mutator , [ Value ; Value ], Int32 )
113
113
; " caml_string_get32u" , (`Mutator , [ Value ; Value ], Int32 )
114
114
; " caml_string_get64" , (`Mutator , [ Value ; Value ], Int64 )
@@ -135,7 +135,7 @@ module Generate (Target : Target_sig.S) = struct
135
135
; " caml_ldexp_float" , (`Pure , [ Float ; Value ], Float )
136
136
; " caml_erf_float" , (`Pure , [ Float ], Float )
137
137
; " caml_erfc_float" , (`Pure , [ Float ], Float )
138
- ; " caml_float_compare" , (`Pure , [ Float ; Float ], Value )
138
+ ; " caml_float_compare" , (`Pure , [ Float ; Float ], Int )
139
139
];
140
140
h
141
141
@@ -310,6 +310,38 @@ module Generate (Target : Target_sig.S) = struct
310
310
(transl_prim_arg ctx ?typ:tz z )
311
311
| _ -> invalid_arity name l ~expected: 3 )
312
312
313
+ let register_comparison name cmp_int cmp_boxed_int cmp_float =
314
+ register_prim name `Mutable (fun ctx _ (hint : Optimization_hint.t option ) l ->
315
+ match l with
316
+ | [ x; y ] -> (
317
+ let x' = transl_prim_arg ctx x in
318
+ let y' = transl_prim_arg ctx y in
319
+ match hint, get_type ctx x, get_type ctx y with
320
+ | _ , Int _ , Int _ -> cmp_int ctx x y
321
+ | Some (Hint_int Int32 ), _ , _ | _ , Number Int32 , Number Int32 ->
322
+ let * x' = Memory. unbox_int32 x' in
323
+ let * y' = Memory. unbox_int32 y' in
324
+ return (W. BinOp (I32 cmp_boxed_int, x', y'))
325
+ | Some (Hint_int Nativeint ), _ , _ | _ , Number Nativeint , Number Nativeint ->
326
+ let * x' = Memory. unbox_nativeint x' in
327
+ let * y' = Memory. unbox_nativeint y' in
328
+ return (W. BinOp (I32 cmp_boxed_int, x', y'))
329
+ | Some (Hint_int Int64 ), _ , _ | _ , Number Int64 , Number Int64 ->
330
+ let * x' = Memory. unbox_int64 x' in
331
+ let * y' = Memory. unbox_int64 y' in
332
+ return (W. BinOp (I64 cmp_boxed_int, x', y'))
333
+ | _ , Number Float , Number Float -> float_comparison cmp_float x' y'
334
+ | _ ->
335
+ let * f =
336
+ register_import
337
+ ~name
338
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
339
+ in
340
+ let * x' = x' in
341
+ let * y' = y' in
342
+ return (W. Call (f, [ x'; y' ])))
343
+ | _ -> invalid_arity name l ~expected: 2 )
344
+
313
345
let () =
314
346
register_bin_prim
315
347
" caml_array_unsafe_get"
@@ -792,6 +824,92 @@ module Generate (Target : Target_sig.S) = struct
792
824
~init: (return [] )
793
825
in
794
826
Memory. allocate ~tag: 0 ~deadcode_sentinal: ctx.deadcode_sentinal ~load l);
827
+ register_comparison
828
+ " caml_greaterthan"
829
+ (fun ctx x y -> translate_int_comparison ctx (fun y x -> Arith. (x < y)) x y)
830
+ (Gt S )
831
+ Gt ;
832
+ register_comparison
833
+ " caml_greaterequal"
834
+ (fun ctx x y -> translate_int_comparison ctx (fun y x -> Arith. (x < = y)) x y)
835
+ (Ge S )
836
+ Ge ;
837
+ register_comparison
838
+ " caml_lessthan"
839
+ (fun ctx x y -> translate_int_comparison ctx Arith. ( < ) x y)
840
+ (Lt S )
841
+ Lt ;
842
+ register_comparison
843
+ " caml_lessequal"
844
+ (fun ctx x y -> translate_int_comparison ctx Arith. ( < = ) x y)
845
+ (Le S )
846
+ Le ;
847
+ register_comparison
848
+ " caml_equal"
849
+ (fun ctx x y -> translate_int_equality ctx ~negate: false x y)
850
+ Eq
851
+ Eq ;
852
+ register_comparison
853
+ " caml_notequal"
854
+ (fun ctx x y -> translate_int_equality ctx ~negate: true x y)
855
+ Ne
856
+ Ne ;
857
+ register_prim " caml_compare" `Mutable (fun ctx _ _ l ->
858
+ match l with
859
+ | [ x; y ] -> (
860
+ let x' = transl_prim_arg ctx x in
861
+ let y' = transl_prim_arg ctx y in
862
+ match get_type ctx x, get_type ctx y with
863
+ | Int _ , Int _ ->
864
+ Arith. (
865
+ (Value. int_val y' < Value. int_val x')
866
+ - (Value. int_val x' < Value. int_val y'))
867
+ | Number Int32 , Number Int32 ->
868
+ let * f =
869
+ register_import
870
+ ~name: " caml_int32_compare"
871
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
872
+ in
873
+ let * x' = Memory. unbox_int32 x' in
874
+ let * y' = Memory. unbox_int32 y' in
875
+ return (W. Call (f, [ x'; y' ]))
876
+ | Number Nativeint , Number Nativeint ->
877
+ let * f =
878
+ register_import
879
+ ~name: " caml_nativeint_compare"
880
+ (Fun (Type. primitive_type 2 ))
881
+ in
882
+ let * x' = Memory. unbox_nativeint x' in
883
+ let * y' = Memory. unbox_nativeint y' in
884
+ return (W. Call (f, [ x'; y' ]))
885
+ | Number Int64 , Number Int64 ->
886
+ let * f =
887
+ register_import
888
+ ~name: " caml_int64_compare"
889
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
890
+ in
891
+ let * x' = Memory. unbox_int64 x' in
892
+ let * y' = Memory. unbox_int64 y' in
893
+ return (W. Call (f, [ x'; y' ]))
894
+ | Number Float , Number Float ->
895
+ let * f =
896
+ register_import
897
+ ~name: " caml_float_compare"
898
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
899
+ in
900
+ let * x' = Memory. unbox_int64 x' in
901
+ let * y' = Memory. unbox_int64 y' in
902
+ return (W. Call (f, [ x'; y' ]))
903
+ | _ ->
904
+ let * f =
905
+ register_import
906
+ ~name: " caml_compare"
907
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
908
+ in
909
+ let * x' = x' in
910
+ let * y' = y' in
911
+ return (W. Call (f, [ x'; y' ])))
912
+ | _ -> invalid_arity " caml_compare" l ~expected: 2 );
795
913
let caml_ba_get ~ctx ~context ~unsafe ~kind ~layout ta indices =
796
914
let ta' = transl_prim_arg ctx ta in
797
915
Bigarray. get
0 commit comments