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