1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001
3 Free Software Foundation, Inc.
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, 59 Temple Place - Suite 330, Boston, MA
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
27 #include "opcodes/z8k-opc.h"
31 #include "safe-ctype.h"
33 const char comment_chars[] = "!";
34 const char line_comment_chars[] = "#";
35 const char line_separator_chars[] = ";";
38 extern int coff_flags;
40 const int md_reloc_size;
48 machine = bfd_mach_z8001;
56 machine = bfd_mach_z8002;
64 record_alignment (now_seg, 1);
67 void obj_coff_section ();
84 if (*input_line_pointer == '\'')
88 c = *input_line_pointer++;
93 c = (tohex (input_line_pointer[0]) << 4)
94 | tohex (input_line_pointer[1]);
95 input_line_pointer += 2;
97 FRAG_APPEND_1_CHAR (c);
98 c = *input_line_pointer++;
100 demand_empty_rest_of_line ();
104 /* This table describes all the machine specific pseudo-ops the assembler
105 has to support. The fields are:
106 pseudo-op name without dot
107 function to call to execute this pseudo-op
108 Integer arg to pass to the function
111 const pseudo_typeS md_pseudo_table[] = {
113 {"data.b" , cons , 1},
114 {"data.w" , cons , 2},
115 {"data.l" , cons , 4},
116 {"form" , listing_psize , 0},
117 {"heading", listing_title , 0},
118 {"import" , s_ignore , 0},
119 {"page" , listing_eject , 0},
120 {"program", s_ignore , 0},
121 {"z8001" , s_segm , 0},
122 {"z8002" , s_unseg , 0},
124 {"segm" , s_segm , 0},
125 {"unsegm" , s_unseg , 0},
126 {"unseg" , s_unseg , 0},
127 {"name" , s_app_file , 0},
128 {"global" , s_globl , 0},
133 {"rsect" , obj_coff_section, 0},
134 {"sect" , obj_coff_section, 0},
135 {"block" , s_space , 0},
140 const char EXP_CHARS[] = "eE";
142 /* Chars that mean this number is a floating point constant.
145 const char FLT_CHARS[] = "rRsSfFdDxXpP";
147 /* Opcode mnemonics. */
148 static struct hash_control *opcode_hash_control;
153 opcode_entry_type *opcode;
154 char *prev_name = "";
157 opcode_hash_control = hash_new ();
159 for (opcode = z8k_table; opcode->name; opcode++)
161 /* Only enter unique codes into the table. */
162 if (strcmp (opcode->name, prev_name))
164 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
168 prev_name = opcode->name;
171 /* Default to z8002. */
174 /* Insert the pseudo ops, too. */
175 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
177 opcode_entry_type *fake_opcode;
178 fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
179 fake_opcode->name = md_pseudo_table[idx].poc_name;
180 fake_opcode->func = (void *) (md_pseudo_table + idx);
181 fake_opcode->opcode = 250;
182 hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
194 typedef struct z8k_op {
195 /* 'b','w','r','q'. */
203 /* Any other register associated with the mode. */
206 /* Any expression. */
210 static expressionS *da_operand;
211 static expressionS *imm_operand;
224 if (ISDIGIT (src[1]))
226 *reg = (src[0] - '0') * 10 + src[1] - '0';
231 *reg = (src[0] - '0');
242 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
250 /* Try to parse a reg name. Return a pointer to the first character
251 in SRC after the reg name. */
254 parse_reg (src, mode, reg)
262 if (src[0] == 's' && src[1] == 'p' && (src[2] == 0 || src[2] == ','))
266 *mode = CLASS_REG_LONG;
271 *mode = CLASS_REG_WORD;
280 if (src[2] < '0' || src[2] > '9')
281 return res; /* Assume no register name but a label starting with 'rr'. */
282 *mode = CLASS_REG_LONG;
283 res = whatreg (reg, src + 2);
286 as_warn (_("register rr%d, out of range."), regno);
288 else if (src[1] == 'h')
290 if (src[2] < '0' || src[2] > '9')
291 return res; /* Assume no register name but a label starting with 'rh'. */
292 *mode = CLASS_REG_BYTE;
293 res = whatreg (reg, src + 2);
296 as_warn (_("register rh%d, out of range."), regno);
298 else if (src[1] == 'l')
300 if (src[2] < '0' || src[2] > '9')
301 return res; /* Assume no register name but a label starting with 'rl'. */
302 *mode = CLASS_REG_BYTE;
303 res = whatreg (reg, src + 2);
306 as_warn (_("register rl%d, out of range."), regno);
309 else if (src[1] == 'q')
311 if (src[2] < '0' || src[2] > '9')
312 return res; /* Assume no register name but a label starting with 'rq'. */
313 *mode = CLASS_REG_QUAD;
314 res = whatreg (reg, src + 2);
317 as_warn (_("register rq%d, out of range."), regno);
321 if (src[1] < '0' || src[1] > '9')
322 return res; /* Assume no register name but a label starting with 'r'. */
323 *mode = CLASS_REG_WORD;
324 res = whatreg (reg, src + 1);
327 as_warn (_("register r%d, out of range."), regno);
338 char *save = input_line_pointer;
341 input_line_pointer = s;
343 if (op->X_op == O_absent)
344 as_bad (_("missing operand"));
345 new = input_line_pointer;
346 input_line_pointer = save;
350 /* The many forms of operand:
372 as_bad (_("expected %c"), what);
377 /* Make sure the mode supplied is the size of a word. */
380 regword (mode, string)
389 as_bad (_("register is wrong size for a word %s"), string);
393 /* Make sure the mode supplied is the size of an address. */
396 regaddr (mode, string)
402 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
405 as_bad (_("register is wrong size for address %s"), string);
414 struct ctrl_names ctrl_table[] = {
427 get_ctrl_operand (ptr, mode, dst)
430 unsigned int dst ATTRIBUTE_UNUSED;
438 mode->mode = CLASS_CTRL;
439 for (i = 0; ctrl_table[i].name; i++)
443 for (j = 0; ctrl_table[i].name[j]; j++)
445 if (ctrl_table[i].name[j] != src[j])
448 the_ctrl = ctrl_table[i].value;
464 struct flag_names flag_table[] = {
475 get_flags_operand (ptr, mode, dst)
478 unsigned int dst ATTRIBUTE_UNUSED;
487 mode->mode = CLASS_FLAGS;
489 for (j = 0; j <= 9; j++)
493 for (i = 0; flag_table[i].name; i++)
495 if (flag_table[i].name[0] == src[j])
497 the_flags = the_flags | flag_table[i].value;
510 struct interrupt_names {
516 struct interrupt_names intr_table[] = {
525 get_interrupt_operand (ptr, mode, dst)
528 unsigned int dst ATTRIBUTE_UNUSED;
536 mode->mode = CLASS_IMM;
537 for (i = 0; intr_table[i].name; i++)
541 for (j = 0; intr_table[i].name[j]; j++)
543 if (intr_table[i].name[j] != src[j])
546 the_interrupt = intr_table[i].value;
562 struct cc_names table[] = {
589 get_cc_operand (ptr, mode, dst)
592 unsigned int dst ATTRIBUTE_UNUSED;
600 mode->mode = CLASS_CC;
601 for (i = 0; table[i].name; i++)
605 for (j = 0; table[i].name[j]; j++)
607 if (table[i].name[j] != src[j])
610 the_cc = table[i].value;
620 get_operand (ptr, mode, dst)
623 unsigned int dst ATTRIBUTE_UNUSED;
634 mode->mode = CLASS_IMM;
635 imm_operand = &(mode->exp);
636 src = parse_exp (src + 1, &(mode->exp));
638 else if (*src == '@')
642 mode->mode = CLASS_IR;
643 src = parse_reg (src + 1, &d, &mode->reg);
649 end = parse_reg (src, &mode->mode, ®n);
659 end = parse_reg (src, &nw, &nr);
666 as_bad (_("Missing ) in ra(rb)"));
670 regaddr (mode->mode, "ra(rb) ra");
672 regword (mode->mode, "ra(rb) rb");
674 mode->mode = CLASS_BX;
684 src = parse_exp (src, &(mode->exp));
685 src = checkfor (src, ')');
686 mode->mode = CLASS_BA;
689 imm_operand = &(mode->exp);
700 /* No initial reg. */
701 src = parse_exp (src, &(mode->exp));
705 end = parse_reg (src, &(mode->mode), ®n);
706 regword (mode->mode, "addr(Ra) ra");
707 mode->mode = CLASS_X;
710 da_operand = &(mode->exp);
711 src = checkfor (end, ')');
715 /* Just an address. */
716 mode->mode = CLASS_DA;
719 da_operand = &(mode->exp);
727 get_operands (opcode, op_end, operand)
728 opcode_entry_type *opcode;
735 switch (opcode->noperands)
744 if (opcode->arg_info[0] == CLASS_CC)
746 get_cc_operand (&ptr, operand + 0, 0);
748 else if (opcode->arg_info[0] == CLASS_FLAGS)
750 get_flags_operand (&ptr, operand + 0, 0);
752 else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2)))
754 get_interrupt_operand (&ptr, operand + 0, 0);
758 get_operand (&ptr, operand + 0, 0);
766 if (opcode->arg_info[0] == CLASS_CC)
768 get_cc_operand (&ptr, operand + 0, 0);
770 else if (opcode->arg_info[0] == CLASS_CTRL)
772 get_ctrl_operand (&ptr, operand + 0, 0);
776 get_operand (&ptr, operand + 0, 0);
781 get_ctrl_operand (&ptr, operand + 1, 1);
787 get_operand (&ptr, operand + 0, 0);
793 get_operand (&ptr, operand + 1, 1);
798 get_operand (&ptr, operand + 0, 0);
801 get_operand (&ptr, operand + 1, 1);
804 get_operand (&ptr, operand + 2, 2);
809 get_operand (&ptr, operand + 0, 0);
812 get_operand (&ptr, operand + 1, 1);
815 get_operand (&ptr, operand + 2, 2);
818 get_cc_operand (&ptr, operand + 3, 3);
828 /* Passed a pointer to a list of opcodes which use different
829 addressing modes. Return the opcode which matches the opcodes
832 static opcode_entry_type *
833 get_specific (opcode, operands)
834 opcode_entry_type *opcode;
838 opcode_entry_type *this_try = opcode;
840 unsigned int noperands = opcode->noperands;
842 int this_index = opcode->idx;
844 while (this_index == opcode->idx && !found)
849 for (i = 0; i < noperands; i++)
851 unsigned int mode = operands[i].mode;
853 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
855 /* It could be an pc rel operand, if this is a da mode
856 and we like disps, then insert it. */
858 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
860 /* This is the case. */
861 operands[i].mode = CLASS_DISP;
863 else if (mode == CLASS_BA && this_try->arg_info[i])
865 /* Can't think of a way to turn what we've been
866 given into something that's OK. */
869 else if (this_try->arg_info[i] & CLASS_PR)
871 if (mode == CLASS_REG_LONG && segmented_mode)
875 else if (mode == CLASS_REG_WORD && !segmented_mode)
885 switch (mode & CLASS_MASK)
900 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
915 #if 0 /* Not used. */
917 check_operand (operand, width, string)
918 struct z8k_op *operand;
922 if (operand->exp.X_add_symbol == 0
923 && operand->exp.X_op_symbol == 0)
926 /* No symbol involved, let's look at offset, it's dangerous if
927 any of the high bits are not 0 or ff's, find out by oring or
928 anding with the width and seeing if the answer is 0 or all
930 if ((operand->exp.X_add_number & ~width) != 0 &&
931 (operand->exp.X_add_number | width) != (~0))
933 as_warn (_("operand %s0x%x out of range."),
934 string, operand->exp.X_add_number);
941 static char buffer[20];
944 newfix (ptr, type, operand)
947 expressionS *operand;
949 if (operand->X_add_symbol
950 || operand->X_op_symbol
951 || operand->X_add_number)
953 fix_new_exp (frag_now,
963 apply_fix (ptr, type, operand, size)
966 expressionS *operand;
969 int n = operand->X_add_number;
971 newfix ((ptr - buffer) / 2, type, operand);
974 case 8: /* 8 nibbles == 32 bits. */
979 case 4: /* 4 nibbles == 16 bits. */
991 /* Now we know what sort of opcodes it is. Let's build the bytes. */
993 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
996 build_bytes (this_try, operand)
997 opcode_entry_type *this_try;
998 struct z8k_op *operand ATTRIBUTE_UNUSED;
1000 char *output_ptr = buffer;
1004 unsigned int *class_ptr;
1006 frag_wane (frag_now);
1009 memset (buffer, 20, 0);
1010 class_ptr = this_try->byte_info;
1012 for (nibble = 0; (c = *class_ptr++); nibble++)
1015 switch (c & CLASS_MASK)
1021 /* Direct address, we don't cope with the SS mode right now. */
1024 /* da_operand->X_add_number |= 0x80000000; -- Now set at relocation time. */
1025 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1029 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1035 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1042 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1049 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1050 output_ptr[-2] = 0x8;
1054 case CLASS_BIT_1OR2:
1055 *output_ptr = c & 0xf;
1058 if (imm_operand->X_add_number == 2)
1060 else if (imm_operand->X_add_number != 1)
1061 as_bad (_("immediate must be 1 or 2"));
1064 as_bad (_("immediate 1 or 2 expected"));
1068 *output_ptr++ = the_cc;
1071 *output_ptr++ = the_ctrl;
1074 *output_ptr++ = the_ctrl | 0x8;
1077 *output_ptr++ = (~the_interrupt & 0x3);
1080 *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1083 *output_ptr++ = the_flags;
1086 *output_ptr++ = c & 0xf;
1089 if (reg[c & 0xf] == 0)
1090 as_bad (_("can't use R0 here"));
1093 case CLASS_REG_BYTE:
1094 case CLASS_REG_WORD:
1095 case CLASS_REG_LONG:
1096 case CLASS_REG_QUAD:
1097 /* Insert bit mattern of right reg. */
1098 *output_ptr++ = reg[c & 0xf];
1101 switch (c & ARG_MASK)
1104 output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4);
1107 output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4);
1110 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1118 switch (c & ARG_MASK)
1121 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1124 imm_operand->X_add_number--;
1125 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1127 case ARG_IMMNMINUS1:
1128 imm_operand->X_add_number--;
1129 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1132 imm_operand->X_add_number = -imm_operand->X_add_number;
1134 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1137 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1141 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1151 /* Copy from the nibble buffer into the frag. */
1153 int length = (output_ptr - buffer) / 2;
1155 char *fragp = frag_more (length);
1157 while (src < output_ptr)
1159 *fragp = (src[0] << 4) | src[1];
1166 /* This is the guts of the machine-dependent assembler. STR points to a
1167 machine dependent instruction. This function is supposed to emit
1168 the frags/bytes it assembles to. */
1177 struct z8k_op operand[3];
1178 opcode_entry_type *opcode;
1179 opcode_entry_type *prev_opcode;
1181 /* Drop leading whitespace. */
1185 /* Find the op code end. */
1186 for (op_start = op_end = str;
1187 *op_end != 0 && *op_end != ' ';
1191 if (op_end == op_start)
1193 as_bad (_("can't find opcode "));
1199 opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start);
1203 as_bad (_("unknown opcode"));
1207 if (opcode->opcode == 250)
1209 /* Was really a pseudo op. */
1214 char *old = input_line_pointer;
1217 input_line_pointer = op_end;
1221 while (*input_line_pointer == ' ')
1222 input_line_pointer++;
1223 p = (pseudo_typeS *) (opcode->func);
1225 (p->poc_handler) (p->poc_val);
1226 input_line_pointer = old;
1231 input_line_pointer = get_operands (opcode, op_end, operand);
1232 prev_opcode = opcode;
1234 opcode = get_specific (opcode, operand);
1238 /* Couldn't find an opcode which matched the operands. */
1239 char *where = frag_more (2);
1244 as_bad (_("Can't find opcode to match operands"));
1248 build_bytes (opcode, operand);
1253 tc_crawl_symbol_chain (headers)
1254 object_headers *headers ATTRIBUTE_UNUSED;
1256 printf (_("call to tc_crawl_symbol_chain \n"));
1260 md_undefined_symbol (name)
1261 char *name ATTRIBUTE_UNUSED;
1267 tc_headers_hook (headers)
1268 object_headers *headers ATTRIBUTE_UNUSED;
1270 printf (_("call to tc_headers_hook \n"));
1273 /* Various routines to kill one day. */
1274 /* Equal to MAX_PRECISION in atof-ieee.c. */
1275 #define MAX_LITTLENUMS 6
1277 /* Turn a string in input_line_pointer into a floating point constant
1278 of type TYPE, and store the appropriate bytes in *LITP. The number
1279 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1280 returned, or NULL on OK. */
1283 md_atof (type, litP, sizeP)
1289 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1290 LITTLENUM_TYPE *wordP;
1322 return _("Bad call to MD_ATOF()");
1324 t = atof_ieee (input_line_pointer, type, words);
1326 input_line_pointer = t;
1328 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1329 for (wordP = words; prec--;)
1331 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1332 litP += sizeof (LITTLENUM_TYPE);
1337 CONST char *md_shortopts = "z:";
1339 struct option md_longopts[] = {
1340 {NULL, no_argument, NULL, 0}
1343 size_t md_longopts_size = sizeof (md_longopts);
1346 md_parse_option (c, arg)
1353 if (!strcmp (arg, "8001"))
1355 else if (!strcmp (arg, "8002"))
1359 as_bad (_("invalid architecture -z%s"), arg);
1372 md_show_usage (stream)
1375 fprintf (stream, _("\
1377 -z8001 generate segmented code\n\
1378 -z8002 generate unsegmented code\n"));
1382 tc_aout_fix_to_chars ()
1384 printf (_("call to tc_aout_fix_to_chars \n"));
1389 md_convert_frag (headers, seg, fragP)
1390 object_headers *headers ATTRIBUTE_UNUSED;
1391 segT seg ATTRIBUTE_UNUSED;
1392 fragS *fragP ATTRIBUTE_UNUSED;
1394 printf (_("call to md_convert_frag \n"));
1399 md_section_align (seg, size)
1403 return ((size + (1 << section_alignment[(int) seg]) - 1)
1404 & (-1 << section_alignment[(int) seg]));
1409 md_apply_fix (fixP, val)
1413 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1415 switch (fixP->fx_r_type)
1418 buf[0] = (buf[0] & 0xf0) | ((buf[0] + val) & 0xf);
1443 *buf++ = (val >> 8);
1447 *buf++ = (val >> 24);
1448 *buf++ = (val >> 16);
1449 *buf++ = (val >> 8);
1454 *buf++ = (val >> 16);
1456 *buf++ = (val >> 8);
1462 md_number_to_chars (buf, val, fixP->fx_size);
1471 md_estimate_size_before_relax (fragP, segment_type)
1472 register fragS *fragP ATTRIBUTE_UNUSED;
1473 register segT segment_type ATTRIBUTE_UNUSED;
1475 printf (_("call tomd_estimate_size_before_relax \n"));
1479 /* Put number into target byte order. */
1482 md_number_to_chars (ptr, use, nbytes)
1487 number_to_chars_bigendian (ptr, use, nbytes);
1491 md_pcrel_from (fixP)
1492 fixS *fixP ATTRIBUTE_UNUSED;
1498 tc_coff_symbol_emit_hook (s)
1499 symbolS *s ATTRIBUTE_UNUSED;
1504 tc_reloc_mangle (fix_ptr, intr, base)
1506 struct internal_reloc *intr;
1510 symbolS *symbol_ptr;
1512 if (fix_ptr->fx_addsy
1513 && fix_ptr->fx_subsy)
1515 symbolS *add = fix_ptr->fx_addsy;
1516 symbolS *sub = fix_ptr->fx_subsy;
1518 if (S_GET_SEGMENT (add) != S_GET_SEGMENT (sub))
1519 as_bad (_("Can't subtract symbols in different sections %s %s"),
1520 S_GET_NAME (add), S_GET_NAME (sub));
1523 int diff = S_GET_VALUE (add) - S_GET_VALUE (sub);
1525 fix_ptr->fx_addsy = 0;
1526 fix_ptr->fx_subsy = 0;
1527 fix_ptr->fx_offset += diff;
1530 symbol_ptr = fix_ptr->fx_addsy;
1532 /* If this relocation is attached to a symbol then it's ok
1534 if (fix_ptr->fx_r_type == 0)
1536 /* cons likes to create reloc32's whatever the size of the reloc. */
1537 switch (fix_ptr->fx_size)
1540 intr->r_type = R_IMM16;
1543 intr->r_type = R_IMM8;
1546 intr->r_type = R_IMM32;
1553 intr->r_type = fix_ptr->fx_r_type;
1555 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1556 intr->r_offset = fix_ptr->fx_offset;
1559 intr->r_symndx = symbol_ptr->sy_number;
1561 intr->r_symndx = -1;