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