* cgen.h (CGEN_ATTR_TYPE): Delete `const', moved to uses.
[platform/upstream/binutils.git] / include / opcode / cgen.h
1 /* Header file for targets using CGEN: Cpu tools GENerator.
2
3 Copyright (C) 1996, 1997, 1998 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 2 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #ifndef CGEN_H
22 #define CGEN_H
23
24 /* Prepend the cpu name, defined in cpu-opc.h, and _cgen_ to symbol S.
25    The lack of spaces in the arg list is important for non-stdc systems.
26    This file is included by <cpu>-opc.h.
27    It can be included independently of cpu-opc.h, in which case the cpu
28    dependent portions will be declared as "unknown_cgen_foo".  */
29
30 #ifndef CGEN_SYM
31 #define CGEN_SYM(s) CONCAT3 (unknown,_cgen_,s)
32 #endif
33
34 /* This file contains the static (unchanging) pieces and as much other stuff
35    as we can reasonably put here.  It's generally cleaner to put stuff here
36    rather than having it machine generated if possible.  */
37
38 /* The assembler syntax is made up of expressions (duh...).
39    At the lowest level the values are mnemonics, register names, numbers, etc.
40    Above that are subexpressions, if any (an example might be the
41    "effective address" in m68k cpus).  At the second highest level are the
42    insns themselves.  Above that are pseudo-insns, synthetic insns, and macros,
43    if any.
44 */
45 \f
46 /* Lots of cpu's have a fixed insn size, or one which rarely changes,
47    and it's generally easier to handle these by treating the insn as an
48    integer type, rather than an array of characters.  So we allow targets
49    to control this.  */
50
51 #ifdef CGEN_INT_INSN
52 typedef unsigned int cgen_insn_t;
53 #else
54 typedef char * cgen_insn_t;
55 #endif
56
57 #ifdef __GNUC__
58 #define CGEN_INLINE inline
59 #else
60 #define CGEN_INLINE
61 #endif
62
63 /* Perhaps we should just use bfd.h, but it's not clear
64    one would want to require that yet.  */
65 enum cgen_endian
66 {
67   CGEN_ENDIAN_UNKNOWN,
68   CGEN_ENDIAN_LITTLE,
69   CGEN_ENDIAN_BIG
70 };
71 \f
72 /* Attributes.
73    Attributes are used to describe various random things.  */
74
75 /* Struct to record attribute information.  */
76 typedef struct
77 {
78   unsigned char num_nonbools;
79   unsigned int bool;
80   unsigned int nonbool[1];
81 } CGEN_ATTR;
82
83 /* Define a structure member for attributes with N non-boolean entries.
84    The attributes are sorted so that the non-boolean ones come first.
85    num_nonbools: count of nonboolean attributes
86    bool: values of boolean attributes
87    nonbool: values of non-boolean attributes
88    There is a maximum of 32 attributes total.  */
89 #define CGEN_ATTR_TYPE(n) \
90 struct { unsigned char num_nonbools; \
91            unsigned int bool; \
92            unsigned int nonbool[(n) ? (n) : 1]; }
93
94 /* Given an attribute number, return its mask.  */
95 #define CGEN_ATTR_MASK(attr) (1 << (attr))
96
97 /* Return the value of boolean attribute ATTR in ATTRS.  */
98 #define CGEN_BOOL_ATTR(attrs, attr) \
99 ((CGEN_ATTR_MASK (attr) & (attrs)) != 0)
100
101 /* Return value of attribute ATTR in ATTR_TABLE for OBJ.
102    OBJ is a pointer to the entity that has the attributes.
103    It's not used at present but is reserved for future purposes.  */
104 #define CGEN_ATTR_VALUE(obj, attr_table, attr) \
105 ((unsigned int) (attr) < (attr_table)->num_nonbools \
106  ? ((attr_table)->nonbool[attr]) \
107  : (((attr_table)->bool & (1 << (attr))) != 0))
108
109 /* Attribute name/value tables.
110    These are used to assist parsing of descriptions at runtime.  */
111
112 typedef struct
113 {
114   const char * name;
115   int value;
116 } CGEN_ATTR_ENTRY;
117
118 /* For each domain (fld,operand,insn), list of attributes.  */
119
120 typedef struct
121 {
122   const char * name;
123   /* NULL for boolean attributes.  */
124   const CGEN_ATTR_ENTRY * vals;
125 } CGEN_ATTR_TABLE;
126 \f
127 /* Parse result (also extraction result).
128
129    The result of parsing an insn is stored here.
130    To generate the actual insn, this is passed to the insert handler.
131    When printing an insn, the result of extraction is stored here.
132    To print the insn, this is passed to the print handler.
133
134    It is machine generated so we don't define it here,
135    but we do need a forward decl for the handler fns.
136
137    There is one member for each possible field in the insn.
138    The type depends on the field.
139    Also recorded here is the computed length of the insn for architectures
140    where it varies.
141 */
142
143 typedef struct cgen_fields CGEN_FIELDS;
144
145 /* Total length of the insn, as recorded in the `fields' struct.  */
146 /* ??? The field insert handler has lots of opportunities for optimization
147    if it ever gets inlined.  On architectures where insns all have the same
148    size, may wish to detect that and make this macro a constant - to allow
149    further optimizations.  */
150 #define CGEN_FIELDS_BITSIZE(fields) ((fields)->length)
151 \f
152 /* Associated with each insn or expression is a set of "handlers" for
153    performing operations like parsing, printing, etc.  */
154
155 /* Forward decl.  */
156 typedef struct cgen_insn CGEN_INSN;
157
158 /* Parse handler.
159    The first argument is a pointer to a struct describing the insn being
160    parsed.
161    The second argument is a pointer to a pointer to the text being parsed.
162    The third argument is a pointer to a cgen_fields struct
163    in which the results are placed.
164    If the expression is successfully parsed, the pointer to the text is
165    updated.  If not it is left alone.
166    The result is NULL if success or an error message.  */
167 typedef const char * (cgen_parse_fn) PARAMS ((const struct cgen_insn *,
168                                               const char **,
169                                               CGEN_FIELDS *));
170
171 /* Print handler.
172    The first argument is a pointer to the disassembly info.
173    Eg: disassemble_info.  It's defined as `PTR' so this file can be included
174    without dis-asm.h.
175    The second argument is a pointer to a struct describing the insn being
176    printed.
177    The third argument is a pointer to a cgen_fields struct.
178    The fourth argument is the pc value of the insn.
179    The fifth argument is the length of the insn, in bytes.  */
180 /* Don't require bfd.h unnecessarily.  */
181 #ifdef BFD_VERSION
182 typedef void (cgen_print_fn) PARAMS ((PTR, const struct cgen_insn *,
183                                       CGEN_FIELDS *, bfd_vma, int));
184 #else
185 typedef void (cgen_print_fn) ();
186 #endif
187
188 /* Insert handler.
189    The first argument is a pointer to a struct describing the insn being
190    parsed.
191    The second argument is a pointer to a cgen_fields struct
192    from which the values are fetched.
193    The third argument is a pointer to a buffer in which to place the insn.
194    The result is an error message or NULL if success.  */
195 typedef const char * (cgen_insert_fn) PARAMS ((const struct cgen_insn *,
196                                                CGEN_FIELDS *, cgen_insn_t *));
197
198 /* Extract handler.
199    The first argument is a pointer to a struct describing the insn being
200    parsed.
201    The second argument is a pointer to a struct controlling extraction
202    (only used for variable length insns).
203    The third argument is the first CGEN_BASE_INSN_SIZE bytes.
204    The fourth argument is a pointer to a cgen_fields struct
205    in which the results are placed.
206    The result is the length of the insn or zero if not recognized.  */
207 typedef int (cgen_extract_fn) PARAMS ((const struct cgen_insn *,
208                                        void *, cgen_insn_t,
209                                        CGEN_FIELDS *));
210
211 /* The `parse' and `insert' fields are indices into these tables.
212    The elements are pointer to specialized handler functions.
213    Element 0 is special, it means use the default handler.  */
214 extern cgen_parse_fn * CGEN_SYM (parse_handlers) [];
215 #define CGEN_PARSE_FN(x) (CGEN_SYM (parse_handlers)[(x)->base.parse])
216 extern cgen_insert_fn * CGEN_SYM (insert_handlers) [];
217 #define CGEN_INSERT_FN(x) (CGEN_SYM (insert_handlers)[(x)->base.insert])
218
219 /* Likewise for the `extract' and `print' fields.  */
220 extern cgen_extract_fn * CGEN_SYM (extract_handlers) [];
221 #define CGEN_EXTRACT_FN(x) (CGEN_SYM (extract_handlers)[(x)->base.extract])
222 extern cgen_print_fn * CGEN_SYM (print_handlers) [];
223 #define CGEN_PRINT_FN(x) (CGEN_SYM (print_handlers)[(x)->base.print])
224 \f
225 /* Base class of parser/printer.
226    (Don't read too much into the use of the phrase "base class".
227    It's a name I'm using to organize my thoughts.)
228
229    Instructions and expressions all share this data in common.
230    It's a collection of the common elements needed to parse, insert, extract,
231    and print each of them.  */
232
233 struct cgen_base
234 {
235   /* Indices into the handler tables.
236      We could use pointers here instead, but in the case of the insn table,
237      90% of them would be identical and that's a lot of redundant data.
238      0 means use the default (what the default is is up to the code).
239      Using indices also keeps assembler code out of the disassembler and
240      vice versa.  */
241   unsigned char parse, insert, extract, print;
242 };
243 \f
244 /* Assembler interface.
245
246    The interface to the assembler is intended to be clean in the sense that
247    libopcodes.a is a standalone entity and could be used with any assembler.
248    Not that one would necessarily want to do that but rather that it helps
249    keep a clean interface.  The interface will obviously be slanted towards
250    GAS, but at least it's a start.
251
252    Parsing is controlled by the assembler which calls
253    CGEN_SYM (assemble_insn).  If it can parse and build the entire insn
254    it doesn't call back to the assembler.  If it needs/wants to call back
255    to the assembler, (*cgen_parse_operand_fn) is called which can either
256
257    - return a number to be inserted in the insn
258    - return a "register" value to be inserted
259      (the register might not be a register per pe)
260    - queue the argument and return a marker saying the expression has been
261      queued (eg: a fix-up)
262    - return an error message indicating the expression wasn't recognizable
263
264    The result is an error message or NULL for success.
265    The parsed value is stored in the bfd_vma *.  */
266
267 /* Values for indicating what the caller wants.  */
268 enum cgen_parse_operand_type
269 {
270   CGEN_PARSE_OPERAND_INIT,
271   CGEN_PARSE_OPERAND_INTEGER,
272   CGEN_PARSE_OPERAND_ADDRESS
273 };
274
275 /* Values for indicating what was parsed.
276    ??? Not too useful at present but in time.  */
277 enum cgen_parse_operand_result
278 {
279   CGEN_PARSE_OPERAND_RESULT_NUMBER,
280   CGEN_PARSE_OPERAND_RESULT_REGISTER,
281   CGEN_PARSE_OPERAND_RESULT_QUEUED,
282   CGEN_PARSE_OPERAND_RESULT_ERROR
283 };
284
285 /* Don't require bfd.h unnecessarily.  */
286 #ifdef BFD_VERSION
287 extern const char * (*cgen_parse_operand_fn)
288      PARAMS ((enum cgen_parse_operand_type, const char **, int, int,
289               enum cgen_parse_operand_result *, bfd_vma *));
290 #endif
291
292 /* Called before trying to match a table entry with the insn.  */
293 extern void cgen_init_parse_operand PARAMS ((void));
294
295 /* Called from <cpu>-asm.c to initialize operand parsing.  */
296
297 /* These are GAS specific.  They're not here as part of the interface,
298    but rather that we need to put them somewhere.  */
299
300 /* Call this from md_assemble to initialize the assembler callback.  */
301 extern void cgen_asm_init_parse PARAMS ((void));
302
303 /* Don't require bfd.h unnecessarily.  */
304 #ifdef BFD_VERSION
305 /* The result is an error message or NULL for success.
306    The parsed value is stored in the bfd_vma *.  */
307 extern const char * cgen_parse_operand
308      PARAMS ((enum cgen_parse_operand_type,
309               const char **, int, int,
310               enum cgen_parse_operand_result *,
311               bfd_vma *));
312 #endif
313
314 extern void cgen_save_fixups PARAMS ((void));
315 extern void cgen_restore_fixups PARAMS ((void));
316 extern void cgen_swap_fixups PARAMS ((void));
317      
318 /* Add a register to the assembler's hash table.
319    This makes lets GAS parse registers for us.
320    ??? This isn't currently used, but it could be in the future.  */
321 extern void cgen_asm_record_register PARAMS ((char *, int));
322
323 /* After CGEN_SYM (assemble_insn) is done, this is called to
324    output the insn and record any fixups.  The address of the
325    assembled instruction is returned in case it is needed by
326    the caller.  */
327 extern char * cgen_asm_finish_insn PARAMS ((const struct cgen_insn *,
328                                             cgen_insn_t *, unsigned int, int));
329 \f
330 /* Operand values (keywords, integers, symbols, etc.)  */
331
332 /* Types of assembler elements.  */
333
334 enum cgen_asm_type
335 {
336   CGEN_ASM_KEYWORD, CGEN_ASM_MAX
337 };
338
339 /* List of hardware elements.  */
340
341 typedef struct cgen_hw_entry
342 {
343   /* The type of this entry, one of `enum hw_type'.
344      This is an int and not the enum as the latter may not be declared yet.  */
345   int type;
346   const struct cgen_hw_entry * next;
347   char * name;
348   enum cgen_asm_type asm_type;
349   PTR asm_data;
350 } CGEN_HW_ENTRY;
351
352 extern const CGEN_HW_ENTRY * cgen_hw_lookup PARAMS ((const char *));
353
354 /* This struct is used to describe things like register names, etc.  */
355
356 typedef struct cgen_keyword_entry
357 {
358   /* Name (as in register name).  */
359   char * name;
360
361   /* Value (as in register number).
362      The value cannot be -1 as that is used to indicate "not found".
363      IDEA: Have "FUNCTION" attribute? [function is called to fetch value].  */
364   int value;
365
366   /* Attributes.
367      This should, but technically needn't, appear last.  It is a variable sized
368      array in that one architecture may have 1 nonbool attribute and another
369      may have more.  Having this last means the non-architecture specific code
370      needn't care.  */
371   /* ??? Moving this last should be done by treating keywords like insn lists
372      and moving the `next' fields into a CGEN_KEYWORD_LIST struct.  */
373   /* FIXME: Not used yet.  */
374 #ifndef CGEN_KEYWORD_NBOOL_ATTRS
375 #define CGEN_KEYWORD_NBOOL_ATTRS 1
376 #endif
377   const CGEN_ATTR_TYPE (CGEN_KEYWORD_NBOOL_ATTRS) attrs;
378
379   /* Next name hash table entry.  */
380   struct cgen_keyword_entry *next_name;
381   /* Next value hash table entry.  */
382   struct cgen_keyword_entry *next_value;
383 } CGEN_KEYWORD_ENTRY;
384
385 /* Top level struct for describing a set of related keywords
386    (e.g. register names).
387
388    This struct supports runtime entry of new values, and hashed lookups.  */
389
390 typedef struct cgen_keyword
391 {
392   /* Pointer to initial [compiled in] values.  */
393   CGEN_KEYWORD_ENTRY * init_entries;
394   
395   /* Number of entries in `init_entries'.  */
396   unsigned int num_init_entries;
397   
398   /* Hash table used for name lookup.  */
399   CGEN_KEYWORD_ENTRY ** name_hash_table;
400   
401   /* Hash table used for value lookup.  */
402   CGEN_KEYWORD_ENTRY ** value_hash_table;
403   
404   /* Number of entries in the hash_tables.  */
405   unsigned int hash_table_size;
406   
407   /* Pointer to null keyword "" entry if present.  */
408   const CGEN_KEYWORD_ENTRY * null_entry;
409 } CGEN_KEYWORD;
410
411 /* Structure used for searching.  */
412
413 typedef struct
414 {
415   /* Table being searched.  */
416   const CGEN_KEYWORD * table;
417   
418   /* Specification of what is being searched for.  */
419   const char * spec;
420   
421   /* Current index in hash table.  */
422   unsigned int current_hash;
423   
424   /* Current element in current hash chain.  */
425   CGEN_KEYWORD_ENTRY * current_entry;
426 } CGEN_KEYWORD_SEARCH;
427
428 /* Lookup a keyword from its name.  */
429 const CGEN_KEYWORD_ENTRY * cgen_keyword_lookup_name
430   PARAMS ((CGEN_KEYWORD *, const char *));
431 /* Lookup a keyword from its value.  */
432 const CGEN_KEYWORD_ENTRY * cgen_keyword_lookup_value
433   PARAMS ((CGEN_KEYWORD *, int));
434 /* Add a keyword.  */
435 void cgen_keyword_add PARAMS ((CGEN_KEYWORD *, CGEN_KEYWORD_ENTRY *));
436 /* Keyword searching.
437    This can be used to retrieve every keyword, or a subset.  */
438 CGEN_KEYWORD_SEARCH cgen_keyword_search_init
439   PARAMS ((CGEN_KEYWORD *, const char *));
440 const CGEN_KEYWORD_ENTRY *cgen_keyword_search_next
441   PARAMS ((CGEN_KEYWORD_SEARCH *));
442
443 /* Operand value support routines.  */
444 /* FIXME: some of the long's here will need to be bfd_vma or some such.  */
445
446 extern const char * cgen_parse_keyword PARAMS ((const char **,
447                                                 CGEN_KEYWORD *,
448                                                 long *));
449 extern const char * cgen_parse_signed_integer PARAMS ((const char **, int,
450                                                        long *));
451 extern const char * cgen_parse_unsigned_integer PARAMS ((const char **, int,
452                                                          unsigned long *));
453 extern const char * cgen_parse_address PARAMS ((const char **, int, int,
454                                                 enum cgen_parse_operand_result *,
455                                                 long *));
456 extern const char * cgen_validate_signed_integer PARAMS ((long, long, long));
457 extern const char * cgen_validate_unsigned_integer PARAMS ((unsigned long,
458                                                             unsigned long,
459                                                             unsigned long));
460 \f
461 /* Operand modes.  */
462
463 /* ??? This duplicates the values in arch.h.  Revisit.
464    These however need the CGEN_ prefix [as does everything in this file].  */
465 /* ??? Targets may need to add their own modes so we may wish to move this
466    to <arch>-opc.h, or add a hook.  */
467
468 enum cgen_mode {
469   CGEN_MODE_VOID, /* FIXME: rename simulator's VM to VOID */
470   CGEN_MODE_BI, CGEN_MODE_QI, CGEN_MODE_HI, CGEN_MODE_SI, CGEN_MODE_DI,
471   CGEN_MODE_UBI, CGEN_MODE_UQI, CGEN_MODE_UHI, CGEN_MODE_USI, CGEN_MODE_UDI,
472   CGEN_MODE_SF, CGEN_MODE_DF, CGEN_MODE_XF, CGEN_MODE_TF,
473   CGEN_MODE_MAX
474 };
475
476 /* FIXME: Until simulator is updated.  */
477 #define CGEN_MODE_VM CGEN_MODE_VOID
478 \f
479 /* This struct defines each entry in the operand table.  */
480
481 typedef struct cgen_operand
482 {
483   /* Name as it appears in the syntax string.  */
484   char * name;
485
486   /* The hardware element associated with this operand.  */
487   const CGEN_HW_ENTRY *hw;
488
489   /* FIXME: We don't yet record ifield definitions, which we should.
490      When we do it might make sense to delete start/length (since they will
491      be duplicated in the ifield's definition) and replace them with a
492      pointer to the ifield entry.  Note that as more complicated situations
493      need to be handled, going more and more with an OOP paradigm will help
494      keep the complication under control.  Of course, this was the goal from
495      the start, but getting there in one step was too much too soon.  */
496
497   /* Bit position (msb of first byte = bit 0).
498      This is just a hint, and may be unused in more complex operands.
499      May be unused for a modifier.  */
500   unsigned char start;
501
502   /* The number of bits in the operand.
503      This is just a hint, and may be unused in more complex operands.
504      May be unused for a modifier.  */
505   unsigned char length;
506
507 #if 0 /* ??? Interesting idea but relocs tend to get too complicated,
508          and ABI dependent, for simple table lookups to work.  */
509   /* Ideally this would be the internal (external?) reloc type.  */
510   int reloc_type;
511 #endif
512
513   /* Attributes.
514      This should, but technically needn't, appear last.  It is a variable sized
515      array in that one architecture may have 1 nonbool attribute and another
516      may have more.  Having this last means the non-architecture specific code
517      needn't care, now or tomorrow.  */
518 #ifndef CGEN_OPERAND_NBOOL_ATTRS
519 #define CGEN_OPERAND_NBOOL_ATTRS 1
520 #endif
521   const CGEN_ATTR_TYPE (CGEN_OPERAND_NBOOL_ATTRS) attrs;
522 #define CGEN_OPERAND_ATTRS(operand) (&(operand)->attrs)
523 } CGEN_OPERAND;
524
525 /* Return value of attribute ATTR in OPERAND.  */
526 #define CGEN_OPERAND_ATTR(operand, attr) \
527 CGEN_ATTR_VALUE (operand, CGEN_OPERAND_ATTRS (operand), attr)
528
529 /* The operand table is currently a very static entity.  */
530 extern const CGEN_OPERAND CGEN_SYM (operand_table)[];
531
532 enum cgen_operand_type;
533
534 #define CGEN_OPERAND_INDEX(operand) ((int) ((operand) - CGEN_SYM (operand_table)))
535 /* FIXME: Rename, cpu-opc.h defines this as the typedef of the enum.  */
536 #define CGEN_OPERAND_TYPE(operand) ((enum cgen_operand_type) CGEN_OPERAND_INDEX (operand))
537 #define CGEN_OPERAND_ENTRY(n) (& CGEN_SYM (operand_table) [n])
538
539 /* Types of parse/insert/extract/print cover-fn handlers.  */
540 /* FIXME: move opindex first to match caller.  */
541 /* FIXME: also need types of insert/extract/print fns.  */
542 /* FIXME: not currently used as type of 3rd arg varies.  */
543 typedef const char * (CGEN_PARSE_OPERAND_FN) PARAMS ((const char **, int,
544                                                       long *));
545 \f
546 /* Instruction operand instances.
547
548    For each instruction, a list of the hardware elements that are read and
549    written are recorded.  */
550
551 /* The type of the instance.  */
552 enum cgen_operand_instance_type {
553   /* End of table marker.  */
554   CGEN_OPERAND_INSTANCE_END = 0,
555   CGEN_OPERAND_INSTANCE_INPUT, CGEN_OPERAND_INSTANCE_OUTPUT
556 };
557
558 typedef struct
559 {
560   /* The type of this operand.  */
561   enum cgen_operand_instance_type type;
562 #define CGEN_OPERAND_INSTANCE_TYPE(opinst) ((opinst)->type)
563
564   /* The hardware element referenced.  */
565   const CGEN_HW_ENTRY *hw;
566 #define CGEN_OPERAND_INSTANCE_HW(opinst) ((opinst)->hw)
567
568   /* The mode in which the operand is being used.  */
569   enum cgen_mode mode;
570 #define CGEN_OPERAND_INSTANCE_MODE(opinst) ((opinst)->mode)
571
572   /* The operand table entry or NULL if there is none (i.e. an explicit
573      hardware reference).  */
574   const CGEN_OPERAND *operand;
575 #define CGEN_OPERAND_INSTANCE_OPERAND(opinst) ((opinst)->operand)
576
577   /* If `operand' is NULL, the index (e.g. into array of registers).  */
578   int index;
579 #define CGEN_OPERAND_INSTANCE_INDEX(opinst) ((opinst)->index)
580 } CGEN_OPERAND_INSTANCE;
581 \f
582 /* Syntax string.
583
584    Each insn format and subexpression has one of these.
585
586    The syntax "string" consists of characters (n > 0 && n < 128), and operand
587    values (n >= 128), and is terminated by 0.  Operand values are 128 + index
588    into the operand table.  The operand table doesn't exist in C, per se, as
589    the data is recorded in the parse/insert/extract/print switch statements. */
590
591 #ifndef CGEN_MAX_SYNTAX_BYTES
592 #define CGEN_MAX_SYNTAX_BYTES 16
593 #endif
594
595 typedef struct
596 {
597   unsigned char syntax[CGEN_MAX_SYNTAX_BYTES];
598 } CGEN_SYNTAX;
599
600 #define CGEN_SYNTAX_STRING(syn) (syn->syntax)
601 #define CGEN_SYNTAX_CHAR_P(c) ((c) < 128)
602 #define CGEN_SYNTAX_CHAR(c) (c)
603 #define CGEN_SYNTAX_FIELD(c) ((c) - 128)
604 #define CGEN_SYNTAX_MAKE_FIELD(c) ((c) + 128)
605
606 /* ??? I can't currently think of any case where the mnemonic doesn't come
607    first [and if one ever doesn't building the hash tables will be tricky].
608    However, we treat mnemonics as just another operand of the instruction.
609    A value of 1 means "this is where the mnemonic appears".  1 isn't
610    special other than it's a non-printable ASCII char.  */
611 #define CGEN_SYNTAX_MNEMONIC       1
612 #define CGEN_SYNTAX_MNEMONIC_P(ch) ((ch) == CGEN_SYNTAX_MNEMONIC)
613 \f
614 /* Instruction formats.
615
616    Instructions are grouped by format.  Associated with an instruction is its
617    format.  Each opcode table entry contains a format table entry.
618    ??? There is usually very few formats compared with the number of insns,
619    so one can reduce the size of the opcode table by recording the format table
620    as a separate entity.  Given that we currently don't, format table entries
621    are also distinguished by their operands.  This increases the size of the
622    table, but reduces the number of tables.  It's all minutiae anyway so it
623    doesn't really matter [at this point in time].
624
625    ??? Support for variable length ISA's is wip.  */
626
627 typedef struct
628 {
629   /* Length that MASK and VALUE have been calculated to
630      [VALUE is recorded elsewhere].
631      Normally it is CGEN_BASE_INSN_BITSIZE.  On [V]LIW architectures where
632      the base insn size may be larger than the size of an insn, this field is
633      less than CGEN_BASE_INSN_BITSIZE.  */
634   unsigned char mask_length;
635
636   /* Total length of instruction, in bits.  */
637   unsigned char length;
638
639   /* Mask to apply to the first MASK_LENGTH bits.
640      Each insn's value is stored with the insn.
641      The first step in recognizing an insn for disassembly is
642      (opcode & mask) == value.  */
643   unsigned int mask;
644 } CGEN_FORMAT;
645 \f
646 /* This struct defines each entry in the instruction table.  */
647
648 struct cgen_insn
649 {
650   /* This field is an array of functions that operand on this entry.  */
651   struct cgen_base base;
652 #define CGEN_INSN_BASE(insn) (&(insn)->base)
653
654   /* Each real instruction is enumerated.  This is used, for example, to do
655      insn profiling in the simulator.
656      Macro-insns are not enumerated.  The simulator doesn't use them and there
657      is currently no other need for it.  */
658   int num;
659 #define CGEN_INSN_NUM(insn) ((insn)->num)
660
661   /* Name of entry (that distinguishes it from all other entries).
662      This is used, for example, in simulator profiling results.  */
663   /* ??? If mnemonics have operands, try to print full mnemonic.  */
664   const char * name;
665 #define CGEN_INSN_NAME(insn) ((insn)->name)
666
667   /* Mnemonic.  This is used when parsing and printing the insn.
668      In the case of insns that have operands on the mnemonics, this is
669      only the constant part.  E.g. for conditional execution of an `add' insn,
670      where the full mnemonic is addeq, addne, etc., this is only "add".  */
671   const char * mnemonic;
672 #define CGEN_INSN_MNEMONIC(insn) ((insn)->mnemonic)
673
674   /* Syntax string.  */
675   const CGEN_SYNTAX syntax;
676 #define CGEN_INSN_SYNTAX(insn) (& (insn)->syntax)
677
678   /* Format entry.  */
679   const CGEN_FORMAT format;
680 #define CGEN_INSN_MASK_BITSIZE(insn) ((insn)->format.mask_length)
681 #define CGEN_INSN_BITSIZE(insn) ((insn)->format.length)
682
683   /* Instruction opcode value.  */
684   unsigned int value;
685 #define CGEN_INSN_VALUE(insn) ((insn)->value)
686 #define CGEN_INSN_MASK(insn) ((insn)->format.mask)
687
688   /* Opaque pointer to "subclass" specific data.
689      In the case of real insns this points to a NULL entry terminated
690      table of operands used, or NULL if none.
691      In the case of macro insns this points to data to control the expansion.
692      ??? I'd rather not get carried away and lay things out with pedantic
693      purity right now.  Sure, other fields might better be tucked away in
694      `data'.  Not now.  */
695   PTR data;
696 #define CGEN_INSN_DATA(insn) ((insn)->data)
697 #define CGEN_INSN_OPERANDS(insn) ((CGEN_OPERAND_INSTANCE *) (insn)->data)
698
699   /* Attributes.
700      This must appear last.  It is a variable sized array in that one
701      architecture may have 1 nonbool attribute and another may have more.
702      Having this last means the non-architecture specific code needn't
703      care.  */
704 #ifndef CGEN_INSN_NBOOL_ATTRS
705 #define CGEN_INSN_NBOOL_ATTRS 1
706 #endif
707   const CGEN_ATTR_TYPE (CGEN_INSN_NBOOL_ATTRS) attrs;
708 #define CGEN_INSN_ATTRS(insn) (&(insn)->attrs)
709 /* Return value of attribute ATTR in INSN.  */
710 #define CGEN_INSN_ATTR(insn, attr) \
711 CGEN_ATTR_VALUE (insn, CGEN_INSN_ATTRS (insn), attr)
712 };
713
714 /* Instruction lists.
715    This is used for adding new entries and for creating the hash lists.  */
716
717 typedef struct cgen_insn_list
718 {
719   struct cgen_insn_list * next;
720   const CGEN_INSN * insn;
721 } CGEN_INSN_LIST;
722
723 /* The table of instructions.  */
724
725 typedef struct
726 {
727   /* Pointer to initial [compiled in] entries.  */
728   const CGEN_INSN * init_entries;
729   
730   /* Size of an entry (since the attribute member is variable sized).  */
731   unsigned int entry_size;
732   
733   /* Number of entries in `init_entries', including trailing NULL entry.  */
734   unsigned int num_init_entries;
735   
736   /* Values added at runtime.  */
737   CGEN_INSN_LIST * new_entries;
738 } CGEN_INSN_TABLE;
739
740 /* ??? This is currently used by the simulator.  */
741 extern const CGEN_INSN CGEN_SYM (insn_table_entries)[];
742
743 /* Return number of instructions.  This includes any added at runtime.  */
744
745 extern int cgen_insn_count PARAMS ((void));
746 \f
747 /* Macro instructions.
748    Macro insns aren't real insns, they map to one or more real insns.
749    E.g. An architecture's "nop" insn may actually be an "mv r0,r0" or
750    some such.
751
752    Macro insns can expand to nothing (e.g. a nop that is optimized away).
753    This is useful in multi-insn macros that build a constant in a register.
754    Of course this isn't the default behaviour and must be explicitly enabled.
755
756    Assembly of macro-insns is relatively straightforward.  Disassembly isn't.
757    However, disassembly of at least some kinds of macro insns is important
758    in order that the disassembled code preserve the readability of the original
759    insn.  What is attempted here is to disassemble all "simple" macro-insns,
760    where "simple" is currently defined to mean "expands to one real insn".
761
762    Simple macro-insns are handled specially.  They are emitted as ALIAS's
763    of real insns.  This simplifies their handling since there's usually more
764    of them than any other kind of macro-insn, and proper disassembly of them
765    falls out for free.  */
766
767 /* For each macro-insn there may be multiple expansion possibilities,
768    depending on the arguments.  This structure is accessed via the `data'
769    member of CGEN_INSN.  */
770
771 typedef struct cgen_minsn_expansion {
772   /* Function to do the expansion.
773      If the expansion fails (e.g. "no match") NULL is returned.
774      Space for the expansion is obtained with malloc.
775      It is up to the caller to free it.  */
776   const char * (* fn) PARAMS ((const struct cgen_minsn_expansion *,
777                                const char *, const char **, int *, CGEN_OPERAND **));
778 #define CGEN_MIEXPN_FN(ex) ((ex)->fn)
779
780   /* Instruction(s) the macro expands to.
781      The format of STR is defined by FN.
782      It is typically the assembly code of the real insn, but it could also be
783      the original Scheme expression or a tokenized form of it (with FN being
784      an appropriate interpreter).  */
785   const char * str;
786 #define CGEN_MIEXPN_STR(ex) ((ex)->str)
787 } CGEN_MINSN_EXPANSION;
788
789 /* Normal expander.
790    When supported, this function will convert the input string to another
791    string and the parser will be invoked recursively.  The output string
792    may contain further macro invocations.  */
793
794 extern const char * cgen_expand_macro_insn
795      PARAMS ((const struct cgen_minsn_expansion *,
796               const char *, const char **, int *, CGEN_OPERAND **));
797 \f
798 /* The assembler insn table is hashed based on some function of the mnemonic
799    (the actually hashing done is up to the target, but we provide a few
800    examples like the first letter or a function of the entire mnemonic).  */
801
802 #ifndef CGEN_ASM_HASH_P
803 #define CGEN_ASM_HASH_P(insn) 1
804 #endif
805
806 /* INSN is the CGEN_INSN entry when building the hash table and NULL
807    when looking up the insn during assembly.  */
808 #ifndef CGEN_ASM_HASH
809 #define CGEN_ASM_HASH_SIZE 127
810 #ifdef CGEN_MNEMONIC_OPERANDS
811 #define CGEN_ASM_HASH(mnem) (*(unsigned char *) (mnem) % CGEN_ASM_HASH_SIZE)
812 #else
813 #define CGEN_ASM_HASH(mnem) (*(unsigned char *) (mnem) % CGEN_ASM_HASH_SIZE) /*FIXME*/
814 #endif
815 #endif
816
817 extern CGEN_INSN_LIST * cgen_asm_lookup_insn PARAMS ((const char *));
818 #define CGEN_ASM_LOOKUP_INSN(string) cgen_asm_lookup_insn (string)
819 #define CGEN_ASM_NEXT_INSN(insn) ((insn)->next)
820
821 /* The disassembler insn table is hashed based on some function of machine
822    instruction (the actually hashing done is up to the target).  */
823
824 #ifndef CGEN_DIS_HASH_P
825 #define CGEN_DIS_HASH_P(insn) 1
826 #endif
827
828 /* It doesn't make much sense to provide a default here,
829    but while this is under development we do.
830    INSN is the CGEN_INSN entry when building the hash table and NULL
831    when looking up the insn during assembly.
832    BUFFER is a pointer to the bytes of the insn.
833    VALUE is the first CGEN_BASE_INSN_SIZE bytes as an int in host order.  */
834 #ifndef CGEN_DIS_HASH
835 #define CGEN_DIS_HASH_SIZE 256
836 #define CGEN_DIS_HASH(buffer, value) (*(unsigned char *) (buffer))
837 #endif
838
839 extern CGEN_INSN_LIST * cgen_dis_lookup_insn PARAMS ((const char *,
840                                                       unsigned long));
841 #define CGEN_DIS_LOOKUP_INSN(buf, value) cgen_dis_lookup_insn ((buf), (value))
842 #define CGEN_DIS_NEXT_INSN(insn) ((insn)->next)
843 \f
844 /* Top level structures and functions.  */
845
846 typedef struct
847 {
848   const CGEN_HW_ENTRY * hw_list;
849
850   /*CGEN_OPERAND_TABLE * operand_table; - FIXME:wip */
851
852   CGEN_INSN_TABLE * insn_table;
853
854   /* Macro instructions are defined separately and are combined with real
855      insns during hash table computation.  */
856   CGEN_INSN_TABLE * macro_insn_table;
857
858   /* Return non-zero if insn should be added to hash table.  */
859   int (* asm_hash_p) PARAMS ((const CGEN_INSN *));
860
861   /* Assembler hash function.  */
862   unsigned int (* asm_hash) PARAMS ((const char *));
863
864   /* Number of entries in assembler hash table.  */
865   unsigned int asm_hash_table_size;
866
867   /* Return non-zero if insn should be added to hash table.  */
868   int (* dis_hash_p) PARAMS ((const CGEN_INSN *));
869
870   /* Disassembler hash function.  */
871   unsigned int (* dis_hash) PARAMS ((const char *, unsigned long));
872
873   /* Number of entries in disassembler hash table.  */
874   unsigned int dis_hash_table_size;
875 } CGEN_OPCODE_TABLE;
876
877 /* Each CPU has one of these.  */
878 extern const CGEN_OPCODE_TABLE CGEN_SYM (opcode_table);
879
880 /* Global state access macros.
881    Some of these are tucked away and accessed with cover fns.
882    Simpler things like the current machine and endian are not.  */
883
884 extern int cgen_current_machine;
885 #define CGEN_CURRENT_MACHINE cgen_current_machine
886
887 extern enum cgen_endian cgen_current_endian;
888 #define CGEN_CURRENT_ENDIAN cgen_current_endian
889
890 /* Prototypes of major functions.  */
891
892 /* Set the current cpu (+ mach number, endian, etc.).  */
893 extern void cgen_set_cpu PARAMS ((const CGEN_OPCODE_TABLE *, int,
894                                   enum cgen_endian));
895
896 /* Initialize the assembler, disassembler.  */
897 extern void cgen_asm_init PARAMS ((void));
898 extern void cgen_dis_init PARAMS ((void));
899
900 /* `init_tables' must be called before `xxx_supported'.  */
901 extern void CGEN_SYM (init_tables) PARAMS ((int));
902 extern void CGEN_SYM (init_asm) PARAMS ((int, enum cgen_endian));
903 extern void CGEN_SYM (init_dis) PARAMS ((int, enum cgen_endian));
904
905 /* FIXME: This prototype is wrong ifndef CGEN_INT_INSN.
906    Furthermore, ifdef CGEN_INT_INSN, the insn is created in
907    target byte order (in which case why use int's at all).
908    Perhaps replace cgen_insn_t * with char *?  */
909 extern const struct cgen_insn *
910 CGEN_SYM (assemble_insn) PARAMS ((const char *, CGEN_FIELDS *,
911                                   cgen_insn_t *, char **));
912 #if 0 /* old */
913 extern int CGEN_SYM (insn_supported) PARAMS ((const struct cgen_insn *));
914 extern int CGEN_SYM (opval_supported) PARAMS ((const struct cgen_opval *));
915 #endif
916
917 extern const CGEN_KEYWORD  CGEN_SYM (operand_mach);
918 extern int CGEN_SYM (get_mach) PARAMS ((const char *));
919
920 extern const CGEN_INSN *
921 CGEN_SYM (lookup_insn) PARAMS ((const CGEN_INSN *, cgen_insn_t,
922                                 int, CGEN_FIELDS *, int));
923 extern void
924 CGEN_SYM (get_insn_operands) PARAMS ((const CGEN_INSN *, const CGEN_FIELDS *,
925                                       int *));
926 extern const CGEN_INSN *
927 CGEN_SYM (lookup_get_insn_operands) PARAMS ((const CGEN_INSN *, cgen_insn_t,
928                                              int, int *));
929
930 CGEN_INLINE void
931 CGEN_SYM (put_operand) PARAMS ((int, const long *,
932                                 CGEN_FIELDS *));
933 CGEN_INLINE long
934 CGEN_SYM (get_operand) PARAMS ((int, const CGEN_FIELDS *));
935
936 extern const char *
937 CGEN_SYM (parse_operand) PARAMS ((int, const char **, CGEN_FIELDS *));
938
939 extern const char *
940 CGEN_SYM (insert_operand) PARAMS ((int, CGEN_FIELDS *, char *));
941
942 /* Default insn parser, printer.  */
943 extern cgen_parse_fn CGEN_SYM (parse_insn);
944 extern cgen_insert_fn CGEN_SYM (insert_insn);
945 extern cgen_extract_fn CGEN_SYM (extract_insn);
946 extern cgen_print_fn CGEN_SYM (print_insn);
947
948 /* Read in a cpu description file.  */
949 extern const char * cgen_read_cpu_file PARAMS ((const char *));
950
951 #endif /* CGEN_H */