Skip to content

Commit 38695ed

Browse files
Distinguish the different kind of integers
Co-authored-by: Olivier Nicole <[email protected]>
1 parent dd2ad41 commit 38695ed

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

compiler/lib/code.ml

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,11 @@ type constant =
363363
| NativeString of Native_string.t
364364
| Float of float
365365
| Float_array of float array
366+
| Int of int32
367+
| Int32 of int32
366368
| Int64 of int64
369+
| NativeInt of nativeint
367370
| Tuple of int * constant array * array_or_not
368-
| Int of int32
369371

370372
let rec constant_equal a b =
371373
match a, b with
@@ -383,26 +385,58 @@ let rec constant_equal a b =
383385
| Some s, Some c -> same := Some (s && c)
384386
done;
385387
!same
388+
| Int a, Int b | Int32 a, Int32 b -> Some (Int32.equal a b)
386389
| Int64 a, Int64 b -> Some (Int64.equal a b)
390+
| NativeInt a, NativeInt b -> Some (Nativeint.equal a b)
387391
| Float_array a, Float_array b -> Some (Array.equal Float.equal a b)
388-
| Int a, Int b -> Some (Int32.equal a b)
389392
| Float a, Float b -> Some (Float.equal a b)
390393
| String _, NativeString _ | NativeString _, String _ -> None
391394
| Int _, Float _ | Float _, Int _ -> None
392395
| Tuple ((0 | 254), _, _), Float_array _ -> None
393396
| Float_array _, Tuple ((0 | 254), _, _) -> None
394-
| Tuple _, (String _ | NativeString _ | Int64 _ | Int _ | Float _ | Float_array _) ->
395-
Some false
396-
| Float_array _, (String _ | NativeString _ | Int64 _ | Int _ | Float _ | Tuple _) ->
397-
Some false
398-
| String _, (Int64 _ | Int _ | Float _ | Tuple _ | Float_array _) -> Some false
399-
| NativeString _, (Int64 _ | Int _ | Float _ | Tuple _ | Float_array _) -> Some false
400-
| Int64 _, (String _ | NativeString _ | Int _ | Float _ | Tuple _ | Float_array _) ->
397+
| ( Tuple _
398+
, ( String _
399+
| NativeString _
400+
| Int64 _
401+
| Int _
402+
| Int32 _
403+
| NativeInt _
404+
| Float _
405+
| Float_array _ ) ) -> Some false
406+
| ( Float_array _
407+
, ( String _
408+
| NativeString _
409+
| Int64 _
410+
| Int _
411+
| Int32 _
412+
| NativeInt _
413+
| Float _
414+
| Tuple _ ) ) -> Some false
415+
| String _, (Int64 _ | Int _ | Int32 _ | NativeInt _ | Float _ | Tuple _ | Float_array _)
416+
-> Some false
417+
| ( NativeString _
418+
, (Int64 _ | Int _ | Int32 _ | NativeInt _ | Float _ | Tuple _ | Float_array _) ) ->
401419
Some false
420+
| ( Int64 _
421+
, ( String _
422+
| NativeString _
423+
| Int _
424+
| Int32 _
425+
| NativeInt _
426+
| Float _
427+
| Tuple _
428+
| Float_array _ ) ) -> Some false
402429
| Float _, (String _ | NativeString _ | Float_array _ | Int64 _ | Tuple (_, _, _)) ->
403430
Some false
404-
| Int _, (String _ | NativeString _ | Float_array _ | Int64 _ | Tuple (_, _, _)) ->
431+
| ( (Int _ | Int32 _ | NativeInt _)
432+
, (String _ | NativeString _ | Float_array _ | Int64 _ | Tuple (_, _, _)) ) ->
405433
Some false
434+
(* Note: the following cases should not occur when compiling to Javascript *)
435+
| Int _, (Int32 _ | NativeInt _)
436+
| Int32 _, (Int _ | NativeInt _)
437+
| NativeInt _, (Int _ | Int32 _)
438+
| (Int32 _ | NativeInt _), Float _
439+
| Float _, (Int32 _ | NativeInt _) -> None
406440

