1 /* tc-w65.c -- Assemble code for the W65816
2 Copyright (C) 1995, 1998, 2000 Free Software Foundation.
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)];
106 /* This function is called once, at assembler startup time. This
107 should set up all the tables, etc that the MD part of the assembler
113 int *p
= xmode
? &X
: &M
;
114 while (*input_line_pointer
== ' ')
115 input_line_pointer
++;
116 if (strncmp (input_line_pointer
, "on", 2) == 0)
118 input_line_pointer
+= 2;
121 else if (strncmp (input_line_pointer
, "off", 3) == 0)
124 input_line_pointer
+= 3;
127 as_bad (_("need on or off."));
128 demand_empty_rest_of_line ();
135 struct opinfo
*opcode
;
136 char *prev_name
= "";
138 opcode_hash_control
= hash_new ();
140 /* Insert unique names into hash table. */
141 for (opcode
= optable
; opcode
->name
; opcode
++)
143 if (strcmp (prev_name
, opcode
->name
))
145 prev_name
= opcode
->name
;
146 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
150 /* Make all the opcodes with the same name point to the same
152 opcode
->name
= prev_name
;
156 /* Initialize the relax table. We use a local variable to avoid
157 warnings about modifying a supposedly const data structure. */
158 table
= (relax_typeS
*) md_relax_table
;
159 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_forward
= BYTE_F
;
160 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_backward
= BYTE_B
;
161 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_length
= 2;
162 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_more
= C (COND_BRANCH
, WORD_DISP
);
164 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_forward
= WORD_F
;
165 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_backward
= WORD_B
;
166 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_length
= 5;
167 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_more
= 0;
169 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_forward
= BYTE_F
;
170 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_backward
= BYTE_B
;
171 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_length
= 2;
172 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_more
= C (UNCOND_BRANCH
, WORD_DISP
);
174 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_forward
= WORD_F
;
175 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_backward
= WORD_B
;
176 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_length
= 3;
177 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_more
= 0;
179 flag_signed_overflow_ok
= 1;
182 static expressionS immediate
; /* absolute expression */
183 static expressionS immediate1
; /* absolute expression */
190 /* JF: '.' is pseudo symbol with value of current location
191 in current segment. */
192 fake
= FAKE_LABEL_NAME
;
193 return symbol_new (fake
,
195 (valueT
) frag_now_fix (),
205 w65_expression (dest
, bytes
)
212 while (*input_line_pointer
== ' ')
213 input_line_pointer
++;
215 if (*input_line_pointer
== '<')
218 input_line_pointer
++;
220 else if (*input_line_pointer
== '>')
223 input_line_pointer
++;
225 else if (*input_line_pointer
== '^')
228 input_line_pointer
++;
244 save
= input_line_pointer
;
245 input_line_pointer
= s
;
246 w65_expression (&immediate
, bytes
);
247 if (immediate
.X_op
== O_absent
)
248 as_bad (_("missing operand"));
249 new = input_line_pointer
;
250 input_line_pointer
= save
;
255 get_operands (info
, ptr
)
259 register int override_len
= 0;
260 register int bytes
= 0;
290 ptr
= parse_exp (ptr
);
292 else if (ptr
[0] == '!')
294 ptr
= parse_exp (ptr
+ 1);
299 amode
= ADDR_ABS_IDX_Y
;
303 else if (ptr
[1] == 'x')
305 amode
= ADDR_ABS_IDX_X
;
311 as_bad (_("syntax error after <exp"));
320 else if (ptr
[0] == '>')
322 ptr
= parse_exp (ptr
+ 1);
323 if (ptr
[0] == ',' && ptr
[1] == 'x')
325 amode
= ADDR_ABS_LONG_IDX_X
;
331 amode
= ADDR_ABS_LONG
;
335 else if (ptr
[0] == '<')
337 ptr
= parse_exp (ptr
+ 1);
342 amode
= ADDR_DIR_IDX_Y
;
346 else if (ptr
[1] == 'x')
348 amode
= ADDR_DIR_IDX_X
;
354 as_bad (_("syntax error after <exp"));
363 else if (ptr
[0] == 'a')
367 else if (ptr
[0] == '(')
388 else if (ptr
[0] == '!')
393 else if (ptr
[0] == '>')
402 ptr
= parse_exp (ptr
);
407 if (ptr
[0] == 'x' && ptr
[1] == ')')
411 if (override_len
== 1)
413 amode
= ADDR_DIR_IDX_IND_X
;
418 amode
= ADDR_ABS_IND_IDX
;
422 else if (ptr
[0] == 's' && ptr
[1] == ')'
423 && ptr
[2] == ',' && ptr
[3] == 'y')
425 amode
= ADDR_STACK_REL_INDX_IDX
;
430 else if (ptr
[0] == ')')
432 if (ptr
[1] == ',' && ptr
[2] == 'y')
434 amode
= ADDR_DIR_IND_IDX_Y
;
440 if (override_len
== 1)
442 amode
= ADDR_DIR_IND
;
447 amode
= ADDR_ABS_IND
;
455 else if (ptr
[0] == '[')
457 ptr
= parse_exp (ptr
+ 1);
461 if (ptr
[0] == ',' && ptr
[1] == 'y')
464 amode
= ADDR_DIR_IND_IDX_Y_LONG
;
469 if (info
->code
== O_jmp
)
472 amode
= ADDR_ABS_IND_LONG
;
477 amode
= ADDR_DIR_IND_LONG
;
484 ptr
= parse_exp (ptr
, 2);
489 if (override_len
== 1)
492 amode
= ADDR_DIR_IDX_Y
;
496 amode
= ADDR_ABS_IDX_Y
;
501 else if (ptr
[1] == 'x')
503 if (override_len
== 1)
505 amode
= ADDR_DIR_IDX_X
;
510 amode
= ADDR_ABS_IDX_X
;
515 else if (ptr
[1] == 's')
518 amode
= ADDR_STACK_REL
;
524 immediate1
= immediate
;
525 ptr
= parse_exp (ptr
+ 1);
526 amode
= ADDR_BLOCK_MOVE
;
537 case ADDR_PC_REL_LONG
:
538 amode
= ADDR_PC_REL_LONG
;
542 if (override_len
== 1)
547 else if (override_len
== 3)
550 amode
= ADDR_ABS_LONG
;
567 if (amode
== ADDR_DIR
)
568 tc_cons_reloc
= R_W65_DP
;
570 tc_cons_reloc
= R_W65_ABS8
;
573 tc_cons_reloc
= R_W65_ABS8S8
;
576 tc_cons_reloc
= R_W65_ABS8S16
;
584 tc_cons_reloc
= R_W65_ABS16
;
587 tc_cons_reloc
= R_W65_ABS16S8
;
590 tc_cons_reloc
= R_W65_ABS16S16
;
597 /* Passed a pointer to a list of opcodes which use different
598 addressing modes, return the opcode which matches the opcodes
601 static struct opinfo
*
602 get_specific (opcode
)
603 struct opinfo
*opcode
;
605 int ocode
= opcode
->code
;
607 for (; opcode
->code
== ocode
; opcode
++)
609 if (opcode
->amode
== amode
)
616 check (operand
, low
, high
)
617 expressionS
*operand
;
621 if (operand
->X_op
!= O_constant
622 || operand
->X_add_number
< low
623 || operand
->X_add_number
> high
)
625 as_bad ("operand must be absolute in range %d..%d", low
, high
);
627 return operand
->X_add_number
;
630 static int log2
[] = { 0, 0, 1, 0, 2 };
632 /* Now we know what sort of opcodes it is, let's build the bytes. */
636 struct opinfo
*opcode
;
643 if (opcode
->amode
== ADDR_IMPLIED
)
645 output
= frag_more (1);
647 else if (opcode
->amode
== ADDR_PC_REL
)
651 /* This is a relaxable insn, so we do some special handling. */
652 type
= opcode
->val
== OP_BRA
? UNCOND_BRANCH
: COND_BRANCH
;
653 output
= frag_var (rs_machine_dependent
,
654 md_relax_table
[C (type
, WORD_DISP
)].rlx_length
,
655 md_relax_table
[C (type
, BYTE_DISP
)].rlx_length
,
656 C (type
, UNDEF_BYTE_DISP
),
657 immediate
.X_add_symbol
,
658 immediate
.X_add_number
,
663 switch (opcode
->amode
)
665 GETINFO (size
, type
, pcrel
);
668 /* If something special was done in the expression modify the
671 type
= tc_cons_reloc
;
673 /* 1 byte for the opcode + the bytes for the addrmode. */
674 output
= frag_more (size
+ 1);
676 if (opcode
->amode
== ADDR_BLOCK_MOVE
)
678 /* Two relocs for this one. */
679 fix_new_exp (frag_now
,
680 output
+ 1 - frag_now
->fr_literal
,
686 fix_new_exp (frag_now
,
687 output
+ 2 - frag_now
->fr_literal
,
694 && opcode
->amode
!= ADDR_IMPLIED
695 && opcode
->amode
!= ADDR_ACC
696 && opcode
->amode
!= ADDR_STACK
)
698 fix_new_exp (frag_now
,
699 output
+ 1 - frag_now
->fr_literal
,
706 output
[0] = opcode
->val
;
709 /* This is the guts of the machine-dependent assembler. STR points to
710 a machine dependent instruction. This function is supposed to emit
711 the frags/bytes it assembles to. */
717 unsigned char *op_start
;
718 unsigned char *op_end
;
719 struct opinfo
*opcode
;
724 /* Drop leading whitespace */
728 /* all opcodes are three letters */
736 opcode
= (struct opinfo
*) hash_find (opcode_hash_control
, name
);
740 as_bad (_("unknown opcode"));
744 if (opcode
->amode
!= ADDR_IMPLIED
745 && opcode
->amode
!= ADDR_STACK
)
747 get_operands (opcode
, str
);
748 opcode
= get_specific (opcode
);
753 /* Couldn't find an opcode which matched the operands. */
755 char *where
= frag_more (1);
759 as_bad (_("invalid operands for opcode"));
763 build_Mytes (opcode
);
767 tc_crawl_symbol_chain (headers
)
768 object_headers
*headers
;
770 printf (_("call to tc_crawl_symbol_chain \n"));
774 md_undefined_symbol (name
)
781 tc_headers_hook (headers
)
782 object_headers
*headers
;
784 printf (_("call to tc_headers_hook \n"));
787 /* Various routines to kill one day. */
788 /* Equal to MAX_PRECISION in atof-ieee.c. */
789 #define MAX_LITTLENUMS 6
791 /* Turn a string in input_line_pointer into a floating point constant
792 of type TYPE, and store the appropriate bytes in *LITP. The number
793 of LITTLENUMS emitted is stored in *SIZEP. An error message is
794 returned, or NULL on OK. */
797 md_atof (type
, litP
, sizeP
)
803 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
804 LITTLENUM_TYPE
*wordP
;
836 return _("Bad call to MD_NTOF()");
838 t
= atof_ieee (input_line_pointer
, type
, words
);
840 input_line_pointer
= t
;
842 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
843 for (wordP
= words
+ prec
- 1; prec
--;)
845 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
846 litP
+= sizeof (LITTLENUM_TYPE
);
852 md_parse_option (c
, a
)
860 tc_Nout_fix_to_chars ()
862 printf (_("call to tc_Nout_fix_to_chars \n"));
866 /* Called after relaxing, change the frags so they know how big they
870 md_convert_frag (headers
, seg
, fragP
)
871 object_headers
*headers
;
877 unsigned char *buffer
=
878 (unsigned char *) (fragP
->fr_fix
+ fragP
->fr_literal
);
880 switch (fragP
->fr_subtype
)
882 case C (COND_BRANCH
, BYTE_DISP
):
883 case C (UNCOND_BRANCH
, BYTE_DISP
):
888 /* Conditional branches to a known 16 bit displacement. */
889 case C (COND_BRANCH
, WORD_DISP
):
900 /* Invert the sense of the test */
902 buffer
[1] = 3; /* Jump over following brl */
913 case C (UNCOND_BRANCH
, WORD_DISP
):
914 /* Unconditional branches to a known 16 bit displacement. */
927 /* Got to create a branch over a reloc here. */
928 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
929 buffer
[0] ^= 0x20; /* invert test */
942 fragP
->fr_fix
+= disp_size
+ inst_size
;
945 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
957 fragP
->fr_fix
+= disp_size
+ inst_size
;
965 /* Get the address of the end of the instruction. */
966 int next_inst
= (fragP
->fr_fix
+ fragP
->fr_address
967 + disp_size
+ inst_size
);
968 int targ_addr
= (S_GET_VALUE (fragP
->fr_symbol
) +
970 int disp
= targ_addr
- next_inst
;
972 md_number_to_chars (buffer
+ inst_size
, disp
, disp_size
);
973 fragP
->fr_fix
+= disp_size
+ inst_size
;
979 md_section_align (seg
, size
)
983 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
984 & (-1 << section_alignment
[(int) seg
]));
988 md_apply_fix (fixP
, val
)
992 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
993 int addr
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
995 if (fixP
->fx_r_type
== 0)
997 if (fixP
->fx_size
== 1)
998 fixP
->fx_r_type
= R_W65_ABS8
;
1000 fixP
->fx_r_type
= R_W65_ABS16
;
1003 switch (fixP
->fx_r_type
)
1012 case R_W65_ABS16S16
:
1026 *buf
++ = val
- addr
- 1;
1029 val
= val
- addr
- 1;
1042 /* Put number into target byte order */
1045 md_number_to_chars (ptr
, use
, nbytes
)
1050 number_to_chars_littleendian (ptr
, use
, nbytes
);
1054 md_pcrel_from (fixP
)
1057 int gap
= fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
- 1;
1062 tc_coff_symbol_emit_hook (x
)
1068 tc_coff_fix2rtype (fix_ptr
)
1071 return fix_ptr
->fx_r_type
;
1075 tc_reloc_mangle (fix_ptr
, intr
, base
)
1077 struct internal_reloc
*intr
;
1081 symbolS
*symbol_ptr
;
1083 symbol_ptr
= fix_ptr
->fx_addsy
;
1085 /* If this relocation is attached to a symbol then it's ok
1087 if (fix_ptr
->fx_r_type
== RELOC_32
)
1089 /* cons likes to create reloc32's whatever the size of the reloc..
1091 switch (fix_ptr
->fx_size
)
1094 intr
->r_type
= R_IMM16
;
1097 intr
->r_type
= R_IMM8
;
1105 if (fix_ptr
->fx_size
== 4)
1106 intr
->r_type
= R_W65_ABS24
;
1108 intr
->r_type
= fix_ptr
->fx_r_type
;
1111 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1112 intr
->r_offset
= fix_ptr
->fx_offset
;
1114 /* Turn the segment of the symbol into an offset. */
1119 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
1122 intr
->r_offset
+= S_GET_VALUE (symbol_ptr
);
1123 intr
->r_symndx
= dot
->sy_number
;
1127 intr
->r_symndx
= symbol_ptr
->sy_number
;
1132 intr
->r_symndx
= -1;
1137 tc_coff_sizemachdep (frag
)
1140 return md_relax_table
[frag
->fr_subtype
].rlx_length
;
1143 /* Called just before address relaxation, return the length by which a
1144 fragment must grow to reach it's destination. */
1147 md_estimate_size_before_relax (fragP
, segment_type
)
1148 register fragS
*fragP
;
1149 register segT segment_type
;
1151 int what
= GET_WHAT (fragP
->fr_subtype
);
1153 switch (fragP
->fr_subtype
)
1157 case C (COND_BRANCH
, UNDEF_BYTE_DISP
):
1158 case C (UNCOND_BRANCH
, UNDEF_BYTE_DISP
):
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
);
1165 fragP
->fr_var
= md_relax_table
[C (what
, BYTE_DISP
)].rlx_length
;
1169 /* Its got a segment, but its not ours, so it will always be
1171 fragP
->fr_subtype
= C (what
, UNDEF_WORD_DISP
);
1172 fragP
->fr_var
= md_relax_table
[C (what
, WORD_DISP
)].rlx_length
;
1173 return md_relax_table
[C (what
, WORD_DISP
)].rlx_length
;
1176 return fragP
->fr_var
;
1179 CONST
char *md_shortopts
= "";
1180 struct option md_longopts
[] = {
1181 #define OPTION_RELAX (OPTION_MD_BASE)
1182 {NULL
, no_argument
, NULL
, 0}
1186 md_show_usage (stream
)
1191 size_t md_longopts_size
= sizeof (md_longopts
);