Skip to content

Commit 1eb6cc7

Browse files
committed
Branch relaxation: mostly accurate estimation of the size of asm statements
1 parent e680a56 commit 1eb6cc7

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

aarch64/Asmexpand.ml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,28 @@ let expand_builtin_inline name args res =
407407
| _ ->
408408
raise (Error ("unrecognized builtin " ^ name))
409409

410+
(* Number of statements in a piece of inline assembly code.
411+
This gives an upper bound on the number of machine instructions.
412+
(Some statements can be labels or directives.) *)
413+
414+
let re_asm_comment = Str.regexp "\\(//\\|^#\\).*$" (* // or # at BOL *)
415+
let re_blank_line = Str.regexp "^[ \t]*\n"
416+
let re_asm_stmt_separator = Str.regexp "[\n;]" (* newline or ; *)
417+
418+
let num_statements_inline_asm txt =
419+
txt |> Str.global_replace re_asm_comment ""
420+
|> Str.global_replace re_blank_line ""
421+
|> Str.split re_asm_stmt_separator
422+
|> List.length
423+
410424
(* Branch relaxation *)
411425

412426
module BInfo: BRANCH_INFORMATION = struct
413427

414428
let builtin_size = function
415429
| EF_annot _ -> 0
416430
| EF_debug _ -> 0
417-
| EF_inline_asm _ -> 32 (* hope it's no more than 8 instructions *)
431+
| EF_inline_asm(txt, _, _) -> 4 * num_statements_inline_asm txt
418432
| _ -> assert false
419433

420434
let instr_size = function

powerpc/Asmexpand.ml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,14 +827,31 @@ let set_cr6 sg =
827827
else emit (Pcrxor(CRbit_6, CRbit_6, CRbit_6))
828828
end
829829

830+
(* Number of statements in a piece of inline assembly code.
831+
This gives an upper bound on the number of machine instructions.
832+
(Some statements can be labels or directives.) *)
833+
834+
let re_asm_comment =
835+
if Configuration.system = "diab"
836+
then Str.regexp "[#;].*$" (* comments start with # or ; *)
837+
else Str.regexp "#.*$" (* comments start with # *)
838+
let re_blank_line = Str.regexp "^[ \t]*\n"
839+
let re_asm_stmt_separator = Str.regexp "[\n;]" (* newline or ; *)
840+
841+
let num_statements_inline_asm txt =
842+
txt |> Str.global_replace re_asm_comment ""
843+
|> Str.global_replace re_blank_line ""
844+
|> Str.split re_asm_stmt_separator
845+
|> List.length
846+
830847
(* Branch relaxation *)
831848

832849
module BInfo: BRANCH_INFORMATION = struct
833850

834851
let builtin_size = function
835852
| EF_annot _ -> 0
836853
| EF_debug _ -> 0
837-
| EF_inline_asm _ -> 32 (* hope it's no more than 8 instructions *)
854+
| EF_inline_asm(txt, _, _) -> 4 * num_statements_inline_asm txt
838855
| _ -> assert false
839856

840857
let instr_size = function

0 commit comments

Comments
 (0)