/src/binutils-gdb/opcodes/loongarch-dis.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* LoongArch opcode support. |
2 | | Copyright (C) 2021-2023 Free Software Foundation, Inc. |
3 | | Contributed by Loongson Ltd. |
4 | | |
5 | | This file is part of the GNU opcodes library. |
6 | | |
7 | | This library is free software; you can redistribute it and/or modify |
8 | | it under the terms of the GNU General Public License as published by |
9 | | the Free Software Foundation; either version 3, or (at your option) |
10 | | any later version. |
11 | | |
12 | | It is distributed in the hope that it will be useful, but WITHOUT |
13 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
14 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
15 | | License for more details. |
16 | | |
17 | | You should have received a copy of the GNU General Public License |
18 | | along with this program; see the file COPYING3. If not, |
19 | | see <http://www.gnu.org/licenses/>. */ |
20 | | |
21 | | #include "sysdep.h" |
22 | | #include "disassemble.h" |
23 | | #include "opintl.h" |
24 | | #include "opcode/loongarch.h" |
25 | | #include "libiberty.h" |
26 | | #include <stdlib.h> |
27 | | |
28 | | static const struct loongarch_opcode * |
29 | | get_loongarch_opcode_by_binfmt (insn_t insn) |
30 | 66.8k | { |
31 | 66.8k | const struct loongarch_opcode *it; |
32 | 66.8k | struct loongarch_ase *ase; |
33 | 66.8k | size_t i; |
34 | 802k | for (ase = loongarch_ASEs; ase->enabled; ase++) |
35 | 760k | { |
36 | 760k | if (!*ase->enabled || (ase->include && !*ase->include) |
37 | 760k | || (ase->exclude && *ase->exclude)) |
38 | 0 | continue; |
39 | | |
40 | 760k | if (!ase->opc_htab_inited) |
41 | 30 | { |
42 | 3.29k | for (it = ase->opcodes; it->mask; it++) |
43 | 3.26k | if (!ase->opc_htab[LARCH_INSN_OPC (it->match)] |
44 | 3.26k | && it->macro == NULL) |
45 | 30 | ase->opc_htab[LARCH_INSN_OPC (it->match)] = it; |
46 | 510 | for (i = 0; i < 16; i++) |
47 | 480 | if (!ase->opc_htab[i]) |
48 | 450 | ase->opc_htab[i] = it; |
49 | 30 | ase->opc_htab_inited = 1; |
50 | 30 | } |
51 | | |
52 | 760k | it = ase->opc_htab[LARCH_INSN_OPC (insn)]; |
53 | 26.8M | for (; it->name; it++) |
54 | 26.0M | if ((insn & it->mask) == it->match && it->mask |
55 | 26.0M | && !(it->include && !*it->include) |
56 | 26.0M | && !(it->exclude && *it->exclude)) |
57 | 24.9k | return it; |
58 | 760k | } |
59 | 41.9k | return NULL; |
60 | 66.8k | } |
61 | | |
62 | | static const char *const *loongarch_r_disname = NULL; |
63 | | static const char *const *loongarch_f_disname = NULL; |
64 | | static const char *const *loongarch_fc_disname = NULL; |
65 | | static const char *const *loongarch_c_disname = NULL; |
66 | | static const char *const *loongarch_cr_disname = NULL; |
67 | | static const char *const *loongarch_v_disname = NULL; |
68 | | static const char *const *loongarch_x_disname = NULL; |
69 | | |
70 | | static void |
71 | | set_default_loongarch_dis_options (void) |
72 | 2 | { |
73 | 2 | LARCH_opts.ase_ilp32 = 1; |
74 | 2 | LARCH_opts.ase_lp64 = 1; |
75 | 2 | LARCH_opts.ase_sf = 1; |
76 | 2 | LARCH_opts.ase_df = 1; |
77 | 2 | LARCH_opts.ase_lsx = 1; |
78 | 2 | LARCH_opts.ase_lasx = 1; |
79 | | |
80 | 2 | loongarch_r_disname = loongarch_r_lp64_name; |
81 | 2 | loongarch_f_disname = loongarch_f_lp64_name; |
82 | 2 | loongarch_fc_disname = loongarch_fc_normal_name; |
83 | 2 | loongarch_c_disname = loongarch_c_normal_name; |
84 | 2 | loongarch_cr_disname = loongarch_cr_normal_name; |
85 | 2 | loongarch_v_disname = loongarch_v_normal_name; |
86 | 2 | loongarch_x_disname = loongarch_x_normal_name; |
87 | 2 | } |
88 | | |
89 | | static int |
90 | | parse_loongarch_dis_option (const char *option) |
91 | 0 | { |
92 | 0 | if (strcmp (option, "numeric") == 0) |
93 | 0 | { |
94 | 0 | loongarch_r_disname = loongarch_r_normal_name; |
95 | 0 | loongarch_f_disname = loongarch_f_normal_name; |
96 | 0 | } |
97 | 0 | return -1; |
98 | 0 | } |
99 | | |
100 | | static int |
101 | | parse_loongarch_dis_options (const char *opts_in) |
102 | 2 | { |
103 | 2 | set_default_loongarch_dis_options (); |
104 | | |
105 | 2 | if (opts_in == NULL) |
106 | 2 | return 0; |
107 | | |
108 | 0 | char *opts, *opt, *opt_end; |
109 | 0 | opts = xmalloc (strlen (opts_in) + 1); |
110 | 0 | strcpy (opts, opts_in); |
111 | |
|
112 | 0 | for (opt = opt_end = opts; opt_end != NULL; opt = opt_end + 1) |
113 | 0 | { |
114 | 0 | if ((opt_end = strchr (opt, ',')) != NULL) |
115 | 0 | *opt_end = 0; |
116 | 0 | if (parse_loongarch_dis_option (opt) != 0) |
117 | 0 | return -1; |
118 | 0 | } |
119 | 0 | free (opts); |
120 | 0 | return 0; |
121 | 0 | } |
122 | | |
123 | | static int32_t |
124 | | dis_one_arg (char esc1, char esc2, const char *bit_field, |
125 | | const char *arg ATTRIBUTE_UNUSED, void *context) |
126 | 94.7k | { |
127 | 94.7k | static int need_comma = 0; |
128 | 94.7k | struct disassemble_info *info = context; |
129 | 94.7k | insn_t insn = *(insn_t *) info->private_data; |
130 | 94.7k | int32_t imm, u_imm; |
131 | | |
132 | 94.7k | if (esc1) |
133 | 69.7k | { |
134 | 69.7k | if (need_comma) |
135 | 44.8k | info->fprintf_func (info->stream, ", "); |
136 | 69.7k | need_comma = 1; |
137 | 69.7k | imm = loongarch_decode_imm (bit_field, insn, 1); |
138 | 69.7k | u_imm = loongarch_decode_imm (bit_field, insn, 0); |
139 | 69.7k | } |
140 | | |
141 | 94.7k | switch (esc1) |
142 | 94.7k | { |
143 | 39.9k | case 'r': |
144 | 39.9k | info->fprintf_func (info->stream, "%s", loongarch_r_disname[u_imm]); |
145 | 39.9k | break; |
146 | 1.71k | case 'f': |
147 | 1.71k | switch (esc2) |
148 | 1.71k | { |
149 | 0 | case 'c': |
150 | 0 | info->fprintf_func (info->stream, "%s", loongarch_fc_disname[u_imm]); |
151 | 0 | break; |
152 | 1.71k | default: |
153 | 1.71k | info->fprintf_func (info->stream, "%s", loongarch_f_disname[u_imm]); |
154 | 1.71k | } |
155 | 1.71k | break; |
156 | 1.71k | case 'c': |
157 | 950 | switch (esc2) |
158 | 950 | { |
159 | 0 | case 'r': |
160 | 0 | info->fprintf_func (info->stream, "%s", loongarch_cr_disname[u_imm]); |
161 | 0 | break; |
162 | 950 | default: |
163 | 950 | info->fprintf_func (info->stream, "%s", loongarch_c_disname[u_imm]); |
164 | 950 | } |
165 | 950 | break; |
166 | 1.41k | case 'v': |
167 | 1.41k | info->fprintf_func (info->stream, "%s", loongarch_v_disname[u_imm]); |
168 | 1.41k | break; |
169 | 1.46k | case 'x': |
170 | 1.46k | info->fprintf_func (info->stream, "%s", loongarch_x_disname[u_imm]); |
171 | 1.46k | break; |
172 | 6.72k | case 'u': |
173 | 6.72k | info->fprintf_func (info->stream, "0x%x", u_imm); |
174 | 6.72k | break; |
175 | 17.6k | case 's': |
176 | 17.6k | if (imm == 0) |
177 | 1.08k | info->fprintf_func (info->stream, "%d", imm); |
178 | 16.5k | else |
179 | 16.5k | info->fprintf_func (info->stream, "%d(0x%x)", imm, u_imm); |
180 | 17.6k | switch (esc2) |
181 | 17.6k | { |
182 | 9.57k | case 'b': |
183 | 9.57k | info->insn_type = dis_branch; |
184 | 9.57k | info->target += imm; |
185 | 17.6k | } |
186 | 17.6k | break; |
187 | 24.9k | case '\0': |
188 | 24.9k | need_comma = 0; |
189 | 94.7k | } |
190 | 94.7k | return 0; |
191 | 94.7k | } |
192 | | |
193 | | static void |
194 | | disassemble_one (insn_t insn, struct disassemble_info *info) |
195 | 66.8k | { |
196 | 66.8k | const struct loongarch_opcode *opc = get_loongarch_opcode_by_binfmt (insn); |
197 | | |
198 | | #ifdef LOONGARCH_DEBUG |
199 | | char have_space[32] = { 0 }; |
200 | | insn_t t; |
201 | | int i; |
202 | | const char *t_f = opc ? opc->format : NULL; |
203 | | if (t_f) |
204 | | while (*t_f) |
205 | | { |
206 | | while (('a' <= t_f[0] && t_f[0] <= 'z') |
207 | | || ('A' <= t_f[0] && t_f[0] <= 'Z') |
208 | | || t_f[0] == ',') |
209 | | t_f++; |
210 | | while (1) |
211 | | { |
212 | | i = strtol (t_f, &t_f, 10); |
213 | | have_space[i] = 1; |
214 | | t_f++; /* ':' */ |
215 | | i += strtol (t_f, &t_f, 10); |
216 | | have_space[i] = 1; |
217 | | if (t_f[0] == '|') |
218 | | t_f++; |
219 | | else |
220 | | break; |
221 | | } |
222 | | if (t_f[0] == '<') |
223 | | t_f += 2; /* '<' '<' */ |
224 | | strtol (t_f, &t_f, 10); |
225 | | } |
226 | | |
227 | | have_space[28] = 1; |
228 | | have_space[0] = 0; |
229 | | t = ~((insn_t) -1 >> 1); |
230 | | for (i = 31; 0 <= i; i--) |
231 | | { |
232 | | if (t & insn) |
233 | | info->fprintf_func (info->stream, "1"); |
234 | | else |
235 | | info->fprintf_func (info->stream, "0"); |
236 | | if (have_space[i]) |
237 | | info->fprintf_func (info->stream, " "); |
238 | | t = t >> 1; |
239 | | } |
240 | | info->fprintf_func (info->stream, "\t"); |
241 | | #endif |
242 | | |
243 | 66.8k | if (!opc) |
244 | 41.9k | { |
245 | 41.9k | info->insn_type = dis_noninsn; |
246 | 41.9k | info->fprintf_func (info->stream, "0x%08x", insn); |
247 | 41.9k | return; |
248 | 41.9k | } |
249 | | |
250 | 24.9k | info->insn_type = dis_nonbranch; |
251 | 24.9k | info->fprintf_func (info->stream, "%-12s", opc->name); |
252 | | |
253 | 24.9k | { |
254 | 24.9k | char *fake_args = xmalloc (strlen (opc->format) + 1); |
255 | 24.9k | const char *fake_arg_strs[MAX_ARG_NUM_PLUS_2]; |
256 | 24.9k | strcpy (fake_args, opc->format); |
257 | 24.9k | if (0 < loongarch_split_args_by_comma (fake_args, fake_arg_strs)) |
258 | 24.9k | info->fprintf_func (info->stream, "\t"); |
259 | 24.9k | info->private_data = &insn; |
260 | 24.9k | loongarch_foreach_args (opc->format, fake_arg_strs, dis_one_arg, info); |
261 | 24.9k | free (fake_args); |
262 | 24.9k | } |
263 | | |
264 | 24.9k | if (info->insn_type == dis_branch || info->insn_type == dis_condbranch |
265 | 24.9k | /* Someother if we have extra info to print. */) |
266 | 9.57k | info->fprintf_func (info->stream, "\t#"); |
267 | | |
268 | 24.9k | if (info->insn_type == dis_branch || info->insn_type == dis_condbranch) |
269 | 9.57k | { |
270 | 9.57k | info->fprintf_func (info->stream, " "); |
271 | 9.57k | info->print_address_func (info->target, info); |
272 | 9.57k | } |
273 | 24.9k | } |
274 | | |
275 | | int |
276 | | print_insn_loongarch (bfd_vma memaddr, struct disassemble_info *info) |
277 | 66.9k | { |
278 | 66.9k | insn_t insn; |
279 | 66.9k | int status; |
280 | | |
281 | 66.9k | static int not_init_yet = 1; |
282 | 66.9k | if (not_init_yet) |
283 | 2 | { |
284 | 2 | parse_loongarch_dis_options (info->disassembler_options); |
285 | 2 | not_init_yet = 0; |
286 | 2 | } |
287 | | |
288 | 66.9k | info->bytes_per_chunk = 4; |
289 | 66.9k | info->bytes_per_line = 4; |
290 | 66.9k | info->display_endian = BFD_ENDIAN_LITTLE; |
291 | 66.9k | info->insn_info_valid = 1; |
292 | 66.9k | info->target = memaddr; |
293 | | |
294 | 66.9k | if ((status = info->read_memory_func (memaddr, (bfd_byte *) &insn, |
295 | 66.9k | sizeof (insn), info)) != 0) |
296 | 103 | { |
297 | 103 | info->memory_error_func (status, memaddr, info); |
298 | 103 | return -1; /* loongarch_insn_length (0); */ |
299 | 103 | } |
300 | | |
301 | 66.8k | disassemble_one (insn, info); |
302 | | |
303 | 66.8k | return loongarch_insn_length (insn); |
304 | 66.9k | } |
305 | | |
306 | | void |
307 | | print_loongarch_disassembler_options (FILE *stream) |
308 | 0 | { |
309 | 0 | fprintf (stream, _("\n\ |
310 | 0 | The following LoongArch disassembler options are supported for use\n\ |
311 | 0 | with the -M switch (multiple options should be separated by commas):\n")); |
312 | |
|
313 | 0 | fprintf (stream, _("\n\ |
314 | 0 | numeric Print numeric register names, rather than ABI names.\n")); |
315 | 0 | fprintf (stream, _("\n")); |
316 | 0 | } |
317 | | |
318 | | int |
319 | | loongarch_parse_dis_options (const char *opts_in) |
320 | 0 | { |
321 | 0 | return parse_loongarch_dis_options (opts_in); |
322 | 0 | } |
323 | | |
324 | | static void |
325 | | my_print_address_func (bfd_vma addr, struct disassemble_info *dinfo) |
326 | 0 | { |
327 | 0 | dinfo->fprintf_func (dinfo->stream, "0x%llx", (long long) addr); |
328 | 0 | } |
329 | | |
330 | | void |
331 | | loongarch_disassemble_one (int64_t pc, insn_t insn, |
332 | | int (*fprintf_func) (void *stream, |
333 | | const char *format, ...), |
334 | | void *stream) |
335 | 0 | { |
336 | 0 | static struct disassemble_info my_disinfo = |
337 | 0 | { |
338 | 0 | .print_address_func = my_print_address_func, |
339 | 0 | }; |
340 | 0 | static int not_init_yet = 1; |
341 | 0 | if (not_init_yet) |
342 | 0 | { |
343 | 0 | loongarch_parse_dis_options (NULL); |
344 | 0 | not_init_yet = 0; |
345 | 0 | } |
346 | |
|
347 | 0 | my_disinfo.fprintf_func = fprintf_func; |
348 | 0 | my_disinfo.stream = stream; |
349 | 0 | my_disinfo.target = pc; |
350 | 0 | disassemble_one (insn, &my_disinfo); |
351 | 0 | } |