* sun build fix for thinko (?)
[binutils-gdb.git] / opcodes / d10v-dis.c
blob04184f8da76481315aab3ae520e99beb191a47f2
1 /* Disassemble D10V instructions.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
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.
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.
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. */
19 #include <stdio.h>
21 #include "opcode/d10v.h"
22 #include "dis-asm.h"
24 /* the PC wraps at 18 bits, except for the segment number */
25 /* so use this mask to keep the parts we want */
26 #define PC_MASK 0x03003FFF
28 static void dis_2_short PARAMS ((unsigned long insn, bfd_vma memaddr,
29 struct disassemble_info *info, int order));
30 static void dis_long PARAMS ((unsigned long insn, bfd_vma memaddr,
31 struct disassemble_info *info));
33 int
34 print_insn_d10v (memaddr, info)
35 bfd_vma memaddr;
36 struct disassemble_info *info;
38 int status;
39 bfd_byte buffer[4];
40 unsigned long insn;
42 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
43 if (status != 0)
45 (*info->memory_error_func) (status, memaddr, info);
46 return -1;
48 insn = bfd_getb32 (buffer);
50 status = insn & FM11;
51 switch (status) {
52 case 0:
53 dis_2_short (insn, memaddr, info, 2);
54 break;
55 case FM01:
56 dis_2_short (insn, memaddr, info, 0);
57 break;
58 case FM10:
59 dis_2_short (insn, memaddr, info, 1);
60 break;
61 case FM11:
62 dis_long (insn, memaddr, info);
63 break;
65 return 4;
68 static void
69 print_operand (oper, insn, op, memaddr, info)
70 struct d10v_operand *oper;
71 unsigned long insn;
72 struct d10v_opcode *op;
73 bfd_vma memaddr;
74 struct disassemble_info *info;
76 int num, shift;
78 if (oper->flags == OPERAND_ATMINUS)
80 (*info->fprintf_func) (info->stream, "@-");
81 return;
83 if (oper->flags == OPERAND_MINUS)
85 (*info->fprintf_func) (info->stream, "-");
86 return;
88 if (oper->flags == OPERAND_PLUS)
90 (*info->fprintf_func) (info->stream, "+");
91 return;
93 if (oper->flags == OPERAND_ATSIGN)
95 (*info->fprintf_func) (info->stream, "@");
96 return;
98 if (oper->flags == OPERAND_ATPAR)
100 (*info->fprintf_func) (info->stream, "@(");
101 return;
104 shift = oper->shift;
106 /* the LONG_L format shifts registers over by 15 */
107 if (op->format == LONG_L && (oper->flags & OPERAND_REG))
108 shift += 15;
110 num = (insn >> shift) & (0x7FFFFFFF >> (31 - oper->bits));
112 if (oper->flags & OPERAND_REG)
114 int i;
115 int match=0;
116 num += oper->flags & (OPERAND_ACC|OPERAND_FLAG|OPERAND_CONTROL);
117 for (i=0;i<reg_name_cnt();i++)
119 if (num == pre_defined_registers[i].value)
121 if (pre_defined_registers[i].pname)
122 (*info->fprintf_func) (info->stream, "%s",pre_defined_registers[i].pname);
123 else
124 (*info->fprintf_func) (info->stream, "%s",pre_defined_registers[i].name);
125 match=1;
126 break;
129 if (match==0)
131 /* this would only get executed if a register was not in the
132 register table */
133 if (oper->flags & OPERAND_ACC)
134 (*info->fprintf_func) (info->stream, "a");
135 else if (oper->flags & OPERAND_CONTROL)
136 (*info->fprintf_func) (info->stream, "cr");
137 else if(oper->flags & OPERAND_REG)
138 (*info->fprintf_func) (info->stream, "r");
139 (*info->fprintf_func) (info->stream, "%d",num);
142 else
144 /* addresses are right-shifted by 2 */
145 if (oper->flags & OPERAND_ADDR)
147 long max;
148 int neg=0;
149 max = (1 << (oper->bits - 1));
150 if (num & max)
152 num = -num & ((1 << oper->bits)-1);
153 neg = 1;
155 num = num<<2;
156 if (neg)
157 (*info->print_address_func) ((memaddr - num) & PC_MASK, info);
158 else
159 (*info->print_address_func) ((memaddr + num) & PC_MASK, info);
161 else
163 if (oper->flags & OPERAND_SIGNED)
165 int max = (1 << (oper->bits - 1));
166 if (num & max)
168 num = -num & ((1 << oper->bits)-1);
169 (*info->fprintf_func) (info->stream, "-");
172 (*info->fprintf_func) (info->stream, "0x%x",num);
178 static void
179 dis_long (insn, memaddr, info)
180 unsigned long insn;
181 bfd_vma memaddr;
182 struct disassemble_info *info;
184 int i;
185 char buf[32];
186 struct d10v_opcode *op = (struct d10v_opcode *)d10v_opcodes;
187 struct d10v_operand *oper;
188 int need_paren = 0;
189 int match = 0;
191 while (op->name)
193 if ((op->format & LONG_OPCODE) && ((op->mask & insn) == op->opcode))
195 match = 1;
196 (*info->fprintf_func) (info->stream, "%s\t", op->name);
197 for ( i=0; op->operands[i]; i++)
199 oper = (struct d10v_operand *)&d10v_operands[op->operands[i]];
200 if (oper->flags == OPERAND_ATPAR)
201 need_paren = 1;
202 print_operand (oper, insn, op, memaddr, info);
203 if (op->operands[i+1] && oper->bits &&
204 d10v_operands[op->operands[i+1]].flags != OPERAND_PLUS &&
205 d10v_operands[op->operands[i+1]].flags != OPERAND_MINUS)
206 (*info->fprintf_func) (info->stream, ", ");
208 break;
210 op++;
213 if (!match)
214 (*info->fprintf_func) (info->stream, ".long\t0x%08x",insn);
216 if (need_paren)
217 (*info->fprintf_func) (info->stream, ")");
220 static void
221 dis_2_short (insn, memaddr, info, order)
222 unsigned long insn;
223 bfd_vma memaddr;
224 struct disassemble_info *info;
225 int order;
227 int i,j;
228 char astr[2][32];
229 unsigned int ins[2];
230 struct d10v_opcode *op;
231 char buf[32];
232 int match, num_match=0;
233 struct d10v_operand *oper;
234 int need_paren = 0;
236 ins[0] = (insn & 0x3FFFFFFF) >> 15;
237 ins[1] = insn & 0x00007FFF;
239 for(j=0;j<2;j++)
241 op = (struct d10v_opcode *)d10v_opcodes;
242 match=0;
243 while (op->name)
245 if ((op->format & SHORT_OPCODE) && ((op->mask & ins[j]) == op->opcode))
247 (*info->fprintf_func) (info->stream, "%s\t",op->name);
248 for (i=0; op->operands[i]; i++)
250 oper = (struct d10v_operand *)&d10v_operands[op->operands[i]];
251 if (oper->flags == OPERAND_ATPAR)
252 need_paren = 1;
253 print_operand (oper, ins[j], op, memaddr, info);
254 if (op->operands[i+1] && oper->bits &&
255 d10v_operands[op->operands[i+1]].flags != OPERAND_PLUS &&
256 d10v_operands[op->operands[i+1]].flags != OPERAND_MINUS)
257 (*info->fprintf_func) (info->stream, ", ");
259 match = 1;
260 num_match++;
261 break;
263 op++;
265 if (!match)
266 (*info->fprintf_func) (info->stream, "unknown");
268 switch (order)
270 case 0:
271 (*info->fprintf_func) (info->stream, "\t->\t");
272 order = -1;
273 break;
274 case 1:
275 (*info->fprintf_func) (info->stream, "\t<-\t");
276 order = -1;
277 break;
278 case 2:
279 (*info->fprintf_func) (info->stream, "\t||\t");
280 order = -1;
281 break;
282 default:
283 break;
287 if (num_match == 0)
288 (*info->fprintf_func) (info->stream, ".long\t0x%08x",insn);
290 if (need_paren)
291 (*info->fprintf_func) (info->stream, ")");