1 /* tc-w65.c -- Assemble code for the W65816
2 Copyright 1995, 1998, 2000, 2001 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"
31 const char comment_chars
[] = "!";
32 CONST
char line_separator_chars
[] = ";";
33 const char line_comment_chars
[] = "!#";
35 /* This table describes all the machine specific pseudo-ops the assembler
36 has to support. The fields are:
38 pseudo-op name without dot
39 function to call to execute this pseudo-op
40 Integer arg to pass to the function */
55 const pseudo_typeS md_pseudo_table
[] = {
58 {"longa", s_longa
, 0},
59 {"longi", s_longa
, 1},
64 void s_align_bytes ();
70 static int relax
; /* set if -relax seen */
72 const char EXP_CHARS
[] = "eE";
74 /* Chars that mean this number is a floating point constant. */
77 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
79 /* Opcode mnemonics */
80 static struct hash_control
*opcode_hash_control
;
85 #define C(a,b) ENCODE_RELAX(a,b)
86 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
88 #define GET_WHAT(x) ((x>>2))
92 #define UNDEF_BYTE_DISP 0
93 #define UNDEF_WORD_DISP 3
96 #define UNCOND_BRANCH 2
99 #define BYTE_F 127 /* How far we can branch forwards */
100 #define BYTE_B -126 /* How far we can branch backwards */
104 relax_typeS md_relax_table
[C (END
, 0)] = {
111 { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */
112 { BYTE_F
, BYTE_B
, 2, C (COND_BRANCH
, WORD_DISP
) }, /* BYTE_DISP */
113 { WORD_F
, WORD_B
, 5, 0 }, /* WORD_DISP */
114 { 0, 0, 5, 0 }, /* UNDEF_WORD_DISP */
117 { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */
118 { BYTE_F
, BYTE_B
, 2, C (UNCOND_BRANCH
, WORD_DISP
) }, /* BYTE_DISP */
119 { WORD_F
, WORD_B
, 3, 0 }, /* WORD_DISP */
120 { 0, 0, 3, 0 } /* UNDEF_WORD_DISP */
124 /* This function is called once, at assembler startup time. This
125 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 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 /* Make all the opcodes with the same name point to the same
169 opcode
->name
= prev_name
;
173 flag_signed_overflow_ok
= 1;
176 static expressionS immediate
; /* absolute expression */
177 static expressionS immediate1
; /* absolute expression */
184 /* JF: '.' is pseudo symbol with value of current location
185 in current segment. */
186 fake
= FAKE_LABEL_NAME
;
187 return symbol_new (fake
,
189 (valueT
) frag_now_fix (),
199 w65_expression (dest
, bytes
)
206 while (*input_line_pointer
== ' ')
207 input_line_pointer
++;
209 if (*input_line_pointer
== '<')
212 input_line_pointer
++;
214 else if (*input_line_pointer
== '>')
217 input_line_pointer
++;
219 else if (*input_line_pointer
== '^')
222 input_line_pointer
++;
238 save
= input_line_pointer
;
239 input_line_pointer
= s
;
240 w65_expression (&immediate
, bytes
);
241 if (immediate
.X_op
== O_absent
)
242 as_bad (_("missing operand"));
243 new = input_line_pointer
;
244 input_line_pointer
= save
;
249 get_operands (info
, ptr
)
253 register int override_len
= 0;
254 register int bytes
= 0;
284 ptr
= parse_exp (ptr
);
286 else if (ptr
[0] == '!')
288 ptr
= parse_exp (ptr
+ 1);
293 amode
= ADDR_ABS_IDX_Y
;
297 else if (ptr
[1] == 'x')
299 amode
= ADDR_ABS_IDX_X
;
305 as_bad (_("syntax error after <exp"));
314 else if (ptr
[0] == '>')
316 ptr
= parse_exp (ptr
+ 1);
317 if (ptr
[0] == ',' && ptr
[1] == 'x')
319 amode
= ADDR_ABS_LONG_IDX_X
;
325 amode
= ADDR_ABS_LONG
;
329 else if (ptr
[0] == '<')
331 ptr
= parse_exp (ptr
+ 1);
336 amode
= ADDR_DIR_IDX_Y
;
340 else if (ptr
[1] == 'x')
342 amode
= ADDR_DIR_IDX_X
;
348 as_bad (_("syntax error after <exp"));
357 else if (ptr
[0] == 'a')
361 else if (ptr
[0] == '(')
382 else if (ptr
[0] == '!')
387 else if (ptr
[0] == '>')
396 ptr
= parse_exp (ptr
);
401 if (ptr
[0] == 'x' && ptr
[1] == ')')
405 if (override_len
== 1)
407 amode
= ADDR_DIR_IDX_IND_X
;
412 amode
= ADDR_ABS_IND_IDX
;
416 else if (ptr
[0] == 's' && ptr
[1] == ')'
417 && ptr
[2] == ',' && ptr
[3] == 'y')
419 amode
= ADDR_STACK_REL_INDX_IDX
;
424 else if (ptr
[0] == ')')
426 if (ptr
[1] == ',' && ptr
[2] == 'y')
428 amode
= ADDR_DIR_IND_IDX_Y
;
434 if (override_len
== 1)
436 amode
= ADDR_DIR_IND
;
441 amode
= ADDR_ABS_IND
;
449 else if (ptr
[0] == '[')
451 ptr
= parse_exp (ptr
+ 1);
455 if (ptr
[0] == ',' && ptr
[1] == 'y')
458 amode
= ADDR_DIR_IND_IDX_Y_LONG
;
463 if (info
->code
== O_jmp
)
466 amode
= ADDR_ABS_IND_LONG
;
471 amode
= ADDR_DIR_IND_LONG
;
478 ptr
= parse_exp (ptr
, 2);
483 if (override_len
== 1)
486 amode
= ADDR_DIR_IDX_Y
;
490 amode
= ADDR_ABS_IDX_Y
;
495 else if (ptr
[1] == 'x')
497 if (override_len
== 1)
499 amode
= ADDR_DIR_IDX_X
;
504 amode
= ADDR_ABS_IDX_X
;
509 else if (ptr
[1] == 's')
512 amode
= ADDR_STACK_REL
;
518 immediate1
= immediate
;
519 ptr
= parse_exp (ptr
+ 1);
520 amode
= ADDR_BLOCK_MOVE
;
531 case ADDR_PC_REL_LONG
:
532 amode
= ADDR_PC_REL_LONG
;
536 if (override_len
== 1)
541 else if (override_len
== 3)
544 amode
= ADDR_ABS_LONG
;
561 if (amode
== ADDR_DIR
)
562 tc_cons_reloc
= R_W65_DP
;
564 tc_cons_reloc
= R_W65_ABS8
;
567 tc_cons_reloc
= R_W65_ABS8S8
;
570 tc_cons_reloc
= R_W65_ABS8S16
;
578 tc_cons_reloc
= R_W65_ABS16
;
581 tc_cons_reloc
= R_W65_ABS16S8
;
584 tc_cons_reloc
= R_W65_ABS16S16
;
591 /* Passed a pointer to a list of opcodes which use different
592 addressing modes, return the opcode which matches the opcodes
595 static struct opinfo
*
596 get_specific (opcode
)
597 struct opinfo
*opcode
;
599 int ocode
= opcode
->code
;
601 for (; opcode
->code
== ocode
; opcode
++)
603 if (opcode
->amode
== amode
)
610 check (operand
, low
, high
)
611 expressionS
*operand
;
615 if (operand
->X_op
!= O_constant
616 || operand
->X_add_number
< low
617 || operand
->X_add_number
> high
)
619 as_bad ("operand must be absolute in range %d..%d", low
, high
);
621 return operand
->X_add_number
;
624 static int log2
[] = { 0, 0, 1, 0, 2 };
626 /* Now we know what sort of opcodes it is, let's build the bytes. */
630 struct opinfo
*opcode
;
637 if (opcode
->amode
== ADDR_IMPLIED
)
639 output
= frag_more (1);
641 else if (opcode
->amode
== ADDR_PC_REL
)
645 /* This is a relaxable insn, so we do some special handling. */
646 type
= opcode
->val
== OP_BRA
? UNCOND_BRANCH
: COND_BRANCH
;
647 output
= frag_var (rs_machine_dependent
,
648 md_relax_table
[C (type
, WORD_DISP
)].rlx_length
,
649 md_relax_table
[C (type
, BYTE_DISP
)].rlx_length
,
650 C (type
, UNDEF_BYTE_DISP
),
651 immediate
.X_add_symbol
,
652 immediate
.X_add_number
,
657 switch (opcode
->amode
)
659 GETINFO (size
, type
, pcrel
);
662 /* If something special was done in the expression modify the
665 type
= tc_cons_reloc
;
667 /* 1 byte for the opcode + the bytes for the addrmode. */
668 output
= frag_more (size
+ 1);
670 if (opcode
->amode
== ADDR_BLOCK_MOVE
)
672 /* Two relocs for this one. */
673 fix_new_exp (frag_now
,
674 output
+ 1 - frag_now
->fr_literal
,
680 fix_new_exp (frag_now
,
681 output
+ 2 - frag_now
->fr_literal
,
688 && opcode
->amode
!= ADDR_IMPLIED
689 && opcode
->amode
!= ADDR_ACC
690 && opcode
->amode
!= ADDR_STACK
)
692 fix_new_exp (frag_now
,
693 output
+ 1 - frag_now
->fr_literal
,
700 output
[0] = opcode
->val
;
703 /* This is the guts of the machine-dependent assembler. STR points to
704 a machine dependent instruction. This function is supposed to emit
705 the frags/bytes it assembles to. */
711 unsigned char *op_start
;
712 unsigned char *op_end
;
713 struct opinfo
*opcode
;
718 /* Drop leading whitespace */
722 /* all opcodes are three letters */
730 opcode
= (struct opinfo
*) hash_find (opcode_hash_control
, name
);
734 as_bad (_("unknown opcode"));
738 if (opcode
->amode
!= ADDR_IMPLIED
739 && opcode
->amode
!= ADDR_STACK
)
741 get_operands (opcode
, str
);
742 opcode
= get_specific (opcode
);
747 /* Couldn't find an opcode which matched the operands. */
749 char *where
= frag_more (1);
753 as_bad (_("invalid operands for opcode"));
757 build_Mytes (opcode
);
761 tc_crawl_symbol_chain (headers
)
762 object_headers
*headers
;
764 printf (_("call to tc_crawl_symbol_chain \n"));
768 md_undefined_symbol (name
)
775 tc_headers_hook (headers
)
776 object_headers
*headers
;
778 printf (_("call to tc_headers_hook \n"));
781 /* Various routines to kill one day. */
782 /* Equal to MAX_PRECISION in atof-ieee.c. */
783 #define MAX_LITTLENUMS 6
785 /* Turn a string in input_line_pointer into a floating point constant
786 of type TYPE, and store the appropriate bytes in *LITP. The number
787 of LITTLENUMS emitted is stored in *SIZEP. An error message is
788 returned, or NULL on OK. */
791 md_atof (type
, litP
, sizeP
)
797 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
798 LITTLENUM_TYPE
*wordP
;
830 return _("Bad call to MD_NTOF()");
832 t
= atof_ieee (input_line_pointer
, type
, words
);
834 input_line_pointer
= t
;
836 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
837 for (wordP
= words
+ prec
- 1; prec
--;)
839 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
840 litP
+= sizeof (LITTLENUM_TYPE
);
846 md_parse_option (c
, a
)
854 tc_Nout_fix_to_chars ()
856 printf (_("call to tc_Nout_fix_to_chars \n"));
860 /* Called after relaxing, change the frags so they know how big they
864 md_convert_frag (headers
, seg
, fragP
)
865 object_headers
*headers
;
871 unsigned char *buffer
=
872 (unsigned char *) (fragP
->fr_fix
+ fragP
->fr_literal
);
874 switch (fragP
->fr_subtype
)
876 case C (COND_BRANCH
, BYTE_DISP
):
877 case C (UNCOND_BRANCH
, BYTE_DISP
):
882 /* Conditional branches to a known 16 bit displacement. */
883 case C (COND_BRANCH
, WORD_DISP
):
894 /* Invert the sense of the test */
896 buffer
[1] = 3; /* Jump over following brl */
907 case C (UNCOND_BRANCH
, WORD_DISP
):
908 /* Unconditional branches to a known 16 bit displacement. */
921 /* Got to create a branch over a reloc here. */
922 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
923 buffer
[0] ^= 0x20; /* invert test */
936 fragP
->fr_fix
+= disp_size
+ inst_size
;
939 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
951 fragP
->fr_fix
+= disp_size
+ inst_size
;
959 /* Get the address of the end of the instruction. */
960 int next_inst
= (fragP
->fr_fix
+ fragP
->fr_address
961 + disp_size
+ inst_size
);
962 int targ_addr
= (S_GET_VALUE (fragP
->fr_symbol
) +
964 int disp
= targ_addr
- next_inst
;
966 md_number_to_chars (buffer
+ inst_size
, disp
, disp_size
);
967 fragP
->fr_fix
+= disp_size
+ inst_size
;
973 md_section_align (seg
, size
)
977 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
978 & (-1 << section_alignment
[(int) seg
]));
982 md_apply_fix (fixP
, val
)
986 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
987 int addr
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
989 if (fixP
->fx_r_type
== 0)
991 if (fixP
->fx_size
== 1)
992 fixP
->fx_r_type
= R_W65_ABS8
;
994 fixP
->fx_r_type
= R_W65_ABS16
;
997 switch (fixP
->fx_r_type
)
1006 case R_W65_ABS16S16
:
1020 *buf
++ = val
- addr
- 1;
1023 val
= val
- addr
- 1;
1036 /* Put number into target byte order */
1039 md_number_to_chars (ptr
, use
, nbytes
)
1044 number_to_chars_littleendian (ptr
, use
, nbytes
);
1048 md_pcrel_from (fixP
)
1051 int gap
= fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
- 1;
1056 tc_coff_symbol_emit_hook (x
)
1062 tc_coff_fix2rtype (fix_ptr
)
1065 return fix_ptr
->fx_r_type
;
1069 tc_reloc_mangle (fix_ptr
, intr
, base
)
1071 struct internal_reloc
*intr
;
1075 symbolS
*symbol_ptr
;
1077 symbol_ptr
= fix_ptr
->fx_addsy
;
1079 /* If this relocation is attached to a symbol then it's ok
1081 if (fix_ptr
->fx_r_type
== RELOC_32
)
1083 /* cons likes to create reloc32's whatever the size of the reloc..
1085 switch (fix_ptr
->fx_size
)
1088 intr
->r_type
= R_IMM16
;
1091 intr
->r_type
= R_IMM8
;
1099 if (fix_ptr
->fx_size
== 4)
1100 intr
->r_type
= R_W65_ABS24
;
1102 intr
->r_type
= fix_ptr
->fx_r_type
;
1105 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1106 intr
->r_offset
= fix_ptr
->fx_offset
;
1108 /* Turn the segment of the symbol into an offset. */
1113 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
1116 intr
->r_offset
+= S_GET_VALUE (symbol_ptr
);
1117 intr
->r_symndx
= dot
->sy_number
;
1121 intr
->r_symndx
= symbol_ptr
->sy_number
;
1126 intr
->r_symndx
= -1;
1131 tc_coff_sizemachdep (frag
)
1134 return md_relax_table
[frag
->fr_subtype
].rlx_length
;
1137 /* Called just before address relaxation, return the length by which a
1138 fragment must grow to reach it's destination. */
1141 md_estimate_size_before_relax (fragP
, segment_type
)
1142 register fragS
*fragP
;
1143 register segT segment_type
;
1147 switch (fragP
->fr_subtype
)
1152 case C (COND_BRANCH
, UNDEF_BYTE_DISP
):
1153 case C (UNCOND_BRANCH
, UNDEF_BYTE_DISP
):
1154 what
= GET_WHAT (fragP
->fr_subtype
);
1155 /* Used to be a branch to somewhere which was unknown. */
1156 if (S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
1158 /* Got a symbol and it's defined in this segment, become byte
1159 sized - maybe it will fix up. */
1160 fragP
->fr_subtype
= C (what
, BYTE_DISP
);
1164 /* Its got a segment, but its not ours, so it will always be
1166 fragP
->fr_subtype
= C (what
, UNDEF_WORD_DISP
);
1170 case C (COND_BRANCH
, BYTE_DISP
):
1171 case C (COND_BRANCH
, WORD_DISP
):
1172 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
1173 case C (UNCOND_BRANCH
, BYTE_DISP
):
1174 case C (UNCOND_BRANCH
, WORD_DISP
):
1175 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
1176 /* When relaxing a section for the second time, we don't need to
1177 do anything besides return the current size. */
1181 fragP
->fr_var
= md_relax_table
[fragP
->fr_subtype
].rlx_length
;
1182 return fragP
->fr_var
;
1185 CONST
char *md_shortopts
= "";
1186 struct option md_longopts
[] = {
1187 #define OPTION_RELAX (OPTION_MD_BASE)
1188 {NULL
, no_argument
, NULL
, 0}
1192 md_show_usage (stream
)
1197 size_t md_longopts_size
= sizeof (md_longopts
);