ubsan: m68hc1x: left shift of negative value
[deliverable/binutils-gdb.git] / opcodes / d10v-dis.c
CommitLineData
252b5132 1/* Disassemble D10V instructions.
82704155 2 Copyright (C) 1996-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/d10v.h"
88c1242d 24#include "disassemble.h"
252b5132 25
2dcee538
KH
26/* The PC wraps at 18 bits, except for the segment number,
27 so use this mask to keep the parts we want. */
252b5132
RH
28#define PC_MASK 0x0303FFFF
29
252b5132 30static void
47b0e7ad
NC
31print_operand (struct d10v_operand *oper,
32 unsigned long insn,
33 struct d10v_opcode *op,
34 bfd_vma memaddr,
35 struct disassemble_info *info)
252b5132
RH
36{
37 int num, shift;
38
39 if (oper->flags == OPERAND_ATMINUS)
40 {
2dcee538 41 (*info->fprintf_func) (info->stream, "@-");
252b5132
RH
42 return;
43 }
44 if (oper->flags == OPERAND_MINUS)
45 {
2dcee538 46 (*info->fprintf_func) (info->stream, "-");
252b5132
RH
47 return;
48 }
49 if (oper->flags == OPERAND_PLUS)
50 {
2dcee538 51 (*info->fprintf_func) (info->stream, "+");
252b5132
RH
52 return;
53 }
54 if (oper->flags == OPERAND_ATSIGN)
55 {
2dcee538 56 (*info->fprintf_func) (info->stream, "@");
252b5132
RH
57 return;
58 }
59 if (oper->flags == OPERAND_ATPAR)
60 {
2dcee538 61 (*info->fprintf_func) (info->stream, "@(");
252b5132
RH
62 return;
63 }
64
65 shift = oper->shift;
66
2dcee538 67 /* The LONG_L format shifts registers over by 15. */
252b5132
RH
68 if (op->format == LONG_L && (oper->flags & OPERAND_REG))
69 shift += 15;
70
71 num = (insn >> shift) & (0x7FFFFFFF >> (31 - oper->bits));
72
73 if (oper->flags & OPERAND_REG)
74 {
75 int i;
2dcee538 76 int match = 0;
47b0e7ad 77
252b5132 78 num += (oper->flags
2dcee538
KH
79 & (OPERAND_GPR | OPERAND_FFLAG | OPERAND_CFLAG | OPERAND_CONTROL));
80 if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
252b5132 81 num += num ? OPERAND_ACC1 : OPERAND_ACC0;
2dcee538 82 for (i = 0; i < d10v_reg_name_cnt (); i++)
252b5132 83 {
d1267250 84 if (num == (d10v_predefined_registers[i].value & ~ OPERAND_SP))
252b5132
RH
85 {
86 if (d10v_predefined_registers[i].pname)
2dcee538
KH
87 (*info->fprintf_func) (info->stream, "%s",
88 d10v_predefined_registers[i].pname);
252b5132 89 else
2dcee538
KH
90 (*info->fprintf_func) (info->stream, "%s",
91 d10v_predefined_registers[i].name);
92 match = 1;
252b5132
RH
93 break;
94 }
95 }
96 if (match == 0)
97 {
2dcee538
KH
98 /* This would only get executed if a register was not in the
99 register table. */
100 if (oper->flags & (OPERAND_ACC0 | OPERAND_ACC1))
252b5132
RH
101 (*info->fprintf_func) (info->stream, "a");
102 else if (oper->flags & OPERAND_CONTROL)
103 (*info->fprintf_func) (info->stream, "cr");
2dcee538 104 else if (oper->flags & OPERAND_REG)
252b5132 105 (*info->fprintf_func) (info->stream, "r");
373efcb3 106 (*info->fprintf_func) (info->stream, "%d", num & REGISTER_MASK);
252b5132
RH
107 }
108 }
109 else
110 {
2dcee538 111 /* Addresses are right-shifted by 2. */
252b5132
RH
112 if (oper->flags & OPERAND_ADDR)
113 {
114 long max;
2dcee538 115 int neg = 0;
47b0e7ad 116
252b5132
RH
117 max = (1 << (oper->bits - 1));
118 if (num & max)
119 {
2dcee538 120 num = -num & ((1 << oper->bits) - 1);
252b5132
RH
121 neg = 1;
122 }
2dcee538 123 num = num << 2;
252b5132
RH
124 if (info->flags & INSN_HAS_RELOC)
125 (*info->print_address_func) (num & PC_MASK, info);
126 else
127 {
128 if (neg)
129 (*info->print_address_func) ((memaddr - num) & PC_MASK, info);
130 else
131 (*info->print_address_func) ((memaddr + num) & PC_MASK, info);
132 }
133 }
134 else
135 {
136 if (oper->flags & OPERAND_SIGNED)
137 {
138 int max = (1 << (oper->bits - 1));
139 if (num & max)
140 {
2dcee538 141 num = -num & ((1 << oper->bits) - 1);
252b5132
RH
142 (*info->fprintf_func) (info->stream, "-");
143 }
144 }
2dcee538 145 (*info->fprintf_func) (info->stream, "0x%x", num);
252b5132
RH
146 }
147 }
148}
149
252b5132 150static void
47b0e7ad
NC
151dis_long (unsigned long insn,
152 bfd_vma memaddr,
153 struct disassemble_info *info)
252b5132
RH
154{
155 int i;
2dcee538 156 struct d10v_opcode *op = (struct d10v_opcode *) d10v_opcodes;
252b5132
RH
157 struct d10v_operand *oper;
158 int need_paren = 0;
159 int match = 0;
160
161 while (op->name)
162 {
47b0e7ad
NC
163 if ((op->format & LONG_OPCODE)
164 && ((op->mask & insn) == (unsigned long) op->opcode))
252b5132
RH
165 {
166 match = 1;
2dcee538 167 (*info->fprintf_func) (info->stream, "%s\t", op->name);
47b0e7ad 168
2dcee538 169 for (i = 0; op->operands[i]; i++)
252b5132 170 {
2dcee538 171 oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
252b5132
RH
172 if (oper->flags == OPERAND_ATPAR)
173 need_paren = 1;
174 print_operand (oper, insn, op, memaddr, info);
2dcee538
KH
175 if (op->operands[i + 1] && oper->bits
176 && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
177 && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
178 (*info->fprintf_func) (info->stream, ", ");
252b5132
RH
179 }
180 break;
181 }
182 op++;
183 }
184
185 if (!match)
0fd3a477 186 (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
252b5132
RH
187
188 if (need_paren)
2dcee538 189 (*info->fprintf_func) (info->stream, ")");
252b5132
RH
190}
191
192static void
47b0e7ad
NC
193dis_2_short (unsigned long insn,
194 bfd_vma memaddr,
195 struct disassemble_info *info,
196 int order)
252b5132 197{
2dcee538 198 int i, j;
252b5132
RH
199 unsigned int ins[2];
200 struct d10v_opcode *op;
2dcee538 201 int match, num_match = 0;
252b5132
RH
202 struct d10v_operand *oper;
203 int need_paren = 0;
204
205 ins[0] = (insn & 0x3FFFFFFF) >> 15;
206 ins[1] = insn & 0x00007FFF;
207
2dcee538 208 for (j = 0; j < 2; j++)
252b5132 209 {
2dcee538
KH
210 op = (struct d10v_opcode *) d10v_opcodes;
211 match = 0;
252b5132
RH
212 while (op->name)
213 {
2dcee538 214 if ((op->format & SHORT_OPCODE)
920a34a7
L
215 && ((((unsigned int) op->mask) & ins[j])
216 == (unsigned int) op->opcode))
252b5132 217 {
2dcee538
KH
218 (*info->fprintf_func) (info->stream, "%s\t", op->name);
219 for (i = 0; op->operands[i]; i++)
252b5132 220 {
2dcee538 221 oper = (struct d10v_operand *) &d10v_operands[op->operands[i]];
252b5132
RH
222 if (oper->flags == OPERAND_ATPAR)
223 need_paren = 1;
224 print_operand (oper, ins[j], op, memaddr, info);
2dcee538
KH
225 if (op->operands[i + 1] && oper->bits
226 && d10v_operands[op->operands[i + 1]].flags != OPERAND_PLUS
227 && d10v_operands[op->operands[i + 1]].flags != OPERAND_MINUS)
228 (*info->fprintf_func) (info->stream, ", ");
252b5132
RH
229 }
230 match = 1;
231 num_match++;
232 break;
233 }
234 op++;
235 }
236 if (!match)
2dcee538 237 (*info->fprintf_func) (info->stream, "unknown");
252b5132
RH
238
239 switch (order)
240 {
241 case 0:
2dcee538 242 (*info->fprintf_func) (info->stream, "\t->\t");
252b5132
RH
243 order = -1;
244 break;
245 case 1:
2dcee538 246 (*info->fprintf_func) (info->stream, "\t<-\t");
252b5132
RH
247 order = -1;
248 break;
249 case 2:
2dcee538 250 (*info->fprintf_func) (info->stream, "\t||\t");
252b5132
RH
251 order = -1;
252 break;
253 default:
254 break;
255 }
256 }
257
258 if (num_match == 0)
0fd3a477 259 (*info->fprintf_func) (info->stream, ".long\t0x%08lx", insn);
252b5132
RH
260
261 if (need_paren)
2dcee538 262 (*info->fprintf_func) (info->stream, ")");
252b5132 263}
47b0e7ad
NC
264
265int
266print_insn_d10v (bfd_vma memaddr, struct disassemble_info *info)
267{
268 int status;
269 bfd_byte buffer[4];
270 unsigned long insn;
271
272 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
273 if (status != 0)
274 {
275 (*info->memory_error_func) (status, memaddr, info);
276 return -1;
277 }
278 insn = bfd_getb32 (buffer);
279
280 status = insn & FM11;
281 switch (status)
282 {
283 case 0:
284 dis_2_short (insn, memaddr, info, 2);
285 break;
286 case FM01:
287 dis_2_short (insn, memaddr, info, 0);
288 break;
289 case FM10:
290 dis_2_short (insn, memaddr, info, 1);
291 break;
292 case FM11:
293 dis_long (insn, memaddr, info);
294 break;
295 }
296 return 4;
297}
This page took 0.941051 seconds and 4 git commands to generate.