xref: /openbsd-src/gnu/usr.bin/binutils-2.17/opcodes/d30v-dis.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* Disassemble D30V instructions.
2*3d8817e4Smiod    Copyright 1997, 1998, 2000, 2001, 2005 Free Software Foundation, Inc.
3*3d8817e4Smiod 
4*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
5*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
6*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
7*3d8817e4Smiod    (at your option) any later version.
8*3d8817e4Smiod 
9*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
10*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*3d8817e4Smiod    GNU General Public License for more details.
13*3d8817e4Smiod 
14*3d8817e4Smiod    You should have received a copy of the GNU General Public License
15*3d8817e4Smiod    along with this program; if not, write to the Free Software
16*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
17*3d8817e4Smiod    MA 02110-1301, USA.  */
18*3d8817e4Smiod 
19*3d8817e4Smiod #include <stdio.h>
20*3d8817e4Smiod #include "sysdep.h"
21*3d8817e4Smiod #include "opcode/d30v.h"
22*3d8817e4Smiod #include "dis-asm.h"
23*3d8817e4Smiod #include "opintl.h"
24*3d8817e4Smiod 
25*3d8817e4Smiod #define PC_MASK 0xFFFFFFFF
26*3d8817e4Smiod 
27*3d8817e4Smiod /* Return 0 if lookup fails,
28*3d8817e4Smiod    1 if found and only one form,
29*3d8817e4Smiod    2 if found and there are short and long forms.  */
30*3d8817e4Smiod 
31*3d8817e4Smiod static int
lookup_opcode(struct d30v_insn * insn,long num,int is_long)32*3d8817e4Smiod lookup_opcode (struct d30v_insn *insn, long num, int is_long)
33*3d8817e4Smiod {
34*3d8817e4Smiod   int i = 0, index;
35*3d8817e4Smiod   struct d30v_format *f;
36*3d8817e4Smiod   struct d30v_opcode *op = (struct d30v_opcode *) d30v_opcode_table;
37*3d8817e4Smiod   int op1 = (num >> 25) & 0x7;
38*3d8817e4Smiod   int op2 = (num >> 20) & 0x1f;
39*3d8817e4Smiod   int mod = (num >> 18) & 0x3;
40*3d8817e4Smiod 
41*3d8817e4Smiod   /* Find the opcode.  */
42*3d8817e4Smiod   do
43*3d8817e4Smiod     {
44*3d8817e4Smiod       if ((op->op1 == op1) && (op->op2 == op2))
45*3d8817e4Smiod 	break;
46*3d8817e4Smiod       op++;
47*3d8817e4Smiod     }
48*3d8817e4Smiod   while (op->name);
49*3d8817e4Smiod 
50*3d8817e4Smiod   if (!op || !op->name)
51*3d8817e4Smiod     return 0;
52*3d8817e4Smiod 
53*3d8817e4Smiod   while (op->op1 == op1 && op->op2 == op2)
54*3d8817e4Smiod     {
55*3d8817e4Smiod       /* Scan through all the formats for the opcode.  */
56*3d8817e4Smiod       index = op->format[i++];
57*3d8817e4Smiod       do
58*3d8817e4Smiod 	{
59*3d8817e4Smiod 	  f = (struct d30v_format *) &d30v_format_table[index];
60*3d8817e4Smiod 	  while (f->form == index)
61*3d8817e4Smiod 	    {
62*3d8817e4Smiod 	      if ((!is_long || f->form >= LONG) && (f->modifier == mod))
63*3d8817e4Smiod 		{
64*3d8817e4Smiod 		  insn->form = f;
65*3d8817e4Smiod 		  break;
66*3d8817e4Smiod 		}
67*3d8817e4Smiod 	      f++;
68*3d8817e4Smiod 	    }
69*3d8817e4Smiod 	  if (insn->form)
70*3d8817e4Smiod 	    break;
71*3d8817e4Smiod 	}
72*3d8817e4Smiod       while ((index = op->format[i++]) != 0);
73*3d8817e4Smiod       if (insn->form)
74*3d8817e4Smiod 	break;
75*3d8817e4Smiod       op++;
76*3d8817e4Smiod       i = 0;
77*3d8817e4Smiod     }
78*3d8817e4Smiod   if (insn->form == NULL)
79*3d8817e4Smiod     return 0;
80*3d8817e4Smiod 
81*3d8817e4Smiod   insn->op = op;
82*3d8817e4Smiod   insn->ecc = (num >> 28) & 0x7;
83*3d8817e4Smiod   if (op->format[1])
84*3d8817e4Smiod     return 2;
85*3d8817e4Smiod   else
86*3d8817e4Smiod     return 1;
87*3d8817e4Smiod }
88*3d8817e4Smiod 
89*3d8817e4Smiod static int
extract_value(long long num,struct d30v_operand * oper,int is_long)90*3d8817e4Smiod extract_value (long long num, struct d30v_operand *oper, int is_long)
91*3d8817e4Smiod {
92*3d8817e4Smiod   int val;
93*3d8817e4Smiod   int shift = 12 - oper->position;
94*3d8817e4Smiod   int mask = (0xFFFFFFFF >> (32 - oper->bits));
95*3d8817e4Smiod 
96*3d8817e4Smiod   if (is_long)
97*3d8817e4Smiod     {
98*3d8817e4Smiod       if (oper->bits == 32)
99*3d8817e4Smiod 	/* Piece together 32-bit constant.  */
100*3d8817e4Smiod 	val = ((num & 0x3FFFF)
101*3d8817e4Smiod 	       | ((num & 0xFF00000) >> 2)
102*3d8817e4Smiod 	       | ((num & 0x3F00000000LL) >> 6));
103*3d8817e4Smiod       else
104*3d8817e4Smiod 	val = (num >> (32 + shift)) & mask;
105*3d8817e4Smiod     }
106*3d8817e4Smiod   else
107*3d8817e4Smiod     val = (num >> shift) & mask;
108*3d8817e4Smiod 
109*3d8817e4Smiod   if (oper->flags & OPERAND_SHIFT)
110*3d8817e4Smiod     val <<= 3;
111*3d8817e4Smiod 
112*3d8817e4Smiod   return val;
113*3d8817e4Smiod }
114*3d8817e4Smiod 
115*3d8817e4Smiod static void
print_insn(struct disassemble_info * info,bfd_vma memaddr,long long num,struct d30v_insn * insn,int is_long,int show_ext)116*3d8817e4Smiod print_insn (struct disassemble_info *info,
117*3d8817e4Smiod 	    bfd_vma memaddr,
118*3d8817e4Smiod 	    long long num,
119*3d8817e4Smiod 	    struct d30v_insn *insn,
120*3d8817e4Smiod 	    int is_long,
121*3d8817e4Smiod 	    int show_ext)
122*3d8817e4Smiod {
123*3d8817e4Smiod   int val, opnum, need_comma = 0;
124*3d8817e4Smiod   struct d30v_operand *oper;
125*3d8817e4Smiod   int i, match, opind = 0, need_paren = 0, found_control = 0;
126*3d8817e4Smiod 
127*3d8817e4Smiod   (*info->fprintf_func) (info->stream, "%s", insn->op->name);
128*3d8817e4Smiod 
129*3d8817e4Smiod   /* Check for CMP or CMPU.  */
130*3d8817e4Smiod   if (d30v_operand_table[insn->form->operands[0]].flags & OPERAND_NAME)
131*3d8817e4Smiod     {
132*3d8817e4Smiod       opind++;
133*3d8817e4Smiod       val =
134*3d8817e4Smiod 	extract_value (num,
135*3d8817e4Smiod 		       (struct d30v_operand *) &d30v_operand_table[insn->form->operands[0]],
136*3d8817e4Smiod 		       is_long);
137*3d8817e4Smiod       (*info->fprintf_func) (info->stream, "%s", d30v_cc_names[val]);
138*3d8817e4Smiod     }
139*3d8817e4Smiod 
140*3d8817e4Smiod   /* Add in ".s" or ".l".  */
141*3d8817e4Smiod   if (show_ext == 2)
142*3d8817e4Smiod     {
143*3d8817e4Smiod       if (is_long)
144*3d8817e4Smiod 	(*info->fprintf_func) (info->stream, ".l");
145*3d8817e4Smiod       else
146*3d8817e4Smiod 	(*info->fprintf_func) (info->stream, ".s");
147*3d8817e4Smiod     }
148*3d8817e4Smiod 
149*3d8817e4Smiod   if (insn->ecc)
150*3d8817e4Smiod     (*info->fprintf_func) (info->stream, "/%s", d30v_ecc_names[insn->ecc]);
151*3d8817e4Smiod 
152*3d8817e4Smiod   (*info->fprintf_func) (info->stream, "\t");
153*3d8817e4Smiod 
154*3d8817e4Smiod   while ((opnum = insn->form->operands[opind++]) != 0)
155*3d8817e4Smiod     {
156*3d8817e4Smiod       int bits;
157*3d8817e4Smiod 
158*3d8817e4Smiod       oper = (struct d30v_operand *) &d30v_operand_table[opnum];
159*3d8817e4Smiod       bits = oper->bits;
160*3d8817e4Smiod       if (oper->flags & OPERAND_SHIFT)
161*3d8817e4Smiod 	bits += 3;
162*3d8817e4Smiod 
163*3d8817e4Smiod       if (need_comma
164*3d8817e4Smiod 	  && oper->flags != OPERAND_PLUS
165*3d8817e4Smiod 	  && oper->flags != OPERAND_MINUS)
166*3d8817e4Smiod 	{
167*3d8817e4Smiod 	  need_comma = 0;
168*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, ", ");
169*3d8817e4Smiod 	}
170*3d8817e4Smiod 
171*3d8817e4Smiod       if (oper->flags == OPERAND_ATMINUS)
172*3d8817e4Smiod 	{
173*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "@-");
174*3d8817e4Smiod 	  continue;
175*3d8817e4Smiod 	}
176*3d8817e4Smiod       if (oper->flags == OPERAND_MINUS)
177*3d8817e4Smiod 	{
178*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "-");
179*3d8817e4Smiod 	  continue;
180*3d8817e4Smiod 	}
181*3d8817e4Smiod       if (oper->flags == OPERAND_PLUS)
182*3d8817e4Smiod 	{
183*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "+");
184*3d8817e4Smiod 	  continue;
185*3d8817e4Smiod 	}
186*3d8817e4Smiod       if (oper->flags == OPERAND_ATSIGN)
187*3d8817e4Smiod 	{
188*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "@");
189*3d8817e4Smiod 	  continue;
190*3d8817e4Smiod 	}
191*3d8817e4Smiod       if (oper->flags == OPERAND_ATPAR)
192*3d8817e4Smiod 	{
193*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "@(");
194*3d8817e4Smiod 	  need_paren = 1;
195*3d8817e4Smiod 	  continue;
196*3d8817e4Smiod 	}
197*3d8817e4Smiod 
198*3d8817e4Smiod       if (oper->flags == OPERAND_SPECIAL)
199*3d8817e4Smiod 	continue;
200*3d8817e4Smiod 
201*3d8817e4Smiod       val = extract_value (num, oper, is_long);
202*3d8817e4Smiod 
203*3d8817e4Smiod       if (oper->flags & OPERAND_REG)
204*3d8817e4Smiod 	{
205*3d8817e4Smiod 	  match = 0;
206*3d8817e4Smiod 	  if (oper->flags & OPERAND_CONTROL)
207*3d8817e4Smiod 	    {
208*3d8817e4Smiod 	      struct d30v_operand *oper3 =
209*3d8817e4Smiod 		(struct d30v_operand *) &d30v_operand_table[insn->form->operands[2]];
210*3d8817e4Smiod 	      int id = extract_value (num, oper3, is_long);
211*3d8817e4Smiod 
212*3d8817e4Smiod 	      found_control = 1;
213*3d8817e4Smiod 	      switch (id)
214*3d8817e4Smiod 		{
215*3d8817e4Smiod 		case 0:
216*3d8817e4Smiod 		  val |= OPERAND_CONTROL;
217*3d8817e4Smiod 		  break;
218*3d8817e4Smiod 		case 1:
219*3d8817e4Smiod 		case 2:
220*3d8817e4Smiod 		  val = OPERAND_CONTROL + MAX_CONTROL_REG + id;
221*3d8817e4Smiod 		  break;
222*3d8817e4Smiod 		case 3:
223*3d8817e4Smiod 		  val |= OPERAND_FLAG;
224*3d8817e4Smiod 		  break;
225*3d8817e4Smiod 		default:
226*3d8817e4Smiod 		  fprintf (stderr, "illegal id (%d)\n", id);
227*3d8817e4Smiod 		}
228*3d8817e4Smiod 	    }
229*3d8817e4Smiod 	  else if (oper->flags & OPERAND_ACC)
230*3d8817e4Smiod 	    val |= OPERAND_ACC;
231*3d8817e4Smiod 	  else if (oper->flags & OPERAND_FLAG)
232*3d8817e4Smiod 	    val |= OPERAND_FLAG;
233*3d8817e4Smiod 	  for (i = 0; i < reg_name_cnt (); i++)
234*3d8817e4Smiod 	    {
235*3d8817e4Smiod 	      if (val == pre_defined_registers[i].value)
236*3d8817e4Smiod 		{
237*3d8817e4Smiod 		  if (pre_defined_registers[i].pname)
238*3d8817e4Smiod 		    (*info->fprintf_func)
239*3d8817e4Smiod 		      (info->stream, "%s", pre_defined_registers[i].pname);
240*3d8817e4Smiod 		  else
241*3d8817e4Smiod 		    (*info->fprintf_func)
242*3d8817e4Smiod 		      (info->stream, "%s", pre_defined_registers[i].name);
243*3d8817e4Smiod 		  match = 1;
244*3d8817e4Smiod 		  break;
245*3d8817e4Smiod 		}
246*3d8817e4Smiod 	    }
247*3d8817e4Smiod 	  if (match == 0)
248*3d8817e4Smiod 	    {
249*3d8817e4Smiod 	      /* This would only get executed if a register was not in
250*3d8817e4Smiod 		 the register table.  */
251*3d8817e4Smiod 	      (*info->fprintf_func)
252*3d8817e4Smiod 		(info->stream, _("<unknown register %d>"), val & 0x3F);
253*3d8817e4Smiod 	    }
254*3d8817e4Smiod 	}
255*3d8817e4Smiod       /* repeati has a relocation, but its first argument is a plain
256*3d8817e4Smiod 	 immediate.  OTOH instructions like djsri have a pc-relative
257*3d8817e4Smiod 	 delay target, but an absolute jump target.  Therefore, a test
258*3d8817e4Smiod 	 of insn->op->reloc_flag is not specific enough; we must test
259*3d8817e4Smiod 	 if the actual operand we are handling now is pc-relative.  */
260*3d8817e4Smiod       else if (oper->flags & OPERAND_PCREL)
261*3d8817e4Smiod 	{
262*3d8817e4Smiod 	  int neg = 0;
263*3d8817e4Smiod 
264*3d8817e4Smiod 	  /* IMM6S3 is unsigned.  */
265*3d8817e4Smiod 	  if (oper->flags & OPERAND_SIGNED || bits == 32)
266*3d8817e4Smiod 	    {
267*3d8817e4Smiod 	      long max;
268*3d8817e4Smiod 	      max = (1 << (bits - 1));
269*3d8817e4Smiod 	      if (val & max)
270*3d8817e4Smiod 		{
271*3d8817e4Smiod 		  if (bits == 32)
272*3d8817e4Smiod 		    val = -val;
273*3d8817e4Smiod 		  else
274*3d8817e4Smiod 		    val = -val & ((1 << bits) - 1);
275*3d8817e4Smiod 		  neg = 1;
276*3d8817e4Smiod 		}
277*3d8817e4Smiod 	    }
278*3d8817e4Smiod 	  if (neg)
279*3d8817e4Smiod 	    {
280*3d8817e4Smiod 	      (*info->fprintf_func) (info->stream, "-%x\t(", val);
281*3d8817e4Smiod 	      (*info->print_address_func) ((memaddr - val) & PC_MASK, info);
282*3d8817e4Smiod 	      (*info->fprintf_func) (info->stream, ")");
283*3d8817e4Smiod 	    }
284*3d8817e4Smiod 	  else
285*3d8817e4Smiod 	    {
286*3d8817e4Smiod 	      (*info->fprintf_func) (info->stream, "%x\t(", val);
287*3d8817e4Smiod 	      (*info->print_address_func) ((memaddr + val) & PC_MASK, info);
288*3d8817e4Smiod 	      (*info->fprintf_func) (info->stream, ")");
289*3d8817e4Smiod 	    }
290*3d8817e4Smiod 	}
291*3d8817e4Smiod       else if (insn->op->reloc_flag == RELOC_ABS)
292*3d8817e4Smiod 	{
293*3d8817e4Smiod 	  (*info->print_address_func) (val, info);
294*3d8817e4Smiod 	}
295*3d8817e4Smiod       else
296*3d8817e4Smiod 	{
297*3d8817e4Smiod 	  if (oper->flags & OPERAND_SIGNED)
298*3d8817e4Smiod 	    {
299*3d8817e4Smiod 	      int max = (1 << (bits - 1));
300*3d8817e4Smiod 
301*3d8817e4Smiod 	      if (val & max)
302*3d8817e4Smiod 		{
303*3d8817e4Smiod 		  val = -val;
304*3d8817e4Smiod 		  if (bits < 32)
305*3d8817e4Smiod 		    val &= ((1 << bits) - 1);
306*3d8817e4Smiod 		  (*info->fprintf_func) (info->stream, "-");
307*3d8817e4Smiod 		}
308*3d8817e4Smiod 	    }
309*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "0x%x", val);
310*3d8817e4Smiod 	}
311*3d8817e4Smiod       /* If there is another operand, then write a comma and space.  */
312*3d8817e4Smiod       if (insn->form->operands[opind] && !(found_control && opind == 2))
313*3d8817e4Smiod 	need_comma = 1;
314*3d8817e4Smiod     }
315*3d8817e4Smiod   if (need_paren)
316*3d8817e4Smiod     (*info->fprintf_func) (info->stream, ")");
317*3d8817e4Smiod }
318*3d8817e4Smiod 
319*3d8817e4Smiod int
print_insn_d30v(bfd_vma memaddr,struct disassemble_info * info)320*3d8817e4Smiod print_insn_d30v (bfd_vma memaddr, struct disassemble_info *info)
321*3d8817e4Smiod {
322*3d8817e4Smiod   int status, result;
323*3d8817e4Smiod   bfd_byte buffer[12];
324*3d8817e4Smiod   unsigned long in1, in2;
325*3d8817e4Smiod   struct d30v_insn insn;
326*3d8817e4Smiod   long long num;
327*3d8817e4Smiod 
328*3d8817e4Smiod   insn.form = NULL;
329*3d8817e4Smiod 
330*3d8817e4Smiod   info->bytes_per_line = 8;
331*3d8817e4Smiod   info->bytes_per_chunk = 4;
332*3d8817e4Smiod   info->display_endian = BFD_ENDIAN_BIG;
333*3d8817e4Smiod 
334*3d8817e4Smiod   status = (*info->read_memory_func) (memaddr, buffer, 4, info);
335*3d8817e4Smiod   if (status != 0)
336*3d8817e4Smiod     {
337*3d8817e4Smiod       (*info->memory_error_func) (status, memaddr, info);
338*3d8817e4Smiod       return -1;
339*3d8817e4Smiod     }
340*3d8817e4Smiod   in1 = bfd_getb32 (buffer);
341*3d8817e4Smiod 
342*3d8817e4Smiod   status = (*info->read_memory_func) (memaddr + 4, buffer, 4, info);
343*3d8817e4Smiod   if (status != 0)
344*3d8817e4Smiod     {
345*3d8817e4Smiod       info->bytes_per_line = 8;
346*3d8817e4Smiod       if (!(result = lookup_opcode (&insn, in1, 0)))
347*3d8817e4Smiod 	(*info->fprintf_func) (info->stream, ".long\t0x%lx", in1);
348*3d8817e4Smiod       else
349*3d8817e4Smiod 	print_insn (info, memaddr, (long long) in1, &insn, 0, result);
350*3d8817e4Smiod       return 4;
351*3d8817e4Smiod     }
352*3d8817e4Smiod   in2 = bfd_getb32 (buffer);
353*3d8817e4Smiod 
354*3d8817e4Smiod   if (in1 & in2 & FM01)
355*3d8817e4Smiod     {
356*3d8817e4Smiod       /* LONG instruction.  */
357*3d8817e4Smiod       if (!(result = lookup_opcode (&insn, in1, 1)))
358*3d8817e4Smiod 	{
359*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, ".long\t0x%lx,0x%lx", in1, in2);
360*3d8817e4Smiod 	  return 8;
361*3d8817e4Smiod 	}
362*3d8817e4Smiod       num = (long long) in1 << 32 | in2;
363*3d8817e4Smiod       print_insn (info, memaddr, num, &insn, 1, result);
364*3d8817e4Smiod     }
365*3d8817e4Smiod   else
366*3d8817e4Smiod     {
367*3d8817e4Smiod       num = in1;
368*3d8817e4Smiod       if (!(result = lookup_opcode (&insn, in1, 0)))
369*3d8817e4Smiod 	(*info->fprintf_func) (info->stream, ".long\t0x%lx", in1);
370*3d8817e4Smiod       else
371*3d8817e4Smiod 	print_insn (info, memaddr, num, &insn, 0, result);
372*3d8817e4Smiod 
373*3d8817e4Smiod       switch (((in1 >> 31) << 1) | (in2 >> 31))
374*3d8817e4Smiod 	{
375*3d8817e4Smiod 	case 0:
376*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "\t||\t");
377*3d8817e4Smiod 	  break;
378*3d8817e4Smiod 	case 1:
379*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "\t->\t");
380*3d8817e4Smiod 	  break;
381*3d8817e4Smiod 	case 2:
382*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "\t<-\t");
383*3d8817e4Smiod 	default:
384*3d8817e4Smiod 	  break;
385*3d8817e4Smiod 	}
386*3d8817e4Smiod 
387*3d8817e4Smiod       insn.form = NULL;
388*3d8817e4Smiod       num = in2;
389*3d8817e4Smiod       if (!(result = lookup_opcode (&insn, in2, 0)))
390*3d8817e4Smiod 	(*info->fprintf_func) (info->stream, ".long\t0x%lx", in2);
391*3d8817e4Smiod       else
392*3d8817e4Smiod 	print_insn (info, memaddr, num, &insn, 0, result);
393*3d8817e4Smiod     }
394*3d8817e4Smiod   return 8;
395*3d8817e4Smiod }
396