1 /* tc-w65.c -- Assemble code for the W65816
2 Copyright 1995, 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 /* Written By Steve Chamberlain <sac@cygnus.com>. */
28 #include "../opcodes/w65-opc.h"
30 const char comment_chars
[] = "!";
31 const char line_separator_chars
[] = ";";
32 const char line_comment_chars
[] = "!#";
34 /* This table describes all the machine specific pseudo-ops the assembler
35 has to support. The fields are:
37 pseudo-op name without dot
38 function to call to execute this pseudo-op
39 Integer arg to pass to the function */
52 static void s_longa
PARAMS ((int));
53 static char *parse_exp
PARAMS ((char *));
54 static char *get_operands
PARAMS ((const struct opinfo
*, char *));
55 static const struct opinfo
*get_specific
PARAMS ((const struct opinfo
*));
56 static void build_Mytes
PARAMS ((const struct opinfo
*));
59 const pseudo_typeS md_pseudo_table
[] = {
62 {"longa", s_longa
, 0},
63 {"longi", s_longa
, 1},
71 const char EXP_CHARS
[] = "eE";
73 /* Chars that mean this number is a floating point constant. */
76 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
78 /* Opcode mnemonics */
79 static struct hash_control
*opcode_hash_control
;
84 #define C(a,b) ENCODE_RELAX(a,b)
85 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
87 #define GET_WHAT(x) ((x>>2))
91 #define UNDEF_BYTE_DISP 0
92 #define UNDEF_WORD_DISP 3
95 #define UNCOND_BRANCH 2
98 #define BYTE_F 127 /* How far we can branch forwards */
99 #define BYTE_B -126 /* How far we can branch backwards */
103 relax_typeS md_relax_table
[C (END
, 0)] = {
110 { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */
111 { BYTE_F
, BYTE_B
, 2, C (COND_BRANCH
, WORD_DISP
) }, /* BYTE_DISP */
112 { WORD_F
, WORD_B
, 5, 0 }, /* WORD_DISP */
113 { 0, 0, 5, 0 }, /* UNDEF_WORD_DISP */
116 { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */
117 { BYTE_F
, BYTE_B
, 2, C (UNCOND_BRANCH
, WORD_DISP
) }, /* BYTE_DISP */
118 { WORD_F
, WORD_B
, 3, 0 }, /* WORD_DISP */
119 { 0, 0, 3, 0 } /* UNDEF_WORD_DISP */
123 /* This function is called once, at assembler startup time. This
124 should set up all the tables, etc that the MD part of the assembler
131 int *p
= xmode
? &X
: &M
;
132 while (*input_line_pointer
== ' ')
133 input_line_pointer
++;
134 if (strncmp (input_line_pointer
, "on", 2) == 0)
136 input_line_pointer
+= 2;
139 else if (strncmp (input_line_pointer
, "off", 3) == 0)
142 input_line_pointer
+= 3;
145 as_bad (_("need on or off."));
146 demand_empty_rest_of_line ();
152 const struct opinfo
*opcode
;
153 char *prev_name
= "";
155 opcode_hash_control
= hash_new ();
157 /* Insert unique names into hash table. */
158 for (opcode
= optable
; opcode
->name
; opcode
++)
160 if (strcmp (prev_name
, opcode
->name
))
162 prev_name
= opcode
->name
;
163 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
167 flag_signed_overflow_ok
= 1;
170 static expressionS immediate
; /* absolute expression */
171 static expressionS immediate1
; /* absolute expression */
177 w65_expression (dest
)
183 while (*input_line_pointer
== ' ')
184 input_line_pointer
++;
186 if (*input_line_pointer
== '<')
189 input_line_pointer
++;
191 else if (*input_line_pointer
== '>')
194 input_line_pointer
++;
196 else if (*input_line_pointer
== '^')
199 input_line_pointer
++;
214 save
= input_line_pointer
;
215 input_line_pointer
= s
;
216 w65_expression (&immediate
);
217 if (immediate
.X_op
== O_absent
)
218 as_bad (_("missing operand"));
219 new = input_line_pointer
;
220 input_line_pointer
= save
;
225 get_operands (info
, ptr
)
226 const struct opinfo
*info
;
229 register int override_len
= 0;
230 register int bytes
= 0;
260 ptr
= parse_exp (ptr
);
262 else if (ptr
[0] == '!')
264 ptr
= parse_exp (ptr
+ 1);
269 amode
= ADDR_ABS_IDX_Y
;
273 else if (ptr
[1] == 'x')
275 amode
= ADDR_ABS_IDX_X
;
281 as_bad (_("syntax error after <exp"));
290 else if (ptr
[0] == '>')
292 ptr
= parse_exp (ptr
+ 1);
293 if (ptr
[0] == ',' && ptr
[1] == 'x')
295 amode
= ADDR_ABS_LONG_IDX_X
;
301 amode
= ADDR_ABS_LONG
;
305 else if (ptr
[0] == '<')
307 ptr
= parse_exp (ptr
+ 1);
312 amode
= ADDR_DIR_IDX_Y
;
316 else if (ptr
[1] == 'x')
318 amode
= ADDR_DIR_IDX_X
;
324 as_bad (_("syntax error after <exp"));
333 else if (ptr
[0] == 'a')
337 else if (ptr
[0] == '(')
358 else if (ptr
[0] == '!')
363 else if (ptr
[0] == '>')
372 ptr
= parse_exp (ptr
);
377 if (ptr
[0] == 'x' && ptr
[1] == ')')
381 if (override_len
== 1)
383 amode
= ADDR_DIR_IDX_IND_X
;
388 amode
= ADDR_ABS_IND_IDX
;
392 else if (ptr
[0] == 's' && ptr
[1] == ')'
393 && ptr
[2] == ',' && ptr
[3] == 'y')
395 amode
= ADDR_STACK_REL_INDX_IDX
;
400 else if (ptr
[0] == ')')
402 if (ptr
[1] == ',' && ptr
[2] == 'y')
404 amode
= ADDR_DIR_IND_IDX_Y
;
410 if (override_len
== 1)
412 amode
= ADDR_DIR_IND
;
417 amode
= ADDR_ABS_IND
;
425 else if (ptr
[0] == '[')
427 ptr
= parse_exp (ptr
+ 1);
431 if (ptr
[0] == ',' && ptr
[1] == 'y')
434 amode
= ADDR_DIR_IND_IDX_Y_LONG
;
439 if (info
->code
== O_jmp
)
442 amode
= ADDR_ABS_IND_LONG
;
447 amode
= ADDR_DIR_IND_LONG
;
454 ptr
= parse_exp (ptr
);
459 if (override_len
== 1)
462 amode
= ADDR_DIR_IDX_Y
;
466 amode
= ADDR_ABS_IDX_Y
;
471 else if (ptr
[1] == 'x')
473 if (override_len
== 1)
475 amode
= ADDR_DIR_IDX_X
;
480 amode
= ADDR_ABS_IDX_X
;
485 else if (ptr
[1] == 's')
488 amode
= ADDR_STACK_REL
;
494 immediate1
= immediate
;
495 ptr
= parse_exp (ptr
+ 1);
496 amode
= ADDR_BLOCK_MOVE
;
507 case ADDR_PC_REL_LONG
:
508 amode
= ADDR_PC_REL_LONG
;
512 if (override_len
== 1)
517 else if (override_len
== 3)
520 amode
= ADDR_ABS_LONG
;
537 if (amode
== ADDR_DIR
)
538 tc_cons_reloc
= R_W65_DP
;
540 tc_cons_reloc
= R_W65_ABS8
;
543 tc_cons_reloc
= R_W65_ABS8S8
;
546 tc_cons_reloc
= R_W65_ABS8S16
;
554 tc_cons_reloc
= R_W65_ABS16
;
557 tc_cons_reloc
= R_W65_ABS16S8
;
560 tc_cons_reloc
= R_W65_ABS16S16
;
567 /* Passed a pointer to a list of opcodes which use different
568 addressing modes, return the opcode which matches the opcodes
571 static const struct opinfo
*
572 get_specific (opcode
)
573 const struct opinfo
*opcode
;
575 int ocode
= opcode
->code
;
577 for (; opcode
->code
== ocode
; opcode
++)
579 if (opcode
->amode
== amode
)
585 /* Now we know what sort of opcodes it is, let's build the bytes. */
589 const struct opinfo
*opcode
;
596 if (opcode
->amode
== ADDR_IMPLIED
)
598 output
= frag_more (1);
600 else if (opcode
->amode
== ADDR_PC_REL
)
604 /* This is a relaxable insn, so we do some special handling. */
605 type
= opcode
->val
== OP_BRA
? UNCOND_BRANCH
: COND_BRANCH
;
606 output
= frag_var (rs_machine_dependent
,
607 md_relax_table
[C (type
, WORD_DISP
)].rlx_length
,
608 md_relax_table
[C (type
, BYTE_DISP
)].rlx_length
,
609 C (type
, UNDEF_BYTE_DISP
),
610 immediate
.X_add_symbol
,
611 immediate
.X_add_number
,
616 switch (opcode
->amode
)
618 GETINFO (size
, type
, pcrel
);
623 /* If something special was done in the expression modify the
626 type
= tc_cons_reloc
;
628 /* 1 byte for the opcode + the bytes for the addrmode. */
629 output
= frag_more (size
+ 1);
631 if (opcode
->amode
== ADDR_BLOCK_MOVE
)
633 /* Two relocs for this one. */
634 fix_new_exp (frag_now
,
635 output
+ 1 - frag_now
->fr_literal
,
641 fix_new_exp (frag_now
,
642 output
+ 2 - frag_now
->fr_literal
,
649 && opcode
->amode
!= ADDR_IMPLIED
650 && opcode
->amode
!= ADDR_ACC
651 && opcode
->amode
!= ADDR_STACK
)
653 fix_new_exp (frag_now
,
654 output
+ 1 - frag_now
->fr_literal
,
661 output
[0] = opcode
->val
;
664 /* This is the guts of the machine-dependent assembler. STR points to
665 a machine dependent instruction. This function is supposed to emit
666 the frags/bytes it assembles to. */
672 const struct opinfo
*opcode
;
675 /* Drop leading whitespace */
679 /* all opcodes are three letters */
687 opcode
= (struct opinfo
*) hash_find (opcode_hash_control
, name
);
691 as_bad (_("unknown opcode"));
695 if (opcode
->amode
!= ADDR_IMPLIED
696 && opcode
->amode
!= ADDR_STACK
)
698 get_operands (opcode
, str
);
699 opcode
= get_specific (opcode
);
704 /* Couldn't find an opcode which matched the operands. */
706 char *where
= frag_more (1);
710 as_bad (_("invalid operands for opcode"));
714 build_Mytes (opcode
);
718 md_undefined_symbol (name
)
719 char *name ATTRIBUTE_UNUSED
;
724 /* Various routines to kill one day. */
725 /* Equal to MAX_PRECISION in atof-ieee.c. */
726 #define MAX_LITTLENUMS 6
728 /* Turn a string in input_line_pointer into a floating point constant
729 of type TYPE, and store the appropriate bytes in *LITP. The number
730 of LITTLENUMS emitted is stored in *SIZEP. An error message is
731 returned, or NULL on OK. */
734 md_atof (type
, litP
, sizeP
)
740 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
741 LITTLENUM_TYPE
*wordP
;
772 return _("Bad call to MD_NTOF()");
774 t
= atof_ieee (input_line_pointer
, type
, words
);
776 input_line_pointer
= t
;
778 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
779 for (wordP
= words
+ prec
- 1; prec
--;)
781 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
782 litP
+= sizeof (LITTLENUM_TYPE
);
788 md_parse_option (c
, a
)
789 int c ATTRIBUTE_UNUSED
;
790 char *a ATTRIBUTE_UNUSED
;
795 /* Called after relaxing, change the frags so they know how big they
799 md_convert_frag (headers
, seg
, fragP
)
800 object_headers
*headers ATTRIBUTE_UNUSED
;
801 segT seg ATTRIBUTE_UNUSED
;
806 unsigned char *buffer
=
807 (unsigned char *) (fragP
->fr_fix
+ fragP
->fr_literal
);
809 switch (fragP
->fr_subtype
)
811 case C (COND_BRANCH
, BYTE_DISP
):
812 case C (UNCOND_BRANCH
, BYTE_DISP
):
817 /* Conditional branches to a known 16 bit displacement. */
818 case C (COND_BRANCH
, WORD_DISP
):
829 /* Invert the sense of the test */
831 buffer
[1] = 3; /* Jump over following brl */
842 case C (UNCOND_BRANCH
, WORD_DISP
):
843 /* Unconditional branches to a known 16 bit displacement. */
856 /* Got to create a branch over a reloc here. */
857 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
858 buffer
[0] ^= 0x20; /* invert test */
871 fragP
->fr_fix
+= disp_size
+ inst_size
;
874 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
886 fragP
->fr_fix
+= disp_size
+ inst_size
;
894 /* Get the address of the end of the instruction. */
895 int next_inst
= (fragP
->fr_fix
+ fragP
->fr_address
896 + disp_size
+ inst_size
);
897 int targ_addr
= (S_GET_VALUE (fragP
->fr_symbol
) +
899 int disp
= targ_addr
- next_inst
;
901 md_number_to_chars (buffer
+ inst_size
, disp
, disp_size
);
902 fragP
->fr_fix
+= disp_size
+ inst_size
;
908 md_section_align (seg
, size
)
912 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
913 & (-1 << section_alignment
[(int) seg
]));
917 md_apply_fix3 (fixP
, valP
, seg
)
920 segT seg ATTRIBUTE_UNUSED
;
922 long val
= * (long *) valP
;
923 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
924 int addr
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
926 if (fixP
->fx_r_type
== 0)
928 if (fixP
->fx_size
== 1)
929 fixP
->fx_r_type
= R_W65_ABS8
;
931 fixP
->fx_r_type
= R_W65_ABS16
;
934 switch (fixP
->fx_r_type
)
957 *buf
++ = val
- addr
- 1;
960 val
= val
- addr
- 1;
972 if (fixP
->fx_addsy
== NULL
&& fixP
->fx_pcrel
== 0)
976 /* Put number into target byte order. */
979 md_number_to_chars (ptr
, use
, nbytes
)
984 number_to_chars_littleendian (ptr
, use
, nbytes
);
991 int gap
= fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
- 1;
996 tc_coff_symbol_emit_hook (x
)
997 symbolS
*x ATTRIBUTE_UNUSED
;
1002 tc_coff_fix2rtype (fix_ptr
)
1005 return fix_ptr
->fx_r_type
;
1009 tc_reloc_mangle (fix_ptr
, intr
, base
)
1011 struct internal_reloc
*intr
;
1015 symbolS
*symbol_ptr
;
1017 symbol_ptr
= fix_ptr
->fx_addsy
;
1019 /* If this relocation is attached to a symbol then it's ok
1021 if (fix_ptr
->fx_r_type
== RELOC_32
)
1023 /* cons likes to create reloc32's whatever the size of the reloc..
1025 switch (fix_ptr
->fx_size
)
1028 intr
->r_type
= R_IMM16
;
1031 intr
->r_type
= R_IMM8
;
1039 if (fix_ptr
->fx_size
== 4)
1040 intr
->r_type
= R_W65_ABS24
;
1042 intr
->r_type
= fix_ptr
->fx_r_type
;
1045 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1046 intr
->r_offset
= fix_ptr
->fx_offset
;
1048 /* Turn the segment of the symbol into an offset. */
1053 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
1056 intr
->r_offset
+= S_GET_VALUE (symbol_ptr
);
1057 intr
->r_symndx
= dot
->sy_number
;
1061 intr
->r_symndx
= symbol_ptr
->sy_number
;
1066 intr
->r_symndx
= -1;
1071 tc_coff_sizemachdep (frag
)
1074 return md_relax_table
[frag
->fr_subtype
].rlx_length
;
1077 /* Called just before address relaxation, return the length by which a
1078 fragment must grow to reach it's destination. */
1081 md_estimate_size_before_relax (fragP
, segment_type
)
1082 register fragS
*fragP
;
1083 register segT segment_type
;
1087 switch (fragP
->fr_subtype
)
1092 case C (COND_BRANCH
, UNDEF_BYTE_DISP
):
1093 case C (UNCOND_BRANCH
, UNDEF_BYTE_DISP
):
1094 what
= GET_WHAT (fragP
->fr_subtype
);
1095 /* Used to be a branch to somewhere which was unknown. */
1096 if (S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
1098 /* Got a symbol and it's defined in this segment, become byte
1099 sized - maybe it will fix up. */
1100 fragP
->fr_subtype
= C (what
, BYTE_DISP
);
1104 /* Its got a segment, but its not ours, so it will always be
1106 fragP
->fr_subtype
= C (what
, UNDEF_WORD_DISP
);
1110 case C (COND_BRANCH
, BYTE_DISP
):
1111 case C (COND_BRANCH
, WORD_DISP
):
1112 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
1113 case C (UNCOND_BRANCH
, BYTE_DISP
):
1114 case C (UNCOND_BRANCH
, WORD_DISP
):
1115 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
1116 /* When relaxing a section for the second time, we don't need to
1117 do anything besides return the current size. */
1121 fragP
->fr_var
= md_relax_table
[fragP
->fr_subtype
].rlx_length
;
1122 return fragP
->fr_var
;
1125 const char *md_shortopts
= "";
1126 struct option md_longopts
[] = {
1127 #define OPTION_RELAX (OPTION_MD_BASE)
1128 {NULL
, no_argument
, NULL
, 0}
1132 md_show_usage (stream
)
1133 FILE *stream ATTRIBUTE_UNUSED
;
1137 size_t md_longopts_size
= sizeof (md_longopts
);