xref: /openbsd-src/gnu/usr.bin/binutils-2.17/opcodes/d10v-dis.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* Disassemble D10V instructions.
2*3d8817e4Smiod    Copyright 1996, 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 
21*3d8817e4Smiod #include "sysdep.h"
22*3d8817e4Smiod #include "opcode/d10v.h"
23*3d8817e4Smiod #include "dis-asm.h"
24*3d8817e4Smiod 
25*3d8817e4Smiod /* The PC wraps at 18 bits, except for the segment number,
26*3d8817e4Smiod    so use this mask to keep the parts we want.  */
27*3d8817e4Smiod #define PC_MASK	0x0303FFFF
28*3d8817e4Smiod 
29*3d8817e4Smiod static void
print_operand(struct d10v_operand * oper,unsigned long insn,struct d10v_opcode * op,bfd_vma memaddr,struct disassemble_info * info)30*3d8817e4Smiod print_operand (struct d10v_operand *oper,
31*3d8817e4Smiod 	       unsigned long insn,
32*3d8817e4Smiod 	       struct d10v_opcode *op,
33*3d8817e4Smiod 	       bfd_vma memaddr,
34*3d8817e4Smiod 	       struct disassemble_info *info)
35*3d8817e4Smiod {
36*3d8817e4Smiod   int num, shift;
37*3d8817e4Smiod 
38*3d8817e4Smiod   if (oper->flags == OPERAND_ATMINUS)
39*3d8817e4Smiod     {
40*3d8817e4Smiod       (*info->fprintf_func) (info->stream, "@-");
41*3d8817e4Smiod       return;
42*3d8817e4Smiod     }
43*3d8817e4Smiod   if (oper->flags == OPERAND_MINUS)
44*3d8817e4Smiod     {
45*3d8817e4Smiod       (*info->fprintf_func) (info->stream, "-");
46*3d8817e4Smiod       return;
47*3d8817e4Smiod     }
48*3d8817e4Smiod   if (oper->flags == OPERAND_PLUS)
49*3d8817e4Smiod     {
50*3d8817e4Smiod       (*info->fprintf_func) (info->stream, "+");
51*3d8817e4Smiod       return;
52*3d8817e4Smiod     }
53*3d8817e4Smiod   if (oper->flags == OPERAND_ATSIGN)
54*3d8817e4Smiod     {
55*3d8817e4Smiod       (*info->fprintf_func) (info->stream, "@");
56*3d8817e4Smiod       return;
57*3d8817e4Smiod     }
58*3d8817e4Smiod   if (oper->flags == OPERAND_ATPAR)
59*3d8817e4Smiod     {
60*3d8817e4Smiod       (*info->fprintf_func) (info->stream, "@(");
61*3d8817e4Smiod       return;
62*3d8817e4Smiod     }
63*3d8817e4Smiod 
64*3d8817e4Smiod   shift = oper->shift;
65*3d8817e4Smiod 
66*3d8817e4Smiod   /* The LONG_L format shifts registers over by 15.  */
67*3d8817e4Smiod   if (op->format == LONG_L && (oper->flags & OPERAND_REG))
68*3d8817e4Smiod     shift += 15;
69*3d8817e4Smiod 
70*3d8817e4Smiod   num = (insn >> shift) & (0x7FFFFFFF >> (31 - oper->bits));
71*3d8817e4Smiod 
72*3d8817e4Smiod   if (oper->flags & OPERAND_REG)
73*3d8817e4Smiod     {
74*3d8817e4Smiod       int i;
75*3d8817e4Smiod       int match = 0;
76*3d8817e4Smiod 
77*3d8817e4Smiod       num += (oper->flags
78*3d8817e4Smiod 	      & (OPERAND_GPR | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL));
79*3d8817e4Smiod       if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
80*3d8817e4Smiod 	num += num ? OPERAND_ACC1 : OPERAND_ACC0;
81*3d8817e4Smiod       for (i = 0; i < d10v_reg_name_cnt (); i++)
82*3d8817e4Smiod 	{
83*3d8817e4Smiod 	  if (num == (d10v_predefined_registers[i].value & ~ OPERAND_SP))
84*3d8817e4Smiod 	    {
85*3d8817e4Smiod 	      if (d10v_predefined_registers[i].pname)
86*3d8817e4Smiod 		(*info->fprintf_func) (info->stream, "%s",
87*3d8817e4Smiod 				       d10v_predefined_registers[i].pname);
88*3d8817e4Smiod 	      else
89*3d8817e4Smiod 		(*info->fprintf_func) (info->stream, "%s",
90*3d8817e4Smiod 				       d10v_predefined_registers[i].name);
91*3d8817e4Smiod 	      match = 1;
92*3d8817e4Smiod 	      break;
93*3d8817e4Smiod 	    }
94*3d8817e4Smiod 	}
95*3d8817e4Smiod       if (match == 0)
96*3d8817e4Smiod 	{
97*3d8817e4Smiod 	  /* This would only get executed if a register was not in the
98*3d8817e4Smiod 	     register table.  */
99*3d8817e4Smiod 	  if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
100*3d8817e4Smiod 	    (*info->fprintf_func) (info->stream, "a");
101*3d8817e4Smiod 	  else if (oper->flags & OPERAND_CONTROL)
102*3d8817e4Smiod 	    (*info->fprintf_func) (info->stream, "cr");
103*3d8817e4Smiod 	  else if (oper->flags & OPERAND_REG)
104*3d8817e4Smiod 	    (*info->fprintf_func) (info->stream, "r");
105*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "%d", num & REGISTER_MASK);
106*3d8817e4Smiod 	}
107*3d8817e4Smiod     }
108*3d8817e4Smiod   else
109*3d8817e4Smiod     {
110*3d8817e4Smiod       /* Addresses are right-shifted by 2.  */
111*3d8817e4Smiod       if (oper->flags & OPERAND_ADDR)
112*3d8817e4Smiod 	{
113*3d8817e4Smiod 	  long max;
114*3d8817e4Smiod 	  int neg = 0;
115*3d8817e4Smiod 
116*3d8817e4Smiod 	  max = (1 << (oper->bits - 1));
117*3d8817e4Smiod 	  if (num & max)
118*3d8817e4Smiod 	    {
119*3d8817e4Smiod 	      num = -num & ((1 << oper->bits) - 1);
120*3d8817e4Smiod 	      neg = 1;
121*3d8817e4Smiod 	    }
122*3d8817e4Smiod 	  num = num << 2;
123*3d8817e4Smiod 	  if (info->flags & INSN_HAS_RELOC)
124*3d8817e4Smiod 	    (*info->print_address_func) (num & PC_MASK, info);
125*3d8817e4Smiod 	  else
126*3d8817e4Smiod 	    {
127*3d8817e4Smiod 	      if (neg)
128*3d8817e4Smiod 		(*info->print_address_func) ((memaddr - num) & PC_MASK, info);
129*3d8817e4Smiod 	      else
130*3d8817e4Smiod 		(*info->print_address_func) ((memaddr + num) & PC_MASK, info);
131*3d8817e4Smiod 	    }
132*3d8817e4Smiod 	}
133*3d8817e4Smiod       else
134*3d8817e4Smiod 	{
135*3d8817e4Smiod 	  if (oper->flags & OPERAND_SIGNED)
136*3d8817e4Smiod 	    {
137*3d8817e4Smiod 	      int max = (1 << (oper->bits - 1));
138*3d8817e4Smiod 	      if (num & max)
139*3d8817e4Smiod 		{
140*3d8817e4Smiod 		  num = -num & ((1 << oper->bits) - 1);
141*3d8817e4Smiod 		  (*info->fprintf_func) (info->stream, "-");
142*3d8817e4Smiod 		}
143*3d8817e4Smiod 	    }
144*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "0x%x", num);
145*3d8817e4Smiod 	}
146*3d8817e4Smiod     }
147*3d8817e4Smiod }
148*3d8817e4Smiod 
149*3d8817e4Smiod static void
dis_long(unsigned long insn,bfd_vma memaddr,struct disassemble_info * info)150*3d8817e4Smiod dis_long (unsigned long insn,
151*3d8817e4Smiod 	  bfd_vma memaddr,
152*3d8817e4Smiod 	  struct disassemble_info *info)
153*3d8817e4Smiod {
154*3d8817e4Smiod   int i;
155*3d8817e4Smiod   struct d10v_opcode *op = (struct d10v_opcode *) d10v_opcodes;
156*3d8817e4Smiod   struct d10v_operand *oper;
157*3d8817e4Smiod   int need_paren = 0;
158*3d8817e4Smiod   int match = 0;
159*3d8817e4Smiod 
160*3d8817e4Smiod   while (op->name)
161*3d8817e4Smiod     {
162*3d8817e4Smiod       if ((op->format & LONG_OPCODE)
163*3d8817e4Smiod 	  && ((op->mask & insn) == (unsigned long) op->opcode))
164*3d8817e4Smiod 	{
165*3d8817e4Smiod 	  match = 1;
166*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "%s\t", op->name);
167*3d8817e4Smiod 
168*3d8817e4Smiod 	  for (i = 0; op->operands[i]; i++)
169*3d8817e4Smiod 	    {
170*3d8817e4Smiod 	      oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
171*3d8817e4Smiod 	      if (oper->flags == OPERAND_ATPAR)
172*3d8817e4Smiod 		need_paren = 1;
173*3d8817e4Smiod 	      print_operand (oper, insn, op, memaddr, info);
174*3d8817e4Smiod 	      if (op->operands[i + 1] && oper->bits
175*3d8817e4Smiod 		  && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
176*3d8817e4Smiod 		  && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
177*3d8817e4Smiod 		(*info->fprintf_func) (info->stream, ", ");
178*3d8817e4Smiod 	    }
179*3d8817e4Smiod 	  break;
180*3d8817e4Smiod 	}
181*3d8817e4Smiod       op++;
182*3d8817e4Smiod     }
183*3d8817e4Smiod 
184*3d8817e4Smiod   if (!match)
185*3d8817e4Smiod     (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
186*3d8817e4Smiod 
187*3d8817e4Smiod   if (need_paren)
188*3d8817e4Smiod     (*info->fprintf_func) (info->stream, ")");
189*3d8817e4Smiod }
190*3d8817e4Smiod 
191*3d8817e4Smiod static void
dis_2_short(unsigned long insn,bfd_vma memaddr,struct disassemble_info * info,int order)192*3d8817e4Smiod dis_2_short (unsigned long insn,
193*3d8817e4Smiod 	     bfd_vma memaddr,
194*3d8817e4Smiod 	     struct disassemble_info *info,
195*3d8817e4Smiod 	     int order)
196*3d8817e4Smiod {
197*3d8817e4Smiod   int i, j;
198*3d8817e4Smiod   unsigned int ins[2];
199*3d8817e4Smiod   struct d10v_opcode *op;
200*3d8817e4Smiod   int match, num_match = 0;
201*3d8817e4Smiod   struct d10v_operand *oper;
202*3d8817e4Smiod   int need_paren = 0;
203*3d8817e4Smiod 
204*3d8817e4Smiod   ins[0] = (insn & 0x3FFFFFFF) >> 15;
205*3d8817e4Smiod   ins[1] = insn & 0x00007FFF;
206*3d8817e4Smiod 
207*3d8817e4Smiod   for (j = 0; j < 2; j++)
208*3d8817e4Smiod     {
209*3d8817e4Smiod       op = (struct d10v_opcode *) d10v_opcodes;
210*3d8817e4Smiod       match = 0;
211*3d8817e4Smiod       while (op->name)
212*3d8817e4Smiod 	{
213*3d8817e4Smiod 	  if ((op->format & SHORT_OPCODE)
214*3d8817e4Smiod 	      && ((((unsigned int) op->mask) & ins[j])
215*3d8817e4Smiod 		  == (unsigned int) op->opcode))
216*3d8817e4Smiod 	    {
217*3d8817e4Smiod 	      (*info->fprintf_func) (info->stream, "%s\t", op->name);
218*3d8817e4Smiod 	      for (i = 0; op->operands[i]; i++)
219*3d8817e4Smiod 		{
220*3d8817e4Smiod 		  oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
221*3d8817e4Smiod 		  if (oper->flags == OPERAND_ATPAR)
222*3d8817e4Smiod 		    need_paren = 1;
223*3d8817e4Smiod 		  print_operand (oper, ins[j], op, memaddr, info);
224*3d8817e4Smiod 		  if (op->operands[i + 1] && oper->bits
225*3d8817e4Smiod 		      && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
226*3d8817e4Smiod 		      && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
227*3d8817e4Smiod 		    (*info->fprintf_func) (info->stream, ", ");
228*3d8817e4Smiod 		}
229*3d8817e4Smiod 	      match = 1;
230*3d8817e4Smiod 	      num_match++;
231*3d8817e4Smiod 	      break;
232*3d8817e4Smiod 	    }
233*3d8817e4Smiod 	  op++;
234*3d8817e4Smiod 	}
235*3d8817e4Smiod       if (!match)
236*3d8817e4Smiod 	(*info->fprintf_func) (info->stream, "unknown");
237*3d8817e4Smiod 
238*3d8817e4Smiod       switch (order)
239*3d8817e4Smiod 	{
240*3d8817e4Smiod 	case 0:
241*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "\t->\t");
242*3d8817e4Smiod 	  order = -1;
243*3d8817e4Smiod 	  break;
244*3d8817e4Smiod 	case 1:
245*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "\t<-\t");
246*3d8817e4Smiod 	  order = -1;
247*3d8817e4Smiod 	  break;
248*3d8817e4Smiod 	case 2:
249*3d8817e4Smiod 	  (*info->fprintf_func) (info->stream, "\t||\t");
250*3d8817e4Smiod 	  order = -1;
251*3d8817e4Smiod 	  break;
252*3d8817e4Smiod 	default:
253*3d8817e4Smiod 	  break;
254*3d8817e4Smiod 	}
255*3d8817e4Smiod     }
256*3d8817e4Smiod 
257*3d8817e4Smiod   if (num_match == 0)
258*3d8817e4Smiod     (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
259*3d8817e4Smiod 
260*3d8817e4Smiod   if (need_paren)
261*3d8817e4Smiod     (*info->fprintf_func) (info->stream, ")");
262*3d8817e4Smiod }
263*3d8817e4Smiod 
264*3d8817e4Smiod int
print_insn_d10v(bfd_vma memaddr,struct disassemble_info * info)265*3d8817e4Smiod print_insn_d10v (bfd_vma memaddr, struct disassemble_info *info)
266*3d8817e4Smiod {
267*3d8817e4Smiod   int status;
268*3d8817e4Smiod   bfd_byte buffer[4];
269*3d8817e4Smiod   unsigned long insn;
270*3d8817e4Smiod 
271*3d8817e4Smiod   status = (*info->read_memory_func) (memaddr, buffer, 4, info);
272*3d8817e4Smiod   if (status != 0)
273*3d8817e4Smiod     {
274*3d8817e4Smiod       (*info->memory_error_func) (status, memaddr, info);
275*3d8817e4Smiod       return -1;
276*3d8817e4Smiod     }
277*3d8817e4Smiod   insn = bfd_getb32 (buffer);
278*3d8817e4Smiod 
279*3d8817e4Smiod   status = insn & FM11;
280*3d8817e4Smiod   switch (status)
281*3d8817e4Smiod     {
282*3d8817e4Smiod     case 0:
283*3d8817e4Smiod       dis_2_short (insn, memaddr, info, 2);
284*3d8817e4Smiod       break;
285*3d8817e4Smiod     case FM01:
286*3d8817e4Smiod       dis_2_short (insn, memaddr, info, 0);
287*3d8817e4Smiod       break;
288*3d8817e4Smiod     case FM10:
289*3d8817e4Smiod       dis_2_short (insn, memaddr, info, 1);
290*3d8817e4Smiod       break;
291*3d8817e4Smiod     case FM11:
292*3d8817e4Smiod       dis_long (insn, memaddr, info);
293*3d8817e4Smiod       break;
294*3d8817e4Smiod     }
295*3d8817e4Smiod   return 4;
296*3d8817e4Smiod }
297