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 */
54 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 static struct hash_control
*opcode_hash_control
; /* Opcode mnemonics */
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)];
105 /* This function is called once, at assembler startup time. This
106 should set up all the tables, etc that the MD part of the assembler
112 int *p
= xmode
? &X
: &M
;
113 while (*input_line_pointer
== ' ')
114 input_line_pointer
++;
115 if (strncmp (input_line_pointer
, "on", 2) == 0)
117 input_line_pointer
+= 2;
120 else if (strncmp (input_line_pointer
, "off", 3) == 0)
123 input_line_pointer
+= 3;
126 as_bad (_("need on or off."));
127 demand_empty_rest_of_line ();
134 struct opinfo
*opcode
;
135 char *prev_name
= "";
137 opcode_hash_control
= hash_new ();
139 /* Insert unique names into hash table. */
140 for (opcode
= optable
; opcode
->name
; opcode
++)
142 if (strcmp (prev_name
, opcode
->name
))
144 prev_name
= opcode
->name
;
145 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
149 /* Make all the opcodes with the same name point to the same
151 opcode
->name
= prev_name
;
155 /* Initialize the relax table. We use a local variable to avoid
156 warnings about modifying a supposedly const data structure. */
157 table
= (relax_typeS
*) md_relax_table
;
158 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_forward
= BYTE_F
;
159 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_backward
= BYTE_B
;
160 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_length
= 2;
161 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_more
= C (COND_BRANCH
, WORD_DISP
);
163 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_forward
= WORD_F
;
164 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_backward
= WORD_B
;
165 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_length
= 5;
166 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_more
= 0;
168 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_forward
= BYTE_F
;
169 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_backward
= BYTE_B
;
170 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_length
= 2;
171 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_more
= C (UNCOND_BRANCH
, WORD_DISP
);
173 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_forward
= WORD_F
;
174 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_backward
= WORD_B
;
175 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_length
= 3;
176 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_more
= 0;
178 flag_signed_overflow_ok
= 1;
181 static expressionS immediate
; /* absolute expression */
182 static expressionS immediate1
; /* absolute expression */
189 /* JF: '.' is pseudo symbol with value of current location
190 in current segment. */
191 fake
= FAKE_LABEL_NAME
;
192 return symbol_new (fake
,
194 (valueT
) frag_now_fix (),
204 w65_expression (dest
, bytes
)
211 while (*input_line_pointer
== ' ')
212 input_line_pointer
++;
214 if (*input_line_pointer
== '<')
217 input_line_pointer
++;
219 else if (*input_line_pointer
== '>')
222 input_line_pointer
++;
224 else if (*input_line_pointer
== '^')
227 input_line_pointer
++;
243 save
= input_line_pointer
;
244 input_line_pointer
= s
;
245 w65_expression (&immediate
, bytes
);
246 if (immediate
.X_op
== O_absent
)
247 as_bad (_("missing operand"));
248 new = input_line_pointer
;
249 input_line_pointer
= save
;
254 get_operands (info
, ptr
)
258 register int override_len
= 0;
259 register int bytes
= 0;
289 ptr
= parse_exp (ptr
);
291 else if (ptr
[0] == '!')
293 ptr
= parse_exp (ptr
+ 1);
298 amode
= ADDR_ABS_IDX_Y
;
302 else if (ptr
[1] == 'x')
304 amode
= ADDR_ABS_IDX_X
;
310 as_bad (_("syntax error after <exp"));
319 else if (ptr
[0] == '>')
321 ptr
= parse_exp (ptr
+ 1);
322 if (ptr
[0] == ',' && ptr
[1] == 'x')
324 amode
= ADDR_ABS_LONG_IDX_X
;
330 amode
= ADDR_ABS_LONG
;
334 else if (ptr
[0] == '<')
336 ptr
= parse_exp (ptr
+ 1);
341 amode
= ADDR_DIR_IDX_Y
;
345 else if (ptr
[1] == 'x')
347 amode
= ADDR_DIR_IDX_X
;
353 as_bad (_("syntax error after <exp"));
362 else if (ptr
[0] == 'a')
366 else if (ptr
[0] == '(')
387 else if (ptr
[0] == '!')
392 else if (ptr
[0] == '>')
401 ptr
= parse_exp (ptr
);
406 if (ptr
[0] == 'x' && ptr
[1] == ')')
410 if (override_len
== 1)
412 amode
= ADDR_DIR_IDX_IND_X
;
417 amode
= ADDR_ABS_IND_IDX
;
421 else if (ptr
[0] == 's' && ptr
[1] == ')'
422 && ptr
[2] == ',' && ptr
[3] == 'y')
424 amode
= ADDR_STACK_REL_INDX_IDX
;
429 else if (ptr
[0] == ')')
431 if (ptr
[1] == ',' && ptr
[2] == 'y')
433 amode
= ADDR_DIR_IND_IDX_Y
;
439 if (override_len
== 1)
441 amode
= ADDR_DIR_IND
;
446 amode
= ADDR_ABS_IND
;
454 else if (ptr
[0] == '[')
456 ptr
= parse_exp (ptr
+ 1);
460 if (ptr
[0] == ',' && ptr
[1] == 'y')
463 amode
= ADDR_DIR_IND_IDX_Y_LONG
;
468 if (info
->code
== O_jmp
)
471 amode
= ADDR_ABS_IND_LONG
;
476 amode
= ADDR_DIR_IND_LONG
;
483 ptr
= parse_exp (ptr
, 2);
488 if (override_len
== 1)
491 amode
= ADDR_DIR_IDX_Y
;
495 amode
= ADDR_ABS_IDX_Y
;
500 else if (ptr
[1] == 'x')
502 if (override_len
== 1)
504 amode
= ADDR_DIR_IDX_X
;
509 amode
= ADDR_ABS_IDX_X
;
514 else if (ptr
[1] == 's')
517 amode
= ADDR_STACK_REL
;
523 immediate1
= immediate
;
524 ptr
= parse_exp (ptr
+ 1);
525 amode
= ADDR_BLOCK_MOVE
;
536 case ADDR_PC_REL_LONG
:
537 amode
= ADDR_PC_REL_LONG
;
541 if (override_len
== 1)
546 else if (override_len
== 3)
549 amode
= ADDR_ABS_LONG
;
566 if (amode
== ADDR_DIR
)
567 tc_cons_reloc
= R_W65_DP
;
569 tc_cons_reloc
= R_W65_ABS8
;
572 tc_cons_reloc
= R_W65_ABS8S8
;
575 tc_cons_reloc
= R_W65_ABS8S16
;
583 tc_cons_reloc
= R_W65_ABS16
;
586 tc_cons_reloc
= R_W65_ABS16S8
;
589 tc_cons_reloc
= R_W65_ABS16S16
;
596 /* Passed a pointer to a list of opcodes which use different
597 addressing modes, return the opcode which matches the opcodes
600 static struct opinfo
*
601 get_specific (opcode
)
602 struct opinfo
*opcode
;
604 int ocode
= opcode
->code
;
606 for (; opcode
->code
== ocode
; opcode
++)
608 if (opcode
->amode
== amode
)
615 check (operand
, low
, high
)
616 expressionS
*operand
;
620 if (operand
->X_op
!= O_constant
621 || operand
->X_add_number
< low
622 || operand
->X_add_number
> high
)
624 as_bad ("operand must be absolute in range %d..%d", low
, high
);
626 return operand
->X_add_number
;
629 static int log2
[] = { 0, 0, 1, 0, 2 };
631 /* Now we know what sort of opcodes it is, let's build the bytes. */
635 struct opinfo
*opcode
;
642 if (opcode
->amode
== ADDR_IMPLIED
)
644 output
= frag_more (1);
646 else if (opcode
->amode
== ADDR_PC_REL
)
650 /* This is a relaxable insn, so we do some special handling. */
651 type
= opcode
->val
== OP_BRA
? UNCOND_BRANCH
: COND_BRANCH
;
652 output
= frag_var (rs_machine_dependent
,
653 md_relax_table
[C (type
, WORD_DISP
)].rlx_length
,
654 md_relax_table
[C (type
, BYTE_DISP
)].rlx_length
,
655 C (type
, UNDEF_BYTE_DISP
),
656 immediate
.X_add_symbol
,
657 immediate
.X_add_number
,
662 switch (opcode
->amode
)
664 GETINFO (size
, type
, pcrel
);
667 /* If something special was done in the expression modify the
670 type
= tc_cons_reloc
;
672 /* 1 byte for the opcode + the bytes for the addrmode. */
673 output
= frag_more (size
+ 1);
675 if (opcode
->amode
== ADDR_BLOCK_MOVE
)
677 /* Two relocs for this one. */
678 fix_new_exp (frag_now
,
679 output
+ 1 - frag_now
->fr_literal
,
685 fix_new_exp (frag_now
,
686 output
+ 2 - frag_now
->fr_literal
,
693 && opcode
->amode
!= ADDR_IMPLIED
694 && opcode
->amode
!= ADDR_ACC
695 && opcode
->amode
!= ADDR_STACK
)
697 fix_new_exp (frag_now
,
698 output
+ 1 - frag_now
->fr_literal
,
705 output
[0] = opcode
->val
;
708 /* This is the guts of the machine-dependent assembler. STR points to
709 a machine dependent instruction. This function is supposed to emit
710 the frags/bytes it assembles to. */
716 unsigned char *op_start
;
717 unsigned char *op_end
;
718 struct opinfo
*opcode
;
723 /* Drop leading whitespace */
727 /* all opcodes are three letters */
735 opcode
= (struct opinfo
*) hash_find (opcode_hash_control
, name
);
739 as_bad (_("unknown opcode"));
743 if (opcode
->amode
!= ADDR_IMPLIED
744 && opcode
->amode
!= ADDR_STACK
)
746 get_operands (opcode
, str
);
747 opcode
= get_specific (opcode
);
752 /* Couldn't find an opcode which matched the operands. */
754 char *where
= frag_more (1);
758 as_bad (_("invalid operands for opcode"));
762 build_Mytes (opcode
);
766 tc_crawl_symbol_chain (headers
)
767 object_headers
*headers
;
769 printf (_("call to tc_crawl_symbol_chain \n"));
773 md_undefined_symbol (name
)
780 tc_headers_hook (headers
)
781 object_headers
*headers
;
783 printf (_("call to tc_headers_hook \n"));
786 /* Various routines to kill one day. */
787 /* Equal to MAX_PRECISION in atof-ieee.c. */
788 #define MAX_LITTLENUMS 6
790 /* Turn a string in input_line_pointer into a floating point constant
791 of type TYPE, and store the appropriate bytes in *LITP. The number
792 of LITTLENUMS emitted is stored in *SIZEP. An error message is
793 returned, or NULL on OK. */
796 md_atof (type
, litP
, sizeP
)
802 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
803 LITTLENUM_TYPE
*wordP
;
835 return _("Bad call to MD_NTOF()");
837 t
= atof_ieee (input_line_pointer
, type
, words
);
839 input_line_pointer
= t
;
841 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
842 for (wordP
= words
+ prec
- 1; prec
--;)
844 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
845 litP
+= sizeof (LITTLENUM_TYPE
);
851 md_parse_option (c
, a
)
859 tc_Nout_fix_to_chars ()
861 printf (_("call to tc_Nout_fix_to_chars \n"));
865 /* Called after relaxing, change the frags so they know how big they
869 md_convert_frag (headers
, seg
, fragP
)
870 object_headers
*headers
;
876 unsigned char *buffer
=
877 (unsigned char *) (fragP
->fr_fix
+ fragP
->fr_literal
);
879 switch (fragP
->fr_subtype
)
881 case C (COND_BRANCH
, BYTE_DISP
):
882 case C (UNCOND_BRANCH
, BYTE_DISP
):
887 /* Conditional branches to a known 16 bit displacement. */
888 case C (COND_BRANCH
, WORD_DISP
):
899 /* Invert the sense of the test */
901 buffer
[1] = 3; /* Jump over following brl */
912 case C (UNCOND_BRANCH
, WORD_DISP
):
913 /* Unconditional branches to a known 16 bit displacement. */
926 /* Got to create a branch over a reloc here. */
927 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
928 buffer
[0] ^= 0x20; /* invert test */
941 fragP
->fr_fix
+= disp_size
+ inst_size
;
944 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
956 fragP
->fr_fix
+= disp_size
+ inst_size
;
964 /* Get the address of the end of the instruction. */
965 int next_inst
= (fragP
->fr_fix
+ fragP
->fr_address
966 + disp_size
+ inst_size
);
967 int targ_addr
= (S_GET_VALUE (fragP
->fr_symbol
) +
969 int disp
= targ_addr
- next_inst
;
971 md_number_to_chars (buffer
+ inst_size
, disp
, disp_size
);
972 fragP
->fr_fix
+= disp_size
+ inst_size
;
978 md_section_align (seg
, size
)
982 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
983 & (-1 << section_alignment
[(int) seg
]));
987 md_apply_fix (fixP
, val
)
991 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
992 int addr
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
994 if (fixP
->fx_r_type
== 0)
996 if (fixP
->fx_size
== 1)
997 fixP
->fx_r_type
= R_W65_ABS8
;
999 fixP
->fx_r_type
= R_W65_ABS16
;
1002 switch (fixP
->fx_r_type
)
1011 case R_W65_ABS16S16
:
1025 *buf
++ = val
- addr
- 1;
1028 val
= val
- addr
- 1;
1041 /* Put number into target byte order */
1044 md_number_to_chars (ptr
, use
, nbytes
)
1049 number_to_chars_littleendian (ptr
, use
, nbytes
);
1053 md_pcrel_from (fixP
)
1056 int gap
= fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
- 1;
1061 tc_coff_symbol_emit_hook (x
)
1067 tc_coff_fix2rtype (fix_ptr
)
1070 return fix_ptr
->fx_r_type
;
1074 tc_reloc_mangle (fix_ptr
, intr
, base
)
1076 struct internal_reloc
*intr
;
1080 symbolS
*symbol_ptr
;
1082 symbol_ptr
= fix_ptr
->fx_addsy
;
1084 /* If this relocation is attached to a symbol then it's ok
1086 if (fix_ptr
->fx_r_type
== RELOC_32
)
1088 /* cons likes to create reloc32's whatever the size of the reloc..
1090 switch (fix_ptr
->fx_size
)
1093 intr
->r_type
= R_IMM16
;
1096 intr
->r_type
= R_IMM8
;
1104 if (fix_ptr
->fx_size
== 4)
1105 intr
->r_type
= R_W65_ABS24
;
1107 intr
->r_type
= fix_ptr
->fx_r_type
;
1110 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1111 intr
->r_offset
= fix_ptr
->fx_offset
;
1113 /* Turn the segment of the symbol into an offset. */
1118 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
1121 intr
->r_offset
+= S_GET_VALUE (symbol_ptr
);
1122 intr
->r_symndx
= dot
->sy_number
;
1126 intr
->r_symndx
= symbol_ptr
->sy_number
;
1131 intr
->r_symndx
= -1;
1136 tc_coff_sizemachdep (frag
)
1139 return md_relax_table
[frag
->fr_subtype
].rlx_length
;
1142 /* Called just before address relaxation, return the length by which a
1143 fragment must grow to reach it's destination. */
1146 md_estimate_size_before_relax (fragP
, segment_type
)
1147 register fragS
*fragP
;
1148 register segT segment_type
;
1150 int what
= GET_WHAT (fragP
->fr_subtype
);
1152 switch (fragP
->fr_subtype
)
1156 case C (COND_BRANCH
, UNDEF_BYTE_DISP
):
1157 case C (UNCOND_BRANCH
, UNDEF_BYTE_DISP
):
1158 /* Used to be a branch to somewhere which was unknown. */
1159 if (S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
1161 /* Got a symbol and it's defined in this segment, become byte
1162 sized - maybe it will fix up. */
1163 fragP
->fr_subtype
= C (what
, BYTE_DISP
);
1164 fragP
->fr_var
= md_relax_table
[C (what
, BYTE_DISP
)].rlx_length
;
1168 /* Its got a segment, but its not ours, so it will always be
1170 fragP
->fr_subtype
= C (what
, UNDEF_WORD_DISP
);
1171 fragP
->fr_var
= md_relax_table
[C (what
, WORD_DISP
)].rlx_length
;
1172 return md_relax_table
[C (what
, WORD_DISP
)].rlx_length
;
1175 return fragP
->fr_var
;
1178 CONST
char *md_shortopts
= "";
1179 struct option md_longopts
[] = {
1180 #define OPTION_RELAX (OPTION_MD_BASE)
1181 {NULL
, no_argument
, NULL
, 0}
1185 md_show_usage (stream
)
1190 size_t md_longopts_size
= sizeof (md_longopts
);