1 /* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
4 THIS FILE IS USED TO GENERATE fr30-asm.c.
6 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
8 This file is part of the GNU Binutils and GDB, the GNU debugger.
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)
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.
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. */
34 #define min(a,b) ((a) < (b) ? (a) : (b))
36 #define max(a,b) ((a) > (b) ? (a) : (b))
40 #define INLINE __inline__
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
));
55 /* -- assembler routines inserted here */
57 /* Main entry point for operand parsing.
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.
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
72 fr30_cgen_parse_operand (od
, opindex
, strp
, fields
)
82 case FR30_OPERAND_RI
:
83 errmsg
= cgen_parse_keyword (od
, strp
, & fr30_cgen_opval_h_gr
, & fields
->f_Ri
);
85 case FR30_OPERAND_RJ
:
86 errmsg
= cgen_parse_keyword (od
, strp
, & fr30_cgen_opval_h_gr
, & fields
->f_Rj
);
90 /* xgettext:c-format */
91 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
98 /* Main entry point for operand insertion.
100 This function is basically just a big switch statement. Earlier versions
101 used tables to look up the function to use, but
102 - if the table contains both assembler and disassembler functions then
103 the disassembler contains much of the assembler and vice-versa,
104 - there's a lot of inlining possibilities as things grow,
105 - using a switch statement avoids the function call overhead.
107 This function could be moved into `parse_insn_normal', but keeping it
108 separate makes clear the interface between `parse_insn_normal' and each of
109 the handlers. It's also needed by GAS to insert operands that couldn't be
110 resolved during parsing.
114 fr30_cgen_insert_operand (od
, opindex
, fields
, buffer
, pc
)
117 CGEN_FIELDS
* fields
;
118 CGEN_INSN_BYTES_PTR buffer
;
125 case FR30_OPERAND_RI
:
126 errmsg
= insert_normal (od
, fields
->f_Ri
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 12, 4, CGEN_FIELDS_BITSIZE (fields
), buffer
);
128 case FR30_OPERAND_RJ
:
129 errmsg
= insert_normal (od
, fields
->f_Rj
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 8, 4, CGEN_FIELDS_BITSIZE (fields
), buffer
);
133 /* xgettext:c-format */
134 fprintf (stderr
, _("Unrecognized field %d while building insn.\n"),
142 cgen_parse_fn
* const fr30_cgen_parse_handlers
[] =
148 cgen_insert_fn
* const fr30_cgen_insert_handlers
[] =
155 fr30_cgen_init_asm (od
)
161 #if ! CGEN_INT_INSN_P
163 /* Subroutine of insert_normal. */
166 insert_1 (od
, value
, start
, length
, word_length
, bufp
)
169 int start
,length
,word_length
;
172 unsigned long x
,mask
;
174 int big_p
= CGEN_OPCODE_INSN_ENDIAN (od
) == CGEN_ENDIAN_BIG
;
183 x
= bfd_getb16 (bufp
);
185 x
= bfd_getl16 (bufp
);
188 /* ??? This may need reworking as these cases don't necessarily
189 want the first byte and the last two bytes handled like this. */
191 x
= (bfd_getb8 (bufp
) << 16) | bfd_getb16 (bufp
+ 1);
193 x
= bfd_getl16 (bufp
) | (bfd_getb8 (bufp
+ 2) << 16);
197 x
= bfd_getb32 (bufp
);
199 x
= bfd_getl32 (bufp
);
205 /* Written this way to avoid undefined behaviour. */
206 mask
= (((1L << (length
- 1)) - 1) << 1) | 1;
207 if (CGEN_INSN_LSB0_P
)
210 shift
= (word_length
- (start
+ length
));
211 x
= (x
& ~(mask
<< shift
)) | ((value
& mask
) << shift
);
220 bfd_putb16 (x
, bufp
);
222 bfd_putl16 (x
, bufp
);
225 /* ??? This may need reworking as these cases don't necessarily
226 want the first byte and the last two bytes handled like this. */
229 bfd_putb8 (x
>> 16, bufp
);
230 bfd_putb16 (x
, bufp
+ 1);
234 bfd_putl16 (x
, bufp
);
235 bfd_putb8 (x
>> 16, bufp
+ 2);
240 bfd_putb32 (x
, bufp
);
242 bfd_putl32 (x
, bufp
);
249 #endif /* ! CGEN_INT_INSN_P */
251 /* Default insertion routine.
253 ATTRS is a mask of the boolean attributes.
254 START is the starting bit number, architecture origin.
255 LENGTH is the length of VALUE in bits.
256 TOTAL_LENGTH is the total length of the insn.
258 The result is an error message or NULL if success. */
260 /* ??? May need to know word length in order to properly place values as
261 an insn may be made of multiple words and the current bit number handling
262 may be insufficient. Word length is an architectural attribute and thus
263 methinks the way to go [if needed] is to fetch this value from OD or
264 define a macro in <arch>-opc.h rather than adding an extra argument -
265 after all that's how endianness is handled. */
266 /* ??? This duplicates functionality with bfd's howto table and
267 bfd_install_relocation. */
268 /* ??? For architectures where insns can be representable as ints,
269 store insn in `field' struct and add registers, etc. while parsing? */
270 /* ??? This doesn't handle bfd_vma's. Create another function when
274 insert_normal (od
, value
, attrs
, start
, length
, total_length
, buffer
)
281 CGEN_INSN_BYTES_PTR buffer
;
283 static char errbuf
[100];
284 /* Written this way to avoid undefined behaviour. */
285 unsigned long mask
= (((1L << (length
- 1)) - 1) << 1) | 1;
287 /* If LENGTH is zero, this operand doesn't contribute to the value. */
291 /* Ensure VALUE will fit. */
292 if ((attrs
& CGEN_ATTR_MASK (CGEN_OPERAND_UNSIGNED
)) != 0)
294 unsigned long maxval
= mask
;
295 if ((unsigned long) value
> maxval
)
297 /* xgettext:c-format */
299 _("operand out of range (%lu not between 0 and %lu)"),
306 long minval
= - (1L << (length
- 1));
307 long maxval
= (1L << (length
- 1)) - 1;
308 if (value
< minval
|| value
> maxval
)
311 /* xgettext:c-format */
312 (errbuf
, _("operand out of range (%ld not between %ld and %ld)"),
313 value
, minval
, maxval
);
320 if (total_length
> 32)
325 if (CGEN_INSN_LSB0_P
)
328 shift
= total_length
- (start
+ length
);
329 *buffer
= (*buffer
& ~(mask
<< shift
)) | ((value
& mask
) << shift
);
334 /* FIXME: unfinished and untested */
336 /* ??? To be defined in <arch>-opc.h as necessary. */
337 #ifndef CGEN_WORD_ENDIAN
338 #define CGEN_WORD_ENDIAN(od) CGEN_OPCODE_ENDIAN (od)
340 #ifndef CGEN_INSN_WORD_ENDIAN
341 #define CGEN_INSN_WORD_ENDIAN(od) CGEN_WORD_ENDIAN (od)
344 /* The hard case is probably too slow for the normal cases.
345 It's certainly more difficult to understand than the normal case.
346 Thus this is split into two. Keep it that way. The hard case is defined
347 to be when a field straddles a (loosely defined) word boundary
348 (??? which may require target specific help to determine). */
352 #define HARD_CASE_P 0 /* FIXME:wip */
356 unsigned char *bufp
= (unsigned char *) buffer
;
357 int insn_length_left
= total_length
;
359 if (CGEN_INSN_LSB0_P
)
361 int word_offset
= (CGEN_INSN_WORD_ENDIAN (od
) == CGEN_ENDIAN_BIG
363 : start
/ CGEN_BASE_INSN_BITSIZE
);
364 bufp
+= word_offset
* (CGEN_BASE_INSN_BITSIZE
/ 8);
365 if (CGEN_INSN_WORD_ENDIAN (od
) == CGEN_ENDIAN_BIG
)
367 start
-= word_offset
* CGEN_BASE_INSN_BITSIZE
;
371 int word_offset
= (CGEN_INSN_WORD_ENDIAN (od
) == CGEN_ENDIAN_BIG
372 ? start
/ CGEN_BASE_INSN_BITSIZE
374 bufp
+= word_offset
* (CGEN_BASE_INSN_BITSIZE
/ 8);
375 if (CGEN_INSN_WORD_ENDIAN (od
) == CGEN_ENDIAN_BIG
)
376 start
-= word_offset
* CGEN_BASE_INSN_BITSIZE
;
380 /* Loop so we handle a field straddling an insn word boundary
381 (remember, "insn word boundary" is loosely defined here). */
385 int this_pass_length
= length
;
386 int this_pass_start
= start
;
387 int this_pass_word_length
= min (insn_length_left
,
388 (CGEN_BASE_INSN_BITSIZE
== 8
390 : CGEN_BASE_INSN_BITSIZE
));
392 insert_1 (od
, value
, attrs
,
393 this_pass_start
, this_pass_length
, this_pass_word_length
,
396 length
-= this_pass_length
;
397 insn_length_left
-= this_pass_word_length
;
408 bufp
+= this_pass_word_length
/ 8;
414 unsigned char *bufp
= (unsigned char *) buffer
;
419 /* Adjust start,total_length,bufp to point to the pseudo-word that holds
420 the value. For example in a 48 bit insn where the value to insert
421 (say an immediate value) is the last 16 bits then word_length here
422 would be 16. To handle a 24 bit insn with an 18 bit immediate,
423 insert_1 handles 24 bits (using a combination of bfd_get8,16). */
425 if (total_length
> 32)
427 int needed_width
= start
% 8 + length
;
428 int fetch_length
= (needed_width
<= 8 ? 8
429 : needed_width
<= 16 ? 16
432 if (CGEN_INSN_LSB0_P
)
434 if (CGEN_INSN_WORD_ENDIAN (od
) == CGEN_ENDIAN_BIG
)
440 int offset
= start
& ~7;
444 total_length
-= offset
;
449 if (CGEN_INSN_WORD_ENDIAN (od
) == CGEN_ENDIAN_BIG
)
451 int offset
= start
& ~7;
455 total_length
-= offset
;
464 insert_1 (od
, value
, start
, length
, total_length
, bufp
);
467 #endif /* ! CGEN_INT_INSN_P */
472 /* Default insn parser.
474 The syntax string is scanned and operands are parsed and stored in FIELDS.
475 Relocs are queued as we go via other callbacks.
477 ??? Note that this is currently an all-or-nothing parser. If we fail to
478 parse the instruction, we return 0 and the caller will start over from
479 the beginning. Backtracking will be necessary in parsing subexpressions,
480 but that can be handled there. Not handling backtracking here may get
481 expensive in the case of the m68k. Deal with later.
483 Returns NULL for success, an error message for failure.
487 parse_insn_normal (od
, insn
, strp
, fields
)
489 const CGEN_INSN
* insn
;
491 CGEN_FIELDS
* fields
;
493 const CGEN_SYNTAX
* syntax
= CGEN_INSN_SYNTAX (insn
);
494 const char * str
= *strp
;
497 const unsigned char * syn
;
498 #ifdef CGEN_MNEMONIC_OPERANDS
503 /* For now we assume the mnemonic is first (there are no leading operands).
504 We can parse it without needing to set up operand parsing.
505 GAS's input scrubber will ensure mnemonics are lowercase, but we may
506 not be called from GAS. */
507 p
= CGEN_INSN_MNEMONIC (insn
);
508 while (*p
&& tolower (*p
) == tolower (*str
))
511 if (* p
|| (* str
&& !isspace (* str
)))
512 return _("unrecognized instruction");
514 CGEN_INIT_PARSE (od
);
515 cgen_init_parse_operand (od
);
516 #ifdef CGEN_MNEMONIC_OPERANDS
520 /* We don't check for (*str != '\0') here because we want to parse
521 any trailing fake arguments in the syntax string. */
522 syn
= CGEN_SYNTAX_STRING (syntax
);
524 /* Mnemonics come first for now, ensure valid string. */
525 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
532 /* Non operand chars must match exactly. */
533 if (CGEN_SYNTAX_CHAR_P (* syn
))
535 if (*str
== CGEN_SYNTAX_CHAR (* syn
))
537 #ifdef CGEN_MNEMONIC_OPERANDS
546 /* Syntax char didn't match. Can't be this insn. */
547 /* FIXME: would like to return something like
548 "expected char `c'" */
549 return _("syntax error");
554 /* We have an operand of some sort. */
555 errmsg
= fr30_cgen_parse_operand (od
, CGEN_SYNTAX_FIELD (*syn
),
560 /* Done with this operand, continue with next one. */
564 /* If we're at the end of the syntax string, we're done. */
567 /* FIXME: For the moment we assume a valid `str' can only contain
568 blanks now. IE: We needn't try again with a longer version of
569 the insn and it is assumed that longer versions of insns appear
570 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
571 while (isspace (* str
))
575 return _("junk at end of line"); /* FIXME: would like to include `str' */
580 /* We couldn't parse it. */
581 return _("unrecognized instruction");
584 /* Default insn builder (insert handler).
585 The instruction is recorded in CGEN_INT_INSN_P byte order
586 (meaning that if CGEN_INT_INSN_P BUFFER is an int * and thus the value is
587 recorded in host byte order, otherwise BUFFER is an array of bytes and the
588 value is recorded in target byte order).
589 The result is an error message or NULL if success. */
592 insert_insn_normal (od
, insn
, fields
, buffer
, pc
)
594 const CGEN_INSN
* insn
;
595 CGEN_FIELDS
* fields
;
596 CGEN_INSN_BYTES_PTR buffer
;
599 const CGEN_SYNTAX
* syntax
= CGEN_INSN_SYNTAX (insn
);
601 const unsigned char * syn
;
603 CGEN_INIT_INSERT (od
);
604 value
= CGEN_INSN_VALUE (insn
);
606 /* If we're recording insns as numbers (rather than a string of bytes),
607 target byte order handling is deferred until later. */
615 cgen_insn_put_value (od
, buffer
, min (CGEN_BASE_INSN_BITSIZE
,
616 CGEN_FIELDS_BITSIZE (fields
)),
619 #endif /* ! CGEN_INT_INSN_P */
621 /* ??? Rather than scanning the syntax string again, we could store
622 in `fields' a null terminated list of the fields that are present. */
624 for (syn
= CGEN_SYNTAX_STRING (syntax
); * syn
!= '\0'; ++ syn
)
628 if (CGEN_SYNTAX_CHAR_P (* syn
))
631 errmsg
= fr30_cgen_insert_operand (od
, CGEN_SYNTAX_FIELD (*syn
),
641 This routine is called for each instruction to be assembled.
642 STR points to the insn to be assembled.
643 We assume all necessary tables have been initialized.
644 The assembled instruction, less any fixups, is stored in BUF.
645 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
646 still needs to be converted to target byte order, otherwise BUF is an array
647 of bytes in target byte order.
648 The result is a pointer to the insn's entry in the opcode table,
649 or NULL if an error occured (an error message will have already been
652 Note that when processing (non-alias) macro-insns,
653 this function recurses. */
656 fr30_cgen_assemble_insn (od
, str
, fields
, buf
, errmsg
)
659 CGEN_FIELDS
* fields
;
660 CGEN_INSN_BYTES_PTR buf
;
664 CGEN_INSN_LIST
* ilist
;
666 /* Skip leading white space. */
667 while (isspace (* str
))
670 /* The instructions are stored in hashed lists.
671 Get the first in the list. */
672 ilist
= CGEN_ASM_LOOKUP_INSN (od
, str
);
674 /* Keep looking until we find a match. */
677 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
679 const CGEN_INSN
*insn
= ilist
->insn
;
681 #if 0 /* not needed as unsupported opcodes shouldn't be in the hash lists */
682 /* Is this insn supported by the selected cpu? */
683 if (! fr30_cgen_insn_supported (od
, insn
))
687 /* If the RELAX attribute is set, this is an insn that shouldn't be
688 chosen immediately. Instead, it is used during assembler/linker
689 relaxation if possible. */
690 if (CGEN_INSN_ATTR (insn
, CGEN_INSN_RELAX
) != 0)
695 /* Record a default length for the insn. This will get set to the
696 correct value while parsing. */
698 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
700 if (! CGEN_PARSE_FN (insn
) (od
, insn
, & str
, fields
))
702 /* ??? 0 is passed for `pc' */
703 if (CGEN_INSERT_FN (insn
) (od
, insn
, fields
, buf
, (bfd_vma
) 0) != NULL
)
705 /* It is up to the caller to actually output the insn and any
710 /* Try the next entry. */
713 /* FIXME: We can return a better error message than this.
714 Need to track why it failed and pick the right one. */
716 static char errbuf
[100];
717 if (strlen (start
) > 50)
718 /* xgettext:c-format */
719 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
721 /* xgettext:c-format */
722 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);
729 #if 0 /* This calls back to GAS which we can't do without care. */
731 /* Record each member of OPVALS in the assembler's symbol table.
732 This lets GAS parse registers for us.
733 ??? Interesting idea but not currently used. */
735 /* Record each member of OPVALS in the assembler's symbol table.
736 FIXME: Not currently used. */
739 fr30_cgen_asm_hash_keywords (od
, opvals
)
741 CGEN_KEYWORD
* opvals
;
743 CGEN_KEYWORD_SEARCH search
= cgen_keyword_search_init (opvals
, NULL
);
744 const CGEN_KEYWORD_ENTRY
* ke
;
746 while ((ke
= cgen_keyword_search_next (& search
)) != NULL
)
748 #if 0 /* Unnecessary, should be done in the search routine. */
749 if (! fr30_cgen_opval_supported (ke
))
752 cgen_asm_record_register (od
, ke
->name
, ke
->value
);