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