1 /* tc-z80.c -- Assemble code for the Zilog Z80 and ASCII R800
2 Copyright 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Arnold Metselaar <arnold_m@operamail.com>
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 3, 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, 51 Franklin Street - Fifth Floor, Boston, MA
23 #include "safe-ctype.h"
26 /* Exported constants. */
27 const char comment_chars
[] = ";\0";
28 const char line_comment_chars
[] = "#;\0";
29 const char line_separator_chars
[] = "\0";
30 const char EXP_CHARS
[] = "eE\0";
31 const char FLT_CHARS
[] = "RrFf\0";
33 /* For machine specific options. */
34 const char * md_shortopts
= ""; /* None yet. */
38 OPTION_MACH_Z80
= OPTION_MD_BASE
,
53 struct option md_longopts
[] =
55 { "z80", no_argument
, NULL
, OPTION_MACH_Z80
},
56 { "r800", no_argument
, NULL
, OPTION_MACH_R800
},
57 { "ignore-undocumented-instructions", no_argument
, NULL
, OPTION_MACH_IUD
},
58 { "Wnud", no_argument
, NULL
, OPTION_MACH_IUD
},
59 { "warn-undocumented-instructions", no_argument
, NULL
, OPTION_MACH_WUD
},
60 { "Wud", no_argument
, NULL
, OPTION_MACH_WUD
},
61 { "forbid-undocumented-instructions", no_argument
, NULL
, OPTION_MACH_FUD
},
62 { "Fud", no_argument
, NULL
, OPTION_MACH_FUD
},
63 { "ignore-unportable-instructions", no_argument
, NULL
, OPTION_MACH_IUP
},
64 { "Wnup", no_argument
, NULL
, OPTION_MACH_IUP
},
65 { "warn-unportable-instructions", no_argument
, NULL
, OPTION_MACH_WUP
},
66 { "Wup", no_argument
, NULL
, OPTION_MACH_WUP
},
67 { "forbid-unportable-instructions", no_argument
, NULL
, OPTION_MACH_FUP
},
68 { "Fup", no_argument
, NULL
, OPTION_MACH_FUP
},
70 { NULL
, no_argument
, NULL
, 0 }
73 size_t md_longopts_size
= sizeof (md_longopts
);
75 extern int coff_flags
;
76 /* Instruction classes that silently assembled. */
77 static int ins_ok
= INS_Z80
| INS_UNDOC
;
78 /* Instruction classes that generate errors. */
79 static int ins_err
= INS_R800
;
80 /* Instruction classes actually used, determines machine type. */
81 static int ins_used
= INS_Z80
;
84 md_parse_option (int c
, char* arg ATTRIBUTE_UNUSED
)
94 case OPTION_MACH_R800
:
95 ins_ok
= INS_Z80
| INS_UNDOC
| INS_R800
;
100 ins_err
&= ~INS_UNDOC
;
102 case OPTION_MACH_IUP
:
103 ins_ok
|= INS_UNDOC
| INS_UNPORT
;
104 ins_err
&= ~(INS_UNDOC
| INS_UNPORT
);
106 case OPTION_MACH_WUD
:
107 if ((ins_ok
& INS_R800
) == 0)
109 ins_ok
&= ~(INS_UNDOC
|INS_UNPORT
);
110 ins_err
&= ~INS_UNDOC
;
113 case OPTION_MACH_WUP
:
114 ins_ok
&= ~INS_UNPORT
;
115 ins_err
&= ~(INS_UNDOC
|INS_UNPORT
);
117 case OPTION_MACH_FUD
:
118 if ((ins_ok
& INS_R800
) == 0)
120 ins_ok
&= (INS_UNDOC
| INS_UNPORT
);
121 ins_err
|= INS_UNDOC
| INS_UNPORT
;
124 case OPTION_MACH_FUP
:
125 ins_ok
&= ~INS_UNPORT
;
126 ins_err
|= INS_UNPORT
;
134 md_show_usage (FILE * f
)
137 CPU model/instruction set options:\n\
139 -z80\t\t assemble for Z80\n\
140 -ignore-undocumented-instructions\n\
142 \tsilently assemble undocumented Z80-instructions that work on R800\n\
143 -ignore-unportable-instructions\n\
145 \tsilently assemble all undocumented Z80-instructions\n\
146 -warn-undocumented-instructions\n\
148 \tissue warnings for undocumented Z80-instructions that work on R800\n\
149 -warn-unportable-instructions\n\
151 \tissue warnings for other undocumented Z80-instructions\n\
152 -forbid-undocumented-instructions\n\
154 \ttreat all undocumented z80-instructions as errors\n\
155 -forbid-unportable-instructions\n\
157 \ttreat undocumented z80-instructions that do not work on R800 as errors\n\
158 -r800\t assemble for R800\n\n\
159 Default: -z80 -ignore-undocument-instructions -warn-unportable-instructions.\n");
162 static symbolS
* zero
;
170 p
= input_line_pointer
;
171 input_line_pointer
= "0";
174 input_line_pointer
= p
;
175 zero
= make_expr_symbol (& nul
);
176 /* We do not use relaxation (yet). */
185 if (ins_used
& (INS_UNPORT
| INS_R800
))
186 ins_used
|= INS_UNDOC
;
191 mach_type
= bfd_mach_z80strict
;
193 case INS_Z80
|INS_UNDOC
:
194 mach_type
= bfd_mach_z80
;
196 case INS_Z80
|INS_UNDOC
|INS_UNPORT
:
197 mach_type
= bfd_mach_z80full
;
199 case INS_Z80
|INS_UNDOC
|INS_R800
:
200 mach_type
= bfd_mach_r800
;
206 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, mach_type
);
210 skip_space (const char *s
)
212 while (*s
== ' ' || *s
== '\t')
217 /* A non-zero return-value causes a continue in the
218 function read_a_source_file () in ../read.c. */
220 z80_start_line_hook (void)
225 /* Convert one character constants. */
226 for (p
= input_line_pointer
; *p
&& *p
!= '\n'; ++p
)
231 if (p
[1] != 0 && p
[1] != '\'' && p
[2] == '\'')
233 snprintf (buf
, 4, "%3d", (unsigned char)p
[1]);
240 for (quote
= *p
++; quote
!= *p
&& '\n' != *p
; ++p
)
244 as_bad (_("-- unterminated string"));
245 ignore_rest_of_line ();
251 /* Check for <label>[:] [.](EQU|DEFL) <value>. */
252 if (is_name_beginner (*input_line_pointer
))
254 char c
, *rest
, *line_start
;
258 line_start
= input_line_pointer
;
263 c
= get_symbol_end ();
264 rest
= input_line_pointer
+ 1;
268 if (*rest
== ' ' || *rest
== '\t')
272 if (strncasecmp (rest
, "EQU", 3) == 0)
274 else if (strncasecmp (rest
, "DEFL", 4) == 0)
278 if (len
&& (rest
[len
] == ' ' || rest
[len
] == '\t'))
280 /* Handle assignment here. */
281 input_line_pointer
= rest
+ len
;
282 if (line_start
[-1] == '\n')
283 bump_line_counters ();
284 /* Most Z80 assemblers require the first definition of a
285 label to use "EQU" and redefinitions to have "DEFL". */
286 if (len
== 3 && (symbolP
= symbol_find (line_start
)) != NULL
)
288 if (S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
289 as_bad (_("symbol `%s' is already defined"), line_start
);
291 equals (line_start
, 1);
296 /* Restore line and pointer. */
297 *input_line_pointer
= c
;
298 input_line_pointer
= line_start
;
305 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
311 md_atof (int type ATTRIBUTE_UNUSED
, char *litP ATTRIBUTE_UNUSED
,
312 int *sizeP ATTRIBUTE_UNUSED
)
314 return _("floating point numbers are not implemented");
318 md_section_align (segT seg ATTRIBUTE_UNUSED
, valueT size
)
324 md_pcrel_from (fixS
* fixp
)
326 return fixp
->fx_where
+
327 fixp
->fx_frag
->fr_address
+ 1;
330 typedef const char * (asfunc
)(char, char, const char*);
332 typedef struct _table_t
340 /* Compares the key for structs that start with a char * to the key. */
342 key_cmp (const void * a
, const void * b
)
344 const char *str_a
, *str_b
;
346 str_a
= *((const char**)a
);
347 str_b
= *((const char**)b
);
348 return strcmp (str_a
, str_b
);
351 #define BUFLEN 8 /* Large enough for any keyword. */
354 const char *key
= buf
;
356 #define R_STACKABLE (0x80)
357 #define R_ARITH (0x40)
360 #define R_INDEX (R_IX | R_IY)
369 #define REG_F (6 | 8)
373 #define REG_AF (3 | R_STACKABLE)
374 #define REG_BC (0 | R_STACKABLE | R_ARITH)
375 #define REG_DE (1 | R_STACKABLE | R_ARITH)
376 #define REG_HL (2 | R_STACKABLE | R_ARITH)
377 #define REG_SP (3 | R_ARITH)
379 static const struct reg_entry
397 {"ix", REG_HL
| R_IX
},
398 {"ixh",REG_H
| R_IX
},
399 {"ixl",REG_L
| R_IX
},
400 {"iy", REG_HL
| R_IY
},
401 {"iyh",REG_H
| R_IY
},
402 {"iyl",REG_L
| R_IY
},
408 /* Prevent an error on a line from also generating
409 a "junk at end of line" error message. */
410 static char err_flag
;
413 error (const char * message
)
415 as_bad ("%s", message
);
422 error (_("illegal operand"));
426 wrong_mach (int ins_type
)
433 p
= "undocumented instruction";
436 p
= "instruction does not work on R800";
439 p
= "instruction only works R800";
442 p
= 0; /* Not reachable. */
445 if (ins_type
& ins_err
)
452 check_mach (int ins_type
)
454 if ((ins_type
& ins_ok
) == 0)
455 wrong_mach (ins_type
);
456 ins_used
|= ins_type
;
459 /* Check whether an expression is indirect. */
461 is_indir (const char *s
)
467 /* Indirection is indicated with parentheses. */
470 for (p
= s
, depth
= 0; *p
&& *p
!= ','; ++p
)
476 for (quote
= *p
++; quote
!= *p
&& *p
!= '\n'; ++p
)
477 if (*p
== '\\' && p
[1])
487 p
= skip_space (p
+ 1);
493 error (_("mismatched parentheses"));
499 error (_("mismatched parentheses"));
504 /* Parse general expression. */
506 parse_exp2 (const char *s
, expressionS
*op
, segT
*pseg
)
511 const struct reg_entry
* regp
;
515 op
->X_md
= indir
= is_indir (p
);
517 p
= skip_space (p
+ 1);
519 for (i
= 0; i
< BUFLEN
; ++i
)
521 if (!ISALPHA (p
[i
])) /* Register names consist of letters only. */
523 buf
[i
] = TOLOWER (p
[i
]);
526 if ((i
< BUFLEN
) && ((p
[i
] == 0) || (strchr (")+-, \t", p
[i
]))))
529 regp
= bsearch (& key
, regtable
, ARRAY_SIZE (regtable
),
530 sizeof (regtable
[0]), key_cmp
);
534 op
->X_add_symbol
= op
->X_op_symbol
= 0;
535 op
->X_add_number
= regp
->number
;
536 op
->X_op
= O_register
;
537 p
+= strlen (regp
->name
);
543 if ((regp
->number
& R_INDEX
) && (regp
->number
& R_ARITH
))
547 if ((*p
== '+') || (*p
== '-'))
549 input_line_pointer
= (char*) p
;
550 expression (& offset
);
551 p
= skip_space (input_line_pointer
);
553 error (_("bad offset expression syntax"));
556 op
->X_add_symbol
= make_expr_symbol (& offset
);
560 /* We treat (i[xy]) as (i[xy]+0), which is how it will
561 end up anyway, unless we're processing jp (i[xy]). */
562 op
->X_add_symbol
= zero
;
567 if ((*p
== 0) || (*p
== ','))
571 /* Not an argument involving a register; use the generic parser. */
572 input_line_pointer
= (char*) s
;
573 *pseg
= expression (op
);
574 if (op
->X_op
== O_absent
)
575 error (_("missing operand"));
576 if (op
->X_op
== O_illegal
)
577 error (_("bad expression syntax"));
578 return input_line_pointer
;
582 parse_exp (const char *s
, expressionS
*op
)
585 return parse_exp2 (s
, op
, & dummy
);
588 /* Condition codes, including some synonyms provided by HiTech zas. */
589 static const struct reg_entry cc_tab
[] =
607 /* Parse condition code. */
609 parse_cc (const char *s
, char * op
)
613 struct reg_entry
* cc_p
;
615 for (i
= 0; i
< BUFLEN
; ++i
)
617 if (!ISALPHA (s
[i
])) /* Condition codes consist of letters only. */
619 buf
[i
] = TOLOWER (s
[i
]);
623 && ((s
[i
] == 0) || (s
[i
] == ',')))
626 cc_p
= bsearch (&key
, cc_tab
, ARRAY_SIZE (cc_tab
),
627 sizeof (cc_tab
[0]), key_cmp
);
644 emit_insn (char prefix
, char opcode
, const char * args
)
659 void z80_cons_fix_new (fragS
*frag_p
, int offset
, int nbytes
, expressionS
*exp
)
661 bfd_reloc_code_real_type r
[4] =
669 if (nbytes
< 1 || nbytes
> 4)
671 as_bad (_("unsupported BFD relocation size %u"), nbytes
);
675 fix_new_exp (frag_p
, offset
, nbytes
, exp
, 0, r
[nbytes
-1]);
680 emit_byte (expressionS
* val
, bfd_reloc_code_real_type r_type
)
687 *p
= val
->X_add_number
;
688 if ((r_type
== BFD_RELOC_8_PCREL
) && (val
->X_op
== O_constant
))
690 as_bad (_("cannot make a relative jump to an absolute location"));
692 else if (val
->X_op
== O_constant
)
695 hi
= (BFD_RELOC_8
== r_type
) ? 255 : 127;
697 if ((val
->X_add_number
< lo
) || (val
->X_add_number
> hi
))
699 if (r_type
== BFD_RELOC_Z80_DISP8
)
700 as_bad (_("offset too large"));
702 as_warn (_("overflow"));
707 fixp
= fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, 1, val
,
708 (r_type
== BFD_RELOC_8_PCREL
) ? TRUE
: FALSE
, r_type
);
709 /* FIXME : Process constant offsets immediately. */
714 emit_word (expressionS
* val
)
719 if ( (val
->X_op
== O_register
)
720 || (val
->X_op
== O_md1
))
724 *p
= val
->X_add_number
;
725 p
[1] = (val
->X_add_number
>>8);
726 if (val
->X_op
!= O_constant
)
727 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, 2,
728 val
, FALSE
, BFD_RELOC_16
);
733 emit_mx (char prefix
, char opcode
, int shift
, expressionS
* arg
)
734 /* The operand m may be r, (hl), (ix+d), (iy+d),
735 if 0 == prefix m may also be ixl, ixh, iyl, iyh. */
740 rnum
= arg
->X_add_number
;
756 if ((prefix
== 0) && (rnum
& R_INDEX
))
758 prefix
= (rnum
& R_IX
) ? 0xDD : 0xFD;
759 check_mach (INS_UNDOC
);
768 q
= frag_more (prefix
? 2 : 1);
771 * q
++ = opcode
+ (rnum
<< shift
);
775 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
776 *q
= (prefix
) ? prefix
: (opcode
+ (6 << shift
));
777 emit_byte (symbol_get_value_expression (arg
->X_add_symbol
),
778 BFD_RELOC_Z80_DISP8
);
782 *q
= opcode
+(6<<shift
);
790 /* The operand m may be r, (hl), (ix+d), (iy+d),
791 if 0 = prefix m may also be ixl, ixh, iyl, iyh. */
793 emit_m (char prefix
, char opcode
, const char *args
)
798 p
= parse_exp (args
, &arg_m
);
803 emit_mx (prefix
, opcode
, 0, &arg_m
);
811 /* The operand m may be as above or one of the undocumented
812 combinations (ix+d),r and (iy+d),r (if unportable instructions
815 emit_mr (char prefix
, char opcode
, const char *args
)
817 expressionS arg_m
, arg_r
;
820 p
= parse_exp (args
, & arg_m
);
827 p
= parse_exp (p
+ 1, & arg_r
);
829 if ((arg_r
.X_md
== 0)
830 && (arg_r
.X_op
== O_register
)
831 && (arg_r
.X_add_number
< 8))
832 opcode
+= arg_r
.X_add_number
-6; /* Emit_mx () will add 6. */
838 check_mach (INS_UNPORT
);
841 emit_mx (prefix
, opcode
, 0, & arg_m
);
850 emit_sx (char prefix
, char opcode
, expressionS
* arg_p
)
858 emit_mx (prefix
, opcode
, 0, arg_p
);
865 q
= frag_more (prefix
? 2 : 1);
869 emit_byte (arg_p
, BFD_RELOC_8
);
874 /* The operand s may be r, (hl), (ix+d), (iy+d), n. */
876 emit_s (char prefix
, char opcode
, const char *args
)
881 p
= parse_exp (args
, & arg_s
);
882 emit_sx (prefix
, opcode
, & arg_s
);
887 emit_call (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
890 const char *p
; char *q
;
892 p
= parse_exp (args
, &addr
);
904 /* Operand may be rr, r, (hl), (ix+d), (iy+d). */
906 emit_incdec (char prefix
, char opcode
, const char * args
)
910 const char *p
; char *q
;
912 p
= parse_exp (args
, &operand
);
913 rnum
= operand
.X_add_number
;
915 && (operand
.X_op
== O_register
)
918 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
920 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
921 *q
= prefix
+ ((rnum
& 3) << 4);
925 if ((operand
.X_op
== O_md1
) || (operand
.X_op
== O_register
))
926 emit_mx (0, opcode
, 3, & operand
);
934 emit_jr (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
940 p
= parse_exp (args
, &addr
);
947 emit_byte (&addr
, BFD_RELOC_8_PCREL
);
953 emit_jp (char prefix
, char opcode
, const char * args
)
960 p
= parse_exp (args
, & addr
);
963 rnum
= addr
.X_add_number
;
964 if ((addr
.X_op
== O_register
&& (rnum
& ~R_INDEX
) == REG_HL
)
965 /* An operand (i[xy]) would have been rewritten to (i[xy]+0)
967 || (addr
.X_op
== O_md1
&& addr
.X_add_symbol
== zero
))
969 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
971 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
987 emit_im (char prefix
, char opcode
, const char * args
)
993 p
= parse_exp (args
, & mode
);
994 if (mode
.X_md
|| (mode
.X_op
!= O_constant
))
997 switch (mode
.X_add_number
)
1001 ++mode
.X_add_number
;
1006 *q
= opcode
+ 8*mode
.X_add_number
;
1015 emit_pop (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1021 p
= parse_exp (args
, & regp
);
1023 && (regp
.X_op
== O_register
)
1024 && (regp
.X_add_number
& R_STACKABLE
))
1028 rnum
= regp
.X_add_number
;
1032 *q
++ = (rnum
&R_IX
)?0xDD:0xFD;
1036 *q
= opcode
+ ((rnum
& 3) << 4);
1045 emit_retcc (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1050 p
= parse_cc (args
, &cc
);
1056 return p
? p
: args
;
1060 emit_adc (char prefix
, char opcode
, const char * args
)
1067 p
= parse_exp (args
, &term
);
1070 error (_("bad intruction syntax"));
1074 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1077 switch (term
.X_add_number
)
1080 p
= emit_s (0, prefix
, p
);
1083 p
= parse_exp (p
, &term
);
1084 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1086 rnum
= term
.X_add_number
;
1087 if (R_ARITH
== (rnum
& (R_ARITH
| R_INDEX
)))
1091 *q
= opcode
+ ((rnum
& 3) << 4);
1103 emit_add (char prefix
, char opcode
, const char * args
)
1110 p
= parse_exp (args
, &term
);
1113 error (_("bad intruction syntax"));
1117 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1120 switch (term
.X_add_number
& ~R_INDEX
)
1123 p
= emit_s (0, prefix
, p
);
1126 lhs
= term
.X_add_number
;
1127 p
= parse_exp (p
, &term
);
1128 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1130 rhs
= term
.X_add_number
;
1132 && ((rhs
== lhs
) || ((rhs
& ~R_INDEX
) != REG_HL
)))
1134 q
= frag_more ((lhs
& R_INDEX
) ? 2 : 1);
1136 *q
++ = (lhs
& R_IX
) ? 0xDD : 0xFD;
1137 *q
= opcode
+ ((rhs
& 3) << 4);
1149 emit_bit (char prefix
, char opcode
, const char * args
)
1155 p
= parse_exp (args
, &b
);
1157 error (_("bad intruction syntax"));
1159 bn
= b
.X_add_number
;
1161 && (b
.X_op
== O_constant
)
1166 /* Bit : no optional third operand. */
1167 p
= emit_m (prefix
, opcode
+ (bn
<< 3), p
);
1169 /* Set, res : resulting byte can be copied to register. */
1170 p
= emit_mr (prefix
, opcode
+ (bn
<< 3), p
);
1178 emit_jpcc (char prefix
, char opcode
, const char * args
)
1183 p
= parse_cc (args
, & cc
);
1184 if (p
&& *p
++ == ',')
1185 p
= emit_call (0, opcode
+ cc
, p
);
1187 p
= (prefix
== (char)0xC3)
1188 ? emit_jp (0xE9, prefix
, args
)
1189 : emit_call (0, prefix
, args
);
1194 emit_jrcc (char prefix
, char opcode
, const char * args
)
1199 p
= parse_cc (args
, &cc
);
1200 if (p
&& *p
++ == ',')
1203 error (_("condition code invalid for jr"));
1205 p
= emit_jr (0, opcode
+ cc
, p
);
1208 p
= emit_jr (0, prefix
, args
);
1214 emit_ex (char prefix_in ATTRIBUTE_UNUSED
,
1215 char opcode_in ATTRIBUTE_UNUSED
, const char * args
)
1219 char prefix
, opcode
;
1221 p
= parse_exp (args
, &op
);
1225 error (_("bad instruction syntax"));
1229 prefix
= opcode
= 0;
1230 if (op
.X_op
== O_register
)
1231 switch (op
.X_add_number
| (op
.X_md
? 0x8000 : 0))
1234 if (TOLOWER (*p
++) == 'a' && TOLOWER (*p
++) == 'f')
1236 /* The scrubber changes '\'' to '`' in this context. */
1243 if (TOLOWER (*p
++) == 'h' && TOLOWER (*p
++) == 'l')
1247 p
= parse_exp (p
, & op
);
1248 if (op
.X_op
== O_register
1250 && (op
.X_add_number
& ~R_INDEX
) == REG_HL
)
1253 if (R_INDEX
& op
.X_add_number
)
1254 prefix
= (R_IX
& op
.X_add_number
) ? 0xDD : 0xFD;
1259 emit_insn (prefix
, opcode
, p
);
1267 emit_in (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1270 expressionS reg
, port
;
1274 p
= parse_exp (args
, ®
);
1277 error (_("bad intruction syntax"));
1281 p
= parse_exp (p
, &port
);
1283 && reg
.X_op
== O_register
1284 && (reg
.X_add_number
<= 7 || reg
.X_add_number
== REG_F
)
1287 if (port
.X_op
!= O_md1
&& port
.X_op
!= O_register
)
1289 if (REG_A
== reg
.X_add_number
)
1293 emit_byte (&port
, BFD_RELOC_8
);
1300 if (port
.X_add_number
== REG_C
)
1302 if (reg
.X_add_number
== REG_F
)
1303 check_mach (INS_UNDOC
);
1308 *q
= 0x40|((reg
.X_add_number
&7)<<3);
1321 emit_out (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1324 expressionS reg
, port
;
1328 p
= parse_exp (args
, & port
);
1331 error (_("bad intruction syntax"));
1334 p
= parse_exp (p
, ®
);
1336 { ill_op (); return p
; }
1337 /* Allow "out (c), 0" as unportable instruction. */
1338 if (reg
.X_op
== O_constant
&& reg
.X_add_number
== 0)
1340 check_mach (INS_UNPORT
);
1341 reg
.X_op
= O_register
;
1342 reg
.X_add_number
= 6;
1345 || reg
.X_op
!= O_register
1346 || reg
.X_add_number
> 7)
1349 if (port
.X_op
!= O_register
&& port
.X_op
!= O_md1
)
1351 if (REG_A
== reg
.X_add_number
)
1355 emit_byte (&port
, BFD_RELOC_8
);
1362 if (REG_C
== port
.X_add_number
)
1366 *q
= 0x41 | (reg
.X_add_number
<< 3);
1375 emit_rst (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1381 p
= parse_exp (args
, &addr
);
1382 if (addr
.X_op
!= O_constant
)
1384 error ("rst needs constant address");
1388 if (addr
.X_add_number
& ~(7 << 3))
1393 *q
= opcode
+ (addr
.X_add_number
& (7 << 3));
1399 emit_ldxhl (char prefix
, char opcode
, expressionS
*src
, expressionS
*d
)
1407 if (src
->X_op
== O_register
)
1409 if (src
->X_add_number
>7)
1418 *q
= opcode
+ src
->X_add_number
;
1420 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1433 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1434 emit_byte (src
, BFD_RELOC_8
);
1440 emit_ldreg (int dest
, expressionS
* src
)
1447 /* 8 Bit ld group: */
1450 if (src
->X_md
== 0 && src
->X_op
== O_register
&& src
->X_add_number
== REG_A
)
1454 *q
= (dest
== REG_I
) ? 0x47 : 0x4F;
1461 if ((src
->X_md
) && src
->X_op
!= O_register
&& src
->X_op
!= O_md1
)
1470 && src
->X_op
== O_register
1471 && (src
->X_add_number
== REG_BC
|| src
->X_add_number
== REG_DE
))
1474 *q
= 0x0A + ((src
->X_add_number
& 1) << 4);
1479 && src
->X_op
== O_register
1480 && (src
->X_add_number
== REG_R
|| src
->X_add_number
== REG_I
))
1484 *q
= (src
->X_add_number
== REG_I
) ? 0x57 : 0x5F;
1492 emit_sx (0, 0x40 + (dest
<< 3), src
);
1497 if ((src
->X_md
== 0)
1498 && (src
->X_op
== O_register
)
1499 && (src
->X_add_number
& R_INDEX
))
1502 emit_sx (0, 0x40 + (dest
<< 3), src
);
1514 check_mach (INS_UNDOC
);
1515 if (src
-> X_op
== O_register
)
1517 rnum
= src
->X_add_number
;
1518 if ((rnum
& ~R_INDEX
) < 8
1519 && ((rnum
& R_INDEX
) == (dest
& R_INDEX
)
1520 || ( (rnum
& ~R_INDEX
) != REG_H
1521 && (rnum
& ~R_INDEX
) != REG_L
)))
1524 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1525 *q
= 0x40 + ((dest
& 0x07) << 3) + (rnum
& 7);
1533 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1534 *q
= 0x06 + ((dest
& 0x07) << 3);
1535 emit_byte (src
, BFD_RELOC_8
);
1539 /* 16 Bit ld group: */
1542 && src
->X_op
== O_register
1543 && REG_HL
== (src
->X_add_number
&~ R_INDEX
))
1545 q
= frag_more ((src
->X_add_number
& R_INDEX
) ? 2 : 1);
1546 if (src
->X_add_number
& R_INDEX
)
1547 *q
++ = (src
->X_add_number
& R_IX
) ? 0xDD : 0xFD;
1554 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1556 q
= frag_more (src
->X_md
? 2 : 1);
1560 *q
= 0x4B + ((dest
& 3) << 4);
1563 *q
= 0x01 + ((dest
& 3) << 4);
1570 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1572 q
= frag_more ((dest
& R_INDEX
) ? 2 : 1);
1574 * q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1575 *q
= (src
->X_md
) ? 0x2A : 0x21;
1590 emit_ld (char prefix_in ATTRIBUTE_UNUSED
, char opcode_in ATTRIBUTE_UNUSED
,
1593 expressionS dst
, src
;
1596 char prefix
, opcode
;
1598 p
= parse_exp (args
, &dst
);
1600 error (_("bad intruction syntax"));
1601 p
= parse_exp (p
, &src
);
1606 emit_ldxhl ((dst
.X_add_number
& R_IX
) ? 0xDD : 0xFD, 0x70,
1607 &src
, symbol_get_value_expression (dst
.X_add_symbol
));
1613 switch (dst
.X_add_number
)
1617 if (src
.X_md
== 0 && src
.X_op
== O_register
&& src
.X_add_number
== REG_A
)
1620 *q
= 0x02 + ( (dst
.X_add_number
& 1) << 4);
1626 emit_ldxhl (0, 0x70, &src
, NULL
);
1633 emit_ldreg (dst
.X_add_number
, &src
);
1637 if (src
.X_md
!= 0 || src
.X_op
!= O_register
)
1639 prefix
= opcode
= 0;
1640 switch (src
.X_add_number
)
1643 opcode
= 0x32; break;
1644 case REG_BC
: case REG_DE
: case REG_SP
:
1645 prefix
= 0xED; opcode
= 0x43 + ((src
.X_add_number
&3)<<4); break;
1647 opcode
= 0x22; break;
1649 prefix
= 0xDD; opcode
= 0x22; break;
1651 prefix
= 0xFD; opcode
= 0x22; break;
1655 q
= frag_more (prefix
?2:1);
1668 emit_data (int size ATTRIBUTE_UNUSED
)
1675 if (is_it_end_of_statement ())
1677 demand_empty_rest_of_line ();
1680 p
= skip_space (input_line_pointer
);
1684 if (*p
== '\"' || *p
== '\'')
1686 for (quote
= *p
, q
= ++p
, cnt
= 0; *p
&& quote
!= *p
; ++p
, ++cnt
)
1688 u
= frag_more (cnt
);
1691 as_warn (_("unterminated string"));
1693 p
= skip_space (p
+1);
1697 p
= parse_exp (p
, &exp
);
1698 if (exp
.X_op
== O_md1
|| exp
.X_op
== O_register
)
1704 as_warn (_("parentheses ignored"));
1705 emit_byte (&exp
, BFD_RELOC_8
);
1709 while (*p
++ == ',') ;
1710 input_line_pointer
= (char *)(p
-1);
1714 emit_mulub (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1718 p
= skip_space (args
);
1719 if (TOLOWER (*p
++) != 'a' || *p
++ != ',')
1725 reg
= TOLOWER (*p
++);
1732 check_mach (INS_R800
);
1733 if (!*skip_space (p
))
1737 *q
= opcode
+ ((reg
- 'b') << 3);
1748 emit_muluw (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1752 p
= skip_space (args
);
1753 if (TOLOWER (*p
++) != 'h' || TOLOWER (*p
++) != 'l' || *p
++ != ',')
1760 p
= parse_exp (p
, & reg
);
1762 if ((!reg
.X_md
) && reg
.X_op
== O_register
)
1763 switch (reg
.X_add_number
)
1767 check_mach (INS_R800
);
1770 *q
= opcode
+ ((reg
.X_add_number
& 3) << 4);
1779 /* Port specific pseudo ops. */
1780 const pseudo_typeS md_pseudo_table
[] =
1782 { "db" , emit_data
, 1},
1785 { "def24", cons
, 3},
1786 { "def32", cons
, 4},
1787 { "defb", emit_data
, 1},
1788 { "defs", s_space
, 1}, /* Synonym for ds on some assemblers. */
1790 { "ds", s_space
, 1}, /* Fill with bytes rather than words. */
1792 { "psect", obj_coff_section
, 0}, /* TODO: Translate attributes. */
1793 { "set", 0, 0}, /* Real instruction on z80. */
1797 static table_t instab
[] =
1799 { "adc", 0x88, 0x4A, emit_adc
},
1800 { "add", 0x80, 0x09, emit_add
},
1801 { "and", 0x00, 0xA0, emit_s
},
1802 { "bit", 0xCB, 0x40, emit_bit
},
1803 { "call", 0xCD, 0xC4, emit_jpcc
},
1804 { "ccf", 0x00, 0x3F, emit_insn
},
1805 { "cp", 0x00, 0xB8, emit_s
},
1806 { "cpd", 0xED, 0xA9, emit_insn
},
1807 { "cpdr", 0xED, 0xB9, emit_insn
},
1808 { "cpi", 0xED, 0xA1, emit_insn
},
1809 { "cpir", 0xED, 0xB1, emit_insn
},
1810 { "cpl", 0x00, 0x2F, emit_insn
},
1811 { "daa", 0x00, 0x27, emit_insn
},
1812 { "dec", 0x0B, 0x05, emit_incdec
},
1813 { "di", 0x00, 0xF3, emit_insn
},
1814 { "djnz", 0x00, 0x10, emit_jr
},
1815 { "ei", 0x00, 0xFB, emit_insn
},
1816 { "ex", 0x00, 0x00, emit_ex
},
1817 { "exx", 0x00, 0xD9, emit_insn
},
1818 { "halt", 0x00, 0x76, emit_insn
},
1819 { "im", 0xED, 0x46, emit_im
},
1820 { "in", 0x00, 0x00, emit_in
},
1821 { "inc", 0x03, 0x04, emit_incdec
},
1822 { "ind", 0xED, 0xAA, emit_insn
},
1823 { "indr", 0xED, 0xBA, emit_insn
},
1824 { "ini", 0xED, 0xA2, emit_insn
},
1825 { "inir", 0xED, 0xB2, emit_insn
},
1826 { "jp", 0xC3, 0xC2, emit_jpcc
},
1827 { "jr", 0x18, 0x20, emit_jrcc
},
1828 { "ld", 0x00, 0x00, emit_ld
},
1829 { "ldd", 0xED, 0xA8, emit_insn
},
1830 { "lddr", 0xED, 0xB8, emit_insn
},
1831 { "ldi", 0xED, 0xA0, emit_insn
},
1832 { "ldir", 0xED, 0xB0, emit_insn
},
1833 { "mulub", 0xED, 0xC5, emit_mulub
}, /* R800 only. */
1834 { "muluw", 0xED, 0xC3, emit_muluw
}, /* R800 only. */
1835 { "neg", 0xed, 0x44, emit_insn
},
1836 { "nop", 0x00, 0x00, emit_insn
},
1837 { "or", 0x00, 0xB0, emit_s
},
1838 { "otdr", 0xED, 0xBB, emit_insn
},
1839 { "otir", 0xED, 0xB3, emit_insn
},
1840 { "out", 0x00, 0x00, emit_out
},
1841 { "outd", 0xED, 0xAB, emit_insn
},
1842 { "outi", 0xED, 0xA3, emit_insn
},
1843 { "pop", 0x00, 0xC1, emit_pop
},
1844 { "push", 0x00, 0xC5, emit_pop
},
1845 { "res", 0xCB, 0x80, emit_bit
},
1846 { "ret", 0xC9, 0xC0, emit_retcc
},
1847 { "reti", 0xED, 0x4D, emit_insn
},
1848 { "retn", 0xED, 0x45, emit_insn
},
1849 { "rl", 0xCB, 0x10, emit_mr
},
1850 { "rla", 0x00, 0x17, emit_insn
},
1851 { "rlc", 0xCB, 0x00, emit_mr
},
1852 { "rlca", 0x00, 0x07, emit_insn
},
1853 { "rld", 0xED, 0x6F, emit_insn
},
1854 { "rr", 0xCB, 0x18, emit_mr
},
1855 { "rra", 0x00, 0x1F, emit_insn
},
1856 { "rrc", 0xCB, 0x08, emit_mr
},
1857 { "rrca", 0x00, 0x0F, emit_insn
},
1858 { "rrd", 0xED, 0x67, emit_insn
},
1859 { "rst", 0x00, 0xC7, emit_rst
},
1860 { "sbc", 0x98, 0x42, emit_adc
},
1861 { "scf", 0x00, 0x37, emit_insn
},
1862 { "set", 0xCB, 0xC0, emit_bit
},
1863 { "sla", 0xCB, 0x20, emit_mr
},
1864 { "sli", 0xCB, 0x30, emit_mr
},
1865 { "sll", 0xCB, 0x30, emit_mr
},
1866 { "sra", 0xCB, 0x28, emit_mr
},
1867 { "srl", 0xCB, 0x38, emit_mr
},
1868 { "sub", 0x00, 0x90, emit_s
},
1869 { "xor", 0x00, 0xA8, emit_s
},
1873 md_assemble (char* str
)
1881 old_ptr
= input_line_pointer
;
1882 p
= skip_space (str
);
1883 for (i
= 0; (i
< BUFLEN
) && (ISALPHA (*p
));)
1884 buf
[i
++] = TOLOWER (*p
++);
1888 buf
[BUFLEN
-3] = buf
[BUFLEN
-2] = '.'; /* Mark opcode as abbreviated. */
1890 as_bad (_("Unknown instruction '%s'"), buf
);
1892 else if ((*p
) && (!ISSPACE (*p
)))
1893 as_bad (_("syntax error"));
1900 insp
= bsearch (&key
, instab
, ARRAY_SIZE (instab
),
1901 sizeof (instab
[0]), key_cmp
);
1903 as_bad (_("Unknown instruction '%s'"), buf
);
1906 p
= insp
->fp (insp
->prefix
, insp
->opcode
, p
);
1908 if ((!err_flag
) && *p
)
1909 as_bad (_("junk at end of line, first unrecognized character is `%c'"),
1913 input_line_pointer
= old_ptr
;
1917 md_apply_fix (fixS
* fixP
, valueT
* valP
, segT seg ATTRIBUTE_UNUSED
)
1919 long val
= * (long *) valP
;
1920 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1922 switch (fixP
->fx_r_type
)
1924 case BFD_RELOC_8_PCREL
:
1927 fixP
->fx_no_overflow
= 1;
1932 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1933 if (!fixP
->fx_no_overflow
)
1934 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1935 _("relative jump out of range"));
1941 case BFD_RELOC_Z80_DISP8
:
1944 fixP
->fx_no_overflow
= 1;
1949 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1950 if (!fixP
->fx_no_overflow
)
1951 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1952 _("index offset out of range"));
1959 if (val
> 255 || val
< -128)
1960 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, _("overflow"));
1962 fixP
->fx_no_overflow
= 1;
1963 if (fixP
->fx_addsy
== NULL
)
1969 *buf
++ = (val
>> 8);
1970 fixP
->fx_no_overflow
= 1;
1971 if (fixP
->fx_addsy
== NULL
)
1975 case BFD_RELOC_24
: /* Def24 may produce this. */
1977 *buf
++ = (val
>> 8);
1978 *buf
++ = (val
>> 16);
1979 fixP
->fx_no_overflow
= 1;
1980 if (fixP
->fx_addsy
== NULL
)
1984 case BFD_RELOC_32
: /* Def32 and .long may produce this. */
1986 *buf
++ = (val
>> 8);
1987 *buf
++ = (val
>> 16);
1988 *buf
++ = (val
>> 24);
1989 if (fixP
->fx_addsy
== NULL
)
1994 printf (_("md_apply_fix: unknown r_type 0x%x\n"), fixP
->fx_r_type
);
1999 /* GAS will call this to generate a reloc. GAS will pass the
2000 resulting reloc to `bfd_install_relocation'. This currently works
2001 poorly, as `bfd_install_relocation' often does the wrong thing, and
2002 instances of `tc_gen_reloc' have been written to work around the
2003 problems, which in turns makes it difficult to fix
2004 `bfd_install_relocation'. */
2006 /* If while processing a fixup, a reloc really
2007 needs to be created then it is done here. */
2010 tc_gen_reloc (asection
*seg ATTRIBUTE_UNUSED
, fixS
*fixp
)
2014 if (! bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
))
2016 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2017 _("reloc %d not supported by object file format"),
2018 (int) fixp
->fx_r_type
);
2022 reloc
= xmalloc (sizeof (arelent
));
2023 reloc
->sym_ptr_ptr
= xmalloc (sizeof (asymbol
*));
2024 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2025 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2026 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2027 reloc
->addend
= fixp
->fx_offset
;