* coffgen.c (bfd_coff_get_comdat_section): Check that
[binutils.git] / opcodes / ip2k-asm.c
bloba0204d0323358772123ae3584c3dc067d7976ebf
1 /* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
4 THIS FILE IS MACHINE GENERATED WITH CGEN.
5 - the resultant file is machine generated, cgen-asm.in isn't
7 Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
9 This file is part of the GNU Binutils and GDB, the GNU debugger.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software Foundation, Inc.,
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 /* ??? Eventually more and more of this stuff can go to cpu-independent files.
26 Keep that in mind. */
28 #include "sysdep.h"
29 #include <stdio.h>
30 #include "ansidecl.h"
31 #include "bfd.h"
32 #include "symcat.h"
33 #include "ip2k-desc.h"
34 #include "ip2k-opc.h"
35 #include "opintl.h"
36 #include "xregex.h"
37 #include "libiberty.h"
38 #include "safe-ctype.h"
40 #undef min
41 #define min(a,b) ((a) < (b) ? (a) : (b))
42 #undef max
43 #define max(a,b) ((a) > (b) ? (a) : (b))
45 static const char * parse_insn_normal
46 (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
48 /* -- assembler routines inserted here. */
50 /* -- asm.c */
52 #define PARSE_FUNC_DECL(name) \
53 static const char *name PARAMS ((CGEN_CPU_DESC, const char **, int, long *))
55 PARSE_FUNC_DECL (parse_fr);
56 PARSE_FUNC_DECL (parse_addr16);
57 PARSE_FUNC_DECL (parse_addr16_cjp);
58 PARSE_FUNC_DECL (parse_lit8);
59 PARSE_FUNC_DECL (parse_bit3);
62 static const char *
63 parse_fr (cd, strp, opindex, valuep)
64 CGEN_CPU_DESC cd;
65 const char **strp;
66 int opindex;
67 long *valuep;
69 const char *errmsg;
70 const char *old_strp;
71 char *afteroffset;
72 enum cgen_parse_operand_result result_type;
73 bfd_vma value;
74 extern CGEN_KEYWORD ip2k_cgen_opval_register_names;
75 bfd_vma tempvalue;
77 old_strp = *strp;
78 afteroffset = NULL;
81 /* Check here to see if you're about to try parsing a w as the first arg */
82 /* and return an error if you are. */
83 if ( (strncmp(*strp,"w",1)==0) || (strncmp(*strp,"W",1)==0) )
85 (*strp)++;
87 if ( (strncmp(*strp,",",1)==0) || ISSPACE(**strp) )
89 /* We've been passed a w. Return with an error message so that */
90 /* cgen will try the next parsing option. */
91 errmsg = _("W keyword invalid in FR operand slot.");
92 return errmsg;
94 *strp = old_strp;
98 /* Attempt parse as register keyword. */
99 /* old_strp = *strp; */
101 errmsg = cgen_parse_keyword (cd, strp, & ip2k_cgen_opval_register_names,
102 valuep);
103 if ( *strp != NULL )
104 if (errmsg == NULL)
105 return errmsg;
107 /* Attempt to parse for "(IP)" */
108 afteroffset = strstr(*strp,"(IP)");
110 if ( afteroffset == NULL)
112 /* Make sure it's not in lower case */
113 afteroffset = strstr(*strp,"(ip)");
116 if ( afteroffset != NULL )
118 if ( afteroffset != *strp )
120 /* Invalid offset present.*/
121 errmsg = _("offset(IP) is not a valid form");
122 return errmsg;
124 else
126 *strp += 4;
127 *valuep = 0;
128 errmsg = NULL;
129 return errmsg;
133 /* Attempt to parse for DP. ex: mov w, offset(DP) */
134 /* mov offset(DP),w */
136 /* Try parsing it as an address and see what comes back */
138 afteroffset = strstr(*strp,"(DP)");
140 if ( afteroffset == NULL)
142 /* Maybe it's in lower case */
143 afteroffset = strstr(*strp,"(dp)");
146 if ( afteroffset != NULL )
148 if ( afteroffset == *strp )
150 /* No offset present. Use 0 by default. */
151 tempvalue = 0;
152 errmsg = NULL;
154 else
156 errmsg = cgen_parse_address (cd, strp, opindex,
157 BFD_RELOC_IP2K_FR_OFFSET,
158 & result_type, & tempvalue);
161 if (errmsg == NULL)
163 if (tempvalue <= 127)
165 /* Value is ok. Fix up the first 2 bits and return */
166 *valuep = 0x0100 | tempvalue;
167 *strp += 4; /* skip over the (DP) in *strp */
168 return errmsg;
170 else
172 /* Found something there in front of (DP) but it's out
173 of range. */
174 errmsg = _("(DP) offset out of range.");
175 return errmsg;
181 /* Attempt to parse for SP. ex: mov w, offset(SP) */
182 /* mov offset(SP), w */
185 afteroffset = strstr(*strp,"(SP)");
187 if (afteroffset == NULL)
189 /* Maybe it's in lower case. */
190 afteroffset = strstr(*strp, "(sp)");
193 if ( afteroffset != NULL )
195 if ( afteroffset == *strp )
197 /* No offset present. Use 0 by default. */
198 tempvalue = 0;
199 errmsg = NULL;
201 else
203 errmsg = cgen_parse_address (cd, strp, opindex,
204 BFD_RELOC_IP2K_FR_OFFSET,
205 & result_type, & tempvalue);
207 if (errmsg == NULL)
209 if (tempvalue <= 127)
211 /* Value is ok. Fix up the first 2 bits and return */
212 *valuep = 0x0180 | tempvalue;
213 *strp += 4; /* skip over the (SP) in *strp */
214 return errmsg;
216 else
218 /* Found something there in front of (SP) but it's out
219 of range. */
220 errmsg = _("(SP) offset out of range.");
221 return errmsg;
228 /* Attempt to parse as an address. */
229 *strp = old_strp;
230 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IP2K_FR9,
231 & result_type, & value);
232 if (errmsg == NULL)
234 *valuep = value;
236 /* if a parenthesis is found, warn about invalid form */
238 if (**strp == '(')
240 errmsg = _("illegal use of parentheses");
242 /* if a numeric value is specified, ensure that it is between
243 1 and 255 */
244 else if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
246 if (value < 0x1 || value > 0xff)
247 errmsg = _("operand out of range (not between 1 and 255)");
250 return errmsg;
253 static const char *
254 parse_addr16 (cd, strp, opindex, valuep)
255 CGEN_CPU_DESC cd;
256 const char **strp;
257 int opindex;
258 long *valuep;
260 const char *errmsg;
261 enum cgen_parse_operand_result result_type;
262 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
263 bfd_vma value;
265 if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16H )
266 code = BFD_RELOC_IP2K_HI8DATA;
267 else if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16L )
268 code = BFD_RELOC_IP2K_LO8DATA;
269 else
271 /* Something is very wrong. opindex has to be one of the above. */
272 errmsg = _("parse_addr16: invalid opindex.");
273 return errmsg;
276 errmsg = cgen_parse_address (cd, strp, opindex, code,
277 & result_type, & value);
278 if (errmsg == NULL)
280 /* We either have a relocation or a number now. */
281 if ( result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER )
283 /* We got a number back. */
284 if ( code == BFD_RELOC_IP2K_HI8DATA )
285 value >>= 8;
286 else /* code = BFD_RELOC_IP2K_LOW8DATA */
287 value &= 0x00FF;
289 *valuep = value;
292 return errmsg;
296 static const char *
297 parse_addr16_cjp (cd, strp, opindex, valuep)
298 CGEN_CPU_DESC cd;
299 const char **strp;
300 int opindex;
301 long *valuep;
303 const char *errmsg;
304 enum cgen_parse_operand_result result_type;
305 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
306 bfd_vma value;
308 if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16CJP )
309 code = BFD_RELOC_IP2K_ADDR16CJP;
310 else if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16P )
311 code = BFD_RELOC_IP2K_PAGE3;
313 errmsg = cgen_parse_address (cd, strp, opindex, code,
314 & result_type, & value);
315 if (errmsg == NULL)
317 if ( result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER )
319 if ( (value & 0x1) == 0) /* If the address is even .... */
321 if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16CJP )
322 *valuep = (value >> 1) & 0x1FFF; /* Should mask be 1FFF? */
323 else if ( opindex == (CGEN_OPERAND_TYPE)IP2K_OPERAND_ADDR16P )
324 *valuep = (value >> 14) & 0x7;
326 else
327 errmsg = _("Byte address required. - must be even.");
329 else if ( result_type == CGEN_PARSE_OPERAND_RESULT_QUEUED )
331 /* This will happen for things like (s2-s1) where s2 and s1
332 are labels. */
333 *valuep = value;
335 else
336 errmsg = _("cgen_parse_address returned a symbol. Literal required.");
338 return errmsg;
342 static const char *
343 parse_lit8 (cd, strp, opindex, valuep)
344 CGEN_CPU_DESC cd;
345 const char **strp;
346 int opindex;
347 long *valuep;
349 const char *errmsg;
350 enum cgen_parse_operand_result result_type;
351 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
352 bfd_vma value;
354 /* Parse %OP relocating operators. */
355 if (strncmp (*strp, "%bank", 5) == 0)
357 *strp += 5;
358 code = BFD_RELOC_IP2K_BANK;
360 else if (strncmp (*strp, "%lo8data", 8) == 0)
362 *strp += 8;
363 code = BFD_RELOC_IP2K_LO8DATA;
365 else if (strncmp (*strp, "%hi8data", 8) == 0)
367 *strp += 8;
368 code = BFD_RELOC_IP2K_HI8DATA;
370 else if (strncmp (*strp, "%ex8data", 8) == 0)
372 *strp += 8;
373 code = BFD_RELOC_IP2K_EX8DATA;
375 else if (strncmp (*strp, "%lo8insn", 8) == 0)
377 *strp += 8;
378 code = BFD_RELOC_IP2K_LO8INSN;
380 else if (strncmp (*strp, "%hi8insn", 8) == 0)
382 *strp += 8;
383 code = BFD_RELOC_IP2K_HI8INSN;
387 /* Parse %op operand. */
388 if (code != BFD_RELOC_NONE)
390 errmsg = cgen_parse_address (cd, strp, opindex, code,
391 & result_type, & value);
392 if ((errmsg == NULL) &&
393 (result_type != CGEN_PARSE_OPERAND_RESULT_QUEUED))
394 errmsg = _("%operator operand is not a symbol");
396 *valuep = value;
398 /* Parse as a number. */
399 else
401 errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
403 /* Truncate to eight bits to accept both signed and unsigned input. */
404 if (errmsg == NULL)
405 *valuep &= 0xFF;
408 return errmsg;
411 static const char *
412 parse_bit3 (cd, strp, opindex, valuep)
413 CGEN_CPU_DESC cd;
414 const char **strp;
415 int opindex;
416 long *valuep;
418 const char *errmsg;
419 char mode = 0;
420 long count = 0;
421 unsigned long value;
423 if (strncmp (*strp, "%bit", 4) == 0)
425 *strp += 4;
426 mode = 1;
428 else if (strncmp (*strp, "%msbbit", 7) == 0)
430 *strp += 7;
431 mode = 1;
433 else if (strncmp (*strp, "%lsbbit", 7) == 0)
435 *strp += 7;
436 mode = 2;
439 errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
440 if (errmsg)
441 return errmsg;
443 if (mode)
445 value = (unsigned long) *valuep;
446 if (value == 0)
448 errmsg = _("Attempt to find bit index of 0");
449 return errmsg;
452 if (mode == 1)
454 count = 31;
455 while ((value & 0x80000000) == 0)
457 count--;
458 value <<= 1;
461 else if (mode == 2)
463 count = 0;
464 while ((value & 0x00000001) == 0)
466 count++;
467 value >>= 1;
471 *valuep = count;
474 return errmsg;
478 /* -- dis.c */
480 const char * ip2k_cgen_parse_operand
481 PARAMS ((CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *));
483 /* Main entry point for operand parsing.
485 This function is basically just a big switch statement. Earlier versions
486 used tables to look up the function to use, but
487 - if the table contains both assembler and disassembler functions then
488 the disassembler contains much of the assembler and vice-versa,
489 - there's a lot of inlining possibilities as things grow,
490 - using a switch statement avoids the function call overhead.
492 This function could be moved into `parse_insn_normal', but keeping it
493 separate makes clear the interface between `parse_insn_normal' and each of
494 the handlers. */
496 const char *
497 ip2k_cgen_parse_operand (cd, opindex, strp, fields)
498 CGEN_CPU_DESC cd;
499 int opindex;
500 const char ** strp;
501 CGEN_FIELDS * fields;
503 const char * errmsg = NULL;
504 /* Used by scalar operands that still need to be parsed. */
505 long junk ATTRIBUTE_UNUSED;
507 switch (opindex)
509 case IP2K_OPERAND_ADDR16CJP :
510 errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16CJP, &fields->f_addr16cjp);
511 break;
512 case IP2K_OPERAND_ADDR16H :
513 errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16H, &fields->f_imm8);
514 break;
515 case IP2K_OPERAND_ADDR16L :
516 errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16L, &fields->f_imm8);
517 break;
518 case IP2K_OPERAND_ADDR16P :
519 errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16P, &fields->f_page3);
520 break;
521 case IP2K_OPERAND_BITNO :
522 errmsg = parse_bit3 (cd, strp, IP2K_OPERAND_BITNO, &fields->f_bitno);
523 break;
524 case IP2K_OPERAND_CBIT :
525 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_CBIT, &junk);
526 break;
527 case IP2K_OPERAND_DCBIT :
528 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_DCBIT, &junk);
529 break;
530 case IP2K_OPERAND_FR :
531 errmsg = parse_fr (cd, strp, IP2K_OPERAND_FR, &fields->f_reg);
532 break;
533 case IP2K_OPERAND_LIT8 :
534 errmsg = parse_lit8 (cd, strp, IP2K_OPERAND_LIT8, &fields->f_imm8);
535 break;
536 case IP2K_OPERAND_PABITS :
537 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_PABITS, &junk);
538 break;
539 case IP2K_OPERAND_RETI3 :
540 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_RETI3, &fields->f_reti3);
541 break;
542 case IP2K_OPERAND_ZBIT :
543 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_ZBIT, &junk);
544 break;
546 default :
547 /* xgettext:c-format */
548 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
549 abort ();
552 return errmsg;
555 cgen_parse_fn * const ip2k_cgen_parse_handlers[] =
557 parse_insn_normal,
560 void
561 ip2k_cgen_init_asm (cd)
562 CGEN_CPU_DESC cd;
564 ip2k_cgen_init_opcode_table (cd);
565 ip2k_cgen_init_ibld_table (cd);
566 cd->parse_handlers = & ip2k_cgen_parse_handlers[0];
567 cd->parse_operand = ip2k_cgen_parse_operand;
572 /* Regex construction routine.
574 This translates an opcode syntax string into a regex string,
575 by replacing any non-character syntax element (such as an
576 opcode) with the pattern '.*'
578 It then compiles the regex and stores it in the opcode, for
579 later use by ip2k_cgen_assemble_insn
581 Returns NULL for success, an error message for failure. */
583 char *
584 ip2k_cgen_build_insn_regex (CGEN_INSN *insn)
586 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
587 const char *mnem = CGEN_INSN_MNEMONIC (insn);
588 char rxbuf[CGEN_MAX_RX_ELEMENTS];
589 char *rx = rxbuf;
590 const CGEN_SYNTAX_CHAR_TYPE *syn;
591 int reg_err;
593 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
595 /* Mnemonics come first in the syntax string. */
596 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
597 return _("missing mnemonic in syntax string");
598 ++syn;
600 /* Generate a case sensitive regular expression that emulates case
601 insensitive matching in the "C" locale. We cannot generate a case
602 insensitive regular expression because in Turkish locales, 'i' and 'I'
603 are not equal modulo case conversion. */
605 /* Copy the literal mnemonic out of the insn. */
606 for (; *mnem; mnem++)
608 char c = *mnem;
610 if (ISALPHA (c))
612 *rx++ = '[';
613 *rx++ = TOLOWER (c);
614 *rx++ = TOUPPER (c);
615 *rx++ = ']';
617 else
618 *rx++ = c;
621 /* Copy any remaining literals from the syntax string into the rx. */
622 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
624 if (CGEN_SYNTAX_CHAR_P (* syn))
626 char c = CGEN_SYNTAX_CHAR (* syn);
628 switch (c)
630 /* Escape any regex metacharacters in the syntax. */
631 case '.': case '[': case '\\':
632 case '*': case '^': case '$':
634 #ifdef CGEN_ESCAPE_EXTENDED_REGEX
635 case '?': case '{': case '}':
636 case '(': case ')': case '*':
637 case '|': case '+': case ']':
638 #endif
639 *rx++ = '\\';
640 *rx++ = c;
641 break;
643 default:
644 if (ISALPHA (c))
646 *rx++ = '[';
647 *rx++ = TOLOWER (c);
648 *rx++ = TOUPPER (c);
649 *rx++ = ']';
651 else
652 *rx++ = c;
653 break;
656 else
658 /* Replace non-syntax fields with globs. */
659 *rx++ = '.';
660 *rx++ = '*';
664 /* Trailing whitespace ok. */
665 * rx++ = '[';
666 * rx++ = ' ';
667 * rx++ = '\t';
668 * rx++ = ']';
669 * rx++ = '*';
671 /* But anchor it after that. */
672 * rx++ = '$';
673 * rx = '\0';
675 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
676 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
678 if (reg_err == 0)
679 return NULL;
680 else
682 static char msg[80];
684 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
685 regfree ((regex_t *) CGEN_INSN_RX (insn));
686 free (CGEN_INSN_RX (insn));
687 (CGEN_INSN_RX (insn)) = NULL;
688 return msg;
693 /* Default insn parser.
695 The syntax string is scanned and operands are parsed and stored in FIELDS.
696 Relocs are queued as we go via other callbacks.
698 ??? Note that this is currently an all-or-nothing parser. If we fail to
699 parse the instruction, we return 0 and the caller will start over from
700 the beginning. Backtracking will be necessary in parsing subexpressions,
701 but that can be handled there. Not handling backtracking here may get
702 expensive in the case of the m68k. Deal with later.
704 Returns NULL for success, an error message for failure. */
706 static const char *
707 parse_insn_normal (CGEN_CPU_DESC cd,
708 const CGEN_INSN *insn,
709 const char **strp,
710 CGEN_FIELDS *fields)
712 /* ??? Runtime added insns not handled yet. */
713 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
714 const char *str = *strp;
715 const char *errmsg;
716 const char *p;
717 const CGEN_SYNTAX_CHAR_TYPE * syn;
718 #ifdef CGEN_MNEMONIC_OPERANDS
719 /* FIXME: wip */
720 int past_opcode_p;
721 #endif
723 /* For now we assume the mnemonic is first (there are no leading operands).
724 We can parse it without needing to set up operand parsing.
725 GAS's input scrubber will ensure mnemonics are lowercase, but we may
726 not be called from GAS. */
727 p = CGEN_INSN_MNEMONIC (insn);
728 while (*p && TOLOWER (*p) == TOLOWER (*str))
729 ++p, ++str;
731 if (* p)
732 return _("unrecognized instruction");
734 #ifndef CGEN_MNEMONIC_OPERANDS
735 if (* str && ! ISSPACE (* str))
736 return _("unrecognized instruction");
737 #endif
739 CGEN_INIT_PARSE (cd);
740 cgen_init_parse_operand (cd);
741 #ifdef CGEN_MNEMONIC_OPERANDS
742 past_opcode_p = 0;
743 #endif
745 /* We don't check for (*str != '\0') here because we want to parse
746 any trailing fake arguments in the syntax string. */
747 syn = CGEN_SYNTAX_STRING (syntax);
749 /* Mnemonics come first for now, ensure valid string. */
750 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
751 abort ();
753 ++syn;
755 while (* syn != 0)
757 /* Non operand chars must match exactly. */
758 if (CGEN_SYNTAX_CHAR_P (* syn))
760 /* FIXME: While we allow for non-GAS callers above, we assume the
761 first char after the mnemonic part is a space. */
762 /* FIXME: We also take inappropriate advantage of the fact that
763 GAS's input scrubber will remove extraneous blanks. */
764 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
766 #ifdef CGEN_MNEMONIC_OPERANDS
767 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
768 past_opcode_p = 1;
769 #endif
770 ++ syn;
771 ++ str;
773 else if (*str)
775 /* Syntax char didn't match. Can't be this insn. */
776 static char msg [80];
778 /* xgettext:c-format */
779 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
780 CGEN_SYNTAX_CHAR(*syn), *str);
781 return msg;
783 else
785 /* Ran out of input. */
786 static char msg [80];
788 /* xgettext:c-format */
789 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
790 CGEN_SYNTAX_CHAR(*syn));
791 return msg;
793 continue;
796 /* We have an operand of some sort. */
797 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
798 &str, fields);
799 if (errmsg)
800 return errmsg;
802 /* Done with this operand, continue with next one. */
803 ++ syn;
806 /* If we're at the end of the syntax string, we're done. */
807 if (* syn == 0)
809 /* FIXME: For the moment we assume a valid `str' can only contain
810 blanks now. IE: We needn't try again with a longer version of
811 the insn and it is assumed that longer versions of insns appear
812 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
813 while (ISSPACE (* str))
814 ++ str;
816 if (* str != '\0')
817 return _("junk at end of line"); /* FIXME: would like to include `str' */
819 return NULL;
822 /* We couldn't parse it. */
823 return _("unrecognized instruction");
826 /* Main entry point.
827 This routine is called for each instruction to be assembled.
828 STR points to the insn to be assembled.
829 We assume all necessary tables have been initialized.
830 The assembled instruction, less any fixups, is stored in BUF.
831 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
832 still needs to be converted to target byte order, otherwise BUF is an array
833 of bytes in target byte order.
834 The result is a pointer to the insn's entry in the opcode table,
835 or NULL if an error occured (an error message will have already been
836 printed).
838 Note that when processing (non-alias) macro-insns,
839 this function recurses.
841 ??? It's possible to make this cpu-independent.
842 One would have to deal with a few minor things.
843 At this point in time doing so would be more of a curiosity than useful
844 [for example this file isn't _that_ big], but keeping the possibility in
845 mind helps keep the design clean. */
847 const CGEN_INSN *
848 ip2k_cgen_assemble_insn (CGEN_CPU_DESC cd,
849 const char *str,
850 CGEN_FIELDS *fields,
851 CGEN_INSN_BYTES_PTR buf,
852 char **errmsg)
854 const char *start;
855 CGEN_INSN_LIST *ilist;
856 const char *parse_errmsg = NULL;
857 const char *insert_errmsg = NULL;
858 int recognized_mnemonic = 0;
860 /* Skip leading white space. */
861 while (ISSPACE (* str))
862 ++ str;
864 /* The instructions are stored in hashed lists.
865 Get the first in the list. */
866 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
868 /* Keep looking until we find a match. */
869 start = str;
870 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
872 const CGEN_INSN *insn = ilist->insn;
873 recognized_mnemonic = 1;
875 #ifdef CGEN_VALIDATE_INSN_SUPPORTED
876 /* Not usually needed as unsupported opcodes
877 shouldn't be in the hash lists. */
878 /* Is this insn supported by the selected cpu? */
879 if (! ip2k_cgen_insn_supported (cd, insn))
880 continue;
881 #endif
882 /* If the RELAXED attribute is set, this is an insn that shouldn't be
883 chosen immediately. Instead, it is used during assembler/linker
884 relaxation if possible. */
885 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
886 continue;
888 str = start;
890 /* Skip this insn if str doesn't look right lexically. */
891 if (CGEN_INSN_RX (insn) != NULL &&
892 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
893 continue;
895 /* Allow parse/insert handlers to obtain length of insn. */
896 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
898 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
899 if (parse_errmsg != NULL)
900 continue;
902 /* ??? 0 is passed for `pc'. */
903 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
904 (bfd_vma) 0);
905 if (insert_errmsg != NULL)
906 continue;
908 /* It is up to the caller to actually output the insn and any
909 queued relocs. */
910 return insn;
914 static char errbuf[150];
915 #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
916 const char *tmp_errmsg;
918 /* If requesting verbose error messages, use insert_errmsg.
919 Failing that, use parse_errmsg. */
920 tmp_errmsg = (insert_errmsg ? insert_errmsg :
921 parse_errmsg ? parse_errmsg :
922 recognized_mnemonic ?
923 _("unrecognized form of instruction") :
924 _("unrecognized instruction"));
926 if (strlen (start) > 50)
927 /* xgettext:c-format */
928 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
929 else
930 /* xgettext:c-format */
931 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
932 #else
933 if (strlen (start) > 50)
934 /* xgettext:c-format */
935 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
936 else
937 /* xgettext:c-format */
938 sprintf (errbuf, _("bad instruction `%.50s'"), start);
939 #endif
941 *errmsg = errbuf;
942 return NULL;
946 #if 0 /* This calls back to GAS which we can't do without care. */
948 /* Record each member of OPVALS in the assembler's symbol table.
949 This lets GAS parse registers for us.
950 ??? Interesting idea but not currently used. */
952 /* Record each member of OPVALS in the assembler's symbol table.
953 FIXME: Not currently used. */
955 void
956 ip2k_cgen_asm_hash_keywords (CGEN_CPU_DESC cd, CGEN_KEYWORD *opvals)
958 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
959 const CGEN_KEYWORD_ENTRY * ke;
961 while ((ke = cgen_keyword_search_next (& search)) != NULL)
963 #if 0 /* Unnecessary, should be done in the search routine. */
964 if (! ip2k_cgen_opval_supported (ke))
965 continue;
966 #endif
967 cgen_asm_record_register (cd, ke->name, ke->value);
971 #endif /* 0 */