1*3d8817e4Smiod /* xtensa-dis.c. Disassembly functions for Xtensa.
2*3d8817e4Smiod Copyright 2003, 2004 Free Software Foundation, Inc.
3*3d8817e4Smiod Contributed by Bob Wilson at Tensilica, Inc. (bwilson@tensilica.com)
4*3d8817e4Smiod
5*3d8817e4Smiod This file is part of GDB, GAS, and the GNU binutils.
6*3d8817e4Smiod
7*3d8817e4Smiod GDB, GAS, and the GNU binutils are free software; you can redistribute
8*3d8817e4Smiod them and/or modify them under the terms of the GNU General Public
9*3d8817e4Smiod License as published by the Free Software Foundation; either version 2,
10*3d8817e4Smiod or (at your option) any later version.
11*3d8817e4Smiod
12*3d8817e4Smiod GDB, GAS, and the GNU binutils are distributed in the hope that they
13*3d8817e4Smiod will be useful, but WITHOUT ANY WARRANTY; without even the implied
14*3d8817e4Smiod warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15*3d8817e4Smiod the GNU General Public License for more details.
16*3d8817e4Smiod
17*3d8817e4Smiod You should have received a copy of the GNU General Public License along
18*3d8817e4Smiod with this file; see the file COPYING. If not, write to the Free
19*3d8817e4Smiod Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
20*3d8817e4Smiod USA. */
21*3d8817e4Smiod
22*3d8817e4Smiod #include <stdlib.h>
23*3d8817e4Smiod #include <stdio.h>
24*3d8817e4Smiod #include <sys/types.h>
25*3d8817e4Smiod #include <string.h>
26*3d8817e4Smiod #include "xtensa-isa.h"
27*3d8817e4Smiod #include "ansidecl.h"
28*3d8817e4Smiod #include "libiberty.h"
29*3d8817e4Smiod #include "sysdep.h"
30*3d8817e4Smiod #include "dis-asm.h"
31*3d8817e4Smiod
32*3d8817e4Smiod #include <setjmp.h>
33*3d8817e4Smiod
34*3d8817e4Smiod extern xtensa_isa xtensa_default_isa;
35*3d8817e4Smiod
36*3d8817e4Smiod #ifndef MAX
37*3d8817e4Smiod #define MAX(a,b) (a > b ? a : b)
38*3d8817e4Smiod #endif
39*3d8817e4Smiod
40*3d8817e4Smiod int show_raw_fields;
41*3d8817e4Smiod
42*3d8817e4Smiod struct dis_private
43*3d8817e4Smiod {
44*3d8817e4Smiod bfd_byte *byte_buf;
45*3d8817e4Smiod jmp_buf bailout;
46*3d8817e4Smiod };
47*3d8817e4Smiod
48*3d8817e4Smiod
49*3d8817e4Smiod static int
fetch_data(struct disassemble_info * info,bfd_vma memaddr)50*3d8817e4Smiod fetch_data (struct disassemble_info *info, bfd_vma memaddr)
51*3d8817e4Smiod {
52*3d8817e4Smiod int length, status = 0;
53*3d8817e4Smiod struct dis_private *priv = (struct dis_private *) info->private_data;
54*3d8817e4Smiod int insn_size = xtensa_isa_maxlength (xtensa_default_isa);
55*3d8817e4Smiod
56*3d8817e4Smiod /* Read the maximum instruction size, padding with zeros if we go past
57*3d8817e4Smiod the end of the text section. This code will automatically adjust
58*3d8817e4Smiod length when we hit the end of the buffer. */
59*3d8817e4Smiod
60*3d8817e4Smiod memset (priv->byte_buf, 0, insn_size);
61*3d8817e4Smiod for (length = insn_size; length > 0; length--)
62*3d8817e4Smiod {
63*3d8817e4Smiod status = (*info->read_memory_func) (memaddr, priv->byte_buf, length,
64*3d8817e4Smiod info);
65*3d8817e4Smiod if (status == 0)
66*3d8817e4Smiod return length;
67*3d8817e4Smiod }
68*3d8817e4Smiod (*info->memory_error_func) (status, memaddr, info);
69*3d8817e4Smiod longjmp (priv->bailout, 1);
70*3d8817e4Smiod /*NOTREACHED*/
71*3d8817e4Smiod }
72*3d8817e4Smiod
73*3d8817e4Smiod
74*3d8817e4Smiod static void
print_xtensa_operand(bfd_vma memaddr,struct disassemble_info * info,xtensa_opcode opc,int opnd,unsigned operand_val)75*3d8817e4Smiod print_xtensa_operand (bfd_vma memaddr,
76*3d8817e4Smiod struct disassemble_info *info,
77*3d8817e4Smiod xtensa_opcode opc,
78*3d8817e4Smiod int opnd,
79*3d8817e4Smiod unsigned operand_val)
80*3d8817e4Smiod {
81*3d8817e4Smiod xtensa_isa isa = xtensa_default_isa;
82*3d8817e4Smiod int signed_operand_val;
83*3d8817e4Smiod
84*3d8817e4Smiod if (show_raw_fields)
85*3d8817e4Smiod {
86*3d8817e4Smiod if (operand_val < 0xa)
87*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%u", operand_val);
88*3d8817e4Smiod else
89*3d8817e4Smiod (*info->fprintf_func) (info->stream, "0x%x", operand_val);
90*3d8817e4Smiod return;
91*3d8817e4Smiod }
92*3d8817e4Smiod
93*3d8817e4Smiod (void) xtensa_operand_decode (isa, opc, opnd, &operand_val);
94*3d8817e4Smiod signed_operand_val = (int) operand_val;
95*3d8817e4Smiod
96*3d8817e4Smiod if (xtensa_operand_is_register (isa, opc, opnd) == 0)
97*3d8817e4Smiod {
98*3d8817e4Smiod if (xtensa_operand_is_PCrelative (isa, opc, opnd) == 1)
99*3d8817e4Smiod {
100*3d8817e4Smiod (void) xtensa_operand_undo_reloc (isa, opc, opnd,
101*3d8817e4Smiod &operand_val, memaddr);
102*3d8817e4Smiod info->target = operand_val;
103*3d8817e4Smiod (*info->print_address_func) (info->target, info);
104*3d8817e4Smiod }
105*3d8817e4Smiod else
106*3d8817e4Smiod {
107*3d8817e4Smiod if ((signed_operand_val > -256) && (signed_operand_val < 256))
108*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d", signed_operand_val);
109*3d8817e4Smiod else
110*3d8817e4Smiod (*info->fprintf_func) (info->stream, "0x%x", signed_operand_val);
111*3d8817e4Smiod }
112*3d8817e4Smiod }
113*3d8817e4Smiod else
114*3d8817e4Smiod {
115*3d8817e4Smiod int i = 1;
116*3d8817e4Smiod xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd);
117*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s%u",
118*3d8817e4Smiod xtensa_regfile_shortname (isa, opnd_rf),
119*3d8817e4Smiod operand_val);
120*3d8817e4Smiod while (i < xtensa_operand_num_regs (isa, opc, opnd))
121*3d8817e4Smiod {
122*3d8817e4Smiod operand_val++;
123*3d8817e4Smiod (*info->fprintf_func) (info->stream, ":%s%u",
124*3d8817e4Smiod xtensa_regfile_shortname (isa, opnd_rf),
125*3d8817e4Smiod operand_val);
126*3d8817e4Smiod i++;
127*3d8817e4Smiod }
128*3d8817e4Smiod }
129*3d8817e4Smiod }
130*3d8817e4Smiod
131*3d8817e4Smiod
132*3d8817e4Smiod /* Print the Xtensa instruction at address MEMADDR on info->stream.
133*3d8817e4Smiod Returns length of the instruction in bytes. */
134*3d8817e4Smiod
135*3d8817e4Smiod int
print_insn_xtensa(bfd_vma memaddr,struct disassemble_info * info)136*3d8817e4Smiod print_insn_xtensa (bfd_vma memaddr, struct disassemble_info *info)
137*3d8817e4Smiod {
138*3d8817e4Smiod unsigned operand_val;
139*3d8817e4Smiod int bytes_fetched, size, maxsize, i, n, noperands, nslots;
140*3d8817e4Smiod xtensa_isa isa;
141*3d8817e4Smiod xtensa_opcode opc;
142*3d8817e4Smiod xtensa_format fmt;
143*3d8817e4Smiod struct dis_private priv;
144*3d8817e4Smiod static bfd_byte *byte_buf = NULL;
145*3d8817e4Smiod static xtensa_insnbuf insn_buffer = NULL;
146*3d8817e4Smiod static xtensa_insnbuf slot_buffer = NULL;
147*3d8817e4Smiod int first, first_slot, valid_insn;
148*3d8817e4Smiod
149*3d8817e4Smiod if (!xtensa_default_isa)
150*3d8817e4Smiod xtensa_default_isa = xtensa_isa_init (0, 0);
151*3d8817e4Smiod
152*3d8817e4Smiod info->target = 0;
153*3d8817e4Smiod maxsize = xtensa_isa_maxlength (xtensa_default_isa);
154*3d8817e4Smiod
155*3d8817e4Smiod /* Set bytes_per_line to control the amount of whitespace between the hex
156*3d8817e4Smiod values and the opcode. For Xtensa, we always print one "chunk" and we
157*3d8817e4Smiod vary bytes_per_chunk to determine how many bytes to print. (objdump
158*3d8817e4Smiod would apparently prefer that we set bytes_per_chunk to 1 and vary
159*3d8817e4Smiod bytes_per_line but that makes it hard to fit 64-bit instructions on
160*3d8817e4Smiod an 80-column screen.) The value of bytes_per_line here is not exactly
161*3d8817e4Smiod right, because objdump adds an extra space for each chunk so that the
162*3d8817e4Smiod amount of whitespace depends on the chunk size. Oh well, it's good
163*3d8817e4Smiod enough.... Note that we set the minimum size to 4 to accomodate
164*3d8817e4Smiod literal pools. */
165*3d8817e4Smiod info->bytes_per_line = MAX (maxsize, 4);
166*3d8817e4Smiod
167*3d8817e4Smiod /* Allocate buffers the first time through. */
168*3d8817e4Smiod if (!insn_buffer)
169*3d8817e4Smiod {
170*3d8817e4Smiod insn_buffer = xtensa_insnbuf_alloc (xtensa_default_isa);
171*3d8817e4Smiod slot_buffer = xtensa_insnbuf_alloc (xtensa_default_isa);
172*3d8817e4Smiod byte_buf = (bfd_byte *) xmalloc (MAX (maxsize, 4));
173*3d8817e4Smiod }
174*3d8817e4Smiod
175*3d8817e4Smiod priv.byte_buf = byte_buf;
176*3d8817e4Smiod
177*3d8817e4Smiod info->private_data = (void *) &priv;
178*3d8817e4Smiod if (setjmp (priv.bailout) != 0)
179*3d8817e4Smiod /* Error return. */
180*3d8817e4Smiod return -1;
181*3d8817e4Smiod
182*3d8817e4Smiod /* Don't set "isa" before the setjmp to keep the compiler from griping. */
183*3d8817e4Smiod isa = xtensa_default_isa;
184*3d8817e4Smiod size = 0;
185*3d8817e4Smiod nslots = 0;
186*3d8817e4Smiod
187*3d8817e4Smiod /* Fetch the maximum size instruction. */
188*3d8817e4Smiod bytes_fetched = fetch_data (info, memaddr);
189*3d8817e4Smiod
190*3d8817e4Smiod /* Copy the bytes into the decode buffer. */
191*3d8817e4Smiod memset (insn_buffer, 0, (xtensa_insnbuf_size (isa) *
192*3d8817e4Smiod sizeof (xtensa_insnbuf_word)));
193*3d8817e4Smiod xtensa_insnbuf_from_chars (isa, insn_buffer, priv.byte_buf, bytes_fetched);
194*3d8817e4Smiod
195*3d8817e4Smiod fmt = xtensa_format_decode (isa, insn_buffer);
196*3d8817e4Smiod if (fmt == XTENSA_UNDEFINED
197*3d8817e4Smiod || ((size = xtensa_format_length (isa, fmt)) > bytes_fetched))
198*3d8817e4Smiod valid_insn = 0;
199*3d8817e4Smiod else
200*3d8817e4Smiod {
201*3d8817e4Smiod /* Make sure all the opcodes are valid. */
202*3d8817e4Smiod valid_insn = 1;
203*3d8817e4Smiod nslots = xtensa_format_num_slots (isa, fmt);
204*3d8817e4Smiod for (n = 0; n < nslots; n++)
205*3d8817e4Smiod {
206*3d8817e4Smiod xtensa_format_get_slot (isa, fmt, n, insn_buffer, slot_buffer);
207*3d8817e4Smiod if (xtensa_opcode_decode (isa, fmt, n, slot_buffer)
208*3d8817e4Smiod == XTENSA_UNDEFINED)
209*3d8817e4Smiod {
210*3d8817e4Smiod valid_insn = 0;
211*3d8817e4Smiod break;
212*3d8817e4Smiod }
213*3d8817e4Smiod }
214*3d8817e4Smiod }
215*3d8817e4Smiod
216*3d8817e4Smiod if (!valid_insn)
217*3d8817e4Smiod {
218*3d8817e4Smiod (*info->fprintf_func) (info->stream, ".byte %#02x", priv.byte_buf[0]);
219*3d8817e4Smiod return 1;
220*3d8817e4Smiod }
221*3d8817e4Smiod
222*3d8817e4Smiod if (nslots > 1)
223*3d8817e4Smiod (*info->fprintf_func) (info->stream, "{ ");
224*3d8817e4Smiod
225*3d8817e4Smiod first_slot = 1;
226*3d8817e4Smiod for (n = 0; n < nslots; n++)
227*3d8817e4Smiod {
228*3d8817e4Smiod if (first_slot)
229*3d8817e4Smiod first_slot = 0;
230*3d8817e4Smiod else
231*3d8817e4Smiod (*info->fprintf_func) (info->stream, "; ");
232*3d8817e4Smiod
233*3d8817e4Smiod xtensa_format_get_slot (isa, fmt, n, insn_buffer, slot_buffer);
234*3d8817e4Smiod opc = xtensa_opcode_decode (isa, fmt, n, slot_buffer);
235*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s",
236*3d8817e4Smiod xtensa_opcode_name (isa, opc));
237*3d8817e4Smiod
238*3d8817e4Smiod /* Print the operands (if any). */
239*3d8817e4Smiod noperands = xtensa_opcode_num_operands (isa, opc);
240*3d8817e4Smiod first = 1;
241*3d8817e4Smiod for (i = 0; i < noperands; i++)
242*3d8817e4Smiod {
243*3d8817e4Smiod if (xtensa_operand_is_visible (isa, opc, i) == 0)
244*3d8817e4Smiod continue;
245*3d8817e4Smiod if (first)
246*3d8817e4Smiod {
247*3d8817e4Smiod (*info->fprintf_func) (info->stream, "\t");
248*3d8817e4Smiod first = 0;
249*3d8817e4Smiod }
250*3d8817e4Smiod else
251*3d8817e4Smiod (*info->fprintf_func) (info->stream, ", ");
252*3d8817e4Smiod (void) xtensa_operand_get_field (isa, opc, i, fmt, n,
253*3d8817e4Smiod slot_buffer, &operand_val);
254*3d8817e4Smiod
255*3d8817e4Smiod print_xtensa_operand (memaddr, info, opc, i, operand_val);
256*3d8817e4Smiod }
257*3d8817e4Smiod }
258*3d8817e4Smiod
259*3d8817e4Smiod if (nslots > 1)
260*3d8817e4Smiod (*info->fprintf_func) (info->stream, " }");
261*3d8817e4Smiod
262*3d8817e4Smiod info->bytes_per_chunk = size;
263*3d8817e4Smiod info->display_endian = info->endian;
264*3d8817e4Smiod
265*3d8817e4Smiod return size;
266*3d8817e4Smiod }
267*3d8817e4Smiod
268