1 /* CGEN generic opcode support.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
5 This file is part of the GNU Binutils and GDB, the GNU debugger.
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, or (at your option)
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.
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. */
25 #include "libiberty.h"
28 #include "opcode/cgen.h"
30 static unsigned int hash_keyword_name
31 PARAMS ((const CGEN_KEYWORD
*, const char *, int));
32 static unsigned int hash_keyword_value
33 PARAMS ((const CGEN_KEYWORD
*, unsigned int));
34 static void build_keyword_hash_tables
35 PARAMS ((CGEN_KEYWORD
*));
37 /* Return number of hash table entries to use for N elements. */
38 #define KEYWORD_HASH_SIZE(n) ((n) <= 31 ? 17 : 31)
40 /* Look up *NAMEP in the keyword table KT.
41 The result is the keyword entry or NULL if not found. */
43 const CGEN_KEYWORD_ENTRY
*
44 cgen_keyword_lookup_name (kt
, name
)
48 const CGEN_KEYWORD_ENTRY
*ke
;
51 if (kt
->name_hash_table
== NULL
)
52 build_keyword_hash_tables (kt
);
54 ke
= kt
->name_hash_table
[hash_keyword_name (kt
, name
, 0)];
56 /* We do case insensitive comparisons.
57 If that ever becomes a problem, add an attribute that denotes
58 "do case sensitive comparisons". */
67 || (isalpha ((unsigned char) *p
)
68 && (tolower ((unsigned char) *p
)
69 == tolower ((unsigned char) *n
)))))
79 return kt
->null_entry
;
83 /* Look up VALUE in the keyword table KT.
84 The result is the keyword entry or NULL if not found. */
86 const CGEN_KEYWORD_ENTRY
*
87 cgen_keyword_lookup_value (kt
, value
)
91 const CGEN_KEYWORD_ENTRY
*ke
;
93 if (kt
->name_hash_table
== NULL
)
94 build_keyword_hash_tables (kt
);
96 ke
= kt
->value_hash_table
[hash_keyword_value (kt
, value
)];
100 if (value
== ke
->value
)
108 /* Add an entry to a keyword table. */
111 cgen_keyword_add (kt
, ke
)
113 CGEN_KEYWORD_ENTRY
*ke
;
117 if (kt
->name_hash_table
== NULL
)
118 build_keyword_hash_tables (kt
);
120 hash
= hash_keyword_name (kt
, ke
->name
, 0);
121 ke
->next_name
= kt
->name_hash_table
[hash
];
122 kt
->name_hash_table
[hash
] = ke
;
124 hash
= hash_keyword_value (kt
, ke
->value
);
125 ke
->next_value
= kt
->value_hash_table
[hash
];
126 kt
->value_hash_table
[hash
] = ke
;
128 if (ke
->name
[0] == 0)
132 /* FIXME: Need function to return count of keywords. */
134 /* Initialize a keyword table search.
135 SPEC is a specification of what to search for.
136 A value of NULL means to find every keyword.
137 Currently NULL is the only acceptable value [further specification
139 The result is an opaque data item used to record the search status.
140 It is passed to each call to cgen_keyword_search_next. */
143 cgen_keyword_search_init (kt
, spec
)
147 CGEN_KEYWORD_SEARCH search
;
149 /* FIXME: Need to specify format of PARAMS. */
153 if (kt
->name_hash_table
== NULL
)
154 build_keyword_hash_tables (kt
);
158 search
.current_hash
= 0;
159 search
.current_entry
= NULL
;
163 /* Return the next keyword specified by SEARCH.
164 The result is the next entry or NULL if there are no more. */
166 const CGEN_KEYWORD_ENTRY
*
167 cgen_keyword_search_next (search
)
168 CGEN_KEYWORD_SEARCH
*search
;
170 /* Has search finished? */
171 if (search
->current_hash
== search
->table
->hash_table_size
)
174 /* Search in progress? */
175 if (search
->current_entry
!= NULL
176 /* Anything left on this hash chain? */
177 && search
->current_entry
->next_name
!= NULL
)
179 search
->current_entry
= search
->current_entry
->next_name
;
180 return search
->current_entry
;
183 /* Move to next hash chain [unless we haven't started yet]. */
184 if (search
->current_entry
!= NULL
)
185 ++search
->current_hash
;
187 while (search
->current_hash
< search
->table
->hash_table_size
)
189 search
->current_entry
= search
->table
->name_hash_table
[search
->current_hash
];
190 if (search
->current_entry
!= NULL
)
191 return search
->current_entry
;
192 ++search
->current_hash
;
198 /* Return first entry in hash chain for NAME.
199 If CASE_SENSITIVE_P is non-zero, return a case sensitive hash. */
202 hash_keyword_name (kt
, name
, case_sensitive_p
)
203 const CGEN_KEYWORD
*kt
;
205 int case_sensitive_p
;
209 if (case_sensitive_p
)
210 for (hash
= 0; *name
; ++name
)
211 hash
= (hash
* 97) + (unsigned char) *name
;
213 for (hash
= 0; *name
; ++name
)
214 hash
= (hash
* 97) + (unsigned char) tolower (*name
);
215 return hash
% kt
->hash_table_size
;
218 /* Return first entry in hash chain for VALUE. */
221 hash_keyword_value (kt
, value
)
222 const CGEN_KEYWORD
*kt
;
225 return value
% kt
->hash_table_size
;
228 /* Build a keyword table's hash tables.
229 We probably needn't build the value hash table for the assembler when
230 we're using the disassembler, but we keep things simple. */
233 build_keyword_hash_tables (kt
)
237 /* Use the number of compiled in entries as an estimate for the
238 typical sized table [not too many added at runtime]. */
239 unsigned int size
= KEYWORD_HASH_SIZE (kt
->num_init_entries
);
241 kt
->hash_table_size
= size
;
242 kt
->name_hash_table
= (CGEN_KEYWORD_ENTRY
**)
243 xmalloc (size
* sizeof (CGEN_KEYWORD_ENTRY
*));
244 memset (kt
->name_hash_table
, 0, size
* sizeof (CGEN_KEYWORD_ENTRY
*));
245 kt
->value_hash_table
= (CGEN_KEYWORD_ENTRY
**)
246 xmalloc (size
* sizeof (CGEN_KEYWORD_ENTRY
*));
247 memset (kt
->value_hash_table
, 0, size
* sizeof (CGEN_KEYWORD_ENTRY
*));
249 /* The table is scanned backwards as we want keywords appearing earlier to
250 be prefered over later ones. */
251 for (i
= kt
->num_init_entries
- 1; i
>= 0; --i
)
252 cgen_keyword_add (kt
, &kt
->init_entries
[i
]);
255 /* Hardware support. */
257 /* Lookup a hardware element by its name.
258 Returns NULL if NAME is not supported by the currently selected
261 const CGEN_HW_ENTRY
*
262 cgen_hw_lookup_by_name (cd
, name
)
267 const CGEN_HW_ENTRY
**hw
= cd
->hw_table
.entries
;
269 for (i
= 0; i
< cd
->hw_table
.num_entries
; ++i
)
270 if (hw
[i
] && strcmp (name
, hw
[i
]->name
) == 0)
276 /* Lookup a hardware element by its number.
277 Hardware elements are enumerated, however it may be possible to add some
278 at runtime, thus HWNUM is not an enum type but rather an int.
279 Returns NULL if HWNUM is not supported by the currently selected mach. */
281 const CGEN_HW_ENTRY
*
282 cgen_hw_lookup_by_num (cd
, hwnum
)
287 const CGEN_HW_ENTRY
**hw
= cd
->hw_table
.entries
;
289 /* ??? This can be speeded up. */
290 for (i
= 0; i
< cd
->hw_table
.num_entries
; ++i
)
291 if (hw
[i
] && hwnum
== hw
[i
]->type
)
297 /* Operand support. */
299 /* Lookup an operand by its name.
300 Returns NULL if NAME is not supported by the currently selected
304 cgen_operand_lookup_by_name (cd
, name
)
309 const CGEN_OPERAND
**op
= cd
->operand_table
.entries
;
311 for (i
= 0; i
< cd
->operand_table
.num_entries
; ++i
)
312 if (op
[i
] && strcmp (name
, op
[i
]->name
) == 0)
318 /* Lookup an operand by its number.
319 Operands are enumerated, however it may be possible to add some
320 at runtime, thus OPNUM is not an enum type but rather an int.
321 Returns NULL if OPNUM is not supported by the currently selected
325 cgen_operand_lookup_by_num (cd
, opnum
)
329 return cd
->operand_table
.entries
[opnum
];
332 /* Instruction support. */
334 /* Return number of instructions. This includes any added at runtime. */
340 int count
= cd
->insn_table
.num_init_entries
;
341 CGEN_INSN_LIST
*rt_insns
= cd
->insn_table
.new_entries
;
343 for ( ; rt_insns
!= NULL
; rt_insns
= rt_insns
->next
)
349 /* Return number of macro-instructions.
350 This includes any added at runtime. */
353 cgen_macro_insn_count (cd
)
356 int count
= cd
->macro_insn_table
.num_init_entries
;
357 CGEN_INSN_LIST
*rt_insns
= cd
->macro_insn_table
.new_entries
;
359 for ( ; rt_insns
!= NULL
; rt_insns
= rt_insns
->next
)
365 /* Cover function to read and properly byteswap an insn value. */
368 cgen_get_insn_value (cd
, buf
, length
)
381 if (cd
->insn_endian
== CGEN_ENDIAN_BIG
)
382 value
= bfd_getb16 (buf
);
384 value
= bfd_getl16 (buf
);
387 if (cd
->insn_endian
== CGEN_ENDIAN_BIG
)
388 value
= bfd_getb32 (buf
);
390 value
= bfd_getl32 (buf
);
399 /* Cover function to store an insn value properly byteswapped. */
402 cgen_put_insn_value (cd
, buf
, length
, value
)
414 if (cd
->insn_endian
== CGEN_ENDIAN_BIG
)
415 bfd_putb16 (value
, buf
);
417 bfd_putl16 (value
, buf
);
420 if (cd
->insn_endian
== CGEN_ENDIAN_BIG
)
421 bfd_putb32 (value
, buf
);
423 bfd_putl32 (value
, buf
);
430 /* Look up instruction INSN_*_VALUE and extract its fields.
431 INSN_INT_VALUE is used if CGEN_INT_INSN_P.
432 Otherwise INSN_BYTES_VALUE is used.
433 INSN, if non-null, is the insn table entry.
434 Otherwise INSN_*_VALUE is examined to compute it.
435 LENGTH is the bit length of INSN_*_VALUE if known, otherwise 0.
436 0 is only valid if `insn == NULL && ! CGEN_INT_INSN_P'.
437 If INSN != NULL, LENGTH must be valid.
438 ALIAS_P is non-zero if alias insns are to be included in the search.
440 The result is a pointer to the insn table entry, or NULL if the instruction
441 wasn't recognized. */
443 /* ??? Will need to be revisited for VLIW architectures. */
446 cgen_lookup_insn (cd
, insn
, insn_int_value
, insn_bytes_value
, length
, fields
,
449 const CGEN_INSN
*insn
;
450 CGEN_INSN_INT insn_int_value
;
451 /* ??? CGEN_INSN_BYTES would be a nice type name to use here. */
452 unsigned char *insn_bytes_value
;
458 CGEN_INSN_INT base_insn
;
459 CGEN_EXTRACT_INFO ex_info
;
460 CGEN_EXTRACT_INFO
*info
;
465 buf
= (unsigned char *) alloca (cd
->max_insn_bitsize
/ 8);
466 cgen_put_insn_value (cd
, buf
, length
, insn_int_value
);
467 base_insn
= insn_int_value
;
472 ex_info
.dis_info
= NULL
;
473 ex_info
.insn_bytes
= insn_bytes_value
;
475 buf
= insn_bytes_value
;
476 base_insn
= cgen_get_insn_value (cd
, buf
, length
);
481 const CGEN_INSN_LIST
*insn_list
;
483 /* The instructions are stored in hash lists.
484 Pick the first one and keep trying until we find the right one. */
486 insn_list
= cgen_dis_lookup_insn (cd
, buf
, base_insn
);
487 while (insn_list
!= NULL
)
489 insn
= insn_list
->insn
;
492 /* FIXME: Ensure ALIAS attribute always has same index. */
493 || ! CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_ALIAS
))
495 /* Basic bit mask must be correct. */
496 /* ??? May wish to allow target to defer this check until the
498 if ((base_insn
& CGEN_INSN_BASE_MASK (insn
))
499 == CGEN_INSN_BASE_VALUE (insn
))
501 /* ??? 0 is passed for `pc' */
502 int elength
= CGEN_EXTRACT_FN (cd
, insn
)
503 (cd
, insn
, info
, base_insn
, fields
, (bfd_vma
) 0);
507 if (length
!= 0 && length
!= elength
)
514 insn_list
= insn_list
->next
;
519 /* Sanity check: can't pass an alias insn if ! alias_p. */
521 && CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_ALIAS
))
523 /* Sanity check: length must be correct. */
524 if (length
!= CGEN_INSN_BITSIZE (insn
))
527 /* ??? 0 is passed for `pc' */
528 length
= CGEN_EXTRACT_FN (cd
, insn
)
529 (cd
, insn
, info
, base_insn
, fields
, (bfd_vma
) 0);
530 /* Sanity check: must succeed.
531 Could relax this later if it ever proves useful. */
540 /* Fill in the operand instances used by INSN whose operands are FIELDS.
541 INDICES is a pointer to a buffer of MAX_OPERAND_INSTANCES ints to be filled
545 cgen_get_insn_operands (cd
, insn
, fields
, indices
)
547 const CGEN_INSN
*insn
;
548 const CGEN_FIELDS
*fields
;
551 const CGEN_OPINST
*opinst
;
554 if (insn
->opinst
== NULL
)
556 for (i
= 0, opinst
= insn
->opinst
; opinst
->type
!= CGEN_OPINST_END
; ++i
, ++opinst
)
558 enum cgen_operand_type op_type
= opinst
->op_type
;
559 if (op_type
== CGEN_OPERAND_NIL
)
560 indices
[i
] = opinst
->index
;
562 indices
[i
] = (*cd
->get_int_operand
) (cd
, op_type
, fields
);
566 /* Cover function to cgen_get_insn_operands when either INSN or FIELDS
568 The INSN, INSN_*_VALUE, and LENGTH arguments are passed to
569 cgen_lookup_insn unchanged.
570 INSN_INT_VALUE is used if CGEN_INT_INSN_P.
571 Otherwise INSN_BYTES_VALUE is used.
573 The result is the insn table entry or NULL if the instruction wasn't
577 cgen_lookup_get_insn_operands (cd
, insn
, insn_int_value
, insn_bytes_value
,
578 length
, indices
, fields
)
580 const CGEN_INSN
*insn
;
581 CGEN_INSN_INT insn_int_value
;
582 /* ??? CGEN_INSN_BYTES would be a nice type name to use here. */
583 unsigned char *insn_bytes_value
;
588 /* Pass non-zero for ALIAS_P only if INSN != NULL.
589 If INSN == NULL, we want a real insn. */
590 insn
= cgen_lookup_insn (cd
, insn
, insn_int_value
, insn_bytes_value
,
591 length
, fields
, insn
!= NULL
);
595 cgen_get_insn_operands (cd
, insn
, fields
, indices
);
599 /* Allow signed overflow of instruction fields. */
601 cgen_set_signed_overflow_ok (cd
)
604 cd
->signed_overflow_ok_p
= 1;
607 /* Generate an error message if a signed field in an instruction overflows. */
609 cgen_clear_signed_overflow_ok (cd
)
612 cd
->signed_overflow_ok_p
= 0;
615 /* Will an error message be generated if a signed field in an instruction overflows ? */
617 cgen_signed_overflow_ok_p (cd
)
620 return cd
->signed_overflow_ok_p
;