1 /* Assembler interface for targets using CGEN. -*- C -*-
2 CGEN: Cpu tools GENerator
4 THIS FILE IS USED TO GENERATE i960c-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. */
30 #include "i960c-opc.h"
34 #define min(a,b) ((a) < (b) ? (a) : (b))
36 #define max(a,b) ((a) > (b) ? (a) : (b))
40 #define INLINE __inline__
45 /* Used by the ifield rtx function. */
46 #define FLD(f) (fields->f)
48 static const char * insert_normal
49 PARAMS ((CGEN_OPCODE_DESC
, long, unsigned int, unsigned int, unsigned int,
50 unsigned int, unsigned int, unsigned int, CGEN_INSN_BYTES_PTR
));
51 static const char * parse_insn_normal
52 PARAMS ((CGEN_OPCODE_DESC
, const CGEN_INSN
*,
53 const char **, CGEN_FIELDS
*));
54 static const char * insert_insn_normal
55 PARAMS ((CGEN_OPCODE_DESC
, const CGEN_INSN
*,
56 CGEN_FIELDS
*, CGEN_INSN_BYTES_PTR
, bfd_vma
));
58 /* -- assembler routines inserted here */
60 /* Main entry point for operand parsing.
62 This function is basically just a big switch statement. Earlier versions
63 used tables to look up the function to use, but
64 - if the table contains both assembler and disassembler functions then
65 the disassembler contains much of the assembler and vice-versa,
66 - there's a lot of inlining possibilities as things grow,
67 - using a switch statement avoids the function call overhead.
69 This function could be moved into `parse_insn_normal', but keeping it
70 separate makes clear the interface between `parse_insn_normal' and each of
75 i960_cgen_parse_operand (od
, opindex
, strp
, fields
)
85 case I960_OPERAND_SRC1
:
86 errmsg
= cgen_parse_keyword (od
, strp
, & i960_cgen_opval_h_gr
, & fields
->f_src1
);
88 case I960_OPERAND_SRC2
:
89 errmsg
= cgen_parse_keyword (od
, strp
, & i960_cgen_opval_h_gr
, & fields
->f_src2
);
91 case I960_OPERAND_DST
:
92 errmsg
= cgen_parse_keyword (od
, strp
, & i960_cgen_opval_h_gr
, & fields
->f_srcdst
);
94 case I960_OPERAND_LIT1
:
95 errmsg
= cgen_parse_unsigned_integer (od
, strp
, I960_OPERAND_LIT1
, &fields
->f_src1
);
97 case I960_OPERAND_LIT2
:
98 errmsg
= cgen_parse_unsigned_integer (od
, strp
, I960_OPERAND_LIT2
, &fields
->f_src2
);
100 case I960_OPERAND_ST_SRC
:
101 errmsg
= cgen_parse_keyword (od
, strp
, & i960_cgen_opval_h_gr
, & fields
->f_srcdst
);
103 case I960_OPERAND_ABASE
:
104 errmsg
= cgen_parse_keyword (od
, strp
, & i960_cgen_opval_h_gr
, & fields
->f_abase
);
106 case I960_OPERAND_OFFSET
:
107 errmsg
= cgen_parse_unsigned_integer (od
, strp
, I960_OPERAND_OFFSET
, &fields
->f_offset
);
109 case I960_OPERAND_SCALE
:
110 errmsg
= cgen_parse_unsigned_integer (od
, strp
, I960_OPERAND_SCALE
, &fields
->f_scale
);
112 case I960_OPERAND_INDEX
:
113 errmsg
= cgen_parse_keyword (od
, strp
, & i960_cgen_opval_h_gr
, & fields
->f_index
);
115 case I960_OPERAND_OPTDISP
:
116 errmsg
= cgen_parse_unsigned_integer (od
, strp
, I960_OPERAND_OPTDISP
, &fields
->f_optdisp
);
118 case I960_OPERAND_BR_SRC1
:
119 errmsg
= cgen_parse_keyword (od
, strp
, & i960_cgen_opval_h_gr
, & fields
->f_br_src1
);
121 case I960_OPERAND_BR_SRC2
:
122 errmsg
= cgen_parse_keyword (od
, strp
, & i960_cgen_opval_h_gr
, & fields
->f_br_src2
);
124 case I960_OPERAND_BR_DISP
:
127 errmsg
= cgen_parse_address (od
, strp
, I960_OPERAND_BR_DISP
, 0, NULL
, & value
);
128 fields
->f_br_disp
= value
;
131 case I960_OPERAND_BR_LIT1
:
132 errmsg
= cgen_parse_unsigned_integer (od
, strp
, I960_OPERAND_BR_LIT1
, &fields
->f_br_src1
);
134 case I960_OPERAND_CTRL_DISP
:
137 errmsg
= cgen_parse_address (od
, strp
, I960_OPERAND_CTRL_DISP
, 0, NULL
, & value
);
138 fields
->f_ctrl_disp
= value
;
143 /* xgettext:c-format */
144 fprintf (stderr
, _("Unrecognized field %d while parsing.\n"), opindex
);
151 /* Main entry point for operand insertion.
153 This function is basically just a big switch statement. Earlier versions
154 used tables to look up the function to use, but
155 - if the table contains both assembler and disassembler functions then
156 the disassembler contains much of the assembler and vice-versa,
157 - there's a lot of inlining possibilities as things grow,
158 - using a switch statement avoids the function call overhead.
160 This function could be moved into `parse_insn_normal', but keeping it
161 separate makes clear the interface between `parse_insn_normal' and each of
162 the handlers. It's also needed by GAS to insert operands that couldn't be
163 resolved during parsing.
167 i960_cgen_insert_operand (od
, opindex
, fields
, buffer
, pc
)
170 CGEN_FIELDS
* fields
;
171 CGEN_INSN_BYTES_PTR buffer
;
175 unsigned int total_length
= CGEN_FIELDS_BITSIZE (fields
);
179 case I960_OPERAND_SRC1
:
180 errmsg
= insert_normal (od
, fields
->f_src1
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 27, 5, 32, total_length
, buffer
);
182 case I960_OPERAND_SRC2
:
183 errmsg
= insert_normal (od
, fields
->f_src2
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 13, 5, 32, total_length
, buffer
);
185 case I960_OPERAND_DST
:
186 errmsg
= insert_normal (od
, fields
->f_srcdst
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 8, 5, 32, total_length
, buffer
);
188 case I960_OPERAND_LIT1
:
189 errmsg
= insert_normal (od
, fields
->f_src1
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 27, 5, 32, total_length
, buffer
);
191 case I960_OPERAND_LIT2
:
192 errmsg
= insert_normal (od
, fields
->f_src2
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 13, 5, 32, total_length
, buffer
);
194 case I960_OPERAND_ST_SRC
:
195 errmsg
= insert_normal (od
, fields
->f_srcdst
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 8, 5, 32, total_length
, buffer
);
197 case I960_OPERAND_ABASE
:
198 errmsg
= insert_normal (od
, fields
->f_abase
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 13, 5, 32, total_length
, buffer
);
200 case I960_OPERAND_OFFSET
:
201 errmsg
= insert_normal (od
, fields
->f_offset
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 20, 12, 32, total_length
, buffer
);
203 case I960_OPERAND_SCALE
:
204 errmsg
= insert_normal (od
, fields
->f_scale
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 22, 3, 32, total_length
, buffer
);
206 case I960_OPERAND_INDEX
:
207 errmsg
= insert_normal (od
, fields
->f_index
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 27, 5, 32, total_length
, buffer
);
209 case I960_OPERAND_OPTDISP
:
210 errmsg
= insert_normal (od
, fields
->f_optdisp
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 32, 0, 32, 32, total_length
, buffer
);
212 case I960_OPERAND_BR_SRC1
:
213 errmsg
= insert_normal (od
, fields
->f_br_src1
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 8, 5, 32, total_length
, buffer
);
215 case I960_OPERAND_BR_SRC2
:
216 errmsg
= insert_normal (od
, fields
->f_br_src2
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 13, 5, 32, total_length
, buffer
);
218 case I960_OPERAND_BR_DISP
:
220 long value
= fields
->f_br_disp
;
221 value
= ((int) (((value
) - (pc
))) >> (2));
222 errmsg
= insert_normal (od
, value
, 0|(1<<CGEN_OPERAND_RELOC
)|(1<<CGEN_OPERAND_PCREL_ADDR
), 0, 19, 11, 32, total_length
, buffer
);
225 case I960_OPERAND_BR_LIT1
:
226 errmsg
= insert_normal (od
, fields
->f_br_src1
, 0|(1<<CGEN_OPERAND_UNSIGNED
), 0, 8, 5, 32, total_length
, buffer
);
228 case I960_OPERAND_CTRL_DISP
:
230 long value
= fields
->f_ctrl_disp
;
231 value
= ((int) (((value
) - (pc
))) >> (2));
232 errmsg
= insert_normal (od
, value
, 0|(1<<CGEN_OPERAND_RELOC
)|(1<<CGEN_OPERAND_PCREL_ADDR
), 0, 8, 22, 32, total_length
, buffer
);
237 /* xgettext:c-format */
238 fprintf (stderr
, _("Unrecognized field %d while building insn.\n"),
246 cgen_parse_fn
* const i960_cgen_parse_handlers
[] =
252 cgen_insert_fn
* const i960_cgen_insert_handlers
[] =
259 i960_cgen_init_asm (od
)
265 #if ! CGEN_INT_INSN_P
267 /* Subroutine of insert_normal. */
270 insert_1 (od
, value
, start
, length
, word_length
, bufp
)
273 int start
,length
,word_length
;
276 unsigned long x
,mask
;
278 int big_p
= CGEN_OPCODE_INSN_ENDIAN (od
) == CGEN_ENDIAN_BIG
;
287 x
= bfd_getb16 (bufp
);
289 x
= bfd_getl16 (bufp
);
292 /* ??? This may need reworking as these cases don't necessarily
293 want the first byte and the last two bytes handled like this. */
295 x
= (bufp
[0] << 16) | bfd_getb16 (bufp
+ 1);
297 x
= bfd_getl16 (bufp
) | (bufp
[2] << 16);
301 x
= bfd_getb32 (bufp
);
303 x
= bfd_getl32 (bufp
);
309 /* Written this way to avoid undefined behaviour. */
310 mask
= (((1L << (length
- 1)) - 1) << 1) | 1;
311 if (CGEN_INSN_LSB0_P
)
312 shift
= (start
+ 1) - length
;
314 shift
= (word_length
- (start
+ length
));
315 x
= (x
& ~(mask
<< shift
)) | ((value
& mask
) << shift
);
324 bfd_putb16 (x
, bufp
);
326 bfd_putl16 (x
, bufp
);
329 /* ??? This may need reworking as these cases don't necessarily
330 want the first byte and the last two bytes handled like this. */
334 bfd_putb16 (x
, bufp
+ 1);
338 bfd_putl16 (x
, bufp
);
344 bfd_putb32 (x
, bufp
);
346 bfd_putl32 (x
, bufp
);
353 #endif /* ! CGEN_INT_INSN_P */
355 /* Default insertion routine.
357 ATTRS is a mask of the boolean attributes.
358 WORD_OFFSET is the offset in bits from the start of the insn of the value.
359 WORD_LENGTH is the length of the word in bits in which the value resides.
360 START is the starting bit number in the word, architecture origin.
361 LENGTH is the length of VALUE in bits.
362 TOTAL_LENGTH is the total length of the insn in bits.
364 The result is an error message or NULL if success. */
366 /* ??? This duplicates functionality with bfd's howto table and
367 bfd_install_relocation. */
368 /* ??? This doesn't handle bfd_vma's. Create another function when
372 insert_normal (od
, value
, attrs
, word_offset
, start
, length
, word_length
,
373 total_length
, buffer
)
377 unsigned int word_offset
, start
, length
, word_length
, total_length
;
378 CGEN_INSN_BYTES_PTR buffer
;
380 static char errbuf
[100];
381 /* Written this way to avoid undefined behaviour. */
382 unsigned long mask
= (((1L << (length
- 1)) - 1) << 1) | 1;
384 /* If LENGTH is zero, this operand doesn't contribute to the value. */
392 if (word_length
> 32)
395 /* For architectures with insns smaller than the insn-base-bitsize,
396 word_length may be too big. */
397 #if CGEN_MIN_INSN_BITSIZE < CGEN_BASE_INSN_BITSIZE
399 && word_length
> total_length
)
400 word_length
= total_length
;
403 /* Ensure VALUE will fit. */
404 if ((attrs
& CGEN_ATTR_MASK (CGEN_OPERAND_UNSIGNED
)) != 0)
406 unsigned long maxval
= mask
;
407 if ((unsigned long) value
> maxval
)
409 /* xgettext:c-format */
411 _("operand out of range (%lu not between 0 and %lu)"),
418 long minval
= - (1L << (length
- 1));
419 long maxval
= (1L << (length
- 1)) - 1;
420 if (value
< minval
|| value
> maxval
)
423 /* xgettext:c-format */
424 (errbuf
, _("operand out of range (%ld not between %ld and %ld)"),
425 value
, minval
, maxval
);
435 if (CGEN_INSN_LSB0_P
)
436 shift
= (start
+ 1) - length
;
438 shift
= word_length
- (start
+ length
);
439 *buffer
= (*buffer
& ~(mask
<< shift
)) | ((value
& mask
) << shift
);
442 #else /* ! CGEN_INT_INSN_P */
445 unsigned char *bufp
= (unsigned char *) buffer
+ word_offset
/ 8;
447 insert_1 (od
, value
, start
, length
, word_length
, bufp
);
450 #endif /* ! CGEN_INT_INSN_P */
455 /* Default insn parser.
457 The syntax string is scanned and operands are parsed and stored in FIELDS.
458 Relocs are queued as we go via other callbacks.
460 ??? Note that this is currently an all-or-nothing parser. If we fail to
461 parse the instruction, we return 0 and the caller will start over from
462 the beginning. Backtracking will be necessary in parsing subexpressions,
463 but that can be handled there. Not handling backtracking here may get
464 expensive in the case of the m68k. Deal with later.
466 Returns NULL for success, an error message for failure.
470 parse_insn_normal (od
, insn
, strp
, fields
)
472 const CGEN_INSN
* insn
;
474 CGEN_FIELDS
* fields
;
476 const CGEN_SYNTAX
* syntax
= CGEN_INSN_SYNTAX (insn
);
477 const char * str
= *strp
;
480 const unsigned char * syn
;
481 #ifdef CGEN_MNEMONIC_OPERANDS
486 /* For now we assume the mnemonic is first (there are no leading operands).
487 We can parse it without needing to set up operand parsing.
488 GAS's input scrubber will ensure mnemonics are lowercase, but we may
489 not be called from GAS. */
490 p
= CGEN_INSN_MNEMONIC (insn
);
491 while (*p
&& tolower (*p
) == tolower (*str
))
494 if (* p
|| (* str
&& !isspace (* str
)))
495 return _("unrecognized instruction");
497 CGEN_INIT_PARSE (od
);
498 cgen_init_parse_operand (od
);
499 #ifdef CGEN_MNEMONIC_OPERANDS
503 /* We don't check for (*str != '\0') here because we want to parse
504 any trailing fake arguments in the syntax string. */
505 syn
= CGEN_SYNTAX_STRING (syntax
);
507 /* Mnemonics come first for now, ensure valid string. */
508 if (! CGEN_SYNTAX_MNEMONIC_P (* syn
))
515 /* Non operand chars must match exactly. */
516 if (CGEN_SYNTAX_CHAR_P (* syn
))
518 if (*str
== CGEN_SYNTAX_CHAR (* syn
))
520 #ifdef CGEN_MNEMONIC_OPERANDS
529 /* Syntax char didn't match. Can't be this insn. */
530 /* FIXME: would like to return something like
531 "expected char `c'" */
532 return _("syntax error");
537 /* We have an operand of some sort. */
538 errmsg
= i960_cgen_parse_operand (od
, CGEN_SYNTAX_FIELD (*syn
),
543 /* Done with this operand, continue with next one. */
547 /* If we're at the end of the syntax string, we're done. */
550 /* FIXME: For the moment we assume a valid `str' can only contain
551 blanks now. IE: We needn't try again with a longer version of
552 the insn and it is assumed that longer versions of insns appear
553 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
554 while (isspace (* str
))
558 return _("junk at end of line"); /* FIXME: would like to include `str' */
563 /* We couldn't parse it. */
564 return _("unrecognized instruction");
567 /* Default insn builder (insert handler).
568 The instruction is recorded in CGEN_INT_INSN_P byte order
569 (meaning that if CGEN_INT_INSN_P BUFFER is an int * and thus the value is
570 recorded in host byte order, otherwise BUFFER is an array of bytes and the
571 value is recorded in target byte order).
572 The result is an error message or NULL if success. */
575 insert_insn_normal (od
, insn
, fields
, buffer
, pc
)
577 const CGEN_INSN
* insn
;
578 CGEN_FIELDS
* fields
;
579 CGEN_INSN_BYTES_PTR buffer
;
582 const CGEN_SYNTAX
* syntax
= CGEN_INSN_SYNTAX (insn
);
584 const unsigned char * syn
;
586 CGEN_INIT_INSERT (od
);
587 value
= CGEN_INSN_BASE_VALUE (insn
);
589 /* If we're recording insns as numbers (rather than a string of bytes),
590 target byte order handling is deferred until later. */
598 cgen_put_insn_value (od
, buffer
, min (CGEN_BASE_INSN_BITSIZE
,
599 CGEN_FIELDS_BITSIZE (fields
)),
602 #endif /* ! CGEN_INT_INSN_P */
604 /* ??? It would be better to scan the format's fields.
605 Still need to be able to insert a value based on the operand though;
606 e.g. storing a branch displacement that got resolved later.
607 Needs more thought first. */
609 for (syn
= CGEN_SYNTAX_STRING (syntax
); * syn
!= '\0'; ++ syn
)
613 if (CGEN_SYNTAX_CHAR_P (* syn
))
616 errmsg
= i960_cgen_insert_operand (od
, CGEN_SYNTAX_FIELD (*syn
),
626 This routine is called for each instruction to be assembled.
627 STR points to the insn to be assembled.
628 We assume all necessary tables have been initialized.
629 The assembled instruction, less any fixups, is stored in BUF.
630 Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
631 still needs to be converted to target byte order, otherwise BUF is an array
632 of bytes in target byte order.
633 The result is a pointer to the insn's entry in the opcode table,
634 or NULL if an error occured (an error message will have already been
637 Note that when processing (non-alias) macro-insns,
638 this function recurses. */
641 i960_cgen_assemble_insn (od
, str
, fields
, buf
, errmsg
)
644 CGEN_FIELDS
* fields
;
645 CGEN_INSN_BYTES_PTR buf
;
649 CGEN_INSN_LIST
* ilist
;
651 /* Skip leading white space. */
652 while (isspace (* str
))
655 /* The instructions are stored in hashed lists.
656 Get the first in the list. */
657 ilist
= CGEN_ASM_LOOKUP_INSN (od
, str
);
659 /* Keep looking until we find a match. */
662 for ( ; ilist
!= NULL
; ilist
= CGEN_ASM_NEXT_INSN (ilist
))
664 const CGEN_INSN
*insn
= ilist
->insn
;
666 #if 0 /* not needed as unsupported opcodes shouldn't be in the hash lists */
667 /* Is this insn supported by the selected cpu? */
668 if (! i960_cgen_insn_supported (od
, insn
))
672 /* If the RELAX attribute is set, this is an insn that shouldn't be
673 chosen immediately. Instead, it is used during assembler/linker
674 relaxation if possible. */
675 if (CGEN_INSN_ATTR (insn
, CGEN_INSN_RELAX
) != 0)
680 /* Allow parse/insert handlers to obtain length of insn. */
681 CGEN_FIELDS_BITSIZE (fields
) = CGEN_INSN_BITSIZE (insn
);
683 if (! CGEN_PARSE_FN (insn
) (od
, insn
, & str
, fields
))
685 /* ??? 0 is passed for `pc' */
686 if (CGEN_INSERT_FN (insn
) (od
, insn
, fields
, buf
, (bfd_vma
) 0) != NULL
)
688 /* It is up to the caller to actually output the insn and any
693 /* Try the next entry. */
696 /* FIXME: We can return a better error message than this.
697 Need to track why it failed and pick the right one. */
699 static char errbuf
[100];
700 if (strlen (start
) > 50)
701 /* xgettext:c-format */
702 sprintf (errbuf
, _("bad instruction `%.50s...'"), start
);
704 /* xgettext:c-format */
705 sprintf (errbuf
, _("bad instruction `%.50s'"), start
);
712 #if 0 /* This calls back to GAS which we can't do without care. */
714 /* Record each member of OPVALS in the assembler's symbol table.
715 This lets GAS parse registers for us.
716 ??? Interesting idea but not currently used. */
718 /* Record each member of OPVALS in the assembler's symbol table.
719 FIXME: Not currently used. */
722 i960_cgen_asm_hash_keywords (od
, opvals
)
724 CGEN_KEYWORD
* opvals
;
726 CGEN_KEYWORD_SEARCH search
= cgen_keyword_search_init (opvals
, NULL
);
727 const CGEN_KEYWORD_ENTRY
* ke
;
729 while ((ke
= cgen_keyword_search_next (& search
)) != NULL
)
731 #if 0 /* Unnecessary, should be done in the search routine. */
732 if (! i960_cgen_opval_supported (ke
))
735 cgen_asm_record_register (od
, ke
->name
, ke
->value
);