Skip to content

Commit 432ba57

Browse files
authored
Avoid polymorphic comparison in backend (ocaml-flambda#3649)
1 parent 37dbb78 commit 432ba57

27 files changed

+120
-52
lines changed

backend/afl_instrument.ml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
(* Insert instrumentation for afl-fuzz *)
1616

17+
open! Int_replace_polymorphic_compare
1718
open Cmm
1819

1920
module V = Backend_var

backend/amd64/emit.ml

-12
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,6 @@ open! Branch_relaxation
4545

4646
let _label s = D.label ~typ:QWORD s
4747

48-
let is_linux = function
49-
| S_linux -> true
50-
| _ -> false
51-
52-
let is_macosx = function
53-
| S_macosx -> true
54-
| _ -> false
55-
56-
let is_win64 = function
57-
| S_win64 -> true
58-
| _ -> false
59-
6048
(* Override proc.ml *)
6149

6250
let int_reg_name =

backend/branch_relaxation.ml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
(* *)
1515
(**************************************************************************)
1616

17+
open! Int_replace_polymorphic_compare
1718
open Linear
1819

1920
module Make (T : Branch_relaxation_intf.S) = struct

backend/cmm.ml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
(* *)
1414
(**************************************************************************)
1515

16+
open! Int_replace_polymorphic_compare
17+
1618
type machtype_component = Cmx_format.machtype_component =
1719
| Val
1820
| Addr

backend/cmm_builtins.ml

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
(* *)
1414
(**************************************************************************)
1515

16+
open! Int_replace_polymorphic_compare
1617
open Cmm
1718
open Cmm_helpers
1819
open Arch
@@ -52,7 +53,7 @@ let if_operation_supported op ~f =
5253
match Proc.operation_supported op with true -> Some (f ()) | false -> None
5354

5455
let if_operation_supported_bi bi op ~f =
55-
if bi = Primitive.Unboxed_int64 && size_int = 4
56+
if Primitive.equal_unboxed_integer bi Primitive.Unboxed_int64 && size_int = 4
5657
then None
5758
else if_operation_supported op ~f
5859

@@ -85,13 +86,14 @@ let clz ~arg_is_non_zero bi arg dbg =
8586
let op = Cclz { arg_is_non_zero } in
8687
if_operation_supported_bi bi op ~f:(fun () ->
8788
let res = Cop (op, [make_unsigned_int bi arg dbg], dbg) in
88-
if bi = Primitive.Unboxed_int32 && size_int = 8
89+
if Primitive.equal_unboxed_integer bi Primitive.Unboxed_int32
90+
&& size_int = 8
8991
then Cop (Caddi, [res; Cconst_int (-32, dbg)], dbg)
9092
else res)
9193

9294
let ctz ~arg_is_non_zero bi arg dbg =
9395
let arg = make_unsigned_int bi arg dbg in
94-
if bi = Primitive.Unboxed_int32 && size_int = 8
96+
if Primitive.equal_unboxed_integer bi Primitive.Unboxed_int32 && size_int = 8
9597
then
9698
(* regardless of the value of the argument [arg_is_non_zero], always set the
9799
corresponding field to [true], because we make it non-zero below by
@@ -798,7 +800,10 @@ let builtin_even_if_not_annotated = function
798800

799801
let extcall ~dbg ~returns ~alloc ~is_c_builtin ~effects ~coeffects ~ty_args name
800802
typ_res args =
801-
if not returns then assert (typ_res = typ_void);
803+
if not returns
804+
then
805+
assert (
806+
Misc.Stdlib.Array.equal Cmm.equal_machtype_component typ_res typ_void);
802807
let default =
803808
Cop
804809
( Cextcall

backend/cmm_helpers.ml

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
[@@@ocaml.warning "+a-4-9-40-41-42-44-45"]
1717

18+
(* CR-soon xclerc for xclerc: try to add open!
19+
Int_replace_polymorphic_compare *)
1820
module V = Backend_var
1921
module VP = Backend_var.With_provenance
2022
open Cmm

backend/cmm_invariants.ml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
[@@@ocaml.warning "-40"]
1616

17+
open! Int_replace_polymorphic_compare
18+
1719
module Int = Numbers.Int
1820

1921
(* Check a number of continuation-related invariants *)

backend/cmmgen_state.ml

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
[@@@ocaml.warning "+a-4-30-40-41-42"]
1919

20+
open! Int_replace_polymorphic_compare
21+
2022
module S = Misc.Stdlib.String
2123

2224
type ustructured_constant =

backend/emitaux.ml

+14-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
(* Common functions for emitting assembly code *)
1717

18+
open! Int_replace_polymorphic_compare
19+
1820
type error =
1921
| Stack_frame_too_large of int
2022
| Stack_frame_way_too_large of int
@@ -62,16 +64,21 @@ let femit_symbol out s = output_string out (symbol_to_string s)
6264
let emit_symbol s = femit_symbol !output_channel s
6365

6466
let femit_string_literal out s =
67+
let between x low high =
68+
Char.compare x low >= 0 && Char.compare x high <= 0
69+
in
6570
let last_was_escape = ref false in
6671
femit_string out "\"";
6772
for i = 0 to String.length s - 1 do
6873
let c = s.[i] in
69-
if c >= '0' && c <= '9'
74+
if between c '0' '9'
7075
then
7176
if !last_was_escape
7277
then Printf.fprintf out "\\%o" (Char.code c)
7378
else output_char out c
74-
else if c >= ' ' && c <= '~' && c <> '"' (* '"' *) && c <> '\\'
79+
else if between c ' ' '~'
80+
&& (not (Char.equal c '"' (* '"' *)))
81+
&& not (Char.equal c '\\')
7582
then (
7683
output_char out c;
7784
last_was_escape := false)
@@ -221,7 +228,7 @@ let emit_frames a =
221228
type t = bool * Debuginfo.Dbg.t
222229

223230
let equal ((rs1 : bool), dbg1) (rs2, dbg2) =
224-
rs1 = rs2 && Debuginfo.Dbg.compare dbg1 dbg2 = 0
231+
Bool.equal rs1 rs2 && Debuginfo.Dbg.compare dbg1 dbg2 = 0
225232

226233
let hash (rs, dbg) = Hashtbl.hash (rs, Debuginfo.Dbg.hash dbg)
227234
end) in
@@ -362,8 +369,8 @@ let emit_frames a =
362369
in
363370
let info =
364371
if is_fully_packable
365-
then fully_pack_info rs d (rest <> [])
366-
else partially_pack_info rs d (rest <> [])
372+
then fully_pack_info rs d (not (Misc.Stdlib.List.is_empty rest))
373+
else partially_pack_info rs d (not (Misc.Stdlib.List.is_empty rest))
367374
in
368375
let loc =
369376
if is_fully_packable
@@ -398,7 +405,7 @@ let emit_frames a =
398405

399406
let isprefix s1 s2 =
400407
String.length s1 <= String.length s2
401-
&& String.sub s2 0 (String.length s1) = s1
408+
&& String.equal (String.sub s2 0 (String.length s1)) s1
402409

403410
let is_generic_function name =
404411
List.exists
@@ -531,7 +538,7 @@ let reduce_heap_size ~reset =
531538
then float !Flambda_backend_flags.heap_reduction_threshold
532539
else Float.infinity
533540
in
534-
if major_words > heap_reduction_threshold
541+
if Float.compare major_words heap_reduction_threshold > 0
535542
then
536543
Profile.record_call "compact" (fun () ->
537544
reset ();

backend/fdo_info.ml

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
(* *)
1313
(**************************************************************************)
1414

15+
open! Int_replace_polymorphic_compare
16+
1517
type info =
1618
{
1719
dbg: Debuginfo.t;

backend/generic_fns.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(* *)
1111
(**************************************************************************)
1212

13+
open! Int_replace_polymorphic_compare
1314
open Cmm
1415
open Cmm_helpers
1516
module CU = Compilation_unit
@@ -117,7 +118,8 @@ module Tbl0 = struct
117118
let entries t : Cmx_format.generic_fns =
118119
let sorted_keys tbl =
119120
let keys = Hashtbl.fold (fun k () acc -> k :: acc) tbl [] in
120-
List.sort compare keys
121+
(* CR-soon xclerc for xclerc: avoid polymorphic compare *)
122+
List.sort Stdlib.compare keys
121123
in
122124
{ curry_fun = sorted_keys t.curry;
123125
apply_fun = sorted_keys t.apply;

backend/linear.ml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
(**************************************************************************)
1515

1616
(* Transformation of Mach code into a list of pseudo-instructions. *)
17+
open! Int_replace_polymorphic_compare
18+
1719
type label = Cmm.label
1820

1921
type instruction =

backend/operation.ml

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
[@@@ocaml.warning "+a-4-9-40-41-42"]
2828

29+
open! Int_replace_polymorphic_compare [@@ocaml.warning "-66"]
30+
2931
type trap_stack =
3032
| Uncaught
3133
| Specific_trap of Cmm.trywith_shared_label * trap_stack

backend/printcmm.ml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
(* Pretty-printing of C-- code *)
1717

18+
open! Int_replace_polymorphic_compare
1819
open Format
1920
open Cmm
2021

backend/printlinear.ml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
(* Pretty-printing of linearized machine code *)
1717

18+
open! Int_replace_polymorphic_compare
1819
open Format
1920
open Linear
2021

backend/printoperation.ml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[@@@ocaml.warning "+a-4-9-40-41-42"]
22

3+
open! Int_replace_polymorphic_compare
34
open Format
45

56
let operation ?(print_reg = Printreg.reg) (op : Operation.t) arg ppf res =

backend/printreg.ml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
[@@@ocaml.warning "+a-4-9-40-41-42"]
1919

20+
open! Int_replace_polymorphic_compare [@@ocaml.warning "-66"]
2021
open Format
2122
open! Reg
2223

backend/reg.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
(* *)
1414
(**************************************************************************)
1515

16+
open! Int_replace_polymorphic_compare
1617
open Cmm
1718

1819
type irc_work_list =
@@ -199,7 +200,7 @@ let reset() =
199200
soft pseudo-registers *)
200201
if !first_virtual_reg_stamp = -1 then begin
201202
first_virtual_reg_stamp := !currstamp;
202-
assert (!reg_list = []) (* Only hard regs created before now *)
203+
assert (Misc.Stdlib.List.is_empty !reg_list) (* Only hard regs created before now *)
203204
end;
204205
currstamp := !first_virtual_reg_stamp;
205206
reg_list := []

backend/vectorize_utils.ml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[@@@ocaml.warning "+a-40-42"]
22

3+
open! Int_replace_polymorphic_compare [@@ocaml.warning "-66"]
34
open Arch
45

56
module Width_in_bits = struct

backend/x86_binary_emitter.ml

+13-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
[@@@ocaml.warning "+A-4-9-69"]
1616

17+
open! Int_replace_polymorphic_compare
1718
open X86_ast
1819
open X86_proc
1920
module String = Misc.Stdlib.String
@@ -275,11 +276,13 @@ let eval_const b current_pos cst =
275276
(*(X86_gas.string_of_constant cst)*) (Printexc.to_string e);
276277
raise e
277278

278-
let is_imm32L n = n < 0x8000_0000L && n >= -0x8000_0000L
279+
let is_imm32L n = Int64.compare n 0x8000_0000L < 0 && Int64.compare n (-0x8000_0000L) >= 0
279280

280-
let is_imm8L x = x < 128L && x >= -128L
281+
let is_imm8L x = Int64.compare x 128L < 0 && Int64.compare x (-128L) >= 0
281282

282-
let is_imm16L n = n < 32768L && n >= -32768L
283+
let is_imm16L n = Int64.compare n 32768L < 0 && Int64.compare n (-32768L) >= 0
284+
285+
let is_x86 = function | X86 -> true | X64 -> false
283286

284287
let rd_of_regf regf =
285288
match regf with
@@ -380,14 +383,14 @@ let record_reloc b offset_from_section_beginning kind =
380383

381384
let declare_label b s =
382385
let sy = get_symbol b s in
383-
assert (sy.sy_pos = None);
386+
assert (Option.is_none sy.sy_pos);
384387
let pos = Buffer.length b.buf in
385388
sy.sy_pos <- Some pos
386389

387390
let buf_opcodes b opcodes =
388391
ListLabels.iter ~f:(fun opcode -> buf_int8 b opcode) opcodes
389392

390-
let arch64 = Config.architecture = "amd64"
393+
let arch64 = String.equal Config.architecture "amd64"
391394

392395
let emit_rex b rexcode =
393396
if arch64 && rexcode <> 0 then buf_int8 b (rexcode lor rex)
@@ -478,7 +481,7 @@ let emit_prefix_modrm b opcodes rm reg ~prefix =
478481
let idx_reg = idx in
479482
let idx = rd_of_reg64 idx in
480483
if scale = 0 then (
481-
assert (base = None && arch = X86);
484+
assert (Option.is_none base && (is_x86 arch));
482485
match offset with
483486
| OImm8 _ -> assert false
484487
| OImm32 (sym, offset) ->
@@ -1455,7 +1458,7 @@ let emit_reloc_jump near_opcodes far_opcodes b loc symbol =
14551458
in
14561459

14571460
(* Printf.printf "%s/%i: backward togo_short=%Ld\n%!" symbol loc togo_short; *)
1458-
if togo_short >= -128L && togo_short < 128L then (
1461+
if Int64.compare togo_short (-128L) >= 0 && Int64.compare togo_short 128L < 0 then (
14591462
buf_opcodes b near_opcodes;
14601463
buf_int8L b togo_short)
14611464
else (
@@ -1480,7 +1483,7 @@ let emit_reloc_jump near_opcodes far_opcodes b loc symbol =
14801483
Printf.printf "%s/%i: short\n%!" symbol loc;
14811484
*)
14821485
let force_far =
1483-
Int64.of_int ((target_loc - loc) * !instr_size) >= 120L
1486+
Int64.compare (Int64.of_int ((target_loc - loc) * !instr_size)) 120L >= 0
14841487
|| IntSet.mem loc !forced_long_jumps
14851488
in
14861489
if force_far then (
@@ -2164,8 +2167,8 @@ let assemble_line b loc ins =
21642167
| External (_, _) -> ()
21652168
| Set (_, _) -> assert false
21662169
| Section _ -> assert false
2167-
| Mode386 -> assert (system = S_win32)
2168-
| Model _ -> assert (system = S_win32)
2170+
| Mode386 -> assert (is_win32 system)
2171+
| Model _ -> assert (is_win32 system)
21692172
| Cfi_startproc -> ()
21702173
| Cfi_endproc -> ()
21712174
| Cfi_adjust_cfa_offset _ -> ()

backend/x86_dsl.ml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*)
2222

2323

24+
open! Int_replace_polymorphic_compare
2425
open X86_ast
2526
open X86_proc
2627

0 commit comments

Comments
 (0)