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 to parse a reg name. Return a pointer to the first character
257 in SRC after the reg name. */
260 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);
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:
374 as_bad (_("expected %c"), what);
379 /* Make sure the mode supplied is the size of a word */
381 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 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 get_ctrl_operand (ptr, mode, dst)
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 get_flags_operand (ptr, mode, dst)
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 get_interrupt_operand (ptr, mode, dst)
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 get_cc_operand (ptr, mode, dst)
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 get_specific (opcode, operands)
850 opcode_entry_type *opcode;
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 check_operand (operand, width, string)
933 struct z8k_op *operand;
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 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 apply_fix (ptr, type, operand, size)
978 expressionS *operand;
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 function is supposed to emit
1191 the frags/bytes it assembles to. */
1200 struct z8k_op operand[3];
1201 opcode_entry_type *opcode;
1202 opcode_entry_type *prev_opcode;
1207 /* Drop leading whitespace */
1211 /* find the op code end */
1212 for (op_start = op_end = str;
1213 *op_end != 0 && *op_end != ' ';
1220 if (op_end == op_start)
1222 as_bad (_("can't find opcode "));
1228 opcode = (opcode_entry_type *) hash_find (opcode_hash_control,
1234 as_bad (_("unknown opcode"));
1238 if (opcode->opcode == 250)
1240 /* was really a pseudo op */
1245 char *old = input_line_pointer;
1249 input_line_pointer = op_end;
1253 while (*input_line_pointer == ' ')
1254 input_line_pointer++;
1255 p = (pseudo_typeS *) (opcode->func);
1257 (p->poc_handler) (p->poc_val);
1258 input_line_pointer = old;
1263 input_line_pointer = get_operands (opcode, op_end,
1265 prev_opcode = opcode;
1267 opcode = get_specific (opcode, operand);
1271 /* Couldn't find an opcode which matched the operands */
1272 char *where = frag_more (2);
1277 as_bad (_("Can't find opcode to match operands"));
1281 build_bytes (opcode, operand);
1286 tc_crawl_symbol_chain (headers)
1287 object_headers *headers;
1289 printf (_("call to tc_crawl_symbol_chain \n"));
1293 md_undefined_symbol (name)
1300 tc_headers_hook (headers)
1301 object_headers *headers;
1303 printf (_("call to tc_headers_hook \n"));
1306 /* Various routines to kill one day */
1307 /* Equal to MAX_PRECISION in atof-ieee.c */
1308 #define MAX_LITTLENUMS 6
1310 /* Turn a string in input_line_pointer into a floating point constant of type
1311 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1312 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1315 md_atof (type, litP, sizeP)
1321 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1322 LITTLENUM_TYPE *wordP;
1354 return _("Bad call to MD_ATOF()");
1356 t = atof_ieee (input_line_pointer, type, words);
1358 input_line_pointer = t;
1360 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1361 for (wordP = words; prec--;)
1363 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1364 litP += sizeof (LITTLENUM_TYPE);
1369 CONST char *md_shortopts = "z:";
1370 struct option md_longopts[] = {
1371 {NULL, no_argument, NULL, 0}
1373 size_t md_longopts_size = sizeof(md_longopts);
1376 md_parse_option (c, arg)
1383 if (!strcmp (arg, "8001"))
1385 else if (!strcmp (arg, "8002"))
1389 as_bad (_("invalid architecture -z%s"), arg);
1402 md_show_usage (stream)
1405 fprintf(stream, _("\
1407 -z8001 generate segmented code\n\
1408 -z8002 generate unsegmented code\n"));
1412 tc_aout_fix_to_chars ()
1414 printf (_("call to tc_aout_fix_to_chars \n"));
1419 md_convert_frag (headers, seg, fragP)
1420 object_headers *headers;
1424 printf (_("call to md_convert_frag \n"));
1429 md_section_align (seg, size)
1433 return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1438 md_apply_fix (fixP, val)
1442 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1444 switch (fixP->fx_r_type)
1447 buf[0] = (buf[0] & 0xf0) | ((buf[0] + val) & 0xf);
1453 /* if (val != 0) abort();*/
1459 /* if (val != 0) abort();*/
1466 *buf++ = (val >> 8);
1470 *buf++ = (val >> 24);
1471 *buf++ = (val >> 16);
1472 *buf++ = (val >> 8);
1477 *buf++ = (val >> 16);
1479 *buf++ = (val >> 8);
1485 md_number_to_chars (buf, val, fixP->fx_size);
1495 md_estimate_size_before_relax (fragP, segment_type)
1496 register fragS *fragP;
1497 register segT segment_type;
1499 printf (_("call tomd_estimate_size_before_relax \n"));
1503 /* Put number into target byte order */
1506 md_number_to_chars (ptr, use, nbytes)
1511 number_to_chars_bigendian (ptr, use, nbytes);
1514 md_pcrel_from (fixP)
1521 tc_coff_symbol_emit_hook (s)
1527 tc_reloc_mangle (fix_ptr, intr, base)
1529 struct internal_reloc *intr;
1533 symbolS *symbol_ptr;
1535 if (fix_ptr->fx_addsy &&
1538 symbolS *add = fix_ptr->fx_addsy;
1539 symbolS *sub = fix_ptr->fx_subsy;
1540 if (S_GET_SEGMENT(add) != S_GET_SEGMENT(sub))
1542 as_bad(_("Can't subtract symbols in different sections %s %s"),
1543 S_GET_NAME(add), S_GET_NAME(sub));
1546 int diff = S_GET_VALUE(add) - S_GET_VALUE(sub);
1547 fix_ptr->fx_addsy = 0;
1548 fix_ptr->fx_subsy = 0;
1549 fix_ptr->fx_offset += diff;
1552 symbol_ptr = fix_ptr->fx_addsy;
1554 /* If this relocation is attached to a symbol then it's ok
1556 if (fix_ptr->fx_r_type == 0)
1558 /* cons likes to create reloc32's whatever the size of the reloc.. */
1559 switch (fix_ptr->fx_size)
1562 intr->r_type = R_IMM16;
1565 intr->r_type = R_IMM8;
1568 intr->r_type = R_IMM32;
1577 intr->r_type = fix_ptr->fx_r_type;
1580 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1581 intr->r_offset = fix_ptr->fx_offset;
1584 intr->r_symndx = symbol_ptr->sy_number;
1586 intr->r_symndx = -1;