* config/tc-z8k.c (build_bytes): Correct order of memset args.
[binutils.git] / gas / config / tc-z8k.c
blob04723f8d834e2b4abe48a2a56ca8e80720eb5423
1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001, 2002
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 void cons ();
43 void
44 s_segm ()
46 segmented_mode = 1;
47 machine = bfd_mach_z8001;
48 coff_flags = F_Z8001;
51 void
52 s_unseg ()
54 segmented_mode = 0;
55 machine = bfd_mach_z8002;
56 coff_flags = F_Z8002;
59 static void
60 even ()
62 frag_align (1, 0, 0);
63 record_alignment (now_seg, 1);
66 void obj_coff_section ();
68 int
69 tohex (c)
70 int c;
72 if (ISDIGIT (c))
73 return c - '0';
74 if (ISLOWER (c))
75 return c - 'a' + 10;
76 return c - 'A' + 10;
79 void
80 sval ()
82 SKIP_WHITESPACE ();
83 if (*input_line_pointer == '\'')
85 int c;
86 input_line_pointer++;
87 c = *input_line_pointer++;
88 while (c != '\'')
90 if (c == '%')
92 c = (tohex (input_line_pointer[0]) << 4)
93 | tohex (input_line_pointer[1]);
94 input_line_pointer += 2;
96 FRAG_APPEND_1_CHAR (c);
97 c = *input_line_pointer++;
99 demand_empty_rest_of_line ();
103 /* This table describes all the machine specific pseudo-ops the assembler
104 has to support. The fields are:
105 pseudo-op name without dot
106 function to call to execute this pseudo-op
107 Integer arg to pass to the function
110 const pseudo_typeS md_pseudo_table[] = {
111 {"int" , cons , 2},
112 {"data.b" , cons , 1},
113 {"data.w" , cons , 2},
114 {"data.l" , cons , 4},
115 {"form" , listing_psize , 0},
116 {"heading", listing_title , 0},
117 {"import" , s_ignore , 0},
118 {"page" , listing_eject , 0},
119 {"program", s_ignore , 0},
120 {"z8001" , s_segm , 0},
121 {"z8002" , s_unseg , 0},
123 {"segm" , s_segm , 0},
124 {"unsegm" , s_unseg , 0},
125 {"unseg" , s_unseg , 0},
126 {"name" , s_app_file , 0},
127 {"global" , s_globl , 0},
128 {"wval" , cons , 2},
129 {"lval" , cons , 4},
130 {"bval" , cons , 1},
131 {"sval" , sval , 0},
132 {"rsect" , obj_coff_section, 0},
133 {"sect" , obj_coff_section, 0},
134 {"block" , s_space , 0},
135 {"even" , even , 0},
136 {0 , 0 , 0}
139 const char EXP_CHARS[] = "eE";
141 /* Chars that mean this number is a floating point constant.
142 As in 0f12.456
143 or 0d1.2345e12 */
144 const char FLT_CHARS[] = "rRsSfFdDxXpP";
146 /* Opcode mnemonics. */
147 static struct hash_control *opcode_hash_control;
149 void
150 md_begin ()
152 opcode_entry_type *opcode;
153 char *prev_name = "";
154 int idx = 0;
156 opcode_hash_control = hash_new ();
158 for (opcode = z8k_table; opcode->name; opcode++)
160 /* Only enter unique codes into the table. */
161 if (strcmp (opcode->name, prev_name))
163 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
164 idx++;
166 opcode->idx = idx;
167 prev_name = opcode->name;
170 /* Default to z8002. */
171 s_unseg ();
173 /* Insert the pseudo ops, too. */
174 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
176 opcode_entry_type *fake_opcode;
177 fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
178 fake_opcode->name = md_pseudo_table[idx].poc_name;
179 fake_opcode->func = (void *) (md_pseudo_table + idx);
180 fake_opcode->opcode = 250;
181 hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
184 linkrelax = 1;
187 struct z8k_exp {
188 char *e_beg;
189 char *e_end;
190 expressionS e_exp;
193 typedef struct z8k_op {
194 /* 'b','w','r','q'. */
195 char regsize;
197 /* 0 .. 15. */
198 unsigned int reg;
200 int mode;
202 /* Any other register associated with the mode. */
203 unsigned int x_reg;
205 /* Any expression. */
206 expressionS exp;
207 } op_type;
209 static expressionS *da_operand;
210 static expressionS *imm_operand;
212 int reg[16];
213 int the_cc;
214 int the_ctrl;
215 int the_flags;
216 int the_interrupt;
218 char *
219 whatreg (reg, src)
220 int *reg;
221 char *src;
223 if (ISDIGIT (src[1]))
225 *reg = (src[0] - '0') * 10 + src[1] - '0';
226 return src + 2;
228 else
230 *reg = (src[0] - '0');
231 return src + 1;
235 /* Parse operands
237 rh0-rh7, rl0-rl7
238 r0-r15
239 rr0-rr14
240 rq0--rq12
241 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
242 r0l,r0h,..r7l,r7h
243 @WREG
244 @WREG+
245 @-WREG
246 #const
249 /* Try to parse a reg name. Return a pointer to the first character
250 in SRC after the reg name. */
252 char *
253 parse_reg (src, mode, reg)
254 char *src;
255 int *mode;
256 unsigned int *reg;
258 char *res = 0;
259 char regno;
261 if (src[0] == 's' && src[1] == 'p' && (src[2] == 0 || src[2] == ','))
263 if (segmented_mode)
265 *mode = CLASS_REG_LONG;
266 *reg = 14;
268 else
270 *mode = CLASS_REG_WORD;
271 *reg = 15;
273 return src + 2;
275 if (src[0] == 'r')
277 if (src[1] == 'r')
279 if (src[2] < '0' || src[2] > '9')
280 return res; /* Assume no register name but a label starting with 'rr'. */
281 *mode = CLASS_REG_LONG;
282 res = whatreg (reg, src + 2);
283 regno = *reg;
284 if (regno > 14)
285 as_warn (_("register rr%d, out of range."), regno);
287 else if (src[1] == 'h')
289 if (src[2] < '0' || src[2] > '9')
290 return res; /* Assume no register name but a label starting with 'rh'. */
291 *mode = CLASS_REG_BYTE;
292 res = whatreg (reg, src + 2);
293 regno = *reg;
294 if (regno > 7)
295 as_warn (_("register rh%d, out of range."), regno);
297 else if (src[1] == 'l')
299 if (src[2] < '0' || src[2] > '9')
300 return res; /* Assume no register name but a label starting with 'rl'. */
301 *mode = CLASS_REG_BYTE;
302 res = whatreg (reg, src + 2);
303 regno = *reg;
304 if (regno > 7)
305 as_warn (_("register rl%d, out of range."), regno);
306 *reg += 8;
308 else if (src[1] == 'q')
310 if (src[2] < '0' || src[2] > '9')
311 return res; /* Assume no register name but a label starting with 'rq'. */
312 *mode = CLASS_REG_QUAD;
313 res = whatreg (reg, src + 2);
314 regno = *reg;
315 if (regno > 12)
316 as_warn (_("register rq%d, out of range."), regno);
318 else
320 if (src[1] < '0' || src[1] > '9')
321 return res; /* Assume no register name but a label starting with 'r'. */
322 *mode = CLASS_REG_WORD;
323 res = whatreg (reg, src + 1);
324 regno = *reg;
325 if (regno > 15)
326 as_warn (_("register r%d, out of range."), regno);
329 return res;
332 char *
333 parse_exp (s, op)
334 char *s;
335 expressionS *op;
337 char *save = input_line_pointer;
338 char *new;
340 input_line_pointer = s;
341 expression (op);
342 if (op->X_op == O_absent)
343 as_bad (_("missing operand"));
344 new = input_line_pointer;
345 input_line_pointer = save;
346 return new;
349 /* The many forms of operand:
351 <rb>
353 <rr>
354 <rq>
356 #exp
358 exp(r)
359 r(#exp)
360 r(r)
363 static char *
364 checkfor (ptr, what)
365 char *ptr;
366 char what;
368 if (*ptr == what)
369 ptr++;
370 else
371 as_bad (_("expected %c"), what);
373 return ptr;
376 /* Make sure the mode supplied is the size of a word. */
378 static void
379 regword (mode, string)
380 int mode;
381 char *string;
383 int ok;
385 ok = CLASS_REG_WORD;
386 if (ok != mode)
388 as_bad (_("register is wrong size for a word %s"), string);
392 /* Make sure the mode supplied is the size of an address. */
394 static void
395 regaddr (mode, string)
396 int mode;
397 char *string;
399 int ok;
401 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
402 if (ok != mode)
404 as_bad (_("register is wrong size for address %s"), string);
408 struct ctrl_names {
409 int value;
410 char *name;
413 struct ctrl_names ctrl_table[] = {
414 { 0x2, "fcw" },
415 { 0x3, "refresh" },
416 { 0x4, "psapseg" },
417 { 0x5, "psapoff" },
418 { 0x5, "psap" },
419 { 0x6, "nspseg" },
420 { 0x7, "nspoff" },
421 { 0x7, "nsp" },
422 { 0 , 0 }
425 static void
426 get_ctrl_operand (ptr, mode, dst)
427 char **ptr;
428 struct z8k_op *mode;
429 unsigned int dst ATTRIBUTE_UNUSED;
431 char *src = *ptr;
432 int i;
434 while (*src == ' ')
435 src++;
437 mode->mode = CLASS_CTRL;
438 for (i = 0; ctrl_table[i].name; i++)
440 int j;
442 for (j = 0; ctrl_table[i].name[j]; j++)
444 if (ctrl_table[i].name[j] != src[j])
445 goto fail;
447 the_ctrl = ctrl_table[i].value;
448 *ptr = src + j;
449 return;
450 fail:
453 the_ctrl = 0;
454 return;
457 struct flag_names {
458 int value;
459 char *name;
463 struct flag_names flag_table[] = {
464 { 0x1, "p" },
465 { 0x1, "v" },
466 { 0x2, "s" },
467 { 0x4, "z" },
468 { 0x8, "c" },
469 { 0x0, "+" },
470 { 0, 0 }
473 static void
474 get_flags_operand (ptr, mode, dst)
475 char **ptr;
476 struct z8k_op *mode;
477 unsigned int dst ATTRIBUTE_UNUSED;
479 char *src = *ptr;
480 int i;
481 int j;
483 while (*src == ' ')
484 src++;
486 mode->mode = CLASS_FLAGS;
487 the_flags = 0;
488 for (j = 0; j <= 9; j++)
490 if (!src[j])
491 goto done;
492 for (i = 0; flag_table[i].name; i++)
494 if (flag_table[i].name[0] == src[j])
496 the_flags = the_flags | flag_table[i].value;
497 goto match;
500 goto done;
501 match:
504 done:
505 *ptr = src + j;
506 return;
509 struct interrupt_names {
510 int value;
511 char *name;
515 struct interrupt_names intr_table[] = {
516 { 0x1, "nvi" },
517 { 0x2, "vi" },
518 { 0x3, "both" },
519 { 0x3, "all" },
520 { 0, 0 }
523 static void
524 get_interrupt_operand (ptr, mode, dst)
525 char **ptr;
526 struct z8k_op *mode;
527 unsigned int dst ATTRIBUTE_UNUSED;
529 char *src = *ptr;
530 int i;
532 while (*src == ' ')
533 src++;
535 mode->mode = CLASS_IMM;
536 for (i = 0; intr_table[i].name; i++)
538 int j;
540 for (j = 0; intr_table[i].name[j]; j++)
542 if (intr_table[i].name[j] != src[j])
543 goto fail;
545 the_interrupt = intr_table[i].value;
546 *ptr = src + j;
547 return;
548 fail:
551 the_interrupt = 0x0;
552 return;
555 struct cc_names {
556 int value;
557 char *name;
561 struct cc_names table[] = {
562 { 0x0, "f" },
563 { 0x1, "lt" },
564 { 0x2, "le" },
565 { 0x3, "ule" },
566 { 0x4, "ov" },
567 { 0x4, "pe" },
568 { 0x5, "mi" },
569 { 0x6, "eq" },
570 { 0x6, "z" },
571 { 0x7, "c" },
572 { 0x7, "ult" },
573 { 0x8, "t" },
574 { 0x9, "ge" },
575 { 0xa, "gt" },
576 { 0xb, "ugt" },
577 { 0xc, "nov" },
578 { 0xc, "po" },
579 { 0xd, "pl" },
580 { 0xe, "ne" },
581 { 0xe, "nz" },
582 { 0xf, "nc" },
583 { 0xf, "uge" },
584 { 0 , 0 }
587 static void
588 get_cc_operand (ptr, mode, dst)
589 char **ptr;
590 struct z8k_op *mode;
591 unsigned int dst ATTRIBUTE_UNUSED;
593 char *src = *ptr;
594 int i;
596 while (*src == ' ')
597 src++;
599 mode->mode = CLASS_CC;
600 for (i = 0; table[i].name; i++)
602 int j;
604 for (j = 0; table[i].name[j]; j++)
606 if (table[i].name[j] != src[j])
607 goto fail;
609 the_cc = table[i].value;
610 *ptr = src + j;
611 return;
612 fail:
615 the_cc = 0x8;
618 static void
619 get_operand (ptr, mode, dst)
620 char **ptr;
621 struct z8k_op *mode;
622 unsigned int dst ATTRIBUTE_UNUSED;
624 char *src = *ptr;
625 char *end;
627 mode->mode = 0;
629 while (*src == ' ')
630 src++;
631 if (*src == '#')
633 mode->mode = CLASS_IMM;
634 imm_operand = &(mode->exp);
635 src = parse_exp (src + 1, &(mode->exp));
637 else if (*src == '@')
639 int d;
641 mode->mode = CLASS_IR;
642 src = parse_reg (src + 1, &d, &mode->reg);
644 else
646 int regn;
648 end = parse_reg (src, &mode->mode, &regn);
650 if (end)
652 int nw, nr;
654 src = end;
655 if (*src == '(')
657 src++;
658 end = parse_reg (src, &nw, &nr);
659 if (end)
661 /* Got Ra(Rb). */
662 src = end;
664 if (*src != ')')
665 as_bad (_("Missing ) in ra(rb)"));
666 else
667 src++;
669 regaddr (mode->mode, "ra(rb) ra");
670 #if 0
671 regword (mode->mode, "ra(rb) rb");
672 #endif
673 mode->mode = CLASS_BX;
674 mode->reg = regn;
675 mode->x_reg = nr;
676 reg[ARG_RX] = nr;
678 else
680 /* Got Ra(disp). */
681 if (*src == '#')
682 src++;
683 src = parse_exp (src, &(mode->exp));
684 src = checkfor (src, ')');
685 mode->mode = CLASS_BA;
686 mode->reg = regn;
687 mode->x_reg = 0;
688 imm_operand = &(mode->exp);
691 else
693 mode->reg = regn;
694 mode->x_reg = 0;
697 else
699 /* No initial reg. */
700 src = parse_exp (src, &(mode->exp));
701 if (*src == '(')
703 src++;
704 end = parse_reg (src, &(mode->mode), &regn);
705 regword (mode->mode, "addr(Ra) ra");
706 mode->mode = CLASS_X;
707 mode->reg = regn;
708 mode->x_reg = 0;
709 da_operand = &(mode->exp);
710 src = checkfor (end, ')');
712 else
714 /* Just an address. */
715 mode->mode = CLASS_DA;
716 mode->reg = 0;
717 mode->x_reg = 0;
718 da_operand = &(mode->exp);
722 *ptr = src;
725 static char *
726 get_operands (opcode, op_end, operand)
727 opcode_entry_type *opcode;
728 char *op_end;
729 op_type *operand;
731 char *ptr = op_end;
732 char *savptr;
734 switch (opcode->noperands)
736 case 0:
737 operand[0].mode = 0;
738 operand[1].mode = 0;
739 break;
741 case 1:
742 ptr++;
743 if (opcode->arg_info[0] == CLASS_CC)
745 get_cc_operand (&ptr, operand + 0, 0);
747 else if (opcode->arg_info[0] == CLASS_FLAGS)
749 get_flags_operand (&ptr, operand + 0, 0);
751 else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2)))
753 get_interrupt_operand (&ptr, operand + 0, 0);
755 else
757 get_operand (&ptr, operand + 0, 0);
759 operand[1].mode = 0;
760 break;
762 case 2:
763 ptr++;
764 savptr = ptr;
765 if (opcode->arg_info[0] == CLASS_CC)
767 get_cc_operand (&ptr, operand + 0, 0);
769 else if (opcode->arg_info[0] == CLASS_CTRL)
771 get_ctrl_operand (&ptr, operand + 0, 0);
772 if (the_ctrl == 0)
774 ptr = savptr;
775 get_operand (&ptr, operand + 0, 0);
776 if (ptr == 0)
777 return NULL;
778 if (*ptr == ',')
779 ptr++;
780 get_ctrl_operand (&ptr, operand + 1, 1);
781 return ptr;
784 else
786 get_operand (&ptr, operand + 0, 0);
788 if (ptr == 0)
789 return NULL;
790 if (*ptr == ',')
791 ptr++;
792 get_operand (&ptr, operand + 1, 1);
793 break;
795 case 3:
796 ptr++;
797 get_operand (&ptr, operand + 0, 0);
798 if (*ptr == ',')
799 ptr++;
800 get_operand (&ptr, operand + 1, 1);
801 if (*ptr == ',')
802 ptr++;
803 get_operand (&ptr, operand + 2, 2);
804 break;
806 case 4:
807 ptr++;
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 if (*ptr == ',')
816 ptr++;
817 get_cc_operand (&ptr, operand + 3, 3);
818 break;
820 default:
821 abort ();
824 return ptr;
827 /* Passed a pointer to a list of opcodes which use different
828 addressing modes. Return the opcode which matches the opcodes
829 provided. */
831 static opcode_entry_type *
832 get_specific (opcode, operands)
833 opcode_entry_type *opcode;
834 op_type *operands;
837 opcode_entry_type *this_try = opcode;
838 int found = 0;
839 unsigned int noperands = opcode->noperands;
841 int this_index = opcode->idx;
843 while (this_index == opcode->idx && !found)
845 unsigned int i;
847 this_try = opcode++;
848 for (i = 0; i < noperands; i++)
850 unsigned int mode = operands[i].mode;
852 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
854 /* It could be an pc rel operand, if this is a da mode
855 and we like disps, then insert it. */
857 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
859 /* This is the case. */
860 operands[i].mode = CLASS_DISP;
862 else if (mode == CLASS_BA && this_try->arg_info[i])
864 /* Can't think of a way to turn what we've been
865 given into something that's OK. */
866 goto fail;
868 else if (this_try->arg_info[i] & CLASS_PR)
870 if (mode == CLASS_REG_LONG && segmented_mode)
872 /* OK. */
874 else if (mode == CLASS_REG_WORD && !segmented_mode)
876 /* OK. */
878 else
879 goto fail;
881 else
882 goto fail;
884 switch (mode & CLASS_MASK)
886 default:
887 break;
888 case CLASS_X:
889 case CLASS_IR:
890 case CLASS_BA:
891 case CLASS_BX:
892 case CLASS_DISP:
893 case CLASS_REG:
894 case CLASS_REG_WORD:
895 case CLASS_REG_BYTE:
896 case CLASS_REG_QUAD:
897 case CLASS_REG_LONG:
898 case CLASS_REGN0:
899 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
900 break;
904 found = 1;
905 fail:
908 if (found)
909 return this_try;
910 else
911 return 0;
914 #if 0 /* Not used. */
915 static void
916 check_operand (operand, width, string)
917 struct z8k_op *operand;
918 unsigned int width;
919 char *string;
921 if (operand->exp.X_add_symbol == 0
922 && operand->exp.X_op_symbol == 0)
925 /* No symbol involved, let's look at offset, it's dangerous if
926 any of the high bits are not 0 or ff's, find out by oring or
927 anding with the width and seeing if the answer is 0 or all
928 fs. */
929 if ((operand->exp.X_add_number & ~width) != 0 &&
930 (operand->exp.X_add_number | width) != (~0))
932 as_warn (_("operand %s0x%x out of range."),
933 string, operand->exp.X_add_number);
938 #endif
940 static char buffer[20];
942 static void
943 newfix (ptr, type, operand)
944 int ptr;
945 int type;
946 expressionS *operand;
948 if (operand->X_add_symbol
949 || operand->X_op_symbol
950 || operand->X_add_number)
952 fix_new_exp (frag_now,
953 ptr,
955 operand,
957 type);
961 static char *
962 apply_fix (ptr, type, operand, size)
963 char *ptr;
964 int type;
965 expressionS *operand;
966 int size;
968 int n = operand->X_add_number;
970 newfix ((ptr - buffer) / 2, type, operand);
971 switch (size)
973 case 8: /* 8 nibbles == 32 bits. */
974 *ptr++ = n >> 28;
975 *ptr++ = n >> 24;
976 *ptr++ = n >> 20;
977 *ptr++ = n >> 16;
978 case 4: /* 4 nibbles == 16 bits. */
979 *ptr++ = n >> 12;
980 *ptr++ = n >> 8;
981 case 2:
982 *ptr++ = n >> 4;
983 case 1:
984 *ptr++ = n >> 0;
985 break;
987 return ptr;
990 /* Now we know what sort of opcodes it is. Let's build the bytes. */
992 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
994 static void
995 build_bytes (this_try, operand)
996 opcode_entry_type *this_try;
997 struct z8k_op *operand ATTRIBUTE_UNUSED;
999 char *output_ptr = buffer;
1000 int c;
1001 int nib;
1002 int nibble;
1003 unsigned int *class_ptr;
1005 frag_wane (frag_now);
1006 frag_new (0);
1008 memset (buffer, 0, sizeof (buffer));
1009 class_ptr = this_try->byte_info;
1011 for (nibble = 0; (c = *class_ptr++); nibble++)
1014 switch (c & CLASS_MASK)
1016 default:
1017 abort ();
1019 case CLASS_ADDRESS:
1020 /* Direct address, we don't cope with the SS mode right now. */
1021 if (segmented_mode)
1023 /* da_operand->X_add_number |= 0x80000000; -- Now set at relocation time. */
1024 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1026 else
1028 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1030 da_operand = 0;
1031 break;
1032 case CLASS_DISP8:
1033 /* pc rel 8 bit */
1034 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1035 da_operand = 0;
1036 break;
1038 case CLASS_0DISP7:
1039 /* pc rel 7 bit */
1040 *output_ptr = 0;
1041 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1042 da_operand = 0;
1043 break;
1045 case CLASS_1DISP7:
1046 /* pc rel 7 bit */
1047 *output_ptr = 0x80;
1048 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1049 output_ptr[-2] = 0x8;
1050 da_operand = 0;
1051 break;
1053 case CLASS_BIT_1OR2:
1054 *output_ptr = c & 0xf;
1055 if (imm_operand)
1057 if (imm_operand->X_add_number == 2)
1058 *output_ptr |= 2;
1059 else if (imm_operand->X_add_number != 1)
1060 as_bad (_("immediate must be 1 or 2"));
1062 else
1063 as_bad (_("immediate 1 or 2 expected"));
1064 output_ptr++;
1065 break;
1066 case CLASS_CC:
1067 *output_ptr++ = the_cc;
1068 break;
1069 case CLASS_0CCC:
1070 *output_ptr++ = the_ctrl;
1071 break;
1072 case CLASS_1CCC:
1073 *output_ptr++ = the_ctrl | 0x8;
1074 break;
1075 case CLASS_00II:
1076 *output_ptr++ = (~the_interrupt & 0x3);
1077 break;
1078 case CLASS_01II:
1079 *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1080 break;
1081 case CLASS_FLAGS:
1082 *output_ptr++ = the_flags;
1083 break;
1084 case CLASS_IGNORE:
1085 case CLASS_BIT:
1086 *output_ptr++ = c & 0xf;
1087 break;
1088 case CLASS_REGN0:
1089 if (reg[c & 0xf] == 0)
1090 as_bad (_("can't use R0 here"));
1091 /* Fall through. */
1092 case CLASS_REG:
1093 case CLASS_REG_BYTE:
1094 case CLASS_REG_WORD:
1095 case CLASS_REG_LONG:
1096 case CLASS_REG_QUAD:
1097 /* Insert bit mattern of right reg. */
1098 *output_ptr++ = reg[c & 0xf];
1099 break;
1100 case CLASS_DISP:
1101 switch (c & ARG_MASK)
1103 case ARG_DISP12:
1104 output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4);
1105 break;
1106 case ARG_DISP16:
1107 output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4);
1108 break;
1109 default:
1110 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1112 da_operand = 0;
1113 break;
1115 case CLASS_IMM:
1117 nib = 0;
1118 switch (c & ARG_MASK)
1120 case ARG_NIM4:
1121 imm_operand->X_add_number = -imm_operand->X_add_number;
1122 /* Drop through. */
1123 case ARG_IMM4:
1124 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1125 break;
1126 case ARG_IMM4M1:
1127 imm_operand->X_add_number--;
1128 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1129 break;
1130 case ARG_IMMNMINUS1:
1131 imm_operand->X_add_number--;
1132 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1133 break;
1134 case ARG_NIM8:
1135 imm_operand->X_add_number = -imm_operand->X_add_number;
1136 case ARG_IMM8:
1137 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1138 break;
1139 case ARG_IMM16:
1140 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1141 break;
1143 case ARG_IMM32:
1144 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1145 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 (str)
1175 char *str;
1177 char c;
1178 char *op_start;
1179 char *op_end;
1180 struct z8k_op operand[3];
1181 opcode_entry_type *opcode;
1182 opcode_entry_type *prev_opcode;
1184 /* Drop leading whitespace. */
1185 while (*str == ' ')
1186 str++;
1188 /* Find the op code end. */
1189 for (op_start = op_end = str;
1190 *op_end != 0 && *op_end != ' ';
1191 op_end++)
1194 if (op_end == op_start)
1196 as_bad (_("can't find opcode "));
1198 c = *op_end;
1200 *op_end = 0;
1202 opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start);
1204 if (opcode == NULL)
1206 as_bad (_("unknown opcode"));
1207 return;
1210 if (opcode->opcode == 250)
1212 pseudo_typeS *p;
1213 char oc;
1214 char *old = input_line_pointer;
1215 *op_end = c;
1217 /* Was really a pseudo op. */
1219 input_line_pointer = op_end;
1221 oc = *old;
1222 *old = '\n';
1223 while (*input_line_pointer == ' ')
1224 input_line_pointer++;
1225 p = (pseudo_typeS *) (opcode->func);
1227 (p->poc_handler) (p->poc_val);
1228 input_line_pointer = old;
1229 *old = oc;
1231 else
1233 char *new_input_line_pointer;
1235 new_input_line_pointer = get_operands (opcode, op_end, operand);
1236 if (new_input_line_pointer)
1237 input_line_pointer = new_input_line_pointer;
1238 prev_opcode = opcode;
1240 opcode = get_specific (opcode, operand);
1242 if (opcode == 0)
1244 /* Couldn't find an opcode which matched the operands. */
1245 char *where = frag_more (2);
1247 where[0] = 0x0;
1248 where[1] = 0x0;
1250 as_bad (_("Can't find opcode to match operands"));
1251 return;
1254 build_bytes (opcode, operand);
1258 void
1259 tc_crawl_symbol_chain (headers)
1260 object_headers *headers ATTRIBUTE_UNUSED;
1262 printf (_("call to tc_crawl_symbol_chain \n"));
1265 symbolS *
1266 md_undefined_symbol (name)
1267 char *name ATTRIBUTE_UNUSED;
1269 return 0;
1272 void
1273 tc_headers_hook (headers)
1274 object_headers *headers ATTRIBUTE_UNUSED;
1276 printf (_("call to tc_headers_hook \n"));
1279 /* Various routines to kill one day. */
1280 /* Equal to MAX_PRECISION in atof-ieee.c. */
1281 #define MAX_LITTLENUMS 6
1283 /* Turn a string in input_line_pointer into a floating point constant
1284 of type TYPE, and store the appropriate bytes in *LITP. The number
1285 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1286 returned, or NULL on OK. */
1288 char *
1289 md_atof (type, litP, sizeP)
1290 char type;
1291 char *litP;
1292 int *sizeP;
1294 int prec;
1295 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1296 LITTLENUM_TYPE *wordP;
1297 char *t;
1298 char *atof_ieee ();
1300 switch (type)
1302 case 'f':
1303 case 'F':
1304 case 's':
1305 case 'S':
1306 prec = 2;
1307 break;
1309 case 'd':
1310 case 'D':
1311 case 'r':
1312 case 'R':
1313 prec = 4;
1314 break;
1316 case 'x':
1317 case 'X':
1318 prec = 6;
1319 break;
1321 case 'p':
1322 case 'P':
1323 prec = 6;
1324 break;
1326 default:
1327 *sizeP = 0;
1328 return _("Bad call to MD_ATOF()");
1330 t = atof_ieee (input_line_pointer, type, words);
1331 if (t)
1332 input_line_pointer = t;
1334 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1335 for (wordP = words; prec--;)
1337 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1338 litP += sizeof (LITTLENUM_TYPE);
1340 return 0;
1343 const char *md_shortopts = "z:";
1345 struct option md_longopts[] =
1347 {NULL, no_argument, NULL, 0}
1350 size_t md_longopts_size = sizeof (md_longopts);
1353 md_parse_option (c, arg)
1354 int c;
1355 char *arg;
1357 switch (c)
1359 case 'z':
1360 if (!strcmp (arg, "8001"))
1361 s_segm ();
1362 else if (!strcmp (arg, "8002"))
1363 s_unseg ();
1364 else
1366 as_bad (_("invalid architecture -z%s"), arg);
1367 return 0;
1369 break;
1371 default:
1372 return 0;
1375 return 1;
1378 void
1379 md_show_usage (stream)
1380 FILE *stream;
1382 fprintf (stream, _("\
1383 Z8K options:\n\
1384 -z8001 generate segmented code\n\
1385 -z8002 generate unsegmented code\n"));
1388 void
1389 tc_aout_fix_to_chars ()
1391 printf (_("call to tc_aout_fix_to_chars \n"));
1392 abort ();
1395 void
1396 md_convert_frag (headers, seg, fragP)
1397 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 (seg, size)
1407 segT seg;
1408 valueT size;
1410 return ((size + (1 << section_alignment[(int) seg]) - 1)
1411 & (-1 << section_alignment[(int) seg]));
1414 void
1415 md_apply_fix3 (fixP, valP, segment)
1416 fixS *fixP;
1417 valueT * valP;
1418 segT segment ATTRIBUTE_UNUSED;
1420 long val = * (long *) valP;
1421 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1423 switch (fixP->fx_r_type)
1425 case R_IMM4L:
1426 buf[0] = (buf[0] & 0xf0) | ((buf[0] + val) & 0xf);
1427 break;
1429 case R_JR:
1431 *buf++ = val;
1432 #if 0
1433 if (val != 0)
1434 abort ();
1435 #endif
1436 break;
1438 case R_DISP7:
1440 *buf++ += val;
1441 #if 0
1442 if (val != 0)
1443 abort ();
1444 #endif
1445 break;
1447 case R_IMM8:
1448 buf[0] += val;
1449 break;
1450 case R_IMM16:
1451 *buf++ = (val >> 8);
1452 *buf++ = val;
1453 break;
1454 case R_IMM32:
1455 *buf++ = (val >> 24);
1456 *buf++ = (val >> 16);
1457 *buf++ = (val >> 8);
1458 *buf++ = val;
1459 break;
1460 #if 0
1461 case R_DA | R_SEG:
1462 *buf++ = (val >> 16);
1463 *buf++ = 0x00;
1464 *buf++ = (val >> 8);
1465 *buf++ = val;
1466 break;
1467 #endif
1469 case 0:
1470 md_number_to_chars (buf, val, fixP->fx_size);
1471 break;
1473 default:
1474 abort ();
1477 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1478 fixP->fx_done = 1;
1482 md_estimate_size_before_relax (fragP, segment_type)
1483 register fragS *fragP ATTRIBUTE_UNUSED;
1484 register segT segment_type ATTRIBUTE_UNUSED;
1486 printf (_("call tomd_estimate_size_before_relax \n"));
1487 abort ();
1490 /* Put number into target byte order. */
1492 void
1493 md_number_to_chars (ptr, use, nbytes)
1494 char *ptr;
1495 valueT use;
1496 int nbytes;
1498 number_to_chars_bigendian (ptr, use, nbytes);
1501 long
1502 md_pcrel_from (fixP)
1503 fixS *fixP ATTRIBUTE_UNUSED;
1505 abort ();
1508 void
1509 tc_coff_symbol_emit_hook (s)
1510 symbolS *s ATTRIBUTE_UNUSED;
1514 void
1515 tc_reloc_mangle (fix_ptr, intr, base)
1516 fixS *fix_ptr;
1517 struct internal_reloc *intr;
1518 bfd_vma base;
1521 symbolS *symbol_ptr;
1523 if (fix_ptr->fx_addsy
1524 && fix_ptr->fx_subsy)
1526 symbolS *add = fix_ptr->fx_addsy;
1527 symbolS *sub = fix_ptr->fx_subsy;
1529 if (S_GET_SEGMENT (add) != S_GET_SEGMENT (sub))
1530 as_bad (_("Can't subtract symbols in different sections %s %s"),
1531 S_GET_NAME (add), S_GET_NAME (sub));
1532 else
1534 int diff = S_GET_VALUE (add) - S_GET_VALUE (sub);
1536 fix_ptr->fx_addsy = 0;
1537 fix_ptr->fx_subsy = 0;
1538 fix_ptr->fx_offset += diff;
1541 symbol_ptr = fix_ptr->fx_addsy;
1543 /* If this relocation is attached to a symbol then it's ok
1544 to output it. */
1545 if (fix_ptr->fx_r_type == 0)
1547 /* cons likes to create reloc32's whatever the size of the reloc. */
1548 switch (fix_ptr->fx_size)
1550 case 2:
1551 intr->r_type = R_IMM16;
1552 break;
1553 case 1:
1554 intr->r_type = R_IMM8;
1555 break;
1556 case 4:
1557 intr->r_type = R_IMM32;
1558 break;
1559 default:
1560 abort ();
1563 else
1564 intr->r_type = fix_ptr->fx_r_type;
1566 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1567 intr->r_offset = fix_ptr->fx_offset;
1569 if (symbol_ptr)
1570 intr->r_symndx = symbol_ptr->sy_number;
1571 else
1572 intr->r_symndx = -1;