1 /* tc-v850.c -- Assembler code for the NEC V850
3 Copyright (C) 1996 Free Software Foundation.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "opcode/v850.h"
28 /* Structure to hold information about predefined registers. */
35 /* Generic assembler global variables which must be defined by all targets. */
37 /* Characters which always start a comment. */
38 const char comment_chars
[] = "#";
40 /* Characters which start a comment at the beginning of a line. */
41 const char line_comment_chars
[] = ";#";
43 /* Characters which may be used to separate multiple commands on a
45 const char line_separator_chars
[] = ";";
47 /* Characters which are used to indicate an exponent in a floating
49 const char EXP_CHARS
[] = "eE";
51 /* Characters which mean that a number is a floating point constant,
53 const char FLT_CHARS
[] = "dD";
56 static unsigned long v850_insert_operand
57 PARAMS ((unsigned long insn
, const struct v850_operand
*operand
,
58 offsetT val
, char *file
, unsigned int line
));
59 static int reg_name_search
PARAMS ((char *name
, const struct reg_name
*, int));
60 static boolean register_name
PARAMS ((expressionS
*expressionP
));
61 static boolean system_register_name
PARAMS ((expressionS
*expressionP
));
62 static boolean cc_name
PARAMS ((expressionS
*expressionP
));
63 static int postfix
PARAMS ((char *p
));
64 static bfd_reloc_code_real_type get_reloc
PARAMS ((struct v850_operand
*op
));
65 static unsigned long build_insn
PARAMS ((struct v850_opcode
*opcode
, expressionS
*opers
));
69 #define MAX_INSN_FIXUPS (5)
74 bfd_reloc_code_real_type reloc
;
76 struct v850_fixup fixups
[MAX_INSN_FIXUPS
];
79 const char *md_shortopts
= "";
80 struct option md_longopts
[] = {
81 {NULL
, no_argument
, NULL
, 0}
83 size_t md_longopts_size
= sizeof(md_longopts
);
85 /* The target specific pseudo-ops which we support. */
86 const pseudo_typeS md_pseudo_table
[] =
89 { "byte", ppc_byte, 0 },
90 { "long", ppc_elf_cons, 4 },
91 { "word", ppc_elf_cons, 2 },
92 { "short", ppc_elf_cons, 2 },
93 { "rdata", ppc_elf_rdata, 0 },
94 { "rodata", ppc_elf_rdata, 0 },
95 { "lcomm", ppc_elf_lcomm, 0 },
100 /* Opcode hash table. */
101 static struct hash_control
*v850_hash
;
103 /* This table is sorted. Suitable for searching by a binary search. */
104 static const struct reg_name pre_defined_registers
[] =
106 { "ep", 30 }, /* ep - element ptr */
107 { "gp", 4 }, /* gp - global ptr */
108 { "lp", 31 }, /* lp - link ptr */
141 { "sp", 3 }, /* sp - stack ptr */
142 { "tp", 5 }, /* tp - text ptr */
145 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct reg_name))
148 static const struct reg_name system_registers
[] =
157 #define SYSREG_NAME_CNT (sizeof(system_registers) / sizeof(struct reg_name))
159 static const struct reg_name cc_names
[] =
182 #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name))
184 /* reg_name_search does a binary search of the given register table
185 to see if "name" is a valid regiter name. Returns the register
186 number from the array on success, or -1 on failure. */
189 reg_name_search (name
, table
, high
)
191 const struct reg_name
*table
;
200 middle
= (low
+ high
) / 2;
201 cmp
= strcasecmp (name
, table
[middle
].name
);
207 return table
[middle
].value
;
214 /* Summary of register_name().
216 * in: Input_line_pointer points to 1st char of operand.
218 * out: A expressionS.
219 * The operand may have been a register: in this case, X_op == O_register,
220 * X_add_number is set to the register number, and truth is returned.
221 * Input_line_pointer->(next non-blank) char after operand, or is in
222 * its original state.
225 register_name (expressionP
)
226 expressionS
*expressionP
;
233 /* Find the spelling of the operand */
234 start
= name
= input_line_pointer
;
236 c
= get_symbol_end ();
237 reg_number
= reg_name_search (name
, pre_defined_registers
, REG_NAME_CNT
- 1);
239 /* look to see if it's in the register table */
242 expressionP
->X_op
= O_register
;
243 expressionP
->X_add_number
= reg_number
;
245 /* make the rest nice */
246 expressionP
->X_add_symbol
= NULL
;
247 expressionP
->X_op_symbol
= NULL
;
248 *input_line_pointer
= c
; /* put back the delimiting char */
253 /* reset the line as if we had not done anything */
254 *input_line_pointer
= c
; /* put back the delimiting char */
255 input_line_pointer
= start
; /* reset input_line pointer */
260 /* Summary of system_register_name().
262 * in: Input_line_pointer points to 1st char of operand.
264 * out: A expressionS.
265 * The operand may have been a register: in this case, X_op == O_register,
266 * X_add_number is set to the register number, and truth is returned.
267 * Input_line_pointer->(next non-blank) char after operand, or is in
268 * its original state.
271 system_register_name (expressionP
)
272 expressionS
*expressionP
;
279 /* Find the spelling of the operand */
280 start
= name
= input_line_pointer
;
282 c
= get_symbol_end ();
283 reg_number
= reg_name_search (name
, system_registers
, SYSREG_NAME_CNT
- 1);
285 /* look to see if it's in the register table */
288 expressionP
->X_op
= O_register
;
289 expressionP
->X_add_number
= reg_number
;
291 /* make the rest nice */
292 expressionP
->X_add_symbol
= NULL
;
293 expressionP
->X_op_symbol
= NULL
;
294 *input_line_pointer
= c
; /* put back the delimiting char */
299 /* reset the line as if we had not done anything */
300 *input_line_pointer
= c
; /* put back the delimiting char */
301 input_line_pointer
= start
; /* reset input_line pointer */
306 /* Summary of cc_name().
308 * in: Input_line_pointer points to 1st char of operand.
310 * out: A expressionS.
311 * The operand may have been a register: in this case, X_op == O_register,
312 * X_add_number is set to the register number, and truth is returned.
313 * Input_line_pointer->(next non-blank) char after operand, or is in
314 * its original state.
317 cc_name (expressionP
)
318 expressionS
*expressionP
;
325 /* Find the spelling of the operand */
326 start
= name
= input_line_pointer
;
328 c
= get_symbol_end ();
329 reg_number
= reg_name_search (name
, cc_names
, CC_NAME_CNT
- 1);
331 /* look to see if it's in the register table */
334 expressionP
->X_op
= O_constant
;
335 expressionP
->X_add_number
= reg_number
;
337 /* make the rest nice */
338 expressionP
->X_add_symbol
= NULL
;
339 expressionP
->X_op_symbol
= NULL
;
340 *input_line_pointer
= c
; /* put back the delimiting char */
345 /* reset the line as if we had not done anything */
346 *input_line_pointer
= c
; /* put back the delimiting char */
347 input_line_pointer
= start
; /* reset input_line pointer */
353 md_show_usage (stream
)
356 fprintf(stream
, "V850 options:\n\
361 md_parse_option (c
, arg
)
369 md_undefined_symbol (name
)
376 md_atof (type
, litp
, sizep
)
382 LITTLENUM_TYPE words
[4];
398 return "bad call to md_atof";
401 t
= atof_ieee (input_line_pointer
, type
, words
);
403 input_line_pointer
= t
;
407 for (i
= prec
- 1; i
>= 0; i
--)
409 md_number_to_chars (litp
, (valueT
) words
[i
], 2);
418 md_convert_frag (abfd
, sec
, fragP
)
423 /* printf ("call to md_convert_frag \n"); */
428 md_section_align (seg
, addr
)
432 int align
= bfd_get_section_alignment (stdoutput
, seg
);
433 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
439 char *prev_name
= "";
440 register const struct v850_opcode
*op
;
441 const struct v850_opcode
*op_end
;
443 v850_hash
= hash_new();
445 /* Insert unique names into hash table. The V850 instruction set
446 has many identical opcode names that have different opcodes based
447 on the operands. This hash table then provides a quick index to
448 the first opcode with a particular name in the opcode table. */
451 op_end
= v850_opcodes
+ v850_num_opcodes
;
453 for (; op
< op_end
; op
++)
455 if (strcmp (prev_name
, op
->name
))
457 prev_name
= (char *) op
->name
;
458 hash_insert (v850_hash
, op
->name
, (char *) op
);
464 static bfd_reloc_code_real_type
466 struct v850_operand
*op
;
477 struct v850_opcode
*opcode
;
478 struct v850_opcode
*next_opcode
;
479 const unsigned char *opindex_ptr
;
488 expressionS myops
[5];
490 /* Get the opcode. */
491 for (s
= str
; *s
!= '\0' && ! isspace (*s
); s
++)
496 /* find the first opcode with the proper name */
497 opcode
= (struct v850_opcode
*)hash_find (v850_hash
, str
);
500 as_bad ("Unrecognized opcode: `%s'", str
);
505 while (isspace (*str
))
508 input_line_pointer
= str
;
512 const char *errmsg
= NULL
;
517 insn
= opcode
->opcode
;
518 for (opindex_ptr
= opcode
->operands
; *opindex_ptr
!= 0; opindex_ptr
++)
520 const struct v850_operand
*operand
;
525 if (next_opindex
== 0)
527 operand
= &v850_operands
[*opindex_ptr
];
531 operand
= &v850_operands
[next_opindex
];
537 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
540 /* Gather the operand. */
541 hold
= input_line_pointer
;
542 input_line_pointer
= str
;
544 if ((operand
->flags
& V850_OPERAND_REG
) != 0)
546 if (!register_name(&ex
))
548 errmsg
= "invalid register name";
552 else if ((operand
->flags
& V850_OPERAND_SRG
) != 0)
554 if (!system_register_name(&ex
))
556 errmsg
= "invalid system register name";
560 else if ((operand
->flags
& V850_OPERAND_CC
) != 0)
564 errmsg
= "invalid condition code name";
568 else if (strncmp(input_line_pointer
, "lo(", 3) == 0)
570 input_line_pointer
+= 3;
573 if (*input_line_pointer
++ != ')')
575 errmsg
= "syntax error: expected `)'";
579 if (ex
.X_op
== O_constant
)
580 ex
.X_add_number
&= 0xffff;
583 if (fc
> MAX_INSN_FIXUPS
)
584 as_fatal ("too many fixups");
587 fixups
[fc
].opindex
= *opindex_ptr
;
588 fixups
[fc
].reloc
= BFD_RELOC_LO16
;
592 else if (strncmp(input_line_pointer
, "hi(", 3) == 0)
594 input_line_pointer
+= 3;
597 if (*input_line_pointer
++ != ')')
599 errmsg
= "syntax error: expected `)'";
603 if (ex
.X_op
== O_constant
)
604 ex
.X_add_number
= (ex
.X_add_number
>> 16) & 0xffff;
607 if (fc
> MAX_INSN_FIXUPS
)
608 as_fatal ("too many fixups");
611 fixups
[fc
].opindex
= *opindex_ptr
;
612 fixups
[fc
].reloc
= BFD_RELOC_HI16
;
616 else if (register_name (&ex
)
617 && (operand
->flags
& V850_OPERAND_REG
) == 0)
619 errmsg
= "syntax error: register not expected";
622 else if (system_register_name (&ex
)
623 && (operand
->flags
& V850_OPERAND_SRG
) == 0)
625 errmsg
= "syntax error: system register not expected";
628 else if (cc_name (&ex
)
629 && (operand
->flags
& V850_OPERAND_CC
) == 0)
631 errmsg
= "syntax error: condition code not expected";
639 str
= input_line_pointer
;
640 input_line_pointer
= hold
;
645 errmsg
= "illegal operand";
648 errmsg
= "missing operand";
651 if ((operand
->flags
& (V850_OPERAND_REG
| V850_OPERAND_SRG
)) == 0)
653 errmsg
= "invalid operand";
657 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
662 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
667 /* We need to generate a fixup for this expression. */
668 if (fc
>= MAX_INSN_FIXUPS
)
669 as_fatal ("too many fixups");
671 fixups
[fc
].opindex
= *opindex_ptr
;
672 fixups
[fc
].reloc
= BFD_RELOC_UNUSED
;
677 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
685 next_opcode
= opcode
+ 1;
686 if (next_opcode
->opcode
!= 0 && !strcmp(next_opcode
->name
, opcode
->name
))
688 opcode
= next_opcode
;
692 as_bad ("%s", errmsg
);
698 while (isspace (*str
))
702 as_bad ("junk at end of line: `%s'", str
);
704 input_line_pointer
= str
;
706 f
= frag_more (opcode
->size
);
707 md_number_to_chars (f
, insn
, opcode
->size
);
711 /* if while processing a fixup, a reloc really needs to be created */
712 /* then it is done here */
715 tc_gen_reloc (seg
, fixp
)
720 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
721 reloc
->sym_ptr_ptr
= &fixp
->fx_addsy
->bsym
;
722 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
723 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
724 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
726 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
727 "reloc %d not supported by object file format", (int)fixp
->fx_r_type
);
730 reloc
->addend
= fixp
->fx_addnumber
;
731 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
736 md_estimate_size_before_relax (fragp
, seg
)
745 md_pcrel_from_section (fixp
, sec
)
750 /* return fixp->fx_frag->fr_address + fixp->fx_where; */
754 md_apply_fix3 (fixp
, valuep
, seg
)
766 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
771 else if (fixp
->fx_pcrel
)
775 value
= fixp
->fx_offset
;
776 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
778 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
779 value
-= S_GET_VALUE (fixp
->fx_subsy
);
782 /* We don't actually support subtracting a symbol. */
783 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
784 "expression too complex");
789 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
791 op_type
= fixp
->fx_r_type
;
792 fixp
->fx_r_type
= get_reloc((struct v850_operand
*)&v850_operands
[op_type
]);
794 /* printf("reloc=%d\n",fixp->fx_r_type); */
796 /* Fetch the instruction, insert the fully resolved operand
797 value, and stuff the instruction back again. */
798 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
799 insn
= bfd_getb32 ((unsigned char *) where
);
800 /* printf(" insn=%x value=%x\n",insn,value); */
802 insn
= v850_insert_operand (insn
, op_type
, (offsetT
) value
);
804 /* printf(" new insn=%x\n",insn); */
806 bfd_putb32 ((bfd_vma
) insn
, (unsigned char *) where
);
811 fixp
->fx_addnumber
= value
;
817 /* Insert an operand value into an instruction. */
820 v850_insert_operand (insn
, operand
, val
, file
, line
)
822 const struct v850_operand
*operand
;
827 if (operand
->bits
!= 32)
832 if ((operand
->flags
& V850_OPERAND_SIGNED
) != 0)
835 if ((operand
->flags
& PPC_OPERAND_SIGNOPT
) != 0
836 && ppc_size
== PPC_OPCODE_32
)
837 max
= (1 << operand
->bits
) - 1;
840 max
= (1 << (operand
->bits
- 1)) - 1;
841 min
= - (1 << (operand
->bits
- 1));
845 max
= (1 << operand
->bits
) - 1;
850 if ((operand
->flags
& PPC_OPERAND_NEGATIVE
) != 0)
857 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)
860 "operand out of range (%s not between %ld and %ld)";
863 sprint_value (buf
, test
);
864 if (file
== (char *) NULL
)
865 as_warn (err
, buf
, min
, max
);
867 as_warn_where (file
, line
, err
, buf
, min
, max
);
871 insn
|= (((long) val
& ((1 << operand
->bits
) - 1)) << operand
->shift
);