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
, unsigned long insn
));
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
;
149 check_range (num
, bits
, sign
)
159 max
= (1 << (bits
- 1)) - 1;
160 min
= - (1 << (bits
- 1));
161 if (((long)num
> max
) || ((long)num
< min
))
166 max
= (1 << bits
) - 1;
168 if ((num
> max
) || (num
< min
))
177 md_show_usage (stream
)
180 fprintf(stream
, "D10V options:\n\
185 md_parse_option (c
, arg
)
193 md_undefined_symbol (name
)
200 md_atof (type
, litp
, sizep
)
209 md_convert_frag (abfd
, sec
, fragP
)
214 printf ("call to md_convert_frag \n");
219 md_section_align (seg
, addr
)
223 int align
= bfd_get_section_alignment (stdoutput
, seg
);
224 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
231 char *prev_name
= "";
232 struct d10v_opcode
*opcode
;
233 d10v_hash
= hash_new();
235 /* Insert unique names into hash table. The D10v instruction set
236 has many identical opcode names that have different opcodes based
237 on the operands. This hash table then provides a quick index to
238 the first opcode with a particular name in the opcode table. */
240 for (opcode
= (struct d10v_opcode
*)d10v_opcodes
; opcode
->name
; opcode
++)
242 if (strcmp (prev_name
, opcode
->name
))
244 prev_name
= (char *)opcode
->name
;
245 hash_insert (d10v_hash
, opcode
->name
, (char *) opcode
);
250 FixUps
[0].next
= &FixUps
[1];
251 FixUps
[1].next
= &FixUps
[0];
255 /* this function removes the postincrement or postdecrement
256 operator ( '+' or '-' ) from an expression */
258 static int postfix (p
)
261 while (*p
!= '-' && *p
!= '+')
263 if (*p
==0 || *p
=='\n' || *p
=='\r')
283 static bfd_reloc_code_real_type
285 struct d10v_operand
*op
;
289 /* printf("get_reloc: bits=%d address=%d\n",bits,op->flags & OPERAND_ADDR); */
293 if (op
->flags
& OPERAND_ADDR
)
296 return (BFD_RELOC_D10V_10_PCREL_R
);
298 return (BFD_RELOC_D10V_18_PCREL
);
301 return (BFD_RELOC_16
);
304 /* get_operands parses a string of operands and returns
305 an array of expressions */
311 char *p
= input_line_pointer
;
317 while (*p
== ' ' || *p
== '\t' || *p
== ',')
319 if (*p
==0 || *p
=='\n' || *p
=='\r')
325 exp
[numops
].X_op
= O_absent
;
329 exp
[numops
].X_add_number
= OPERAND_ATPAR
;
334 exp
[numops
].X_add_number
= OPERAND_ATMINUS
;
338 exp
[numops
].X_add_number
= OPERAND_ATSIGN
;
347 /* just skip the trailing paren */
352 input_line_pointer
= p
;
355 /* check to see if it might be a register name */
356 if (!register_name (&exp
[numops
]))
358 /* parse as an expression */
359 expression (&exp
[numops
]);
362 if (exp
[numops
].X_op
== O_illegal
)
363 as_bad ("illegal operand");
364 else if (exp
[numops
].X_op
== O_absent
)
365 as_bad ("missing operand");
368 p
= input_line_pointer
;
373 case -1: /* postdecrement mode */
374 exp
[numops
].X_op
= O_absent
;
375 exp
[numops
++].X_add_number
= OPERAND_MINUS
;
377 case 1: /* postincrement mode */
378 exp
[numops
].X_op
= O_absent
;
379 exp
[numops
++].X_add_number
= OPERAND_PLUS
;
383 exp
[numops
].X_op
= 0;
388 d10v_insert_operand (insn
, op_type
, value
, left
)
396 shift
= d10v_operands
[op_type
].shift
;
400 bits
= d10v_operands
[op_type
].bits
;
402 /* truncate to the proper number of bits */
403 /* FIXME: overflow checking here? */
405 if (check_range (value
, bits
, d10v_operands
[op_type
].flags
& OPERAND_SIGNED
))
406 as_bad("operand out of range: %d",value
);
408 value
&= 0x7FFFFFFF >> (31 - bits
);
409 insn
|= (value
<< shift
);
415 /* build_insn takes a pointer to the opcode entry in the opcode table
416 and the array of operand expressions and returns the instruction */
419 build_insn (opcode
, opers
, insn
)
420 struct d10v_opcode
*opcode
;
424 int i
, bits
, shift
, flags
, format
;
427 /* the insn argument is only used for the DIVS kludge */
432 insn
= opcode
->opcode
;
433 format
= opcode
->format
;
436 for (i
=0;opcode
->operands
[i
];i
++)
438 flags
= d10v_operands
[opcode
->operands
[i
]].flags
;
439 bits
= d10v_operands
[opcode
->operands
[i
]].bits
;
440 shift
= d10v_operands
[opcode
->operands
[i
]].shift
;
441 number
= opers
[i
].X_add_number
;
443 if (flags
& OPERAND_REG
)
445 number
&= REGISTER_MASK
;
446 if (format
== LONG_L
)
450 if (opers
[i
].X_op
!= O_register
&& opers
[i
].X_op
!= O_constant
)
452 /* now create a fixup */
455 printf("need a fixup: ");
456 print_expr_1(stdout,&opers[i]);
460 if (fixups
->fc
>= MAX_INSN_FIXUPS
)
461 as_fatal ("too many fixups");
462 fixups
->fix
[fixups
->fc
].exp
= opers
[i
];
464 /* put the operand number here for now. We can look up
465 the reloc type and/or fixup the instruction in md_apply_fix() */
466 fixups
->fix
[fixups
->fc
].reloc
= opcode
->operands
[i
];
470 /* truncate to the proper number of bits */
471 if ((opers
[i
].X_op
== O_constant
) && check_range (number
, bits
, flags
& OPERAND_SIGNED
))
472 as_bad("operand out of range: %d",number
);
473 number
&= 0x7FFFFFFF >> (31 - bits
);
474 insn
= insn
| (number
<< shift
);
477 /* kludge: for DIVS, we need to put the operands in twice */
478 /* on the second pass, format is changed to LONG_R to force */
479 /* the second set of operands to not be shifted over 15 */
480 if ((opcode
->opcode
== OPCODE_DIVS
) && (format
==LONG_L
))
481 insn
= build_insn (opcode
, opers
, insn
);
486 /* write out a long form instruction */
488 write_long (opcode
, insn
, fx
)
489 struct d10v_opcode
*opcode
;
494 char *f
= frag_more(4);
497 /* printf("INSN: %08x\n",insn); */
498 number_to_chars_bigendian (f
, insn
, 4);
500 for (i
=0; i
< fx
->fc
; i
++)
502 if (get_reloc((struct d10v_operand
*)&d10v_operands
[fx
->fix
[i
].reloc
]))
505 printf("fix_new_exp: where:%x size:4\n ",f - frag_now->fr_literal);
506 print_expr_1(stdout,&(fx->fix[i].exp));
510 fix_new_exp (frag_now
,
511 f
- frag_now
->fr_literal
,
522 /* write out a short form instruction by itself */
524 write_1_short (opcode
, insn
, fx
)
525 struct d10v_opcode
*opcode
;
529 char *f
= frag_more(4);
532 /* the other container needs to be NOP */
533 /* according to 4.3.1: for FM=00, sub-instructions performed only
534 by IU cannot be encoded in L-container. */
535 if (opcode
->unit
== IU
)
536 insn
|= FM00
| (NOP
<< 15); /* right container */
538 insn
= FM00
| (insn
<< 15) | NOP
; /* left container */
540 /* printf("INSN: %08x\n",insn); */
541 number_to_chars_bigendian (f
, insn
, 4);
542 for (i
=0; i
< fx
->fc
; i
++)
544 if (get_reloc((struct d10v_operand
*)&d10v_operands
[fx
->fix
[i
].reloc
]))
547 printf("fix_new_exp: where:%x size:4\n ",f - frag_now->fr_literal);
548 print_expr_1(stdout,&(fx->fix[i].exp));
552 fix_new_exp (frag_now
,
553 f
- frag_now
->fr_literal
,
563 /* write out a short form instruction if possible */
564 /* return number of instructions not written out */
566 write_2_short (opcode1
, insn1
, opcode2
, insn2
, exec_type
, fx
)
567 struct d10v_opcode
*opcode1
, *opcode2
;
568 unsigned long insn1
, insn2
;
576 if(opcode1
->exec_type
== BRANCH_LINK
)
578 /* subroutines must be called from 32-bit boundaries */
579 /* so the return address will be correct */
580 write_1_short (opcode1
, insn1
, fx
->next
);
587 if (opcode1
->unit
== IU
)
589 /* reverse sequential */
590 insn
= FM10
| (insn2
<< 15) | insn1
;
594 insn
= FM01
| (insn1
<< 15) | insn2
;
598 case 1: /* parallel */
599 insn
= FM00
| (insn1
<< 15) | insn2
;
602 case 2: /* sequential */
603 insn
= FM01
| (insn1
<< 15) | insn2
;
606 case 3: /* reverse sequential */
607 insn
= FM10
| (insn1
<< 15) | insn2
;
610 as_fatal("unknown execution type passed to write_2_short()");
613 /* printf("INSN: %08x\n",insn); */
615 number_to_chars_bigendian (f
, insn
, 4);
619 bfd_reloc_code_real_type reloc
;
620 for (i
=0; i
< fx
->fc
; i
++)
622 reloc
= get_reloc((struct d10v_operand
*)&d10v_operands
[fx
->fix
[i
].reloc
]);
625 if ( (reloc
== BFD_RELOC_D10V_10_PCREL_R
) && (j
== 0) )
626 fx
->fix
[i
].reloc
|= 1024;
629 printf("fix_new_exp: where:%x reloc:%d\n ",f - frag_now->fr_literal,fx->fix[i].reloc);
630 print_expr_1(stdout,&(fx->fix[i].exp));
633 fix_new_exp (frag_now
,
634 f
- frag_now
->fr_literal
,
649 /* This is the main entry point for the machine-dependent assembler. str points to a
650 machine-dependent instruction. This function is supposed to emit the frags/bytes
651 it assembles to. For the D10V, it mostly handles the special VLIW parsing and packing
652 and leaves the difficult stuff to do_assemble().
655 static unsigned long prev_insn
;
656 static struct d10v_opcode
*prev_opcode
= 0;
657 static subsegT prev_subseg
;
658 static segT prev_seg
;
664 struct d10v_opcode
*opcode
;
669 /* printf("md_assemble: str=%s\n",str); */
671 /* look for the special multiple instruction seperators */
672 str2
= strstr (str
, "||");
677 str2
= strstr (str
, "->");
682 str2
= strstr (str
, "<-");
689 /* str2 points to the seperator, if one */
694 /* if two instructions are present and we already have one saved
695 then first write it out */
697 write_1_short (prev_opcode
, prev_insn
, fixups
->next
);
699 /* assemble first instruction and save it */
700 prev_insn
= do_assemble (str
, &prev_opcode
);
701 fixups
= fixups
->next
;
705 insn
= do_assemble (str
, &opcode
);
707 /* if this is a long instruction, write it and any previous short instruction */
708 if (opcode
->format
& LONG_OPCODE
)
711 as_fatal("Unable to mix instructions as specified");
714 write_1_short (prev_opcode
, prev_insn
, fixups
->next
);
717 write_long (opcode
, insn
, fixups
);
722 if (prev_opcode
&& (write_2_short (prev_opcode
, prev_insn
, opcode
, insn
, t
, fixups
) == 0))
724 /* no instructions saved */
730 as_fatal("Unable to mix instructions as specified");
731 /* save off last instruction so it may be packed on next pass */
732 prev_opcode
= opcode
;
735 prev_subseg
= now_subseg
;
736 fixups
= fixups
->next
;
742 do_assemble (str
, opcode
)
744 struct d10v_opcode
**opcode
;
746 struct d10v_opcode
*next_opcode
;
747 unsigned char *op_start
, *save
;
748 unsigned char *op_end
;
750 int nlen
= 0, i
, match
, numops
;
751 expressionS myops
[6];
754 /* printf("do_assemble: str=%s\n",str); */
756 /* Drop leading whitespace */
760 /* find the opcode end */
761 for (op_start
= op_end
= (unsigned char *) (str
);
764 && !is_end_of_line
[*op_end
] && *op_end
!= ' ';
767 name
[nlen
] = op_start
[nlen
];
773 as_bad ("can't find opcode ");
775 /* find the first opcode with the proper name */
776 *opcode
= (struct d10v_opcode
*)hash_find (d10v_hash
, name
);
779 as_fatal ("unknown opcode: %s",name
);
783 save
= input_line_pointer
;
784 input_line_pointer
= op_end
;
786 /* get all the operands and save them as expressions */
787 numops
= get_operands (myops
);
789 /* now see if the operand is a fake. If so, find the correct size */
790 /* instruction, if possible */
792 if ((*opcode
)->format
== OPCODE_FAKE
)
794 int opnum
= (*opcode
)->operands
[0];
795 if (myops
[opnum
].X_op
== O_constant
)
797 next_opcode
=(*opcode
)+1;
798 for (i
=0; (*opcode
)->operands
[i
+1]; i
++)
800 int bits
= d10v_operands
[next_opcode
->operands
[opnum
]].bits
;
801 int flags
= d10v_operands
[next_opcode
->operands
[opnum
]].flags
;
802 if (!check_range (myops
[opnum
].X_add_number
, bits
, flags
& OPERAND_SIGNED
))
812 /* not a constant, so use a long instruction */
813 next_opcode
= (*opcode
)+2;
817 *opcode
= next_opcode
;
819 as_fatal ("value out of range");
823 /* now search the opcode table table for one with operands */
824 /* that match what we've got */
828 for (i
= 0; (*opcode
)->operands
[i
]; i
++)
830 int flags
= d10v_operands
[(*opcode
)->operands
[i
]].flags
;
831 int X_op
= myops
[i
].X_op
;
832 int num
= myops
[i
].X_add_number
;
840 if (flags
& OPERAND_REG
)
842 if ((X_op
!= O_register
) ||
843 ((flags
& OPERAND_ACC
) != (num
& OPERAND_ACC
)) ||
844 ((flags
& OPERAND_FLAG
) != (num
& OPERAND_FLAG
)) ||
845 ((flags
& OPERAND_CONTROL
) != (num
& OPERAND_CONTROL
)))
852 if (((flags
& OPERAND_MINUS
) && ((X_op
!= O_absent
) || (num
!= OPERAND_MINUS
))) ||
853 ((flags
& OPERAND_PLUS
) && ((X_op
!= O_absent
) || (num
!= OPERAND_PLUS
))) ||
854 ((flags
& OPERAND_ATMINUS
) && ((X_op
!= O_absent
) || (num
!= OPERAND_ATMINUS
))) ||
855 ((flags
& OPERAND_ATPAR
) && ((X_op
!= O_absent
) || (num
!= OPERAND_ATPAR
))) ||
856 ((flags
& OPERAND_ATSIGN
) && ((X_op
!= O_absent
) || (num
!= OPERAND_ATSIGN
))))
864 /* we're only done if the operands matched AND there
865 are no more to check */
866 if (match
&& myops
[i
].X_op
==0)
869 next_opcode
= (*opcode
)+1;
870 if (next_opcode
->opcode
== 0)
872 if (strcmp(next_opcode
->name
, (*opcode
)->name
))
874 (*opcode
) = next_opcode
;
880 as_bad ("bad opcode or operands");
884 /* Check that all registers that are required to be even are. */
885 /* Also, if any operands were marked as registers, but were really symbols */
887 for (i
=0; (*opcode
)->operands
[i
]; i
++)
889 if ((d10v_operands
[(*opcode
)->operands
[i
]].flags
& OPERAND_EVEN
) &&
890 (myops
[i
].X_add_number
& 1))
891 as_fatal("Register number must be EVEN");
892 if (myops
[i
].X_op
== O_register
)
894 if (!(d10v_operands
[(*opcode
)->operands
[i
]].flags
& OPERAND_REG
))
896 myops
[i
].X_op
= O_symbol
;
897 myops
[i
].X_add_symbol
= symbol_find_or_make ((char *)myops
[i
].X_op_symbol
);
898 myops
[i
].X_add_number
= 0;
899 myops
[i
].X_op_symbol
= NULL
;
900 /* FIXME create a fixup */
905 input_line_pointer
= save
;
907 /* at this point, we have "opcode" pointing to the opcode entry in the
908 d10v opcode table, with myops filled out with the operands. */
909 insn
= build_insn ((*opcode
), myops
, 0);
910 /* printf("sub-insn = %lx\n",insn); */
916 /* if while processing a fixup, a reloc really needs to be created */
917 /* then it is done here */
920 tc_gen_reloc (seg
, fixp
)
925 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
926 reloc
->sym_ptr_ptr
= &fixp
->fx_addsy
->bsym
;
927 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
928 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
929 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
931 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
932 "reloc %d not supported by object file format", (int)fixp
->fx_r_type
);
935 reloc
->addend
= fixp
->fx_addnumber
;
936 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
941 md_estimate_size_before_relax (fragp
, seg
)
950 md_pcrel_from_section (fixp
, sec
)
955 /* return fixp->fx_frag->fr_address + fixp->fx_where; */
959 md_apply_fix3 (fixp
, valuep
, seg
)
970 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
975 else if (fixp
->fx_pcrel
)
979 value
= fixp
->fx_offset
;
980 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
982 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
983 value
-= S_GET_VALUE (fixp
->fx_subsy
);
986 /* We don't actually support subtracting a symbol. */
987 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
988 "expression too complex");
993 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
995 op_type
= fixp
->fx_r_type
;
999 fixp
->fx_r_type
= BFD_RELOC_D10V_10_PCREL_L
;
1003 fixp
->fx_r_type
= get_reloc((struct d10v_operand
*)&d10v_operands
[op_type
]);
1005 /* Fetch the instruction, insert the fully resolved operand
1006 value, and stuff the instruction back again. */
1007 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1008 insn
= bfd_getb32 ((unsigned char *) where
);
1010 switch (fixp
->fx_r_type
)
1012 case BFD_RELOC_D10V_10_PCREL_L
:
1013 case BFD_RELOC_D10V_10_PCREL_R
:
1014 case BFD_RELOC_D10V_18_PCREL
:
1015 /* instruction addresses are always right-shifted by 2
1017 if (!fixp
->fx_pcrel
)
1018 value
-= fixp
->fx_where
;
1023 /* printf(" insn=%x value=%x where=%x pcrel=%x\n",insn,value,fixp->fx_where,fixp->fx_pcrel); */
1025 insn
= d10v_insert_operand (insn
, op_type
, (offsetT
)value
, left
);
1027 /* printf(" new insn=%x\n",insn); */
1029 bfd_putb32 ((bfd_vma
) insn
, (unsigned char *) where
);
1034 fixp
->fx_addnumber
= value
;
1039 /* d10v_cleanup() is called after the assembler has finished parsing the input
1040 file or after a label is defined. Because the D10V assembler sometimes saves short
1041 instructions to see if it can package them with the next instruction, there may
1042 be a short instruction that still needs written. */
1050 if ( prev_opcode
&& (done
|| (now_seg
== prev_seg
) && (now_subseg
== prev_subseg
)))
1053 subseg
= now_subseg
;
1054 subseg_set (prev_seg
, prev_subseg
);
1055 write_1_short (prev_opcode
, prev_insn
, fixups
);
1056 subseg_set (seg
, subseg
);