2 Copyright 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 /*======================================================================*/
23 * Herein lies the support for dynamic specification of processor
24 * instructions and registers. Mnemonics, values, and formats for each
25 * instruction and register are specified in an ascii file consisting of
26 * table entries. The grammar for the table is defined in the document
27 * "Processor instruction table specification".
29 * Instructions use the gnu assembler syntax, with the addition of
30 * allowing mnemonics for register.
31 * Eg. "func $2,reg3,0x100,symbol ; comment"
34 * reg3 - mnemonic for processor's register defined in table
35 * 0xddd..d - immediate value
36 * symbol - address of label or external symbol
38 * First, itbl_parse reads in the table of register and instruction
39 * names and formats, and builds a list of entries for each
40 * processor/type combination. lex and yacc are used to parse
41 * the entries in the table and call functions defined here to
42 * add each entry to our list.
44 * Then, when assembling or disassembling, these functions are called to
45 * 1) get information on a processor's registers and
46 * 2) assemble/disassemble an instruction.
47 * To assemble(disassemble) an instruction, the function
48 * itbl_assemble(itbl_disassemble) is called to search the list of
49 * instruction entries, and if a match is found, uses the format
50 * described in the instruction entry structure to complete the action.
52 * Eg. Suppose we have a Mips coprocessor "cop3" with data register "d2"
53 * and we want to define function "pig" which takes two operands.
55 * Given the table entries:
56 * "p3 insn pig 0x1:24-21 dreg:20-16 immed:15-0"
58 * and that the instruction encoding for coprocessor pz has encoding:
59 * #define MIPS_ENCODE_COP_NUM(z) ((0x21|(z<<1))<<25)
60 * #define ITBL_ENCODE_PNUM(pnum) MIPS_ENCODE_COP_NUM(pnum)
62 * a structure to describe the instruction might look something like:
63 * struct itbl_entry = {
64 * e_processor processor = e_p3
65 * e_type type = e_insn
69 * struct itbl_range range = 24-21
70 * struct itbl_field *field = {
71 * e_type type = e_dreg
72 * struct itbl_range range = 20-16
73 * struct itbl_field *next = {
74 * e_type type = e_immed
75 * struct itbl_range range = 15-0
76 * struct itbl_field *next = 0
79 * struct itbl_entry *next = 0
82 * And the assembler instructions:
86 * would both assemble to the hex value:
95 #include <itbl-parse.h>
101 #define ASSERT(x) assert(x)
102 #define DBG(x) printf x
109 #define min(a,b) (a<b?a:b)
112 int itbl_have_entries = 0;
114 /*======================================================================*/
115 /* structures for keeping itbl format entries */
118 int sbit; /* mask starting bit position */
119 int ebit; /* mask ending bit position */
123 e_type type; /* dreg/creg/greg/immed/symb */
124 struct itbl_range range; /* field's bitfield range within instruction */
125 unsigned long flags; /* field flags */
126 struct itbl_field *next; /* next field in list */
129 /* These structures define the instructions and registers for a processor.
130 * If the type is an instruction, the structure defines the format of an
131 * instruction where the fields are the list of operands.
132 * The flags field below uses the same values as those defined in the
133 * gnu assembler and are machine specific. */
135 e_processor processor; /* processor number */
136 e_type type; /* dreg/creg/greg/insn */
137 char *name; /* mnemionic name for insn/register */
138 unsigned long value; /* opcode/instruction mask/register number */
139 unsigned long flags; /* effects of the instruction */
140 struct itbl_range range; /* bit range within instruction for value */
141 struct itbl_field *fields; /* list of operand definitions (if any) */
142 struct itbl_entry *next; /* next entry */
145 /* local data and structures */
147 static int itbl_num_opcodes = 0;
148 /* Array of entries for each processor and entry type */
149 static struct itbl_entry *entries[e_nprocs][e_ntypes] = {
156 /* local prototypes */
157 static unsigned long build_opcode PARAMS ((struct itbl_entry *e));
158 static e_type get_type PARAMS ((int yytype));
159 static e_processor get_processor PARAMS ((int yyproc));
160 static struct itbl_entry **get_entries PARAMS ((e_processor processor,
162 static struct itbl_entry *find_entry_byname PARAMS ((e_processor processor,
163 e_type type, char *name));
164 static struct itbl_entry *find_entry_byval PARAMS ((e_processor processor,
165 e_type type, unsigned long val, struct itbl_range *r));
166 static struct itbl_entry *alloc_entry PARAMS ((e_processor processor,
167 e_type type, char *name, unsigned long value));
168 static unsigned long apply_range PARAMS ((unsigned long value,
169 struct itbl_range r));
170 static unsigned long extract_range PARAMS ((unsigned long value,
171 struct itbl_range r));
172 static struct itbl_field *alloc_field PARAMS ((e_type type, int sbit,
173 int ebit, unsigned long flags));
175 /*======================================================================*/
176 /* Interfaces to the parser */
178 /* Open the table and use lex and yacc to parse the entries.
179 * Return 1 for failure; 0 for success. */
182 itbl_parse (char *insntbl)
185 extern int yyparse (void);
187 yyin = fopen (insntbl, FOPEN_RT);
190 printf ("Can't open processor instruction specification file \"%s\"\n",
199 itbl_have_entries = 1;
203 /* Add a register entry */
206 itbl_add_reg (int yyprocessor, int yytype, char *regname,
212 /* Since register names don't have a prefix, we put them in the symbol table so
213 they can't be used as symbols. This also simplifies argument parsing as
214 we can let gas parse registers for us. The recorded register number is
216 /* Use symbol_create here instead of symbol_new so we don't try to
217 output registers into the object file's symbol table. */
218 symbol_table_insert (symbol_create (regname, reg_section,
219 regnum, &zero_address_frag));
221 return alloc_entry (get_processor (yyprocessor), get_type (yytype), regname,
222 (unsigned long) regnum);
225 /* Add an instruction entry */
228 itbl_add_insn (int yyprocessor, char *name, unsigned long value,
229 int sbit, int ebit, unsigned long flags)
231 struct itbl_entry *e;
232 e = alloc_entry (get_processor (yyprocessor), e_insn, name, value);
235 e->range.sbit = sbit;
236 e->range.ebit = ebit;
243 /* Add an operand to an instruction entry */
246 itbl_add_operand (struct itbl_entry *e, int yytype, int sbit,
247 int ebit, unsigned long flags)
249 struct itbl_field *f, **last_f;
252 /* Add to end of fields' list. */
253 f = alloc_field (get_type (yytype), sbit, ebit, flags);
258 last_f = &(*last_f)->next;
265 /*======================================================================*/
266 /* Interfaces for assembler and disassembler */
271 static void append_insns_as_macros (void);
273 /* Initialize for gas. */
278 struct itbl_entry *e, **es;
282 if (!itbl_have_entries)
285 /* Since register names don't have a prefix, put them in the symbol table so
286 they can't be used as symbols. This simplifies argument parsing as
287 we can let gas parse registers for us. */
288 /* Use symbol_create instead of symbol_new so we don't try to
289 output registers into the object file's symbol table. */
291 for (type = e_regtype0; type < e_nregtypes; type++)
292 for (procn = e_p0; procn < e_nprocs; procn++)
294 es = get_entries (procn, type);
295 for (e = *es; e; e = e->next)
297 symbol_table_insert (symbol_create (e->name, reg_section,
298 e->value, &zero_address_frag));
301 append_insns_as_macros ();
304 /* Append insns to opcodes table and increase number of opcodes
305 * Structure of opcodes table:
309 * const char *args; - string describing the arguments.
310 * unsigned long match; - opcode, or ISA level if pinfo=INSN_MACRO
311 * unsigned long mask; - opcode mask, or macro id if pinfo=INSN_MACRO
312 * unsigned long pinfo; - insn flags, or INSN_MACRO
315 * {"li", "t,i", 0x34000000, 0xffe00000, WR_t },
316 * {"li", "t,I", 0, (int) M_LI, INSN_MACRO },
319 static char *form_args (struct itbl_entry *e);
321 append_insns_as_macros (void)
323 struct ITBL_OPCODE_STRUCT *new_opcodes, *o;
324 struct itbl_entry *e, **es;
325 int n, id, size, new_size, new_num_opcodes;
327 if (!itbl_have_entries)
330 if (!itbl_num_opcodes) /* no new instructions to add! */
334 DBG (("previous num_opcodes=%d\n", ITBL_NUM_OPCODES));
336 new_num_opcodes = ITBL_NUM_OPCODES + itbl_num_opcodes;
337 ASSERT (new_num_opcodes >= itbl_num_opcodes);
339 size = sizeof (struct ITBL_OPCODE_STRUCT) * ITBL_NUM_OPCODES;
341 DBG (("I get=%d\n", size / sizeof (ITBL_OPCODES[0])));
343 new_size = sizeof (struct ITBL_OPCODE_STRUCT) * new_num_opcodes;
344 ASSERT (new_size > size);
346 /* FIXME since ITBL_OPCODES culd be a static table,
347 we can't realloc or delete the old memory. */
348 new_opcodes = (struct ITBL_OPCODE_STRUCT *) malloc (new_size);
351 printf (_("Unable to allocate memory for new instructions\n"));
354 if (size) /* copy preexisting opcodes table */
355 memcpy (new_opcodes, ITBL_OPCODES, size);
357 /* FIXME! some NUMOPCODES are calculated expressions.
358 These need to be changed before itbls can be supported. */
360 id = ITBL_NUM_MACROS; /* begin the next macro id after the last */
361 o = &new_opcodes[ITBL_NUM_OPCODES]; /* append macro to opcodes list */
362 for (n = e_p0; n < e_nprocs; n++)
364 es = get_entries (n, e_insn);
365 for (e = *es; e; e = e->next)
367 /* name, args, mask, match, pinfo
368 * {"li", "t,i", 0x34000000, 0xffe00000, WR_t },
369 * {"li", "t,I", 0, (int) M_LI, INSN_MACRO },
370 * Construct args from itbl_fields.
373 o->args = strdup (form_args (e));
374 o->mask = apply_range (e->value, e->range);
375 /* FIXME how to catch during assembly? */
376 /* mask to identify this insn */
377 o->match = apply_range (e->value, e->range);
381 o->mask = id++; /* FIXME how to catch during assembly? */
382 o->match = 0; /* for macros, the insn_isa number */
383 o->pinfo = INSN_MACRO;
386 /* Don't add instructions which caused an error */
393 ITBL_OPCODES = new_opcodes;
394 ITBL_NUM_OPCODES = new_num_opcodes;
397 At this point, we can free the entries, as they should have
398 been added to the assembler's tables.
399 Don't free name though, since name is being used by the new
402 Eventually, we should also free the new opcodes table itself
408 form_args (struct itbl_entry *e)
412 struct itbl_field *f;
415 for (f = e->fields; f; f = f->next)
435 c = 0; /* ignore; unknown field type */
447 #endif /* !STAND_ALONE */
449 /* Get processor's register name from val */
452 itbl_get_reg_val (char *name, unsigned long *pval)
457 for (p = e_p0; p < e_nprocs; p++)
459 for (t = e_regtype0; t < e_nregtypes; t++)
461 if (itbl_get_val (p, t, name, pval))
469 itbl_get_name (e_processor processor, e_type type, unsigned long val)
471 struct itbl_entry *r;
472 /* type depends on instruction passed */
473 r = find_entry_byval (processor, type, val, 0);
477 return 0; /* error; invalid operand */
480 /* Get processor's register value from name */
483 itbl_get_val (e_processor processor, e_type type, char *name,
486 struct itbl_entry *r;
487 /* type depends on instruction passed */
488 r = find_entry_byname (processor, type, name);
495 /* Assemble instruction "name" with operands "s".
496 * name - name of instruction
498 * returns - long word for assembled instruction */
501 itbl_assemble (char *name, char *s)
503 unsigned long opcode;
504 struct itbl_entry *e = NULL;
505 struct itbl_field *f;
510 return 0; /* error! must have an opcode name/expr */
512 /* find entry in list of instructions for all processors */
513 for (processor = 0; processor < e_nprocs; processor++)
515 e = find_entry_byname (processor, e_insn, name);
520 return 0; /* opcode not in table; invalid instruction */
521 opcode = build_opcode (e);
523 /* parse opcode's args (if any) */
524 for (f = e->fields; f; f = f->next) /* for each arg, ... */
526 struct itbl_entry *r;
529 return 0; /* error - not enough operands */
530 n = itbl_get_field (&s);
531 /* n should be in form $n or 0xhhh (are symbol names valid?? */
537 /* Accept either a string name
538 * or '$' followed by the register number */
542 value = strtol (n, 0, 10);
543 /* FIXME! could have "0l"... then what?? */
544 if (value == 0 && *n != '0')
545 return 0; /* error; invalid operand */
549 r = find_entry_byname (e->processor, f->type, n);
553 return 0; /* error; invalid operand */
557 /* use assembler's symbol table to find symbol */
558 /* FIXME!! Do we need this?
559 if so, what about relocs??
560 my_getExpression (&imm_expr, s);
561 return 0; /-* error; invalid operand *-/
564 /* If not a symbol, fall thru to IMMED */
566 if (*n == '0' && *(n + 1) == 'x') /* hex begins 0x... */
569 value = strtol (n, 0, 16);
570 /* FIXME! could have "0xl"... then what?? */
574 value = strtol (n, 0, 10);
575 /* FIXME! could have "0l"... then what?? */
576 if (value == 0 && *n != '0')
577 return 0; /* error; invalid operand */
581 return 0; /* error; invalid field spec */
583 opcode |= apply_range (value, f->range);
586 return 0; /* error - too many operands */
587 return opcode; /* done! */
590 /* Disassemble instruction "insn".
592 * s - buffer to hold disassembled instruction
593 * returns - 1 if succeeded; 0 if failed
597 itbl_disassemble (char *s, unsigned long insn)
599 e_processor processor;
600 struct itbl_entry *e;
601 struct itbl_field *f;
603 if (!ITBL_IS_INSN (insn))
604 return 0; /* error */
605 processor = get_processor (ITBL_DECODE_PNUM (insn));
607 /* find entry in list */
608 e = find_entry_byval (processor, e_insn, insn, 0);
610 return 0; /* opcode not in table; invalid instruction */
613 /* Parse insn's args (if any). */
614 for (f = e->fields; f; f = f->next) /* for each arg, ... */
616 struct itbl_entry *r;
619 if (f == e->fields) /* First operand is preceded by tab. */
621 else /* ','s separate following operands. */
623 value = extract_range (insn, f->range);
624 /* n should be in form $n or 0xhhh (are symbol names valid?? */
630 /* Accept either a string name
631 or '$' followed by the register number. */
632 r = find_entry_byval (e->processor, f->type, value, &f->range);
636 sprintf (s, "%s$%lu", s, value);
639 /* Use assembler's symbol table to find symbol. */
640 /* FIXME!! Do we need this? If so, what about relocs?? */
641 /* If not a symbol, fall through to IMMED. */
643 sprintf (s, "%s0x%lx", s, value);
646 return 0; /* error; invalid field spec */
649 return 1; /* Done! */
652 /*======================================================================*/
654 * Local functions for manipulating private structures containing
655 * the names and format for the new instructions and registers
656 * for each processor.
659 /* Calculate instruction's opcode and function values from entry */
662 build_opcode (struct itbl_entry *e)
664 unsigned long opcode;
666 opcode = apply_range (e->value, e->range);
667 opcode |= ITBL_ENCODE_PNUM (e->processor);
671 /* Calculate absolute value given the relative value and bit position range
672 * within the instruction.
673 * The range is inclusive where 0 is least significant bit.
674 * A range of { 24, 20 } will have a mask of
676 * pos: 1098 7654 3210 9876 5432 1098 7654 3210
677 * bin: 0000 0001 1111 0000 0000 0000 0000 0000
678 * hex: 0 1 f 0 0 0 0 0
683 apply_range (unsigned long rval, struct itbl_range r)
687 int len = MAX_BITPOS - r.sbit;
689 ASSERT (r.sbit >= r.ebit);
690 ASSERT (MAX_BITPOS >= r.sbit);
691 ASSERT (r.ebit >= 0);
693 /* create mask by truncating 1s by shifting */
694 mask = 0xffffffff << len;
696 mask = mask >> r.ebit;
697 mask = mask << r.ebit;
699 aval = (rval << r.ebit) & mask;
703 /* Calculate relative value given the absolute value and bit position range
704 * within the instruction. */
707 extract_range (unsigned long aval, struct itbl_range r)
711 int len = MAX_BITPOS - r.sbit;
713 /* create mask by truncating 1s by shifting */
714 mask = 0xffffffff << len;
716 mask = mask >> r.ebit;
717 mask = mask << r.ebit;
719 rval = (aval & mask) >> r.ebit;
723 /* Extract processor's assembly instruction field name from s;
724 * forms are "n args" "n,args" or "n" */
725 /* Return next argument from string pointer "s" and advance s.
726 * delimiters are " ,()" */
729 itbl_get_field (char **S)
738 /* FIXME: This is a weird set of delimiters. */
739 len = strcspn (s, " \t,()");
740 ASSERT (128 > len + 1);
744 s = 0; /* no more args */
746 s += len + 1; /* advance to next arg */
752 /* Search entries for a given processor and type
753 * to find one matching the name "n".
754 * Return a pointer to the entry */
756 static struct itbl_entry *
757 find_entry_byname (e_processor processor,
758 e_type type, char *n)
760 struct itbl_entry *e, **es;
762 es = get_entries (processor, type);
763 for (e = *es; e; e = e->next) /* for each entry, ... */
765 if (!strcmp (e->name, n))
771 /* Search entries for a given processor and type
772 * to find one matching the value "val" for the range "r".
773 * Return a pointer to the entry.
774 * This function is used for disassembling fields of an instruction.
777 static struct itbl_entry *
778 find_entry_byval (e_processor processor, e_type type,
779 unsigned long val, struct itbl_range *r)
781 struct itbl_entry *e, **es;
784 es = get_entries (processor, type);
785 for (e = *es; e; e = e->next) /* for each entry, ... */
787 if (processor != e->processor)
789 /* For insns, we might not know the range of the opcode,
790 * so a range of 0 will allow this routine to match against
791 * the range of the entry to be compared with.
792 * This could cause ambiguities.
793 * For operands, we get an extracted value and a range.
795 /* if range is 0, mask val against the range of the compared entry. */
796 if (r == 0) /* if no range passed, must be whole 32-bits
797 * so create 32-bit value from entry's range */
799 eval = apply_range (e->value, e->range);
800 val &= apply_range (0xffffffff, e->range);
802 else if ((r->sbit == e->range.sbit && r->ebit == e->range.ebit)
803 || (e->range.sbit == 0 && e->range.ebit == 0))
805 eval = apply_range (e->value, *r);
806 val = apply_range (val, *r);
816 /* Return a pointer to the list of entries for a given processor and type. */
818 static struct itbl_entry **
819 get_entries (e_processor processor, e_type type)
821 return &entries[processor][type];
824 /* Return an integral value for the processor passed from yyparse. */
827 get_processor (int yyproc)
829 /* translate from yacc's processor to enum */
830 if (yyproc >= e_p0 && yyproc < e_nprocs)
831 return (e_processor) yyproc;
832 return e_invproc; /* error; invalid processor */
835 /* Return an integral value for the entry type passed from yyparse. */
838 get_type (int yytype)
842 /* translate from yacc's type to enum */
856 return e_invtype; /* error; invalid type */
860 /* Allocate and initialize an entry */
862 static struct itbl_entry *
863 alloc_entry (e_processor processor, e_type type,
864 char *name, unsigned long value)
866 struct itbl_entry *e, **es;
869 e = (struct itbl_entry *) malloc (sizeof (struct itbl_entry));
872 memset (e, 0, sizeof (struct itbl_entry));
873 e->name = (char *) malloc (sizeof (strlen (name)) + 1);
875 strcpy (e->name, name);
876 e->processor = processor;
879 es = get_entries (e->processor, e->type);
886 /* Allocate and initialize an entry's field */
888 static struct itbl_field *
889 alloc_field (e_type type, int sbit, int ebit,
892 struct itbl_field *f;
893 f = (struct itbl_field *) malloc (sizeof (struct itbl_field));
896 memset (f, 0, sizeof (struct itbl_field));
898 f->range.sbit = sbit;
899 f->range.ebit = ebit;