Skip to content

Commit 9594d2c

Browse files
committed
Compiler: introduce Targetint
1 parent 30d4cd4 commit 9594d2c

17 files changed

+279
-173
lines changed

compiler/lib/code.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ type constant =
327327
| NativeString of Native_string.t
328328
| Float of float
329329
| Float_array of float array
330-
| Int of Int32.t
330+
| Int of Targetint.t
331331
| Int32 of Int32.t
332332
| Int64 of Int64.t
333333
| NativeInt of Int32.t (* Native int are 32bit on all known backend *)
@@ -352,7 +352,8 @@ module Constant = struct
352352
| Some s, Some c -> same := Some (s && c)
353353
done;
354354
!same
355-
| Int a, Int b | Int32 a, Int32 b -> Some (Int32.equal a b)
355+
| Int a, Int b -> Some (Targetint.equal a b)
356+
| Int32 a, Int32 b -> Some (Int32.equal a b)
356357
| Int64 a, Int64 b -> Some (Int64.equal a b)
357358
| NativeInt a, NativeInt b -> Some (Int32.equal a b)
358359
| Float_array a, Float_array b -> Some (Array.equal Float.ieee_equal a b)
@@ -497,7 +498,7 @@ module Print = struct
497498
Format.fprintf f "%.12g" a.(i)
498499
done;
499500
Format.fprintf f "|]"
500-
| Int i -> Format.fprintf f "%ld" i
501+
| Int i -> Format.fprintf f "%s" (Targetint.to_string i)
501502
| Int32 i -> Format.fprintf f "%ldl" i
502503
| Int64 i -> Format.fprintf f "%LdL" i
503504
| NativeInt i -> Format.fprintf f "%ldn" i

compiler/lib/code.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ type constant =
173173
| NativeString of Native_string.t
174174
| Float of float
175175
| Float_array of float array
176-
| Int of Int32.t
176+
| Int of Targetint.t
177177
| Int32 of Int32.t (** Only produced when compiling to WebAssembly. *)
178178
| Int64 of Int64.t
179179
| NativeInt of Int32.t (** Only produced when compiling to WebAssembly. *)

compiler/lib/effects.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ let cps_branch ~st ~src (pc, args) loc =
300300
(* We are jumping to a block that is also used as a continuation.
301301
We pass it a dummy argument. *)
302302
let x = Var.fresh () in
303-
[ x ], [ Let (x, Constant (Int 0l)), noloc ]
303+
[ x ], [ Let (x, Constant (Int Targetint.zero)), noloc ]
304304
else args, []
305305
in
306306
(* We check the stack depth only for backward edges (so, at
@@ -402,7 +402,9 @@ let cps_last ~st ~alloc_jump_closures pc ((last, last_loc) : last * loc) ~k :
402402
( x'
403403
, Prim
404404
( Extern "caml_maybe_attach_backtrace"
405-
, [ Pv x; Pc (Int (if force then 1l else 0l)) ] ) )
405+
, [ Pv x
406+
; Pc (Int (if force then Targetint.one else Targetint.zero))
407+
] ) )
406408
, noloc )
407409
]
408410
in
@@ -483,7 +485,8 @@ let cps_instr ~st (instr : instr) : instr =
483485
| Pc (Int a) ->
484486
Let
485487
( x
486-
, Prim (Extern "caml_alloc_dummy_function", [ size; Pc (Int (Int32.succ a)) ])
488+
, Prim
489+
(Extern "caml_alloc_dummy_function", [ size; Pc (Int (Targetint.succ a)) ])
487490
)
488491
| _ -> assert false)
489492
| Let (x, Apply { f; args; _ }) when not (Var.Set.mem x st.cps_needed) ->
@@ -562,7 +565,7 @@ let cps_block ~st ~k pc block =
562565
[ arg; k' ]
563566
loc)
564567
| Prim (Extern "%perform", [ Pv effect_ ]) ->
565-
perform_effect ~effect_ ~continuation:(Pc (Int 0l)) loc
568+
perform_effect ~effect_ ~continuation:(Pc (Int Targetint.zero)) loc
566569
| Prim (Extern "%reperform", [ Pv effect_; continuation ]) ->
567570
perform_effect ~effect_ ~continuation loc
568571
| _ -> None

