Subversion Repositories Kolibri OS

Rev

Rev 5191 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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