1 /* IP2K opcode support. -*- C -*-
2 Copyright 2002, 2005, 2011 Free Software Foundation, Inc.
4 Contributed by Red Hat Inc;
6 This file is part of the GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 Each section is delimited with start and end markers.
26 <arch>-opc.h additions use: "-- opc.h"
27 <arch>-opc.c additions use: "-- opc.c"
28 <arch>-asm.c additions use: "-- asm.c"
29 <arch>-dis.c additions use: "-- dis.c"
30 <arch>-ibd.h additions use: "-- ibd.h". */
34 /* Check applicability of instructions against machines. */
35 #define CGEN_VALIDATE_INSN_SUPPORTED
37 /* Allows reason codes to be output when assembler errors occur. */
38 #define CGEN_VERBOSE_ASSEMBLER_ERRORS
40 /* Override disassembly hashing - there are variable bits in the top
41 byte of these instructions. */
42 #define CGEN_DIS_HASH_SIZE 8
43 #define CGEN_DIS_HASH(buf, value) \
44 (((* (unsigned char*) (buf)) >> 5) % CGEN_DIS_HASH_SIZE)
46 #define CGEN_ASM_HASH_SIZE 127
47 #define CGEN_ASM_HASH(insn) ip2k_asm_hash (insn)
49 extern unsigned int ip2k_asm_hash (const char *);
50 extern int ip2k_cgen_insn_supported (CGEN_CPU_DESC, const CGEN_INSN *);
54 #include "safe-ctype.h"
56 /* A better hash function for instruction mnemonics. */
58 ip2k_asm_hash (const char* insn)
63 for (hash = 0; *m && ! ISSPACE (*m); m++)
64 hash = (hash * 23) ^ (0x1F & TOLOWER (*m));
66 /* printf ("%s %d\n", insn, (hash % CGEN_ASM_HASH_SIZE)); */
68 return hash % CGEN_ASM_HASH_SIZE;
72 /* Special check to ensure that instruction exists for given machine. */
75 ip2k_cgen_insn_supported (CGEN_CPU_DESC cd, const CGEN_INSN *insn)
77 int machs = CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_MACH);
79 /* No mach attribute? Assume it's supported for all machs. */
83 return (machs & cd->machs) != 0;
90 parse_fr (CGEN_CPU_DESC cd,
93 unsigned long *valuep)
98 enum cgen_parse_operand_result result_type;
100 extern CGEN_KEYWORD ip2k_cgen_opval_register_names;
106 /* Check here to see if you're about to try parsing a w as the first arg
107 and return an error if you are. */
108 if ((strncmp (*strp, "w", 1) == 0) || (strncmp (*strp, "W", 1) == 0))
112 if ((strncmp (*strp, ",", 1) == 0) || ISSPACE (**strp))
114 /* We've been passed a w. Return with an error message so that
115 cgen will try the next parsing option. */
116 errmsg = _("W keyword invalid in FR operand slot.");
122 /* Attempt parse as register keyword. */
123 errmsg = cgen_parse_keyword (cd, strp, & ip2k_cgen_opval_register_names,
129 /* Attempt to parse for "(IP)". */
130 afteroffset = strstr (*strp, "(IP)");
132 if (afteroffset == NULL)
133 /* Make sure it's not in lower case. */
134 afteroffset = strstr (*strp, "(ip)");
136 if (afteroffset != NULL)
138 if (afteroffset != *strp)
140 /* Invalid offset present. */
141 errmsg = _("offset(IP) is not a valid form");
153 /* Attempt to parse for DP. ex: mov w, offset(DP)
156 /* Try parsing it as an address and see what comes back. */
157 afteroffset = strstr (*strp, "(DP)");
159 if (afteroffset == NULL)
160 /* Maybe it's in lower case. */
161 afteroffset = strstr (*strp, "(dp)");
163 if (afteroffset != NULL)
165 if (afteroffset == *strp)
167 /* No offset present. Use 0 by default. */
172 errmsg = cgen_parse_address (cd, strp, opindex,
173 BFD_RELOC_IP2K_FR_OFFSET,
174 & result_type, & tempvalue);
178 if (tempvalue <= 127)
180 /* Value is ok. Fix up the first 2 bits and return. */
181 *valuep = 0x0100 | tempvalue;
182 *strp += 4; /* Skip over the (DP) in *strp. */
187 /* Found something there in front of (DP) but it's out
189 errmsg = _("(DP) offset out of range.");
196 /* Attempt to parse for SP. ex: mov w, offset(SP)
197 mov offset(SP), w. */
198 afteroffset = strstr (*strp, "(SP)");
200 if (afteroffset == NULL)
201 /* Maybe it's in lower case. */
202 afteroffset = strstr (*strp, "(sp)");
204 if (afteroffset != NULL)
206 if (afteroffset == *strp)
208 /* No offset present. Use 0 by default. */
213 errmsg = cgen_parse_address (cd, strp, opindex,
214 BFD_RELOC_IP2K_FR_OFFSET,
215 & result_type, & tempvalue);
219 if (tempvalue <= 127)
221 /* Value is ok. Fix up the first 2 bits and return. */
222 *valuep = 0x0180 | tempvalue;
223 *strp += 4; /* Skip over the (SP) in *strp. */
228 /* Found something there in front of (SP) but it's out
230 errmsg = _("(SP) offset out of range.");
236 /* Attempt to parse as an address. */
238 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IP2K_FR9,
239 & result_type, & value);
244 /* If a parenthesis is found, warn about invalid form. */
246 errmsg = _("illegal use of parentheses");
248 /* If a numeric value is specified, ensure that it is between
250 else if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
252 if (value < 0x1 || value > 0xff)
253 errmsg = _("operand out of range (not between 1 and 255)");
260 parse_addr16 (CGEN_CPU_DESC cd,
263 unsigned long *valuep)
266 enum cgen_parse_operand_result result_type;
267 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
270 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16H)
271 code = BFD_RELOC_IP2K_HI8DATA;
272 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16L)
273 code = BFD_RELOC_IP2K_LO8DATA;
276 /* Something is very wrong. opindex has to be one of the above. */
277 errmsg = _("parse_addr16: invalid opindex.");
281 errmsg = cgen_parse_address (cd, strp, opindex, code,
282 & result_type, & value);
285 /* We either have a relocation or a number now. */
286 if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
288 /* We got a number back. */
289 if (code == BFD_RELOC_IP2K_HI8DATA)
292 /* code = BFD_RELOC_IP2K_LOW8DATA. */
302 parse_addr16_cjp (CGEN_CPU_DESC cd,
305 unsigned long *valuep)
308 enum cgen_parse_operand_result result_type;
309 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
312 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP)
313 code = BFD_RELOC_IP2K_ADDR16CJP;
314 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P)
315 code = BFD_RELOC_IP2K_PAGE3;
317 errmsg = cgen_parse_address (cd, strp, opindex, code,
318 & result_type, & value);
321 if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
323 if ((value & 0x1) == 0) /* If the address is even .... */
325 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP)
326 *valuep = (value >> 1) & 0x1FFF; /* Should mask be 1FFF? */
327 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P)
328 *valuep = (value >> 14) & 0x7;
331 errmsg = _("Byte address required. - must be even.");
333 else if (result_type == CGEN_PARSE_OPERAND_RESULT_QUEUED)
335 /* This will happen for things like (s2-s1) where s2 and s1
340 errmsg = _("cgen_parse_address returned a symbol. Literal required.");
346 parse_lit8 (CGEN_CPU_DESC cd,
352 enum cgen_parse_operand_result result_type;
353 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
356 /* Parse %OP relocating operators. */
357 if (strncmp (*strp, "%bank", 5) == 0)
360 code = BFD_RELOC_IP2K_BANK;
362 else if (strncmp (*strp, "%lo8data", 8) == 0)
365 code = BFD_RELOC_IP2K_LO8DATA;
367 else if (strncmp (*strp, "%hi8data", 8) == 0)
370 code = BFD_RELOC_IP2K_HI8DATA;
372 else if (strncmp (*strp, "%ex8data", 8) == 0)
375 code = BFD_RELOC_IP2K_EX8DATA;
377 else if (strncmp (*strp, "%lo8insn", 8) == 0)
380 code = BFD_RELOC_IP2K_LO8INSN;
382 else if (strncmp (*strp, "%hi8insn", 8) == 0)
385 code = BFD_RELOC_IP2K_HI8INSN;
388 /* Parse %op operand. */
389 if (code != BFD_RELOC_NONE)
391 errmsg = cgen_parse_address (cd, strp, opindex, code,
392 & result_type, & value);
393 if ((errmsg == NULL) &&
394 (result_type != CGEN_PARSE_OPERAND_RESULT_QUEUED))
395 errmsg = _("percent-operator operand is not a symbol");
399 /* Parse as a number. */
402 errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
404 /* Truncate to eight bits to accept both signed and unsigned input. */
413 parse_bit3 (CGEN_CPU_DESC cd,
416 unsigned long *valuep)
423 if (strncmp (*strp, "%bit", 4) == 0)
428 else if (strncmp (*strp, "%msbbit", 7) == 0)
433 else if (strncmp (*strp, "%lsbbit", 7) == 0)
439 errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
448 errmsg = _("Attempt to find bit index of 0");
455 while ((value & 0x80000000) == 0)
464 while ((value & 0x00000001) == 0)
480 print_fr (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
483 unsigned int attrs ATTRIBUTE_UNUSED,
484 bfd_vma pc ATTRIBUTE_UNUSED,
485 int length ATTRIBUTE_UNUSED)
487 disassemble_info *info = (disassemble_info *) dis_info;
488 const CGEN_KEYWORD_ENTRY *ke;
489 extern CGEN_KEYWORD ip2k_cgen_opval_register_names;
493 if (value == 0) /* This is (IP). */
495 (*info->fprintf_func) (info->stream, "%s", "(IP)");
499 offsettest = value >> 7;
500 offsetvalue = value & 0x7F;
502 /* Check to see if first two bits are 10 -> (DP). */
505 if (offsetvalue == 0)
506 (*info->fprintf_func) (info->stream, "%s","(DP)");
508 (*info->fprintf_func) (info->stream, "$%lx%s", offsetvalue, "(DP)");
512 /* Check to see if first two bits are 11 -> (SP). */
515 if (offsetvalue == 0)
516 (*info->fprintf_func) (info->stream, "%s", "(SP)");
518 (*info->fprintf_func) (info->stream, "$%lx%s", offsetvalue,"(SP)");
522 /* Attempt to print as a register keyword. */
523 ke = cgen_keyword_lookup_value (& ip2k_cgen_opval_register_names, value);
526 (*info->fprintf_func) (info->stream, "%s", ke->name);
528 /* Print as an address literal. */
529 (*info->fprintf_func) (info->stream, "$%02lx", value);
533 print_dollarhex (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
536 unsigned int attrs ATTRIBUTE_UNUSED,
537 bfd_vma pc ATTRIBUTE_UNUSED,
538 int length ATTRIBUTE_UNUSED)
540 disassemble_info *info = (disassemble_info *) dis_info;
542 (*info->fprintf_func) (info->stream, "$%lx", value);
546 print_dollarhex8 (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
549 unsigned int attrs ATTRIBUTE_UNUSED,
550 bfd_vma pc ATTRIBUTE_UNUSED,
551 int length ATTRIBUTE_UNUSED)
553 disassemble_info *info = (disassemble_info *) dis_info;
555 (*info->fprintf_func) (info->stream, "$%02lx", value);
559 print_dollarhex_addr16h (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
562 unsigned int attrs ATTRIBUTE_UNUSED,
563 bfd_vma pc ATTRIBUTE_UNUSED,
564 int length ATTRIBUTE_UNUSED)
566 disassemble_info *info = (disassemble_info *) dis_info;
568 /* This is a loadh instruction. Shift the value to the left
569 by 8 bits so that disassembled code will reassemble properly. */
570 value = ((value << 8) & 0xFF00);
572 (*info->fprintf_func) (info->stream, "$%04lx", value);
576 print_dollarhex_addr16l (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
579 unsigned int attrs ATTRIBUTE_UNUSED,
580 bfd_vma pc ATTRIBUTE_UNUSED,
581 int length ATTRIBUTE_UNUSED)
583 disassemble_info *info = (disassemble_info *) dis_info;
585 (*info->fprintf_func) (info->stream, "$%04lx", value);
589 print_dollarhex_p (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
592 unsigned int attrs ATTRIBUTE_UNUSED,
593 bfd_vma pc ATTRIBUTE_UNUSED,
594 int length ATTRIBUTE_UNUSED)
596 disassemble_info *info = (disassemble_info *) dis_info;
598 value = ((value << 14) & 0x1C000);
599 ;value = (value & 0x1FFFF);
600 (*info->fprintf_func) (info->stream, "$%05lx", value);
604 print_dollarhex_cj (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
607 unsigned int attrs ATTRIBUTE_UNUSED,
608 bfd_vma pc ATTRIBUTE_UNUSED,
609 int length ATTRIBUTE_UNUSED)
611 disassemble_info *info = (disassemble_info *) dis_info;
613 value = ((value << 1) & 0x1FFFF);
614 (*info->fprintf_func) (info->stream, "$%05lx", value);
618 print_decimal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
621 unsigned int attrs ATTRIBUTE_UNUSED,
622 bfd_vma pc ATTRIBUTE_UNUSED,
623 int length ATTRIBUTE_UNUSED)
625 disassemble_info *info = (disassemble_info *) dis_info;
627 (*info->fprintf_func) (info->stream, "%ld", value);