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 */
54 const pseudo_typeS md_pseudo_table
[] = {
57 {"longa", s_longa
, 0},
58 {"longi", s_longa
, 1},
63 void s_align_bytes ();
69 static int relax
; /* set if -relax seen */
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
130 int *p
= xmode
? &X
: &M
;
131 while (*input_line_pointer
== ' ')
132 input_line_pointer
++;
133 if (strncmp (input_line_pointer
, "on", 2) == 0)
135 input_line_pointer
+= 2;
138 else if (strncmp (input_line_pointer
, "off", 3) == 0)
141 input_line_pointer
+= 3;
144 as_bad (_("need on or off."));
145 demand_empty_rest_of_line ();
151 struct opinfo
*opcode
;
152 char *prev_name
= "";
154 opcode_hash_control
= hash_new ();
156 /* Insert unique names into hash table. */
157 for (opcode
= optable
; opcode
->name
; opcode
++)
159 if (strcmp (prev_name
, opcode
->name
))
161 prev_name
= opcode
->name
;
162 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
166 /* Make all the opcodes with the same name point to the same
168 opcode
->name
= prev_name
;
172 flag_signed_overflow_ok
= 1;
175 static expressionS immediate
; /* absolute expression */
176 static expressionS immediate1
; /* absolute expression */
183 /* JF: '.' is pseudo symbol with value of current location
184 in current segment. */
185 fake
= FAKE_LABEL_NAME
;
186 return symbol_new (fake
,
188 (valueT
) frag_now_fix (),
198 w65_expression (dest
, bytes
)
205 while (*input_line_pointer
== ' ')
206 input_line_pointer
++;
208 if (*input_line_pointer
== '<')
211 input_line_pointer
++;
213 else if (*input_line_pointer
== '>')
216 input_line_pointer
++;
218 else if (*input_line_pointer
== '^')
221 input_line_pointer
++;
237 save
= input_line_pointer
;
238 input_line_pointer
= s
;
239 w65_expression (&immediate
, bytes
);
240 if (immediate
.X_op
== O_absent
)
241 as_bad (_("missing operand"));
242 new = input_line_pointer
;
243 input_line_pointer
= save
;
248 get_operands (info
, ptr
)
252 register int override_len
= 0;
253 register int bytes
= 0;
283 ptr
= parse_exp (ptr
);
285 else if (ptr
[0] == '!')
287 ptr
= parse_exp (ptr
+ 1);
292 amode
= ADDR_ABS_IDX_Y
;
296 else if (ptr
[1] == 'x')
298 amode
= ADDR_ABS_IDX_X
;
304 as_bad (_("syntax error after <exp"));
313 else if (ptr
[0] == '>')
315 ptr
= parse_exp (ptr
+ 1);
316 if (ptr
[0] == ',' && ptr
[1] == 'x')
318 amode
= ADDR_ABS_LONG_IDX_X
;
324 amode
= ADDR_ABS_LONG
;
328 else if (ptr
[0] == '<')
330 ptr
= parse_exp (ptr
+ 1);
335 amode
= ADDR_DIR_IDX_Y
;
339 else if (ptr
[1] == 'x')
341 amode
= ADDR_DIR_IDX_X
;
347 as_bad (_("syntax error after <exp"));
356 else if (ptr
[0] == 'a')
360 else if (ptr
[0] == '(')
381 else if (ptr
[0] == '!')
386 else if (ptr
[0] == '>')
395 ptr
= parse_exp (ptr
);
400 if (ptr
[0] == 'x' && ptr
[1] == ')')
404 if (override_len
== 1)
406 amode
= ADDR_DIR_IDX_IND_X
;
411 amode
= ADDR_ABS_IND_IDX
;
415 else if (ptr
[0] == 's' && ptr
[1] == ')'
416 && ptr
[2] == ',' && ptr
[3] == 'y')
418 amode
= ADDR_STACK_REL_INDX_IDX
;
423 else if (ptr
[0] == ')')
425 if (ptr
[1] == ',' && ptr
[2] == 'y')
427 amode
= ADDR_DIR_IND_IDX_Y
;
433 if (override_len
== 1)
435 amode
= ADDR_DIR_IND
;
440 amode
= ADDR_ABS_IND
;
448 else if (ptr
[0] == '[')
450 ptr
= parse_exp (ptr
+ 1);
454 if (ptr
[0] == ',' && ptr
[1] == 'y')
457 amode
= ADDR_DIR_IND_IDX_Y_LONG
;
462 if (info
->code
== O_jmp
)
465 amode
= ADDR_ABS_IND_LONG
;
470 amode
= ADDR_DIR_IND_LONG
;
477 ptr
= parse_exp (ptr
, 2);
482 if (override_len
== 1)
485 amode
= ADDR_DIR_IDX_Y
;
489 amode
= ADDR_ABS_IDX_Y
;
494 else if (ptr
[1] == 'x')
496 if (override_len
== 1)
498 amode
= ADDR_DIR_IDX_X
;
503 amode
= ADDR_ABS_IDX_X
;
508 else if (ptr
[1] == 's')
511 amode
= ADDR_STACK_REL
;
517 immediate1
= immediate
;
518 ptr
= parse_exp (ptr
+ 1);
519 amode
= ADDR_BLOCK_MOVE
;
530 case ADDR_PC_REL_LONG
:
531 amode
= ADDR_PC_REL_LONG
;
535 if (override_len
== 1)
540 else if (override_len
== 3)
543 amode
= ADDR_ABS_LONG
;
560 if (amode
== ADDR_DIR
)
561 tc_cons_reloc
= R_W65_DP
;
563 tc_cons_reloc
= R_W65_ABS8
;
566 tc_cons_reloc
= R_W65_ABS8S8
;
569 tc_cons_reloc
= R_W65_ABS8S16
;
577 tc_cons_reloc
= R_W65_ABS16
;
580 tc_cons_reloc
= R_W65_ABS16S8
;
583 tc_cons_reloc
= R_W65_ABS16S16
;
590 /* Passed a pointer to a list of opcodes which use different
591 addressing modes, return the opcode which matches the opcodes
594 static struct opinfo
*
595 get_specific (opcode
)
596 struct opinfo
*opcode
;
598 int ocode
= opcode
->code
;
600 for (; opcode
->code
== ocode
; opcode
++)
602 if (opcode
->amode
== amode
)
609 check (operand
, low
, high
)
610 expressionS
*operand
;
614 if (operand
->X_op
!= O_constant
615 || operand
->X_add_number
< low
616 || operand
->X_add_number
> high
)
618 as_bad ("operand must be absolute in range %d..%d", low
, high
);
620 return operand
->X_add_number
;
623 static int log2
[] = { 0, 0, 1, 0, 2 };
625 /* Now we know what sort of opcodes it is, let's build the bytes. */
629 struct opinfo
*opcode
;
636 if (opcode
->amode
== ADDR_IMPLIED
)
638 output
= frag_more (1);
640 else if (opcode
->amode
== ADDR_PC_REL
)
644 /* This is a relaxable insn, so we do some special handling. */
645 type
= opcode
->val
== OP_BRA
? UNCOND_BRANCH
: COND_BRANCH
;
646 output
= frag_var (rs_machine_dependent
,
647 md_relax_table
[C (type
, WORD_DISP
)].rlx_length
,
648 md_relax_table
[C (type
, BYTE_DISP
)].rlx_length
,
649 C (type
, UNDEF_BYTE_DISP
),
650 immediate
.X_add_symbol
,
651 immediate
.X_add_number
,
656 switch (opcode
->amode
)
658 GETINFO (size
, type
, pcrel
);
661 /* If something special was done in the expression modify the
664 type
= tc_cons_reloc
;
666 /* 1 byte for the opcode + the bytes for the addrmode. */
667 output
= frag_more (size
+ 1);
669 if (opcode
->amode
== ADDR_BLOCK_MOVE
)
671 /* Two relocs for this one. */
672 fix_new_exp (frag_now
,
673 output
+ 1 - frag_now
->fr_literal
,
679 fix_new_exp (frag_now
,
680 output
+ 2 - frag_now
->fr_literal
,
687 && opcode
->amode
!= ADDR_IMPLIED
688 && opcode
->amode
!= ADDR_ACC
689 && opcode
->amode
!= ADDR_STACK
)
691 fix_new_exp (frag_now
,
692 output
+ 1 - frag_now
->fr_literal
,
699 output
[0] = opcode
->val
;
702 /* This is the guts of the machine-dependent assembler. STR points to
703 a machine dependent instruction. This function is supposed to emit
704 the frags/bytes it assembles to. */
710 unsigned char *op_start
;
711 unsigned char *op_end
;
712 struct opinfo
*opcode
;
717 /* Drop leading whitespace */
721 /* all opcodes are three letters */
729 opcode
= (struct opinfo
*) hash_find (opcode_hash_control
, name
);
733 as_bad (_("unknown opcode"));
737 if (opcode
->amode
!= ADDR_IMPLIED
738 && opcode
->amode
!= ADDR_STACK
)
740 get_operands (opcode
, str
);
741 opcode
= get_specific (opcode
);
746 /* Couldn't find an opcode which matched the operands. */
748 char *where
= frag_more (1);
752 as_bad (_("invalid operands for opcode"));
756 build_Mytes (opcode
);
760 tc_crawl_symbol_chain (headers
)
761 object_headers
*headers
;
763 printf (_("call to tc_crawl_symbol_chain \n"));
767 md_undefined_symbol (name
)
774 tc_headers_hook (headers
)
775 object_headers
*headers
;
777 printf (_("call to tc_headers_hook \n"));
780 /* Various routines to kill one day. */
781 /* Equal to MAX_PRECISION in atof-ieee.c. */
782 #define MAX_LITTLENUMS 6
784 /* Turn a string in input_line_pointer into a floating point constant
785 of type TYPE, and store the appropriate bytes in *LITP. The number
786 of LITTLENUMS emitted is stored in *SIZEP. An error message is
787 returned, or NULL on OK. */
790 md_atof (type
, litP
, sizeP
)
796 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
797 LITTLENUM_TYPE
*wordP
;
829 return _("Bad call to MD_NTOF()");
831 t
= atof_ieee (input_line_pointer
, type
, words
);
833 input_line_pointer
= t
;
835 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
836 for (wordP
= words
+ prec
- 1; prec
--;)
838 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
839 litP
+= sizeof (LITTLENUM_TYPE
);
845 md_parse_option (c
, a
)
853 tc_Nout_fix_to_chars ()
855 printf (_("call to tc_Nout_fix_to_chars \n"));
859 /* Called after relaxing, change the frags so they know how big they
863 md_convert_frag (headers
, seg
, fragP
)
864 object_headers
*headers
;
870 unsigned char *buffer
=
871 (unsigned char *) (fragP
->fr_fix
+ fragP
->fr_literal
);
873 switch (fragP
->fr_subtype
)
875 case C (COND_BRANCH
, BYTE_DISP
):
876 case C (UNCOND_BRANCH
, BYTE_DISP
):
881 /* Conditional branches to a known 16 bit displacement. */
882 case C (COND_BRANCH
, WORD_DISP
):
893 /* Invert the sense of the test */
895 buffer
[1] = 3; /* Jump over following brl */
906 case C (UNCOND_BRANCH
, WORD_DISP
):
907 /* Unconditional branches to a known 16 bit displacement. */
920 /* Got to create a branch over a reloc here. */
921 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
922 buffer
[0] ^= 0x20; /* invert test */
935 fragP
->fr_fix
+= disp_size
+ inst_size
;
938 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
950 fragP
->fr_fix
+= disp_size
+ inst_size
;
958 /* Get the address of the end of the instruction. */
959 int next_inst
= (fragP
->fr_fix
+ fragP
->fr_address
960 + disp_size
+ inst_size
);
961 int targ_addr
= (S_GET_VALUE (fragP
->fr_symbol
) +
963 int disp
= targ_addr
- next_inst
;
965 md_number_to_chars (buffer
+ inst_size
, disp
, disp_size
);
966 fragP
->fr_fix
+= disp_size
+ inst_size
;
972 md_section_align (seg
, size
)
976 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
977 & (-1 << section_alignment
[(int) seg
]));
981 md_apply_fix3 (fixP
, valP
, seg
)
984 segT seg ATTRIBUTE_UNUSED
;
986 long val
= * (long *) valP
;
987 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
988 int addr
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
990 if (fixP
->fx_r_type
== 0)
992 if (fixP
->fx_size
== 1)
993 fixP
->fx_r_type
= R_W65_ABS8
;
995 fixP
->fx_r_type
= R_W65_ABS16
;
998 switch (fixP
->fx_r_type
)
1007 case R_W65_ABS16S16
:
1021 *buf
++ = val
- addr
- 1;
1024 val
= val
- addr
- 1;
1036 if (fixP
->fx_addsy
== NULL
&& fixP
->fx_pcrel
== 0)
1040 /* Put number into target byte order. */
1043 md_number_to_chars (ptr
, use
, nbytes
)
1048 number_to_chars_littleendian (ptr
, use
, nbytes
);
1052 md_pcrel_from (fixP
)
1055 int gap
= fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
- 1;
1060 tc_coff_symbol_emit_hook (x
)
1066 tc_coff_fix2rtype (fix_ptr
)
1069 return fix_ptr
->fx_r_type
;
1073 tc_reloc_mangle (fix_ptr
, intr
, base
)
1075 struct internal_reloc
*intr
;
1079 symbolS
*symbol_ptr
;
1081 symbol_ptr
= fix_ptr
->fx_addsy
;
1083 /* If this relocation is attached to a symbol then it's ok
1085 if (fix_ptr
->fx_r_type
== RELOC_32
)
1087 /* cons likes to create reloc32's whatever the size of the reloc..
1089 switch (fix_ptr
->fx_size
)
1092 intr
->r_type
= R_IMM16
;
1095 intr
->r_type
= R_IMM8
;
1103 if (fix_ptr
->fx_size
== 4)
1104 intr
->r_type
= R_W65_ABS24
;
1106 intr
->r_type
= fix_ptr
->fx_r_type
;
1109 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1110 intr
->r_offset
= fix_ptr
->fx_offset
;
1112 /* Turn the segment of the symbol into an offset. */
1117 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
1120 intr
->r_offset
+= S_GET_VALUE (symbol_ptr
);
1121 intr
->r_symndx
= dot
->sy_number
;
1125 intr
->r_symndx
= symbol_ptr
->sy_number
;
1130 intr
->r_symndx
= -1;
1135 tc_coff_sizemachdep (frag
)
1138 return md_relax_table
[frag
->fr_subtype
].rlx_length
;
1141 /* Called just before address relaxation, return the length by which a
1142 fragment must grow to reach it's destination. */
1145 md_estimate_size_before_relax (fragP
, segment_type
)
1146 register fragS
*fragP
;
1147 register segT segment_type
;
1151 switch (fragP
->fr_subtype
)
1156 case C (COND_BRANCH
, UNDEF_BYTE_DISP
):
1157 case C (UNCOND_BRANCH
, UNDEF_BYTE_DISP
):
1158 what
= GET_WHAT (fragP
->fr_subtype
);
1159 /* Used to be a branch to somewhere which was unknown. */
1160 if (S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
1162 /* Got a symbol and it's defined in this segment, become byte
1163 sized - maybe it will fix up. */
1164 fragP
->fr_subtype
= C (what
, BYTE_DISP
);
1168 /* Its got a segment, but its not ours, so it will always be
1170 fragP
->fr_subtype
= C (what
, UNDEF_WORD_DISP
);
1174 case C (COND_BRANCH
, BYTE_DISP
):
1175 case C (COND_BRANCH
, WORD_DISP
):
1176 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
1177 case C (UNCOND_BRANCH
, BYTE_DISP
):
1178 case C (UNCOND_BRANCH
, WORD_DISP
):
1179 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
1180 /* When relaxing a section for the second time, we don't need to
1181 do anything besides return the current size. */
1185 fragP
->fr_var
= md_relax_table
[fragP
->fr_subtype
].rlx_length
;
1186 return fragP
->fr_var
;
1189 const char *md_shortopts
= "";
1190 struct option md_longopts
[] = {
1191 #define OPTION_RELAX (OPTION_MD_BASE)
1192 {NULL
, no_argument
, NULL
, 0}
1196 md_show_usage (stream
)
1201 size_t md_longopts_size
= sizeof (md_longopts
);