merge from gcc
[binutils.git] / gas / config / tc-z8k.c
blob355ac12c2bbbd89f6db1e178c1c6d5eab87caba2
1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001, 2002, 2003,
3 2005 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)
10 any later version.
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, 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
24 #include <stdio.h>
26 #include "as.h"
27 #include "bfd.h"
28 #include "safe-ctype.h"
29 #define DEFINE_TABLE
30 #include "opcodes/z8k-opc.h"
32 const char comment_chars[] = "!";
33 const char line_comment_chars[] = "#";
34 const char line_separator_chars[] = ";";
36 extern int machine;
37 extern int coff_flags;
38 int segmented_mode;
40 /* This is non-zero if target was set from the command line. */
41 static int z8k_target_from_cmdline;
43 static void
44 s_segm (int segm)
46 if (segm)
48 segmented_mode = 1;
49 bfd_set_arch_mach (stdoutput, TARGET_ARCH, bfd_mach_z8001);
51 else
53 segmented_mode = 0;
54 bfd_set_arch_mach (stdoutput, TARGET_ARCH, bfd_mach_z8002);
58 static void
59 even (int ignore ATTRIBUTE_UNUSED)
61 frag_align (1, 0, 0);
62 record_alignment (now_seg, 1);
65 static int
66 tohex (int c)
68 if (ISDIGIT (c))
69 return c - '0';
70 if (ISLOWER (c))
71 return c - 'a' + 10;
72 return c - 'A' + 10;
75 static void
76 sval (int ignore ATTRIBUTE_UNUSED)
78 SKIP_WHITESPACE ();
79 if (*input_line_pointer == '\'')
81 int c;
82 input_line_pointer++;
83 c = *input_line_pointer++;
84 while (c != '\'')
86 if (c == '%')
88 c = (tohex (input_line_pointer[0]) << 4)
89 | tohex (input_line_pointer[1]);
90 input_line_pointer += 2;
92 FRAG_APPEND_1_CHAR (c);
93 c = *input_line_pointer++;
95 demand_empty_rest_of_line ();
99 /* This table describes all the machine specific pseudo-ops the assembler
100 has to support. The fields are:
101 pseudo-op name without dot
102 function to call to execute this pseudo-op
103 Integer arg to pass to the function
106 const pseudo_typeS md_pseudo_table[] = {
107 {"int" , cons , 2},
108 {"data.b" , cons , 1},
109 {"data.w" , cons , 2},
110 {"data.l" , cons , 4},
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 , 1},
117 {"z8002" , s_segm , 0},
119 {"segm" , s_segm , 1},
120 {"unsegm" , s_segm , 0},
121 {"unseg" , s_segm , 0},
122 {"name" , s_app_file , 0},
123 {"global" , s_globl , 0},
124 {"wval" , cons , 2},
125 {"lval" , cons , 4},
126 {"bval" , cons , 1},
127 {"sval" , sval , 0},
128 {"rsect" , obj_coff_section, 0},
129 {"sect" , obj_coff_section, 0},
130 {"block" , s_space , 0},
131 {"even" , even , 0},
132 {0 , 0 , 0}
135 const char EXP_CHARS[] = "eE";
137 /* Chars that mean this number is a floating point constant.
138 As in 0f12.456
139 or 0d1.2345e12 */
140 const char FLT_CHARS[] = "rRsSfFdDxXpP";
142 /* Opcode mnemonics. */
143 static struct hash_control *opcode_hash_control;
145 void
146 md_begin (void)
148 const opcode_entry_type *opcode;
149 int idx = -1;
151 opcode_hash_control = hash_new ();
153 for (opcode = z8k_table; opcode->name; opcode++)
155 /* Only enter unique codes into the table. */
156 if (idx != opcode->idx)
157 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
158 idx = opcode->idx;
161 /* Default to z8002. */
162 if (! z8k_target_from_cmdline)
163 s_segm (0);
165 /* Insert the pseudo ops, too. */
166 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
168 opcode_entry_type *fake_opcode;
169 fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
170 fake_opcode->name = md_pseudo_table[idx].poc_name;
171 fake_opcode->func = (void *) (md_pseudo_table + idx);
172 fake_opcode->opcode = 250;
173 hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
177 typedef struct z8k_op {
178 /* CLASS_REG_xxx. */
179 int regsize;
181 /* 0 .. 15. */
182 unsigned int reg;
184 int mode;
186 /* Any other register associated with the mode. */
187 unsigned int x_reg;
189 /* Any expression. */
190 expressionS exp;
191 } op_type;
193 static expressionS *da_operand;
194 static expressionS *imm_operand;
196 static int reg[16];
197 static int the_cc;
198 static int the_ctrl;
199 static int the_flags;
200 static int the_interrupt;
202 static char *
203 whatreg (unsigned int *reg, char *src)
205 if (ISDIGIT (src[1]))
207 *reg = (src[0] - '0') * 10 + src[1] - '0';
208 return src + 2;
210 else
212 *reg = (src[0] - '0');
213 return src + 1;
217 /* Parse operands
219 rh0-rh7, rl0-rl7
220 r0-r15
221 rr0-rr14
222 rq0--rq12
223 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
224 r0l,r0h,..r7l,r7h
225 @WREG
226 @WREG+
227 @-WREG
228 #const
231 /* Try to parse a reg name. Return a pointer to the first character
232 in SRC after the reg name. */
234 static char *
235 parse_reg (char *src, int *mode, unsigned int *reg)
237 char *res = 0;
238 char regno;
240 /* Check for stack pointer "sp" alias. */
241 if ((src[0] == 's' || src[0] == 'S')
242 && (src[1] == 'p' || src[1] == 'P')
243 && (src[2] == 0 || src[2] == ','))
245 if (segmented_mode)
247 *mode = CLASS_REG_LONG;
248 *reg = 14;
250 else
252 *mode = CLASS_REG_WORD;
253 *reg = 15;
255 return src + 2;
258 if (src[0] == 'r' || src[0] == 'R')
260 if (src[1] == 'r' || src[1] == 'R')
262 if (src[2] < '0' || src[2] > '9')
263 return res; /* Assume no register name but a label starting with 'rr'. */
264 *mode = CLASS_REG_LONG;
265 res = whatreg (reg, src + 2);
266 regno = *reg;
267 if (regno > 14)
268 as_bad (_("register rr%d out of range"), regno);
269 if (regno & 1)
270 as_bad (_("register rr%d does not exist"), regno);
272 else if (src[1] == 'h' || src[1] == 'H')
274 if (src[2] < '0' || src[2] > '9')
275 return res; /* Assume no register name but a label starting with 'rh'. */
276 *mode = CLASS_REG_BYTE;
277 res = whatreg (reg, src + 2);
278 regno = *reg;
279 if (regno > 7)
280 as_bad (_("register rh%d out of range"), regno);
282 else if (src[1] == 'l' || src[1] == 'L')
284 if (src[2] < '0' || src[2] > '9')
285 return res; /* Assume no register name but a label starting with 'rl'. */
286 *mode = CLASS_REG_BYTE;
287 res = whatreg (reg, src + 2);
288 regno = *reg;
289 if (regno > 7)
290 as_bad (_("register rl%d out of range"), regno);
291 *reg += 8;
293 else if (src[1] == 'q' || src[1] == 'Q')
295 if (src[2] < '0' || src[2] > '9')
296 return res; /* Assume no register name but a label starting with 'rq'. */
297 *mode = CLASS_REG_QUAD;
298 res = whatreg (reg, src + 2);
299 regno = *reg;
300 if (regno > 12)
301 as_bad (_("register rq%d out of range"), regno);
302 if (regno & 3)
303 as_bad (_("register rq%d does not exist"), regno);
305 else
307 if (src[1] < '0' || src[1] > '9')
308 return res; /* Assume no register name but a label starting with 'r'. */
309 *mode = CLASS_REG_WORD;
310 res = whatreg (reg, src + 1);
311 regno = *reg;
312 if (regno > 15)
313 as_bad (_("register r%d out of range"), regno);
316 return res;
319 static char *
320 parse_exp (char *s, expressionS *op)
322 char *save = input_line_pointer;
323 char *new;
325 input_line_pointer = s;
326 expression (op);
327 if (op->X_op == O_absent)
328 as_bad (_("missing operand"));
329 new = input_line_pointer;
330 input_line_pointer = save;
331 return new;
334 /* The many forms of operand:
336 <rb>
338 <rr>
339 <rq>
341 #exp
343 exp(r)
344 r(#exp)
345 r(r)
348 static char *
349 checkfor (char *ptr, char what)
351 if (*ptr == what)
352 ptr++;
353 else
354 as_bad (_("expected %c"), what);
356 return ptr;
359 /* Make sure the mode supplied is the size of a word. */
361 static void
362 regword (int mode, char *string)
364 int ok;
366 ok = CLASS_REG_WORD;
367 if (ok != mode)
369 as_bad (_("register is wrong size for a word %s"), string);
373 /* Make sure the mode supplied is the size of an address. */
375 static void
376 regaddr (int mode, char *string)
378 int ok;
380 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
381 if (ok != mode)
383 as_bad (_("register is wrong size for address %s"), string);
387 struct ctrl_names {
388 int value;
389 char *name;
392 static struct ctrl_names ctrl_table[] = {
393 { 0x1, "flags" }, /* ldctlb only. */
394 { 0x2, "fcw" }, /* ldctl only. Applies to all remaining control registers. */
395 { 0x3, "refresh" },
396 { 0x4, "psapseg" },
397 { 0x5, "psapoff" },
398 { 0x5, "psap" },
399 { 0x6, "nspseg" },
400 { 0x7, "nspoff" },
401 { 0x7, "nsp" },
402 { 0 , 0 }
405 static void
406 get_ctrl_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
408 char *src = *ptr;
409 int i, l;
411 while (*src == ' ')
412 src++;
414 mode->mode = CLASS_CTRL;
415 for (i = 0; ctrl_table[i].name; i++)
417 l = strlen (ctrl_table[i].name);
418 if (! strncasecmp (ctrl_table[i].name, src, l))
420 the_ctrl = ctrl_table[i].value;
421 if (*(src + l) && *(src + l) != ',')
422 break;
423 *ptr = src + l; /* Valid control name found: "consume" it. */
424 return;
427 the_ctrl = 0;
430 struct flag_names {
431 int value;
432 char *name;
435 static struct flag_names flag_table[] = {
436 { 0x1, "P" },
437 { 0x1, "V" },
438 { 0x2, "S" },
439 { 0x4, "Z" },
440 { 0x8, "C" },
441 { 0x0, "+" },
442 { 0x0, "," },
443 { 0, 0 }
446 static void
447 get_flags_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
449 char *src = *ptr;
450 char c;
451 int i;
452 int j;
454 while (*src == ' ')
455 src++;
457 mode->mode = CLASS_FLAGS;
458 the_flags = 0;
459 for (j = 0; j <= 9; j++)
461 if (!src[j])
462 goto done;
463 c = TOUPPER(src[j]);
464 for (i = 0; flag_table[i].name; i++)
466 if (flag_table[i].name[0] == c)
468 the_flags = the_flags | flag_table[i].value;
469 goto match;
472 goto done;
473 match:
476 done:
477 *ptr = src + j;
480 struct interrupt_names {
481 int value;
482 char *name;
485 static struct interrupt_names intr_table[] = {
486 { 0x1, "nvi" },
487 { 0x2, "vi" },
488 { 0x3, "both" },
489 { 0x3, "all" },
490 { 0, 0 }
493 static void
494 get_interrupt_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
496 char *src = *ptr;
497 int i, l;
499 while (*src == ' ')
500 src++;
502 mode->mode = CLASS_IMM;
503 the_interrupt = 0;
505 while (*src)
507 for (i = 0; intr_table[i].name; i++)
509 l = strlen (intr_table[i].name);
510 if (! strncasecmp (intr_table[i].name, src, l))
512 the_interrupt |= intr_table[i].value;
513 if (*(src + l) && *(src + l) != ',')
515 *ptr = src + l;
516 invalid:
517 as_bad (_("unknown interrupt %s"), src);
518 while (**ptr && ! is_end_of_line[(unsigned char) **ptr])
519 (*ptr)++; /* Consume rest of line. */
520 return;
522 src += l;
523 if (! *src)
525 *ptr = src;
526 return;
530 if (*src == ',')
531 src++;
532 else
534 *ptr = src;
535 goto invalid;
539 /* No interrupt type specified, opcode won't do anything. */
540 as_warn (_("opcode has no effect"));
541 the_interrupt = 0x0;
544 struct cc_names {
545 int value;
546 char *name;
549 static struct cc_names table[] = {
550 { 0x0, "f" },
551 { 0x1, "lt" },
552 { 0x2, "le" },
553 { 0x3, "ule" },
554 { 0x4, "ov/pe" },
555 { 0x4, "ov" },
556 { 0x4, "pe/ov" },
557 { 0x4, "pe" },
558 { 0x5, "mi" },
559 { 0x6, "eq" },
560 { 0x6, "z" },
561 { 0x7, "c/ult" },
562 { 0x7, "c" },
563 { 0x7, "ult/c" },
564 { 0x7, "ult" },
565 { 0x8, "t" },
566 { 0x9, "ge" },
567 { 0xa, "gt" },
568 { 0xb, "ugt" },
569 { 0xc, "nov/po" },
570 { 0xc, "nov" },
571 { 0xc, "po/nov" },
572 { 0xc, "po" },
573 { 0xd, "pl" },
574 { 0xe, "ne" },
575 { 0xe, "nz" },
576 { 0xf, "nc/uge" },
577 { 0xf, "nc" },
578 { 0xf, "uge/nc" },
579 { 0xf, "uge" },
580 { 0 , 0 }
583 static void
584 get_cc_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
586 char *src = *ptr;
587 int i, l;
589 while (*src == ' ')
590 src++;
592 mode->mode = CLASS_CC;
593 for (i = 0; table[i].name; i++)
595 l = strlen (table[i].name);
596 if (! strncasecmp (table[i].name, src, l))
598 the_cc = table[i].value;
599 if (*(src + l) && *(src + l) != ',')
600 break;
601 *ptr = src + l; /* Valid cc found: "consume" it. */
602 return;
605 the_cc = 0x8; /* Not recognizing the cc defaults to t. (Assuming no cc present.) */
608 static void
609 get_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
611 char *src = *ptr;
612 char *end;
614 mode->mode = 0;
616 while (*src == ' ')
617 src++;
618 if (*src == '#')
620 mode->mode = CLASS_IMM;
621 imm_operand = &(mode->exp);
622 src = parse_exp (src + 1, &(mode->exp));
624 else if (*src == '@')
626 mode->mode = CLASS_IR;
627 src = parse_reg (src + 1, &mode->regsize, &mode->reg);
629 else
631 unsigned int regn;
633 end = parse_reg (src, &mode->mode, &regn);
635 if (end)
637 int nw;
638 unsigned int nr;
640 src = end;
641 if (*src == '(')
643 src++;
644 end = parse_reg (src, &nw, &nr);
645 if (end)
647 /* Got Ra(Rb). */
648 src = end;
650 if (*src != ')')
651 as_bad (_("Missing ) in ra(rb)"));
652 else
653 src++;
655 regaddr (mode->mode, "ra(rb) ra");
656 mode->mode = CLASS_BX;
657 mode->reg = regn;
658 mode->x_reg = nr;
659 reg[ARG_RX] = nr;
661 else
663 /* Got Ra(disp). */
664 if (*src == '#')
665 src++;
666 src = parse_exp (src, &(mode->exp));
667 src = checkfor (src, ')');
668 mode->mode = CLASS_BA;
669 mode->reg = regn;
670 mode->x_reg = 0;
671 imm_operand = &(mode->exp);
674 else
676 mode->reg = regn;
677 mode->x_reg = 0;
680 else
682 /* No initial reg. */
683 src = parse_exp (src, &(mode->exp));
684 if (*src == '(')
686 src++;
687 end = parse_reg (src, &(mode->mode), &regn);
688 regword (mode->mode, "addr(Ra) ra");
689 mode->mode = CLASS_X;
690 mode->reg = regn;
691 mode->x_reg = 0;
692 da_operand = &(mode->exp);
693 src = checkfor (end, ')');
695 else
697 /* Just an address. */
698 mode->mode = CLASS_DA;
699 mode->reg = 0;
700 mode->x_reg = 0;
701 da_operand = &(mode->exp);
705 *ptr = src;
708 static char *
709 get_operands (const opcode_entry_type *opcode, char *op_end, op_type *operand)
711 char *ptr = op_end;
712 char *savptr;
714 switch (opcode->noperands)
716 case 0:
717 operand[0].mode = 0;
718 operand[1].mode = 0;
719 while (*ptr == ' ')
720 ptr++;
721 break;
723 case 1:
724 if (opcode->arg_info[0] == CLASS_CC)
726 get_cc_operand (&ptr, operand + 0, 0);
727 while (*ptr == ' ')
728 ptr++;
729 if (*ptr && ! is_end_of_line[(unsigned char) *ptr])
731 as_bad (_("invalid condition code '%s'"), ptr);
732 while (*ptr && ! is_end_of_line[(unsigned char) *ptr])
733 ptr++; /* Consume rest of line. */
736 else if (opcode->arg_info[0] == CLASS_FLAGS)
738 get_flags_operand (&ptr, operand + 0, 0);
739 while (*ptr == ' ')
740 ptr++;
741 if (*ptr && ! is_end_of_line[(unsigned char) *ptr])
743 as_bad (_("invalid flag '%s'"), ptr);
744 while (*ptr && ! is_end_of_line[(unsigned char) *ptr])
745 ptr++; /* Consume rest of line. */
748 else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2)))
749 get_interrupt_operand (&ptr, operand + 0, 0);
750 else
751 get_operand (&ptr, operand + 0, 0);
753 operand[1].mode = 0;
754 break;
756 case 2:
757 savptr = ptr;
758 if (opcode->arg_info[0] == CLASS_CC)
760 get_cc_operand (&ptr, operand + 0, 0);
761 while (*ptr == ' ')
762 ptr++;
763 if (*ptr != ',' && strchr (ptr + 1, ','))
765 savptr = ptr;
766 while (*ptr != ',')
767 ptr++;
768 *ptr = 0;
769 ptr++;
770 as_bad (_("invalid condition code '%s'"), savptr);
773 else if (opcode->arg_info[0] == CLASS_CTRL)
775 get_ctrl_operand (&ptr, operand + 0, 0);
777 if (the_ctrl == 0)
779 ptr = savptr;
780 get_operand (&ptr, operand + 0, 0);
782 if (ptr == 0)
783 return NULL;
784 if (*ptr == ',')
785 ptr++;
786 get_ctrl_operand (&ptr, operand + 1, 1);
787 if (the_ctrl == 0)
788 return NULL;
789 return ptr;
792 else
793 get_operand (&ptr, operand + 0, 0);
795 if (ptr == 0)
796 return NULL;
797 if (*ptr == ',')
798 ptr++;
799 get_operand (&ptr, operand + 1, 1);
800 break;
802 case 3:
803 get_operand (&ptr, operand + 0, 0);
804 if (*ptr == ',')
805 ptr++;
806 get_operand (&ptr, operand + 1, 1);
807 if (*ptr == ',')
808 ptr++;
809 get_operand (&ptr, operand + 2, 2);
810 break;
812 case 4:
813 get_operand (&ptr, operand + 0, 0);
814 if (*ptr == ',')
815 ptr++;
816 get_operand (&ptr, operand + 1, 1);
817 if (*ptr == ',')
818 ptr++;
819 get_operand (&ptr, operand + 2, 2);
820 if (*ptr == ',')
821 ptr++;
822 get_cc_operand (&ptr, operand + 3, 3);
823 break;
825 default:
826 abort ();
829 return ptr;
832 /* Passed a pointer to a list of opcodes which use different
833 addressing modes. Return the opcode which matches the opcodes
834 provided. */
836 static opcode_entry_type *
837 get_specific (opcode_entry_type *opcode, op_type *operands)
839 opcode_entry_type *this_try = opcode;
840 int found = 0;
841 unsigned int noperands = opcode->noperands;
843 int this_index = opcode->idx;
845 while (this_index == opcode->idx && !found)
847 unsigned int i;
849 this_try = opcode++;
850 for (i = 0; i < noperands; i++)
852 unsigned int mode = operands[i].mode;
854 if (((mode & CLASS_MASK) == CLASS_IR) && ((this_try->arg_info[i] & CLASS_MASK) == CLASS_IRO))
856 mode = operands[i].mode = (operands[i].mode & ~CLASS_MASK) | CLASS_IRO;
859 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
861 /* It could be a pc rel operand, if this is a da mode
862 and we like disps, then insert it. */
864 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
866 /* This is the case. */
867 operands[i].mode = CLASS_DISP;
869 else if (mode == CLASS_BA && this_try->arg_info[i])
871 /* Can't think of a way to turn what we've been
872 given into something that's OK. */
873 goto fail;
875 else if (this_try->arg_info[i] & CLASS_PR)
877 if (mode == CLASS_REG_LONG && segmented_mode)
879 /* OK. */
881 else if (mode == CLASS_REG_WORD && !segmented_mode)
883 /* OK. */
885 else
886 goto fail;
888 else
889 goto fail;
891 switch (mode & CLASS_MASK)
893 default:
894 break;
895 case CLASS_IRO:
896 if (operands[i].regsize != CLASS_REG_WORD)
897 as_bad (_("invalid indirect register size"));
898 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
899 break;
900 case CLASS_IR:
901 if ((segmented_mode && operands[i].regsize != CLASS_REG_LONG)
902 || (!segmented_mode && operands[i].regsize != CLASS_REG_WORD))
903 as_bad (_("invalid indirect register size"));
904 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
905 break;
906 case CLASS_X:
907 case CLASS_BA:
908 case CLASS_BX:
909 case CLASS_DISP:
910 case CLASS_REG:
911 case CLASS_REG_WORD:
912 case CLASS_REG_BYTE:
913 case CLASS_REG_QUAD:
914 case CLASS_REG_LONG:
915 case CLASS_REGN0:
916 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
917 break;
918 case CLASS_CTRL:
919 if (this_try->opcode == OPC_ldctlb && the_ctrl != 1)
920 as_bad (_("invalid control register name"));
921 break;
925 found = 1;
926 fail:
929 if (found)
930 return this_try;
931 else
932 return 0;
935 static char buffer[20];
937 static void
938 newfix (int ptr, int type, int size, expressionS *operand)
940 int is_pcrel = 0;
941 fixS *fixP;
943 /* Size is in nibbles. */
944 if (operand->X_add_symbol
945 || operand->X_op_symbol
946 || operand->X_add_number)
948 switch(type)
950 case BFD_RELOC_8_PCREL:
951 case BFD_RELOC_Z8K_CALLR:
952 case BFD_RELOC_Z8K_DISP7:
953 is_pcrel = 1;
955 fixP = fix_new_exp (frag_now, ptr, size / 2,
956 operand, is_pcrel, type);
957 if (is_pcrel)
958 fixP->fx_no_overflow = 1;
962 static char *
963 apply_fix (char *ptr, int type, expressionS *operand, int size)
965 long n = operand->X_add_number;
967 /* size is in nibbles. */
969 newfix ((ptr - buffer) / 2, type, size + 1, operand);
970 switch (size)
972 case 8: /* 8 nibbles == 32 bits. */
973 *ptr++ = n >> 28;
974 *ptr++ = n >> 24;
975 *ptr++ = n >> 20;
976 *ptr++ = n >> 16;
977 case 4: /* 4 nibbles == 16 bits. */
978 *ptr++ = n >> 12;
979 *ptr++ = n >> 8;
980 case 2:
981 *ptr++ = n >> 4;
982 case 1:
983 *ptr++ = n >> 0;
984 break;
986 return ptr;
989 /* Now we know what sort of opcodes it is. Let's build the bytes. */
991 static void
992 build_bytes (opcode_entry_type *this_try, struct z8k_op *operand ATTRIBUTE_UNUSED)
994 char *output_ptr = buffer;
995 int c;
996 int nibble;
997 unsigned int *class_ptr;
999 frag_wane (frag_now);
1000 frag_new (0);
1002 if (frag_room () < 8)
1003 frag_grow (8); /* Make room for maximum instruction size. */
1005 memset (buffer, 0, sizeof (buffer));
1006 class_ptr = this_try->byte_info;
1008 for (nibble = 0; (c = *class_ptr++); nibble++)
1011 switch (c & CLASS_MASK)
1013 default:
1014 abort ();
1016 case CLASS_ADDRESS:
1017 /* Direct address, we don't cope with the SS mode right now. */
1018 if (segmented_mode)
1020 /* da_operand->X_add_number |= 0x80000000; -- Now set at relocation time. */
1021 output_ptr = apply_fix (output_ptr, BFD_RELOC_32, da_operand, 8);
1023 else
1025 output_ptr = apply_fix (output_ptr, BFD_RELOC_16, da_operand, 4);
1027 da_operand = 0;
1028 break;
1029 case CLASS_DISP8:
1030 /* pc rel 8 bit */
1031 output_ptr = apply_fix (output_ptr, BFD_RELOC_8_PCREL, da_operand, 2);
1032 da_operand = 0;
1033 break;
1035 case CLASS_0DISP7:
1036 /* pc rel 7 bit */
1037 *output_ptr = 0;
1038 output_ptr = apply_fix (output_ptr, BFD_RELOC_Z8K_DISP7, da_operand, 2);
1039 da_operand = 0;
1040 break;
1042 case CLASS_1DISP7:
1043 /* pc rel 7 bit */
1044 *output_ptr = 0x80;
1045 output_ptr = apply_fix (output_ptr, BFD_RELOC_Z8K_DISP7, da_operand, 2);
1046 output_ptr[-2] = 0x8;
1047 da_operand = 0;
1048 break;
1050 case CLASS_BIT_1OR2:
1051 *output_ptr = c & 0xf;
1052 if (imm_operand)
1054 if (imm_operand->X_add_number == 2)
1055 *output_ptr |= 2;
1056 else if (imm_operand->X_add_number != 1)
1057 as_bad (_("immediate must be 1 or 2"));
1059 else
1060 as_bad (_("immediate 1 or 2 expected"));
1061 output_ptr++;
1062 break;
1063 case CLASS_CC:
1064 *output_ptr++ = the_cc;
1065 break;
1066 case CLASS_0CCC:
1067 if (the_ctrl < 2 || the_ctrl > 7)
1068 as_bad (_("invalid control register name"));
1069 *output_ptr++ = the_ctrl;
1070 break;
1071 case CLASS_1CCC:
1072 if (the_ctrl < 2 || the_ctrl > 7)
1073 as_bad (_("invalid control register name"));
1074 *output_ptr++ = the_ctrl | 0x8;
1075 break;
1076 case CLASS_00II:
1077 *output_ptr++ = (~the_interrupt & 0x3);
1078 break;
1079 case CLASS_01II:
1080 *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1081 break;
1082 case CLASS_FLAGS:
1083 *output_ptr++ = the_flags;
1084 break;
1085 case CLASS_IGNORE:
1086 case CLASS_BIT:
1087 *output_ptr++ = c & 0xf;
1088 break;
1089 case CLASS_REGN0:
1090 if (reg[c & 0xf] == 0)
1091 as_bad (_("can't use R0 here"));
1092 /* Fall through. */
1093 case CLASS_REG:
1094 case CLASS_REG_BYTE:
1095 case CLASS_REG_WORD:
1096 case CLASS_REG_LONG:
1097 case CLASS_REG_QUAD:
1098 /* Insert bit mattern of right reg. */
1099 *output_ptr++ = reg[c & 0xf];
1100 break;
1101 case CLASS_DISP:
1102 switch (c & ARG_MASK)
1104 case ARG_DISP12:
1105 output_ptr = apply_fix (output_ptr, BFD_RELOC_Z8K_CALLR, da_operand, 4);
1106 break;
1107 case ARG_DISP16:
1108 output_ptr = apply_fix (output_ptr, BFD_RELOC_16_PCREL, da_operand, 4);
1109 break;
1110 default:
1111 output_ptr = apply_fix (output_ptr, BFD_RELOC_16, da_operand, 4);
1113 da_operand = 0;
1114 break;
1116 case CLASS_IMM:
1118 switch (c & ARG_MASK)
1120 case ARG_NIM4:
1121 if (imm_operand->X_add_number > 15)
1122 as_bad (_("immediate value out of range"));
1123 imm_operand->X_add_number = -imm_operand->X_add_number;
1124 output_ptr = apply_fix (output_ptr, BFD_RELOC_Z8K_IMM4L, imm_operand, 1);
1125 break;
1126 /*case ARG_IMMNMINUS1: not used. */
1127 case ARG_IMM4M1:
1128 imm_operand->X_add_number--;
1129 /* Drop through. */
1130 case ARG_IMM4:
1131 if (imm_operand->X_add_number > 15)
1132 as_bad (_("immediate value out of range"));
1133 output_ptr = apply_fix (output_ptr, BFD_RELOC_Z8K_IMM4L, imm_operand, 1);
1134 break;
1135 case ARG_NIM8:
1136 imm_operand->X_add_number = -imm_operand->X_add_number;
1137 /* Drop through. */
1138 case ARG_IMM8:
1139 output_ptr = apply_fix (output_ptr, BFD_RELOC_8, imm_operand, 2);
1140 break;
1141 case ARG_IMM16:
1142 output_ptr = apply_fix (output_ptr, BFD_RELOC_16, imm_operand, 4);
1143 break;
1144 case ARG_IMM32:
1145 output_ptr = apply_fix (output_ptr, BFD_RELOC_32, imm_operand, 8);
1146 break;
1147 default:
1148 abort ();
1154 /* Copy from the nibble buffer into the frag. */
1156 int length = (output_ptr - buffer) / 2;
1157 char *src = buffer;
1158 char *fragp = frag_more (length);
1160 while (src < output_ptr)
1162 *fragp = (src[0] << 4) | src[1];
1163 src += 2;
1164 fragp++;
1169 /* This is the guts of the machine-dependent assembler. STR points to a
1170 machine dependent instruction. This function is supposed to emit
1171 the frags/bytes it assembles to. */
1173 void
1174 md_assemble (char *str)
1176 char c;
1177 char *op_start;
1178 char *op_end;
1179 struct z8k_op operand[4];
1180 opcode_entry_type *opcode;
1182 /* Drop leading whitespace. */
1183 while (*str == ' ')
1184 str++;
1186 /* Find the op code end. */
1187 for (op_start = op_end = str;
1188 *op_end != 0 && *op_end != ' ' && ! is_end_of_line[(unsigned char) *op_end];
1189 op_end++)
1192 if (op_end == op_start)
1194 as_bad (_("can't find opcode "));
1196 c = *op_end;
1198 *op_end = 0; /* Zero-terminate op code string for hash_find() call. */
1200 opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start);
1202 if (opcode == NULL)
1204 as_bad (_("unknown opcode"));
1205 return;
1208 *op_end = c; /* Restore original string. */
1210 if (opcode->opcode == 250)
1212 pseudo_typeS *p;
1213 char oc;
1214 char *old = input_line_pointer;
1216 /* Was really a pseudo op. */
1218 input_line_pointer = op_end;
1220 oc = *old;
1221 *old = '\n';
1222 while (*input_line_pointer == ' ')
1223 input_line_pointer++;
1224 p = (pseudo_typeS *) (opcode->func);
1226 (p->poc_handler) (p->poc_val);
1227 input_line_pointer = old;
1228 *old = oc;
1230 else
1232 char *new_input_line_pointer;
1234 new_input_line_pointer = get_operands (opcode, op_end, operand);
1235 if (new_input_line_pointer)
1237 input_line_pointer = new_input_line_pointer;
1238 opcode = get_specific (opcode, operand);
1241 if (new_input_line_pointer == NULL || opcode == NULL)
1243 /* Couldn't find an opcode which matched the operands. */
1244 char *where = frag_more (2);
1246 where[0] = 0x0;
1247 where[1] = 0x0;
1249 as_bad (_("Can't find opcode to match operands"));
1250 return;
1253 build_bytes (opcode, operand);
1257 /* We have no need to default values of symbols. */
1259 symbolS *
1260 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1262 return 0;
1265 /* Various routines to kill one day. */
1266 /* Equal to MAX_PRECISION in atof-ieee.c. */
1267 #define MAX_LITTLENUMS 6
1269 /* Turn a string in input_line_pointer into a floating point constant
1270 of type TYPE, and store the appropriate bytes in *LITP. The number
1271 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1272 returned, or NULL on OK. */
1274 char *
1275 md_atof (int type, char *litP, int *sizeP)
1277 int prec;
1278 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1279 LITTLENUM_TYPE *wordP;
1280 char *t;
1282 switch (type)
1284 case 'f':
1285 case 'F':
1286 case 's':
1287 case 'S':
1288 prec = 2;
1289 break;
1291 case 'd':
1292 case 'D':
1293 case 'r':
1294 case 'R':
1295 prec = 4;
1296 break;
1298 case 'x':
1299 case 'X':
1300 prec = 6;
1301 break;
1303 case 'p':
1304 case 'P':
1305 prec = 6;
1306 break;
1308 default:
1309 *sizeP = 0;
1310 return _("Bad call to MD_ATOF()");
1312 t = atof_ieee (input_line_pointer, type, words);
1313 if (t)
1314 input_line_pointer = t;
1316 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1317 for (wordP = words; prec--;)
1319 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1320 litP += sizeof (LITTLENUM_TYPE);
1322 return 0;
1325 const char *md_shortopts = "z:";
1327 struct option md_longopts[] =
1329 #define OPTION_RELAX (OPTION_MD_BASE)
1330 {"linkrelax", no_argument, NULL, OPTION_RELAX},
1331 {NULL, no_argument, NULL, 0}
1334 size_t md_longopts_size = sizeof (md_longopts);
1337 md_parse_option (int c, char *arg)
1339 switch (c)
1341 case 'z':
1342 if (!strcmp (arg, "8001"))
1343 s_segm (1);
1344 else if (!strcmp (arg, "8002"))
1345 s_segm (0);
1346 else
1348 as_bad (_("invalid architecture -z%s"), arg);
1349 return 0;
1351 z8k_target_from_cmdline = 1;
1352 break;
1354 case OPTION_RELAX:
1355 linkrelax = 1;
1356 break;
1358 default:
1359 return 0;
1362 return 1;
1365 void
1366 md_show_usage (FILE *stream)
1368 fprintf (stream, _("\
1369 Z8K options:\n\
1370 -z8001 generate segmented code\n\
1371 -z8002 generate unsegmented code\n\
1372 -linkrelax create linker relaxable code\n"));
1375 void
1376 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
1377 segT sec ATTRIBUTE_UNUSED,
1378 fragS *fragP ATTRIBUTE_UNUSED)
1380 printf (_("call to md_convert_frag\n"));
1381 abort ();
1384 /* Generate a machine dependent reloc from a fixup. */
1386 arelent*
1387 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED,
1388 fixS *fixp ATTRIBUTE_UNUSED)
1390 arelent *reloc;
1392 reloc = xmalloc (sizeof (*reloc));
1393 reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1394 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1395 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1396 reloc->addend = fixp->fx_offset;
1397 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1399 if (! reloc->howto)
1401 as_bad_where (fixp->fx_file, fixp->fx_line,
1402 "Cannot represent %s relocation in object file",
1403 bfd_get_reloc_code_name (fixp->fx_r_type));
1404 abort ();
1406 return reloc;
1409 valueT
1410 md_section_align (segT seg, valueT size)
1412 int align = bfd_get_section_alignment (stdoutput, seg);
1413 valueT mask = ((valueT) 1 << align) - 1;
1415 return (size + mask) & ~mask;
1418 /* Attempt to simplify or eliminate a fixup. To indicate that a fixup
1419 has been eliminated, set fix->fx_done. If fix->fx_addsy is non-NULL,
1420 we will have to generate a reloc entry. */
1421 void
1422 md_apply_fix (fixS *fixP, valueT *valP, segT segment ATTRIBUTE_UNUSED)
1424 long val = * (long *) valP;
1425 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1427 switch (fixP->fx_r_type)
1429 case BFD_RELOC_Z8K_IMM4L:
1430 if (fixP->fx_addsy)
1432 fixP->fx_no_overflow = 1;
1433 fixP->fx_done = 0;
1435 else
1436 buf[0] = (buf[0] & 0xf0) | (val & 0xf);
1437 break;
1439 case BFD_RELOC_8:
1440 if (fixP->fx_addsy)
1442 fixP->fx_no_overflow = 1;
1443 fixP->fx_done = 0;
1445 else
1446 *buf++ = val;
1447 break;
1449 case BFD_RELOC_16:
1450 if (fixP->fx_addsy)
1452 fixP->fx_no_overflow = 1;
1453 fixP->fx_done = 0;
1455 else
1457 *buf++ = (val >> 8);
1458 *buf++ = val;
1460 break;
1462 case BFD_RELOC_32:
1463 if (fixP->fx_addsy)
1465 fixP->fx_no_overflow = 1;
1466 fixP->fx_done = 0;
1468 else
1470 *buf++ = (val >> 24);
1471 *buf++ = (val >> 16);
1472 *buf++ = (val >> 8);
1473 *buf++ = val;
1475 break;
1477 case BFD_RELOC_8_PCREL:
1478 if (fixP->fx_addsy)
1480 fixP->fx_no_overflow = 1;
1481 fixP->fx_done = 0;
1483 else
1485 if (val & 1)
1486 as_bad_where (fixP->fx_file, fixP->fx_line,
1487 _("cannot branch to odd address"));
1488 val /= 2;
1489 if (val > 127 || val < -128)
1490 as_bad_where (fixP->fx_file, fixP->fx_line,
1491 _("relative jump out of range"));
1492 *buf++ = val;
1493 fixP->fx_no_overflow = 1;
1494 fixP->fx_done = 1;
1496 break;
1498 case BFD_RELOC_16_PCREL:
1499 if (fixP->fx_addsy)
1501 fixP->fx_no_overflow = 1;
1502 fixP->fx_done = 0;
1504 else
1506 val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size;
1507 if (val > 32767 || val < -32768)
1508 as_bad_where (fixP->fx_file, fixP->fx_line,
1509 _("relative address out of range"));
1510 *buf++ = (val >> 8);
1511 *buf++ = val;
1512 fixP->fx_no_overflow = 1;
1513 fixP->fx_done = 1;
1515 break;
1517 case BFD_RELOC_Z8K_CALLR:
1518 if (fixP->fx_addsy)
1520 fixP->fx_no_overflow = 1;
1521 fixP->fx_done = 0;
1523 else
1525 if (val & 1)
1526 as_bad_where (fixP->fx_file, fixP->fx_line,
1527 _("cannot branch to odd address"));
1528 if (val > 4096 || val < -4095)
1529 as_bad_where (fixP->fx_file, fixP->fx_line,
1530 _("relative call out of range"));
1531 val = -val / 2;
1532 *buf = (*buf & 0xf0) | ((val >> 8) & 0xf);
1533 buf++;
1534 *buf++ = val & 0xff;
1535 fixP->fx_no_overflow = 1;
1536 fixP->fx_done = 1;
1538 break;
1540 case BFD_RELOC_Z8K_DISP7:
1541 if (fixP->fx_addsy)
1543 fixP->fx_no_overflow = 1;
1544 fixP->fx_done = 0;
1546 else
1548 if (val & 1)
1549 as_bad_where (fixP->fx_file, fixP->fx_line,
1550 _("cannot branch to odd address"));
1551 val /= 2;
1552 if (val > 0 || val < -127)
1553 as_bad_where (fixP->fx_file, fixP->fx_line,
1554 _("relative jump out of range"));
1555 *buf = (*buf & 0x80) | (-val & 0x7f);
1556 fixP->fx_no_overflow = 1;
1557 fixP->fx_done = 1;
1559 break;
1561 default:
1562 printf(_("md_apply_fix: unknown r_type 0x%x\n"), fixP->fx_r_type);
1563 abort ();
1566 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1567 fixP->fx_done = 1;
1571 md_estimate_size_before_relax (fragS *fragP ATTRIBUTE_UNUSED,
1572 segT segment_type ATTRIBUTE_UNUSED)
1574 printf (_("call to md_estimate_size_before_relax\n"));
1575 abort ();
1578 /* Put number into target byte order. */
1580 void
1581 md_number_to_chars (char *ptr, valueT use, int nbytes)
1583 number_to_chars_bigendian (ptr, use, nbytes);
1586 /* On the Z8000, a PC-relative offset is relative to the address of the
1587 instruction plus its size. */
1588 long
1589 md_pcrel_from (fixS *fixP)
1591 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
1594 void
1595 tc_coff_symbol_emit_hook (symbolS *s ATTRIBUTE_UNUSED)