Path: blob/master/tools/perf/arch/arm64/annotate/instructions.c
50159 views
// SPDX-License-Identifier: GPL-2.01#include <linux/compiler.h>2#include <errno.h>3#include <sys/types.h>4#include <regex.h>5#include <stdlib.h>67struct arm64_annotate {8regex_t call_insn,9jump_insn;10};1112static int arm64_mov__parse(struct arch *arch __maybe_unused,13struct ins_operands *ops,14struct map_symbol *ms __maybe_unused,15struct disasm_line *dl __maybe_unused)16{17char *s = strchr(ops->raw, ','), *target, *endptr;1819if (s == NULL)20return -1;2122*s = '\0';23ops->source.raw = strdup(ops->raw);24*s = ',';2526if (ops->source.raw == NULL)27return -1;2829target = ++s;30ops->target.raw = strdup(target);31if (ops->target.raw == NULL)32goto out_free_source;3334ops->target.addr = strtoull(target, &endptr, 16);35if (endptr == target)36goto out_free_target;3738s = strchr(endptr, '<');39if (s == NULL)40goto out_free_target;41endptr = strchr(s + 1, '>');42if (endptr == NULL)43goto out_free_target;4445*endptr = '\0';46*s = ' ';47ops->target.name = strdup(s);48*s = '<';49*endptr = '>';50if (ops->target.name == NULL)51goto out_free_target;5253return 0;5455out_free_target:56zfree(&ops->target.raw);57out_free_source:58zfree(&ops->source.raw);59return -1;60}6162static int mov__scnprintf(struct ins *ins, char *bf, size_t size,63struct ins_operands *ops, int max_ins_name);6465static struct ins_ops arm64_mov_ops = {66.parse = arm64_mov__parse,67.scnprintf = mov__scnprintf,68};6970static struct ins_ops *arm64__associate_instruction_ops(struct arch *arch, const char *name)71{72struct arm64_annotate *arm = arch->priv;73struct ins_ops *ops;74regmatch_t match[2];7576if (!regexec(&arm->jump_insn, name, 2, match, 0))77ops = &jump_ops;78else if (!regexec(&arm->call_insn, name, 2, match, 0))79ops = &call_ops;80else if (!strcmp(name, "ret"))81ops = &ret_ops;82else83ops = &arm64_mov_ops;8485arch__associate_ins_ops(arch, name, ops);86return ops;87}8889static int arm64__annotate_init(struct arch *arch, char *cpuid __maybe_unused)90{91struct arm64_annotate *arm;92int err;9394if (arch->initialized)95return 0;9697arm = zalloc(sizeof(*arm));98if (!arm)99return ENOMEM;100101/* bl, blr */102err = regcomp(&arm->call_insn, "^blr?$", REG_EXTENDED);103if (err)104goto out_free_arm;105/* b, b.cond, br, cbz/cbnz, tbz/tbnz */106err = regcomp(&arm->jump_insn, "^[ct]?br?\\.?(cc|cs|eq|ge|gt|hi|hs|le|lo|ls|lt|mi|ne|pl|vc|vs)?n?z?$",107REG_EXTENDED);108if (err)109goto out_free_call;110111arch->initialized = true;112arch->priv = arm;113arch->associate_instruction_ops = arm64__associate_instruction_ops;114arch->objdump.comment_char = '/';115arch->objdump.skip_functions_char = '+';116arch->e_machine = EM_AARCH64;117arch->e_flags = 0;118return 0;119120out_free_call:121regfree(&arm->call_insn);122out_free_arm:123free(arm);124return SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP;125}126127128