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