Update year range in copyright notice of binutils files
[deliverable/binutils-gdb.git] / opcodes / fr30-asm.c
CommitLineData
4162bb66 1/* DO NOT EDIT! -*- buffer-read-only: t -*- vi:set ro: */
252b5132
RH
2/* Assembler interface for targets using CGEN. -*- C -*-
3 CGEN: Cpu tools GENerator
4
47b0e7ad
NC
5 THIS FILE IS MACHINE GENERATED WITH CGEN.
6 - the resultant file is machine generated, cgen-asm.in isn't
252b5132 7
219d1afa 8 Copyright (C) 1996-2018 Free Software Foundation, Inc.
252b5132 9
9b201bb5 10 This file is part of libopcodes.
252b5132 11
9b201bb5 12 This library is free software; you can redistribute it and/or modify
47b0e7ad 13 it under the terms of the GNU General Public License as published by
9b201bb5 14 the Free Software Foundation; either version 3, or (at your option)
47b0e7ad 15 any later version.
252b5132 16
9b201bb5
NC
17 It is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 License for more details.
252b5132 21
47b0e7ad
NC
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132 25
9b201bb5 26
252b5132
RH
27/* ??? Eventually more and more of this stuff can go to cpu-independent files.
28 Keep that in mind. */
29
30#include "sysdep.h"
252b5132
RH
31#include <stdio.h>
32#include "ansidecl.h"
33#include "bfd.h"
34#include "symcat.h"
35#include "fr30-desc.h"
36#include "fr30-opc.h"
37#include "opintl.h"
fc7bc883 38#include "xregex.h"
d5b2f4d6 39#include "libiberty.h"
37111cc7 40#include "safe-ctype.h"
252b5132 41
37111cc7 42#undef min
252b5132 43#define min(a,b) ((a) < (b) ? (a) : (b))
37111cc7 44#undef max
252b5132
RH
45#define max(a,b) ((a) > (b) ? (a) : (b))
46
0e2ee3ca 47static const char * parse_insn_normal
ffead7ae 48 (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
252b5132 49\f
37111cc7 50/* -- assembler routines inserted here. */
252b5132
RH
51
52/* -- asm.c */
0e2ee3ca
NC
53/* Handle register lists for LDMx and STMx. */
54
252b5132 55static int
47b0e7ad 56parse_register_number (const char **strp)
252b5132
RH
57{
58 int regno;
47b0e7ad 59
252b5132 60 if (**strp < '0' || **strp > '9')
47b0e7ad 61 return -1; /* Error. */
252b5132
RH
62 regno = **strp - '0';
63 ++*strp;
64
65 if (**strp >= '0' && **strp <= '9')
66 {
67 regno = regno * 10 + (**strp - '0');
68 ++*strp;
69 }
70
71 return regno;
72}
73
74static const char *
47b0e7ad
NC
75parse_register_list (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
76 const char **strp,
77 int opindex ATTRIBUTE_UNUSED,
78 unsigned long *valuep,
79 int high_low, /* 0 == high, 1 == low. */
80 int load_store) /* 0 == load, 1 == store. */
252b5132 81{
252b5132
RH
82 *valuep = 0;
83 while (**strp && **strp != ')')
84 {
47b0e7ad
NC
85 int regno;
86
252b5132
RH
87 if (**strp != 'R' && **strp != 'r')
88 break;
89 ++*strp;
90
91 regno = parse_register_number (strp);
92 if (regno == -1)
47b0e7ad 93 return _("Register number is not valid");
252b5132 94 if (regno > 7 && !high_low)
47b0e7ad 95 return _("Register must be between r0 and r7");
252b5132 96 if (regno < 8 && high_low)
47b0e7ad 97 return _("Register must be between r8 and r15");
252b5132
RH
98
99 if (high_low)
100 regno -= 8;
101
0e2ee3ca 102 if (load_store) /* Mask is reversed for store. */
252b5132
RH
103 *valuep |= 0x80 >> regno;
104 else
105 *valuep |= 1 << regno;
106
107 if (**strp == ',')
108 {
109 if (*(*strp + 1) == ')')
110 break;
111 ++*strp;
112 }
113 }
114
115 if (!*strp || **strp != ')')
47b0e7ad 116 return _("Register list is not valid");
252b5132
RH
117
118 return NULL;
119}
120
121static const char *
47b0e7ad
NC
122parse_low_register_list_ld (CGEN_CPU_DESC cd,
123 const char **strp,
124 int opindex,
125 unsigned long *valuep)
252b5132 126{
47b0e7ad
NC
127 return parse_register_list (cd, strp, opindex, valuep,
128 0 /* Low. */, 0 /* Load. */);
252b5132
RH
129}
130
131static const char *
47b0e7ad
NC
132parse_hi_register_list_ld (CGEN_CPU_DESC cd,
133 const char **strp,
134 int opindex,
135 unsigned long *valuep)
252b5132 136{
47b0e7ad
NC
137 return parse_register_list (cd, strp, opindex, valuep,
138 1 /* High. */, 0 /* Load. */);
252b5132
RH
139}
140
141static const char *
47b0e7ad
NC
142parse_low_register_list_st (CGEN_CPU_DESC cd,
143 const char **strp,
144 int opindex,
145 unsigned long *valuep)
252b5132 146{
47b0e7ad
NC
147 return parse_register_list (cd, strp, opindex, valuep,
148 0 /* Low. */, 1 /* Store. */);
252b5132
RH
149}
150
151static const char *
47b0e7ad
NC
152parse_hi_register_list_st (CGEN_CPU_DESC cd,
153 const char **strp,
154 int opindex,
155 unsigned long *valuep)
252b5132 156{
47b0e7ad
NC
157 return parse_register_list (cd, strp, opindex, valuep,
158 1 /* High. */, 1 /* Store. */);
252b5132
RH
159}
160
161/* -- */
162
0e2ee3ca 163const char * fr30_cgen_parse_operand
47b0e7ad 164 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
0e2ee3ca 165
252b5132
RH
166/* Main entry point for operand parsing.
167
168 This function is basically just a big switch statement. Earlier versions
169 used tables to look up the function to use, but
170 - if the table contains both assembler and disassembler functions then
171 the disassembler contains much of the assembler and vice-versa,
172 - there's a lot of inlining possibilities as things grow,
173 - using a switch statement avoids the function call overhead.
174
175 This function could be moved into `parse_insn_normal', but keeping it
176 separate makes clear the interface between `parse_insn_normal' and each of
9a2e995d 177 the handlers. */
252b5132
RH
178
179const char *
47b0e7ad
NC
180fr30_cgen_parse_operand (CGEN_CPU_DESC cd,
181 int opindex,
182 const char ** strp,
183 CGEN_FIELDS * fields)
252b5132 184{
eb1b03df
DE
185 const char * errmsg = NULL;
186 /* Used by scalar operands that still need to be parsed. */
0e2ee3ca 187 long junk ATTRIBUTE_UNUSED;
252b5132
RH
188
189 switch (opindex)
190 {
191 case FR30_OPERAND_CRI :
192 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRi);
193 break;
194 case FR30_OPERAND_CRJ :
195 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_cr_names, & fields->f_CRj);
196 break;
197 case FR30_OPERAND_R13 :
eb1b03df 198 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r13, & junk);
252b5132
RH
199 break;
200 case FR30_OPERAND_R14 :
eb1b03df 201 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r14, & junk);
252b5132
RH
202 break;
203 case FR30_OPERAND_R15 :
eb1b03df 204 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_r15, & junk);
252b5132
RH
205 break;
206 case FR30_OPERAND_RI :
207 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ri);
208 break;
209 case FR30_OPERAND_RIC :
210 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Ric);
211 break;
212 case FR30_OPERAND_RJ :
213 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rj);
214 break;
215 case FR30_OPERAND_RJC :
216 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_gr_names, & fields->f_Rjc);
217 break;
218 case FR30_OPERAND_RS1 :
219 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs1);
220 break;
221 case FR30_OPERAND_RS2 :
222 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_dr_names, & fields->f_Rs2);
223 break;
224 case FR30_OPERAND_CC :
33b71eeb 225 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CC, (unsigned long *) (& fields->f_cc));
252b5132
RH
226 break;
227 case FR30_OPERAND_CCC :
33b71eeb 228 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_CCC, (unsigned long *) (& fields->f_ccc));
252b5132
RH
229 break;
230 case FR30_OPERAND_DIR10 :
33b71eeb 231 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR10, (unsigned long *) (& fields->f_dir10));
252b5132
RH
232 break;
233 case FR30_OPERAND_DIR8 :
33b71eeb 234 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR8, (unsigned long *) (& fields->f_dir8));
252b5132
RH
235 break;
236 case FR30_OPERAND_DIR9 :
33b71eeb 237 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_DIR9, (unsigned long *) (& fields->f_dir9));
252b5132
RH
238 break;
239 case FR30_OPERAND_DISP10 :
33b71eeb 240 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP10, (long *) (& fields->f_disp10));
252b5132
RH
241 break;
242 case FR30_OPERAND_DISP8 :
33b71eeb 243 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP8, (long *) (& fields->f_disp8));
252b5132
RH
244 break;
245 case FR30_OPERAND_DISP9 :
33b71eeb 246 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_DISP9, (long *) (& fields->f_disp9));
252b5132
RH
247 break;
248 case FR30_OPERAND_I20 :
33b71eeb 249 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I20, (unsigned long *) (& fields->f_i20));
252b5132
RH
250 break;
251 case FR30_OPERAND_I32 :
33b71eeb 252 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I32, (unsigned long *) (& fields->f_i32));
252b5132
RH
253 break;
254 case FR30_OPERAND_I8 :
33b71eeb 255 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_I8, (unsigned long *) (& fields->f_i8));
252b5132
RH
256 break;
257 case FR30_OPERAND_LABEL12 :
258 {
9494d739 259 bfd_vma value = 0;
252b5132
RH
260 errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL12, 0, NULL, & value);
261 fields->f_rel12 = value;
262 }
263 break;
264 case FR30_OPERAND_LABEL9 :
265 {
9494d739 266 bfd_vma value = 0;
252b5132
RH
267 errmsg = cgen_parse_address (cd, strp, FR30_OPERAND_LABEL9, 0, NULL, & value);
268 fields->f_rel9 = value;
269 }
270 break;
271 case FR30_OPERAND_M4 :
33b71eeb 272 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_M4, (long *) (& fields->f_m4));
252b5132
RH
273 break;
274 case FR30_OPERAND_PS :
eb1b03df 275 errmsg = cgen_parse_keyword (cd, strp, & fr30_cgen_opval_h_ps, & junk);
252b5132
RH
276 break;
277 case FR30_OPERAND_REGLIST_HI_LD :
33b71eeb 278 errmsg = parse_hi_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_HI_LD, (unsigned long *) (& fields->f_reglist_hi_ld));
252b5132
RH
279 break;
280 case FR30_OPERAND_REGLIST_HI_ST :
33b71eeb 281 errmsg = parse_hi_register_list_st (cd, strp, FR30_OPERAND_REGLIST_HI_ST, (unsigned long *) (& fields->f_reglist_hi_st));
252b5132
RH
282 break;
283 case FR30_OPERAND_REGLIST_LOW_LD :
33b71eeb 284 errmsg = parse_low_register_list_ld (cd, strp, FR30_OPERAND_REGLIST_LOW_LD, (unsigned long *) (& fields->f_reglist_low_ld));
252b5132
RH
285 break;
286 case FR30_OPERAND_REGLIST_LOW_ST :
33b71eeb 287 errmsg = parse_low_register_list_st (cd, strp, FR30_OPERAND_REGLIST_LOW_ST, (unsigned long *) (& fields->f_reglist_low_st));
252b5132
RH
288 break;
289 case FR30_OPERAND_S10 :
33b71eeb 290 errmsg = cgen_parse_signed_integer (cd, strp, FR30_OPERAND_S10, (long *) (& fields->f_s10));
252b5132
RH
291 break;
292 case FR30_OPERAND_U10 :
33b71eeb 293 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U10, (unsigned long *) (& fields->f_u10));
252b5132
RH
294 break;
295 case FR30_OPERAND_U4 :
33b71eeb 296 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4, (unsigned long *) (& fields->f_u4));
252b5132
RH
297 break;
298 case FR30_OPERAND_U4C :
33b71eeb 299 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U4C, (unsigned long *) (& fields->f_u4c));
252b5132
RH
300 break;
301 case FR30_OPERAND_U8 :
33b71eeb 302 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_U8, (unsigned long *) (& fields->f_u8));
252b5132
RH
303 break;
304 case FR30_OPERAND_UDISP6 :
33b71eeb 305 errmsg = cgen_parse_unsigned_integer (cd, strp, FR30_OPERAND_UDISP6, (unsigned long *) (& fields->f_udisp6));
252b5132
RH
306 break;
307
308 default :
309 /* xgettext:c-format */
310 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
311 abort ();
312 }
313
314 return errmsg;
315}
316
43e65147 317cgen_parse_fn * const fr30_cgen_parse_handlers[] =
252b5132
RH
318{
319 parse_insn_normal,
320};
321
322void
47b0e7ad 323fr30_cgen_init_asm (CGEN_CPU_DESC cd)
252b5132
RH
324{
325 fr30_cgen_init_opcode_table (cd);
326 fr30_cgen_init_ibld_table (cd);
327 cd->parse_handlers = & fr30_cgen_parse_handlers[0];
328 cd->parse_operand = fr30_cgen_parse_operand;
1620f33d
AM
329#ifdef CGEN_ASM_INIT_HOOK
330CGEN_ASM_INIT_HOOK
331#endif
252b5132
RH
332}
333
fc7bc883
RH
334\f
335
37111cc7 336/* Regex construction routine.
fc7bc883 337
37111cc7
NC
338 This translates an opcode syntax string into a regex string,
339 by replacing any non-character syntax element (such as an
340 opcode) with the pattern '.*'
fc7bc883 341
37111cc7
NC
342 It then compiles the regex and stores it in the opcode, for
343 later use by fr30_cgen_assemble_insn
fc7bc883 344
37111cc7 345 Returns NULL for success, an error message for failure. */
fc7bc883 346
43e65147 347char *
ffead7ae 348fr30_cgen_build_insn_regex (CGEN_INSN *insn)
43e65147 349{
d5b2f4d6 350 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
fc7bc883 351 const char *mnem = CGEN_INSN_MNEMONIC (insn);
fc7bc883
RH
352 char rxbuf[CGEN_MAX_RX_ELEMENTS];
353 char *rx = rxbuf;
354 const CGEN_SYNTAX_CHAR_TYPE *syn;
355 int reg_err;
356
357 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
358
f3a55c17
NC
359 /* Mnemonics come first in the syntax string. */
360 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
361 return _("missing mnemonic in syntax string");
fc7bc883
RH
362 ++syn;
363
f3a55c17
NC
364 /* Generate a case sensitive regular expression that emulates case
365 insensitive matching in the "C" locale. We cannot generate a case
366 insensitive regular expression because in Turkish locales, 'i' and 'I'
367 are not equal modulo case conversion. */
fc7bc883 368
f3a55c17
NC
369 /* Copy the literal mnemonic out of the insn. */
370 for (; *mnem; mnem++)
371 {
372 char c = *mnem;
373
374 if (ISALPHA (c))
375 {
376 *rx++ = '[';
377 *rx++ = TOLOWER (c);
378 *rx++ = TOUPPER (c);
379 *rx++ = ']';
380 }
381 else
382 *rx++ = c;
383 }
384
385 /* Copy any remaining literals from the syntax string into the rx. */
386 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
fc7bc883 387 {
43e65147 388 if (CGEN_SYNTAX_CHAR_P (* syn))
fc7bc883 389 {
f3a55c17
NC
390 char c = CGEN_SYNTAX_CHAR (* syn);
391
43e65147 392 switch (c)
f3a55c17
NC
393 {
394 /* Escape any regex metacharacters in the syntax. */
43e65147
L
395 case '.': case '[': case '\\':
396 case '*': case '^': case '$':
fc7bc883
RH
397
398#ifdef CGEN_ESCAPE_EXTENDED_REGEX
43e65147 399 case '?': case '{': case '}':
f3a55c17
NC
400 case '(': case ')': case '*':
401 case '|': case '+': case ']':
fc7bc883 402#endif
f3a55c17
NC
403 *rx++ = '\\';
404 *rx++ = c;
405 break;
406
407 default:
408 if (ISALPHA (c))
409 {
410 *rx++ = '[';
411 *rx++ = TOLOWER (c);
412 *rx++ = TOUPPER (c);
413 *rx++ = ']';
414 }
415 else
416 *rx++ = c;
417 break;
418 }
fc7bc883
RH
419 }
420 else
421 {
f3a55c17
NC
422 /* Replace non-syntax fields with globs. */
423 *rx++ = '.';
424 *rx++ = '*';
fc7bc883
RH
425 }
426 }
427
f3a55c17 428 /* Trailing whitespace ok. */
43e65147
L
429 * rx++ = '[';
430 * rx++ = ' ';
431 * rx++ = '\t';
432 * rx++ = ']';
433 * rx++ = '*';
fc7bc883 434
f3a55c17 435 /* But anchor it after that. */
43e65147 436 * rx++ = '$';
fc7bc883
RH
437 * rx = '\0';
438
439 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
f3a55c17 440 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
fc7bc883 441
43e65147 442 if (reg_err == 0)
fc7bc883
RH
443 return NULL;
444 else
445 {
446 static char msg[80];
f3a55c17 447
fc7bc883
RH
448 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
449 regfree ((regex_t *) CGEN_INSN_RX (insn));
450 free (CGEN_INSN_RX (insn));
451 (CGEN_INSN_RX (insn)) = NULL;
37111cc7 452 return msg;
fc7bc883
RH
453 }
454}
455
252b5132
RH
456\f
457/* Default insn parser.
458
459 The syntax string is scanned and operands are parsed and stored in FIELDS.
460 Relocs are queued as we go via other callbacks.
461
462 ??? Note that this is currently an all-or-nothing parser. If we fail to
463 parse the instruction, we return 0 and the caller will start over from
464 the beginning. Backtracking will be necessary in parsing subexpressions,
465 but that can be handled there. Not handling backtracking here may get
466 expensive in the case of the m68k. Deal with later.
467
f3a55c17 468 Returns NULL for success, an error message for failure. */
252b5132
RH
469
470static const char *
ffead7ae
MM
471parse_insn_normal (CGEN_CPU_DESC cd,
472 const CGEN_INSN *insn,
473 const char **strp,
474 CGEN_FIELDS *fields)
252b5132
RH
475{
476 /* ??? Runtime added insns not handled yet. */
477 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
478 const char *str = *strp;
479 const char *errmsg;
480 const char *p;
b3466c39 481 const CGEN_SYNTAX_CHAR_TYPE * syn;
252b5132
RH
482#ifdef CGEN_MNEMONIC_OPERANDS
483 /* FIXME: wip */
484 int past_opcode_p;
485#endif
486
487 /* For now we assume the mnemonic is first (there are no leading operands).
488 We can parse it without needing to set up operand parsing.
489 GAS's input scrubber will ensure mnemonics are lowercase, but we may
490 not be called from GAS. */
491 p = CGEN_INSN_MNEMONIC (insn);
37111cc7 492 while (*p && TOLOWER (*p) == TOLOWER (*str))
252b5132 493 ++p, ++str;
1fa60b5d
DE
494
495 if (* p)
496 return _("unrecognized instruction");
497
498#ifndef CGEN_MNEMONIC_OPERANDS
37111cc7 499 if (* str && ! ISSPACE (* str))
252b5132 500 return _("unrecognized instruction");
1fa60b5d 501#endif
252b5132
RH
502
503 CGEN_INIT_PARSE (cd);
504 cgen_init_parse_operand (cd);
505#ifdef CGEN_MNEMONIC_OPERANDS
506 past_opcode_p = 0;
507#endif
508
509 /* We don't check for (*str != '\0') here because we want to parse
510 any trailing fake arguments in the syntax string. */
511 syn = CGEN_SYNTAX_STRING (syntax);
512
513 /* Mnemonics come first for now, ensure valid string. */
514 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
515 abort ();
516
517 ++syn;
518
519 while (* syn != 0)
520 {
521 /* Non operand chars must match exactly. */
522 if (CGEN_SYNTAX_CHAR_P (* syn))
523 {
1fa60b5d
DE
524 /* FIXME: While we allow for non-GAS callers above, we assume the
525 first char after the mnemonic part is a space. */
526 /* FIXME: We also take inappropriate advantage of the fact that
527 GAS's input scrubber will remove extraneous blanks. */
37111cc7 528 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
252b5132
RH
529 {
530#ifdef CGEN_MNEMONIC_OPERANDS
b3466c39 531 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
252b5132
RH
532 past_opcode_p = 1;
533#endif
534 ++ syn;
535 ++ str;
536 }
b3466c39 537 else if (*str)
252b5132
RH
538 {
539 /* Syntax char didn't match. Can't be this insn. */
6bb95a0f 540 static char msg [80];
f3a55c17 541
6bb95a0f
DB
542 /* xgettext:c-format */
543 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
b3466c39
DB
544 CGEN_SYNTAX_CHAR(*syn), *str);
545 return msg;
546 }
547 else
548 {
549 /* Ran out of input. */
550 static char msg [80];
f3a55c17 551
b3466c39
DB
552 /* xgettext:c-format */
553 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
554 CGEN_SYNTAX_CHAR(*syn));
6bb95a0f 555 return msg;
252b5132
RH
556 }
557 continue;
558 }
559
c7e2358a
AM
560#ifdef CGEN_MNEMONIC_OPERANDS
561 (void) past_opcode_p;
562#endif
252b5132 563 /* We have an operand of some sort. */
c7e2358a 564 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
252b5132
RH
565 if (errmsg)
566 return errmsg;
567
568 /* Done with this operand, continue with next one. */
569 ++ syn;
570 }
571
572 /* If we're at the end of the syntax string, we're done. */
b3466c39 573 if (* syn == 0)
252b5132
RH
574 {
575 /* FIXME: For the moment we assume a valid `str' can only contain
576 blanks now. IE: We needn't try again with a longer version of
577 the insn and it is assumed that longer versions of insns appear
578 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
37111cc7 579 while (ISSPACE (* str))
252b5132
RH
580 ++ str;
581
582 if (* str != '\0')
583 return _("junk at end of line"); /* FIXME: would like to include `str' */
584
585 return NULL;
586 }
587
588 /* We couldn't parse it. */
589 return _("unrecognized instruction");
590}
591\f
592/* Main entry point.
593 This routine is called for each instruction to be assembled.
594 STR points to the insn to be assembled.
595 We assume all necessary tables have been initialized.
596 The assembled instruction, less any fixups, is stored in BUF.
597 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
598 still needs to be converted to target byte order, otherwise BUF is an array
599 of bytes in target byte order.
600 The result is a pointer to the insn's entry in the opcode table,
601 or NULL if an error occured (an error message will have already been
602 printed).
603
604 Note that when processing (non-alias) macro-insns,
605 this function recurses.
606
607 ??? It's possible to make this cpu-independent.
608 One would have to deal with a few minor things.
609 At this point in time doing so would be more of a curiosity than useful
610 [for example this file isn't _that_ big], but keeping the possibility in
611 mind helps keep the design clean. */
612
613const CGEN_INSN *
ffead7ae
MM
614fr30_cgen_assemble_insn (CGEN_CPU_DESC cd,
615 const char *str,
616 CGEN_FIELDS *fields,
617 CGEN_INSN_BYTES_PTR buf,
618 char **errmsg)
252b5132
RH
619{
620 const char *start;
621 CGEN_INSN_LIST *ilist;
b3466c39
DB
622 const char *parse_errmsg = NULL;
623 const char *insert_errmsg = NULL;
fc7bc883 624 int recognized_mnemonic = 0;
252b5132
RH
625
626 /* Skip leading white space. */
37111cc7 627 while (ISSPACE (* str))
252b5132
RH
628 ++ str;
629
630 /* The instructions are stored in hashed lists.
631 Get the first in the list. */
632 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
633
634 /* Keep looking until we find a match. */
252b5132
RH
635 start = str;
636 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
637 {
638 const CGEN_INSN *insn = ilist->insn;
fc7bc883 639 recognized_mnemonic = 1;
252b5132 640
43e65147 641#ifdef CGEN_VALIDATE_INSN_SUPPORTED
f3a55c17
NC
642 /* Not usually needed as unsupported opcodes
643 shouldn't be in the hash lists. */
252b5132
RH
644 /* Is this insn supported by the selected cpu? */
645 if (! fr30_cgen_insn_supported (cd, insn))
646 continue;
647#endif
b11dcf4e 648 /* If the RELAXED attribute is set, this is an insn that shouldn't be
252b5132
RH
649 chosen immediately. Instead, it is used during assembler/linker
650 relaxation if possible. */
b11dcf4e 651 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
252b5132
RH
652 continue;
653
654 str = start;
655
f3a55c17 656 /* Skip this insn if str doesn't look right lexically. */
fc7bc883
RH
657 if (CGEN_INSN_RX (insn) != NULL &&
658 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
659 continue;
660
252b5132
RH
661 /* Allow parse/insert handlers to obtain length of insn. */
662 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
663
b3466c39
DB
664 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
665 if (parse_errmsg != NULL)
6bb95a0f 666 continue;
252b5132 667
f3a55c17 668 /* ??? 0 is passed for `pc'. */
b3466c39
DB
669 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
670 (bfd_vma) 0);
671 if (insert_errmsg != NULL)
6bb95a0f
DB
672 continue;
673
674 /* It is up to the caller to actually output the insn and any
675 queued relocs. */
676 return insn;
252b5132
RH
677 }
678
252b5132 679 {
6bb95a0f 680 static char errbuf[150];
b3466c39 681 const char *tmp_errmsg;
c7e2358a
AM
682#ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
683#define be_verbose 1
6bb95a0f 684#else
c7e2358a 685#define be_verbose 0
6bb95a0f 686#endif
c7e2358a
AM
687
688 if (be_verbose)
689 {
690 /* If requesting verbose error messages, use insert_errmsg.
691 Failing that, use parse_errmsg. */
692 tmp_errmsg = (insert_errmsg ? insert_errmsg :
693 parse_errmsg ? parse_errmsg :
694 recognized_mnemonic ?
695 _("unrecognized form of instruction") :
696 _("unrecognized instruction"));
697
698 if (strlen (start) > 50)
699 /* xgettext:c-format */
700 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
43e65147 701 else
c7e2358a
AM
702 /* xgettext:c-format */
703 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
704 }
705 else
706 {
707 if (strlen (start) > 50)
708 /* xgettext:c-format */
709 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
43e65147 710 else
c7e2358a
AM
711 /* xgettext:c-format */
712 sprintf (errbuf, _("bad instruction `%.50s'"), start);
713 }
43e65147 714
252b5132
RH
715 *errmsg = errbuf;
716 return NULL;
717 }
718}
This page took 0.892251 seconds and 4 git commands to generate.