Fixup gdb.python/py-value.exp for bare-metal aarch64-elf
[deliverable/binutils-gdb.git] / opcodes / ip2k-asm.c
CommitLineData
a40cbfa3
NC
1/* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
3
47b0e7ad
NC
4 THIS FILE IS MACHINE GENERATED WITH CGEN.
5 - the resultant file is machine generated, cgen-asm.in isn't
a40cbfa3 6
6f2750fe 7 Copyright (C) 1996-2016 Free Software Foundation, Inc.
a40cbfa3 8
9b201bb5 9 This file is part of libopcodes.
a40cbfa3 10
9b201bb5 11 This library is free software; you can redistribute it and/or modify
47b0e7ad 12 it under the terms of the GNU General Public License as published by
9b201bb5 13 the Free Software Foundation; either version 3, or (at your option)
47b0e7ad 14 any later version.
a40cbfa3 15
9b201bb5
NC
16 It is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 License for more details.
a40cbfa3 20
47b0e7ad
NC
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 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
a40cbfa3 24
9b201bb5 25
a40cbfa3
NC
26/* ??? Eventually more and more of this stuff can go to cpu-independent files.
27 Keep that in mind. */
28
29#include "sysdep.h"
30#include <stdio.h>
31#include "ansidecl.h"
32#include "bfd.h"
33#include "symcat.h"
34#include "ip2k-desc.h"
35#include "ip2k-opc.h"
36#include "opintl.h"
37#include "xregex.h"
38#include "libiberty.h"
39#include "safe-ctype.h"
40
41#undef min
42#define min(a,b) ((a) < (b) ? (a) : (b))
43#undef max
44#define max(a,b) ((a) > (b) ? (a) : (b))
45
46static const char * parse_insn_normal
ffead7ae 47 (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
a40cbfa3
NC
48\f
49/* -- assembler routines inserted here. */
50
51/* -- asm.c */
52
53static const char *
47b0e7ad
NC
54parse_fr (CGEN_CPU_DESC cd,
55 const char **strp,
56 int opindex,
57 unsigned long *valuep)
a40cbfa3
NC
58{
59 const char *errmsg;
98f70fc4 60 const char *old_strp;
43e65147 61 char *afteroffset;
a40cbfa3
NC
62 enum cgen_parse_operand_result result_type;
63 bfd_vma value;
64 extern CGEN_KEYWORD ip2k_cgen_opval_register_names;
98f70fc4 65 bfd_vma tempvalue;
a40cbfa3
NC
66
67 old_strp = *strp;
47b0e7ad 68 afteroffset = NULL;
a40cbfa3 69
33b71eeb
NC
70 /* Check here to see if you're about to try parsing a w as the first arg
71 and return an error if you are. */
72 if ((strncmp (*strp, "w", 1) == 0) || (strncmp (*strp, "W", 1) == 0))
98f70fc4
AM
73 {
74 (*strp)++;
a40cbfa3 75
33b71eeb 76 if ((strncmp (*strp, ",", 1) == 0) || ISSPACE (**strp))
98f70fc4 77 {
33b71eeb
NC
78 /* We've been passed a w. Return with an error message so that
79 cgen will try the next parsing option. */
98f70fc4
AM
80 errmsg = _("W keyword invalid in FR operand slot.");
81 return errmsg;
82 }
83 *strp = old_strp;
84 }
a40cbfa3 85
a40cbfa3 86 /* Attempt parse as register keyword. */
98f70fc4 87 errmsg = cgen_parse_keyword (cd, strp, & ip2k_cgen_opval_register_names,
33b71eeb
NC
88 (long *) valuep);
89 if (*strp != NULL
90 && errmsg == NULL)
91 return errmsg;
a40cbfa3 92
33b71eeb
NC
93 /* Attempt to parse for "(IP)". */
94 afteroffset = strstr (*strp, "(IP)");
a40cbfa3 95
33b71eeb
NC
96 if (afteroffset == NULL)
97 /* Make sure it's not in lower case. */
98 afteroffset = strstr (*strp, "(ip)");
a40cbfa3 99
33b71eeb 100 if (afteroffset != NULL)
a40cbfa3 101 {
33b71eeb 102 if (afteroffset != *strp)
a40cbfa3 103 {
33b71eeb 104 /* Invalid offset present. */
a40cbfa3
NC
105 errmsg = _("offset(IP) is not a valid form");
106 return errmsg;
107 }
108 else
109 {
43e65147 110 *strp += 4;
a40cbfa3
NC
111 *valuep = 0;
112 errmsg = NULL;
113 return errmsg;
114 }
115 }
116
33b71eeb
NC
117 /* Attempt to parse for DP. ex: mov w, offset(DP)
118 mov offset(DP),w */
a40cbfa3 119
33b71eeb
NC
120 /* Try parsing it as an address and see what comes back. */
121 afteroffset = strstr (*strp, "(DP)");
a40cbfa3 122
33b71eeb
NC
123 if (afteroffset == NULL)
124 /* Maybe it's in lower case. */
125 afteroffset = strstr (*strp, "(dp)");
a40cbfa3 126
33b71eeb 127 if (afteroffset != NULL)
98f70fc4 128 {
33b71eeb 129 if (afteroffset == *strp)
98f70fc4 130 {
33b71eeb 131 /* No offset present. Use 0 by default. */
98f70fc4
AM
132 tempvalue = 0;
133 errmsg = NULL;
134 }
135 else
33b71eeb
NC
136 errmsg = cgen_parse_address (cd, strp, opindex,
137 BFD_RELOC_IP2K_FR_OFFSET,
138 & result_type, & tempvalue);
98f70fc4
AM
139
140 if (errmsg == NULL)
141 {
142 if (tempvalue <= 127)
143 {
33b71eeb 144 /* Value is ok. Fix up the first 2 bits and return. */
98f70fc4 145 *valuep = 0x0100 | tempvalue;
47b0e7ad 146 *strp += 4; /* Skip over the (DP) in *strp. */
98f70fc4
AM
147 return errmsg;
148 }
149 else
150 {
151 /* Found something there in front of (DP) but it's out
33b71eeb 152 of range. */
98f70fc4
AM
153 errmsg = _("(DP) offset out of range.");
154 return errmsg;
155 }
156 }
157 }
a40cbfa3
NC
158
159
33b71eeb
NC
160 /* Attempt to parse for SP. ex: mov w, offset(SP)
161 mov offset(SP), w. */
162 afteroffset = strstr (*strp, "(SP)");
a40cbfa3
NC
163
164 if (afteroffset == NULL)
33b71eeb
NC
165 /* Maybe it's in lower case. */
166 afteroffset = strstr (*strp, "(sp)");
a40cbfa3 167
33b71eeb 168 if (afteroffset != NULL)
98f70fc4 169 {
33b71eeb 170 if (afteroffset == *strp)
98f70fc4 171 {
33b71eeb 172 /* No offset present. Use 0 by default. */
98f70fc4
AM
173 tempvalue = 0;
174 errmsg = NULL;
175 }
176 else
33b71eeb
NC
177 errmsg = cgen_parse_address (cd, strp, opindex,
178 BFD_RELOC_IP2K_FR_OFFSET,
179 & result_type, & tempvalue);
180
98f70fc4
AM
181 if (errmsg == NULL)
182 {
183 if (tempvalue <= 127)
184 {
33b71eeb 185 /* Value is ok. Fix up the first 2 bits and return. */
98f70fc4 186 *valuep = 0x0180 | tempvalue;
47b0e7ad 187 *strp += 4; /* Skip over the (SP) in *strp. */
98f70fc4
AM
188 return errmsg;
189 }
190 else
191 {
192 /* Found something there in front of (SP) but it's out
33b71eeb 193 of range. */
98f70fc4
AM
194 errmsg = _("(SP) offset out of range.");
195 return errmsg;
196 }
98f70fc4
AM
197 }
198 }
a40cbfa3 199
33b71eeb 200 /* Attempt to parse as an address. */
a40cbfa3
NC
201 *strp = old_strp;
202 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IP2K_FR9,
203 & result_type, & value);
204 if (errmsg == NULL)
205 {
206 *valuep = value;
207
47b0e7ad 208 /* If a parenthesis is found, warn about invalid form. */
a40cbfa3 209 if (**strp == '(')
33b71eeb
NC
210 errmsg = _("illegal use of parentheses");
211
47b0e7ad 212 /* If a numeric value is specified, ensure that it is between
33b71eeb 213 1 and 255. */
a40cbfa3
NC
214 else if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
215 {
216 if (value < 0x1 || value > 0xff)
217 errmsg = _("operand out of range (not between 1 and 255)");
218 }
219 }
220 return errmsg;
221}
222
223static const char *
47b0e7ad
NC
224parse_addr16 (CGEN_CPU_DESC cd,
225 const char **strp,
226 int opindex,
227 unsigned long *valuep)
a40cbfa3
NC
228{
229 const char *errmsg;
230 enum cgen_parse_operand_result result_type;
231 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
98f70fc4 232 bfd_vma value;
a40cbfa3 233
33b71eeb 234 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16H)
98f70fc4 235 code = BFD_RELOC_IP2K_HI8DATA;
33b71eeb 236 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16L)
98f70fc4 237 code = BFD_RELOC_IP2K_LO8DATA;
a40cbfa3
NC
238 else
239 {
47b0e7ad 240 /* Something is very wrong. opindex has to be one of the above. */
a40cbfa3
NC
241 errmsg = _("parse_addr16: invalid opindex.");
242 return errmsg;
243 }
43e65147 244
a40cbfa3 245 errmsg = cgen_parse_address (cd, strp, opindex, code,
98f70fc4 246 & result_type, & value);
a40cbfa3
NC
247 if (errmsg == NULL)
248 {
47b0e7ad 249 /* We either have a relocation or a number now. */
33b71eeb 250 if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
98f70fc4 251 {
47b0e7ad 252 /* We got a number back. */
33b71eeb 253 if (code == BFD_RELOC_IP2K_HI8DATA)
a40cbfa3 254 value >>= 8;
47b0e7ad
NC
255 else
256 /* code = BFD_RELOC_IP2K_LOW8DATA. */
a40cbfa3 257 value &= 0x00FF;
43e65147 258 }
98f70fc4
AM
259 *valuep = value;
260 }
a40cbfa3
NC
261
262 return errmsg;
263}
264
98f70fc4 265static const char *
47b0e7ad
NC
266parse_addr16_cjp (CGEN_CPU_DESC cd,
267 const char **strp,
268 int opindex,
269 unsigned long *valuep)
98f70fc4
AM
270{
271 const char *errmsg;
272 enum cgen_parse_operand_result result_type;
273 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
274 bfd_vma value;
43e65147 275
33b71eeb 276 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP)
98f70fc4 277 code = BFD_RELOC_IP2K_ADDR16CJP;
33b71eeb 278 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P)
98f70fc4
AM
279 code = BFD_RELOC_IP2K_PAGE3;
280
281 errmsg = cgen_parse_address (cd, strp, opindex, code,
282 & result_type, & value);
283 if (errmsg == NULL)
284 {
33b71eeb 285 if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
98f70fc4 286 {
33b71eeb 287 if ((value & 0x1) == 0) /* If the address is even .... */
98f70fc4 288 {
33b71eeb 289 if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP)
47b0e7ad 290 *valuep = (value >> 1) & 0x1FFF; /* Should mask be 1FFF? */
33b71eeb 291 else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P)
a40cbfa3 292 *valuep = (value >> 14) & 0x7;
98f70fc4 293 }
a40cbfa3
NC
294 else
295 errmsg = _("Byte address required. - must be even.");
98f70fc4 296 }
33b71eeb 297 else if (result_type == CGEN_PARSE_OPERAND_RESULT_QUEUED)
98f70fc4
AM
298 {
299 /* This will happen for things like (s2-s1) where s2 and s1
300 are labels. */
301 *valuep = value;
302 }
43e65147 303 else
a40cbfa3 304 errmsg = _("cgen_parse_address returned a symbol. Literal required.");
98f70fc4 305 }
43e65147 306 return errmsg;
98f70fc4 307}
a40cbfa3 308
a40cbfa3 309static const char *
47b0e7ad
NC
310parse_lit8 (CGEN_CPU_DESC cd,
311 const char **strp,
312 int opindex,
313 long *valuep)
a40cbfa3
NC
314{
315 const char *errmsg;
316 enum cgen_parse_operand_result result_type;
317 bfd_reloc_code_real_type code = BFD_RELOC_NONE;
98f70fc4 318 bfd_vma value;
a40cbfa3 319
47b0e7ad 320 /* Parse %OP relocating operators. */
a40cbfa3
NC
321 if (strncmp (*strp, "%bank", 5) == 0)
322 {
323 *strp += 5;
324 code = BFD_RELOC_IP2K_BANK;
325 }
326 else if (strncmp (*strp, "%lo8data", 8) == 0)
327 {
328 *strp += 8;
329 code = BFD_RELOC_IP2K_LO8DATA;
330 }
331 else if (strncmp (*strp, "%hi8data", 8) == 0)
332 {
333 *strp += 8;
334 code = BFD_RELOC_IP2K_HI8DATA;
335 }
336 else if (strncmp (*strp, "%ex8data", 8) == 0)
337 {
338 *strp += 8;
339 code = BFD_RELOC_IP2K_EX8DATA;
340 }
341 else if (strncmp (*strp, "%lo8insn", 8) == 0)
342 {
343 *strp += 8;
344 code = BFD_RELOC_IP2K_LO8INSN;
345 }
346 else if (strncmp (*strp, "%hi8insn", 8) == 0)
347 {
348 *strp += 8;
349 code = BFD_RELOC_IP2K_HI8INSN;
350 }
a40cbfa3
NC
351
352 /* Parse %op operand. */
353 if (code != BFD_RELOC_NONE)
354 {
43e65147 355 errmsg = cgen_parse_address (cd, strp, opindex, code,
a40cbfa3
NC
356 & result_type, & value);
357 if ((errmsg == NULL) &&
358 (result_type != CGEN_PARSE_OPERAND_RESULT_QUEUED))
ebdb0383 359 errmsg = _("percent-operator operand is not a symbol");
a40cbfa3
NC
360
361 *valuep = value;
362 }
363 /* Parse as a number. */
364 else
365 {
366 errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
367
47b0e7ad 368 /* Truncate to eight bits to accept both signed and unsigned input. */
a40cbfa3
NC
369 if (errmsg == NULL)
370 *valuep &= 0xFF;
371 }
372
373 return errmsg;
374}
375
376static const char *
47b0e7ad
NC
377parse_bit3 (CGEN_CPU_DESC cd,
378 const char **strp,
379 int opindex,
380 unsigned long *valuep)
a40cbfa3
NC
381{
382 const char *errmsg;
383 char mode = 0;
384 long count = 0;
385 unsigned long value;
386
387 if (strncmp (*strp, "%bit", 4) == 0)
388 {
389 *strp += 4;
390 mode = 1;
391 }
392 else if (strncmp (*strp, "%msbbit", 7) == 0)
393 {
394 *strp += 7;
395 mode = 1;
396 }
397 else if (strncmp (*strp, "%lsbbit", 7) == 0)
398 {
399 *strp += 7;
400 mode = 2;
401 }
402
33b71eeb 403 errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
98f70fc4 404 if (errmsg)
a40cbfa3 405 return errmsg;
a40cbfa3 406
98f70fc4
AM
407 if (mode)
408 {
33b71eeb 409 value = * valuep;
98f70fc4
AM
410 if (value == 0)
411 {
412 errmsg = _("Attempt to find bit index of 0");
413 return errmsg;
414 }
43e65147 415
98f70fc4
AM
416 if (mode == 1)
417 {
418 count = 31;
419 while ((value & 0x80000000) == 0)
420 {
421 count--;
422 value <<= 1;
423 }
424 }
425 else if (mode == 2)
426 {
427 count = 0;
428 while ((value & 0x00000001) == 0)
429 {
430 count++;
431 value >>= 1;
432 }
433 }
43e65147 434
98f70fc4
AM
435 *valuep = count;
436 }
a40cbfa3
NC
437
438 return errmsg;
439}
440
a40cbfa3
NC
441/* -- dis.c */
442
443const char * ip2k_cgen_parse_operand
47b0e7ad 444 (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
a40cbfa3
NC
445
446/* Main entry point for operand parsing.
447
448 This function is basically just a big switch statement. Earlier versions
449 used tables to look up the function to use, but
450 - if the table contains both assembler and disassembler functions then
451 the disassembler contains much of the assembler and vice-versa,
452 - there's a lot of inlining possibilities as things grow,
453 - using a switch statement avoids the function call overhead.
454
455 This function could be moved into `parse_insn_normal', but keeping it
456 separate makes clear the interface between `parse_insn_normal' and each of
457 the handlers. */
458
459const char *
47b0e7ad
NC
460ip2k_cgen_parse_operand (CGEN_CPU_DESC cd,
461 int opindex,
462 const char ** strp,
463 CGEN_FIELDS * fields)
a40cbfa3
NC
464{
465 const char * errmsg = NULL;
466 /* Used by scalar operands that still need to be parsed. */
467 long junk ATTRIBUTE_UNUSED;
468
469 switch (opindex)
470 {
471 case IP2K_OPERAND_ADDR16CJP :
33b71eeb 472 errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16CJP, (unsigned long *) (& fields->f_addr16cjp));
a40cbfa3
NC
473 break;
474 case IP2K_OPERAND_ADDR16H :
33b71eeb 475 errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16H, (unsigned long *) (& fields->f_imm8));
a40cbfa3
NC
476 break;
477 case IP2K_OPERAND_ADDR16L :
33b71eeb 478 errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16L, (unsigned long *) (& fields->f_imm8));
a40cbfa3
NC
479 break;
480 case IP2K_OPERAND_ADDR16P :
33b71eeb 481 errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16P, (unsigned long *) (& fields->f_page3));
a40cbfa3
NC
482 break;
483 case IP2K_OPERAND_BITNO :
33b71eeb 484 errmsg = parse_bit3 (cd, strp, IP2K_OPERAND_BITNO, (unsigned long *) (& fields->f_bitno));
a40cbfa3
NC
485 break;
486 case IP2K_OPERAND_CBIT :
33b71eeb 487 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_CBIT, (unsigned long *) (& junk));
a40cbfa3
NC
488 break;
489 case IP2K_OPERAND_DCBIT :
33b71eeb 490 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_DCBIT, (unsigned long *) (& junk));
a40cbfa3
NC
491 break;
492 case IP2K_OPERAND_FR :
33b71eeb 493 errmsg = parse_fr (cd, strp, IP2K_OPERAND_FR, (unsigned long *) (& fields->f_reg));
a40cbfa3
NC
494 break;
495 case IP2K_OPERAND_LIT8 :
33b71eeb 496 errmsg = parse_lit8 (cd, strp, IP2K_OPERAND_LIT8, (long *) (& fields->f_imm8));
a40cbfa3
NC
497 break;
498 case IP2K_OPERAND_PABITS :
33b71eeb 499 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_PABITS, (unsigned long *) (& junk));
a40cbfa3
NC
500 break;
501 case IP2K_OPERAND_RETI3 :
33b71eeb 502 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_RETI3, (unsigned long *) (& fields->f_reti3));
a40cbfa3
NC
503 break;
504 case IP2K_OPERAND_ZBIT :
33b71eeb 505 errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_ZBIT, (unsigned long *) (& junk));
a40cbfa3
NC
506 break;
507
508 default :
509 /* xgettext:c-format */
510 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
511 abort ();
512 }
513
514 return errmsg;
515}
516
43e65147 517cgen_parse_fn * const ip2k_cgen_parse_handlers[] =
a40cbfa3
NC
518{
519 parse_insn_normal,
520};
521
522void
47b0e7ad 523ip2k_cgen_init_asm (CGEN_CPU_DESC cd)
a40cbfa3
NC
524{
525 ip2k_cgen_init_opcode_table (cd);
526 ip2k_cgen_init_ibld_table (cd);
527 cd->parse_handlers = & ip2k_cgen_parse_handlers[0];
528 cd->parse_operand = ip2k_cgen_parse_operand;
1620f33d
AM
529#ifdef CGEN_ASM_INIT_HOOK
530CGEN_ASM_INIT_HOOK
531#endif
a40cbfa3
NC
532}
533
534\f
535
536/* Regex construction routine.
537
538 This translates an opcode syntax string into a regex string,
539 by replacing any non-character syntax element (such as an
540 opcode) with the pattern '.*'
541
542 It then compiles the regex and stores it in the opcode, for
543 later use by ip2k_cgen_assemble_insn
544
545 Returns NULL for success, an error message for failure. */
546
43e65147 547char *
ffead7ae 548ip2k_cgen_build_insn_regex (CGEN_INSN *insn)
43e65147 549{
a40cbfa3
NC
550 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
551 const char *mnem = CGEN_INSN_MNEMONIC (insn);
552 char rxbuf[CGEN_MAX_RX_ELEMENTS];
553 char *rx = rxbuf;
554 const CGEN_SYNTAX_CHAR_TYPE *syn;
555 int reg_err;
556
557 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
558
559 /* Mnemonics come first in the syntax string. */
560 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
561 return _("missing mnemonic in syntax string");
562 ++syn;
563
564 /* Generate a case sensitive regular expression that emulates case
565 insensitive matching in the "C" locale. We cannot generate a case
566 insensitive regular expression because in Turkish locales, 'i' and 'I'
567 are not equal modulo case conversion. */
568
569 /* Copy the literal mnemonic out of the insn. */
570 for (; *mnem; mnem++)
571 {
572 char c = *mnem;
573
574 if (ISALPHA (c))
575 {
576 *rx++ = '[';
577 *rx++ = TOLOWER (c);
578 *rx++ = TOUPPER (c);
579 *rx++ = ']';
580 }
581 else
582 *rx++ = c;
583 }
584
585 /* Copy any remaining literals from the syntax string into the rx. */
586 for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
587 {
43e65147 588 if (CGEN_SYNTAX_CHAR_P (* syn))
a40cbfa3
NC
589 {
590 char c = CGEN_SYNTAX_CHAR (* syn);
591
43e65147 592 switch (c)
a40cbfa3
NC
593 {
594 /* Escape any regex metacharacters in the syntax. */
43e65147
L
595 case '.': case '[': case '\\':
596 case '*': case '^': case '$':
a40cbfa3
NC
597
598#ifdef CGEN_ESCAPE_EXTENDED_REGEX
43e65147 599 case '?': case '{': case '}':
a40cbfa3
NC
600 case '(': case ')': case '*':
601 case '|': case '+': case ']':
602#endif
603 *rx++ = '\\';
604 *rx++ = c;
605 break;
606
607 default:
608 if (ISALPHA (c))
609 {
610 *rx++ = '[';
611 *rx++ = TOLOWER (c);
612 *rx++ = TOUPPER (c);
613 *rx++ = ']';
614 }
615 else
616 *rx++ = c;
617 break;
618 }
619 }
620 else
621 {
622 /* Replace non-syntax fields with globs. */
623 *rx++ = '.';
624 *rx++ = '*';
625 }
626 }
627
628 /* Trailing whitespace ok. */
43e65147
L
629 * rx++ = '[';
630 * rx++ = ' ';
631 * rx++ = '\t';
632 * rx++ = ']';
633 * rx++ = '*';
a40cbfa3
NC
634
635 /* But anchor it after that. */
43e65147 636 * rx++ = '$';
a40cbfa3
NC
637 * rx = '\0';
638
639 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
640 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
641
43e65147 642 if (reg_err == 0)
a40cbfa3
NC
643 return NULL;
644 else
645 {
646 static char msg[80];
647
648 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
649 regfree ((regex_t *) CGEN_INSN_RX (insn));
650 free (CGEN_INSN_RX (insn));
651 (CGEN_INSN_RX (insn)) = NULL;
652 return msg;
653 }
654}
655
656\f
657/* Default insn parser.
658
659 The syntax string is scanned and operands are parsed and stored in FIELDS.
660 Relocs are queued as we go via other callbacks.
661
662 ??? Note that this is currently an all-or-nothing parser. If we fail to
663 parse the instruction, we return 0 and the caller will start over from
664 the beginning. Backtracking will be necessary in parsing subexpressions,
665 but that can be handled there. Not handling backtracking here may get
666 expensive in the case of the m68k. Deal with later.
667
668 Returns NULL for success, an error message for failure. */
669
670static const char *
ffead7ae
MM
671parse_insn_normal (CGEN_CPU_DESC cd,
672 const CGEN_INSN *insn,
673 const char **strp,
674 CGEN_FIELDS *fields)
a40cbfa3
NC
675{
676 /* ??? Runtime added insns not handled yet. */
677 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
678 const char *str = *strp;
679 const char *errmsg;
680 const char *p;
681 const CGEN_SYNTAX_CHAR_TYPE * syn;
682#ifdef CGEN_MNEMONIC_OPERANDS
683 /* FIXME: wip */
684 int past_opcode_p;
685#endif
686
687 /* For now we assume the mnemonic is first (there are no leading operands).
688 We can parse it without needing to set up operand parsing.
689 GAS's input scrubber will ensure mnemonics are lowercase, but we may
690 not be called from GAS. */
691 p = CGEN_INSN_MNEMONIC (insn);
692 while (*p && TOLOWER (*p) == TOLOWER (*str))
693 ++p, ++str;
694
695 if (* p)
696 return _("unrecognized instruction");
697
698#ifndef CGEN_MNEMONIC_OPERANDS
699 if (* str && ! ISSPACE (* str))
700 return _("unrecognized instruction");
701#endif
702
703 CGEN_INIT_PARSE (cd);
704 cgen_init_parse_operand (cd);
705#ifdef CGEN_MNEMONIC_OPERANDS
706 past_opcode_p = 0;
707#endif
708
709 /* We don't check for (*str != '\0') here because we want to parse
710 any trailing fake arguments in the syntax string. */
711 syn = CGEN_SYNTAX_STRING (syntax);
712
713 /* Mnemonics come first for now, ensure valid string. */
714 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
715 abort ();
716
717 ++syn;
718
719 while (* syn != 0)
720 {
721 /* Non operand chars must match exactly. */
722 if (CGEN_SYNTAX_CHAR_P (* syn))
723 {
724 /* FIXME: While we allow for non-GAS callers above, we assume the
725 first char after the mnemonic part is a space. */
726 /* FIXME: We also take inappropriate advantage of the fact that
727 GAS's input scrubber will remove extraneous blanks. */
728 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
729 {
730#ifdef CGEN_MNEMONIC_OPERANDS
731 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
732 past_opcode_p = 1;
733#endif
734 ++ syn;
735 ++ str;
736 }
737 else if (*str)
738 {
739 /* Syntax char didn't match. Can't be this insn. */
740 static char msg [80];
741
742 /* xgettext:c-format */
743 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
744 CGEN_SYNTAX_CHAR(*syn), *str);
745 return msg;
746 }
747 else
748 {
749 /* Ran out of input. */
750 static char msg [80];
751
752 /* xgettext:c-format */
753 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
754 CGEN_SYNTAX_CHAR(*syn));
755 return msg;
756 }
757 continue;
758 }
759
c7e2358a
AM
760#ifdef CGEN_MNEMONIC_OPERANDS
761 (void) past_opcode_p;
762#endif
a40cbfa3 763 /* We have an operand of some sort. */
c7e2358a 764 errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
a40cbfa3
NC
765 if (errmsg)
766 return errmsg;
767
768 /* Done with this operand, continue with next one. */
769 ++ syn;
770 }
771
772 /* If we're at the end of the syntax string, we're done. */
773 if (* syn == 0)
774 {
775 /* FIXME: For the moment we assume a valid `str' can only contain
776 blanks now. IE: We needn't try again with a longer version of
777 the insn and it is assumed that longer versions of insns appear
778 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
779 while (ISSPACE (* str))
780 ++ str;
781
782 if (* str != '\0')
783 return _("junk at end of line"); /* FIXME: would like to include `str' */
784
785 return NULL;
786 }
787
788 /* We couldn't parse it. */
789 return _("unrecognized instruction");
790}
791\f
792/* Main entry point.
793 This routine is called for each instruction to be assembled.
794 STR points to the insn to be assembled.
795 We assume all necessary tables have been initialized.
796 The assembled instruction, less any fixups, is stored in BUF.
797 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
798 still needs to be converted to target byte order, otherwise BUF is an array
799 of bytes in target byte order.
800 The result is a pointer to the insn's entry in the opcode table,
801 or NULL if an error occured (an error message will have already been
802 printed).
803
804 Note that when processing (non-alias) macro-insns,
805 this function recurses.
806
807 ??? It's possible to make this cpu-independent.
808 One would have to deal with a few minor things.
809 At this point in time doing so would be more of a curiosity than useful
810 [for example this file isn't _that_ big], but keeping the possibility in
811 mind helps keep the design clean. */
812
813const CGEN_INSN *
ffead7ae
MM
814ip2k_cgen_assemble_insn (CGEN_CPU_DESC cd,
815 const char *str,
816 CGEN_FIELDS *fields,
817 CGEN_INSN_BYTES_PTR buf,
818 char **errmsg)
a40cbfa3
NC
819{
820 const char *start;
821 CGEN_INSN_LIST *ilist;
822 const char *parse_errmsg = NULL;
823 const char *insert_errmsg = NULL;
824 int recognized_mnemonic = 0;
825
826 /* Skip leading white space. */
827 while (ISSPACE (* str))
828 ++ str;
829
830 /* The instructions are stored in hashed lists.
831 Get the first in the list. */
832 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
833
834 /* Keep looking until we find a match. */
835 start = str;
836 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
837 {
838 const CGEN_INSN *insn = ilist->insn;
839 recognized_mnemonic = 1;
840
43e65147 841#ifdef CGEN_VALIDATE_INSN_SUPPORTED
a40cbfa3
NC
842 /* Not usually needed as unsupported opcodes
843 shouldn't be in the hash lists. */
844 /* Is this insn supported by the selected cpu? */
845 if (! ip2k_cgen_insn_supported (cd, insn))
846 continue;
847#endif
b11dcf4e 848 /* If the RELAXED attribute is set, this is an insn that shouldn't be
a40cbfa3
NC
849 chosen immediately. Instead, it is used during assembler/linker
850 relaxation if possible. */
b11dcf4e 851 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
a40cbfa3
NC
852 continue;
853
854 str = start;
855
856 /* Skip this insn if str doesn't look right lexically. */
857 if (CGEN_INSN_RX (insn) != NULL &&
858 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
859 continue;
860
861 /* Allow parse/insert handlers to obtain length of insn. */
862 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
863
864 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
865 if (parse_errmsg != NULL)
866 continue;
867
868 /* ??? 0 is passed for `pc'. */
869 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
870 (bfd_vma) 0);
871 if (insert_errmsg != NULL)
872 continue;
873
874 /* It is up to the caller to actually output the insn and any
875 queued relocs. */
876 return insn;
877 }
878
879 {
880 static char errbuf[150];
a40cbfa3 881 const char *tmp_errmsg;
c7e2358a
AM
882#ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
883#define be_verbose 1
a40cbfa3 884#else
c7e2358a 885#define be_verbose 0
a40cbfa3 886#endif
c7e2358a
AM
887
888 if (be_verbose)
889 {
890 /* If requesting verbose error messages, use insert_errmsg.
891 Failing that, use parse_errmsg. */
892 tmp_errmsg = (insert_errmsg ? insert_errmsg :
893 parse_errmsg ? parse_errmsg :
894 recognized_mnemonic ?
895 _("unrecognized form of instruction") :
896 _("unrecognized instruction"));
897
898 if (strlen (start) > 50)
899 /* xgettext:c-format */
900 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
43e65147 901 else
c7e2358a
AM
902 /* xgettext:c-format */
903 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
904 }
905 else
906 {
907 if (strlen (start) > 50)
908 /* xgettext:c-format */
909 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
43e65147 910 else
c7e2358a
AM
911 /* xgettext:c-format */
912 sprintf (errbuf, _("bad instruction `%.50s'"), start);
913 }
43e65147 914
a40cbfa3
NC
915 *errmsg = errbuf;
916 return NULL;
917 }
918}
This page took 0.74344 seconds and 4 git commands to generate.