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