407441
type loc =
408442
| No
@@ -492,7 +526,10 @@ module Print = struct
492526
Format.fprintf f "%.12g" a.(i)
493527
done;
494528
Format.fprintf f "|]"
529+
| Int i -> Format.fprintf f "%ld" i
530+
| Int32 i -> Format.fprintf f "%ldl" i
495531
| Int64 i -> Format.fprintf f "%LdL" i
532+
| NativeInt i -> Format.fprintf f "%ndn" i
496533
| Tuple (tag, a, _) -> (
497534
Format.fprintf f "<%d>" tag;
498535
match Array.length a with
@@ -509,7 +546,6 @@ module Print = struct
509546
constant f a.(i)
510547
done;
511548
Format.fprintf f ")")
512-
| Int i -> Format.fprintf f "%ld" i
513549

514550
let arg f a =
515551
match a with

compiler/lib/code.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,11 @@ type constant =
181181
| NativeString of Native_string.t
182182
| Float of float
183183
| Float_array of float array
184+
| Int of int32
185+
| Int32 of int32 (** Only produced when compiling to WebAssembly. *)
184186
| Int64 of int64
187+
| NativeInt of nativeint (** Only produced when compiling to WebAssembly. *)
185188
| Tuple of int * constant array * array_or_not
186-
| Int of int32
187189

188190
val constant_equal : constant -> constant -> bool option
189191

compiler/lib/eval.ml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ let eval_prim x =
110110
| "caml_mul_float", _ -> float_binop l ( *. )
111111
| "caml_div_float", _ -> float_binop l ( /. )
112112
| "caml_fmod_float", _ -> float_binop l mod_float
113-
| "caml_int_of_float", [ Float f ] -> Some (Int (Int32.of_float f))
114-
| "to_int", [ Float f ] -> Some (Int (Int32.of_float f))
113+
| "caml_int_of_float", [ Float f ] -> Some (Int (Int.of_float f))
114+
| "to_int", [ Float f ] -> Some (Int (Int.of_float f))
115115
| "to_int", [ Int i ] -> Some (Int i)
116116
(* Math *)
117117
| "caml_neg_float", _ -> float_unop l ( ~-. )
@@ -130,9 +130,9 @@ let eval_prim x =
130130
| "caml_sqrt_float", _ -> float_unop l sqrt
131131
| "caml_tan_float", _ -> float_unop l tan
132132
| ("caml_string_get" | "caml_string_unsafe_get"), [ String s; Int pos ] ->
133-
let pos = Int.to_int pos in
133+
let pos = Int32.to_int pos in
134134
if Config.Flag.safe_string () && pos >= 0 && pos < String.length s
135-
then Some (Int (Int.of_int (Char.code s.[pos])))
135+
then Some (Int (Int32.of_int (Char.code s.[pos])))
136136
else None
137137
| "caml_string_equal", [ String s1; String s2 ] -> bool (String.equal s1 s2)
138138
| "caml_string_notequal", [ String s1; String s2 ] ->
@@ -332,10 +332,12 @@ let the_cond_of info x =
332332
info
333333
(fun x ->
334334
match Flow.Info.def info x with
335-
| Some (Constant (Int 0l)) -> Zero
335+
| Some (Constant (Int 0l | Int32 0l | NativeInt 0n)) -> Zero
336336
| Some
337337
(Constant
338338
( Int _
339+
| Int32 _
340+
| NativeInt _
339341
| Float _
340342
| Tuple _
341343
| String _

compiler/lib/generate.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ let rec constant_rec ~ctx x level instrs =
490490
in
491491
Mlvalue.Block.make ~tag ~args:l, instrs)
492492
| Int i -> int32 i, instrs
493+
| Int32 _ | NativeInt _ ->
494+
assert false (* Should not be produced when compiling to Javascript *)
493495

494496
let constant ~ctx x level =
495497
let expr, instr = constant_rec ~ctx x level [] in

compiler/lib/parse_bytecode.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ end = struct
515515
| Float_array _ -> false
516516
| Int64 _ -> false
517517
| Tuple _ -> false
518-
| Int _ -> true
518+
| Int _ | Int32 _ | NativeInt _ -> true
519519
end
520520

521521
let const i = Constant (Int i)

0 commit comments

Comments
 (0)