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