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 /* Generic assembler global variables which must be defined by all targets. */
30 /* Characters which always start a comment. */
31 const char comment_chars
[] = "#";
33 /* Characters which start a comment at the beginning of a line. */
34 const char line_comment_chars
[] = ";#";
36 /* Characters which may be used to separate multiple commands on a
38 const char line_separator_chars
[] = ";";
40 /* Characters which are used to indicate an exponent in a floating
42 const char EXP_CHARS
[] = "eE";
44 /* Characters which mean that a number is a floating point constant,
46 const char FLT_CHARS
[] = "dD";
49 static unsigned long v850_insert_operand
50 PARAMS ((unsigned long insn
, const struct v850_operand
*operand
,
51 offsetT val
, char *file
, unsigned int line
));
52 static int reg_name_search
PARAMS ((char *name
));
53 static boolean register_name
PARAMS ((expressionS
*expressionP
));
54 static int postfix
PARAMS ((char *p
));
55 static bfd_reloc_code_real_type get_reloc
PARAMS ((struct v850_operand
*op
));
56 static unsigned long build_insn
PARAMS ((struct v850_opcode
*opcode
, expressionS
*opers
));
60 #define MAX_INSN_FIXUPS (5)
65 bfd_reloc_code_real_type reloc
;
67 struct v850_fixup fixups
[MAX_INSN_FIXUPS
];
70 const char *md_shortopts
= "";
71 struct option md_longopts
[] = {
72 {NULL
, no_argument
, NULL
, 0}
74 size_t md_longopts_size
= sizeof(md_longopts
);
76 /* The target specific pseudo-ops which we support. */
77 const pseudo_typeS md_pseudo_table
[] =
80 { "byte", ppc_byte, 0 },
81 { "long", ppc_elf_cons, 4 },
82 { "word", ppc_elf_cons, 2 },
83 { "short", ppc_elf_cons, 2 },
84 { "rdata", ppc_elf_rdata, 0 },
85 { "rodata", ppc_elf_rdata, 0 },
86 { "lcomm", ppc_elf_lcomm, 0 },
91 /* Opcode hash table. */
92 static struct hash_control
*v850_hash
;
94 /* Structure to hold information about predefined registers. */
101 /* This table is sorted. Suitable for searching by a binary search. */
102 static const struct reg_name pre_defined_registers
[] =
104 { "ep", 30 }, /* ep - element ptr */
105 { "gp", 4 }, /* gp - global ptr */
106 { "lp", 31 }, /* lp - link ptr */
139 { "sp", 3 }, /* sp - stack ptr */
140 { "tp", 5 }, /* tp - text ptr */
143 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct reg_name))
146 static const struct reg_name system_registers
[] =
156 static const struct reg_name cc_names
[] =
180 /* reg_name_search does a binary search of the pre_defined_registers
181 array to see if "name" is a valid regiter name. Returns the register
182 number from the array on success, or -1 on failure. */
185 reg_name_search (name
)
188 int middle
, low
, high
;
192 high
= REG_NAME_CNT
- 1;
196 middle
= (low
+ high
) / 2;
197 cmp
= strcasecmp (name
, pre_defined_registers
[middle
].name
);
203 return pre_defined_registers
[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 (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 */
257 md_show_usage (stream
)
260 fprintf(stream
, "V850 options:\n\
265 md_parse_option (c
, arg
)
273 md_undefined_symbol (name
)
280 md_atof (type
, litp
, sizep
)
286 LITTLENUM_TYPE words
[4];
302 return "bad call to md_atof";
305 t
= atof_ieee (input_line_pointer
, type
, words
);
307 input_line_pointer
= t
;
311 for (i
= prec
- 1; i
>= 0; i
--)
313 md_number_to_chars (litp
, (valueT
) words
[i
], 2);
322 md_convert_frag (abfd
, sec
, fragP
)
327 /* printf ("call to md_convert_frag \n"); */
332 md_section_align (seg
, addr
)
336 int align
= bfd_get_section_alignment (stdoutput
, seg
);
337 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
343 char *prev_name
= "";
344 register const struct v850_opcode
*op
;
345 const struct v850_opcode
*op_end
;
347 v850_hash
= hash_new();
349 /* Insert unique names into hash table. The V850 instruction set
350 has many identical opcode names that have different opcodes based
351 on the operands. This hash table then provides a quick index to
352 the first opcode with a particular name in the opcode table. */
355 op_end
= v850_opcodes
+ v850_num_opcodes
;
357 for (; op
< op_end
; op
++)
359 if (strcmp (prev_name
, op
->name
))
361 prev_name
= (char *) op
->name
;
362 hash_insert (v850_hash
, op
->name
, (char *) op
);
368 static bfd_reloc_code_real_type
370 struct v850_operand
*op
;
381 struct v850_opcode
*opcode
;
382 struct v850_opcode
*next_opcode
;
383 const unsigned char *opindex_ptr
;
392 expressionS myops
[5];
394 /* Get the opcode. */
395 for (s
= str
; *s
!= '\0' && ! isspace (*s
); s
++)
400 /* find the first opcode with the proper name */
401 opcode
= (struct v850_opcode
*)hash_find (v850_hash
, str
);
404 as_bad ("Unrecognized opcode: `%s'", str
);
409 while (isspace (*str
))
412 input_line_pointer
= str
;
416 const char *errmsg
= NULL
;
421 for (opindex_ptr
= opcode
->operands
; *opindex_ptr
!= 0; opindex_ptr
++)
423 const struct v850_operand
*operand
;
428 if (next_opindex
== 0)
430 operand
= &v850_operands
[*opindex_ptr
];
434 operand
= &v850_operands
[next_opindex
];
440 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
443 /* Gather the operand. */
444 hold
= input_line_pointer
;
445 input_line_pointer
= str
;
447 if ((operand
->flags
& V850_OPERAND_REG
) != 0)
449 if (!register_name(&ex
))
451 errmsg
= "invalid register name";
455 else if (strncmp(input_line_pointer
, "lo(", 3) == 0)
457 input_line_pointer
+= 3;
460 if (*input_line_pointer
++ != ')')
462 errmsg
= "syntax error: expected `)'";
466 if (ex
.X_op
== O_constant
)
467 ex
.X_add_number
&= 0xffff;
470 if (fc
> MAX_INSN_FIXUPS
)
471 as_fatal ("too many fixups");
474 fixups
[fc
].opindex
= *opindex_ptr
;
475 fixups
[fc
].reloc
= BFD_RELOC_LO16
;
479 else if (strncmp(input_line_pointer
, "hi(", 3) == 0)
481 input_line_pointer
+= 3;
484 if (*input_line_pointer
++ != ')')
486 errmsg
= "syntax error: expected `)'";
490 if (ex
.X_op
== O_constant
)
491 ex
.X_add_number
= (ex
.X_add_number
>> 16) & 0xffff;
494 if (fc
> MAX_INSN_FIXUPS
)
495 as_fatal ("too many fixups");
498 fixups
[fc
].opindex
= *opindex_ptr
;
499 fixups
[fc
].reloc
= BFD_RELOC_HI16
;
503 else if (register_name (&ex
)
504 && (operand
->flags
& V850_OPERAND_REG
) == 0)
506 errmsg
= "syntax error: register not expected";
514 str
= input_line_pointer
;
515 input_line_pointer
= hold
;
520 errmsg
= "illegal operand";
523 errmsg
= "missing operand";
526 if ((operand
->flags
& V850_OPERAND_REG
) == 0)
528 errmsg
= "invalid operand";
532 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
537 insn
= v850_insert_operand (insn
, operand
, ex
.X_add_number
,
542 /* We need to generate a fixup for this expression. */
543 if (fc
>= MAX_INSN_FIXUPS
)
544 as_fatal ("too many fixups");
546 fixups
[fc
].opindex
= *opindex_ptr
;
547 fixups
[fc
].reloc
= BFD_RELOC_UNUSED
;
552 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
560 next_opcode
= opcode
+ 1;
561 if (next_opcode
->opcode
!= 0 && !strcmp(next_opcode
->name
, opcode
->name
))
563 opcode
= next_opcode
;
567 as_bad ("%s", errmsg
);
573 while (isspace (*str
))
577 as_bad ("junk at end of line: `%s'", str
);
579 input_line_pointer
= str
;
581 f
= frag_more (opcode
->size
);
582 md_number_to_chars (f
, insn
, opcode
->size
);
586 /* if while processing a fixup, a reloc really needs to be created */
587 /* then it is done here */
590 tc_gen_reloc (seg
, fixp
)
595 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
596 reloc
->sym_ptr_ptr
= &fixp
->fx_addsy
->bsym
;
597 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
598 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
599 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
601 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
602 "reloc %d not supported by object file format", (int)fixp
->fx_r_type
);
605 reloc
->addend
= fixp
->fx_addnumber
;
606 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
611 md_estimate_size_before_relax (fragp
, seg
)
620 md_pcrel_from_section (fixp
, sec
)
625 /* return fixp->fx_frag->fr_address + fixp->fx_where; */
629 md_apply_fix3 (fixp
, valuep
, seg
)
641 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
646 else if (fixp
->fx_pcrel
)
650 value
= fixp
->fx_offset
;
651 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
653 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
654 value
-= S_GET_VALUE (fixp
->fx_subsy
);
657 /* We don't actually support subtracting a symbol. */
658 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
659 "expression too complex");
664 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
666 op_type
= fixp
->fx_r_type
;
667 fixp
->fx_r_type
= get_reloc((struct v850_operand
*)&v850_operands
[op_type
]);
669 /* printf("reloc=%d\n",fixp->fx_r_type); */
671 /* Fetch the instruction, insert the fully resolved operand
672 value, and stuff the instruction back again. */
673 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
674 insn
= bfd_getb32 ((unsigned char *) where
);
675 /* printf(" insn=%x value=%x\n",insn,value); */
677 insn
= v850_insert_operand (insn
, op_type
, (offsetT
) value
);
679 /* printf(" new insn=%x\n",insn); */
681 bfd_putb32 ((bfd_vma
) insn
, (unsigned char *) where
);
686 fixp
->fx_addnumber
= value
;
692 /* Insert an operand value into an instruction. */
695 v850_insert_operand (insn
, operand
, val
, file
, line
)
697 const struct v850_operand
*operand
;
702 if (operand
->bits
!= 32)
707 if ((operand
->flags
& V850_OPERAND_SIGNED
) != 0)
710 if ((operand
->flags
& PPC_OPERAND_SIGNOPT
) != 0
711 && ppc_size
== PPC_OPCODE_32
)
712 max
= (1 << operand
->bits
) - 1;
715 max
= (1 << (operand
->bits
- 1)) - 1;
716 min
= - (1 << (operand
->bits
- 1));
720 max
= (1 << operand
->bits
) - 1;
725 if ((operand
->flags
& PPC_OPERAND_NEGATIVE
) != 0)
732 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)
735 "operand out of range (%s not between %ld and %ld)";
738 sprint_value (buf
, test
);
739 if (file
== (char *) NULL
)
740 as_warn (err
, buf
, min
, max
);
742 as_warn_where (file
, line
, err
, buf
, min
, max
);
746 insn
|= (((long) val
& ((1 << operand
->bits
) - 1)) << operand
->shift
);