compiler/lib/eval.ml

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let set_static_env s value = Hashtbl.add static_env s value
2929

3030
let get_static_env s = try Some (Hashtbl.find static_env s) with Not_found -> None
3131

32-
module Int = Int32
32+
module Int = Targetint
3333

3434
let int_binop l f =
3535
match l with
@@ -38,16 +38,16 @@ let int_binop l f =
3838

3939
let shift l f =
4040
match l with
41-
| [ Int i; Int j ] -> Some (Int (f i (Int32.to_int j land 0x1f)))
41+
| [ Int i; Int j ] -> Some (Int (f i (Targetint.to_int_exn j land 0x1f)))
4242
| _ -> None
4343

4444
let float_binop_aux (l : constant list) (f : float -> float -> 'a) : 'a option =
4545
let args =
4646
match l with
4747
| [ Float i; Float j ] -> Some (i, j)
48-
| [ Int i; Int j ] -> Some (Int32.to_float i, Int32.to_float j)
49-
| [ Int i; Float j ] -> Some (Int32.to_float i, j)
50-
| [ Float i; Int j ] -> Some (i, Int32.to_float j)
48+
| [ Int i; Int j ] -> Some (Targetint.to_float i, Targetint.to_float j)
49+
| [ Int i; Float j ] -> Some (Targetint.to_float i, j)
50+
| [ Float i; Int j ] -> Some (i, Targetint.to_float j)
5151
| _ -> None
5252
in
5353
match args with
@@ -62,10 +62,10 @@ let float_binop (l : constant list) (f : float -> float -> float) : constant opt
6262
let float_unop (l : constant list) (f : float -> float) : constant option =
6363
match l with
6464
| [ Float i ] -> Some (Float (f i))
65-
| [ Int i ] -> Some (Float (f (Int32.to_float i)))
65+
| [ Int i ] -> Some (Float (f (Targetint.to_float i)))
6666
| _ -> None
6767

68-
let bool' b = Int (if b then 1l else 0l)
68+
let bool' b = Int Targetint.(if b then one else zero)
6969

7070
let bool b = Some (bool' b)
7171

@@ -76,20 +76,20 @@ let float_binop_bool l f =
7676

7777
let eval_prim x =
7878
match x with
79-
| Not, [ Int i ] -> bool Int32.(i = 0l)
80-
| Lt, [ Int i; Int j ] -> bool Int32.(i < j)
81-
| Le, [ Int i; Int j ] -> bool Int32.(i <= j)
82-
| Eq, [ Int i; Int j ] -> bool Int32.(i = j)
83-
| Neq, [ Int i; Int j ] -> bool Int32.(i <> j)
84-
| Ult, [ Int i; Int j ] -> bool (Int32.(j < 0l) || Int32.(i < j))
79+
| Not, [ Int i ] -> bool (Targetint.is_zero i)
80+
| Lt, [ Int i; Int j ] -> bool Targetint.(i < j)
81+
| Le, [ Int i; Int j ] -> bool Targetint.(i <= j)
82+
| Eq, [ Int i; Int j ] -> bool Targetint.(i = j)
83+
| Neq, [ Int i; Int j ] -> bool Targetint.(i <> j)
84+
| Ult, [ Int i; Int j ] -> bool (Targetint.(j < zero) || Targetint.(i < j))
8585
| Extern name, l -> (
8686
let name = Primitive.resolve name in
8787
match name, l with
8888
(* int *)
8989
| "%int_add", _ -> int_binop l Int.add
9090
| "%int_sub", _ -> int_binop l Int.sub
9191
| "%direct_int_mul", _ -> int_binop l Int.mul
92-
| "%direct_int_div", [ _; Int 0l ] -> None
92+
| "%direct_int_div", [ _; Int maybe_zero ] when Targetint.is_zero maybe_zero -> None
9393
| "%direct_int_div", _ -> int_binop l Int.div
9494
| "%direct_int_mod", _ -> int_binop l Int.rem
9595
| "%int_and", _ -> int_binop l Int.logand
@@ -111,9 +111,7 @@ let eval_prim x =
111111
| "caml_mul_float", _ -> float_binop l ( *. )
112112
| "caml_div_float", _ -> float_binop l ( /. )
113113
| "caml_fmod_float", _ -> float_binop l mod_float
114-
| "caml_int_of_float", [ Float f ] -> Some (Int (Int32.of_float f))
115-
| "to_int", [ Float f ] -> Some (Int (Int32.of_float f))
116-
| "to_int", [ Int i ] -> Some (Int i)
114+
| "caml_int_of_float", [ Float f ] -> Some (Int (Targetint.of_float f))
117115
(* Math *)
118116
| "caml_neg_float", _ -> float_unop l ( ~-. )
119117
| "caml_abs_float", _ -> float_unop l abs_float
@@ -131,9 +129,9 @@ let eval_prim x =
131129
| "caml_sqrt_float", _ -> float_unop l sqrt
132130
| "caml_tan_float", _ -> float_unop l tan
133131
| ("caml_string_get" | "caml_string_unsafe_get"), [ String s; Int pos ] ->
134-
let pos = Int.to_int pos in
132+
let pos = Int.to_int_exn pos in
135133
if Config.Flag.safe_string () && pos >= 0 && pos < String.length s
136-
then Some (Int (Int.of_int (Char.code s.[pos])))
134+
then Some (Int (Int.of_int_exn (Char.code s.[pos])))
137135
else None
138136
| "caml_string_equal", [ String s1; String s2 ] -> bool (String.equal s1 s2)
139137
| "caml_string_notequal", [ String s1; String s2 ] ->
@@ -142,10 +140,11 @@ let eval_prim x =
142140
match get_static_env s with
143141
| Some env -> Some (String env)
144142
| None -> None)
145-
| "caml_sys_const_word_size", [ _ ] -> Some (Int 32l)
146-
| "caml_sys_const_int_size", [ _ ] -> Some (Int 32l)
147-
| "caml_sys_const_big_endian", [ _ ] -> Some (Int 0l)
148-
| "caml_sys_const_naked_pointers_checked", [ _ ] -> Some (Int 0l)
143+
| "caml_sys_const_word_size", [ _ ] -> Some (Int (Targetint.of_int_exn 32))
144+
| "caml_sys_const_int_size", [ _ ] ->
145+
Some (Int (Targetint.of_int_exn Targetint.num_bits))
146+
| "caml_sys_const_big_endian", [ _ ] -> Some (Int Targetint.zero)
147+
| "caml_sys_const_naked_pointers_checked", [ _ ] -> Some (Int Targetint.zero)
149148
| _ -> None)
150149
| _ -> None
151150

@@ -154,14 +153,14 @@ let the_length_of info x =
154153
info
155154
(fun x ->
156155
match Flow.Info.def info x with
157-
| Some (Constant (String s)) -> Some (Int32.of_int (String.length s))
156+
| Some (Constant (String s)) -> Some (Targetint.of_int_exn (String.length s))
158157
| Some (Prim (Extern "caml_create_string", [ arg ]))
159158
| Some (Prim (Extern "caml_create_bytes", [ arg ])) -> the_int info arg
160159
| None | Some _ -> None)
161160
None
162161
(fun u v ->
163162
match u, v with
164-
| Some l, Some l' when Int32.(l = l') -> Some l
163+
| Some l, Some l' when Targetint.(l = l') -> Some l
165164
| _ -> None)
166165
x
167166

