Skip to content

Commit fa28352

Browse files
committed
Fix MIPS operands with base
1 parent 2ab519d commit fa28352

File tree

5 files changed

+48
-32
lines changed

5 files changed

+48
-32
lines changed

src/diff.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ fn arg_eq(
268268
right_diff.ins.as_ref().and_then(|i| i.reloc.as_ref()),
269269
)
270270
}
271-
ObjInsArg::MipsArg(ls) => {
272-
matches!(right, ObjInsArg::MipsArg(rs) if ls == rs)
271+
ObjInsArg::MipsArg(ls) | ObjInsArg::MipsArgWithBase(ls) => {
272+
matches!(right, ObjInsArg::MipsArg(rs) | ObjInsArg::MipsArgWithBase(rs) if ls == rs)
273273
}
274274
ObjInsArg::BranchOffset(_) => {
275275
// Compare dest instruction idx after diffing
@@ -324,7 +324,7 @@ fn compare_ins(
324324
let a_str = match a {
325325
ObjInsArg::PpcArg(arg) => format!("{arg}"),
326326
ObjInsArg::Reloc | ObjInsArg::RelocWithBase => String::new(),
327-
ObjInsArg::MipsArg(str) => str.clone(),
327+
ObjInsArg::MipsArg(str) | ObjInsArg::MipsArgWithBase(str) => str.clone(),
328328
ObjInsArg::BranchOffset(arg) => format!("{arg}"),
329329
};
330330
let a_diff = if let Some(idx) = state.left_args_idx.get(&a_str) {
@@ -338,7 +338,7 @@ fn compare_ins(
338338
let b_str = match b {
339339
ObjInsArg::PpcArg(arg) => format!("{arg}"),
340340
ObjInsArg::Reloc | ObjInsArg::RelocWithBase => String::new(),
341-
ObjInsArg::MipsArg(str) => str.clone(),
341+
ObjInsArg::MipsArg(str) | ObjInsArg::MipsArgWithBase(str) => str.clone(),
342342
ObjInsArg::BranchOffset(arg) => format!("{arg}"),
343343
};
344344
let b_diff = if let Some(idx) = state.right_args_idx.get(&b_str) {

src/editops.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ pub struct LevEditOp {
4242

4343
pub fn editops_find<T>(query: &[T], choice: &[T]) -> Vec<LevEditOp>
4444
where T: PartialEq {
45-
let Affix {
46-
prefix_len,
47-
suffix_len,
48-
} = Affix::find(query, choice);
45+
let Affix { prefix_len, suffix_len } = Affix::find(query, choice);
4946

5047
let first_string = &query[prefix_len..query.len() - suffix_len];
5148
let second_string = &choice[prefix_len..choice.len() - suffix_len];
@@ -185,19 +182,14 @@ pub struct Affix {
185182
impl Affix {
186183
pub fn find<T>(s1: &[T], s2: &[T]) -> Affix
187184
where T: PartialEq {
188-
let prefix_len = s1.iter()
189-
.zip(s2.iter())
190-
.take_while(|t| t.0 == t.1)
191-
.count();
192-
let suffix_len = s1[prefix_len..].iter()
185+
let prefix_len = s1.iter().zip(s2.iter()).take_while(|t| t.0 == t.1).count();
186+
let suffix_len = s1[prefix_len..]
187+
.iter()
193188
.rev()
194189
.zip(s2[prefix_len..].iter().rev())
195190
.take_while(|t| t.0 == t.1)
196191
.count();
197192

198-
Affix {
199-
prefix_len,
200-
suffix_len,
201-
}
193+
Affix { prefix_len, suffix_len }
202194
}
203195
}

src/obj/mips.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::BTreeMap;
22

33
use anyhow::Result;
4-
use rabbitizer::{config, Abi, Instruction, InstrCategory, OperandType};
4+
use rabbitizer::{config, Abi, InstrCategory, Instruction, OperandType};
55

66
use crate::obj::{ObjIns, ObjInsArg, ObjReloc};
77

@@ -37,37 +37,49 @@ pub fn process_code(
3737
let branch_offset = instruction.branch_offset();
3838
let branch_dest =
3939
if is_branch { Some((cur_addr as i32 + branch_offset) as u32) } else { None };
40-
let args = instruction
41-
.get_operands_slice()
42-
.iter()
43-
.map(|op| match op {
44-
OperandType::cpu_immediate | OperandType::cpu_label | OperandType::cpu_branch_target_label => {
40+
41+
println!("{:?}", instruction.get_operands_slice());
42+
let mut args = Vec::new();
43+
for op in instruction.get_operands_slice() {
44+
match op {
45+
OperandType::cpu_immediate
46+
| OperandType::cpu_label
47+
| OperandType::cpu_branch_target_label => {
4548
if is_branch {
46-
ObjInsArg::BranchOffset(branch_offset)
49+
args.push(ObjInsArg::BranchOffset(branch_offset));
4750
} else if let Some(reloc) = reloc {
4851
if matches!(&reloc.target_section, Some(s) if s == ".text")
4952
&& reloc.target.address > start_address
5053
&& reloc.target.address < end_address
5154
{
5255
// Inter-function reloc, convert to branch offset
53-
ObjInsArg::BranchOffset(reloc.target.address as i32 - cur_addr as i32)
56+
args.push(ObjInsArg::BranchOffset(
57+
reloc.target.address as i32 - cur_addr as i32,
58+
));
5459
} else {
55-
ObjInsArg::Reloc
60+
args.push(ObjInsArg::Reloc);
5661
}
5762
} else {
58-
ObjInsArg::MipsArg(op.disassemble(&instruction, None))
63+
args.push(ObjInsArg::MipsArg(op.disassemble(&instruction, None)));
5964
}
6065
}
6166
OperandType::cpu_immediate_base => {
6267
if reloc.is_some() {
63-
ObjInsArg::RelocWithBase
68+
args.push(ObjInsArg::RelocWithBase);
6469
} else {
65-
ObjInsArg::MipsArg(op.disassemble(&instruction, None))
70+
args.push(ObjInsArg::MipsArgWithBase(
71+
OperandType::cpu_immediate.disassemble(&instruction, None),
72+
));
6673
}
74+
args.push(ObjInsArg::MipsArg(
75+
OperandType::cpu_rs.disassemble(&instruction, None),
76+
));
77+
}
78+
_ => {
79+
args.push(ObjInsArg::MipsArg(op.disassemble(&instruction, None)));
6780
}
68-
_ => ObjInsArg::MipsArg(op.disassemble(&instruction, None)),
69-
})
70-
.collect();
81+
}
82+
}
7183
let line =
7284
line_info.as_ref().and_then(|map| map.range(..=cur_addr).last().map(|(_, &b)| b));
7385
insts.push(ObjIns {

src/obj/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub struct ObjSection {
4141
pub enum ObjInsArg {
4242
PpcArg(ppc750cl::Argument),
4343
MipsArg(String),
44+
MipsArgWithBase(String),
4445
Reloc,
4546
RelocWithBase,
4647
BranchOffset(i32),

src/views/function_diff.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ fn write_ins(
132132
config.code_font.clone(),
133133
);
134134
}
135+
ObjInsArg::MipsArgWithBase(str) => {
136+
write_text(
137+
str.strip_prefix('$').unwrap_or(str),
138+
color,
139+
job,
140+
config.code_font.clone(),
141+
);
142+
write_text("(", base_color, job, config.code_font.clone());
143+
writing_offset = true;
144+
continue;
145+
}
135146
ObjInsArg::BranchOffset(offset) => {
136147
let addr = offset + ins.address as i32 - base_addr as i32;
137148
write_text(&format!("{addr:x}"), color, job, config.code_font.clone());

0 commit comments

Comments
 (0)