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 ((const struct reg_name
*, int, const char *));
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 (regs
, regcount
, name
)
190 const struct reg_name
*regs
;
194 int middle
, low
, high
;
202 middle
= (low
+ high
) / 2;
203 cmp
= strcasecmp (name
, regs
[middle
].name
);
209 return regs
[middle
].value
;
216 /* Summary of register_name().
218 * in: Input_line_pointer points to 1st char of operand.
220 * out: A expressionS.
221 * The operand may have been a register: in this case, X_op == O_register,
222 * X_add_number is set to the register number, and truth is returned.
223 * Input_line_pointer->(next non-blank) char after operand, or is in
224 * its original state.
227 register_name (expressionP
)
228 expressionS
*expressionP
;
235 /* Find the spelling of the operand */
236 start
= name
= input_line_pointer
;
238 c
= get_symbol_end ();
239 reg_number
= reg_name_search (pre_defined_registers
, REG_NAME_CNT
, name
);
241 /* look to see if it's in the register table */
244 expressionP
->X_op
= O_register
;
245 expressionP
->X_add_number
= reg_number
;
247 /* make the rest nice */
248 expressionP
->X_add_symbol
= NULL
;
249 expressionP
->X_op_symbol
= NULL
;
250 *input_line_pointer
= c
; /* put back the delimiting char */
255 /* reset the line as if we had not done anything */
256 *input_line_pointer
= c
; /* put back the delimiting char */
257 input_line_pointer
= start
; /* reset input_line pointer */
262 /* Summary of system_register_name().
264 * in: Input_line_pointer points to 1st char of operand.
266 * out: A expressionS.
267 * The operand may have been a register: in this case, X_op == O_register,
268 * X_add_number is set to the register number, and truth is returned.
269 * Input_line_pointer->(next non-blank) char after operand, or is in
270 * its original state.
273 system_register_name (expressionP
)
274 expressionS
*expressionP
;
281 /* Find the spelling of the operand */
282 start
= name
= input_line_pointer
;
284 c
= get_symbol_end ();
285 reg_number
= reg_name_search (system_registers
, SYSREG_NAME_CNT
, name
);
287 /* look to see if it's in the register table */
290 expressionP
->X_op
= O_register
;
291 expressionP
->X_add_number
= reg_number
;
293 /* make the rest nice */
294 expressionP
->X_add_symbol
= NULL
;
295 expressionP
->X_op_symbol
= NULL
;
296 *input_line_pointer
= c
; /* put back the delimiting char */
301 /* reset the line as if we had not done anything */
302 *input_line_pointer
= c
; /* put back the delimiting char */
303 input_line_pointer
= start
; /* reset input_line pointer */
308 /* Summary of cc_name().
310 * in: Input_line_pointer points to 1st char of operand.
312 * out: A expressionS.
313 * The operand may have been a register: in this case, X_op == O_register,
314 * X_add_number is set to the register number, and truth is returned.
315 * Input_line_pointer->(next non-blank) char after operand, or is in
316 * its original state.
319 cc_name (expressionP
)
320 expressionS
*expressionP
;
327 /* Find the spelling of the operand */
328 start
= name
= input_line_pointer
;
330 c
= get_symbol_end ();
331 reg_number
= reg_name_search (cc_names
, CC_NAME_CNT
, name
);
333 /* look to see if it's in the register table */
336 expressionP
->X_op
= O_constant
;
337 expressionP
->X_add_number
= reg_number
;
339 /* make the rest nice */
340 expressionP
->X_add_symbol
= NULL
;
341 expressionP
->X_op_symbol
= NULL
;
342 *input_line_pointer
= c
; /* put back the delimiting char */
347 /* reset the line as if we had not done anything */
348 *input_line_pointer
= c
; /* put back the delimiting char */
349 input_line_pointer
= start
; /* reset input_line pointer */
355 md_show_usage (stream
)
358 fprintf(stream
, "V850 options:\n\
363 md_parse_option (c
, arg
)
371 md_undefined_symbol (name
)
378 md_atof (type
, litp
, sizep
)
384 LITTLENUM_TYPE words
[4];
400 return "bad call to md_atof";
403 t
= atof_ieee (input_line_pointer
, type
, words
);
405 input_line_pointer
= t
;
409 for (i
= prec
- 1; i
>= 0; i
--)
411 md_number_to_chars (litp
, (valueT
) words
[i
], 2);
420 md_convert_frag (abfd
, sec
, fragP
)
425 /* printf ("call to md_convert_frag \n"); */
430 md_section_align (seg
, addr
)
434 int align
= bfd_get_section_alignment (stdoutput
, seg
);
435 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
441 char *prev_name
= "";
442 register const struct v850_opcode
*op
;
443 const struct v850_opcode
*op_end
;
445 v850_hash
= hash_new();
447 /* Insert unique names into hash table. The V850 instruction set
448 has many identical opcode names that have different opcodes based
449 on the operands. This hash table then provides a quick index to
450 the first opcode with a particular name in the opcode table. */
455 if (strcmp (prev_name
, op
->name
))
457 prev_name
= (char *) op
->name
;
458 hash_insert (v850_hash
, op
->name
, (char *) op
);
465 static bfd_reloc_code_real_type
468 if (strncmp(input_line_pointer
, "hi0(", 4) == 0)
470 input_line_pointer
+= 4;
471 return BFD_RELOC_HI16
;
473 if (strncmp(input_line_pointer
, "hi(", 3) == 0)
475 input_line_pointer
+= 3;
476 return BFD_RELOC_HI16_S
;
478 if (strncmp (input_line_pointer
, "lo(", 3) == 0)
480 input_line_pointer
+= 3;
481 return BFD_RELOC_LO16
;
484 /* FIXME: implement sda, tda, zda here */
486 return BFD_RELOC_UNUSED
;
494 struct v850_opcode
*opcode
;
495 struct v850_opcode
*next_opcode
;
496 const unsigned char *opindex_ptr
;
503 bfd_reloc_code_real_type reloc
;
506 expressionS myops
[5];
508 /* Get the opcode. */
509 for (s
= str
; *s
!= '\0' && ! isspace (*s
); s
++)
514 /* find the first opcode with the proper name */
515 opcode
= (struct v850_opcode
*)hash_find (v850_hash
, str
);
518 as_bad ("Unrecognized opcode: `%s'", str
);
523 while (isspace (*str
))
526 input_line_pointer
= str
;
530 const char *errmsg
= NULL
;
535 insn
= opcode
->opcode
;
536 for (opindex_ptr
= opcode
->operands
; *opindex_ptr
!= 0; opindex_ptr
++)
538 const struct v850_operand
*operand
;
543 if (next_opindex
== 0)
545 operand
= &v850_operands
[*opindex_ptr
];
549 operand
= &v850_operands
[next_opindex
];
555 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
558 /* Gather the operand. */
559 hold
= input_line_pointer
;
560 input_line_pointer
= str
;
563 /* lo(), hi(), hi0(), etc... */
564 if ((reloc
= v850_reloc_prefix()) != BFD_RELOC_UNUSED
)
568 if (*input_line_pointer
++ != ')')
570 errmsg
= "syntax error: expected `)'";
574 if (ex
.X_op
== O_constant
)
579 ex
.X_add_number
&= 0xffff;
583 ex
.X_add_number
= ((ex
.X_add_number
>> 16) & 0xffff);
586 case BFD_RELOC_HI16_S
:
587 ex
.X_add_number
= ((ex
.X_add_number
>> 16) & 0xffff)
588 + ((ex
.X_add_number
>> 15) & 1);
592 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
597 if (fc
> MAX_INSN_FIXUPS
)
598 as_fatal ("too many fixups");
601 fixups
[fc
].opindex
= *opindex_ptr
;
602 fixups
[fc
].reloc
= reloc
;
608 if ((operand
->flags
& V850_OPERAND_REG
) != 0)
610 if (!register_name(&ex
))
612 errmsg
= "invalid register name";
616 else if ((operand
->flags
& V850_OPERAND_SRG
) != 0)
618 if (!system_register_name(&ex
))
620 errmsg
= "invalid system register name";
624 else if ((operand
->flags
& V850_OPERAND_EP
) != 0)
626 char *start
= input_line_pointer
;
627 char c
= get_symbol_end ();
628 if (strcmp (start
, "ep") != 0 && strcmp (start
, "r30") != 0)
630 /* Put things back the way we found them. */
631 *input_line_pointer
= c
;
632 input_line_pointer
= start
;
633 errmsg
= "expected EP register";
636 *input_line_pointer
= c
;
637 str
= input_line_pointer
;
638 input_line_pointer
= hold
;
640 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
644 else if ((operand
->flags
& V850_OPERAND_CC
) != 0)
648 errmsg
= "invalid condition code name";
652 else if (register_name (&ex
)
653 && (operand
->flags
& V850_OPERAND_REG
) == 0)
655 errmsg
= "syntax error: register not expected";
658 else if (system_register_name (&ex
)
659 && (operand
->flags
& V850_OPERAND_SRG
) == 0)
661 errmsg
= "syntax error: system register not expected";
664 else if (cc_name (&ex
)
665 && (operand
->flags
& V850_OPERAND_CC
) == 0)
667 errmsg
= "syntax error: condition code not expected";
678 errmsg
= "illegal operand";
681 errmsg
= "missing operand";
684 if ((operand
->flags
& (V850_OPERAND_REG
| V850_OPERAND_SRG
)) == 0)
686 errmsg
= "invalid operand";
690 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
695 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
700 /* We need to generate a fixup for this expression. */
701 if (fc
>= MAX_INSN_FIXUPS
)
702 as_fatal ("too many fixups");
704 fixups
[fc
].opindex
= *opindex_ptr
;
705 fixups
[fc
].reloc
= BFD_RELOC_UNUSED
;
712 str
= input_line_pointer
;
713 input_line_pointer
= hold
;
715 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
723 next_opcode
= opcode
+ 1;
724 if (next_opcode
->opcode
!= 0 && !strcmp(next_opcode
->name
, opcode
->name
))
726 opcode
= next_opcode
;
730 as_bad ("%s", errmsg
);
736 while (isspace (*str
))
740 as_bad ("junk at end of line: `%s'", str
);
742 input_line_pointer
= str
;
744 /* Write out the instruction
745 FIXME: we can determine the size of the opcode by the instruction
746 pattern, it does not have to be stored in the opcode table. */
747 f
= frag_more (opcode
->size
);
748 md_number_to_chars (f
, insn
, opcode
->size
);
751 /* Create any fixups. At this point we do not use a
752 bfd_reloc_code_real_type, but instead just use the
753 BFD_RELOC_UNUSED plus the operand index. This lets us easily
754 handle fixups for any operand type, although that is admittedly
755 not a very exciting feature. We pick a BFD reloc type in
757 for (i
= 0; i
< fc
; i
++)
759 const struct v850_operand
*operand
;
761 operand
= &v850_operands
[fixups
[i
].opindex
];
762 if (fixups
[i
].reloc
!= BFD_RELOC_UNUSED
)
764 reloc_howto_type
*reloc_howto
= bfd_reloc_type_lookup (stdoutput
, fixups
[i
].reloc
);
772 size
= bfd_get_reloc_size (reloc_howto
);
775 if (size
< 1 || size
> 4)
778 fixP
= fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ offset
, size
,
780 reloc_howto
->pc_relative
,
785 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
, 4,
787 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
788 ((bfd_reloc_code_real_type
)
789 (fixups
[i
].opindex
+ (int) BFD_RELOC_UNUSED
)));
795 /* if while processing a fixup, a reloc really needs to be created */
796 /* then it is done here */
799 tc_gen_reloc (seg
, fixp
)
804 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
805 reloc
->sym_ptr_ptr
= &fixp
->fx_addsy
->bsym
;
806 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
807 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
808 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
810 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
811 "reloc %d not supported by object file format", (int)fixp
->fx_r_type
);
814 reloc
->addend
= fixp
->fx_addnumber
;
815 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
820 md_estimate_size_before_relax (fragp
, seg
)
831 if (fixp
->fx_addsy
!= (symbolS
*) NULL
&& ! S_IS_DEFINED (fixp
->fx_addsy
))
833 /* The symbol is undefined. Let the linker figure it out. */
836 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
840 md_apply_fix3 (fixp
, valuep
, seg
)
850 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
855 else if (fixp
->fx_pcrel
)
859 value
= fixp
->fx_offset
;
860 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
862 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
863 value
-= S_GET_VALUE (fixp
->fx_subsy
);
866 /* We don't actually support subtracting a symbol. */
867 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
868 "expression too complex");
873 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
875 if ((int) fixp
->fx_r_type
>= (int) BFD_RELOC_UNUSED
)
878 const struct v850_operand
*operand
;
882 opindex
= (int) fixp
->fx_r_type
- (int) BFD_RELOC_UNUSED
;
883 operand
= &v850_operands
[opindex
];
885 /* Fetch the instruction, insert the fully resolved operand
886 value, and stuff the instruction back again.
888 Note the instruction has been stored in little endian
890 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
892 insn
= bfd_getl32((unsigned char *) where
);
893 insn
= v850_insert_operand (insn
, operand
, (offsetT
) value
,
894 fixp
->fx_file
, fixp
->fx_line
);
895 bfd_putl32((bfd_vma
) insn
, (unsigned char *) where
);
899 /* Nothing else to do here. */
903 /* Determine a BFD reloc value based on the operand information.
904 We are only prepared to turn a few of the operands into relocs. */
906 if (operand
->bits
== 22)
907 fixp
->fx_r_type
= BFD_RELOC_V850_22_PCREL
;
908 else if (operand
->bits
== 9)
909 fixp
->fx_r_type
= BFD_RELOC_V850_9_PCREL
;
912 as_bad_where(fixp
->fx_file
, fixp
->fx_line
,
913 "unresolved expression that must be resolved");
919 fixp
->fx_addnumber
= value
;
924 /* Insert an operand value into an instruction. */
927 v850_insert_operand (insn
, operand
, val
, file
, line
)
929 const struct v850_operand
*operand
;
934 if (operand
->bits
!= 16)
939 if ((operand
->flags
& V850_OPERAND_SIGNED
) != 0)
942 if ((operand
->flags
& PPC_OPERAND_SIGNOPT
) != 0
943 && ppc_size
== PPC_OPCODE_32
)
944 max
= (1 << operand
->bits
) - 1;
947 max
= (1 << (operand
->bits
- 1)) - 1;
948 min
= - (1 << (operand
->bits
- 1));
952 max
= (1 << operand
->bits
) - 1;
957 if ((operand
->flags
& PPC_OPERAND_NEGATIVE
) != 0)
964 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)
967 "operand out of range (%s not between %ld and %ld)";
970 sprint_value (buf
, test
);
971 if (file
== (char *) NULL
)
972 as_warn (err
, buf
, min
, max
);
974 as_warn_where (file
, line
, err
, buf
, min
, max
);
980 const char *message
= NULL
;
981 insn
= (operand
->insert
) (insn
, val
, &message
);
984 if (file
== (char *) NULL
)
987 as_warn_where (file
, line
, message
);
991 insn
|= (((long) val
& ((1 << operand
->bits
) - 1)) << operand
->shift
);