* mmo.c (File Layout): Correct place of misplaced blurb about
[binutils.git] / gas / config / tc-z8k.c
blobf2aa9ad6115a9ba9dfd266cadf5b8e8d3ddfc08a
1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001, 2002, 2003
3 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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
24 #define DEFINE_TABLE
25 #include <stdio.h>
27 #include "as.h"
28 #include "bfd.h"
29 #include "safe-ctype.h"
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;
39 const int md_reloc_size;
41 /* This is non-zero if target was set from the command line. */
42 static int z8k_target_from_cmdline;
44 static void
45 s_segm (int segm)
47 if (segm)
49 segmented_mode = 1;
50 machine = bfd_mach_z8001;
51 coff_flags = F_Z8001;
53 else
55 segmented_mode = 0;
56 machine = bfd_mach_z8002;
57 coff_flags = F_Z8002;
61 static void
62 even (int ignore ATTRIBUTE_UNUSED)
64 frag_align (1, 0, 0);
65 record_alignment (now_seg, 1);
68 static int
69 tohex (int c)
71 if (ISDIGIT (c))
72 return c - '0';
73 if (ISLOWER (c))
74 return c - 'a' + 10;
75 return c - 'A' + 10;
78 static void
79 sval (int ignore ATTRIBUTE_UNUSED)
81 SKIP_WHITESPACE ();
82 if (*input_line_pointer == '\'')
84 int c;
85 input_line_pointer++;
86 c = *input_line_pointer++;
87 while (c != '\'')
89 if (c == '%')
91 c = (tohex (input_line_pointer[0]) << 4)
92 | tohex (input_line_pointer[1]);
93 input_line_pointer += 2;
95 FRAG_APPEND_1_CHAR (c);
96 c = *input_line_pointer++;
98 demand_empty_rest_of_line ();
102 /* This table describes all the machine specific pseudo-ops the assembler
103 has to support. The fields are:
104 pseudo-op name without dot
105 function to call to execute this pseudo-op
106 Integer arg to pass to the function
109 const pseudo_typeS md_pseudo_table[] = {
110 {"int" , cons , 2},
111 {"data.b" , cons , 1},
112 {"data.w" , cons , 2},
113 {"data.l" , cons , 4},
114 {"form" , listing_psize , 0},
115 {"heading", listing_title , 0},
116 {"import" , s_ignore , 0},
117 {"page" , listing_eject , 0},
118 {"program", s_ignore , 0},
119 {"z8001" , s_segm , 1},
120 {"z8002" , s_segm , 0},
122 {"segm" , s_segm , 1},
123 {"unsegm" , s_segm , 0},
124 {"unseg" , s_segm , 0},
125 {"name" , s_app_file , 0},
126 {"global" , s_globl , 0},
127 {"wval" , cons , 2},
128 {"lval" , cons , 4},
129 {"bval" , cons , 1},
130 {"sval" , sval , 0},
131 {"rsect" , obj_coff_section, 0},
132 {"sect" , obj_coff_section, 0},
133 {"block" , s_space , 0},
134 {"even" , even , 0},
135 {0 , 0 , 0}
138 const char EXP_CHARS[] = "eE";
140 /* Chars that mean this number is a floating point constant.
141 As in 0f12.456
142 or 0d1.2345e12 */
143 const char FLT_CHARS[] = "rRsSfFdDxXpP";
145 /* Opcode mnemonics. */
146 static struct hash_control *opcode_hash_control;
148 void
149 md_begin (void)
151 const opcode_entry_type *opcode;
152 int idx = -1;
154 opcode_hash_control = hash_new ();
156 for (opcode = z8k_table; opcode->name; opcode++)
158 /* Only enter unique codes into the table. */
159 if (idx != opcode->idx)
160 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
161 idx = opcode->idx;
164 /* Default to z8002. */
165 if (! z8k_target_from_cmdline)
166 s_segm (0);
168 /* Insert the pseudo ops, too. */
169 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
171 opcode_entry_type *fake_opcode;
172 fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
173 fake_opcode->name = md_pseudo_table[idx].poc_name;
174 fake_opcode->func = (void *) (md_pseudo_table + idx);
175 fake_opcode->opcode = 250;
176 hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
180 typedef struct z8k_op {
181 /* CLASS_REG_xxx. */
182 int regsize;
184 /* 0 .. 15. */
185 unsigned int reg;
187 int mode;
189 /* Any other register associated with the mode. */
190 unsigned int x_reg;
192 /* Any expression. */
193 expressionS exp;
194 } op_type;
196 static expressionS *da_operand;
197 static expressionS *imm_operand;
199 static int reg[16];
200 static int the_cc;
201 static int the_ctrl;
202 static int the_flags;
203 static int the_interrupt;
205 static char *
206 whatreg (int *reg, char *src)
208 if (ISDIGIT (src[1]))
210 *reg = (src[0] - '0') * 10 + src[1] - '0';
211 return src + 2;
213 else
215 *reg = (src[0] - '0');
216 return src + 1;
220 /* Parse operands
222 rh0-rh7, rl0-rl7
223 r0-r15
224 rr0-rr14
225 rq0--rq12
226 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
227 r0l,r0h,..r7l,r7h
228 @WREG
229 @WREG+
230 @-WREG
231 #const
234 /* Try to parse a reg name. Return a pointer to the first character
235 in SRC after the reg name. */
237 static char *
238 parse_reg (char *src, int *mode, unsigned int *reg)
240 char *res = 0;
241 char regno;
243 /* Check for stack pointer "sp" alias. */
244 if ((src[0] == 's' || src[0] == 'S')
245 && (src[1] == 'p' || src[1] == 'P')
246 && (src[2] == 0 || src[2] == ','))
248 if (segmented_mode)
250 *mode = CLASS_REG_LONG;
251 *reg = 14;
253 else
255 *mode = CLASS_REG_WORD;
256 *reg = 15;
258 return src + 2;
261 if (src[0] == 'r' || src[0] == 'R')
263 if (src[1] == 'r' || src[1] == 'R')
265 if (src[2] < '0' || src[2] > '9')
266 return res; /* Assume no register name but a label starting with 'rr'. */
267 *mode = CLASS_REG_LONG;
268 res = whatreg (reg, src + 2);
269 regno = *reg;
270 if (regno > 14)
271 as_bad (_("register rr%d out of range"), regno);
272 if (regno & 1)
273 as_bad (_("register rr%d does not exist"), regno);
275 else if (src[1] == 'h' || src[1] == 'H')
277 if (src[2] < '0' || src[2] > '9')
278 return res; /* Assume no register name but a label starting with 'rh'. */
279 *mode = CLASS_REG_BYTE;
280 res = whatreg (reg, src + 2);
281 regno = *reg;
282 if (regno > 7)
283 as_bad (_("register rh%d out of range"), regno);
285 else if (src[1] == 'l' || src[1] == 'L')
287 if (src[2] < '0' || src[2] > '9')
288 return res; /* Assume no register name but a label starting with 'rl'. */
289 *mode = CLASS_REG_BYTE;
290 res = whatreg (reg, src + 2);
291 regno = *reg;
292 if (regno > 7)
293 as_bad (_("register rl%d out of range"), regno);
294 *reg += 8;
296 else if (src[1] == 'q' || src[1] == 'Q')
298 if (src[2] < '0' || src[2] > '9')
299 return res; /* Assume no register name but a label starting with 'rq'. */
300 *mode = CLASS_REG_QUAD;
301 res = whatreg (reg, src + 2);
302 regno = *reg;
303 if (regno > 12)
304 as_bad (_("register rq%d out of range"), regno);
305 if (regno & 3)
306 as_bad (_("register rq%d does not exist"), regno);
308 else
310 if (src[1] < '0' || src[1] > '9')
311 return res; /* Assume no register name but a label starting with 'r'. */
312 *mode = CLASS_REG_WORD;
313 res = whatreg (reg, src + 1);
314 regno = *reg;
315 if (regno > 15)
316 as_bad (_("register r%d out of range"), regno);
319 return res;
322 static char *
323 parse_exp (char *s, expressionS *op)
325 char *save = input_line_pointer;
326 char *new;
328 input_line_pointer = s;
329 expression (op);
330 if (op->X_op == O_absent)
331 as_bad (_("missing operand"));
332 new = input_line_pointer;
333 input_line_pointer = save;
334 return new;
337 /* The many forms of operand:
339 <rb>
341 <rr>
342 <rq>
344 #exp
346 exp(r)
347 r(#exp)
348 r(r)
351 static char *
352 checkfor (char *ptr, char what)
354 if (*ptr == what)
355 ptr++;
356 else
357 as_bad (_("expected %c"), what);
359 return ptr;
362 /* Make sure the mode supplied is the size of a word. */
364 static void
365 regword (int mode, char *string)
367 int ok;
369 ok = CLASS_REG_WORD;
370 if (ok != mode)
372 as_bad (_("register is wrong size for a word %s"), string);
376 /* Make sure the mode supplied is the size of an address. */
378 static void
379 regaddr (int mode, char *string)
381 int ok;
383 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
384 if (ok != mode)
386 as_bad (_("register is wrong size for address %s"), string);
390 struct ctrl_names {
391 int value;
392 char *name;
395 static struct ctrl_names ctrl_table[] = {
396 { 0x1, "flags" }, /* ldctlb only. */
397 { 0x2, "fcw" }, /* ldctl only. Applies to all remaining control registers. */
398 { 0x3, "refresh" },
399 { 0x4, "psapseg" },
400 { 0x5, "psapoff" },
401 { 0x5, "psap" },
402 { 0x6, "nspseg" },
403 { 0x7, "nspoff" },
404 { 0x7, "nsp" },
405 { 0 , 0 }
408 static void
409 get_ctrl_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
411 char *src = *ptr;
412 int i, l;
414 while (*src == ' ')
415 src++;
417 mode->mode = CLASS_CTRL;
418 for (i = 0; ctrl_table[i].name; i++)
420 l = strlen (ctrl_table[i].name);
421 if (! strncasecmp (ctrl_table[i].name, src, l))
423 the_ctrl = ctrl_table[i].value;
424 if (*(src + l) && *(src + l) != ',')
425 break;
426 *ptr = src + l; /* Valid control name found: "consume" it. */
427 return;
430 the_ctrl = 0;
433 struct flag_names {
434 int value;
435 char *name;
438 static struct flag_names flag_table[] = {
439 { 0x1, "P" },
440 { 0x1, "V" },
441 { 0x2, "S" },
442 { 0x4, "Z" },
443 { 0x8, "C" },
444 { 0x0, "+" },
445 { 0x0, "," },
446 { 0, 0 }
449 static void
450 get_flags_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
452 char *src = *ptr;
453 char c;
454 int i;
455 int j;
457 while (*src == ' ')
458 src++;
460 mode->mode = CLASS_FLAGS;
461 the_flags = 0;
462 for (j = 0; j <= 9; j++)
464 if (!src[j])
465 goto done;
466 c = TOUPPER(src[j]);
467 for (i = 0; flag_table[i].name; i++)
469 if (flag_table[i].name[0] == c)
471 the_flags = the_flags | flag_table[i].value;
472 goto match;
475 goto done;
476 match:
479 done:
480 *ptr = src + j;
483 struct interrupt_names {
484 int value;
485 char *name;
488 static struct interrupt_names intr_table[] = {
489 { 0x1, "nvi" },
490 { 0x2, "vi" },
491 { 0x3, "both" },
492 { 0x3, "all" },
493 { 0, 0 }
496 static void
497 get_interrupt_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
499 char *src = *ptr;
500 int i, l;
502 while (*src == ' ')
503 src++;
505 mode->mode = CLASS_IMM;
506 the_interrupt = 0;
508 while (*src)
510 for (i = 0; intr_table[i].name; i++)
512 l = strlen (intr_table[i].name);
513 if (! strncasecmp (intr_table[i].name, src, l))
515 the_interrupt |= intr_table[i].value;
516 if (*(src + l) && *(src + l) != ',')
518 *ptr = src + l;
519 invalid:
520 as_bad (_("unknown interrupt %s"), src);
521 while (**ptr && ! is_end_of_line[(unsigned char) **ptr])
522 (*ptr)++; /* Consume rest of line. */
523 return;
525 src += l;
526 if (! *src)
528 *ptr = src;
529 return;
533 if (*src == ',')
534 src++;
535 else
537 *ptr = src;
538 goto invalid;
542 /* No interrupt type specified, opcode won't do anything. */
543 as_warn (_("opcode has no effect"));
544 the_interrupt = 0x0;
547 struct cc_names {
548 int value;
549 char *name;
552 static struct cc_names table[] = {
553 { 0x0, "f" },
554 { 0x1, "lt" },
555 { 0x2, "le" },
556 { 0x3, "ule" },
557 { 0x4, "ov/pe" },
558 { 0x4, "ov" },
559 { 0x4, "pe/ov" },
560 { 0x4, "pe" },
561 { 0x5, "mi" },
562 { 0x6, "eq" },
563 { 0x6, "z" },
564 { 0x7, "c/ult" },
565 { 0x7, "c" },
566 { 0x7, "ult/c" },
567 { 0x7, "ult" },
568 { 0x8, "t" },
569 { 0x9, "ge" },
570 { 0xa, "gt" },
571 { 0xb, "ugt" },
572 { 0xc, "nov/po" },
573 { 0xc, "nov" },
574 { 0xc, "po/nov" },
575 { 0xc, "po" },
576 { 0xd, "pl" },
577 { 0xe, "ne" },
578 { 0xe, "nz" },
579 { 0xf, "nc/uge" },
580 { 0xf, "nc" },
581 { 0xf, "uge/nc" },
582 { 0xf, "uge" },
583 { 0 , 0 }
586 static void
587 get_cc_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
589 char *src = *ptr;
590 int i, l;
592 while (*src == ' ')
593 src++;
595 mode->mode = CLASS_CC;
596 for (i = 0; table[i].name; i++)
598 l = strlen (table[i].name);
599 if (! strncasecmp (table[i].name, src, l))
601 the_cc = table[i].value;
602 if (*(src + l) && *(src + l) != ',')
603 break;
604 *ptr = src + l; /* Valid cc found: "consume" it. */
605 return;
608 the_cc = 0x8; /* Not recognizing the cc defaults to t. (Assuming no cc present.) */
611 static void
612 get_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
614 char *src = *ptr;
615 char *end;
617 mode->mode = 0;
619 while (*src == ' ')
620 src++;
621 if (*src == '#')
623 mode->mode = CLASS_IMM;
624 imm_operand = &(mode->exp);
625 src = parse_exp (src + 1, &(mode->exp));
627 else if (*src == '@')
629 mode->mode = CLASS_IR;
630 src = parse_reg (src + 1, &mode->regsize, &mode->reg);
632 else
634 int regn;
636 end = parse_reg (src, &mode->mode, &regn);
638 if (end)
640 int nw, nr;
642 src = end;
643 if (*src == '(')
645 src++;
646 end = parse_reg (src, &nw, &nr);
647 if (end)
649 /* Got Ra(Rb). */
650 src = end;
652 if (*src != ')')
653 as_bad (_("Missing ) in ra(rb)"));
654 else
655 src++;
657 regaddr (mode->mode, "ra(rb) ra");
658 #if 0
659 regword (mode->mode, "ra(rb) rb");
660 #endif
661 mode->mode = CLASS_BX;
662 mode->reg = regn;
663 mode->x_reg = nr;
664 reg[ARG_RX] = nr;
666 else
668 /* Got Ra(disp). */
669 if (*src == '#')
670 src++;
671 src = parse_exp (src, &(mode->exp));
672 src = checkfor (src, ')');
673 mode->mode = CLASS_BA;
674 mode->reg = regn;
675 mode->x_reg = 0;
676 imm_operand = &(mode->exp);
679 else
681 mode->reg = regn;
682 mode->x_reg = 0;
685 else
687 /* No initial reg. */
688 src = parse_exp (src, &(mode->exp));
689 if (*src == '(')
691 src++;
692 end = parse_reg (src, &(mode->mode), &regn);
693 regword (mode->mode, "addr(Ra) ra");
694 mode->mode = CLASS_X;
695 mode->reg = regn;
696 mode->x_reg = 0;
697 da_operand = &(mode->exp);
698 src = checkfor (end, ')');
700 else
702 /* Just an address. */
703 mode->mode = CLASS_DA;
704 mode->reg = 0;
705 mode->x_reg = 0;
706 da_operand = &(mode->exp);
710 *ptr = src;
713 static char *
714 get_operands (const opcode_entry_type *opcode, char *op_end, op_type *operand)
716 char *ptr = op_end;
717 char *savptr;
719 switch (opcode->noperands)
721 case 0:
722 operand[0].mode = 0;
723 operand[1].mode = 0;
724 while (*ptr == ' ')
725 ptr++;
726 break;
728 case 1:
729 if (opcode->arg_info[0] == CLASS_CC)
731 get_cc_operand (&ptr, operand + 0, 0);
732 while (*ptr == ' ')
733 ptr++;
734 if (*ptr && ! is_end_of_line[(unsigned char) *ptr])
736 as_bad (_("invalid condition code '%s'"), ptr);
737 while (*ptr && ! is_end_of_line[(unsigned char) *ptr])
738 ptr++; /* Consume rest of line. */
741 else if (opcode->arg_info[0] == CLASS_FLAGS)
743 get_flags_operand (&ptr, operand + 0, 0);
744 while (*ptr == ' ')
745 ptr++;
746 if (*ptr && ! is_end_of_line[(unsigned char) *ptr])
748 as_bad (_("invalid flag '%s'"), ptr);
749 while (*ptr && ! is_end_of_line[(unsigned char) *ptr])
750 ptr++; /* Consume rest of line. */
753 else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2)))
754 get_interrupt_operand (&ptr, operand + 0, 0);
755 else
756 get_operand (&ptr, operand + 0, 0);
758 operand[1].mode = 0;
759 break;
761 case 2:
762 savptr = ptr;
763 if (opcode->arg_info[0] == CLASS_CC)
765 get_cc_operand (&ptr, operand + 0, 0);
766 while (*ptr == ' ')
767 ptr++;
768 if (*ptr != ',' && strchr (ptr + 1, ','))
770 savptr = ptr;
771 while (*ptr != ',')
772 ptr++;
773 *ptr = 0;
774 ptr++;
775 as_bad (_("invalid condition code '%s'"), savptr);
778 else if (opcode->arg_info[0] == CLASS_CTRL)
780 get_ctrl_operand (&ptr, operand + 0, 0);
782 if (the_ctrl == 0)
784 ptr = savptr;
785 get_operand (&ptr, operand + 0, 0);
787 if (ptr == 0)
788 return NULL;
789 if (*ptr == ',')
790 ptr++;
791 get_ctrl_operand (&ptr, operand + 1, 1);
792 if (the_ctrl == 0)
793 return NULL;
794 return ptr;
797 else
798 get_operand (&ptr, operand + 0, 0);
800 if (ptr == 0)
801 return NULL;
802 if (*ptr == ',')
803 ptr++;
804 get_operand (&ptr, operand + 1, 1);
805 break;
807 case 3:
808 get_operand (&ptr, operand + 0, 0);
809 if (*ptr == ',')
810 ptr++;
811 get_operand (&ptr, operand + 1, 1);
812 if (*ptr == ',')
813 ptr++;
814 get_operand (&ptr, operand + 2, 2);
815 break;
817 case 4:
818 get_operand (&ptr, operand + 0, 0);
819 if (*ptr == ',')
820 ptr++;
821 get_operand (&ptr, operand + 1, 1);
822 if (*ptr == ',')
823 ptr++;
824 get_operand (&ptr, operand + 2, 2);
825 if (*ptr == ',')
826 ptr++;
827 get_cc_operand (&ptr, operand + 3, 3);
828 break;
830 default:
831 abort ();
834 return ptr;
837 /* Passed a pointer to a list of opcodes which use different
838 addressing modes. Return the opcode which matches the opcodes
839 provided. */
841 static opcode_entry_type *
842 get_specific (opcode_entry_type *opcode, op_type *operands)
844 opcode_entry_type *this_try = opcode;
845 int found = 0;
846 unsigned int noperands = opcode->noperands;
848 int this_index = opcode->idx;
850 while (this_index == opcode->idx && !found)
852 unsigned int i;
854 this_try = opcode++;
855 for (i = 0; i < noperands; i++)
857 unsigned int mode = operands[i].mode;
859 if (((mode & CLASS_MASK) == CLASS_IR) && ((this_try->arg_info[i] & CLASS_MASK) == CLASS_IRO))
861 mode = operands[i].mode = (operands[i].mode & ~CLASS_MASK) | CLASS_IRO;
864 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
866 /* It could be a pc rel operand, if this is a da mode
867 and we like disps, then insert it. */
869 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
871 /* This is the case. */
872 operands[i].mode = CLASS_DISP;
874 else if (mode == CLASS_BA && this_try->arg_info[i])
876 /* Can't think of a way to turn what we've been
877 given into something that's OK. */
878 goto fail;
880 else if (this_try->arg_info[i] & CLASS_PR)
882 if (mode == CLASS_REG_LONG && segmented_mode)
884 /* OK. */
886 else if (mode == CLASS_REG_WORD && !segmented_mode)
888 /* OK. */
890 else
891 goto fail;
893 else
894 goto fail;
896 switch (mode & CLASS_MASK)
898 default:
899 break;
900 case CLASS_IRO:
901 if (operands[i].regsize != CLASS_REG_WORD)
902 as_bad (_("invalid indirect register size"));
903 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
904 break;
905 case CLASS_IR:
906 if ((segmented_mode && operands[i].regsize != CLASS_REG_LONG)
907 || (!segmented_mode && operands[i].regsize != CLASS_REG_WORD))
908 as_bad (_("invalid indirect register size"));
909 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
910 break;
911 case CLASS_X:
912 case CLASS_BA:
913 case CLASS_BX:
914 case CLASS_DISP:
915 case CLASS_REG:
916 case CLASS_REG_WORD:
917 case CLASS_REG_BYTE:
918 case CLASS_REG_QUAD:
919 case CLASS_REG_LONG:
920 case CLASS_REGN0:
921 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
922 break;
923 case CLASS_CTRL:
924 if (this_try->opcode == OPC_ldctlb && the_ctrl != 1)
925 as_bad (_("invalid control register name"));
926 break;
930 found = 1;
931 fail:
934 if (found)
935 return this_try;
936 else
937 return 0;
940 static char buffer[20];
942 static void
943 newfix (int ptr, int type, int size, expressionS *operand)
945 int is_pcrel = 0;
947 /* size is in nibbles. */
949 if (operand->X_add_symbol
950 || operand->X_op_symbol
951 || operand->X_add_number)
953 switch(type)
955 case R_JR:
956 case R_DISP7:
957 case R_CALLR:
958 is_pcrel = 1;
960 fix_new_exp (frag_now,
961 ptr,
962 size / 2,
963 operand,
964 is_pcrel,
965 type);
969 static char *
970 apply_fix (char *ptr, int type, expressionS *operand, int size)
972 long n = operand->X_add_number;
974 /* size is in nibbles. */
976 newfix ((ptr - buffer) / 2, type, size + 1, operand);
977 switch (size)
979 case 8: /* 8 nibbles == 32 bits. */
980 *ptr++ = n >> 28;
981 *ptr++ = n >> 24;
982 *ptr++ = n >> 20;
983 *ptr++ = n >> 16;
984 case 4: /* 4 nibbles == 16 bits. */
985 *ptr++ = n >> 12;
986 *ptr++ = n >> 8;
987 case 2:
988 *ptr++ = n >> 4;
989 case 1:
990 *ptr++ = n >> 0;
991 break;
993 return ptr;
996 /* Now we know what sort of opcodes it is. Let's build the bytes. */
998 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
1000 static void
1001 build_bytes (opcode_entry_type *this_try, struct z8k_op *operand ATTRIBUTE_UNUSED)
1003 char *output_ptr = buffer;
1004 int c;
1005 int nibble;
1006 unsigned int *class_ptr;
1008 frag_wane (frag_now);
1009 frag_new (0);
1011 memset (buffer, 0, sizeof (buffer));
1012 class_ptr = this_try->byte_info;
1014 for (nibble = 0; (c = *class_ptr++); nibble++)
1017 switch (c & CLASS_MASK)
1019 default:
1020 abort ();
1022 case CLASS_ADDRESS:
1023 /* Direct address, we don't cope with the SS mode right now. */
1024 if (segmented_mode)
1026 /* da_operand->X_add_number |= 0x80000000; -- Now set at relocation time. */
1027 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1029 else
1031 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1033 da_operand = 0;
1034 break;
1035 case CLASS_DISP8:
1036 /* pc rel 8 bit */
1037 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1038 da_operand = 0;
1039 break;
1041 case CLASS_0DISP7:
1042 /* pc rel 7 bit */
1043 *output_ptr = 0;
1044 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1045 da_operand = 0;
1046 break;
1048 case CLASS_1DISP7:
1049 /* pc rel 7 bit */
1050 *output_ptr = 0x80;
1051 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1052 output_ptr[-2] = 0x8;
1053 da_operand = 0;
1054 break;
1056 case CLASS_BIT_1OR2:
1057 *output_ptr = c & 0xf;
1058 if (imm_operand)
1060 if (imm_operand->X_add_number == 2)
1061 *output_ptr |= 2;
1062 else if (imm_operand->X_add_number != 1)
1063 as_bad (_("immediate must be 1 or 2"));
1065 else
1066 as_bad (_("immediate 1 or 2 expected"));
1067 output_ptr++;
1068 break;
1069 case CLASS_CC:
1070 *output_ptr++ = the_cc;
1071 break;
1072 case CLASS_0CCC:
1073 if (the_ctrl < 2 || the_ctrl > 7)
1074 as_bad (_("invalid control register name"));
1075 *output_ptr++ = the_ctrl;
1076 break;
1077 case CLASS_1CCC:
1078 if (the_ctrl < 2 || the_ctrl > 7)
1079 as_bad (_("invalid control register name"));
1080 *output_ptr++ = the_ctrl | 0x8;
1081 break;
1082 case CLASS_00II:
1083 *output_ptr++ = (~the_interrupt & 0x3);
1084 break;
1085 case CLASS_01II:
1086 *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1087 break;
1088 case CLASS_FLAGS:
1089 *output_ptr++ = the_flags;
1090 break;
1091 case CLASS_IGNORE:
1092 case CLASS_BIT:
1093 *output_ptr++ = c & 0xf;
1094 break;
1095 case CLASS_REGN0:
1096 if (reg[c & 0xf] == 0)
1097 as_bad (_("can't use R0 here"));
1098 /* Fall through. */
1099 case CLASS_REG:
1100 case CLASS_REG_BYTE:
1101 case CLASS_REG_WORD:
1102 case CLASS_REG_LONG:
1103 case CLASS_REG_QUAD:
1104 /* Insert bit mattern of right reg. */
1105 *output_ptr++ = reg[c & 0xf];
1106 break;
1107 case CLASS_DISP:
1108 switch (c & ARG_MASK)
1110 case ARG_DISP12:
1111 output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4);
1112 break;
1113 case ARG_DISP16:
1114 output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4);
1115 break;
1116 default:
1117 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1119 da_operand = 0;
1120 break;
1122 case CLASS_IMM:
1124 switch (c & ARG_MASK)
1126 case ARG_NIM4:
1127 if (imm_operand->X_add_number > 15)
1129 as_bad (_("immediate value out of range"));
1131 imm_operand->X_add_number = -imm_operand->X_add_number;
1132 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1133 break;
1134 /*case ARG_IMMNMINUS1: not used. */
1135 case ARG_IMM4M1:
1136 imm_operand->X_add_number--;
1137 /* Drop through. */
1138 case ARG_IMM4:
1139 if (imm_operand->X_add_number > 15)
1141 as_bad (_("immediate value out of range"));
1143 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1144 break;
1145 case ARG_NIM8:
1146 imm_operand->X_add_number = -imm_operand->X_add_number;
1147 /* Drop through. */
1148 case ARG_IMM8:
1149 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1150 break;
1151 case ARG_IMM16:
1152 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1153 break;
1154 case ARG_IMM32:
1155 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1156 break;
1157 default:
1158 abort ();
1164 /* Copy from the nibble buffer into the frag. */
1166 int length = (output_ptr - buffer) / 2;
1167 char *src = buffer;
1168 char *fragp = frag_more (length);
1170 while (src < output_ptr)
1172 *fragp = (src[0] << 4) | src[1];
1173 src += 2;
1174 fragp++;
1179 /* This is the guts of the machine-dependent assembler. STR points to a
1180 machine dependent instruction. This function is supposed to emit
1181 the frags/bytes it assembles to. */
1183 void
1184 md_assemble (char *str)
1186 char c;
1187 char *op_start;
1188 char *op_end;
1189 struct z8k_op operand[3];
1190 opcode_entry_type *opcode;
1192 /* Drop leading whitespace. */
1193 while (*str == ' ')
1194 str++;
1196 /* Find the op code end. */
1197 for (op_start = op_end = str;
1198 *op_end != 0 && *op_end != ' ' && ! is_end_of_line[(unsigned char) *op_end];
1199 op_end++)
1202 if (op_end == op_start)
1204 as_bad (_("can't find opcode "));
1206 c = *op_end;
1208 *op_end = 0; /* Zero-terminate op code string for hash_find() call. */
1210 opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start);
1212 if (opcode == NULL)
1214 as_bad (_("unknown opcode"));
1215 return;
1218 *op_end = c; /* Restore original string. */
1220 if (opcode->opcode == 250)
1222 pseudo_typeS *p;
1223 char oc;
1224 char *old = input_line_pointer;
1226 /* Was really a pseudo op. */
1228 input_line_pointer = op_end;
1230 oc = *old;
1231 *old = '\n';
1232 while (*input_line_pointer == ' ')
1233 input_line_pointer++;
1234 p = (pseudo_typeS *) (opcode->func);
1236 (p->poc_handler) (p->poc_val);
1237 input_line_pointer = old;
1238 *old = oc;
1240 else
1242 char *new_input_line_pointer;
1244 new_input_line_pointer = get_operands (opcode, op_end, operand);
1245 if (new_input_line_pointer)
1246 input_line_pointer = new_input_line_pointer;
1248 opcode = get_specific (opcode, operand);
1250 if (opcode == 0)
1252 /* Couldn't find an opcode which matched the operands. */
1253 char *where = frag_more (2);
1255 where[0] = 0x0;
1256 where[1] = 0x0;
1258 as_bad (_("Can't find opcode to match operands"));
1259 return;
1262 build_bytes (opcode, operand);
1266 void
1267 tc_crawl_symbol_chain (object_headers *headers ATTRIBUTE_UNUSED)
1269 printf (_("call to tc_crawl_symbol_chain \n"));
1272 /* We have no need to default values of symbols. */
1274 symbolS *
1275 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1277 return 0;
1280 void
1281 tc_headers_hook (object_headers *headers ATTRIBUTE_UNUSED)
1283 printf (_("call to tc_headers_hook \n"));
1286 /* Various routines to kill one day. */
1287 /* Equal to MAX_PRECISION in atof-ieee.c. */
1288 #define MAX_LITTLENUMS 6
1290 /* Turn a string in input_line_pointer into a floating point constant
1291 of type TYPE, and store the appropriate bytes in *LITP. The number
1292 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1293 returned, or NULL on OK. */
1295 char *
1296 md_atof (int type, char *litP, int *sizeP)
1298 int prec;
1299 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1300 LITTLENUM_TYPE *wordP;
1301 char *t;
1303 switch (type)
1305 case 'f':
1306 case 'F':
1307 case 's':
1308 case 'S':
1309 prec = 2;
1310 break;
1312 case 'd':
1313 case 'D':
1314 case 'r':
1315 case 'R':
1316 prec = 4;
1317 break;
1319 case 'x':
1320 case 'X':
1321 prec = 6;
1322 break;
1324 case 'p':
1325 case 'P':
1326 prec = 6;
1327 break;
1329 default:
1330 *sizeP = 0;
1331 return _("Bad call to MD_ATOF()");
1333 t = atof_ieee (input_line_pointer, type, words);
1334 if (t)
1335 input_line_pointer = t;
1337 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1338 for (wordP = words; prec--;)
1340 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1341 litP += sizeof (LITTLENUM_TYPE);
1343 return 0;
1346 const char *md_shortopts = "z:";
1348 struct option md_longopts[] =
1350 #define OPTION_RELAX (OPTION_MD_BASE)
1351 {"linkrelax", no_argument, NULL, OPTION_RELAX},
1352 {NULL, no_argument, NULL, 0}
1355 size_t md_longopts_size = sizeof (md_longopts);
1358 md_parse_option (int c, char *arg)
1360 switch (c)
1362 case 'z':
1363 if (!strcmp (arg, "8001"))
1364 s_segm (1);
1365 else if (!strcmp (arg, "8002"))
1366 s_segm (0);
1367 else
1369 as_bad (_("invalid architecture -z%s"), arg);
1370 return 0;
1372 z8k_target_from_cmdline = 1;
1373 break;
1375 case OPTION_RELAX:
1376 linkrelax = 1;
1377 break;
1379 default:
1380 return 0;
1383 return 1;
1386 void
1387 md_show_usage (FILE *stream)
1389 fprintf (stream, _("\
1390 Z8K options:\n\
1391 -z8001 generate segmented code\n\
1392 -z8002 generate unsegmented code\n\
1393 -linkrelax create linker relaxable code\n"));
1396 void
1397 md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED,
1398 segT seg ATTRIBUTE_UNUSED,
1399 fragS *fragP ATTRIBUTE_UNUSED)
1401 printf (_("call to md_convert_frag\n"));
1402 abort ();
1405 valueT
1406 md_section_align (segT seg, valueT size)
1408 return ((size + (1 << section_alignment[(int) seg]) - 1)
1409 & (-1 << section_alignment[(int) seg]));
1412 /* Attempt to simplify or eliminate a fixup. To indicate that a fixup
1413 has been eliminated, set fix->fx_done. If fix->fx_addsy is non-NULL,
1414 we will have to generate a reloc entry. */
1415 void
1416 md_apply_fix3 (fixS *fixP, valueT *valP, segT segment ATTRIBUTE_UNUSED)
1418 long val = * (long *) valP;
1419 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1421 switch (fixP->fx_r_type)
1423 case R_IMM4L:
1424 buf[0] = (buf[0] & 0xf0) | (val & 0xf);
1425 break;
1427 case R_JR:
1428 if (fixP->fx_addsy)
1430 fixP->fx_no_overflow = 1;
1431 fixP->fx_done = 0;
1433 else
1435 if (val & 1)
1436 as_bad (_("cannot branch to odd address"));
1437 val /= 2;
1438 if (val > 127 || val < -128)
1439 as_warn (_("relative jump out of range"));
1440 *buf++ = val;
1441 fixP->fx_no_overflow = 1;
1442 fixP->fx_done = 1;
1444 break;
1446 case R_DISP7:
1447 if (fixP->fx_addsy)
1449 fixP->fx_no_overflow = 1;
1450 fixP->fx_done = 0;
1452 else
1454 if (val & 1)
1455 as_bad (_("cannot branch to odd address"));
1456 val /= 2;
1457 if (val > 0 || val < -127)
1458 as_bad (_("relative jump out of range"));
1459 *buf = (*buf & 0x80) | (-val & 0x7f);
1460 fixP->fx_no_overflow = 1;
1461 fixP->fx_done = 1;
1463 break;
1465 case R_CALLR:
1466 if (fixP->fx_addsy)
1468 fixP->fx_no_overflow = 1;
1469 fixP->fx_done = 0;
1471 else
1473 if (val & 1)
1474 as_bad (_("cannot branch to odd address"));
1475 if (val > 4096 || val < -4095)
1476 as_bad (_("relative call out of range"));
1477 val = -val / 2;
1478 *buf = (*buf & 0xf0) | ((val >> 8) & 0xf);
1479 buf++;
1480 *buf++ = val & 0xff;
1481 fixP->fx_no_overflow = 1;
1482 fixP->fx_done = 1;
1484 break;
1486 case R_IMM8:
1487 *buf++ = val;
1488 break;
1490 case R_IMM16:
1491 *buf++ = (val >> 8);
1492 *buf++ = val;
1493 break;
1495 case R_IMM32:
1496 *buf++ = (val >> 24);
1497 *buf++ = (val >> 16);
1498 *buf++ = (val >> 8);
1499 *buf++ = val;
1500 break;
1502 case R_REL16:
1503 val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size;
1504 if (val > 32767 || val < -32768)
1505 as_bad (_("relative address out of range"));
1506 *buf++ = (val >> 8);
1507 *buf++ = val;
1508 fixP->fx_no_overflow = 1;
1509 break;
1511 #if 0
1512 case R_DA | R_SEG:
1513 *buf++ = (val >> 16);
1514 *buf++ = 0x00;
1515 *buf++ = (val >> 8);
1516 *buf++ = val;
1517 break;
1518 #endif
1520 case 0:
1521 md_number_to_chars (buf, val, fixP->fx_size);
1522 break;
1524 default:
1525 printf(_("md_apply_fix3: unknown r_type 0x%x\n"), fixP->fx_r_type);
1526 abort ();
1529 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1530 fixP->fx_done = 1;
1534 md_estimate_size_before_relax (fragS *fragP ATTRIBUTE_UNUSED,
1535 segT segment_type ATTRIBUTE_UNUSED)
1537 printf (_("call to md_estimate_size_before_relax\n"));
1538 abort ();
1541 /* Put number into target byte order. */
1543 void
1544 md_number_to_chars (char *ptr, valueT use, int nbytes)
1546 number_to_chars_bigendian (ptr, use, nbytes);
1549 /* On the Z8000, a PC-relative offset is relative to the address of the
1550 instruction plus its size. */
1551 long
1552 md_pcrel_from (fixS *fixP)
1554 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
1557 void
1558 tc_coff_symbol_emit_hook (symbolS *s ATTRIBUTE_UNUSED)
1562 void
1563 tc_reloc_mangle (fixS *fix_ptr, struct internal_reloc *intr, bfd_vma base)
1565 symbolS *symbol_ptr;
1567 if (fix_ptr->fx_addsy
1568 && fix_ptr->fx_subsy)
1570 symbolS *add = fix_ptr->fx_addsy;
1571 symbolS *sub = fix_ptr->fx_subsy;
1573 if (S_GET_SEGMENT (add) != S_GET_SEGMENT (sub))
1574 as_bad (_("Can't subtract symbols in different sections %s %s"),
1575 S_GET_NAME (add), S_GET_NAME (sub));
1576 else
1578 int diff = S_GET_VALUE (add) - S_GET_VALUE (sub);
1580 fix_ptr->fx_addsy = 0;
1581 fix_ptr->fx_subsy = 0;
1582 fix_ptr->fx_offset += diff;
1585 symbol_ptr = fix_ptr->fx_addsy;
1587 /* If this relocation is attached to a symbol then it's ok
1588 to output it. */
1589 if (fix_ptr->fx_r_type == 0)
1591 /* cons likes to create reloc32's whatever the size of the reloc. */
1592 switch (fix_ptr->fx_size)
1594 case 2:
1595 intr->r_type = R_IMM16;
1596 break;
1597 case 1:
1598 intr->r_type = R_IMM8;
1599 break;
1600 case 4:
1601 intr->r_type = R_IMM32;
1602 break;
1603 default:
1604 abort ();
1607 else
1608 intr->r_type = fix_ptr->fx_r_type;
1610 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1611 intr->r_offset = fix_ptr->fx_offset;
1613 if (symbol_ptr)
1614 intr->r_symndx = symbol_ptr->sy_number;
1615 else
1616 intr->r_symndx = -1;