Skip to content

Commit ae00481

Browse files
author
Commeownist
committed
Improve template len estimation
1 parent e435e57 commit ae00481

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/asm.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ use crate::type_of::LayoutGccExt;
6464
// and one register variable assigned to the desired register.
6565
//
6666

67+
const ATT_SYNTAX_INS: &str = ".att_syntax noprefix\n\t";
68+
const INTEL_SYNTAX_INS: &str = "\n\t.intel_syntax noprefix";
69+
70+
6771
struct AsmOutOperand<'a, 'tcx, 'gcc> {
6872
rust_idx: usize,
6973
constraint: &'a str,
@@ -360,7 +364,11 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
360364
}
361365

362366
// Build the template string
363-
let mut template_str = String::with_capacity(estimate_template_length(template, constants_len));
367+
let mut template_str = String::with_capacity(estimate_template_length(template, constants_len, att_dialect));
368+
if !intel_dialect {
369+
template_str.push_str(ATT_SYNTAX_INS);
370+
}
371+
364372
for piece in template {
365373
match *piece {
366374
InlineAsmTemplatePiece::String(ref string) => {
@@ -439,16 +447,10 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
439447
}
440448
}
441449

442-
let template_str =
443-
if intel_dialect {
444-
template_str
445-
}
446-
else {
447-
// FIXME(antoyo): this might break the "m" memory constraint:
448-
// https://stackoverflow.com/a/9347957/389119
449-
format!(".att_syntax noprefix\n\t{}\n\t.intel_syntax noprefix", template_str)
450-
};
451-
450+
if !intel_dialect {
451+
template_str.push_str(INTEL_SYNTAX_INS);
452+
}
453+
452454
let block = self.llbb();
453455
let extended_asm = block.add_extended_asm(None, &template_str);
454456

@@ -496,15 +498,15 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
496498
}
497499
}
498500

499-
fn estimate_template_length(template: &[InlineAsmTemplatePiece], constants_len: usize) -> usize {
501+
fn estimate_template_length(template: &[InlineAsmTemplatePiece], constants_len: usize, att_dialect: bool) -> usize {
500502
let len: usize = template.iter().map(|piece| {
501503
match *piece {
502504
InlineAsmTemplatePiece::String(ref string) => {
503505
string.len()
504506
}
505507
InlineAsmTemplatePiece::Placeholder { .. } => {
506-
// '%' + 1 char modifier + 2 chars index
507-
4
508+
// '%' + 1 char modifier + 1 char index
509+
3
508510
}
509511
}
510512
})
@@ -513,7 +515,12 @@ fn estimate_template_length(template: &[InlineAsmTemplatePiece], constants_len:
513515
// increase it by 5% to account for possible '%' signs that'll be duplicated
514516
// I pulled the number out of blue, but should be fair enough
515517
// as the upper bound
516-
(len as f32 * 1.05) as usize + constants_len
518+
let mut res = (len as f32 * 1.05) as usize + constants_len;
519+
520+
if att_dialect {
521+
res += INTEL_SYNTAX_INS.len() + ATT_SYNTAX_INS.len();
522+
}
523+
res
517524
}
518525

519526
/// Converts a register class to a GCC constraint code.

0 commit comments

Comments
 (0)