/src/binutils-gdb/opcodes/xgate-dis.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* xgate-dis.c -- Freescale XGATE disassembly |
2 | | Copyright (C) 2009-2023 Free Software Foundation, Inc. |
3 | | Written by Sean Keys (skeys@ipdatasys.com) |
4 | | |
5 | | This file is part of the GNU opcodes library. |
6 | | |
7 | | This library is free software; you can redistribute it and/or modify |
8 | | it under the terms of the GNU General Public License as published by |
9 | | the Free Software Foundation; either version 3, or (at your option) |
10 | | any later version. |
11 | | |
12 | | It is distributed in the hope that it will be useful, but WITHOUT |
13 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
14 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
15 | | License for more details. |
16 | | |
17 | | You should have received a copy of the GNU General Public License |
18 | | along with this program; if not, write to the Free Software |
19 | | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | | MA 02110-1301, USA. */ |
21 | | |
22 | | #include "sysdep.h" |
23 | | #include <assert.h> |
24 | | #include "disassemble.h" |
25 | | #include "opintl.h" |
26 | | #include "libiberty.h" |
27 | | #include "ansidecl.h" |
28 | | #include "opcode/xgate.h" |
29 | | |
30 | 21.9k | #define XGATE_TWO_BYTES 0x02 |
31 | 434 | #define XGATE_NINE_BITS 0x1FF |
32 | 17 | #define XGATE_TEN_BITS 0x3FF |
33 | 987 | #define XGATE_NINE_SIGNBIT 0x100 |
34 | 60 | #define XGATE_TEN_SIGNBIT 0x200 |
35 | | |
36 | | /* Structures. */ |
37 | | struct decodeInfo |
38 | | { |
39 | | unsigned int operMask; |
40 | | unsigned int operMasksRegisterBits; |
41 | | struct xgate_opcode *opcodePTR; |
42 | | }; |
43 | | |
44 | | /* Prototypes for local functions. */ |
45 | | static int print_insn (bfd_vma, struct disassemble_info *); |
46 | | static int read_memory (bfd_vma, bfd_byte*, int, struct disassemble_info *); |
47 | | static int ripBits (unsigned int *, int, |
48 | | struct xgate_opcode *, unsigned int); |
49 | | static int macro_search (char *, char *); |
50 | | static struct decodeInfo * find_match (unsigned int); |
51 | | |
52 | | /* Statics. */ |
53 | | static struct decodeInfo *decodeTable; |
54 | | static int initialized; |
55 | | static char previousOpName[10]; |
56 | | static unsigned int perviousBin; |
57 | | |
58 | | /* Disassemble one instruction at address 'memaddr'. Returns the number |
59 | | of bytes used by that instruction. */ |
60 | | |
61 | | static int |
62 | | print_insn (bfd_vma memaddr, struct disassemble_info* info) |
63 | 10.9k | { |
64 | 10.9k | int status; |
65 | 10.9k | unsigned int raw_code; |
66 | 10.9k | char *s = 0; |
67 | 10.9k | long bytesRead = 0; |
68 | 10.9k | int i = 0; |
69 | 10.9k | struct xgate_opcode *opcodePTR = (struct xgate_opcode*) xgate_opcodes; |
70 | 10.9k | struct decodeInfo *decodeTablePTR = 0; |
71 | 10.9k | struct decodeInfo *decodePTR = 0; |
72 | 10.9k | unsigned int operandRegisterBits = 0; |
73 | 10.9k | signed int relAddr = 0; |
74 | 10.9k | signed int operandOne = 0; |
75 | 10.9k | signed int operandTwo = 0; |
76 | 10.9k | bfd_byte buffer[4]; |
77 | 10.9k | bfd_vma absAddress; |
78 | | |
79 | 10.9k | unsigned int operMaskReg = 0; |
80 | | /* Initialize our array of opcode masks and check them against our constant |
81 | | table. */ |
82 | 10.9k | if (!initialized) |
83 | 1 | { |
84 | 1 | decodeTable = xmalloc (sizeof (struct decodeInfo) * xgate_num_opcodes); |
85 | 96 | for (i = 0, decodeTablePTR = decodeTable; i < xgate_num_opcodes; |
86 | 95 | i++, decodeTablePTR++, opcodePTR++) |
87 | 95 | { |
88 | 95 | unsigned int bin = 0; |
89 | 95 | unsigned int mask = 0; |
90 | 1.61k | for (s = opcodePTR->format; *s; s++) |
91 | 1.52k | { |
92 | 1.52k | bin <<= 1; |
93 | 1.52k | mask <<= 1; |
94 | 1.52k | operandRegisterBits <<= 1; |
95 | 1.52k | bin |= (*s == '1'); |
96 | 1.52k | mask |= (*s == '0' || *s == '1'); |
97 | 1.52k | operandRegisterBits |= (*s == 'r'); |
98 | 1.52k | } |
99 | | /* Asserting will uncover inconsistencies in our table. */ |
100 | 95 | assert ((s - opcodePTR->format) == 16 || (s - opcodePTR->format) == 32); |
101 | 95 | assert (opcodePTR->bin_opcode == bin); |
102 | | |
103 | 95 | decodeTablePTR->operMask = mask; |
104 | 95 | decodeTablePTR->operMasksRegisterBits = operandRegisterBits; |
105 | 95 | decodeTablePTR->opcodePTR = opcodePTR; |
106 | 95 | } |
107 | 1 | initialized = 1; |
108 | 1 | } |
109 | | |
110 | | /* Read 16 bits. */ |
111 | 10.9k | bytesRead += XGATE_TWO_BYTES; |
112 | 10.9k | status = read_memory (memaddr, buffer, XGATE_TWO_BYTES, info); |
113 | 10.9k | if (status == 0) |
114 | 10.9k | { |
115 | 10.9k | raw_code = buffer[0]; |
116 | 10.9k | raw_code <<= 8; |
117 | 10.9k | raw_code += buffer[1]; |
118 | | |
119 | 10.9k | decodePTR = find_match (raw_code); |
120 | 10.9k | if (decodePTR) |
121 | 8.65k | { |
122 | 8.65k | operMaskReg = decodePTR->operMasksRegisterBits; |
123 | 8.65k | (*info->fprintf_func)(info->stream, "%s", decodePTR->opcodePTR->name); |
124 | | |
125 | | /* First we compare the shorthand format of the constraints. If we |
126 | | still are unable to pinpoint the operands |
127 | | we analyze the opcodes constraint string. */ |
128 | 8.65k | if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON_R_C)) |
129 | 21 | { |
130 | 21 | (*info->fprintf_func)(info->stream, " R%x, CCR", |
131 | 21 | (raw_code >> 8) & 0x7); |
132 | 21 | } |
133 | 8.63k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON_C_R)) |
134 | 1 | { |
135 | 1 | (*info->fprintf_func)(info->stream, " CCR, R%x", |
136 | 1 | (raw_code >> 8) & 0x7); |
137 | 1 | } |
138 | 8.63k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON_R_P)) |
139 | 4 | { |
140 | 4 | (*info->fprintf_func)(info->stream, " R%x, PC", |
141 | 4 | (raw_code >> 8) & 0x7); |
142 | 4 | } |
143 | 8.62k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_TRI)) |
144 | 650 | { |
145 | 650 | (*info->fprintf_func)(info->stream, " R%x, R%x, R%x", |
146 | 650 | (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7, |
147 | 650 | (raw_code >> 2) & 0x7); |
148 | 650 | } |
149 | 7.97k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IDR)) |
150 | 963 | { |
151 | 963 | if (raw_code & 0x01) |
152 | 233 | { |
153 | 233 | (*info->fprintf_func)(info->stream, " R%x, (R%x, R%x+)", |
154 | 233 | (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7, |
155 | 233 | (raw_code >> 2) & 0x7); |
156 | 233 | } |
157 | 730 | else if (raw_code & 0x02) |
158 | 302 | { |
159 | 302 | (*info->fprintf_func)(info->stream, " R%x, (R%x, -R%x)", |
160 | 302 | (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7, |
161 | 302 | (raw_code >> 2) & 0x7); |
162 | 302 | } |
163 | 428 | else |
164 | 428 | { |
165 | 428 | (*info->fprintf_func)(info->stream, " R%x, (R%x, R%x)", |
166 | 428 | (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7, |
167 | 428 | (raw_code >> 2) & 0x7); |
168 | 428 | } |
169 | 963 | } |
170 | 7.01k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_DYA)) |
171 | 69 | { |
172 | 69 | operandOne = ripBits (&operMaskReg, 3, decodePTR->opcodePTR, raw_code); |
173 | 69 | operandTwo = ripBits (&operMaskReg, 3, decodePTR->opcodePTR, raw_code); |
174 | 69 | ( *info->fprintf_func)(info->stream, " R%x, R%x", operandOne, |
175 | 69 | operandTwo); |
176 | 69 | } |
177 | 6.94k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IDO5)) |
178 | 826 | { |
179 | 826 | (*info->fprintf_func)(info->stream, " R%x, (R%x, #0x%x)", |
180 | 826 | (raw_code >> 8) & 0x7, (raw_code >> 5) & 0x7, raw_code & 0x1f); |
181 | 826 | } |
182 | 6.11k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_MON)) |
183 | 12 | { |
184 | 12 | operandOne = ripBits (&operMaskReg, 3, decodePTR->opcodePTR, |
185 | 12 | raw_code); |
186 | 12 | (*info->fprintf_func)(info->stream, " R%x", operandOne); |
187 | 12 | } |
188 | 6.10k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_REL9)) |
189 | 987 | { |
190 | | /* If address is negative handle it accordingly. */ |
191 | 987 | if (raw_code & XGATE_NINE_SIGNBIT) |
192 | 434 | { |
193 | 434 | relAddr = XGATE_NINE_BITS >> 1; /* Clip sign bit. */ |
194 | 434 | relAddr = ~relAddr; /* Make signed. */ |
195 | 434 | relAddr |= (raw_code & 0xFF) + 1; /* Apply our value. */ |
196 | 434 | relAddr *= 2; /* Multiply by two as per processor docs. */ |
197 | 434 | } |
198 | 553 | else |
199 | 553 | { |
200 | 553 | relAddr = raw_code & 0xff; |
201 | 553 | relAddr = relAddr * 2 + 2; |
202 | 553 | } |
203 | 987 | (*info->fprintf_func)(info->stream, " *%d", relAddr); |
204 | 987 | (*info->fprintf_func)(info->stream, " Abs* 0x"); |
205 | 987 | (*info->print_address_func)(memaddr + relAddr, info); |
206 | 987 | } |
207 | 5.11k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_REL10)) |
208 | 60 | { |
209 | | /* If address is negative handle it accordingly. */ |
210 | 60 | if (raw_code & XGATE_TEN_SIGNBIT) |
211 | 17 | { |
212 | 17 | relAddr = XGATE_TEN_BITS >> 1; /* Clip sign bit. */ |
213 | 17 | relAddr = ~relAddr; /* Make signed. */ |
214 | 17 | relAddr |= (raw_code & 0x1FF) + 1; /* Apply our value. */ |
215 | 17 | relAddr *= 2; /* Multiply by two as per processor docs. */ |
216 | 17 | } |
217 | 43 | else |
218 | 43 | { |
219 | 43 | relAddr = raw_code & 0x1FF; |
220 | 43 | relAddr = relAddr * 2 + 2; |
221 | 43 | } |
222 | 60 | (*info->fprintf_func)(info->stream, " *%d", relAddr); |
223 | 60 | (*info->fprintf_func)(info->stream, " Abs* 0x"); |
224 | 60 | (*info->print_address_func)(memaddr + relAddr, info); |
225 | 60 | } |
226 | 5.05k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IMM4)) |
227 | 110 | { |
228 | 110 | (*info->fprintf_func)(info->stream, " R%x, #0x%02x", |
229 | 110 | (raw_code >> 8) & 0x7, (raw_code >> 4) & 0xF); |
230 | 110 | } |
231 | 4.94k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IMM8)) |
232 | 2.53k | { |
233 | 2.53k | if (macro_search (decodePTR->opcodePTR->name, previousOpName) && |
234 | 2.53k | previousOpName[0]) |
235 | 757 | { |
236 | 757 | absAddress = (0xFF & raw_code) << 8; |
237 | 757 | absAddress |= perviousBin & 0xFF; |
238 | 757 | (*info->fprintf_func)(info->stream, " R%x, #0x%02x Abs* 0x", |
239 | 757 | (raw_code >> 8) & 0x7, raw_code & 0xff); |
240 | 757 | (*info->print_address_func)(absAddress, info); |
241 | 757 | previousOpName[0] = 0; |
242 | 757 | } |
243 | 1.78k | else |
244 | 1.78k | { |
245 | 1.78k | strcpy (previousOpName, decodePTR->opcodePTR->name); |
246 | 1.78k | (*info->fprintf_func)(info->stream, " R%x, #0x%02x", |
247 | 1.78k | (raw_code >> 8) & 0x7, raw_code & 0xff); |
248 | 1.78k | } |
249 | 2.53k | } |
250 | 2.41k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_IMM3)) |
251 | 3 | { |
252 | 3 | (*info->fprintf_func)(info->stream, " #0x%x", |
253 | 3 | (raw_code >> 8) & 0x7); |
254 | 3 | } |
255 | 2.40k | else if (!strcmp (decodePTR->opcodePTR->constraints, XGATE_OP_INH)) |
256 | 2.40k | { |
257 | 2.40k | } |
258 | 0 | else |
259 | 0 | { |
260 | 0 | (*info->fprintf_func)(info->stream, " unhandled mode %s", |
261 | 0 | decodePTR->opcodePTR->constraints); |
262 | 0 | } |
263 | 8.65k | perviousBin = raw_code; |
264 | 8.65k | } |
265 | 2.31k | else |
266 | 2.31k | { |
267 | 2.31k | (*info->fprintf_func)(info->stream, |
268 | 2.31k | " unable to find opcode match #0%x", raw_code); |
269 | 2.31k | } |
270 | 10.9k | } |
271 | 10.9k | return bytesRead; |
272 | 10.9k | } |
273 | | |
274 | | int |
275 | | print_insn_xgate (bfd_vma memaddr, struct disassemble_info* info) |
276 | 10.9k | { |
277 | 10.9k | return print_insn (memaddr, info); |
278 | 10.9k | } |
279 | | |
280 | | static int |
281 | | read_memory (bfd_vma memaddr, bfd_byte* buffer, int size, |
282 | | struct disassemble_info* info) |
283 | 10.9k | { |
284 | 10.9k | int status; |
285 | 10.9k | status = (*info->read_memory_func) (memaddr, buffer, size, info); |
286 | 10.9k | if (status != 0) |
287 | 9 | { |
288 | 9 | (*info->memory_error_func) (status, memaddr, info); |
289 | 9 | return -1; |
290 | 9 | } |
291 | 10.9k | return 0; |
292 | 10.9k | } |
293 | | |
294 | | static int |
295 | | ripBits (unsigned int *operandBitsRemaining, |
296 | | int numBitsRequested, |
297 | | struct xgate_opcode *opcodePTR, |
298 | | unsigned int memory) |
299 | 150 | { |
300 | 150 | unsigned int currentBit; |
301 | 150 | unsigned int operand = 0; |
302 | 150 | int numBitsFound; |
303 | | |
304 | 150 | for (numBitsFound = 0, currentBit = 1u << ((opcodePTR->size * 8) - 1); |
305 | 1.55k | numBitsFound < numBitsRequested && currentBit != 0; |
306 | 1.40k | currentBit >>= 1) |
307 | 1.40k | { |
308 | 1.40k | if (currentBit & *operandBitsRemaining) |
309 | 450 | { |
310 | 450 | *operandBitsRemaining &= ~(currentBit); /* Consume the current bit. */ |
311 | 450 | operand <<= 1; /* Make room for our next bit. */ |
312 | 450 | numBitsFound++; |
313 | 450 | operand |= (currentBit & memory) > 0; |
314 | 450 | } |
315 | 1.40k | } |
316 | 150 | return operand; |
317 | 150 | } |
318 | | |
319 | | static int |
320 | | macro_search (char *currentName, char *lastName) |
321 | 2.53k | { |
322 | 2.53k | int i; |
323 | 2.53k | int length = 0; |
324 | 2.53k | char *where; |
325 | | |
326 | 233k | for (i = 0; i < xgate_num_opcodes; i++) |
327 | 232k | { |
328 | 232k | where = strstr (xgate_opcodes[i].constraints, lastName); |
329 | | |
330 | 232k | if (where) |
331 | 69.0k | { |
332 | 69.0k | length = strlen (where); |
333 | 69.0k | } |
334 | 232k | if (length) |
335 | 70.2k | { |
336 | 70.2k | where = strstr (xgate_opcodes[i].constraints, currentName); |
337 | 70.2k | if (where) |
338 | 1.30k | { |
339 | 1.30k | length = strlen (where); |
340 | 1.30k | return 1; |
341 | 1.30k | } |
342 | 70.2k | } |
343 | 232k | } |
344 | 1.23k | return 0; |
345 | 2.53k | } |
346 | | |
347 | | static struct decodeInfo * |
348 | | find_match (unsigned int raw_code) |
349 | 10.9k | { |
350 | 10.9k | struct decodeInfo *decodeTablePTR = 0; |
351 | 10.9k | int i; |
352 | | |
353 | 547k | for (i = 0, decodeTablePTR = decodeTable; i < xgate_num_opcodes; |
354 | 536k | i++, decodeTablePTR++) |
355 | 545k | { |
356 | 545k | if ((raw_code & decodeTablePTR->operMask) |
357 | 545k | == decodeTablePTR->opcodePTR->bin_opcode) |
358 | 22.5k | { |
359 | | /* Make sure we didn't run into a macro or alias. */ |
360 | 22.5k | if (decodeTablePTR->opcodePTR->cycles_min != 0) |
361 | 8.65k | { |
362 | 8.65k | return decodeTablePTR; |
363 | 0 | break; |
364 | 8.65k | } |
365 | 13.9k | else |
366 | 13.9k | continue; |
367 | 22.5k | } |
368 | 545k | } |
369 | 2.31k | return 0; |
370 | 10.9k | } |