start-sanitize-d10v
[deliverable/binutils-gdb.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_REG)
110 {
111 int i;
112 int match=0;
113 num += oper->flags & (OPERAND_ACC|OPERAND_FLAG|OPERAND_CONTROL);
114 for (i=0;i<reg_name_cnt();i++)
115 {
116 if (num == pre_defined_registers[i].value)
117 {
118 if (pre_defined_registers[i].pname)
119 strcpy(buf,pre_defined_registers[i].pname);
120 else
121 strcpy(buf,pre_defined_registers[i].name);
122 match=1;
123 break;
124 }
125 }
126 if (match==0)
127 {
128 if (oper->flags & OPERAND_ACC)
129 *buf++ = 'a';
130 else if (oper->flags & OPERAND_CONTROL)
131 {
132 *buf++ ='c';
133 *buf++ ='r';
134 }
135 else if(oper->flags & OPERAND_REG)
136 *buf++ = 'r';
137 sprintf (buf, "%d", num);
138 }
139 }
140 else
141 sprintf (buf, "0x%x", num);
142 }
143
144
145 static void
146 dis_long (insn, str)
147 unsigned long insn;
148 char *str;
149 {
150 int i;
151 char buf[32];
152 struct d10v_opcode *op = (struct d10v_opcode *)d10v_opcodes;
153 struct d10v_operand *oper;
154 int need_paren = 0;
155
156 while (op->name)
157 {
158 if ((op->format & LONG_OPCODE) && ((op->mask & insn) == op->opcode))
159 {
160 strcpy (str, op->name);
161 strcat (str, "\t");
162 for ( i=0; op->operands[i]; i++)
163 {
164 oper = (struct d10v_operand *)&d10v_operands[op->operands[i]];
165 if (oper->flags == OPERAND_ATPAR)
166 need_paren = 1;
167 print_operand (buf, oper, insn, op);
168 strcat (str, buf);
169 if (op->operands[i+1] && oper->bits &&
170 d10v_operands[op->operands[i+1]].flags != OPERAND_PLUS &&
171 d10v_operands[op->operands[i+1]].flags != OPERAND_MINUS)
172 strcat (str,", ");
173 }
174 break;
175 }
176 op++;
177 }
178 if (need_paren)
179 strcat (str, ")");
180 }
181
182 static void
183 dis_2_short (insn, str, order)
184 unsigned long insn;
185 char *str;
186 int order;
187 {
188 int i,j;
189 char astr[2][32];
190 unsigned int ins[2];
191 struct d10v_opcode *op;
192 char buf[32];
193 int match, num_match=0;
194 struct d10v_operand *oper;
195 int need_paren = 0;
196
197 ins[0] = (insn & 0x3FFFFFFF) >> 15;
198 ins[1] = insn & 0x00007FFF;
199
200 *str = 0;
201
202 for(j=0;j<2;j++)
203 {
204 op = (struct d10v_opcode *)d10v_opcodes;
205 match=0;
206 while (op->name)
207 {
208 if ((op->format & SHORT_OPCODE) && ((op->mask & ins[j]) == op->opcode))
209 {
210 strcat (str, op->name);
211 strcat (str, "\t");
212 for (i=0; op->operands[i]; i++)
213 {
214 oper = (struct d10v_operand *)&d10v_operands[op->operands[i]];
215 if (oper->flags == OPERAND_ATPAR)
216 need_paren = 1;
217 print_operand (buf, oper, ins[j], op);
218 strcat (str, buf);
219 if (op->operands[i+1] && oper->bits &&
220 d10v_operands[op->operands[i+1]].flags != OPERAND_PLUS &&
221 d10v_operands[op->operands[i+1]].flags != OPERAND_MINUS)
222 strcat( str,", ");
223 }
224 match = 1;
225 num_match++;
226 break;
227 }
228 op++;
229 }
230 if (!match)
231 strcat (str, "unknown");
232
233 switch (order)
234 {
235 case 0:
236 strcat ( str, "\t->\t");
237 order = -1;
238 break;
239 case 1:
240 strcat (str, "\t<-\t");
241 order = -1;
242 break;
243 case 2:
244 strcat (str, "\t||\t");
245 order = -1;
246 break;
247 default:
248 break;
249 }
250 }
251
252 if (num_match == 0)
253 sprintf (str, ".long\t0x%08x", insn);
254
255 if (need_paren)
256 strcat (str, ")");
257 }
This page took 0.076202 seconds and 5 git commands to generate.