Skip to content

Commit 688d744

Browse files
author
Will Chandler
committed
Fix rust-lang#406, handle LLVM comments in asm filtering
The addition of LLVM comments to the generated asm has prevented the filtering script from recognizing some labels. This commit fixes the missing labels and should keep comments in the filtered asm.
1 parent 8dbd4df commit 688d744

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

ui/src/asm_cleanup.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ pub fn filter_asm(block: &str) -> String {
3434
lazy_static! {
3535
// Example: mov rax, rdx
3636
// Always inlude in results
37-
static ref OPCODE_REGEX: Regex = Regex::new(r"^\s*[a-zA-Z]+.*[^:]$").unwrap();
37+
static ref OPCODE_REGEX: Regex = Regex::new(r"^\s+[a-zA-Z]+.*[^:]$").unwrap();
3838
}
3939
lazy_static! {
4040
// Example:.Lfunc_end7:
4141
// Finds label declarations
4242
// Include in results only if it is referenced by an opcode, or is a function
43-
static ref LABEL_DECL_REGEX: Regex = Regex::new(r"([a-zA-Z_.<][a-zA-Z0-9$&_.,<>\[\]{}:' ]*):$").unwrap();
43+
static ref LABEL_DECL_REGEX: Regex = Regex::new(r"^([a-zA-Z_.<][a-zA-Z0-9$&_.,<>\[\]{}:' ]*):(\s+#.*)?$").unwrap();
4444
}
4545
lazy_static! {
4646
// Example: mov lea rdi, [rip + str.0] // str.0 is the referenced label
@@ -51,19 +51,19 @@ pub fn filter_asm(block: &str) -> String {
5151
// Example: .string "Hello, world!"
5252
// Note: this is a type of directive
5353
// Include in results if it is part of a used label, may contain label references
54-
static ref DATA_REGEX: Regex = Regex::new(r"^\s*\.(string|asciz|ascii|[1248]?byte|short|word|long|quad|value|zero)").unwrap();
54+
static ref DATA_REGEX: Regex = Regex::new(r"^\s+\.(string|asciz|ascii|[1248]?byte|short|word|long|quad|value|zero)").unwrap();
5555
}
5656
lazy_static! {
5757
// Example: .type main,@function
5858
// Note: this is a type of directive
5959
// Never include in results, but is used to find and include functions
60-
static ref FUNCTION_REGEX: Regex = Regex::new(r"^\s*\.type\s*(.*),@function$").unwrap();
60+
static ref FUNCTION_REGEX: Regex = Regex::new(r"^\s+\.type\s*(.*),@function$").unwrap();
6161
}
6262
lazy_static! {
6363
// Example: .p2align 4, 0x90
6464
// Note: this will also match entries found by DATA_REGEX and FUNCTION_REGEX
6565
// Never include in results
66-
static ref DIRECTIVE_REGEX: Regex = Regex::new(r"^\s*\..*[^:]$").unwrap();
66+
static ref DIRECTIVE_REGEX: Regex = Regex::new(r"^\s+\..*[^:]$").unwrap();
6767
}
6868
lazy_static! {
6969
// Never include in results
@@ -188,9 +188,8 @@ mod test {
188188
#[test]
189189
fn used_label_kept() {
190190
assert_eq!(
191-
192-
super::filter_asm(".Lcfi0:\ncallq .Lcfi0\n"),
193-
"\n.Lcfi0:\ncallq .Lcfi0\n");
191+
super::filter_asm(".Lcfi0:\n callq .Lcfi0\n"),
192+
"\n.Lcfi0:\n callq .Lcfi0\n");
194193
}
195194

196195
#[test]
@@ -214,14 +213,14 @@ mod test {
214213

215214
#[test]
216215
fn blank_lines_removed() {
217-
assert_eq!(super::filter_asm(" mov rbp, rsp\n main:\n jmp core::fmt::Arguments\n \n"),
216+
assert_eq!(super::filter_asm(" mov rbp, rsp\nmain:\n jmp core::fmt::Arguments\n \n"),
218217
" mov rbp, rsp\n jmp core::fmt::Arguments\n")
219218
}
220219

221220
#[test]
222221
fn functions_kept() {
223-
assert_eq!(super::filter_asm(" .type main,@function\n main:\n pushq %rax\n"),
224-
"\n main:\n pushq %rax\n");
222+
assert_eq!(super::filter_asm(" .type main,@function\nmain:\n pushq %rax\n"),
223+
"\nmain:\n pushq %rax\n");
225224
}
226225
#[test]
227226
fn used_data_ref_label_kept() {
@@ -238,4 +237,14 @@ mod test {
238237
assert_eq!(super::filter_asm("main:\n .quad ref.1\n mov main\nref.1:\n .quad ref.2\nref.2:\n .quad 1"),
239238
"\nmain:\n .quad ref.1\n mov main\n\nref.1:\n .quad ref.2\n\nref.2:\n .quad 1\n");
240239
}
240+
#[test]
241+
fn label_with_comment_recognized() {
242+
assert_eq!(super::filter_asm(".LBB6_10: # =>Comment\n movq %r15, %rsi\n movq %rbx, %rdx\n ja .LBB6_10\n"),
243+
"\n.LBB6_10: # =>Comment\n movq %r15, %rsi\n movq %rbx, %rdx\n ja .LBB6_10\n");
244+
}
245+
#[test]
246+
fn comment_retained() {
247+
assert_eq!(super::filter_asm("# %bb.0:\n subq $24, %rsp\n"),
248+
"# %bb.0:\n subq $24, %rsp\n")
249+
}
241250
}

0 commit comments

Comments
 (0)