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 int M; /* M flag. */
54 static int X; /* X flag. */
56 /* This function is called once, at assembler startup time. This
57 should set up all the tables, etc that the MD part of the assembler
63 int *p = xmode ? &X : &M;
65 while (*input_line_pointer == ' ')
67 if (strncmp (input_line_pointer, "on", 2) == 0)
69 input_line_pointer += 2;
72 else if (strncmp (input_line_pointer, "off", 3) == 0)
75 input_line_pointer += 3;
78 as_bad (_("need on or off."));
79 demand_empty_rest_of_line ();
82 const pseudo_typeS md_pseudo_table[] =
86 {"longa", s_longa, 0},
87 {"longi", s_longa, 1},
91 const char EXP_CHARS[] = "eE";
93 /* Chars that mean this number is a floating point constant. */
96 const char FLT_CHARS[] = "rRsSfFdDxXpP";
98 /* Opcode mnemonics */
99 static struct hash_control *opcode_hash_control;
101 #define C(a, b) ENCODE_RELAX(a,b)
102 #define ENCODE_RELAX(what, length) (((what) << 2) + (length))
104 #define GET_WHAT(x) (((x) >> 2))
108 #define UNDEF_BYTE_DISP 0
109 #define UNDEF_WORD_DISP 3
110 #define COND_BRANCH 1
111 #define UNCOND_BRANCH 2
114 #define BYTE_F 127 /* How far we can branch forwards. */
115 #define BYTE_B -126 /* How far we can branch backwards. */
119 relax_typeS md_relax_table[C (END, 0)] =
127 { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */
128 { BYTE_F, BYTE_B, 2, C (COND_BRANCH, WORD_DISP) }, /* BYTE_DISP */
129 { WORD_F, WORD_B, 5, 0 }, /* WORD_DISP */
130 { 0, 0, 5, 0 }, /* UNDEF_WORD_DISP */
133 { 0, 0, 0, 0 }, /* UNDEF_BYTE_DISP */
134 { BYTE_F, BYTE_B, 2, C (UNCOND_BRANCH, WORD_DISP) }, /* BYTE_DISP */
135 { WORD_F, WORD_B, 3, 0 }, /* WORD_DISP */
136 { 0, 0, 3, 0 } /* UNDEF_WORD_DISP */
142 const struct opinfo *opcode;
143 char *prev_name = "";
145 opcode_hash_control = hash_new ();
147 /* Insert unique names into hash table. */
148 for (opcode = optable; opcode->name; opcode++)
150 if (strcmp (prev_name, opcode->name))
152 prev_name = opcode->name;
153 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
157 flag_signed_overflow_ok = 1;
160 static expressionS immediate; /* Absolute expression. */
161 static expressionS immediate1; /* Absolute expression. */
167 w65_expression (expressionS *dest)
172 while (*input_line_pointer == ' ')
173 input_line_pointer++;
175 if (*input_line_pointer == '<')
178 input_line_pointer++;
180 else if (*input_line_pointer == '>')
183 input_line_pointer++;
185 else if (*input_line_pointer == '^')
188 input_line_pointer++;
202 save = input_line_pointer;
203 input_line_pointer = s;
204 w65_expression (&immediate);
205 if (immediate.X_op == O_absent)
206 as_bad (_("missing operand"));
207 new = input_line_pointer;
208 input_line_pointer = save;
213 get_operands (const struct opinfo *info, char *ptr)
215 int override_len = 0;
246 ptr = parse_exp (ptr);
248 else if (ptr[0] == '!')
250 ptr = parse_exp (ptr + 1);
255 amode = ADDR_ABS_IDX_Y;
259 else if (ptr[1] == 'x')
261 amode = ADDR_ABS_IDX_X;
266 as_bad (_("syntax error after <exp"));
274 else if (ptr[0] == '>')
276 ptr = parse_exp (ptr + 1);
277 if (ptr[0] == ',' && ptr[1] == 'x')
279 amode = ADDR_ABS_LONG_IDX_X;
285 amode = ADDR_ABS_LONG;
289 else if (ptr[0] == '<')
291 ptr = parse_exp (ptr + 1);
296 amode = ADDR_DIR_IDX_Y;
300 else if (ptr[1] == 'x')
302 amode = ADDR_DIR_IDX_X;
307 as_bad (_("syntax error after <exp"));
315 else if (ptr[0] == 'a')
317 else if (ptr[0] == '(')
338 else if (ptr[0] == '!')
343 else if (ptr[0] == '>')
351 ptr = parse_exp (ptr);
356 if (ptr[0] == 'x' && ptr[1] == ')')
360 if (override_len == 1)
362 amode = ADDR_DIR_IDX_IND_X;
367 amode = ADDR_ABS_IND_IDX;
371 else if (ptr[0] == 's' && ptr[1] == ')'
372 && ptr[2] == ',' && ptr[3] == 'y')
374 amode = ADDR_STACK_REL_INDX_IDX;
379 else if (ptr[0] == ')')
381 if (ptr[1] == ',' && ptr[2] == 'y')
383 amode = ADDR_DIR_IND_IDX_Y;
389 if (override_len == 1)
391 amode = ADDR_DIR_IND;
396 amode = ADDR_ABS_IND;
403 else if (ptr[0] == '[')
405 ptr = parse_exp (ptr + 1);
410 if (ptr[0] == ',' && ptr[1] == 'y')
413 amode = ADDR_DIR_IND_IDX_Y_LONG;
418 if (info->code == O_jmp)
421 amode = ADDR_ABS_IND_LONG;
426 amode = ADDR_DIR_IND_LONG;
433 ptr = parse_exp (ptr);
438 if (override_len == 1)
441 amode = ADDR_DIR_IDX_Y;
445 amode = ADDR_ABS_IDX_Y;
450 else if (ptr[1] == 'x')
452 if (override_len == 1)
454 amode = ADDR_DIR_IDX_X;
459 amode = ADDR_ABS_IDX_X;
464 else if (ptr[1] == 's')
467 amode = ADDR_STACK_REL;
473 immediate1 = immediate;
474 ptr = parse_exp (ptr + 1);
475 amode = ADDR_BLOCK_MOVE;
486 case ADDR_PC_REL_LONG:
487 amode = ADDR_PC_REL_LONG;
491 if (override_len == 1)
496 else if (override_len == 3)
499 amode = ADDR_ABS_LONG;
516 if (amode == ADDR_DIR)
517 tc_cons_reloc = R_W65_DP;
519 tc_cons_reloc = R_W65_ABS8;
522 tc_cons_reloc = R_W65_ABS8S8;
525 tc_cons_reloc = R_W65_ABS8S16;
533 tc_cons_reloc = R_W65_ABS16;
536 tc_cons_reloc = R_W65_ABS16S8;
539 tc_cons_reloc = R_W65_ABS16S16;
546 /* Passed a pointer to a list of opcodes which use different
547 addressing modes, return the opcode which matches the opcodes
550 static const struct opinfo *
551 get_specific (const struct opinfo *opcode)
553 int ocode = opcode->code;
555 for (; opcode->code == ocode; opcode++)
556 if (opcode->amode == amode)
562 /* Now we know what sort of opcodes it is, let's build the bytes. */
565 build_Mytes (const struct opinfo *opcode)
572 if (opcode->amode == ADDR_IMPLIED)
573 output = frag_more (1);
575 else if (opcode->amode == ADDR_PC_REL)
579 /* This is a relaxable insn, so we do some special handling. */
580 type = opcode->val == OP_BRA ? UNCOND_BRANCH : COND_BRANCH;
581 output = frag_var (rs_machine_dependent,
582 md_relax_table[C (type, WORD_DISP)].rlx_length,
583 md_relax_table[C (type, BYTE_DISP)].rlx_length,
584 C (type, UNDEF_BYTE_DISP),
585 immediate.X_add_symbol,
586 immediate.X_add_number,
591 switch (opcode->amode)
593 GETINFO (size, type, pcrel);
598 /* If something special was done in the expression modify the
601 type = tc_cons_reloc;
603 /* 1 byte for the opcode + the bytes for the addrmode. */
604 output = frag_more (size + 1);
606 if (opcode->amode == ADDR_BLOCK_MOVE)
608 /* Two relocs for this one. */
609 fix_new_exp (frag_now,
610 output + 1 - frag_now->fr_literal,
616 fix_new_exp (frag_now,
617 output + 2 - frag_now->fr_literal,
624 && opcode->amode != ADDR_IMPLIED
625 && opcode->amode != ADDR_ACC
626 && opcode->amode != ADDR_STACK)
628 fix_new_exp (frag_now,
629 output + 1 - frag_now->fr_literal,
636 output[0] = opcode->val;
639 /* This is the guts of the machine-dependent assembler. STR points to
640 a machine dependent instruction. This function is supposed to emit
641 the frags/bytes it assembles to. */
644 md_assemble (char *str)
646 const struct opinfo *opcode;
649 /* Drop leading whitespace */
653 /* All opcodes are three letters. */
661 opcode = (struct opinfo *) hash_find (opcode_hash_control, name);
665 as_bad (_("unknown opcode"));
669 if (opcode->amode != ADDR_IMPLIED
670 && opcode->amode != ADDR_STACK)
672 get_operands (opcode, str);
673 opcode = get_specific (opcode);
678 /* Couldn't find an opcode which matched the operands. */
680 char *where = frag_more (1);
684 as_bad (_("invalid operands for opcode"));
688 build_Mytes (opcode);
692 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
697 /* Various routines to kill one day. */
698 /* Equal to MAX_PRECISION in atof-ieee.c. */
699 #define MAX_LITTLENUMS 6
701 /* Turn a string in input_line_pointer into a floating point constant
702 of type TYPE, and store the appropriate bytes in *LITP. The number
703 of LITTLENUMS emitted is stored in *SIZEP. An error message is
704 returned, or NULL on OK. */
707 md_atof (int type, char *litP, int *sizeP)
710 LITTLENUM_TYPE words[MAX_LITTLENUMS];
711 LITTLENUM_TYPE *wordP;
742 return _("Bad call to MD_NTOF()");
744 t = atof_ieee (input_line_pointer, type, words);
746 input_line_pointer = t;
748 *sizeP = prec * sizeof (LITTLENUM_TYPE);
749 for (wordP = words + prec - 1; prec--;)
751 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
752 litP += sizeof (LITTLENUM_TYPE);
758 md_parse_option (int c ATTRIBUTE_UNUSED, char *a ATTRIBUTE_UNUSED)
763 /* Called after relaxing, change the frags so they know how big they
767 md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED,
768 segT seg ATTRIBUTE_UNUSED,
773 unsigned char *buffer =
774 (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
776 switch (fragP->fr_subtype)
778 case C (COND_BRANCH, BYTE_DISP):
779 case C (UNCOND_BRANCH, BYTE_DISP):
784 /* Conditional branches to a known 16 bit displacement. */
785 case C (COND_BRANCH, WORD_DISP):
796 /* Invert the sense of the test. */
798 buffer[1] = 3; /* Jump over following brl. */
809 case C (UNCOND_BRANCH, WORD_DISP):
810 /* Unconditional branches to a known 16 bit displacement. */
823 /* Got to create a branch over a reloc here. */
824 case C (COND_BRANCH, UNDEF_WORD_DISP):
825 buffer[0] ^= 0x20; /* Invert test. */
838 fragP->fr_fix += disp_size + inst_size;
841 case C (UNCOND_BRANCH, UNDEF_WORD_DISP):
853 fragP->fr_fix += disp_size + inst_size;
861 /* Get the address of the end of the instruction. */
862 int next_inst = (fragP->fr_fix + fragP->fr_address
863 + disp_size + inst_size);
864 int targ_addr = (S_GET_VALUE (fragP->fr_symbol) +
866 int disp = targ_addr - next_inst;
868 md_number_to_chars ((char *) buffer + inst_size, disp, disp_size);
869 fragP->fr_fix += disp_size + inst_size;
875 md_section_align (segT seg, valueT size)
877 return ((size + (1 << section_alignment[(int) seg]) - 1)
878 & (-1 << section_alignment[(int) seg]));
882 md_apply_fix3 (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
884 long val = * (long *) valP;
885 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
886 int addr = fixP->fx_frag->fr_address + fixP->fx_where;
888 if (fixP->fx_r_type == 0)
890 if (fixP->fx_size == 1)
891 fixP->fx_r_type = R_W65_ABS8;
893 fixP->fx_r_type = R_W65_ABS16;
896 switch (fixP->fx_r_type)
919 *buf++ = val - addr - 1;
922 val = val - addr - 1;
934 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
938 /* Put number into target byte order. */
941 md_number_to_chars (char *ptr, valueT use, int nbytes)
943 number_to_chars_littleendian (ptr, use, nbytes);
947 md_pcrel_from (fixS *fixP)
949 int gap = fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address - 1;
954 tc_coff_symbol_emit_hook (symbolS *x ATTRIBUTE_UNUSED)
959 tc_coff_fix2rtype (fixS *fix_ptr)
961 return fix_ptr->fx_r_type;
965 tc_reloc_mangle (fixS *fix_ptr,
966 struct internal_reloc *intr,
972 symbol_ptr = fix_ptr->fx_addsy;
974 /* If this relocation is attached to a symbol then it's ok
976 if (fix_ptr->fx_r_type == RELOC_32)
978 /* Cons likes to create reloc32's whatever the size of the reloc. */
979 switch (fix_ptr->fx_size)
982 intr->r_type = R_IMM16;
985 intr->r_type = R_IMM8;
993 if (fix_ptr->fx_size == 4)
994 intr->r_type = R_W65_ABS24;
996 intr->r_type = fix_ptr->fx_r_type;
999 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1000 intr->r_offset = fix_ptr->fx_offset;
1002 /* Turn the segment of the symbol into an offset. */
1007 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1010 intr->r_offset += S_GET_VALUE (symbol_ptr);
1011 intr->r_symndx = dot->sy_number;
1014 intr->r_symndx = symbol_ptr->sy_number;
1017 intr->r_symndx = -1;
1021 tc_coff_sizemachdep (fragS *frag)
1023 return md_relax_table[frag->fr_subtype].rlx_length;
1026 /* Called just before address relaxation, return the length by which a
1027 fragment must grow to reach it's destination. */
1030 md_estimate_size_before_relax (fragS *fragP, segT segment_type)
1034 switch (fragP->fr_subtype)
1039 case C (COND_BRANCH, UNDEF_BYTE_DISP):
1040 case C (UNCOND_BRANCH, UNDEF_BYTE_DISP):
1041 what = GET_WHAT (fragP->fr_subtype);
1042 /* Used to be a branch to somewhere which was unknown. */
1043 if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1044 /* Got a symbol and it's defined in this segment, become byte
1045 sized - maybe it will fix up. */
1046 fragP->fr_subtype = C (what, BYTE_DISP);
1048 /* Its got a segment, but its not ours, so it will always be
1050 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
1053 case C (COND_BRANCH, BYTE_DISP):
1054 case C (COND_BRANCH, WORD_DISP):
1055 case C (COND_BRANCH, UNDEF_WORD_DISP):
1056 case C (UNCOND_BRANCH, BYTE_DISP):
1057 case C (UNCOND_BRANCH, WORD_DISP):
1058 case C (UNCOND_BRANCH, UNDEF_WORD_DISP):
1059 /* When relaxing a section for the second time, we don't need to
1060 do anything besides return the current size. */
1064 fragP->fr_var = md_relax_table[fragP->fr_subtype].rlx_length;
1065 return fragP->fr_var;
1068 const char *md_shortopts = "";
1069 struct option md_longopts[] =
1071 #define OPTION_RELAX (OPTION_MD_BASE)
1072 {NULL, no_argument, NULL, 0}
1076 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
1080 size_t md_longopts_size = sizeof (md_longopts);