Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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