Skip to content

Commit

Permalink
Merge pull request #378 from pacak/mca-intel
Browse files Browse the repository at this point in the history
mca analysis: only include .intel_syntax if original file had it
  • Loading branch information
pacak authored Feb 13, 2025
2 parents be37c0d + 5062414 commit 6752138
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/asm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(clippy::missing_errors_doc)]
use crate::asm::statements::{GenericDirective, Label};
use crate::asm::statements::Label;
use crate::cached_lines::CachedLines;
use crate::demangle::LabelKind;
use crate::{
Expand All @@ -12,7 +12,7 @@ mod statements;

use owo_colors::OwoColorize;
use statements::{parse_statement, Loc};
pub use statements::{Directive, Instruction, Statement};
pub use statements::{Directive, GenericDirective, Instruction, Statement};
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::ops::Range;
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ pub trait Dumpable {
/// Given a set of lines find all the interesting items
fn find_items(lines: &[Self::Line<'_>]) -> BTreeMap<Item, Range<usize>>;

/// Initialize freshly created Dumpable using additional information from the file
fn init(&mut self, _lines: &[Self::Line<'_>]) {}

/// print all the lines from this range, aplying the required formatting
fn dump_range(&self, fmt: &Format, lines: &[Self::Line<'_>]) -> anyhow::Result<()>;

Expand All @@ -300,7 +303,7 @@ pub trait Dumpable {

/// Parse a dumpable item from a file and dump it with all the extra context
pub fn dump_function<T: Dumpable>(
dumpable: &T,
dumpable: &mut T,
goal: ToDump,
path: &Path,
fmt: &Format,
Expand All @@ -312,6 +315,7 @@ pub fn dump_function<T: Dumpable>(

let lines = T::split_lines(&contents)?;
let items = T::find_items(&lines);
dumpable.init(&lines);

match pick_dump_item(goal, fmt, &items) {
Some(range) => {
Expand Down
17 changes: 8 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ fn main() -> anyhow::Result<()> {
match file.extension() {
Some(ext) if ext == "s" => {
let nope = PathBuf::new();
let asm = Asm::new(&nope, &nope);
let mut asm = Asm::new(&nope, &nope);
let mut format = opts.format;
// For standalone file we don't know the matching
// system root so don't even try to dump it
format.rust = false;
dump_function(&asm, opts.to_dump, file, &format)?;
dump_function(&mut asm, opts.to_dump, file, &format)?;
}
_ => {
#[cfg(feature = "disasm")]
Expand Down Expand Up @@ -295,21 +295,20 @@ fn main() -> anyhow::Result<()> {

match opts.syntax.output_type {
OutputType::Asm | OutputType::Wasm => {
let asm = Asm::new(metadata.workspace_root.as_std_path(), &sysroot);
dump_function(&asm, opts.to_dump, &asm_path, &opts.format)
let mut asm = Asm::new(metadata.workspace_root.as_std_path(), &sysroot);
dump_function(&mut asm, opts.to_dump, &asm_path, &opts.format)
}
OutputType::Llvm | OutputType::LlvmInput => {
dump_function(&Llvm, opts.to_dump, &asm_path, &opts.format)
dump_function(&mut Llvm, opts.to_dump, &asm_path, &opts.format)
}
OutputType::Mir => dump_function(&Mir, opts.to_dump, &asm_path, &opts.format),
OutputType::Mir => dump_function(&mut Mir, opts.to_dump, &asm_path, &opts.format),
OutputType::Mca => {
let mca = Mca::new(
let mut mca = Mca::new(
&opts.mca_arg,
opts.syntax.output_style,
cargo.target.as_deref(),
opts.target_cpu.as_deref(),
);
dump_function(&mca, opts.to_dump, &asm_path, &opts.format)
dump_function(&mut mca, opts.to_dump, &asm_path, &opts.format)
}
#[cfg(not(feature = "disasm"))]
OutputType::Disasm => no_disasm!(),
Expand Down
31 changes: 18 additions & 13 deletions src/mca.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use crate::{
asm::Statement,
demangle, esafeprintln,
opts::{Format, OutputStyle},
safeprintln, Dumpable,
};
use crate::{asm::Statement, demangle, esafeprintln, opts::Format, safeprintln, Dumpable};
use std::{
io::{BufRead, BufReader},
process::{Command, Stdio},
Expand All @@ -12,22 +7,21 @@ use std::{
pub struct Mca<'a> {
/// mca specific arguments
args: &'a [String],
output_style: OutputStyle,
target_triple: Option<&'a str>,
target_cpu: Option<&'a str>,
intel_syntax: bool,
}
impl<'a> Mca<'a> {
pub fn new(
mca_args: &'a [String],
output_style: OutputStyle,
target_triple: Option<&'a str>,
target_cpu: Option<&'a str>,
) -> Self {
Self {
args: mca_args,
output_style,
target_triple,
target_cpu,
intel_syntax: false,
}
}
}
Expand All @@ -39,6 +33,18 @@ impl Dumpable for Mca<'_> {
crate::asm::parse_file(contents)
}

fn init(&mut self, lines: &[Self::Line<'_>]) {
use crate::asm::{Directive, GenericDirective, Statement};
for line in lines {
let Statement::Directive(Directive::Generic(GenericDirective(dir))) = line else {
return;
};
if dir.contains("intel_syntax") {
self.intel_syntax = true;
}
}
}

fn find_items(
lines: &[Self::Line<'_>],
) -> std::collections::BTreeMap<crate::Item, std::ops::Range<usize>> {
Expand Down Expand Up @@ -73,11 +79,10 @@ impl Dumpable for Mca<'_> {
let o = mca.stdout.take().expect("Stdout should be piped");
let e = mca.stderr.take().expect("Stderr should be piped");

match self.output_style {
if self.intel_syntax {
// without that llvm-mca gets confused for some instructions
OutputStyle::Intel => writeln!(i, ".intel_syntax")?,
OutputStyle::Att => {}
};
writeln!(i, ".intel_syntax")?
}

for line in lines.iter() {
match line {
Expand Down

0 comments on commit 6752138

Please sign in to comment.