* config/obj-elf.c: (obj_elf_change_section): Add "group" param.
[deliverable/binutils-gdb.git] / opcodes / openrisc-asm.c
CommitLineData
87e6d782
NC
1/* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
3
4THIS FILE IS MACHINE GENERATED WITH CGEN.
5- the resultant file is machine generated, cgen-asm.in isn't
6
7Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
8
9This file is part of the GNU Binutils and GDB, the GNU debugger.
10
11This program is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published by
13the Free Software Foundation; either version 2, or (at your option)
14any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software Foundation, Inc.,
2359 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24
25/* ??? Eventually more and more of this stuff can go to cpu-independent files.
26 Keep that in mind. */
27
28#include "sysdep.h"
87e6d782
NC
29#include <stdio.h>
30#include "ansidecl.h"
3882b010 31#include "safe-ctype.h"
87e6d782
NC
32#include "bfd.h"
33#include "symcat.h"
34#include "openrisc-desc.h"
35#include "openrisc-opc.h"
36#include "opintl.h"
fc7bc883 37#include "xregex.h"
fc05c67f 38#include "libiberty.h"
87e6d782
NC
39
40#undef min
41#define min(a,b) ((a) < (b) ? (a) : (b))
42#undef max
43#define max(a,b) ((a) > (b) ? (a) : (b))
44
45static const char * parse_insn_normal
46 PARAMS ((CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *));
fc05c67f
NC
47long openrisc_sign_extend_16bit
48 PARAMS ((long));
49static const char * parse_hi16
50 PARAMS ((CGEN_CPU_DESC, const char **, int, unsigned long *));
51static const char * parse_lo16
52 PARAMS ((CGEN_CPU_DESC, const char **, int, unsigned long *));
53const char * openrisc_cgen_parse_operand
54 PARAMS ((CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *));
87e6d782
NC
55\f
56/* -- assembler routines inserted here */
57
58/* -- asm.c */
59
60#define CGEN_VERBOSE_ASSEMBLER_ERRORS
61
62long
63openrisc_sign_extend_16bit (value)
64 long value;
65{
66 return (long) (short) value;
67}
68
69
70/* Handle hi(). */
71
72static const char *
73parse_hi16 (cd, strp, opindex, valuep)
74 CGEN_CPU_DESC cd;
75 const char **strp;
76 int opindex;
77 unsigned long *valuep;
78{
79 const char *errmsg;
80 enum cgen_parse_operand_result result_type;
81 bfd_vma value;
82
83 if (**strp == '#')
84 ++*strp;
85
86 if (strncasecmp (*strp, "hi(", 3) == 0)
87 {
88 *strp += 3;
89
90#if 0
91 errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
92 if (errmsg != NULL)
93 fprintf (stderr, "parse_hi: %s\n", errmsg);
94 if (errmsg != NULL)
95#endif
96 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_HI16,
97 &result_type, &value);
98 if (**strp != ')')
99 return "missing `)'";
100 ++*strp;
101 if (errmsg == NULL
102 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
103 value >>= 16;
104 *valuep = (long) (short) value;
105
106 return errmsg;
107 }
108 else
109 {
110 if (**strp == '-')
111 errmsg = cgen_parse_signed_integer (cd, strp, opindex, (long *) &value);
112 else
2c8721ec 113 errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, (unsigned long *) &value);
87e6d782
NC
114 }
115 *valuep = (long) (short) (value & 0xffff);
116 return errmsg;
117}
118
119
120/* Handle lo() */
121
122static const char *
123parse_lo16 (cd, strp, opindex, valuep)
124 CGEN_CPU_DESC cd;
125 const char **strp;
126 int opindex;
127 unsigned long *valuep;
128{
129 const char *errmsg;
130 enum cgen_parse_operand_result result_type;
131 bfd_vma value;
132
133 if (**strp == '#')
134 ++*strp;
135
136 if (strncasecmp (*strp, "lo(", 3) == 0)
137 {
138 *strp += 3;
139
140#if 0
141 errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
142 if (errmsg != NULL)
143 fprintf (stderr, "parse_lo: %s\n", errmsg);
144
145 if (errmsg != NULL)
146#endif
147 errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_LO16,
148 &result_type, &value);
149 if (**strp != ')')
150 return "missing `)'";
151 ++*strp;
152 if (errmsg == NULL
153 && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
154 value &= 0xffff;
155 *valuep = (long) (short) value;
156
157 return errmsg;
158 }
159
160 if (**strp == '-')
161 errmsg = cgen_parse_signed_integer (cd, strp, opindex, (long *) &value);
162 else
2c8721ec 163 errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, (unsigned long *) &value);
87e6d782
NC
164 *valuep = (long) (short) (value & 0xffff);
165 return errmsg;
166}
167
168/* -- */
169
170/* Main entry point for operand parsing.
171
172 This function is basically just a big switch statement. Earlier versions
173 used tables to look up the function to use, but
174 - if the table contains both assembler and disassembler functions then
175 the disassembler contains much of the assembler and vice-versa,
176 - there's a lot of inlining possibilities as things grow,
177 - using a switch statement avoids the function call overhead.
178
179 This function could be moved into `parse_insn_normal', but keeping it
180 separate makes clear the interface between `parse_insn_normal' and each of
181 the handlers.
182*/
183
184const char *
185openrisc_cgen_parse_operand (cd, opindex, strp, fields)
186 CGEN_CPU_DESC cd;
187 int opindex;
188 const char ** strp;
189 CGEN_FIELDS * fields;
190{
191 const char * errmsg = NULL;
192 /* Used by scalar operands that still need to be parsed. */
fc05c67f 193 long junk ATTRIBUTE_UNUSED;
87e6d782
NC
194
195 switch (opindex)
196 {
197 case OPENRISC_OPERAND_ABS_26 :
198 {
199 bfd_vma value;
200 errmsg = cgen_parse_address (cd, strp, OPENRISC_OPERAND_ABS_26, 0, NULL, & value);
201 fields->f_abs26 = value;
202 }
203 break;
204 case OPENRISC_OPERAND_DISP_26 :
205 {
206 bfd_vma value;
207 errmsg = cgen_parse_address (cd, strp, OPENRISC_OPERAND_DISP_26, 0, NULL, & value);
208 fields->f_disp26 = value;
209 }
210 break;
211 case OPENRISC_OPERAND_HI16 :
212 errmsg = parse_hi16 (cd, strp, OPENRISC_OPERAND_HI16, &fields->f_simm16);
213 break;
214 case OPENRISC_OPERAND_LO16 :
215 errmsg = parse_lo16 (cd, strp, OPENRISC_OPERAND_LO16, &fields->f_lo16);
216 break;
217 case OPENRISC_OPERAND_OP_F_23 :
218 errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_OP_F_23, &fields->f_op4);
219 break;
220 case OPENRISC_OPERAND_OP_F_3 :
221 errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_OP_F_3, &fields->f_op5);
222 break;
223 case OPENRISC_OPERAND_RA :
224 errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r2);
225 break;
226 case OPENRISC_OPERAND_RB :
227 errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r3);
228 break;
229 case OPENRISC_OPERAND_RD :
230 errmsg = cgen_parse_keyword (cd, strp, & openrisc_cgen_opval_h_gr, & fields->f_r1);
231 break;
232 case OPENRISC_OPERAND_SIMM_16 :
233 errmsg = cgen_parse_signed_integer (cd, strp, OPENRISC_OPERAND_SIMM_16, &fields->f_simm16);
234 break;
235 case OPENRISC_OPERAND_UI16NC :
236 errmsg = parse_lo16 (cd, strp, OPENRISC_OPERAND_UI16NC, &fields->f_i16nc);
237 break;
238 case OPENRISC_OPERAND_UIMM_16 :
239 errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_UIMM_16, &fields->f_uimm16);
240 break;
241 case OPENRISC_OPERAND_UIMM_5 :
242 errmsg = cgen_parse_unsigned_integer (cd, strp, OPENRISC_OPERAND_UIMM_5, &fields->f_uimm5);
243 break;
244
245 default :
246 /* xgettext:c-format */
247 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
248 abort ();
249 }
250
251 return errmsg;
252}
253
254cgen_parse_fn * const openrisc_cgen_parse_handlers[] =
255{
256 parse_insn_normal,
257};
258
259void
260openrisc_cgen_init_asm (cd)
261 CGEN_CPU_DESC cd;
262{
263 openrisc_cgen_init_opcode_table (cd);
264 openrisc_cgen_init_ibld_table (cd);
265 cd->parse_handlers = & openrisc_cgen_parse_handlers[0];
266 cd->parse_operand = openrisc_cgen_parse_operand;
267}
268
fc7bc883
RH
269\f
270
271/*
272 Regex construction routine.
273
274 This translates an opcode syntax string into a regex string,
275 by replacing any non-character syntax element (such as an
276 opcode) with the pattern '.*'
277
278 It then compiles the regex and stores it in the opcode, for
279 later use by openrisc_cgen_assemble_insn
280
281 returns NULL for success, an error message for failure
282*/
283
284char *
285openrisc_cgen_build_insn_regex (insn)
286 CGEN_INSN *insn;
287{
fc05c67f 288 CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
fc7bc883
RH
289 const char *mnem = CGEN_INSN_MNEMONIC (insn);
290 int mnem_len;
291 char rxbuf[CGEN_MAX_RX_ELEMENTS];
292 char *rx = rxbuf;
293 const CGEN_SYNTAX_CHAR_TYPE *syn;
294 int reg_err;
295
296 syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
297
298 /* Mnemonics come first in the syntax string */
299 if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) return "missing mnemonic in syntax string";
300 ++syn;
301
302 /* copy the literal mnemonic out of the insn */
303 memset (rx, 0, CGEN_MAX_RX_ELEMENTS);
304 mnem_len = strlen(mnem);
305 memcpy (rx, mnem, mnem_len);
306 rx += mnem_len;
307
308 /* copy any remaining literals from the syntax string into the rx */
309 for(; * syn != 0 && rx < rxbuf + (CGEN_MAX_RX_ELEMENTS - 9); ++syn, ++rx)
310 {
311 if (CGEN_SYNTAX_CHAR_P (* syn))
312 {
313 char tmp = CGEN_SYNTAX_CHAR (* syn);
314 switch (tmp)
315 {
316 /* escape any regex metacharacters in the syntax */
317 case '.': case '[': case '\\':
318 case '*': case '^': case '$':
319
320#ifdef CGEN_ESCAPE_EXTENDED_REGEX
321 case '?': case '{': case '}':
322 case '(': case ')': case '*':
323 case '|': case '+': case ']':
324#endif
325
326 * rx++ = '\\';
327 break;
328 }
329 /* insert syntax char into rx */
330 * rx = tmp;
331 }
332 else
333 {
334 /* replace non-syntax fields with globs */
335 * rx = '.';
336 * ++rx = '*';
337 }
338 }
339
340 /* trailing whitespace ok */
341 * rx++ = '[';
342 * rx++ = ' ';
343 * rx++ = '\t';
344 * rx++ = ']';
345 * rx++ = '*';
346
347 /* but anchor it after that */
348 * rx++ = '$';
349 * rx = '\0';
350
351 CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
352 reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB|REG_ICASE);
353
354 if (reg_err == 0)
355 return NULL;
356 else
357 {
358 static char msg[80];
359 regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
360 regfree ((regex_t *) CGEN_INSN_RX (insn));
361 free (CGEN_INSN_RX (insn));
362 (CGEN_INSN_RX (insn)) = NULL;
363 return msg;
364 }
365}
366
87e6d782
NC
367\f
368/* Default insn parser.
369
370 The syntax string is scanned and operands are parsed and stored in FIELDS.
371 Relocs are queued as we go via other callbacks.
372
373 ??? Note that this is currently an all-or-nothing parser. If we fail to
374 parse the instruction, we return 0 and the caller will start over from
375 the beginning. Backtracking will be necessary in parsing subexpressions,
376 but that can be handled there. Not handling backtracking here may get
377 expensive in the case of the m68k. Deal with later.
378
379 Returns NULL for success, an error message for failure.
380*/
381
382static const char *
383parse_insn_normal (cd, insn, strp, fields)
384 CGEN_CPU_DESC cd;
385 const CGEN_INSN *insn;
386 const char **strp;
387 CGEN_FIELDS *fields;
388{
389 /* ??? Runtime added insns not handled yet. */
390 const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
391 const char *str = *strp;
392 const char *errmsg;
393 const char *p;
394 const CGEN_SYNTAX_CHAR_TYPE * syn;
395#ifdef CGEN_MNEMONIC_OPERANDS
396 /* FIXME: wip */
397 int past_opcode_p;
398#endif
399
400 /* For now we assume the mnemonic is first (there are no leading operands).
401 We can parse it without needing to set up operand parsing.
402 GAS's input scrubber will ensure mnemonics are lowercase, but we may
403 not be called from GAS. */
404 p = CGEN_INSN_MNEMONIC (insn);
3882b010 405 while (*p && TOLOWER (*p) == TOLOWER (*str))
87e6d782
NC
406 ++p, ++str;
407
408 if (* p)
409 return _("unrecognized instruction");
410
411#ifndef CGEN_MNEMONIC_OPERANDS
3882b010 412 if (* str && !ISSPACE (* str))
87e6d782
NC
413 return _("unrecognized instruction");
414#endif
415
416 CGEN_INIT_PARSE (cd);
417 cgen_init_parse_operand (cd);
418#ifdef CGEN_MNEMONIC_OPERANDS
419 past_opcode_p = 0;
420#endif
421
422 /* We don't check for (*str != '\0') here because we want to parse
423 any trailing fake arguments in the syntax string. */
424 syn = CGEN_SYNTAX_STRING (syntax);
425
426 /* Mnemonics come first for now, ensure valid string. */
427 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
428 abort ();
429
430 ++syn;
431
432 while (* syn != 0)
433 {
434 /* Non operand chars must match exactly. */
435 if (CGEN_SYNTAX_CHAR_P (* syn))
436 {
437 /* FIXME: While we allow for non-GAS callers above, we assume the
438 first char after the mnemonic part is a space. */
439 /* FIXME: We also take inappropriate advantage of the fact that
440 GAS's input scrubber will remove extraneous blanks. */
3882b010 441 if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
87e6d782
NC
442 {
443#ifdef CGEN_MNEMONIC_OPERANDS
444 if (CGEN_SYNTAX_CHAR(* syn) == ' ')
445 past_opcode_p = 1;
446#endif
447 ++ syn;
448 ++ str;
449 }
450 else if (*str)
451 {
452 /* Syntax char didn't match. Can't be this insn. */
453 static char msg [80];
454 /* xgettext:c-format */
455 sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
456 CGEN_SYNTAX_CHAR(*syn), *str);
457 return msg;
458 }
459 else
460 {
461 /* Ran out of input. */
462 static char msg [80];
463 /* xgettext:c-format */
464 sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
465 CGEN_SYNTAX_CHAR(*syn));
466 return msg;
467 }
468 continue;
469 }
470
471 /* We have an operand of some sort. */
472 errmsg = openrisc_cgen_parse_operand (cd, CGEN_SYNTAX_FIELD (*syn),
473 &str, fields);
474 if (errmsg)
475 return errmsg;
476
477 /* Done with this operand, continue with next one. */
478 ++ syn;
479 }
480
481 /* If we're at the end of the syntax string, we're done. */
482 if (* syn == 0)
483 {
484 /* FIXME: For the moment we assume a valid `str' can only contain
485 blanks now. IE: We needn't try again with a longer version of
486 the insn and it is assumed that longer versions of insns appear
487 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
3882b010 488 while (ISSPACE (* str))
87e6d782
NC
489 ++ str;
490
491 if (* str != '\0')
492 return _("junk at end of line"); /* FIXME: would like to include `str' */
493
494 return NULL;
495 }
496
497 /* We couldn't parse it. */
498 return _("unrecognized instruction");
499}
500\f
501/* Main entry point.
502 This routine is called for each instruction to be assembled.
503 STR points to the insn to be assembled.
504 We assume all necessary tables have been initialized.
505 The assembled instruction, less any fixups, is stored in BUF.
506 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
507 still needs to be converted to target byte order, otherwise BUF is an array
508 of bytes in target byte order.
509 The result is a pointer to the insn's entry in the opcode table,
510 or NULL if an error occured (an error message will have already been
511 printed).
512
513 Note that when processing (non-alias) macro-insns,
514 this function recurses.
515
516 ??? It's possible to make this cpu-independent.
517 One would have to deal with a few minor things.
518 At this point in time doing so would be more of a curiosity than useful
519 [for example this file isn't _that_ big], but keeping the possibility in
520 mind helps keep the design clean. */
521
522const CGEN_INSN *
523openrisc_cgen_assemble_insn (cd, str, fields, buf, errmsg)
524 CGEN_CPU_DESC cd;
525 const char *str;
526 CGEN_FIELDS *fields;
527 CGEN_INSN_BYTES_PTR buf;
528 char **errmsg;
529{
530 const char *start;
531 CGEN_INSN_LIST *ilist;
532 const char *parse_errmsg = NULL;
533 const char *insert_errmsg = NULL;
fc7bc883 534 int recognized_mnemonic = 0;
87e6d782
NC
535
536 /* Skip leading white space. */
3882b010 537 while (ISSPACE (* str))
87e6d782
NC
538 ++ str;
539
540 /* The instructions are stored in hashed lists.
541 Get the first in the list. */
542 ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
543
544 /* Keep looking until we find a match. */
545
546 start = str;
547 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
548 {
549 const CGEN_INSN *insn = ilist->insn;
fc7bc883 550 recognized_mnemonic = 1;
87e6d782
NC
551
552#ifdef CGEN_VALIDATE_INSN_SUPPORTED
553 /* not usually needed as unsupported opcodes shouldn't be in the hash lists */
554 /* Is this insn supported by the selected cpu? */
555 if (! openrisc_cgen_insn_supported (cd, insn))
556 continue;
557#endif
558
559 /* If the RELAX attribute is set, this is an insn that shouldn't be
560 chosen immediately. Instead, it is used during assembler/linker
561 relaxation if possible. */
562 if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX) != 0)
563 continue;
564
565 str = start;
566
fc7bc883
RH
567 /* skip this insn if str doesn't look right lexically */
568 if (CGEN_INSN_RX (insn) != NULL &&
569 regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
570 continue;
571
87e6d782
NC
572 /* Allow parse/insert handlers to obtain length of insn. */
573 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
574
575 parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
576 if (parse_errmsg != NULL)
577 continue;
578
579 /* ??? 0 is passed for `pc' */
580 insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
581 (bfd_vma) 0);
582 if (insert_errmsg != NULL)
583 continue;
584
585 /* It is up to the caller to actually output the insn and any
586 queued relocs. */
587 return insn;
588 }
589
590 {
591 static char errbuf[150];
592#ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
593 const char *tmp_errmsg;
594
595 /* If requesting verbose error messages, use insert_errmsg.
596 Failing that, use parse_errmsg */
597 tmp_errmsg = (insert_errmsg ? insert_errmsg :
598 parse_errmsg ? parse_errmsg :
fc7bc883 599 recognized_mnemonic ? _("unrecognized form of instruction") :
87e6d782
NC
600 _("unrecognized instruction"));
601
602 if (strlen (start) > 50)
603 /* xgettext:c-format */
604 sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
605 else
606 /* xgettext:c-format */
607 sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
608#else
609 if (strlen (start) > 50)
610 /* xgettext:c-format */
611 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
612 else
613 /* xgettext:c-format */
614 sprintf (errbuf, _("bad instruction `%.50s'"), start);
615#endif
616
617 *errmsg = errbuf;
618 return NULL;
619 }
620}
621\f
622#if 0 /* This calls back to GAS which we can't do without care. */
623
624/* Record each member of OPVALS in the assembler's symbol table.
625 This lets GAS parse registers for us.
626 ??? Interesting idea but not currently used. */
627
628/* Record each member of OPVALS in the assembler's symbol table.
629 FIXME: Not currently used. */
630
631void
632openrisc_cgen_asm_hash_keywords (cd, opvals)
633 CGEN_CPU_DESC cd;
634 CGEN_KEYWORD *opvals;
635{
636 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
637 const CGEN_KEYWORD_ENTRY * ke;
638
639 while ((ke = cgen_keyword_search_next (& search)) != NULL)
640 {
641#if 0 /* Unnecessary, should be done in the search routine. */
642 if (! openrisc_cgen_opval_supported (ke))
643 continue;
644#endif
645 cgen_asm_record_register (cd, ke->name, ke->value);
646 }
647}
648
649#endif /* 0 */
This page took 0.087978 seconds and 4 git commands to generate.