2 Copyright (C) 1997-2024 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 3, 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, 51 Franklin Street - Fifth Floor, 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:
93 #include <itbl-parse.h>
99 #define ASSERT(x) gas_assert (x)
100 #define DBG(x) printf x
107 #define min(a,b) (a<b?a:b)
110 int itbl_have_entries
= 0;
112 /*======================================================================*/
113 /* structures for keeping itbl format entries */
116 int sbit
; /* mask starting bit position */
117 int ebit
; /* mask ending bit position */
121 e_type type
; /* dreg/creg/greg/immed/symb */
122 struct itbl_range range
; /* field's bitfield range within instruction */
123 unsigned long flags
; /* field flags */
124 struct itbl_field
*next
; /* next field in list */
127 /* These structures define the instructions and registers for a processor.
128 * If the type is an instruction, the structure defines the format of an
129 * instruction where the fields are the list of operands.
130 * The flags field below uses the same values as those defined in the
131 * gnu assembler and are machine specific. */
133 e_processor processor
; /* processor number */
134 e_type type
; /* dreg/creg/greg/insn */
135 char *name
; /* mnemonic name for insn/register */
136 unsigned long value
; /* opcode/instruction mask/register number */
137 unsigned long flags
; /* effects of the instruction */
138 struct itbl_range range
; /* bit range within instruction for value */
139 struct itbl_field
*fields
; /* list of operand definitions (if any) */
140 struct itbl_entry
*next
; /* next entry */
143 /* local data and structures */
145 static int itbl_num_opcodes
= 0;
146 /* Array of entries for each processor and entry type */
147 static struct itbl_entry
*entries
[e_nprocs
][e_ntypes
];
149 /* local prototypes */
150 static unsigned long build_opcode (struct itbl_entry
*e
);
151 static e_type
get_type (int yytype
);
152 static e_processor
get_processor (int yyproc
);
153 static struct itbl_entry
**get_entries (e_processor processor
,
155 static struct itbl_entry
*find_entry_byname (e_processor processor
,
156 e_type type
, char *name
);
157 static struct itbl_entry
*find_entry_byval (e_processor processor
,
158 e_type type
, unsigned long val
, struct itbl_range
*r
);
159 static struct itbl_entry
*alloc_entry (e_processor processor
,
160 e_type type
, char *name
, unsigned long value
);
161 static unsigned long apply_range (unsigned long value
, struct itbl_range r
);
162 static unsigned long extract_range (unsigned long value
, struct itbl_range r
);
163 static struct itbl_field
*alloc_field (e_type type
, int sbit
,
164 int ebit
, unsigned long flags
);
166 /*======================================================================*/
167 /* Interfaces to the parser */
169 /* Open the table and use lex and yacc to parse the entries.
170 * Return 1 for failure; 0 for success. */
173 itbl_parse (char *insntbl
)
176 extern int yyparse (void);
178 yyin
= fopen (insntbl
, FOPEN_RT
);
181 printf ("Can't open processor instruction specification file \"%s\"\n",
190 itbl_have_entries
= 1;
194 /* Add a register entry */
197 itbl_add_reg (int yyprocessor
, int yytype
, char *regname
,
200 return alloc_entry (get_processor (yyprocessor
), get_type (yytype
), regname
,
201 (unsigned long) regnum
);
204 /* Add an instruction entry */
207 itbl_add_insn (int yyprocessor
, char *name
, unsigned long value
,
208 int sbit
, int ebit
, unsigned long flags
)
210 struct itbl_entry
*e
;
211 e
= alloc_entry (get_processor (yyprocessor
), e_insn
, name
, value
);
214 e
->range
.sbit
= sbit
;
215 e
->range
.ebit
= ebit
;
222 /* Add an operand to an instruction entry */
225 itbl_add_operand (struct itbl_entry
*e
, int yytype
, int sbit
,
226 int ebit
, unsigned long flags
)
228 struct itbl_field
*f
, **last_f
;
231 /* Add to end of fields' list. */
232 f
= alloc_field (get_type (yytype
), sbit
, ebit
, flags
);
237 last_f
= &(*last_f
)->next
;
244 /*======================================================================*/
245 /* Interfaces for assembler and disassembler */
248 static void append_insns_as_macros (void);
250 /* Initialize for gas. */
255 struct itbl_entry
*e
, **es
;
259 if (!itbl_have_entries
)
262 /* Since register names don't have a prefix, put them in the symbol table so
263 they can't be used as symbols. This simplifies argument parsing as
264 we can let gas parse registers for us. */
265 /* Use symbol_create instead of symbol_new so we don't try to
266 output registers into the object file's symbol table. */
268 for (type
= e_regtype0
; type
< e_nregtypes
; type
++)
269 for (procn
= e_p0
; procn
< e_nprocs
; procn
++)
271 es
= get_entries (procn
, type
);
272 for (e
= *es
; e
; e
= e
->next
)
274 symbol_table_insert (symbol_create (e
->name
, reg_section
,
275 &zero_address_frag
, e
->value
));
278 append_insns_as_macros ();
281 /* Append insns to opcodes table and increase number of opcodes
282 * Structure of opcodes table:
286 * const char *args; - string describing the arguments.
287 * unsigned long match; - opcode, or ISA level if pinfo=INSN_MACRO
288 * unsigned long mask; - opcode mask, or macro id if pinfo=INSN_MACRO
289 * unsigned long pinfo; - insn flags, or INSN_MACRO
292 * {"li", "t,i", 0x34000000, 0xffe00000, WR_t },
293 * {"li", "t,I", 0, (int) M_LI, INSN_MACRO },
296 static char *form_args (struct itbl_entry
*e
);
298 append_insns_as_macros (void)
300 struct ITBL_OPCODE_STRUCT
*new_opcodes
, *o
;
301 struct itbl_entry
*e
, **es
;
302 int n
, size
, new_num_opcodes
;
307 if (!itbl_have_entries
)
310 if (!itbl_num_opcodes
) /* no new instructions to add! */
314 DBG (("previous num_opcodes=%d\n", ITBL_NUM_OPCODES
));
316 new_num_opcodes
= ITBL_NUM_OPCODES
+ itbl_num_opcodes
;
317 ASSERT (new_num_opcodes
>= itbl_num_opcodes
);
319 size
= sizeof (struct ITBL_OPCODE_STRUCT
) * ITBL_NUM_OPCODES
;
321 DBG (("I get=%d\n", size
/ sizeof (ITBL_OPCODES
[0])));
323 /* FIXME since ITBL_OPCODES could be a static table,
324 we can't realloc or delete the old memory. */
325 new_opcodes
= XNEWVEC (struct ITBL_OPCODE_STRUCT
, new_num_opcodes
);
328 printf (_("Unable to allocate memory for new instructions\n"));
331 if (size
) /* copy preexisting opcodes table */
332 memcpy (new_opcodes
, ITBL_OPCODES
, size
);
334 /* FIXME! some NUMOPCODES are calculated expressions.
335 These need to be changed before itbls can be supported. */
338 id
= ITBL_NUM_MACROS
; /* begin the next macro id after the last */
340 o
= &new_opcodes
[ITBL_NUM_OPCODES
]; /* append macro to opcodes list */
341 for (n
= e_p0
; n
< e_nprocs
; n
++)
343 es
= get_entries (n
, e_insn
);
344 for (e
= *es
; e
; e
= e
->next
)
346 /* name, args, mask, match, pinfo
347 * {"li", "t,i", 0x34000000, 0xffe00000, WR_t },
348 * {"li", "t,I", 0, (int) M_LI, INSN_MACRO },
349 * Construct args from itbl_fields.
352 o
->args
= strdup (form_args (e
));
353 o
->mask
= apply_range (e
->value
, e
->range
);
354 /* FIXME how to catch during assembly? */
355 /* mask to identify this insn */
356 o
->match
= apply_range (e
->value
, e
->range
);
360 o
->mask
= id
++; /* FIXME how to catch during assembly? */
361 o
->match
= 0; /* for macros, the insn_isa number */
362 o
->pinfo
= INSN_MACRO
;
365 /* Don't add instructions which caused an error */
372 ITBL_OPCODES
= new_opcodes
;
373 ITBL_NUM_OPCODES
= new_num_opcodes
;
376 At this point, we can free the entries, as they should have
377 been added to the assembler's tables.
378 Don't free name though, since name is being used by the new
381 Eventually, we should also free the new opcodes table itself
387 form_args (struct itbl_entry
*e
)
391 struct itbl_field
*f
;
394 for (f
= e
->fields
; f
; f
= f
->next
)
414 c
= 0; /* ignore; unknown field type */
426 #endif /* !STAND_ALONE */
428 /* Get processor's register name from val */
431 itbl_get_reg_val (char *name
, unsigned long *pval
)
436 for (p
= e_p0
; p
< e_nprocs
; p
++)
438 for (t
= e_regtype0
; t
< e_nregtypes
; t
++)
440 if (itbl_get_val (p
, t
, name
, pval
))
448 itbl_get_name (e_processor processor
, e_type type
, unsigned long val
)
450 struct itbl_entry
*r
;
451 /* type depends on instruction passed */
452 r
= find_entry_byval (processor
, type
, val
, 0);
456 return 0; /* error; invalid operand */
459 /* Get processor's register value from name */
462 itbl_get_val (e_processor processor
, e_type type
, char *name
,
465 struct itbl_entry
*r
;
466 /* type depends on instruction passed */
467 r
= find_entry_byname (processor
, type
, name
);
474 /* Assemble instruction "name" with operands "s".
475 * name - name of instruction
477 * returns - long word for assembled instruction */
480 itbl_assemble (char *name
, char *s
)
482 unsigned long opcode
;
483 struct itbl_entry
*e
= NULL
;
484 struct itbl_field
*f
;
489 return 0; /* error! must have an opcode name/expr */
491 /* find entry in list of instructions for all processors */
492 for (processor
= 0; processor
< e_nprocs
; processor
++)
494 e
= find_entry_byname (processor
, e_insn
, name
);
499 return 0; /* opcode not in table; invalid instruction */
500 opcode
= build_opcode (e
);
502 /* parse opcode's args (if any) */
503 for (f
= e
->fields
; f
; f
= f
->next
) /* for each arg, ... */
505 struct itbl_entry
*r
;
508 return 0; /* error - not enough operands */
509 n
= itbl_get_field (&s
);
510 /* n should be in form $n or 0xhhh (are symbol names valid?? */
516 /* Accept either a string name
517 * or '$' followed by the register number */
521 value
= strtol (n
, 0, 10);
522 /* FIXME! could have "0l"... then what?? */
523 if (value
== 0 && *n
!= '0')
524 return 0; /* error; invalid operand */
528 r
= find_entry_byname (e
->processor
, f
->type
, n
);
532 return 0; /* error; invalid operand */
536 /* use assembler's symbol table to find symbol */
537 /* FIXME!! Do we need this?
538 if so, what about relocs??
539 my_getExpression (&imm_expr, s);
540 return 0; /-* error; invalid operand *-/
543 /* If not a symbol, fallthru to IMMED */
545 if (*n
== '0' && *(n
+ 1) == 'x') /* hex begins 0x... */
548 value
= strtol (n
, 0, 16);
549 /* FIXME! could have "0xl"... then what?? */
553 value
= strtol (n
, 0, 10);
554 /* FIXME! could have "0l"... then what?? */
555 if (value
== 0 && *n
!= '0')
556 return 0; /* error; invalid operand */
560 return 0; /* error; invalid field spec */
562 opcode
|= apply_range (value
, f
->range
);
565 return 0; /* error - too many operands */
566 return opcode
; /* done! */
569 /* Disassemble instruction "insn".
571 * s - buffer to hold disassembled instruction
572 * returns - 1 if succeeded; 0 if failed
576 itbl_disassemble (char *s
, unsigned long insn
)
578 e_processor processor
;
579 struct itbl_entry
*e
;
580 struct itbl_field
*f
;
582 if (!ITBL_IS_INSN (insn
))
583 return 0; /* error */
584 processor
= get_processor (ITBL_DECODE_PNUM (insn
));
586 /* find entry in list */
587 e
= find_entry_byval (processor
, e_insn
, insn
, 0);
589 return 0; /* opcode not in table; invalid instruction */
592 /* Parse insn's args (if any). */
593 for (f
= e
->fields
; f
; f
= f
->next
) /* for each arg, ... */
595 struct itbl_entry
*r
;
599 if (f
== e
->fields
) /* First operand is preceded by tab. */
601 else /* ','s separate following operands. */
603 value
= extract_range (insn
, f
->range
);
604 /* n should be in form $n or 0xhhh (are symbol names valid?? */
610 /* Accept either a string name
611 or '$' followed by the register number. */
612 r
= find_entry_byval (e
->processor
, f
->type
, value
, &f
->range
);
617 sprintf (s_value
, "$%lu", value
);
622 /* Use assembler's symbol table to find symbol. */
623 /* FIXME!! Do we need this? If so, what about relocs?? */
624 /* If not a symbol, fall through to IMMED. */
626 sprintf (s_value
, "0x%lx", value
);
630 return 0; /* error; invalid field spec */
633 return 1; /* Done! */
636 /*======================================================================*/
638 * Local functions for manipulating private structures containing
639 * the names and format for the new instructions and registers
640 * for each processor.
643 /* Calculate instruction's opcode and function values from entry */
646 build_opcode (struct itbl_entry
*e
)
648 unsigned long opcode
;
650 opcode
= apply_range (e
->value
, e
->range
);
651 opcode
|= ITBL_ENCODE_PNUM (e
->processor
);
655 /* Calculate absolute value given the relative value and bit position range
656 * within the instruction.
657 * The range is inclusive where 0 is least significant bit.
658 * A range of { 24, 20 } will have a mask of
660 * pos: 1098 7654 3210 9876 5432 1098 7654 3210
661 * bin: 0000 0001 1111 0000 0000 0000 0000 0000
662 * hex: 0 1 f 0 0 0 0 0
667 apply_range (unsigned long rval
, struct itbl_range r
)
671 int len
= MAX_BITPOS
- r
.sbit
;
673 ASSERT (r
.sbit
>= r
.ebit
);
674 ASSERT (MAX_BITPOS
>= r
.sbit
);
675 ASSERT (r
.ebit
>= 0);
677 /* create mask by truncating 1s by shifting */
678 mask
= 0xffffffff << len
;
680 mask
= mask
>> r
.ebit
;
681 mask
= mask
<< r
.ebit
;
683 aval
= (rval
<< r
.ebit
) & mask
;
687 /* Calculate relative value given the absolute value and bit position range
688 * within the instruction. */
691 extract_range (unsigned long aval
, struct itbl_range r
)
695 int len
= MAX_BITPOS
- r
.sbit
;
697 /* create mask by truncating 1s by shifting */
698 mask
= 0xffffffff << len
;
700 mask
= mask
>> r
.ebit
;
701 mask
= mask
<< r
.ebit
;
703 rval
= (aval
& mask
) >> r
.ebit
;
707 /* Extract processor's assembly instruction field name from s;
708 * forms are "n args" "n,args" or "n" */
709 /* Return next argument from string pointer "s" and advance s.
710 * delimiters are " ,()" */
713 itbl_get_field (char **S
)
722 /* FIXME: This is a weird set of delimiters. */
723 len
= strcspn (s
, " \t,()");
724 ASSERT (128 > len
+ 1);
728 s
= 0; /* no more args */
730 s
+= len
+ 1; /* advance to next arg */
736 /* Search entries for a given processor and type
737 * to find one matching the name "n".
738 * Return a pointer to the entry */
740 static struct itbl_entry
*
741 find_entry_byname (e_processor processor
,
742 e_type type
, char *n
)
744 struct itbl_entry
*e
, **es
;
746 es
= get_entries (processor
, type
);
747 for (e
= *es
; e
; e
= e
->next
) /* for each entry, ... */
749 if (!strcmp (e
->name
, n
))
755 /* Search entries for a given processor and type
756 * to find one matching the value "val" for the range "r".
757 * Return a pointer to the entry.
758 * This function is used for disassembling fields of an instruction.
761 static struct itbl_entry
*
762 find_entry_byval (e_processor processor
, e_type type
,
763 unsigned long val
, struct itbl_range
*r
)
765 struct itbl_entry
*e
, **es
;
768 es
= get_entries (processor
, type
);
769 for (e
= *es
; e
; e
= e
->next
) /* for each entry, ... */
771 if (processor
!= e
->processor
)
773 /* For insns, we might not know the range of the opcode,
774 * so a range of 0 will allow this routine to match against
775 * the range of the entry to be compared with.
776 * This could cause ambiguities.
777 * For operands, we get an extracted value and a range.
779 /* if range is 0, mask val against the range of the compared entry. */
780 if (r
== 0) /* if no range passed, must be whole 32-bits
781 * so create 32-bit value from entry's range */
783 eval
= apply_range (e
->value
, e
->range
);
784 val
&= apply_range (0xffffffff, e
->range
);
786 else if ((r
->sbit
== e
->range
.sbit
&& r
->ebit
== e
->range
.ebit
)
787 || (e
->range
.sbit
== 0 && e
->range
.ebit
== 0))
789 eval
= apply_range (e
->value
, *r
);
790 val
= apply_range (val
, *r
);
800 /* Return a pointer to the list of entries for a given processor and type. */
802 static struct itbl_entry
**
803 get_entries (e_processor processor
, e_type type
)
805 return &entries
[processor
][type
];
808 /* Return an integral value for the processor passed from yyparse. */
811 get_processor (int yyproc
)
813 /* translate from yacc's processor to enum */
814 if (yyproc
>= e_p0
&& yyproc
< e_nprocs
)
815 return (e_processor
) yyproc
;
816 return e_invproc
; /* error; invalid processor */
819 /* Return an integral value for the entry type passed from yyparse. */
822 get_type (int yytype
)
826 /* translate from yacc's type to enum */
840 return e_invtype
; /* error; invalid type */
844 /* Allocate and initialize an entry */
846 static struct itbl_entry
*
847 alloc_entry (e_processor processor
, e_type type
,
848 char *name
, unsigned long value
)
850 struct itbl_entry
*e
, **es
;
853 e
= XNEW (struct itbl_entry
);
856 memset (e
, 0, sizeof (struct itbl_entry
));
857 e
->name
= xstrdup (name
);
858 e
->processor
= processor
;
861 es
= get_entries (e
->processor
, e
->type
);
868 /* Allocate and initialize an entry's field */
870 static struct itbl_field
*
871 alloc_field (e_type type
, int sbit
, int ebit
,
874 struct itbl_field
*f
;
875 f
= XNEW (struct itbl_field
);
878 memset (f
, 0, sizeof (struct itbl_field
));
880 f
->range
.sbit
= sbit
;
881 f
->range
.ebit
= ebit
;