* cgen-asm.in (insert_1): Replace calls to bfd_getb8/putb8.
[deliverable/binutils-gdb.git] / opcodes / fr30-asm.c
1 /* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
3
4 THIS FILE IS USED TO GENERATE fr30-asm.c.
5
6 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
7
8 This file is part of the GNU Binutils and GDB, the GNU debugger.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software Foundation, Inc.,
22 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23
24 #include "sysdep.h"
25 #include <ctype.h>
26 #include <stdio.h>
27 #include "ansidecl.h"
28 #include "bfd.h"
29 #include "symcat.h"
30 #include "fr30-opc.h"
31 #include "opintl.h"
32
33 #undef min
34 #define min(a,b) ((a) < (b) ? (a) : (b))
35 #undef max
36 #define max(a,b) ((a) > (b) ? (a) : (b))
37
38 #undef INLINE
39 #ifdef __GNUC__
40 #define INLINE __inline__
41 #else
42 #define INLINE
43 #endif
44
45 static const char * insert_normal
46 PARAMS ((CGEN_OPCODE_DESC, long, unsigned int, int, int, int,
47 CGEN_INSN_BYTES_PTR));
48 static const char * parse_insn_normal
49 PARAMS ((CGEN_OPCODE_DESC, const CGEN_INSN *,
50 const char **, CGEN_FIELDS *));
51 static const char * insert_insn_normal
52 PARAMS ((CGEN_OPCODE_DESC, const CGEN_INSN *,
53 CGEN_FIELDS *, CGEN_INSN_BYTES_PTR, bfd_vma));
54 \f
55 /* -- assembler routines inserted here */
56
57 /* Main entry point for operand parsing.
58
59 This function is basically just a big switch statement. Earlier versions
60 used tables to look up the function to use, but
61 - if the table contains both assembler and disassembler functions then
62 the disassembler contains much of the assembler and vice-versa,
63 - there's a lot of inlining possibilities as things grow,
64 - using a switch statement avoids the function call overhead.
65
66 This function could be moved into `parse_insn_normal', but keeping it
67 separate makes clear the interface between `parse_insn_normal' and each of
68 the handlers.
69 */
70
71 const char *
72 fr30_cgen_parse_operand (od, opindex, strp, fields)
73 CGEN_OPCODE_DESC od;
74 int opindex;
75 const char ** strp;
76 CGEN_FIELDS * fields;
77 {
78 const char * errmsg;
79
80 switch (opindex)
81 {
82 case FR30_OPERAND_RI :
83 errmsg = cgen_parse_keyword (od, strp, & fr30_cgen_opval_h_gr, & fields->f_Ri);
84 break;
85 case FR30_OPERAND_RJ :
86 errmsg = cgen_parse_keyword (od, strp, & fr30_cgen_opval_h_gr, & fields->f_Rj);
87 break;
88 case FR30_OPERAND_RS1 :
89 errmsg = cgen_parse_keyword (od, strp, & fr30_cgen_opval_h_dr, & fields->f_Rs1);
90 break;
91 case FR30_OPERAND_RS2 :
92 errmsg = cgen_parse_keyword (od, strp, & fr30_cgen_opval_h_dr, & fields->f_Rs2);
93 break;
94 case FR30_OPERAND_R13 :
95 errmsg = cgen_parse_keyword (od, strp, & fr30_cgen_opval_h_r13, & fields->f_nil);
96 break;
97 case FR30_OPERAND_R14 :
98 errmsg = cgen_parse_keyword (od, strp, & fr30_cgen_opval_h_r14, & fields->f_nil);
99 break;
100 case FR30_OPERAND_R15 :
101 errmsg = cgen_parse_keyword (od, strp, & fr30_cgen_opval_h_r15, & fields->f_nil);
102 break;
103 case FR30_OPERAND_PS :
104 errmsg = cgen_parse_keyword (od, strp, & fr30_cgen_opval_h_ps, & fields->f_nil);
105 break;
106 case FR30_OPERAND_U4 :
107 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_U4, &fields->f_u4);
108 break;
109 case FR30_OPERAND_M4 :
110 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_M4, &fields->f_m4);
111 break;
112 case FR30_OPERAND_U8 :
113 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_U8, &fields->f_u8);
114 break;
115 case FR30_OPERAND_I8 :
116 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_I8, &fields->f_i8);
117 break;
118 case FR30_OPERAND_UDISP6 :
119 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_UDISP6, &fields->f_udisp6);
120 break;
121 case FR30_OPERAND_DISP8 :
122 errmsg = cgen_parse_signed_integer (od, strp, FR30_OPERAND_DISP8, &fields->f_disp8);
123 break;
124 case FR30_OPERAND_DISP9 :
125 errmsg = cgen_parse_signed_integer (od, strp, FR30_OPERAND_DISP9, &fields->f_disp9);
126 break;
127 case FR30_OPERAND_DISP10 :
128 errmsg = cgen_parse_signed_integer (od, strp, FR30_OPERAND_DISP10, &fields->f_disp10);
129 break;
130 case FR30_OPERAND_S10 :
131 errmsg = cgen_parse_signed_integer (od, strp, FR30_OPERAND_S10, &fields->f_s10);
132 break;
133 case FR30_OPERAND_U10 :
134 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_U10, &fields->f_u10);
135 break;
136 case FR30_OPERAND_I32 :
137 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_I32, &fields->f_i32);
138 break;
139 case FR30_OPERAND_DIR8 :
140 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_DIR8, &fields->f_dir8);
141 break;
142 case FR30_OPERAND_DIR9 :
143 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_DIR9, &fields->f_dir9);
144 break;
145 case FR30_OPERAND_DIR10 :
146 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_DIR10, &fields->f_dir10);
147 break;
148 case FR30_OPERAND_LABEL9 :
149 errmsg = cgen_parse_signed_integer (od, strp, FR30_OPERAND_LABEL9, &fields->f_rel9);
150 break;
151 case FR30_OPERAND_LABEL12 :
152 errmsg = cgen_parse_signed_integer (od, strp, FR30_OPERAND_LABEL12, &fields->f_rel12);
153 break;
154 case FR30_OPERAND_CC :
155 errmsg = cgen_parse_unsigned_integer (od, strp, FR30_OPERAND_CC, &fields->f_cc);
156 break;
157
158 default :
159 /* xgettext:c-format */
160 fprintf (stderr, _("Unrecognized field %d while parsing.\n"), opindex);
161 abort ();
162 }
163
164 return errmsg;
165 }
166
167 /* Main entry point for operand insertion.
168
169 This function is basically just a big switch statement. Earlier versions
170 used tables to look up the function to use, but
171 - if the table contains both assembler and disassembler functions then
172 the disassembler contains much of the assembler and vice-versa,
173 - there's a lot of inlining possibilities as things grow,
174 - using a switch statement avoids the function call overhead.
175
176 This function could be moved into `parse_insn_normal', but keeping it
177 separate makes clear the interface between `parse_insn_normal' and each of
178 the handlers. It's also needed by GAS to insert operands that couldn't be
179 resolved during parsing.
180 */
181
182 const char *
183 fr30_cgen_insert_operand (od, opindex, fields, buffer, pc)
184 CGEN_OPCODE_DESC od;
185 int opindex;
186 CGEN_FIELDS * fields;
187 CGEN_INSN_BYTES_PTR buffer;
188 bfd_vma pc;
189 {
190 const char * errmsg;
191
192 switch (opindex)
193 {
194 case FR30_OPERAND_RI :
195 errmsg = insert_normal (od, fields->f_Ri, 0|(1<<CGEN_OPERAND_UNSIGNED), 12, 4, CGEN_FIELDS_BITSIZE (fields), buffer);
196 break;
197 case FR30_OPERAND_RJ :
198 errmsg = insert_normal (od, fields->f_Rj, 0|(1<<CGEN_OPERAND_UNSIGNED), 8, 4, CGEN_FIELDS_BITSIZE (fields), buffer);
199 break;
200 case FR30_OPERAND_RS1 :
201 errmsg = insert_normal (od, fields->f_Rs1, 0|(1<<CGEN_OPERAND_UNSIGNED), 8, 4, CGEN_FIELDS_BITSIZE (fields), buffer);
202 break;
203 case FR30_OPERAND_RS2 :
204 errmsg = insert_normal (od, fields->f_Rs2, 0|(1<<CGEN_OPERAND_UNSIGNED), 12, 4, CGEN_FIELDS_BITSIZE (fields), buffer);
205 break;
206 case FR30_OPERAND_R13 :
207 errmsg = insert_normal (od, fields->f_nil, 0, 0, 0, CGEN_FIELDS_BITSIZE (fields), buffer);
208 break;
209 case FR30_OPERAND_R14 :
210 errmsg = insert_normal (od, fields->f_nil, 0, 0, 0, CGEN_FIELDS_BITSIZE (fields), buffer);
211 break;
212 case FR30_OPERAND_R15 :
213 errmsg = insert_normal (od, fields->f_nil, 0, 0, 0, CGEN_FIELDS_BITSIZE (fields), buffer);
214 break;
215 case FR30_OPERAND_PS :
216 errmsg = insert_normal (od, fields->f_nil, 0, 0, 0, CGEN_FIELDS_BITSIZE (fields), buffer);
217 break;
218 case FR30_OPERAND_U4 :
219 errmsg = insert_normal (od, fields->f_u4, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_UNSIGNED), 8, 4, CGEN_FIELDS_BITSIZE (fields), buffer);
220 break;
221 case FR30_OPERAND_M4 :
222 {
223 long value = fields->f_m4;
224 value = ((value) & (15));
225 errmsg = insert_normal (od, value, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_UNSIGNED), 8, 4, CGEN_FIELDS_BITSIZE (fields), buffer);
226 }
227 break;
228 case FR30_OPERAND_U8 :
229 errmsg = insert_normal (od, fields->f_u8, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_UNSIGNED), 8, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
230 break;
231 case FR30_OPERAND_I8 :
232 errmsg = insert_normal (od, fields->f_i8, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_UNSIGNED), 4, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
233 break;
234 case FR30_OPERAND_UDISP6 :
235 {
236 long value = fields->f_udisp6;
237 value = ((unsigned int) (value) >> (2));
238 errmsg = insert_normal (od, value, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_UNSIGNED), 8, 4, CGEN_FIELDS_BITSIZE (fields), buffer);
239 }
240 break;
241 case FR30_OPERAND_DISP8 :
242 errmsg = insert_normal (od, fields->f_disp8, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_SIGNED), 4, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
243 break;
244 case FR30_OPERAND_DISP9 :
245 {
246 long value = fields->f_disp9;
247 value = ((int) (value) >> (1));
248 errmsg = insert_normal (od, value, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_SIGNED), 4, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
249 }
250 break;
251 case FR30_OPERAND_DISP10 :
252 {
253 long value = fields->f_disp10;
254 value = ((int) (value) >> (2));
255 errmsg = insert_normal (od, value, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_SIGNED), 4, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
256 }
257 break;
258 case FR30_OPERAND_S10 :
259 {
260 long value = fields->f_s10;
261 value = ((int) (value) >> (2));
262 errmsg = insert_normal (od, value, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_SIGNED), 8, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
263 }
264 break;
265 case FR30_OPERAND_U10 :
266 {
267 long value = fields->f_u10;
268 value = ((unsigned int) (value) >> (2));
269 errmsg = insert_normal (od, value, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_UNSIGNED), 8, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
270 }
271 break;
272 case FR30_OPERAND_I32 :
273 errmsg = insert_normal (od, fields->f_i32, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_SIGN_OPT)|(1<<CGEN_OPERAND_UNSIGNED), 16, 32, CGEN_FIELDS_BITSIZE (fields), buffer);
274 break;
275 case FR30_OPERAND_DIR8 :
276 errmsg = insert_normal (od, fields->f_dir8, 0|(1<<CGEN_OPERAND_UNSIGNED), 8, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
277 break;
278 case FR30_OPERAND_DIR9 :
279 {
280 long value = fields->f_dir9;
281 value = ((unsigned int) (value) >> (1));
282 errmsg = insert_normal (od, value, 0|(1<<CGEN_OPERAND_UNSIGNED), 8, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
283 }
284 break;
285 case FR30_OPERAND_DIR10 :
286 {
287 long value = fields->f_dir10;
288 value = ((unsigned int) (value) >> (2));
289 errmsg = insert_normal (od, value, 0|(1<<CGEN_OPERAND_UNSIGNED), 8, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
290 }
291 break;
292 case FR30_OPERAND_LABEL9 :
293 {
294 long value = fields->f_rel9;
295 value = ((int) (((value) - (((pc) & (-2))))) >> (1));
296 errmsg = insert_normal (od, value, 0|(1<<CGEN_OPERAND_SIGNED), 8, 8, CGEN_FIELDS_BITSIZE (fields), buffer);
297 }
298 break;
299 case FR30_OPERAND_LABEL12 :
300 {
301 long value = fields->f_rel12;
302 value = ((int) (((value) - (((pc) & (-2))))) >> (1));
303 errmsg = insert_normal (od, value, 0|(1<<CGEN_OPERAND_SIGNED), 5, 11, CGEN_FIELDS_BITSIZE (fields), buffer);
304 }
305 break;
306 case FR30_OPERAND_CC :
307 errmsg = insert_normal (od, fields->f_cc, 0|(1<<CGEN_OPERAND_UNSIGNED), 4, 4, CGEN_FIELDS_BITSIZE (fields), buffer);
308 break;
309
310 default :
311 /* xgettext:c-format */
312 fprintf (stderr, _("Unrecognized field %d while building insn.\n"),
313 opindex);
314 abort ();
315 }
316
317 return errmsg;
318 }
319
320 cgen_parse_fn * const fr30_cgen_parse_handlers[] =
321 {
322 0, /* default */
323 parse_insn_normal,
324 };
325
326 cgen_insert_fn * const fr30_cgen_insert_handlers[] =
327 {
328 0, /* default */
329 insert_insn_normal,
330 };
331
332 void
333 fr30_cgen_init_asm (od)
334 CGEN_OPCODE_DESC od;
335 {
336 }
337
338 \f
339 #if ! CGEN_INT_INSN_P
340
341 /* Subroutine of insert_normal. */
342
343 static INLINE void
344 insert_1 (od, value, start, length, word_length, bufp)
345 CGEN_OPCODE_DESC od;
346 unsigned long value;
347 int start,length,word_length;
348 unsigned char *bufp;
349 {
350 unsigned long x,mask;
351 int shift;
352 int big_p = CGEN_OPCODE_INSN_ENDIAN (od) == CGEN_ENDIAN_BIG;
353
354 switch (word_length)
355 {
356 case 8:
357 x = *bufp;
358 break;
359 case 16:
360 if (big_p)
361 x = bfd_getb16 (bufp);
362 else
363 x = bfd_getl16 (bufp);
364 break;
365 case 24:
366 /* ??? This may need reworking as these cases don't necessarily
367 want the first byte and the last two bytes handled like this. */
368 if (big_p)
369 x = (bufp[0] << 16) | bfd_getb16 (bufp + 1);
370 else
371 x = bfd_getl16 (bufp) | (bufp[2] << 16);
372 break;
373 case 32:
374 if (big_p)
375 x = bfd_getb32 (bufp);
376 else
377 x = bfd_getl32 (bufp);
378 break;
379 default :
380 abort ();
381 }
382
383 /* Written this way to avoid undefined behaviour. */
384 mask = (((1L << (length - 1)) - 1) << 1) | 1;
385 if (CGEN_INSN_LSB0_P)
386 shift = start;
387 else
388 shift = (word_length - (start + length));
389 x = (x & ~(mask << shift)) | ((value & mask) << shift);
390
391 switch (word_length)
392 {
393 case 8:
394 *bufp = x;
395 break;
396 case 16:
397 if (big_p)
398 bfd_putb16 (x, bufp);
399 else
400 bfd_putl16 (x, bufp);
401 break;
402 case 24:
403 /* ??? This may need reworking as these cases don't necessarily
404 want the first byte and the last two bytes handled like this. */
405 if (big_p)
406 {
407 bufp[0] = x >> 16;
408 bfd_putb16 (x, bufp + 1);
409 }
410 else
411 {
412 bfd_putl16 (x, bufp);
413 bufp[2] = x >> 16;
414 }
415 break;
416 case 32:
417 if (big_p)
418 bfd_putb32 (x, bufp);
419 else
420 bfd_putl32 (x, bufp);
421 break;
422 default :
423 abort ();
424 }
425 }
426
427 #endif /* ! CGEN_INT_INSN_P */
428
429 /* Default insertion routine.
430
431 ATTRS is a mask of the boolean attributes.
432 START is the starting bit number, architecture origin.
433 LENGTH is the length of VALUE in bits.
434 TOTAL_LENGTH is the total length of the insn.
435
436 The result is an error message or NULL if success. */
437
438 /* ??? This duplicates functionality with bfd's howto table and
439 bfd_install_relocation. */
440 /* ??? For architectures where insns can be representable as ints,
441 store insn in `field' struct and add registers, etc. while parsing? */
442 /* ??? This doesn't handle bfd_vma's. Create another function when
443 necessary. */
444
445 static const char *
446 insert_normal (od, value, attrs, start, length, total_length, buffer)
447 CGEN_OPCODE_DESC od;
448 long value;
449 unsigned int attrs;
450 int start;
451 int length;
452 int total_length;
453 CGEN_INSN_BYTES_PTR buffer;
454 {
455 static char errbuf[100];
456 /* Written this way to avoid undefined behaviour. */
457 unsigned long mask = (((1L << (length - 1)) - 1) << 1) | 1;
458
459 /* If LENGTH is zero, this operand doesn't contribute to the value. */
460 if (length == 0)
461 return NULL;
462
463 /* Ensure VALUE will fit. */
464 if ((attrs & CGEN_ATTR_MASK (CGEN_OPERAND_UNSIGNED)) != 0)
465 {
466 unsigned long maxval = mask;
467 if ((unsigned long) value > maxval)
468 {
469 /* xgettext:c-format */
470 sprintf (errbuf,
471 _("operand out of range (%lu not between 0 and %lu)"),
472 value, maxval);
473 return errbuf;
474 }
475 }
476 else
477 {
478 long minval = - (1L << (length - 1));
479 long maxval = (1L << (length - 1)) - 1;
480 if (value < minval || value > maxval)
481 {
482 sprintf
483 /* xgettext:c-format */
484 (errbuf, _("operand out of range (%ld not between %ld and %ld)"),
485 value, minval, maxval);
486 return errbuf;
487 }
488 }
489
490 #if CGEN_INT_INSN_P
491
492 if (total_length > 32) /* 32 bits in a portable host int */
493 abort ();
494 {
495 int shift;
496
497 if (CGEN_INSN_LSB0_P)
498 shift = start;
499 else
500 shift = total_length - (start + length);
501 *buffer = (*buffer & ~(mask << shift)) | ((value & mask) << shift);
502 }
503
504 #else
505
506 /* FIXME: unfinished and untested */
507
508 /* The hard case is probably too slow for the normal cases.
509 It's certainly more difficult to understand than the normal case.
510 Thus this is split into two. The hard case is defined
511 to be when a field straddles a (loosely defined) word boundary
512 (??? which may require target specific help to determine). */
513
514 #if 0 /*wip*/
515
516 #define HARD_CASE_P 0 /* FIXME:wip */
517
518 if (HARD_CASE_P)
519 {
520 unsigned char *bufp = (unsigned char *) buffer;
521 int insn_length_left = total_length;
522
523 if (CGEN_INSN_LSB0_P)
524 {
525 int word_offset = (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG
526 ? ...
527 : start / CGEN_BASE_INSN_BITSIZE);
528 bufp += word_offset * (CGEN_BASE_INSN_BITSIZE / 8);
529 if (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG)
530 else
531 start -= word_offset * CGEN_BASE_INSN_BITSIZE;
532 }
533 else
534 {
535 int word_offset = (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG
536 ? start / CGEN_BASE_INSN_BITSIZE
537 : ...);
538 bufp += word_offset * (CGEN_BASE_INSN_BITSIZE / 8);
539 if (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG)
540 start -= word_offset * CGEN_BASE_INSN_BITSIZE;
541 else
542 }
543
544 /* Loop so we handle a field straddling an insn word boundary
545 (remember, "insn word boundary" is loosely defined here). */
546
547 while (length > 0)
548 {
549 int this_pass_length = length;
550 int this_pass_start = start;
551 int this_pass_word_length = min (insn_length_left,
552 (CGEN_BASE_INSN_BITSIZE == 8
553 ? 32
554 : CGEN_BASE_INSN_BITSIZE));
555
556 insert_1 (od, value, attrs,
557 this_pass_start, this_pass_length, this_pass_word_length,
558 bufp);
559
560 length -= this_pass_length;
561 insn_length_left -= this_pass_word_length;
562 if (???)
563 {
564 value >>= ???;
565 start += ???;
566 }
567 else
568 {
569 value >>= ???;
570 start += ???;
571 }
572 bufp += this_pass_word_length / 8;
573 }
574 }
575 else
576 #endif /* 0 */
577 {
578 unsigned char *bufp = (unsigned char *) buffer;
579
580 if (length > 32)
581 abort ();
582
583 /* Adjust start,total_length,bufp to point to the pseudo-word that holds
584 the value. For example in a 48 bit insn where the value to insert
585 (say an immediate value) is the last 16 bits then fetch_length here
586 would be 16. To handle a 24 bit insn with an 18 bit immediate,
587 insert_1 handles 24 bits. */
588
589 if (total_length > 32)
590 {
591 int needed_width = start % 8 + length;
592 int fetch_length = (needed_width <= 8 ? 8
593 : needed_width <= 16 ? 16
594 : 32);
595
596 if (CGEN_INSN_LSB0_P)
597 {
598 if (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG)
599 {
600 abort (); /* wip */
601 }
602 else
603 {
604 int offset = start & ~7;
605
606 bufp += offset / 8;
607 start -= offset;
608 total_length = fetch_length;
609 }
610 }
611 else
612 {
613 if (CGEN_INSN_WORD_ENDIAN (od) == CGEN_ENDIAN_BIG)
614 {
615 int offset = start & ~7;
616
617 bufp += offset / 8;
618 start -= offset;
619 total_length = fetch_length;
620 }
621 else
622 {
623 abort (); /* wip */
624 }
625 }
626 }
627
628 insert_1 (od, value, start, length, total_length, bufp);
629 }
630
631 #endif /* ! CGEN_INT_INSN_P */
632
633 return NULL;
634 }
635 \f
636 /* Default insn parser.
637
638 The syntax string is scanned and operands are parsed and stored in FIELDS.
639 Relocs are queued as we go via other callbacks.
640
641 ??? Note that this is currently an all-or-nothing parser. If we fail to
642 parse the instruction, we return 0 and the caller will start over from
643 the beginning. Backtracking will be necessary in parsing subexpressions,
644 but that can be handled there. Not handling backtracking here may get
645 expensive in the case of the m68k. Deal with later.
646
647 Returns NULL for success, an error message for failure.
648 */
649
650 static const char *
651 parse_insn_normal (od, insn, strp, fields)
652 CGEN_OPCODE_DESC od;
653 const CGEN_INSN * insn;
654 const char ** strp;
655 CGEN_FIELDS * fields;
656 {
657 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
658 const char * str = *strp;
659 const char * errmsg;
660 const char * p;
661 const unsigned char * syn;
662 #ifdef CGEN_MNEMONIC_OPERANDS
663 /* FIXME: wip */
664 int past_opcode_p;
665 #endif
666
667 /* For now we assume the mnemonic is first (there are no leading operands).
668 We can parse it without needing to set up operand parsing.
669 GAS's input scrubber will ensure mnemonics are lowercase, but we may
670 not be called from GAS. */
671 p = CGEN_INSN_MNEMONIC (insn);
672 while (*p && tolower (*p) == tolower (*str))
673 ++p, ++str;
674
675 if (* p || (* str && !isspace (* str)))
676 return _("unrecognized instruction");
677
678 CGEN_INIT_PARSE (od);
679 cgen_init_parse_operand (od);
680 #ifdef CGEN_MNEMONIC_OPERANDS
681 past_opcode_p = 0;
682 #endif
683
684 /* We don't check for (*str != '\0') here because we want to parse
685 any trailing fake arguments in the syntax string. */
686 syn = CGEN_SYNTAX_STRING (syntax);
687
688 /* Mnemonics come first for now, ensure valid string. */
689 if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
690 abort ();
691
692 ++syn;
693
694 while (* syn != 0)
695 {
696 /* Non operand chars must match exactly. */
697 if (CGEN_SYNTAX_CHAR_P (* syn))
698 {
699 if (*str == CGEN_SYNTAX_CHAR (* syn))
700 {
701 #ifdef CGEN_MNEMONIC_OPERANDS
702 if (* syn == ' ')
703 past_opcode_p = 1;
704 #endif
705 ++ syn;
706 ++ str;
707 }
708 else
709 {
710 /* Syntax char didn't match. Can't be this insn. */
711 /* FIXME: would like to return something like
712 "expected char `c'" */
713 return _("syntax error");
714 }
715 continue;
716 }
717
718 /* We have an operand of some sort. */
719 errmsg = fr30_cgen_parse_operand (od, CGEN_SYNTAX_FIELD (*syn),
720 &str, fields);
721 if (errmsg)
722 return errmsg;
723
724 /* Done with this operand, continue with next one. */
725 ++ syn;
726 }
727
728 /* If we're at the end of the syntax string, we're done. */
729 if (* syn == '\0')
730 {
731 /* FIXME: For the moment we assume a valid `str' can only contain
732 blanks now. IE: We needn't try again with a longer version of
733 the insn and it is assumed that longer versions of insns appear
734 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
735 while (isspace (* str))
736 ++ str;
737
738 if (* str != '\0')
739 return _("junk at end of line"); /* FIXME: would like to include `str' */
740
741 return NULL;
742 }
743
744 /* We couldn't parse it. */
745 return _("unrecognized instruction");
746 }
747
748 /* Default insn builder (insert handler).
749 The instruction is recorded in CGEN_INT_INSN_P byte order
750 (meaning that if CGEN_INT_INSN_P BUFFER is an int * and thus the value is
751 recorded in host byte order, otherwise BUFFER is an array of bytes and the
752 value is recorded in target byte order).
753 The result is an error message or NULL if success. */
754
755 static const char *
756 insert_insn_normal (od, insn, fields, buffer, pc)
757 CGEN_OPCODE_DESC od;
758 const CGEN_INSN * insn;
759 CGEN_FIELDS * fields;
760 CGEN_INSN_BYTES_PTR buffer;
761 bfd_vma pc;
762 {
763 const CGEN_SYNTAX * syntax = CGEN_INSN_SYNTAX (insn);
764 unsigned long value;
765 const unsigned char * syn;
766
767 CGEN_INIT_INSERT (od);
768 value = CGEN_INSN_VALUE (insn);
769
770 /* If we're recording insns as numbers (rather than a string of bytes),
771 target byte order handling is deferred until later. */
772
773 #if CGEN_INT_INSN_P
774
775 *buffer = value;
776
777 #else
778
779 cgen_put_insn_value (od, buffer, min (CGEN_BASE_INSN_BITSIZE,
780 CGEN_FIELDS_BITSIZE (fields)),
781 value);
782
783 #endif /* ! CGEN_INT_INSN_P */
784
785 /* ??? Rather than scanning the syntax string again, we could store
786 in `fields' a null terminated list of the fields that are present. */
787
788 for (syn = CGEN_SYNTAX_STRING (syntax); * syn != '\0'; ++ syn)
789 {
790 const char *errmsg;
791
792 if (CGEN_SYNTAX_CHAR_P (* syn))
793 continue;
794
795 errmsg = fr30_cgen_insert_operand (od, CGEN_SYNTAX_FIELD (*syn),
796 fields, buffer, pc);
797 if (errmsg)
798 return errmsg;
799 }
800
801 return NULL;
802 }
803 \f
804 /* Main entry point.
805 This routine is called for each instruction to be assembled.
806 STR points to the insn to be assembled.
807 We assume all necessary tables have been initialized.
808 The assembled instruction, less any fixups, is stored in BUF.
809 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
810 still needs to be converted to target byte order, otherwise BUF is an array
811 of bytes in target byte order.
812 The result is a pointer to the insn's entry in the opcode table,
813 or NULL if an error occured (an error message will have already been
814 printed).
815
816 Note that when processing (non-alias) macro-insns,
817 this function recurses. */
818
819 const CGEN_INSN *
820 fr30_cgen_assemble_insn (od, str, fields, buf, errmsg)
821 CGEN_OPCODE_DESC od;
822 const char * str;
823 CGEN_FIELDS * fields;
824 CGEN_INSN_BYTES_PTR buf;
825 char ** errmsg;
826 {
827 const char * start;
828 CGEN_INSN_LIST * ilist;
829
830 /* Skip leading white space. */
831 while (isspace (* str))
832 ++ str;
833
834 /* The instructions are stored in hashed lists.
835 Get the first in the list. */
836 ilist = CGEN_ASM_LOOKUP_INSN (od, str);
837
838 /* Keep looking until we find a match. */
839
840 start = str;
841 for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
842 {
843 const CGEN_INSN *insn = ilist->insn;
844
845 #if 0 /* not needed as unsupported opcodes shouldn't be in the hash lists */
846 /* Is this insn supported by the selected cpu? */
847 if (! fr30_cgen_insn_supported (od, insn))
848 continue;
849 #endif
850
851 /* If the RELAX 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 if (CGEN_INSN_ATTR (insn, CGEN_INSN_RELAX) != 0)
855 continue;
856
857 str = start;
858
859 /* Record a default length for the insn. This will get set to the
860 correct value while parsing. */
861 /* FIXME: wip */
862 CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
863
864 if (! CGEN_PARSE_FN (insn) (od, insn, & str, fields))
865 {
866 /* ??? 0 is passed for `pc' */
867 if (CGEN_INSERT_FN (insn) (od, insn, fields, buf, (bfd_vma) 0) != NULL)
868 continue;
869 /* It is up to the caller to actually output the insn and any
870 queued relocs. */
871 return insn;
872 }
873
874 /* Try the next entry. */
875 }
876
877 /* FIXME: We can return a better error message than this.
878 Need to track why it failed and pick the right one. */
879 {
880 static char errbuf[100];
881 if (strlen (start) > 50)
882 /* xgettext:c-format */
883 sprintf (errbuf, _("bad instruction `%.50s...'"), start);
884 else
885 /* xgettext:c-format */
886 sprintf (errbuf, _("bad instruction `%.50s'"), start);
887
888 *errmsg = errbuf;
889 return NULL;
890 }
891 }
892 \f
893 #if 0 /* This calls back to GAS which we can't do without care. */
894
895 /* Record each member of OPVALS in the assembler's symbol table.
896 This lets GAS parse registers for us.
897 ??? Interesting idea but not currently used. */
898
899 /* Record each member of OPVALS in the assembler's symbol table.
900 FIXME: Not currently used. */
901
902 void
903 fr30_cgen_asm_hash_keywords (od, opvals)
904 CGEN_OPCODE_DESC od;
905 CGEN_KEYWORD * opvals;
906 {
907 CGEN_KEYWORD_SEARCH search = cgen_keyword_search_init (opvals, NULL);
908 const CGEN_KEYWORD_ENTRY * ke;
909
910 while ((ke = cgen_keyword_search_next (& search)) != NULL)
911 {
912 #if 0 /* Unnecessary, should be done in the search routine. */
913 if (! fr30_cgen_opval_supported (ke))
914 continue;
915 #endif
916 cgen_asm_record_register (od, ke->name, ke->value);
917 }
918 }
919
920 #endif /* 0 */
This page took 0.073369 seconds and 5 git commands to generate.