Path: blob/master/tools/perf/arch/arm64/annotate/instructions.c
26292 views
// SPDX-License-Identifier: GPL-2.01#include <linux/compiler.h>2#include <sys/types.h>3#include <regex.h>4#include <stdlib.h>56struct arm64_annotate {7regex_t call_insn,8jump_insn;9};1011static int arm64_mov__parse(struct arch *arch __maybe_unused,12struct ins_operands *ops,13struct map_symbol *ms __maybe_unused,14struct disasm_line *dl __maybe_unused)15{16char *s = strchr(ops->raw, ','), *target, *endptr;1718if (s == NULL)19return -1;2021*s = '\0';22ops->source.raw = strdup(ops->raw);23*s = ',';2425if (ops->source.raw == NULL)26return -1;2728target = ++s;29ops->target.raw = strdup(target);30if (ops->target.raw == NULL)31goto out_free_source;3233ops->target.addr = strtoull(target, &endptr, 16);34if (endptr == target)35goto out_free_target;3637s = strchr(endptr, '<');38if (s == NULL)39goto out_free_target;40endptr = strchr(s + 1, '>');41if (endptr == NULL)42goto out_free_target;4344*endptr = '\0';45*s = ' ';46ops->target.name = strdup(s);47*s = '<';48*endptr = '>';49if (ops->target.name == NULL)50goto out_free_target;5152return 0;5354out_free_target:55zfree(&ops->target.raw);56out_free_source:57zfree(&ops->source.raw);58return -1;59}6061static int mov__scnprintf(struct ins *ins, char *bf, size_t size,62struct ins_operands *ops, int max_ins_name);6364static struct ins_ops arm64_mov_ops = {65.parse = arm64_mov__parse,66.scnprintf = mov__scnprintf,67};6869static struct ins_ops *arm64__associate_instruction_ops(struct arch *arch, const char *name)70{71struct arm64_annotate *arm = arch->priv;72struct ins_ops *ops;73regmatch_t match[2];7475if (!regexec(&arm->jump_insn, name, 2, match, 0))76ops = &jump_ops;77else if (!regexec(&arm->call_insn, name, 2, match, 0))78ops = &call_ops;79else if (!strcmp(name, "ret"))80ops = &ret_ops;81else82ops = &arm64_mov_ops;8384arch__associate_ins_ops(arch, name, ops);85return ops;86}8788static int arm64__annotate_init(struct arch *arch, char *cpuid __maybe_unused)89{90struct arm64_annotate *arm;91int err;9293if (arch->initialized)94return 0;9596arm = zalloc(sizeof(*arm));97if (!arm)98return ENOMEM;99100/* bl, blr */101err = regcomp(&arm->call_insn, "^blr?$", REG_EXTENDED);102if (err)103goto out_free_arm;104/* b, b.cond, br, cbz/cbnz, tbz/tbnz */105err = regcomp(&arm->jump_insn, "^[ct]?br?\\.?(cc|cs|eq|ge|gt|hi|hs|le|lo|ls|lt|mi|ne|pl|vc|vs)?n?z?$",106REG_EXTENDED);107if (err)108goto out_free_call;109110arch->initialized = true;111arch->priv = arm;112arch->associate_instruction_ops = arm64__associate_instruction_ops;113arch->objdump.comment_char = '/';114arch->objdump.skip_functions_char = '+';115arch->e_machine = EM_AARCH64;116arch->e_flags = 0;117return 0;118119out_free_call:120regfree(&arm->call_insn);121out_free_arm:122free(arm);123return SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP;124}125126127