1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 1998 Free Software Foundation.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 Written By Steve Chamberlain
28 #include "opcodes/z8k-opc.h"
34 const char comment_chars[] =
36 const char line_separator_chars[] =
38 const char line_comment_chars[] =
42 extern int coff_flags;
44 const int md_reloc_size;
46 /* This table describes all the machine specific pseudo-ops the assembler
47 has to support. The fields are:
48 pseudo-op name without dot
49 function to call to execute this pseudo-op
50 Integer arg to pass to the function
59 machine = bfd_mach_z8001;
67 machine = bfd_mach_z8002;
76 record_alignment (now_seg, 1);
79 void obj_coff_section ();
97 if (*input_line_pointer == '\'')
100 input_line_pointer++;
101 c = *input_line_pointer++;
106 c = (tohex (input_line_pointer[0]) << 4)
107 | tohex (input_line_pointer[1]);
108 input_line_pointer += 2;
110 FRAG_APPEND_1_CHAR (c);
111 c = *input_line_pointer++;
113 demand_empty_rest_of_line ();
117 const pseudo_typeS md_pseudo_table[] =
123 {"form", listing_psize, 0},
124 {"heading", listing_title, 0},
125 {"import", s_ignore, 0},
126 {"page", listing_eject, 0},
127 {"program", s_ignore, 0},
128 {"z8001", s_segm, 0},
129 {"z8002", s_unseg, 0},
133 {"unsegm", s_unseg, 0},
134 {"unseg", s_unseg, 0},
135 {"name", s_app_file, 0},
136 {"global", s_globl, 0},
141 {"rsect", obj_coff_section, 0},
142 {"sect", obj_coff_section, 0},
143 {"block", s_space, 0},
148 const char EXP_CHARS[] = "eE";
150 /* Chars that mean this number is a floating point constant */
153 const char FLT_CHARS[] = "rRsSfFdDxXpP";
155 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
160 opcode_entry_type *opcode;
161 char *prev_name = "";
164 opcode_hash_control = hash_new ();
166 for (opcode = z8k_table; opcode->name; opcode++)
168 /* Only enter unique codes into the table */
169 char *src = opcode->name;
171 if (strcmp (opcode->name, prev_name))
173 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
177 prev_name = opcode->name;
180 /* default to z8002 */
183 /* insert the pseudo ops too */
184 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
186 opcode_entry_type *fake_opcode;
187 fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
188 fake_opcode->name = md_pseudo_table[idx].poc_name,
189 fake_opcode->func = (void *) (md_pseudo_table + idx);
190 fake_opcode->opcode = 250;
191 hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
203 typedef struct z8k_op
205 char regsize; /* 'b','w','r','q' */
206 unsigned int reg; /* 0..15 */
210 unsigned int x_reg; /* any other register associated with the mode */
211 expressionS exp; /* any expression */
216 static expressionS *da_operand;
217 static expressionS *imm_operand;
226 DEFUN (whatreg, (reg, src),
230 if (isdigit (src[1]))
232 *reg = (src[0] - '0') * 10 + src[1] - '0';
237 *reg = (src[0] - '0');
249 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
258 /* try and parse a reg name, returns number of chars consumed */
260 DEFUN (parse_reg, (src, mode, reg),
268 if (src[0] == 's' && src[1] == 'p')
272 *mode = CLASS_REG_LONG;
277 *mode = CLASS_REG_WORD;
286 *mode = CLASS_REG_LONG;
287 res = whatreg (reg, src + 2);
290 as_warn (_("register rr%d, out of range."),regno);
292 else if (src[1] == 'h')
294 *mode = CLASS_REG_BYTE;
295 res = whatreg (reg, src + 2);
298 as_warn (_("register rh%d, out of range."),regno);
300 else if (src[1] == 'l')
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 *mode = CLASS_REG_QUAD;
312 res = whatreg (reg, src + 2);
315 as_warn (_("register rq%d, out of range."),regno);
319 *mode = CLASS_REG_WORD;
320 res = whatreg (reg, src + 1);
323 as_warn (_("register r%d, out of range."),regno);
331 DEFUN (parse_exp, (s, op),
335 char *save = input_line_pointer;
338 input_line_pointer = s;
340 if (op->X_op == O_absent)
341 as_bad (_("missing operand"));
342 new = input_line_pointer;
343 input_line_pointer = save;
347 /* The many forms of operand:
366 DEFUN (checkfor, (ptr, what),
374 as_bad (_("expected %c"), what);
379 /* Make sure the mode supplied is the size of a word */
381 DEFUN (regword, (mode, string),
390 as_bad (_("register is wrong size for a word %s"), string);
394 /* Make sure the mode supplied is the size of an address */
396 DEFUN (regaddr, (mode, string),
402 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
405 as_bad (_("register is wrong size for address %s"), string);
415 struct ctrl_names ctrl_table[] =
429 DEFUN (get_ctrl_operand, (ptr, mode, dst),
431 struct z8k_op *mode AND
441 mode->mode = CLASS_CTRL;
442 for (i = 0; ctrl_table[i].name; i++)
446 for (j = 0; ctrl_table[i].name[j]; j++)
448 if (ctrl_table[i].name[j] != src[j])
451 the_ctrl = ctrl_table[i].value;
467 struct flag_names flag_table[] =
479 DEFUN (get_flags_operand, (ptr, mode, dst),
481 struct z8k_op *mode AND
492 mode->mode = CLASS_FLAGS;
494 for (j = 0; j <= 9; j++)
498 for (i = 0; flag_table[i].name; i++)
500 if (flag_table[i].name[0] == src[j])
502 the_flags = the_flags | flag_table[i].value;
516 struct interrupt_names
523 struct interrupt_names intr_table[] =
533 DEFUN (get_interrupt_operand, (ptr, mode, dst),
535 struct z8k_op *mode AND
545 mode->mode = CLASS_IMM;
546 for (i = 0; intr_table[i].name; i++)
550 for (j = 0; intr_table[i].name[j]; j++)
552 if (intr_table[i].name[j] != src[j])
555 the_interrupt = intr_table[i].value;
571 struct cc_names table[] =
599 DEFUN (get_cc_operand, (ptr, mode, dst),
601 struct z8k_op *mode AND
611 mode->mode = CLASS_CC;
612 for (i = 0; table[i].name; i++)
616 for (j = 0; table[i].name[j]; j++)
618 if (table[i].name[j] != src[j])
621 the_cc = table[i].value;
630 get_operand (ptr, mode, dst)
647 mode->mode = CLASS_IMM;
648 imm_operand = &(mode->exp);
649 src = parse_exp (src + 1, &(mode->exp));
651 else if (*src == '@')
655 mode->mode = CLASS_IR;
656 src = parse_reg (src + 1, &d, &mode->reg);
662 end = parse_reg (src, &mode->mode, ®n);
672 end = parse_reg (src, &nw, &nr);
680 as_bad (_("Missing ) in ra(rb)"));
687 regaddr (mode->mode, "ra(rb) ra");
688 /* regword (mode->mode, "ra(rb) rb");*/
689 mode->mode = CLASS_BX;
699 src = parse_exp (src, &(mode->exp));
700 src = checkfor (src, ')');
701 mode->mode = CLASS_BA;
704 imm_operand = &(mode->exp);
716 src = parse_exp (src, &(mode->exp));
720 end = parse_reg (src, &(mode->mode), ®n);
721 regword (mode->mode, "addr(Ra) ra");
722 mode->mode = CLASS_X;
725 da_operand = &(mode->exp);
726 src = checkfor (end, ')');
730 /* Just an address */
731 mode->mode = CLASS_DA;
734 da_operand = &(mode->exp);
743 get_operands (opcode, op_end, operand)
744 opcode_entry_type *opcode;
750 switch (opcode->noperands)
759 if (opcode->arg_info[0] == CLASS_CC)
761 get_cc_operand (&ptr, operand + 0, 0);
763 else if (opcode->arg_info[0] == CLASS_FLAGS)
765 get_flags_operand (&ptr, operand + 0, 0);
767 else if (opcode->arg_info[0] == (CLASS_IMM +(ARG_IMM2)))
769 get_interrupt_operand (&ptr, operand + 0, 0);
773 get_operand (&ptr, operand + 0, 0);
781 if (opcode->arg_info[0] == CLASS_CC)
783 get_cc_operand (&ptr, operand + 0, 0);
785 else if (opcode->arg_info[0] == CLASS_CTRL)
787 get_ctrl_operand (&ptr, operand + 0, 0);
791 get_operand (&ptr, operand + 0, 0);
796 get_ctrl_operand (&ptr, operand + 1, 1);
802 get_operand (&ptr, operand + 0, 0);
808 get_operand (&ptr, operand + 1, 1);
813 get_operand (&ptr, operand + 0, 0);
816 get_operand (&ptr, operand + 1, 1);
819 get_operand (&ptr, operand + 2, 2);
824 get_operand (&ptr, operand + 0, 0);
827 get_operand (&ptr, operand + 1, 1);
830 get_operand (&ptr, operand + 2, 2);
833 get_cc_operand (&ptr, operand + 3, 3);
842 /* Passed a pointer to a list of opcodes which use different
843 addressing modes, return the opcode which matches the opcodes
849 DEFUN (get_specific, (opcode, operands),
850 opcode_entry_type * opcode AND
854 opcode_entry_type *this_try = opcode;
856 unsigned int noperands = opcode->noperands;
858 unsigned int dispreg;
859 unsigned int this_index = opcode->idx;
861 while (this_index == opcode->idx && !found)
866 for (i = 0; i < noperands; i++)
868 int mode = operands[i].mode;
870 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
872 /* it could be an pc rel operand, if this is a da mode and
873 we like disps, then insert it */
875 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
877 /* This is the case */
878 operands[i].mode = CLASS_DISP;
880 else if (mode == CLASS_BA && this_try->arg_info[i])
882 /* Can't think of a way to turn what we've been given into
883 something that's ok */
886 else if (this_try->arg_info[i] & CLASS_PR)
888 if (mode == CLASS_REG_LONG && segmented_mode)
892 else if (mode == CLASS_REG_WORD && !segmented_mode)
902 switch (mode & CLASS_MASK)
917 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
932 DEFUN (check_operand, (operand, width, string),
933 struct z8k_op *operand AND
934 unsigned int width AND
937 if (operand->exp.X_add_symbol == 0
938 && operand->exp.X_op_symbol == 0)
941 /* No symbol involved, let's look at offset, it's dangerous if any of
942 the high bits are not 0 or ff's, find out by oring or anding with
943 the width and seeing if the answer is 0 or all fs*/
944 if ((operand->exp.X_add_number & ~width) != 0 &&
945 (operand->exp.X_add_number | width) != (~0))
947 as_warn (_("operand %s0x%x out of range."), string, operand->exp.X_add_number);
953 static char buffer[20];
956 DEFUN (newfix, (ptr, type, operand),
959 expressionS * operand)
961 if (operand->X_add_symbol
962 || operand->X_op_symbol
963 || operand->X_add_number)
965 fix_new_exp (frag_now,
975 DEFUN (apply_fix, (ptr, type, operand, size),
978 expressionS * operand AND
981 int n = operand->X_add_number;
983 operand->X_add_number = n;
984 newfix ((ptr - buffer) / 2, type, operand);
988 case 8: /* 8 nibbles == 32 bits */
993 case 4: /* 4 niblles == 16 bits */
1007 /* Now we know what sort of opcodes it is, lets build the bytes -
1009 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
1011 build_bytes (this_try, operand)
1012 opcode_entry_type * this_try;
1013 struct z8k_op *operand;
1019 char *output_ptr = buffer;
1025 unsigned int *class_ptr;
1027 frag_wane (frag_now);
1030 memset (buffer, 20, 0);
1031 class_ptr = this_try->byte_info;
1034 for (nibble = 0; c = *class_ptr++; nibble++)
1037 switch (c & CLASS_MASK)
1043 /* Direct address, we don't cope with the SS mode right now */
1046 da_operand->X_add_number |= 0x80000000;
1047 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1051 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1057 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1064 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1071 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1072 output_ptr[-2] = 0x8;
1076 case CLASS_BIT_1OR2:
1077 *output_ptr = c & 0xf;
1080 if (imm_operand->X_add_number == 2)
1084 else if (imm_operand->X_add_number != 1)
1086 as_bad (_("immediate must be 1 or 2"));
1091 as_bad (_("immediate 1 or 2 expected"));
1096 *output_ptr++ = the_cc;
1099 *output_ptr++ = the_ctrl;
1102 *output_ptr++ = the_ctrl | 0x8;
1105 *output_ptr++ = (~the_interrupt & 0x3);
1108 *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1111 *output_ptr++ = the_flags;
1114 *output_ptr++ = c & 0xf;
1117 if (reg[c & 0xf] == 0)
1119 as_bad (_("can't use R0 here"));
1122 case CLASS_REG_BYTE:
1123 case CLASS_REG_WORD:
1124 case CLASS_REG_LONG:
1125 case CLASS_REG_QUAD:
1126 /* Insert bit mattern of
1128 *output_ptr++ = reg[c & 0xf];
1131 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1138 switch (c & ARG_MASK)
1141 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1144 imm_operand->X_add_number--;
1145 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1147 case ARG_IMMNMINUS1:
1148 imm_operand->X_add_number--;
1149 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1152 imm_operand->X_add_number = -imm_operand->X_add_number;
1154 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1157 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1161 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1171 /* Copy from the nibble buffer into the frag */
1174 int length = (output_ptr - buffer) / 2;
1176 char *fragp = frag_more (length);
1178 while (src < output_ptr)
1180 *fragp = (src[0] << 4) | src[1];
1189 /* This is the guts of the machine-dependent assembler. STR points to a
1190 machine dependent instruction. This funciton is supposed to emit
1191 the frags/bytes it assembles to.
1195 DEFUN (md_assemble, (str),
1201 struct z8k_op operand[3];
1202 opcode_entry_type *opcode;
1203 opcode_entry_type *prev_opcode;
1208 /* Drop leading whitespace */
1212 /* find the op code end */
1213 for (op_start = op_end = str;
1214 *op_end != 0 && *op_end != ' ';
1221 if (op_end == op_start)
1223 as_bad (_("can't find opcode "));
1229 opcode = (opcode_entry_type *) hash_find (opcode_hash_control,
1235 as_bad (_("unknown opcode"));
1239 if (opcode->opcode == 250)
1241 /* was really a pseudo op */
1246 char *old = input_line_pointer;
1250 input_line_pointer = op_end;
1254 while (*input_line_pointer == ' ')
1255 input_line_pointer++;
1256 p = (pseudo_typeS *) (opcode->func);
1258 (p->poc_handler) (p->poc_val);
1259 input_line_pointer = old;
1264 input_line_pointer = get_operands (opcode, op_end,
1266 prev_opcode = opcode;
1268 opcode = get_specific (opcode, operand);
1272 /* Couldn't find an opcode which matched the operands */
1273 char *where = frag_more (2);
1278 as_bad (_("Can't find opcode to match operands"));
1282 build_bytes (opcode, operand);
1287 DEFUN (tc_crawl_symbol_chain, (headers),
1288 object_headers * headers)
1290 printf (_("call to tc_crawl_symbol_chain \n"));
1294 DEFUN (md_undefined_symbol, (name),
1301 DEFUN (tc_headers_hook, (headers),
1302 object_headers * headers)
1304 printf (_("call to tc_headers_hook \n"));
1307 /* Various routines to kill one day */
1308 /* Equal to MAX_PRECISION in atof-ieee.c */
1309 #define MAX_LITTLENUMS 6
1311 /* Turn a string in input_line_pointer into a floating point constant of type
1312 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1313 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1316 md_atof (type, litP, sizeP)
1322 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1323 LITTLENUM_TYPE *wordP;
1355 return _("Bad call to MD_ATOF()");
1357 t = atof_ieee (input_line_pointer, type, words);
1359 input_line_pointer = t;
1361 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1362 for (wordP = words; prec--;)
1364 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1365 litP += sizeof (LITTLENUM_TYPE);
1370 CONST char *md_shortopts = "z:";
1371 struct option md_longopts[] = {
1372 {NULL, no_argument, NULL, 0}
1374 size_t md_longopts_size = sizeof(md_longopts);
1377 md_parse_option (c, arg)
1384 if (!strcmp (arg, "8001"))
1386 else if (!strcmp (arg, "8002"))
1390 as_bad (_("invalid architecture -z%s"), arg);
1403 md_show_usage (stream)
1406 fprintf(stream, _("\
1408 -z8001 generate segmented code\n\
1409 -z8002 generate unsegmented code\n"));
1413 tc_aout_fix_to_chars ()
1415 printf (_("call to tc_aout_fix_to_chars \n"));
1420 md_convert_frag (headers, seg, fragP)
1421 object_headers *headers;
1425 printf (_("call to md_convert_frag \n"));
1430 DEFUN (md_section_align, (seg, size),
1434 return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1439 md_apply_fix (fixP, val)
1443 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1445 switch (fixP->fx_r_type)
1448 buf[0] = (buf[0] & 0xf0) | ((buf[0] + val) & 0xf);
1454 /* if (val != 0) abort();*/
1460 /* if (val != 0) abort();*/
1467 *buf++ = (val >> 8);
1471 *buf++ = (val >> 24);
1472 *buf++ = (val >> 16);
1473 *buf++ = (val >> 8);
1478 *buf++ = (val >> 16);
1480 *buf++ = (val >> 8);
1486 md_number_to_chars (buf, val, fixP->fx_size);
1496 md_estimate_size_before_relax (fragP, segment_type)
1497 register fragS *fragP;
1498 register segT segment_type;
1500 printf (_("call tomd_estimate_size_before_relax \n"));
1504 /* Put number into target byte order */
1507 DEFUN (md_number_to_chars, (ptr, use, nbytes),
1512 number_to_chars_bigendian (ptr, use, nbytes);
1515 md_pcrel_from (fixP)
1522 tc_coff_symbol_emit_hook (s)
1528 tc_reloc_mangle (fix_ptr, intr, base)
1530 struct internal_reloc *intr;
1534 symbolS *symbol_ptr;
1536 if (fix_ptr->fx_addsy &&
1539 symbolS *add = fix_ptr->fx_addsy;
1540 symbolS *sub = fix_ptr->fx_subsy;
1541 if (S_GET_SEGMENT(add) != S_GET_SEGMENT(sub))
1543 as_bad(_("Can't subtract symbols in different sections %s %s"),
1544 S_GET_NAME(add), S_GET_NAME(sub));
1547 int diff = S_GET_VALUE(add) - S_GET_VALUE(sub);
1548 fix_ptr->fx_addsy = 0;
1549 fix_ptr->fx_subsy = 0;
1550 fix_ptr->fx_offset += diff;
1553 symbol_ptr = fix_ptr->fx_addsy;
1555 /* If this relocation is attached to a symbol then it's ok
1557 if (fix_ptr->fx_r_type == 0)
1559 /* cons likes to create reloc32's whatever the size of the reloc.. */
1560 switch (fix_ptr->fx_size)
1563 intr->r_type = R_IMM16;
1566 intr->r_type = R_IMM8;
1569 intr->r_type = R_IMM32;
1578 intr->r_type = fix_ptr->fx_r_type;
1581 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1582 intr->r_offset = fix_ptr->fx_offset;
1585 intr->r_symndx = symbol_ptr->sy_number;
1587 intr->r_symndx = -1;