• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Header file for targets using CGEN: Cpu tools GENerator.
2 
3    Copyright (C) 1996-2014 Free Software Foundation, Inc.
4 
5    This file is part of GDB, the GNU debugger, and the GNU Binutils.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License along
18    with this program; if not, write to the Free Software Foundation, Inc.,
19    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20 
21 #ifndef OPCODE_CGEN_H
22 #define OPCODE_CGEN_H
23 
24 #include "symcat.h"
25 #include "cgen/bitset.h"
26 
27 /* ??? IWBN to replace bfd in the name.  */
28 #include "bfd_stdint.h"
29 
30 /* ??? This file requires bfd.h but only to get bfd_vma.
31    Seems like an awful lot to require just to get such a fundamental type.
32    Perhaps the definition of bfd_vma can be moved outside of bfd.h.
33    Or perhaps one could duplicate its definition in another file.
34    Until such time, this file conditionally compiles definitions that require
35    bfd_vma using __BFD_H_SEEN__.  */
36 
37 /* Enums must be defined before they can be used.
38    Allow them to be used in struct definitions, even though the enum must
39    be defined elsewhere.
40    If CGEN_ARCH isn't defined, this file is being included by something other
41    than <arch>-desc.h.  */
42 
43 /* Prepend the arch name, defined in <arch>-desc.h, and _cgen_ to symbol S.
44    The lack of spaces in the arg list is important for non-stdc systems.
45    This file is included by <arch>-desc.h.
46    It can be included independently of <arch>-desc.h, in which case the arch
47    dependent portions will be declared as "unknown_cgen_foo".  */
48 
49 #ifndef CGEN_SYM
50 #define CGEN_SYM(s) CONCAT3 (unknown,_cgen_,s)
51 #endif
52 
53 /* This file contains the static (unchanging) pieces and as much other stuff
54    as we can reasonably put here.  It's generally cleaner to put stuff here
55    rather than having it machine generated if possible.  */
56 
57 /* The assembler syntax is made up of expressions (duh...).
58    At the lowest level the values are mnemonics, register names, numbers, etc.
59    Above that are subexpressions, if any (an example might be the
60    "effective address" in m68k cpus).  Subexpressions are wip.
61    At the second highest level are the insns themselves.  Above that are
62    pseudo-insns, synthetic insns, and macros, if any.  */
63 
64 /* Lots of cpu's have a fixed insn size, or one which rarely changes,
65    and it's generally easier to handle these by treating the insn as an
66    integer type, rather than an array of characters.  So we allow targets
67    to control this.  When an integer type the value is in host byte order,
68    when an array of characters the value is in target byte order.  */
69 
70 typedef unsigned int CGEN_INSN_INT;
71 typedef int64_t CGEN_INSN_LGSINT; /* large/long SINT */
72 typedef uint64_t CGEN_INSN_LGUINT; /* large/long UINT */
73 
74 #if CGEN_INT_INSN_P
75 typedef CGEN_INSN_INT CGEN_INSN_BYTES;
76 typedef CGEN_INSN_INT *CGEN_INSN_BYTES_PTR;
77 #else
78 typedef unsigned char *CGEN_INSN_BYTES;
79 typedef unsigned char *CGEN_INSN_BYTES_PTR;
80 #endif
81 
82 #ifdef __GNUC__
83 #define CGEN_INLINE __inline__
84 #else
85 #define CGEN_INLINE
86 #endif
87 
88 enum cgen_endian
89 {
90   CGEN_ENDIAN_UNKNOWN,
91   CGEN_ENDIAN_LITTLE,
92   CGEN_ENDIAN_BIG
93 };
94 
95 /* Forward decl.  */
96 
97 typedef struct cgen_insn CGEN_INSN;
98 
99 /* Opaque pointer version for use by external world.  */
100 
101 typedef struct cgen_cpu_desc *CGEN_CPU_DESC;
102 
103 /* Attributes.
104    Attributes are used to describe various random things associated with
105    an object (ifield, hardware, operand, insn, whatever) and are specified
106    as name/value pairs.
107    Integer attributes computed at compile time are currently all that's
108    supported, though adding string attributes and run-time computation is
109    straightforward.  Integer attribute values are always host int's
110    (signed or unsigned).  For portability, this means 32 bits.
111    Integer attributes are further categorized as boolean, bitset, integer,
112    and enum types.  Boolean attributes appear frequently enough that they're
113    recorded in one host int.  This limits the maximum number of boolean
114    attributes to 32, though that's a *lot* of attributes.  */
115 
116 /* Type of attribute values.  */
117 
118 typedef CGEN_BITSET     CGEN_ATTR_VALUE_BITSET_TYPE;
119 typedef int             CGEN_ATTR_VALUE_ENUM_TYPE;
120 typedef union
121 {
122   CGEN_ATTR_VALUE_BITSET_TYPE bitset;
123   CGEN_ATTR_VALUE_ENUM_TYPE   nonbitset;
124 } CGEN_ATTR_VALUE_TYPE;
125 
126 /* Struct to record attribute information.  */
127 
128 typedef struct
129 {
130   /* Boolean attributes.  */
131   unsigned int bool_;
132   /* Non-boolean integer attributes.  */
133   CGEN_ATTR_VALUE_TYPE nonbool[1];
134 } CGEN_ATTR;
135 
136 /* Define a structure member for attributes with N non-boolean entries.
137    There is no maximum number of non-boolean attributes.
138    There is a maximum of 32 boolean attributes (since they are all recorded
139    in one host int).  */
140 
141 #define CGEN_ATTR_TYPE(n) \
142 struct { unsigned int bool_; \
143 	 CGEN_ATTR_VALUE_TYPE nonbool[(n) ? (n) : 1]; }
144 
145 /* Return the boolean attributes.  */
146 
147 #define CGEN_ATTR_BOOLS(a) ((a)->bool_)
148 
149 /* Non-boolean attribute numbers are offset by this much.  */
150 
151 #define CGEN_ATTR_NBOOL_OFFSET 32
152 
153 /* Given a boolean attribute number, return its mask.  */
154 
155 #define CGEN_ATTR_MASK(attr) (1 << (attr))
156 
157 /* Return the value of boolean attribute ATTR in ATTRS.  */
158 
159 #define CGEN_BOOL_ATTR(attrs, attr) ((CGEN_ATTR_MASK (attr) & (attrs)) != 0)
160 
161 /* Return value of attribute ATTR in ATTR_TABLE for OBJ.
162    OBJ is a pointer to the entity that has the attributes
163    (??? not used at present but is reserved for future purposes - eventually
164    the goal is to allow recording attributes in source form and computing
165    them lazily at runtime, not sure of the details yet).  */
166 
167 #define CGEN_ATTR_VALUE(obj, attr_table, attr) \
168 ((unsigned int) (attr) < CGEN_ATTR_NBOOL_OFFSET \
169  ? ((CGEN_ATTR_BOOLS (attr_table) & CGEN_ATTR_MASK (attr)) != 0) \
170  : ((attr_table)->nonbool[(attr) - CGEN_ATTR_NBOOL_OFFSET].nonbitset))
171 #define CGEN_BITSET_ATTR_VALUE(obj, attr_table, attr) \
172  ((attr_table)->nonbool[(attr) - CGEN_ATTR_NBOOL_OFFSET].bitset)
173 
174 /* Attribute name/value tables.
175    These are used to assist parsing of descriptions at run-time.  */
176 
177 typedef struct
178 {
179   const char * name;
180   unsigned value;
181 } CGEN_ATTR_ENTRY;
182 
183 /* For each domain (ifld,hw,operand,insn), list of attributes.  */
184 
185 typedef struct
186 {
187   const char * name;
188   const CGEN_ATTR_ENTRY * dfault;
189   const CGEN_ATTR_ENTRY * vals;
190 } CGEN_ATTR_TABLE;
191 
192 /* Instruction set variants.  */
193 
194 typedef struct {
195   const char *name;
196 
197   /* Default instruction size (in bits).
198      This is used by the assembler when it encounters an unknown insn.  */
199   unsigned int default_insn_bitsize;
200 
201   /* Base instruction size (in bits).
202      For non-LIW cpus this is generally the length of the smallest insn.
203      For LIW cpus its wip (work-in-progress).  For the m32r its 32.  */
204   unsigned int base_insn_bitsize;
205 
206   /* Minimum/maximum instruction size (in bits).  */
207   unsigned int min_insn_bitsize;
208   unsigned int max_insn_bitsize;
209 } CGEN_ISA;
210 
211 /* Machine variants.  */
212 
213 typedef struct {
214   const char *name;
215   /* The argument to bfd_arch_info->scan.  */
216   const char *bfd_name;
217   /* one of enum mach_attr */
218   int num;
219   /* parameter from mach->cpu */
220   unsigned int insn_chunk_bitsize;
221 } CGEN_MACH;
222 
223 /* Parse result (also extraction result).
224 
225    The result of parsing an insn is stored here.
226    To generate the actual insn, this is passed to the insert handler.
227    When printing an insn, the result of extraction is stored here.
228    To print the insn, this is passed to the print handler.
229 
230    It is machine generated so we don't define it here,
231    but we do need a forward decl for the handler fns.
232 
233    There is one member for each possible field in the insn.
234    The type depends on the field.
235    Also recorded here is the computed length of the insn for architectures
236    where it varies.
237 */
238 
239 typedef struct cgen_fields CGEN_FIELDS;
240 
241 /* Total length of the insn, as recorded in the `fields' struct.  */
242 /* ??? The field insert handler has lots of opportunities for optimization
243    if it ever gets inlined.  On architectures where insns all have the same
244    size, may wish to detect that and make this macro a constant - to allow
245    further optimizations.  */
246 
247 #define CGEN_FIELDS_BITSIZE(fields) ((fields)->length)
248 
249 /* Extraction support for variable length insn sets.  */
250 
251 /* When disassembling we don't know the number of bytes to read at the start.
252    So the first CGEN_BASE_INSN_SIZE bytes are read at the start and the rest
253    are read when needed.  This struct controls this.  It is basically the
254    disassemble_info stuff, except that we provide a cache for values already
255    read (since bytes can typically be read several times to fetch multiple
256    operands that may be in them), and that extraction of fields is needed
257    in contexts other than disassembly.  */
258 
259 typedef struct {
260   /* A pointer to the disassemble_info struct.
261      We don't require dis-asm.h so we use void * for the type here.
262      If NULL, BYTES is full of valid data (VALID == -1).  */
263   void *dis_info;
264   /* Points to a working buffer of sufficient size.  */
265   unsigned char *insn_bytes;
266   /* Mask of bytes that are valid in INSN_BYTES.  */
267   unsigned int valid;
268 } CGEN_EXTRACT_INFO;
269 
270 /* Associated with each insn or expression is a set of "handlers" for
271    performing operations like parsing, printing, etc.  These require a bfd_vma
272    value to be passed around but we don't want all applications to need bfd.h.
273    So this stuff is only provided if bfd.h has been included.  */
274 
275 /* Parse handler.
276    CD is a cpu table descriptor.
277    INSN is a pointer to a struct describing the insn being parsed.
278    STRP is a pointer to a pointer to the text being parsed.
279    FIELDS is a pointer to a cgen_fields struct in which the results are placed.
280    If the expression is successfully parsed, *STRP is updated.
281    If not it is left alone.
282    The result is NULL if success or an error message.  */
283 typedef const char * (cgen_parse_fn)
284   (CGEN_CPU_DESC, const CGEN_INSN *insn_,
285    const char **strp_, CGEN_FIELDS *fields_);
286 
287 /* Insert handler.
288    CD is a cpu table descriptor.
289    INSN is a pointer to a struct describing the insn being parsed.
290    FIELDS is a pointer to a cgen_fields struct from which the values
291    are fetched.
292    INSNP is a pointer to a buffer in which to place the insn.
293    PC is the pc value of the insn.
294    The result is an error message or NULL if success.  */
295 
296 #ifdef __BFD_H_SEEN__
297 typedef const char * (cgen_insert_fn)
298   (CGEN_CPU_DESC, const CGEN_INSN *insn_,
299    CGEN_FIELDS *fields_, CGEN_INSN_BYTES_PTR insnp_,
300    bfd_vma pc_);
301 #else
302 typedef const char * (cgen_insert_fn) ();
303 #endif
304 
305 /* Extract handler.
306    CD is a cpu table descriptor.
307    INSN is a pointer to a struct describing the insn being parsed.
308    The second argument is a pointer to a struct controlling extraction
309    (only used for variable length insns).
310    EX_INFO is a pointer to a struct for controlling reading of further
311    bytes for the insn.
312    BASE_INSN is the first CGEN_BASE_INSN_SIZE bytes (host order).
313    FIELDS is a pointer to a cgen_fields struct in which the results are placed.
314    PC is the pc value of the insn.
315    The result is the length of the insn in bits or zero if not recognized.  */
316 
317 #ifdef __BFD_H_SEEN__
318 typedef int (cgen_extract_fn)
319   (CGEN_CPU_DESC, const CGEN_INSN *insn_,
320    CGEN_EXTRACT_INFO *ex_info_, CGEN_INSN_INT base_insn_,
321    CGEN_FIELDS *fields_, bfd_vma pc_);
322 #else
323 typedef int (cgen_extract_fn) ();
324 #endif
325 
326 /* Print handler.
327    CD is a cpu table descriptor.
328    INFO is a pointer to the disassembly info.
329    Eg: disassemble_info.  It's defined as `PTR' so this file can be included
330    without dis-asm.h.
331    INSN is a pointer to a struct describing the insn being printed.
332    FIELDS is a pointer to a cgen_fields struct.
333    PC is the pc value of the insn.
334    LEN is the length of the insn, in bits.  */
335 
336 #ifdef __BFD_H_SEEN__
337 typedef void (cgen_print_fn)
338   (CGEN_CPU_DESC, void * info_, const CGEN_INSN *insn_,
339    CGEN_FIELDS *fields_, bfd_vma pc_, int len_);
340 #else
341 typedef void (cgen_print_fn) ();
342 #endif
343 
344 /* Parse/insert/extract/print handlers.
345 
346    Indices into the handler tables.
347    We could use pointers here instead, but 90% of them are generally identical
348    and that's a lot of redundant data.  Making these unsigned char indices
349    into tables of pointers saves a bit of space.
350    Using indices also keeps assembler code out of the disassembler and
351    vice versa.  */
352 
353 struct cgen_opcode_handler
354 {
355   unsigned char parse, insert, extract, print;
356 };
357 
358 /* Assembler interface.
359 
360    The interface to the assembler is intended to be clean in the sense that
361    libopcodes.a is a standalone entity and could be used with any assembler.
362    Not that one would necessarily want to do that but rather that it helps
363    keep a clean interface.  The interface will obviously be slanted towards
364    GAS, but at least it's a start.
365    ??? Note that one possible user of the assembler besides GAS is GDB.
366 
367    Parsing is controlled by the assembler which calls
368    CGEN_SYM (assemble_insn).  If it can parse and build the entire insn
369    it doesn't call back to the assembler.  If it needs/wants to call back
370    to the assembler, cgen_parse_operand_fn is called which can either
371 
372    - return a number to be inserted in the insn
373    - return a "register" value to be inserted
374      (the register might not be a register per pe)
375    - queue the argument and return a marker saying the expression has been
376      queued (eg: a fix-up)
377    - return an error message indicating the expression wasn't recognizable
378 
379    The result is an error message or NULL for success.
380    The parsed value is stored in the bfd_vma *.  */
381 
382 /* Values for indicating what the caller wants.  */
383 
384 enum cgen_parse_operand_type
385 {
386   CGEN_PARSE_OPERAND_INIT,
387   CGEN_PARSE_OPERAND_INTEGER,
388   CGEN_PARSE_OPERAND_ADDRESS,
389   CGEN_PARSE_OPERAND_SYMBOLIC
390 };
391 
392 /* Values for indicating what was parsed.  */
393 
394 enum cgen_parse_operand_result
395 {
396   CGEN_PARSE_OPERAND_RESULT_NUMBER,
397   CGEN_PARSE_OPERAND_RESULT_REGISTER,
398   CGEN_PARSE_OPERAND_RESULT_QUEUED,
399   CGEN_PARSE_OPERAND_RESULT_ERROR
400 };
401 
402 #ifdef __BFD_H_SEEN__ /* Don't require bfd.h unnecessarily.  */
403 typedef const char * (cgen_parse_operand_fn)
404   (CGEN_CPU_DESC,
405    enum cgen_parse_operand_type, const char **, int, int,
406    enum cgen_parse_operand_result *, bfd_vma *);
407 #else
408 typedef const char * (cgen_parse_operand_fn) ();
409 #endif
410 
411 /* Set the cgen_parse_operand_fn callback.  */
412 
413 extern void cgen_set_parse_operand_fn
414   (CGEN_CPU_DESC, cgen_parse_operand_fn);
415 
416 /* Called before trying to match a table entry with the insn.  */
417 
418 extern void cgen_init_parse_operand (CGEN_CPU_DESC);
419 
420 /* Operand values (keywords, integers, symbols, etc.)  */
421 
422 /* Types of assembler elements.  */
423 
424 enum cgen_asm_type
425 {
426   CGEN_ASM_NONE, CGEN_ASM_KEYWORD, CGEN_ASM_MAX
427 };
428 
429 #ifndef CGEN_ARCH
430 enum cgen_hw_type { CGEN_HW_MAX };
431 #endif
432 
433 /* List of hardware elements.  */
434 
435 typedef struct
436 {
437   char *name;
438   enum cgen_hw_type type;
439   /* There is currently no example where both index specs and value specs
440      are required, so for now both are clumped under "asm_data".  */
441   enum cgen_asm_type asm_type;
442   void *asm_data;
443 #ifndef CGEN_HW_NBOOL_ATTRS
444 #define CGEN_HW_NBOOL_ATTRS 1
445 #endif
446   CGEN_ATTR_TYPE (CGEN_HW_NBOOL_ATTRS) attrs;
447 #define CGEN_HW_ATTRS(hw) (&(hw)->attrs)
448 } CGEN_HW_ENTRY;
449 
450 /* Return value of attribute ATTR in HW.  */
451 
452 #define CGEN_HW_ATTR_VALUE(hw, attr) \
453 CGEN_ATTR_VALUE ((hw), CGEN_HW_ATTRS (hw), (attr))
454 
455 /* Table of hardware elements for selected mach, computed at runtime.
456    enum cgen_hw_type is an index into this table (specifically `entries').  */
457 
458 typedef struct {
459   /* Pointer to null terminated table of all compiled in entries.  */
460   const CGEN_HW_ENTRY *init_entries;
461   unsigned int entry_size; /* since the attribute member is variable sized */
462   /* Array of all entries, initial and run-time added.  */
463   const CGEN_HW_ENTRY **entries;
464   /* Number of elements in `entries'.  */
465   unsigned int num_entries;
466   /* For now, xrealloc is called each time a new entry is added at runtime.
467      ??? May wish to keep track of some slop to reduce the number of calls to
468      xrealloc, except that there's unlikely to be many and not expected to be
469      in speed critical code.  */
470 } CGEN_HW_TABLE;
471 
472 extern const CGEN_HW_ENTRY * cgen_hw_lookup_by_name
473   (CGEN_CPU_DESC, const char *);
474 extern const CGEN_HW_ENTRY * cgen_hw_lookup_by_num
475   (CGEN_CPU_DESC, unsigned int);
476 
477 /* This struct is used to describe things like register names, etc.  */
478 
479 typedef struct cgen_keyword_entry
480 {
481   /* Name (as in register name).  */
482   char * name;
483 
484   /* Value (as in register number).
485      The value cannot be -1 as that is used to indicate "not found".
486      IDEA: Have "FUNCTION" attribute? [function is called to fetch value].  */
487   int value;
488 
489   /* Attributes.
490      This should, but technically needn't, appear last.  It is a variable sized
491      array in that one architecture may have 1 nonbool attribute and another
492      may have more.  Having this last means the non-architecture specific code
493      needn't care.  The goal is to eventually record
494      attributes in their raw form, evaluate them at run-time, and cache the
495      values, so this worry will go away anyway.  */
496   /* ??? Moving this last should be done by treating keywords like insn lists
497      and moving the `next' fields into a CGEN_KEYWORD_LIST struct.  */
498   /* FIXME: Not used yet.  */
499 #ifndef CGEN_KEYWORD_NBOOL_ATTRS
500 #define CGEN_KEYWORD_NBOOL_ATTRS 1
501 #endif
502   CGEN_ATTR_TYPE (CGEN_KEYWORD_NBOOL_ATTRS) attrs;
503 
504   /* ??? Putting these here means compiled in entries can't be const.
505      Not a really big deal, but something to consider.  */
506   /* Next name hash table entry.  */
507   struct cgen_keyword_entry *next_name;
508   /* Next value hash table entry.  */
509   struct cgen_keyword_entry *next_value;
510 } CGEN_KEYWORD_ENTRY;
511 
512 /* Top level struct for describing a set of related keywords
513    (e.g. register names).
514 
515    This struct supports run-time entry of new values, and hashed lookups.  */
516 
517 typedef struct cgen_keyword
518 {
519   /* Pointer to initial [compiled in] values.  */
520   CGEN_KEYWORD_ENTRY *init_entries;
521 
522   /* Number of entries in `init_entries'.  */
523   unsigned int num_init_entries;
524 
525   /* Hash table used for name lookup.  */
526   CGEN_KEYWORD_ENTRY **name_hash_table;
527 
528   /* Hash table used for value lookup.  */
529   CGEN_KEYWORD_ENTRY **value_hash_table;
530 
531   /* Number of entries in the hash_tables.  */
532   unsigned int hash_table_size;
533 
534   /* Pointer to null keyword "" entry if present.  */
535   const CGEN_KEYWORD_ENTRY *null_entry;
536 
537   /* String containing non-alphanumeric characters used
538      in keywords.
539      At present, the highest number of entries used is 1.  */
540   char nonalpha_chars[8];
541 } CGEN_KEYWORD;
542 
543 /* Structure used for searching.  */
544 
545 typedef struct
546 {
547   /* Table being searched.  */
548   const CGEN_KEYWORD *table;
549 
550   /* Specification of what is being searched for.  */
551   const char *spec;
552 
553   /* Current index in hash table.  */
554   unsigned int current_hash;
555 
556   /* Current element in current hash chain.  */
557   CGEN_KEYWORD_ENTRY *current_entry;
558 } CGEN_KEYWORD_SEARCH;
559 
560 /* Lookup a keyword from its name.  */
561 
562 const CGEN_KEYWORD_ENTRY *cgen_keyword_lookup_name
563   (CGEN_KEYWORD *, const char *);
564 
565 /* Lookup a keyword from its value.  */
566 
567 const CGEN_KEYWORD_ENTRY *cgen_keyword_lookup_value
568   (CGEN_KEYWORD *, int);
569 
570 /* Add a keyword.  */
571 
572 void cgen_keyword_add (CGEN_KEYWORD *, CGEN_KEYWORD_ENTRY *);
573 
574 /* Keyword searching.
575    This can be used to retrieve every keyword, or a subset.  */
576 
577 CGEN_KEYWORD_SEARCH cgen_keyword_search_init
578   (CGEN_KEYWORD *, const char *);
579 const CGEN_KEYWORD_ENTRY *cgen_keyword_search_next
580   (CGEN_KEYWORD_SEARCH *);
581 
582 /* Operand value support routines.  */
583 
584 extern const char *cgen_parse_keyword
585   (CGEN_CPU_DESC, const char **, CGEN_KEYWORD *, long *);
586 #ifdef __BFD_H_SEEN__ /* Don't require bfd.h unnecessarily.  */
587 extern const char *cgen_parse_signed_integer
588   (CGEN_CPU_DESC, const char **, int, long *);
589 extern const char *cgen_parse_unsigned_integer
590   (CGEN_CPU_DESC, const char **, int, unsigned long *);
591 extern const char *cgen_parse_address
592   (CGEN_CPU_DESC, const char **, int, int,
593    enum cgen_parse_operand_result *, bfd_vma *);
594 extern const char *cgen_validate_signed_integer
595   (long, long, long);
596 extern const char *cgen_validate_unsigned_integer
597   (unsigned long, unsigned long, unsigned long);
598 #endif
599 
600 /* Operand modes.  */
601 
602 /* ??? This duplicates the values in arch.h.  Revisit.
603    These however need the CGEN_ prefix [as does everything in this file].  */
604 /* ??? Targets may need to add their own modes so we may wish to move this
605    to <arch>-opc.h, or add a hook.  */
606 
607 enum cgen_mode {
608   CGEN_MODE_VOID, /* ??? rename simulator's VM to VOID? */
609   CGEN_MODE_BI, CGEN_MODE_QI, CGEN_MODE_HI, CGEN_MODE_SI, CGEN_MODE_DI,
610   CGEN_MODE_UBI, CGEN_MODE_UQI, CGEN_MODE_UHI, CGEN_MODE_USI, CGEN_MODE_UDI,
611   CGEN_MODE_SF, CGEN_MODE_DF, CGEN_MODE_XF, CGEN_MODE_TF,
612   CGEN_MODE_TARGET_MAX,
613   CGEN_MODE_INT, CGEN_MODE_UINT,
614   CGEN_MODE_MAX
615 };
616 
617 /* FIXME: Until simulator is updated.  */
618 
619 #define CGEN_MODE_VM CGEN_MODE_VOID
620 
621 /* Operands.  */
622 
623 #ifndef CGEN_ARCH
624 enum cgen_operand_type { CGEN_OPERAND_MAX };
625 #endif
626 
627 /* "nil" indicator for the operand instance table */
628 #define CGEN_OPERAND_NIL CGEN_OPERAND_MAX
629 
630 /* A tree of these structs represents the multi-ifield
631    structure of an operand's hw-index value, if it exists.  */
632 
633 struct cgen_ifld;
634 
635 typedef struct cgen_maybe_multi_ifield
636 {
637   int count; /* 0: indexed by single cgen_ifld (possibly null: dead entry);
638 		n: indexed by array of more cgen_maybe_multi_ifields.  */
639   union
640   {
641     const void *p;
642     const struct cgen_maybe_multi_ifield * multi;
643     const struct cgen_ifld * leaf;
644   } val;
645 }
646 CGEN_MAYBE_MULTI_IFLD;
647 
648 /* This struct defines each entry in the operand table.  */
649 
650 typedef struct
651 {
652   /* Name as it appears in the syntax string.  */
653   char *name;
654 
655   /* Operand type.  */
656   enum cgen_operand_type type;
657 
658   /* The hardware element associated with this operand.  */
659   enum cgen_hw_type hw_type;
660 
661   /* FIXME: We don't yet record ifield definitions, which we should.
662      When we do it might make sense to delete start/length (since they will
663      be duplicated in the ifield's definition) and replace them with a
664      pointer to the ifield entry.  */
665 
666   /* Bit position.
667      This is just a hint, and may be unused in more complex operands.
668      May be unused for a modifier.  */
669   unsigned char start;
670 
671   /* The number of bits in the operand.
672      This is just a hint, and may be unused in more complex operands.
673      May be unused for a modifier.  */
674   unsigned char length;
675 
676   /* The (possibly-multi) ifield used as an index for this operand, if it
677      is indexed by a field at all. This substitutes / extends the start and
678      length fields above, but unsure at this time whether they are used
679      anywhere.  */
680   CGEN_MAYBE_MULTI_IFLD index_fields;
681 #if 0 /* ??? Interesting idea but relocs tend to get too complicated,
682 	 and ABI dependent, for simple table lookups to work.  */
683   /* Ideally this would be the internal (external?) reloc type.  */
684   int reloc_type;
685 #endif
686 
687   /* Attributes.
688      This should, but technically needn't, appear last.  It is a variable sized
689      array in that one architecture may have 1 nonbool attribute and another
690      may have more.  Having this last means the non-architecture specific code
691      needn't care, now or tomorrow.  The goal is to eventually record
692      attributes in their raw form, evaluate them at run-time, and cache the
693      values, so this worry will go away anyway.  */
694 #ifndef CGEN_OPERAND_NBOOL_ATTRS
695 #define CGEN_OPERAND_NBOOL_ATTRS 1
696 #endif
697   CGEN_ATTR_TYPE (CGEN_OPERAND_NBOOL_ATTRS) attrs;
698 #define CGEN_OPERAND_ATTRS(operand) (&(operand)->attrs)
699 } CGEN_OPERAND;
700 
701 /* Return value of attribute ATTR in OPERAND.  */
702 
703 #define CGEN_OPERAND_ATTR_VALUE(operand, attr) \
704 CGEN_ATTR_VALUE ((operand), CGEN_OPERAND_ATTRS (operand), (attr))
705 
706 /* Table of operands for selected mach/isa, computed at runtime.
707    enum cgen_operand_type is an index into this table (specifically
708    `entries').  */
709 
710 typedef struct {
711   /* Pointer to null terminated table of all compiled in entries.  */
712   const CGEN_OPERAND *init_entries;
713   unsigned int entry_size; /* since the attribute member is variable sized */
714   /* Array of all entries, initial and run-time added.  */
715   const CGEN_OPERAND **entries;
716   /* Number of elements in `entries'.  */
717   unsigned int num_entries;
718   /* For now, xrealloc is called each time a new entry is added at runtime.
719      ??? May wish to keep track of some slop to reduce the number of calls to
720      xrealloc, except that there's unlikely to be many and not expected to be
721      in speed critical code.  */
722 } CGEN_OPERAND_TABLE;
723 
724 extern const CGEN_OPERAND * cgen_operand_lookup_by_name
725   (CGEN_CPU_DESC, const char *);
726 extern const CGEN_OPERAND * cgen_operand_lookup_by_num
727   (CGEN_CPU_DESC, int);
728 
729 /* Instruction operand instances.
730 
731    For each instruction, a list of the hardware elements that are read and
732    written are recorded.  */
733 
734 /* The type of the instance.  */
735 
736 enum cgen_opinst_type {
737   /* End of table marker.  */
738   CGEN_OPINST_END = 0,
739   CGEN_OPINST_INPUT, CGEN_OPINST_OUTPUT
740 };
741 
742 typedef struct
743 {
744   /* Input or output indicator.  */
745   enum cgen_opinst_type type;
746 
747   /* Name of operand.  */
748   const char *name;
749 
750   /* The hardware element referenced.  */
751   enum cgen_hw_type hw_type;
752 
753   /* The mode in which the operand is being used.  */
754   enum cgen_mode mode;
755 
756   /* The operand table entry CGEN_OPERAND_NIL if there is none
757      (i.e. an explicit hardware reference).  */
758   enum cgen_operand_type op_type;
759 
760   /* If `operand' is "nil", the index (e.g. into array of registers).  */
761   int index;
762 
763   /* Attributes.
764      ??? This perhaps should be a real attribute struct but there's
765      no current need, so we save a bit of space and just have a set of
766      flags.  The interface is such that this can easily be made attributes
767      should it prove useful.  */
768   unsigned int attrs;
769 #define CGEN_OPINST_ATTRS(opinst) ((opinst)->attrs)
770 /* Return value of attribute ATTR in OPINST.  */
771 #define CGEN_OPINST_ATTR(opinst, attr) \
772 ((CGEN_OPINST_ATTRS (opinst) & (attr)) != 0)
773 /* Operand is conditionally referenced (read/written).  */
774 #define CGEN_OPINST_COND_REF 1
775 } CGEN_OPINST;
776 
777 /* Syntax string.
778 
779    Each insn format and subexpression has one of these.
780 
781    The syntax "string" consists of characters (n > 0 && n < 128), and operand
782    values (n >= 128), and is terminated by 0.  Operand values are 128 + index
783    into the operand table.  The operand table doesn't exist in C, per se, as
784    the data is recorded in the parse/insert/extract/print switch statements. */
785 
786 /* This should be at least as large as necessary for any target. */
787 #define CGEN_MAX_SYNTAX_ELEMENTS 48
788 
789 /* A target may know its own precise maximum.  Assert that it falls below
790    the above limit. */
791 #ifdef CGEN_ACTUAL_MAX_SYNTAX_ELEMENTS
792 #if CGEN_ACTUAL_MAX_SYNTAX_ELEMENTS > CGEN_MAX_SYNTAX_ELEMENTS
793 #error "CGEN_ACTUAL_MAX_SYNTAX_ELEMENTS too high - enlarge CGEN_MAX_SYNTAX_ELEMENTS"
794 #endif
795 #endif
796 
797 typedef unsigned short CGEN_SYNTAX_CHAR_TYPE;
798 
799 typedef struct
800 {
801   CGEN_SYNTAX_CHAR_TYPE syntax[CGEN_MAX_SYNTAX_ELEMENTS];
802 } CGEN_SYNTAX;
803 
804 #define CGEN_SYNTAX_STRING(syn) (syn->syntax)
805 #define CGEN_SYNTAX_CHAR_P(c) ((c) < 128)
806 #define CGEN_SYNTAX_CHAR(c) ((unsigned char)c)
807 #define CGEN_SYNTAX_FIELD(c) ((c) - 128)
808 #define CGEN_SYNTAX_MAKE_FIELD(c) ((c) + 128)
809 
810 /* ??? I can't currently think of any case where the mnemonic doesn't come
811    first [and if one ever doesn't building the hash tables will be tricky].
812    However, we treat mnemonics as just another operand of the instruction.
813    A value of 1 means "this is where the mnemonic appears".  1 isn't
814    special other than it's a non-printable ASCII char.  */
815 
816 #define CGEN_SYNTAX_MNEMONIC       1
817 #define CGEN_SYNTAX_MNEMONIC_P(ch) ((ch) == CGEN_SYNTAX_MNEMONIC)
818 
819 /* Instruction fields.
820 
821    ??? We currently don't allow adding fields at run-time.
822    Easy to fix when needed.  */
823 
824 typedef struct cgen_ifld {
825   /* Enum of ifield.  */
826   int num;
827 #define CGEN_IFLD_NUM(f) ((f)->num)
828 
829   /* Name of the field, distinguishes it from all other fields.  */
830   const char *name;
831 #define CGEN_IFLD_NAME(f) ((f)->name)
832 
833   /* Default offset, in bits, from the start of the insn to the word
834      containing the field.  */
835   int word_offset;
836 #define CGEN_IFLD_WORD_OFFSET(f) ((f)->word_offset)
837 
838   /* Default length of the word containing the field.  */
839   int word_size;
840 #define CGEN_IFLD_WORD_SIZE(f) ((f)->word_size)
841 
842   /* Default starting bit number.
843      Whether lsb=0 or msb=0 is determined by CGEN_INSN_LSB0_P.  */
844   int start;
845 #define CGEN_IFLD_START(f) ((f)->start)
846 
847   /* Length of the field, in bits.  */
848   int length;
849 #define CGEN_IFLD_LENGTH(f) ((f)->length)
850 
851 #ifndef CGEN_IFLD_NBOOL_ATTRS
852 #define CGEN_IFLD_NBOOL_ATTRS 1
853 #endif
854   CGEN_ATTR_TYPE (CGEN_IFLD_NBOOL_ATTRS) attrs;
855 #define CGEN_IFLD_ATTRS(f) (&(f)->attrs)
856 } CGEN_IFLD;
857 
858 /* Return value of attribute ATTR in IFLD.  */
859 #define CGEN_IFLD_ATTR_VALUE(ifld, attr) \
860 CGEN_ATTR_VALUE ((ifld), CGEN_IFLD_ATTRS (ifld), (attr))
861 
862 /* Instruction data.  */
863 
864 /* Instruction formats.
865 
866    Instructions are grouped by format.  Associated with an instruction is its
867    format.  Each insn's opcode table entry contains a format table entry.
868    ??? There is usually very few formats compared with the number of insns,
869    so one can reduce the size of the opcode table by recording the format table
870    as a separate entity.  Given that we currently don't, format table entries
871    are also distinguished by their operands.  This increases the size of the
872    table, but reduces the number of tables.  It's all minutiae anyway so it
873    doesn't really matter [at this point in time].
874 
875    ??? Support for variable length ISA's is wip.  */
876 
877 /* Accompanying each iformat description is a list of its fields.  */
878 
879 typedef struct {
880   const CGEN_IFLD *ifld;
881 #define CGEN_IFMT_IFLD_IFLD(ii) ((ii)->ifld)
882 } CGEN_IFMT_IFLD;
883 
884 /* This should be at least as large as necessary for any target. */
885 #define CGEN_MAX_IFMT_OPERANDS 16
886 
887 /* A target may know its own precise maximum.  Assert that it falls below
888    the above limit. */
889 #ifdef CGEN_ACTUAL_MAX_IFMT_OPERANDS
890 #if CGEN_ACTUAL_MAX_IFMT_OPERANDS > CGEN_MAX_IFMT_OPERANDS
891 #error "CGEN_ACTUAL_MAX_IFMT_OPERANDS too high - enlarge CGEN_MAX_IFMT_OPERANDS"
892 #endif
893 #endif
894 
895 
896 typedef struct
897 {
898   /* Length that MASK and VALUE have been calculated to
899      [VALUE is recorded elsewhere].
900      Normally it is base_insn_bitsize.  On [V]LIW architectures where the base
901      insn size may be larger than the size of an insn, this field is less than
902      base_insn_bitsize.  */
903   unsigned char mask_length;
904 #define CGEN_IFMT_MASK_LENGTH(ifmt) ((ifmt)->mask_length)
905 
906   /* Total length of instruction, in bits.  */
907   unsigned char length;
908 #define CGEN_IFMT_LENGTH(ifmt) ((ifmt)->length)
909 
910   /* Mask to apply to the first MASK_LENGTH bits.
911      Each insn's value is stored with the insn.
912      The first step in recognizing an insn for disassembly is
913      (opcode & mask) == value.  */
914   CGEN_INSN_INT mask;
915 #define CGEN_IFMT_MASK(ifmt) ((ifmt)->mask)
916 
917   /* Instruction fields.
918      +1 for trailing NULL.  */
919   CGEN_IFMT_IFLD iflds[CGEN_MAX_IFMT_OPERANDS + 1];
920 #define CGEN_IFMT_IFLDS(ifmt) ((ifmt)->iflds)
921 } CGEN_IFMT;
922 
923 /* Instruction values.  */
924 
925 typedef struct
926 {
927   /* The opcode portion of the base insn.  */
928   CGEN_INSN_INT base_value;
929 
930 #ifdef CGEN_MAX_EXTRA_OPCODE_OPERANDS
931   /* Extra opcode values beyond base_value.  */
932   unsigned long ifield_values[CGEN_MAX_EXTRA_OPCODE_OPERANDS];
933 #endif
934 } CGEN_IVALUE;
935 
936 /* Instruction opcode table.
937    This contains the syntax and format data of an instruction.  */
938 
939 /* ??? Some ports already have an opcode table yet still need to use the rest
940    of what cgen_insn has.  Plus keeping the opcode data with the operand
941    instance data can create a pretty big file.  So we keep them separately.
942    Not sure this is a good idea in the long run.  */
943 
944 typedef struct
945 {
946   /* Indices into parse/insert/extract/print handler tables.  */
947   struct cgen_opcode_handler handlers;
948 #define CGEN_OPCODE_HANDLERS(opc) (& (opc)->handlers)
949 
950   /* Syntax string.  */
951   CGEN_SYNTAX syntax;
952 #define CGEN_OPCODE_SYNTAX(opc) (& (opc)->syntax)
953 
954   /* Format entry.  */
955   const CGEN_IFMT *format;
956 #define CGEN_OPCODE_FORMAT(opc) ((opc)->format)
957 #define CGEN_OPCODE_MASK_BITSIZE(opc) CGEN_IFMT_MASK_LENGTH (CGEN_OPCODE_FORMAT (opc))
958 #define CGEN_OPCODE_BITSIZE(opc) CGEN_IFMT_LENGTH (CGEN_OPCODE_FORMAT (opc))
959 #define CGEN_OPCODE_IFLDS(opc) CGEN_IFMT_IFLDS (CGEN_OPCODE_FORMAT (opc))
960 
961   /* Instruction opcode value.  */
962   CGEN_IVALUE value;
963 #define CGEN_OPCODE_VALUE(opc) (& (opc)->value)
964 #define CGEN_OPCODE_BASE_VALUE(opc) (CGEN_OPCODE_VALUE (opc)->base_value)
965 #define CGEN_OPCODE_BASE_MASK(opc) CGEN_IFMT_MASK (CGEN_OPCODE_FORMAT (opc))
966 } CGEN_OPCODE;
967 
968 /* Instruction attributes.
969    This is made a published type as applications can cache a pointer to
970    the attributes for speed.  */
971 
972 #ifndef CGEN_INSN_NBOOL_ATTRS
973 #define CGEN_INSN_NBOOL_ATTRS 1
974 #endif
975 typedef CGEN_ATTR_TYPE (CGEN_INSN_NBOOL_ATTRS) CGEN_INSN_ATTR_TYPE;
976 
977 /* Enum of architecture independent attributes.  */
978 
979 #ifndef CGEN_ARCH
980 /* ??? Numbers here are recorded in two places.  */
981 typedef enum cgen_insn_attr {
982   CGEN_INSN_ALIAS = 0
983 } CGEN_INSN_ATTR;
984 #define CGEN_ATTR_CGEN_INSN_ALIAS_VALUE(attrs) ((attrs)->bool_ & (1 << CGEN_INSN_ALIAS))
985 #endif
986 
987 /* This struct defines each entry in the instruction table.  */
988 
989 typedef struct
990 {
991   /* Each real instruction is enumerated.  */
992   /* ??? This may go away in time.  */
993   int num;
994 #define CGEN_INSN_NUM(insn) ((insn)->base->num)
995 
996   /* Name of entry (that distinguishes it from all other entries).  */
997   /* ??? If mnemonics have operands, try to print full mnemonic.  */
998   const char *name;
999 #define CGEN_INSN_NAME(insn) ((insn)->base->name)
1000 
1001   /* Mnemonic.  This is used when parsing and printing the insn.
1002      In the case of insns that have operands on the mnemonics, this is
1003      only the constant part.  E.g. for conditional execution of an `add' insn,
1004      where the full mnemonic is addeq, addne, etc., and the condition is
1005      treated as an operand, this is only "add".  */
1006   const char *mnemonic;
1007 #define CGEN_INSN_MNEMONIC(insn) ((insn)->base->mnemonic)
1008 
1009   /* Total length of instruction, in bits.  */
1010   int bitsize;
1011 #define CGEN_INSN_BITSIZE(insn) ((insn)->base->bitsize)
1012 
1013 #if 0 /* ??? Disabled for now as there is a problem with embedded newlines
1014 	 and the table is already pretty big.  Should perhaps be moved
1015 	 to a file of its own.  */
1016   /* Semantics, as RTL.  */
1017   /* ??? Plain text or bytecodes?  */
1018   /* ??? Note that the operand instance table could be computed at run-time
1019      if we parse this and cache the results.  Something to eventually do.  */
1020   const char *rtx;
1021 #define CGEN_INSN_RTX(insn) ((insn)->base->rtx)
1022 #endif
1023 
1024   /* Attributes.
1025      This must appear last.  It is a variable sized array in that one
1026      architecture may have 1 nonbool attribute and another may have more.
1027      Having this last means the non-architecture specific code needn't
1028      care.  The goal is to eventually record attributes in their raw form,
1029      evaluate them at run-time, and cache the values, so this worry will go
1030      away anyway.  */
1031   CGEN_INSN_ATTR_TYPE attrs;
1032 #define CGEN_INSN_ATTRS(insn) (&(insn)->base->attrs)
1033 /* Return value of attribute ATTR in INSN.  */
1034 #define CGEN_INSN_ATTR_VALUE(insn, attr) \
1035 CGEN_ATTR_VALUE ((insn), CGEN_INSN_ATTRS (insn), (attr))
1036 #define CGEN_INSN_BITSET_ATTR_VALUE(insn, attr) \
1037   CGEN_BITSET_ATTR_VALUE ((insn), CGEN_INSN_ATTRS (insn), (attr))
1038 } CGEN_IBASE;
1039 
1040 /* Return non-zero if INSN is the "invalid" insn marker.  */
1041 
1042 #define CGEN_INSN_INVALID_P(insn) (CGEN_INSN_MNEMONIC (insn) == 0)
1043 
1044 /* Main struct contain instruction information.
1045    BASE is always present, the rest is present only if asked for.  */
1046 
1047 struct cgen_insn
1048 {
1049   /* ??? May be of use to put a type indicator here.
1050      Then this struct could different info for different classes of insns.  */
1051   /* ??? A speedup can be had by moving `base' into this struct.
1052      Maybe later.  */
1053   const CGEN_IBASE *base;
1054   const CGEN_OPCODE *opcode;
1055   const CGEN_OPINST *opinst;
1056 
1057   /* Regex to disambiguate overloaded opcodes */
1058   void *rx;
1059 #define CGEN_INSN_RX(insn) ((insn)->rx)
1060 #define CGEN_MAX_RX_ELEMENTS (CGEN_MAX_SYNTAX_ELEMENTS * 5)
1061 };
1062 
1063 /* Instruction lists.
1064    This is used for adding new entries and for creating the hash lists.  */
1065 
1066 typedef struct cgen_insn_list
1067 {
1068   struct cgen_insn_list *next;
1069   const CGEN_INSN *insn;
1070 } CGEN_INSN_LIST;
1071 
1072 /* Table of instructions.  */
1073 
1074 typedef struct
1075 {
1076   const CGEN_INSN *init_entries;
1077   unsigned int entry_size; /* since the attribute member is variable sized */
1078   unsigned int num_init_entries;
1079   CGEN_INSN_LIST *new_entries;
1080 } CGEN_INSN_TABLE;
1081 
1082 /* Return number of instructions.  This includes any added at run-time.  */
1083 
1084 extern int cgen_insn_count (CGEN_CPU_DESC);
1085 extern int cgen_macro_insn_count (CGEN_CPU_DESC);
1086 
1087 /* Macros to access the other insn elements not recorded in CGEN_IBASE.  */
1088 
1089 /* Fetch INSN's operand instance table.  */
1090 /* ??? Doesn't handle insns added at runtime.  */
1091 #define CGEN_INSN_OPERANDS(insn) ((insn)->opinst)
1092 
1093 /* Return INSN's opcode table entry.  */
1094 #define CGEN_INSN_OPCODE(insn) ((insn)->opcode)
1095 
1096 /* Return INSN's handler data.  */
1097 #define CGEN_INSN_HANDLERS(insn) CGEN_OPCODE_HANDLERS (CGEN_INSN_OPCODE (insn))
1098 
1099 /* Return INSN's syntax.  */
1100 #define CGEN_INSN_SYNTAX(insn) CGEN_OPCODE_SYNTAX (CGEN_INSN_OPCODE (insn))
1101 
1102 /* Return size of base mask in bits.  */
1103 #define CGEN_INSN_MASK_BITSIZE(insn) \
1104   CGEN_OPCODE_MASK_BITSIZE (CGEN_INSN_OPCODE (insn))
1105 
1106 /* Return mask of base part of INSN.  */
1107 #define CGEN_INSN_BASE_MASK(insn) \
1108   CGEN_OPCODE_BASE_MASK (CGEN_INSN_OPCODE (insn))
1109 
1110 /* Return value of base part of INSN.  */
1111 #define CGEN_INSN_BASE_VALUE(insn) \
1112   CGEN_OPCODE_BASE_VALUE (CGEN_INSN_OPCODE (insn))
1113 
1114 /* Standard way to test whether INSN is supported by MACH.
1115    MACH is one of enum mach_attr.
1116    The "|1" is because the base mach is always selected.  */
1117 #define CGEN_INSN_MACH_HAS_P(insn, mach) \
1118 ((CGEN_INSN_ATTR_VALUE ((insn), CGEN_INSN_MACH) & ((1 << (mach)) | 1)) != 0)
1119 
1120 /* Macro instructions.
1121    Macro insns aren't real insns, they map to one or more real insns.
1122    E.g. An architecture's "nop" insn may actually be an "mv r0,r0" or
1123    some such.
1124 
1125    Macro insns can expand to nothing (e.g. a nop that is optimized away).
1126    This is useful in multi-insn macros that build a constant in a register.
1127    Of course this isn't the default behaviour and must be explicitly enabled.
1128 
1129    Assembly of macro-insns is relatively straightforward.  Disassembly isn't.
1130    However, disassembly of at least some kinds of macro insns is important
1131    in order that the disassembled code preserve the readability of the original
1132    insn.  What is attempted here is to disassemble all "simple" macro-insns,
1133    where "simple" is currently defined to mean "expands to one real insn".
1134 
1135    Simple macro-insns are handled specially.  They are emitted as ALIAS's
1136    of real insns.  This simplifies their handling since there's usually more
1137    of them than any other kind of macro-insn, and proper disassembly of them
1138    falls out for free.  */
1139 
1140 /* For each macro-insn there may be multiple expansion possibilities,
1141    depending on the arguments.  This structure is accessed via the `data'
1142    member of CGEN_INSN.  */
1143 
1144 typedef struct cgen_minsn_expansion {
1145   /* Function to do the expansion.
1146      If the expansion fails (e.g. "no match") NULL is returned.
1147      Space for the expansion is obtained with malloc.
1148      It is up to the caller to free it.  */
1149   const char * (* fn)
1150      (const struct cgen_minsn_expansion *,
1151       const char *, const char **, int *,
1152       CGEN_OPERAND **);
1153 #define CGEN_MIEXPN_FN(ex) ((ex)->fn)
1154 
1155   /* Instruction(s) the macro expands to.
1156      The format of STR is defined by FN.
1157      It is typically the assembly code of the real insn, but it could also be
1158      the original Scheme expression or a tokenized form of it (with FN being
1159      an appropriate interpreter).  */
1160   const char * str;
1161 #define CGEN_MIEXPN_STR(ex) ((ex)->str)
1162 } CGEN_MINSN_EXPANSION;
1163 
1164 /* Normal expander.
1165    When supported, this function will convert the input string to another
1166    string and the parser will be invoked recursively.  The output string
1167    may contain further macro invocations.  */
1168 
1169 extern const char * cgen_expand_macro_insn
1170   (CGEN_CPU_DESC, const struct cgen_minsn_expansion *,
1171    const char *, const char **, int *, CGEN_OPERAND **);
1172 
1173 /* The assembler insn table is hashed based on some function of the mnemonic
1174    (the actually hashing done is up to the target, but we provide a few
1175    examples like the first letter or a function of the entire mnemonic).  */
1176 
1177 extern CGEN_INSN_LIST * cgen_asm_lookup_insn
1178   (CGEN_CPU_DESC, const char *);
1179 #define CGEN_ASM_LOOKUP_INSN(cd, string) cgen_asm_lookup_insn ((cd), (string))
1180 #define CGEN_ASM_NEXT_INSN(insn) ((insn)->next)
1181 
1182 /* The disassembler insn table is hashed based on some function of machine
1183    instruction (the actually hashing done is up to the target).  */
1184 
1185 extern CGEN_INSN_LIST * cgen_dis_lookup_insn
1186   (CGEN_CPU_DESC, const char *, CGEN_INSN_INT);
1187 /* FIXME: delete these two */
1188 #define CGEN_DIS_LOOKUP_INSN(cd, buf, value) cgen_dis_lookup_insn ((cd), (buf), (value))
1189 #define CGEN_DIS_NEXT_INSN(insn) ((insn)->next)
1190 
1191 /* The CPU description.
1192    A copy of this is created when the cpu table is "opened".
1193    All global state information is recorded here.
1194    Access macros are provided for "public" members.  */
1195 
1196 typedef struct cgen_cpu_desc
1197 {
1198   /* Bitmap of selected machine(s) (a la BFD machine number).  */
1199   int machs;
1200 
1201   /* Bitmap of selected isa(s).  */
1202   CGEN_BITSET *isas;
1203 #define CGEN_CPU_ISAS(cd) ((cd)->isas)
1204 
1205   /* Current endian.  */
1206   enum cgen_endian endian;
1207 #define CGEN_CPU_ENDIAN(cd) ((cd)->endian)
1208 
1209   /* Current insn endian.  */
1210   enum cgen_endian insn_endian;
1211 #define CGEN_CPU_INSN_ENDIAN(cd) ((cd)->insn_endian)
1212 
1213   /* Word size (in bits).  */
1214   /* ??? Or maybe maximum word size - might we ever need to allow a cpu table
1215      to be opened for both sparc32/sparc64?
1216      ??? Another alternative is to create a table of selected machs and
1217      lazily fetch the data from there.  */
1218   unsigned int word_bitsize;
1219 
1220   /* Instruction chunk size (in bits), for purposes of endianness
1221      conversion.  */
1222   unsigned int insn_chunk_bitsize;
1223 
1224   /* Indicator if sizes are unknown.
1225      This is used by default_insn_bitsize,base_insn_bitsize if there is a
1226      difference between the selected isa's.  */
1227 #define CGEN_SIZE_UNKNOWN 65535
1228 
1229   /* Default instruction size (in bits).
1230      This is used by the assembler when it encounters an unknown insn.  */
1231   unsigned int default_insn_bitsize;
1232 
1233   /* Base instruction size (in bits).
1234      For non-LIW cpus this is generally the length of the smallest insn.
1235      For LIW cpus its wip (work-in-progress).  For the m32r its 32.  */
1236   unsigned int base_insn_bitsize;
1237 
1238   /* Minimum/maximum instruction size (in bits).  */
1239   unsigned int min_insn_bitsize;
1240   unsigned int max_insn_bitsize;
1241 
1242   /* Instruction set variants.  */
1243   const CGEN_ISA *isa_table;
1244 
1245   /* Machine variants.  */
1246   const CGEN_MACH *mach_table;
1247 
1248   /* Hardware elements.  */
1249   CGEN_HW_TABLE hw_table;
1250 
1251   /* Instruction fields.  */
1252   const CGEN_IFLD *ifld_table;
1253 
1254   /* Operands.  */
1255   CGEN_OPERAND_TABLE operand_table;
1256 
1257   /* Main instruction table.  */
1258   CGEN_INSN_TABLE insn_table;
1259 #define CGEN_CPU_INSN_TABLE(cd) (& (cd)->insn_table)
1260 
1261   /* Macro instructions are defined separately and are combined with real
1262      insns during hash table computation.  */
1263   CGEN_INSN_TABLE macro_insn_table;
1264 
1265   /* Copy of CGEN_INT_INSN_P.  */
1266   int int_insn_p;
1267 
1268   /* Called to rebuild the tables after something has changed.  */
1269   void (*rebuild_tables) (CGEN_CPU_DESC);
1270 
1271   /* Operand parser callback.  */
1272   cgen_parse_operand_fn * parse_operand_fn;
1273 
1274   /* Parse/insert/extract/print cover fns for operands.  */
1275   const char * (*parse_operand)
1276     (CGEN_CPU_DESC, int opindex_, const char **, CGEN_FIELDS *fields_);
1277 #ifdef __BFD_H_SEEN__
1278   const char * (*insert_operand)
1279     (CGEN_CPU_DESC, int opindex_, CGEN_FIELDS *fields_,
1280      CGEN_INSN_BYTES_PTR, bfd_vma pc_);
1281   int (*extract_operand)
1282     (CGEN_CPU_DESC, int opindex_, CGEN_EXTRACT_INFO *, CGEN_INSN_INT,
1283      CGEN_FIELDS *fields_, bfd_vma pc_);
1284   void (*print_operand)
1285     (CGEN_CPU_DESC, int opindex_, void * info_, CGEN_FIELDS * fields_,
1286      void const *attrs_, bfd_vma pc_, int length_);
1287 #else
1288   const char * (*insert_operand) ();
1289   int (*extract_operand) ();
1290   void (*print_operand) ();
1291 #endif
1292 #define CGEN_CPU_PARSE_OPERAND(cd) ((cd)->parse_operand)
1293 #define CGEN_CPU_INSERT_OPERAND(cd) ((cd)->insert_operand)
1294 #define CGEN_CPU_EXTRACT_OPERAND(cd) ((cd)->extract_operand)
1295 #define CGEN_CPU_PRINT_OPERAND(cd) ((cd)->print_operand)
1296 
1297   /* Size of CGEN_FIELDS struct.  */
1298   unsigned int sizeof_fields;
1299 #define CGEN_CPU_SIZEOF_FIELDS(cd) ((cd)->sizeof_fields)
1300 
1301   /* Set the bitsize field.  */
1302   void (*set_fields_bitsize) (CGEN_FIELDS *fields_, int size_);
1303 #define CGEN_CPU_SET_FIELDS_BITSIZE(cd) ((cd)->set_fields_bitsize)
1304 
1305   /* CGEN_FIELDS accessors.  */
1306   int (*get_int_operand)
1307     (CGEN_CPU_DESC, int opindex_, const CGEN_FIELDS *fields_);
1308   void (*set_int_operand)
1309     (CGEN_CPU_DESC, int opindex_, CGEN_FIELDS *fields_, int value_);
1310 #ifdef __BFD_H_SEEN__
1311   bfd_vma (*get_vma_operand)
1312     (CGEN_CPU_DESC, int opindex_, const CGEN_FIELDS *fields_);
1313   void (*set_vma_operand)
1314     (CGEN_CPU_DESC, int opindex_, CGEN_FIELDS *fields_, bfd_vma value_);
1315 #else
1316   long (*get_vma_operand) ();
1317   void (*set_vma_operand) ();
1318 #endif
1319 #define CGEN_CPU_GET_INT_OPERAND(cd) ((cd)->get_int_operand)
1320 #define CGEN_CPU_SET_INT_OPERAND(cd) ((cd)->set_int_operand)
1321 #define CGEN_CPU_GET_VMA_OPERAND(cd) ((cd)->get_vma_operand)
1322 #define CGEN_CPU_SET_VMA_OPERAND(cd) ((cd)->set_vma_operand)
1323 
1324   /* Instruction parse/insert/extract/print handlers.  */
1325   /* FIXME: make these types uppercase.  */
1326   cgen_parse_fn * const *parse_handlers;
1327   cgen_insert_fn * const *insert_handlers;
1328   cgen_extract_fn * const *extract_handlers;
1329   cgen_print_fn * const *print_handlers;
1330 #define CGEN_PARSE_FN(cd, insn)   (cd->parse_handlers[(insn)->opcode->handlers.parse])
1331 #define CGEN_INSERT_FN(cd, insn)  (cd->insert_handlers[(insn)->opcode->handlers.insert])
1332 #define CGEN_EXTRACT_FN(cd, insn) (cd->extract_handlers[(insn)->opcode->handlers.extract])
1333 #define CGEN_PRINT_FN(cd, insn)   (cd->print_handlers[(insn)->opcode->handlers.print])
1334 
1335   /* Return non-zero if insn should be added to hash table.  */
1336   int (* asm_hash_p) (const CGEN_INSN *);
1337 
1338   /* Assembler hash function.  */
1339   unsigned int (* asm_hash) (const char *);
1340 
1341   /* Number of entries in assembler hash table.  */
1342   unsigned int asm_hash_size;
1343 
1344   /* Return non-zero if insn should be added to hash table.  */
1345   int (* dis_hash_p) (const CGEN_INSN *);
1346 
1347   /* Disassembler hash function.  */
1348   unsigned int (* dis_hash) (const char *, CGEN_INSN_INT);
1349 
1350   /* Number of entries in disassembler hash table.  */
1351   unsigned int dis_hash_size;
1352 
1353   /* Assembler instruction hash table.  */
1354   CGEN_INSN_LIST **asm_hash_table;
1355   CGEN_INSN_LIST *asm_hash_table_entries;
1356 
1357   /* Disassembler instruction hash table.  */
1358   CGEN_INSN_LIST **dis_hash_table;
1359   CGEN_INSN_LIST *dis_hash_table_entries;
1360 
1361   /* This field could be turned into a bitfield if room for other flags is needed.  */
1362   unsigned int signed_overflow_ok_p;
1363 
1364 } CGEN_CPU_TABLE;
1365 
1366 /* wip */
1367 #ifndef CGEN_WORD_ENDIAN
1368 #define CGEN_WORD_ENDIAN(cd) CGEN_CPU_ENDIAN (cd)
1369 #endif
1370 #ifndef CGEN_INSN_WORD_ENDIAN
1371 #define CGEN_INSN_WORD_ENDIAN(cd) CGEN_CPU_INSN_ENDIAN (cd)
1372 #endif
1373 
1374 /* Prototypes of major functions.  */
1375 /* FIXME: Move more CGEN_SYM-defined functions into CGEN_CPU_DESC.
1376    Not the init fns though, as that would drag in things that mightn't be
1377    used and might not even exist.  */
1378 
1379 /* Argument types to cpu_open.  */
1380 
1381 enum cgen_cpu_open_arg {
1382   CGEN_CPU_OPEN_END,
1383   /* Select instruction set(s), arg is bitmap or 0 meaning "unspecified".  */
1384   CGEN_CPU_OPEN_ISAS,
1385   /* Select machine(s), arg is bitmap or 0 meaning "unspecified".  */
1386   CGEN_CPU_OPEN_MACHS,
1387   /* Select machine, arg is mach's bfd name.
1388      Multiple machines can be specified by repeated use.  */
1389   CGEN_CPU_OPEN_BFDMACH,
1390   /* Select endian, arg is CGEN_ENDIAN_*.  */
1391   CGEN_CPU_OPEN_ENDIAN
1392 };
1393 
1394 /* Open a cpu descriptor table for use.
1395    ??? We only support ISO C stdargs here, not K&R.
1396    Laziness, plus experiment to see if anything requires K&R - eventually
1397    K&R will no longer be supported - e.g. GDB is currently trying this.  */
1398 
1399 extern CGEN_CPU_DESC CGEN_SYM (cpu_open) (enum cgen_cpu_open_arg, ...);
1400 
1401 /* Cover fn to handle simple case.  */
1402 
1403 extern CGEN_CPU_DESC CGEN_SYM (cpu_open_1)
1404    (const char *mach_name_, enum cgen_endian endian_);
1405 
1406 /* Close it.  */
1407 
1408 extern void CGEN_SYM (cpu_close) (CGEN_CPU_DESC);
1409 
1410 /* Initialize the opcode table for use.
1411    Called by init_asm/init_dis.  */
1412 
1413 extern void CGEN_SYM (init_opcode_table) (CGEN_CPU_DESC cd_);
1414 
1415 /* build the insn selection regex.
1416    called by init_opcode_table */
1417 
1418 extern char * CGEN_SYM(build_insn_regex) (CGEN_INSN *insn_);
1419 
1420 /* Initialize the ibld table for use.
1421    Called by init_asm/init_dis.  */
1422 
1423 extern void CGEN_SYM (init_ibld_table) (CGEN_CPU_DESC cd_);
1424 
1425 /* Initialize an cpu table for assembler or disassembler use.
1426    These must be called immediately after cpu_open.  */
1427 
1428 extern void CGEN_SYM (init_asm) (CGEN_CPU_DESC);
1429 extern void CGEN_SYM (init_dis) (CGEN_CPU_DESC);
1430 
1431 /* Initialize the operand instance table for use.  */
1432 
1433 extern void CGEN_SYM (init_opinst_table) (CGEN_CPU_DESC cd_);
1434 
1435 /* Assemble an instruction.  */
1436 
1437 extern const CGEN_INSN * CGEN_SYM (assemble_insn)
1438   (CGEN_CPU_DESC, const char *, CGEN_FIELDS *,
1439    CGEN_INSN_BYTES_PTR, char **);
1440 
1441 extern const CGEN_KEYWORD CGEN_SYM (operand_mach);
1442 extern int CGEN_SYM (get_mach) (const char *);
1443 
1444 /* Operand index computation.  */
1445 extern const CGEN_INSN * cgen_lookup_insn
1446   (CGEN_CPU_DESC, const CGEN_INSN * insn_,
1447    CGEN_INSN_INT int_value_, unsigned char *bytes_value_,
1448    int length_, CGEN_FIELDS *fields_, int alias_p_);
1449 extern void cgen_get_insn_operands
1450   (CGEN_CPU_DESC, const CGEN_INSN * insn_,
1451    const CGEN_FIELDS *fields_, int *indices_);
1452 extern const CGEN_INSN * cgen_lookup_get_insn_operands
1453   (CGEN_CPU_DESC, const CGEN_INSN *insn_,
1454    CGEN_INSN_INT int_value_, unsigned char *bytes_value_,
1455    int length_, int *indices_, CGEN_FIELDS *fields_);
1456 
1457 /* Cover fns to bfd_get/set.  */
1458 
1459 extern CGEN_INSN_INT cgen_get_insn_value
1460   (CGEN_CPU_DESC, unsigned char *, int);
1461 extern void cgen_put_insn_value
1462   (CGEN_CPU_DESC, unsigned char *, int, CGEN_INSN_INT);
1463 
1464 /* Read in a cpu description file.
1465    ??? For future concerns, including adding instructions to the assembler/
1466    disassembler at run-time.  */
1467 
1468 extern const char * cgen_read_cpu_file (CGEN_CPU_DESC, const char * filename_);
1469 
1470 /* Allow signed overflow of instruction fields.  */
1471 extern void cgen_set_signed_overflow_ok (CGEN_CPU_DESC);
1472 
1473 /* Generate an error message if a signed field in an instruction overflows.  */
1474 extern void cgen_clear_signed_overflow_ok (CGEN_CPU_DESC);
1475 
1476 /* Will an error message be generated if a signed field in an instruction overflows ? */
1477 extern unsigned int cgen_signed_overflow_ok_p (CGEN_CPU_DESC);
1478 
1479 #endif /* OPCODE_CGEN_H */
1480