1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation.
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
23 Written By Steve Chamberlain
29 #include "opcodes/z8k-opc.h"
35 const char comment_chars[] = "!";
36 const char line_comment_chars[] = "#";
37 const char line_separator_chars[] = ";";
40 extern int coff_flags;
42 const int md_reloc_size;
44 /* This table describes all the machine specific pseudo-ops the assembler
45 has to support. The fields are:
46 pseudo-op name without dot
47 function to call to execute this pseudo-op
48 Integer arg to pass to the function
57 machine = bfd_mach_z8001;
65 machine = bfd_mach_z8002;
74 record_alignment (now_seg, 1);
77 void obj_coff_section ();
95 if (*input_line_pointer == '\'')
99 c = *input_line_pointer++;
104 c = (tohex (input_line_pointer[0]) << 4)
105 | tohex (input_line_pointer[1]);
106 input_line_pointer += 2;
108 FRAG_APPEND_1_CHAR (c);
109 c = *input_line_pointer++;
111 demand_empty_rest_of_line ();
115 const pseudo_typeS md_pseudo_table[] =
121 {"form", listing_psize, 0},
122 {"heading", listing_title, 0},
123 {"import", s_ignore, 0},
124 {"page", listing_eject, 0},
125 {"program", s_ignore, 0},
126 {"z8001", s_segm, 0},
127 {"z8002", s_unseg, 0},
131 {"unsegm", s_unseg, 0},
132 {"unseg", s_unseg, 0},
133 {"name", s_app_file, 0},
134 {"global", s_globl, 0},
139 {"rsect", obj_coff_section, 0},
140 {"sect", obj_coff_section, 0},
141 {"block", s_space, 0},
146 const char EXP_CHARS[] = "eE";
148 /* Chars that mean this number is a floating point constant */
151 const char FLT_CHARS[] = "rRsSfFdDxXpP";
153 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
158 opcode_entry_type *opcode;
159 char *prev_name = "";
162 opcode_hash_control = hash_new ();
164 for (opcode = z8k_table; opcode->name; opcode++)
166 /* Only enter unique codes into the table */
167 char *src = opcode->name;
169 if (strcmp (opcode->name, prev_name))
171 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
175 prev_name = opcode->name;
178 /* default to z8002 */
181 /* insert the pseudo ops too */
182 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
184 opcode_entry_type *fake_opcode;
185 fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
186 fake_opcode->name = md_pseudo_table[idx].poc_name,
187 fake_opcode->func = (void *) (md_pseudo_table + idx);
188 fake_opcode->opcode = 250;
189 hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
201 typedef struct z8k_op
203 char regsize; /* 'b','w','r','q' */
204 unsigned int reg; /* 0..15 */
208 unsigned int x_reg; /* any other register associated with the mode */
209 expressionS exp; /* any expression */
214 static expressionS *da_operand;
215 static expressionS *imm_operand;
228 if (isdigit (src[1]))
230 *reg = (src[0] - '0') * 10 + src[1] - '0';
235 *reg = (src[0] - '0');
247 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
256 /* try and parse a reg name, returns number of chars consumed */
258 parse_reg (src, mode, reg)
266 if (src[0] == 's' && src[1] == 'p')
270 *mode = CLASS_REG_LONG;
275 *mode = CLASS_REG_WORD;
284 *mode = CLASS_REG_LONG;
285 res = whatreg (reg, src + 2);
288 as_warn (_("register rr%d, out of range."),regno);
290 else if (src[1] == 'h')
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 *mode = CLASS_REG_BYTE;
301 res = whatreg (reg, src + 2);
304 as_warn (_("register rl%d, out of range."),regno);
307 else if (src[1] == 'q')
309 *mode = CLASS_REG_QUAD;
310 res = whatreg (reg, src + 2);
313 as_warn (_("register rq%d, out of range."),regno);
317 *mode = CLASS_REG_WORD;
318 res = whatreg (reg, src + 1);
321 as_warn (_("register r%d, out of range."),regno);
333 char *save = input_line_pointer;
336 input_line_pointer = s;
338 if (op->X_op == O_absent)
339 as_bad (_("missing operand"));
340 new = input_line_pointer;
341 input_line_pointer = save;
345 /* The many forms of operand:
372 as_bad (_("expected %c"), what);
377 /* Make sure the mode supplied is the size of a word */
379 regword (mode, string)
388 as_bad (_("register is wrong size for a word %s"), string);
392 /* Make sure the mode supplied is the size of an address */
394 regaddr (mode, string)
400 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
403 as_bad (_("register is wrong size for address %s"), string);
413 struct ctrl_names ctrl_table[] =
427 get_ctrl_operand (ptr, mode, dst)
439 mode->mode = CLASS_CTRL;
440 for (i = 0; ctrl_table[i].name; i++)
444 for (j = 0; ctrl_table[i].name[j]; j++)
446 if (ctrl_table[i].name[j] != src[j])
449 the_ctrl = ctrl_table[i].value;
465 struct flag_names flag_table[] =
477 get_flags_operand (ptr, mode, dst)
490 mode->mode = CLASS_FLAGS;
492 for (j = 0; j <= 9; j++)
496 for (i = 0; flag_table[i].name; i++)
498 if (flag_table[i].name[0] == src[j])
500 the_flags = the_flags | flag_table[i].value;
514 struct interrupt_names
521 struct interrupt_names intr_table[] =
531 get_interrupt_operand (ptr, mode, dst)
543 mode->mode = CLASS_IMM;
544 for (i = 0; intr_table[i].name; i++)
548 for (j = 0; intr_table[i].name[j]; j++)
550 if (intr_table[i].name[j] != src[j])
553 the_interrupt = intr_table[i].value;
569 struct cc_names table[] =
597 get_cc_operand (ptr, mode, dst)
609 mode->mode = CLASS_CC;
610 for (i = 0; table[i].name; i++)
614 for (j = 0; table[i].name[j]; j++)
616 if (table[i].name[j] != src[j])
619 the_cc = table[i].value;
628 get_operand (ptr, mode, dst)
645 mode->mode = CLASS_IMM;
646 imm_operand = &(mode->exp);
647 src = parse_exp (src + 1, &(mode->exp));
649 else if (*src == '@')
653 mode->mode = CLASS_IR;
654 src = parse_reg (src + 1, &d, &mode->reg);
660 end = parse_reg (src, &mode->mode, ®n);
670 end = parse_reg (src, &nw, &nr);
678 as_bad (_("Missing ) in ra(rb)"));
685 regaddr (mode->mode, "ra(rb) ra");
686 /* regword (mode->mode, "ra(rb) rb");*/
687 mode->mode = CLASS_BX;
697 src = parse_exp (src, &(mode->exp));
698 src = checkfor (src, ')');
699 mode->mode = CLASS_BA;
702 imm_operand = &(mode->exp);
714 src = parse_exp (src, &(mode->exp));
718 end = parse_reg (src, &(mode->mode), ®n);
719 regword (mode->mode, "addr(Ra) ra");
720 mode->mode = CLASS_X;
723 da_operand = &(mode->exp);
724 src = checkfor (end, ')');
728 /* Just an address */
729 mode->mode = CLASS_DA;
732 da_operand = &(mode->exp);
741 get_operands (opcode, op_end, operand)
742 opcode_entry_type *opcode;
748 switch (opcode->noperands)
757 if (opcode->arg_info[0] == CLASS_CC)
759 get_cc_operand (&ptr, operand + 0, 0);
761 else if (opcode->arg_info[0] == CLASS_FLAGS)
763 get_flags_operand (&ptr, operand + 0, 0);
765 else if (opcode->arg_info[0] == (CLASS_IMM +(ARG_IMM2)))
767 get_interrupt_operand (&ptr, operand + 0, 0);
771 get_operand (&ptr, operand + 0, 0);
779 if (opcode->arg_info[0] == CLASS_CC)
781 get_cc_operand (&ptr, operand + 0, 0);
783 else if (opcode->arg_info[0] == CLASS_CTRL)
785 get_ctrl_operand (&ptr, operand + 0, 0);
789 get_operand (&ptr, operand + 0, 0);
794 get_ctrl_operand (&ptr, operand + 1, 1);
800 get_operand (&ptr, operand + 0, 0);
806 get_operand (&ptr, operand + 1, 1);
811 get_operand (&ptr, operand + 0, 0);
814 get_operand (&ptr, operand + 1, 1);
817 get_operand (&ptr, operand + 2, 2);
822 get_operand (&ptr, operand + 0, 0);
825 get_operand (&ptr, operand + 1, 1);
828 get_operand (&ptr, operand + 2, 2);
831 get_cc_operand (&ptr, operand + 3, 3);
840 /* Passed a pointer to a list of opcodes which use different
841 addressing modes, return the opcode which matches the opcodes
847 get_specific (opcode, operands)
848 opcode_entry_type *opcode;
852 opcode_entry_type *this_try = opcode;
854 unsigned int noperands = opcode->noperands;
856 unsigned int dispreg;
857 unsigned int this_index = opcode->idx;
859 while (this_index == opcode->idx && !found)
864 for (i = 0; i < noperands; i++)
866 int mode = operands[i].mode;
868 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
870 /* it could be an pc rel operand, if this is a da mode and
871 we like disps, then insert it */
873 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
875 /* This is the case */
876 operands[i].mode = CLASS_DISP;
878 else if (mode == CLASS_BA && this_try->arg_info[i])
880 /* Can't think of a way to turn what we've been given into
881 something that's ok */
884 else if (this_try->arg_info[i] & CLASS_PR)
886 if (mode == CLASS_REG_LONG && segmented_mode)
890 else if (mode == CLASS_REG_WORD && !segmented_mode)
900 switch (mode & CLASS_MASK)
915 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
930 check_operand (operand, width, string)
931 struct z8k_op *operand;
935 if (operand->exp.X_add_symbol == 0
936 && operand->exp.X_op_symbol == 0)
939 /* No symbol involved, let's look at offset, it's dangerous if any of
940 the high bits are not 0 or ff's, find out by oring or anding with
941 the width and seeing if the answer is 0 or all fs*/
942 if ((operand->exp.X_add_number & ~width) != 0 &&
943 (operand->exp.X_add_number | width) != (~0))
945 as_warn (_("operand %s0x%x out of range."), string, operand->exp.X_add_number);
951 static char buffer[20];
954 newfix (ptr, type, operand)
957 expressionS *operand;
959 if (operand->X_add_symbol
960 || operand->X_op_symbol
961 || operand->X_add_number)
963 fix_new_exp (frag_now,
973 apply_fix (ptr, type, operand, size)
976 expressionS *operand;
979 int n = operand->X_add_number;
981 operand->X_add_number = n;
982 newfix ((ptr - buffer) / 2, type, operand);
986 case 8: /* 8 nibbles == 32 bits */
991 case 4: /* 4 niblles == 16 bits */
1005 /* Now we know what sort of opcodes it is, lets build the bytes -
1007 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
1009 build_bytes (this_try, operand)
1010 opcode_entry_type * this_try;
1011 struct z8k_op *operand;
1017 char *output_ptr = buffer;
1023 unsigned int *class_ptr;
1025 frag_wane (frag_now);
1028 memset (buffer, 20, 0);
1029 class_ptr = this_try->byte_info;
1032 for (nibble = 0; c = *class_ptr++; nibble++)
1035 switch (c & CLASS_MASK)
1041 /* Direct address, we don't cope with the SS mode right now */
1044 da_operand->X_add_number |= 0x80000000;
1045 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1049 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1055 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1062 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1069 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1070 output_ptr[-2] = 0x8;
1074 case CLASS_BIT_1OR2:
1075 *output_ptr = c & 0xf;
1078 if (imm_operand->X_add_number == 2)
1082 else if (imm_operand->X_add_number != 1)
1084 as_bad (_("immediate must be 1 or 2"));
1089 as_bad (_("immediate 1 or 2 expected"));
1094 *output_ptr++ = the_cc;
1097 *output_ptr++ = the_ctrl;
1100 *output_ptr++ = the_ctrl | 0x8;
1103 *output_ptr++ = (~the_interrupt & 0x3);
1106 *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1109 *output_ptr++ = the_flags;
1112 *output_ptr++ = c & 0xf;
1115 if (reg[c & 0xf] == 0)
1117 as_bad (_("can't use R0 here"));
1120 case CLASS_REG_BYTE:
1121 case CLASS_REG_WORD:
1122 case CLASS_REG_LONG:
1123 case CLASS_REG_QUAD:
1124 /* Insert bit mattern of
1126 *output_ptr++ = reg[c & 0xf];
1129 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1136 switch (c & ARG_MASK)
1139 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1142 imm_operand->X_add_number--;
1143 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1145 case ARG_IMMNMINUS1:
1146 imm_operand->X_add_number--;
1147 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1150 imm_operand->X_add_number = -imm_operand->X_add_number;
1152 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1155 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1159 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1169 /* Copy from the nibble buffer into the frag */
1172 int length = (output_ptr - buffer) / 2;
1174 char *fragp = frag_more (length);
1176 while (src < output_ptr)
1178 *fragp = (src[0] << 4) | src[1];
1187 /* This is the guts of the machine-dependent assembler. STR points to a
1188 machine dependent instruction. This funciton is supposed to emit
1189 the frags/bytes it assembles to.
1199 struct z8k_op operand[3];
1200 opcode_entry_type *opcode;
1201 opcode_entry_type *prev_opcode;
1206 /* Drop leading whitespace */
1210 /* find the op code end */
1211 for (op_start = op_end = str;
1212 *op_end != 0 && *op_end != ' ';
1219 if (op_end == op_start)
1221 as_bad (_("can't find opcode "));
1227 opcode = (opcode_entry_type *) hash_find (opcode_hash_control,
1233 as_bad (_("unknown opcode"));
1237 if (opcode->opcode == 250)
1239 /* was really a pseudo op */
1244 char *old = input_line_pointer;
1248 input_line_pointer = op_end;
1252 while (*input_line_pointer == ' ')
1253 input_line_pointer++;
1254 p = (pseudo_typeS *) (opcode->func);
1256 (p->poc_handler) (p->poc_val);
1257 input_line_pointer = old;
1262 input_line_pointer = get_operands (opcode, op_end,
1264 prev_opcode = opcode;
1266 opcode = get_specific (opcode, operand);
1270 /* Couldn't find an opcode which matched the operands */
1271 char *where = frag_more (2);
1276 as_bad (_("Can't find opcode to match operands"));
1280 build_bytes (opcode, operand);
1285 tc_crawl_symbol_chain (headers)
1286 object_headers *headers;
1288 printf (_("call to tc_crawl_symbol_chain \n"));
1292 md_undefined_symbol (name)
1299 tc_headers_hook (headers)
1300 object_headers *headers;
1302 printf (_("call to tc_headers_hook \n"));
1305 /* Various routines to kill one day */
1306 /* Equal to MAX_PRECISION in atof-ieee.c */
1307 #define MAX_LITTLENUMS 6
1309 /* Turn a string in input_line_pointer into a floating point constant of type
1310 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1311 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1314 md_atof (type, litP, sizeP)
1320 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1321 LITTLENUM_TYPE *wordP;
1353 return _("Bad call to MD_ATOF()");
1355 t = atof_ieee (input_line_pointer, type, words);
1357 input_line_pointer = t;
1359 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1360 for (wordP = words; prec--;)
1362 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1363 litP += sizeof (LITTLENUM_TYPE);
1368 CONST char *md_shortopts = "z:";
1369 struct option md_longopts[] = {
1370 {NULL, no_argument, NULL, 0}
1372 size_t md_longopts_size = sizeof(md_longopts);
1375 md_parse_option (c, arg)
1382 if (!strcmp (arg, "8001"))
1384 else if (!strcmp (arg, "8002"))
1388 as_bad (_("invalid architecture -z%s"), arg);
1401 md_show_usage (stream)
1404 fprintf(stream, _("\
1406 -z8001 generate segmented code\n\
1407 -z8002 generate unsegmented code\n"));
1411 tc_aout_fix_to_chars ()
1413 printf (_("call to tc_aout_fix_to_chars \n"));
1418 md_convert_frag (headers, seg, fragP)
1419 object_headers *headers;
1423 printf (_("call to md_convert_frag \n"));
1428 md_section_align (seg, size)
1432 return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1437 md_apply_fix (fixP, val)
1441 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1443 switch (fixP->fx_r_type)
1446 buf[0] = (buf[0] & 0xf0) | ((buf[0] + val) & 0xf);
1452 /* if (val != 0) abort();*/
1458 /* if (val != 0) abort();*/
1465 *buf++ = (val >> 8);
1469 *buf++ = (val >> 24);
1470 *buf++ = (val >> 16);
1471 *buf++ = (val >> 8);
1476 *buf++ = (val >> 16);
1478 *buf++ = (val >> 8);
1484 md_number_to_chars (buf, val, fixP->fx_size);
1494 md_estimate_size_before_relax (fragP, segment_type)
1495 register fragS *fragP;
1496 register segT segment_type;
1498 printf (_("call tomd_estimate_size_before_relax \n"));
1502 /* Put number into target byte order */
1505 md_number_to_chars (ptr, use, nbytes)
1510 number_to_chars_bigendian (ptr, use, nbytes);
1513 md_pcrel_from (fixP)
1520 tc_coff_symbol_emit_hook (s)
1526 tc_reloc_mangle (fix_ptr, intr, base)
1528 struct internal_reloc *intr;
1532 symbolS *symbol_ptr;
1534 if (fix_ptr->fx_addsy &&
1537 symbolS *add = fix_ptr->fx_addsy;
1538 symbolS *sub = fix_ptr->fx_subsy;
1539 if (S_GET_SEGMENT(add) != S_GET_SEGMENT(sub))
1541 as_bad(_("Can't subtract symbols in different sections %s %s"),
1542 S_GET_NAME(add), S_GET_NAME(sub));
1545 int diff = S_GET_VALUE(add) - S_GET_VALUE(sub);
1546 fix_ptr->fx_addsy = 0;
1547 fix_ptr->fx_subsy = 0;
1548 fix_ptr->fx_offset += diff;
1551 symbol_ptr = fix_ptr->fx_addsy;
1553 /* If this relocation is attached to a symbol then it's ok
1555 if (fix_ptr->fx_r_type == 0)
1557 /* cons likes to create reloc32's whatever the size of the reloc.. */
1558 switch (fix_ptr->fx_size)
1561 intr->r_type = R_IMM16;
1564 intr->r_type = R_IMM8;
1567 intr->r_type = R_IMM32;
1576 intr->r_type = fix_ptr->fx_r_type;
1579 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1580 intr->r_offset = fix_ptr->fx_offset;
1583 intr->r_symndx = symbol_ptr->sy_number;
1585 intr->r_symndx = -1;