@@ -225,7 +224,7 @@ let the_cont_of info x (a : cont array) =
225224
(fun x ->
226225
match Flow.Info.def info x with
227226
| Some (Prim (Extern "%direct_obj_tag", [ b ])) -> the_tag_of info b get
228-
| Some (Constant (Int j)) -> get (Int32.to_int j)
227+
| Some (Constant (Int j)) -> get (Targetint.to_int_exn j)
229228
| None | Some _ -> None)
230229
None
231230
(fun u v ->
@@ -237,7 +236,7 @@ let the_cont_of info x (a : cont array) =
237236
(* If [constant_js_equal a b = Some v], then [caml_js_equals a b = v]). *)
238237
let constant_js_equal a b =
239238
match a, b with
240-
| Int i, Int j -> Some (Int32.equal i j)
239+
| Int i, Int j -> Some (Targetint.equal i j)
241240
| Float a, Float b -> Some (Float.ieee_equal a b)
242241
| NativeString a, NativeString b -> Some (Native_string.equal a b)
243242
| String a, String b when Config.Flag.use_js_string () -> Some (String.equal a b)
@@ -289,7 +288,7 @@ let eval_instr info ((x, loc) as i) =
289288
| Let (x, Prim (Extern "caml_ml_string_length", [ s ])) -> (
290289
let c =
291290
match s with
292-
| Pc (String s) -> Some (Int32.of_int (String.length s))
291+
| Pc (String s) -> Some (Targetint.of_int_exn (String.length s))
293292
| Pv v -> the_length_of info v
294293
| _ -> None
295294
in
@@ -324,7 +323,7 @@ let eval_instr info ((x, loc) as i) =
324323
| Let (x, Prim (Extern "%direct_obj_tag", [ y ])) -> (
325324
match the_tag_of info y (fun x -> Some x) with
326325
| Some tag ->
327-
let c = Constant (Int (Int32.of_int tag)) in
326+
let c = Constant (Int (Targetint.of_int_exn tag)) in
328327
Flow.Info.update_def info x c;
329328
[ Let (x, c), loc ]
330329
| None -> [ i ])
@@ -381,11 +380,10 @@ let the_cond_of info x =
381380
info
382381
(fun x ->
383382
match Flow.Info.def info x with
384-
| Some (Constant (Int 0l)) -> Zero
383+
| Some (Constant (Int x)) -> if Targetint.is_zero x then Zero else Non_zero
385384
| Some
386385
(Constant
387-
( Int _
388-
| Int32 _
386+
( Int32 _
389387
| NativeInt _
390388
| Float _
391389
| Tuple _

compiler/lib/flow.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ let the_def_of info x =
345345
= true]) and if both are floats, they are bitwise equal. *)
346346
let constant_identical a b =
347347
match a, b with
348-
| Int i, Int j -> Int32.equal i j
348+
| Int i, Int j -> Targetint.equal i j
349349
| Float a, Float b -> Float.bitwise_equal a b
350350
| NativeString a, NativeString b -> Native_string.equal a b
351351
| String a, String b -> Config.Flag.use_js_string () && String.equal a b

compiler/lib/flow.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ val the_string_of : Info.t -> Code.prim_arg -> string option
5858

5959
val the_native_string_of : Info.t -> Code.prim_arg -> Code.Native_string.t option
6060

61-
val the_int : Info.t -> Code.prim_arg -> int32 option
61+
val the_int : Info.t -> Code.prim_arg -> Stdlib.Targetint.t option
6262

6363
val f : ?skip_param:bool -> Code.program -> Code.program * Info.t

compiler/lib/generate.ml

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ type edge_kind =
309309

310310
let var x = J.EVar (J.V x)
311311

312-
let int n = J.ENum (J.Num.of_int32 (Int32.of_int n))
312+
let int n = J.ENum (J.Num.of_targetint (Targetint.of_int_exn n))
313313

314-
let int32 n = J.ENum (J.Num.of_int32 n)
314+
let targetint n = J.ENum (J.Num.of_targetint n)
315315

316316
let to_int cx = J.EBin (J.Bor, cx, int 0)
317317

@@ -325,7 +325,7 @@ let unsigned x =
325325
in
326326
let pos_int32 =
327327
match x with
328-
| J.ENum num -> ( try Int32.(J.Num.to_int32 num >= 0l) with _ -> false)
328+
| J.ENum num -> ( try Targetint.(J.Num.to_targetint num >= zero) with _ -> false)
329329
| _ -> false
330330
in
331331
if pos_int32 then x else unsigned' x
@@ -455,7 +455,8 @@ let rec constant_rec ~ctx x level instrs =
455455
let constant_max_depth = Config.Param.constant_max_depth () in
456456
let rec detect_list n acc = function
457457
| Tuple (0, [| x; l |], _) -> detect_list (succ n) (x :: acc) l
458-
| Int 0l -> if n > constant_max_depth then Some acc else None
458+
| Int maybe_zero when Targetint.is_zero maybe_zero ->
459+
if n > constant_max_depth then Some acc else None
459460
| _ -> None
460461
in
461462
match detect_list 0 [] x with
@@ -492,7 +493,7 @@ let rec constant_rec ~ctx x level instrs =
492493
else List.map ~f:(fun x -> J.Element x) (List.rev l), instrs
493494
in
494495
Mlvalue.Block.make ~tag ~args:l, instrs)
495-
| Int i -> int32 i, instrs
496+
| Int i -> targetint i, instrs
496497
| Int32 _ | NativeInt _ ->
497498
assert false (* Should not be produced when compiling to Javascript *)
498499

@@ -568,9 +569,9 @@ module DTree = struct
568569

569570
type cond =
570571
| IsTrue
571-
| CEq of int32
572-
| CLt of int32
573-
| CLe of int32
572+
| CEq of Targetint.t
573+
| CLt of Targetint.t
574+
| CLe of Targetint.t
574575

575576
type 'a branch = int list * 'a
576577

@@ -609,9 +610,9 @@ module DTree = struct
609610
(* try to optimize when there are only 2 branch *)
610611
match array_norm with
611612
| [| (b1, ([ i1 ] as l1)); (b2, l2) |] ->
612-
If (CEq (Int32.of_int i1), Branch (l1, b1), Branch (l2, b2))
613+
If (CEq (Targetint.of_int_exn i1), Branch (l1, b1), Branch (l2, b2))
613614
| [| (b1, l1); (b2, ([ i2 ] as l2)) |] ->
614-
If (CEq (Int32.of_int i2), Branch (l2, b2), Branch (l1, b1))
615+
If (CEq (Targetint.of_int_exn i2), Branch (l2, b2), Branch (l1, b1))
615616
| [| (b1, l1); (b2, l2) |] ->
616617
let bound l1 =
617618
match l1, List.rev l1 with
@@ -621,9 +622,9 @@ module DTree = struct
621622
let min1, max1 = bound l1 in
622623
let min2, max2 = bound l2 in
623624
if max1 < min2
624-
then If (CLt (Int32.of_int max1), Branch (l2, b2), Branch (l1, b1))
625+
then If (CLt (Targetint.of_int_exn max1), Branch (l2, b2), Branch (l1, b1))
625626
else if max2 < min1
626-
then If (CLt (Int32.of_int max2), Branch (l1, b1), Branch (l2, b2))
627+
then If (CLt (Targetint.of_int_exn max2), Branch (l1, b1), Branch (l2, b2))
627628
else raise Not_found
628629
| _ -> raise Not_found
629630
with Not_found -> (
@@ -641,7 +642,7 @@ module DTree = struct
641642
let range1 = snd ai.(h) and range2 = snd ai.(succ h) in
642643
match range1, range2 with
643644
| [], _ | _, [] -> assert false
644-
| _, lower_bound2 :: _ -> If (CLe (Int32.of_int lower_bound2), b2, b1))
645+
| _, lower_bound2 :: _ -> If (CLe (Targetint.of_int_exn lower_bound2), b2, b1))
645646
in
646647
let len = Array.length ai in
647648
assert (len > 0);
@@ -1269,7 +1270,7 @@ let rec translate_expr ctx queue loc x e level : _ * J.statement_list =
12691270
let i, queue =
12701271
let (_px, cx), queue = access_queue' ~ctx queue size in
12711272
match cx with
1272-
| J.ENum i -> Int32.to_int (J.Num.to_int32 i), queue
1273+
| J.ENum i -> Targetint.to_int_exn (J.Num.to_targetint i), queue
12731274
| _ -> assert false
12741275
in
12751276
let args = Array.to_list (Array.init i ~f:(fun _ -> J.V (Var.fresh ()))) in
@@ -1668,9 +1669,9 @@ and compile_decision_tree kind st scope_stack loc cx dtree ~fall_through =
16681669
let e' =
16691670
match cond with
16701671
| IsTrue -> cx
1671-
| CEq n -> J.EBin (J.EqEqEq, int32 n, cx)
1672-
| CLt n -> J.EBin (J.LtInt, int32 n, cx)
1673-
| CLe n -> J.EBin (J.LeInt, int32 n, cx)
1672+
| CEq n -> J.EBin (J.EqEqEq, targetint n, cx)
1673+
| CLt n -> J.EBin (J.LtInt, targetint n, cx)
1674+
| CLe n -> J.EBin (J.LeInt, targetint n, cx)
16741675
in
16751676
( never1 && never2
16761677
, Js_simpl.if_statement

0 commit comments

Comments
 (0)