1 /* tc-z80.c -- Assemble code for the Zilog Z80 and ASCII R800
2 Copyright 2005 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 2, 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
25 #include "safe-ctype.h"
28 #include "libiberty.h"
30 /* Exported constants. */
31 const char comment_chars
[] = ";\0";
32 const char line_comment_chars
[] = "#;\0";
33 const char line_separator_chars
[] = "\0";
34 const char EXP_CHARS
[] = "eE\0";
35 const char FLT_CHARS
[] = "RrFf\0";
37 /* For machine specific options. */
38 const char * md_shortopts
= ""; /* None yet. */
42 OPTION_MACH_Z80
= OPTION_MD_BASE
,
57 struct option md_longopts
[] =
59 { "z80", no_argument
, NULL
, OPTION_MACH_Z80
},
60 { "r800", no_argument
, NULL
, OPTION_MACH_R800
},
61 { "ignore-undocumented-instructions", no_argument
, NULL
, OPTION_MACH_IUD
},
62 { "Wnud", no_argument
, NULL
, OPTION_MACH_IUD
},
63 { "warn-undocumented-instructions", no_argument
, NULL
, OPTION_MACH_WUD
},
64 { "Wud", no_argument
, NULL
, OPTION_MACH_WUD
},
65 { "forbid-undocumented-instructions", no_argument
, NULL
, OPTION_MACH_FUD
},
66 { "Fud", no_argument
, NULL
, OPTION_MACH_FUD
},
67 { "ignore-unportable-instructions", no_argument
, NULL
, OPTION_MACH_IUP
},
68 { "Wnup", no_argument
, NULL
, OPTION_MACH_IUP
},
69 { "warn-unportable-instructions", no_argument
, NULL
, OPTION_MACH_WUP
},
70 { "Wup", no_argument
, NULL
, OPTION_MACH_WUP
},
71 { "forbid-unportable-instructions", no_argument
, NULL
, OPTION_MACH_FUP
},
72 { "Fup", no_argument
, NULL
, OPTION_MACH_FUP
},
74 { NULL
, no_argument
, NULL
, 0 }
77 size_t md_longopts_size
= sizeof (md_longopts
);
79 extern int coff_flags
;
80 /* Instruction classes that silently assembled. */
81 static int ins_ok
= INS_Z80
| INS_UNDOC
;
82 /* Instruction classes that generate errors. */
83 static int ins_err
= INS_R800
;
84 /* Instruction classes actually used, determines machine type. */
85 static int ins_used
= INS_Z80
;
88 md_parse_option (int c
, char* arg ATTRIBUTE_UNUSED
)
98 case OPTION_MACH_R800
:
99 ins_ok
= INS_Z80
| INS_UNDOC
| INS_R800
;
100 ins_err
= INS_UNPORT
;
102 case OPTION_MACH_IUD
:
104 ins_err
&= ~INS_UNDOC
;
106 case OPTION_MACH_IUP
:
107 ins_ok
|= INS_UNDOC
| INS_UNPORT
;
108 ins_err
&= ~(INS_UNDOC
| INS_UNPORT
);
110 case OPTION_MACH_WUD
:
111 if ((ins_ok
& INS_R800
) == 0)
113 ins_ok
&= ~(INS_UNDOC
|INS_UNPORT
);
114 ins_err
&= ~INS_UNDOC
;
117 case OPTION_MACH_WUP
:
118 ins_ok
&= ~INS_UNPORT
;
119 ins_err
&= ~(INS_UNDOC
|INS_UNPORT
);
121 case OPTION_MACH_FUD
:
122 if ((ins_ok
& INS_R800
) == 0)
124 ins_ok
&= (INS_UNDOC
| INS_UNPORT
);
125 ins_err
|= INS_UNDOC
| INS_UNPORT
;
128 case OPTION_MACH_FUP
:
129 ins_ok
&= ~INS_UNPORT
;
130 ins_err
|= INS_UNPORT
;
138 md_show_usage (FILE * f
)
141 CPU model/instruction set options:\n\
143 -z80\t\t assemble for Z80\n\
144 -ignore-undocumented-instructions\n\
146 \tsilently assemble undocumented Z80-instructions that work on R800\n\
147 -ignore-unportable-instructions\n\
149 \tsilently assemble all undocumented Z80-instructions\n\
150 -warn-undocumented-instructions\n\
152 \tissue warnings for undocumented Z80-instructions that work on R800\n\
153 -warn-unportable-instructions\n\
155 \tissue warnings for other undocumented Z80-instructions\n\
156 -forbid-undocumented-instructions\n\
158 \ttreat all undocumented z80-instructions as errors\n\
159 -forbid-unportable-instructions\n\
161 \ttreat undocumented z80-instructions that do not work on R800 as errors\n\
162 -r800\t assemble for R800\n\n\
163 Default: -z80 -ignore-undocument-instructions -warn-unportable-instructions.\n");
166 static symbolS
* zero
;
174 p
= input_line_pointer
;
175 input_line_pointer
= "0";
178 input_line_pointer
= p
;
179 zero
= make_expr_symbol (& nul
);
180 /* We do not use relaxation (yet). */
189 if (ins_used
& (INS_UNPORT
| INS_R800
))
190 ins_used
|= INS_UNDOC
;
195 mach_type
= bfd_mach_z80strict
;
197 case INS_Z80
|INS_UNDOC
:
198 mach_type
= bfd_mach_z80
;
200 case INS_Z80
|INS_UNDOC
|INS_UNPORT
:
201 mach_type
= bfd_mach_z80full
;
203 case INS_Z80
|INS_UNDOC
|INS_R800
:
204 mach_type
= bfd_mach_r800
;
210 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, mach_type
);
214 skip_space (const char *s
)
216 while (*s
== ' ' || *s
== '\t')
221 /* A non-zero return-value causes a continue in the
222 function read_a_source_file () in ../read.c. */
224 z80_start_line_hook (void)
229 /* Convert one character constants. */
230 for (p
= input_line_pointer
; *p
&& *p
!= '\n'; ++p
)
235 if (p
[1] != 0 && p
[1] != '\'' && p
[2] == '\'')
237 snprintf (buf
, 4, "%3d", (unsigned char)p
[1]);
244 for (quote
= *p
++; quote
!= *p
&& '\n' != *p
; ++p
)
248 as_bad (_("-- unterminated string"));
249 ignore_rest_of_line ();
255 /* Check for <label>[:] [.](EQU|DEFL) <value>. */
256 if (is_name_beginner (*input_line_pointer
))
258 char c
, *rest
, *line_start
;
262 line_start
= input_line_pointer
;
267 c
= get_symbol_end ();
268 rest
= input_line_pointer
+ 1;
272 if (*rest
== ' ' || *rest
== '\t')
276 if (strncasecmp (rest
, "EQU", 3) == 0)
278 else if (strncasecmp (rest
, "DEFL", 4) == 0)
282 if (len
&& (rest
[len
] == ' ' || rest
[len
] == '\t'))
284 /* Handle assignment here. */
285 input_line_pointer
= rest
+ len
;
286 if (line_start
[-1] == '\n')
287 bump_line_counters ();
288 /* Most Z80 assemblers require the first definition of a
289 label to use "EQU" and redefinitions to have "DEFL". */
290 if (len
== 3 && (symbolP
= symbol_find (line_start
)) != NULL
)
292 if (S_IS_DEFINED (symbolP
) || symbol_equated_p (symbolP
))
293 as_bad (_("symbol `%s' is already defined"), line_start
);
295 equals (line_start
, 1);
300 /* Restore line and pointer. */
301 *input_line_pointer
= c
;
302 input_line_pointer
= line_start
;
309 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
315 md_atof (int type ATTRIBUTE_UNUSED
, char *litP ATTRIBUTE_UNUSED
,
316 int *sizeP ATTRIBUTE_UNUSED
)
318 return _("floating point numbers are not implemented");
322 md_section_align (segT seg ATTRIBUTE_UNUSED
, valueT size
)
328 md_pcrel_from (fixS
* fixp
)
330 return fixp
->fx_where
+
331 fixp
->fx_frag
->fr_address
+ 1;
334 typedef const char * (asfunc
)(char, char, const char*);
336 typedef struct _table_t
344 /* Compares the key for structs that start with a char * to the key. */
346 key_cmp (const void * a
, const void * b
)
348 const char *str_a
, *str_b
;
350 str_a
= *((const char**)a
);
351 str_b
= *((const char**)b
);
352 return strcmp (str_a
, str_b
);
355 #define BUFLEN 8 /* Large enough for any keyword. */
358 const char *key
= buf
;
360 #define R_STACKABLE (0x80)
361 #define R_ARITH (0x40)
364 #define R_INDEX (R_IX | R_IY)
373 #define REG_F (6 | 8)
377 #define REG_AF (3 | R_STACKABLE)
378 #define REG_BC (0 | R_STACKABLE | R_ARITH)
379 #define REG_DE (1 | R_STACKABLE | R_ARITH)
380 #define REG_HL (2 | R_STACKABLE | R_ARITH)
381 #define REG_SP (3 | R_ARITH)
383 static const struct reg_entry
401 {"ix", REG_HL
| R_IX
},
402 {"ixh",REG_H
| R_IX
},
403 {"ixl",REG_L
| R_IX
},
404 {"iy", REG_HL
| R_IY
},
405 {"iyh",REG_H
| R_IY
},
406 {"iyl",REG_L
| R_IY
},
412 /* Prevent an error on a line from also generating
413 a "junk at end of line" error message. */
414 static char err_flag
;
417 error (const char * message
)
426 error (_("illegal operand"));
430 wrong_mach (int ins_type
)
437 p
= "undocumented instruction";
440 p
= "instruction does not work on R800";
443 p
= "instruction only works R800";
446 p
= 0; /* Not reachable. */
449 if (ins_type
& ins_err
)
456 check_mach (int ins_type
)
458 if ((ins_type
& ins_ok
) == 0)
459 wrong_mach (ins_type
);
460 ins_used
|= ins_type
;
463 /* Check whether an expression is indirect. */
465 is_indir (const char *s
)
471 /* Indirection is indicated with parentheses. */
474 for (p
= s
, depth
= 0; *p
&& *p
!= ','; ++p
)
480 for (quote
= *p
++; quote
!= *p
&& *p
!= '\n'; ++p
)
481 if (*p
== '\\' && p
[1])
491 p
= skip_space (p
+ 1);
497 error (_("mismatched parentheses"));
503 error (_("mismatched parentheses"));
508 /* Parse general expression. */
510 parse_exp2 (const char *s
, expressionS
*op
, segT
*pseg
)
515 const struct reg_entry
* regp
;
519 op
->X_md
= indir
= is_indir (p
);
521 p
= skip_space (p
+ 1);
523 for (i
= 0; i
< BUFLEN
; ++i
)
525 if (!ISALPHA (p
[i
])) /* Register names consist of letters only. */
527 buf
[i
] = TOLOWER (p
[i
]);
530 if ((i
< BUFLEN
) && ((p
[i
] == 0) || (strchr (")+-, \t", p
[i
]))))
533 regp
= bsearch (& key
, regtable
, ARRAY_SIZE (regtable
),
534 sizeof (regtable
[0]), key_cmp
);
538 op
->X_add_symbol
= op
->X_op_symbol
= 0;
539 op
->X_add_number
= regp
->number
;
540 op
->X_op
= O_register
;
541 p
+= strlen (regp
->name
);
547 if ((regp
->number
& R_INDEX
) && (regp
->number
& R_ARITH
))
551 if ((*p
== '+') || (*p
== '-'))
553 input_line_pointer
= (char*) p
;
554 expression (& offset
);
555 p
= skip_space (input_line_pointer
);
557 error (_("bad offset expression syntax"));
560 op
->X_add_symbol
= make_expr_symbol (& offset
);
564 /* We treat (i[xy]) as (i[xy]+0), which is how it will
565 end up anyway, unless we're processing jp (i[xy]). */
566 op
->X_add_symbol
= zero
;
571 if ((*p
== 0) || (*p
== ','))
575 /* Not an argument involving a register; use the generic parser. */
576 input_line_pointer
= (char*) s
;
577 *pseg
= expression (op
);
578 if (op
->X_op
== O_absent
)
579 error (_("missing operand"));
580 if (op
->X_op
== O_illegal
)
581 error (_("bad expression syntax"));
582 return input_line_pointer
;
586 parse_exp (const char *s
, expressionS
*op
)
589 return parse_exp2 (s
, op
, & dummy
);
592 /* Condition codes, including some synonyms provided by HiTech zas. */
593 static const struct reg_entry cc_tab
[] =
611 /* Parse condition code. */
613 parse_cc (const char *s
, char * op
)
617 struct reg_entry
* cc_p
;
619 for (i
= 0; i
< BUFLEN
; ++i
)
621 if (!ISALPHA (s
[i
])) /* Condition codes consist of letters only. */
623 buf
[i
] = TOLOWER (s
[i
]);
627 && ((s
[i
] == 0) || (s
[i
] == ',')))
630 cc_p
= bsearch (&key
, cc_tab
, ARRAY_SIZE (cc_tab
),
631 sizeof (cc_tab
[0]), key_cmp
);
648 emit_insn (char prefix
, char opcode
, const char * args
)
663 void z80_cons_fix_new (fragS
*frag_p
, int offset
, int nbytes
, expressionS
*exp
)
665 bfd_reloc_code_real_type r
[4] =
673 if (nbytes
< 1 || nbytes
> 4)
675 as_bad (_("unsupported BFD relocation size %u"), nbytes
);
679 fix_new_exp (frag_p
, offset
, nbytes
, exp
, 0, r
[nbytes
-1]);
684 emit_byte (expressionS
* val
, bfd_reloc_code_real_type r_type
)
691 *p
= val
->X_add_number
;
692 if ((r_type
== BFD_RELOC_8_PCREL
) && (val
->X_op
== O_constant
))
694 as_bad(_("cannot make a relative jump to an absolute location"));
696 else if (val
->X_op
== O_constant
)
699 hi
= (BFD_RELOC_8
== r_type
) ? 255 : 127;
701 if ((val
->X_add_number
< lo
) || (val
->X_add_number
> hi
))
703 if (r_type
== BFD_RELOC_Z80_DISP8
)
704 as_bad (_("offset too large"));
706 as_warn (_("overflow"));
711 fixp
= fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, 1, val
,
712 (r_type
== BFD_RELOC_8_PCREL
) ? TRUE
: FALSE
, r_type
);
713 /* FIXME : Process constant offsets immediately. */
718 emit_word (expressionS
* val
)
723 if ( (val
->X_op
== O_register
)
724 || (val
->X_op
== O_md1
))
728 *p
= val
->X_add_number
;
729 p
[1] = (val
->X_add_number
>>8);
730 if (val
->X_op
!= O_constant
)
731 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, 2,
732 val
, FALSE
, BFD_RELOC_16
);
737 emit_mx (char prefix
, char opcode
, int shift
, expressionS
* arg
)
738 /* The operand m may be r, (hl), (ix+d), (iy+d),
739 if 0 == prefix m may also be ixl, ixh, iyl, iyh. */
744 rnum
= arg
->X_add_number
;
760 if ((prefix
== 0) && (rnum
& R_INDEX
))
762 prefix
= (rnum
& R_IX
) ? 0xDD : 0xFD;
763 check_mach (INS_UNDOC
);
772 q
= frag_more (prefix
? 2 : 1);
775 * q
++ = opcode
+ (rnum
<< shift
);
779 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
780 *q
= (prefix
) ? prefix
: (opcode
+ (6 << shift
));
781 emit_byte (symbol_get_value_expression (arg
->X_add_symbol
),
782 BFD_RELOC_Z80_DISP8
);
786 *q
= opcode
+(6<<shift
);
794 /* The operand m may be r, (hl), (ix+d), (iy+d),
795 if 0 = prefix m may also be ixl, ixh, iyl, iyh. */
797 emit_m (char prefix
, char opcode
, const char *args
)
802 p
= parse_exp (args
, &arg_m
);
807 emit_mx (prefix
, opcode
, 0, &arg_m
);
815 /* The operand m may be as above or one of the undocumented
816 combinations (ix+d),r and (iy+d),r (if unportable instructions
819 emit_mr (char prefix
, char opcode
, const char *args
)
821 expressionS arg_m
, arg_r
;
824 p
= parse_exp (args
, & arg_m
);
831 p
= parse_exp (p
+ 1, & arg_r
);
833 if ((arg_r
.X_md
== 0)
834 && (arg_r
.X_op
== O_register
)
835 && (arg_r
.X_add_number
< 8))
836 opcode
+= arg_r
.X_add_number
-6; /* Emit_mx () will add 6. */
842 check_mach (INS_UNPORT
);
845 emit_mx (prefix
, opcode
, 0, & arg_m
);
854 emit_sx (char prefix
, char opcode
, expressionS
* arg_p
)
862 emit_mx (prefix
, opcode
, 0, arg_p
);
869 q
= frag_more (prefix
? 2 : 1);
873 emit_byte (arg_p
, BFD_RELOC_8
);
878 /* The operand s may be r, (hl), (ix+d), (iy+d), n. */
880 emit_s (char prefix
, char opcode
, const char *args
)
885 p
= parse_exp (args
, & arg_s
);
886 emit_sx (prefix
, opcode
, & arg_s
);
891 emit_call (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
894 const char *p
; char *q
;
896 p
= parse_exp (args
, &addr
);
908 /* Operand may be rr, r, (hl), (ix+d), (iy+d). */
910 emit_incdec (char prefix
, char opcode
, const char * args
)
914 const char *p
; char *q
;
916 p
= parse_exp (args
, &operand
);
917 rnum
= operand
.X_add_number
;
919 && (operand
.X_op
== O_register
)
922 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
924 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
925 *q
= prefix
+ ((rnum
& 3) << 4);
929 if ((operand
.X_op
== O_md1
) || (operand
.X_op
== O_register
))
930 emit_mx (0, opcode
, 3, & operand
);
938 emit_jr (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
944 p
= parse_exp (args
, &addr
);
951 emit_byte (&addr
, BFD_RELOC_8_PCREL
);
957 emit_jp (char prefix
, char opcode
, const char * args
)
964 p
= parse_exp (args
, & addr
);
967 rnum
= addr
.X_add_number
;
968 if ((addr
.X_op
== O_register
&& (rnum
& ~R_INDEX
) == REG_HL
)
969 /* An operand (i[xy]) would have been rewritten to (i[xy]+0)
971 || (addr
.X_op
== O_md1
&& addr
.X_add_symbol
== zero
))
973 q
= frag_more ((rnum
& R_INDEX
) ? 2 : 1);
975 *q
++ = (rnum
& R_IX
) ? 0xDD : 0xFD;
991 emit_im (char prefix
, char opcode
, const char * args
)
997 p
= parse_exp (args
, & mode
);
998 if (mode
.X_md
|| (mode
.X_op
!= O_constant
))
1001 switch (mode
.X_add_number
)
1005 ++mode
.X_add_number
;
1010 *q
= opcode
+ 8*mode
.X_add_number
;
1019 emit_pop (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1025 p
= parse_exp (args
, & regp
);
1027 && (regp
.X_op
== O_register
)
1028 && (regp
.X_add_number
& R_STACKABLE
))
1032 rnum
= regp
.X_add_number
;
1036 *q
++ = (rnum
&R_IX
)?0xDD:0xFD;
1040 *q
= opcode
+ ((rnum
& 3) << 4);
1049 emit_retcc (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1054 p
= parse_cc (args
, &cc
);
1060 return p
? p
: args
;
1064 emit_adc (char prefix
, char opcode
, const char * args
)
1071 p
= parse_exp (args
, &term
);
1074 error (_("bad intruction syntax"));
1078 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1081 switch (term
.X_add_number
)
1084 p
= emit_s (0, prefix
, p
);
1087 p
= parse_exp (p
, &term
);
1088 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1090 rnum
= term
.X_add_number
;
1091 if (R_ARITH
== (rnum
& (R_ARITH
| R_INDEX
)))
1095 *q
= opcode
+ ((rnum
& 3) << 4);
1107 emit_add (char prefix
, char opcode
, const char * args
)
1114 p
= parse_exp (args
, &term
);
1117 error (_("bad intruction syntax"));
1121 if ((term
.X_md
) || (term
.X_op
!= O_register
))
1124 switch (term
.X_add_number
& ~R_INDEX
)
1127 p
= emit_s (0, prefix
, p
);
1130 lhs
= term
.X_add_number
;
1131 p
= parse_exp (p
, &term
);
1132 if ((!term
.X_md
) && (term
.X_op
== O_register
))
1134 rhs
= term
.X_add_number
;
1136 && ((rhs
== lhs
) || ((rhs
& ~R_INDEX
) != REG_HL
)))
1138 q
= frag_more ((lhs
& R_INDEX
) ? 2 : 1);
1140 *q
++ = (lhs
& R_IX
) ? 0xDD : 0xFD;
1141 *q
= opcode
+ ((rhs
& 3) << 4);
1153 emit_bit (char prefix
, char opcode
, const char * args
)
1159 p
= parse_exp (args
, &b
);
1161 error (_("bad intruction syntax"));
1163 bn
= b
.X_add_number
;
1165 && (b
.X_op
== O_constant
)
1170 /* Bit : no optional third operand. */
1171 p
= emit_m (prefix
, opcode
+ (bn
<< 3), p
);
1173 /* Set, res : resulting byte can be copied to register. */
1174 p
= emit_mr (prefix
, opcode
+ (bn
<< 3), p
);
1182 emit_jpcc (char prefix
, char opcode
, const char * args
)
1187 p
= parse_cc (args
, & cc
);
1188 if (p
&& *p
++ == ',')
1189 p
= emit_call (0, opcode
+ cc
, p
);
1191 p
= (prefix
== (char)0xC3)
1192 ? emit_jp (0xE9, prefix
, args
)
1193 : emit_call (0, prefix
, args
);
1198 emit_jrcc (char prefix
, char opcode
, const char * args
)
1203 p
= parse_cc (args
, &cc
);
1204 if (p
&& *p
++ == ',')
1207 error (_("condition code invalid for jr"));
1209 p
= emit_jr (0, opcode
+ cc
, p
);
1212 p
= emit_jr (0, prefix
, args
);
1218 emit_ex (char prefix_in ATTRIBUTE_UNUSED
,
1219 char opcode_in ATTRIBUTE_UNUSED
, const char * args
)
1223 char prefix
, opcode
;
1225 p
= parse_exp (args
, &op
);
1229 error (_("bad instruction syntax"));
1233 prefix
= opcode
= 0;
1234 if (op
.X_op
== O_register
)
1235 switch (op
.X_add_number
| (op
.X_md
? 0x8000 : 0))
1238 if (TOLOWER (*p
++) == 'a' && TOLOWER (*p
++) == 'f')
1240 /* The scrubber changes '\'' to '`' in this context. */
1247 if (TOLOWER (*p
++) == 'h' && TOLOWER (*p
++) == 'l')
1251 p
= parse_exp (p
, & op
);
1252 if (op
.X_op
== O_register
1254 && (op
.X_add_number
& ~R_INDEX
) == REG_HL
)
1257 if (R_INDEX
& op
.X_add_number
)
1258 prefix
= (R_IX
& op
.X_add_number
) ? 0xDD : 0xFD;
1263 emit_insn (prefix
, opcode
, p
);
1271 emit_in (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1274 expressionS reg
, port
;
1278 p
= parse_exp (args
, ®
);
1281 error (_("bad intruction syntax"));
1285 p
= parse_exp (p
, &port
);
1287 && reg
.X_op
== O_register
1288 && (reg
.X_add_number
<= 7 || reg
.X_add_number
== REG_F
)
1291 if (port
.X_op
!= O_md1
&& port
.X_op
!= O_register
)
1293 if (REG_A
== reg
.X_add_number
)
1297 emit_byte (&port
, BFD_RELOC_8
);
1304 if (port
.X_add_number
== REG_C
)
1306 if (reg
.X_add_number
== REG_F
)
1307 check_mach (INS_UNDOC
);
1312 *q
= 0x40|((reg
.X_add_number
&7)<<3);
1325 emit_out (char prefix ATTRIBUTE_UNUSED
, char opcode ATTRIBUTE_UNUSED
,
1328 expressionS reg
, port
;
1332 p
= parse_exp (args
, & port
);
1335 error (_("bad intruction syntax"));
1338 p
= parse_exp (p
, ®
);
1340 { ill_op (); return p
; }
1341 /* Allow "out (c), 0" as unportable instruction. */
1342 if (reg
.X_op
== O_constant
&& reg
.X_add_number
== 0)
1344 check_mach (INS_UNPORT
);
1345 reg
.X_op
= O_register
;
1346 reg
.X_add_number
= 6;
1349 || reg
.X_op
!= O_register
1350 || reg
.X_add_number
> 7)
1353 if (port
.X_op
!= O_register
&& port
.X_op
!= O_md1
)
1355 if (REG_A
== reg
.X_add_number
)
1359 emit_byte (&port
, BFD_RELOC_8
);
1366 if (REG_C
== port
.X_add_number
)
1370 *q
= 0x41 | (reg
.X_add_number
<< 3);
1379 emit_rst (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1385 p
= parse_exp (args
, &addr
);
1386 if (addr
.X_op
!= O_constant
)
1388 error ("rst needs constant address");
1392 if (addr
.X_add_number
& ~(7 << 3))
1397 *q
= opcode
+ (addr
.X_add_number
& (7 << 3));
1403 emit_ldxhl (char prefix
, char opcode
, expressionS
*src
, expressionS
*d
)
1411 if (src
->X_op
== O_register
)
1413 if (src
->X_add_number
>7)
1422 *q
= opcode
+ src
->X_add_number
;
1424 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1437 emit_byte (d
, BFD_RELOC_Z80_DISP8
);
1438 emit_byte (src
, BFD_RELOC_8
);
1444 emit_ldreg (int dest
, expressionS
* src
)
1451 /* 8 Bit ld group: */
1454 if (src
->X_md
== 0 && src
->X_op
== O_register
&& src
->X_add_number
== REG_A
)
1458 *q
= (dest
== REG_I
) ? 0x47 : 0x4F;
1465 if ((src
->X_md
) && src
->X_op
!= O_register
&& src
->X_op
!= O_md1
)
1474 && src
->X_op
== O_register
1475 && (src
->X_add_number
== REG_BC
|| src
->X_add_number
== REG_DE
))
1478 *q
= 0x0A + ((dest
& 1) << 4);
1483 && src
->X_op
== O_register
1484 && (src
->X_add_number
== REG_R
|| src
->X_add_number
== REG_I
))
1488 *q
= (src
->X_add_number
== REG_I
) ? 0x57 : 0x5F;
1496 emit_sx (0, 0x40 + (dest
<< 3), src
);
1501 if ((src
->X_md
== 0)
1502 && (src
->X_op
== O_register
)
1503 && (src
->X_add_number
& R_INDEX
))
1506 emit_sx (0, 0x40 + (dest
<< 3), src
);
1518 check_mach (INS_UNDOC
);
1519 if (src
-> X_op
== O_register
)
1521 rnum
= src
->X_add_number
;
1522 if ((rnum
& ~R_INDEX
) < 8
1523 && ((rnum
& R_INDEX
) == (dest
& R_INDEX
)
1524 || ( (rnum
& ~R_INDEX
) != REG_H
1525 && (rnum
& ~R_INDEX
) != REG_L
)))
1528 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1529 *q
= 0x40 + ((dest
& 0x07) << 3) + (rnum
& 7);
1537 *q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1538 *q
= 0x06 + ((dest
& 0x07) << 3);
1539 emit_byte (src
, BFD_RELOC_8
);
1543 /* 16 Bit ld group: */
1546 && src
->X_op
== O_register
1547 && REG_HL
== (src
->X_add_number
&~ R_INDEX
))
1549 q
= frag_more ((src
->X_add_number
& R_INDEX
) ? 2 : 1);
1550 if (src
->X_add_number
& R_INDEX
)
1551 *q
++ = (src
->X_add_number
& R_IX
) ? 0xDD : 0xFD;
1558 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1560 q
= frag_more (src
->X_md
? 2 : 1);
1564 *q
= 0x4B + ((dest
& 3) << 4);
1567 *q
= 0x01 + ((dest
& 3) << 4);
1574 if (src
->X_op
== O_register
|| src
->X_op
== O_md1
)
1576 q
= frag_more ((dest
& R_INDEX
) ? 2 : 1);
1578 * q
++ = (dest
& R_IX
) ? 0xDD : 0xFD;
1579 *q
= (src
->X_md
) ? 0x2A : 0x21;
1594 emit_ld (char prefix_in ATTRIBUTE_UNUSED
, char opcode_in ATTRIBUTE_UNUSED
,
1597 expressionS dst
, src
;
1600 char prefix
, opcode
;
1602 p
= parse_exp (args
, &dst
);
1604 error (_("bad intruction syntax"));
1605 p
= parse_exp (p
, &src
);
1610 emit_ldxhl ((dst
.X_add_number
& R_IX
) ? 0xDD : 0xFD, 0x70,
1611 &src
, symbol_get_value_expression (dst
.X_add_symbol
));
1617 switch (dst
.X_add_number
)
1621 if (src
.X_md
== 0 && src
.X_op
== O_register
&& src
.X_add_number
== REG_A
)
1624 *q
= 0x02 + ( (dst
.X_add_number
& 1) << 4);
1630 emit_ldxhl (0, 0x70, &src
, NULL
);
1637 emit_ldreg (dst
.X_add_number
, &src
);
1641 if (src
.X_md
!= 0 || src
.X_op
!= O_register
)
1643 prefix
= opcode
= 0;
1644 switch (src
.X_add_number
)
1647 opcode
= 0x32; break;
1648 case REG_BC
: case REG_DE
: case REG_SP
:
1649 prefix
= 0xED; opcode
= 0x43 + ((src
.X_add_number
&3)<<4); break;
1651 opcode
= 0x22; break;
1653 prefix
= 0xDD; opcode
= 0x22; break;
1655 prefix
= 0xFD; opcode
= 0x22; break;
1659 q
= frag_more (prefix
?2:1);
1672 emit_data (int size ATTRIBUTE_UNUSED
)
1679 if (is_it_end_of_statement ())
1681 demand_empty_rest_of_line ();
1684 p
= skip_space (input_line_pointer
);
1688 if (*p
== '\"' || *p
== '\'')
1690 for (quote
= *p
, q
= ++p
, cnt
= 0; *p
&& quote
!= *p
; ++p
, ++cnt
)
1692 u
= frag_more (cnt
);
1695 as_warn (_("unterminated string"));
1697 p
= skip_space (p
+1);
1701 p
= parse_exp (p
, &exp
);
1702 if (exp
.X_op
== O_md1
|| exp
.X_op
== O_register
)
1708 as_warn (_("parentheses ignored"));
1709 emit_byte (&exp
, BFD_RELOC_8
);
1713 while (*p
++ == ',') ;
1714 input_line_pointer
= (char *)(p
-1);
1718 emit_mulub (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1722 p
= skip_space (args
);
1723 if (TOLOWER (*p
++) != 'a' || *p
++ != ',')
1729 reg
= TOLOWER (*p
++);
1736 check_mach (INS_R800
);
1737 if (!*skip_space (p
))
1741 *q
= opcode
+ ((reg
- 'b') << 3);
1752 emit_muluw (char prefix ATTRIBUTE_UNUSED
, char opcode
, const char * args
)
1756 p
= skip_space (args
);
1757 if (TOLOWER (*p
++) != 'h' || TOLOWER (*p
++) != 'l' || *p
++ != ',')
1764 p
= parse_exp (p
, & reg
);
1766 if ((!reg
.X_md
) && reg
.X_op
== O_register
)
1767 switch (reg
.X_add_number
)
1771 check_mach (INS_R800
);
1774 *q
= opcode
+ ((reg
.X_add_number
& 3) << 4);
1783 /* Port specific pseudo ops. */
1784 const pseudo_typeS md_pseudo_table
[] =
1786 { "db" , emit_data
, 1},
1789 { "def24", cons
, 3},
1790 { "def32", cons
, 4},
1791 { "defb", emit_data
, 1},
1792 { "defs", s_space
, 1}, /* Synonym for ds on some assemblers. */
1794 { "ds", s_space
, 1}, /* Fill with bytes rather than words. */
1796 { "psect", obj_coff_section
, 0}, /* TODO: Translate attributes. */
1797 { "set", 0, 0}, /* Real instruction on z80. */
1801 static table_t instab
[] =
1803 { "adc", 0x88, 0x4A, emit_adc
},
1804 { "add", 0x80, 0x09, emit_add
},
1805 { "and", 0x00, 0xA0, emit_s
},
1806 { "bit", 0xCB, 0x40, emit_bit
},
1807 { "call", 0xCD, 0xC4, emit_jpcc
},
1808 { "ccf", 0x00, 0x3F, emit_insn
},
1809 { "cp", 0x00, 0xB8, emit_s
},
1810 { "cpd", 0xED, 0xA9, emit_insn
},
1811 { "cpdr", 0xED, 0xB9, emit_insn
},
1812 { "cpi", 0xED, 0xA1, emit_insn
},
1813 { "cpir", 0xED, 0xB1, emit_insn
},
1814 { "cpl", 0x00, 0x2F, emit_insn
},
1815 { "daa", 0x00, 0x27, emit_insn
},
1816 { "dec", 0x0B, 0x05, emit_incdec
},
1817 { "di", 0x00, 0xF3, emit_insn
},
1818 { "djnz", 0x00, 0x10, emit_jr
},
1819 { "ei", 0x00, 0xFB, emit_insn
},
1820 { "ex", 0x00, 0x00, emit_ex
},
1821 { "exx", 0x00, 0xD9, emit_insn
},
1822 { "halt", 0x00, 0x76, emit_insn
},
1823 { "im", 0xED, 0x46, emit_im
},
1824 { "in", 0x00, 0x00, emit_in
},
1825 { "inc", 0x03, 0x04, emit_incdec
},
1826 { "ind", 0xED, 0xAA, emit_insn
},
1827 { "indr", 0xED, 0xBA, emit_insn
},
1828 { "ini", 0xED, 0xA2, emit_insn
},
1829 { "inir", 0xED, 0xB2, emit_insn
},
1830 { "jp", 0xC3, 0xC2, emit_jpcc
},
1831 { "jr", 0x18, 0x20, emit_jrcc
},
1832 { "ld", 0x00, 0x00, emit_ld
},
1833 { "ldd", 0xED, 0xA8, emit_insn
},
1834 { "lddr", 0xED, 0xB8, emit_insn
},
1835 { "ldi", 0xED, 0xA0, emit_insn
},
1836 { "ldir", 0xED, 0xB0, emit_insn
},
1837 { "mulub", 0xED, 0xC5, emit_mulub
}, /* R800 only. */
1838 { "muluw", 0xED, 0xC3, emit_muluw
}, /* R800 only. */
1839 { "neg", 0xed, 0x44, emit_insn
},
1840 { "nop", 0x00, 0x00, emit_insn
},
1841 { "or", 0x00, 0xB0, emit_s
},
1842 { "otdr", 0xED, 0xBB, emit_insn
},
1843 { "otir", 0xED, 0xB3, emit_insn
},
1844 { "out", 0x00, 0x00, emit_out
},
1845 { "outd", 0xED, 0xAB, emit_insn
},
1846 { "outi", 0xED, 0xA3, emit_insn
},
1847 { "pop", 0x00, 0xC1, emit_pop
},
1848 { "push", 0x00, 0xC5, emit_pop
},
1849 { "res", 0xCB, 0x80, emit_bit
},
1850 { "ret", 0xC9, 0xC0, emit_retcc
},
1851 { "reti", 0xED, 0x4D, emit_insn
},
1852 { "retn", 0xED, 0x45, emit_insn
},
1853 { "rl", 0xCB, 0x10, emit_mr
},
1854 { "rla", 0x00, 0x17, emit_insn
},
1855 { "rlc", 0xCB, 0x00, emit_mr
},
1856 { "rlca", 0x00, 0x07, emit_insn
},
1857 { "rld", 0xED, 0x6F, emit_insn
},
1858 { "rr", 0xCB, 0x18, emit_mr
},
1859 { "rra", 0x00, 0x1F, emit_insn
},
1860 { "rrc", 0xCB, 0x08, emit_mr
},
1861 { "rrca", 0x00, 0x0F, emit_insn
},
1862 { "rrd", 0xED, 0x67, emit_insn
},
1863 { "rst", 0x00, 0xC7, emit_rst
},
1864 { "sbc", 0x98, 0x42, emit_adc
},
1865 { "scf", 0x00, 0x37, emit_insn
},
1866 { "set", 0xCB, 0xC0, emit_bit
},
1867 { "sla", 0xCB, 0x20, emit_mr
},
1868 { "sli", 0xCB, 0x30, emit_mr
},
1869 { "sll", 0xCB, 0x30, emit_mr
},
1870 { "sra", 0xCB, 0x28, emit_mr
},
1871 { "srl", 0xCB, 0x38, emit_mr
},
1872 { "sub", 0x00, 0x90, emit_s
},
1873 { "xor", 0x00, 0xA8, emit_s
},
1877 md_assemble (char* str
)
1885 old_ptr
= input_line_pointer
;
1886 p
= skip_space (str
);
1887 for (i
= 0; (i
< BUFLEN
) && (ISALPHA (*p
));)
1888 buf
[i
++] = TOLOWER (*p
++);
1892 buf
[BUFLEN
-3] = buf
[BUFLEN
-2] = '.'; /* Mark opcode as abbreviated. */
1894 as_bad (_("Unknown instruction '%s'"), buf
);
1896 else if ((*p
) && (!ISSPACE (*p
)))
1897 as_bad (_("syntax error"));
1904 insp
= bsearch (&key
, instab
, ARRAY_SIZE (instab
),
1905 sizeof (instab
[0]), key_cmp
);
1907 as_bad (_("Unknown instruction '%s'"), buf
);
1910 p
= insp
->fp (insp
->prefix
, insp
->opcode
, p
);
1912 if ((!err_flag
) && *p
)
1913 as_bad (_("junk at end of line, first unrecognized character is `%c'"),
1917 input_line_pointer
= old_ptr
;
1921 md_apply_fix (fixS
* fixP
, valueT
* valP
, segT seg ATTRIBUTE_UNUSED
)
1923 long val
= * (long *) valP
;
1924 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1926 switch (fixP
->fx_r_type
)
1928 case BFD_RELOC_8_PCREL
:
1931 fixP
->fx_no_overflow
= 1;
1936 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1937 if (!fixP
->fx_no_overflow
)
1938 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1939 _("relative jump out of range"));
1945 case BFD_RELOC_Z80_DISP8
:
1948 fixP
->fx_no_overflow
= 1;
1953 fixP
->fx_no_overflow
= (-128 <= val
&& val
< 128);
1954 if (!fixP
->fx_no_overflow
)
1955 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1956 _("index offset out of range"));
1963 if (val
> 255 || val
< -128)
1964 as_warn_where (fixP
->fx_file
, fixP
->fx_line
, _("overflow"));
1966 fixP
->fx_no_overflow
= 1;
1967 if (fixP
->fx_addsy
== NULL
)
1973 *buf
++ = (val
>> 8);
1974 fixP
->fx_no_overflow
= 1;
1975 if (fixP
->fx_addsy
== NULL
)
1979 case BFD_RELOC_24
: /* Def24 may produce this. */
1981 *buf
++ = (val
>> 8);
1982 *buf
++ = (val
>> 16);
1983 fixP
->fx_no_overflow
= 1;
1984 if (fixP
->fx_addsy
== NULL
)
1988 case BFD_RELOC_32
: /* Def32 and .long may produce this. */
1990 *buf
++ = (val
>> 8);
1991 *buf
++ = (val
>> 16);
1992 *buf
++ = (val
>> 24);
1993 if (fixP
->fx_addsy
== NULL
)
1998 printf (_("md_apply_fix: unknown r_type 0x%x\n"), fixP
->fx_r_type
);
2003 /* GAS will call this to generate a reloc. GAS will pass the
2004 resulting reloc to `bfd_install_relocation'. This currently works
2005 poorly, as `bfd_install_relocation' often does the wrong thing, and
2006 instances of `tc_gen_reloc' have been written to work around the
2007 problems, which in turns makes it difficult to fix
2008 `bfd_install_relocation'. */
2010 /* If while processing a fixup, a reloc really
2011 needs to be created then it is done here. */
2014 tc_gen_reloc (asection
*seg ATTRIBUTE_UNUSED
, fixS
*fixp
)
2018 if (! bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
))
2020 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2021 _("reloc %d not supported by object file format"),
2022 (int) fixp
->fx_r_type
);
2026 reloc
= xmalloc (sizeof (arelent
));
2027 reloc
->sym_ptr_ptr
= xmalloc (sizeof (asymbol
*));
2028 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2029 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2030 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2031 reloc
->addend
= fixp
->fx_offset
;