* sh-opc.h (sh_arg_type): Added A_SSR and A_SPC.
[platform/upstream/binutils.git] / opcodes / sh-dis.c
1 /* Disassemble SH instructions.
2    Copyright (C) 1993, 1995 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #include <stdio.h>
19 #define STATIC_TABLE
20 #define DEFINE_TABLE
21
22 #include "sh-opc.h"
23 #include "dis-asm.h"
24
25 #define LITTLE_BIT 2
26
27 static int 
28 print_insn_shx(memaddr, info)
29      bfd_vma memaddr;
30      struct disassemble_info *info;
31 {
32   fprintf_ftype fprintf = info->fprintf_func;
33   void *stream = info->stream;
34   unsigned  char insn[2];
35   unsigned  char nibs[4];
36   int status;
37   int relmask = ~0;
38   sh_opcode_info *op;
39   
40   status = info->read_memory_func(memaddr, insn, 2, info);
41
42   if (status != 0) 
43     {
44       info->memory_error_func(status, memaddr, info);
45       return -1;
46     }
47
48
49
50   if (info->flags & LITTLE_BIT) 
51     {
52       nibs[0] = (insn[1] >> 4) & 0xf;
53       nibs[1] = insn[1] & 0xf;
54
55       nibs[2] = (insn[0] >> 4) & 0xf;
56       nibs[3] = insn[0] & 0xf;
57     }
58   else 
59     {
60       nibs[0] = (insn[0] >> 4) & 0xf;
61       nibs[1] = insn[0] & 0xf;
62
63       nibs[2] = (insn[1] >> 4) & 0xf;
64       nibs[3] = insn[1] & 0xf;
65     }
66
67   for (op = sh_table; op->name; op++) 
68     {
69       int n;
70       int imm;
71       int rn;
72       int rm;
73       for (n = 0; n < 4; n++) {
74         int i = op->nibbles[n];
75         if (i < 16) 
76           {
77             if (nibs[n] == i) continue;
78             goto fail;
79           }
80         switch (i)
81           {
82           case BRANCH_8:
83             imm = (nibs[2] << 4) | (nibs[3]);     
84             if (imm & 0x80)
85               imm |= ~0xff;
86             imm = ((char)imm) * 2 + 4 ;
87             goto ok;
88
89           case BRANCH_12:
90             imm = ((nibs[1]) << 8) | (nibs[2] << 4) | (nibs[3]);
91             if (imm & 0x800)
92               imm |= ~0xfff;
93             imm = imm * 2 + 4;
94             goto ok;
95           case IMM_4:
96             imm = nibs[3];
97             goto ok;
98           case IMM_4BY2:
99             imm = nibs[3] <<1;
100             goto ok;
101           case IMM_4BY4:
102             imm = nibs[3] <<2;
103             goto ok;
104
105             
106           case IMM_8:
107             imm = (nibs[2] << 4) | nibs[3];
108             goto ok;
109           case PCRELIMM_8BY2:
110             imm = ((nibs[2] << 4) | nibs[3]) <<1;
111             relmask  = ~1;
112             
113             goto ok;
114
115           case PCRELIMM_8BY4:
116             imm = ((nibs[2] << 4) | nibs[3]) <<2;
117             relmask  = ~3;          
118             goto ok;
119             
120           case IMM_8BY2:
121             imm = ((nibs[2] << 4) | nibs[3]) <<1;
122             goto ok;
123           case IMM_8BY4:
124             imm = ((nibs[2] << 4) | nibs[3]) <<2;
125             goto ok;
126           case DISP_8:
127             imm = (nibs[2] << 4) | (nibs[3]);     
128             goto ok;
129           case DISP_4:
130             imm = nibs[3];
131             goto ok;
132           case REG_N:
133             rn = nibs[n];
134             break;
135           case REG_M:
136             rm = nibs[n];
137             break;
138           default:
139             abort();
140           }
141
142       }
143     ok:
144       fprintf(stream,"%s\t", op->name);
145       for (n = 0; n < 3 && op->arg[n] != A_END; n++) 
146         {
147           if (n && op->arg[1] != A_END)
148             fprintf(stream,",");
149           switch (op->arg[n]) 
150             {
151             case A_IMM:
152               fprintf(stream,"#%d", (char)(imm));
153               break;
154             case A_R0:
155               fprintf(stream,"r0");
156               break;
157             case A_REG_N:
158               fprintf(stream,"r%d", rn);
159               break;
160             case A_INC_N:
161               fprintf(stream,"@r%d+", rn);      
162               break;
163             case A_DEC_N:
164               fprintf(stream,"@-r%d", rn);      
165               break;
166             case A_IND_N:
167               fprintf(stream,"@r%d", rn);       
168               break;
169             case A_DISP_REG_N:
170               fprintf(stream,"@(%d,r%d)",imm, rn);      
171               break;
172             case A_REG_M:
173               fprintf(stream,"r%d", rm);
174               break;
175             case A_INC_M:
176               fprintf(stream,"@r%d+", rm);      
177               break;
178             case A_DEC_M:
179               fprintf(stream,"@-r%d", rm);      
180               break;
181             case A_IND_M:
182               fprintf(stream,"@r%d", rm);       
183               break;
184             case A_DISP_REG_M:
185               fprintf(stream,"@(%d,r%d)",imm, rm);      
186               break;
187             case A_DISP_PC:
188               fprintf(stream,"0x%0x", imm+ 4+(memaddr&relmask));
189               break;
190             case A_IND_R0_REG_N:
191               fprintf(stream,"@(r0,r%d)", rn);
192               break; 
193             case A_IND_R0_REG_M:
194               fprintf(stream,"@(r0,r%d)", rm);
195               break; 
196             case A_DISP_GBR:
197               fprintf(stream,"@(%d,gbr)",imm);
198               break;
199             case A_R0_GBR:
200               fprintf(stream,"@(r0,gbr)");
201               break;
202             case A_BDISP12:
203             case A_BDISP8:
204               (*info->print_address_func) (imm + memaddr, info);
205               break;
206             case A_SR:
207               fprintf(stream,"sr");
208               break;
209             case A_GBR:
210               fprintf(stream,"gbr");
211               break;
212             case A_VBR:
213               fprintf(stream,"vbr");
214               break;
215             case A_SSR:
216               fprintf(stream,"ssr");
217               break;
218             case A_SPC:
219               fprintf(stream,"spc");
220               break;
221             case A_MACH:
222               fprintf(stream,"mach");
223               break;
224             case A_MACL:
225               fprintf(stream,"macl");
226               break;
227             case A_PR:
228               fprintf(stream,"pr");
229               break;
230 /* start-sanitize-sh3e */
231             case F_REG_N:
232               fprintf(stream,"fr%d", rn);
233               break;
234             case F_REG_M:
235               fprintf(stream,"fr%d", rm);
236               break;
237             case FPSCR_M:
238             case FPSCR_N:
239               fprintf(stream,"fpscr");
240               break;
241             case FPUL_M:
242             case FPUL_N:
243               fprintf(stream,"fpul");
244               break;
245             case F_FR0:
246               fprintf(stream,"fr0");
247               break;
248 /* end-sanitize-sh3e */
249             default:
250               abort();
251             }
252         
253         }
254       if (!(info->flags & 1)
255           && (op->name[0] == 'j'
256               || (op->name[0] == 'b'
257                   && (op->name[1] == 'r' 
258                       || op->name[1] == 's'))
259               || (op->name[0] == 'r' && op->name[1] == 't')
260               || (op->name[0] == 'b' && op->name[2] == '.')))
261         {
262           info->flags |= 1;
263           fprintf(stream,"\t(slot ");  print_insn_shx(memaddr +2, info);
264           info->flags &= ~1;
265           fprintf(stream,")");
266           return 4;
267         }
268       
269       return 2;
270     fail:
271       ;
272
273     }
274   fprintf(stream,".word 0x%x%x%x%x", nibs[0], nibs[1], nibs[2], nibs[3]);
275   return 2;
276 }
277
278
279 int 
280 print_insn_shl(memaddr, info)
281      bfd_vma memaddr;
282      struct disassemble_info *info;
283 {
284   int r;
285   info->flags = LITTLE_BIT;
286   r =print_insn_shx (memaddr, info);
287   return r;
288 }
289
290 int 
291 print_insn_sh(memaddr, info)
292      bfd_vma memaddr;
293      struct disassemble_info *info;
294 {
295   int r;
296   info->flags = 0;
297   r =print_insn_shx (memaddr, info);
298   return r;
299 }