1 /* tc-z80.c -- Assemble code for the Zilog Z80 and ASCII R800
2 Copyright (C) 2005-2017 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
853 emit_mr (char prefix
, char opcode
, const char *args
)
855 expressionS arg_m
, arg_r
;
858 p
= parse_exp (args
, & arg_m
);
865 p
= parse_exp (p
+ 1, & arg_r
);
867 if ((arg_r
.X_md
== 0)
868 && (arg_r
.X_op
== O_register
)
869 && (arg_r
.X_add_number
< 8))
870 opcode
+= arg_r
.X_add_number
-6; /* Emit_mx () will add 6. */
876 check_mach (INS_UNPORT
);
880 emit_mx (prefix
, opcode
, 0, & arg_m
);
889 emit_sx (char prefix
, char opcode
, expressionS
* arg_p
)
897 emit_mx (prefix
, opcode
, 0, arg_p
);
904 q
= frag_more (prefix
? 2 : 1);
908 emit_byte (arg_p
, BFD_RELOC_8
);
913 /* The operand s may be r, (hl), (ix+d), (iy+d), n. */
915 emit_s (char prefix
, char opcode
, const char *args
)
920 p
= parse_exp (args
, & arg_s
);
921 emit_sx (prefix
, opcode
, & arg_s
);
926 emit_call (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
929 const char *p
; char *q
;
931 p
= parse_exp_not_indexed (args
, &addr
);
943 /* Operand may be rr, r, (hl), (ix+d), (iy+d). */
945 emit_incdec (char prefix
, char opcode
, const char * args
)
949 const char *p
; char *q
;
951 p
= parse_exp (args
, &operand
);
952 rnum
= operand
.X_add_number
;
954 && (operand
.X_op
== O_register
)
957 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
959 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
960 *q
= prefix
+ ((rnum
& 3) << 4);
964 if ((operand
.X_op
== O_md1
) || (operand
.X_op
== O_register
))
965 emit_mx (0, opcode
, 3, & operand
);
973 emit_jr (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
979 p
= parse_exp_not_indexed (args
, &addr
);
986 emit_byte (&addr
, BFD_RELOC_8_PCREL
);
992 emit_jp (char prefix
, char opcode
, const char * args
)
999 p
= parse_exp_not_indexed (args
, & addr
);
1002 rnum
= addr
.X_add_number
;
1003 if ((O_register
== addr
.X_op
) && (REG_HL
== (rnum
& ~R_INDEX
)))
1005 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
1007 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
1023 emit_im (char prefix
, char opcode
, const char * args
)
1029 p
= parse_exp (args
, & mode
);
1030 if (mode
.X_md
|| (mode
.X_op
!= O_constant
))
1033 switch (mode
.X_add_number
)
1037 ++mode
.X_add_number
;
1042 *q
= opcode
+ 8*mode
.X_add_number
;
1051 emit_pop (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1057 p
= parse_exp (args
, & regp
);
1059 && (regp
.X_op
== O_register
)
1060 && (regp
.X_add_number
& R_STACKABLE
))
1064 rnum
= regp
.X_add_number
;
1068 *q
++ = (rnum
&R_IX
)?0xDD:0xFD;
1072 *q
= opcode
+ ((rnum
& 3) << 4);
1081 emit_retcc (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1086 p
= parse_cc (args
, &cc
);
1092 return p
? p
: args
;
1096 emit_adc (char prefix
, char opcode
, const char * args
)
1103 p
= parse_exp (args
, &term
);
1106 error (_("bad instruction syntax"));
1110 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1113 switch (term
.X_add_number
)
1116 p
= emit_s (0, prefix
, p
);
1119 p
= parse_exp (p
, &term
);
1120 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1122 rnum
= term
.X_add_number
;
1123 if (R_ARITH
== (rnum
& (R_ARITH
| R_INDEX
)))
1127 *q
= opcode
+ ((rnum
& 3) << 4);
1139 emit_add (char prefix
, char opcode
, const char * args
)
1146 p
= parse_exp (args
, &term
);
1149 error (_("bad instruction syntax"));
1153 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1156 switch (term
.X_add_number
& ~R_INDEX
)
1159 p
= emit_s (0, prefix
, p
);
1162 lhs
= term
.X_add_number
;
1163 p
= parse_exp (p
, &term
);
1164 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1166 rhs
= term
.X_add_number
;
1168 && ((rhs
== lhs
) || ((rhs
& ~R_INDEX
) != REG_HL
)))
1170 q
= frag_more ((lhs
& R_INDEX
) ? 2 : 1);
1172 *q
++ = (lhs
& R_IX
) ? 0xDD : 0xFD;
1173 *q
= opcode
+ ((rhs
& 3) << 4);
1185 emit_bit (char prefix
, char opcode
, const char * args
)
1191 p
= parse_exp (args
, &b
);
1193 error (_("bad instruction syntax"));
1195 bn
= b
.X_add_number
;
1197 && (b
.X_op
== O_constant
)
1202 /* Bit : no optional third operand. */
1203 p
= emit_m (prefix
, opcode
+ (bn
<< 3), p
);
1205 /* Set, res : resulting byte can be copied to register. */
1206 p
= emit_mr (prefix
, opcode
+ (bn
<< 3), p
);
1214 emit_jpcc (char prefix
, char opcode
, const char * args
)
1219 p
= parse_cc (args
, & cc
);
1220 if (p
&& *p
++ == ',')
1221 p
= emit_call (0, opcode
+ cc
, p
);
1223 p
= (prefix
== (char)0xC3)
1224 ? emit_jp (0xE9, prefix
, args
)
1225 : emit_call (0, prefix
, args
);
1230 emit_jrcc (char prefix
, char opcode
, const char * args
)
1235 p
= parse_cc (args
, &cc
);
1236 if (p
&& *p
++ == ',')
1239 error (_("condition code invalid for jr"));
1241 p
= emit_jr (0, opcode
+ cc
, p
);
1244 p
= emit_jr (0, prefix
, args
);
1250 emit_ex (char prefix_in ATTRIBUTE_UNUSED
,
1251 char opcode_in ATTRIBUTE_UNUSED
, const char * args
)
1255 char prefix
, opcode
;
1257 p
= parse_exp_not_indexed (args
, &op
);
1261 error (_("bad instruction syntax"));
1265 prefix
= opcode
= 0;
1266 if (op
.X_op
== O_register
)
1267 switch (op
.X_add_number
| (op
.X_md
? 0x8000 : 0))
1270 if (TOLOWER (*p
++) == 'a' && TOLOWER (*p
++) == 'f')
1272 /* The scrubber changes '\'' to '`' in this context. */
1279 if (TOLOWER (*p
++) == 'h' && TOLOWER (*p
++) == 'l')
1283 p
= parse_exp (p
, & op
);
1284 if (op
.X_op
== O_register
1286 && (op
.X_add_number
& ~R_INDEX
) == REG_HL
)
1289 if (R_INDEX
& op
.X_add_number
)
1290 prefix
= (R_IX
& op
.X_add_number
) ? 0xDD : 0xFD;
1295 emit_insn (prefix
, opcode
, p
);
1303 emit_in (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1306 expressionS reg
, port
;
1310 p
= parse_exp (args
, ®
);
1313 error (_("bad instruction syntax"));
1317 p
= parse_exp (p
, &port
);
1319 && reg
.X_op
== O_register
1320 && (reg
.X_add_number
<= 7 || reg
.X_add_number
== REG_F
)
1323 if (port
.X_op
!= O_md1
&& port
.X_op
!= O_register
)
1325 if (REG_A
== reg
.X_add_number
)
1329 emit_byte (&port
, BFD_RELOC_8
);
1336 if (port
.X_add_number
== REG_C
)
1338 if (reg
.X_add_number
== REG_F
)
1339 check_mach (INS_UNDOC
);
1344 *q
= 0x40|((reg
.X_add_number
&7)<<3);
1357 emit_out (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1360 expressionS reg
, port
;
1364 p
= parse_exp (args
, & port
);
1367 error (_("bad instruction syntax"));
1370 p
= parse_exp (p
, ®
);
1372 { ill_op (); return p
; }
1373 /* Allow "out (c), 0" as unportable instruction. */
1374 if (reg
.X_op
== O_constant
&& reg
.X_add_number
== 0)
1376 check_mach (INS_UNPORT
);
1377 reg
.X_op
= O_register
;
1378 reg
.X_add_number
= 6;
1381 || reg
.X_op
!= O_register
1382 || reg
.X_add_number
> 7)
1385 if (port
.X_op
!= O_register
&& port
.X_op
!= O_md1
)
1387 if (REG_A
== reg
.X_add_number
)
1391 emit_byte (&port
, BFD_RELOC_8
);
1398 if (REG_C
== port
.X_add_number
)
1402 *q
= 0x41 | (reg
.X_add_number
<< 3);
1411 emit_rst (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1417 p
= parse_exp_not_indexed (args
, &addr
);
1418 if (addr
.X_op
!= O_constant
)
1420 error ("rst needs constant address");
1424 if (addr
.X_add_number
& ~(7 << 3))
1429 *q
= opcode
+ (addr
.X_add_number
& (7 << 3));
1435 emit_ldxhl (char prefix
, char opcode
, expressionS
*src
, expressionS
*d
)
1443 if (src
->X_op
== O_register
)
1445 if (src
->X_add_number
>7)
1454 *q
= opcode
+ src
->X_add_number
;
1456 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1469 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1470 emit_byte (src
, BFD_RELOC_8
);
1476 emit_ldreg (int dest
, expressionS
* src
)
1483 /* 8 Bit ld group: */
1486 if (src
->X_md
== 0 && src
->X_op
== O_register
&& src
->X_add_number
== REG_A
)
1490 *q
= (dest
== REG_I
) ? 0x47 : 0x4F;
1497 if ((src
->X_md
) && src
->X_op
!= O_register
&& src
->X_op
!= O_md1
)
1506 && src
->X_op
== O_register
1507 && (src
->X_add_number
== REG_BC
|| src
->X_add_number
== REG_DE
))
1510 *q
= 0x0A + ((src
->X_add_number
& 1) << 4);
1515 && src
->X_op
== O_register
1516 && (src
->X_add_number
== REG_R
|| src
->X_add_number
== REG_I
))
1520 *q
= (src
->X_add_number
== REG_I
) ? 0x57 : 0x5F;
1528 emit_sx (0, 0x40 + (dest
<< 3), src
);
1533 if ((src
->X_md
== 0)
1534 && (src
->X_op
== O_register
)
1535 && (src
->X_add_number
& R_INDEX
))
1538 emit_sx (0, 0x40 + (dest
<< 3), src
);
1550 check_mach (INS_UNDOC
);
1551 if (src
-> X_op
== O_register
)
1553 rnum
= src
->X_add_number
;
1554 if ((rnum
& ~R_INDEX
) < 8
1555 && ((rnum
& R_INDEX
) == (dest
& R_INDEX
)
1556 || ( (rnum
& ~R_INDEX
) != REG_H
1557 && (rnum
& ~R_INDEX
) != REG_L
)))
1560 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1561 *q
= 0x40 + ((dest
& 0x07) << 3) + (rnum
& 7);
1569 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1570 *q
= 0x06 + ((dest
& 0x07) << 3);
1571 emit_byte (src
, BFD_RELOC_8
);
1575 /* 16 Bit ld group: */
1578 && src
->X_op
== O_register
1579 && REG_HL
== (src
->X_add_number
&~ R_INDEX
))
1581 q
= frag_more ((src
->X_add_number
& R_INDEX
) ? 2 : 1);
1582 if (src
->X_add_number
& R_INDEX
)
1583 *q
++ = (src
->X_add_number
& R_IX
) ? 0xDD : 0xFD;
1590 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1592 q
= frag_more (src
->X_md
? 2 : 1);
1596 *q
= 0x4B + ((dest
& 3) << 4);
1599 *q
= 0x01 + ((dest
& 3) << 4);
1606 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1608 q
= frag_more ((dest
& R_INDEX
) ? 2 : 1);
1610 * q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1611 *q
= (src
->X_md
) ? 0x2A : 0x21;
1626 emit_ld (char prefix_in ATTRIBUTE_UNUSED
, char opcode_in ATTRIBUTE_UNUSED
,
1629 expressionS dst
, src
;
1632 char prefix
, opcode
;
1634 p
= parse_exp (args
, &dst
);
1636 error (_("bad instruction syntax"));
1637 p
= parse_exp (p
, &src
);
1643 expressionS dst_offset
= dst
;
1644 dst_offset
.X_op
= O_symbol
;
1645 dst_offset
.X_add_number
= 0;
1646 emit_ldxhl ((dst
.X_add_number
& R_IX
) ? 0xDD : 0xFD, 0x70,
1654 switch (dst
.X_add_number
)
1658 if (src
.X_md
== 0 && src
.X_op
== O_register
&& src
.X_add_number
== REG_A
)
1661 *q
= 0x02 + ( (dst
.X_add_number
& 1) << 4);
1667 emit_ldxhl (0, 0x70, &src
, NULL
);
1674 emit_ldreg (dst
.X_add_number
, &src
);
1678 if (src
.X_md
!= 0 || src
.X_op
!= O_register
)
1680 prefix
= opcode
= 0;
1681 switch (src
.X_add_number
)
1684 opcode
= 0x32; break;
1685 case REG_BC
: case REG_DE
: case REG_SP
:
1686 prefix
= 0xED; opcode
= 0x43 + ((src
.X_add_number
&3)<<4); break;
1688 opcode
= 0x22; break;
1690 prefix
= 0xDD; opcode
= 0x22; break;
1692 prefix
= 0xFD; opcode
= 0x22; break;
1696 q
= frag_more (prefix
?2:1);
1709 emit_data (int size ATTRIBUTE_UNUSED
)
1716 if (is_it_end_of_statement ())
1718 demand_empty_rest_of_line ();
1721 p
= skip_space (input_line_pointer
);
1725 if (*p
== '\"' || *p
== '\'')
1727 for (quote
= *p
, q
= ++p
, cnt
= 0; *p
&& quote
!= *p
; ++p
, ++cnt
)
1729 u
= frag_more (cnt
);
1732 as_warn (_("unterminated string"));
1734 p
= skip_space (p
+1);
1738 p
= parse_exp (p
, &exp
);
1739 if (exp
.X_op
== O_md1
|| exp
.X_op
== O_register
)
1745 as_warn (_("parentheses ignored"));
1746 emit_byte (&exp
, BFD_RELOC_8
);
1750 while (*p
++ == ',') ;
1751 input_line_pointer
= (char *)(p
-1);
1755 emit_mulub (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1759 p
= skip_space (args
);
1760 if (TOLOWER (*p
++) != 'a' || *p
++ != ',')
1766 reg
= TOLOWER (*p
++);
1773 check_mach (INS_R800
);
1774 if (!*skip_space (p
))
1778 *q
= opcode
+ ((reg
- 'b') << 3);
1790 emit_muluw (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1794 p
= skip_space (args
);
1795 if (TOLOWER (*p
++) != 'h' || TOLOWER (*p
++) != 'l' || *p
++ != ',')
1802 p
= parse_exp (p
, & reg
);
1804 if ((!reg
.X_md
) && reg
.X_op
== O_register
)
1805 switch (reg
.X_add_number
)
1809 check_mach (INS_R800
);
1812 *q
= opcode
+ ((reg
.X_add_number
& 3) << 4);
1821 /* Port specific pseudo ops. */
1822 const pseudo_typeS md_pseudo_table
[] =
1824 { "db" , emit_data
, 1},
1827 { "def24", cons
, 3},
1828 { "def32", cons
, 4},
1829 { "defb", emit_data
, 1},
1830 { "defs", s_space
, 1}, /* Synonym for ds on some assemblers. */
1832 { "ds", s_space
, 1}, /* Fill with bytes rather than words. */
1834 { "psect", obj_coff_section
, 0}, /* TODO: Translate attributes. */
1835 { "set", 0, 0}, /* Real instruction on z80. */
1839 static table_t instab
[] =
1841 { "adc", 0x88, 0x4A, emit_adc
},
1842 { "add", 0x80, 0x09, emit_add
},
1843 { "and", 0x00, 0xA0, emit_s
},
1844 { "bit", 0xCB, 0x40, emit_bit
},
1845 { "call", 0xCD, 0xC4, emit_jpcc
},
1846 { "ccf", 0x00, 0x3F, emit_insn
},
1847 { "cp", 0x00, 0xB8, emit_s
},
1848 { "cpd", 0xED, 0xA9, emit_insn
},
1849 { "cpdr", 0xED, 0xB9, emit_insn
},
1850 { "cpi", 0xED, 0xA1, emit_insn
},
1851 { "cpir", 0xED, 0xB1, emit_insn
},
1852 { "cpl", 0x00, 0x2F, emit_insn
},
1853 { "daa", 0x00, 0x27, emit_insn
},
1854 { "dec", 0x0B, 0x05, emit_incdec
},
1855 { "di", 0x00, 0xF3, emit_insn
},
1856 { "djnz", 0x00, 0x10, emit_jr
},
1857 { "ei", 0x00, 0xFB, emit_insn
},
1858 { "ex", 0x00, 0x00, emit_ex
},
1859 { "exx", 0x00, 0xD9, emit_insn
},
1860 { "halt", 0x00, 0x76, emit_insn
},
1861 { "im", 0xED, 0x46, emit_im
},
1862 { "in", 0x00, 0x00, emit_in
},
1863 { "inc", 0x03, 0x04, emit_incdec
},
1864 { "ind", 0xED, 0xAA, emit_insn
},
1865 { "indr", 0xED, 0xBA, emit_insn
},
1866 { "ini", 0xED, 0xA2, emit_insn
},
1867 { "inir", 0xED, 0xB2, emit_insn
},
1868 { "jp", 0xC3, 0xC2, emit_jpcc
},
1869 { "jr", 0x18, 0x20, emit_jrcc
},
1870 { "ld", 0x00, 0x00, emit_ld
},
1871 { "ldd", 0xED, 0xA8, emit_insn
},
1872 { "lddr", 0xED, 0xB8, emit_insn
},
1873 { "ldi", 0xED, 0xA0, emit_insn
},
1874 { "ldir", 0xED, 0xB0, emit_insn
},
1875 { "mulub", 0xED, 0xC5, emit_mulub
}, /* R800 only. */
1876 { "muluw", 0xED, 0xC3, emit_muluw
}, /* R800 only. */
1877 { "neg", 0xed, 0x44, emit_insn
},
1878 { "nop", 0x00, 0x00, emit_insn
},
1879 { "or", 0x00, 0xB0, emit_s
},
1880 { "otdr", 0xED, 0xBB, emit_insn
},
1881 { "otir", 0xED, 0xB3, emit_insn
},
1882 { "out", 0x00, 0x00, emit_out
},
1883 { "outd", 0xED, 0xAB, emit_insn
},
1884 { "outi", 0xED, 0xA3, emit_insn
},
1885 { "pop", 0x00, 0xC1, emit_pop
},
1886 { "push", 0x00, 0xC5, emit_pop
},
1887 { "res", 0xCB, 0x80, emit_bit
},
1888 { "ret", 0xC9, 0xC0, emit_retcc
},
1889 { "reti", 0xED, 0x4D, emit_insn
},
1890 { "retn", 0xED, 0x45, emit_insn
},
1891 { "rl", 0xCB, 0x10, emit_mr
},
1892 { "rla", 0x00, 0x17, emit_insn
},
1893 { "rlc", 0xCB, 0x00, emit_mr
},
1894 { "rlca", 0x00, 0x07, emit_insn
},
1895 { "rld", 0xED, 0x6F, emit_insn
},
1896 { "rr", 0xCB, 0x18, emit_mr
},
1897 { "rra", 0x00, 0x1F, emit_insn
},
1898 { "rrc", 0xCB, 0x08, emit_mr
},
1899 { "rrca", 0x00, 0x0F, emit_insn
},
1900 { "rrd", 0xED, 0x67, emit_insn
},
1901 { "rst", 0x00, 0xC7, emit_rst
},
1902 { "sbc", 0x98, 0x42, emit_adc
},
1903 { "scf", 0x00, 0x37, emit_insn
},
1904 { "set", 0xCB, 0xC0, emit_bit
},
1905 { "sla", 0xCB, 0x20, emit_mr
},
1906 { "sli", 0xCB, 0x30, emit_mr
},
1907 { "sll", 0xCB, 0x30, emit_mr
},
1908 { "sra", 0xCB, 0x28, emit_mr
},
1909 { "srl", 0xCB, 0x38, emit_mr
},
1910 { "sub", 0x00, 0x90, emit_s
},
1911 { "xor", 0x00, 0xA8, emit_s
},
1915 md_assemble (char* str
)
1923 old_ptr
= input_line_pointer
;
1924 p
= skip_space (str
);
1925 for (i
= 0; (i
< BUFLEN
) && (ISALPHA (*p
));)
1926 buf
[i
++] = TOLOWER (*p
++);
1930 buf
[BUFLEN
-3] = buf
[BUFLEN
-2] = '.'; /* Mark opcode as abbreviated. */
1932 as_bad (_("Unknown instruction '%s'"), buf
);
1934 else if ((*p
) && (!ISSPACE (*p
)))
1935 as_bad (_("syntax error"));
1942 insp
= bsearch (&key
, instab
, ARRAY_SIZE (instab
),
1943 sizeof (instab
[0]), key_cmp
);
1945 as_bad (_("Unknown instruction '%s'"), buf
);
1948 p
= insp
->fp (insp
->prefix
, insp
->opcode
, p
);
1950 if ((!err_flag
) && *p
)
1951 as_bad (_("junk at end of line, first unrecognized character is `%c'"),
1955 input_line_pointer
= old_ptr
;
1959 md_apply_fix (fixS
* fixP
, valueT
* valP
, segT seg ATTRIBUTE_UNUSED
)
1961 long val
= * (long *) valP
;
1962 char *p_lit
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1964 switch (fixP
->fx_r_type
)
1966 case BFD_RELOC_8_PCREL
:
1969 fixP
->fx_no_overflow
= 1;
1974 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1975 if (!fixP
->fx_no_overflow
)
1976 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1977 _("relative jump out of range"));
1983 case BFD_RELOC_Z80_DISP8
:
1986 fixP
->fx_no_overflow
= 1;
1991 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1992 if (!fixP
->fx_no_overflow
)
1993 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1994 _("index offset out of range"));
2001 if (val
> 255 || val
< -128)
2002 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, _("overflow"));
2004 fixP
->fx_no_overflow
= 1;
2005 if (fixP
->fx_addsy
== NULL
)
2011 *p_lit
++ = (val
>> 8);
2012 fixP
->fx_no_overflow
= 1;
2013 if (fixP
->fx_addsy
== NULL
)
2017 case BFD_RELOC_24
: /* Def24 may produce this. */
2019 *p_lit
++ = (val
>> 8);
2020 *p_lit
++ = (val
>> 16);
2021 fixP
->fx_no_overflow
= 1;
2022 if (fixP
->fx_addsy
== NULL
)
2026 case BFD_RELOC_32
: /* Def32 and .long may produce this. */
2028 *p_lit
++ = (val
>> 8);
2029 *p_lit
++ = (val
>> 16);
2030 *p_lit
++ = (val
>> 24);
2031 if (fixP
->fx_addsy
== NULL
)
2036 printf (_("md_apply_fix: unknown r_type 0x%x\n"), fixP
->fx_r_type
);
2041 /* GAS will call this to generate a reloc. GAS will pass the
2042 resulting reloc to `bfd_install_relocation'. This currently works
2043 poorly, as `bfd_install_relocation' often does the wrong thing, and
2044 instances of `tc_gen_reloc' have been written to work around the
2045 problems, which in turns makes it difficult to fix
2046 `bfd_install_relocation'. */
2048 /* If while processing a fixup, a reloc really
2049 needs to be created then it is done here. */
2052 tc_gen_reloc (asection
*seg ATTRIBUTE_UNUSED
, fixS
*fixp
)
2056 if (! bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
))
2058 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2059 _("reloc %d not supported by object file format"),
2060 (int) fixp
->fx_r_type
);
2064 reloc
= XNEW (arelent
);
2065 reloc
->sym_ptr_ptr
= XNEW (asymbol
*);
2066 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2067 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2068 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2069 reloc
->addend
= fixp
->fx_offset
;