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 int postfix
PARAMS ((char *p
));
63 static bfd_reloc_code_real_type get_reloc
PARAMS ((struct v850_operand
*op
));
64 static unsigned long build_insn
PARAMS ((struct v850_opcode
*opcode
, expressionS
*opers
));
68 #define MAX_INSN_FIXUPS (5)
73 bfd_reloc_code_real_type reloc
;
75 struct v850_fixup fixups
[MAX_INSN_FIXUPS
];
78 const char *md_shortopts
= "";
79 struct option md_longopts
[] = {
80 {NULL
, no_argument
, NULL
, 0}
82 size_t md_longopts_size
= sizeof(md_longopts
);
84 /* The target specific pseudo-ops which we support. */
85 const pseudo_typeS md_pseudo_table
[] =
88 { "byte", ppc_byte, 0 },
89 { "long", ppc_elf_cons, 4 },
90 { "word", ppc_elf_cons, 2 },
91 { "short", ppc_elf_cons, 2 },
92 { "rdata", ppc_elf_rdata, 0 },
93 { "rodata", ppc_elf_rdata, 0 },
94 { "lcomm", ppc_elf_lcomm, 0 },
99 /* Opcode hash table. */
100 static struct hash_control
*v850_hash
;
102 /* This table is sorted. Suitable for searching by a binary search. */
103 static const struct reg_name pre_defined_registers
[] =
105 { "ep", 30 }, /* ep - element ptr */
106 { "gp", 4 }, /* gp - global ptr */
107 { "lp", 31 }, /* lp - link ptr */
140 { "sp", 3 }, /* sp - stack ptr */
141 { "tp", 5 }, /* tp - text ptr */
144 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct reg_name))
147 static const struct reg_name system_registers
[] =
156 #define SYSREG_NAME_CNT (sizeof(system_registers) / sizeof(struct reg_name))
158 static const struct reg_name cc_names
[] =
182 /* reg_name_search does a binary search of the given register table
183 to see if "name" is a valid regiter name. Returns the register
184 number from the array on success, or -1 on failure. */
187 reg_name_search (name
, table
, high
)
189 const struct reg_name
*table
;
198 middle
= (low
+ high
) / 2;
199 cmp
= strcasecmp (name
, table
[middle
].name
);
205 return table
[middle
].value
;
212 /* Summary of register_name().
214 * in: Input_line_pointer points to 1st char of operand.
216 * out: A expressionS.
217 * The operand may have been a register: in this case, X_op == O_register,
218 * X_add_number is set to the register number, and truth is returned.
219 * Input_line_pointer->(next non-blank) char after operand, or is in
220 * its original state.
223 register_name (expressionP
)
224 expressionS
*expressionP
;
231 /* Find the spelling of the operand */
232 start
= name
= input_line_pointer
;
234 c
= get_symbol_end ();
235 reg_number
= reg_name_search (name
, pre_defined_registers
, REG_NAME_CNT
- 1);
237 /* look to see if it's in the register table */
240 expressionP
->X_op
= O_register
;
241 expressionP
->X_add_number
= reg_number
;
243 /* make the rest nice */
244 expressionP
->X_add_symbol
= NULL
;
245 expressionP
->X_op_symbol
= NULL
;
246 *input_line_pointer
= c
; /* put back the delimiting char */
251 /* reset the line as if we had not done anything */
252 *input_line_pointer
= c
; /* put back the delimiting char */
253 input_line_pointer
= start
; /* reset input_line pointer */
258 /* Summary of system_register_name().
260 * in: Input_line_pointer points to 1st char of operand.
262 * out: A expressionS.
263 * The operand may have been a register: in this case, X_op == O_register,
264 * X_add_number is set to the register number, and truth is returned.
265 * Input_line_pointer->(next non-blank) char after operand, or is in
266 * its original state.
269 system_register_name (expressionP
)
270 expressionS
*expressionP
;
277 /* Find the spelling of the operand */
278 start
= name
= input_line_pointer
;
280 c
= get_symbol_end ();
281 reg_number
= reg_name_search (name
, system_registers
, SYSREG_NAME_CNT
- 1);
283 /* look to see if it's in the register table */
286 expressionP
->X_op
= O_register
;
287 expressionP
->X_add_number
= reg_number
;
289 /* make the rest nice */
290 expressionP
->X_add_symbol
= NULL
;
291 expressionP
->X_op_symbol
= NULL
;
292 *input_line_pointer
= c
; /* put back the delimiting char */
297 /* reset the line as if we had not done anything */
298 *input_line_pointer
= c
; /* put back the delimiting char */
299 input_line_pointer
= start
; /* reset input_line pointer */
305 md_show_usage (stream
)
308 fprintf(stream
, "V850 options:\n\
313 md_parse_option (c
, arg
)
321 md_undefined_symbol (name
)
328 md_atof (type
, litp
, sizep
)
334 LITTLENUM_TYPE words
[4];
350 return "bad call to md_atof";
353 t
= atof_ieee (input_line_pointer
, type
, words
);
355 input_line_pointer
= t
;
359 for (i
= prec
- 1; i
>= 0; i
--)
361 md_number_to_chars (litp
, (valueT
) words
[i
], 2);
370 md_convert_frag (abfd
, sec
, fragP
)
375 /* printf ("call to md_convert_frag \n"); */
380 md_section_align (seg
, addr
)
384 int align
= bfd_get_section_alignment (stdoutput
, seg
);
385 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
391 char *prev_name
= "";
392 register const struct v850_opcode
*op
;
393 const struct v850_opcode
*op_end
;
395 v850_hash
= hash_new();
397 /* Insert unique names into hash table. The V850 instruction set
398 has many identical opcode names that have different opcodes based
399 on the operands. This hash table then provides a quick index to
400 the first opcode with a particular name in the opcode table. */
403 op_end
= v850_opcodes
+ v850_num_opcodes
;
405 for (; op
< op_end
; op
++)
407 if (strcmp (prev_name
, op
->name
))
409 prev_name
= (char *) op
->name
;
410 hash_insert (v850_hash
, op
->name
, (char *) op
);
416 static bfd_reloc_code_real_type
418 struct v850_operand
*op
;
429 struct v850_opcode
*opcode
;
430 struct v850_opcode
*next_opcode
;
431 const unsigned char *opindex_ptr
;
440 expressionS myops
[5];
442 /* Get the opcode. */
443 for (s
= str
; *s
!= '\0' && ! isspace (*s
); s
++)
448 /* find the first opcode with the proper name */
449 opcode
= (struct v850_opcode
*)hash_find (v850_hash
, str
);
452 as_bad ("Unrecognized opcode: `%s'", str
);
457 while (isspace (*str
))
460 input_line_pointer
= str
;
464 const char *errmsg
= NULL
;
469 insn
= opcode
->opcode
;
470 for (opindex_ptr
= opcode
->operands
; *opindex_ptr
!= 0; opindex_ptr
++)
472 const struct v850_operand
*operand
;
477 if (next_opindex
== 0)
479 operand
= &v850_operands
[*opindex_ptr
];
483 operand
= &v850_operands
[next_opindex
];
489 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
492 /* Gather the operand. */
493 hold
= input_line_pointer
;
494 input_line_pointer
= str
;
496 if ((operand
->flags
& V850_OPERAND_REG
) != 0)
498 if (!register_name(&ex
))
500 errmsg
= "invalid register name";
504 else if ((operand
->flags
& V850_OPERAND_SRG
) != 0)
506 if (!system_register_name(&ex
))
508 errmsg
= "invalid system register name";
512 else if (strncmp(input_line_pointer
, "lo(", 3) == 0)
514 input_line_pointer
+= 3;
517 if (*input_line_pointer
++ != ')')
519 errmsg
= "syntax error: expected `)'";
523 if (ex
.X_op
== O_constant
)
524 ex
.X_add_number
&= 0xffff;
527 if (fc
> MAX_INSN_FIXUPS
)
528 as_fatal ("too many fixups");
531 fixups
[fc
].opindex
= *opindex_ptr
;
532 fixups
[fc
].reloc
= BFD_RELOC_LO16
;
536 else if (strncmp(input_line_pointer
, "hi(", 3) == 0)
538 input_line_pointer
+= 3;
541 if (*input_line_pointer
++ != ')')
543 errmsg
= "syntax error: expected `)'";
547 if (ex
.X_op
== O_constant
)
548 ex
.X_add_number
= (ex
.X_add_number
>> 16) & 0xffff;
551 if (fc
> MAX_INSN_FIXUPS
)
552 as_fatal ("too many fixups");
555 fixups
[fc
].opindex
= *opindex_ptr
;
556 fixups
[fc
].reloc
= BFD_RELOC_HI16
;
560 else if (register_name (&ex
)
561 && (operand
->flags
& V850_OPERAND_REG
) == 0)
563 errmsg
= "syntax error: register not expected";
566 else if (system_register_name (&ex
)
567 && (operand
->flags
& V850_OPERAND_SRG
) == 0)
569 errmsg
= "syntax error: system register not expected";
577 str
= input_line_pointer
;
578 input_line_pointer
= hold
;
583 errmsg
= "illegal operand";
586 errmsg
= "missing operand";
589 if ((operand
->flags
& (V850_OPERAND_REG
| V850_OPERAND_SRG
)) == 0)
591 errmsg
= "invalid operand";
595 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
600 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
605 /* We need to generate a fixup for this expression. */
606 if (fc
>= MAX_INSN_FIXUPS
)
607 as_fatal ("too many fixups");
609 fixups
[fc
].opindex
= *opindex_ptr
;
610 fixups
[fc
].reloc
= BFD_RELOC_UNUSED
;
615 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
623 next_opcode
= opcode
+ 1;
624 if (next_opcode
->opcode
!= 0 && !strcmp(next_opcode
->name
, opcode
->name
))
626 opcode
= next_opcode
;
630 as_bad ("%s", errmsg
);
636 while (isspace (*str
))
640 as_bad ("junk at end of line: `%s'", str
);
642 input_line_pointer
= str
;
644 f
= frag_more (opcode
->size
);
645 md_number_to_chars (f
, insn
, opcode
->size
);
649 /* if while processing a fixup, a reloc really needs to be created */
650 /* then it is done here */
653 tc_gen_reloc (seg
, fixp
)
658 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
659 reloc
->sym_ptr_ptr
= &fixp
->fx_addsy
->bsym
;
660 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
661 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
662 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
664 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
665 "reloc %d not supported by object file format", (int)fixp
->fx_r_type
);
668 reloc
->addend
= fixp
->fx_addnumber
;
669 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
674 md_estimate_size_before_relax (fragp
, seg
)
683 md_pcrel_from_section (fixp
, sec
)
688 /* return fixp->fx_frag->fr_address + fixp->fx_where; */
692 md_apply_fix3 (fixp
, valuep
, seg
)
704 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
709 else if (fixp
->fx_pcrel
)
713 value
= fixp
->fx_offset
;
714 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
716 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
717 value
-= S_GET_VALUE (fixp
->fx_subsy
);
720 /* We don't actually support subtracting a symbol. */
721 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
722 "expression too complex");
727 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
729 op_type
= fixp
->fx_r_type
;
730 fixp
->fx_r_type
= get_reloc((struct v850_operand
*)&v850_operands
[op_type
]);
732 /* printf("reloc=%d\n",fixp->fx_r_type); */
734 /* Fetch the instruction, insert the fully resolved operand
735 value, and stuff the instruction back again. */
736 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
737 insn
= bfd_getb32 ((unsigned char *) where
);
738 /* printf(" insn=%x value=%x\n",insn,value); */
740 insn
= v850_insert_operand (insn
, op_type
, (offsetT
) value
);
742 /* printf(" new insn=%x\n",insn); */
744 bfd_putb32 ((bfd_vma
) insn
, (unsigned char *) where
);
749 fixp
->fx_addnumber
= value
;
755 /* Insert an operand value into an instruction. */
758 v850_insert_operand (insn
, operand
, val
, file
, line
)
760 const struct v850_operand
*operand
;
765 if (operand
->bits
!= 32)
770 if ((operand
->flags
& V850_OPERAND_SIGNED
) != 0)
773 if ((operand
->flags
& PPC_OPERAND_SIGNOPT
) != 0
774 && ppc_size
== PPC_OPCODE_32
)
775 max
= (1 << operand
->bits
) - 1;
778 max
= (1 << (operand
->bits
- 1)) - 1;
779 min
= - (1 << (operand
->bits
- 1));
783 max
= (1 << operand
->bits
) - 1;
788 if ((operand
->flags
& PPC_OPERAND_NEGATIVE
) != 0)
795 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)
798 "operand out of range (%s not between %ld and %ld)";
801 sprint_value (buf
, test
);
802 if (file
== (char *) NULL
)
803 as_warn (err
, buf
, min
, max
);
805 as_warn_where (file
, line
, err
, buf
, min
, max
);
809 insn
|= (((long) val
& ((1 << operand
->bits
) - 1)) << operand
->shift
);