1 /* IP2K opcode support. -*- C -*-
2 Copyright (C) 2000 Red Hat, Inc.
3 Copyright (C) 2002, 2005 Free Software Foundation, Inc.
4 This file is part of CGEN. */
7 Each section is delimited with start and end markers.
9 <arch>-opc.h additions use: "-- opc.h"
10 <arch>-opc.c additions use: "-- opc.c"
11 <arch>-asm.c additions use: "-- asm.c"
12 <arch>-dis.c additions use: "-- dis.c"
13 <arch>-ibd.h additions use: "-- ibd.h". */
17 /* Check applicability of instructions against machines. */
18 #define CGEN_VALIDATE_INSN_SUPPORTED
20 /* Allows reason codes to be output when assembler errors occur. */
21 #define CGEN_VERBOSE_ASSEMBLER_ERRORS
23 /* Override disassembly hashing - there are variable bits in the top
24 byte of these instructions. */
25 #define CGEN_DIS_HASH_SIZE 8
26 #define CGEN_DIS_HASH(buf, value) \
27 (((* (unsigned char*) (buf)) >> 5) % CGEN_DIS_HASH_SIZE)
29 #define CGEN_ASM_HASH_SIZE 127
30 #define CGEN_ASM_HASH(insn) ip2k_asm_hash (insn)
32 extern unsigned int ip2k_asm_hash (const char *);
33 extern int ip2k_cgen_insn_supported (CGEN_CPU_DESC, const CGEN_INSN *);
37 #include "safe-ctype.h"
39 /* A better hash function for instruction mnemonics. */
41 ip2k_asm_hash (const char* insn)
46 for (hash = 0; *m && ! ISSPACE (*m); m++)
47 hash = (hash * 23) ^ (0x1F & TOLOWER (*m));
49 /* printf ("%s %d\n", insn, (hash % CGEN_ASM_HASH_SIZE)); */
51 return hash % CGEN_ASM_HASH_SIZE;
55 /* Special check to ensure that instruction exists for given machine. */
58 ip2k_cgen_insn_supported (CGEN_CPU_DESC cd, const CGEN_INSN *insn)
60 int machs = CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_MACH);
62 /* No mach attribute? Assume it's supported for all machs. */
66 return (machs & cd->machs) != 0;
73 parse_fr (CGEN_CPU_DESC cd,
76 unsigned long *valuep)
81 enum cgen_parse_operand_result result_type;
83 extern CGEN_KEYWORD ip2k_cgen_opval_register_names;
89 /* Check here to see if you're about to try parsing a w as the first arg
90 and return an error if you are. */
91 if ((strncmp (*strp, "w", 1) == 0) || (strncmp (*strp, "W", 1) == 0))
95 if ((strncmp (*strp, ",", 1) == 0) || ISSPACE (**strp))
97 /* We've been passed a w. Return with an error message so that
98 cgen will try the next parsing option. */
99 errmsg = _("W keyword invalid in FR operand slot.");
105 /* Attempt parse as register keyword. */
106 errmsg = cgen_parse_keyword (cd, strp, & ip2k_cgen_opval_register_names,
112 /* Attempt to parse for "(IP)". */
113 afteroffset = strstr (*strp, "(IP)");
115 if (afteroffset == NULL)
116 /* Make sure it's not in lower case. */
117 afteroffset = strstr (*strp, "(ip)");
119 if (afteroffset != NULL)
121 if (afteroffset != *strp)
123 /* Invalid offset present. */
124 errmsg = _("offset(IP) is not a valid form");
136 /* Attempt to parse for DP. ex: mov w, offset(DP)
139 /* Try parsing it as an address and see what comes back. */
140 afteroffset = strstr (*strp, "(DP)");
142 if (afteroffset == NULL)
143 /* Maybe it's in lower case. */
144 afteroffset = strstr (*strp, "(dp)");
146 if (afteroffset != NULL)
148 if (afteroffset == *strp)
150 /* No offset present. Use 0 by default. */
155 errmsg = cgen_parse_address (cd, strp, opindex,
156 BFD_RELOC_IP2K_FR_OFFSET,
157 & result_type, & tempvalue);
161 if (tempvalue <= 127)
163 /* Value is ok. Fix up the first 2 bits and return. */
164 *valuep = 0x0100 | tempvalue;
165 *strp += 4; /* Skip over the (DP) in *strp. */
170 /* Found something there in front of (DP) but it's out
172 errmsg = _("(DP) offset out of range.");
179 /* Attempt to parse for SP. ex: mov w, offset(SP)
180 mov offset(SP), w. */
181 afteroffset = strstr (*strp, "(SP)");
183 if (afteroffset == NULL)
184 /* Maybe it's in lower case. */
185 afteroffset = strstr (*strp, "(sp)");
187 if (afteroffset != NULL)
189 if (afteroffset == *strp)
191 /* No offset present. Use 0 by default. */
196 errmsg = cgen_parse_address (cd, strp, opindex,
197 BFD_RELOC_IP2K_FR_OFFSET,
198 & result_type, & tempvalue);
202 if (tempvalue <= 127)
204 /* Value is ok. Fix up the first 2 bits and return. */
205 *valuep = 0x0180 | tempvalue;
206 *strp += 4; /* Skip over the (SP) in *strp. */
211 /* Found something there in front of (SP) but it's out
213 errmsg = _("(SP) offset out of range.");
219 /* Attempt to parse as an address. */
221 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IP2K_FR9,
222 & result_type, & value);
227 /* If a parenthesis is found, warn about invalid form. */
229 errmsg = _("illegal use of parentheses");
231 /* If a numeric value is specified, ensure that it is between
233 else if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
235 if (value < 0x1 || value > 0xff)
236 errmsg = _("operand out of range (not between 1 and 255)");
243 parse_addr16 (CGEN_CPU_DESC cd,
246 unsigned long *valuep)
249 enum cgen_parse_operand_result result_type;
250 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
253 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16H)
254 code = BFD_RELOC_IP2K_HI8DATA;
255 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16L)
256 code = BFD_RELOC_IP2K_LO8DATA;
259 /* Something is very wrong. opindex has to be one of the above. */
260 errmsg = _("parse_addr16: invalid opindex.");
264 errmsg = cgen_parse_address (cd, strp, opindex, code,
265 & result_type, & value);
268 /* We either have a relocation or a number now. */
269 if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
271 /* We got a number back. */
272 if (code == BFD_RELOC_IP2K_HI8DATA)
275 /* code = BFD_RELOC_IP2K_LOW8DATA. */
285 parse_addr16_cjp (CGEN_CPU_DESC cd,
288 unsigned long *valuep)
291 enum cgen_parse_operand_result result_type;
292 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
295 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP)
296 code = BFD_RELOC_IP2K_ADDR16CJP;
297 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P)
298 code = BFD_RELOC_IP2K_PAGE3;
300 errmsg = cgen_parse_address (cd, strp, opindex, code,
301 & result_type, & value);
304 if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
306 if ((value & 0x1) == 0) /* If the address is even .... */
308 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP)
309 *valuep = (value >> 1) & 0x1FFF; /* Should mask be 1FFF? */
310 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P)
311 *valuep = (value >> 14) & 0x7;
314 errmsg = _("Byte address required. - must be even.");
316 else if (result_type == CGEN_PARSE_OPERAND_RESULT_QUEUED)
318 /* This will happen for things like (s2-s1) where s2 and s1
323 errmsg = _("cgen_parse_address returned a symbol. Literal required.");
329 parse_lit8 (CGEN_CPU_DESC cd,
335 enum cgen_parse_operand_result result_type;
336 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
339 /* Parse %OP relocating operators. */
340 if (strncmp (*strp, "%bank", 5) == 0)
343 code = BFD_RELOC_IP2K_BANK;
345 else if (strncmp (*strp, "%lo8data", 8) == 0)
348 code = BFD_RELOC_IP2K_LO8DATA;
350 else if (strncmp (*strp, "%hi8data", 8) == 0)
353 code = BFD_RELOC_IP2K_HI8DATA;
355 else if (strncmp (*strp, "%ex8data", 8) == 0)
358 code = BFD_RELOC_IP2K_EX8DATA;
360 else if (strncmp (*strp, "%lo8insn", 8) == 0)
363 code = BFD_RELOC_IP2K_LO8INSN;
365 else if (strncmp (*strp, "%hi8insn", 8) == 0)
368 code = BFD_RELOC_IP2K_HI8INSN;
371 /* Parse %op operand. */
372 if (code != BFD_RELOC_NONE)
374 errmsg = cgen_parse_address (cd, strp, opindex, code,
375 & result_type, & value);
376 if ((errmsg == NULL) &&
377 (result_type != CGEN_PARSE_OPERAND_RESULT_QUEUED))
378 errmsg = _("percent-operator operand is not a symbol");
382 /* Parse as a number. */
385 errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
387 /* Truncate to eight bits to accept both signed and unsigned input. */
396 parse_bit3 (CGEN_CPU_DESC cd,
399 unsigned long *valuep)
406 if (strncmp (*strp, "%bit", 4) == 0)
411 else if (strncmp (*strp, "%msbbit", 7) == 0)
416 else if (strncmp (*strp, "%lsbbit", 7) == 0)
422 errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
431 errmsg = _("Attempt to find bit index of 0");
438 while ((value & 0x80000000) == 0)
447 while ((value & 0x00000001) == 0)
463 print_fr (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
466 unsigned int attrs ATTRIBUTE_UNUSED,
467 bfd_vma pc ATTRIBUTE_UNUSED,
468 int length ATTRIBUTE_UNUSED)
470 disassemble_info *info = (disassemble_info *) dis_info;
471 const CGEN_KEYWORD_ENTRY *ke;
472 extern CGEN_KEYWORD ip2k_cgen_opval_register_names;
476 if (value == 0) /* This is (IP). */
478 (*info->fprintf_func) (info->stream, "%s", "(IP)");
482 offsettest = value >> 7;
483 offsetvalue = value & 0x7F;
485 /* Check to see if first two bits are 10 -> (DP). */
488 if (offsetvalue == 0)
489 (*info->fprintf_func) (info->stream, "%s","(DP)");
491 (*info->fprintf_func) (info->stream, "$%lx%s", offsetvalue, "(DP)");
495 /* Check to see if first two bits are 11 -> (SP). */
498 if (offsetvalue == 0)
499 (*info->fprintf_func) (info->stream, "%s", "(SP)");
501 (*info->fprintf_func) (info->stream, "$%lx%s", offsetvalue,"(SP)");
505 /* Attempt to print as a register keyword. */
506 ke = cgen_keyword_lookup_value (& ip2k_cgen_opval_register_names, value);
509 (*info->fprintf_func) (info->stream, "%s", ke->name);
511 /* Print as an address literal. */
512 (*info->fprintf_func) (info->stream, "$%02lx", value);
516 print_dollarhex (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
519 unsigned int attrs ATTRIBUTE_UNUSED,
520 bfd_vma pc ATTRIBUTE_UNUSED,
521 int length ATTRIBUTE_UNUSED)
523 disassemble_info *info = (disassemble_info *) dis_info;
525 (*info->fprintf_func) (info->stream, "$%lx", value);
529 print_dollarhex8 (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
532 unsigned int attrs ATTRIBUTE_UNUSED,
533 bfd_vma pc ATTRIBUTE_UNUSED,
534 int length ATTRIBUTE_UNUSED)
536 disassemble_info *info = (disassemble_info *) dis_info;
538 (*info->fprintf_func) (info->stream, "$%02lx", value);
542 print_dollarhex_addr16h (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
545 unsigned int attrs ATTRIBUTE_UNUSED,
546 bfd_vma pc ATTRIBUTE_UNUSED,
547 int length ATTRIBUTE_UNUSED)
549 disassemble_info *info = (disassemble_info *) dis_info;
551 /* This is a loadh instruction. Shift the value to the left
552 by 8 bits so that disassembled code will reassemble properly. */
553 value = ((value << 8) & 0xFF00);
555 (*info->fprintf_func) (info->stream, "$%04lx", value);
559 print_dollarhex_addr16l (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 (*info->fprintf_func) (info->stream, "$%04lx", value);
572 print_dollarhex_p (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
575 unsigned int attrs ATTRIBUTE_UNUSED,
576 bfd_vma pc ATTRIBUTE_UNUSED,
577 int length ATTRIBUTE_UNUSED)
579 disassemble_info *info = (disassemble_info *) dis_info;
581 value = ((value << 14) & 0x1C000);
582 ;value = (value & 0x1FFFF);
583 (*info->fprintf_func) (info->stream, "$%05lx", value);
587 print_dollarhex_cj (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
590 unsigned int attrs ATTRIBUTE_UNUSED,
591 bfd_vma pc ATTRIBUTE_UNUSED,
592 int length ATTRIBUTE_UNUSED)
594 disassemble_info *info = (disassemble_info *) dis_info;
596 value = ((value << 1) & 0x1FFFF);
597 (*info->fprintf_func) (info->stream, "$%05lx", value);
601 print_decimal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
604 unsigned int attrs ATTRIBUTE_UNUSED,
605 bfd_vma pc ATTRIBUTE_UNUSED,
606 int length ATTRIBUTE_UNUSED)
608 disassemble_info *info = (disassemble_info *) dis_info;
610 (*info->fprintf_func) (info->stream, "%ld", value);