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