1 /* tc-w65.c -- Assemble code for the W65816
2 Copyright 1995, 1998, 2000, 2001, 2002, 2005
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
29 #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 */
53 static void s_longa
PARAMS ((int));
54 static char *parse_exp
PARAMS ((char *));
55 static char *get_operands
PARAMS ((const struct opinfo
*, char *));
56 static const struct opinfo
*get_specific
PARAMS ((const struct opinfo
*));
57 static void build_Mytes
PARAMS ((const struct opinfo
*));
60 const pseudo_typeS md_pseudo_table
[] = {
63 {"longa", s_longa
, 0},
64 {"longi", s_longa
, 1},
68 const char EXP_CHARS
[] = "eE";
70 /* Chars that mean this number is a floating point constant. */
73 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
75 /* Opcode mnemonics */
76 static struct hash_control
*opcode_hash_control
;
81 #define C(a,b) ENCODE_RELAX(a,b)
82 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
84 #define GET_WHAT(x) ((x>>2))
88 #define UNDEF_BYTE_DISP 0
89 #define UNDEF_WORD_DISP 3
92 #define UNCOND_BRANCH 2
95 #define BYTE_F 127 /* How far we can branch forwards */
96 #define BYTE_B -126 /* How far we can branch backwards */
100 relax_typeS md_relax_table
[C (END
, 0)] = {
107 { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */
108 { BYTE_F
, BYTE_B
, 2, C (COND_BRANCH
, WORD_DISP
) }, /* BYTE_DISP */
109 { WORD_F
, WORD_B
, 5, 0 }, /* WORD_DISP */
110 { 0, 0, 5, 0 }, /* UNDEF_WORD_DISP */
113 { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */
114 { BYTE_F
, BYTE_B
, 2, C (UNCOND_BRANCH
, WORD_DISP
) }, /* BYTE_DISP */
115 { WORD_F
, WORD_B
, 3, 0 }, /* WORD_DISP */
116 { 0, 0, 3, 0 } /* UNDEF_WORD_DISP */
120 /* This function is called once, at assembler startup time. This
121 should set up all the tables, etc that the MD part of the assembler
128 int *p
= xmode
? &X
: &M
;
129 while (*input_line_pointer
== ' ')
130 input_line_pointer
++;
131 if (strncmp (input_line_pointer
, "on", 2) == 0)
133 input_line_pointer
+= 2;
136 else if (strncmp (input_line_pointer
, "off", 3) == 0)
139 input_line_pointer
+= 3;
142 as_bad (_("need on or off."));
143 demand_empty_rest_of_line ();
149 const struct opinfo
*opcode
;
150 char *prev_name
= "";
152 opcode_hash_control
= hash_new ();
154 /* Insert unique names into hash table. */
155 for (opcode
= optable
; opcode
->name
; opcode
++)
157 if (strcmp (prev_name
, opcode
->name
))
159 prev_name
= opcode
->name
;
160 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
164 flag_signed_overflow_ok
= 1;
167 static expressionS immediate
; /* absolute expression */
168 static expressionS immediate1
; /* absolute expression */
174 w65_expression (dest
)
180 while (*input_line_pointer
== ' ')
181 input_line_pointer
++;
183 if (*input_line_pointer
== '<')
186 input_line_pointer
++;
188 else if (*input_line_pointer
== '>')
191 input_line_pointer
++;
193 else if (*input_line_pointer
== '^')
196 input_line_pointer
++;
211 save
= input_line_pointer
;
212 input_line_pointer
= s
;
213 w65_expression (&immediate
);
214 if (immediate
.X_op
== O_absent
)
215 as_bad (_("missing operand"));
216 new = input_line_pointer
;
217 input_line_pointer
= save
;
222 get_operands (info
, ptr
)
223 const struct opinfo
*info
;
226 register int override_len
= 0;
227 register int bytes
= 0;
257 ptr
= parse_exp (ptr
);
259 else if (ptr
[0] == '!')
261 ptr
= parse_exp (ptr
+ 1);
266 amode
= ADDR_ABS_IDX_Y
;
270 else if (ptr
[1] == 'x')
272 amode
= ADDR_ABS_IDX_X
;
278 as_bad (_("syntax error after <exp"));
287 else if (ptr
[0] == '>')
289 ptr
= parse_exp (ptr
+ 1);
290 if (ptr
[0] == ',' && ptr
[1] == 'x')
292 amode
= ADDR_ABS_LONG_IDX_X
;
298 amode
= ADDR_ABS_LONG
;
302 else if (ptr
[0] == '<')
304 ptr
= parse_exp (ptr
+ 1);
309 amode
= ADDR_DIR_IDX_Y
;
313 else if (ptr
[1] == 'x')
315 amode
= ADDR_DIR_IDX_X
;
321 as_bad (_("syntax error after <exp"));
330 else if (ptr
[0] == 'a')
334 else if (ptr
[0] == '(')
355 else if (ptr
[0] == '!')
360 else if (ptr
[0] == '>')
369 ptr
= parse_exp (ptr
);
374 if (ptr
[0] == 'x' && ptr
[1] == ')')
378 if (override_len
== 1)
380 amode
= ADDR_DIR_IDX_IND_X
;
385 amode
= ADDR_ABS_IND_IDX
;
389 else if (ptr
[0] == 's' && ptr
[1] == ')'
390 && ptr
[2] == ',' && ptr
[3] == 'y')
392 amode
= ADDR_STACK_REL_INDX_IDX
;
397 else if (ptr
[0] == ')')
399 if (ptr
[1] == ',' && ptr
[2] == 'y')
401 amode
= ADDR_DIR_IND_IDX_Y
;
407 if (override_len
== 1)
409 amode
= ADDR_DIR_IND
;
414 amode
= ADDR_ABS_IND
;
422 else if (ptr
[0] == '[')
424 ptr
= parse_exp (ptr
+ 1);
428 if (ptr
[0] == ',' && ptr
[1] == 'y')
431 amode
= ADDR_DIR_IND_IDX_Y_LONG
;
436 if (info
->code
== O_jmp
)
439 amode
= ADDR_ABS_IND_LONG
;
444 amode
= ADDR_DIR_IND_LONG
;
451 ptr
= parse_exp (ptr
);
456 if (override_len
== 1)
459 amode
= ADDR_DIR_IDX_Y
;
463 amode
= ADDR_ABS_IDX_Y
;
468 else if (ptr
[1] == 'x')
470 if (override_len
== 1)
472 amode
= ADDR_DIR_IDX_X
;
477 amode
= ADDR_ABS_IDX_X
;
482 else if (ptr
[1] == 's')
485 amode
= ADDR_STACK_REL
;
491 immediate1
= immediate
;
492 ptr
= parse_exp (ptr
+ 1);
493 amode
= ADDR_BLOCK_MOVE
;
504 case ADDR_PC_REL_LONG
:
505 amode
= ADDR_PC_REL_LONG
;
509 if (override_len
== 1)
514 else if (override_len
== 3)
517 amode
= ADDR_ABS_LONG
;
534 if (amode
== ADDR_DIR
)
535 tc_cons_reloc
= R_W65_DP
;
537 tc_cons_reloc
= R_W65_ABS8
;
540 tc_cons_reloc
= R_W65_ABS8S8
;
543 tc_cons_reloc
= R_W65_ABS8S16
;
551 tc_cons_reloc
= R_W65_ABS16
;
554 tc_cons_reloc
= R_W65_ABS16S8
;
557 tc_cons_reloc
= R_W65_ABS16S16
;
564 /* Passed a pointer to a list of opcodes which use different
565 addressing modes, return the opcode which matches the opcodes
568 static const struct opinfo
*
569 get_specific (opcode
)
570 const struct opinfo
*opcode
;
572 int ocode
= opcode
->code
;
574 for (; opcode
->code
== ocode
; opcode
++)
576 if (opcode
->amode
== amode
)
582 /* Now we know what sort of opcodes it is, let's build the bytes. */
586 const struct opinfo
*opcode
;
593 if (opcode
->amode
== ADDR_IMPLIED
)
595 output
= frag_more (1);
597 else if (opcode
->amode
== ADDR_PC_REL
)
601 /* This is a relaxable insn, so we do some special handling. */
602 type
= opcode
->val
== OP_BRA
? UNCOND_BRANCH
: COND_BRANCH
;
603 output
= frag_var (rs_machine_dependent
,
604 md_relax_table
[C (type
, WORD_DISP
)].rlx_length
,
605 md_relax_table
[C (type
, BYTE_DISP
)].rlx_length
,
606 C (type
, UNDEF_BYTE_DISP
),
607 immediate
.X_add_symbol
,
608 immediate
.X_add_number
,
613 switch (opcode
->amode
)
615 GETINFO (size
, type
, pcrel
);
620 /* If something special was done in the expression modify the
623 type
= tc_cons_reloc
;
625 /* 1 byte for the opcode + the bytes for the addrmode. */
626 output
= frag_more (size
+ 1);
628 if (opcode
->amode
== ADDR_BLOCK_MOVE
)
630 /* Two relocs for this one. */
631 fix_new_exp (frag_now
,
632 output
+ 1 - frag_now
->fr_literal
,
638 fix_new_exp (frag_now
,
639 output
+ 2 - frag_now
->fr_literal
,
646 && opcode
->amode
!= ADDR_IMPLIED
647 && opcode
->amode
!= ADDR_ACC
648 && opcode
->amode
!= ADDR_STACK
)
650 fix_new_exp (frag_now
,
651 output
+ 1 - frag_now
->fr_literal
,
658 output
[0] = opcode
->val
;
661 /* This is the guts of the machine-dependent assembler. STR points to
662 a machine dependent instruction. This function is supposed to emit
663 the frags/bytes it assembles to. */
669 const struct opinfo
*opcode
;
672 /* Drop leading whitespace */
676 /* all opcodes are three letters */
684 opcode
= (struct opinfo
*) hash_find (opcode_hash_control
, name
);
688 as_bad (_("unknown opcode"));
692 if (opcode
->amode
!= ADDR_IMPLIED
693 && opcode
->amode
!= ADDR_STACK
)
695 get_operands (opcode
, str
);
696 opcode
= get_specific (opcode
);
701 /* Couldn't find an opcode which matched the operands. */
703 char *where
= frag_more (1);
707 as_bad (_("invalid operands for opcode"));
711 build_Mytes (opcode
);
715 md_undefined_symbol (name
)
716 char *name ATTRIBUTE_UNUSED
;
721 /* Various routines to kill one day. */
722 /* Equal to MAX_PRECISION in atof-ieee.c. */
723 #define MAX_LITTLENUMS 6
725 /* Turn a string in input_line_pointer into a floating point constant
726 of type TYPE, and store the appropriate bytes in *LITP. The number
727 of LITTLENUMS emitted is stored in *SIZEP. An error message is
728 returned, or NULL on OK. */
731 md_atof (type
, litP
, sizeP
)
737 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
738 LITTLENUM_TYPE
*wordP
;
769 return _("Bad call to MD_NTOF()");
771 t
= atof_ieee (input_line_pointer
, type
, words
);
773 input_line_pointer
= t
;
775 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
776 for (wordP
= words
+ prec
- 1; prec
--;)
778 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
779 litP
+= sizeof (LITTLENUM_TYPE
);
785 md_parse_option (c
, a
)
786 int c ATTRIBUTE_UNUSED
;
787 char *a ATTRIBUTE_UNUSED
;
792 /* Called after relaxing, change the frags so they know how big they
796 md_convert_frag (headers
, seg
, fragP
)
797 object_headers
*headers ATTRIBUTE_UNUSED
;
798 segT seg ATTRIBUTE_UNUSED
;
803 unsigned char *buffer
=
804 (unsigned char *) (fragP
->fr_fix
+ fragP
->fr_literal
);
806 switch (fragP
->fr_subtype
)
808 case C (COND_BRANCH
, BYTE_DISP
):
809 case C (UNCOND_BRANCH
, BYTE_DISP
):
814 /* Conditional branches to a known 16 bit displacement. */
815 case C (COND_BRANCH
, WORD_DISP
):
826 /* Invert the sense of the test */
828 buffer
[1] = 3; /* Jump over following brl */
839 case C (UNCOND_BRANCH
, WORD_DISP
):
840 /* Unconditional branches to a known 16 bit displacement. */
853 /* Got to create a branch over a reloc here. */
854 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
855 buffer
[0] ^= 0x20; /* invert test */
868 fragP
->fr_fix
+= disp_size
+ inst_size
;
871 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
883 fragP
->fr_fix
+= disp_size
+ inst_size
;
891 /* Get the address of the end of the instruction. */
892 int next_inst
= (fragP
->fr_fix
+ fragP
->fr_address
893 + disp_size
+ inst_size
);
894 int targ_addr
= (S_GET_VALUE (fragP
->fr_symbol
) +
896 int disp
= targ_addr
- next_inst
;
898 md_number_to_chars ((char *) buffer
+ inst_size
, disp
, disp_size
);
899 fragP
->fr_fix
+= disp_size
+ inst_size
;
905 md_section_align (seg
, size
)
909 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
910 & (-1 << section_alignment
[(int) seg
]));
914 md_apply_fix3 (fixP
, valP
, seg
)
917 segT seg ATTRIBUTE_UNUSED
;
919 long val
= * (long *) valP
;
920 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
921 int addr
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
923 if (fixP
->fx_r_type
== 0)
925 if (fixP
->fx_size
== 1)
926 fixP
->fx_r_type
= R_W65_ABS8
;
928 fixP
->fx_r_type
= R_W65_ABS16
;
931 switch (fixP
->fx_r_type
)
954 *buf
++ = val
- addr
- 1;
957 val
= val
- addr
- 1;
969 if (fixP
->fx_addsy
== NULL
&& fixP
->fx_pcrel
== 0)
973 /* Put number into target byte order. */
976 md_number_to_chars (ptr
, use
, nbytes
)
981 number_to_chars_littleendian (ptr
, use
, nbytes
);
988 int gap
= fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
- 1;
993 tc_coff_symbol_emit_hook (x
)
994 symbolS
*x ATTRIBUTE_UNUSED
;
999 tc_coff_fix2rtype (fix_ptr
)
1002 return fix_ptr
->fx_r_type
;
1006 tc_reloc_mangle (fix_ptr
, intr
, base
)
1008 struct internal_reloc
*intr
;
1012 symbolS
*symbol_ptr
;
1014 symbol_ptr
= fix_ptr
->fx_addsy
;
1016 /* If this relocation is attached to a symbol then it's ok
1018 if (fix_ptr
->fx_r_type
== RELOC_32
)
1020 /* cons likes to create reloc32's whatever the size of the reloc..
1022 switch (fix_ptr
->fx_size
)
1025 intr
->r_type
= R_IMM16
;
1028 intr
->r_type
= R_IMM8
;
1036 if (fix_ptr
->fx_size
== 4)
1037 intr
->r_type
= R_W65_ABS24
;
1039 intr
->r_type
= fix_ptr
->fx_r_type
;
1042 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1043 intr
->r_offset
= fix_ptr
->fx_offset
;
1045 /* Turn the segment of the symbol into an offset. */
1050 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
1053 intr
->r_offset
+= S_GET_VALUE (symbol_ptr
);
1054 intr
->r_symndx
= dot
->sy_number
;
1058 intr
->r_symndx
= symbol_ptr
->sy_number
;
1063 intr
->r_symndx
= -1;
1068 tc_coff_sizemachdep (frag
)
1071 return md_relax_table
[frag
->fr_subtype
].rlx_length
;
1074 /* Called just before address relaxation, return the length by which a
1075 fragment must grow to reach it's destination. */
1078 md_estimate_size_before_relax (fragP
, segment_type
)
1079 register fragS
*fragP
;
1080 register segT segment_type
;
1084 switch (fragP
->fr_subtype
)
1089 case C (COND_BRANCH
, UNDEF_BYTE_DISP
):
1090 case C (UNCOND_BRANCH
, UNDEF_BYTE_DISP
):
1091 what
= GET_WHAT (fragP
->fr_subtype
);
1092 /* Used to be a branch to somewhere which was unknown. */
1093 if (S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
1095 /* Got a symbol and it's defined in this segment, become byte
1096 sized - maybe it will fix up. */
1097 fragP
->fr_subtype
= C (what
, BYTE_DISP
);
1101 /* Its got a segment, but its not ours, so it will always be
1103 fragP
->fr_subtype
= C (what
, UNDEF_WORD_DISP
);
1107 case C (COND_BRANCH
, BYTE_DISP
):
1108 case C (COND_BRANCH
, WORD_DISP
):
1109 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
1110 case C (UNCOND_BRANCH
, BYTE_DISP
):
1111 case C (UNCOND_BRANCH
, WORD_DISP
):
1112 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
1113 /* When relaxing a section for the second time, we don't need to
1114 do anything besides return the current size. */
1118 fragP
->fr_var
= md_relax_table
[fragP
->fr_subtype
].rlx_length
;
1119 return fragP
->fr_var
;
1122 const char *md_shortopts
= "";
1123 struct option md_longopts
[] = {
1124 #define OPTION_RELAX (OPTION_MD_BASE)
1125 {NULL
, no_argument
, NULL
, 0}
1129 md_show_usage (stream
)
1130 FILE *stream ATTRIBUTE_UNUSED
;
1134 size_t md_longopts_size
= sizeof (md_longopts
);