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 /* Temporarily holds the reloc in a cons expression. */
29 static bfd_reloc_code_real_type hold_cons_reloc
;
31 /* Structure to hold information about predefined registers. */
38 /* Generic assembler global variables which must be defined by all targets. */
40 /* Characters which always start a comment. */
41 const char comment_chars
[] = "#";
43 /* Characters which start a comment at the beginning of a line. */
44 const char line_comment_chars
[] = ";#";
46 /* Characters which may be used to separate multiple commands on a
48 const char line_separator_chars
[] = ";";
50 /* Characters which are used to indicate an exponent in a floating
52 const char EXP_CHARS
[] = "eE";
54 /* Characters which mean that a number is a floating point constant,
56 const char FLT_CHARS
[] = "dD";
60 static unsigned long v850_insert_operand
61 PARAMS ((unsigned long insn
, const struct v850_operand
*operand
,
62 offsetT val
, char *file
, unsigned int line
));
63 static int reg_name_search
PARAMS ((const struct reg_name
*, int, const char *));
64 static boolean register_name
PARAMS ((expressionS
*expressionP
));
65 static boolean system_register_name
PARAMS ((expressionS
*expressionP
));
66 static boolean cc_name
PARAMS ((expressionS
*expressionP
));
67 static bfd_reloc_code_real_type v850_reloc_prefix
PARAMS ((void));
71 #define MAX_INSN_FIXUPS (5)
76 bfd_reloc_code_real_type reloc
;
78 struct v850_fixup fixups
[MAX_INSN_FIXUPS
];
81 const char *md_shortopts
= "";
82 struct option md_longopts
[] = {
83 {NULL
, no_argument
, NULL
, 0}
85 size_t md_longopts_size
= sizeof(md_longopts
);
87 /* The target specific pseudo-ops which we support. */
88 const pseudo_typeS md_pseudo_table
[] =
94 /* Opcode hash table. */
95 static struct hash_control
*v850_hash
;
97 /* This table is sorted. Suitable for searching by a binary search. */
98 static const struct reg_name pre_defined_registers
[] =
100 { "ep", 30 }, /* ep - element ptr */
101 { "gp", 4 }, /* gp - global ptr */
102 { "lp", 31 }, /* lp - link ptr */
135 { "sp", 3 }, /* sp - stack ptr */
136 { "tp", 5 }, /* tp - text ptr */
139 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct reg_name))
142 static const struct reg_name system_registers
[] =
151 #define SYSREG_NAME_CNT (sizeof(system_registers) / sizeof(struct reg_name))
153 static const struct reg_name cc_names
[] =
176 #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name))
178 /* reg_name_search does a binary search of the given register table
179 to see if "name" is a valid regiter name. Returns the register
180 number from the array on success, or -1 on failure. */
183 reg_name_search (regs
, regcount
, name
)
184 const struct reg_name
*regs
;
188 int middle
, low
, high
;
196 middle
= (low
+ high
) / 2;
197 cmp
= strcasecmp (name
, regs
[middle
].name
);
203 return regs
[middle
].value
;
210 /* Summary of register_name().
212 * in: Input_line_pointer points to 1st char of operand.
214 * out: A expressionS.
215 * The operand may have been a register: in this case, X_op == O_register,
216 * X_add_number is set to the register number, and truth is returned.
217 * Input_line_pointer->(next non-blank) char after operand, or is in
218 * its original state.
221 register_name (expressionP
)
222 expressionS
*expressionP
;
229 /* Find the spelling of the operand */
230 start
= name
= input_line_pointer
;
232 c
= get_symbol_end ();
233 reg_number
= reg_name_search (pre_defined_registers
, REG_NAME_CNT
, name
);
235 /* look to see if it's in the register table */
238 expressionP
->X_op
= O_register
;
239 expressionP
->X_add_number
= reg_number
;
241 /* make the rest nice */
242 expressionP
->X_add_symbol
= NULL
;
243 expressionP
->X_op_symbol
= NULL
;
244 *input_line_pointer
= c
; /* put back the delimiting char */
249 /* reset the line as if we had not done anything */
250 *input_line_pointer
= c
; /* put back the delimiting char */
251 input_line_pointer
= start
; /* reset input_line pointer */
256 /* Summary of system_register_name().
258 * in: Input_line_pointer points to 1st char of operand.
260 * out: A expressionS.
261 * The operand may have been a register: in this case, X_op == O_register,
262 * X_add_number is set to the register number, and truth is returned.
263 * Input_line_pointer->(next non-blank) char after operand, or is in
264 * its original state.
267 system_register_name (expressionP
)
268 expressionS
*expressionP
;
275 /* Find the spelling of the operand */
276 start
= name
= input_line_pointer
;
278 c
= get_symbol_end ();
279 reg_number
= reg_name_search (system_registers
, SYSREG_NAME_CNT
, name
);
281 /* look to see if it's in the register table */
284 expressionP
->X_op
= O_register
;
285 expressionP
->X_add_number
= reg_number
;
287 /* make the rest nice */
288 expressionP
->X_add_symbol
= NULL
;
289 expressionP
->X_op_symbol
= NULL
;
290 *input_line_pointer
= c
; /* put back the delimiting char */
295 /* reset the line as if we had not done anything */
296 *input_line_pointer
= c
; /* put back the delimiting char */
297 input_line_pointer
= start
; /* reset input_line pointer */
302 /* Summary of cc_name().
304 * in: Input_line_pointer points to 1st char of operand.
306 * out: A expressionS.
307 * The operand may have been a register: in this case, X_op == O_register,
308 * X_add_number is set to the register number, and truth is returned.
309 * Input_line_pointer->(next non-blank) char after operand, or is in
310 * its original state.
313 cc_name (expressionP
)
314 expressionS
*expressionP
;
321 /* Find the spelling of the operand */
322 start
= name
= input_line_pointer
;
324 c
= get_symbol_end ();
325 reg_number
= reg_name_search (cc_names
, CC_NAME_CNT
, name
);
327 /* look to see if it's in the register table */
330 expressionP
->X_op
= O_constant
;
331 expressionP
->X_add_number
= reg_number
;
333 /* make the rest nice */
334 expressionP
->X_add_symbol
= NULL
;
335 expressionP
->X_op_symbol
= NULL
;
336 *input_line_pointer
= c
; /* put back the delimiting char */
341 /* reset the line as if we had not done anything */
342 *input_line_pointer
= c
; /* put back the delimiting char */
343 input_line_pointer
= start
; /* reset input_line pointer */
349 md_show_usage (stream
)
352 fprintf(stream
, "V850 options:\n\
357 md_parse_option (c
, arg
)
365 md_undefined_symbol (name
)
372 md_atof (type
, litp
, sizep
)
378 LITTLENUM_TYPE words
[4];
394 return "bad call to md_atof";
397 t
= atof_ieee (input_line_pointer
, type
, words
);
399 input_line_pointer
= t
;
403 for (i
= prec
- 1; i
>= 0; i
--)
405 md_number_to_chars (litp
, (valueT
) words
[i
], 2);
414 md_convert_frag (abfd
, sec
, fragP
)
419 /* printf ("call to md_convert_frag \n"); */
424 md_section_align (seg
, addr
)
428 int align
= bfd_get_section_alignment (stdoutput
, seg
);
429 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
435 char *prev_name
= "";
436 register const struct v850_opcode
*op
;
438 v850_hash
= hash_new();
440 /* Insert unique names into hash table. The V850 instruction set
441 has many identical opcode names that have different opcodes based
442 on the operands. This hash table then provides a quick index to
443 the first opcode with a particular name in the opcode table. */
448 if (strcmp (prev_name
, op
->name
))
450 prev_name
= (char *) op
->name
;
451 hash_insert (v850_hash
, op
->name
, (char *) op
);
457 static bfd_reloc_code_real_type
460 if (strncmp(input_line_pointer
, "hi0(", 4) == 0)
462 input_line_pointer
+= 3;
463 return BFD_RELOC_HI16
;
465 if (strncmp(input_line_pointer
, "hi(", 3) == 0)
467 input_line_pointer
+= 2;
468 return BFD_RELOC_HI16_S
;
470 if (strncmp (input_line_pointer
, "lo(", 3) == 0)
472 input_line_pointer
+= 2;
473 return BFD_RELOC_LO16
;
476 if (strncmp (input_line_pointer
, "sdaoff(", 7) == 0)
478 input_line_pointer
+= 6;
479 return BFD_RELOC_V850_SDA_OFFSET
;
482 if (strncmp (input_line_pointer
, "zdaoff(", 7) == 0)
484 input_line_pointer
+= 6;
485 return BFD_RELOC_V850_ZDA_OFFSET
;
488 if (strncmp (input_line_pointer
, "tdaoff(", 7) == 0)
490 input_line_pointer
+= 6;
491 return BFD_RELOC_V850_TDA_OFFSET
;
495 if (strncmp(input_line_pointer
, "(hi0(", 5) == 0)
497 input_line_pointer
+= 4;
498 return BFD_RELOC_HI16
;
500 if (strncmp(input_line_pointer
, "(hi(", 4) == 0)
502 input_line_pointer
+= 3;
503 return BFD_RELOC_HI16_S
;
505 if (strncmp (input_line_pointer
, "(lo(", 4) == 0)
507 input_line_pointer
+= 3;
508 return BFD_RELOC_LO16
;
511 if (strncmp (input_line_pointer
, "(sdaoff(", 8) == 0)
513 input_line_pointer
+= 7;
514 return BFD_RELOC_V850_SDA_OFFSET
;
517 if (strncmp (input_line_pointer
, "(zdaoff(", 8) == 0)
519 input_line_pointer
+= 7;
520 return BFD_RELOC_V850_ZDA_OFFSET
;
523 if (strncmp (input_line_pointer
, "(tdaoff(", 8) == 0)
525 input_line_pointer
+= 7;
526 return BFD_RELOC_V850_TDA_OFFSET
;
529 return BFD_RELOC_UNUSED
;
537 struct v850_opcode
*opcode
;
538 struct v850_opcode
*next_opcode
;
539 const unsigned char *opindex_ptr
;
541 unsigned long insn
, insn_size
;
545 bfd_reloc_code_real_type reloc
;
547 /* Get the opcode. */
548 for (s
= str
; *s
!= '\0' && ! isspace (*s
); s
++)
553 /* find the first opcode with the proper name */
554 opcode
= (struct v850_opcode
*)hash_find (v850_hash
, str
);
557 as_bad ("Unrecognized opcode: `%s'", str
);
562 while (isspace (*str
))
565 input_line_pointer
= str
;
569 const char *errmsg
= NULL
;
574 insn
= opcode
->opcode
;
575 for (opindex_ptr
= opcode
->operands
; *opindex_ptr
!= 0; opindex_ptr
++)
577 const struct v850_operand
*operand
;
581 if (next_opindex
== 0)
583 operand
= &v850_operands
[*opindex_ptr
];
587 operand
= &v850_operands
[next_opindex
];
593 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
596 /* Gather the operand. */
597 hold
= input_line_pointer
;
598 input_line_pointer
= str
;
601 /* lo(), hi(), hi0(), etc... */
602 if ((reloc
= v850_reloc_prefix()) != BFD_RELOC_UNUSED
)
606 if (ex
.X_op
== O_constant
)
612 /* Truncate, then sign extend the value. */
613 int temp
= ex
.X_add_number
& 0xffff;
615 /* XXX Assumes 32bit ints! */
616 temp
= (temp
<< 16) >> 16;
617 ex
.X_add_number
= temp
;
622 ex
.X_add_number
= ((ex
.X_add_number
>> 16) & 0xffff);
625 case BFD_RELOC_HI16_S
:
626 ex
.X_add_number
= ((ex
.X_add_number
>> 16) & 0xffff)
627 + ((ex
.X_add_number
>> 15) & 1);
634 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
639 if (fc
> MAX_INSN_FIXUPS
)
640 as_fatal ("too many fixups");
643 fixups
[fc
].opindex
= *opindex_ptr
;
644 fixups
[fc
].reloc
= reloc
;
650 if ((operand
->flags
& V850_OPERAND_REG
) != 0)
652 if (!register_name(&ex
))
654 errmsg
= "invalid register name";
658 else if ((operand
->flags
& V850_OPERAND_SRG
) != 0)
660 if (!system_register_name(&ex
))
662 errmsg
= "invalid system register name";
666 else if ((operand
->flags
& V850_OPERAND_EP
) != 0)
668 char *start
= input_line_pointer
;
669 char c
= get_symbol_end ();
670 if (strcmp (start
, "ep") != 0 && strcmp (start
, "r30") != 0)
672 /* Put things back the way we found them. */
673 *input_line_pointer
= c
;
674 input_line_pointer
= start
;
675 errmsg
= "expected EP register";
678 *input_line_pointer
= c
;
679 str
= input_line_pointer
;
680 input_line_pointer
= hold
;
682 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
686 else if ((operand
->flags
& V850_OPERAND_CC
) != 0)
690 errmsg
= "invalid condition code name";
694 else if (register_name (&ex
)
695 && (operand
->flags
& V850_OPERAND_REG
) == 0)
697 errmsg
= "syntax error: register not expected";
700 else if (system_register_name (&ex
)
701 && (operand
->flags
& V850_OPERAND_SRG
) == 0)
703 errmsg
= "syntax error: system register not expected";
706 else if (cc_name (&ex
)
707 && (operand
->flags
& V850_OPERAND_CC
) == 0)
709 errmsg
= "syntax error: condition code not expected";
720 errmsg
= "illegal operand";
723 errmsg
= "missing operand";
726 if ((operand
->flags
& (V850_OPERAND_REG
| V850_OPERAND_SRG
)) == 0)
728 errmsg
= "invalid operand";
732 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
737 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
742 /* We need to generate a fixup for this expression. */
743 if (fc
>= MAX_INSN_FIXUPS
)
744 as_fatal ("too many fixups");
746 fixups
[fc
].opindex
= *opindex_ptr
;
747 fixups
[fc
].reloc
= BFD_RELOC_UNUSED
;
754 str
= input_line_pointer
;
755 input_line_pointer
= hold
;
757 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']'
766 next_opcode
= opcode
+ 1;
767 if (next_opcode
->opcode
!= 0 && !strcmp(next_opcode
->name
, opcode
->name
))
769 opcode
= next_opcode
;
773 as_bad ("%s", errmsg
);
779 while (isspace (*str
))
783 as_bad ("junk at end of line: `%s'", str
);
785 input_line_pointer
= str
;
787 /* Write out the instruction.
789 Four byte insns have an opcode with the two high bits on. */
790 if ((insn
& 0x0600) == 0x0600)
794 f
= frag_more (insn_size
);
795 md_number_to_chars (f
, insn
, insn_size
);
797 /* Create any fixups. At this point we do not use a
798 bfd_reloc_code_real_type, but instead just use the
799 BFD_RELOC_UNUSED plus the operand index. This lets us easily
800 handle fixups for any operand type, although that is admittedly
801 not a very exciting feature. We pick a BFD reloc type in
803 for (i
= 0; i
< fc
; i
++)
805 const struct v850_operand
*operand
;
807 operand
= &v850_operands
[fixups
[i
].opindex
];
808 if (fixups
[i
].reloc
!= BFD_RELOC_UNUSED
)
810 reloc_howto_type
*reloc_howto
= bfd_reloc_type_lookup (stdoutput
, fixups
[i
].reloc
);
818 size
= bfd_get_reloc_size (reloc_howto
);
820 /* The "size" of a TDA_OFFSET reloc varies depending
821 on what kind of instruction it's used in! */
822 if (reloc_howto
->type
== 11 && insn_size
> 2)
825 if (size
< 1 || size
> 4)
829 fixP
= fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ offset
, size
,
831 reloc_howto
->pc_relative
,
836 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
, 4,
838 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
839 ((bfd_reloc_code_real_type
)
840 (fixups
[i
].opindex
+ (int) BFD_RELOC_UNUSED
)));
846 /* if while processing a fixup, a reloc really needs to be created */
847 /* then it is done here */
850 tc_gen_reloc (seg
, fixp
)
855 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
856 reloc
->sym_ptr_ptr
= &fixp
->fx_addsy
->bsym
;
857 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
858 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
859 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
861 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
862 "reloc %d not supported by object file format", (int)fixp
->fx_r_type
);
865 reloc
->addend
= fixp
->fx_addnumber
;
866 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
871 md_estimate_size_before_relax (fragp
, seg
)
882 /* If the symbol is undefined, or in a section other than our own,
883 then let the linker figure it out. */
884 if (fixp
->fx_addsy
!= (symbolS
*) NULL
&& ! S_IS_DEFINED (fixp
->fx_addsy
))
886 /* The symbol is undefined. Let the linker figure it out. */
889 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
893 md_apply_fix3 (fixp
, valuep
, seg
)
901 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
906 else if (fixp
->fx_pcrel
)
910 value
= fixp
->fx_offset
;
911 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
913 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
914 value
-= S_GET_VALUE (fixp
->fx_subsy
);
917 /* We don't actually support subtracting a symbol. */
918 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
919 "expression too complex");
924 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
926 if ((int) fixp
->fx_r_type
>= (int) BFD_RELOC_UNUSED
)
929 const struct v850_operand
*operand
;
933 opindex
= (int) fixp
->fx_r_type
- (int) BFD_RELOC_UNUSED
;
934 operand
= &v850_operands
[opindex
];
936 /* Fetch the instruction, insert the fully resolved operand
937 value, and stuff the instruction back again.
939 Note the instruction has been stored in little endian
941 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
943 insn
= bfd_getl32((unsigned char *) where
);
944 insn
= v850_insert_operand (insn
, operand
, (offsetT
) value
,
945 fixp
->fx_file
, fixp
->fx_line
);
946 bfd_putl32((bfd_vma
) insn
, (unsigned char *) where
);
950 /* Nothing else to do here. */
954 /* Determine a BFD reloc value based on the operand information.
955 We are only prepared to turn a few of the operands into relocs. */
957 if (operand
->bits
== 22)
958 fixp
->fx_r_type
= BFD_RELOC_V850_22_PCREL
;
959 else if (operand
->bits
== 9)
960 fixp
->fx_r_type
= BFD_RELOC_V850_9_PCREL
;
963 as_bad_where(fixp
->fx_file
, fixp
->fx_line
,
964 "unresolved expression that must be resolved");
969 else if (fixp
->fx_done
)
971 /* We still have to insert the value into memory! */
972 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
973 if (fixp
->fx_size
== 1)
974 *where
= value
& 0xff;
975 if (fixp
->fx_size
== 2)
976 bfd_putl16(value
& 0xffff, (unsigned char *) where
);
977 if (fixp
->fx_size
== 4)
978 bfd_putl32(value
, (unsigned char *) where
);
981 fixp
->fx_addnumber
= value
;
986 /* Insert an operand value into an instruction. */
989 v850_insert_operand (insn
, operand
, val
, file
, line
)
991 const struct v850_operand
*operand
;
996 if (operand
->bits
!= 16)
1001 if ((operand
->flags
& V850_OPERAND_SIGNED
) != 0)
1003 max
= (1 << (operand
->bits
- 1)) - 1;
1004 min
= - (1 << (operand
->bits
- 1));
1008 max
= (1 << operand
->bits
) - 1;
1015 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)
1018 "operand out of range (%s not between %ld and %ld)";
1021 sprint_value (buf
, test
);
1022 if (file
== (char *) NULL
)
1023 as_warn (err
, buf
, min
, max
);
1025 as_warn_where (file
, line
, err
, buf
, min
, max
);
1029 if (operand
->insert
)
1031 const char *message
= NULL
;
1032 insn
= (*operand
->insert
) (insn
, val
, &message
);
1033 if (message
!= NULL
)
1035 if (file
== (char *) NULL
)
1038 as_warn_where (file
, line
, message
);
1042 insn
|= (((long) val
& ((1 << operand
->bits
) - 1)) << operand
->shift
);
1046 /* Parse a cons expression. We have to handle hi(), lo(), etc
1049 parse_cons_expression_v850 (exp
)
1052 /* See if there's a reloc prefix like hi() we have to handle. */
1053 hold_cons_reloc
= v850_reloc_prefix ();
1055 /* Do normal expression parsing. */
1059 /* Create a fixup for a cons expression. If parse_cons_expression_v850
1060 found a reloc prefix, then we use that reloc, else we choose an
1061 appropriate one based on the size of the expression. */
1063 cons_fix_new_v850 (frag
, where
, size
, exp
)
1069 if (hold_cons_reloc
== BFD_RELOC_UNUSED
)
1072 hold_cons_reloc
= BFD_RELOC_32
;
1074 hold_cons_reloc
= BFD_RELOC_16
;
1076 hold_cons_reloc
= BFD_RELOC_8
;
1080 fix_new_exp (frag
, where
, size
, exp
, 0, hold_cons_reloc
);
1082 fix_new (frag
, where
, size
, NULL
, 0, 0, hold_cons_reloc
);