1 /* tc-z80.c -- Assemble code for the Zilog Z80 and ASCII R800
2 Copyright 2005, 2006, 2007, 2008, 2009 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
;
257 line_start
= input_line_pointer
;
261 c
= get_symbol_end ();
262 rest
= input_line_pointer
+ 1;
266 if (*rest
== ' ' || *rest
== '\t')
270 if (strncasecmp (rest
, "EQU", 3) == 0)
272 else if (strncasecmp (rest
, "DEFL", 4) == 0)
276 if (len
&& (!ISALPHA(rest
[len
]) ) )
278 /* Handle assignment here. */
279 if (line_start
[-1] == '\n')
281 bump_line_counters ();
284 input_line_pointer
= rest
+ len
- 1;
285 /* Allow redefining with "DEFL" (len == 4), but not with "EQU". */
286 equals (line_start
, len
== 4);
291 /* Restore line and pointer. */
292 *input_line_pointer
= c
;
293 input_line_pointer
= line_start
;
300 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
306 md_atof (int type ATTRIBUTE_UNUSED
, char *litP ATTRIBUTE_UNUSED
,
307 int *sizeP ATTRIBUTE_UNUSED
)
309 return _("floating point numbers are not implemented");
313 md_section_align (segT seg ATTRIBUTE_UNUSED
, valueT size
)
319 md_pcrel_from (fixS
* fixp
)
321 return fixp
->fx_where
+
322 fixp
->fx_frag
->fr_address
+ 1;
325 typedef const char * (asfunc
)(char, char, const char*);
327 typedef struct _table_t
335 /* Compares the key for structs that start with a char * to the key. */
337 key_cmp (const void * a
, const void * b
)
339 const char *str_a
, *str_b
;
341 str_a
= *((const char**)a
);
342 str_b
= *((const char**)b
);
343 return strcmp (str_a
, str_b
);
346 #define BUFLEN 8 /* Large enough for any keyword. */
349 const char *key
= buf
;
351 #define R_STACKABLE (0x80)
352 #define R_ARITH (0x40)
355 #define R_INDEX (R_IX | R_IY)
364 #define REG_F (6 | 8)
368 #define REG_AF (3 | R_STACKABLE)
369 #define REG_BC (0 | R_STACKABLE | R_ARITH)
370 #define REG_DE (1 | R_STACKABLE | R_ARITH)
371 #define REG_HL (2 | R_STACKABLE | R_ARITH)
372 #define REG_SP (3 | R_ARITH)
374 static const struct reg_entry
392 {"ix", REG_HL
| R_IX
},
393 {"ixh",REG_H
| R_IX
},
394 {"ixl",REG_L
| R_IX
},
395 {"iy", REG_HL
| R_IY
},
396 {"iyh",REG_H
| R_IY
},
397 {"iyl",REG_L
| R_IY
},
403 /* Prevent an error on a line from also generating
404 a "junk at end of line" error message. */
405 static char err_flag
;
408 error (const char * message
)
410 as_bad ("%s", message
);
417 error (_("illegal operand"));
421 wrong_mach (int ins_type
)
428 p
= "undocumented instruction";
431 p
= "instruction does not work on R800";
434 p
= "instruction only works R800";
437 p
= 0; /* Not reachable. */
440 if (ins_type
& ins_err
)
447 check_mach (int ins_type
)
449 if ((ins_type
& ins_ok
) == 0)
450 wrong_mach (ins_type
);
451 ins_used
|= ins_type
;
454 /* Check whether an expression is indirect. */
456 is_indir (const char *s
)
462 /* Indirection is indicated with parentheses. */
465 for (p
= s
, depth
= 0; *p
&& *p
!= ','; ++p
)
471 for (quote
= *p
++; quote
!= *p
&& *p
!= '\n'; ++p
)
472 if (*p
== '\\' && p
[1])
482 p
= skip_space (p
+ 1);
488 error (_("mismatched parentheses"));
494 error (_("mismatched parentheses"));
499 /* Parse general expression. */
501 parse_exp2 (const char *s
, expressionS
*op
, segT
*pseg
)
506 const struct reg_entry
* regp
;
510 op
->X_md
= indir
= is_indir (p
);
512 p
= skip_space (p
+ 1);
514 for (i
= 0; i
< BUFLEN
; ++i
)
516 if (!ISALPHA (p
[i
])) /* Register names consist of letters only. */
518 buf
[i
] = TOLOWER (p
[i
]);
521 if ((i
< BUFLEN
) && ((p
[i
] == 0) || (strchr (")+-, \t", p
[i
]))))
524 regp
= bsearch (& key
, regtable
, ARRAY_SIZE (regtable
),
525 sizeof (regtable
[0]), key_cmp
);
529 op
->X_add_symbol
= op
->X_op_symbol
= 0;
530 op
->X_add_number
= regp
->number
;
531 op
->X_op
= O_register
;
532 p
+= strlen (regp
->name
);
538 if ((regp
->number
& R_INDEX
) && (regp
->number
& R_ARITH
))
542 if ((*p
== '+') || (*p
== '-'))
544 input_line_pointer
= (char*) p
;
545 expression (& offset
);
546 p
= skip_space (input_line_pointer
);
548 error (_("bad offset expression syntax"));
551 op
->X_add_symbol
= make_expr_symbol (& offset
);
555 /* We treat (i[xy]) as (i[xy]+0), which is how it will
556 end up anyway, unless we're processing jp (i[xy]). */
557 op
->X_add_symbol
= zero
;
562 if ((*p
== 0) || (*p
== ','))
566 /* Not an argument involving a register; use the generic parser. */
567 input_line_pointer
= (char*) s
;
568 *pseg
= expression (op
);
569 if (op
->X_op
== O_absent
)
570 error (_("missing operand"));
571 if (op
->X_op
== O_illegal
)
572 error (_("bad expression syntax"));
573 return input_line_pointer
;
577 parse_exp (const char *s
, expressionS
*op
)
580 return parse_exp2 (s
, op
, & dummy
);
583 /* Condition codes, including some synonyms provided by HiTech zas. */
584 static const struct reg_entry cc_tab
[] =
602 /* Parse condition code. */
604 parse_cc (const char *s
, char * op
)
608 struct reg_entry
* cc_p
;
610 for (i
= 0; i
< BUFLEN
; ++i
)
612 if (!ISALPHA (s
[i
])) /* Condition codes consist of letters only. */
614 buf
[i
] = TOLOWER (s
[i
]);
618 && ((s
[i
] == 0) || (s
[i
] == ',')))
621 cc_p
= bsearch (&key
, cc_tab
, ARRAY_SIZE (cc_tab
),
622 sizeof (cc_tab
[0]), key_cmp
);
639 emit_insn (char prefix
, char opcode
, const char * args
)
654 void z80_cons_fix_new (fragS
*frag_p
, int offset
, int nbytes
, expressionS
*exp
)
656 bfd_reloc_code_real_type r
[4] =
664 if (nbytes
< 1 || nbytes
> 4)
666 as_bad (_("unsupported BFD relocation size %u"), nbytes
);
670 fix_new_exp (frag_p
, offset
, nbytes
, exp
, 0, r
[nbytes
-1]);
675 emit_byte (expressionS
* val
, bfd_reloc_code_real_type r_type
)
682 *p
= val
->X_add_number
;
683 if ((r_type
== BFD_RELOC_8_PCREL
) && (val
->X_op
== O_constant
))
685 as_bad (_("cannot make a relative jump to an absolute location"));
687 else if (val
->X_op
== O_constant
)
690 hi
= (BFD_RELOC_8
== r_type
) ? 255 : 127;
692 if ((val
->X_add_number
< lo
) || (val
->X_add_number
> hi
))
694 if (r_type
== BFD_RELOC_Z80_DISP8
)
695 as_bad (_("offset too large"));
697 as_warn (_("overflow"));
702 fixp
= fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, 1, val
,
703 (r_type
== BFD_RELOC_8_PCREL
) ? TRUE
: FALSE
, r_type
);
704 /* FIXME : Process constant offsets immediately. */
709 emit_word (expressionS
* val
)
714 if ( (val
->X_op
== O_register
)
715 || (val
->X_op
== O_md1
))
719 *p
= val
->X_add_number
;
720 p
[1] = (val
->X_add_number
>>8);
721 if (val
->X_op
!= O_constant
)
722 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, 2,
723 val
, FALSE
, BFD_RELOC_16
);
728 emit_mx (char prefix
, char opcode
, int shift
, expressionS
* arg
)
729 /* The operand m may be r, (hl), (ix+d), (iy+d),
730 if 0 == prefix m may also be ixl, ixh, iyl, iyh. */
735 rnum
= arg
->X_add_number
;
751 if ((prefix
== 0) && (rnum
& R_INDEX
))
753 prefix
= (rnum
& R_IX
) ? 0xDD : 0xFD;
754 check_mach (INS_UNDOC
);
763 q
= frag_more (prefix
? 2 : 1);
766 * q
++ = opcode
+ (rnum
<< shift
);
770 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
771 *q
= (prefix
) ? prefix
: (opcode
+ (6 << shift
));
772 emit_byte (symbol_get_value_expression (arg
->X_add_symbol
),
773 BFD_RELOC_Z80_DISP8
);
777 *q
= opcode
+(6<<shift
);
785 /* The operand m may be r, (hl), (ix+d), (iy+d),
786 if 0 = prefix m may also be ixl, ixh, iyl, iyh. */
788 emit_m (char prefix
, char opcode
, const char *args
)
793 p
= parse_exp (args
, &arg_m
);
798 emit_mx (prefix
, opcode
, 0, &arg_m
);
806 /* The operand m may be as above or one of the undocumented
807 combinations (ix+d),r and (iy+d),r (if unportable instructions
810 emit_mr (char prefix
, char opcode
, const char *args
)
812 expressionS arg_m
, arg_r
;
815 p
= parse_exp (args
, & arg_m
);
822 p
= parse_exp (p
+ 1, & arg_r
);
824 if ((arg_r
.X_md
== 0)
825 && (arg_r
.X_op
== O_register
)
826 && (arg_r
.X_add_number
< 8))
827 opcode
+= arg_r
.X_add_number
-6; /* Emit_mx () will add 6. */
833 check_mach (INS_UNPORT
);
836 emit_mx (prefix
, opcode
, 0, & arg_m
);
845 emit_sx (char prefix
, char opcode
, expressionS
* arg_p
)
853 emit_mx (prefix
, opcode
, 0, arg_p
);
860 q
= frag_more (prefix
? 2 : 1);
864 emit_byte (arg_p
, BFD_RELOC_8
);
869 /* The operand s may be r, (hl), (ix+d), (iy+d), n. */
871 emit_s (char prefix
, char opcode
, const char *args
)
876 p
= parse_exp (args
, & arg_s
);
877 emit_sx (prefix
, opcode
, & arg_s
);
882 emit_call (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
885 const char *p
; char *q
;
887 p
= parse_exp (args
, &addr
);
899 /* Operand may be rr, r, (hl), (ix+d), (iy+d). */
901 emit_incdec (char prefix
, char opcode
, const char * args
)
905 const char *p
; char *q
;
907 p
= parse_exp (args
, &operand
);
908 rnum
= operand
.X_add_number
;
910 && (operand
.X_op
== O_register
)
913 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
915 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
916 *q
= prefix
+ ((rnum
& 3) << 4);
920 if ((operand
.X_op
== O_md1
) || (operand
.X_op
== O_register
))
921 emit_mx (0, opcode
, 3, & operand
);
929 emit_jr (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
935 p
= parse_exp (args
, &addr
);
942 emit_byte (&addr
, BFD_RELOC_8_PCREL
);
948 emit_jp (char prefix
, char opcode
, const char * args
)
955 p
= parse_exp (args
, & addr
);
958 rnum
= addr
.X_add_number
;
959 if ((addr
.X_op
== O_register
&& (rnum
& ~R_INDEX
) == REG_HL
)
960 /* An operand (i[xy]) would have been rewritten to (i[xy]+0)
962 || (addr
.X_op
== O_md1
&& addr
.X_add_symbol
== zero
))
964 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
966 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
982 emit_im (char prefix
, char opcode
, const char * args
)
988 p
= parse_exp (args
, & mode
);
989 if (mode
.X_md
|| (mode
.X_op
!= O_constant
))
992 switch (mode
.X_add_number
)
1001 *q
= opcode
+ 8*mode
.X_add_number
;
1010 emit_pop (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1016 p
= parse_exp (args
, & regp
);
1018 && (regp
.X_op
== O_register
)
1019 && (regp
.X_add_number
& R_STACKABLE
))
1023 rnum
= regp
.X_add_number
;
1027 *q
++ = (rnum
&R_IX
)?0xDD:0xFD;
1031 *q
= opcode
+ ((rnum
& 3) << 4);
1040 emit_retcc (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1045 p
= parse_cc (args
, &cc
);
1051 return p
? p
: args
;
1055 emit_adc (char prefix
, char opcode
, const char * args
)
1062 p
= parse_exp (args
, &term
);
1065 error (_("bad intruction syntax"));
1069 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1072 switch (term
.X_add_number
)
1075 p
= emit_s (0, prefix
, p
);
1078 p
= parse_exp (p
, &term
);
1079 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1081 rnum
= term
.X_add_number
;
1082 if (R_ARITH
== (rnum
& (R_ARITH
| R_INDEX
)))
1086 *q
= opcode
+ ((rnum
& 3) << 4);
1098 emit_add (char prefix
, char opcode
, const char * args
)
1105 p
= parse_exp (args
, &term
);
1108 error (_("bad intruction syntax"));
1112 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1115 switch (term
.X_add_number
& ~R_INDEX
)
1118 p
= emit_s (0, prefix
, p
);
1121 lhs
= term
.X_add_number
;
1122 p
= parse_exp (p
, &term
);
1123 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1125 rhs
= term
.X_add_number
;
1127 && ((rhs
== lhs
) || ((rhs
& ~R_INDEX
) != REG_HL
)))
1129 q
= frag_more ((lhs
& R_INDEX
) ? 2 : 1);
1131 *q
++ = (lhs
& R_IX
) ? 0xDD : 0xFD;
1132 *q
= opcode
+ ((rhs
& 3) << 4);
1144 emit_bit (char prefix
, char opcode
, const char * args
)
1150 p
= parse_exp (args
, &b
);
1152 error (_("bad intruction syntax"));
1154 bn
= b
.X_add_number
;
1156 && (b
.X_op
== O_constant
)
1161 /* Bit : no optional third operand. */
1162 p
= emit_m (prefix
, opcode
+ (bn
<< 3), p
);
1164 /* Set, res : resulting byte can be copied to register. */
1165 p
= emit_mr (prefix
, opcode
+ (bn
<< 3), p
);
1173 emit_jpcc (char prefix
, char opcode
, const char * args
)
1178 p
= parse_cc (args
, & cc
);
1179 if (p
&& *p
++ == ',')
1180 p
= emit_call (0, opcode
+ cc
, p
);
1182 p
= (prefix
== (char)0xC3)
1183 ? emit_jp (0xE9, prefix
, args
)
1184 : emit_call (0, prefix
, args
);
1189 emit_jrcc (char prefix
, char opcode
, const char * args
)
1194 p
= parse_cc (args
, &cc
);
1195 if (p
&& *p
++ == ',')
1198 error (_("condition code invalid for jr"));
1200 p
= emit_jr (0, opcode
+ cc
, p
);
1203 p
= emit_jr (0, prefix
, args
);
1209 emit_ex (char prefix_in ATTRIBUTE_UNUSED
,
1210 char opcode_in ATTRIBUTE_UNUSED
, const char * args
)
1214 char prefix
, opcode
;
1216 p
= parse_exp (args
, &op
);
1220 error (_("bad instruction syntax"));
1224 prefix
= opcode
= 0;
1225 if (op
.X_op
== O_register
)
1226 switch (op
.X_add_number
| (op
.X_md
? 0x8000 : 0))
1229 if (TOLOWER (*p
++) == 'a' && TOLOWER (*p
++) == 'f')
1231 /* The scrubber changes '\'' to '`' in this context. */
1238 if (TOLOWER (*p
++) == 'h' && TOLOWER (*p
++) == 'l')
1242 p
= parse_exp (p
, & op
);
1243 if (op
.X_op
== O_register
1245 && (op
.X_add_number
& ~R_INDEX
) == REG_HL
)
1248 if (R_INDEX
& op
.X_add_number
)
1249 prefix
= (R_IX
& op
.X_add_number
) ? 0xDD : 0xFD;
1254 emit_insn (prefix
, opcode
, p
);
1262 emit_in (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1265 expressionS reg
, port
;
1269 p
= parse_exp (args
, ®
);
1272 error (_("bad intruction syntax"));
1276 p
= parse_exp (p
, &port
);
1278 && reg
.X_op
== O_register
1279 && (reg
.X_add_number
<= 7 || reg
.X_add_number
== REG_F
)
1282 if (port
.X_op
!= O_md1
&& port
.X_op
!= O_register
)
1284 if (REG_A
== reg
.X_add_number
)
1288 emit_byte (&port
, BFD_RELOC_8
);
1295 if (port
.X_add_number
== REG_C
)
1297 if (reg
.X_add_number
== REG_F
)
1298 check_mach (INS_UNDOC
);
1303 *q
= 0x40|((reg
.X_add_number
&7)<<3);
1316 emit_out (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1319 expressionS reg
, port
;
1323 p
= parse_exp (args
, & port
);
1326 error (_("bad intruction syntax"));
1329 p
= parse_exp (p
, ®
);
1331 { ill_op (); return p
; }
1332 /* Allow "out (c), 0" as unportable instruction. */
1333 if (reg
.X_op
== O_constant
&& reg
.X_add_number
== 0)
1335 check_mach (INS_UNPORT
);
1336 reg
.X_op
= O_register
;
1337 reg
.X_add_number
= 6;
1340 || reg
.X_op
!= O_register
1341 || reg
.X_add_number
> 7)
1344 if (port
.X_op
!= O_register
&& port
.X_op
!= O_md1
)
1346 if (REG_A
== reg
.X_add_number
)
1350 emit_byte (&port
, BFD_RELOC_8
);
1357 if (REG_C
== port
.X_add_number
)
1361 *q
= 0x41 | (reg
.X_add_number
<< 3);
1370 emit_rst (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1376 p
= parse_exp (args
, &addr
);
1377 if (addr
.X_op
!= O_constant
)
1379 error ("rst needs constant address");
1383 if (addr
.X_add_number
& ~(7 << 3))
1388 *q
= opcode
+ (addr
.X_add_number
& (7 << 3));
1394 emit_ldxhl (char prefix
, char opcode
, expressionS
*src
, expressionS
*d
)
1402 if (src
->X_op
== O_register
)
1404 if (src
->X_add_number
>7)
1413 *q
= opcode
+ src
->X_add_number
;
1415 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1428 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1429 emit_byte (src
, BFD_RELOC_8
);
1435 emit_ldreg (int dest
, expressionS
* src
)
1442 /* 8 Bit ld group: */
1445 if (src
->X_md
== 0 && src
->X_op
== O_register
&& src
->X_add_number
== REG_A
)
1449 *q
= (dest
== REG_I
) ? 0x47 : 0x4F;
1456 if ((src
->X_md
) && src
->X_op
!= O_register
&& src
->X_op
!= O_md1
)
1465 && src
->X_op
== O_register
1466 && (src
->X_add_number
== REG_BC
|| src
->X_add_number
== REG_DE
))
1469 *q
= 0x0A + ((src
->X_add_number
& 1) << 4);
1474 && src
->X_op
== O_register
1475 && (src
->X_add_number
== REG_R
|| src
->X_add_number
== REG_I
))
1479 *q
= (src
->X_add_number
== REG_I
) ? 0x57 : 0x5F;
1487 emit_sx (0, 0x40 + (dest
<< 3), src
);
1492 if ((src
->X_md
== 0)
1493 && (src
->X_op
== O_register
)
1494 && (src
->X_add_number
& R_INDEX
))
1497 emit_sx (0, 0x40 + (dest
<< 3), src
);
1509 check_mach (INS_UNDOC
);
1510 if (src
-> X_op
== O_register
)
1512 rnum
= src
->X_add_number
;
1513 if ((rnum
& ~R_INDEX
) < 8
1514 && ((rnum
& R_INDEX
) == (dest
& R_INDEX
)
1515 || ( (rnum
& ~R_INDEX
) != REG_H
1516 && (rnum
& ~R_INDEX
) != REG_L
)))
1519 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1520 *q
= 0x40 + ((dest
& 0x07) << 3) + (rnum
& 7);
1528 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1529 *q
= 0x06 + ((dest
& 0x07) << 3);
1530 emit_byte (src
, BFD_RELOC_8
);
1534 /* 16 Bit ld group: */
1537 && src
->X_op
== O_register
1538 && REG_HL
== (src
->X_add_number
&~ R_INDEX
))
1540 q
= frag_more ((src
->X_add_number
& R_INDEX
) ? 2 : 1);
1541 if (src
->X_add_number
& R_INDEX
)
1542 *q
++ = (src
->X_add_number
& R_IX
) ? 0xDD : 0xFD;
1549 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1551 q
= frag_more (src
->X_md
? 2 : 1);
1555 *q
= 0x4B + ((dest
& 3) << 4);
1558 *q
= 0x01 + ((dest
& 3) << 4);
1565 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1567 q
= frag_more ((dest
& R_INDEX
) ? 2 : 1);
1569 * q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1570 *q
= (src
->X_md
) ? 0x2A : 0x21;
1585 emit_ld (char prefix_in ATTRIBUTE_UNUSED
, char opcode_in ATTRIBUTE_UNUSED
,
1588 expressionS dst
, src
;
1591 char prefix
, opcode
;
1593 p
= parse_exp (args
, &dst
);
1595 error (_("bad intruction syntax"));
1596 p
= parse_exp (p
, &src
);
1601 emit_ldxhl ((dst
.X_add_number
& R_IX
) ? 0xDD : 0xFD, 0x70,
1602 &src
, symbol_get_value_expression (dst
.X_add_symbol
));
1608 switch (dst
.X_add_number
)
1612 if (src
.X_md
== 0 && src
.X_op
== O_register
&& src
.X_add_number
== REG_A
)
1615 *q
= 0x02 + ( (dst
.X_add_number
& 1) << 4);
1621 emit_ldxhl (0, 0x70, &src
, NULL
);
1628 emit_ldreg (dst
.X_add_number
, &src
);
1632 if (src
.X_md
!= 0 || src
.X_op
!= O_register
)
1634 prefix
= opcode
= 0;
1635 switch (src
.X_add_number
)
1638 opcode
= 0x32; break;
1639 case REG_BC
: case REG_DE
: case REG_SP
:
1640 prefix
= 0xED; opcode
= 0x43 + ((src
.X_add_number
&3)<<4); break;
1642 opcode
= 0x22; break;
1644 prefix
= 0xDD; opcode
= 0x22; break;
1646 prefix
= 0xFD; opcode
= 0x22; break;
1650 q
= frag_more (prefix
?2:1);
1663 emit_data (int size ATTRIBUTE_UNUSED
)
1670 if (is_it_end_of_statement ())
1672 demand_empty_rest_of_line ();
1675 p
= skip_space (input_line_pointer
);
1679 if (*p
== '\"' || *p
== '\'')
1681 for (quote
= *p
, q
= ++p
, cnt
= 0; *p
&& quote
!= *p
; ++p
, ++cnt
)
1683 u
= frag_more (cnt
);
1686 as_warn (_("unterminated string"));
1688 p
= skip_space (p
+1);
1692 p
= parse_exp (p
, &exp
);
1693 if (exp
.X_op
== O_md1
|| exp
.X_op
== O_register
)
1699 as_warn (_("parentheses ignored"));
1700 emit_byte (&exp
, BFD_RELOC_8
);
1704 while (*p
++ == ',') ;
1705 input_line_pointer
= (char *)(p
-1);
1709 emit_mulub (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1713 p
= skip_space (args
);
1714 if (TOLOWER (*p
++) != 'a' || *p
++ != ',')
1720 reg
= TOLOWER (*p
++);
1727 check_mach (INS_R800
);
1728 if (!*skip_space (p
))
1732 *q
= opcode
+ ((reg
- 'b') << 3);
1743 emit_muluw (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1747 p
= skip_space (args
);
1748 if (TOLOWER (*p
++) != 'h' || TOLOWER (*p
++) != 'l' || *p
++ != ',')
1755 p
= parse_exp (p
, & reg
);
1757 if ((!reg
.X_md
) && reg
.X_op
== O_register
)
1758 switch (reg
.X_add_number
)
1762 check_mach (INS_R800
);
1765 *q
= opcode
+ ((reg
.X_add_number
& 3) << 4);
1774 /* Port specific pseudo ops. */
1775 const pseudo_typeS md_pseudo_table
[] =
1777 { "db" , emit_data
, 1},
1780 { "def24", cons
, 3},
1781 { "def32", cons
, 4},
1782 { "defb", emit_data
, 1},
1783 { "defs", s_space
, 1}, /* Synonym for ds on some assemblers. */
1785 { "ds", s_space
, 1}, /* Fill with bytes rather than words. */
1787 { "psect", obj_coff_section
, 0}, /* TODO: Translate attributes. */
1788 { "set", 0, 0}, /* Real instruction on z80. */
1792 static table_t instab
[] =
1794 { "adc", 0x88, 0x4A, emit_adc
},
1795 { "add", 0x80, 0x09, emit_add
},
1796 { "and", 0x00, 0xA0, emit_s
},
1797 { "bit", 0xCB, 0x40, emit_bit
},
1798 { "call", 0xCD, 0xC4, emit_jpcc
},
1799 { "ccf", 0x00, 0x3F, emit_insn
},
1800 { "cp", 0x00, 0xB8, emit_s
},
1801 { "cpd", 0xED, 0xA9, emit_insn
},
1802 { "cpdr", 0xED, 0xB9, emit_insn
},
1803 { "cpi", 0xED, 0xA1, emit_insn
},
1804 { "cpir", 0xED, 0xB1, emit_insn
},
1805 { "cpl", 0x00, 0x2F, emit_insn
},
1806 { "daa", 0x00, 0x27, emit_insn
},
1807 { "dec", 0x0B, 0x05, emit_incdec
},
1808 { "di", 0x00, 0xF3, emit_insn
},
1809 { "djnz", 0x00, 0x10, emit_jr
},
1810 { "ei", 0x00, 0xFB, emit_insn
},
1811 { "ex", 0x00, 0x00, emit_ex
},
1812 { "exx", 0x00, 0xD9, emit_insn
},
1813 { "halt", 0x00, 0x76, emit_insn
},
1814 { "im", 0xED, 0x46, emit_im
},
1815 { "in", 0x00, 0x00, emit_in
},
1816 { "inc", 0x03, 0x04, emit_incdec
},
1817 { "ind", 0xED, 0xAA, emit_insn
},
1818 { "indr", 0xED, 0xBA, emit_insn
},
1819 { "ini", 0xED, 0xA2, emit_insn
},
1820 { "inir", 0xED, 0xB2, emit_insn
},
1821 { "jp", 0xC3, 0xC2, emit_jpcc
},
1822 { "jr", 0x18, 0x20, emit_jrcc
},
1823 { "ld", 0x00, 0x00, emit_ld
},
1824 { "ldd", 0xED, 0xA8, emit_insn
},
1825 { "lddr", 0xED, 0xB8, emit_insn
},
1826 { "ldi", 0xED, 0xA0, emit_insn
},
1827 { "ldir", 0xED, 0xB0, emit_insn
},
1828 { "mulub", 0xED, 0xC5, emit_mulub
}, /* R800 only. */
1829 { "muluw", 0xED, 0xC3, emit_muluw
}, /* R800 only. */
1830 { "neg", 0xed, 0x44, emit_insn
},
1831 { "nop", 0x00, 0x00, emit_insn
},
1832 { "or", 0x00, 0xB0, emit_s
},
1833 { "otdr", 0xED, 0xBB, emit_insn
},
1834 { "otir", 0xED, 0xB3, emit_insn
},
1835 { "out", 0x00, 0x00, emit_out
},
1836 { "outd", 0xED, 0xAB, emit_insn
},
1837 { "outi", 0xED, 0xA3, emit_insn
},
1838 { "pop", 0x00, 0xC1, emit_pop
},
1839 { "push", 0x00, 0xC5, emit_pop
},
1840 { "res", 0xCB, 0x80, emit_bit
},
1841 { "ret", 0xC9, 0xC0, emit_retcc
},
1842 { "reti", 0xED, 0x4D, emit_insn
},
1843 { "retn", 0xED, 0x45, emit_insn
},
1844 { "rl", 0xCB, 0x10, emit_mr
},
1845 { "rla", 0x00, 0x17, emit_insn
},
1846 { "rlc", 0xCB, 0x00, emit_mr
},
1847 { "rlca", 0x00, 0x07, emit_insn
},
1848 { "rld", 0xED, 0x6F, emit_insn
},
1849 { "rr", 0xCB, 0x18, emit_mr
},
1850 { "rra", 0x00, 0x1F, emit_insn
},
1851 { "rrc", 0xCB, 0x08, emit_mr
},
1852 { "rrca", 0x00, 0x0F, emit_insn
},
1853 { "rrd", 0xED, 0x67, emit_insn
},
1854 { "rst", 0x00, 0xC7, emit_rst
},
1855 { "sbc", 0x98, 0x42, emit_adc
},
1856 { "scf", 0x00, 0x37, emit_insn
},
1857 { "set", 0xCB, 0xC0, emit_bit
},
1858 { "sla", 0xCB, 0x20, emit_mr
},
1859 { "sli", 0xCB, 0x30, emit_mr
},
1860 { "sll", 0xCB, 0x30, emit_mr
},
1861 { "sra", 0xCB, 0x28, emit_mr
},
1862 { "srl", 0xCB, 0x38, emit_mr
},
1863 { "sub", 0x00, 0x90, emit_s
},
1864 { "xor", 0x00, 0xA8, emit_s
},
1868 md_assemble (char* str
)
1876 old_ptr
= input_line_pointer
;
1877 p
= skip_space (str
);
1878 for (i
= 0; (i
< BUFLEN
) && (ISALPHA (*p
));)
1879 buf
[i
++] = TOLOWER (*p
++);
1883 buf
[BUFLEN
-3] = buf
[BUFLEN
-2] = '.'; /* Mark opcode as abbreviated. */
1885 as_bad (_("Unknown instruction '%s'"), buf
);
1887 else if ((*p
) && (!ISSPACE (*p
)))
1888 as_bad (_("syntax error"));
1895 insp
= bsearch (&key
, instab
, ARRAY_SIZE (instab
),
1896 sizeof (instab
[0]), key_cmp
);
1898 as_bad (_("Unknown instruction '%s'"), buf
);
1901 p
= insp
->fp (insp
->prefix
, insp
->opcode
, p
);
1903 if ((!err_flag
) && *p
)
1904 as_bad (_("junk at end of line, first unrecognized character is `%c'"),
1908 input_line_pointer
= old_ptr
;
1912 md_apply_fix (fixS
* fixP
, valueT
* valP
, segT seg ATTRIBUTE_UNUSED
)
1914 long val
= * (long *) valP
;
1915 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1917 switch (fixP
->fx_r_type
)
1919 case BFD_RELOC_8_PCREL
:
1922 fixP
->fx_no_overflow
= 1;
1927 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1928 if (!fixP
->fx_no_overflow
)
1929 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1930 _("relative jump out of range"));
1936 case BFD_RELOC_Z80_DISP8
:
1939 fixP
->fx_no_overflow
= 1;
1944 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1945 if (!fixP
->fx_no_overflow
)
1946 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1947 _("index offset out of range"));
1954 if (val
> 255 || val
< -128)
1955 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, _("overflow"));
1957 fixP
->fx_no_overflow
= 1;
1958 if (fixP
->fx_addsy
== NULL
)
1964 *buf
++ = (val
>> 8);
1965 fixP
->fx_no_overflow
= 1;
1966 if (fixP
->fx_addsy
== NULL
)
1970 case BFD_RELOC_24
: /* Def24 may produce this. */
1972 *buf
++ = (val
>> 8);
1973 *buf
++ = (val
>> 16);
1974 fixP
->fx_no_overflow
= 1;
1975 if (fixP
->fx_addsy
== NULL
)
1979 case BFD_RELOC_32
: /* Def32 and .long may produce this. */
1981 *buf
++ = (val
>> 8);
1982 *buf
++ = (val
>> 16);
1983 *buf
++ = (val
>> 24);
1984 if (fixP
->fx_addsy
== NULL
)
1989 printf (_("md_apply_fix: unknown r_type 0x%x\n"), fixP
->fx_r_type
);
1994 /* GAS will call this to generate a reloc. GAS will pass the
1995 resulting reloc to `bfd_install_relocation'. This currently works
1996 poorly, as `bfd_install_relocation' often does the wrong thing, and
1997 instances of `tc_gen_reloc' have been written to work around the
1998 problems, which in turns makes it difficult to fix
1999 `bfd_install_relocation'. */
2001 /* If while processing a fixup, a reloc really
2002 needs to be created then it is done here. */
2005 tc_gen_reloc (asection
*seg ATTRIBUTE_UNUSED
, fixS
*fixp
)
2009 if (! bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
))
2011 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2012 _("reloc %d not supported by object file format"),
2013 (int) fixp
->fx_r_type
);
2017 reloc
= xmalloc (sizeof (arelent
));
2018 reloc
->sym_ptr_ptr
= xmalloc (sizeof (asymbol
*));
2019 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2020 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2021 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2022 reloc
->addend
= fixp
->fx_offset
;