1*3d8817e4Smiod /* Disassemble AVR instructions.
2*3d8817e4Smiod Copyright 1999, 2000, 2002, 2004, 2005, 2006
3*3d8817e4Smiod Free Software Foundation, Inc.
4*3d8817e4Smiod
5*3d8817e4Smiod Contributed by Denis Chertykov <denisc@overta.ru>
6*3d8817e4Smiod
7*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
8*3d8817e4Smiod it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
10*3d8817e4Smiod (at your option) any later version.
11*3d8817e4Smiod
12*3d8817e4Smiod This program is distributed in the hope that it will be useful,
13*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*3d8817e4Smiod GNU General Public License for more details.
16*3d8817e4Smiod
17*3d8817e4Smiod You should have received a copy of the GNU General Public License
18*3d8817e4Smiod along with this program; if not, write to the Free Software
19*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20*3d8817e4Smiod
21*3d8817e4Smiod #include <assert.h>
22*3d8817e4Smiod #include "sysdep.h"
23*3d8817e4Smiod #include "dis-asm.h"
24*3d8817e4Smiod #include "opintl.h"
25*3d8817e4Smiod #include "libiberty.h"
26*3d8817e4Smiod
27*3d8817e4Smiod struct avr_opcodes_s
28*3d8817e4Smiod {
29*3d8817e4Smiod char *name;
30*3d8817e4Smiod char *constraints;
31*3d8817e4Smiod char *opcode;
32*3d8817e4Smiod int insn_size; /* In words. */
33*3d8817e4Smiod int isa;
34*3d8817e4Smiod unsigned int bin_opcode;
35*3d8817e4Smiod };
36*3d8817e4Smiod
37*3d8817e4Smiod #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
38*3d8817e4Smiod {#NAME, CONSTR, OPCODE, SIZE, ISA, BIN},
39*3d8817e4Smiod
40*3d8817e4Smiod const struct avr_opcodes_s avr_opcodes[] =
41*3d8817e4Smiod {
42*3d8817e4Smiod #include "opcode/avr.h"
43*3d8817e4Smiod {NULL, NULL, NULL, 0, 0, 0}
44*3d8817e4Smiod };
45*3d8817e4Smiod
46*3d8817e4Smiod static int
avr_operand(unsigned int insn,unsigned int insn2,unsigned int pc,int constraint,char * buf,char * comment,int regs,int * sym,bfd_vma * sym_addr)47*3d8817e4Smiod avr_operand (unsigned int insn, unsigned int insn2, unsigned int pc, int constraint,
48*3d8817e4Smiod char *buf, char *comment, int regs, int *sym, bfd_vma *sym_addr)
49*3d8817e4Smiod {
50*3d8817e4Smiod int ok = 1;
51*3d8817e4Smiod *sym = 0;
52*3d8817e4Smiod
53*3d8817e4Smiod switch (constraint)
54*3d8817e4Smiod {
55*3d8817e4Smiod /* Any register operand. */
56*3d8817e4Smiod case 'r':
57*3d8817e4Smiod if (regs)
58*3d8817e4Smiod insn = (insn & 0xf) | ((insn & 0x0200) >> 5); /* Source register. */
59*3d8817e4Smiod else
60*3d8817e4Smiod insn = (insn & 0x01f0) >> 4; /* Destination register. */
61*3d8817e4Smiod
62*3d8817e4Smiod sprintf (buf, "r%d", insn);
63*3d8817e4Smiod break;
64*3d8817e4Smiod
65*3d8817e4Smiod case 'd':
66*3d8817e4Smiod if (regs)
67*3d8817e4Smiod sprintf (buf, "r%d", 16 + (insn & 0xf));
68*3d8817e4Smiod else
69*3d8817e4Smiod sprintf (buf, "r%d", 16 + ((insn & 0xf0) >> 4));
70*3d8817e4Smiod break;
71*3d8817e4Smiod
72*3d8817e4Smiod case 'w':
73*3d8817e4Smiod sprintf (buf, "r%d", 24 + ((insn & 0x30) >> 3));
74*3d8817e4Smiod break;
75*3d8817e4Smiod
76*3d8817e4Smiod case 'a':
77*3d8817e4Smiod if (regs)
78*3d8817e4Smiod sprintf (buf, "r%d", 16 + (insn & 7));
79*3d8817e4Smiod else
80*3d8817e4Smiod sprintf (buf, "r%d", 16 + ((insn >> 4) & 7));
81*3d8817e4Smiod break;
82*3d8817e4Smiod
83*3d8817e4Smiod case 'v':
84*3d8817e4Smiod if (regs)
85*3d8817e4Smiod sprintf (buf, "r%d", (insn & 0xf) * 2);
86*3d8817e4Smiod else
87*3d8817e4Smiod sprintf (buf, "r%d", ((insn & 0xf0) >> 3));
88*3d8817e4Smiod break;
89*3d8817e4Smiod
90*3d8817e4Smiod case 'e':
91*3d8817e4Smiod {
92*3d8817e4Smiod char *xyz;
93*3d8817e4Smiod
94*3d8817e4Smiod switch (insn & 0x100f)
95*3d8817e4Smiod {
96*3d8817e4Smiod case 0x0000: xyz = "Z"; break;
97*3d8817e4Smiod case 0x1001: xyz = "Z+"; break;
98*3d8817e4Smiod case 0x1002: xyz = "-Z"; break;
99*3d8817e4Smiod case 0x0008: xyz = "Y"; break;
100*3d8817e4Smiod case 0x1009: xyz = "Y+"; break;
101*3d8817e4Smiod case 0x100a: xyz = "-Y"; break;
102*3d8817e4Smiod case 0x100c: xyz = "X"; break;
103*3d8817e4Smiod case 0x100d: xyz = "X+"; break;
104*3d8817e4Smiod case 0x100e: xyz = "-X"; break;
105*3d8817e4Smiod default: xyz = "??"; ok = 0;
106*3d8817e4Smiod }
107*3d8817e4Smiod sprintf (buf, xyz);
108*3d8817e4Smiod
109*3d8817e4Smiod if (AVR_UNDEF_P (insn))
110*3d8817e4Smiod sprintf (comment, _("undefined"));
111*3d8817e4Smiod }
112*3d8817e4Smiod break;
113*3d8817e4Smiod
114*3d8817e4Smiod case 'z':
115*3d8817e4Smiod *buf++ = 'Z';
116*3d8817e4Smiod if (insn & 0x1)
117*3d8817e4Smiod *buf++ = '+';
118*3d8817e4Smiod *buf = '\0';
119*3d8817e4Smiod if (AVR_UNDEF_P (insn))
120*3d8817e4Smiod sprintf (comment, _("undefined"));
121*3d8817e4Smiod break;
122*3d8817e4Smiod
123*3d8817e4Smiod case 'b':
124*3d8817e4Smiod {
125*3d8817e4Smiod unsigned int x;
126*3d8817e4Smiod
127*3d8817e4Smiod x = (insn & 7);
128*3d8817e4Smiod x |= (insn >> 7) & (3 << 3);
129*3d8817e4Smiod x |= (insn >> 8) & (1 << 5);
130*3d8817e4Smiod
131*3d8817e4Smiod if (insn & 0x8)
132*3d8817e4Smiod *buf++ = 'Y';
133*3d8817e4Smiod else
134*3d8817e4Smiod *buf++ = 'Z';
135*3d8817e4Smiod sprintf (buf, "+%d", x);
136*3d8817e4Smiod sprintf (comment, "0x%02x", x);
137*3d8817e4Smiod }
138*3d8817e4Smiod break;
139*3d8817e4Smiod
140*3d8817e4Smiod case 'h':
141*3d8817e4Smiod *sym = 1;
142*3d8817e4Smiod *sym_addr = ((((insn & 1) | ((insn & 0x1f0) >> 3)) << 16) | insn2) * 2;
143*3d8817e4Smiod /* See PR binutils/2545. Ideally we would like to display the hex
144*3d8817e4Smiod value of the address only once, but this would mean recoding
145*3d8817e4Smiod objdump_print_address() which would affect many targets. */
146*3d8817e4Smiod sprintf (buf, "%#lx", (unsigned long) *sym_addr);
147*3d8817e4Smiod sprintf (comment, "0x");
148*3d8817e4Smiod
149*3d8817e4Smiod break;
150*3d8817e4Smiod
151*3d8817e4Smiod case 'L':
152*3d8817e4Smiod {
153*3d8817e4Smiod int rel_addr = (((insn & 0xfff) ^ 0x800) - 0x800) * 2;
154*3d8817e4Smiod sprintf (buf, ".%+-8d", rel_addr);
155*3d8817e4Smiod *sym = 1;
156*3d8817e4Smiod *sym_addr = pc + 2 + rel_addr;
157*3d8817e4Smiod sprintf (comment, "0x");
158*3d8817e4Smiod }
159*3d8817e4Smiod break;
160*3d8817e4Smiod
161*3d8817e4Smiod case 'l':
162*3d8817e4Smiod {
163*3d8817e4Smiod int rel_addr = ((((insn >> 3) & 0x7f) ^ 0x40) - 0x40) * 2;
164*3d8817e4Smiod sprintf (buf, ".%+-8d", rel_addr);
165*3d8817e4Smiod *sym = 1;
166*3d8817e4Smiod *sym_addr = pc + 2 + rel_addr;
167*3d8817e4Smiod sprintf (comment, "0x");
168*3d8817e4Smiod }
169*3d8817e4Smiod break;
170*3d8817e4Smiod
171*3d8817e4Smiod case 'i':
172*3d8817e4Smiod sprintf (buf, "0x%04X", insn2);
173*3d8817e4Smiod break;
174*3d8817e4Smiod
175*3d8817e4Smiod case 'M':
176*3d8817e4Smiod sprintf (buf, "0x%02X", ((insn & 0xf00) >> 4) | (insn & 0xf));
177*3d8817e4Smiod sprintf (comment, "%d", ((insn & 0xf00) >> 4) | (insn & 0xf));
178*3d8817e4Smiod break;
179*3d8817e4Smiod
180*3d8817e4Smiod case 'n':
181*3d8817e4Smiod sprintf (buf, "??");
182*3d8817e4Smiod fprintf (stderr, _("Internal disassembler error"));
183*3d8817e4Smiod ok = 0;
184*3d8817e4Smiod break;
185*3d8817e4Smiod
186*3d8817e4Smiod case 'K':
187*3d8817e4Smiod {
188*3d8817e4Smiod unsigned int x;
189*3d8817e4Smiod
190*3d8817e4Smiod x = (insn & 0xf) | ((insn >> 2) & 0x30);
191*3d8817e4Smiod sprintf (buf, "0x%02x", x);
192*3d8817e4Smiod sprintf (comment, "%d", x);
193*3d8817e4Smiod }
194*3d8817e4Smiod break;
195*3d8817e4Smiod
196*3d8817e4Smiod case 's':
197*3d8817e4Smiod sprintf (buf, "%d", insn & 7);
198*3d8817e4Smiod break;
199*3d8817e4Smiod
200*3d8817e4Smiod case 'S':
201*3d8817e4Smiod sprintf (buf, "%d", (insn >> 4) & 7);
202*3d8817e4Smiod break;
203*3d8817e4Smiod
204*3d8817e4Smiod case 'P':
205*3d8817e4Smiod {
206*3d8817e4Smiod unsigned int x;
207*3d8817e4Smiod
208*3d8817e4Smiod x = (insn & 0xf);
209*3d8817e4Smiod x |= (insn >> 5) & 0x30;
210*3d8817e4Smiod sprintf (buf, "0x%02x", x);
211*3d8817e4Smiod sprintf (comment, "%d", x);
212*3d8817e4Smiod }
213*3d8817e4Smiod break;
214*3d8817e4Smiod
215*3d8817e4Smiod case 'p':
216*3d8817e4Smiod {
217*3d8817e4Smiod unsigned int x;
218*3d8817e4Smiod
219*3d8817e4Smiod x = (insn >> 3) & 0x1f;
220*3d8817e4Smiod sprintf (buf, "0x%02x", x);
221*3d8817e4Smiod sprintf (comment, "%d", x);
222*3d8817e4Smiod }
223*3d8817e4Smiod break;
224*3d8817e4Smiod
225*3d8817e4Smiod case '?':
226*3d8817e4Smiod *buf = '\0';
227*3d8817e4Smiod break;
228*3d8817e4Smiod
229*3d8817e4Smiod default:
230*3d8817e4Smiod sprintf (buf, "??");
231*3d8817e4Smiod fprintf (stderr, _("unknown constraint `%c'"), constraint);
232*3d8817e4Smiod ok = 0;
233*3d8817e4Smiod }
234*3d8817e4Smiod
235*3d8817e4Smiod return ok;
236*3d8817e4Smiod }
237*3d8817e4Smiod
238*3d8817e4Smiod static unsigned short
avrdis_opcode(bfd_vma addr,disassemble_info * info)239*3d8817e4Smiod avrdis_opcode (bfd_vma addr, disassemble_info *info)
240*3d8817e4Smiod {
241*3d8817e4Smiod bfd_byte buffer[2];
242*3d8817e4Smiod int status;
243*3d8817e4Smiod
244*3d8817e4Smiod status = info->read_memory_func (addr, buffer, 2, info);
245*3d8817e4Smiod
246*3d8817e4Smiod if (status == 0)
247*3d8817e4Smiod return bfd_getl16 (buffer);
248*3d8817e4Smiod
249*3d8817e4Smiod info->memory_error_func (status, addr, info);
250*3d8817e4Smiod return -1;
251*3d8817e4Smiod }
252*3d8817e4Smiod
253*3d8817e4Smiod
254*3d8817e4Smiod int
print_insn_avr(bfd_vma addr,disassemble_info * info)255*3d8817e4Smiod print_insn_avr (bfd_vma addr, disassemble_info *info)
256*3d8817e4Smiod {
257*3d8817e4Smiod unsigned int insn, insn2;
258*3d8817e4Smiod const struct avr_opcodes_s *opcode;
259*3d8817e4Smiod static unsigned int *maskptr;
260*3d8817e4Smiod void *stream = info->stream;
261*3d8817e4Smiod fprintf_ftype prin = info->fprintf_func;
262*3d8817e4Smiod static unsigned int *avr_bin_masks;
263*3d8817e4Smiod static int initialized;
264*3d8817e4Smiod int cmd_len = 2;
265*3d8817e4Smiod int ok = 0;
266*3d8817e4Smiod char op1[20], op2[20], comment1[40], comment2[40];
267*3d8817e4Smiod int sym_op1 = 0, sym_op2 = 0;
268*3d8817e4Smiod bfd_vma sym_addr1, sym_addr2;
269*3d8817e4Smiod
270*3d8817e4Smiod if (!initialized)
271*3d8817e4Smiod {
272*3d8817e4Smiod unsigned int nopcodes;
273*3d8817e4Smiod
274*3d8817e4Smiod nopcodes = sizeof (avr_opcodes) / sizeof (struct avr_opcodes_s);
275*3d8817e4Smiod
276*3d8817e4Smiod avr_bin_masks = xmalloc (nopcodes * sizeof (unsigned int));
277*3d8817e4Smiod
278*3d8817e4Smiod for (opcode = avr_opcodes, maskptr = avr_bin_masks;
279*3d8817e4Smiod opcode->name;
280*3d8817e4Smiod opcode++, maskptr++)
281*3d8817e4Smiod {
282*3d8817e4Smiod char * s;
283*3d8817e4Smiod unsigned int bin = 0;
284*3d8817e4Smiod unsigned int mask = 0;
285*3d8817e4Smiod
286*3d8817e4Smiod for (s = opcode->opcode; *s; ++s)
287*3d8817e4Smiod {
288*3d8817e4Smiod bin <<= 1;
289*3d8817e4Smiod mask <<= 1;
290*3d8817e4Smiod bin |= (*s == '1');
291*3d8817e4Smiod mask |= (*s == '1' || *s == '0');
292*3d8817e4Smiod }
293*3d8817e4Smiod assert (s - opcode->opcode == 16);
294*3d8817e4Smiod assert (opcode->bin_opcode == bin);
295*3d8817e4Smiod *maskptr = mask;
296*3d8817e4Smiod }
297*3d8817e4Smiod
298*3d8817e4Smiod initialized = 1;
299*3d8817e4Smiod }
300*3d8817e4Smiod
301*3d8817e4Smiod insn = avrdis_opcode (addr, info);
302*3d8817e4Smiod
303*3d8817e4Smiod for (opcode = avr_opcodes, maskptr = avr_bin_masks;
304*3d8817e4Smiod opcode->name;
305*3d8817e4Smiod opcode++, maskptr++)
306*3d8817e4Smiod if ((insn & *maskptr) == opcode->bin_opcode)
307*3d8817e4Smiod break;
308*3d8817e4Smiod
309*3d8817e4Smiod /* Special case: disassemble `ldd r,b+0' as `ld r,b', and
310*3d8817e4Smiod `std b+0,r' as `st b,r' (next entry in the table). */
311*3d8817e4Smiod
312*3d8817e4Smiod if (AVR_DISP0_P (insn))
313*3d8817e4Smiod opcode++;
314*3d8817e4Smiod
315*3d8817e4Smiod op1[0] = 0;
316*3d8817e4Smiod op2[0] = 0;
317*3d8817e4Smiod comment1[0] = 0;
318*3d8817e4Smiod comment2[0] = 0;
319*3d8817e4Smiod
320*3d8817e4Smiod if (opcode->name)
321*3d8817e4Smiod {
322*3d8817e4Smiod char *op = opcode->constraints;
323*3d8817e4Smiod
324*3d8817e4Smiod insn2 = 0;
325*3d8817e4Smiod ok = 1;
326*3d8817e4Smiod
327*3d8817e4Smiod if (opcode->insn_size > 1)
328*3d8817e4Smiod {
329*3d8817e4Smiod insn2 = avrdis_opcode (addr + 2, info);
330*3d8817e4Smiod cmd_len = 4;
331*3d8817e4Smiod }
332*3d8817e4Smiod
333*3d8817e4Smiod if (*op && *op != '?')
334*3d8817e4Smiod {
335*3d8817e4Smiod int regs = REGISTER_P (*op);
336*3d8817e4Smiod
337*3d8817e4Smiod ok = avr_operand (insn, insn2, addr, *op, op1, comment1, 0, &sym_op1, &sym_addr1);
338*3d8817e4Smiod
339*3d8817e4Smiod if (ok && *(++op) == ',')
340*3d8817e4Smiod ok = avr_operand (insn, insn2, addr, *(++op), op2,
341*3d8817e4Smiod *comment1 ? comment2 : comment1, regs, &sym_op2, &sym_addr2);
342*3d8817e4Smiod }
343*3d8817e4Smiod }
344*3d8817e4Smiod
345*3d8817e4Smiod if (!ok)
346*3d8817e4Smiod {
347*3d8817e4Smiod /* Unknown opcode, or invalid combination of operands. */
348*3d8817e4Smiod sprintf (op1, "0x%04x", insn);
349*3d8817e4Smiod op2[0] = 0;
350*3d8817e4Smiod sprintf (comment1, "????");
351*3d8817e4Smiod comment2[0] = 0;
352*3d8817e4Smiod }
353*3d8817e4Smiod
354*3d8817e4Smiod (*prin) (stream, "%s", ok ? opcode->name : ".word");
355*3d8817e4Smiod
356*3d8817e4Smiod if (*op1)
357*3d8817e4Smiod (*prin) (stream, "\t%s", op1);
358*3d8817e4Smiod
359*3d8817e4Smiod if (*op2)
360*3d8817e4Smiod (*prin) (stream, ", %s", op2);
361*3d8817e4Smiod
362*3d8817e4Smiod if (*comment1)
363*3d8817e4Smiod (*prin) (stream, "\t; %s", comment1);
364*3d8817e4Smiod
365*3d8817e4Smiod if (sym_op1)
366*3d8817e4Smiod info->print_address_func (sym_addr1, info);
367*3d8817e4Smiod
368*3d8817e4Smiod if (*comment2)
369*3d8817e4Smiod (*prin) (stream, " %s", comment2);
370*3d8817e4Smiod
371*3d8817e4Smiod if (sym_op2)
372*3d8817e4Smiod info->print_address_func (sym_addr2, info);
373*3d8817e4Smiod
374*3d8817e4Smiod return cmd_len;
375*3d8817e4Smiod }
376