1 /* tc-z80.c -- Assemble code for the Zilog Z80 and ASCII R800
2 Copyright (C) 2005-2015 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
;
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
= "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]);
321 for (quote
= *p
++; quote
!= *p
&& '\n' != *p
; ++p
)
325 as_bad (_("-- unterminated string"));
326 ignore_rest_of_line ();
332 /* Check for <label>[:] [.](EQU|DEFL) <value>. */
333 if (is_name_beginner (*input_line_pointer
))
336 char c
, *rest
, *line_start
;
339 line_start
= input_line_pointer
;
343 c
= get_symbol_name (&name
);
344 rest
= input_line_pointer
+ 1;
348 if (*rest
== ' ' || *rest
== '\t')
352 if (strncasecmp (rest
, "EQU", 3) == 0)
354 else if (strncasecmp (rest
, "DEFL", 4) == 0)
358 if (len
&& (!ISALPHA(rest
[len
]) ) )
360 /* Handle assignment here. */
361 if (line_start
[-1] == '\n')
363 bump_line_counters ();
366 input_line_pointer
= rest
+ len
- 1;
367 /* Allow redefining with "DEFL" (len == 4), but not with "EQU". */
368 equals (name
, len
== 4);
373 /* Restore line and pointer. */
374 (void) restore_line_pointer (c
);
375 input_line_pointer
= line_start
;
382 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
388 md_atof (int type ATTRIBUTE_UNUSED
, char *litP ATTRIBUTE_UNUSED
,
389 int *sizeP ATTRIBUTE_UNUSED
)
391 return _("floating point numbers are not implemented");
395 md_section_align (segT seg ATTRIBUTE_UNUSED
, valueT size
)
401 md_pcrel_from (fixS
* fixp
)
403 return fixp
->fx_where
+
404 fixp
->fx_frag
->fr_address
+ 1;
407 typedef const char * (asfunc
)(char, char, const char*);
409 typedef struct _table_t
417 /* Compares the key for structs that start with a char * to the key. */
419 key_cmp (const void * a
, const void * b
)
421 const char *str_a
, *str_b
;
423 str_a
= *((const char**)a
);
424 str_b
= *((const char**)b
);
425 return strcmp (str_a
, str_b
);
429 const char *key
= buf
;
431 /* Prevent an error on a line from also generating
432 a "junk at end of line" error message. */
433 static char err_flag
;
436 error (const char * message
)
438 as_bad ("%s", message
);
445 error (_("illegal operand"));
449 wrong_mach (int ins_type
)
456 p
= "undocumented instruction";
459 p
= "instruction does not work on R800";
462 p
= "instruction only works R800";
465 p
= 0; /* Not reachable. */
468 if (ins_type
& ins_err
)
471 as_warn ("%s", _(p
));
475 check_mach (int ins_type
)
477 if ((ins_type
& ins_ok
) == 0)
478 wrong_mach (ins_type
);
479 ins_used
|= ins_type
;
482 /* Check whether an expression is indirect. */
484 is_indir (const char *s
)
490 /* Indirection is indicated with parentheses. */
493 for (p
= s
, depth
= 0; *p
&& *p
!= ','; ++p
)
499 for (quote
= *p
++; quote
!= *p
&& *p
!= '\n'; ++p
)
500 if (*p
== '\\' && p
[1])
510 p
= skip_space (p
+ 1);
516 error (_("mismatched parentheses"));
522 error (_("mismatched parentheses"));
527 /* Check whether a symbol involves a register. */
529 contains_register(symbolS
*sym
)
533 expressionS
* ex
= symbol_get_value_expression(sym
);
534 return (O_register
== ex
->X_op
)
535 || (ex
->X_add_symbol
&& contains_register(ex
->X_add_symbol
))
536 || (ex
->X_op_symbol
&& contains_register(ex
->X_op_symbol
));
542 /* Parse general expression, not loooking for indexed adressing. */
544 parse_exp_not_indexed (const char *s
, expressionS
*op
)
550 op
->X_md
= indir
= is_indir (p
);
551 input_line_pointer
= (char*) s
;
556 error (_("missing operand"));
559 error (_("bad expression syntax"));
564 return input_line_pointer
;
567 /* Parse expression, change operator to O_md1 for indexed addressing*/
569 parse_exp (const char *s
, expressionS
*op
)
571 const char* res
= parse_exp_not_indexed (s
, op
);
576 if (op
->X_md
&& (O_register
== symbol_get_value_expression(op
->X_add_symbol
)->X_op
))
578 int rnum
= symbol_get_value_expression(op
->X_add_symbol
)->X_add_number
;
579 if ( ((REG_IX
!= rnum
) && (REG_IY
!= rnum
)) || contains_register(op
->X_op_symbol
) )
585 if (O_subtract
== op
->X_op
)
588 minus
.X_op
= O_uminus
;
589 minus
.X_add_number
= 0;
590 minus
.X_add_symbol
= op
->X_op_symbol
;
591 minus
.X_op_symbol
= 0;
592 op
->X_op_symbol
= make_expr_symbol(&minus
);
595 symbol_get_value_expression(op
->X_op_symbol
)->X_add_number
+= op
->X_add_number
;
596 op
->X_add_number
= rnum
;
597 op
->X_add_symbol
= op
->X_op_symbol
;
604 if ( op
->X_md
&& ((REG_IX
== op
->X_add_number
)||(REG_IY
== op
->X_add_number
)) )
606 op
->X_add_symbol
= zero
;
616 /* Condition codes, including some synonyms provided by HiTech zas. */
617 static const struct reg_entry cc_tab
[] =
635 /* Parse condition code. */
637 parse_cc (const char *s
, char * op
)
641 struct reg_entry
* cc_p
;
643 for (i
= 0; i
< BUFLEN
; ++i
)
645 if (!ISALPHA (s
[i
])) /* Condition codes consist of letters only. */
647 buf
[i
] = TOLOWER (s
[i
]);
651 && ((s
[i
] == 0) || (s
[i
] == ',')))
654 cc_p
= bsearch (&key
, cc_tab
, ARRAY_SIZE (cc_tab
),
655 sizeof (cc_tab
[0]), key_cmp
);
672 emit_insn (char prefix
, char opcode
, const char * args
)
687 void z80_cons_fix_new (fragS
*frag_p
, int offset
, int nbytes
, expressionS
*exp
)
689 bfd_reloc_code_real_type r
[4] =
697 if (nbytes
< 1 || nbytes
> 4)
699 as_bad (_("unsupported BFD relocation size %u"), nbytes
);
703 fix_new_exp (frag_p
, offset
, nbytes
, exp
, 0, r
[nbytes
-1]);
708 emit_byte (expressionS
* val
, bfd_reloc_code_real_type r_type
)
714 *p
= val
->X_add_number
;
715 if ( contains_register(val
->X_add_symbol
) || contains_register(val
->X_op_symbol
) )
719 else if ((r_type
== BFD_RELOC_8_PCREL
) && (val
->X_op
== O_constant
))
721 as_bad (_("cannot make a relative jump to an absolute location"));
723 else if (val
->X_op
== O_constant
)
726 hi
= (BFD_RELOC_8
== r_type
) ? 255 : 127;
728 if ((val
->X_add_number
< lo
) || (val
->X_add_number
> hi
))
730 if (r_type
== BFD_RELOC_Z80_DISP8
)
731 as_bad (_("offset too large"));
733 as_warn (_("overflow"));
738 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, 1, val
,
739 (r_type
== BFD_RELOC_8_PCREL
) ? TRUE
: FALSE
, r_type
);
740 /* FIXME : Process constant offsets immediately. */
745 emit_word (expressionS
* val
)
750 if ( (val
->X_op
== O_register
)
751 || (val
->X_op
== O_md1
)
752 || contains_register(val
->X_add_symbol
)
753 || contains_register(val
->X_op_symbol
) )
757 *p
= val
->X_add_number
;
758 p
[1] = (val
->X_add_number
>>8);
759 if (val
->X_op
!= O_constant
)
760 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, 2,
761 val
, FALSE
, BFD_RELOC_16
);
766 emit_mx (char prefix
, char opcode
, int shift
, expressionS
* arg
)
767 /* The operand m may be r, (hl), (ix+d), (iy+d),
768 if 0 == prefix m may also be ixl, ixh, iyl, iyh. */
773 rnum
= arg
->X_add_number
;
789 if ((prefix
== 0) && (rnum
& R_INDEX
))
791 prefix
= (rnum
& R_IX
) ? 0xDD : 0xFD;
792 check_mach (INS_UNDOC
);
801 q
= frag_more (prefix
? 2 : 1);
804 * q
++ = opcode
+ (rnum
<< shift
);
808 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
809 *q
= (prefix
) ? prefix
: (opcode
+ (6 << shift
));
811 expressionS offset
= *arg
;
812 offset
.X_op
= O_symbol
;
813 offset
.X_add_number
= 0;
814 emit_byte (&offset
, BFD_RELOC_Z80_DISP8
);
819 *q
= opcode
+(6<<shift
);
827 /* The operand m may be r, (hl), (ix+d), (iy+d),
828 if 0 = prefix m may also be ixl, ixh, iyl, iyh. */
830 emit_m (char prefix
, char opcode
, const char *args
)
835 p
= parse_exp (args
, &arg_m
);
840 emit_mx (prefix
, opcode
, 0, &arg_m
);
848 /* The operand m may be as above or one of the undocumented
849 combinations (ix+d),r and (iy+d),r (if unportable instructions
852 emit_mr (char prefix
, char opcode
, const char *args
)
854 expressionS arg_m
, arg_r
;
857 p
= parse_exp (args
, & arg_m
);
864 p
= parse_exp (p
+ 1, & arg_r
);
866 if ((arg_r
.X_md
== 0)
867 && (arg_r
.X_op
== O_register
)
868 && (arg_r
.X_add_number
< 8))
869 opcode
+= arg_r
.X_add_number
-6; /* Emit_mx () will add 6. */
875 check_mach (INS_UNPORT
);
878 emit_mx (prefix
, opcode
, 0, & arg_m
);
887 emit_sx (char prefix
, char opcode
, expressionS
* arg_p
)
895 emit_mx (prefix
, opcode
, 0, arg_p
);
902 q
= frag_more (prefix
? 2 : 1);
906 emit_byte (arg_p
, BFD_RELOC_8
);
911 /* The operand s may be r, (hl), (ix+d), (iy+d), n. */
913 emit_s (char prefix
, char opcode
, const char *args
)
918 p
= parse_exp (args
, & arg_s
);
919 emit_sx (prefix
, opcode
, & arg_s
);
924 emit_call (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
927 const char *p
; char *q
;
929 p
= parse_exp_not_indexed (args
, &addr
);
941 /* Operand may be rr, r, (hl), (ix+d), (iy+d). */
943 emit_incdec (char prefix
, char opcode
, const char * args
)
947 const char *p
; char *q
;
949 p
= parse_exp (args
, &operand
);
950 rnum
= operand
.X_add_number
;
952 && (operand
.X_op
== O_register
)
955 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
957 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
958 *q
= prefix
+ ((rnum
& 3) << 4);
962 if ((operand
.X_op
== O_md1
) || (operand
.X_op
== O_register
))
963 emit_mx (0, opcode
, 3, & operand
);
971 emit_jr (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
977 p
= parse_exp_not_indexed (args
, &addr
);
984 emit_byte (&addr
, BFD_RELOC_8_PCREL
);
990 emit_jp (char prefix
, char opcode
, const char * args
)
997 p
= parse_exp_not_indexed (args
, & addr
);
1000 rnum
= addr
.X_add_number
;
1001 if ((O_register
== addr
.X_op
) && (REG_HL
== (rnum
& ~R_INDEX
)))
1003 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
1005 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
1021 emit_im (char prefix
, char opcode
, const char * args
)
1027 p
= parse_exp (args
, & mode
);
1028 if (mode
.X_md
|| (mode
.X_op
!= O_constant
))
1031 switch (mode
.X_add_number
)
1035 ++mode
.X_add_number
;
1040 *q
= opcode
+ 8*mode
.X_add_number
;
1049 emit_pop (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1055 p
= parse_exp (args
, & regp
);
1057 && (regp
.X_op
== O_register
)
1058 && (regp
.X_add_number
& R_STACKABLE
))
1062 rnum
= regp
.X_add_number
;
1066 *q
++ = (rnum
&R_IX
)?0xDD:0xFD;
1070 *q
= opcode
+ ((rnum
& 3) << 4);
1079 emit_retcc (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1084 p
= parse_cc (args
, &cc
);
1090 return p
? p
: args
;
1094 emit_adc (char prefix
, char opcode
, const char * args
)
1101 p
= parse_exp (args
, &term
);
1104 error (_("bad instruction syntax"));
1108 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1111 switch (term
.X_add_number
)
1114 p
= emit_s (0, prefix
, p
);
1117 p
= parse_exp (p
, &term
);
1118 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1120 rnum
= term
.X_add_number
;
1121 if (R_ARITH
== (rnum
& (R_ARITH
| R_INDEX
)))
1125 *q
= opcode
+ ((rnum
& 3) << 4);
1137 emit_add (char prefix
, char opcode
, const char * args
)
1144 p
= parse_exp (args
, &term
);
1147 error (_("bad instruction syntax"));
1151 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1154 switch (term
.X_add_number
& ~R_INDEX
)
1157 p
= emit_s (0, prefix
, p
);
1160 lhs
= term
.X_add_number
;
1161 p
= parse_exp (p
, &term
);
1162 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1164 rhs
= term
.X_add_number
;
1166 && ((rhs
== lhs
) || ((rhs
& ~R_INDEX
) != REG_HL
)))
1168 q
= frag_more ((lhs
& R_INDEX
) ? 2 : 1);
1170 *q
++ = (lhs
& R_IX
) ? 0xDD : 0xFD;
1171 *q
= opcode
+ ((rhs
& 3) << 4);
1183 emit_bit (char prefix
, char opcode
, const char * args
)
1189 p
= parse_exp (args
, &b
);
1191 error (_("bad instruction syntax"));
1193 bn
= b
.X_add_number
;
1195 && (b
.X_op
== O_constant
)
1200 /* Bit : no optional third operand. */
1201 p
= emit_m (prefix
, opcode
+ (bn
<< 3), p
);
1203 /* Set, res : resulting byte can be copied to register. */
1204 p
= emit_mr (prefix
, opcode
+ (bn
<< 3), p
);
1212 emit_jpcc (char prefix
, char opcode
, const char * args
)
1217 p
= parse_cc (args
, & cc
);
1218 if (p
&& *p
++ == ',')
1219 p
= emit_call (0, opcode
+ cc
, p
);
1221 p
= (prefix
== (char)0xC3)
1222 ? emit_jp (0xE9, prefix
, args
)
1223 : emit_call (0, prefix
, args
);
1228 emit_jrcc (char prefix
, char opcode
, const char * args
)
1233 p
= parse_cc (args
, &cc
);
1234 if (p
&& *p
++ == ',')
1237 error (_("condition code invalid for jr"));
1239 p
= emit_jr (0, opcode
+ cc
, p
);
1242 p
= emit_jr (0, prefix
, args
);
1248 emit_ex (char prefix_in ATTRIBUTE_UNUSED
,
1249 char opcode_in ATTRIBUTE_UNUSED
, const char * args
)
1253 char prefix
, opcode
;
1255 p
= parse_exp_not_indexed (args
, &op
);
1259 error (_("bad instruction syntax"));
1263 prefix
= opcode
= 0;
1264 if (op
.X_op
== O_register
)
1265 switch (op
.X_add_number
| (op
.X_md
? 0x8000 : 0))
1268 if (TOLOWER (*p
++) == 'a' && TOLOWER (*p
++) == 'f')
1270 /* The scrubber changes '\'' to '`' in this context. */
1277 if (TOLOWER (*p
++) == 'h' && TOLOWER (*p
++) == 'l')
1281 p
= parse_exp (p
, & op
);
1282 if (op
.X_op
== O_register
1284 && (op
.X_add_number
& ~R_INDEX
) == REG_HL
)
1287 if (R_INDEX
& op
.X_add_number
)
1288 prefix
= (R_IX
& op
.X_add_number
) ? 0xDD : 0xFD;
1293 emit_insn (prefix
, opcode
, p
);
1301 emit_in (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1304 expressionS reg
, port
;
1308 p
= parse_exp (args
, ®
);
1311 error (_("bad instruction syntax"));
1315 p
= parse_exp (p
, &port
);
1317 && reg
.X_op
== O_register
1318 && (reg
.X_add_number
<= 7 || reg
.X_add_number
== REG_F
)
1321 if (port
.X_op
!= O_md1
&& port
.X_op
!= O_register
)
1323 if (REG_A
== reg
.X_add_number
)
1327 emit_byte (&port
, BFD_RELOC_8
);
1334 if (port
.X_add_number
== REG_C
)
1336 if (reg
.X_add_number
== REG_F
)
1337 check_mach (INS_UNDOC
);
1342 *q
= 0x40|((reg
.X_add_number
&7)<<3);
1355 emit_out (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1358 expressionS reg
, port
;
1362 p
= parse_exp (args
, & port
);
1365 error (_("bad instruction syntax"));
1368 p
= parse_exp (p
, ®
);
1370 { ill_op (); return p
; }
1371 /* Allow "out (c), 0" as unportable instruction. */
1372 if (reg
.X_op
== O_constant
&& reg
.X_add_number
== 0)
1374 check_mach (INS_UNPORT
);
1375 reg
.X_op
= O_register
;
1376 reg
.X_add_number
= 6;
1379 || reg
.X_op
!= O_register
1380 || reg
.X_add_number
> 7)
1383 if (port
.X_op
!= O_register
&& port
.X_op
!= O_md1
)
1385 if (REG_A
== reg
.X_add_number
)
1389 emit_byte (&port
, BFD_RELOC_8
);
1396 if (REG_C
== port
.X_add_number
)
1400 *q
= 0x41 | (reg
.X_add_number
<< 3);
1409 emit_rst (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1415 p
= parse_exp_not_indexed (args
, &addr
);
1416 if (addr
.X_op
!= O_constant
)
1418 error ("rst needs constant address");
1422 if (addr
.X_add_number
& ~(7 << 3))
1427 *q
= opcode
+ (addr
.X_add_number
& (7 << 3));
1433 emit_ldxhl (char prefix
, char opcode
, expressionS
*src
, expressionS
*d
)
1441 if (src
->X_op
== O_register
)
1443 if (src
->X_add_number
>7)
1452 *q
= opcode
+ src
->X_add_number
;
1454 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1467 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1468 emit_byte (src
, BFD_RELOC_8
);
1474 emit_ldreg (int dest
, expressionS
* src
)
1481 /* 8 Bit ld group: */
1484 if (src
->X_md
== 0 && src
->X_op
== O_register
&& src
->X_add_number
== REG_A
)
1488 *q
= (dest
== REG_I
) ? 0x47 : 0x4F;
1495 if ((src
->X_md
) && src
->X_op
!= O_register
&& src
->X_op
!= O_md1
)
1504 && src
->X_op
== O_register
1505 && (src
->X_add_number
== REG_BC
|| src
->X_add_number
== REG_DE
))
1508 *q
= 0x0A + ((src
->X_add_number
& 1) << 4);
1513 && src
->X_op
== O_register
1514 && (src
->X_add_number
== REG_R
|| src
->X_add_number
== REG_I
))
1518 *q
= (src
->X_add_number
== REG_I
) ? 0x57 : 0x5F;
1526 emit_sx (0, 0x40 + (dest
<< 3), src
);
1531 if ((src
->X_md
== 0)
1532 && (src
->X_op
== O_register
)
1533 && (src
->X_add_number
& R_INDEX
))
1536 emit_sx (0, 0x40 + (dest
<< 3), src
);
1548 check_mach (INS_UNDOC
);
1549 if (src
-> X_op
== O_register
)
1551 rnum
= src
->X_add_number
;
1552 if ((rnum
& ~R_INDEX
) < 8
1553 && ((rnum
& R_INDEX
) == (dest
& R_INDEX
)
1554 || ( (rnum
& ~R_INDEX
) != REG_H
1555 && (rnum
& ~R_INDEX
) != REG_L
)))
1558 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1559 *q
= 0x40 + ((dest
& 0x07) << 3) + (rnum
& 7);
1567 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1568 *q
= 0x06 + ((dest
& 0x07) << 3);
1569 emit_byte (src
, BFD_RELOC_8
);
1573 /* 16 Bit ld group: */
1576 && src
->X_op
== O_register
1577 && REG_HL
== (src
->X_add_number
&~ R_INDEX
))
1579 q
= frag_more ((src
->X_add_number
& R_INDEX
) ? 2 : 1);
1580 if (src
->X_add_number
& R_INDEX
)
1581 *q
++ = (src
->X_add_number
& R_IX
) ? 0xDD : 0xFD;
1588 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1590 q
= frag_more (src
->X_md
? 2 : 1);
1594 *q
= 0x4B + ((dest
& 3) << 4);
1597 *q
= 0x01 + ((dest
& 3) << 4);
1604 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1606 q
= frag_more ((dest
& R_INDEX
) ? 2 : 1);
1608 * q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1609 *q
= (src
->X_md
) ? 0x2A : 0x21;
1624 emit_ld (char prefix_in ATTRIBUTE_UNUSED
, char opcode_in ATTRIBUTE_UNUSED
,
1627 expressionS dst
, src
;
1630 char prefix
, opcode
;
1632 p
= parse_exp (args
, &dst
);
1634 error (_("bad instruction syntax"));
1635 p
= parse_exp (p
, &src
);
1641 expressionS dst_offset
= dst
;
1642 dst_offset
.X_op
= O_symbol
;
1643 dst_offset
.X_add_number
= 0;
1644 emit_ldxhl ((dst
.X_add_number
& R_IX
) ? 0xDD : 0xFD, 0x70,
1652 switch (dst
.X_add_number
)
1656 if (src
.X_md
== 0 && src
.X_op
== O_register
&& src
.X_add_number
== REG_A
)
1659 *q
= 0x02 + ( (dst
.X_add_number
& 1) << 4);
1665 emit_ldxhl (0, 0x70, &src
, NULL
);
1672 emit_ldreg (dst
.X_add_number
, &src
);
1676 if (src
.X_md
!= 0 || src
.X_op
!= O_register
)
1678 prefix
= opcode
= 0;
1679 switch (src
.X_add_number
)
1682 opcode
= 0x32; break;
1683 case REG_BC
: case REG_DE
: case REG_SP
:
1684 prefix
= 0xED; opcode
= 0x43 + ((src
.X_add_number
&3)<<4); break;
1686 opcode
= 0x22; break;
1688 prefix
= 0xDD; opcode
= 0x22; break;
1690 prefix
= 0xFD; opcode
= 0x22; break;
1694 q
= frag_more (prefix
?2:1);
1707 emit_data (int size ATTRIBUTE_UNUSED
)
1714 if (is_it_end_of_statement ())
1716 demand_empty_rest_of_line ();
1719 p
= skip_space (input_line_pointer
);
1723 if (*p
== '\"' || *p
== '\'')
1725 for (quote
= *p
, q
= ++p
, cnt
= 0; *p
&& quote
!= *p
; ++p
, ++cnt
)
1727 u
= frag_more (cnt
);
1730 as_warn (_("unterminated string"));
1732 p
= skip_space (p
+1);
1736 p
= parse_exp (p
, &exp
);
1737 if (exp
.X_op
== O_md1
|| exp
.X_op
== O_register
)
1743 as_warn (_("parentheses ignored"));
1744 emit_byte (&exp
, BFD_RELOC_8
);
1748 while (*p
++ == ',') ;
1749 input_line_pointer
= (char *)(p
-1);
1753 emit_mulub (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1757 p
= skip_space (args
);
1758 if (TOLOWER (*p
++) != 'a' || *p
++ != ',')
1764 reg
= TOLOWER (*p
++);
1771 check_mach (INS_R800
);
1772 if (!*skip_space (p
))
1776 *q
= opcode
+ ((reg
- 'b') << 3);
1787 emit_muluw (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1791 p
= skip_space (args
);
1792 if (TOLOWER (*p
++) != 'h' || TOLOWER (*p
++) != 'l' || *p
++ != ',')
1799 p
= parse_exp (p
, & reg
);
1801 if ((!reg
.X_md
) && reg
.X_op
== O_register
)
1802 switch (reg
.X_add_number
)
1806 check_mach (INS_R800
);
1809 *q
= opcode
+ ((reg
.X_add_number
& 3) << 4);
1818 /* Port specific pseudo ops. */
1819 const pseudo_typeS md_pseudo_table
[] =
1821 { "db" , emit_data
, 1},
1824 { "def24", cons
, 3},
1825 { "def32", cons
, 4},
1826 { "defb", emit_data
, 1},
1827 { "defs", s_space
, 1}, /* Synonym for ds on some assemblers. */
1829 { "ds", s_space
, 1}, /* Fill with bytes rather than words. */
1831 { "psect", obj_coff_section
, 0}, /* TODO: Translate attributes. */
1832 { "set", 0, 0}, /* Real instruction on z80. */
1836 static table_t instab
[] =
1838 { "adc", 0x88, 0x4A, emit_adc
},
1839 { "add", 0x80, 0x09, emit_add
},
1840 { "and", 0x00, 0xA0, emit_s
},
1841 { "bit", 0xCB, 0x40, emit_bit
},
1842 { "call", 0xCD, 0xC4, emit_jpcc
},
1843 { "ccf", 0x00, 0x3F, emit_insn
},
1844 { "cp", 0x00, 0xB8, emit_s
},
1845 { "cpd", 0xED, 0xA9, emit_insn
},
1846 { "cpdr", 0xED, 0xB9, emit_insn
},
1847 { "cpi", 0xED, 0xA1, emit_insn
},
1848 { "cpir", 0xED, 0xB1, emit_insn
},
1849 { "cpl", 0x00, 0x2F, emit_insn
},
1850 { "daa", 0x00, 0x27, emit_insn
},
1851 { "dec", 0x0B, 0x05, emit_incdec
},
1852 { "di", 0x00, 0xF3, emit_insn
},
1853 { "djnz", 0x00, 0x10, emit_jr
},
1854 { "ei", 0x00, 0xFB, emit_insn
},
1855 { "ex", 0x00, 0x00, emit_ex
},
1856 { "exx", 0x00, 0xD9, emit_insn
},
1857 { "halt", 0x00, 0x76, emit_insn
},
1858 { "im", 0xED, 0x46, emit_im
},
1859 { "in", 0x00, 0x00, emit_in
},
1860 { "inc", 0x03, 0x04, emit_incdec
},
1861 { "ind", 0xED, 0xAA, emit_insn
},
1862 { "indr", 0xED, 0xBA, emit_insn
},
1863 { "ini", 0xED, 0xA2, emit_insn
},
1864 { "inir", 0xED, 0xB2, emit_insn
},
1865 { "jp", 0xC3, 0xC2, emit_jpcc
},
1866 { "jr", 0x18, 0x20, emit_jrcc
},
1867 { "ld", 0x00, 0x00, emit_ld
},
1868 { "ldd", 0xED, 0xA8, emit_insn
},
1869 { "lddr", 0xED, 0xB8, emit_insn
},
1870 { "ldi", 0xED, 0xA0, emit_insn
},
1871 { "ldir", 0xED, 0xB0, emit_insn
},
1872 { "mulub", 0xED, 0xC5, emit_mulub
}, /* R800 only. */
1873 { "muluw", 0xED, 0xC3, emit_muluw
}, /* R800 only. */
1874 { "neg", 0xed, 0x44, emit_insn
},
1875 { "nop", 0x00, 0x00, emit_insn
},
1876 { "or", 0x00, 0xB0, emit_s
},
1877 { "otdr", 0xED, 0xBB, emit_insn
},
1878 { "otir", 0xED, 0xB3, emit_insn
},
1879 { "out", 0x00, 0x00, emit_out
},
1880 { "outd", 0xED, 0xAB, emit_insn
},
1881 { "outi", 0xED, 0xA3, emit_insn
},
1882 { "pop", 0x00, 0xC1, emit_pop
},
1883 { "push", 0x00, 0xC5, emit_pop
},
1884 { "res", 0xCB, 0x80, emit_bit
},
1885 { "ret", 0xC9, 0xC0, emit_retcc
},
1886 { "reti", 0xED, 0x4D, emit_insn
},
1887 { "retn", 0xED, 0x45, emit_insn
},
1888 { "rl", 0xCB, 0x10, emit_mr
},
1889 { "rla", 0x00, 0x17, emit_insn
},
1890 { "rlc", 0xCB, 0x00, emit_mr
},
1891 { "rlca", 0x00, 0x07, emit_insn
},
1892 { "rld", 0xED, 0x6F, emit_insn
},
1893 { "rr", 0xCB, 0x18, emit_mr
},
1894 { "rra", 0x00, 0x1F, emit_insn
},
1895 { "rrc", 0xCB, 0x08, emit_mr
},
1896 { "rrca", 0x00, 0x0F, emit_insn
},
1897 { "rrd", 0xED, 0x67, emit_insn
},
1898 { "rst", 0x00, 0xC7, emit_rst
},
1899 { "sbc", 0x98, 0x42, emit_adc
},
1900 { "scf", 0x00, 0x37, emit_insn
},
1901 { "set", 0xCB, 0xC0, emit_bit
},
1902 { "sla", 0xCB, 0x20, emit_mr
},
1903 { "sli", 0xCB, 0x30, emit_mr
},
1904 { "sll", 0xCB, 0x30, emit_mr
},
1905 { "sra", 0xCB, 0x28, emit_mr
},
1906 { "srl", 0xCB, 0x38, emit_mr
},
1907 { "sub", 0x00, 0x90, emit_s
},
1908 { "xor", 0x00, 0xA8, emit_s
},
1912 md_assemble (char* str
)
1920 old_ptr
= input_line_pointer
;
1921 p
= skip_space (str
);
1922 for (i
= 0; (i
< BUFLEN
) && (ISALPHA (*p
));)
1923 buf
[i
++] = TOLOWER (*p
++);
1927 buf
[BUFLEN
-3] = buf
[BUFLEN
-2] = '.'; /* Mark opcode as abbreviated. */
1929 as_bad (_("Unknown instruction '%s'"), buf
);
1931 else if ((*p
) && (!ISSPACE (*p
)))
1932 as_bad (_("syntax error"));
1939 insp
= bsearch (&key
, instab
, ARRAY_SIZE (instab
),
1940 sizeof (instab
[0]), key_cmp
);
1942 as_bad (_("Unknown instruction '%s'"), buf
);
1945 p
= insp
->fp (insp
->prefix
, insp
->opcode
, p
);
1947 if ((!err_flag
) && *p
)
1948 as_bad (_("junk at end of line, first unrecognized character is `%c'"),
1952 input_line_pointer
= old_ptr
;
1956 md_apply_fix (fixS
* fixP
, valueT
* valP
, segT seg ATTRIBUTE_UNUSED
)
1958 long val
= * (long *) valP
;
1959 char *p_lit
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1961 switch (fixP
->fx_r_type
)
1963 case BFD_RELOC_8_PCREL
:
1966 fixP
->fx_no_overflow
= 1;
1971 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1972 if (!fixP
->fx_no_overflow
)
1973 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1974 _("relative jump out of range"));
1980 case BFD_RELOC_Z80_DISP8
:
1983 fixP
->fx_no_overflow
= 1;
1988 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1989 if (!fixP
->fx_no_overflow
)
1990 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1991 _("index offset out of range"));
1998 if (val
> 255 || val
< -128)
1999 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, _("overflow"));
2001 fixP
->fx_no_overflow
= 1;
2002 if (fixP
->fx_addsy
== NULL
)
2008 *p_lit
++ = (val
>> 8);
2009 fixP
->fx_no_overflow
= 1;
2010 if (fixP
->fx_addsy
== NULL
)
2014 case BFD_RELOC_24
: /* Def24 may produce this. */
2016 *p_lit
++ = (val
>> 8);
2017 *p_lit
++ = (val
>> 16);
2018 fixP
->fx_no_overflow
= 1;
2019 if (fixP
->fx_addsy
== NULL
)
2023 case BFD_RELOC_32
: /* Def32 and .long may produce this. */
2025 *p_lit
++ = (val
>> 8);
2026 *p_lit
++ = (val
>> 16);
2027 *p_lit
++ = (val
>> 24);
2028 if (fixP
->fx_addsy
== NULL
)
2033 printf (_("md_apply_fix: unknown r_type 0x%x\n"), fixP
->fx_r_type
);
2038 /* GAS will call this to generate a reloc. GAS will pass the
2039 resulting reloc to `bfd_install_relocation'. This currently works
2040 poorly, as `bfd_install_relocation' often does the wrong thing, and
2041 instances of `tc_gen_reloc' have been written to work around the
2042 problems, which in turns makes it difficult to fix
2043 `bfd_install_relocation'. */
2045 /* If while processing a fixup, a reloc really
2046 needs to be created then it is done here. */
2049 tc_gen_reloc (asection
*seg ATTRIBUTE_UNUSED
, fixS
*fixp
)
2053 if (! bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
))
2055 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2056 _("reloc %d not supported by object file format"),
2057 (int) fixp
->fx_r_type
);
2061 reloc
= xmalloc (sizeof (arelent
));
2062 reloc
->sym_ptr_ptr
= xmalloc (sizeof (asymbol
*));
2063 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2064 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2065 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2066 reloc
->addend
= fixp
->fx_offset
;