/src/binutils-gdb/opcodes/csky-dis.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* C-SKY disassembler. |
2 | | Copyright (C) 1988-2023 Free Software Foundation, Inc. |
3 | | Contributed by C-SKY Microsystems and Mentor Graphics. |
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; if not, write to the Free Software |
19 | | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | | MA 02110-1301, USA. */ |
21 | | |
22 | | #include "sysdep.h" |
23 | | #include "config.h" |
24 | | #include <stdio.h> |
25 | | #include <stdint.h> |
26 | | #include <elf/csky.h> |
27 | | #include "disassemble.h" |
28 | | #include "elf-bfd.h" |
29 | | #include "opcode/csky.h" |
30 | | #include "libiberty.h" |
31 | | #include "csky-opc.h" |
32 | | #include "floatformat.h" |
33 | | |
34 | 408k | #define CSKY_INST_TYPE unsigned long |
35 | 125M | #define HAS_SUB_OPERAND (unsigned int)0xffffffff |
36 | 273 | #define CSKY_DEFAULT_ISA 0xffffffff |
37 | | |
38 | | enum sym_type |
39 | | { |
40 | | CUR_TEXT, |
41 | | CUR_DATA |
42 | | }; |
43 | | |
44 | | struct csky_dis_info |
45 | | { |
46 | | /* Mem to disassemble. */ |
47 | | bfd_vma mem; |
48 | | /* Disassemble info. */ |
49 | | disassemble_info *info; |
50 | | /* Opcode information. */ |
51 | | struct csky_opcode_info const *opinfo; |
52 | | uint64_t isa; |
53 | | /* The value of operand to show. */ |
54 | | int value; |
55 | | /* Whether to look up/print a symbol name. */ |
56 | | int need_output_symbol; |
57 | | } dis_info; |
58 | | |
59 | | |
60 | | enum sym_type last_type; |
61 | | int last_map_sym = 1; |
62 | | bfd_vma last_map_addr = 0; |
63 | | int using_abi = 0; |
64 | | |
65 | | /* Only for objdump tool. */ |
66 | 816k | #define INIT_MACH_FLAG 0xffffffff |
67 | 1.22M | #define BINARY_MACH_FLAG 0x0 |
68 | | |
69 | | static unsigned int mach_flag = INIT_MACH_FLAG; |
70 | | |
71 | | static void |
72 | | print_insn_data (bfd_vma pc ATTRIBUTE_UNUSED, |
73 | | struct disassemble_info *info, |
74 | | long given) |
75 | 0 | { |
76 | 0 | switch (info->bytes_per_chunk) |
77 | 0 | { |
78 | 0 | case 1: |
79 | 0 | info->fprintf_func (info->stream, ".byte\t0x%02lx", given); |
80 | 0 | break; |
81 | 0 | case 2: |
82 | 0 | info->fprintf_func (info->stream, ".short\t0x%04lx", given); |
83 | 0 | break; |
84 | 0 | case 4: |
85 | 0 | info->fprintf_func (info->stream, ".long\t0x%08lx", given); |
86 | 0 | break; |
87 | 0 | default: |
88 | 0 | abort (); |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | | static int |
93 | | get_sym_code_type (struct disassemble_info *info, |
94 | | int n, |
95 | | enum sym_type *sym_type) |
96 | 0 | { |
97 | 0 | const char *name; |
98 | 0 | name = bfd_asymbol_name (info->symtab[n]); |
99 | 0 | if (name[0] == '$' && (name[1] == 't' || name[1] == 'd') |
100 | 0 | && (name[2] == 0 || name[2] == '.')) |
101 | 0 | { |
102 | 0 | *sym_type = ((name[1] == 't') ? CUR_TEXT : CUR_DATA); |
103 | 0 | return true; |
104 | 0 | } |
105 | 0 | return false; |
106 | 0 | } |
107 | | |
108 | | static int |
109 | | csky_get_operand_mask (struct operand const *oprnd) |
110 | 124M | { |
111 | 124M | int mask = 0; |
112 | 124M | if (oprnd->mask == HAS_SUB_OPERAND) |
113 | 5.29M | { |
114 | 5.29M | struct soperand *sop = (struct soperand *)oprnd; |
115 | 5.29M | mask |= csky_get_operand_mask (&sop->subs[0]); |
116 | 5.29M | mask |= csky_get_operand_mask (&sop->subs[1]); |
117 | 5.29M | return mask; |
118 | 5.29M | } |
119 | 119M | return oprnd->mask; |
120 | 124M | } |
121 | | |
122 | | static int |
123 | | csky_get_mask (struct csky_opcode_info const *pinfo) |
124 | 206M | { |
125 | 206M | int i = 0; |
126 | 206M | int mask = 0; |
127 | | /* List type. */ |
128 | 206M | if (pinfo->operand_num == -1) |
129 | 685k | mask |= csky_get_operand_mask (&pinfo->oprnd.oprnds[i]); |
130 | 205M | else |
131 | 319M | for (; i < pinfo->operand_num; i++) |
132 | 113M | mask |= csky_get_operand_mask (&pinfo->oprnd.oprnds[i]); |
133 | | |
134 | 206M | mask = ~mask; |
135 | 206M | return mask; |
136 | 206M | } |
137 | | |
138 | | static unsigned int |
139 | | csky_chars_to_number (unsigned char * buf, int n) |
140 | 38.7k | { |
141 | 38.7k | int i; |
142 | 38.7k | unsigned int val = 0; |
143 | | |
144 | 38.7k | if (dis_info.info->endian == BFD_ENDIAN_BIG) |
145 | 190k | for (i = 0; i < n; i++) |
146 | 152k | val = val << 8 | buf[i]; |
147 | 697 | else |
148 | 3.48k | for (i = n - 1; i >= 0; i--) |
149 | 2.78k | val = val << 8 | buf[i]; |
150 | 38.7k | return val; |
151 | 38.7k | } |
152 | | |
153 | | static struct csky_opcode const *g_opcodeP; |
154 | | |
155 | | static struct csky_opcode const * |
156 | | csky_find_inst_info (struct csky_opcode_info const **pinfo, |
157 | | CSKY_INST_TYPE inst, int length) |
158 | 408k | { |
159 | 408k | int i; |
160 | 408k | unsigned int mask; |
161 | 408k | struct csky_opcode const *p; |
162 | | |
163 | 408k | p = g_opcodeP; |
164 | 117M | while (p->mnemonic) |
165 | 117M | { |
166 | 117M | if (!(p->isa_flag16 & dis_info.isa) |
167 | 117M | && !(p->isa_flag32 & dis_info.isa)) |
168 | 14.0M | { |
169 | 14.0M | p++; |
170 | 14.0M | continue; |
171 | 14.0M | } |
172 | | |
173 | | /* Get the opcode mask. */ |
174 | 309M | for (i = 0; i < OP_TABLE_NUM; i++) |
175 | 206M | if (length == 2) |
176 | 165M | { |
177 | 165M | mask = csky_get_mask (&p->op16[i]); |
178 | 165M | if (mask != 0 && (inst & mask) == p->op16[i].opcode) |
179 | 353k | { |
180 | 353k | *pinfo = &p->op16[i]; |
181 | 353k | g_opcodeP = p; |
182 | 353k | return p; |
183 | 353k | } |
184 | 165M | } |
185 | 40.5M | else if (length == 4) |
186 | 40.5M | { |
187 | 40.5M | mask = csky_get_mask (&p->op32[i]); |
188 | 40.5M | if (mask != 0 |
189 | 40.5M | && ((unsigned long)(inst & mask) |
190 | 40.5M | == (unsigned long)p->op32[i].opcode)) |
191 | 15.0k | { |
192 | 15.0k | *pinfo = &p->op32[i]; |
193 | 15.0k | g_opcodeP = p; |
194 | 15.0k | return p; |
195 | 15.0k | } |
196 | 40.5M | } |
197 | 102M | p++; |
198 | 102M | } |
199 | | |
200 | 40.4k | return NULL; |
201 | 408k | } |
202 | | |
203 | | static bool |
204 | | is_extern_symbol (struct disassemble_info *info, int addr) |
205 | 39.5k | { |
206 | 39.5k | unsigned int rel_count = 0; |
207 | | |
208 | 39.5k | if (info->section == NULL) |
209 | 0 | return 0; |
210 | 39.5k | if ((info->section->flags & SEC_RELOC) != 0) /* Fit .o file. */ |
211 | 0 | { |
212 | 0 | struct reloc_cache_entry *pt = info->section->relocation; |
213 | 0 | for (; rel_count < info->section->reloc_count; rel_count++, pt++) |
214 | 0 | if ((long unsigned int)addr == pt->address) |
215 | 0 | return true; |
216 | 0 | return false; |
217 | 0 | } |
218 | 39.5k | return false; |
219 | 39.5k | } |
220 | | |
221 | | |
222 | | /* Suppress printing of mapping symbols emitted by the assembler to mark |
223 | | the beginning of code and data sequences. */ |
224 | | |
225 | | bool |
226 | | csky_symbol_is_valid (asymbol *sym, |
227 | | struct disassemble_info *info ATTRIBUTE_UNUSED) |
228 | 0 | { |
229 | 0 | const char *name; |
230 | |
|
231 | 0 | if (sym == NULL) |
232 | 0 | return false; |
233 | 0 | name = bfd_asymbol_name (sym); |
234 | 0 | return name && *name != '$'; |
235 | 0 | } |
236 | | |
237 | | disassembler_ftype |
238 | | csky_get_disassembler (bfd *abfd) |
239 | 273 | { |
240 | 273 | obj_attribute *attr; |
241 | 273 | const char *sec_name = NULL; |
242 | 273 | if (!abfd || bfd_get_flavour (abfd) != bfd_target_elf_flavour) |
243 | 0 | dis_info.isa = CSKY_DEFAULT_ISA; |
244 | 273 | else |
245 | 273 | { |
246 | 273 | mach_flag = elf_elfheader (abfd)->e_flags; |
247 | | |
248 | 273 | sec_name = get_elf_backend_data (abfd)->obj_attrs_section; |
249 | | /* Skip any input that hasn't attribute section. |
250 | | This enables to link object files without attribute section with |
251 | | any others. */ |
252 | 273 | if (bfd_get_section_by_name (abfd, sec_name) != NULL) |
253 | 0 | { |
254 | 0 | attr = elf_known_obj_attributes_proc (abfd); |
255 | 0 | dis_info.isa = attr[Tag_CSKY_ISA_EXT_FLAGS].i; |
256 | 0 | dis_info.isa <<= 32; |
257 | 0 | dis_info.isa |= attr[Tag_CSKY_ISA_FLAGS].i; |
258 | 0 | } |
259 | 273 | else |
260 | 273 | dis_info.isa = CSKY_DEFAULT_ISA; |
261 | 273 | } |
262 | | |
263 | 273 | return print_insn_csky; |
264 | 273 | } |
265 | | |
266 | | /* Parse the string of disassembler options. */ |
267 | | static void |
268 | | parse_csky_dis_options (const char *opts_in) |
269 | 0 | { |
270 | 0 | char *opts = xstrdup (opts_in); |
271 | 0 | char *opt = opts; |
272 | 0 | char *opt_end = opts; |
273 | |
|
274 | 0 | for (; opt_end != NULL; opt = opt_end + 1) |
275 | 0 | { |
276 | 0 | if ((opt_end = strchr (opt, ',')) != NULL) |
277 | 0 | *opt_end = 0; |
278 | 0 | if (strcmp (opt, "abi-names") == 0) |
279 | 0 | using_abi = 1; |
280 | 0 | else |
281 | 0 | fprintf (stderr, |
282 | 0 | "unrecognized disassembler option: %s", opt); |
283 | 0 | } |
284 | 0 | } |
285 | | |
286 | | /* Get general register name. */ |
287 | | static const char * |
288 | | get_gr_name (int regno) |
289 | 430k | { |
290 | 430k | return csky_get_general_reg_name (mach_flag, regno, using_abi); |
291 | 430k | } |
292 | | |
293 | | /* Get control register name. */ |
294 | | static const char * |
295 | | get_cr_name (unsigned int regno, int bank) |
296 | 3.61k | { |
297 | 3.61k | return csky_get_control_reg_name (mach_flag, bank, regno, using_abi); |
298 | 3.61k | } |
299 | | |
300 | | static int |
301 | | csky_output_operand (char *str, struct operand const *oprnd, |
302 | | CSKY_INST_TYPE inst, int reloc ATTRIBUTE_UNUSED) |
303 | 686k | { |
304 | 686k | int ret = 0;; |
305 | 686k | int bit = 0; |
306 | 686k | int result = 0; |
307 | 686k | bfd_vma value; |
308 | 686k | int mask = oprnd->mask; |
309 | 686k | int max = 0; |
310 | 686k | char buf[128]; |
311 | | |
312 | | /* Get operand value with mask. */ |
313 | 686k | value = inst & mask; |
314 | 6.41M | for (; mask; mask >>= 1, value >>=1) |
315 | 5.72M | if (mask & 0x1) |
316 | 3.21M | { |
317 | 3.21M | result |= ((value & 0x1) << bit); |
318 | 3.21M | max |= (1 << bit); |
319 | 3.21M | bit++; |
320 | 3.21M | } |
321 | 686k | value = result; |
322 | | |
323 | | /* Here is general instructions that have no reloc. */ |
324 | 686k | switch (oprnd->type) |
325 | 686k | { |
326 | 3.65k | case OPRND_TYPE_CTRLREG: |
327 | 3.65k | if (IS_CSKY_V1(mach_flag) && ((value & 0x1f) == 0x1f)) |
328 | 41 | return -1; |
329 | 3.61k | strcat (str, get_cr_name((value & 0x1f), (value >> 5))); |
330 | 3.61k | break; |
331 | 18.8k | case OPRND_TYPE_DUMMY_REG: |
332 | 18.8k | mask = dis_info.opinfo->oprnd.oprnds[0].mask; |
333 | 18.8k | value = inst & mask; |
334 | 94.0k | for (; mask; mask >>= 1, value >>=1) |
335 | 75.2k | if (mask & 0x1) |
336 | 75.2k | { |
337 | 75.2k | result |= ((value & 0x1) << bit); |
338 | 75.2k | bit++; |
339 | 75.2k | } |
340 | 18.8k | value = result; |
341 | 18.8k | strcat (str, get_gr_name (value)); |
342 | 18.8k | break; |
343 | 191k | case OPRND_TYPE_GREG0_7: |
344 | 366k | case OPRND_TYPE_GREG0_15: |
345 | 366k | case OPRND_TYPE_GREG16_31: |
346 | 373k | case OPRND_TYPE_REGnsplr: |
347 | 385k | case OPRND_TYPE_AREG: |
348 | 385k | strcat (str, get_gr_name (value)); |
349 | 385k | break; |
350 | 1.50k | case OPRND_TYPE_CPREG: |
351 | 1.50k | sprintf (buf, "cpr%d", (int)value); |
352 | 1.50k | strcat (str, buf); |
353 | 1.50k | break; |
354 | 490 | case OPRND_TYPE_FREG: |
355 | 490 | sprintf (buf, "fr%d", (int)value); |
356 | 490 | strcat (str, buf); |
357 | 490 | break; |
358 | 1.21k | case OPRND_TYPE_VREG: |
359 | 1.21k | dis_info.value = value; |
360 | 1.21k | sprintf (buf, "vr%d", (int)value); |
361 | 1.21k | strcat (str, buf); |
362 | 1.21k | break; |
363 | 1.08k | case OPRND_TYPE_CPCREG: |
364 | 1.08k | sprintf (buf, "cpcr%d", (int)value); |
365 | 1.08k | strcat (str, buf); |
366 | 1.08k | break; |
367 | 999 | case OPRND_TYPE_CPIDX: |
368 | 999 | sprintf (buf, "cp%d", (int)value); |
369 | 999 | strcat (str, buf); |
370 | 999 | break; |
371 | 35 | case OPRND_TYPE_IMM2b_JMPIX: |
372 | 35 | value = (value + 2) << 3; |
373 | 35 | sprintf (buf, "%d", (int)value); |
374 | 35 | strcat (str, buf); |
375 | 35 | break; |
376 | 71.5k | case OPRND_TYPE_IMM_LDST: |
377 | 71.5k | case OPRND_TYPE_IMM_FLDST: |
378 | 71.5k | value <<= oprnd->shift; |
379 | 71.5k | sprintf (buf, "0x%x", (unsigned int)value); |
380 | 71.5k | strcat (str, buf); |
381 | 71.5k | break; |
382 | 910 | case OPRND_TYPE_IMM7b_LS2: |
383 | 6.44k | case OPRND_TYPE_IMM8b_LS2: |
384 | 6.44k | sprintf (buf, "%d", (int)(value << 2)); |
385 | 6.44k | strcat (str, buf); |
386 | 6.44k | ret = 0; |
387 | 6.44k | break; |
388 | 567 | case OPRND_TYPE_IMM5b_BMASKI: |
389 | 567 | if ((value != 0) && (value > 31 || value < 8)) |
390 | 135 | { |
391 | 135 | ret = -1; |
392 | 135 | break; |
393 | 135 | } |
394 | 432 | sprintf (buf, "%d", (int)value); |
395 | 432 | strcat (str, buf); |
396 | 432 | ret = 0; |
397 | 432 | break; |
398 | 2.25k | case OPRND_TYPE_IMM5b_1_31: |
399 | 2.25k | if (value > 31 || value < 1) |
400 | 103 | { |
401 | 103 | ret = -1; |
402 | 103 | break; |
403 | 103 | } |
404 | 2.15k | sprintf (buf, "%d", (int)value); |
405 | 2.15k | strcat (str, buf); |
406 | 2.15k | ret = 0; |
407 | 2.15k | break; |
408 | 760 | case OPRND_TYPE_IMM5b_7_31: |
409 | 760 | if (value > 31 || value < 7) |
410 | 248 | { |
411 | 248 | ret = -1; |
412 | 248 | break; |
413 | 248 | } |
414 | 512 | sprintf (buf, "%d", (int)value); |
415 | 512 | strcat (str, buf); |
416 | 512 | ret = 0; |
417 | 512 | break; |
418 | 23 | case OPRND_TYPE_IMM5b_VSH: |
419 | 23 | { |
420 | 23 | char num[128]; |
421 | 23 | value = ((value & 0x1) << 4) | (value >> 1); |
422 | 23 | sprintf (num, "%d", (int)value); |
423 | 23 | strcat (str, num); |
424 | 23 | ret = 0; |
425 | 23 | break; |
426 | 760 | } |
427 | 10 | case OPRND_TYPE_MSB2SIZE: |
428 | 20 | case OPRND_TYPE_LSB2SIZE: |
429 | 20 | { |
430 | 20 | static int size; |
431 | 20 | if (oprnd->type == OPRND_TYPE_MSB2SIZE) |
432 | 10 | size = value; |
433 | 10 | else |
434 | 10 | { |
435 | 10 | str[strlen (str) - 2] = '\0'; |
436 | 10 | sprintf (buf, "%d, %d", (int)(size + value), (int)value); |
437 | 10 | strcat (str, buf); |
438 | 10 | } |
439 | 20 | break; |
440 | 10 | } |
441 | 0 | case OPRND_TYPE_IMM1b: |
442 | 803 | case OPRND_TYPE_IMM2b: |
443 | 927 | case OPRND_TYPE_IMM4b: |
444 | 34.6k | case OPRND_TYPE_IMM5b: |
445 | 34.7k | case OPRND_TYPE_IMM5b_LS: |
446 | 40.3k | case OPRND_TYPE_IMM7b: |
447 | 47.0k | case OPRND_TYPE_IMM8b: |
448 | 49.1k | case OPRND_TYPE_IMM12b: |
449 | 53.4k | case OPRND_TYPE_IMM15b: |
450 | 53.5k | case OPRND_TYPE_IMM16b: |
451 | 53.5k | case OPRND_TYPE_IMM16b_MOVIH: |
452 | 55.0k | case OPRND_TYPE_IMM16b_ORI: |
453 | 55.0k | sprintf (buf, "%d", (int)value); |
454 | 55.0k | strcat (str, buf); |
455 | 55.0k | ret = 0; |
456 | 55.0k | break; |
457 | 1.01k | case OPRND_TYPE_OFF8b: |
458 | 1.02k | case OPRND_TYPE_OFF16b: |
459 | 1.02k | { |
460 | 1.02k | unsigned char ibytes[4]; |
461 | 1.02k | int shift = oprnd->shift; |
462 | 1.02k | int status; |
463 | 1.02k | unsigned int mem_val; |
464 | | |
465 | 1.02k | dis_info.info->stop_vma = 0; |
466 | | |
467 | 1.02k | value = ((dis_info.mem + (value << shift) |
468 | 1.02k | + ((IS_CSKY_V1 (mach_flag)) ? 2 : 0)) |
469 | 1.02k | & 0xfffffffc); |
470 | 1.02k | status = dis_info.info->read_memory_func (value, ibytes, 4, |
471 | 1.02k | dis_info.info); |
472 | 1.02k | if (status != 0) |
473 | 122 | { |
474 | 122 | dis_info.info->memory_error_func (status, dis_info.mem, |
475 | 122 | dis_info.info); |
476 | 122 | return -1; |
477 | 122 | } |
478 | 899 | mem_val = csky_chars_to_number (ibytes, 4); |
479 | | /* Remove [] around literal value to match ABI syntax. */ |
480 | 899 | sprintf (buf, "0x%X", mem_val); |
481 | 899 | strcat (str, buf); |
482 | | /* For jmpi/jsri, we'll try to get a symbol for the target. */ |
483 | 899 | if (dis_info.info->print_address_func && mem_val != 0) |
484 | 778 | { |
485 | 778 | dis_info.value = mem_val; |
486 | 778 | dis_info.need_output_symbol = 1; |
487 | 778 | } |
488 | 121 | else |
489 | 121 | { |
490 | 121 | sprintf (buf, "\t// from address pool at 0x%x", |
491 | 121 | (unsigned int)value); |
492 | 121 | strcat (str, buf); |
493 | 121 | } |
494 | 899 | break; |
495 | 1.02k | } |
496 | 30 | case OPRND_TYPE_BLOOP_OFF4b: |
497 | 60 | case OPRND_TYPE_BLOOP_OFF12b: |
498 | 22.3k | case OPRND_TYPE_OFF11b: |
499 | 22.7k | case OPRND_TYPE_OFF16b_LSL1: |
500 | 22.9k | case OPRND_TYPE_IMM_OFF18b: |
501 | 24.7k | case OPRND_TYPE_OFF26b: |
502 | 24.7k | { |
503 | 24.7k | int shift = oprnd->shift; |
504 | 24.7k | if (value & ((max >> 1) + 1)) |
505 | 12.1k | value |= ~max; |
506 | 24.7k | if (is_extern_symbol (dis_info.info, dis_info.mem)) |
507 | 0 | value = 0; |
508 | 24.7k | else if (IS_CSKY_V1 (mach_flag)) |
509 | 22.3k | value = dis_info.mem + 2 + (value << shift); |
510 | 2.37k | else |
511 | 2.37k | value = dis_info.mem + (value << shift); |
512 | 24.7k | dis_info.need_output_symbol = 1; |
513 | 24.7k | dis_info.value= value; |
514 | 24.7k | sprintf (buf, "0x%x", (unsigned int)value); |
515 | 24.7k | strcat (str, buf); |
516 | 24.7k | break; |
517 | 22.9k | } |
518 | 11.7k | case OPRND_TYPE_CONSTANT: |
519 | 11.7k | case OPRND_TYPE_FCONSTANT: |
520 | 11.7k | { |
521 | 11.7k | int shift = oprnd->shift; |
522 | 11.7k | bfd_byte ibytes[8]; |
523 | 11.7k | int status; |
524 | 11.7k | bfd_vma addr; |
525 | 11.7k | int nbytes; |
526 | | |
527 | 11.7k | dis_info.info->stop_vma = 0; |
528 | 11.7k | value <<= shift; |
529 | | |
530 | 11.7k | if (IS_CSKY_V1 (mach_flag)) |
531 | 6.17k | addr = (dis_info.mem + 2 + value) & 0xfffffffc; |
532 | 5.60k | else |
533 | 5.60k | addr = (dis_info.mem + value) & 0xfffffffc; |
534 | | |
535 | 11.7k | if (oprnd->type == OPRND_TYPE_FCONSTANT |
536 | 11.7k | && dis_info.opinfo->opcode != CSKYV2_INST_FLRW) |
537 | 4 | nbytes = 8; |
538 | 11.7k | else |
539 | 11.7k | nbytes = 4; |
540 | | |
541 | 11.7k | status = dis_info.info->read_memory_func (addr, ibytes, |
542 | 11.7k | nbytes, dis_info.info); |
543 | 11.7k | if (status != 0) |
544 | | /* Address out of bounds. -> lrw rx, [pc, 0ffset]. */ |
545 | 1.19k | sprintf (buf, "[pc, %d]\t// from address pool at %x", (int)value, |
546 | 1.19k | (unsigned int)addr); |
547 | 10.5k | else if (oprnd->type == OPRND_TYPE_FCONSTANT) |
548 | 0 | { |
549 | 0 | double f; |
550 | |
|
551 | 0 | if (dis_info.opinfo->opcode == CSKYV2_INST_FLRW) |
552 | | /* flrws. */ |
553 | 0 | floatformat_to_double ((dis_info.info->endian == BFD_ENDIAN_BIG |
554 | 0 | ? &floatformat_ieee_single_big |
555 | 0 | : &floatformat_ieee_single_little), |
556 | 0 | ibytes, &f); |
557 | 0 | else |
558 | 0 | floatformat_to_double ((dis_info.info->endian == BFD_ENDIAN_BIG |
559 | 0 | ? &floatformat_ieee_double_big |
560 | 0 | : &floatformat_ieee_double_little), |
561 | 0 | ibytes, &f); |
562 | 0 | sprintf (buf, "%.7g", f); |
563 | 0 | } |
564 | 10.5k | else |
565 | 10.5k | { |
566 | 10.5k | dis_info.value = addr; |
567 | 10.5k | dis_info.need_output_symbol = 1; |
568 | 10.5k | value = csky_chars_to_number (ibytes, 4); |
569 | 10.5k | sprintf (buf, "0x%x", (unsigned int) value); |
570 | 10.5k | } |
571 | | |
572 | 11.7k | strcat (str, buf); |
573 | 11.7k | break; |
574 | 11.7k | } |
575 | 35.7k | case OPRND_TYPE_ELRW_CONSTANT: |
576 | 35.7k | { |
577 | 35.7k | int shift = oprnd->shift; |
578 | 35.7k | char ibytes[4]; |
579 | 35.7k | int status; |
580 | 35.7k | bfd_vma addr; |
581 | 35.7k | dis_info.info->stop_vma = 0; |
582 | | |
583 | 35.7k | value = 0x80 + ((~value) & 0x7f); |
584 | | |
585 | 35.7k | value = value << shift; |
586 | 35.7k | addr = (dis_info.mem + value) & 0xfffffffc; |
587 | | |
588 | 35.7k | status = dis_info.info->read_memory_func (addr, (bfd_byte *)ibytes, |
589 | 35.7k | 4, dis_info.info); |
590 | 35.7k | if (status != 0) |
591 | | /* Address out of bounds. -> lrw rx, [pc, 0ffset]. */ |
592 | 8.48k | sprintf (buf, "[pc, %d]\t// from address pool at %x", (int) value, |
593 | 8.48k | (unsigned int)addr); |
594 | 27.2k | else |
595 | 27.2k | { |
596 | 27.2k | dis_info.value = addr; |
597 | 27.2k | value = csky_chars_to_number ((unsigned char *)ibytes, 4); |
598 | 27.2k | dis_info.need_output_symbol = 1; |
599 | 27.2k | sprintf (buf, "0x%x", (unsigned int)value); |
600 | 27.2k | } |
601 | | |
602 | 35.7k | strcat (str, buf); |
603 | 35.7k | break; |
604 | 11.7k | } |
605 | 3 | case OPRND_TYPE_SFLOAT: |
606 | 13 | case OPRND_TYPE_DFLOAT: |
607 | 13 | { |
608 | | /* This is for fmovis/fmovid, which have an internal 13-bit |
609 | | encoding that they convert to single/double precision |
610 | | (respectively). We'll convert the 13-bit encoding to an IEEE |
611 | | double and then to host double format to print it. |
612 | | Sign bit: bit 20. |
613 | | 4-bit exponent: bits 19:16, biased by 11. |
614 | | 8-bit mantissa: split between 24:21 and 7:4. */ |
615 | 13 | uint64_t imm4; |
616 | 13 | uint64_t imm8; |
617 | 13 | uint64_t dbnum; |
618 | 13 | unsigned char valbytes[8]; |
619 | 13 | double fvalue; |
620 | | |
621 | 13 | imm4 = ((inst >> 16) & 0xf); |
622 | 13 | imm4 = (uint64_t)(1023 - (imm4 - 11)) << 52; |
623 | | |
624 | 13 | imm8 = (uint64_t)((inst >> 4) & 0xf) << 44; |
625 | 13 | imm8 |= (uint64_t)((inst >> 21) & 0xf) << 48; |
626 | | |
627 | 13 | dbnum = (uint64_t)((inst >> 20) & 1) << 63; |
628 | 13 | dbnum |= imm4 | imm8; |
629 | | |
630 | | /* Do this a byte at a time so we don't have to |
631 | | worry about the host's endianness. */ |
632 | 13 | valbytes[0] = dbnum & 0xff; |
633 | 13 | valbytes[1] = (dbnum >> 8) & 0xff; |
634 | 13 | valbytes[2] = (dbnum >> 16) & 0xff; |
635 | 13 | valbytes[3] = (dbnum >> 24) & 0xff; |
636 | 13 | valbytes[4] = (dbnum >> 32) & 0xff; |
637 | 13 | valbytes[5] = (dbnum >> 40) & 0xff; |
638 | 13 | valbytes[6] = (dbnum >> 48) & 0xff; |
639 | 13 | valbytes[7] = (dbnum >> 56) & 0xff; |
640 | | |
641 | 13 | floatformat_to_double (&floatformat_ieee_double_little, valbytes, |
642 | 13 | &fvalue); |
643 | | |
644 | 13 | sprintf (buf, "%.7g", fvalue); |
645 | 13 | strcat (str, buf); |
646 | 13 | break; |
647 | 3 | } |
648 | 0 | case OPRND_TYPE_HFLOAT_FMOVI: |
649 | 0 | case OPRND_TYPE_SFLOAT_FMOVI: |
650 | 0 | { |
651 | 0 | int imm4; |
652 | 0 | int imm8; |
653 | 0 | imm4 = ((inst >> 16) & 0xf); |
654 | 0 | imm4 = (138 - imm4) << 23; |
655 | |
|
656 | 0 | imm8 = ((inst >> 8) & 0x3); |
657 | 0 | imm8 |= (((inst >> 20) & 0x3f) << 2); |
658 | 0 | imm8 <<= 15; |
659 | |
|
660 | 0 | value = ((inst >> 5) & 1) << 31; |
661 | 0 | value |= imm4 | imm8; |
662 | |
|
663 | 0 | imm4 = 138 - (imm4 >> 23); |
664 | 0 | imm8 >>= 15; |
665 | 0 | if ((inst >> 5) & 1) |
666 | 0 | { |
667 | 0 | imm8 = 0 - imm8; |
668 | 0 | } |
669 | |
|
670 | 0 | float f = 0; |
671 | 0 | memcpy (&f, &value, sizeof (float)); |
672 | 0 | sprintf (buf, "%.7g\t// imm9:%4d, imm4:%2d", f, imm8, imm4); |
673 | 0 | strcat (str, buf); |
674 | |
|
675 | 0 | break; |
676 | 0 | } |
677 | | |
678 | 0 | case OPRND_TYPE_DFLOAT_FMOVI: |
679 | 0 | { |
680 | 0 | uint64_t imm4; |
681 | 0 | uint64_t imm8; |
682 | 0 | uint64_t dvalue; |
683 | 0 | imm4 = ((inst >> 16) & 0xf); |
684 | 0 | imm4 = (1034 - imm4) << 52; |
685 | |
|
686 | 0 | imm8 = ((inst >> 8) & 0x3); |
687 | 0 | imm8 |= (((inst >> 20) & 0x3f) << 2); |
688 | 0 | imm8 <<= 44; |
689 | |
|
690 | 0 | dvalue = (((uint64_t)inst >> 5) & 1) << 63; |
691 | 0 | dvalue |= imm4 | imm8; |
692 | |
|
693 | 0 | imm4 = 1034 - (imm4 >> 52); |
694 | 0 | imm8 >>= 44; |
695 | 0 | if (inst >> 5) |
696 | 0 | { |
697 | 0 | imm8 = 0 - imm8; |
698 | 0 | } |
699 | 0 | double d = 0; |
700 | 0 | memcpy (&d, &dvalue, sizeof (double)); |
701 | 0 | sprintf (buf, "%.7g\t// imm9:%4ld, imm4:%2ld", d, (long) imm8, (long) imm4); |
702 | 0 | strcat (str, buf); |
703 | |
|
704 | 0 | break; |
705 | 0 | } |
706 | 912 | case OPRND_TYPE_LABEL_WITH_BRACKET: |
707 | 912 | sprintf (buf, "[0x%x]", (unsigned int)value); |
708 | 912 | strcat (str, buf); |
709 | 912 | strcat (str, "\t// the offset is based on .data"); |
710 | 912 | break; |
711 | 2.51k | case OPRND_TYPE_OIMM3b: |
712 | 2.64k | case OPRND_TYPE_OIMM4b: |
713 | 6.93k | case OPRND_TYPE_OIMM5b: |
714 | 6.93k | case OPRND_TYPE_OIMM5b_IDLY: |
715 | 20.6k | case OPRND_TYPE_OIMM8b: |
716 | 20.9k | case OPRND_TYPE_OIMM12b: |
717 | 21.1k | case OPRND_TYPE_OIMM16b: |
718 | 21.4k | case OPRND_TYPE_OIMM18b: |
719 | 21.4k | value += 1; |
720 | 21.4k | sprintf (buf, "%d", (int)value); |
721 | 21.4k | strcat (str, buf); |
722 | 21.4k | break; |
723 | 0 | case OPRND_TYPE_OIMM5b_BMASKI: |
724 | 0 | if (value > 32 || value < 16) |
725 | 0 | { |
726 | 0 | ret = -1; |
727 | 0 | break; |
728 | 0 | } |
729 | 0 | sprintf (buf, "%d", (int)(value + 1)); |
730 | 0 | strcat (str, buf); |
731 | 0 | ret = 0; |
732 | 0 | break; |
733 | 7 | case OPRND_TYPE_FREGLIST_DASH: |
734 | 7 | if (IS_CSKY_V2 (mach_flag)) |
735 | 7 | { |
736 | 7 | int vrx = 0; |
737 | 7 | int vry = 0; |
738 | 7 | if (dis_info.isa & CSKY_ISA_FLOAT_7E60 |
739 | 7 | && (strstr (str, "fstm") != NULL |
740 | 0 | || strstr (str, "fldm") != NULL)) |
741 | 0 | { |
742 | 0 | vrx = value & 0x1f; |
743 | 0 | vry = vrx + (value >> 5); |
744 | 0 | } |
745 | 7 | else |
746 | 7 | { |
747 | 7 | vrx = value & 0xf; |
748 | 7 | vry = vrx + (value >> 4); |
749 | 7 | } |
750 | 7 | sprintf (buf, "fr%d-fr%d", vrx, vry); |
751 | 7 | strcat (str, buf); |
752 | 7 | } |
753 | 7 | break; |
754 | 1.09k | case OPRND_TYPE_REGLIST_DASH: |
755 | 1.09k | if (IS_CSKY_V1 (mach_flag)) |
756 | 1.08k | { |
757 | 1.08k | sprintf (buf, "%s-r15", get_gr_name (value)); |
758 | 1.08k | strcat (str, buf); |
759 | 1.08k | } |
760 | 4 | else |
761 | 4 | { |
762 | 4 | if ((value & 0x1f) + (value >> 5) > 31) |
763 | 0 | { |
764 | 0 | ret = -1; |
765 | 0 | break; |
766 | 0 | } |
767 | 4 | strcat (str, get_gr_name ((value >> 5))); |
768 | 4 | strcat (str, "-"); |
769 | 4 | strcat (str, get_gr_name ((value & 0x1f) + (value >> 5))); |
770 | 4 | } |
771 | 1.09k | break; |
772 | 1.09k | case OPRND_TYPE_PSR_BITS_LIST: |
773 | 34 | { |
774 | 34 | struct psrbit const *bits; |
775 | 34 | int first_oprnd = true; |
776 | 34 | int i = 0; |
777 | 34 | if (IS_CSKY_V1 (mach_flag)) |
778 | 34 | { |
779 | 34 | if (value == 0) |
780 | 3 | { |
781 | 3 | strcat (str, "af"); |
782 | 3 | break; |
783 | 3 | } |
784 | 31 | bits = cskyv1_psr_bits; |
785 | 31 | } |
786 | 0 | else |
787 | 0 | bits = cskyv2_psr_bits; |
788 | 117 | while (value != 0 && bits[i].name != NULL) |
789 | 86 | { |
790 | 86 | if (value & bits[i].value) |
791 | 80 | { |
792 | 80 | if (!first_oprnd) |
793 | 49 | strcat (str, ", "); |
794 | 80 | strcat (str, bits[i].name); |
795 | 80 | value &= ~bits[i].value; |
796 | 80 | first_oprnd = false; |
797 | 80 | } |
798 | 86 | i++; |
799 | 86 | } |
800 | 31 | break; |
801 | 34 | } |
802 | 1.08k | case OPRND_TYPE_REGbsp: |
803 | 1.08k | if (IS_CSKY_V1 (mach_flag)) |
804 | 1.08k | sprintf(buf, "(%s)", get_gr_name (0)); |
805 | 0 | else |
806 | 0 | sprintf(buf, "(%s)", get_gr_name (14)); |
807 | 1.08k | strcat (str, buf); |
808 | 1.08k | break; |
809 | 16.7k | case OPRND_TYPE_REGsp: |
810 | 16.7k | if (IS_CSKY_V1 (mach_flag)) |
811 | 0 | strcat (str, get_gr_name (0)); |
812 | 16.7k | else |
813 | 16.7k | strcat (str, get_gr_name (14)); |
814 | 16.7k | break; |
815 | 1.07k | case OPRND_TYPE_REGnr4_r7: |
816 | 1.10k | case OPRND_TYPE_AREG_WITH_BRACKET: |
817 | 1.10k | strcat (str, "("); |
818 | 1.10k | strcat (str, get_gr_name (value)); |
819 | 1.10k | strcat (str, ")"); |
820 | 1.10k | break; |
821 | 371 | case OPRND_TYPE_AREG_WITH_LSHIFT: |
822 | 371 | strcat (str, get_gr_name (value >> 5)); |
823 | 371 | strcat (str, " << "); |
824 | 371 | if ((value & 0x1f) == 0x1) |
825 | 52 | strcat (str, "0"); |
826 | 319 | else if ((value & 0x1f) == 0x2) |
827 | 1 | strcat (str, "1"); |
828 | 318 | else if ((value & 0x1f) == 0x4) |
829 | 5 | strcat (str, "2"); |
830 | 313 | else if ((value & 0x1f) == 0x8) |
831 | 9 | strcat (str, "3"); |
832 | 371 | break; |
833 | 51 | case OPRND_TYPE_AREG_WITH_LSHIFT_FPU: |
834 | 51 | strcat (str, get_gr_name (value >> 2)); |
835 | 51 | strcat (str, " << "); |
836 | 51 | if ((value & 0x3) == 0x0) |
837 | 13 | strcat (str, "0"); |
838 | 38 | else if ((value & 0x3) == 0x1) |
839 | 11 | strcat (str, "1"); |
840 | 27 | else if ((value & 0x3) == 0x2) |
841 | 6 | strcat (str, "2"); |
842 | 21 | else if ((value & 0x3) == 0x3) |
843 | 21 | strcat (str, "3"); |
844 | 51 | break; |
845 | 2 | case OPRND_TYPE_VREG_WITH_INDEX: |
846 | 2 | { |
847 | 2 | unsigned freg_val = value & 0xf; |
848 | 2 | unsigned index_val = (value >> 4) & 0xf; |
849 | 2 | sprintf (buf, "vr%d[%d]", freg_val, index_val); |
850 | 2 | strcat(str, buf); |
851 | 2 | break; |
852 | 1.07k | } |
853 | 0 | case OPRND_TYPE_FREG_WITH_INDEX: |
854 | 0 | { |
855 | 0 | unsigned freg_val = value & 0xf; |
856 | 0 | unsigned index_val = (value >> 4) & 0xf; |
857 | 0 | sprintf (buf, "fr%d[%d]", freg_val, index_val); |
858 | 0 | strcat(str, buf); |
859 | 0 | break; |
860 | 1.07k | } |
861 | 1.07k | case OPRND_TYPE_REGr4_r7: |
862 | 1.07k | if (IS_CSKY_V1 (mach_flag)) |
863 | 1.07k | { |
864 | 1.07k | sprintf (buf, "%s-%s", get_gr_name (4), get_gr_name (7)); |
865 | 1.07k | strcat (str, buf); |
866 | 1.07k | } |
867 | 1.07k | break; |
868 | 1.50k | case OPRND_TYPE_CONST1: |
869 | 1.50k | strcat (str, "1"); |
870 | 1.50k | break; |
871 | 2.73k | case OPRND_TYPE_REG_r1a: |
872 | 2.80k | case OPRND_TYPE_REG_r1b: |
873 | 2.80k | strcat (str, get_gr_name (1)); |
874 | 2.80k | break; |
875 | 246 | case OPRND_TYPE_REG_r28: |
876 | 246 | strcat (str, get_gr_name (28)); |
877 | 246 | break; |
878 | 127 | case OPRND_TYPE_REGLIST_DASH_COMMA: |
879 | | /* 16-bit reglist. */ |
880 | 127 | if (value & 0xf) |
881 | 121 | { |
882 | 121 | strcat (str, get_gr_name (4)); |
883 | 121 | if ((value & 0xf) > 1) |
884 | 121 | { |
885 | 121 | strcat (str, "-"); |
886 | 121 | strcat (str, get_gr_name ((value & 0xf) + 3)); |
887 | 121 | } |
888 | 121 | if (value & ~0xf) |
889 | 56 | strcat (str, ", "); |
890 | 121 | } |
891 | 127 | if (value & 0x10) |
892 | 60 | { |
893 | | /* r15. */ |
894 | 60 | strcat (str, get_gr_name (15)); |
895 | 60 | if (value & ~0x1f) |
896 | 0 | strcat (str, ", "); |
897 | 60 | } |
898 | 127 | if (dis_info.opinfo->oprnd.oprnds[0].mask != OPRND_MASK_0_4) |
899 | 0 | { |
900 | | /* 32bits reglist. */ |
901 | 0 | value >>= 5; |
902 | 0 | if (value & 0x3) |
903 | 0 | { |
904 | 0 | strcat (str, get_gr_name (16)); |
905 | 0 | if ((value & 0x7) > 1) |
906 | 0 | { |
907 | 0 | strcat (str, "-"); |
908 | 0 | strcat (str, get_gr_name ((value & 0x7) + 15)); |
909 | 0 | } |
910 | 0 | if (value & ~0x7) |
911 | 0 | strcat (str, ", "); |
912 | 0 | } |
913 | 0 | if (value & 0x8) |
914 | | /* r15. */ |
915 | 0 | strcat (str, get_gr_name (28)); |
916 | 0 | } |
917 | 127 | break; |
918 | 5.24k | case OPRND_TYPE_UNCOND10b: |
919 | 5.52k | case OPRND_TYPE_UNCOND16b: |
920 | 14.7k | case OPRND_TYPE_COND10b: |
921 | 14.8k | case OPRND_TYPE_COND16b: |
922 | 14.8k | { |
923 | 14.8k | int shift = oprnd->shift; |
924 | | |
925 | 14.8k | if (value & ((max >> 1) + 1)) |
926 | 6.46k | value |= ~max; |
927 | 14.8k | if (is_extern_symbol (dis_info.info, dis_info.mem)) |
928 | 0 | value = 0; |
929 | 14.8k | else |
930 | 14.8k | value = dis_info.mem + (value << shift); |
931 | 14.8k | sprintf (buf, "0x%x", (unsigned int)value); |
932 | 14.8k | strcat (str, buf); |
933 | 14.8k | dis_info.need_output_symbol = 1; |
934 | 14.8k | dis_info.value = value; |
935 | 14.8k | } |
936 | 14.8k | break; |
937 | | |
938 | 41 | default: |
939 | 41 | ret = -1; |
940 | 41 | break; |
941 | 686k | } |
942 | 686k | return ret; |
943 | 686k | } |
944 | | |
945 | | static int |
946 | | csky_print_operand (char *str, struct operand const *oprnd, |
947 | | CSKY_INST_TYPE inst, int reloc) |
948 | 764k | { |
949 | 764k | int ret = -1; |
950 | 764k | char *lc = ""; |
951 | 764k | char *rc = ""; |
952 | 764k | if (oprnd->mask == HAS_SUB_OPERAND) |
953 | 78.1k | { |
954 | 78.1k | struct soperand *sop = (struct soperand *)oprnd; |
955 | 78.1k | if (oprnd->type == OPRND_TYPE_BRACKET) |
956 | 72.0k | { |
957 | 72.0k | lc = "("; |
958 | 72.0k | rc = ")"; |
959 | 72.0k | } |
960 | 6.16k | else if (oprnd->type == OPRND_TYPE_ABRACKET) |
961 | 6.16k | { |
962 | 6.16k | lc = "<"; |
963 | 6.16k | rc = ">"; |
964 | 6.16k | } |
965 | 78.1k | strcat (str, lc); |
966 | 78.1k | ret = csky_print_operand (str, &sop->subs[0], inst, reloc); |
967 | 78.1k | if (ret) |
968 | 0 | return ret; |
969 | 78.1k | strcat (str, ", "); |
970 | 78.1k | ret = csky_print_operand (str, &sop->subs[1], inst, reloc); |
971 | 78.1k | strcat (str, rc); |
972 | 78.1k | return ret; |
973 | 78.1k | } |
974 | 686k | return csky_output_operand (str, oprnd, inst, reloc); |
975 | 764k | } |
976 | | |
977 | | static int |
978 | | csky_print_operands (char *str, struct csky_opcode_info const *pinfo, |
979 | | struct disassemble_info *info, CSKY_INST_TYPE inst, |
980 | | int reloc) |
981 | 368k | { |
982 | 368k | int i = 0; |
983 | 368k | int ret = 0; |
984 | 368k | if (pinfo->operand_num) |
985 | 302k | strcat (str, " \t"); |
986 | 368k | if (pinfo->operand_num == -1) |
987 | 161 | { |
988 | 161 | ret = csky_print_operand (str, &pinfo->oprnd.oprnds[i], inst, reloc); |
989 | 161 | if (ret) |
990 | 0 | return ret; |
991 | 161 | } |
992 | 368k | else |
993 | 976k | for (; i < pinfo->operand_num; i++) |
994 | 608k | { |
995 | 608k | if (i != 0) |
996 | 305k | strcat (str, ", "); |
997 | 608k | ret = csky_print_operand (str, &pinfo->oprnd.oprnds[i], inst, reloc); |
998 | 608k | if (ret) |
999 | 690 | return ret; |
1000 | 608k | } |
1001 | 367k | info->fprintf_func (info->stream, "%s", str); |
1002 | 367k | if (dis_info.need_output_symbol) |
1003 | 78.1k | { |
1004 | 78.1k | info->fprintf_func (info->stream, "\t// "); |
1005 | 78.1k | info->print_address_func (dis_info.value, dis_info.info); |
1006 | 78.1k | } |
1007 | 367k | return 0; |
1008 | 368k | } |
1009 | | |
1010 | | static void |
1011 | | number_to_chars_littleendian (char *buf, CSKY_INST_TYPE val, int n) |
1012 | 0 | { |
1013 | 0 | if (n <= 0) |
1014 | 0 | abort (); |
1015 | 0 | while (n--) |
1016 | 0 | { |
1017 | 0 | *buf++ = val & 0xff; |
1018 | 0 | val >>= 8; |
1019 | 0 | } |
1020 | 0 | } |
1021 | | |
1022 | 440k | #define CSKY_READ_DATA() \ |
1023 | 440k | { \ |
1024 | 440k | status = info->read_memory_func (memaddr, buf, 2, info); \ |
1025 | 440k | if (status) \ |
1026 | 440k | { \ |
1027 | 99 | info->memory_error_func (status, memaddr, info); \ |
1028 | 99 | return -1; \ |
1029 | 99 | } \ |
1030 | 440k | if (info->endian == BFD_ENDIAN_BIG) \ |
1031 | 440k | inst |= (buf[0] << 8) | buf[1]; \ |
1032 | 440k | else if (info->endian == BFD_ENDIAN_LITTLE) \ |
1033 | 22.4k | inst |= (buf[1] << 8) | buf[0]; \ |
1034 | 22.4k | else \ |
1035 | 22.4k | abort(); \ |
1036 | 440k | info->bytes_per_chunk += 2; \ |
1037 | 440k | memaddr += 2; \ |
1038 | 440k | } |
1039 | | |
1040 | | int |
1041 | | print_insn_csky (bfd_vma memaddr, struct disassemble_info *info) |
1042 | 408k | { |
1043 | 408k | unsigned char buf[4]; |
1044 | 408k | CSKY_INST_TYPE inst = 0; |
1045 | 408k | int status; |
1046 | 408k | char str[256]; |
1047 | 408k | unsigned long given; |
1048 | 408k | int is_data = false; |
1049 | 408k | void (*printer) (bfd_vma, struct disassemble_info *, long); |
1050 | 408k | unsigned int size = 4; |
1051 | | |
1052 | 408k | memset (str, 0, sizeof (str)); |
1053 | 408k | info->bytes_per_chunk = 0; |
1054 | 408k | info->bytes_per_chunk = 0; |
1055 | 408k | dis_info.mem = memaddr; |
1056 | 408k | dis_info.info = info; |
1057 | 408k | dis_info.need_output_symbol = 0; |
1058 | | |
1059 | 408k | if (info->disassembler_options) |
1060 | 0 | { |
1061 | 0 | parse_csky_dis_options (info->disassembler_options); |
1062 | 0 | info->disassembler_options = NULL; |
1063 | 0 | } |
1064 | | |
1065 | 408k | if (mach_flag != INIT_MACH_FLAG && mach_flag != BINARY_MACH_FLAG) |
1066 | 408k | info->mach = mach_flag; |
1067 | 0 | else if (mach_flag == INIT_MACH_FLAG) |
1068 | 0 | { |
1069 | 0 | mach_flag = info->mach; |
1070 | 0 | dis_info.isa = CSKY_DEFAULT_ISA; |
1071 | 0 | } |
1072 | | |
1073 | 408k | if (mach_flag == BINARY_MACH_FLAG && info->endian == BFD_ENDIAN_UNKNOWN) |
1074 | 0 | { |
1075 | 0 | info->endian = BFD_ENDIAN_LITTLE; |
1076 | 0 | dis_info.isa = CSKY_DEFAULT_ISA; |
1077 | 0 | } |
1078 | | |
1079 | | /* First check the full symtab for a mapping symbol, even if there |
1080 | | are no usable non-mapping symbols for this address. */ |
1081 | 408k | if (info->symtab_size != 0 |
1082 | 408k | && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour) |
1083 | 0 | { |
1084 | 0 | bfd_vma addr; |
1085 | 0 | int n; |
1086 | 0 | int last_sym = -1; |
1087 | 0 | enum sym_type type = CUR_TEXT; |
1088 | |
|
1089 | 0 | if (memaddr <= last_map_addr) |
1090 | 0 | last_map_sym = -1; |
1091 | | /* Start scanning at the start of the function, or wherever |
1092 | | we finished last time. */ |
1093 | 0 | n = 0; |
1094 | 0 | if (n < last_map_sym) |
1095 | 0 | n = last_map_sym; |
1096 | | |
1097 | | /* Scan up to the location being disassembled. */ |
1098 | 0 | for (; n < info->symtab_size; n++) |
1099 | 0 | { |
1100 | 0 | addr = bfd_asymbol_value (info->symtab[n]); |
1101 | 0 | if (addr > memaddr) |
1102 | 0 | break; |
1103 | 0 | if ((info->section == NULL |
1104 | 0 | || info->section == info->symtab[n]->section) |
1105 | 0 | && get_sym_code_type (info, n, &type)) |
1106 | 0 | last_sym = n; |
1107 | 0 | } |
1108 | 0 | last_map_sym = last_sym; |
1109 | 0 | last_type = type; |
1110 | 0 | is_data = (last_type == CUR_DATA); |
1111 | 0 | if (is_data) |
1112 | 0 | { |
1113 | 0 | size = 4 - ( memaddr & 3); |
1114 | 0 | for (n = last_sym + 1; n < info->symtab_size; n++) |
1115 | 0 | { |
1116 | 0 | addr = bfd_asymbol_value (info->symtab[n]); |
1117 | 0 | if (addr > memaddr) |
1118 | 0 | { |
1119 | 0 | if (addr - memaddr < size) |
1120 | 0 | size = addr - memaddr; |
1121 | 0 | break; |
1122 | 0 | } |
1123 | 0 | } |
1124 | | /* If the next symbol is after three bytes, we need to |
1125 | | print only part of the data, so that we can use either |
1126 | | .byte or .short. */ |
1127 | 0 | if (size == 3) |
1128 | 0 | size = (memaddr & 1) ? 1 : 2; |
1129 | 0 | } |
1130 | 0 | } |
1131 | 408k | info->bytes_per_line = 4; |
1132 | | |
1133 | 408k | if (is_data) |
1134 | 0 | { |
1135 | 0 | int i; |
1136 | | |
1137 | | /* Size was already set above. */ |
1138 | 0 | info->bytes_per_chunk = size; |
1139 | 0 | printer = print_insn_data; |
1140 | |
|
1141 | 0 | status = info->read_memory_func (memaddr, (bfd_byte *) buf, size, info); |
1142 | 0 | given = 0; |
1143 | 0 | if (info->endian == BFD_ENDIAN_LITTLE) |
1144 | 0 | for (i = size - 1; i >= 0; i--) |
1145 | 0 | given = buf[i] | (given << 8); |
1146 | 0 | else |
1147 | 0 | for (i = 0; i < (int) size; i++) |
1148 | 0 | given = buf[i] | (given << 8); |
1149 | |
|
1150 | 0 | printer (memaddr, info, given); |
1151 | 0 | return info->bytes_per_chunk; |
1152 | 0 | } |
1153 | | |
1154 | | /* Handle instructions. */ |
1155 | 1.22M | CSKY_READ_DATA(); |
1156 | 1.22M | if ((inst & 0xc000) == 0xc000 && IS_CSKY_V2 (mach_flag)) |
1157 | 31.8k | { |
1158 | | /* It's a 32-bit instruction. */ |
1159 | 31.8k | inst <<= 16; |
1160 | 63.6k | CSKY_READ_DATA(); |
1161 | 63.6k | if (info->buffer && (info->endian == BFD_ENDIAN_LITTLE)) |
1162 | 0 | { |
1163 | 0 | char* src = (char *)(info->buffer |
1164 | 0 | + ((memaddr - 4 - info->buffer_vma) |
1165 | 0 | * info->octets_per_byte)); |
1166 | 0 | if (info->endian == BFD_ENDIAN_LITTLE) |
1167 | 0 | number_to_chars_littleendian (src, inst, 4); |
1168 | 0 | } |
1169 | 63.6k | } |
1170 | | |
1171 | 408k | if (IS_CSKY_V1 (mach_flag)) |
1172 | 139k | g_opcodeP = csky_v1_opcodes; |
1173 | 268k | else |
1174 | 268k | g_opcodeP = csky_v2_opcodes; |
1175 | | |
1176 | 408k | do |
1177 | 408k | { |
1178 | 408k | struct csky_opcode const *op; |
1179 | 408k | struct csky_opcode_info const *pinfo = NULL; |
1180 | 408k | int reloc; |
1181 | | |
1182 | 408k | memset (str, 0, sizeof (str)); |
1183 | 408k | op = csky_find_inst_info (&pinfo, inst, info->bytes_per_chunk); |
1184 | 408k | if (!op) |
1185 | 40.4k | { |
1186 | 40.4k | if (IS_CSKY_V1 (mach_flag)) |
1187 | 285 | info->fprintf_func (info->stream, ".short: 0x%04x", |
1188 | 285 | (unsigned short)inst); |
1189 | 40.1k | else |
1190 | 40.1k | info->fprintf_func (info->stream, ".long: 0x%08x", |
1191 | 40.1k | (unsigned int)inst); |
1192 | 40.4k | return info->bytes_per_chunk; |
1193 | 40.4k | } |
1194 | | |
1195 | 368k | if (info->bytes_per_chunk == 2) |
1196 | 353k | reloc = op->reloc16; |
1197 | 15.0k | else |
1198 | 15.0k | reloc = op->reloc32; |
1199 | 368k | dis_info.opinfo = pinfo; |
1200 | 368k | strcat (str, op->mnemonic); |
1201 | | |
1202 | 368k | if (csky_print_operands (str, pinfo, info, inst, reloc)) |
1203 | 690 | g_opcodeP++; |
1204 | 367k | else |
1205 | 367k | break; |
1206 | 368k | } while (1); |
1207 | | |
1208 | 367k | return info->bytes_per_chunk; |
1209 | 408k | } |