start-sanitize-d10v
[external/binutils.git] / opcodes / d10v-dis.c
1 /* Disassemble D10V instructions.
2    Copyright (C) 1996 Free Software Foundation, Inc.
3
4 This program 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
19 #include <stdio.h>
20
21 #include "opcode/d10v.h" 
22 #include "dis-asm.h"
23
24 static void dis_2_short PARAMS ((unsigned long insn, char *str, int order));
25 static void dis_long PARAMS ((unsigned long insn, char *str));
26
27 int 
28 print_insn_d10v (memaddr, info)
29      bfd_vma memaddr;
30      struct disassemble_info *info;
31 {
32   int status;
33   bfd_byte buffer[4];
34   unsigned long insn;
35   char str[64];
36
37   strcpy (str, "unknown");
38
39   status = (*info->read_memory_func) (memaddr, buffer, 4, info);
40   if (status != 0)
41     {
42       (*info->memory_error_func) (status, memaddr, info);
43       return -1;
44     }
45   insn = bfd_getb32 (buffer);
46
47   status = insn & FM11;
48   switch (status) {
49   case 0:
50     dis_2_short (insn, str, 2);
51     break;
52   case FM01:
53     dis_2_short (insn, str, 0);
54     break;
55   case FM10:
56     dis_2_short (insn, str, 1);
57     break;
58   case FM11:
59     dis_long (insn, str);
60     break;
61   }
62   (*info->fprintf_func) (info->stream, "\t%s", str, insn);  
63   return 4;
64 }
65
66 static void
67 print_operand (buf, oper, insn, op)
68      char *buf;
69      struct d10v_operand *oper;
70      unsigned long insn;
71      struct d10v_opcode *op;
72 {
73   int num, shift;
74
75   if (oper->flags == OPERAND_ATMINUS)
76     {
77       strcpy (buf,"@-");
78       return;
79     }
80   if (oper->flags == OPERAND_MINUS)
81     {
82       strcpy (buf,"-");
83       return;
84     }
85   if (oper->flags == OPERAND_PLUS)
86     {
87       strcpy (buf,"+");
88       return;
89     }
90   if (oper->flags == OPERAND_ATSIGN)
91     {
92       strcpy (buf,"@");
93       return;
94     }
95   if (oper->flags == OPERAND_ATPAR)
96     {
97       strcpy (buf,"@(");
98       return;
99     }
100
101   shift = oper->shift;
102
103   /* the LONG_L format shifts registers over by 15 */
104   if (op->format == LONG_L && (oper->flags & OPERAND_REG))
105     shift += 15;
106
107   num = (insn >> shift) & (0x7FFFFFFF >> (31 - oper->bits));
108
109   if (oper->flags & OPERAND_ACC)
110     *buf++ = 'a';
111   else if (oper->flags & OPERAND_CONTROL)
112     {
113       *buf++ ='c';
114       *buf++ ='r';
115     }
116   else if(oper->flags & OPERAND_REG)
117     *buf++ = 'r';
118
119   if (oper->flags & OPERAND_REG)
120     sprintf (buf, "%d", num);
121   else
122     sprintf (buf, "0x%x", num);
123 }
124
125
126 static void
127 dis_long (insn, str)
128      unsigned long insn;
129      char *str;
130 {
131   int i;
132   char buf[32];
133   struct d10v_opcode *op = (struct d10v_opcode *)d10v_opcodes;
134   struct d10v_operand *oper;
135   int need_paren = 0;
136
137   while (op->name)
138     {
139       if ((op->format & LONG_OPCODE) && ((op->mask & insn) == op->opcode))
140         {
141           strcpy (str, op->name);
142           strcat (str, "\t");
143           for ( i=0; op->operands[i]; i++)
144             {
145               oper = (struct d10v_operand *)&d10v_operands[op->operands[i]];
146               if (oper->flags == OPERAND_ATPAR)
147                 need_paren = 1;
148               print_operand (buf, oper, insn, op);
149               strcat (str, buf);
150               if (op->operands[i+1] && oper->bits &&
151                   d10v_operands[op->operands[i+1]].flags != OPERAND_PLUS &&
152                   d10v_operands[op->operands[i+1]].flags != OPERAND_MINUS)
153                 strcat (str,", ");
154             }
155           break;
156         }
157       op++;
158     }
159   if (need_paren)
160     strcat (str, ")");
161 }
162
163 static void
164 dis_2_short (insn, str, order)
165      unsigned long insn;
166      char *str;
167      int order;
168 {
169   int i,j;
170   char astr[2][32];
171   unsigned int ins[2];
172   struct d10v_opcode *op;
173   char buf[32];
174   int match, num_match=0;
175   struct d10v_operand *oper;
176   int need_paren = 0;
177
178   ins[0] = (insn & 0x3FFFFFFF) >> 15;
179   ins[1] = insn & 0x00007FFF;
180
181   *str = 0;
182
183   for(j=0;j<2;j++)
184     {
185       op = (struct d10v_opcode *)d10v_opcodes;
186       match=0;
187       while (op->name)
188         {
189           if ((op->format & SHORT_OPCODE) && ((op->mask & ins[j]) == op->opcode))
190             {
191               strcat (str, op->name);
192               strcat (str, "\t");
193               for (i=0; op->operands[i]; i++)
194                 {
195                   oper = (struct d10v_operand *)&d10v_operands[op->operands[i]];
196                   if (oper->flags == OPERAND_ATPAR)
197                     need_paren = 1;
198                   print_operand (buf, oper, ins[j], op);
199                   strcat (str, buf);
200                   if (op->operands[i+1] && oper->bits && 
201                   d10v_operands[op->operands[i+1]].flags != OPERAND_PLUS &&
202                   d10v_operands[op->operands[i+1]].flags != OPERAND_MINUS)
203                     strcat( str,", ");
204                 }
205               match = 1;
206               num_match++;
207               break;
208             }
209           op++;
210         }
211       if (!match)
212           strcat (str, "unknown");
213
214       switch (order)
215         {
216         case 0:
217           strcat ( str, "\t->\t");
218           order = -1;
219           break;
220         case 1:
221           strcat (str, "\t<-\t");
222           order = -1;
223           break;
224         case 2:
225           strcat (str, "\t||\t");
226           order = -1;
227           break;
228         default:
229           break;
230         }
231     }
232
233   if (num_match == 0)
234     sprintf (str, ".long\t0x%08x", insn);
235
236   if (need_paren)
237     strcat (str, ")");
238 }