xref: /linux/tools/perf/arch/arm/annotate/instructions.c (revision 40d269c000bda9fcd276a0412a9cebd3f6e344c5)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/zalloc.h>
4 #include <sys/types.h>
5 #include <regex.h>
6 #include <stdlib.h>
7 
8 struct arm_annotate {
9 	regex_t call_insn,
10 		jump_insn;
11 };
12 
13 static struct ins_ops *arm__associate_instruction_ops(struct arch *arch, const char *name)
14 {
15 	struct arm_annotate *arm = arch->priv;
16 	struct ins_ops *ops;
17 	regmatch_t match[2];
18 
19 	if (!regexec(&arm->call_insn, name, 2, match, 0))
20 		ops = &call_ops;
21 	else if (!regexec(&arm->jump_insn, name, 2, match, 0))
22 		ops = &jump_ops;
23 	else
24 		return NULL;
25 
26 	arch__associate_ins_ops(arch, name, ops);
27 	return ops;
28 }
29 
30 static int arm__annotate_init(struct arch *arch, char *cpuid __maybe_unused)
31 {
32 	struct arm_annotate *arm;
33 	int err;
34 
35 	if (arch->initialized)
36 		return 0;
37 
38 	arm = zalloc(sizeof(*arm));
39 	if (!arm)
40 		return ENOMEM;
41 
42 #define ARM_CONDS "(cc|cs|eq|ge|gt|hi|le|ls|lt|mi|ne|pl|vc|vs)"
43 	err = regcomp(&arm->call_insn, "^blx?" ARM_CONDS "?$", REG_EXTENDED);
44 	if (err)
45 		goto out_free_arm;
46 	err = regcomp(&arm->jump_insn, "^bx?" ARM_CONDS "?$", REG_EXTENDED);
47 	if (err)
48 		goto out_free_call;
49 #undef ARM_CONDS
50 
51 	arch->initialized = true;
52 	arch->priv	  = arm;
53 	arch->associate_instruction_ops   = arm__associate_instruction_ops;
54 	arch->objdump.comment_char	  = ';';
55 	arch->objdump.skip_functions_char = '+';
56 	return 0;
57 
58 out_free_call:
59 	regfree(&arm->call_insn);
60 out_free_arm:
61 	free(arm);
62 	return SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP;
63 }
64