1 /* tc-w65.c -- Assemble code for the W65816
3 Copyright (C) 1995 Free Software Foundation.
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
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 Written By Steve Chamberlain
31 #include "../opcodes/w65-opc.h"
34 const char comment_chars
[] = "!";
35 CONST
char line_separator_chars
[] = ";";
36 const char line_comment_chars
[] = "!#";
38 /* This table describes all the machine specific pseudo-ops the assembler
39 has to support. The fields are:
40 pseudo-op name without dot
41 function to call to execute this pseudo-op
42 Integer arg to pass to the function
57 const pseudo_typeS md_pseudo_table
[] =
61 {"longa", s_longa
, 0},
62 {"longi", s_longa
, 1},
68 void s_align_bytes ();
71 /*int md_reloc_size; */
73 static int relax
; /* set if -relax seen */
75 const char EXP_CHARS
[] = "eE";
77 /* Chars that mean this number is a floating point constant */
80 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
84 static struct hash_control
*opcode_hash_control
; /* Opcode mnemonics */
92 #define C(a,b) ENCODE_RELAX(a,b)
93 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
95 #define GET_WHAT(x) ((x>>2))
99 #define UNDEF_BYTE_DISP 0
100 #define UNDEF_WORD_DISP 3
102 #define COND_BRANCH 1
103 #define UNCOND_BRANCH 2
106 #define BYTE_F 127 /* How far we can branch forwards */
107 #define BYTE_B -126 /* How far we can branch backwards */
111 relax_typeS md_relax_table
[C (END
, 0)];
114 This function is called once, at assembler startup time. This should
115 set up all the tables, etc that the MD part of the assembler needs
122 int *p
= xmode
? &X
: &M
;
123 while (*input_line_pointer
== ' ')
124 input_line_pointer
++;
125 if (strncmp (input_line_pointer
, "on", 2) == 0)
127 input_line_pointer
+= 2;
130 else if (strncmp (input_line_pointer
, "off", 3) == 0)
133 input_line_pointer
+= 3;
136 as_bad ("need on or off.");
137 demand_empty_rest_of_line ();
143 struct opinfo
*opcode
;
144 char *prev_name
= "";
146 opcode_hash_control
= hash_new ();
148 /* Insert unique names into hash table */
149 for (opcode
= optable
; opcode
->name
; opcode
++)
151 if (strcmp (prev_name
, opcode
->name
))
153 prev_name
= opcode
->name
;
154 hash_insert (opcode_hash_control
, opcode
->name
, (char *) opcode
);
158 /* Make all the opcodes with the same name point to the same
160 opcode
->name
= prev_name
;
165 /* Initialize the relax table. We use a local variable to avoid
166 warnings about modifying a supposedly const data structure. */
167 table
= (relax_typeS
*) md_relax_table
;
168 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_forward
= BYTE_F
;
169 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_backward
= BYTE_B
;
170 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_length
= 2;
171 table
[C (COND_BRANCH
, BYTE_DISP
)].rlx_more
= C (COND_BRANCH
, WORD_DISP
);
173 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_forward
= WORD_F
;
174 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_backward
= WORD_B
;
175 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_length
= 5;
176 table
[C (COND_BRANCH
, WORD_DISP
)].rlx_more
= 0;
178 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_forward
= BYTE_F
;
179 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_backward
= BYTE_B
;
180 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_length
= 2;
181 table
[C (UNCOND_BRANCH
, BYTE_DISP
)].rlx_more
= C (UNCOND_BRANCH
, WORD_DISP
);
183 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_forward
= WORD_F
;
184 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_backward
= WORD_B
;
185 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_length
= 3;
186 table
[C (UNCOND_BRANCH
, WORD_DISP
)].rlx_more
= 0;
188 flag_signed_overflow_ok
= 1;
191 static expressionS immediate
; /* absolute expression */
192 static expressionS immediate1
; /* absolute expression */
200 /* JF: '.' is pseudo symbol with value of current location
201 in current segment. */
202 fake
= FAKE_LABEL_NAME
;
203 return symbol_new (fake
,
205 (valueT
) frag_now_fix (),
214 w65_expression (dest
, bytes
)
221 while (*input_line_pointer
== ' ')
222 input_line_pointer
++;
224 if (*input_line_pointer
== '<')
227 input_line_pointer
++;
229 else if (*input_line_pointer
== '>')
232 input_line_pointer
++;
234 else if (*input_line_pointer
== '^')
237 input_line_pointer
++;
253 save
= input_line_pointer
;
254 input_line_pointer
= s
;
255 w65_expression (&immediate
, bytes
);
256 if (immediate
.X_op
== O_absent
)
257 as_bad ("missing operand");
258 new = input_line_pointer
;
259 input_line_pointer
= save
;
266 get_operands (info
, ptr
)
270 register int override_len
= 0;
271 register int bytes
= 0;
300 ptr
= parse_exp (ptr
);
302 else if (ptr
[0] == '!')
304 ptr
= parse_exp (ptr
+ 1);
309 amode
= ADDR_ABS_IDX_Y
;
313 else if (ptr
[1] == 'x')
315 amode
= ADDR_ABS_IDX_X
;
321 as_bad ("syntax error after <exp");
330 else if (ptr
[0] == '>')
332 ptr
= parse_exp (ptr
+ 1);
333 if (ptr
[0] == ',' && ptr
[1] == 'x')
335 amode
= ADDR_ABS_LONG_IDX_X
;
341 amode
= ADDR_ABS_LONG
;
345 else if (ptr
[0] == '<')
347 ptr
= parse_exp (ptr
+ 1);
352 amode
= ADDR_DIR_IDX_Y
;
356 else if (ptr
[1] == 'x')
358 amode
= ADDR_DIR_IDX_X
;
364 as_bad ("syntax error after <exp");
373 else if (ptr
[0] == 'a')
377 else if (ptr
[0] == '(')
398 else if (ptr
[0] == '!')
403 else if (ptr
[0] == '>')
412 ptr
= parse_exp (ptr
);
417 if (ptr
[0] == 'x' && ptr
[1] == ')')
421 if (override_len
== 1)
423 amode
= ADDR_DIR_IDX_IND_X
;
428 amode
= ADDR_ABS_IND_IDX
;
432 else if (ptr
[0] == 's' && ptr
[1] == ')' && ptr
[2] == ',' && ptr
[3] == 'y')
434 amode
= ADDR_STACK_REL_INDX_IDX
;
439 else if (ptr
[0] == ')')
441 if (ptr
[1] == ',' && ptr
[2] == 'y')
443 amode
= ADDR_DIR_IND_IDX_Y
;
449 if (override_len
== 1)
451 amode
= ADDR_DIR_IND
;
456 amode
= ADDR_ABS_IND
;
465 else if (ptr
[0] == '[')
467 ptr
= parse_exp (ptr
+ 1);
471 if (ptr
[0] == ',' && ptr
[1] == 'y')
474 amode
= ADDR_DIR_IND_IDX_Y_LONG
;
479 if (info
->code
== O_jmp
)
482 amode
= ADDR_ABS_IND_LONG
;
488 amode
= ADDR_DIR_IND_LONG
;
495 ptr
= parse_exp (ptr
, 2);
500 if (override_len
== 1)
503 amode
= ADDR_DIR_IDX_Y
;
507 amode
= ADDR_ABS_IDX_Y
;
512 else if (ptr
[1] == 'x')
514 if (override_len
== 1)
516 amode
= ADDR_DIR_IDX_X
;
521 amode
= ADDR_ABS_IDX_X
;
526 else if (ptr
[1] == 's')
529 amode
= ADDR_STACK_REL
;
535 immediate1
= immediate
;
536 ptr
= parse_exp (ptr
+ 1);
537 amode
= ADDR_BLOCK_MOVE
;
548 case ADDR_PC_REL_LONG
:
549 amode
= ADDR_PC_REL_LONG
;
553 if (override_len
== 1)
558 else if (override_len
== 3)
561 amode
= ADDR_ABS_LONG
;
578 if (amode
== ADDR_DIR
)
579 tc_cons_reloc
= R_W65_DP
;
581 tc_cons_reloc
= R_W65_ABS8
;
584 tc_cons_reloc
= R_W65_ABS8S8
;
587 tc_cons_reloc
= R_W65_ABS8S16
;
595 tc_cons_reloc
= R_W65_ABS16
;
598 tc_cons_reloc
= R_W65_ABS16S8
;
601 tc_cons_reloc
= R_W65_ABS16S16
;
608 /* Passed a pointer to a list of opcodes which use different
609 addressing modes, return the opcode which matches the opcodes
615 get_specific (opcode
)
616 struct opinfo
*opcode
;
618 int ocode
= opcode
->code
;
620 for (; opcode
->code
== ocode
; opcode
++)
622 if (opcode
->amode
== amode
)
629 check (operand
, low
, high
)
630 expressionS
*operand
;
634 if (operand
->X_op
!= O_constant
635 || operand
->X_add_number
< low
636 || operand
->X_add_number
> high
)
638 as_bad ("operand must be absolute in range %d..%d", low
, high
);
640 return operand
->X_add_number
;
647 /* Now we know what sort of opcodes it is, lets build the bytes -
651 struct opinfo
*opcode
;
658 if (opcode
->amode
== ADDR_IMPLIED
)
660 output
= frag_more (1);
662 else if (opcode
->amode
== ADDR_PC_REL
)
665 /* This is a relaxable insn, so we do some special handling */
666 type
= opcode
->val
== OP_BRA
? UNCOND_BRANCH
: COND_BRANCH
;
667 output
= frag_var (rs_machine_dependent
,
668 md_relax_table
[C (type
, WORD_DISP
)].rlx_length
,
669 md_relax_table
[C (type
, BYTE_DISP
)].rlx_length
,
670 C (type
, UNDEF_BYTE_DISP
),
671 immediate
.X_add_symbol
,
672 immediate
.X_add_number
,
677 switch (opcode
->amode
)
679 GETINFO (size
, type
, pcrel
);
682 /* If something special was done in the
683 expression modify the reloc type */
686 type
= tc_cons_reloc
;
691 /* 1 byte for the opcode + the bytes for the addrmode */
692 output
= frag_more (size
+ 1);
694 if (opcode
->amode
== ADDR_BLOCK_MOVE
)
696 /* Two relocs for this one */
697 fix_new_exp (frag_now
,
698 output
+ 1 - frag_now
->fr_literal
,
704 fix_new_exp (frag_now
,
705 output
+ 2 - frag_now
->fr_literal
,
712 && opcode
->amode
!= ADDR_IMPLIED
713 && opcode
->amode
!= ADDR_ACC
714 && opcode
->amode
!= ADDR_STACK
)
716 fix_new_exp (frag_now
,
717 output
+ 1 - frag_now
->fr_literal
,
724 output
[0] = opcode
->val
;
727 /* This is the guts of the machine-dependent assembler. STR points to a
728 machine dependent instruction. This function is supposed to emit
729 the frags/bytes it assembles to.
736 unsigned char *op_start
;
737 unsigned char *op_end
;
738 struct opinfo
*opcode
;
743 /* Drop leading whitespace */
747 /* all opcodes are three letters */
755 opcode
= (struct opinfo
*) hash_find (opcode_hash_control
, name
);
759 as_bad ("unknown opcode");
763 if (opcode
->amode
!= ADDR_IMPLIED
764 && opcode
->amode
!= ADDR_STACK
)
766 get_operands (opcode
, str
);
767 opcode
= get_specific (opcode
);
772 /* Couldn't find an opcode which matched the operands */
775 char *where
= frag_more (1);
779 as_bad ("invalid operands for opcode");
783 build_Mytes (opcode
);
788 DEFUN (tc_crawl_symbol_chain
, (headers
),
789 object_headers
* headers
)
791 printf ("call to tc_crawl_symbol_chain \n");
795 DEFUN (md_undefined_symbol
, (name
),
802 DEFUN (tc_headers_hook
, (headers
),
803 object_headers
* headers
)
805 printf ("call to tc_headers_hook \n");
808 /* Various routines to kill one day */
809 /* Equal to MAX_PRECISION in atof-ieee.c */
810 #define MAX_LITTLENUMS 6
812 /* Turn a string in input_line_pointer into a floating point constant of type
813 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
814 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
817 md_atof (type
, litP
, sizeP
)
823 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
824 LITTLENUM_TYPE
*wordP
;
856 return "Bad call to MD_NTOF()";
858 t
= atof_ieee (input_line_pointer
, type
, words
);
860 input_line_pointer
= t
;
862 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
863 for (wordP
= words
+ prec
- 1; prec
--;)
865 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
866 litP
+= sizeof (LITTLENUM_TYPE
);
872 md_parse_option (c
,a
)
880 int md_short_jump_size
;
883 tc_Nout_fix_to_chars ()
885 printf ("call to tc_Nout_fix_to_chars \n");
890 md_create_short_jump (ptr
, from_Nddr
, to_Nddr
, frag
, to_symbol
)
897 as_fatal ("failed sanity check.");
901 md_create_long_jump (ptr
, from_Nddr
, to_Nddr
, frag
, to_symbol
)
903 addressT from_Nddr
, to_Nddr
;
907 as_fatal ("failed sanity check.");
912 called after relaxing, change the frags so they know how big they are
915 md_convert_frag (headers
, seg
, fragP
)
916 object_headers
*headers
;
922 unsigned char *buffer
= (unsigned char *) (fragP
->fr_fix
+ fragP
->fr_literal
);
924 switch (fragP
->fr_subtype
)
926 case C (COND_BRANCH
, BYTE_DISP
):
927 case C (UNCOND_BRANCH
, BYTE_DISP
):
932 /* cond branches to a known 16 bit displacement */
933 case C (COND_BRANCH
, WORD_DISP
):
944 /* Invert the sense of the test */
946 buffer
[1] = 3; /* Jump over following brl */
957 case C (UNCOND_BRANCH
, WORD_DISP
):
958 /* Unconditional branches to a known 16 bit displacement */
971 /* got to create a branch over a reloc here */
972 case C (COND_BRANCH
, UNDEF_WORD_DISP
):
973 buffer
[0] ^= 0x20; /* invert test */
986 fragP
->fr_fix
+= disp_size
+ inst_size
;
989 case C (UNCOND_BRANCH
, UNDEF_WORD_DISP
):
1001 fragP
->fr_fix
+= disp_size
+ inst_size
;
1009 /* Get the address of the end of the instruction */
1010 int next_inst
= fragP
->fr_fix
+ fragP
->fr_address
+ disp_size
+ inst_size
;
1011 int targ_addr
= (S_GET_VALUE (fragP
->fr_symbol
) +
1013 int disp
= targ_addr
- next_inst
;
1015 md_number_to_chars (buffer
+ inst_size
, disp
, disp_size
);
1016 fragP
->fr_fix
+= disp_size
+ inst_size
;
1023 DEFUN (md_section_align
, (seg
, size
),
1027 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1)
1028 & (-1 << section_alignment
[(int) seg
]));
1033 md_apply_fix (fixP
, val
)
1037 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1038 int addr
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
1040 if (fixP
->fx_r_type
== 0)
1042 if (fixP
->fx_size
== 1)
1043 fixP
->fx_r_type
= R_W65_ABS8
;
1045 fixP
->fx_r_type
= R_W65_ABS16
;
1048 switch (fixP
->fx_r_type
)
1057 case R_W65_ABS16S16
:
1071 *buf
++ = val
- addr
- 1;
1074 val
= val
- addr
- 1;
1087 int md_long_jump_size
;
1089 /* Put number into target byte order */
1092 md_number_to_chars (ptr
, use
, nbytes
)
1097 number_to_chars_littleendian (ptr
, use
, nbytes
);
1101 md_pcrel_from (fixP
)
1104 int gap
= fixP
->fx_size
+ fixP
->fx_where
+ fixP
->fx_frag
->fr_address
- 1;
1109 tc_coff_symbol_emit_hook (x
)
1115 tc_coff_fix2rtype (fix_ptr
)
1118 return fix_ptr
->fx_r_type
;
1122 tc_reloc_mangle (fix_ptr
, intr
, base
)
1124 struct internal_reloc
*intr
;
1128 symbolS
*symbol_ptr
;
1130 symbol_ptr
= fix_ptr
->fx_addsy
;
1132 /* If this relocation is attached to a symbol then it's ok
1134 if (fix_ptr
->fx_r_type
== RELOC_32
)
1136 /* cons likes to create reloc32's whatever the size of the reloc..
1138 switch (fix_ptr
->fx_size
)
1141 intr
->r_type
= R_IMM16
;
1144 intr
->r_type
= R_IMM8
;
1152 if (fix_ptr
->fx_size
== 4)
1153 intr
->r_type
= R_W65_ABS24
;
1155 intr
->r_type
= fix_ptr
->fx_r_type
;
1158 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1159 intr
->r_offset
= fix_ptr
->fx_offset
;
1161 /* Turn the segment of the symbol into an offset. */
1166 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
1169 intr
->r_offset
+= S_GET_VALUE (symbol_ptr
);
1170 intr
->r_symndx
= dot
->sy_number
;
1174 intr
->r_symndx
= symbol_ptr
->sy_number
;
1179 intr
->r_symndx
= -1;
1184 tc_coff_sizemachdep (frag
)
1187 return md_relax_table
[frag
->fr_subtype
].rlx_length
;
1195 called just before address relaxation, return the length
1196 by which a fragment must grow to reach it's destination
1199 md_estimate_size_before_relax (fragP
, segment_type
)
1200 register fragS
*fragP
;
1201 register segT segment_type
;
1203 int what
= GET_WHAT (fragP
->fr_subtype
);
1205 switch (fragP
->fr_subtype
)
1209 case C (COND_BRANCH
, UNDEF_BYTE_DISP
):
1210 case C (UNCOND_BRANCH
, UNDEF_BYTE_DISP
):
1211 /* used to be a branch to somewhere which was unknown */
1212 if (S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
1214 /* Got a symbol and it's defined in this segment, become byte
1215 sized - maybe it will fix up */
1216 fragP
->fr_subtype
= C (what
, BYTE_DISP
);
1217 fragP
->fr_var
= md_relax_table
[C (what
, BYTE_DISP
)].rlx_length
;
1221 /* Its got a segment, but its not ours, so it will always be long */
1222 fragP
->fr_subtype
= C (what
, UNDEF_WORD_DISP
);
1223 fragP
->fr_var
= md_relax_table
[C (what
, WORD_DISP
)].rlx_length
;
1224 return md_relax_table
[C (what
, WORD_DISP
)].rlx_length
;
1227 return fragP
->fr_var
;
1232 CONST
char *md_shortopts
= "";
1233 struct option md_longopts
[] = {
1234 #define OPTION_RELAX (OPTION_MD_BASE)
1235 {NULL
, no_argument
, NULL
, 0}
1239 md_show_usage (stream
)
1245 size_t md_longopts_size
= sizeof(md_longopts
);