1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 2000
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"
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')
266 *mode = CLASS_REG_LONG;
271 *mode = CLASS_REG_WORD;
280 *mode = CLASS_REG_LONG;
281 res = whatreg (reg, src + 2);
284 as_warn (_("register rr%d, out of range."), regno);
286 else if (src[1] == 'h')
288 *mode = CLASS_REG_BYTE;
289 res = whatreg (reg, src + 2);
292 as_warn (_("register rh%d, out of range."), regno);
294 else if (src[1] == 'l')
296 *mode = CLASS_REG_BYTE;
297 res = whatreg (reg, src + 2);
300 as_warn (_("register rl%d, out of range."), regno);
303 else if (src[1] == 'q')
305 *mode = CLASS_REG_QUAD;
306 res = whatreg (reg, src + 2);
309 as_warn (_("register rq%d, out of range."), regno);
313 *mode = CLASS_REG_WORD;
314 res = whatreg (reg, src + 1);
317 as_warn (_("register r%d, out of range."), regno);
328 char *save = input_line_pointer;
331 input_line_pointer = s;
333 if (op->X_op == O_absent)
334 as_bad (_("missing operand"));
335 new = input_line_pointer;
336 input_line_pointer = save;
340 /* The many forms of operand:
362 as_bad (_("expected %c"), what);
367 /* Make sure the mode supplied is the size of a word. */
370 regword (mode, string)
379 as_bad (_("register is wrong size for a word %s"), string);
383 /* Make sure the mode supplied is the size of an address. */
386 regaddr (mode, string)
392 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
395 as_bad (_("register is wrong size for address %s"), string);
404 struct ctrl_names ctrl_table[] = {
417 get_ctrl_operand (ptr, mode, dst)
420 unsigned int dst ATTRIBUTE_UNUSED;
428 mode->mode = CLASS_CTRL;
429 for (i = 0; ctrl_table[i].name; i++)
433 for (j = 0; ctrl_table[i].name[j]; j++)
435 if (ctrl_table[i].name[j] != src[j])
438 the_ctrl = ctrl_table[i].value;
454 struct flag_names flag_table[] = {
465 get_flags_operand (ptr, mode, dst)
468 unsigned int dst ATTRIBUTE_UNUSED;
477 mode->mode = CLASS_FLAGS;
479 for (j = 0; j <= 9; j++)
483 for (i = 0; flag_table[i].name; i++)
485 if (flag_table[i].name[0] == src[j])
487 the_flags = the_flags | flag_table[i].value;
500 struct interrupt_names {
506 struct interrupt_names intr_table[] = {
515 get_interrupt_operand (ptr, mode, dst)
518 unsigned int dst ATTRIBUTE_UNUSED;
526 mode->mode = CLASS_IMM;
527 for (i = 0; intr_table[i].name; i++)
531 for (j = 0; intr_table[i].name[j]; j++)
533 if (intr_table[i].name[j] != src[j])
536 the_interrupt = intr_table[i].value;
552 struct cc_names table[] = {
579 get_cc_operand (ptr, mode, dst)
582 unsigned int dst ATTRIBUTE_UNUSED;
590 mode->mode = CLASS_CC;
591 for (i = 0; table[i].name; i++)
595 for (j = 0; table[i].name[j]; j++)
597 if (table[i].name[j] != src[j])
600 the_cc = table[i].value;
610 get_operand (ptr, mode, dst)
613 unsigned int dst ATTRIBUTE_UNUSED;
624 mode->mode = CLASS_IMM;
625 imm_operand = &(mode->exp);
626 src = parse_exp (src + 1, &(mode->exp));
628 else if (*src == '@')
632 mode->mode = CLASS_IR;
633 src = parse_reg (src + 1, &d, &mode->reg);
639 end = parse_reg (src, &mode->mode, ®n);
649 end = parse_reg (src, &nw, &nr);
656 as_bad (_("Missing ) in ra(rb)"));
660 regaddr (mode->mode, "ra(rb) ra");
662 regword (mode->mode, "ra(rb) rb");
664 mode->mode = CLASS_BX;
674 src = parse_exp (src, &(mode->exp));
675 src = checkfor (src, ')');
676 mode->mode = CLASS_BA;
679 imm_operand = &(mode->exp);
690 /* No initial reg. */
691 src = parse_exp (src, &(mode->exp));
695 end = parse_reg (src, &(mode->mode), ®n);
696 regword (mode->mode, "addr(Ra) ra");
697 mode->mode = CLASS_X;
700 da_operand = &(mode->exp);
701 src = checkfor (end, ')');
705 /* Just an address. */
706 mode->mode = CLASS_DA;
709 da_operand = &(mode->exp);
717 get_operands (opcode, op_end, operand)
718 opcode_entry_type *opcode;
725 switch (opcode->noperands)
734 if (opcode->arg_info[0] == CLASS_CC)
736 get_cc_operand (&ptr, operand + 0, 0);
738 else if (opcode->arg_info[0] == CLASS_FLAGS)
740 get_flags_operand (&ptr, operand + 0, 0);
742 else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2)))
744 get_interrupt_operand (&ptr, operand + 0, 0);
748 get_operand (&ptr, operand + 0, 0);
756 if (opcode->arg_info[0] == CLASS_CC)
758 get_cc_operand (&ptr, operand + 0, 0);
760 else if (opcode->arg_info[0] == CLASS_CTRL)
762 get_ctrl_operand (&ptr, operand + 0, 0);
766 get_operand (&ptr, operand + 0, 0);
771 get_ctrl_operand (&ptr, operand + 1, 1);
777 get_operand (&ptr, operand + 0, 0);
783 get_operand (&ptr, operand + 1, 1);
788 get_operand (&ptr, operand + 0, 0);
791 get_operand (&ptr, operand + 1, 1);
794 get_operand (&ptr, operand + 2, 2);
799 get_operand (&ptr, operand + 0, 0);
802 get_operand (&ptr, operand + 1, 1);
805 get_operand (&ptr, operand + 2, 2);
808 get_cc_operand (&ptr, operand + 3, 3);
818 /* Passed a pointer to a list of opcodes which use different
819 addressing modes. Return the opcode which matches the opcodes
822 static opcode_entry_type *
823 get_specific (opcode, operands)
824 opcode_entry_type *opcode;
828 opcode_entry_type *this_try = opcode;
830 unsigned int noperands = opcode->noperands;
832 int this_index = opcode->idx;
834 while (this_index == opcode->idx && !found)
839 for (i = 0; i < noperands; i++)
841 unsigned int mode = operands[i].mode;
843 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
845 /* It could be an pc rel operand, if this is a da mode
846 and we like disps, then insert it. */
848 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
850 /* This is the case. */
851 operands[i].mode = CLASS_DISP;
853 else if (mode == CLASS_BA && this_try->arg_info[i])
855 /* Can't think of a way to turn what we've been
856 given into something that's OK. */
859 else if (this_try->arg_info[i] & CLASS_PR)
861 if (mode == CLASS_REG_LONG && segmented_mode)
865 else if (mode == CLASS_REG_WORD && !segmented_mode)
875 switch (mode & CLASS_MASK)
890 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
905 #if 0 /* Not used. */
907 check_operand (operand, width, string)
908 struct z8k_op *operand;
912 if (operand->exp.X_add_symbol == 0
913 && operand->exp.X_op_symbol == 0)
916 /* No symbol involved, let's look at offset, it's dangerous if
917 any of the high bits are not 0 or ff's, find out by oring or
918 anding with the width and seeing if the answer is 0 or all
920 if ((operand->exp.X_add_number & ~width) != 0 &&
921 (operand->exp.X_add_number | width) != (~0))
923 as_warn (_("operand %s0x%x out of range."),
924 string, operand->exp.X_add_number);
931 static char buffer[20];
934 newfix (ptr, type, operand)
937 expressionS *operand;
939 if (operand->X_add_symbol
940 || operand->X_op_symbol
941 || operand->X_add_number)
943 fix_new_exp (frag_now,
953 apply_fix (ptr, type, operand, size)
956 expressionS *operand;
959 int n = operand->X_add_number;
961 newfix ((ptr - buffer) / 2, type, operand);
964 case 8: /* 8 nibbles == 32 bits. */
969 case 4: /* 4 nibbles == 16 bits. */
981 /* Now we know what sort of opcodes it is. Let's build the bytes. */
983 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
986 build_bytes (this_try, operand)
987 opcode_entry_type *this_try;
988 struct z8k_op *operand ATTRIBUTE_UNUSED;
990 char *output_ptr = buffer;
994 unsigned int *class_ptr;
996 frag_wane (frag_now);
999 memset (buffer, 20, 0);
1000 class_ptr = this_try->byte_info;
1002 for (nibble = 0; (c = *class_ptr++); nibble++)
1005 switch (c & CLASS_MASK)
1011 /* Direct address, we don't cope with the SS mode right now. */
1014 /* da_operand->X_add_number |= 0x80000000; -- Now set at relocation time. */
1015 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1019 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1025 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1032 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1039 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1040 output_ptr[-2] = 0x8;
1044 case CLASS_BIT_1OR2:
1045 *output_ptr = c & 0xf;
1048 if (imm_operand->X_add_number == 2)
1050 else if (imm_operand->X_add_number != 1)
1051 as_bad (_("immediate must be 1 or 2"));
1054 as_bad (_("immediate 1 or 2 expected"));
1058 *output_ptr++ = the_cc;
1061 *output_ptr++ = the_ctrl;
1064 *output_ptr++ = the_ctrl | 0x8;
1067 *output_ptr++ = (~the_interrupt & 0x3);
1070 *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1073 *output_ptr++ = the_flags;
1076 *output_ptr++ = c & 0xf;
1079 if (reg[c & 0xf] == 0)
1080 as_bad (_("can't use R0 here"));
1083 case CLASS_REG_BYTE:
1084 case CLASS_REG_WORD:
1085 case CLASS_REG_LONG:
1086 case CLASS_REG_QUAD:
1087 /* Insert bit mattern of right reg. */
1088 *output_ptr++ = reg[c & 0xf];
1091 switch (c & ARG_MASK)
1094 output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4);
1097 output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4);
1100 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1108 switch (c & ARG_MASK)
1111 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1114 imm_operand->X_add_number--;
1115 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1117 case ARG_IMMNMINUS1:
1118 imm_operand->X_add_number--;
1119 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1122 imm_operand->X_add_number = -imm_operand->X_add_number;
1124 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1127 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1131 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1141 /* Copy from the nibble buffer into the frag. */
1143 int length = (output_ptr - buffer) / 2;
1145 char *fragp = frag_more (length);
1147 while (src < output_ptr)
1149 *fragp = (src[0] << 4) | src[1];
1156 /* This is the guts of the machine-dependent assembler. STR points to a
1157 machine dependent instruction. This function is supposed to emit
1158 the frags/bytes it assembles to. */
1167 struct z8k_op operand[3];
1168 opcode_entry_type *opcode;
1169 opcode_entry_type *prev_opcode;
1171 /* Drop leading whitespace. */
1175 /* Find the op code end. */
1176 for (op_start = op_end = str;
1177 *op_end != 0 && *op_end != ' ';
1181 if (op_end == op_start)
1183 as_bad (_("can't find opcode "));
1189 opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start);
1193 as_bad (_("unknown opcode"));
1197 if (opcode->opcode == 250)
1199 /* Was really a pseudo op. */
1204 char *old = input_line_pointer;
1207 input_line_pointer = op_end;
1211 while (*input_line_pointer == ' ')
1212 input_line_pointer++;
1213 p = (pseudo_typeS *) (opcode->func);
1215 (p->poc_handler) (p->poc_val);
1216 input_line_pointer = old;
1221 input_line_pointer = get_operands (opcode, op_end, operand);
1222 prev_opcode = opcode;
1224 opcode = get_specific (opcode, operand);
1228 /* Couldn't find an opcode which matched the operands. */
1229 char *where = frag_more (2);
1234 as_bad (_("Can't find opcode to match operands"));
1238 build_bytes (opcode, operand);
1243 tc_crawl_symbol_chain (headers)
1244 object_headers *headers ATTRIBUTE_UNUSED;
1246 printf (_("call to tc_crawl_symbol_chain \n"));
1250 md_undefined_symbol (name)
1251 char *name ATTRIBUTE_UNUSED;
1257 tc_headers_hook (headers)
1258 object_headers *headers ATTRIBUTE_UNUSED;
1260 printf (_("call to tc_headers_hook \n"));
1263 /* Various routines to kill one day. */
1264 /* Equal to MAX_PRECISION in atof-ieee.c. */
1265 #define MAX_LITTLENUMS 6
1267 /* Turn a string in input_line_pointer into a floating point constant
1268 of type TYPE, and store the appropriate bytes in *LITP. The number
1269 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1270 returned, or NULL on OK. */
1273 md_atof (type, litP, sizeP)
1279 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1280 LITTLENUM_TYPE *wordP;
1312 return _("Bad call to MD_ATOF()");
1314 t = atof_ieee (input_line_pointer, type, words);
1316 input_line_pointer = t;
1318 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1319 for (wordP = words; prec--;)
1321 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1322 litP += sizeof (LITTLENUM_TYPE);
1327 CONST char *md_shortopts = "z:";
1329 struct option md_longopts[] = {
1330 {NULL, no_argument, NULL, 0}
1333 size_t md_longopts_size = sizeof (md_longopts);
1336 md_parse_option (c, arg)
1343 if (!strcmp (arg, "8001"))
1345 else if (!strcmp (arg, "8002"))
1349 as_bad (_("invalid architecture -z%s"), arg);
1362 md_show_usage (stream)
1365 fprintf (stream, _("\
1367 -z8001 generate segmented code\n\
1368 -z8002 generate unsegmented code\n"));
1372 tc_aout_fix_to_chars ()
1374 printf (_("call to tc_aout_fix_to_chars \n"));
1379 md_convert_frag (headers, seg, fragP)
1380 object_headers *headers ATTRIBUTE_UNUSED;
1381 segT seg ATTRIBUTE_UNUSED;
1382 fragS *fragP ATTRIBUTE_UNUSED;
1384 printf (_("call to md_convert_frag \n"));
1389 md_section_align (seg, size)
1393 return ((size + (1 << section_alignment[(int) seg]) - 1)
1394 & (-1 << section_alignment[(int) seg]));
1399 md_apply_fix (fixP, val)
1403 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1405 switch (fixP->fx_r_type)
1408 buf[0] = (buf[0] & 0xf0) | ((buf[0] + val) & 0xf);
1433 *buf++ = (val >> 8);
1437 *buf++ = (val >> 24);
1438 *buf++ = (val >> 16);
1439 *buf++ = (val >> 8);
1444 *buf++ = (val >> 16);
1446 *buf++ = (val >> 8);
1452 md_number_to_chars (buf, val, fixP->fx_size);
1461 md_estimate_size_before_relax (fragP, segment_type)
1462 register fragS *fragP ATTRIBUTE_UNUSED;
1463 register segT segment_type ATTRIBUTE_UNUSED;
1465 printf (_("call tomd_estimate_size_before_relax \n"));
1469 /* Put number into target byte order. */
1472 md_number_to_chars (ptr, use, nbytes)
1477 number_to_chars_bigendian (ptr, use, nbytes);
1481 md_pcrel_from (fixP)
1482 fixS *fixP ATTRIBUTE_UNUSED;
1488 tc_coff_symbol_emit_hook (s)
1489 symbolS *s ATTRIBUTE_UNUSED;
1494 tc_reloc_mangle (fix_ptr, intr, base)
1496 struct internal_reloc *intr;
1500 symbolS *symbol_ptr;
1502 if (fix_ptr->fx_addsy
1503 && fix_ptr->fx_subsy)
1505 symbolS *add = fix_ptr->fx_addsy;
1506 symbolS *sub = fix_ptr->fx_subsy;
1508 if (S_GET_SEGMENT (add) != S_GET_SEGMENT (sub))
1509 as_bad (_("Can't subtract symbols in different sections %s %s"),
1510 S_GET_NAME (add), S_GET_NAME (sub));
1513 int diff = S_GET_VALUE (add) - S_GET_VALUE (sub);
1515 fix_ptr->fx_addsy = 0;
1516 fix_ptr->fx_subsy = 0;
1517 fix_ptr->fx_offset += diff;
1520 symbol_ptr = fix_ptr->fx_addsy;
1522 /* If this relocation is attached to a symbol then it's ok
1524 if (fix_ptr->fx_r_type == 0)
1526 /* cons likes to create reloc32's whatever the size of the reloc. */
1527 switch (fix_ptr->fx_size)
1530 intr->r_type = R_IMM16;
1533 intr->r_type = R_IMM8;
1536 intr->r_type = R_IMM32;
1543 intr->r_type = fix_ptr->fx_r_type;
1545 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1546 intr->r_offset = fix_ptr->fx_offset;
1549 intr->r_symndx = symbol_ptr->sy_number;
1551 intr->r_symndx = -1;