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