1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef __PERF_UTIL_DISASM_H
3 #define __PERF_UTIL_DISASM_H
4 
5 #include "map_symbol.h"
6 
7 #ifdef HAVE_DWARF_SUPPORT
8 #include "dwarf-aux.h"
9 #endif
10 
11 struct annotation_options;
12 struct disasm_line;
13 struct ins;
14 struct evsel;
15 struct symbol;
16 struct data_loc_info;
17 struct type_state;
18 struct disasm_line;
19 
20 struct arch {
21 	const char	*name;
22 	struct ins	*instructions;
23 	size_t		nr_instructions;
24 	size_t		nr_instructions_allocated;
25 	struct ins_ops  *(*associate_instruction_ops)(struct arch *arch, const char *name);
26 	bool		sorted_instructions;
27 	bool		initialized;
28 	const char	*insn_suffix;
29 	void		*priv;
30 	unsigned int	model;
31 	unsigned int	family;
32 	int		(*init)(struct arch *arch, char *cpuid);
33 	bool		(*ins_is_fused)(struct arch *arch, const char *ins1,
34 					const char *ins2);
35 	struct		{
36 		char comment_char;
37 		char skip_functions_char;
38 		char register_char;
39 		char memory_ref_char;
40 		char imm_char;
41 	} objdump;
42 #ifdef HAVE_DWARF_SUPPORT
43 	void		(*update_insn_state)(struct type_state *state,
44 				struct data_loc_info *dloc, Dwarf_Die *cu_die,
45 				struct disasm_line *dl);
46 #endif
47 };
48 
49 struct ins {
50 	const char     *name;
51 	struct ins_ops *ops;
52 };
53 
54 struct ins_operands {
55 	char	*raw;
56 	struct {
57 		char	*raw;
58 		char	*name;
59 		struct symbol *sym;
60 		u64	addr;
61 		s64	offset;
62 		bool	offset_avail;
63 		bool	outside;
64 		bool	multi_regs;
65 		bool	mem_ref;
66 	} target;
67 	union {
68 		struct {
69 			char	*raw;
70 			char	*name;
71 			u64	addr;
72 			bool	multi_regs;
73 			bool	mem_ref;
74 		} source;
75 		struct {
76 			struct ins	    ins;
77 			struct ins_operands *ops;
78 		} locked;
79 		struct {
80 			char	*raw_comment;
81 			char	*raw_func_start;
82 		} jump;
83 	};
84 };
85 
86 struct ins_ops {
87 	void (*free)(struct ins_operands *ops);
88 	int (*parse)(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
89 			struct disasm_line *dl);
90 	int (*scnprintf)(struct ins *ins, char *bf, size_t size,
91 			 struct ins_operands *ops, int max_ins_name);
92 };
93 
94 struct annotate_args {
95 	struct arch		  *arch;
96 	struct map_symbol	  ms;
97 	struct evsel		  *evsel;
98 	struct annotation_options *options;
99 	s64			  offset;
100 	char			  *line;
101 	int			  line_nr;
102 	char			  *fileloc;
103 };
104 
105 struct arch *arch__find(const char *name);
106 bool arch__is(struct arch *arch, const char *name);
107 
108 struct ins_ops *ins__find(struct arch *arch, const char *name, struct disasm_line *dl);
109 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
110 		   struct ins_operands *ops, int max_ins_name);
111 
112 bool ins__is_call(const struct ins *ins);
113 bool ins__is_jump(const struct ins *ins);
114 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
115 bool ins__is_nop(const struct ins *ins);
116 bool ins__is_ret(const struct ins *ins);
117 bool ins__is_lock(const struct ins *ins);
118 
119 struct disasm_line *disasm_line__new(struct annotate_args *args);
120 void disasm_line__free(struct disasm_line *dl);
121 
122 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size,
123 			   bool raw, int max_ins_name);
124 
125 int symbol__disassemble(struct symbol *sym, struct annotate_args *args);
126 
127 #endif /* __PERF_UTIL_DISASM_H */
128