@@ -110,13 +110,6 @@ module Generate (Target : Target_sig.S) = struct
110
110
; " caml_erf_float" , (`Pure , [ Float ], Float )
111
111
; " caml_erfc_float" , (`Pure , [ Float ], Float )
112
112
; " caml_float_compare" , (`Pure , [ Float ; Float ], Int )
113
- ; " caml_greaterthan" , (`Mutable , [ Value ; Value ], Int )
114
- ; " caml_greaterequal" , (`Mutable , [ Value ; Value ], Int )
115
- ; " caml_lessthan" , (`Mutable , [ Value ; Value ], Int )
116
- ; " caml_lessequal" , (`Mutable , [ Value ; Value ], Int )
117
- ; " caml_equal" , (`Mutable , [ Value ; Value ], Int )
118
- ; " caml_notequal" , (`Mutable , [ Value ; Value ], Int )
119
- ; " caml_compare" , (`Mutable , [ Value ; Value ], Int )
120
113
];
121
114
h
122
115
@@ -285,6 +278,39 @@ module Generate (Target : Target_sig.S) = struct
285
278
(transl_prim_arg ctx ?typ:tz z )
286
279
| _ -> invalid_arity name l ~expected: 3 )
287
280
281
+ let register_comparison name cmp_int cmp_boxed_int cmp_float =
282
+ register_prim name `Mutable (fun ctx _ l ->
283
+ match l with
284
+ | [ x; y ] -> (
285
+ match get_type ctx x, get_type ctx y with
286
+ | Int _ , Int _ -> cmp_int ctx x y
287
+ | Number (Int32, _ ), Number (Int32, _ ) ->
288
+ let x = transl_prim_arg ctx ~typ: (Number (Int32 , Unboxed )) x in
289
+ let y = transl_prim_arg ctx ~typ: (Number (Int32 , Unboxed )) y in
290
+ int32_bin_op cmp_boxed_int x y
291
+ | Number (Nativeint, _ ), Number (Nativeint, _ ) ->
292
+ let x = transl_prim_arg ctx ~typ: (Number (Nativeint , Unboxed )) x in
293
+ let y = transl_prim_arg ctx ~typ: (Number (Nativeint , Unboxed )) y in
294
+ nativeint_bin_op cmp_boxed_int x y
295
+ | Number (Int64, _ ), Number (Int64, _ ) ->
296
+ let x = transl_prim_arg ctx ~typ: (Number (Int64 , Unboxed )) x in
297
+ let y = transl_prim_arg ctx ~typ: (Number (Int64 , Unboxed )) y in
298
+ int64_bin_op cmp_boxed_int x y
299
+ | Number (Float, _ ), Number (Float, _ ) ->
300
+ let x = transl_prim_arg ctx ~typ: (Number (Float , Unboxed )) x in
301
+ let y = transl_prim_arg ctx ~typ: (Number (Float , Unboxed )) y in
302
+ float_bin_op cmp_float x y
303
+ | _ ->
304
+ let * f =
305
+ register_import
306
+ ~name
307
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
308
+ in
309
+ let * x = transl_prim_arg ctx x in
310
+ let * y = transl_prim_arg ctx y in
311
+ return (W. Call (f, [ x; y ])))
312
+ | _ -> invalid_arity name l ~expected: 2 )
313
+
288
314
let () =
289
315
register_bin_prim
290
316
" caml_floatarray_unsafe_get"
@@ -1087,7 +1113,83 @@ module Generate (Target : Target_sig.S) = struct
1087
1113
~ty: (Int Normalized )
1088
1114
(fun i j -> Arith. ((j < i) - (i < j)));
1089
1115
register_prim " %js_array" `Pure (fun ctx _ l ->
1090
- Memory. allocate ~tag: 0 (expression_list (fun x -> transl_prim_arg ctx x) l))
1116
+ Memory. allocate ~tag: 0 (expression_list (fun x -> transl_prim_arg ctx x) l));
1117
+ register_comparison
1118
+ " caml_greaterthan"
1119
+ (fun ctx x y -> translate_int_comparison ctx (fun y x -> Arith. (x < y)) x y)
1120
+ (Gt S )
1121
+ Gt ;
1122
+ register_comparison
1123
+ " caml_greaterequal"
1124
+ (fun ctx x y -> translate_int_comparison ctx (fun y x -> Arith. (x < = y)) x y)
1125
+ (Ge S )
1126
+ Ge ;
1127
+ register_comparison
1128
+ " caml_lessthan"
1129
+ (fun ctx x y -> translate_int_comparison ctx Arith. ( < ) x y)
1130
+ (Lt S )
1131
+ Lt ;
1132
+ register_comparison
1133
+ " caml_lessequal"
1134
+ (fun ctx x y -> translate_int_comparison ctx Arith. ( < = ) x y)
1135
+ (Le S )
1136
+ Le ;
1137
+ register_comparison
1138
+ " caml_equal"
1139
+ (fun ctx x y -> translate_int_equality ctx ~negate: false x y)
1140
+ Eq
1141
+ Eq ;
1142
+ register_comparison
1143
+ " caml_notequal"
1144
+ (fun ctx x y -> translate_int_equality ctx ~negate: true x y)
1145
+ Ne
1146
+ Ne ;
1147
+ register_prim " caml_compare" `Mutable (fun ctx _ l ->
1148
+ match l with
1149
+ | [ x; y ] -> (
1150
+ match get_type ctx x, get_type ctx y with
1151
+ | Int _ , Int _ ->
1152
+ let x' = transl_prim_arg ctx ~typ: (Int Normalized ) x in
1153
+ let y' = transl_prim_arg ctx ~typ: (Int Normalized ) y in
1154
+ Arith. ((y' < x') - (x' < y'))
1155
+ | Number (Int32 , _), Number (Int32 , _)
1156
+ | Number (Nativeint, _ ), Number (Nativeint, _ ) ->
1157
+ let * f =
1158
+ register_import
1159
+ ~name: " caml_int32_compare"
1160
+ (Fun { W. params = [ I32 ; I32 ]; result = [ I32 ] })
1161
+ in
1162
+ let * x' = transl_prim_arg ctx ~typ: (Number (Int32 , Unboxed )) x in
1163
+ let * y' = transl_prim_arg ctx ~typ: (Number (Int32 , Unboxed )) y in
1164
+ return (W. Call (f, [ x'; y' ]))
1165
+ | Number (Int64, _ ), Number (Int64, _ ) ->
1166
+ let * f =
1167
+ register_import
1168
+ ~name: " caml_int64_compare"
1169
+ (Fun { W. params = [ I64 ; I64 ]; result = [ I32 ] })
1170
+ in
1171
+ let * x' = transl_prim_arg ctx ~typ: (Number (Int64 , Unboxed )) x in
1172
+ let * y' = transl_prim_arg ctx ~typ: (Number (Int64 , Unboxed )) y in
1173
+ return (W. Call (f, [ x'; y' ]))
1174
+ | Number (Float, _ ), Number (Float, _ ) ->
1175
+ let * f =
1176
+ register_import
1177
+ ~name: " caml_float_compare"
1178
+ (Fun { W. params = [ F64 ; F64 ]; result = [ I32 ] })
1179
+ in
1180
+ let * x' = transl_prim_arg ctx ~typ: (Number (Float , Unboxed )) x in
1181
+ let * y' = transl_prim_arg ctx ~typ: (Number (Float , Unboxed )) y in
1182
+ return (W. Call (f, [ x'; y' ]))
1183
+ | _ ->
1184
+ let * f =
1185
+ register_import
1186
+ ~name: " caml_compare"
1187
+ (Fun { W. params = [ Type. value; Type. value ]; result = [ I32 ] })
1188
+ in
1189
+ let * x' = transl_prim_arg ctx x in
1190
+ let * y' = transl_prim_arg ctx y in
1191
+ return (W. Call (f, [ x'; y' ])))
1192
+ | _ -> invalid_arity " caml_compare" l ~expected: 2 )
1091
1193
1092
1194
let unboxed_type ty : W.value_type option =
1093
1195
match ty with
0 commit comments