]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - opcodes/d10v-dis.c
2005-05-07 H.J. Lu <hongjiu.lu@intel.com>
[thirdparty/binutils-gdb.git] / opcodes / d10v-dis.c
CommitLineData
252b5132 1/* Disassemble D10V instructions.
2dcee538 2 Copyright 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
252b5132
RH
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
f4321104 16Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132 17
252b5132
RH
18#include <stdio.h>
19
0d8dfecf 20#include "sysdep.h"
2dcee538 21#include "opcode/d10v.h"
252b5132
RH
22#include "dis-asm.h"
23
2dcee538
KH
24/* The PC wraps at 18 bits, except for the segment number,
25 so use this mask to keep the parts we want. */
252b5132
RH
26#define PC_MASK 0x0303FFFF
27
2dcee538 28static void dis_2_short PARAMS ((unsigned long insn, bfd_vma memaddr,
252b5132 29 struct disassemble_info *info, int order));
2dcee538 30static void dis_long PARAMS ((unsigned long insn, bfd_vma memaddr,
252b5132 31 struct disassemble_info *info));
d83c6548
AJ
32static void print_operand
33 PARAMS ((struct d10v_operand *, long unsigned int, struct d10v_opcode *,
34 bfd_vma, struct disassemble_info *));
252b5132 35
2dcee538 36int
252b5132
RH
37print_insn_d10v (memaddr, info)
38 bfd_vma memaddr;
39 struct disassemble_info *info;
40{
41 int status;
42 bfd_byte buffer[4];
43 unsigned long insn;
44
45 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
46 if (status != 0)
47 {
48 (*info->memory_error_func) (status, memaddr, info);
49 return -1;
50 }
51 insn = bfd_getb32 (buffer);
52
53 status = insn & FM11;
2dcee538
KH
54 switch (status)
55 {
56 case 0:
57 dis_2_short (insn, memaddr, info, 2);
58 break;
59 case FM01:
60 dis_2_short (insn, memaddr, info, 0);
61 break;
62 case FM10:
63 dis_2_short (insn, memaddr, info, 1);
64 break;
65 case FM11:
66 dis_long (insn, memaddr, info);
67 break;
68 }
252b5132
RH
69 return 4;
70}
71
72static void
73print_operand (oper, insn, op, memaddr, info)
74 struct d10v_operand *oper;
75 unsigned long insn;
76 struct d10v_opcode *op;
77 bfd_vma memaddr;
78 struct disassemble_info *info;
79{
80 int num, shift;
81
82 if (oper->flags == OPERAND_ATMINUS)
83 {
2dcee538 84 (*info->fprintf_func) (info->stream, "@-");
252b5132
RH
85 return;
86 }
87 if (oper->flags == OPERAND_MINUS)
88 {
2dcee538 89 (*info->fprintf_func) (info->stream, "-");
252b5132
RH
90 return;
91 }
92 if (oper->flags == OPERAND_PLUS)
93 {
2dcee538 94 (*info->fprintf_func) (info->stream, "+");
252b5132
RH
95 return;
96 }
97 if (oper->flags == OPERAND_ATSIGN)
98 {
2dcee538 99 (*info->fprintf_func) (info->stream, "@");
252b5132
RH
100 return;
101 }
102 if (oper->flags == OPERAND_ATPAR)
103 {
2dcee538 104 (*info->fprintf_func) (info->stream, "@(");
252b5132
RH
105 return;
106 }
107
108 shift = oper->shift;
109
2dcee538 110 /* The LONG_L format shifts registers over by 15. */
252b5132
RH
111 if (op->format == LONG_L && (oper->flags & OPERAND_REG))
112 shift += 15;
113
114 num = (insn >> shift) & (0x7FFFFFFF >> (31 - oper->bits));
115
116 if (oper->flags & OPERAND_REG)
117 {
118 int i;
2dcee538 119 int match = 0;
252b5132 120 num += (oper->flags
2dcee538
KH
121 & (OPERAND_GPR | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL));
122 if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
252b5132 123 num += num ? OPERAND_ACC1 : OPERAND_ACC0;
2dcee538 124 for (i = 0; i < d10v_reg_name_cnt (); i++)
252b5132 125 {
d1267250 126 if (num == (d10v_predefined_registers[i].value & ~ OPERAND_SP))
252b5132
RH
127 {
128 if (d10v_predefined_registers[i].pname)
2dcee538
KH
129 (*info->fprintf_func) (info->stream, "%s",
130 d10v_predefined_registers[i].pname);
252b5132 131 else
2dcee538
KH
132 (*info->fprintf_func) (info->stream, "%s",
133 d10v_predefined_registers[i].name);
134 match = 1;
252b5132
RH
135 break;
136 }
137 }
138 if (match == 0)
139 {
2dcee538
KH
140 /* This would only get executed if a register was not in the
141 register table. */
142 if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
252b5132
RH
143 (*info->fprintf_func) (info->stream, "a");
144 else if (oper->flags & OPERAND_CONTROL)
145 (*info->fprintf_func) (info->stream, "cr");
2dcee538 146 else if (oper->flags & OPERAND_REG)
252b5132 147 (*info->fprintf_func) (info->stream, "r");
373efcb3 148 (*info->fprintf_func) (info->stream, "%d", num & REGISTER_MASK);
252b5132
RH
149 }
150 }
151 else
152 {
2dcee538 153 /* Addresses are right-shifted by 2. */
252b5132
RH
154 if (oper->flags & OPERAND_ADDR)
155 {
156 long max;
2dcee538 157 int neg = 0;
252b5132
RH
158 max = (1 << (oper->bits - 1));
159 if (num & max)
160 {
2dcee538 161 num = -num & ((1 << oper->bits) - 1);
252b5132
RH
162 neg = 1;
163 }
2dcee538 164 num = num << 2;
252b5132
RH
165 if (info->flags & INSN_HAS_RELOC)
166 (*info->print_address_func) (num & PC_MASK, info);
167 else
168 {
169 if (neg)
170 (*info->print_address_func) ((memaddr - num) & PC_MASK, info);
171 else
172 (*info->print_address_func) ((memaddr + num) & PC_MASK, info);
173 }
174 }
175 else
176 {
177 if (oper->flags & OPERAND_SIGNED)
178 {
179 int max = (1 << (oper->bits - 1));
180 if (num & max)
181 {
2dcee538 182 num = -num & ((1 << oper->bits) - 1);
252b5132
RH
183 (*info->fprintf_func) (info->stream, "-");
184 }
185 }
2dcee538 186 (*info->fprintf_func) (info->stream, "0x%x", num);
252b5132
RH
187 }
188 }
189}
190
252b5132
RH
191static void
192dis_long (insn, memaddr, info)
193 unsigned long insn;
194 bfd_vma memaddr;
195 struct disassemble_info *info;
196{
197 int i;
2dcee538 198 struct d10v_opcode *op = (struct d10v_opcode *) d10v_opcodes;
252b5132
RH
199 struct d10v_operand *oper;
200 int need_paren = 0;
201 int match = 0;
202
203 while (op->name)
204 {
d5b2f4d6 205 if ((op->format & LONG_OPCODE) && ((op->mask & insn) == (unsigned long) op->opcode))
252b5132
RH
206 {
207 match = 1;
2dcee538
KH
208 (*info->fprintf_func) (info->stream, "%s\t", op->name);
209 for (i = 0; op->operands[i]; i++)
252b5132 210 {
2dcee538 211 oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
252b5132
RH
212 if (oper->flags == OPERAND_ATPAR)
213 need_paren = 1;
214 print_operand (oper, insn, op, memaddr, info);
2dcee538
KH
215 if (op->operands[i + 1] && oper->bits
216 && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
217 && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
218 (*info->fprintf_func) (info->stream, ", ");
252b5132
RH
219 }
220 break;
221 }
222 op++;
223 }
224
225 if (!match)
2dcee538 226 (*info->fprintf_func) (info->stream, ".long\t0x%08x", insn);
252b5132
RH
227
228 if (need_paren)
2dcee538 229 (*info->fprintf_func) (info->stream, ")");
252b5132
RH
230}
231
232static void
233dis_2_short (insn, memaddr, info, order)
234 unsigned long insn;
235 bfd_vma memaddr;
236 struct disassemble_info *info;
237 int order;
238{
2dcee538 239 int i, j;
252b5132
RH
240 unsigned int ins[2];
241 struct d10v_opcode *op;
2dcee538 242 int match, num_match = 0;
252b5132
RH
243 struct d10v_operand *oper;
244 int need_paren = 0;
245
246 ins[0] = (insn & 0x3FFFFFFF) >> 15;
247 ins[1] = insn & 0x00007FFF;
248
2dcee538 249 for (j = 0; j < 2; j++)
252b5132 250 {
2dcee538
KH
251 op = (struct d10v_opcode *) d10v_opcodes;
252 match = 0;
252b5132
RH
253 while (op->name)
254 {
2dcee538 255 if ((op->format & SHORT_OPCODE)
d5b2f4d6 256 && ((op->mask & ins[j]) == (unsigned long) op->opcode))
252b5132 257 {
2dcee538
KH
258 (*info->fprintf_func) (info->stream, "%s\t", op->name);
259 for (i = 0; op->operands[i]; i++)
252b5132 260 {
2dcee538 261 oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
252b5132
RH
262 if (oper->flags == OPERAND_ATPAR)
263 need_paren = 1;
264 print_operand (oper, ins[j], op, memaddr, info);
2dcee538
KH
265 if (op->operands[i + 1] && oper->bits
266 && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
267 && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
268 (*info->fprintf_func) (info->stream, ", ");
252b5132
RH
269 }
270 match = 1;
271 num_match++;
272 break;
273 }
274 op++;
275 }
276 if (!match)
2dcee538 277 (*info->fprintf_func) (info->stream, "unknown");
252b5132
RH
278
279 switch (order)
280 {
281 case 0:
2dcee538 282 (*info->fprintf_func) (info->stream, "\t->\t");
252b5132
RH
283 order = -1;
284 break;
285 case 1:
2dcee538 286 (*info->fprintf_func) (info->stream, "\t<-\t");
252b5132
RH
287 order = -1;
288 break;
289 case 2:
2dcee538 290 (*info->fprintf_func) (info->stream, "\t||\t");
252b5132
RH
291 order = -1;
292 break;
293 default:
294 break;
295 }
296 }
297
298 if (num_match == 0)
2dcee538 299 (*info->fprintf_func) (info->stream, ".long\t0x%08x", insn);
252b5132
RH
300
301 if (need_paren)
2dcee538 302 (*info->fprintf_func) (info->stream, ")");
252b5132 303}