1 /* tc-z80.c -- Assemble code for the Zilog Z80 and ASCII R800
2 Copyright (C) 2005-2019 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
, const 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-undocumented-instructions -warn-unportable-instructions.\n");
162 static symbolS
* zero
;
169 #define R_STACKABLE (0x80)
170 #define R_ARITH (0x40)
173 #define R_INDEX (R_IX | R_IY)
182 #define REG_F (6 | 8)
186 #define REG_AF (3 | R_STACKABLE)
187 #define REG_BC (0 | R_STACKABLE | R_ARITH)
188 #define REG_DE (1 | R_STACKABLE | R_ARITH)
189 #define REG_HL (2 | R_STACKABLE | R_ARITH)
190 #define REG_IX (REG_HL | R_IX)
191 #define REG_IY (REG_HL | R_IY)
192 #define REG_SP (3 | R_ARITH)
194 static const struct reg_entry regtable
[] =
209 {"ixh",REG_H
| R_IX
},
210 {"ixl",REG_L
| R_IX
},
212 {"iyh",REG_H
| R_IY
},
213 {"iyl",REG_L
| R_IY
},
219 #define BUFLEN 8 /* Large enough for any keyword. */
224 expressionS nul
, reg
;
226 unsigned int i
, j
, k
;
229 reg
.X_op
= O_register
;
231 reg
.X_add_symbol
= reg
.X_op_symbol
= 0;
232 for ( i
= 0 ; i
< ARRAY_SIZE ( regtable
) ; ++i
)
234 reg
.X_add_number
= regtable
[i
].number
;
235 k
= strlen ( regtable
[i
].name
);
239 for ( j
= ( 1<<k
) ; j
; --j
)
241 for ( k
= 0 ; regtable
[i
].name
[k
] ; ++k
)
243 buf
[k
] = ( j
& ( 1<<k
) ) ? TOUPPER ( regtable
[i
].name
[k
] ) : regtable
[i
].name
[k
];
245 symbolS
* psym
= symbol_find_or_make(buf
);
246 S_SET_SEGMENT(psym
, reg_section
);
247 symbol_set_value_expression(psym
, ®
);
251 p
= input_line_pointer
;
252 input_line_pointer
= (char *) "0";
255 input_line_pointer
= p
;
256 zero
= make_expr_symbol (& nul
);
257 /* We do not use relaxation (yet). */
266 if (ins_used
& (INS_UNPORT
| INS_R800
))
267 ins_used
|= INS_UNDOC
;
272 mach_type
= bfd_mach_z80strict
;
274 case INS_Z80
|INS_UNDOC
:
275 mach_type
= bfd_mach_z80
;
277 case INS_Z80
|INS_UNDOC
|INS_UNPORT
:
278 mach_type
= bfd_mach_z80full
;
280 case INS_Z80
|INS_UNDOC
|INS_R800
:
281 mach_type
= bfd_mach_r800
;
287 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, mach_type
);
291 skip_space (const char *s
)
293 while (*s
== ' ' || *s
== '\t')
298 /* A non-zero return-value causes a continue in the
299 function read_a_source_file () in ../read.c. */
301 z80_start_line_hook (void)
306 /* Convert one character constants. */
307 for (p
= input_line_pointer
; *p
&& *p
!= '\n'; ++p
)
312 if (p
[1] != 0 && p
[1] != '\'' && p
[2] == '\'')
314 snprintf (buf
, 4, "%3d", (unsigned char)p
[1]);
322 for (quote
= *p
++; quote
!= *p
&& '\n' != *p
; ++p
)
326 as_bad (_("-- unterminated string"));
327 ignore_rest_of_line ();
333 /* Check for <label>[:] [.](EQU|DEFL) <value>. */
334 if (is_name_beginner (*input_line_pointer
))
337 char c
, *rest
, *line_start
;
340 line_start
= input_line_pointer
;
344 c
= get_symbol_name (&name
);
345 rest
= input_line_pointer
+ 1;
349 if (*rest
== ' ' || *rest
== '\t')
353 if (strncasecmp (rest
, "EQU", 3) == 0)
355 else if (strncasecmp (rest
, "DEFL", 4) == 0)
359 if (len
&& (!ISALPHA(rest
[len
]) ) )
361 /* Handle assignment here. */
362 if (line_start
[-1] == '\n')
364 bump_line_counters ();
367 input_line_pointer
= rest
+ len
- 1;
368 /* Allow redefining with "DEFL" (len == 4), but not with "EQU". */
369 equals (name
, len
== 4);
374 /* Restore line and pointer. */
375 (void) restore_line_pointer (c
);
376 input_line_pointer
= line_start
;
383 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
389 md_atof (int type ATTRIBUTE_UNUSED
, char *litP ATTRIBUTE_UNUSED
,
390 int *sizeP ATTRIBUTE_UNUSED
)
392 return _("floating point numbers are not implemented");
396 md_section_align (segT seg ATTRIBUTE_UNUSED
, valueT size
)
402 md_pcrel_from (fixS
* fixp
)
404 return fixp
->fx_where
+
405 fixp
->fx_frag
->fr_address
+ 1;
408 typedef const char * (asfunc
)(char, char, const char*);
410 typedef struct _table_t
413 unsigned char prefix
;
414 unsigned char opcode
;
418 /* Compares the key for structs that start with a char * to the key. */
420 key_cmp (const void * a
, const void * b
)
422 const char *str_a
, *str_b
;
424 str_a
= *((const char**)a
);
425 str_b
= *((const char**)b
);
426 return strcmp (str_a
, str_b
);
430 const char *key
= buf
;
432 /* Prevent an error on a line from also generating
433 a "junk at end of line" error message. */
434 static char err_flag
;
437 error (const char * message
)
439 as_bad ("%s", message
);
446 error (_("illegal operand"));
450 wrong_mach (int ins_type
)
457 p
= "undocumented instruction";
460 p
= "instruction does not work on R800";
463 p
= "instruction only works R800";
466 p
= 0; /* Not reachable. */
469 if (ins_type
& ins_err
)
472 as_warn ("%s", _(p
));
476 check_mach (int ins_type
)
478 if ((ins_type
& ins_ok
) == 0)
479 wrong_mach (ins_type
);
480 ins_used
|= ins_type
;
483 /* Check whether an expression is indirect. */
485 is_indir (const char *s
)
491 /* Indirection is indicated with parentheses. */
494 for (p
= s
, depth
= 0; *p
&& *p
!= ','; ++p
)
500 for (quote
= *p
++; quote
!= *p
&& *p
!= '\n'; ++p
)
501 if (*p
== '\\' && p
[1])
511 p
= skip_space (p
+ 1);
517 error (_("mismatched parentheses"));
523 error (_("mismatched parentheses"));
528 /* Check whether a symbol involves a register. */
530 contains_register(symbolS
*sym
)
534 expressionS
* ex
= symbol_get_value_expression(sym
);
535 return (O_register
== ex
->X_op
)
536 || (ex
->X_add_symbol
&& contains_register(ex
->X_add_symbol
))
537 || (ex
->X_op_symbol
&& contains_register(ex
->X_op_symbol
));
543 /* Parse general expression, not looking for indexed addressing. */
545 parse_exp_not_indexed (const char *s
, expressionS
*op
)
551 op
->X_md
= indir
= is_indir (p
);
552 input_line_pointer
= (char*) s
;
557 error (_("missing operand"));
560 error (_("bad expression syntax"));
565 return input_line_pointer
;
568 /* Parse expression, change operator to O_md1 for indexed addressing*/
570 parse_exp (const char *s
, expressionS
*op
)
572 const char* res
= parse_exp_not_indexed (s
, op
);
577 if (op
->X_md
&& (O_register
== symbol_get_value_expression(op
->X_add_symbol
)->X_op
))
579 int rnum
= symbol_get_value_expression(op
->X_add_symbol
)->X_add_number
;
580 if ( ((REG_IX
!= rnum
) && (REG_IY
!= rnum
)) || contains_register(op
->X_op_symbol
) )
586 if (O_subtract
== op
->X_op
)
589 minus
.X_op
= O_uminus
;
590 minus
.X_add_number
= 0;
591 minus
.X_add_symbol
= op
->X_op_symbol
;
592 minus
.X_op_symbol
= 0;
593 op
->X_op_symbol
= make_expr_symbol(&minus
);
596 symbol_get_value_expression(op
->X_op_symbol
)->X_add_number
+= op
->X_add_number
;
597 op
->X_add_number
= rnum
;
598 op
->X_add_symbol
= op
->X_op_symbol
;
605 if ( op
->X_md
&& ((REG_IX
== op
->X_add_number
)||(REG_IY
== op
->X_add_number
)) )
607 op
->X_add_symbol
= zero
;
617 /* Condition codes, including some synonyms provided by HiTech zas. */
618 static const struct reg_entry cc_tab
[] =
636 /* Parse condition code. */
638 parse_cc (const char *s
, char * op
)
642 struct reg_entry
* cc_p
;
644 for (i
= 0; i
< BUFLEN
; ++i
)
646 if (!ISALPHA (s
[i
])) /* Condition codes consist of letters only. */
648 buf
[i
] = TOLOWER (s
[i
]);
652 && ((s
[i
] == 0) || (s
[i
] == ',')))
655 cc_p
= bsearch (&key
, cc_tab
, ARRAY_SIZE (cc_tab
),
656 sizeof (cc_tab
[0]), key_cmp
);
673 emit_insn (char prefix
, char opcode
, const char * args
)
688 void z80_cons_fix_new (fragS
*frag_p
, int offset
, int nbytes
, expressionS
*exp
)
690 bfd_reloc_code_real_type r
[4] =
698 if (nbytes
< 1 || nbytes
> 4)
700 as_bad (_("unsupported BFD relocation size %u"), nbytes
);
704 fix_new_exp (frag_p
, offset
, nbytes
, exp
, 0, r
[nbytes
-1]);
709 emit_byte (expressionS
* val
, bfd_reloc_code_real_type r_type
)
715 *p
= val
->X_add_number
;
716 if ( contains_register(val
->X_add_symbol
) || contains_register(val
->X_op_symbol
) )
720 else if ((r_type
== BFD_RELOC_8_PCREL
) && (val
->X_op
== O_constant
))
722 as_bad (_("cannot make a relative jump to an absolute location"));
724 else if (val
->X_op
== O_constant
)
727 hi
= (BFD_RELOC_8
== r_type
) ? 255 : 127;
729 if ((val
->X_add_number
< lo
) || (val
->X_add_number
> hi
))
731 if (r_type
== BFD_RELOC_Z80_DISP8
)
732 as_bad (_("offset too large"));
734 as_warn (_("overflow"));
739 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, 1, val
,
740 (r_type
== BFD_RELOC_8_PCREL
) ? TRUE
: FALSE
, r_type
);
741 /* FIXME : Process constant offsets immediately. */
746 emit_word (expressionS
* val
)
751 if ( (val
->X_op
== O_register
)
752 || (val
->X_op
== O_md1
)
753 || contains_register(val
->X_add_symbol
)
754 || contains_register(val
->X_op_symbol
) )
758 *p
= val
->X_add_number
;
759 p
[1] = (val
->X_add_number
>>8);
760 if (val
->X_op
!= O_constant
)
761 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, 2,
762 val
, FALSE
, BFD_RELOC_16
);
767 emit_mx (char prefix
, char opcode
, int shift
, expressionS
* arg
)
768 /* The operand m may be r, (hl), (ix+d), (iy+d),
769 if 0 == prefix m may also be ixl, ixh, iyl, iyh. */
774 rnum
= arg
->X_add_number
;
790 if ((prefix
== 0) && (rnum
& R_INDEX
))
792 prefix
= (rnum
& R_IX
) ? 0xDD : 0xFD;
793 check_mach (INS_UNDOC
);
802 q
= frag_more (prefix
? 2 : 1);
805 * q
++ = opcode
+ (rnum
<< shift
);
809 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
810 *q
= (prefix
) ? prefix
: (opcode
+ (6 << shift
));
812 expressionS offset
= *arg
;
813 offset
.X_op
= O_symbol
;
814 offset
.X_add_number
= 0;
815 emit_byte (&offset
, BFD_RELOC_Z80_DISP8
);
820 *q
= opcode
+(6<<shift
);
828 /* The operand m may be r, (hl), (ix+d), (iy+d),
829 if 0 = prefix m may also be ixl, ixh, iyl, iyh. */
831 emit_m (char prefix
, char opcode
, const char *args
)
836 p
= parse_exp (args
, &arg_m
);
841 emit_mx (prefix
, opcode
, 0, &arg_m
);
849 /* The operand m may be as above or one of the undocumented
850 combinations (ix+d),r and (iy+d),r (if unportable instructions
854 emit_mr (char prefix
, char opcode
, const char *args
, bfd_boolean unportable
)
856 expressionS arg_m
, arg_r
;
859 p
= parse_exp (args
, & arg_m
);
866 p
= parse_exp (p
+ 1, & arg_r
);
868 if ((arg_r
.X_md
== 0)
869 && (arg_r
.X_op
== O_register
)
870 && (arg_r
.X_add_number
< 8))
871 opcode
+= arg_r
.X_add_number
- 6; /* Emit_mx () will add 6. */
877 check_mach (INS_UNPORT
);
883 check_mach (INS_UNPORT
);
884 emit_mx (prefix
, opcode
, 0, & arg_m
);
893 emit_mr_z80 (char prefix
, char opcode
, const char *args
)
895 return emit_mr (prefix
, opcode
, args
, FALSE
);
899 emit_mr_unport (char prefix
, char opcode
, const char *args
)
901 return emit_mr (prefix
, opcode
, args
, TRUE
);
905 emit_sx (char prefix
, char opcode
, expressionS
* arg_p
)
913 emit_mx (prefix
, opcode
, 0, arg_p
);
920 q
= frag_more (prefix
? 2 : 1);
924 emit_byte (arg_p
, BFD_RELOC_8
);
929 /* The operand s may be r, (hl), (ix+d), (iy+d), n. */
931 emit_s (char prefix
, char opcode
, const char *args
)
936 p
= parse_exp (args
, & arg_s
);
937 emit_sx (prefix
, opcode
, & arg_s
);
942 emit_call (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
945 const char *p
; char *q
;
947 p
= parse_exp_not_indexed (args
, &addr
);
959 /* Operand may be rr, r, (hl), (ix+d), (iy+d). */
961 emit_incdec (char prefix
, char opcode
, const char * args
)
965 const char *p
; char *q
;
967 p
= parse_exp (args
, &operand
);
968 rnum
= operand
.X_add_number
;
970 && (operand
.X_op
== O_register
)
973 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
975 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
976 *q
= prefix
+ ((rnum
& 3) << 4);
980 if ((operand
.X_op
== O_md1
) || (operand
.X_op
== O_register
))
981 emit_mx (0, opcode
, 3, & operand
);
989 emit_jr (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
995 p
= parse_exp_not_indexed (args
, &addr
);
1002 emit_byte (&addr
, BFD_RELOC_8_PCREL
);
1008 emit_jp (char prefix
, char opcode
, const char * args
)
1015 p
= parse_exp_not_indexed (args
, & addr
);
1018 rnum
= addr
.X_add_number
;
1019 if ((O_register
== addr
.X_op
) && (REG_HL
== (rnum
& ~R_INDEX
)))
1021 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
1023 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
1039 emit_im (char prefix
, char opcode
, const char * args
)
1045 p
= parse_exp (args
, & mode
);
1046 if (mode
.X_md
|| (mode
.X_op
!= O_constant
))
1049 switch (mode
.X_add_number
)
1053 ++mode
.X_add_number
;
1058 *q
= opcode
+ 8*mode
.X_add_number
;
1067 emit_pop (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1073 p
= parse_exp (args
, & regp
);
1075 && (regp
.X_op
== O_register
)
1076 && (regp
.X_add_number
& R_STACKABLE
))
1080 rnum
= regp
.X_add_number
;
1084 *q
++ = (rnum
&R_IX
)?0xDD:0xFD;
1088 *q
= opcode
+ ((rnum
& 3) << 4);
1097 emit_retcc (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1102 p
= parse_cc (args
, &cc
);
1108 return p
? p
: args
;
1112 emit_adc (char prefix
, char opcode
, const char * args
)
1119 p
= parse_exp (args
, &term
);
1122 error (_("bad instruction syntax"));
1126 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1129 switch (term
.X_add_number
)
1132 p
= emit_s (0, prefix
, p
);
1135 p
= parse_exp (p
, &term
);
1136 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1138 rnum
= term
.X_add_number
;
1139 if (R_ARITH
== (rnum
& (R_ARITH
| R_INDEX
)))
1143 *q
= opcode
+ ((rnum
& 3) << 4);
1155 emit_add (char prefix
, char opcode
, const char * args
)
1162 p
= parse_exp (args
, &term
);
1165 error (_("bad instruction syntax"));
1169 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1172 switch (term
.X_add_number
& ~R_INDEX
)
1175 p
= emit_s (0, prefix
, p
);
1178 lhs
= term
.X_add_number
;
1179 p
= parse_exp (p
, &term
);
1180 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1182 rhs
= term
.X_add_number
;
1184 && ((rhs
== lhs
) || ((rhs
& ~R_INDEX
) != REG_HL
)))
1186 q
= frag_more ((lhs
& R_INDEX
) ? 2 : 1);
1188 *q
++ = (lhs
& R_IX
) ? 0xDD : 0xFD;
1189 *q
= opcode
+ ((rhs
& 3) << 4);
1201 emit_bit (char prefix
, char opcode
, const char * args
)
1207 p
= parse_exp (args
, &b
);
1209 error (_("bad instruction syntax"));
1211 bn
= b
.X_add_number
;
1213 && (b
.X_op
== O_constant
)
1218 /* Bit : no optional third operand. */
1219 p
= emit_m (prefix
, opcode
+ (bn
<< 3), p
);
1221 /* Set, res : resulting byte can be copied to register. */
1222 p
= emit_mr (prefix
, opcode
+ (bn
<< 3), p
, FALSE
);
1230 emit_jpcc (char prefix
, char opcode
, const char * args
)
1235 p
= parse_cc (args
, & cc
);
1236 if (p
&& *p
++ == ',')
1237 p
= emit_call (0, opcode
+ cc
, p
);
1239 p
= (prefix
== (char)0xC3)
1240 ? emit_jp (0xE9, prefix
, args
)
1241 : emit_call (0, prefix
, args
);
1246 emit_jrcc (char prefix
, char opcode
, const char * args
)
1251 p
= parse_cc (args
, &cc
);
1252 if (p
&& *p
++ == ',')
1255 error (_("condition code invalid for jr"));
1257 p
= emit_jr (0, opcode
+ cc
, p
);
1260 p
= emit_jr (0, prefix
, args
);
1266 emit_ex (char prefix_in ATTRIBUTE_UNUSED
,
1267 char opcode_in ATTRIBUTE_UNUSED
, const char * args
)
1271 char prefix
, opcode
;
1273 p
= parse_exp_not_indexed (args
, &op
);
1277 error (_("bad instruction syntax"));
1281 prefix
= opcode
= 0;
1282 if (op
.X_op
== O_register
)
1283 switch (op
.X_add_number
| (op
.X_md
? 0x8000 : 0))
1286 if (TOLOWER (*p
++) == 'a' && TOLOWER (*p
++) == 'f')
1288 /* The scrubber changes '\'' to '`' in this context. */
1295 if (TOLOWER (*p
++) == 'h' && TOLOWER (*p
++) == 'l')
1299 p
= parse_exp (p
, & op
);
1300 if (op
.X_op
== O_register
1302 && (op
.X_add_number
& ~R_INDEX
) == REG_HL
)
1305 if (R_INDEX
& op
.X_add_number
)
1306 prefix
= (R_IX
& op
.X_add_number
) ? 0xDD : 0xFD;
1311 emit_insn (prefix
, opcode
, p
);
1319 emit_in (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1322 expressionS reg
, port
;
1326 p
= parse_exp (args
, ®
);
1329 error (_("bad instruction syntax"));
1333 p
= parse_exp (p
, &port
);
1335 && reg
.X_op
== O_register
1336 && (reg
.X_add_number
<= 7 || reg
.X_add_number
== REG_F
)
1339 if (port
.X_op
!= O_md1
&& port
.X_op
!= O_register
)
1341 if (REG_A
== reg
.X_add_number
)
1345 emit_byte (&port
, BFD_RELOC_8
);
1352 if (port
.X_add_number
== REG_C
)
1354 if (reg
.X_add_number
== REG_F
)
1355 check_mach (INS_UNDOC
);
1360 *q
= 0x40|((reg
.X_add_number
&7)<<3);
1373 emit_out (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1376 expressionS reg
, port
;
1380 p
= parse_exp (args
, & port
);
1383 error (_("bad instruction syntax"));
1386 p
= parse_exp (p
, ®
);
1388 { ill_op (); return p
; }
1389 /* Allow "out (c), 0" as unportable instruction. */
1390 if (reg
.X_op
== O_constant
&& reg
.X_add_number
== 0)
1392 check_mach (INS_UNPORT
);
1393 reg
.X_op
= O_register
;
1394 reg
.X_add_number
= 6;
1397 || reg
.X_op
!= O_register
1398 || reg
.X_add_number
> 7)
1401 if (port
.X_op
!= O_register
&& port
.X_op
!= O_md1
)
1403 if (REG_A
== reg
.X_add_number
)
1407 emit_byte (&port
, BFD_RELOC_8
);
1414 if (REG_C
== port
.X_add_number
)
1418 *q
= 0x41 | (reg
.X_add_number
<< 3);
1427 emit_rst (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1433 p
= parse_exp_not_indexed (args
, &addr
);
1434 if (addr
.X_op
!= O_constant
)
1436 error ("rst needs constant address");
1440 if (addr
.X_add_number
& ~(7 << 3))
1445 *q
= opcode
+ (addr
.X_add_number
& (7 << 3));
1451 emit_ldxhl (char prefix
, char opcode
, expressionS
*src
, expressionS
*d
)
1459 if (src
->X_op
== O_register
)
1461 if (src
->X_add_number
>7)
1470 *q
= opcode
+ src
->X_add_number
;
1472 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1485 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1486 emit_byte (src
, BFD_RELOC_8
);
1492 emit_ldreg (int dest
, expressionS
* src
)
1499 /* 8 Bit ld group: */
1502 if (src
->X_md
== 0 && src
->X_op
== O_register
&& src
->X_add_number
== REG_A
)
1506 *q
= (dest
== REG_I
) ? 0x47 : 0x4F;
1513 if ((src
->X_md
) && src
->X_op
!= O_register
&& src
->X_op
!= O_md1
)
1522 && src
->X_op
== O_register
1523 && (src
->X_add_number
== REG_BC
|| src
->X_add_number
== REG_DE
))
1526 *q
= 0x0A + ((src
->X_add_number
& 1) << 4);
1531 && src
->X_op
== O_register
1532 && (src
->X_add_number
== REG_R
|| src
->X_add_number
== REG_I
))
1536 *q
= (src
->X_add_number
== REG_I
) ? 0x57 : 0x5F;
1544 emit_sx (0, 0x40 + (dest
<< 3), src
);
1549 if ((src
->X_md
== 0)
1550 && (src
->X_op
== O_register
)
1551 && (src
->X_add_number
& R_INDEX
))
1554 emit_sx (0, 0x40 + (dest
<< 3), src
);
1566 check_mach (INS_UNDOC
);
1567 if (src
-> X_op
== O_register
)
1569 rnum
= src
->X_add_number
;
1570 if ((rnum
& ~R_INDEX
) < 8
1571 && ((rnum
& R_INDEX
) == (dest
& R_INDEX
)
1572 || ( (rnum
& ~R_INDEX
) != REG_H
1573 && (rnum
& ~R_INDEX
) != REG_L
)))
1576 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1577 *q
= 0x40 + ((dest
& 0x07) << 3) + (rnum
& 7);
1585 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1586 *q
= 0x06 + ((dest
& 0x07) << 3);
1587 emit_byte (src
, BFD_RELOC_8
);
1591 /* 16 Bit ld group: */
1594 && src
->X_op
== O_register
1595 && REG_HL
== (src
->X_add_number
&~ R_INDEX
))
1597 q
= frag_more ((src
->X_add_number
& R_INDEX
) ? 2 : 1);
1598 if (src
->X_add_number
& R_INDEX
)
1599 *q
++ = (src
->X_add_number
& R_IX
) ? 0xDD : 0xFD;
1606 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1608 q
= frag_more (src
->X_md
? 2 : 1);
1612 *q
= 0x4B + ((dest
& 3) << 4);
1615 *q
= 0x01 + ((dest
& 3) << 4);
1622 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1624 q
= frag_more ((dest
& R_INDEX
) ? 2 : 1);
1626 * q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1627 *q
= (src
->X_md
) ? 0x2A : 0x21;
1642 emit_ld (char prefix_in ATTRIBUTE_UNUSED
, char opcode_in ATTRIBUTE_UNUSED
,
1645 expressionS dst
, src
;
1648 char prefix
, opcode
;
1650 p
= parse_exp (args
, &dst
);
1652 error (_("bad instruction syntax"));
1653 p
= parse_exp (p
, &src
);
1659 expressionS dst_offset
= dst
;
1660 dst_offset
.X_op
= O_symbol
;
1661 dst_offset
.X_add_number
= 0;
1662 emit_ldxhl ((dst
.X_add_number
& R_IX
) ? 0xDD : 0xFD, 0x70,
1670 switch (dst
.X_add_number
)
1674 if (src
.X_md
== 0 && src
.X_op
== O_register
&& src
.X_add_number
== REG_A
)
1677 *q
= 0x02 + ( (dst
.X_add_number
& 1) << 4);
1683 emit_ldxhl (0, 0x70, &src
, NULL
);
1690 emit_ldreg (dst
.X_add_number
, &src
);
1694 if (src
.X_md
!= 0 || src
.X_op
!= O_register
)
1696 prefix
= opcode
= 0;
1697 switch (src
.X_add_number
)
1700 opcode
= 0x32; break;
1701 case REG_BC
: case REG_DE
: case REG_SP
:
1702 prefix
= 0xED; opcode
= 0x43 + ((src
.X_add_number
&3)<<4); break;
1704 opcode
= 0x22; break;
1706 prefix
= 0xDD; opcode
= 0x22; break;
1708 prefix
= 0xFD; opcode
= 0x22; break;
1712 q
= frag_more (prefix
?2:1);
1725 emit_data (int size ATTRIBUTE_UNUSED
)
1732 if (is_it_end_of_statement ())
1734 demand_empty_rest_of_line ();
1737 p
= skip_space (input_line_pointer
);
1741 if (*p
== '\"' || *p
== '\'')
1743 for (quote
= *p
, q
= ++p
, cnt
= 0; *p
&& quote
!= *p
; ++p
, ++cnt
)
1745 u
= frag_more (cnt
);
1748 as_warn (_("unterminated string"));
1750 p
= skip_space (p
+1);
1754 p
= parse_exp (p
, &exp
);
1755 if (exp
.X_op
== O_md1
|| exp
.X_op
== O_register
)
1761 as_warn (_("parentheses ignored"));
1762 emit_byte (&exp
, BFD_RELOC_8
);
1766 while (*p
++ == ',') ;
1767 input_line_pointer
= (char *)(p
-1);
1771 emit_mulub (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1775 p
= skip_space (args
);
1776 if (TOLOWER (*p
++) != 'a' || *p
++ != ',')
1782 reg
= TOLOWER (*p
++);
1789 check_mach (INS_R800
);
1790 if (!*skip_space (p
))
1794 *q
= opcode
+ ((reg
- 'b') << 3);
1806 emit_muluw (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1810 p
= skip_space (args
);
1811 if (TOLOWER (*p
++) != 'h' || TOLOWER (*p
++) != 'l' || *p
++ != ',')
1818 p
= parse_exp (p
, & reg
);
1820 if ((!reg
.X_md
) && reg
.X_op
== O_register
)
1821 switch (reg
.X_add_number
)
1825 check_mach (INS_R800
);
1828 *q
= opcode
+ ((reg
.X_add_number
& 3) << 4);
1837 /* Port specific pseudo ops. */
1838 const pseudo_typeS md_pseudo_table
[] =
1840 { "db" , emit_data
, 1},
1843 { "def24", cons
, 3},
1844 { "def32", cons
, 4},
1845 { "defb", emit_data
, 1},
1846 { "defs", s_space
, 1}, /* Synonym for ds on some assemblers. */
1848 { "ds", s_space
, 1}, /* Fill with bytes rather than words. */
1850 { "psect", obj_coff_section
, 0}, /* TODO: Translate attributes. */
1851 { "set", 0, 0}, /* Real instruction on z80. */
1855 static table_t instab
[] =
1857 { "adc", 0x88, 0x4A, emit_adc
},
1858 { "add", 0x80, 0x09, emit_add
},
1859 { "and", 0x00, 0xA0, emit_s
},
1860 { "bit", 0xCB, 0x40, emit_bit
},
1861 { "call", 0xCD, 0xC4, emit_jpcc
},
1862 { "ccf", 0x00, 0x3F, emit_insn
},
1863 { "cp", 0x00, 0xB8, emit_s
},
1864 { "cpd", 0xED, 0xA9, emit_insn
},
1865 { "cpdr", 0xED, 0xB9, emit_insn
},
1866 { "cpi", 0xED, 0xA1, emit_insn
},
1867 { "cpir", 0xED, 0xB1, emit_insn
},
1868 { "cpl", 0x00, 0x2F, emit_insn
},
1869 { "daa", 0x00, 0x27, emit_insn
},
1870 { "dec", 0x0B, 0x05, emit_incdec
},
1871 { "di", 0x00, 0xF3, emit_insn
},
1872 { "djnz", 0x00, 0x10, emit_jr
},
1873 { "ei", 0x00, 0xFB, emit_insn
},
1874 { "ex", 0x00, 0x00, emit_ex
},
1875 { "exx", 0x00, 0xD9, emit_insn
},
1876 { "halt", 0x00, 0x76, emit_insn
},
1877 { "im", 0xED, 0x46, emit_im
},
1878 { "in", 0x00, 0x00, emit_in
},
1879 { "inc", 0x03, 0x04, emit_incdec
},
1880 { "ind", 0xED, 0xAA, emit_insn
},
1881 { "indr", 0xED, 0xBA, emit_insn
},
1882 { "ini", 0xED, 0xA2, emit_insn
},
1883 { "inir", 0xED, 0xB2, emit_insn
},
1884 { "jp", 0xC3, 0xC2, emit_jpcc
},
1885 { "jr", 0x18, 0x20, emit_jrcc
},
1886 { "ld", 0x00, 0x00, emit_ld
},
1887 { "ldd", 0xED, 0xA8, emit_insn
},
1888 { "lddr", 0xED, 0xB8, emit_insn
},
1889 { "ldi", 0xED, 0xA0, emit_insn
},
1890 { "ldir", 0xED, 0xB0, emit_insn
},
1891 { "mulub", 0xED, 0xC5, emit_mulub
}, /* R800 only. */
1892 { "muluw", 0xED, 0xC3, emit_muluw
}, /* R800 only. */
1893 { "neg", 0xed, 0x44, emit_insn
},
1894 { "nop", 0x00, 0x00, emit_insn
},
1895 { "or", 0x00, 0xB0, emit_s
},
1896 { "otdr", 0xED, 0xBB, emit_insn
},
1897 { "otir", 0xED, 0xB3, emit_insn
},
1898 { "out", 0x00, 0x00, emit_out
},
1899 { "outd", 0xED, 0xAB, emit_insn
},
1900 { "outi", 0xED, 0xA3, emit_insn
},
1901 { "pop", 0x00, 0xC1, emit_pop
},
1902 { "push", 0x00, 0xC5, emit_pop
},
1903 { "res", 0xCB, 0x80, emit_bit
},
1904 { "ret", 0xC9, 0xC0, emit_retcc
},
1905 { "reti", 0xED, 0x4D, emit_insn
},
1906 { "retn", 0xED, 0x45, emit_insn
},
1907 { "rl", 0xCB, 0x10, emit_mr_z80
},
1908 { "rla", 0x00, 0x17, emit_insn
},
1909 { "rlc", 0xCB, 0x00, emit_mr_z80
},
1910 { "rlca", 0x00, 0x07, emit_insn
},
1911 { "rld", 0xED, 0x6F, emit_insn
},
1912 { "rr", 0xCB, 0x18, emit_mr_z80
},
1913 { "rra", 0x00, 0x1F, emit_insn
},
1914 { "rrc", 0xCB, 0x08, emit_mr_z80
},
1915 { "rrca", 0x00, 0x0F, emit_insn
},
1916 { "rrd", 0xED, 0x67, emit_insn
},
1917 { "rst", 0x00, 0xC7, emit_rst
},
1918 { "sbc", 0x98, 0x42, emit_adc
},
1919 { "scf", 0x00, 0x37, emit_insn
},
1920 { "set", 0xCB, 0xC0, emit_bit
},
1921 { "sla", 0xCB, 0x20, emit_mr_z80
},
1922 { "sli", 0xCB, 0x30, emit_mr_unport
},
1923 { "sll", 0xCB, 0x30, emit_mr_unport
},
1924 { "sra", 0xCB, 0x28, emit_mr_z80
},
1925 { "srl", 0xCB, 0x38, emit_mr_z80
},
1926 { "sub", 0x00, 0x90, emit_s
},
1927 { "xor", 0x00, 0xA8, emit_s
},
1931 md_assemble (char *str
)
1939 old_ptr
= input_line_pointer
;
1940 p
= skip_space (str
);
1941 for (i
= 0; (i
< BUFLEN
) && (ISALPHA (*p
));)
1942 buf
[i
++] = TOLOWER (*p
++);
1946 buf
[BUFLEN
-3] = buf
[BUFLEN
-2] = '.'; /* Mark opcode as abbreviated. */
1948 as_bad (_("Unknown instruction '%s'"), buf
);
1950 else if ((*p
) && (!ISSPACE (*p
)))
1951 as_bad (_("syntax error"));
1958 insp
= bsearch (&key
, instab
, ARRAY_SIZE (instab
),
1959 sizeof (instab
[0]), key_cmp
);
1961 as_bad (_("Unknown instruction '%s'"), buf
);
1964 p
= insp
->fp (insp
->prefix
, insp
->opcode
, p
);
1966 if ((!err_flag
) && *p
)
1967 as_bad (_("junk at end of line, first unrecognized character is `%c'"),
1971 input_line_pointer
= old_ptr
;
1975 md_apply_fix (fixS
* fixP
, valueT
* valP
, segT seg ATTRIBUTE_UNUSED
)
1977 long val
= * (long *) valP
;
1978 char *p_lit
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1980 switch (fixP
->fx_r_type
)
1982 case BFD_RELOC_8_PCREL
:
1985 fixP
->fx_no_overflow
= 1;
1990 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1991 if (!fixP
->fx_no_overflow
)
1992 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1993 _("relative jump out of range"));
1999 case BFD_RELOC_Z80_DISP8
:
2002 fixP
->fx_no_overflow
= 1;
2007 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
2008 if (!fixP
->fx_no_overflow
)
2009 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2010 _("index offset out of range"));
2017 if (val
> 255 || val
< -128)
2018 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, _("overflow"));
2020 fixP
->fx_no_overflow
= 1;
2021 if (fixP
->fx_addsy
== NULL
)
2027 *p_lit
++ = (val
>> 8);
2028 fixP
->fx_no_overflow
= 1;
2029 if (fixP
->fx_addsy
== NULL
)
2033 case BFD_RELOC_24
: /* Def24 may produce this. */
2035 *p_lit
++ = (val
>> 8);
2036 *p_lit
++ = (val
>> 16);
2037 fixP
->fx_no_overflow
= 1;
2038 if (fixP
->fx_addsy
== NULL
)
2042 case BFD_RELOC_32
: /* Def32 and .long may produce this. */
2044 *p_lit
++ = (val
>> 8);
2045 *p_lit
++ = (val
>> 16);
2046 *p_lit
++ = (val
>> 24);
2047 if (fixP
->fx_addsy
== NULL
)
2052 printf (_("md_apply_fix: unknown r_type 0x%x\n"), fixP
->fx_r_type
);
2057 /* GAS will call this to generate a reloc. GAS will pass the
2058 resulting reloc to `bfd_install_relocation'. This currently works
2059 poorly, as `bfd_install_relocation' often does the wrong thing, and
2060 instances of `tc_gen_reloc' have been written to work around the
2061 problems, which in turns makes it difficult to fix
2062 `bfd_install_relocation'. */
2064 /* If while processing a fixup, a reloc really
2065 needs to be created then it is done here. */
2068 tc_gen_reloc (asection
*seg ATTRIBUTE_UNUSED
, fixS
*fixp
)
2072 if (! bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
))
2074 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2075 _("reloc %d not supported by object file format"),
2076 (int) fixp
->fx_r_type
);
2080 reloc
= XNEW (arelent
);
2081 reloc
->sym_ptr_ptr
= XNEW (asymbol
*);
2082 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2083 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2084 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2085 reloc
->addend
= fixp
->fx_offset
;