daily update
[binutils.git] / opcodes / cgen-asm.c
blob7231e2d7aedfded367f70cfb8de7ffdae28a6296
1 /* CGEN generic assembler support code.
3 Copyright 1996, 1997, 1998, 1999, 2000, 2001 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)
10 any later version.
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. */
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"
31 static CGEN_INSN_LIST * hash_insn_array (CGEN_CPU_DESC, const CGEN_INSN *, int, int, CGEN_INSN_LIST **, CGEN_INSN_LIST *);
32 static CGEN_INSN_LIST * hash_insn_list (CGEN_CPU_DESC, const CGEN_INSN_LIST *, CGEN_INSN_LIST **, CGEN_INSN_LIST *);
33 static void build_asm_hash_table (CGEN_CPU_DESC);
35 /* Set the cgen_parse_operand_fn callback. */
37 void
38 cgen_set_parse_operand_fn (CGEN_CPU_DESC cd, cgen_parse_operand_fn fn)
40 cd->parse_operand_fn = fn;
43 /* Called whenever starting to parse an insn. */
45 void
46 cgen_init_parse_operand (CGEN_CPU_DESC cd)
48 /* This tells the callback to re-initialize. */
49 (void) (* cd->parse_operand_fn)
50 (cd, CGEN_PARSE_OPERAND_INIT, NULL, 0, 0, NULL, NULL);
53 /* Subroutine of build_asm_hash_table to add INSNS to the hash table.
55 COUNT is the number of elements in INSNS.
56 ENTSIZE is sizeof (CGEN_IBASE) for the target.
57 ??? No longer used but leave in for now.
58 HTABLE points to the hash table.
59 HENTBUF is a pointer to sufficiently large buffer of hash entries.
60 The result is a pointer to the next entry to use.
62 The table is scanned backwards as additions are made to the front of the
63 list and we want earlier ones to be prefered. */
65 static CGEN_INSN_LIST *
66 hash_insn_array (CGEN_CPU_DESC cd,
67 const CGEN_INSN *insns,
68 int count,
69 int entsize ATTRIBUTE_UNUSED,
70 CGEN_INSN_LIST **htable,
71 CGEN_INSN_LIST *hentbuf)
73 int i;
75 for (i = count - 1; i >= 0; --i, ++hentbuf)
77 unsigned int hash;
78 const CGEN_INSN *insn = &insns[i];
80 if (! (* cd->asm_hash_p) (insn))
81 continue;
82 hash = (* cd->asm_hash) (CGEN_INSN_MNEMONIC (insn));
83 hentbuf->next = htable[hash];
84 hentbuf->insn = insn;
85 htable[hash] = hentbuf;
88 return hentbuf;
91 /* Subroutine of build_asm_hash_table to add INSNS to the hash table.
92 This function is identical to hash_insn_array except the insns are
93 in a list. */
95 static CGEN_INSN_LIST *
96 hash_insn_list (CGEN_CPU_DESC cd,
97 const CGEN_INSN_LIST *insns,
98 CGEN_INSN_LIST **htable,
99 CGEN_INSN_LIST *hentbuf)
101 const CGEN_INSN_LIST *ilist;
103 for (ilist = insns; ilist != NULL; ilist = ilist->next, ++ hentbuf)
105 unsigned int hash;
107 if (! (* cd->asm_hash_p) (ilist->insn))
108 continue;
109 hash = (* cd->asm_hash) (CGEN_INSN_MNEMONIC (ilist->insn));
110 hentbuf->next = htable[hash];
111 hentbuf->insn = ilist->insn;
112 htable[hash] = hentbuf;
115 return hentbuf;
118 /* Build the assembler instruction hash table. */
120 static void
121 build_asm_hash_table (CGEN_CPU_DESC cd)
123 int count = cgen_insn_count (cd) + cgen_macro_insn_count (cd);
124 CGEN_INSN_TABLE *insn_table = &cd->insn_table;
125 CGEN_INSN_TABLE *macro_insn_table = &cd->macro_insn_table;
126 unsigned int hash_size = cd->asm_hash_size;
127 CGEN_INSN_LIST *hash_entry_buf;
128 CGEN_INSN_LIST **asm_hash_table;
129 CGEN_INSN_LIST *asm_hash_table_entries;
131 /* The space allocated for the hash table consists of two parts:
132 the hash table and the hash lists. */
134 asm_hash_table = (CGEN_INSN_LIST **)
135 xmalloc (hash_size * sizeof (CGEN_INSN_LIST *));
136 memset (asm_hash_table, 0, hash_size * sizeof (CGEN_INSN_LIST *));
137 asm_hash_table_entries = hash_entry_buf = (CGEN_INSN_LIST *)
138 xmalloc (count * sizeof (CGEN_INSN_LIST));
140 /* Add compiled in insns.
141 Don't include the first one as it is a reserved entry. */
142 /* ??? It was the end of all hash chains, and also the special
143 "invalid insn" marker. May be able to do it differently now. */
145 hash_entry_buf = hash_insn_array (cd,
146 insn_table->init_entries + 1,
147 insn_table->num_init_entries - 1,
148 insn_table->entry_size,
149 asm_hash_table, hash_entry_buf);
151 /* Add compiled in macro-insns. */
153 hash_entry_buf = hash_insn_array (cd, macro_insn_table->init_entries,
154 macro_insn_table->num_init_entries,
155 macro_insn_table->entry_size,
156 asm_hash_table, hash_entry_buf);
158 /* Add runtime added insns.
159 Later added insns will be prefered over earlier ones. */
161 hash_entry_buf = hash_insn_list (cd, insn_table->new_entries,
162 asm_hash_table, hash_entry_buf);
164 /* Add runtime added macro-insns. */
166 hash_insn_list (cd, macro_insn_table->new_entries,
167 asm_hash_table, hash_entry_buf);
169 cd->asm_hash_table = asm_hash_table;
170 cd->asm_hash_table_entries = asm_hash_table_entries;
173 /* Return the first entry in the hash list for INSN. */
175 CGEN_INSN_LIST *
176 cgen_asm_lookup_insn (CGEN_CPU_DESC cd, const char *insn)
178 unsigned int hash;
180 if (cd->asm_hash_table == NULL)
181 build_asm_hash_table (cd);
183 hash = (* cd->asm_hash) (insn);
184 return cd->asm_hash_table[hash];
187 /* Keyword parser.
188 The result is NULL upon success or an error message.
189 If successful, *STRP is updated to point passed the keyword.
191 ??? At present we have a static notion of how to pick out a keyword.
192 Later we can allow a target to customize this if necessary [say by
193 recording something in the keyword table]. */
195 const char *
196 cgen_parse_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
197 const char **strp,
198 CGEN_KEYWORD *keyword_table,
199 long *valuep)
201 const CGEN_KEYWORD_ENTRY *ke;
202 char buf[256];
203 const char *p,*start;
205 if (keyword_table->name_hash_table == NULL)
206 (void) cgen_keyword_search_init (keyword_table, NULL);
208 p = start = *strp;
210 /* Allow any first character. This is to make life easier for
211 the fairly common case of suffixes, eg. 'ld.b.w', where the first
212 character of the suffix ('.') is special. */
213 if (*p)
214 ++p;
216 /* Allow letters, digits, and any special characters. */
217 while (((p - start) < (int) sizeof (buf))
218 && *p
219 && (ISALNUM (*p)
220 || *p == '_'
221 || strchr (keyword_table->nonalpha_chars, *p)))
222 ++p;
224 if (p - start >= (int) sizeof (buf))
226 /* All non-empty CGEN keywords can fit into BUF. The only thing
227 we can match here is the empty keyword. */
228 buf[0] = 0;
230 else
232 memcpy (buf, start, p - start);
233 buf[p - start] = 0;
236 ke = cgen_keyword_lookup_name (keyword_table, buf);
238 if (ke != NULL)
240 *valuep = ke->value;
241 /* Don't advance pointer if we recognized the null keyword. */
242 if (ke->name[0] != 0)
243 *strp = p;
244 return NULL;
247 return "unrecognized keyword/register name";
250 /* Parse a small signed integer parser.
251 ??? VALUEP is not a bfd_vma * on purpose, though this is confusing.
252 Note that if the caller expects a bfd_vma result, it should call
253 cgen_parse_address. */
255 const char *
256 cgen_parse_signed_integer (CGEN_CPU_DESC cd,
257 const char **strp,
258 int opindex,
259 long *valuep)
261 bfd_vma value;
262 enum cgen_parse_operand_result result;
263 const char *errmsg;
265 errmsg = (* cd->parse_operand_fn)
266 (cd, CGEN_PARSE_OPERAND_INTEGER, strp, opindex, BFD_RELOC_NONE,
267 &result, &value);
268 /* FIXME: Examine `result'. */
269 if (!errmsg)
270 *valuep = value;
271 return errmsg;
274 /* Parse a small unsigned integer parser.
275 ??? VALUEP is not a bfd_vma * on purpose, though this is confusing.
276 Note that if the caller expects a bfd_vma result, it should call
277 cgen_parse_address. */
279 const char *
280 cgen_parse_unsigned_integer (CGEN_CPU_DESC cd,
281 const char **strp,
282 int opindex,
283 unsigned long *valuep)
285 bfd_vma value;
286 enum cgen_parse_operand_result result;
287 const char *errmsg;
289 errmsg = (* cd->parse_operand_fn)
290 (cd, CGEN_PARSE_OPERAND_INTEGER, strp, opindex, BFD_RELOC_NONE,
291 &result, &value);
292 /* FIXME: Examine `result'. */
293 if (!errmsg)
294 *valuep = value;
295 return errmsg;
298 /* Address parser. */
300 const char *
301 cgen_parse_address (CGEN_CPU_DESC cd,
302 const char **strp,
303 int opindex,
304 int opinfo,
305 enum cgen_parse_operand_result *resultp,
306 bfd_vma *valuep)
308 bfd_vma value;
309 enum cgen_parse_operand_result result_type;
310 const char *errmsg;
312 errmsg = (* cd->parse_operand_fn)
313 (cd, CGEN_PARSE_OPERAND_ADDRESS, strp, opindex, opinfo,
314 &result_type, &value);
315 /* FIXME: Examine `result'. */
316 if (!errmsg)
318 if (resultp != NULL)
319 *resultp = result_type;
320 *valuep = value;
322 return errmsg;
325 /* Signed integer validation routine. */
327 const char *
328 cgen_validate_signed_integer (long value, long min, long max)
330 if (value < min || value > max)
332 static char buf[100];
334 /* xgettext:c-format */
335 sprintf (buf, _("operand out of range (%ld not between %ld and %ld)"),
336 value, min, max);
337 return buf;
340 return NULL;
343 /* Unsigned integer validation routine.
344 Supplying `min' here may seem unnecessary, but we also want to handle
345 cases where min != 0 (and max > LONG_MAX). */
347 const char *
348 cgen_validate_unsigned_integer (unsigned long value,
349 unsigned long min,
350 unsigned long max)
352 if (value < min || value > max)
354 static char buf[100];
356 /* xgettext:c-format */
357 sprintf (buf, _("operand out of range (%lu not between %lu and %lu)"),
358 value, min, max);
359 return buf;
362 return NULL;