1 /* tc-d10v.c -- Assembler code for the Mitsubishi D10V
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/d10v.h"
29 const char comment_chars
[] = "#;";
30 const char line_comment_chars
[] = "#";
31 const char line_separator_chars
[] = "";
32 const char *md_shortopts
= "";
33 const char EXP_CHARS
[] = "eE";
34 const char FLT_CHARS
[] = "dD";
38 #define MAX_INSN_FIXUPS (5)
42 bfd_reloc_code_real_type reloc
;
45 typedef struct _fixups
48 struct d10v_fixup fix
[MAX_INSN_FIXUPS
];
52 static Fixups FixUps
[2];
53 static Fixups
*fixups
;
56 static int reg_name_search
PARAMS ((char *name
));
57 static int register_name
PARAMS ((expressionS
*expressionP
));
58 static int postfix
PARAMS ((char *p
));
59 static bfd_reloc_code_real_type get_reloc
PARAMS ((struct d10v_operand
*op
));
60 static int get_operands
PARAMS ((expressionS exp
[]));
61 static unsigned long build_insn
PARAMS ((struct d10v_opcode
*opcode
, expressionS
*opers
));
62 static void write_long
PARAMS ((struct d10v_opcode
*opcode
, unsigned long insn
, Fixups
*fx
));
63 static void write_1_short
PARAMS ((struct d10v_opcode
*opcode
, unsigned long insn
, Fixups
*fx
));
64 static int write_2_short
PARAMS ((struct d10v_opcode
*opcode1
, unsigned long insn1
,
65 struct d10v_opcode
*opcode2
, unsigned long insn2
, int exec_type
, Fixups
*fx
));
66 static unsigned long do_assemble
PARAMS ((char *str
, struct d10v_opcode
**opcode
));
67 static unsigned long d10v_insert_operand
PARAMS (( unsigned long insn
, int op_type
,
68 offsetT value
, int left
));
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
[] =
82 /* Opcode hash table. */
83 static struct hash_control
*d10v_hash
;
85 /* reg_name_search does a binary search of the pre_defined_registers
86 array to see if "name" is a valid regiter name. Returns the register
87 number from the array on success, or -1 on failure. */
90 reg_name_search (name
)
93 int middle
, low
, high
;
97 high
= reg_name_cnt() - 1;
101 middle
= (low
+ high
) / 2;
102 cmp
= strcasecmp (name
, pre_defined_registers
[middle
].name
);
108 return pre_defined_registers
[middle
].value
;
114 /* register_name() checks the string at input_line_pointer
115 to see if it is a valid register name */
118 register_name (expressionP
)
119 expressionS
*expressionP
;
122 char c
, *p
= input_line_pointer
;
124 while (*p
&& *p
!='\n' && *p
!='\r' && *p
!=',' && *p
!=' ' && *p
!=')')
131 /* look to see if it's in the register table */
132 reg_number
= reg_name_search (input_line_pointer
);
135 expressionP
->X_op
= O_register
;
136 /* temporarily store a pointer to the string here */
137 expressionP
->X_op_symbol
= (struct symbol
*)input_line_pointer
;
138 expressionP
->X_add_number
= reg_number
;
139 input_line_pointer
= p
;
148 md_show_usage (stream
)
151 fprintf(stream
, "D10V options:\n\
156 md_parse_option (c
, arg
)
164 md_undefined_symbol (name
)
171 md_atof (type
, litp
, sizep
)
180 md_convert_frag (abfd
, sec
, fragP
)
185 printf ("call to md_convert_frag \n");
190 md_section_align (seg
, addr
)
194 int align
= bfd_get_section_alignment (stdoutput
, seg
);
195 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
202 char *prev_name
= "";
203 struct d10v_opcode
*opcode
;
204 d10v_hash
= hash_new();
206 /* Insert unique names into hash table. The D10v instruction set
207 has many identical opcode names that have different opcodes based
208 on the operands. This hash table then provides a quick index to
209 the first opcode with a particular name in the opcode table. */
211 for (opcode
= (struct d10v_opcode
*)d10v_opcodes
; opcode
->name
; opcode
++)
213 if (strcmp (prev_name
, opcode
->name
))
215 prev_name
= (char *)opcode
->name
;
216 hash_insert (d10v_hash
, opcode
->name
, (char *) opcode
);
221 FixUps
[0].next
= &FixUps
[1];
222 FixUps
[1].next
= &FixUps
[0];
226 /* this function removes the postincrement or postdecrement
227 operator ( '+' or '-' ) from an expression */
229 static int postfix (p
)
232 while (*p
!= '-' && *p
!= '+')
234 if (*p
==0 || *p
=='\n' || *p
=='\r')
254 static bfd_reloc_code_real_type
256 struct d10v_operand
*op
;
260 /* printf("get_reloc: bits=%d address=%d\n",bits,op->flags & OPERAND_ADDR); */
264 if (op
->flags
& OPERAND_ADDR
)
267 return (BFD_RELOC_D10V_10_PCREL_R
);
269 return (BFD_RELOC_D10V_18_PCREL
);
272 return (BFD_RELOC_16
);
275 /* get_operands parses a string of operands and returns
276 an array of expressions */
282 char *p
= input_line_pointer
;
288 while (*p
== ' ' || *p
== '\t' || *p
== ',')
290 if (*p
==0 || *p
=='\n' || *p
=='\r')
296 exp
[numops
].X_op
= O_absent
;
300 exp
[numops
].X_add_number
= OPERAND_ATPAR
;
305 exp
[numops
].X_add_number
= OPERAND_ATMINUS
;
309 exp
[numops
].X_add_number
= OPERAND_ATSIGN
;
318 /* just skip the trailing paren */
323 input_line_pointer
= p
;
326 /* check to see if it might be a register name */
327 if (!register_name (&exp
[numops
]))
329 /* parse as an expression */
330 expression (&exp
[numops
]);
333 if (exp
[numops
].X_op
== O_illegal
)
334 as_bad ("illegal operand");
335 else if (exp
[numops
].X_op
== O_absent
)
336 as_bad ("missing operand");
339 p
= input_line_pointer
;
344 case -1: /* postdecrement mode */
345 exp
[numops
].X_op
= O_absent
;
346 exp
[numops
++].X_add_number
= OPERAND_MINUS
;
348 case 1: /* postincrement mode */
349 exp
[numops
].X_op
= O_absent
;
350 exp
[numops
++].X_add_number
= OPERAND_PLUS
;
354 exp
[numops
].X_op
= 0;
359 d10v_insert_operand (insn
, op_type
, value
, left
)
367 shift
= d10v_operands
[op_type
].shift
;
371 bits
= d10v_operands
[op_type
].bits
;
373 /* truncate to the proper number of bits */
374 /* FIXME: overflow checking here? */
375 value
&= 0x7FFFFFFF >> (31 - bits
);
376 insn
|= (value
<< shift
);
382 /* build_insn takes a pointer to the opcode entry in the opcode table
383 and the array of operand expressions and returns the instruction */
386 build_insn (opcode
, opers
)
387 struct d10v_opcode
*opcode
;
390 int i
, bits
, shift
, flags
;
393 insn
= opcode
->opcode
;
395 for (i
=0;opcode
->operands
[i
];i
++)
397 flags
= d10v_operands
[opcode
->operands
[i
]].flags
;
398 bits
= d10v_operands
[opcode
->operands
[i
]].bits
;
399 shift
= d10v_operands
[opcode
->operands
[i
]].shift
;
400 number
= opers
[i
].X_add_number
;
402 if (flags
& OPERAND_REG
)
404 number
&= REGISTER_MASK
;
405 if (opcode
->format
== LONG_L
)
409 if (opers
[i
].X_op
!= O_register
&& opers
[i
].X_op
!= O_constant
)
411 /* now create a fixup */
414 printf("need a fixup: ");
415 print_expr_1(stdout,&opers[i]);
419 if (fixups
->fc
>= MAX_INSN_FIXUPS
)
420 as_fatal ("too many fixups");
421 fixups
->fix
[fixups
->fc
].exp
= opers
[i
];
423 /* put the operand number here for now. We can look up
424 the reloc type and/or fixup the instruction in md_apply_fix() */
425 fixups
->fix
[fixups
->fc
].reloc
= opcode
->operands
[i
];
429 /* truncate to the proper number of bits */
430 /* FIXME: overflow checking here? */
431 number
&= 0x7FFFFFFF >> (31 - bits
);
432 insn
= insn
| (number
<< shift
);
437 /* write out a long form instruction */
439 write_long (opcode
, insn
, fx
)
440 struct d10v_opcode
*opcode
;
445 char *f
= frag_more(4);
448 /* printf("INSN: %08x\n",insn); */
449 number_to_chars_bigendian (f
, insn
, 4);
451 for (i
=0; i
< fx
->fc
; i
++)
453 if (get_reloc((struct d10v_operand
*)&d10v_operands
[fx
->fix
[i
].reloc
]))
456 printf("fix_new_exp: where:%x size:4\n ",f - frag_now->fr_literal);
457 print_expr_1(stdout,&(fx->fix[i].exp));
461 fix_new_exp (frag_now
,
462 f
- frag_now
->fr_literal
,
473 /* write out a short form instruction by itself */
475 write_1_short (opcode
, insn
, fx
)
476 struct d10v_opcode
*opcode
;
480 char *f
= frag_more(4);
483 insn
|= FM00
| (NOP
<< 15);
484 /* printf("INSN: %08x\n",insn); */
485 number_to_chars_bigendian (f
, insn
, 4);
486 for (i
=0; i
< fx
->fc
; i
++)
488 if (get_reloc((struct d10v_operand
*)&d10v_operands
[fx
->fix
[i
].reloc
]))
491 printf("fix_new_exp: where:%x size:4\n ",f - frag_now->fr_literal);
492 print_expr_1(stdout,&(fx->fix[i].exp));
496 fix_new_exp (frag_now
,
497 f
- frag_now
->fr_literal
,
507 /* write out a short form instruction if possible */
508 /* return number of instructions not written out */
510 write_2_short (opcode1
, insn1
, opcode2
, insn2
, exec_type
, fx
)
511 struct d10v_opcode
*opcode1
, *opcode2
;
512 unsigned long insn1
, insn2
;
520 if(opcode1
->exec_type
== BRANCH_LINK
)
522 /* subroutines must be called from 32-bit boundaries */
523 /* so the return address will be correct */
524 write_1_short (opcode1
, insn1
, fx
->next
);
531 if (opcode1
->unit
== IU
)
533 /* reverse sequential */
534 insn
= FM10
| (insn2
<< 15) | insn1
;
538 insn
= FM01
| (insn1
<< 15) | insn2
;
542 case 1: /* parallel */
543 insn
= FM00
| (insn1
<< 15) | insn2
;
546 case 2: /* sequential */
547 insn
= FM01
| (insn1
<< 15) | insn2
;
550 case 3: /* reverse sequential */
551 insn
= FM10
| (insn1
<< 15) | insn2
;
554 as_fatal("unknown execution type passed to write_2_short()");
557 /* printf("INSN: %08x\n",insn); */
559 number_to_chars_bigendian (f
, insn
, 4);
563 bfd_reloc_code_real_type reloc
;
564 for (i
=0; i
< fx
->fc
; i
++)
566 reloc
= get_reloc((struct d10v_operand
*)&d10v_operands
[fx
->fix
[i
].reloc
]);
569 if ( (reloc
== BFD_RELOC_D10V_10_PCREL_R
) && (j
== 0) )
570 fx
->fix
[i
].reloc
|= 1024;
573 printf("fix_new_exp: where:%x reloc:%d\n ",f - frag_now->fr_literal,fx->fix[i].reloc);
574 print_expr_1(stdout,&(fx->fix[i].exp));
577 fix_new_exp (frag_now
,
578 f
- frag_now
->fr_literal
,
593 /* This is the main entry point for the machine-dependent assembler. str points to a
594 machine-dependent instruction. This function is supposed to emit the frags/bytes
595 it assembles to. For the D10V, it mostly handles the special VLIW parsing and packing
596 and leaves the difficult stuff to do_assemble().
599 static unsigned long prev_insn
;
600 static struct d10v_opcode
*prev_opcode
= 0;
601 static subsegT prev_subseg
;
602 static segT prev_seg
;
608 struct d10v_opcode
*opcode
;
613 /* printf("md_assemble: str=%s\n",str); */
615 /* look for the special multiple instruction seperators */
616 str2
= strstr (str
, "||");
621 str2
= strstr (str
, "->");
626 str2
= strstr (str
, "<-");
633 /* str2 points to the seperator, if one */
638 /* if two instructions are present and we already have one saved
639 then first write it out */
641 write_1_short (prev_opcode
, prev_insn
, fixups
->next
);
643 /* assemble first instruction and save it */
644 prev_insn
= do_assemble (str
, &prev_opcode
);
645 fixups
= fixups
->next
;
649 insn
= do_assemble (str
, &opcode
);
651 /* if this is a long instruction, write it and any previous short instruction */
652 if (opcode
->format
& LONG_OPCODE
)
655 as_fatal("Unable to mix instructions as specified");
658 write_1_short (prev_opcode
, prev_insn
, fixups
->next
);
661 write_long (opcode
, insn
, fixups
);
666 if (prev_opcode
&& (write_2_short (prev_opcode
, prev_insn
, opcode
, insn
, t
, fixups
) == 0))
668 /* no instructions saved */
674 as_fatal("Unable to mix instructions as specified");
675 /* save off last instruction so it may be packed on next pass */
676 prev_opcode
= opcode
;
679 prev_subseg
= now_subseg
;
680 fixups
= fixups
->next
;
686 do_assemble (str
, opcode
)
688 struct d10v_opcode
**opcode
;
690 struct d10v_opcode
*next_opcode
;
691 unsigned char *op_start
, *save
;
692 unsigned char *op_end
;
694 int nlen
= 0, i
, match
, numops
;
695 expressionS myops
[6];
698 /* printf("do_assemble: str=%s\n",str); */
700 /* Drop leading whitespace */
704 /* find the opcode end */
705 for (op_start
= op_end
= (unsigned char *) (str
);
708 && !is_end_of_line
[*op_end
] && *op_end
!= ' ';
711 name
[nlen
] = op_start
[nlen
];
717 as_bad ("can't find opcode ");
719 /* find the first opcode with the proper name */
720 *opcode
= (struct d10v_opcode
*)hash_find (d10v_hash
, name
);
723 as_bad ("unknown opcode");
727 save
= input_line_pointer
;
728 input_line_pointer
= op_end
;
730 /* get all the operands and save them as expressions */
731 numops
= get_operands (myops
);
733 /* now search the opcode table table for one with operands */
734 /* that match what we've got */
738 for (i
= 0; (*opcode
)->operands
[i
]; i
++)
740 int flags
= d10v_operands
[(*opcode
)->operands
[i
]].flags
;
742 if (myops
[i
].X_op
==0)
748 if (flags
& OPERAND_REG
)
750 if ((myops
[i
].X_op
!= O_register
) ||
751 ((flags
& OPERAND_ACC
) != (myops
[i
].X_add_number
& OPERAND_ACC
)) ||
752 ((flags
& OPERAND_FLAG
) != (myops
[i
].X_add_number
& OPERAND_FLAG
)) ||
753 ((flags
& OPERAND_CONTROL
) != (myops
[i
].X_add_number
& OPERAND_CONTROL
)))
760 if (((flags
& OPERAND_MINUS
) && ((myops
[i
].X_op
!= O_absent
) || (myops
[i
].X_add_number
!= OPERAND_MINUS
))) ||
761 ((flags
& OPERAND_PLUS
) && ((myops
[i
].X_op
!= O_absent
) || (myops
[i
].X_add_number
!= OPERAND_PLUS
))) ||
762 ((flags
& OPERAND_ATMINUS
) && ((myops
[i
].X_op
!= O_absent
) || (myops
[i
].X_add_number
!= OPERAND_ATMINUS
))) ||
763 ((flags
& OPERAND_ATPAR
) && ((myops
[i
].X_op
!= O_absent
) || (myops
[i
].X_add_number
!= OPERAND_ATPAR
))) ||
764 ((flags
& OPERAND_ATSIGN
) && ((myops
[i
].X_op
!= O_absent
) || (myops
[i
].X_add_number
!= OPERAND_ATSIGN
))))
771 /* we're only done if the operands matched AND there
772 are no more to check */
773 if (match
&& myops
[i
].X_op
==0)
776 next_opcode
= (*opcode
)+1;
777 if (next_opcode
->opcode
== 0)
779 if (strcmp(next_opcode
->name
, (*opcode
)->name
))
781 (*opcode
) = next_opcode
;
786 as_bad ("bad opcode or operands");
790 /* Check that all registers that are required to be even are. */
791 /* Also, if any operands were marked as registers, but were really symbols */
793 for (i
=0; (*opcode
)->operands
[i
]; i
++)
795 if ((d10v_operands
[(*opcode
)->operands
[i
]].flags
& OPERAND_EVEN
) &&
796 (myops
[i
].X_add_number
& 1))
797 as_fatal("Register number must be EVEN");
798 if (myops
[i
].X_op
== O_register
)
800 if (!(d10v_operands
[(*opcode
)->operands
[i
]].flags
& OPERAND_REG
))
802 myops
[i
].X_op
= O_symbol
;
803 myops
[i
].X_add_symbol
= symbol_find_or_make ((char *)myops
[i
].X_op_symbol
);
804 myops
[i
].X_add_number
= 0;
805 myops
[i
].X_op_symbol
= NULL
;
806 /* FIXME create a fixup */
811 input_line_pointer
= save
;
813 /* at this point, we have "opcode" pointing to the opcode entry in the
814 d10v opcode table, with myops filled out with the operands. */
815 insn
= build_insn ((*opcode
), myops
);
816 /* printf("sub-insn = %lx\n",insn); */
822 /* if while processing a fixup, a reloc really needs to be created */
823 /* then it is done here */
826 tc_gen_reloc (seg
, fixp
)
831 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
832 reloc
->sym_ptr_ptr
= &fixp
->fx_addsy
->bsym
;
833 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
834 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
835 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
837 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
838 "reloc %d not supported by object file format", (int)fixp
->fx_r_type
);
841 reloc
->addend
= fixp
->fx_addnumber
;
842 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
847 md_estimate_size_before_relax (fragp
, seg
)
856 md_pcrel_from_section (fixp
, sec
)
861 /* return fixp->fx_frag->fr_address + fixp->fx_where; */
865 md_apply_fix3 (fixp
, valuep
, seg
)
876 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
881 else if (fixp
->fx_pcrel
)
885 value
= fixp
->fx_offset
;
886 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
888 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
889 value
-= S_GET_VALUE (fixp
->fx_subsy
);
892 /* We don't actually support subtracting a symbol. */
893 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
894 "expression too complex");
899 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
901 op_type
= fixp
->fx_r_type
;
905 fixp
->fx_r_type
= BFD_RELOC_D10V_10_PCREL_L
;
909 fixp
->fx_r_type
= get_reloc((struct d10v_operand
*)&d10v_operands
[op_type
]);
911 /* Fetch the instruction, insert the fully resolved operand
912 value, and stuff the instruction back again. */
913 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
914 insn
= bfd_getb32 ((unsigned char *) where
);
916 switch (fixp
->fx_r_type
)
918 case BFD_RELOC_D10V_10_PCREL_L
:
919 case BFD_RELOC_D10V_10_PCREL_R
:
920 case BFD_RELOC_D10V_18_PCREL
:
921 /* instruction addresses are always right-shifted by 2
924 value
-= fixp
->fx_where
;
929 /* printf(" insn=%x value=%x where=%x pcrel=%x\n",insn,value,fixp->fx_where,fixp->fx_pcrel); */
931 insn
= d10v_insert_operand (insn
, op_type
, (offsetT
)value
, left
);
933 /* printf(" new insn=%x\n",insn); */
935 bfd_putb32 ((bfd_vma
) insn
, (unsigned char *) where
);
940 fixp
->fx_addnumber
= value
;
945 /* d10v_cleanup() is called after the assembler has finished parsing the input
946 file or after a label is defined. Because the D10V assembler sometimes saves short
947 instructions to see if it can package them with the next instruction, there may
948 be a short instruction that still needs written. */
959 subseg_set (prev_seg
, prev_subseg
);
960 write_1_short (prev_opcode
, prev_insn
, fixups
);
961 subseg_set (seg
, subseg
);