/src/binutils-gdb/opcodes/cr16-dis.c
Line | Count | Source |
1 | | /* Disassembler code for CR16. |
2 | | Copyright (C) 2007-2026 Free Software Foundation, Inc. |
3 | | Contributed by M R Swami Reddy (MR.Swami.Reddy@nsc.com). |
4 | | |
5 | | This file is part of GAS, GDB and the GNU binutils. |
6 | | |
7 | | This program is free software; you can redistribute it and/or modify it |
8 | | under the terms of the GNU General Public License as published by the |
9 | | Free Software Foundation; either version 3, or (at your option) |
10 | | any later version. |
11 | | |
12 | | This program is distributed in the hope that it will be useful, but WITHOUT |
13 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
14 | | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
15 | | 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 Foundation, |
19 | | Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
20 | | |
21 | | #include "sysdep.h" |
22 | | #include "disassemble.h" |
23 | | #include "opcode/cr16.h" |
24 | | #include "libiberty.h" |
25 | | |
26 | | /* String to print when opcode was not matched. */ |
27 | 39.6k | #define ILLEGAL "illegal" |
28 | | /* Escape to 16-bit immediate. */ |
29 | | #define ESCAPE_16_BIT 0xB |
30 | | |
31 | | /* Extract 'n_bits' from 'a' starting from offset 'offs'. */ |
32 | | #define EXTRACT(a, offs, n_bits) \ |
33 | 371k | (((a) >> (offs)) & ((1ul << ((n_bits) - 1) << 1) - 1)) |
34 | | |
35 | | /* Set Bit Mask - a mask to set all bits in a 32-bit word starting |
36 | | from offset 'offs'. */ |
37 | 38.4M | #define SBM(offs) ((1ul << 31 << 1) - (1ul << (offs))) |
38 | | |
39 | | /* Structure to map valid 'cinv' instruction options. */ |
40 | | |
41 | | typedef struct |
42 | | { |
43 | | /* Cinv printed string. */ |
44 | | char *istr; |
45 | | /* Value corresponding to the string. */ |
46 | | char *ostr; |
47 | | } |
48 | | cinv_entry; |
49 | | |
50 | | /* CR16 'cinv' options mapping. */ |
51 | | static const cinv_entry cr16_cinvs[] = |
52 | | { |
53 | | {"cinv[i]", "cinv [i]"}, |
54 | | {"cinv[i,u]", "cinv [i,u]"}, |
55 | | {"cinv[d]", "cinv [d]"}, |
56 | | {"cinv[d,u]", "cinv [d,u]"}, |
57 | | {"cinv[d,i]", "cinv [d,i]"}, |
58 | | {"cinv[d,i,u]", "cinv [d,i,u]"} |
59 | | }; |
60 | | |
61 | | /* Number of valid 'cinv' instruction options. */ |
62 | | static int NUMCINVS = ARRAY_SIZE (cr16_cinvs); |
63 | | |
64 | | /* Enum to distinguish different registers argument types. */ |
65 | | typedef enum REG_ARG_TYPE |
66 | | { |
67 | | /* General purpose register (r<N>). */ |
68 | | REG_ARG = 0, |
69 | | /*Processor register */ |
70 | | P_ARG, |
71 | | } |
72 | | REG_ARG_TYPE; |
73 | | |
74 | | /* Current opcode table entry we're disassembling. */ |
75 | | static const inst *instruction; |
76 | | /* Current instruction we're disassembling. */ |
77 | | static ins cr16_currInsn; |
78 | | /* The current instruction is read into 3 consecutive words. */ |
79 | | static wordU cr16_words[3]; |
80 | | /* Contains all words in appropriate order. */ |
81 | | static ULONGLONG cr16_allWords; |
82 | | /* Nonzero means the instruction's original size is |
83 | | incremented (escape sequence is used). */ |
84 | | static int size_changed; |
85 | | |
86 | | |
87 | | /* Print the constant expression length. */ |
88 | | |
89 | | static char * |
90 | | print_exp_len (int size) |
91 | 89.0k | { |
92 | 89.0k | switch (size) |
93 | 89.0k | { |
94 | 0 | case 4: |
95 | 0 | case 5: |
96 | 0 | case 6: |
97 | 0 | case 8: |
98 | 0 | case 14: |
99 | 53.4k | case 16: |
100 | 53.4k | return ":s"; |
101 | 0 | case 20: |
102 | 0 | case 24: |
103 | 33.6k | case 32: |
104 | 33.6k | return ":m"; |
105 | 1.91k | case 48: |
106 | 1.91k | return ":l"; |
107 | 0 | default: |
108 | 0 | return ""; |
109 | 89.0k | } |
110 | 89.0k | } |
111 | | |
112 | | |
113 | | /* Retrieve the number of operands for the current assembled instruction. */ |
114 | | |
115 | | static int |
116 | | get_number_of_operands (void) |
117 | 158k | { |
118 | 158k | int i; |
119 | | |
120 | 465k | for (i = 0; instruction->operands[i].op_type && i < MAX_OPERANDS; i++) |
121 | 306k | ; |
122 | | |
123 | 158k | return i; |
124 | 158k | } |
125 | | |
126 | | /* Return the bit size for a given operand. */ |
127 | | |
128 | | static int |
129 | | getbits (operand_type op) |
130 | 306k | { |
131 | 306k | if (op < MAX_OPRD) |
132 | 306k | return cr16_optab[op].bit_size; |
133 | | |
134 | 0 | return 0; |
135 | 306k | } |
136 | | |
137 | | /* Return the argument type of a given operand. */ |
138 | | |
139 | | static argtype |
140 | | getargtype (operand_type op) |
141 | 306k | { |
142 | 306k | if (op < MAX_OPRD) |
143 | 306k | return cr16_optab[op].arg_type; |
144 | | |
145 | 0 | return nullargs; |
146 | 306k | } |
147 | | |
148 | | /* Given a 'CC' instruction constant operand, return its corresponding |
149 | | string. This routine is used when disassembling the 'CC' instruction. */ |
150 | | |
151 | | static char * |
152 | | getccstring (unsigned cc_insn) |
153 | 12.3k | { |
154 | 12.3k | return (char *) cr16_b_cond_tab[cc_insn]; |
155 | 12.3k | } |
156 | | |
157 | | |
158 | | /* Given a 'cinv' instruction constant operand, return its corresponding |
159 | | string. This routine is used when disassembling the 'cinv' instruction. */ |
160 | | |
161 | | static char * |
162 | | getcinvstring (const char *str) |
163 | 1.39k | { |
164 | 1.39k | const cinv_entry *cinv; |
165 | | |
166 | 5.43k | for (cinv = cr16_cinvs; cinv < (cr16_cinvs + NUMCINVS); cinv++) |
167 | 5.43k | if (strcmp (cinv->istr, str) == 0) |
168 | 1.39k | return cinv->ostr; |
169 | | |
170 | 0 | return ILLEGAL; |
171 | 1.39k | } |
172 | | |
173 | | /* Given the trap index in dispatch table, return its name. |
174 | | This routine is used when disassembling the 'excp' instruction. */ |
175 | | |
176 | | static char * |
177 | | gettrapstring (unsigned int trap_index) |
178 | 902 | { |
179 | 902 | const trap_entry *trap; |
180 | | |
181 | 6.95k | for (trap = cr16_traps; trap < cr16_traps + NUMTRAPS; trap++) |
182 | 6.47k | if (trap->entry == trap_index) |
183 | 417 | return trap->name; |
184 | | |
185 | 485 | return ILLEGAL; |
186 | 902 | } |
187 | | |
188 | | /* Given a register enum value, retrieve its name. */ |
189 | | |
190 | | static char * |
191 | | getregname (reg r) |
192 | 109k | { |
193 | 109k | const reg_entry * regentry = cr16_regtab + r; |
194 | | |
195 | 109k | if (regentry->type != CR16_R_REGTYPE) |
196 | 0 | return ILLEGAL; |
197 | | |
198 | 109k | return regentry->name; |
199 | 109k | } |
200 | | |
201 | | /* Given a register pair enum value, retrieve its name. */ |
202 | | |
203 | | static char * |
204 | | getregpname (reg r) |
205 | 90.5k | { |
206 | 90.5k | const reg_entry * regentry = cr16_regptab + r; |
207 | | |
208 | 90.5k | if (regentry->type != CR16_RP_REGTYPE) |
209 | 0 | return ILLEGAL; |
210 | | |
211 | 90.5k | return regentry->name; |
212 | 90.5k | } |
213 | | |
214 | | /* Given a index register pair enum value, retrieve its name. */ |
215 | | |
216 | | static char * |
217 | | getidxregpname (reg r) |
218 | 4.91k | { |
219 | 4.91k | const reg_entry * regentry; |
220 | | |
221 | 4.91k | switch (r) |
222 | 4.91k | { |
223 | 840 | case 0: r = 0; break; |
224 | 257 | case 1: r = 2; break; |
225 | 339 | case 2: r = 4; break; |
226 | 687 | case 3: r = 6; break; |
227 | 200 | case 4: r = 8; break; |
228 | 176 | case 5: r = 10; break; |
229 | 1.86k | case 6: r = 3; break; |
230 | 556 | case 7: r = 5; break; |
231 | 0 | default: |
232 | 0 | break; |
233 | 4.91k | } |
234 | | |
235 | 4.91k | regentry = cr16_regptab + r; |
236 | | |
237 | 4.91k | if (regentry->type != CR16_RP_REGTYPE) |
238 | 0 | return ILLEGAL; |
239 | | |
240 | 4.91k | return regentry->name; |
241 | 4.91k | } |
242 | | |
243 | | /* Getting a processor register name. */ |
244 | | |
245 | | static char * |
246 | | getprocregname (int reg_index) |
247 | 491 | { |
248 | 491 | const reg_entry *r; |
249 | | |
250 | 912 | for (r = cr16_pregtab; r < cr16_pregtab + NUMPREGS; r++) |
251 | 912 | if (r->image == reg_index) |
252 | 491 | return r->name; |
253 | | |
254 | 0 | return "ILLEGAL REGISTER"; |
255 | 491 | } |
256 | | |
257 | | /* Getting a processor register name - 32 bit size. */ |
258 | | |
259 | | static char * |
260 | | getprocpregname (int reg_index) |
261 | 423 | { |
262 | 423 | const reg_entry *r; |
263 | | |
264 | 3.86k | for (r = cr16_pregptab; r < cr16_pregptab + NUMPREGPS; r++) |
265 | 3.65k | if (r->image == reg_index) |
266 | 210 | return r->name; |
267 | | |
268 | 213 | return "ILLEGAL REGISTER"; |
269 | 423 | } |
270 | | |
271 | | /* START and END are relating 'cr16_allWords' struct, which is 48 bits size. |
272 | | |
273 | | START|--------|END |
274 | | +---------+---------+---------+---------+ |
275 | | | | V | A | L | |
276 | | +---------+---------+---------+---------+ |
277 | | 0 16 32 48 |
278 | | words [0] [1] [2] */ |
279 | | |
280 | | static inline dwordU |
281 | | makelongparameter (ULONGLONG val, int start, int end) |
282 | 371k | { |
283 | 371k | return EXTRACT (val, 48 - end, end - start); |
284 | 371k | } |
285 | | |
286 | | /* Build a mask of the instruction's 'constant' opcode, |
287 | | based on the instruction's printing flags. */ |
288 | | |
289 | | static unsigned long |
290 | | build_mask (void) |
291 | 38.4M | { |
292 | 38.4M | unsigned long mask = SBM (instruction->match_bits); |
293 | | |
294 | | /* Adjust mask for bcond with 32-bit size instruction. */ |
295 | 38.4M | if ((IS_INSN_MNEMONIC("b") && instruction->size == 2)) |
296 | 102k | mask = 0xff0f0000; |
297 | | |
298 | 38.4M | return mask; |
299 | 38.4M | } |
300 | | |
301 | | /* Search for a matching opcode. Return 1 for success, 0 for failure. */ |
302 | | |
303 | | int |
304 | | cr16_match_opcode (void) |
305 | 197k | { |
306 | 197k | unsigned long mask; |
307 | | /* The instruction 'constant' opcode doesn't exceed 32 bits. */ |
308 | 197k | unsigned long doubleWord = cr16_words[1] + ((unsigned) cr16_words[0] << 16); |
309 | | |
310 | | /* Start searching from end of instruction table. */ |
311 | 197k | instruction = &cr16_instruction[NUMOPCODES - 2]; |
312 | | |
313 | | /* Loop over instruction table until a full match is found. */ |
314 | 38.5M | while (instruction >= cr16_instruction) |
315 | 38.4M | { |
316 | 38.4M | mask = build_mask (); |
317 | | |
318 | 38.4M | if ((doubleWord & mask) == BIN (instruction->match, |
319 | 38.4M | instruction->match_bits)) |
320 | 158k | return 1; |
321 | 38.3M | else |
322 | 38.3M | instruction--; |
323 | 38.4M | } |
324 | 39.1k | return 0; |
325 | 197k | } |
326 | | |
327 | | /* Set the proper parameter value for different type of arguments. */ |
328 | | |
329 | | static void |
330 | | make_argument (argument * a, int start_bits) |
331 | 306k | { |
332 | 306k | int inst_bit_size; |
333 | 306k | dwordU p; |
334 | | |
335 | 306k | if ((instruction->size == 3) && a->size >= 16) |
336 | 7.55k | inst_bit_size = 48; |
337 | 299k | else |
338 | 299k | inst_bit_size = 32; |
339 | | |
340 | 306k | switch (a->type) |
341 | 306k | { |
342 | 109k | case arg_r: |
343 | 109k | p = makelongparameter (cr16_allWords, |
344 | 109k | inst_bit_size - (start_bits + a->size), |
345 | 109k | inst_bit_size - start_bits); |
346 | 109k | a->r = p; |
347 | 109k | break; |
348 | | |
349 | 39.6k | case arg_rp: |
350 | 39.6k | p = makelongparameter (cr16_allWords, |
351 | 39.6k | inst_bit_size - (start_bits + a->size), |
352 | 39.6k | inst_bit_size - start_bits); |
353 | 39.6k | a->rp = p; |
354 | 39.6k | break; |
355 | | |
356 | 490 | case arg_pr: |
357 | 490 | p = makelongparameter (cr16_allWords, |
358 | 490 | inst_bit_size - (start_bits + a->size), |
359 | 490 | inst_bit_size - start_bits); |
360 | 490 | a->pr = p; |
361 | 490 | break; |
362 | | |
363 | 423 | case arg_prp: |
364 | 423 | p = makelongparameter (cr16_allWords, |
365 | 423 | inst_bit_size - (start_bits + a->size), |
366 | 423 | inst_bit_size - start_bits); |
367 | 423 | a->prp = p; |
368 | 423 | break; |
369 | | |
370 | 55.8k | case arg_ic: |
371 | 55.8k | p = makelongparameter (cr16_allWords, |
372 | 55.8k | inst_bit_size - (start_bits + a->size), |
373 | 55.8k | inst_bit_size - start_bits); |
374 | 55.8k | a->constant = p; |
375 | 55.8k | break; |
376 | | |
377 | 12.3k | case arg_cc: |
378 | 12.3k | p = makelongparameter (cr16_allWords, |
379 | 12.3k | inst_bit_size - (start_bits + a->size), |
380 | 12.3k | inst_bit_size - start_bits); |
381 | 12.3k | a->cc = p; |
382 | 12.3k | break; |
383 | | |
384 | 9.33k | case arg_idxr: |
385 | 9.33k | if (IS_INSN_TYPE (CSTBIT_INS) && instruction->mnemonic[4] == 'b') |
386 | 2.10k | p = makelongparameter (cr16_allWords, 8, 9); |
387 | 7.22k | else |
388 | 7.22k | p = makelongparameter (cr16_allWords, 9, 10); |
389 | 9.33k | a->i_r = p; |
390 | 9.33k | p = makelongparameter (cr16_allWords, |
391 | 9.33k | inst_bit_size - a->size, inst_bit_size); |
392 | 9.33k | a->constant = p; |
393 | 9.33k | break; |
394 | | |
395 | 4.90k | case arg_idxrp: |
396 | 4.90k | p = makelongparameter (cr16_allWords, start_bits + 12, start_bits + 13); |
397 | 4.90k | a->i_r = p; |
398 | 4.90k | p = makelongparameter (cr16_allWords, start_bits + 13, start_bits + 16); |
399 | 4.90k | a->rp = p; |
400 | 4.90k | if (inst_bit_size > 32) |
401 | 508 | { |
402 | 508 | p = makelongparameter (cr16_allWords, inst_bit_size - start_bits - 12, |
403 | 508 | inst_bit_size); |
404 | 508 | a->constant = (p & 0xffff) | (p >> 8 & 0xf0000); |
405 | 508 | } |
406 | 4.39k | else if (instruction->size == 2) |
407 | 949 | { |
408 | 949 | p = makelongparameter (cr16_allWords, inst_bit_size - 22, |
409 | 949 | inst_bit_size); |
410 | 949 | a->constant = ((p & 0xf) | (((p >> 20) & 0x3) << 4) |
411 | 949 | | ((p >> 14 & 0x3) << 6) | (((p >>7) & 0x1f) << 7)); |
412 | 949 | } |
413 | 3.44k | else if (instruction->size == 1 && a->size == 0) |
414 | 3.44k | a->constant = 0; |
415 | | |
416 | 4.90k | break; |
417 | | |
418 | 0 | case arg_rbase: |
419 | 0 | p = makelongparameter (cr16_allWords, inst_bit_size, inst_bit_size); |
420 | 0 | a->constant = p; |
421 | 0 | p = makelongparameter (cr16_allWords, inst_bit_size - (start_bits + 4), |
422 | 0 | inst_bit_size - start_bits); |
423 | 0 | a->r = p; |
424 | 0 | break; |
425 | | |
426 | 275 | case arg_cr: |
427 | 275 | p = makelongparameter (cr16_allWords, start_bits + 12, start_bits + 16); |
428 | 275 | a->r = p; |
429 | 275 | p = makelongparameter (cr16_allWords, inst_bit_size - 28, inst_bit_size); |
430 | 275 | a->constant = ((p >> 8) & 0xf0000) | (p & 0xffff); |
431 | 275 | break; |
432 | | |
433 | 52.5k | case arg_crp: |
434 | 52.5k | if (instruction->size == 1) |
435 | 35.8k | p = makelongparameter (cr16_allWords, 12, 16); |
436 | 16.6k | else |
437 | 16.6k | p = makelongparameter (cr16_allWords, start_bits + 12, start_bits + 16); |
438 | 52.5k | a->rp = p; |
439 | | |
440 | 52.5k | if (inst_bit_size > 32) |
441 | 300 | { |
442 | 300 | p = makelongparameter (cr16_allWords, inst_bit_size - start_bits - 12, |
443 | 300 | inst_bit_size); |
444 | 300 | a->constant = ((p & 0xffff) | (p >> 8 & 0xf0000)); |
445 | 300 | } |
446 | 52.2k | else if (instruction->size == 2) |
447 | 16.3k | { |
448 | 16.3k | p = makelongparameter (cr16_allWords, inst_bit_size - 16, |
449 | 16.3k | inst_bit_size); |
450 | 16.3k | a->constant = p; |
451 | 16.3k | } |
452 | 35.8k | else if (instruction->size == 1 && a->size != 0) |
453 | 31.8k | { |
454 | 31.8k | p = makelongparameter (cr16_allWords, 4, 8); |
455 | 31.8k | if (IS_INSN_MNEMONIC ("loadw") |
456 | 27.0k | || IS_INSN_MNEMONIC ("loadd") |
457 | 21.4k | || IS_INSN_MNEMONIC ("storw") |
458 | 16.5k | || IS_INSN_MNEMONIC ("stord")) |
459 | 20.9k | a->constant = p * 2; |
460 | 10.8k | else |
461 | 10.8k | a->constant = p; |
462 | 31.8k | } |
463 | 4.08k | else /* below case for 0x0(reg pair) */ |
464 | 4.08k | a->constant = 0; |
465 | | |
466 | 52.5k | break; |
467 | | |
468 | 21.7k | case arg_c: |
469 | | |
470 | 21.7k | if ((IS_INSN_TYPE (BRANCH_INS)) |
471 | 9.35k | || (IS_INSN_MNEMONIC ("bal")) |
472 | 6.98k | || (IS_INSN_TYPE (CSTBIT_INS)) |
473 | 5.20k | || (IS_INSN_TYPE (LD_STOR_INS))) |
474 | 19.3k | { |
475 | 19.3k | switch (a->size) |
476 | 19.3k | { |
477 | 11.4k | case 8 : |
478 | 11.4k | p = makelongparameter (cr16_allWords, 0, start_bits); |
479 | 11.4k | a->constant = ((p & 0xf00) >> 4) | (p & 0xf); |
480 | 11.4k | break; |
481 | | |
482 | 2.86k | case 24: |
483 | 2.86k | if (instruction->size == 3) |
484 | 834 | { |
485 | 834 | p = makelongparameter (cr16_allWords, 16, inst_bit_size); |
486 | 834 | a->constant = ((((p >> 16) & 0xf) << 20) |
487 | 834 | | (((p >> 24) & 0xf) << 16) |
488 | 834 | | (p & 0xffff)); |
489 | 834 | } |
490 | 2.03k | else if (instruction->size == 2) |
491 | 2.03k | { |
492 | 2.03k | p = makelongparameter (cr16_allWords, 8, inst_bit_size); |
493 | 2.03k | a->constant = p; |
494 | 2.03k | } |
495 | 2.86k | break; |
496 | | |
497 | 4.99k | default: |
498 | 4.99k | p = makelongparameter (cr16_allWords, |
499 | 4.99k | inst_bit_size - (start_bits + a->size), |
500 | 4.99k | inst_bit_size - start_bits); |
501 | 4.99k | a->constant = p; |
502 | 4.99k | break; |
503 | 19.3k | } |
504 | 19.3k | } |
505 | 2.45k | else |
506 | 2.45k | { |
507 | 2.45k | p = makelongparameter (cr16_allWords, |
508 | 2.45k | inst_bit_size - (start_bits + a->size), |
509 | 2.45k | inst_bit_size - start_bits); |
510 | 2.45k | a->constant = p; |
511 | 2.45k | } |
512 | 21.7k | break; |
513 | | |
514 | 21.7k | default: |
515 | 0 | break; |
516 | 306k | } |
517 | 306k | } |
518 | | |
519 | | /* Print a single argument. */ |
520 | | |
521 | | static void |
522 | | print_arg (argument *a, bfd_vma memaddr, struct disassemble_info *info) |
523 | 305k | { |
524 | 305k | LONGLONG longdisp, mask; |
525 | 305k | int sign_flag = 0; |
526 | 305k | int relative = 0; |
527 | 305k | bfd_vma number; |
528 | 305k | void *stream = info->stream; |
529 | 305k | fprintf_ftype func = info->fprintf_func; |
530 | | |
531 | 305k | switch (a->type) |
532 | 305k | { |
533 | 109k | case arg_r: |
534 | 109k | func (stream, "%s", getregname (a->r)); |
535 | 109k | break; |
536 | | |
537 | 37.8k | case arg_rp: |
538 | 37.8k | func (stream, "%s", getregpname (a->rp)); |
539 | 37.8k | break; |
540 | | |
541 | 491 | case arg_pr: |
542 | 491 | func (stream, "%s", getprocregname (a->pr)); |
543 | 491 | break; |
544 | | |
545 | 423 | case arg_prp: |
546 | 423 | func (stream, "%s", getprocpregname (a->prp)); |
547 | 423 | break; |
548 | | |
549 | 12.3k | case arg_cc: |
550 | 12.3k | func (stream, "%s", getccstring (a->cc)); |
551 | 12.3k | func (stream, "%s", "\t"); |
552 | 12.3k | break; |
553 | | |
554 | 55.8k | case arg_ic: |
555 | 55.8k | if (IS_INSN_MNEMONIC ("excp")) |
556 | 902 | { |
557 | 902 | func (stream, "%s", gettrapstring (a->constant)); |
558 | 902 | break; |
559 | 902 | } |
560 | 54.9k | else if ((IS_INSN_TYPE (ARITH_INS) || IS_INSN_TYPE (ARITH_BYTE_INS)) |
561 | 20.5k | && ((instruction->size == 1) && (a->constant == 9))) |
562 | 334 | func (stream, "$%d", -1); |
563 | 54.6k | else if (INST_HAS_REG_LIST) |
564 | 8.48k | func (stream, "$0x%lx", a->constant +1); |
565 | 46.1k | else if (IS_INSN_TYPE (SHIFT_INS)) |
566 | 7.40k | { |
567 | 7.40k | longdisp = a->constant; |
568 | 7.40k | mask = ((LONGLONG)1 << a->size) - 1; |
569 | 7.40k | if (longdisp & ((LONGLONG)1 << (a->size -1))) |
570 | 3.09k | { |
571 | 3.09k | sign_flag = 1; |
572 | 3.09k | longdisp = ~(longdisp) + 1; |
573 | 3.09k | } |
574 | 7.40k | a->constant = (unsigned long int) (longdisp & mask); |
575 | 7.40k | func (stream, "$%d", ((int)(sign_flag ? -a->constant : |
576 | 7.40k | a->constant))); |
577 | 7.40k | } |
578 | 38.7k | else |
579 | 38.7k | func (stream, "$0x%lx", a->constant); |
580 | 54.9k | switch (a->size) |
581 | 54.9k | { |
582 | 32.9k | case 4 : case 5 : case 6 : case 8 : |
583 | 32.9k | func (stream, "%s", ":s"); break; |
584 | 2.78k | case 16 : case 20 : func (stream, "%s", ":m"); break; |
585 | 5.64k | case 24 : case 32 : func (stream, "%s", ":l"); break; |
586 | 13.6k | default: break; |
587 | 54.9k | } |
588 | 54.9k | break; |
589 | | |
590 | 54.9k | case arg_idxr: |
591 | 9.35k | if (a->i_r == 0) func (stream, "[r12]"); |
592 | 9.35k | if (a->i_r == 1) func (stream, "[r13]"); |
593 | 9.35k | func (stream, "0x%lx", a->constant); |
594 | 9.35k | func (stream, "%s", print_exp_len (instruction->size * 16)); |
595 | 9.35k | break; |
596 | | |
597 | 4.91k | case arg_idxrp: |
598 | 4.91k | if (a->i_r == 0) func (stream, "[r12]"); |
599 | 4.91k | if (a->i_r == 1) func (stream, "[r13]"); |
600 | 4.91k | func (stream, "0x%lx", a->constant); |
601 | 4.91k | func (stream, "%s", print_exp_len (instruction->size * 16)); |
602 | 4.91k | func (stream, "%s", getidxregpname (a->rp)); |
603 | 4.91k | break; |
604 | | |
605 | 0 | case arg_rbase: |
606 | 0 | func (stream, "(%s)", getregname (a->r)); |
607 | 0 | break; |
608 | | |
609 | 275 | case arg_cr: |
610 | 275 | func (stream, "0x%lx", a->constant); |
611 | 275 | func (stream, "%s", print_exp_len (instruction->size * 16)); |
612 | 275 | func (stream, "(%s)", getregname (a->r)); |
613 | 275 | break; |
614 | | |
615 | 52.6k | case arg_crp: |
616 | 52.6k | func (stream, "0x%lx", a->constant); |
617 | 52.6k | func (stream, "%s", print_exp_len (instruction->size * 16)); |
618 | 52.6k | func (stream, "%s", getregpname (a->rp)); |
619 | 52.6k | break; |
620 | | |
621 | 21.8k | case arg_c: |
622 | | /*Removed the *2 part as because implicit zeros are no more required. |
623 | | Have to fix this as this needs a bit of extension in terms of branch |
624 | | instructions. */ |
625 | 21.8k | if (IS_INSN_TYPE (BRANCH_INS) || IS_INSN_MNEMONIC ("bal")) |
626 | 14.7k | { |
627 | 14.7k | relative = 1; |
628 | 14.7k | longdisp = a->constant; |
629 | | /* REVISIT: To sync with WinIDEA and CR16 4.1tools, the below |
630 | | line commented */ |
631 | | /* longdisp <<= 1; */ |
632 | 14.7k | mask = ((LONGLONG)1 << a->size) - 1; |
633 | 14.7k | switch (a->size) |
634 | 14.7k | { |
635 | 11.4k | case 8 : |
636 | 11.4k | { |
637 | 11.4k | longdisp <<= 1; |
638 | 11.4k | if (longdisp & ((LONGLONG)1 << a->size)) |
639 | 4.88k | { |
640 | 4.88k | sign_flag = 1; |
641 | 4.88k | longdisp = ~(longdisp) + 1; |
642 | 4.88k | } |
643 | 11.4k | break; |
644 | 0 | } |
645 | 491 | case 16 : |
646 | 3.32k | case 24 : |
647 | 3.32k | { |
648 | 3.32k | if (longdisp & 1) |
649 | 486 | { |
650 | 486 | sign_flag = 1; |
651 | 486 | longdisp = ~(longdisp) + 1; |
652 | 486 | } |
653 | 3.32k | break; |
654 | 491 | } |
655 | 0 | default: |
656 | 0 | func (stream, "Wrong offset used in branch/bal instruction"); |
657 | 0 | break; |
658 | 14.7k | } |
659 | 14.7k | a->constant = (unsigned long int) (longdisp & mask); |
660 | 14.7k | } |
661 | | /* For branch Neq instruction it is 2*offset + 2. */ |
662 | 7.10k | else if (IS_INSN_TYPE (BRANCH_NEQ_INS)) |
663 | 2.45k | a->constant = 2 * a->constant + 2; |
664 | | |
665 | 21.8k | if ((!IS_INSN_TYPE (CSTBIT_INS)) && (!IS_INSN_TYPE (LD_STOR_INS))) |
666 | 17.3k | (sign_flag) ? func (stream, "%s", "*-"): func (stream, "%s","*+"); |
667 | | |
668 | | /* PR 10173: Avoid printing the 0x prefix twice. */ |
669 | 21.8k | if (info->symtab_size > 0) |
670 | 0 | func (stream, "%s", "0x"); |
671 | 21.8k | number = ((relative ? memaddr : 0) + |
672 | 21.8k | (sign_flag ? ((- a->constant) & 0xffffffe) : a->constant)); |
673 | | |
674 | 21.8k | (*info->print_address_func) ((number & ((1 << 24) - 1)), info); |
675 | | |
676 | 21.8k | func (stream, "%s", print_exp_len (instruction->size * 16)); |
677 | 21.8k | break; |
678 | | |
679 | 0 | default: |
680 | 0 | break; |
681 | 305k | } |
682 | 305k | } |
683 | | |
684 | | /* Print all the arguments of CURRINSN instruction. */ |
685 | | |
686 | | static void |
687 | | print_arguments (ins *currentInsn, bfd_vma memaddr, struct disassemble_info *info) |
688 | 158k | { |
689 | 158k | int i; |
690 | | |
691 | | /* For "pop/push/popret RA instruction only. */ |
692 | 158k | if ((IS_INSN_MNEMONIC ("pop") |
693 | 155k | || (IS_INSN_MNEMONIC ("popret") |
694 | 153k | || (IS_INSN_MNEMONIC ("push")))) |
695 | 7.94k | && currentInsn->nargs == 1) |
696 | 110 | { |
697 | 110 | info->fprintf_func (info->stream, "RA"); |
698 | 110 | return; |
699 | 110 | } |
700 | | |
701 | 467k | for (i = 0; i < currentInsn->nargs; i++) |
702 | 308k | { |
703 | | /* For "bal (ra), disp17" instruction only. */ |
704 | 308k | if ((IS_INSN_MNEMONIC ("bal")) && (i == 0) && instruction->size == 2) |
705 | 2.03k | { |
706 | 2.03k | info->fprintf_func (info->stream, "(ra),"); |
707 | 2.03k | continue; |
708 | 2.03k | } |
709 | | |
710 | 306k | if ((INST_HAS_REG_LIST) && (i == 2)) |
711 | 1.67k | info->fprintf_func (info->stream, "RA"); |
712 | 305k | else |
713 | 305k | print_arg (¤tInsn->arg[i], memaddr, info); |
714 | | |
715 | 306k | if ((i != currentInsn->nargs - 1) && (!IS_INSN_MNEMONIC ("b"))) |
716 | 139k | info->fprintf_func (info->stream, ","); |
717 | 306k | } |
718 | 158k | } |
719 | | |
720 | | /* Build the instruction's arguments. */ |
721 | | |
722 | | void |
723 | | cr16_make_instruction (void) |
724 | 158k | { |
725 | 158k | int i; |
726 | 158k | unsigned int shift; |
727 | | |
728 | 465k | for (i = 0; i < cr16_currInsn.nargs; i++) |
729 | 306k | { |
730 | 306k | argument a; |
731 | | |
732 | 306k | memset (&a, 0, sizeof (a)); |
733 | 306k | a.type = getargtype (instruction->operands[i].op_type); |
734 | 306k | a.size = getbits (instruction->operands[i].op_type); |
735 | 306k | shift = instruction->operands[i].shift; |
736 | | |
737 | 306k | make_argument (&a, shift); |
738 | 306k | cr16_currInsn.arg[i] = a; |
739 | 306k | } |
740 | | |
741 | | /* Calculate instruction size (in bytes). */ |
742 | 158k | cr16_currInsn.size = instruction->size + (size_changed ? 1 : 0); |
743 | | /* Now in bits. */ |
744 | 158k | cr16_currInsn.size *= 2; |
745 | 158k | } |
746 | | |
747 | | /* Retrieve a single word from a given memory address. */ |
748 | | |
749 | | static wordU |
750 | | get_word_at_PC (bfd_vma memaddr, struct disassemble_info *info) |
751 | 593k | { |
752 | 593k | bfd_byte buffer[4]; |
753 | 593k | int status; |
754 | 593k | wordU insn = 0; |
755 | | |
756 | 593k | status = info->read_memory_func (memaddr, buffer, 2, info); |
757 | | |
758 | 593k | if (status == 0) |
759 | 592k | insn = (wordU) bfd_getl16 (buffer); |
760 | | |
761 | 593k | return insn; |
762 | 593k | } |
763 | | |
764 | | /* Retrieve multiple words (3) from a given memory address. */ |
765 | | |
766 | | static void |
767 | | get_words_at_PC (bfd_vma memaddr, struct disassemble_info *info) |
768 | 197k | { |
769 | 197k | int i; |
770 | 197k | bfd_vma mem; |
771 | | |
772 | 791k | for (i = 0, mem = memaddr; i < 3; i++, mem += 2) |
773 | 593k | cr16_words[i] = get_word_at_PC (mem, info); |
774 | | |
775 | 197k | cr16_allWords = ((ULONGLONG) cr16_words[0] << 32) |
776 | 197k | + ((unsigned long) cr16_words[1] << 16) + cr16_words[2]; |
777 | 197k | } |
778 | | |
779 | | /* Prints the instruction by calling print_arguments after proper matching. */ |
780 | | |
781 | | int |
782 | | print_insn_cr16 (bfd_vma memaddr, struct disassemble_info *info) |
783 | 197k | { |
784 | 197k | int is_decoded; /* Nonzero means instruction has a match. */ |
785 | | |
786 | | /* Initialize global variables. */ |
787 | 197k | size_changed = 0; |
788 | | |
789 | | /* Retrieve the encoding from current memory location. */ |
790 | 197k | get_words_at_PC (memaddr, info); |
791 | | /* Find a matching opcode in table. */ |
792 | 197k | is_decoded = cr16_match_opcode (); |
793 | | /* If found, print the instruction's mnemonic and arguments. */ |
794 | 197k | if (is_decoded > 0 && (cr16_words[0] != 0 || cr16_words[1] != 0)) |
795 | 158k | { |
796 | 158k | if (startswith (instruction->mnemonic, "cinv")) |
797 | 1.39k | info->fprintf_func (info->stream,"%s", |
798 | 1.39k | getcinvstring (instruction->mnemonic)); |
799 | 157k | else |
800 | 157k | info->fprintf_func (info->stream, "%s", instruction->mnemonic); |
801 | | |
802 | 158k | if (((cr16_currInsn.nargs = get_number_of_operands ()) != 0) |
803 | 155k | && ! (IS_INSN_MNEMONIC ("b"))) |
804 | 143k | info->fprintf_func (info->stream, "\t"); |
805 | 158k | cr16_make_instruction (); |
806 | | /* For push/pop/pushrtn with RA instructions. */ |
807 | 158k | if ((INST_HAS_REG_LIST) && ((cr16_words[0] >> 7) & 0x1)) |
808 | 2.32k | cr16_currInsn.nargs +=1; |
809 | 158k | print_arguments (&cr16_currInsn, memaddr, info); |
810 | 158k | return cr16_currInsn.size; |
811 | 158k | } |
812 | | |
813 | | /* No match found. */ |
814 | 39.1k | info->fprintf_func (info->stream,"%s ",ILLEGAL); |
815 | 39.1k | return 2; |
816 | 197k | } |