s12z-opc.c formatting fixes
[deliverable/binutils-gdb.git] / opcodes / d30v-dis.c
CommitLineData
252b5132 1/* Disassemble D30V instructions.
82704155 2 Copyright (C) 1997-2019 Free Software Foundation, Inc.
252b5132 3
9b201bb5
NC
4 This file is part of the GNU opcodes library.
5
6 This library is free software; you can redistribute it and/or modify
47b0e7ad 7 it under the terms of the GNU General Public License as published by
9b201bb5
NC
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
252b5132 10
9b201bb5
NC
11 It is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
252b5132 15
47b0e7ad
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
252b5132 20
0d8dfecf 21#include "sysdep.h"
df7b86aa 22#include <stdio.h>
2dcee538 23#include "opcode/d30v.h"
88c1242d 24#include "disassemble.h"
252b5132 25#include "opintl.h"
9adb2591 26#include "libiberty.h"
252b5132
RH
27
28#define PC_MASK 0xFFFFFFFF
29
2dcee538
KH
30/* Return 0 if lookup fails,
31 1 if found and only one form,
32 2 if found and there are short and long forms. */
252b5132 33
252b5132 34static int
47b0e7ad 35lookup_opcode (struct d30v_insn *insn, long num, int is_long)
252b5132 36{
91d6fa6a 37 int i = 0, op_index;
252b5132 38 struct d30v_format *f;
2dcee538 39 struct d30v_opcode *op = (struct d30v_opcode *) d30v_opcode_table;
252b5132
RH
40 int op1 = (num >> 25) & 0x7;
41 int op2 = (num >> 20) & 0x1f;
42 int mod = (num >> 18) & 0x3;
43
2dcee538
KH
44 /* Find the opcode. */
45 do
46 {
47 if ((op->op1 == op1) && (op->op2 == op2))
48 break;
49 op++;
50 }
51 while (op->name);
252b5132
RH
52
53 if (!op || !op->name)
54 return 0;
55
56 while (op->op1 == op1 && op->op2 == op2)
57 {
2dcee538 58 /* Scan through all the formats for the opcode. */
91d6fa6a 59 op_index = op->format[i++];
2dcee538 60 do
252b5132 61 {
91d6fa6a
NC
62 f = (struct d30v_format *) &d30v_format_table[op_index];
63 while (f->form == op_index)
252b5132
RH
64 {
65 if ((!is_long || f->form >= LONG) && (f->modifier == mod))
66 {
67 insn->form = f;
68 break;
69 }
70 f++;
71 }
72 if (insn->form)
73 break;
2dcee538 74 }
91d6fa6a 75 while ((op_index = op->format[i++]) != 0);
252b5132
RH
76 if (insn->form)
77 break;
78 op++;
2dcee538 79 i = 0;
252b5132
RH
80 }
81 if (insn->form == NULL)
82 return 0;
83
84 insn->op = op;
85 insn->ecc = (num >> 28) & 0x7;
86 if (op->format[1])
87 return 2;
88 else
89 return 1;
90}
91
47b0e7ad
NC
92static int
93extract_value (long long num, struct d30v_operand *oper, int is_long)
94{
95 int val;
96 int shift = 12 - oper->position;
97 int mask = (0xFFFFFFFF >> (32 - oper->bits));
98
99 if (is_long)
100 {
101 if (oper->bits == 32)
102 /* Piece together 32-bit constant. */
103 val = ((num & 0x3FFFF)
104 | ((num & 0xFF00000) >> 2)
105 | ((num & 0x3F00000000LL) >> 6));
106 else
107 val = (num >> (32 + shift)) & mask;
108 }
109 else
110 val = (num >> shift) & mask;
111
112 if (oper->flags & OPERAND_SHIFT)
113 val <<= 3;
114
115 return val;
116}
117
2dcee538 118static void
47b0e7ad
NC
119print_insn (struct disassemble_info *info,
120 bfd_vma memaddr,
121 long long num,
122 struct d30v_insn *insn,
123 int is_long,
124 int show_ext)
252b5132 125{
2dcee538 126 int val, opnum, need_comma = 0;
252b5132 127 struct d30v_operand *oper;
2dcee538 128 int i, match, opind = 0, need_paren = 0, found_control = 0;
252b5132 129
2dcee538 130 (*info->fprintf_func) (info->stream, "%s", insn->op->name);
252b5132 131
2dcee538 132 /* Check for CMP or CMPU. */
252b5132
RH
133 if (d30v_operand_table[insn->form->operands[0]].flags & OPERAND_NAME)
134 {
135 opind++;
2dcee538
KH
136 val =
137 extract_value (num,
138 (struct d30v_operand *) &d30v_operand_table[insn->form->operands[0]],
139 is_long);
140 (*info->fprintf_func) (info->stream, "%s", d30v_cc_names[val]);
252b5132
RH
141 }
142
2dcee538 143 /* Add in ".s" or ".l". */
252b5132
RH
144 if (show_ext == 2)
145 {
146 if (is_long)
147 (*info->fprintf_func) (info->stream, ".l");
148 else
149 (*info->fprintf_func) (info->stream, ".s");
150 }
151
152 if (insn->ecc)
2dcee538 153 (*info->fprintf_func) (info->stream, "/%s", d30v_ecc_names[insn->ecc]);
252b5132
RH
154
155 (*info->fprintf_func) (info->stream, "\t");
156
157 while ((opnum = insn->form->operands[opind++]) != 0)
158 {
159 int bits;
47b0e7ad 160
2dcee538 161 oper = (struct d30v_operand *) &d30v_operand_table[opnum];
252b5132
RH
162 bits = oper->bits;
163 if (oper->flags & OPERAND_SHIFT)
164 bits += 3;
165
2dcee538
KH
166 if (need_comma
167 && oper->flags != OPERAND_PLUS
168 && oper->flags != OPERAND_MINUS)
252b5132 169 {
2dcee538 170 need_comma = 0;
252b5132
RH
171 (*info->fprintf_func) (info->stream, ", ");
172 }
173
174 if (oper->flags == OPERAND_ATMINUS)
175 {
176 (*info->fprintf_func) (info->stream, "@-");
177 continue;
178 }
179 if (oper->flags == OPERAND_MINUS)
180 {
2dcee538 181 (*info->fprintf_func) (info->stream, "-");
252b5132
RH
182 continue;
183 }
184 if (oper->flags == OPERAND_PLUS)
185 {
2dcee538 186 (*info->fprintf_func) (info->stream, "+");
252b5132
RH
187 continue;
188 }
189 if (oper->flags == OPERAND_ATSIGN)
190 {
2dcee538 191 (*info->fprintf_func) (info->stream, "@");
252b5132
RH
192 continue;
193 }
194 if (oper->flags == OPERAND_ATPAR)
195 {
2dcee538 196 (*info->fprintf_func) (info->stream, "@(");
252b5132
RH
197 need_paren = 1;
198 continue;
199 }
200
201 if (oper->flags == OPERAND_SPECIAL)
202 continue;
203
2dcee538
KH
204 val = extract_value (num, oper, is_long);
205
252b5132
RH
206 if (oper->flags & OPERAND_REG)
207 {
208 match = 0;
209 if (oper->flags & OPERAND_CONTROL)
210 {
2dcee538
KH
211 struct d30v_operand *oper3 =
212 (struct d30v_operand *) &d30v_operand_table[insn->form->operands[2]];
213 int id = extract_value (num, oper3, is_long);
47b0e7ad 214
252b5132 215 found_control = 1;
2dcee538 216 switch (id)
252b5132
RH
217 {
218 case 0:
219 val |= OPERAND_CONTROL;
220 break;
221 case 1:
222 case 2:
223 val = OPERAND_CONTROL + MAX_CONTROL_REG + id;
224 break;
225 case 3:
226 val |= OPERAND_FLAG;
227 break;
228 default:
a6743a54
AM
229 /* xgettext: c-format */
230 opcodes_error_handler (_("illegal id (%d)"), id);
231 abort ();
252b5132
RH
232 }
233 }
234 else if (oper->flags & OPERAND_ACC)
235 val |= OPERAND_ACC;
236 else if (oper->flags & OPERAND_FLAG)
237 val |= OPERAND_FLAG;
2dcee538 238 for (i = 0; i < reg_name_cnt (); i++)
252b5132
RH
239 {
240 if (val == pre_defined_registers[i].value)
241 {
242 if (pre_defined_registers[i].pname)
243 (*info->fprintf_func)
2dcee538 244 (info->stream, "%s", pre_defined_registers[i].pname);
252b5132
RH
245 else
246 (*info->fprintf_func)
2dcee538
KH
247 (info->stream, "%s", pre_defined_registers[i].name);
248 match = 1;
252b5132
RH
249 break;
250 }
251 }
2dcee538 252 if (match == 0)
252b5132 253 {
2dcee538
KH
254 /* This would only get executed if a register was not in
255 the register table. */
252b5132 256 (*info->fprintf_func)
2dcee538 257 (info->stream, _("<unknown register %d>"), val & 0x3F);
252b5132
RH
258 }
259 }
866afedc
NC
260 /* repeati has a relocation, but its first argument is a plain
261 immediate. OTOH instructions like djsri have a pc-relative
d9a35582 262 delay target, but an absolute jump target. Therefore, a test
866afedc
NC
263 of insn->op->reloc_flag is not specific enough; we must test
264 if the actual operand we are handling now is pc-relative. */
265 else if (oper->flags & OPERAND_PCREL)
252b5132 266 {
866afedc 267 int neg = 0;
2dcee538 268
866afedc
NC
269 /* IMM6S3 is unsigned. */
270 if (oper->flags & OPERAND_SIGNED || bits == 32)
252b5132 271 {
866afedc
NC
272 long max;
273 max = (1 << (bits - 1));
274 if (val & max)
275 {
276 if (bits == 32)
277 val = -val;
278 else
2dcee538 279 val = -val & ((1 << bits) - 1);
866afedc
NC
280 neg = 1;
281 }
282 }
283 if (neg)
284 {
2dcee538 285 (*info->fprintf_func) (info->stream, "-%x\t(", val);
866afedc
NC
286 (*info->print_address_func) ((memaddr - val) & PC_MASK, info);
287 (*info->fprintf_func) (info->stream, ")");
252b5132 288 }
866afedc 289 else
252b5132 290 {
2dcee538 291 (*info->fprintf_func) (info->stream, "%x\t(", val);
866afedc
NC
292 (*info->print_address_func) ((memaddr + val) & PC_MASK, info);
293 (*info->fprintf_func) (info->stream, ")");
252b5132 294 }
252b5132
RH
295 }
296 else if (insn->op->reloc_flag == RELOC_ABS)
297 {
298 (*info->print_address_func) (val, info);
299 }
300 else
301 {
302 if (oper->flags & OPERAND_SIGNED)
303 {
304 int max = (1 << (bits - 1));
47b0e7ad 305
252b5132
RH
306 if (val & max)
307 {
308 val = -val;
309 if (bits < 32)
310 val &= ((1 << bits) - 1);
311 (*info->fprintf_func) (info->stream, "-");
312 }
313 }
2dcee538 314 (*info->fprintf_func) (info->stream, "0x%x", val);
252b5132 315 }
2dcee538 316 /* If there is another operand, then write a comma and space. */
9adb2591
NC
317 if (opind < (int) ARRAY_SIZE (insn->form->operands)
318 && insn->form->operands[opind]
319 && !(found_control && opind == 2))
252b5132
RH
320 need_comma = 1;
321 }
322 if (need_paren)
323 (*info->fprintf_func) (info->stream, ")");
324}
325
47b0e7ad
NC
326int
327print_insn_d30v (bfd_vma memaddr, struct disassemble_info *info)
252b5132 328{
47b0e7ad
NC
329 int status, result;
330 bfd_byte buffer[12];
331 unsigned long in1, in2;
332 struct d30v_insn insn;
333 long long num;
252b5132 334
47b0e7ad
NC
335 insn.form = NULL;
336
337 info->bytes_per_line = 8;
338 info->bytes_per_chunk = 4;
339 info->display_endian = BFD_ENDIAN_BIG;
340
341 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
342 if (status != 0)
252b5132 343 {
47b0e7ad
NC
344 (*info->memory_error_func) (status, memaddr, info);
345 return -1;
346 }
347 in1 = bfd_getb32 (buffer);
348
349 status = (*info->read_memory_func) (memaddr + 4, buffer, 4, info);
350 if (status != 0)
351 {
352 info->bytes_per_line = 8;
353 if (!(result = lookup_opcode (&insn, in1, 0)))
0fd3a477 354 (*info->fprintf_func) (info->stream, ".long\t0x%lx", in1);
47b0e7ad
NC
355 else
356 print_insn (info, memaddr, (long long) in1, &insn, 0, result);
357 return 4;
358 }
359 in2 = bfd_getb32 (buffer);
360
361 if (in1 & in2 & FM01)
362 {
363 /* LONG instruction. */
364 if (!(result = lookup_opcode (&insn, in1, 1)))
252b5132 365 {
0fd3a477 366 (*info->fprintf_func) (info->stream, ".long\t0x%lx,0x%lx", in1, in2);
47b0e7ad 367 return 8;
252b5132 368 }
47b0e7ad
NC
369 num = (long long) in1 << 32 | in2;
370 print_insn (info, memaddr, num, &insn, 1, result);
252b5132
RH
371 }
372 else
47b0e7ad
NC
373 {
374 num = in1;
375 if (!(result = lookup_opcode (&insn, in1, 0)))
0fd3a477 376 (*info->fprintf_func) (info->stream, ".long\t0x%lx", in1);
47b0e7ad
NC
377 else
378 print_insn (info, memaddr, num, &insn, 0, result);
252b5132 379
47b0e7ad
NC
380 switch (((in1 >> 31) << 1) | (in2 >> 31))
381 {
382 case 0:
383 (*info->fprintf_func) (info->stream, "\t||\t");
384 break;
385 case 1:
386 (*info->fprintf_func) (info->stream, "\t->\t");
387 break;
388 case 2:
389 (*info->fprintf_func) (info->stream, "\t<-\t");
390 default:
391 break;
392 }
252b5132 393
47b0e7ad
NC
394 insn.form = NULL;
395 num = in2;
396 if (!(result = lookup_opcode (&insn, in2, 0)))
0fd3a477 397 (*info->fprintf_func) (info->stream, ".long\t0x%lx", in2);
47b0e7ad
NC
398 else
399 print_insn (info, memaddr, num, &insn, 0, result);
400 }
401 return 8;
252b5132 402}
This page took 0.909436 seconds and 4 git commands to generate.