@@ -67,6 +67,7 @@ module Generate (Target : Target_sig.S) = struct
67
67
type repr =
68
68
| Value
69
69
| Float
70
+ | Int
70
71
| Int32
71
72
| Nativeint
72
73
| Int64
@@ -75,24 +76,23 @@ module Generate (Target : Target_sig.S) = struct
75
76
match r with
76
77
| Value -> Type. value
77
78
| Float -> F64
78
- | Int32 -> I32
79
- | Nativeint -> I32
79
+ | Int | Int32 | Nativeint -> I32
80
80
| Int64 -> I64
81
81
82
82
let specialized_primitive_type (_ , params , result ) =
83
83
{ W. params = List. map ~f: repr_type params; result = [ repr_type result ] }
84
84
85
85
let box_value r e =
86
86
match r with
87
- | Value -> e
87
+ | Value | Int -> e
88
88
| Float -> Memory. box_float e
89
89
| Int32 -> Memory. box_int32 e
90
90
| Nativeint -> Memory. box_nativeint e
91
91
| Int64 -> Memory. box_int64 e
92
92
93
93
let unbox_value r e =
94
94
match r with
95
- | Value -> e
95
+ | Value | Int -> e
96
96
| Float -> Memory. unbox_float e
97
97
| Int32 -> Memory. unbox_int32 e
98
98
| Nativeint -> Memory. unbox_nativeint e
@@ -105,9 +105,9 @@ module Generate (Target : Target_sig.S) = struct
105
105
[ " caml_int32_bswap" , (`Pure , [ Int32 ], Int32 )
106
106
; " caml_nativeint_bswap" , (`Pure , [ Nativeint ], Nativeint )
107
107
; " caml_int64_bswap" , (`Pure , [ Int64 ], Int64 )
108
- ; " caml_int32_compare" , (`Pure , [ Int32 ; Int32 ], Value )
109
- ; " caml_nativeint_compare" , (`Pure , [ Nativeint ; Nativeint ], Value )
110
- ; " caml_int64_compare" , (`Pure , [ Int64 ; Int64 ], Value )
108
+ ; " caml_int32_compare" , (`Pure , [ Int32 ; Int32 ], Int )
109
+ ; " caml_nativeint_compare" , (`Pure , [ Nativeint ; Nativeint ], Int )
110
+ ; " caml_int64_compare" , (`Pure , [ Int64 ; Int64 ], Int )
111
111
; " caml_string_get32" , (`Mutator , [ Value ; Value ], Int32 )
112
112
; " caml_string_get64" , (`Mutator , [ Value ; Value ], Int64 )
113
113
; " caml_bytes_get32" , (`Mutator , [ Value ; Value ], Int32 )
@@ -124,7 +124,7 @@ module Generate (Target : Target_sig.S) = struct
124
124
; " caml_ldexp_float" , (`Pure , [ Float ; Value ], Float )
125
125
; " caml_erf_float" , (`Pure , [ Float ], Float )
126
126
; " caml_erfc_float" , (`Pure , [ Float ], Float )
127
- ; " caml_float_compare" , (`Pure , [ Float ; Float ], Value )
127
+ ; " caml_float_compare" , (`Pure , [ Float ; Float ], Int )
128
128
];
129
129
h
130
130
@@ -299,6 +299,38 @@ module Generate (Target : Target_sig.S) = struct
299
299
(transl_prim_arg ctx ?typ:tz z )
300
300
| _ -> invalid_arity name l ~expected: 3 )
301
301
302
+ let register_comparison name cmp_int cmp_boxed_int cmp_float =
303
+ register_prim name `Mutable (fun ctx _ l ->
304
+ match l with
305
+ | [ x; y ] -> (
306
+ let x' = transl_prim_arg ctx x in
307
+ let y' = transl_prim_arg ctx y in
308
+ match get_type ctx x, get_type ctx y with
309
+ | Int _ , Int _ -> cmp_int ctx x y
310
+ | Number Int32 , Number Int32 ->
311
+ let * x' = Memory. unbox_int32 x' in
312
+ let * y' = Memory. unbox_int32 y' in
313
+ return (W. BinOp (I32 cmp_boxed_int, x', y'))
314
+ | Number Nativeint , Number Nativeint ->
315
+ let * x' = Memory. unbox_nativeint x' in
316
+ let * y' = Memory. unbox_nativeint y' in
317
+ return (W. BinOp (I32 cmp_boxed_int, x', y'))
318
+ | Number Int64 , Number Int64 ->
319
+ let * x' = Memory. unbox_int64 x' in
320
+ let * y' = Memory. unbox_int64 y' in
321
+ return (W. BinOp (I64 cmp_boxed_int, x', y'))
322
+ | Number Float , Number Float -> float_comparison cmp_float x' y'
323
+ | _ ->
324
+ let * f =
325
+ register_import
326
+ ~name
327
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
328
+ in
329
+ let * x' = x' in
330
+ let * y' = y' in
331
+ return (W. Call (f, [ x'; y' ])))
332
+ | _ -> invalid_arity name l ~expected: 2 )
333
+
302
334
let () =
303
335
register_bin_prim
304
336
" caml_array_unsafe_get"
@@ -780,7 +812,93 @@ module Generate (Target : Target_sig.S) = struct
780
812
l
781
813
~init: (return [] )
782
814
in
783
- Memory. allocate ~tag: 0 ~deadcode_sentinal: ctx.deadcode_sentinal ~load l)
815
+ Memory. allocate ~tag: 0 ~deadcode_sentinal: ctx.deadcode_sentinal ~load l);
816
+ register_comparison
817
+ " caml_greaterthan"
818
+ (fun ctx x y -> translate_int_comparison ctx (fun y x -> Arith. (x < y)) x y)
819
+ (Gt S )
820
+ Gt ;
821
+ register_comparison
822
+ " caml_greaterequal"
823
+ (fun ctx x y -> translate_int_comparison ctx (fun y x -> Arith. (x < = y)) x y)
824
+ (Ge S )
825
+ Ge ;
826
+ register_comparison
827
+ " caml_lessthan"
828
+ (fun ctx x y -> translate_int_comparison ctx Arith. ( < ) x y)
829
+ (Lt S )
830
+ Lt ;
831
+ register_comparison
832
+ " caml_lessequal"
833
+ (fun ctx x y -> translate_int_comparison ctx Arith. ( < = ) x y)
834
+ (Le S )
835
+ Le ;
836
+ register_comparison
837
+ " caml_equal"
838
+ (fun ctx x y -> translate_int_equality ctx Arith. ( = ) Value. eq x y)
839
+ Eq
840
+ Eq ;
841
+ register_comparison
842
+ " caml_notequal"
843
+ (fun ctx x y -> translate_int_equality ctx Arith. ( <> ) Value. neq x y)
844
+ Ne
845
+ Ne ;
846
+ register_prim " caml_compare" `Mutable (fun ctx _ l ->
847
+ match l with
848
+ | [ x; y ] -> (
849
+ let x' = transl_prim_arg ctx x in
850
+ let y' = transl_prim_arg ctx y in
851
+ match get_type ctx x, get_type ctx y with
852
+ | Int _ , Int _ ->
853
+ Arith. (
854
+ (Value. int_val y' < Value. int_val x')
855
+ - (Value. int_val x' < Value. int_val y'))
856
+ | Number Int32 , Number Int32 ->
857
+ let * f =
858
+ register_import
859
+ ~name: " caml_int32_compare"
860
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
861
+ in
862
+ let * x' = Memory. unbox_int32 x' in
863
+ let * y' = Memory. unbox_int32 y' in
864
+ return (W. Call (f, [ x'; y' ]))
865
+ | Number Nativeint , Number Nativeint ->
866
+ let * f =
867
+ register_import
868
+ ~name: " caml_nativeint_compare"
869
+ (Fun (Type. primitive_type 2 ))
870
+ in
871
+ let * x' = Memory. unbox_nativeint x' in
872
+ let * y' = Memory. unbox_nativeint y' in
873
+ return (W. Call (f, [ x'; y' ]))
874
+ | Number Int64 , Number Int64 ->
875
+ let * f =
876
+ register_import
877
+ ~name: " caml_int64_compare"
878
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
879
+ in
880
+ let * x' = Memory. unbox_int64 x' in
881
+ let * y' = Memory. unbox_int64 y' in
882
+ return (W. Call (f, [ x'; y' ]))
883
+ | Number Float , Number Float ->
884
+ let * f =
885
+ register_import
886
+ ~name: " caml_float_compare"
887
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
888
+ in
889
+ let * x' = Memory. unbox_int64 x' in
890
+ let * y' = Memory. unbox_int64 y' in
891
+ return (W. Call (f, [ x'; y' ]))
892
+ | _ ->
893
+ let * f =
894
+ register_import
895
+ ~name: " caml_compare"
896
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
897
+ in
898
+ let * x' = x' in
899
+ let * y' = y' in
900
+ return (W. Call (f, [ x'; y' ])))
901
+ | _ -> invalid_arity " caml_compare" l ~expected: 2 )
784
902
785
903
let rec translate_expr ctx context x e =
786
904
match e with
0 commit comments