/src/binutils-gdb/opcodes/ip2k-asm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* DO NOT EDIT! -*- buffer-read-only: t -*- vi:set ro: */ |
2 | | /* Assembler interface for targets using CGEN. -*- C -*- |
3 | | CGEN: Cpu tools GENerator |
4 | | |
5 | | THIS FILE IS MACHINE GENERATED WITH CGEN. |
6 | | - the resultant file is machine generated, cgen-asm.in isn't |
7 | | |
8 | | Copyright (C) 1996-2025 Free Software Foundation, Inc. |
9 | | |
10 | | This file is part of libopcodes. |
11 | | |
12 | | This library is free software; you can redistribute it and/or modify |
13 | | it under the terms of the GNU General Public License as published by |
14 | | the Free Software Foundation; either version 3, or (at your option) |
15 | | any later version. |
16 | | |
17 | | It is distributed in the hope that it will be useful, but WITHOUT |
18 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
19 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
20 | | License for more details. |
21 | | |
22 | | You should have received a copy of the GNU General Public License |
23 | | along with this program; if not, write to the Free Software Foundation, Inc., |
24 | | 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
25 | | |
26 | | |
27 | | /* ??? Eventually more and more of this stuff can go to cpu-independent files. |
28 | | Keep that in mind. */ |
29 | | |
30 | | #include "sysdep.h" |
31 | | #include <stdio.h> |
32 | | #include "ansidecl.h" |
33 | | #include "bfd.h" |
34 | | #include "symcat.h" |
35 | | #include "ip2k-desc.h" |
36 | | #include "ip2k-opc.h" |
37 | | #include "opintl.h" |
38 | | #include "xregex.h" |
39 | | #include "libiberty.h" |
40 | | #include "safe-ctype.h" |
41 | | |
42 | | #undef min |
43 | | #define min(a,b) ((a) < (b) ? (a) : (b)) |
44 | | #undef max |
45 | | #define max(a,b) ((a) > (b) ? (a) : (b)) |
46 | | |
47 | | static const char * parse_insn_normal |
48 | | (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *); |
49 | | |
50 | | /* -- assembler routines inserted here. */ |
51 | | |
52 | | /* -- asm.c */ |
53 | | |
54 | | static const char * |
55 | | parse_fr (CGEN_CPU_DESC cd, |
56 | | const char **strp, |
57 | | int opindex, |
58 | | unsigned long *valuep) |
59 | 0 | { |
60 | 0 | const char *errmsg; |
61 | 0 | const char *old_strp; |
62 | 0 | char *afteroffset; |
63 | 0 | enum cgen_parse_operand_result result_type; |
64 | 0 | bfd_vma value; |
65 | 0 | extern CGEN_KEYWORD ip2k_cgen_opval_register_names; |
66 | 0 | bfd_vma tempvalue; |
67 | |
|
68 | 0 | old_strp = *strp; |
69 | 0 | afteroffset = NULL; |
70 | | |
71 | | /* Check here to see if you're about to try parsing a w as the first arg |
72 | | and return an error if you are. */ |
73 | 0 | if ((strncmp (*strp, "w", 1) == 0) || (strncmp (*strp, "W", 1) == 0)) |
74 | 0 | { |
75 | 0 | (*strp)++; |
76 | |
|
77 | 0 | if ((strncmp (*strp, ",", 1) == 0) || ISSPACE (**strp)) |
78 | 0 | { |
79 | | /* We've been passed a w. Return with an error message so that |
80 | | cgen will try the next parsing option. */ |
81 | 0 | errmsg = _("W keyword invalid in FR operand slot."); |
82 | 0 | return errmsg; |
83 | 0 | } |
84 | 0 | *strp = old_strp; |
85 | 0 | } |
86 | | |
87 | | /* Attempt parse as register keyword. */ |
88 | 0 | errmsg = cgen_parse_keyword (cd, strp, & ip2k_cgen_opval_register_names, |
89 | 0 | (long *) valuep); |
90 | 0 | if (*strp != NULL |
91 | 0 | && errmsg == NULL) |
92 | 0 | return errmsg; |
93 | | |
94 | | /* Attempt to parse for "(IP)". */ |
95 | 0 | afteroffset = strstr (*strp, "(IP)"); |
96 | |
|
97 | 0 | if (afteroffset == NULL) |
98 | | /* Make sure it's not in lower case. */ |
99 | 0 | afteroffset = strstr (*strp, "(ip)"); |
100 | |
|
101 | 0 | if (afteroffset != NULL) |
102 | 0 | { |
103 | 0 | if (afteroffset != *strp) |
104 | 0 | { |
105 | | /* Invalid offset present. */ |
106 | 0 | errmsg = _("offset(IP) is not a valid form"); |
107 | 0 | return errmsg; |
108 | 0 | } |
109 | 0 | else |
110 | 0 | { |
111 | 0 | *strp += 4; |
112 | 0 | *valuep = 0; |
113 | 0 | errmsg = NULL; |
114 | 0 | return errmsg; |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | /* Attempt to parse for DP. ex: mov w, offset(DP) |
119 | | mov offset(DP),w */ |
120 | | |
121 | | /* Try parsing it as an address and see what comes back. */ |
122 | 0 | afteroffset = strstr (*strp, "(DP)"); |
123 | |
|
124 | 0 | if (afteroffset == NULL) |
125 | | /* Maybe it's in lower case. */ |
126 | 0 | afteroffset = strstr (*strp, "(dp)"); |
127 | |
|
128 | 0 | if (afteroffset != NULL) |
129 | 0 | { |
130 | 0 | if (afteroffset == *strp) |
131 | 0 | { |
132 | | /* No offset present. Use 0 by default. */ |
133 | 0 | tempvalue = 0; |
134 | 0 | errmsg = NULL; |
135 | 0 | } |
136 | 0 | else |
137 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, |
138 | 0 | BFD_RELOC_IP2K_FR_OFFSET, |
139 | 0 | & result_type, & tempvalue); |
140 | |
|
141 | 0 | if (errmsg == NULL) |
142 | 0 | { |
143 | 0 | if (tempvalue <= 127) |
144 | 0 | { |
145 | | /* Value is ok. Fix up the first 2 bits and return. */ |
146 | 0 | *valuep = 0x0100 | tempvalue; |
147 | 0 | *strp += 4; /* Skip over the (DP) in *strp. */ |
148 | 0 | return errmsg; |
149 | 0 | } |
150 | 0 | else |
151 | 0 | { |
152 | | /* Found something there in front of (DP) but it's out |
153 | | of range. */ |
154 | 0 | errmsg = _("(DP) offset out of range."); |
155 | 0 | return errmsg; |
156 | 0 | } |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | |
161 | | /* Attempt to parse for SP. ex: mov w, offset(SP) |
162 | | mov offset(SP), w. */ |
163 | 0 | afteroffset = strstr (*strp, "(SP)"); |
164 | |
|
165 | 0 | if (afteroffset == NULL) |
166 | | /* Maybe it's in lower case. */ |
167 | 0 | afteroffset = strstr (*strp, "(sp)"); |
168 | |
|
169 | 0 | if (afteroffset != NULL) |
170 | 0 | { |
171 | 0 | if (afteroffset == *strp) |
172 | 0 | { |
173 | | /* No offset present. Use 0 by default. */ |
174 | 0 | tempvalue = 0; |
175 | 0 | errmsg = NULL; |
176 | 0 | } |
177 | 0 | else |
178 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, |
179 | 0 | BFD_RELOC_IP2K_FR_OFFSET, |
180 | 0 | & result_type, & tempvalue); |
181 | |
|
182 | 0 | if (errmsg == NULL) |
183 | 0 | { |
184 | 0 | if (tempvalue <= 127) |
185 | 0 | { |
186 | | /* Value is ok. Fix up the first 2 bits and return. */ |
187 | 0 | *valuep = 0x0180 | tempvalue; |
188 | 0 | *strp += 4; /* Skip over the (SP) in *strp. */ |
189 | 0 | return errmsg; |
190 | 0 | } |
191 | 0 | else |
192 | 0 | { |
193 | | /* Found something there in front of (SP) but it's out |
194 | | of range. */ |
195 | 0 | errmsg = _("(SP) offset out of range."); |
196 | 0 | return errmsg; |
197 | 0 | } |
198 | 0 | } |
199 | 0 | } |
200 | | |
201 | | /* Attempt to parse as an address. */ |
202 | 0 | *strp = old_strp; |
203 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, BFD_RELOC_IP2K_FR9, |
204 | 0 | & result_type, & value); |
205 | 0 | if (errmsg == NULL) |
206 | 0 | { |
207 | 0 | *valuep = value; |
208 | | |
209 | | /* If a parenthesis is found, warn about invalid form. */ |
210 | 0 | if (**strp == '(') |
211 | 0 | errmsg = _("illegal use of parentheses"); |
212 | | |
213 | | /* If a numeric value is specified, ensure that it is between |
214 | | 1 and 255. */ |
215 | 0 | else if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
216 | 0 | { |
217 | 0 | if (value < 0x1 || value > 0xff) |
218 | 0 | errmsg = _("operand out of range (not between 1 and 255)"); |
219 | 0 | } |
220 | 0 | } |
221 | 0 | return errmsg; |
222 | 0 | } |
223 | | |
224 | | static const char * |
225 | | parse_addr16 (CGEN_CPU_DESC cd, |
226 | | const char **strp, |
227 | | int opindex, |
228 | | unsigned long *valuep) |
229 | 0 | { |
230 | 0 | const char *errmsg; |
231 | 0 | enum cgen_parse_operand_result result_type; |
232 | 0 | bfd_reloc_code_real_type code = BFD_RELOC_NONE; |
233 | 0 | bfd_vma value; |
234 | |
|
235 | 0 | if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16H) |
236 | 0 | code = BFD_RELOC_IP2K_HI8DATA; |
237 | 0 | else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16L) |
238 | 0 | code = BFD_RELOC_IP2K_LO8DATA; |
239 | 0 | else |
240 | 0 | { |
241 | | /* Something is very wrong. opindex has to be one of the above. */ |
242 | 0 | errmsg = _("parse_addr16: invalid opindex."); |
243 | 0 | return errmsg; |
244 | 0 | } |
245 | | |
246 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, code, |
247 | 0 | & result_type, & value); |
248 | 0 | if (errmsg == NULL) |
249 | 0 | { |
250 | | /* We either have a relocation or a number now. */ |
251 | 0 | if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
252 | 0 | { |
253 | | /* We got a number back. */ |
254 | 0 | if (code == BFD_RELOC_IP2K_HI8DATA) |
255 | 0 | value >>= 8; |
256 | 0 | else |
257 | | /* code = BFD_RELOC_IP2K_LOW8DATA. */ |
258 | 0 | value &= 0x00FF; |
259 | 0 | } |
260 | 0 | *valuep = value; |
261 | 0 | } |
262 | |
|
263 | 0 | return errmsg; |
264 | 0 | } |
265 | | |
266 | | static const char * |
267 | | parse_addr16_cjp (CGEN_CPU_DESC cd, |
268 | | const char **strp, |
269 | | int opindex, |
270 | | unsigned long *valuep) |
271 | 0 | { |
272 | 0 | const char *errmsg; |
273 | 0 | enum cgen_parse_operand_result result_type; |
274 | 0 | bfd_reloc_code_real_type code = BFD_RELOC_NONE; |
275 | 0 | bfd_vma value; |
276 | |
|
277 | 0 | if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP) |
278 | 0 | code = BFD_RELOC_IP2K_ADDR16CJP; |
279 | 0 | else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P) |
280 | 0 | code = BFD_RELOC_IP2K_PAGE3; |
281 | |
|
282 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, code, |
283 | 0 | & result_type, & value); |
284 | 0 | if (errmsg == NULL) |
285 | 0 | { |
286 | 0 | if (result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER) |
287 | 0 | { |
288 | 0 | if ((value & 0x1) == 0) /* If the address is even .... */ |
289 | 0 | { |
290 | 0 | if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16CJP) |
291 | 0 | *valuep = (value >> 1) & 0x1FFF; /* Should mask be 1FFF? */ |
292 | 0 | else if (opindex == (CGEN_OPERAND_TYPE) IP2K_OPERAND_ADDR16P) |
293 | 0 | *valuep = (value >> 14) & 0x7; |
294 | 0 | } |
295 | 0 | else |
296 | 0 | errmsg = _("Byte address required. - must be even."); |
297 | 0 | } |
298 | 0 | else if (result_type == CGEN_PARSE_OPERAND_RESULT_QUEUED) |
299 | 0 | { |
300 | | /* This will happen for things like (s2-s1) where s2 and s1 |
301 | | are labels. */ |
302 | 0 | *valuep = value; |
303 | 0 | } |
304 | 0 | else |
305 | 0 | errmsg = _("cgen_parse_address returned a symbol. Literal required."); |
306 | 0 | } |
307 | 0 | return errmsg; |
308 | 0 | } |
309 | | |
310 | | static const char * |
311 | | parse_lit8 (CGEN_CPU_DESC cd, |
312 | | const char **strp, |
313 | | int opindex, |
314 | | long *valuep) |
315 | 0 | { |
316 | 0 | const char *errmsg; |
317 | 0 | enum cgen_parse_operand_result result_type; |
318 | 0 | bfd_reloc_code_real_type code = BFD_RELOC_NONE; |
319 | 0 | bfd_vma value; |
320 | | |
321 | | /* Parse %OP relocating operators. */ |
322 | 0 | if (strncmp (*strp, "%bank", 5) == 0) |
323 | 0 | { |
324 | 0 | *strp += 5; |
325 | 0 | code = BFD_RELOC_IP2K_BANK; |
326 | 0 | } |
327 | 0 | else if (strncmp (*strp, "%lo8data", 8) == 0) |
328 | 0 | { |
329 | 0 | *strp += 8; |
330 | 0 | code = BFD_RELOC_IP2K_LO8DATA; |
331 | 0 | } |
332 | 0 | else if (strncmp (*strp, "%hi8data", 8) == 0) |
333 | 0 | { |
334 | 0 | *strp += 8; |
335 | 0 | code = BFD_RELOC_IP2K_HI8DATA; |
336 | 0 | } |
337 | 0 | else if (strncmp (*strp, "%ex8data", 8) == 0) |
338 | 0 | { |
339 | 0 | *strp += 8; |
340 | 0 | code = BFD_RELOC_IP2K_EX8DATA; |
341 | 0 | } |
342 | 0 | else if (strncmp (*strp, "%lo8insn", 8) == 0) |
343 | 0 | { |
344 | 0 | *strp += 8; |
345 | 0 | code = BFD_RELOC_IP2K_LO8INSN; |
346 | 0 | } |
347 | 0 | else if (strncmp (*strp, "%hi8insn", 8) == 0) |
348 | 0 | { |
349 | 0 | *strp += 8; |
350 | 0 | code = BFD_RELOC_IP2K_HI8INSN; |
351 | 0 | } |
352 | | |
353 | | /* Parse %op operand. */ |
354 | 0 | if (code != BFD_RELOC_NONE) |
355 | 0 | { |
356 | 0 | errmsg = cgen_parse_address (cd, strp, opindex, code, |
357 | 0 | & result_type, & value); |
358 | 0 | if ((errmsg == NULL) && |
359 | 0 | (result_type != CGEN_PARSE_OPERAND_RESULT_QUEUED)) |
360 | 0 | errmsg = _("percent-operator operand is not a symbol"); |
361 | |
|
362 | 0 | *valuep = value; |
363 | 0 | } |
364 | | /* Parse as a number. */ |
365 | 0 | else |
366 | 0 | { |
367 | 0 | errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep); |
368 | | |
369 | | /* Truncate to eight bits to accept both signed and unsigned input. */ |
370 | 0 | if (errmsg == NULL) |
371 | 0 | *valuep &= 0xFF; |
372 | 0 | } |
373 | |
|
374 | 0 | return errmsg; |
375 | 0 | } |
376 | | |
377 | | static const char * |
378 | | parse_bit3 (CGEN_CPU_DESC cd, |
379 | | const char **strp, |
380 | | int opindex, |
381 | | unsigned long *valuep) |
382 | 0 | { |
383 | 0 | const char *errmsg; |
384 | 0 | char mode = 0; |
385 | 0 | long count = 0; |
386 | 0 | unsigned long value; |
387 | |
|
388 | 0 | if (strncmp (*strp, "%bit", 4) == 0) |
389 | 0 | { |
390 | 0 | *strp += 4; |
391 | 0 | mode = 1; |
392 | 0 | } |
393 | 0 | else if (strncmp (*strp, "%msbbit", 7) == 0) |
394 | 0 | { |
395 | 0 | *strp += 7; |
396 | 0 | mode = 1; |
397 | 0 | } |
398 | 0 | else if (strncmp (*strp, "%lsbbit", 7) == 0) |
399 | 0 | { |
400 | 0 | *strp += 7; |
401 | 0 | mode = 2; |
402 | 0 | } |
403 | |
|
404 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep); |
405 | 0 | if (errmsg) |
406 | 0 | return errmsg; |
407 | | |
408 | 0 | if (mode) |
409 | 0 | { |
410 | 0 | value = * valuep; |
411 | 0 | if (value == 0) |
412 | 0 | { |
413 | 0 | errmsg = _("Attempt to find bit index of 0"); |
414 | 0 | return errmsg; |
415 | 0 | } |
416 | | |
417 | 0 | if (mode == 1) |
418 | 0 | { |
419 | 0 | count = 31; |
420 | 0 | while ((value & 0x80000000) == 0) |
421 | 0 | { |
422 | 0 | count--; |
423 | 0 | value <<= 1; |
424 | 0 | } |
425 | 0 | } |
426 | 0 | else if (mode == 2) |
427 | 0 | { |
428 | 0 | count = 0; |
429 | 0 | while ((value & 0x00000001) == 0) |
430 | 0 | { |
431 | 0 | count++; |
432 | 0 | value >>= 1; |
433 | 0 | } |
434 | 0 | } |
435 | |
|
436 | 0 | *valuep = count; |
437 | 0 | } |
438 | | |
439 | 0 | return errmsg; |
440 | 0 | } |
441 | | |
442 | | /* -- dis.c */ |
443 | | |
444 | | const char * ip2k_cgen_parse_operand |
445 | | (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *); |
446 | | |
447 | | /* Main entry point for operand parsing. |
448 | | |
449 | | This function is basically just a big switch statement. Earlier versions |
450 | | used tables to look up the function to use, but |
451 | | - if the table contains both assembler and disassembler functions then |
452 | | the disassembler contains much of the assembler and vice-versa, |
453 | | - there's a lot of inlining possibilities as things grow, |
454 | | - using a switch statement avoids the function call overhead. |
455 | | |
456 | | This function could be moved into `parse_insn_normal', but keeping it |
457 | | separate makes clear the interface between `parse_insn_normal' and each of |
458 | | the handlers. */ |
459 | | |
460 | | const char * |
461 | | ip2k_cgen_parse_operand (CGEN_CPU_DESC cd, |
462 | | int opindex, |
463 | | const char ** strp, |
464 | | CGEN_FIELDS * fields) |
465 | 0 | { |
466 | 0 | const char * errmsg = NULL; |
467 | | /* Used by scalar operands that still need to be parsed. */ |
468 | 0 | long junk ATTRIBUTE_UNUSED; |
469 | |
|
470 | 0 | switch (opindex) |
471 | 0 | { |
472 | 0 | case IP2K_OPERAND_ADDR16CJP : |
473 | 0 | errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16CJP, (unsigned long *) (& fields->f_addr16cjp)); |
474 | 0 | break; |
475 | 0 | case IP2K_OPERAND_ADDR16H : |
476 | 0 | errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16H, (unsigned long *) (& fields->f_imm8)); |
477 | 0 | break; |
478 | 0 | case IP2K_OPERAND_ADDR16L : |
479 | 0 | errmsg = parse_addr16 (cd, strp, IP2K_OPERAND_ADDR16L, (unsigned long *) (& fields->f_imm8)); |
480 | 0 | break; |
481 | 0 | case IP2K_OPERAND_ADDR16P : |
482 | 0 | errmsg = parse_addr16_cjp (cd, strp, IP2K_OPERAND_ADDR16P, (unsigned long *) (& fields->f_page3)); |
483 | 0 | break; |
484 | 0 | case IP2K_OPERAND_BITNO : |
485 | 0 | errmsg = parse_bit3 (cd, strp, IP2K_OPERAND_BITNO, (unsigned long *) (& fields->f_bitno)); |
486 | 0 | break; |
487 | 0 | case IP2K_OPERAND_CBIT : |
488 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_CBIT, (unsigned long *) (& junk)); |
489 | 0 | break; |
490 | 0 | case IP2K_OPERAND_DCBIT : |
491 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_DCBIT, (unsigned long *) (& junk)); |
492 | 0 | break; |
493 | 0 | case IP2K_OPERAND_FR : |
494 | 0 | errmsg = parse_fr (cd, strp, IP2K_OPERAND_FR, (unsigned long *) (& fields->f_reg)); |
495 | 0 | break; |
496 | 0 | case IP2K_OPERAND_LIT8 : |
497 | 0 | errmsg = parse_lit8 (cd, strp, IP2K_OPERAND_LIT8, (long *) (& fields->f_imm8)); |
498 | 0 | break; |
499 | 0 | case IP2K_OPERAND_PABITS : |
500 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_PABITS, (unsigned long *) (& junk)); |
501 | 0 | break; |
502 | 0 | case IP2K_OPERAND_RETI3 : |
503 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_RETI3, (unsigned long *) (& fields->f_reti3)); |
504 | 0 | break; |
505 | 0 | case IP2K_OPERAND_ZBIT : |
506 | 0 | errmsg = cgen_parse_unsigned_integer (cd, strp, IP2K_OPERAND_ZBIT, (unsigned long *) (& junk)); |
507 | 0 | break; |
508 | | |
509 | 0 | default : |
510 | | /* xgettext:c-format */ |
511 | 0 | opcodes_error_handler |
512 | 0 | (_("internal error: unrecognized field %d while parsing"), |
513 | 0 | opindex); |
514 | 0 | abort (); |
515 | 0 | } |
516 | | |
517 | 0 | return errmsg; |
518 | 0 | } |
519 | | |
520 | | cgen_parse_fn * const ip2k_cgen_parse_handlers[] = |
521 | | { |
522 | | parse_insn_normal, |
523 | | }; |
524 | | |
525 | | void |
526 | | ip2k_cgen_init_asm (CGEN_CPU_DESC cd) |
527 | 0 | { |
528 | 0 | ip2k_cgen_init_opcode_table (cd); |
529 | 0 | ip2k_cgen_init_ibld_table (cd); |
530 | 0 | cd->parse_handlers = & ip2k_cgen_parse_handlers[0]; |
531 | 0 | cd->parse_operand = ip2k_cgen_parse_operand; |
532 | | #ifdef CGEN_ASM_INIT_HOOK |
533 | | CGEN_ASM_INIT_HOOK |
534 | | #endif |
535 | 0 | } |
536 | | |
537 | | |
538 | | |
539 | | /* Regex construction routine. |
540 | | |
541 | | This translates an opcode syntax string into a regex string, |
542 | | by replacing any non-character syntax element (such as an |
543 | | opcode) with the pattern '.*' |
544 | | |
545 | | It then compiles the regex and stores it in the opcode, for |
546 | | later use by ip2k_cgen_assemble_insn |
547 | | |
548 | | Returns NULL for success, an error message for failure. */ |
549 | | |
550 | | char * |
551 | | ip2k_cgen_build_insn_regex (CGEN_INSN *insn) |
552 | 372 | { |
553 | 372 | CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn); |
554 | 372 | const char *mnem = CGEN_INSN_MNEMONIC (insn); |
555 | 372 | char rxbuf[CGEN_MAX_RX_ELEMENTS]; |
556 | 372 | char *rx = rxbuf; |
557 | 372 | const CGEN_SYNTAX_CHAR_TYPE *syn; |
558 | 372 | int reg_err; |
559 | | |
560 | 372 | syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc)); |
561 | | |
562 | | /* Mnemonics come first in the syntax string. */ |
563 | 372 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) |
564 | 4 | return _("missing mnemonic in syntax string"); |
565 | 368 | ++syn; |
566 | | |
567 | | /* Generate a case sensitive regular expression that emulates case |
568 | | insensitive matching in the "C" locale. We cannot generate a case |
569 | | insensitive regular expression because in Turkish locales, 'i' and 'I' |
570 | | are not equal modulo case conversion. */ |
571 | | |
572 | | /* Copy the literal mnemonic out of the insn. */ |
573 | 1.76k | for (; *mnem; mnem++) |
574 | 1.40k | { |
575 | 1.40k | char c = *mnem; |
576 | | |
577 | 1.40k | if (ISALPHA (c)) |
578 | 1.40k | { |
579 | 1.40k | *rx++ = '['; |
580 | 1.40k | *rx++ = TOLOWER (c); |
581 | 1.40k | *rx++ = TOUPPER (c); |
582 | 1.40k | *rx++ = ']'; |
583 | 1.40k | } |
584 | 0 | else |
585 | 0 | *rx++ = c; |
586 | 1.40k | } |
587 | | |
588 | | /* Copy any remaining literals from the syntax string into the rx. */ |
589 | 1.37k | for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn) |
590 | 1.00k | { |
591 | 1.00k | if (CGEN_SYNTAX_CHAR_P (* syn)) |
592 | 704 | { |
593 | 704 | char c = CGEN_SYNTAX_CHAR (* syn); |
594 | | |
595 | 704 | switch (c) |
596 | 704 | { |
597 | | /* Escape any regex metacharacters in the syntax. */ |
598 | 0 | case '.': case '[': case '\\': |
599 | 0 | case '*': case '^': case '$': |
600 | |
|
601 | | #ifdef CGEN_ESCAPE_EXTENDED_REGEX |
602 | | case '?': case '{': case '}': |
603 | | case '(': case ')': case '*': |
604 | | case '|': case '+': case ']': |
605 | | #endif |
606 | 0 | *rx++ = '\\'; |
607 | 0 | *rx++ = c; |
608 | 0 | break; |
609 | | |
610 | 704 | default: |
611 | 704 | if (ISALPHA (c)) |
612 | 168 | { |
613 | 168 | *rx++ = '['; |
614 | 168 | *rx++ = TOLOWER (c); |
615 | 168 | *rx++ = TOUPPER (c); |
616 | 168 | *rx++ = ']'; |
617 | 168 | } |
618 | 536 | else |
619 | 536 | *rx++ = c; |
620 | 704 | break; |
621 | 704 | } |
622 | 704 | } |
623 | 300 | else |
624 | 300 | { |
625 | | /* Replace non-syntax fields with globs. */ |
626 | 300 | *rx++ = '.'; |
627 | 300 | *rx++ = '*'; |
628 | 300 | } |
629 | 1.00k | } |
630 | | |
631 | | /* Trailing whitespace ok. */ |
632 | 368 | * rx++ = '['; |
633 | 368 | * rx++ = ' '; |
634 | 368 | * rx++ = '\t'; |
635 | 368 | * rx++ = ']'; |
636 | 368 | * rx++ = '*'; |
637 | | |
638 | | /* But anchor it after that. */ |
639 | 368 | * rx++ = '$'; |
640 | 368 | * rx = '\0'; |
641 | | |
642 | 368 | CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t)); |
643 | 368 | reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB); |
644 | | |
645 | 368 | if (reg_err == 0) |
646 | 368 | return NULL; |
647 | 0 | else |
648 | 0 | { |
649 | 0 | static char msg[80]; |
650 | |
|
651 | 0 | regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80); |
652 | 0 | regfree ((regex_t *) CGEN_INSN_RX (insn)); |
653 | 0 | free (CGEN_INSN_RX (insn)); |
654 | 0 | (CGEN_INSN_RX (insn)) = NULL; |
655 | 0 | return msg; |
656 | 0 | } |
657 | 368 | } |
658 | | |
659 | | |
660 | | /* Default insn parser. |
661 | | |
662 | | The syntax string is scanned and operands are parsed and stored in FIELDS. |
663 | | Relocs are queued as we go via other callbacks. |
664 | | |
665 | | ??? Note that this is currently an all-or-nothing parser. If we fail to |
666 | | parse the instruction, we return 0 and the caller will start over from |
667 | | the beginning. Backtracking will be necessary in parsing subexpressions, |
668 | | but that can be handled there. Not handling backtracking here may get |
669 | | expensive in the case of the m68k. Deal with later. |
670 | | |
671 | | Returns NULL for success, an error message for failure. */ |
672 | | |
673 | | static const char * |
674 | | parse_insn_normal (CGEN_CPU_DESC cd, |
675 | | const CGEN_INSN *insn, |
676 | | const char **strp, |
677 | | CGEN_FIELDS *fields) |
678 | 0 | { |
679 | | /* ??? Runtime added insns not handled yet. */ |
680 | 0 | const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); |
681 | 0 | const char *str = *strp; |
682 | 0 | const char *errmsg; |
683 | 0 | const char *p; |
684 | 0 | const CGEN_SYNTAX_CHAR_TYPE * syn; |
685 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
686 | | /* FIXME: wip */ |
687 | 0 | int past_opcode_p; |
688 | 0 | #endif |
689 | | |
690 | | /* For now we assume the mnemonic is first (there are no leading operands). |
691 | | We can parse it without needing to set up operand parsing. |
692 | | GAS's input scrubber will ensure mnemonics are lowercase, but we may |
693 | | not be called from GAS. */ |
694 | 0 | p = CGEN_INSN_MNEMONIC (insn); |
695 | 0 | while (*p && TOLOWER (*p) == TOLOWER (*str)) |
696 | 0 | ++p, ++str; |
697 | |
|
698 | 0 | if (* p) |
699 | 0 | return _("unrecognized instruction"); |
700 | | |
701 | | #ifndef CGEN_MNEMONIC_OPERANDS |
702 | | if (* str && ! ISSPACE (* str)) |
703 | | return _("unrecognized instruction"); |
704 | | #endif |
705 | | |
706 | 0 | CGEN_INIT_PARSE (cd); |
707 | 0 | cgen_init_parse_operand (cd); |
708 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
709 | 0 | past_opcode_p = 0; |
710 | 0 | #endif |
711 | | |
712 | | /* We don't check for (*str != '\0') here because we want to parse |
713 | | any trailing fake arguments in the syntax string. */ |
714 | 0 | syn = CGEN_SYNTAX_STRING (syntax); |
715 | | |
716 | | /* Mnemonics come first for now, ensure valid string. */ |
717 | 0 | if (! CGEN_SYNTAX_MNEMONIC_P (* syn)) |
718 | 0 | abort (); |
719 | | |
720 | 0 | ++syn; |
721 | |
|
722 | 0 | while (* syn != 0) |
723 | 0 | { |
724 | | /* Non operand chars must match exactly. */ |
725 | 0 | if (CGEN_SYNTAX_CHAR_P (* syn)) |
726 | 0 | { |
727 | | /* FIXME: While we allow for non-GAS callers above, we assume the |
728 | | first char after the mnemonic part is a space. */ |
729 | | /* FIXME: We also take inappropriate advantage of the fact that |
730 | | GAS's input scrubber will remove extraneous blanks. */ |
731 | 0 | if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn))) |
732 | 0 | { |
733 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
734 | 0 | if (CGEN_SYNTAX_CHAR(* syn) == ' ') |
735 | 0 | past_opcode_p = 1; |
736 | 0 | #endif |
737 | 0 | ++ syn; |
738 | 0 | ++ str; |
739 | 0 | } |
740 | 0 | else if (*str) |
741 | 0 | { |
742 | | /* Syntax char didn't match. Can't be this insn. */ |
743 | 0 | static char msg [80]; |
744 | | |
745 | | /* xgettext:c-format */ |
746 | 0 | sprintf (msg, _("syntax error (expected char `%c', found `%c')"), |
747 | 0 | CGEN_SYNTAX_CHAR(*syn), *str); |
748 | 0 | return msg; |
749 | 0 | } |
750 | 0 | else |
751 | 0 | { |
752 | | /* Ran out of input. */ |
753 | 0 | static char msg [80]; |
754 | | |
755 | | /* xgettext:c-format */ |
756 | 0 | sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"), |
757 | 0 | CGEN_SYNTAX_CHAR(*syn)); |
758 | 0 | return msg; |
759 | 0 | } |
760 | 0 | continue; |
761 | 0 | } |
762 | | |
763 | 0 | #ifdef CGEN_MNEMONIC_OPERANDS |
764 | 0 | (void) past_opcode_p; |
765 | 0 | #endif |
766 | | /* We have an operand of some sort. */ |
767 | 0 | errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields); |
768 | 0 | if (errmsg) |
769 | 0 | return errmsg; |
770 | | |
771 | | /* Done with this operand, continue with next one. */ |
772 | 0 | ++ syn; |
773 | 0 | } |
774 | | |
775 | | /* If we're at the end of the syntax string, we're done. */ |
776 | 0 | if (* syn == 0) |
777 | 0 | { |
778 | | /* FIXME: For the moment we assume a valid `str' can only contain |
779 | | blanks now. IE: We needn't try again with a longer version of |
780 | | the insn and it is assumed that longer versions of insns appear |
781 | | before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */ |
782 | 0 | while (ISSPACE (* str)) |
783 | 0 | ++ str; |
784 | |
|
785 | 0 | if (* str != '\0') |
786 | 0 | return _("junk at end of line"); /* FIXME: would like to include `str' */ |
787 | | |
788 | 0 | return NULL; |
789 | 0 | } |
790 | | |
791 | | /* We couldn't parse it. */ |
792 | 0 | return _("unrecognized instruction"); |
793 | 0 | } |
794 | | |
795 | | /* Main entry point. |
796 | | This routine is called for each instruction to be assembled. |
797 | | STR points to the insn to be assembled. |
798 | | We assume all necessary tables have been initialized. |
799 | | The assembled instruction, less any fixups, is stored in BUF. |
800 | | Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value |
801 | | still needs to be converted to target byte order, otherwise BUF is an array |
802 | | of bytes in target byte order. |
803 | | The result is a pointer to the insn's entry in the opcode table, |
804 | | or NULL if an error occured (an error message will have already been |
805 | | printed). |
806 | | |
807 | | Note that when processing (non-alias) macro-insns, |
808 | | this function recurses. |
809 | | |
810 | | ??? It's possible to make this cpu-independent. |
811 | | One would have to deal with a few minor things. |
812 | | At this point in time doing so would be more of a curiosity than useful |
813 | | [for example this file isn't _that_ big], but keeping the possibility in |
814 | | mind helps keep the design clean. */ |
815 | | |
816 | | const CGEN_INSN * |
817 | | ip2k_cgen_assemble_insn (CGEN_CPU_DESC cd, |
818 | | const char *str, |
819 | | CGEN_FIELDS *fields, |
820 | | CGEN_INSN_BYTES_PTR buf, |
821 | | char **errmsg) |
822 | 0 | { |
823 | 0 | const char *start; |
824 | 0 | CGEN_INSN_LIST *ilist; |
825 | 0 | const char *parse_errmsg = NULL; |
826 | 0 | const char *insert_errmsg = NULL; |
827 | 0 | int recognized_mnemonic = 0; |
828 | | |
829 | | /* Skip leading white space. */ |
830 | 0 | while (ISSPACE (* str)) |
831 | 0 | ++ str; |
832 | | |
833 | | /* The instructions are stored in hashed lists. |
834 | | Get the first in the list. */ |
835 | 0 | ilist = CGEN_ASM_LOOKUP_INSN (cd, str); |
836 | | |
837 | | /* Keep looking until we find a match. */ |
838 | 0 | start = str; |
839 | 0 | for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist)) |
840 | 0 | { |
841 | 0 | const CGEN_INSN *insn = ilist->insn; |
842 | 0 | recognized_mnemonic = 1; |
843 | |
|
844 | 0 | #ifdef CGEN_VALIDATE_INSN_SUPPORTED |
845 | | /* Not usually needed as unsupported opcodes |
846 | | shouldn't be in the hash lists. */ |
847 | | /* Is this insn supported by the selected cpu? */ |
848 | 0 | if (! ip2k_cgen_insn_supported (cd, insn)) |
849 | 0 | continue; |
850 | 0 | #endif |
851 | | /* If the RELAXED attribute is set, this is an insn that shouldn't be |
852 | | chosen immediately. Instead, it is used during assembler/linker |
853 | | relaxation if possible. */ |
854 | 0 | if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0) |
855 | 0 | continue; |
856 | | |
857 | 0 | str = start; |
858 | | |
859 | | /* Skip this insn if str doesn't look right lexically. */ |
860 | 0 | if (CGEN_INSN_RX (insn) != NULL && |
861 | 0 | regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH) |
862 | 0 | continue; |
863 | | |
864 | | /* Allow parse/insert handlers to obtain length of insn. */ |
865 | 0 | CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn); |
866 | |
|
867 | 0 | parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields); |
868 | 0 | if (parse_errmsg != NULL) |
869 | 0 | continue; |
870 | | |
871 | | /* ??? 0 is passed for `pc'. */ |
872 | 0 | insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf, |
873 | 0 | (bfd_vma) 0); |
874 | 0 | if (insert_errmsg != NULL) |
875 | 0 | continue; |
876 | | |
877 | | /* It is up to the caller to actually output the insn and any |
878 | | queued relocs. */ |
879 | 0 | return insn; |
880 | 0 | } |
881 | | |
882 | 0 | { |
883 | 0 | static char errbuf[150]; |
884 | 0 | const char *tmp_errmsg; |
885 | 0 | #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS |
886 | 0 | #define be_verbose 1 |
887 | | #else |
888 | | #define be_verbose 0 |
889 | | #endif |
890 | |
|
891 | 0 | if (be_verbose) |
892 | 0 | { |
893 | | /* If requesting verbose error messages, use insert_errmsg. |
894 | | Failing that, use parse_errmsg. */ |
895 | 0 | tmp_errmsg = (insert_errmsg ? insert_errmsg : |
896 | 0 | parse_errmsg ? parse_errmsg : |
897 | 0 | recognized_mnemonic ? |
898 | 0 | _("unrecognized form of instruction") : |
899 | 0 | _("unrecognized instruction")); |
900 | |
|
901 | 0 | if (strlen (start) > 50) |
902 | | /* xgettext:c-format */ |
903 | 0 | sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start); |
904 | 0 | else |
905 | | /* xgettext:c-format */ |
906 | 0 | sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start); |
907 | 0 | } |
908 | 0 | else |
909 | 0 | { |
910 | 0 | if (strlen (start) > 50) |
911 | | /* xgettext:c-format */ |
912 | 0 | sprintf (errbuf, _("bad instruction `%.50s...'"), start); |
913 | 0 | else |
914 | | /* xgettext:c-format */ |
915 | 0 | sprintf (errbuf, _("bad instruction `%.50s'"), start); |
916 | 0 | } |
917 | |
|
918 | 0 | *errmsg = errbuf; |
919 | 0 | return NULL; |
920 | 0 | } |
921 | 0 | } |