1 | /* CGEN generic assembler support code.
|
---|
2 |
|
---|
3 | Copyright 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | This file is part of the GNU Binutils and GDB, the GNU debugger.
|
---|
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, or (at your option)
|
---|
10 | 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 | #include "sysdep.h"
|
---|
22 | #include <stdio.h>
|
---|
23 | #include "ansidecl.h"
|
---|
24 | #include "libiberty.h"
|
---|
25 | #include "safe-ctype.h"
|
---|
26 | #include "bfd.h"
|
---|
27 | #include "symcat.h"
|
---|
28 | #include "opcode/cgen.h"
|
---|
29 | #include "opintl.h"
|
---|
30 |
|
---|
31 | static CGEN_INSN_LIST * hash_insn_array PARAMS ((CGEN_CPU_DESC, const CGEN_INSN *, int, int, CGEN_INSN_LIST **, CGEN_INSN_LIST *));
|
---|
32 | static CGEN_INSN_LIST * hash_insn_list PARAMS ((CGEN_CPU_DESC, const CGEN_INSN_LIST *, CGEN_INSN_LIST **, CGEN_INSN_LIST *));
|
---|
33 | static void build_asm_hash_table PARAMS ((CGEN_CPU_DESC));
|
---|
34 |
|
---|
35 | /* Set the cgen_parse_operand_fn callback. */
|
---|
36 |
|
---|
37 | void
|
---|
38 | cgen_set_parse_operand_fn (cd, fn)
|
---|
39 | CGEN_CPU_DESC cd;
|
---|
40 | cgen_parse_operand_fn fn;
|
---|
41 | {
|
---|
42 | cd->parse_operand_fn = fn;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /* Called whenever starting to parse an insn. */
|
---|
46 |
|
---|
47 | void
|
---|
48 | cgen_init_parse_operand (cd)
|
---|
49 | CGEN_CPU_DESC cd;
|
---|
50 | {
|
---|
51 | /* This tells the callback to re-initialize. */
|
---|
52 | (void) (* cd->parse_operand_fn)
|
---|
53 | (cd, CGEN_PARSE_OPERAND_INIT, NULL, 0, 0, NULL, NULL);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /* Subroutine of build_asm_hash_table to add INSNS to the hash table.
|
---|
57 |
|
---|
58 | COUNT is the number of elements in INSNS.
|
---|
59 | ENTSIZE is sizeof (CGEN_IBASE) for the target.
|
---|
60 | ??? No longer used but leave in for now.
|
---|
61 | HTABLE points to the hash table.
|
---|
62 | HENTBUF is a pointer to sufficiently large buffer of hash entries.
|
---|
63 | The result is a pointer to the next entry to use.
|
---|
64 |
|
---|
65 | The table is scanned backwards as additions are made to the front of the
|
---|
66 | list and we want earlier ones to be prefered. */
|
---|
67 |
|
---|
68 | static CGEN_INSN_LIST *
|
---|
69 | hash_insn_array (cd, insns, count, entsize, htable, hentbuf)
|
---|
70 | CGEN_CPU_DESC cd;
|
---|
71 | const CGEN_INSN *insns;
|
---|
72 | int count;
|
---|
73 | int entsize ATTRIBUTE_UNUSED;
|
---|
74 | CGEN_INSN_LIST **htable;
|
---|
75 | CGEN_INSN_LIST *hentbuf;
|
---|
76 | {
|
---|
77 | int i;
|
---|
78 |
|
---|
79 | for (i = count - 1; i >= 0; --i, ++hentbuf)
|
---|
80 | {
|
---|
81 | unsigned int hash;
|
---|
82 | const CGEN_INSN *insn = &insns[i];
|
---|
83 |
|
---|
84 | if (! (* cd->asm_hash_p) (insn))
|
---|
85 | continue;
|
---|
86 | hash = (* cd->asm_hash) (CGEN_INSN_MNEMONIC (insn));
|
---|
87 | hentbuf->next = htable[hash];
|
---|
88 | hentbuf->insn = insn;
|
---|
89 | htable[hash] = hentbuf;
|
---|
90 | }
|
---|
91 |
|
---|
92 | return hentbuf;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /* Subroutine of build_asm_hash_table to add INSNS to the hash table.
|
---|
96 | This function is identical to hash_insn_array except the insns are
|
---|
97 | in a list. */
|
---|
98 |
|
---|
99 | static CGEN_INSN_LIST *
|
---|
100 | hash_insn_list (cd, insns, htable, hentbuf)
|
---|
101 | CGEN_CPU_DESC cd;
|
---|
102 | const CGEN_INSN_LIST *insns;
|
---|
103 | CGEN_INSN_LIST **htable;
|
---|
104 | CGEN_INSN_LIST *hentbuf;
|
---|
105 | {
|
---|
106 | const CGEN_INSN_LIST *ilist;
|
---|
107 |
|
---|
108 | for (ilist = insns; ilist != NULL; ilist = ilist->next, ++ hentbuf)
|
---|
109 | {
|
---|
110 | unsigned int hash;
|
---|
111 |
|
---|
112 | if (! (* cd->asm_hash_p) (ilist->insn))
|
---|
113 | continue;
|
---|
114 | hash = (* cd->asm_hash) (CGEN_INSN_MNEMONIC (ilist->insn));
|
---|
115 | hentbuf->next = htable[hash];
|
---|
116 | hentbuf->insn = ilist->insn;
|
---|
117 | htable[hash] = hentbuf;
|
---|
118 | }
|
---|
119 |
|
---|
120 | return hentbuf;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* Build the assembler instruction hash table. */
|
---|
124 |
|
---|
125 | static void
|
---|
126 | build_asm_hash_table (cd)
|
---|
127 | CGEN_CPU_DESC cd;
|
---|
128 | {
|
---|
129 | int count = cgen_insn_count (cd) + cgen_macro_insn_count (cd);
|
---|
130 | CGEN_INSN_TABLE *insn_table = &cd->insn_table;
|
---|
131 | CGEN_INSN_TABLE *macro_insn_table = &cd->macro_insn_table;
|
---|
132 | unsigned int hash_size = cd->asm_hash_size;
|
---|
133 | CGEN_INSN_LIST *hash_entry_buf;
|
---|
134 | CGEN_INSN_LIST **asm_hash_table;
|
---|
135 | CGEN_INSN_LIST *asm_hash_table_entries;
|
---|
136 |
|
---|
137 | /* The space allocated for the hash table consists of two parts:
|
---|
138 | the hash table and the hash lists. */
|
---|
139 |
|
---|
140 | asm_hash_table = (CGEN_INSN_LIST **)
|
---|
141 | xmalloc (hash_size * sizeof (CGEN_INSN_LIST *));
|
---|
142 | memset (asm_hash_table, 0, hash_size * sizeof (CGEN_INSN_LIST *));
|
---|
143 | asm_hash_table_entries = hash_entry_buf = (CGEN_INSN_LIST *)
|
---|
144 | xmalloc (count * sizeof (CGEN_INSN_LIST));
|
---|
145 |
|
---|
146 | /* Add compiled in insns.
|
---|
147 | Don't include the first one as it is a reserved entry. */
|
---|
148 | /* ??? It was the end of all hash chains, and also the special
|
---|
149 | "invalid insn" marker. May be able to do it differently now. */
|
---|
150 |
|
---|
151 | hash_entry_buf = hash_insn_array (cd,
|
---|
152 | insn_table->init_entries + 1,
|
---|
153 | insn_table->num_init_entries - 1,
|
---|
154 | insn_table->entry_size,
|
---|
155 | asm_hash_table, hash_entry_buf);
|
---|
156 |
|
---|
157 | /* Add compiled in macro-insns. */
|
---|
158 |
|
---|
159 | hash_entry_buf = hash_insn_array (cd, macro_insn_table->init_entries,
|
---|
160 | macro_insn_table->num_init_entries,
|
---|
161 | macro_insn_table->entry_size,
|
---|
162 | asm_hash_table, hash_entry_buf);
|
---|
163 |
|
---|
164 | /* Add runtime added insns.
|
---|
165 | Later added insns will be prefered over earlier ones. */
|
---|
166 |
|
---|
167 | hash_entry_buf = hash_insn_list (cd, insn_table->new_entries,
|
---|
168 | asm_hash_table, hash_entry_buf);
|
---|
169 |
|
---|
170 | /* Add runtime added macro-insns. */
|
---|
171 |
|
---|
172 | hash_insn_list (cd, macro_insn_table->new_entries,
|
---|
173 | asm_hash_table, hash_entry_buf);
|
---|
174 |
|
---|
175 | cd->asm_hash_table = asm_hash_table;
|
---|
176 | cd->asm_hash_table_entries = asm_hash_table_entries;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /* Return the first entry in the hash list for INSN. */
|
---|
180 |
|
---|
181 | CGEN_INSN_LIST *
|
---|
182 | cgen_asm_lookup_insn (cd, insn)
|
---|
183 | CGEN_CPU_DESC cd;
|
---|
184 | const char *insn;
|
---|
185 | {
|
---|
186 | unsigned int hash;
|
---|
187 |
|
---|
188 | if (cd->asm_hash_table == NULL)
|
---|
189 | build_asm_hash_table (cd);
|
---|
190 |
|
---|
191 | hash = (* cd->asm_hash) (insn);
|
---|
192 | return cd->asm_hash_table[hash];
|
---|
193 | }
|
---|
194 | |
---|
195 |
|
---|
196 | /* Keyword parser.
|
---|
197 | The result is NULL upon success or an error message.
|
---|
198 | If successful, *STRP is updated to point passed the keyword.
|
---|
199 |
|
---|
200 | ??? At present we have a static notion of how to pick out a keyword.
|
---|
201 | Later we can allow a target to customize this if necessary [say by
|
---|
202 | recording something in the keyword table]. */
|
---|
203 |
|
---|
204 | const char *
|
---|
205 | cgen_parse_keyword (cd, strp, keyword_table, valuep)
|
---|
206 | CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
|
---|
207 | const char **strp;
|
---|
208 | CGEN_KEYWORD *keyword_table;
|
---|
209 | long *valuep;
|
---|
210 | {
|
---|
211 | const CGEN_KEYWORD_ENTRY *ke;
|
---|
212 | char buf[256];
|
---|
213 | const char *p,*start;
|
---|
214 |
|
---|
215 | if (keyword_table->name_hash_table == NULL)
|
---|
216 | (void) cgen_keyword_search_init (keyword_table, NULL);
|
---|
217 |
|
---|
218 | p = start = *strp;
|
---|
219 |
|
---|
220 | /* Allow any first character. This is to make life easier for
|
---|
221 | the fairly common case of suffixes, eg. 'ld.b.w', where the first
|
---|
222 | character of the suffix ('.') is special. */
|
---|
223 | if (*p)
|
---|
224 | ++p;
|
---|
225 |
|
---|
226 | /* Allow letters, digits, and any special characters. */
|
---|
227 | while (((p - start) < (int) sizeof (buf))
|
---|
228 | && *p
|
---|
229 | && (ISALNUM (*p)
|
---|
230 | || *p == '_'
|
---|
231 | || strchr (keyword_table->nonalpha_chars, *p)))
|
---|
232 | ++p;
|
---|
233 |
|
---|
234 | if (p - start >= (int) sizeof (buf))
|
---|
235 | {
|
---|
236 | /* All non-empty CGEN keywords can fit into BUF. The only thing
|
---|
237 | we can match here is the empty keyword. */
|
---|
238 | buf[0] = 0;
|
---|
239 | }
|
---|
240 | else
|
---|
241 | {
|
---|
242 | memcpy (buf, start, p - start);
|
---|
243 | buf[p - start] = 0;
|
---|
244 | }
|
---|
245 |
|
---|
246 | ke = cgen_keyword_lookup_name (keyword_table, buf);
|
---|
247 |
|
---|
248 | if (ke != NULL)
|
---|
249 | {
|
---|
250 | *valuep = ke->value;
|
---|
251 | /* Don't advance pointer if we recognized the null keyword. */
|
---|
252 | if (ke->name[0] != 0)
|
---|
253 | *strp = p;
|
---|
254 | return NULL;
|
---|
255 | }
|
---|
256 |
|
---|
257 | return "unrecognized keyword/register name";
|
---|
258 | }
|
---|
259 |
|
---|
260 | /* Parse a small signed integer parser.
|
---|
261 | ??? VALUEP is not a bfd_vma * on purpose, though this is confusing.
|
---|
262 | Note that if the caller expects a bfd_vma result, it should call
|
---|
263 | cgen_parse_address. */
|
---|
264 |
|
---|
265 | const char *
|
---|
266 | cgen_parse_signed_integer (cd, strp, opindex, valuep)
|
---|
267 | CGEN_CPU_DESC cd;
|
---|
268 | const char **strp;
|
---|
269 | int opindex;
|
---|
270 | long *valuep;
|
---|
271 | {
|
---|
272 | bfd_vma value;
|
---|
273 | enum cgen_parse_operand_result result;
|
---|
274 | const char *errmsg;
|
---|
275 |
|
---|
276 | errmsg = (* cd->parse_operand_fn)
|
---|
277 | (cd, CGEN_PARSE_OPERAND_INTEGER, strp, opindex, BFD_RELOC_NONE,
|
---|
278 | &result, &value);
|
---|
279 | /* FIXME: Examine `result'. */
|
---|
280 | if (!errmsg)
|
---|
281 | *valuep = value;
|
---|
282 | return errmsg;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /* Parse a small unsigned integer parser.
|
---|
286 | ??? VALUEP is not a bfd_vma * on purpose, though this is confusing.
|
---|
287 | Note that if the caller expects a bfd_vma result, it should call
|
---|
288 | cgen_parse_address. */
|
---|
289 |
|
---|
290 | const char *
|
---|
291 | cgen_parse_unsigned_integer (cd, strp, opindex, valuep)
|
---|
292 | CGEN_CPU_DESC cd;
|
---|
293 | const char **strp;
|
---|
294 | int opindex;
|
---|
295 | unsigned long *valuep;
|
---|
296 | {
|
---|
297 | bfd_vma value;
|
---|
298 | enum cgen_parse_operand_result result;
|
---|
299 | const char *errmsg;
|
---|
300 |
|
---|
301 | errmsg = (* cd->parse_operand_fn)
|
---|
302 | (cd, CGEN_PARSE_OPERAND_INTEGER, strp, opindex, BFD_RELOC_NONE,
|
---|
303 | &result, &value);
|
---|
304 | /* FIXME: Examine `result'. */
|
---|
305 | if (!errmsg)
|
---|
306 | *valuep = value;
|
---|
307 | return errmsg;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /* Address parser. */
|
---|
311 |
|
---|
312 | const char *
|
---|
313 | cgen_parse_address (cd, strp, opindex, opinfo, resultp, valuep)
|
---|
314 | CGEN_CPU_DESC cd;
|
---|
315 | const char **strp;
|
---|
316 | int opindex;
|
---|
317 | int opinfo;
|
---|
318 | enum cgen_parse_operand_result *resultp;
|
---|
319 | bfd_vma *valuep;
|
---|
320 | {
|
---|
321 | bfd_vma value;
|
---|
322 | enum cgen_parse_operand_result result_type;
|
---|
323 | const char *errmsg;
|
---|
324 |
|
---|
325 | errmsg = (* cd->parse_operand_fn)
|
---|
326 | (cd, CGEN_PARSE_OPERAND_ADDRESS, strp, opindex, opinfo,
|
---|
327 | &result_type, &value);
|
---|
328 | /* FIXME: Examine `result'. */
|
---|
329 | if (!errmsg)
|
---|
330 | {
|
---|
331 | if (resultp != NULL)
|
---|
332 | *resultp = result_type;
|
---|
333 | *valuep = value;
|
---|
334 | }
|
---|
335 | return errmsg;
|
---|
336 | }
|
---|
337 | |
---|
338 |
|
---|
339 | /* Signed integer validation routine. */
|
---|
340 |
|
---|
341 | const char *
|
---|
342 | cgen_validate_signed_integer (value, min, max)
|
---|
343 | long value, min, max;
|
---|
344 | {
|
---|
345 | if (value < min || value > max)
|
---|
346 | {
|
---|
347 | static char buf[100];
|
---|
348 |
|
---|
349 | /* xgettext:c-format */
|
---|
350 | sprintf (buf, _("operand out of range (%ld not between %ld and %ld)"),
|
---|
351 | value, min, max);
|
---|
352 | return buf;
|
---|
353 | }
|
---|
354 |
|
---|
355 | return NULL;
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* Unsigned integer validation routine.
|
---|
359 | Supplying `min' here may seem unnecessary, but we also want to handle
|
---|
360 | cases where min != 0 (and max > LONG_MAX). */
|
---|
361 |
|
---|
362 | const char *
|
---|
363 | cgen_validate_unsigned_integer (value, min, max)
|
---|
364 | unsigned long value, min, max;
|
---|
365 | {
|
---|
366 | if (value < min || value > max)
|
---|
367 | {
|
---|
368 | static char buf[100];
|
---|
369 |
|
---|
370 | /* xgettext:c-format */
|
---|
371 | sprintf (buf, _("operand out of range (%lu not between %lu and %lu)"),
|
---|
372 | value, min, max);
|
---|
373 | return buf;
|
---|
374 | }
|
---|
375 |
|
---|
376 | return NULL;
|
---|
377 | }
|
---|