]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - include/opcode/cgen.h
Oopps - forgot to save cgen.h before commiting previous delta.
[thirdparty/binutils-gdb.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 #ifndef CGEN_CAT3
25 #if defined(__STDC__) || defined(ALMOST_STDC)
26 #define CGEN_XCAT3(a,b,c) a ## b ## c
27 #define CGEN_CAT3(a,b,c) CGEN_XCAT3 (a, b, c)
28 #else
29 #define CGEN_CAT3(a,b,c) a/**/b/**/c
30 #endif
31 #endif
32
33 /* Prepend the cpu name, defined in cpu-opc.h, and _cgen_ to symbol S.
34 The lack of spaces in the arg list is important for non-stdc systems.
35 This file is included by <cpu>-opc.h.
36 It can be included independently of cpu-opc.h, in which case the cpu
37 dependent portions will be declared as "unknown_cgen_foo". */
38
39 #ifndef CGEN_SYM
40 #define CGEN_SYM(s) CGEN_CAT3 (unknown,_cgen_,s)
41 #endif
42
43 /* This file contains the static (unchanging) pieces and as much other stuff
44 as we can reasonably put here. It's generally cleaner to put stuff here
45 rather than having it machine generated if possible. */
46
47 /* The assembler syntax is made up of expressions (duh...).
48 At the lowest level the values are mnemonics, register names, numbers, etc.
49 Above that are subexpressions, if any (an example might be the
50 "effective address" in m68k cpus). At the second highest level are the
51 insns themselves. Above that are pseudo-insns, synthetic insns, and macros,
52 if any.
53 */
54 \f
55 /* Lots of cpu's have a fixed insn size, or one which rarely changes,
56 and it's generally easier to handle these by treating the insn as an
57 integer type, rather than an array of characters. So we allow targets
58 to control this. */
59
60 #ifdef CGEN_INT_INSN
61 typedef unsigned int cgen_insn_t;
62 #else
63 typedef char * cgen_insn_t;
64 #endif
65
66 #ifdef __GNUC__
67 #define CGEN_INLINE inline
68 #else
69 #define CGEN_INLINE
70 #endif
71
72 /* Perhaps we should just use bfd.h, but it's not clear
73 one would want to require that yet. */
74 enum cgen_endian
75 {
76 CGEN_ENDIAN_UNKNOWN,
77 CGEN_ENDIAN_LITTLE,
78 CGEN_ENDIAN_BIG
79 };
80 \f
81 /* Attributes.
82 Attributes are used to describe various random things. */
83
84 /* Struct to record attribute information. */
85 typedef struct
86 {
87 unsigned char num_nonbools;
88 unsigned int bool;
89 unsigned int nonbool[1];
90 } CGEN_ATTR;
91
92 /* Define a structure member for attributes with N non-boolean entries.
93 The attributes are sorted so that the non-boolean ones come first.
94 num_nonbools: count of nonboolean attributes
95 bool: values of boolean attributes
96 nonbool: values of non-boolean attributes
97 There is a maximum of 32 attributes total. */
98 #define CGEN_ATTR_TYPE(n) \
99 const struct { unsigned char num_nonbools; \
100 unsigned int bool; \
101 unsigned int nonbool[(n) ? (n) : 1]; }
102
103 /* Given an attribute number, return its mask. */
104 #define CGEN_ATTR_MASK(attr) (1 << (attr))
105
106 /* Return value of attribute ATTR in ATTR_TABLE for OBJ.
107 OBJ is a pointer to the entity that has the attributes.
108 It's not used at present but is reserved for future purposes. */
109 #define CGEN_ATTR_VALUE(obj, attr_table, attr) \
110 ((unsigned int) (attr) < (attr_table)->num_nonbools \
111 ? ((attr_table)->nonbool[attr]) \
112 : (((attr_table)->bool & (1 << (attr))) != 0))
113
114 /* Attribute name/value tables.
115 These are used to assist parsing of descriptions at runtime. */
116
117 typedef struct
118 {
119 const char * name;
120 int value;
121 } CGEN_ATTR_ENTRY;
122
123 /* For each domain (fld,operand,insn), list of attributes. */
124
125 typedef struct
126 {
127 const char * name;
128 /* NULL for boolean attributes. */
129 const CGEN_ATTR_ENTRY * vals;
130 } CGEN_ATTR_TABLE;
131 \f
132 /* Parse result (also extraction result).
133
134 The result of parsing an insn is stored here.
135 To generate the actual insn, this is passed to the insert handler.
136 When printing an insn, the result of extraction is stored here.
137 To print the insn, this is passed to the print handler.
138
139 It is machine generated so we don't define it here,
140 but we do need a forward decl for the handler fns.
141
142 There is one member for each possible field in the insn.
143 The type depends on the field.
144 Also recorded here is the computed length of the insn for architectures
145 where it varies.
146 */
147
148 struct cgen_fields;
149
150 /* Total length of the insn, as recorded in the `fields' struct. */
151 /* ??? The field insert handler has lots of opportunities for optimization
152 if it ever gets inlined. On architectures where insns all have the same
153 size, may wish to detect that and make this macro a constant - to allow
154 further optimizations. */
155 #define CGEN_FIELDS_BITSIZE(fields) ((fields)->length)
156 \f
157 /* Associated with each insn or expression is a set of "handlers" for
158 performing operations like parsing, printing, etc. */
159
160 /* Forward decl. */
161 typedef struct cgen_insn CGEN_INSN;
162
163 /* Parse handler.
164 The first argument is a pointer to a struct describing the insn being
165 parsed.
166 The second argument is a pointer to a pointer to the text being parsed.
167 The third argument is a pointer to a cgen_fields struct
168 in which the results are placed.
169 If the expression is successfully parsed, the pointer to the text is
170 updated. If not it is left alone.
171 The result is NULL if success or an error message. */
172 typedef const char * (cgen_parse_fn) PARAMS ((const struct cgen_insn *,
173 const char **,
174 struct cgen_fields *));
175
176 /* Print handler.
177 The first argument is a pointer to the disassembly info.
178 Eg: disassemble_info. It's defined as `PTR' so this file can be included
179 without dis-asm.h.
180 The second argument is a pointer to a struct describing the insn being
181 printed.
182 The third argument is a pointer to a cgen_fields struct.
183 The fourth argument is the pc value of the insn.
184 The fifth argument is the length of the insn, in bytes. */
185 /* Don't require bfd.h unnecessarily. */
186 #ifdef BFD_VERSION
187 typedef void (cgen_print_fn) PARAMS ((PTR, const struct cgen_insn *,
188 struct cgen_fields *, bfd_vma, int));
189 #else
190 typedef void (cgen_print_fn) ();
191 #endif
192
193 /* Insert handler.
194 The first argument is a pointer to a struct describing the insn being
195 parsed.
196 The second argument is a pointer to a cgen_fields struct
197 from which the values are fetched.
198 The third argument is a pointer to a buffer in which to place the insn. */
199 typedef void (cgen_insert_fn) PARAMS ((const struct cgen_insn *,
200 struct cgen_fields *, cgen_insn_t *));
201
202 /* Extract handler.
203 The first argument is a pointer to a struct describing the insn being
204 parsed.
205 The second argument is a pointer to a struct controlling extraction
206 (only used for variable length insns).
207 The third argument is the first CGEN_BASE_INSN_SIZE bytes.
208 The fourth argument is a pointer to a cgen_fields struct
209 in which the results are placed.
210 The result is the length of the insn or zero if not recognized. */
211 typedef int (cgen_extract_fn) PARAMS ((const struct cgen_insn *,
212 void *, cgen_insn_t,
213 struct cgen_fields *));
214
215 /* The `parse' and `insert' fields are indices into these tables.
216 The elements are pointer to specialized handler functions.
217 Element 0 is special, it means use the default handler. */
218 extern cgen_parse_fn * CGEN_SYM (parse_handlers) [];
219 #define CGEN_PARSE_FN(x) (CGEN_SYM (parse_handlers)[(x)->base.parse])
220 extern cgen_insert_fn * CGEN_SYM (insert_handlers) [];
221 #define CGEN_INSERT_FN(x) (CGEN_SYM (insert_handlers)[(x)->base.insert])
222
223 /* Likewise for the `extract' and `print' fields. */
224 extern cgen_extract_fn * CGEN_SYM (extract_handlers) [];
225 #define CGEN_EXTRACT_FN(x) (CGEN_SYM (extract_handlers)[(x)->base.extract])
226 extern cgen_print_fn * CGEN_SYM (print_handlers) [];
227 #define CGEN_PRINT_FN(x) (CGEN_SYM (print_handlers)[(x)->base.print])
228 \f
229 /* Base class of parser/printer.
230 (Don't read too much into the use of the phrase "base class".
231 It's a name I'm using to organize my thoughts.)
232
233 Instructions and expressions all share this data in common.
234 It's a collection of the common elements needed to parse, insert, extract,
235 and print each of them. */
236
237 #ifndef CGEN_MAX_INSN_ATTRS
238 #define CGEN_MAX_INSN_ATTRS 1
239 #endif
240
241 struct cgen_base
242 {
243 /* Indices into the handler tables.
244 We could use pointers here instead, but in the case of the insn table,
245 90% of them would be identical and that's a lot of redundant data.
246 0 means use the default (what the default is is up to the code). */
247 unsigned char parse, insert, extract, print;
248 };
249 \f
250 /* Assembler interface.
251
252 The interface to the assembler is intended to be clean in the sense that
253 libopcodes.a is a standalone entity and could be used with any assembler.
254 Not that one would necessarily want to do that but rather that it helps
255 keep a clean interface. The interface will obviously be slanted towards
256 GAS, but at least it's a start.
257
258 Parsing is controlled by the assembler which calls
259 CGEN_SYM (assemble_insn). If it can parse and build the entire insn
260 it doesn't call back to the assembler. If it needs/wants to call back
261 to the assembler, (*cgen_parse_operand_fn) is called which can either
262
263 - return a number to be inserted in the insn
264 - return a "register" value to be inserted
265 (the register might not be a register per pe)
266 - queue the argument and return a marker saying the expression has been
267 queued (eg: a fix-up)
268 - return an error message indicating the expression wasn't recognizable
269
270 The result is an error message or NULL for success.
271 The parsed value is stored in the bfd_vma *. */
272
273 /* Values for indicating what the caller wants. */
274 enum cgen_parse_operand_type
275 {
276 CGEN_PARSE_OPERAND_INIT,
277 CGEN_PARSE_OPERAND_INTEGER,
278 CGEN_PARSE_OPERAND_ADDRESS
279 };
280
281 /* Values for indicating what was parsed.
282 ??? Not too useful at present but in time. */
283 enum cgen_parse_operand_result
284 {
285 CGEN_PARSE_OPERAND_RESULT_NUMBER,
286 CGEN_PARSE_OPERAND_RESULT_REGISTER,
287 CGEN_PARSE_OPERAND_RESULT_QUEUED,
288 CGEN_PARSE_OPERAND_RESULT_ERROR
289 };
290
291 /* Don't require bfd.h unnecessarily. */
292 #ifdef BFD_VERSION
293 extern const char * (*cgen_parse_operand_fn)
294 PARAMS ((enum cgen_parse_operand_type, const char **, int, int,
295 enum cgen_parse_operand_result *, bfd_vma *));
296 #endif
297
298 /* Called before trying to match a table entry with the insn. */
299 void cgen_init_parse_operand PARAMS ((void));
300
301 /* Called from <cpu>-asm.c to initialize operand parsing. */
302
303 /* These are GAS specific. They're not here as part of the interface,
304 but rather that we need to put them somewhere. */
305
306 /* Call this from md_assemble to initialize the assembler callback. */
307 void cgen_asm_init_parse PARAMS ((void));
308
309 /* Don't require bfd.h unnecessarily. */
310 #ifdef BFD_VERSION
311 /* The result is an error message or NULL for success.
312 The parsed value is stored in the bfd_vma *. */
313 const char * cgen_parse_operand PARAMS ((enum cgen_parse_operand_type,
314 const char **, int, int,
315 enum cgen_parse_operand_result *,
316 bfd_vma *));
317 #endif
318
319 void cgen_save_fixups PARAMS ((void));
320 void cgen_restore_fixups PARAMS ((void));
321 void cgen_swap_fixups PARAMS ((void));
322
323 /* Add a register to the assembler's hash table.
324 This makes lets GAS parse registers for us.
325 ??? This isn't currently used, but it could be in the future. */
326 void cgen_asm_record_register PARAMS ((char *, int));
327
328 /* After CGEN_SYM (assemble_insn) is done, this is called to
329 output the insn and record any fixups. The address of the
330 assembled instruction is returned in case it is needed by
331 the caller. */
332 char * cgen_asm_finish_insn PARAMS ((const struct cgen_insn *, cgen_insn_t *,
333 unsigned int));
334 \f
335 /* Operand values (keywords, integers, symbols, etc.) */
336
337 /* Types of assembler elements. */
338
339 enum cgen_asm_type
340 {
341 CGEN_ASM_KEYWORD, CGEN_ASM_MAX
342 };
343
344 /* List of hardware elements. */
345
346 typedef struct cgen_hw_entry
347 {
348 struct cgen_hw_entry * next;
349 char * name;
350 enum cgen_asm_type asm_type;
351 PTR asm_data;
352 } CGEN_HW_ENTRY;
353
354 extern CGEN_HW_ENTRY * CGEN_SYM (hw_list);
355
356 CGEN_HW_ENTRY * cgen_hw_lookup PARAMS ((const char *));
357
358 #ifndef CGEN_MAX_KEYWORD_ATTRS
359 #define CGEN_MAX_KEYWORD_ATTRS 1
360 #endif
361
362 /* This struct is used to describe things like register names, etc. */
363
364 typedef struct cgen_keyword_entry
365 {
366 /* Name (as in register name). */
367 char * name;
368
369 /* Value (as in register number).
370 The value cannot be -1 as that is used to indicate "not found".
371 IDEA: Have "FUNCTION" attribute? [function is called to fetch value]. */
372 int value;
373
374 /* Attributes.
375 This should, but technically needn't, appear last. It is a variable sized
376 array in that one architecture may have 1 nonbool attribute and another
377 may have more. Having this last means the non-architecture specific code
378 needn't care. */
379 /* ??? Moving this last should be done by treating keywords like insn lists
380 and moving the `next' fields into a CGEN_KEYWORD_LIST struct. */
381 /* FIXME: Not used yet. */
382 CGEN_ATTR_TYPE (CGEN_MAX_KEYWORD_ATTRS) attrs;
383
384 /* Next name hash table entry. */
385 struct cgen_keyword_entry *next_name;
386 /* Next value hash table entry. */
387 struct cgen_keyword_entry *next_value;
388 } CGEN_KEYWORD_ENTRY;
389
390 /* Top level struct for describing a set of related keywords
391 (e.g. register names).
392
393 This struct supports runtime entry of new values, and hashed lookups. */
394
395 typedef struct cgen_keyword
396 {
397 /* Pointer to initial [compiled in] values. */
398 CGEN_KEYWORD_ENTRY * init_entries;
399
400 /* Number of entries in `init_entries'. */
401 unsigned int num_init_entries;
402
403 /* Hash table used for name lookup. */
404 CGEN_KEYWORD_ENTRY ** name_hash_table;
405
406 /* Hash table used for value lookup. */
407 CGEN_KEYWORD_ENTRY ** value_hash_table;
408
409 /* Number of entries in the hash_tables. */
410 unsigned int hash_table_size;
411
412 /* Pointer to null keyword "" entry if present. */
413 const CGEN_KEYWORD_ENTRY * null_entry;
414 } CGEN_KEYWORD;
415
416 /* Structure used for searching. */
417
418 typedef struct
419 {
420 /* Table being searched. */
421 const CGEN_KEYWORD * table;
422
423 /* Specification of what is being searched for. */
424 const char * spec;
425
426 /* Current index in hash table. */
427 unsigned int current_hash;
428
429 /* Current element in current hash chain. */
430 CGEN_KEYWORD_ENTRY * current_entry;
431 } CGEN_KEYWORD_SEARCH;
432
433 /* Lookup a keyword from its name. */
434 const CGEN_KEYWORD_ENTRY * cgen_keyword_lookup_name
435 PARAMS ((CGEN_KEYWORD *, const char *));
436 /* Lookup a keyword from its value. */
437 const CGEN_KEYWORD_ENTRY * cgen_keyword_lookup_value
438 PARAMS ((CGEN_KEYWORD *, int));
439 /* Add a keyword. */
440 void cgen_keyword_add PARAMS ((CGEN_KEYWORD *, CGEN_KEYWORD_ENTRY *));
441 /* Keyword searching.
442 This can be used to retrieve every keyword, or a subset. */
443 CGEN_KEYWORD_SEARCH cgen_keyword_search_init
444 PARAMS ((CGEN_KEYWORD *, const char *));
445 const CGEN_KEYWORD_ENTRY *cgen_keyword_search_next
446 PARAMS ((CGEN_KEYWORD_SEARCH *));
447
448 /* Operand value support routines. */
449 /* FIXME: some of the long's here will need to be bfd_vma or some such. */
450
451 const char * cgen_parse_keyword PARAMS ((const char **,
452 CGEN_KEYWORD *,
453 long *));
454 const char * cgen_parse_signed_integer PARAMS ((const char **, int,
455 long, long, long *));
456 const char * cgen_parse_unsigned_integer PARAMS ((const char **, int,
457 unsigned long, unsigned long,
458 unsigned long *));
459 const char * cgen_parse_address PARAMS ((const char **, int, int,
460 enum cgen_parse_operand_result *,
461 long *));
462 const char * cgen_validate_signed_integer PARAMS ((long, long, long));
463 const char * cgen_validate_unsigned_integer PARAMS ((unsigned long,
464 unsigned long,
465 unsigned long));
466 \f
467 /* This struct defines each entry in the operand table. */
468
469 #ifndef CGEN_MAX_OPERAND_ATTRS
470 #define CGEN_MAX_OPERAND_ATTRS 1
471 #endif
472
473 typedef struct cgen_operand
474 {
475 /* Name as it appears in the syntax string. */
476 char * name;
477
478 /* Bit position (msb of first byte = bit 0).
479 This is just a hint, and may be unused in more complex operands.
480 May be unused for a modifier. */
481 unsigned char start;
482
483 /* The number of bits in the operand.
484 This is just a hint, and may be unused in more complex operands.
485 May be unused for a modifier. */
486 unsigned char length;
487
488 #if 0 /* ??? Interesting idea but relocs tend to get too complicated,
489 and ABI dependent, for simple table lookups to work. */
490 /* Ideally this would be the internal (external?) reloc type. */
491 int reloc_type;
492 #endif
493
494 /* Attributes.
495 This should, but technically needn't, appear last. It is a variable sized
496 array in that one architecture may have 1 nonbool attribute and another
497 may have more. Having this last means the non-architecture specific code
498 needn't care, now or tomorrow. */
499 CGEN_ATTR_TYPE (CGEN_MAX_OPERAND_ATTRS) attrs;
500 #define CGEN_OPERAND_ATTRS(operand) (&(operand)->attrs)
501 } CGEN_OPERAND;
502
503 /* Return value of attribute ATTR in OPERAND. */
504 #define CGEN_OPERAND_ATTR(operand, attr) \
505 CGEN_ATTR_VALUE (operand, CGEN_OPERAND_ATTRS (operand), attr)
506
507 /* The operand table is currently a very static entity. */
508 extern const CGEN_OPERAND CGEN_SYM (operand_table)[];
509
510 enum cgen_operand_type;
511
512 #define CGEN_OPERAND_INDEX(operand) ((int) ((operand) - CGEN_SYM (operand_table)))
513 /* FIXME: Rename, cpu-opc.h defines this as the typedef of the enum. */
514 #define CGEN_OPERAND_TYPE(operand) ((enum cgen_operand_type) CGEN_OPERAND_INDEX (operand))
515 #define CGEN_OPERAND_ENTRY(n) (& CGEN_SYM (operand_table) [n])
516 \f
517 /* Syntax string.
518
519 Each insn format and subexpression has one of these.
520
521 The syntax "string" consists of characters (n > 0 && n < 128), and operand
522 values (n >= 128), and is terminated by 0. Operand values are 128 + index
523 into the operand table. The operand table doesn't exist in C, per se, as
524 the data is recorded in the parse/insert/extract/print switch statements. */
525
526 #ifndef CGEN_MAX_SYNTAX_BYTES
527 #define CGEN_MAX_SYNTAX_BYTES 16
528 #endif
529
530 typedef struct
531 {
532 unsigned char syntax[CGEN_MAX_SYNTAX_BYTES];
533 } CGEN_SYNTAX;
534
535 #define CGEN_SYNTAX_STRING(syn) (syn->syntax)
536 #define CGEN_SYNTAX_CHAR_P(c) ((c) < 128)
537 #define CGEN_SYNTAX_CHAR(c) (c)
538 #define CGEN_SYNTAX_FIELD(c) ((c) - 128)
539
540 /* ??? I can't currently think of any case where the mnemonic doesn't come
541 first [and if one ever doesn't building the hash tables will be tricky].
542 However, we treat mnemonics as just another operand of the instruction.
543 A value of 1 means "this is where the mnemonic appears". 1 isn't
544 special other than it's a non-printable ASCII char. */
545 #define CGEN_SYNTAX_MNEMONIC_P(ch) ((ch) == 1)
546 \f
547 /* Instruction formats.
548
549 Instructions are grouped by format. Associated with an instruction is its
550 format. Each opcode table entry contains a pointer into the format table.
551 This cuts down on the size of the opcode table as there are relatively few
552 formats compared with the number of instructions. */
553
554 typedef struct
555 {
556 /* Length that MASK and VALUE have been calculated to
557 [VALUE is recorded elsewhere].
558 Normally it is CGEN_BASE_INSN_BITSIZE. On [V]LIW architectures where
559 the base insn size may be larger than the size of an insn, this field is
560 less than CGEN_BASE_INSN_BITSIZE. */
561 unsigned char mask_length;
562
563 /* Total length of instruction. */
564 unsigned char length;
565
566 /* Mask to apply to the first BASE_LENGTH bits.
567 Each insn's value is stored with the insn.
568 The first step in recognizing an insn for disassembly is
569 (opcode & mask) == value. */
570 unsigned int mask;
571 } CGEN_FORMAT;
572 \f
573 /* This struct defines each entry in the instruction table. */
574
575 struct cgen_insn
576 {
577 /* ??? Further table size reductions can be had by moving this element
578 either to the format table or to a separate table of its own. Not
579 sure this is desirable yet. */
580 struct cgen_base base;
581
582 /* Given a pointer to a cgen_insn struct, return a pointer to `base'. */
583 #define CGEN_INSN_BASE(insn) (&(insn)->base)
584
585 /* Name of entry (that distinguishes it from all other entries).
586 This is used, for example, in simulator profiling results. */
587 /* ??? If mnemonics have operands, try to print full mnemonic. */
588 const char * name;
589 #define CGEN_INSN_NAME(insn) ((insn)->name)
590
591 /* Mnemonic. This is used when parsing and printing the insn.
592 In the case of insns that have operands on the mnemonics, this is
593 only the constant part. E.g. for conditional execution of an `add' insn,
594 where the full mnemonic is addeq, addne, etc., this is only "add". */
595 const char * mnemonic;
596 #define CGEN_INSN_MNEMONIC(insn) ((insn)->mnemonic)
597
598 /* Syntax string.
599 For now this only points to CGEN_SYNTAX elements, but it can point
600 to other things (e.g. something different for macros?). */
601 const CGEN_SYNTAX * syntax;
602 #define CGEN_INSN_SYNTAX(insn) ((CGEN_SYNTAX *) (insn)->syntax)
603
604 /* Format entry.
605 For now this only points to CGEN_FORMAT elements, but it can point
606 to other things (e.g. something different for macros?). */
607 const CGEN_FORMAT * format;
608 #define CGEN_INSN_MASK_BITSIZE(insn) (((CGEN_FORMAT *) (insn)->format)->mask_length)
609 #define CGEN_INSN_BITSIZE(insn) (((CGEN_FORMAT *) (insn)->format)->length)
610
611 /* Instruction opcode value. */
612 unsigned int value;
613 #define CGEN_INSN_VALUE(insn) ((insn)->value)
614 #define CGEN_INSN_MASK(insn) (((CGEN_FORMAT *) (insn)->format)->mask)
615
616 /* Attributes.
617 This must appear last. It is a variable sized array in that one
618 architecture may have 1 nonbool attribute and another may have more.
619 Having this last means the non-architecture specific code needn't
620 care. */
621 CGEN_ATTR_TYPE (CGEN_MAX_INSN_ATTRS) attrs;
622 #define CGEN_INSN_ATTRS(insn) (&(insn)->attrs)
623 /* Return value of attribute ATTR in INSN. */
624 #define CGEN_INSN_ATTR(insn, attr) \
625 CGEN_ATTR_VALUE (insn, CGEN_INSN_ATTRS (insn), attr)
626 };
627
628 /* Instruction lists.
629 This is used for adding new entries and for creating the hash lists. */
630
631 typedef struct cgen_insn_list
632 {
633 struct cgen_insn_list * next;
634 const CGEN_INSN * insn;
635 } CGEN_INSN_LIST;
636
637 /* The table of instructions. */
638
639 typedef struct
640 {
641 /* Pointer to initial [compiled in] entries. */
642 const CGEN_INSN * init_entries;
643
644 /* Size of an entry (since the attribute member is variable sized). */
645 unsigned int entry_size;
646
647 /* Number of entries in `init_entries', including trailing NULL entry. */
648 unsigned int num_init_entries;
649
650 /* Values added at runtime. */
651 CGEN_INSN_LIST * new_entries;
652
653 /* Assembler hash function. */
654 unsigned int (* asm_hash) PARAMS ((const char *));
655
656 /* Number of entries in assembler hash table. */
657 unsigned int asm_hash_table_size;
658
659 /* Disassembler hash function. */
660 unsigned int (* dis_hash) PARAMS ((const char *, unsigned long));
661
662 /* Number of entries in disassembler hash table. */
663 unsigned int dis_hash_table_size;
664 } CGEN_INSN_TABLE;
665
666 /* ??? This is currently used by the simulator.
667 We want this to be fast and the simulator currently doesn't handle
668 runtime added instructions so this is ok. An alternative would be to
669 store the index in the table. */
670 extern const CGEN_INSN CGEN_SYM (insn_table_entries)[];
671 #define CGEN_INSN_INDEX(insn) ((int) ((insn) - CGEN_SYM (insn_table_entries)))
672 #define CGEN_INSN_ENTRY(n) (& CGEN_SYM (insn_table_entries) [n])
673
674 /* Return number of instructions. This includes any added at runtime. */
675
676 int cgen_insn_count PARAMS ((void));
677
678 /* The assembler insn table is hashed based on some function of the mnemonic
679 (the actually hashing done is up to the target, but we provide a few
680 examples like the first letter or a function of the entire mnemonic).
681 The index of each entry is the index of the corresponding table entry.
682 The value of each entry is the index of the next entry, with a 0
683 terminating (thus the first entry is reserved). */
684
685 #ifndef CGEN_ASM_HASH
686 #ifdef CGEN_MNEMONIC_OPERANDS
687 #define CGEN_ASM_HASH_SIZE 127
688 #define CGEN_ASM_HASH(string) (*(unsigned char *) (string) % CGEN_ASM_HASH_SIZE)
689 #else
690 #define CGEN_ASM_HASH_SIZE 128
691 #define CGEN_ASM_HASH(string) (*(unsigned char *) (string) % CGEN_ASM_HASH_SIZE) /*FIXME*/
692 #endif
693 #endif
694
695 unsigned int CGEN_SYM (asm_hash_insn) PARAMS ((const char *));
696 CGEN_INSN_LIST * cgen_asm_lookup_insn PARAMS ((const char *));
697 #define CGEN_ASM_LOOKUP_INSN(insn) cgen_asm_lookup_insn (insn)
698 #define CGEN_ASM_NEXT_INSN(insn) ((insn)->next)
699
700 /* The disassembler insn table is hashed based on some function of machine
701 instruction (the actually hashing done is up to the target). */
702
703 /* It doesn't make much sense to provide a default here,
704 but while this is under development we do.
705 BUFFER is a pointer to the bytes of the insn.
706 INSN is the first CGEN_BASE_INSN_SIZE bytes as an int in host order. */
707 #ifndef CGEN_DIS_HASH
708 #define CGEN_DIS_HASH_SIZE 256
709 #define CGEN_DIS_HASH(buffer, insn) (*(unsigned char *) (buffer))
710 #endif
711
712 unsigned int CGEN_SYM (dis_hash_insn) PARAMS ((const char *, unsigned long));
713 CGEN_INSN_LIST * cgen_dis_lookup_insn PARAMS ((const char *, unsigned long));
714 #define CGEN_DIS_LOOKUP_INSN(buf, insn) cgen_dis_lookup_insn (buf, insn)
715 #define CGEN_DIS_NEXT_INSN(insn) ((insn)->next)
716 \f
717 /* Top level structures and functions. */
718
719 typedef struct
720 {
721 CGEN_HW_ENTRY * hw_list;
722 /*CGEN_OPERAND_TABLE * operand_table; - FIXME:wip */
723 CGEN_INSN_TABLE * insn_table;
724 } CGEN_OPCODE_DATA;
725
726 /* Each CPU has one of these. */
727 extern CGEN_OPCODE_DATA CGEN_SYM (opcode_data);
728
729 /* Global state access macros.
730 Some of these are tucked away and accessed with cover fns.
731 Simpler things like the current machine and endian are not. */
732
733 extern int cgen_current_machine;
734 #define CGEN_CURRENT_MACHINE cgen_current_machine
735
736 extern enum cgen_endian cgen_current_endian;
737 #define CGEN_CURRENT_ENDIAN cgen_current_endian
738
739 /* Prototypes of major functions. */
740
741 /* Set the current cpu (+ mach number, endian, etc.). *?
742 void cgen_set_cpu PARAMS ((CGEN_OPCODE_DATA *, int, enum cgen_endian));
743
744 /* Initialize the assembler, disassembler. */
745 void cgen_asm_init PARAMS ((void));
746 void cgen_dis_init PARAMS ((void));
747
748 /* `init_tables' must be called before `xxx_supported'. */
749 void CGEN_SYM (init_tables) PARAMS ((int));
750 void CGEN_SYM (init_asm) PARAMS ((int, enum cgen_endian));
751 void CGEN_SYM (init_dis) PARAMS ((int, enum cgen_endian));
752 void CGEN_SYM (init_parse) PARAMS ((void));
753 void CGEN_SYM (init_print) PARAMS ((void));
754 void CGEN_SYM (init_insert) PARAMS ((void));
755 void CGEN_SYM (init_extract) PARAMS ((void));
756 const struct cgen_insn *
757 CGEN_SYM (assemble_insn) PARAMS ((const char *, struct cgen_fields *,
758 cgen_insn_t *, char **));
759 #if 0 /* old */
760 int CGEN_SYM (insn_supported) PARAMS ((const struct cgen_insn *));
761 int CGEN_SYM (opval_supported) PARAMS ((const struct cgen_opval *));
762 #endif
763
764 extern const CGEN_KEYWORD CGEN_SYM (operand_mach);
765 int CGEN_SYM (get_mach) PARAMS ((const char *));
766
767 CGEN_INLINE void
768 CGEN_SYM (put_operand) PARAMS ((int, const long *,
769 struct cgen_fields *));
770 CGEN_INLINE long
771 CGEN_SYM (get_operand) PARAMS ((int, const struct cgen_fields *));
772
773 CGEN_INLINE const char *
774 CGEN_SYM (parse_operand) PARAMS ((int, const char **, struct cgen_fields *));
775
776 CGEN_INLINE const char *
777 CGEN_SYM (validate_operand) PARAMS ((int, const struct cgen_fields *));
778
779 /* Default insn parser, printer. */
780 extern cgen_parse_fn CGEN_SYM (parse_insn);
781 extern cgen_insert_fn CGEN_SYM (insert_insn);
782 extern cgen_extract_fn CGEN_SYM (extract_insn);
783 extern cgen_print_fn CGEN_SYM (print_insn);
784
785 /* Read in a cpu description file. */
786 const char * cgen_read_cpu_file PARAMS ((const char *));
787
788 #endif /* CGEN_H */