Skip to content

Commit e33816d

Browse files
committed
review: print the implicit registers as well
1 parent a7c2823 commit e33816d

File tree

7 files changed

+66
-23
lines changed

7 files changed

+66
-23
lines changed

cranelift/assembler-x64/meta/src/generate/format.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl dsl::Format {
1414
/// once Cranelift has switched to using this assembler predominantly
1515
/// (TODO).
1616
#[must_use]
17-
pub fn generate_att_style_operands(&self) -> String {
17+
pub(crate) fn generate_att_style_operands(&self) -> String {
1818
let ordered_ops: Vec<_> = self
1919
.operands
2020
.iter()
@@ -25,7 +25,22 @@ impl dsl::Format {
2525
ordered_ops.join(", ")
2626
}
2727

28-
pub fn generate_rex_encoding(&self, f: &mut Formatter, rex: &dsl::Rex) {
28+
#[must_use]
29+
pub(crate) fn generate_implicit_operands(&self) -> String {
30+
let ops: Vec<_> = self
31+
.operands
32+
.iter()
33+
.filter(|o| o.implicit)
34+
.map(|o| format!("{{{}}}", o.location))
35+
.collect();
36+
if ops.is_empty() {
37+
String::new()
38+
} else {
39+
format!(" ;; implicit: {}", ops.join(", "))
40+
}
41+
}
42+
43+
pub(crate) fn generate_rex_encoding(&self, f: &mut Formatter, rex: &dsl::Rex) {
2944
self.generate_prefixes(f, rex);
3045
self.generate_rex_prefix(f, rex);
3146
self.generate_opcodes(f, rex);

cranelift/assembler-x64/meta/src/generate/inst.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl dsl::Inst {
194194
f.add_block(
195195
"fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result",
196196
|f| {
197-
for op in self.format.operands.iter().filter(|o| !o.implicit) {
197+
for op in self.format.operands.iter() {
198198
let location = op.location;
199199
let to_string = location.generate_to_string(op.extension);
200200
fmtln!(f, "let {location} = {to_string};");
@@ -207,7 +207,8 @@ impl dsl::Inst {
207207
&self.mnemonic
208208
};
209209
let ordered_ops = self.format.generate_att_style_operands();
210-
fmtln!(f, "write!(f, \"{inst_name} {ordered_ops}\")");
210+
let implicit_ops = self.format.generate_implicit_operands();
211+
fmtln!(f, "write!(f, \"{inst_name} {ordered_ops}{implicit_ops}\")");
211212
},
212213
);
213214
},

cranelift/assembler-x64/src/fuzz.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn roundtrip(inst: &Inst<FuzzRegs>) {
2727
// off the instruction offset first.
2828
let expected = expected.split_once(' ').unwrap().1;
2929
let actual = inst.to_string();
30-
if expected != actual && expected != replace_signed_immediates(&actual) {
30+
if expected != actual && expected != fix_up(&actual) {
3131
println!("> {inst}");
3232
println!(" debug: {inst:x?}");
3333
println!(" assembled: {}", pretty_print_hexadecimal(&assembled));
@@ -166,6 +166,33 @@ fn replace() {
166166
);
167167
}
168168

169+
/// Remove everything after the first semicolon in the disassembly and trim any
170+
/// trailing spaces. This is necessary to remove the implicit operands we end up
171+
/// printing for Cranelift's sake.
172+
fn remove_after_semicolon(dis: &str) -> &str {
173+
match dis.find(';') {
174+
None => dis,
175+
Some(idx) => {
176+
let (prefix, _) = dis.split_at(idx);
177+
prefix.trim()
178+
}
179+
}
180+
}
181+
182+
#[test]
183+
fn remove_after_parenthesis_test() {
184+
assert_eq!(
185+
remove_after_semicolon("imulb 0x7658eddd(%rcx) ;; implicit: %ax"),
186+
"imulb 0x7658eddd(%rcx)"
187+
);
188+
}
189+
190+
/// Run some post-processing on the disassembly to make it match Capstone.
191+
fn fix_up(dis: &str) -> std::borrow::Cow<str> {
192+
let dis = remove_after_semicolon(dis);
193+
replace_signed_immediates(&dis)
194+
}
195+
169196
/// Fuzz-specific registers.
170197
///
171198
/// For the fuzzer, we do not need any fancy register types; see [`FuzzReg`].

cranelift/filetests/filetests/isa/x64/i128.clif

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ block0(v0: i128, v1: i128):
207207
; addq %rsi, %rdx
208208
; movq %rdi, %rax
209209
; movq %rdx, %r8
210-
; mulq %rcx
210+
; mulq %rcx ;; implicit: %rax, %rdx
211211
; movq %rdx, %rcx
212212
; movq %r8, %rdx
213213
; addq %rcx, %rdx
@@ -1904,7 +1904,7 @@ block0(v0: i64, v1: i64):
19041904
; movq %rsp, %rbp
19051905
; block0:
19061906
; movq %rdi, %rax
1907-
; mulq %rsi
1907+
; mulq %rsi ;; implicit: %rax, %rdx
19081908
; movq %rbp, %rsp
19091909
; popq %rbp
19101910
; ret
@@ -1968,7 +1968,7 @@ block0(v0: i64, v1: i64):
19681968
; movq %rsp, %rbp
19691969
; block0:
19701970
; movq %rdi, %rax
1971-
; mulq %rsi
1971+
; mulq %rsi ;; implicit: %rax, %rdx
19721972
; movq %rbp, %rsp
19731973
; popq %rbp
19741974
; ret
@@ -1997,7 +1997,7 @@ block0(v0: i64, v1: i64):
19971997
; movq %rsp, %rbp
19981998
; block0:
19991999
; movq %rdi, %rax
2000-
; imulq %rsi
2000+
; imulq %rsi ;; implicit: %rax, %rdx
20012001
; movq %rbp, %rsp
20022002
; popq %rbp
20032003
; ret

cranelift/filetests/filetests/isa/x64/mul.clif

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ block0(v0: i8, v1: i8):
1313
; movq %rsp, %rbp
1414
; block0:
1515
; movq %rdi, %rax
16-
; mulb %sil
16+
; mulb %sil ;; implicit: %ax
1717
; movq %rbp, %rsp
1818
; popq %rbp
1919
; ret
@@ -122,8 +122,8 @@ block0(v0: i8, v1: i8, v2: i8):
122122
; movq %rsp, %rbp
123123
; block0:
124124
; movq %rdi, %rax
125-
; mulb %sil
126-
; mulb %dl
125+
; mulb %sil ;; implicit: %ax
126+
; mulb %dl ;; implicit: %ax
127127
; movq %rbp, %rsp
128128
; popq %rbp
129129
; ret
@@ -237,7 +237,7 @@ block0(v0: i8):
237237
; movq %rsp, %rbp
238238
; block0:
239239
; movq %rdi, %rax
240-
; mulb (%rip)
240+
; mulb (%rip) ;; implicit: %ax
241241
; movq %rbp, %rsp
242242
; popq %rbp
243243
; ret
@@ -502,7 +502,7 @@ block0(v0: i8, v1: i8):
502502
; movq %rsp, %rbp
503503
; block0:
504504
; movq %rdi, %rax
505-
; imulb %sil
505+
; imulb %sil ;; implicit: %ax
506506
; movq %rbp, %rsp
507507
; popq %rbp
508508
; ret
@@ -531,7 +531,7 @@ block0(v0: i8, v1: i8):
531531
; movq %rsp, %rbp
532532
; block0:
533533
; movq %rdi, %rax
534-
; mulb %sil
534+
; mulb %sil ;; implicit: %ax
535535
; movq %rbp, %rsp
536536
; popq %rbp
537537
; ret

cranelift/filetests/filetests/isa/x64/smulhi.clif

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ block0(v0: i8, v1: i8):
1212
; movq %rsp, %rbp
1313
; block0:
1414
; movq %rdi, %rax
15-
; imulb %sil
15+
; imulb %sil ;; implicit: %ax
1616
; sarw $8, %ax, %ax
1717
; movq %rbp, %rsp
1818
; popq %rbp
@@ -41,7 +41,7 @@ block0(v0: i16, v1: i16):
4141
; movq %rsp, %rbp
4242
; block0:
4343
; movq %rdi, %rax
44-
; imulw %si
44+
; imulw %si ;; implicit: %ax, %dx
4545
; movq %rdx, %rax
4646
; movq %rbp, %rsp
4747
; popq %rbp
@@ -70,7 +70,7 @@ block0(v0: i32, v1: i32):
7070
; movq %rsp, %rbp
7171
; block0:
7272
; movq %rdi, %rax
73-
; imull %esi
73+
; imull %esi ;; implicit: %eax, %edx
7474
; movq %rdx, %rax
7575
; movq %rbp, %rsp
7676
; popq %rbp
@@ -99,7 +99,7 @@ block0(v0: i64, v1: i64):
9999
; movq %rsp, %rbp
100100
; block0:
101101
; movq %rdi, %rax
102-
; imulq %rsi
102+
; imulq %rsi ;; implicit: %rax, %rdx
103103
; movq %rdx, %rax
104104
; movq %rbp, %rsp
105105
; popq %rbp

cranelift/filetests/filetests/isa/x64/umulhi.clif

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ block0(v0: i8, v1: i8):
1212
; movq %rsp, %rbp
1313
; block0:
1414
; movq %rdi, %rax
15-
; mulb %sil
15+
; mulb %sil ;; implicit: %ax
1616
; shrw $8, %ax, %ax
1717
; movq %rbp, %rsp
1818
; popq %rbp
@@ -41,7 +41,7 @@ block0(v0: i16, v1: i16):
4141
; movq %rsp, %rbp
4242
; block0:
4343
; movq %rdi, %rax
44-
; mulw %si
44+
; mulw %si ;; implicit: %ax, %dx
4545
; movq %rdx, %rax
4646
; movq %rbp, %rsp
4747
; popq %rbp
@@ -70,7 +70,7 @@ block0(v0: i32, v1: i32):
7070
; movq %rsp, %rbp
7171
; block0:
7272
; movq %rdi, %rax
73-
; mull %esi
73+
; mull %esi ;; implicit: %eax, %edx
7474
; movq %rdx, %rax
7575
; movq %rbp, %rsp
7676
; popq %rbp
@@ -99,7 +99,7 @@ block0(v0: i64, v1: i64):
9999
; movq %rsp, %rbp
100100
; block0:
101101
; movq %rdi, %rax
102-
; mulq %rsi
102+
; mulq %rsi ;; implicit: %rax, %rdx
103103
; movq %rdx, %rax
104104
; movq %rbp, %rsp
105105
; popq %rbp

0 commit comments

Comments
 (0)