/src/binutils-gdb/opcodes/cgen-asm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* CGEN generic assembler support code. |
2 | | Copyright (C) 1996-2025 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of libopcodes. |
5 | | |
6 | | This library is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 3, or (at your option) |
9 | | any later version. |
10 | | |
11 | | It is distributed in the hope that it will be useful, but WITHOUT |
12 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
14 | | License for more details. |
15 | | |
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 | | #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 (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); |
34 | | |
35 | | /* Set the cgen_parse_operand_fn callback. */ |
36 | | |
37 | | void |
38 | | cgen_set_parse_operand_fn (CGEN_CPU_DESC cd, cgen_parse_operand_fn fn) |
39 | 0 | { |
40 | 0 | cd->parse_operand_fn = fn; |
41 | 0 | } |
42 | | |
43 | | /* Called whenever starting to parse an insn. */ |
44 | | |
45 | | void |
46 | | cgen_init_parse_operand (CGEN_CPU_DESC cd) |
47 | 0 | { |
48 | | /* This tells the callback to re-initialize. */ |
49 | 0 | (void) (* cd->parse_operand_fn) |
50 | 0 | (cd, CGEN_PARSE_OPERAND_INIT, NULL, 0, 0, NULL, NULL); |
51 | 0 | } |
52 | | |
53 | | /* Subroutine of build_asm_hash_table to add INSNS to the hash table. |
54 | | |
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. |
61 | | |
62 | | The table is scanned backwards as additions are made to the front of the |
63 | | list and we want earlier ones to be preferred. */ |
64 | | |
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) |
72 | 0 | { |
73 | 0 | int i; |
74 | |
|
75 | 0 | for (i = count - 1; i >= 0; --i, ++hentbuf) |
76 | 0 | { |
77 | 0 | unsigned int hash; |
78 | 0 | const CGEN_INSN *insn = &insns[i]; |
79 | |
|
80 | 0 | if (! (* cd->asm_hash_p) (insn)) |
81 | 0 | continue; |
82 | 0 | hash = (* cd->asm_hash) (CGEN_INSN_MNEMONIC (insn)); |
83 | 0 | hentbuf->next = htable[hash]; |
84 | 0 | hentbuf->insn = insn; |
85 | 0 | htable[hash] = hentbuf; |
86 | 0 | } |
87 | |
|
88 | 0 | return hentbuf; |
89 | 0 | } |
90 | | |
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. */ |
94 | | |
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) |
100 | 0 | { |
101 | 0 | const CGEN_INSN_LIST *ilist; |
102 | |
|
103 | 0 | for (ilist = insns; ilist != NULL; ilist = ilist->next, ++ hentbuf) |
104 | 0 | { |
105 | 0 | unsigned int hash; |
106 | |
|
107 | 0 | if (! (* cd->asm_hash_p) (ilist->insn)) |
108 | 0 | continue; |
109 | 0 | hash = (* cd->asm_hash) (CGEN_INSN_MNEMONIC (ilist->insn)); |
110 | 0 | hentbuf->next = htable[hash]; |
111 | 0 | hentbuf->insn = ilist->insn; |
112 | 0 | htable[hash] = hentbuf; |
113 | 0 | } |
114 | |
|
115 | 0 | return hentbuf; |
116 | 0 | } |
117 | | |
118 | | /* Build the assembler instruction hash table. */ |
119 | | |
120 | | static void |
121 | | build_asm_hash_table (CGEN_CPU_DESC cd) |
122 | 0 | { |
123 | 0 | int count = cgen_insn_count (cd) + cgen_macro_insn_count (cd); |
124 | 0 | CGEN_INSN_TABLE *insn_table = &cd->insn_table; |
125 | 0 | CGEN_INSN_TABLE *macro_insn_table = &cd->macro_insn_table; |
126 | 0 | unsigned int hash_size = cd->asm_hash_size; |
127 | 0 | CGEN_INSN_LIST *hash_entry_buf; |
128 | 0 | CGEN_INSN_LIST **asm_hash_table; |
129 | 0 | CGEN_INSN_LIST *asm_hash_table_entries; |
130 | | |
131 | | /* The space allocated for the hash table consists of two parts: |
132 | | the hash table and the hash lists. */ |
133 | |
|
134 | 0 | asm_hash_table = (CGEN_INSN_LIST **) |
135 | 0 | xmalloc (hash_size * sizeof (CGEN_INSN_LIST *)); |
136 | 0 | memset (asm_hash_table, 0, hash_size * sizeof (CGEN_INSN_LIST *)); |
137 | 0 | asm_hash_table_entries = hash_entry_buf = (CGEN_INSN_LIST *) |
138 | 0 | xmalloc (count * sizeof (CGEN_INSN_LIST)); |
139 | | |
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. */ |
144 | |
|
145 | 0 | hash_entry_buf = hash_insn_array (cd, |
146 | 0 | insn_table->init_entries + 1, |
147 | 0 | insn_table->num_init_entries - 1, |
148 | 0 | insn_table->entry_size, |
149 | 0 | asm_hash_table, hash_entry_buf); |
150 | | |
151 | | /* Add compiled in macro-insns. */ |
152 | |
|
153 | 0 | hash_entry_buf = hash_insn_array (cd, macro_insn_table->init_entries, |
154 | 0 | macro_insn_table->num_init_entries, |
155 | 0 | macro_insn_table->entry_size, |
156 | 0 | asm_hash_table, hash_entry_buf); |
157 | | |
158 | | /* Add runtime added insns. |
159 | | Later added insns will be preferred over earlier ones. */ |
160 | |
|
161 | 0 | hash_entry_buf = hash_insn_list (cd, insn_table->new_entries, |
162 | 0 | asm_hash_table, hash_entry_buf); |
163 | | |
164 | | /* Add runtime added macro-insns. */ |
165 | |
|
166 | 0 | hash_insn_list (cd, macro_insn_table->new_entries, |
167 | 0 | asm_hash_table, hash_entry_buf); |
168 | |
|
169 | 0 | cd->asm_hash_table = asm_hash_table; |
170 | 0 | cd->asm_hash_table_entries = asm_hash_table_entries; |
171 | 0 | } |
172 | | |
173 | | /* Return the first entry in the hash list for INSN. */ |
174 | | |
175 | | CGEN_INSN_LIST * |
176 | | cgen_asm_lookup_insn (CGEN_CPU_DESC cd, const char *insn) |
177 | 0 | { |
178 | 0 | unsigned int hash; |
179 | |
|
180 | 0 | if (cd->asm_hash_table == NULL) |
181 | 0 | build_asm_hash_table (cd); |
182 | |
|
183 | 0 | hash = (* cd->asm_hash) (insn); |
184 | 0 | return cd->asm_hash_table[hash]; |
185 | 0 | } |
186 | | |
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. |
190 | | |
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]. */ |
194 | | |
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) |
200 | 0 | { |
201 | 0 | const CGEN_KEYWORD_ENTRY *ke; |
202 | 0 | char buf[256]; |
203 | 0 | const char *p,*start; |
204 | |
|
205 | 0 | if (keyword_table->name_hash_table == NULL) |
206 | 0 | (void) cgen_keyword_search_init (keyword_table, NULL); |
207 | |
|
208 | 0 | p = start = *strp; |
209 | | |
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 | 0 | if (*p) |
214 | 0 | ++p; |
215 | | |
216 | | /* Allow letters, digits, and any special characters. */ |
217 | 0 | while (((p - start) < (int) sizeof (buf)) |
218 | 0 | && *p |
219 | 0 | && (ISALNUM (*p) |
220 | 0 | || *p == '_' |
221 | 0 | || strchr (keyword_table->nonalpha_chars, *p))) |
222 | 0 | ++p; |
223 | |
|
224 | 0 | if (p - start >= (int) sizeof (buf)) |
225 | 0 | { |
226 | | /* All non-empty CGEN keywords can fit into BUF. The only thing |
227 | | we can match here is the empty keyword. */ |
228 | 0 | buf[0] = 0; |
229 | 0 | } |
230 | 0 | else |
231 | 0 | { |
232 | 0 | memcpy (buf, start, p - start); |
233 | 0 | buf[p - start] = 0; |
234 | 0 | } |
235 | |
|
236 | 0 | ke = cgen_keyword_lookup_name (keyword_table, buf); |
237 | |
|
238 | 0 | if (ke != NULL) |
239 | 0 | { |
240 | 0 | *valuep = ke->value; |
241 | | /* Don't advance pointer if we recognized the null keyword. */ |
242 | 0 | if (ke->name[0] != 0) |
243 | 0 | *strp = p; |
244 | 0 | return NULL; |
245 | 0 | } |
246 | | |
247 | 0 | return "unrecognized keyword/register name"; |
248 | 0 | } |
249 | | |
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. */ |
254 | | |
255 | | const char * |
256 | | cgen_parse_signed_integer (CGEN_CPU_DESC cd, |
257 | | const char **strp, |
258 | | int opindex, |
259 | | long *valuep) |
260 | 0 | { |
261 | 0 | bfd_vma value; |
262 | 0 | enum cgen_parse_operand_result result; |
263 | 0 | const char *errmsg; |
264 | |
|
265 | 0 | errmsg = (* cd->parse_operand_fn) |
266 | 0 | (cd, CGEN_PARSE_OPERAND_INTEGER, strp, opindex, BFD_RELOC_NONE, |
267 | 0 | &result, &value); |
268 | | /* FIXME: Examine `result'. */ |
269 | 0 | if (!errmsg) |
270 | 0 | { |
271 | | /* Handle the case where a hex value is parsed on a 64-bit host. |
272 | | A value like 0xffffe000 is clearly intended to be a negative |
273 | | 16-bit value, but on a 64-bit host it will be parsed by gas |
274 | | as 0x00000000ffffe000. |
275 | | |
276 | | The shifts below are designed not to produce compile time |
277 | | warnings on a 32-bit host. */ |
278 | 0 | if (sizeof (value) > 4 |
279 | 0 | && result == CGEN_PARSE_OPERAND_RESULT_NUMBER |
280 | 0 | && value > 0 |
281 | 0 | && (value & 0x80000000) |
282 | 0 | && ((value >> 31) == 1)) |
283 | 0 | value |= ((bfd_vma) -1) << 31; |
284 | |
|
285 | 0 | *valuep = value; |
286 | 0 | } |
287 | 0 | return errmsg; |
288 | 0 | } |
289 | | |
290 | | /* Parse a small unsigned integer parser. |
291 | | ??? VALUEP is not a bfd_vma * on purpose, though this is confusing. |
292 | | Note that if the caller expects a bfd_vma result, it should call |
293 | | cgen_parse_address. */ |
294 | | |
295 | | const char * |
296 | | cgen_parse_unsigned_integer (CGEN_CPU_DESC cd, |
297 | | const char **strp, |
298 | | int opindex, |
299 | | unsigned long *valuep) |
300 | 0 | { |
301 | 0 | bfd_vma value; |
302 | 0 | enum cgen_parse_operand_result result; |
303 | 0 | const char *errmsg; |
304 | |
|
305 | 0 | errmsg = (* cd->parse_operand_fn) |
306 | 0 | (cd, CGEN_PARSE_OPERAND_INTEGER, strp, opindex, BFD_RELOC_NONE, |
307 | 0 | &result, &value); |
308 | | /* FIXME: Examine `result'. */ |
309 | 0 | if (!errmsg) |
310 | 0 | *valuep = value; |
311 | 0 | return errmsg; |
312 | 0 | } |
313 | | |
314 | | /* Address parser. */ |
315 | | |
316 | | const char * |
317 | | cgen_parse_address (CGEN_CPU_DESC cd, |
318 | | const char **strp, |
319 | | int opindex, |
320 | | int opinfo, |
321 | | enum cgen_parse_operand_result *resultp, |
322 | | bfd_vma *valuep) |
323 | 0 | { |
324 | 0 | bfd_vma value; |
325 | 0 | enum cgen_parse_operand_result result_type; |
326 | 0 | const char *errmsg; |
327 | |
|
328 | 0 | errmsg = (* cd->parse_operand_fn) |
329 | 0 | (cd, CGEN_PARSE_OPERAND_ADDRESS, strp, opindex, opinfo, |
330 | 0 | &result_type, &value); |
331 | | /* FIXME: Examine `result'. */ |
332 | 0 | if (!errmsg) |
333 | 0 | { |
334 | 0 | if (resultp != NULL) |
335 | 0 | *resultp = result_type; |
336 | 0 | *valuep = value; |
337 | 0 | } |
338 | 0 | return errmsg; |
339 | 0 | } |
340 | | |
341 | | /* Signed integer validation routine. */ |
342 | | |
343 | | const char * |
344 | | cgen_validate_signed_integer (long value, long min, long max) |
345 | 0 | { |
346 | 0 | if (value < min || value > max) |
347 | 0 | { |
348 | 0 | static char buf[100]; |
349 | | |
350 | | /* xgettext:c-format */ |
351 | 0 | sprintf (buf, _("operand out of range (%ld not between %ld and %ld)"), |
352 | 0 | value, min, max); |
353 | 0 | return buf; |
354 | 0 | } |
355 | | |
356 | 0 | return NULL; |
357 | 0 | } |
358 | | |
359 | | /* Unsigned integer validation routine. |
360 | | Supplying `min' here may seem unnecessary, but we also want to handle |
361 | | cases where min != 0 (and max > LONG_MAX). */ |
362 | | |
363 | | const char * |
364 | | cgen_validate_unsigned_integer (unsigned long value, |
365 | | unsigned long min, |
366 | | unsigned long max) |
367 | 0 | { |
368 | 0 | if (value < min || value > max) |
369 | 0 | { |
370 | 0 | static char buf[100]; |
371 | | |
372 | | /* xgettext:c-format */ |
373 | 0 | sprintf (buf, _("operand out of range (%lu not between %lu and %lu)"), |
374 | 0 | value, min, max); |
375 | 0 | return buf; |
376 | 0 | } |
377 | | |
378 | 0 | return NULL; |
379 | 0 | } |