1 /* CGEN generic assembler support code.
3 Copyright (C) 1996, 1997 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. */
30 #include "libiberty.h"
32 #include "opcode/cgen.h"
34 /* Operand parsing callback. */
35 const char * (*cgen_asm_parse_operand_fn
)
36 PARAMS ((const char **, int, int, enum cgen_asm_result
*, bfd_vma
*));
38 /* This is not published as part of the public interface so we don't
39 declare this in cgen.h. */
40 extern CGEN_OPCODE_DATA
*cgen_current_opcode_data
;
42 /* Assembler instruction hash table. */
43 static CGEN_INSN_LIST
**asm_hash_table
;
50 free (asm_hash_table
);
51 asm_hash_table
= NULL
;
55 /* Build the assembler instruction hash table. */
58 build_asm_hash_table ()
62 int count
= cgen_insn_count ();
63 CGEN_OPCODE_DATA
*data
= cgen_current_opcode_data
;
64 CGEN_INSN_TABLE
*insn_table
= data
->insn_table
;
65 unsigned int hash_size
= insn_table
->asm_hash_table_size
;
66 const CGEN_INSN
*insn
;
67 CGEN_INSN_LIST
*insn_lists
,*new_insns
;
69 /* The space allocated for the hash table consists of two parts:
70 the hash table and the hash lists. */
72 asm_hash_table
= (CGEN_INSN_LIST
**)
73 xmalloc (hash_size
* sizeof (CGEN_INSN_LIST
*)
74 + count
* sizeof (CGEN_INSN_LIST
));
75 memset (asm_hash_table
, 0,
76 hash_size
* sizeof (CGEN_INSN_LIST
*)
77 + count
* sizeof (CGEN_INSN_LIST
));
78 insn_lists
= (CGEN_INSN_LIST
*) (asm_hash_table
+ hash_size
);
80 /* Add compiled in insns.
81 The table is scanned backwards as later additions are inserted in
82 front of earlier ones and we want earlier ones to be prefered.
83 We stop at the first one as it is a reserved entry. */
85 for (insn
= insn_table
->init_entries
+ insn_table
->num_init_entries
- 1;
86 insn
> insn_table
->init_entries
;
89 hash
= (*insn_table
->asm_hash
) (insn
->syntax
.mnemonic
);
90 insn_lists
->next
= asm_hash_table
[hash
];
91 insn_lists
->insn
= insn
;
92 asm_hash_table
[hash
] = insn_lists
;
95 /* Add runtime added insns.
96 ??? Currently later added insns will be prefered over earlier ones.
97 Not sure this is a bug or not. */
98 for (new_insns
= insn_table
->new_entries
;
100 new_insns
= new_insns
->next
, ++insn_lists
)
102 hash
= (*insn_table
->asm_hash
) (new_insns
->insn
->syntax
.mnemonic
);
103 insn_lists
->next
= asm_hash_table
[hash
];
104 insn_lists
->insn
= new_insns
->insn
;
105 asm_hash_table
[hash
] = insn_lists
;
109 /* Return the first entry in the hash list for INSN. */
112 cgen_asm_lookup_insn (insn
)
117 if (asm_hash_table
== NULL
)
118 build_asm_hash_table ();
120 hash
= (*cgen_current_opcode_data
->insn_table
->asm_hash
) (insn
);
121 return asm_hash_table
[hash
];
125 The result is NULL upon success or an error message.
126 If successful, *STRP is updated to point passed the keyword.
128 ??? At present we have a static notion of how to pick out a keyword.
129 Later we can allow a target to customize this if necessary [say by
130 recording something in the keyword table]. */
133 cgen_parse_keyword (strp
, keyword_table
, valuep
)
135 struct cgen_keyword
*keyword_table
;
138 const struct cgen_keyword_entry
*ke
;
144 /* Allow any first character. */
148 /* Now allow letters, digits, and _. */
149 while (isalnum (*p
) || *p
== '_')
153 return "unrecognized keyword/register name";
155 memcpy (buf
, *strp
, p
- *strp
);
158 ke
= cgen_keyword_lookup_name (keyword_table
, buf
);
167 return "unrecognized keyword/register name";
170 /* Signed integer parser. */
173 cgen_parse_signed_integer (strp
, opindex
, min
, max
, valuep
)
180 enum cgen_asm_result result
;
183 errmsg
= (*cgen_asm_parse_operand_fn
) (strp
, opindex
, BFD_RELOC_NONE
,
185 /* FIXME: Examine `result'. */
188 if (value
< min
|| value
> max
)
189 return "integer operand out of range";
195 /* Unsigned integer parser. */
198 cgen_parse_unsigned_integer (strp
, opindex
, min
, max
, valuep
)
201 unsigned long min
, max
;
202 unsigned long *valuep
;
205 enum cgen_asm_result result
;
208 errmsg
= (*cgen_asm_parse_operand_fn
) (strp
, opindex
, BFD_RELOC_NONE
,
210 /* FIXME: Examine `result'. */
213 if (value
< min
|| value
> max
)
214 return "integer operand out of range";
220 /* Address parser. */
223 cgen_parse_address (strp
, opindex
, opinfo
, valuep
)
230 enum cgen_asm_result result
;
233 errmsg
= (*cgen_asm_parse_operand_fn
) (strp
, opindex
, opinfo
,
235 /* FIXME: Examine `result'. */
243 /* Signed integer validation routine. */
246 cgen_validate_signed_integer (value
, min
, max
)
247 long value
, min
, max
;
249 if (value
< min
|| value
> max
)
252 "operand out of range (%ld not between %ld and %ld)";
253 static char buf
[100];
255 sprintf (buf
, err
, value
, min
, max
);
262 /* Unsigned integer validation routine.
263 Supplying `min' here may seem unnecessary, but we also want to handle
264 cases where min != 0 (and max > LONG_MAX). */
267 cgen_validate_unsigned_integer (value
, min
, max
)
268 unsigned long value
, min
, max
;
270 if (value
< min
|| value
> max
)
273 "operand out of range (%lu not between %lu and %lu)";
274 static char buf
[100];
276 sprintf (buf
, err
, value
, min
, max
);