1 /* tc-z8k.c -- Assemble code for the Zilog Z800N
2 Copyright (C) 1992 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
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 Written By Steve Chamberlain
27 #include "../opcodes/z8k-opc.h"
33 const char comment_chars[] =
35 const char line_separator_chars[] =
37 const char line_comment_chars[] = { '#', 0};
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;
73 record_alignment(now_seg,1);
75 void obj_coff_section();
80 if (isdigit(c)) return c - '0';
81 if (islower(c)) return c - 'a' + 10;
88 if (*input_line_pointer == '\'') {
91 c = *input_line_pointer++;
94 c = (tohex(input_line_pointer[0]) << 4)
95 | tohex(input_line_pointer[1]);
96 input_line_pointer+=2;
98 FRAG_APPEND_1_CHAR(c);
99 c = *input_line_pointer++;
101 demand_empty_rest_of_line();
105 const pseudo_typeS md_pseudo_table[] =
111 {"form", listing_psize, 0},
112 {"heading", listing_title, 0},
113 {"import", s_ignore, 0},
114 {"page", listing_eject, 0},
115 {"program", s_ignore, 0},
116 {"z8001", s_segm, 0},
117 {"z8002", s_unseg, 0},
121 {"unsegm", s_unseg, 0},
122 {"name", s_app_file, 0},
123 {"global",s_globl,0},
128 {"rsect",obj_coff_section,0},
129 {"sect",obj_coff_section,0},
135 const char EXP_CHARS[] = "eE";
137 /* Chars that mean this number is a floating point constant */
140 const char FLT_CHARS[] = "rRsSfFdDxXpP";
142 const relax_typeS md_relax_table[1];
144 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
149 opcode_entry_type *opcode;
150 char *prev_name = "";
153 opcode_hash_control = hash_new ();
155 for (opcode = z8k_table; opcode->name; opcode++)
157 /* Only enter unique codes into the table */
158 char *src = opcode->name;
160 if (strcmp (opcode->name, prev_name))
162 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
166 prev_name = opcode->name;
169 /* default to z8002 */
172 /* insert the pseudo ops too */
173 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
175 opcode_entry_type *fake_opcode;
176 fake_opcode = (opcode_entry_type*)malloc(sizeof(opcode_entry_type));
177 fake_opcode->name = md_pseudo_table[idx].poc_name,
178 fake_opcode->func = (void *)(md_pseudo_table+idx);
179 fake_opcode->opcode = 250;
181 hash_insert(opcode_hash_control,fake_opcode->name,fake_opcode);
192 typedef struct z8k_op
194 char regsize; /* 'b','w','r','q' */
195 unsigned int reg; /* 0..15 */
199 unsigned int x_reg; /* any other register associated with the mode */
200 expressionS exp; /* any expression */
205 static expressionS *da_operand;
206 static expressionS *imm_operand;
212 DEFUN (whatreg, (reg, src),
216 if (isdigit (src[1]))
218 *reg = (src[0] - '0') * 10 + src[1] - '0';
223 *reg = (src[0] - '0');
235 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
244 /* try and parse a reg name, returns number of chars consumed */
246 DEFUN (parse_reg, (src, mode, reg),
253 if (src[0] == 's' && src[1]=='p') {
254 if (segmented_mode) {
255 *mode = CLASS_REG_LONG;
260 *mode = CLASS_REG_WORD;
269 *mode = CLASS_REG_LONG;
270 res = whatreg (reg, src + 2);
272 else if (src[1] == 'h' )
274 *mode = CLASS_REG_BYTE;
275 res = whatreg (reg, src + 2) ;
277 else if (src[1] == 'l' )
279 *mode = CLASS_REG_BYTE;
280 res = whatreg (reg, src + 2);
283 else if (src[1] == 'q')
285 *mode = CLASS_REG_QUAD;
286 res = whatreg (reg, src + 2);
290 *mode = CLASS_REG_WORD;
291 res = whatreg (reg, src + 1);
299 DEFUN (parse_exp, (s, op),
303 char *save = input_line_pointer;
307 input_line_pointer = s;
309 new = input_line_pointer;
310 input_line_pointer = save;
311 if (SEG_NORMAL (seg))
322 as_bad ("Missing operand");
325 as_bad ("Don't understand operand of type %s", segment_name (seg));
330 /* The many forms of operand:
349 DEFUN (checkfor, (ptr, what),
357 as_bad ("expected %c", what);
362 /* Make sure the mode supplied is the size of a word */
364 DEFUN (regword, (mode, string),
373 as_bad ("register is wrong size for a word %s", string);
377 /* Make sure the mode supplied is the size of an address */
379 DEFUN (regaddr, (mode, string),
385 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
388 as_bad ("register is wrong size for address %s", string);
399 struct cc_names table[] =
427 DEFUN (get_cc_operand, (ptr, mode, dst),
429 struct z8k_op *mode AND
439 mode->mode = CLASS_CC;
440 for (i = 0; table[i].name; i++)
444 for (j = 0; table[i].name[j]; j++)
446 if (table[i].name[j] != src[j])
449 the_cc = table[i].value;
459 DEFUN (get_operand, (ptr, mode, dst),
461 struct z8k_op *mode AND
476 mode->mode = CLASS_IMM;
477 imm_operand = &(mode->exp);
478 src = parse_exp (src + 1, &(mode->exp));
480 else if (*src == '@')
484 mode->mode = CLASS_IR;
485 src = parse_reg (src + 1, &d, &mode->reg);
491 end = parse_reg (src, &mode->mode, ®n);
501 end = parse_reg (src, &nw, &nr);
509 as_bad ("Missing ) in ra(rb)");
516 regaddr (mode->mode, "ra(rb) ra");
517 regword (mode->mode, "ra(rb) rb");
518 mode->mode = CLASS_BX;
528 src = parse_exp (src, &(mode->exp));
529 src = checkfor (src, ')');
530 mode->mode = CLASS_BA;
533 imm_operand = &(mode->exp);
545 src = parse_exp (src, &(mode->exp));
549 end = parse_reg (src, &(mode->mode), ®n);
550 regword (mode->mode, "addr(Ra) ra");
551 mode->mode = CLASS_X;
554 da_operand = &(mode->exp);
555 src = checkfor (end, ')');
559 /* Just an address */
560 mode->mode = CLASS_DA;
563 da_operand = &(mode->exp);
572 DEFUN (get_operands, (opcode, op_end, operand),
573 opcode_entry_type * opcode AND
579 switch (opcode->noperands)
588 if (opcode->arg_info[0] == CLASS_CC)
590 get_cc_operand (&ptr, operand + 0, 0);
595 get_operand (&ptr, operand + 0, 0);
602 if (opcode->arg_info[0] == CLASS_CC)
604 get_cc_operand (&ptr, operand + 0, 0);
609 get_operand (&ptr, operand + 0, 0);
615 get_operand (&ptr, operand + 1, 1);
620 get_operand (&ptr, operand + 0, 0);
623 get_operand (&ptr, operand + 1, 1);
626 get_operand (&ptr, operand + 2, 2);
631 get_operand (&ptr, operand + 0, 0);
634 get_operand (&ptr, operand + 1, 1);
637 get_operand (&ptr, operand + 2, 2);
640 get_cc_operand (&ptr, operand + 3, 3);
649 /* Passed a pointer to a list of opcodes which use different
650 addressing modes, return the opcode which matches the opcodes
656 DEFUN (get_specific, (opcode, operands),
657 opcode_entry_type * opcode AND
661 opcode_entry_type *this_try = opcode;
663 unsigned int noperands = opcode->noperands;
665 unsigned int dispreg;
666 unsigned int this_index = opcode->idx;
668 while (this_index == opcode->idx && !found)
673 for (i = 0; i < noperands; i++)
675 int mode = operands[i].mode;
677 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
679 /* it could be an pc rel operand, if this is a da mode and
680 we like disps, then insert it */
682 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
684 /* This is the case */
685 operands[i].mode = CLASS_DISP;
687 else if (mode == CLASS_BA && this_try->arg_info[i])
689 /* Can't think of a way to turn what we've been given into
690 something that's ok */
696 switch (mode & CLASS_MASK)
711 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
726 DEFUN (check_operand, (operand, width, string),
727 struct z8k_op *operand AND
728 unsigned int width AND
731 if (operand->exp.X_add_symbol == 0
732 && operand->exp.X_subtract_symbol == 0)
735 /* No symbol involved, let's look at offset, it's dangerous if any of
736 the high bits are not 0 or ff's, find out by oring or anding with
737 the width and seeing if the answer is 0 or all fs*/
738 if ((operand->exp.X_add_number & ~width) != 0 &&
739 (operand->exp.X_add_number | width) != (~0))
741 as_warn ("operand %s0x%x out of range.", string, operand->exp.X_add_number);
747 static char buffer[20];
750 DEFUN (newfix, (ptr, type, operand),
753 expressionS * operand)
755 if (operand->X_add_symbol
756 || operand->X_subtract_symbol
757 || operand->X_add_number)
762 operand->X_add_symbol,
763 operand->X_subtract_symbol,
764 operand->X_add_number,
771 DEFUN (apply_fix, (ptr, type, operand, size),
774 expressionS * operand AND
777 int n = operand->X_add_number;
779 operand->X_add_number = n;
780 newfix ((ptr - buffer) / 2, type, operand);
784 case 8: /* 8 nibbles == 32 bits */
789 case 4: /* 4 niblles == 16 bits */
803 /* Now we know what sort of opcodes it is, lets build the bytes -
805 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
807 DEFUN (build_bytes, (this_try, operand),
808 opcode_entry_type * this_try AND
809 struct z8k_op *operand)
815 char *output_ptr = buffer;
821 unsigned short *class_ptr;
823 frag_wane (frag_now);
826 memset (buffer, 20, 0);
827 class_ptr = this_try->byte_info;
830 for (nibble = 0; c = *class_ptr++; nibble++)
833 switch (c & CLASS_MASK)
839 /* Direct address, we don't cope with the SS mode right now */
842 da_operand->X_add_number |= 0x80000000;
843 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
847 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
853 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
858 *output_ptr = c & 0xf;
861 if (imm_operand->X_add_number==2)
865 else if (imm_operand->X_add_number != 1)
867 as_bad("immediate must be 1 or 2");
872 as_bad("immediate 1 or 2 expected");
877 *output_ptr++ = the_cc;
880 *output_ptr++ = c & 0xf;
883 if (reg[c & 0xf] == 0)
885 as_bad ("can't use R0 here");
892 /* Insert bit mattern of
894 *output_ptr++ = reg[c & 0xf];
897 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
904 switch (c & ARG_MASK)
907 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
910 imm_operand->X_add_number--;
911 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
914 imm_operand->X_add_number--;
915 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
918 imm_operand->X_add_number = -imm_operand->X_add_number;
920 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
923 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
927 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
937 /* Copy from the nibble buffer into the frag */
940 int length = (output_ptr - buffer) / 2;
942 char *fragp = frag_more (length);
944 while (src < output_ptr)
946 *fragp = (src[0] << 4) | src[1];
955 /* This is the guts of the machine-dependent assembler. STR points to a
956 machine dependent instruction. This funciton is supposed to emit
957 the frags/bytes it assembles to.
961 DEFUN (md_assemble, (str),
967 struct z8k_op operand[3];
968 opcode_entry_type *opcode;
969 opcode_entry_type *prev_opcode;
974 /* Drop leading whitespace */
978 /* find the op code end */
979 for (op_start = op_end = str;
980 *op_end != 0 && *op_end != ' ';
987 if (op_end == op_start)
989 as_bad ("can't find opcode ");
995 opcode = (opcode_entry_type *) hash_find (opcode_hash_control,
1001 as_bad ("unknown opcode");
1005 if (opcode->opcode == 250)
1007 /* was really a pseudo op */
1012 char *old = input_line_pointer;
1016 input_line_pointer = op_end;
1020 while (*input_line_pointer == ' ')
1021 input_line_pointer++;
1022 p = (pseudo_typeS *)(opcode->func);
1024 (p->poc_handler)(p->poc_val);
1025 input_line_pointer = old;
1029 input_line_pointer = get_operands (opcode, op_end,
1032 prev_opcode = opcode;
1034 opcode = get_specific (opcode, operand);
1038 /* Couldn't find an opcode which matched the operands */
1039 char *where = frag_more (2);
1044 as_bad ("Can't find opcode to match operands");
1048 build_bytes (opcode, operand);
1053 DEFUN (tc_crawl_symbol_chain, (headers),
1054 object_headers * headers)
1056 printf ("call to tc_crawl_symbol_chain \n");
1060 DEFUN (md_undefined_symbol, (name),
1067 DEFUN (tc_headers_hook, (headers),
1068 object_headers * headers)
1070 printf ("call to tc_headers_hook \n");
1078 /* Various routines to kill one day */
1079 /* Equal to MAX_PRECISION in atof-ieee.c */
1080 #define MAX_LITTLENUMS 6
1082 /* Turn a string in input_line_pointer into a floating point constant of type
1083 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1084 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1087 md_atof (type, litP, sizeP)
1093 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1094 LITTLENUM_TYPE *wordP;
1126 return "Bad call to MD_ATOF()";
1128 t = atof_ieee (input_line_pointer, type, words);
1130 input_line_pointer = t;
1132 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1133 for (wordP = words; prec--;)
1135 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1136 litP += sizeof (LITTLENUM_TYPE);
1138 return ""; /* Someone should teach Dean about null pointers */
1142 md_parse_option (argP, cntP, vecP)
1148 if (!strcmp(*argP,"z8001")) {
1151 else if (!strcmp(*argP,"z8002")) {
1159 int md_short_jump_size;
1162 tc_aout_fix_to_chars ()
1164 printf ("call to tc_aout_fix_to_chars \n");
1169 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1176 as_fatal ("failed sanity check.");
1180 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1182 addressT from_addr, to_addr;
1186 as_fatal ("failed sanity check.");
1190 md_convert_frag (headers, fragP)
1191 object_headers *headers;
1195 printf ("call to md_convert_frag \n");
1200 DEFUN (md_section_align, (seg, size),
1204 return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1209 md_apply_fix (fixP, val)
1213 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1215 switch (fixP->fx_r_type)
1218 buf[0] = (buf[0] & 0xf0) | ((buf[0] + val) & 0xf);
1224 /* if (val != 0) abort();*/
1231 *buf++ = (val >> 8);
1235 *buf++ = (val >> 24);
1236 *buf++ = (val >> 16);
1237 *buf++ = (val >> 8);
1242 *buf++ = (val >> 16);
1244 *buf++ = (val >> 8);
1256 DEFUN (md_operand, (expressionP), expressionS * expressionP)
1260 int md_long_jump_size;
1262 md_estimate_size_before_relax (fragP, segment_type)
1263 register fragS *fragP;
1264 register segT segment_type;
1266 printf ("call tomd_estimate_size_before_relax \n");
1270 /* Put number into target byte order */
1273 DEFUN (md_number_to_chars, (ptr, use, nbytes),
1281 *ptr++ = (use >> 24) & 0xff;
1283 *ptr++ = (use >> 16) & 0xff;
1285 *ptr++ = (use >> 8) & 0xff;
1287 *ptr++ = (use >> 0) & 0xff;
1294 md_pcrel_from (fixP)
1301 tc_coff_symbol_emit_hook ()
1306 tc_reloc_mangle (fix_ptr, intr, base)
1308 struct internal_reloc *intr;
1312 symbolS *symbol_ptr;
1314 symbol_ptr = fix_ptr->fx_addsy;
1316 /* If this relocation is attached to a symbol then it's ok
1318 if (fix_ptr->fx_r_type == 0)
1320 /* cons likes to create reloc32's whatever the size of the reloc..
1322 switch (fix_ptr->fx_size)
1326 intr->r_type = R_IMM16;
1329 intr->r_type = R_IMM8;
1332 intr->r_type = R_IMM32;
1342 intr->r_type = fix_ptr->fx_r_type;
1345 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1346 intr->r_offset = fix_ptr->fx_offset;
1349 intr->r_symndx = symbol_ptr->sy_number;
1351 intr->r_symndx = -1;