Add support for the new names of the RISC-V fmv.x.s and fmv.s.x instructions, vis...
[external/binutils.git] / opcodes / ft32-dis.c
1 /* Disassemble ft32 instructions.
2    Copyright (C) 2013-2017 Free Software Foundation, Inc.
3    Contributed by FTDI (support@ftdichip.com)
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 <stdio.h>
24 #define STATIC_TABLE
25 #define DEFINE_TABLE
26
27 #include "opcode/ft32.h"
28 #include "disassemble.h"
29
30 extern const ft32_opc_info_t ft32_opc_info[128];
31
32 static fprintf_ftype fpr;
33 static void *stream;
34
35 static int
36 sign_extend(int bit, int value)
37 {
38   int onebit = (1 << bit);
39   return (value & (onebit - 1)) - (value & onebit);
40 }
41
42 int
43 print_insn_ft32 (bfd_vma addr, struct disassemble_info *info)
44 {
45   int status;
46   stream = info->stream;
47   bfd_byte buffer[4];
48   unsigned int iword;
49   const ft32_opc_info_t *oo;
50
51   fpr = info->fprintf_func;
52
53   if ((status = info->read_memory_func (addr, buffer, 4, info)))
54     goto fail;
55
56   iword = bfd_getl32 (buffer);
57
58   for (oo = ft32_opc_info; oo->name; oo++)
59     if ((iword & oo->mask) == oo->bits)
60       break;
61
62   if (oo->name)
63     {
64       int f = oo->fields;
65       int imm;
66
67       fpr (stream, "%08x %s", iword, oo->name);
68       if (oo->dw)
69         {
70           fpr (stream, ".%c ", "bsl"[(iword >> FT32_FLD_DW_BIT) & 3]);
71         }
72       else
73         {
74           fpr (stream, " ");
75         }
76
77       while (f)
78         {
79           int lobit = f & -f;
80           if (f & lobit)
81             {
82               switch (lobit)
83               {
84               case  FT32_FLD_CBCRCV:
85                 // imm is {CB, CV}
86                 imm = ((iword >> FT32_FLD_CB_BIT) & ((1 << FT32_FLD_CB_SIZ) - 1)) << 4;
87                 imm |= ((iword >> FT32_FLD_CV_BIT) & ((1 << FT32_FLD_CV_SIZ) - 1));
88                 switch (imm)
89                 {
90                 case 0x00: fpr(stream, "nz");  break;
91                 case 0x01: fpr(stream, "z");   break;
92                 case 0x10: fpr(stream, "ae");  break;
93                 case 0x11: fpr(stream, "b");   break;
94                 case 0x20: fpr(stream, "no");  break;
95                 case 0x21: fpr(stream, "o");   break;
96                 case 0x30: fpr(stream, "ns");  break;
97                 case 0x31: fpr(stream, "s");   break;
98                 case 0x40: fpr(stream, "lt");  break;
99                 case 0x41: fpr(stream, "gte"); break;
100                 case 0x50: fpr(stream, "lte"); break;
101                 case 0x51: fpr(stream, "gt");  break;
102                 case 0x60: fpr(stream, "be");  break;
103                 case 0x61: fpr(stream, "a");   break;
104                 default:   fpr(stream, "%d,$r30,%d", (imm >> 4), (imm & 1)); break;
105                 }
106                 break;
107               case  FT32_FLD_CB:
108                 imm = (iword >> FT32_FLD_CB_BIT) & ((1 << FT32_FLD_CB_SIZ) - 1);
109                 fpr(stream, "%d", imm);
110                 break;
111               case  FT32_FLD_R_D:
112                 fpr(stream, "$r%d", (iword >> FT32_FLD_R_D_BIT) & 0x1f);
113                 break;
114               case  FT32_FLD_CR:
115                 imm = (iword >> FT32_FLD_CR_BIT) & ((1 << FT32_FLD_CR_SIZ) - 1);
116                 fpr(stream, "$r%d", 28 + imm);
117                 break;
118               case  FT32_FLD_CV:
119                 imm = (iword >> FT32_FLD_CV_BIT) & ((1 << FT32_FLD_CV_SIZ) - 1);
120                 fpr(stream, "%d", imm);
121                 break;
122               case  FT32_FLD_R_1:
123                 fpr(stream, "$r%d", (iword >> FT32_FLD_R_1_BIT) & 0x1f);
124                 break;
125               case  FT32_FLD_RIMM:
126                 imm = (iword >> FT32_FLD_RIMM_BIT) & ((1 << FT32_FLD_RIMM_SIZ) - 1);
127                 if (imm & 0x400)
128                   fpr(stream, "%d", sign_extend(9, imm));
129                 else
130                   fpr(stream, "$r%d", imm & 0x1f);
131                 break;
132               case  FT32_FLD_R_2:
133                 fpr(stream, "$r%d", (iword >> FT32_FLD_R_2_BIT) & 0x1f);
134                 break;
135               case  FT32_FLD_K20:
136                 imm = iword & ((1 << FT32_FLD_K20_SIZ) - 1);
137                 fpr(stream, "%d", sign_extend(19, imm));
138                 break;
139               case  FT32_FLD_PA:
140                 imm = (iword & ((1 << FT32_FLD_PA_SIZ) - 1)) << 2;
141                 info->print_address_func ((bfd_vma) imm, info);
142                 break;
143               case  FT32_FLD_AA:
144                 imm = iword & ((1 << FT32_FLD_AA_SIZ) - 1);
145                 info->print_address_func ((1 << 23) | (bfd_vma) imm, info);
146                 break;
147               case  FT32_FLD_K16:
148                 imm = iword & ((1 << FT32_FLD_K16_SIZ) - 1);
149                 fpr(stream, "%d", imm);
150                 break;
151               case  FT32_FLD_K8:
152                 imm = iword & ((1 << FT32_FLD_K8_SIZ) - 1);
153                 fpr(stream, "%d", sign_extend(7, imm));
154                 break;
155               case  FT32_FLD_R_D_POST:
156                 fpr(stream, "$r%d", (iword >> FT32_FLD_R_D_BIT) & 0x1f);
157                 break;
158               case  FT32_FLD_R_1_POST:
159                 fpr(stream, "$r%d", (iword >> FT32_FLD_R_1_BIT) & 0x1f);
160                 break;
161               default:
162                 break;
163               }
164               f &= ~lobit;
165               if (f)
166                 {
167                   fpr(stream, ",");
168                 }
169             }
170         }
171     }
172     else
173     {
174       fpr (stream, "%08x!", iword);
175     }
176
177   return 4;
178
179  fail:
180   info->memory_error_func (status, addr, info);
181   return -1;
182 }