OSDN Git Service

Fix compile time warnings
[pf3gnuchains/pf3gnuchains3x.git] / opcodes / pdp11-dis.c
1 /* Print DEC PDP-11 instructions.
2    Copyright 2001 Free Software Foundation, Inc.
3
4 This file 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 "sysdep.h"
19 #include "dis-asm.h"
20 #include "opcode/pdp11.h"
21
22 #define AFTER_INSTRUCTION       "\t"
23 #define OPERAND_SEPARATOR       ", "
24
25 #define JUMP    0x1000  /* flag that this operand is used in a jump */
26
27 #define FPRINTF (*info->fprintf_func)
28 #define F       info->stream
29
30 /* sign-extend a 16-bit number in an int */
31 #define SIGN_BITS       (8 * sizeof (int) - 16)
32 #define sign_extend(x) (((x) << SIGN_BITS) >> SIGN_BITS)
33
34 static int read_word PARAMS ((bfd_vma memaddr, int *word,
35                               disassemble_info *info));
36 static void print_signed_octal PARAMS ((int n, disassemble_info *info));
37 static void print_reg PARAMS ((int reg, disassemble_info *info));
38 static void print_freg PARAMS ((int freg, disassemble_info *info));
39 static int print_operand PARAMS ((bfd_vma *memaddr, int code,
40                                   disassemble_info *info));
41 int print_insn_pdp11 PARAMS ((bfd_vma memaddr, disassemble_info *info));
42
43 static int
44 read_word (memaddr, word, info)
45      bfd_vma memaddr;
46      int *word;
47      disassemble_info *info;
48 {
49   int status;
50   bfd_byte x[2];
51
52   status = (*info->read_memory_func) (memaddr, x, 2, info);
53   if (status != 0)
54     return -1;
55
56   *word = x[1] << 8 | x[0];
57   return 0;
58 }
59
60 static void
61 print_signed_octal (n, info)
62      int n;
63      disassemble_info *info;
64 {
65   if (n < 0)
66     FPRINTF (F, "-%o", -n);
67   else
68     FPRINTF (F, "%o", n);
69 }
70
71 static void
72 print_reg (reg, info)
73      int reg;
74      disassemble_info *info;
75 {
76   /* mask off the addressing mode, if any */
77   reg &= 7;
78
79   switch (reg)
80     {
81     case 0: case 1: case 2: case 3: case 4: case 5:
82                 FPRINTF (F, "r%d", reg); break;
83     case 6:     FPRINTF (F, "sp"); break;
84     case 7:     FPRINTF (F, "pc"); break;
85     default:    /* error */
86     }
87 }
88
89 static void
90 print_freg (freg, info)
91      int freg;
92      disassemble_info *info;
93 {
94   FPRINTF (F, "fr%d", freg);
95 }
96
97 static int
98 print_operand (memaddr, code, info)
99      bfd_vma *memaddr;
100      int code;
101      disassemble_info *info;
102 {
103   int mode = (code >> 3) & 7;
104   int reg = code & 7;
105   int disp;
106
107   switch (mode)
108     {
109     case 0:
110       print_reg (reg, info);
111       break;
112     case 1:
113       FPRINTF (F, "(");
114       print_reg (reg, info);
115       FPRINTF (F, ")");
116       break;
117     case 2:
118       if (reg == 7)
119         {
120           int data;
121           if (read_word (*memaddr, &data, info) < 0)
122             return -1;
123           FPRINTF (F, "$");
124           print_signed_octal (sign_extend (data), info);
125           *memaddr += 2;
126         }
127       else
128         {
129           FPRINTF (F, "(");
130           print_reg (reg, info);
131           FPRINTF (F, ")+");
132         }
133         break;
134     case 3:
135       if (reg == 7)
136         {
137           int address;
138           if (read_word (*memaddr, &address, info) < 0)
139             return -1;
140           FPRINTF (F, "*$%o", address);
141           *memaddr += 2;
142         }
143       else
144         {
145           FPRINTF (F, "*(");
146           print_reg (reg, info);
147           FPRINTF (F, ")+");
148         }
149         break;
150     case 4:
151       FPRINTF (F, "-(");
152       print_reg (reg, info);
153       FPRINTF (F, ")");
154       break;
155     case 5:
156       FPRINTF (F, "*-(");
157       print_reg (reg, info);
158       FPRINTF (F, ")");
159       break;
160     case 6:
161     case 7:
162       if (read_word (*memaddr, &disp, info) < 0)
163         return -1;
164       *memaddr += 2;
165       if (reg == 7)
166         {
167           bfd_vma address = *memaddr + sign_extend (disp);
168           if (!(code & JUMP))
169             FPRINTF (F, "*$");
170           (*info->print_address_func) (address, info);
171         }
172       else
173         {
174           if (mode == 7)
175             FPRINTF (F, "*");
176           print_signed_octal (sign_extend (disp), info);
177           FPRINTF (F, "(");
178           print_reg (reg, info);
179           FPRINTF (F, ")");
180         }
181       break;
182     }
183
184   return 0;
185 }
186
187 /* Print the PDP-11 instruction at address MEMADDR in debugged memory,
188    on INFO->STREAM.  Returns length of the instruction, in bytes.  */
189
190 int
191 print_insn_pdp11 (memaddr, info)
192      bfd_vma memaddr;
193      disassemble_info *info;
194 {
195   bfd_vma start_memaddr = memaddr;
196   int opcode;
197   int src, dst;
198   int i;
199
200   info->bytes_per_line = 6;
201   info->bytes_per_chunk = 2;
202   info->display_endian = BFD_ENDIAN_LITTLE;
203
204   if (read_word (memaddr, &opcode, info) != 0)
205     return -1;
206   memaddr += 2;
207
208   src = (opcode >> 6) & 0x3f;
209   dst = opcode & 0x3f;
210
211   for (i = 0; i < pdp11_num_opcodes; i++)
212     {
213 #define OP pdp11_opcodes[i]
214       if ((opcode & OP.mask) == OP.opcode)
215         switch (OP.type)
216           {
217           case PDP11_OPCODE_NO_OPS:
218             FPRINTF (F, OP.name);
219             break;
220           case PDP11_OPCODE_REG:
221             FPRINTF (F, OP.name);
222             FPRINTF (F, AFTER_INSTRUCTION);
223             print_reg (dst, info);
224             break;
225           case PDP11_OPCODE_OP:
226             FPRINTF (F, OP.name);
227             FPRINTF (F, AFTER_INSTRUCTION);
228             if (strcmp (OP.name, "jmp") == 0)
229               dst |= JUMP;
230             if (print_operand (&memaddr, dst, info) < 0)
231               return -1;
232             break;
233           case PDP11_OPCODE_REG_OP:
234             FPRINTF (F, OP.name);
235             FPRINTF (F, AFTER_INSTRUCTION);
236             print_reg (src, info);
237             FPRINTF (F, OPERAND_SEPARATOR);
238             if (strcmp (OP.name, "jsr") == 0)
239               dst |= JUMP;
240             if (print_operand (&memaddr, dst, info) < 0)
241               return -1;
242             break;
243           case PDP11_OPCODE_REG_OP_REV:
244             FPRINTF (F, OP.name);
245             FPRINTF (F, AFTER_INSTRUCTION);
246             if (print_operand (&memaddr, dst, info) < 0)
247               return -1;
248             FPRINTF (F, OPERAND_SEPARATOR);
249             print_reg (src, info);
250             break;
251           case PDP11_OPCODE_AC_OP:
252             {
253               int ac = (opcode & 0xe0) >> 6;
254               FPRINTF (F, OP.name);
255               FPRINTF (F, AFTER_INSTRUCTION);
256               print_freg (ac, info);
257               FPRINTF (F, OPERAND_SEPARATOR);
258               if (print_operand (&memaddr, dst, info) < 0)
259                 return -1;
260               break;
261             }
262           case PDP11_OPCODE_OP_OP:
263             FPRINTF (F, OP.name);
264             FPRINTF (F, AFTER_INSTRUCTION);
265             if (print_operand (&memaddr, src, info) < 0)
266               return -1;
267             FPRINTF (F, OPERAND_SEPARATOR);
268             if (print_operand (&memaddr, dst, info) < 0)
269               return -1;
270             break;
271           case PDP11_OPCODE_DISPL:
272             {
273               int displ = (opcode & 0xff) << 8;
274               bfd_vma address = memaddr + (sign_extend (displ) >> 7);
275               FPRINTF (F, OP.name);
276               FPRINTF (F, AFTER_INSTRUCTION);
277               (*info->print_address_func) (address, info);
278               break;
279             }
280           case PDP11_OPCODE_REG_DISPL:
281             {
282               int displ = (opcode & 0x3f) << 10;
283               bfd_vma address = memaddr + (sign_extend (displ) >> 9);
284               FPRINTF (F, OP.name);
285               FPRINTF (F, AFTER_INSTRUCTION);
286               print_reg (src, info);
287               FPRINTF (F, OPERAND_SEPARATOR);
288               (*info->print_address_func) (address, info);
289               break;
290             }
291           case PDP11_OPCODE_IMM8:
292             {
293               int code = opcode & 0xff;
294               FPRINTF (F, OP.name);
295               FPRINTF (F, AFTER_INSTRUCTION);
296               FPRINTF (F, "%o", code);
297               break;
298             }
299           case PDP11_OPCODE_IMM6:
300             {
301               int code = opcode & 0x3f;
302               FPRINTF (F, OP.name);
303               FPRINTF (F, AFTER_INSTRUCTION);
304               FPRINTF (F, "%o", code);
305               break;
306             }
307           case PDP11_OPCODE_IMM3:
308             {
309               int code = opcode & 7;
310               FPRINTF (F, OP.name);
311               FPRINTF (F, AFTER_INSTRUCTION);
312               FPRINTF (F, "%o", code);
313               break;
314             }
315           default:
316             /* TODO: is this a proper way of signalling an error? */
317             FPRINTF (F, "<internal error: unrecognized instruction type>");
318             return -1;
319           }
320 #undef OP
321     }
322
323   return memaddr - start_memaddr;
324 }