* elfxx-mips.c (_bfd_mips_elf_modify_segment_map): Fix PT_DYNAMIC
[binutils.git] / gas / config / tc-z8k.c
blob451cb8a1edaf434679b269ce85bc31c7d7a2257c
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 s_segm PARAMS ((int));
45 static void even PARAMS ((int));
46 static int tohex PARAMS ((int));
47 static void sval PARAMS ((int));
49 static void
50 s_segm (segm)
51 int segm;
53 if (segm)
55 segmented_mode = 1;
56 machine = bfd_mach_z8001;
57 coff_flags = F_Z8001;
59 else
61 segmented_mode = 0;
62 machine = bfd_mach_z8002;
63 coff_flags = F_Z8002;
67 static void
68 even (ignore)
69 int ignore ATTRIBUTE_UNUSED;
71 frag_align (1, 0, 0);
72 record_alignment (now_seg, 1);
75 static int
76 tohex (c)
77 int c;
79 if (ISDIGIT (c))
80 return c - '0';
81 if (ISLOWER (c))
82 return c - 'a' + 10;
83 return c - 'A' + 10;
86 static void
87 sval (ignore)
88 int ignore ATTRIBUTE_UNUSED;
90 SKIP_WHITESPACE ();
91 if (*input_line_pointer == '\'')
93 int c;
94 input_line_pointer++;
95 c = *input_line_pointer++;
96 while (c != '\'')
98 if (c == '%')
100 c = (tohex (input_line_pointer[0]) << 4)
101 | tohex (input_line_pointer[1]);
102 input_line_pointer += 2;
104 FRAG_APPEND_1_CHAR (c);
105 c = *input_line_pointer++;
107 demand_empty_rest_of_line ();
111 /* This table describes all the machine specific pseudo-ops the assembler
112 has to support. The fields are:
113 pseudo-op name without dot
114 function to call to execute this pseudo-op
115 Integer arg to pass to the function
118 const pseudo_typeS md_pseudo_table[] = {
119 {"int" , cons , 2},
120 {"data.b" , cons , 1},
121 {"data.w" , cons , 2},
122 {"data.l" , cons , 4},
123 {"form" , listing_psize , 0},
124 {"heading", listing_title , 0},
125 {"import" , s_ignore , 0},
126 {"page" , listing_eject , 0},
127 {"program", s_ignore , 0},
128 {"z8001" , s_segm , 1},
129 {"z8002" , s_segm , 0},
131 {"segm" , s_segm , 1},
132 {"unsegm" , s_segm , 0},
133 {"unseg" , s_segm , 0},
134 {"name" , s_app_file , 0},
135 {"global" , s_globl , 0},
136 {"wval" , cons , 2},
137 {"lval" , cons , 4},
138 {"bval" , cons , 1},
139 {"sval" , sval , 0},
140 {"rsect" , obj_coff_section, 0},
141 {"sect" , obj_coff_section, 0},
142 {"block" , s_space , 0},
143 {"even" , even , 0},
144 {0 , 0 , 0}
147 const char EXP_CHARS[] = "eE";
149 /* Chars that mean this number is a floating point constant.
150 As in 0f12.456
151 or 0d1.2345e12 */
152 const char FLT_CHARS[] = "rRsSfFdDxXpP";
154 /* Opcode mnemonics. */
155 static struct hash_control *opcode_hash_control;
157 void
158 md_begin ()
160 const opcode_entry_type *opcode;
161 int idx = -1;
163 opcode_hash_control = hash_new ();
165 for (opcode = z8k_table; opcode->name; opcode++)
167 /* Only enter unique codes into the table. */
168 if (idx != opcode->idx)
169 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
170 idx = opcode->idx;
173 /* Default to z8002. */
174 if (! z8k_target_from_cmdline)
175 s_segm (0);
177 /* Insert the pseudo ops, too. */
178 for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
180 opcode_entry_type *fake_opcode;
181 fake_opcode = (opcode_entry_type *) malloc (sizeof (opcode_entry_type));
182 fake_opcode->name = md_pseudo_table[idx].poc_name;
183 fake_opcode->func = (void *) (md_pseudo_table + idx);
184 fake_opcode->opcode = 250;
185 hash_insert (opcode_hash_control, fake_opcode->name, fake_opcode);
189 struct z8k_exp {
190 char *e_beg;
191 char *e_end;
192 expressionS e_exp;
195 typedef struct z8k_op {
196 /* CLASS_REG_xxx. */
197 int regsize;
199 /* 0 .. 15. */
200 unsigned int reg;
202 int mode;
204 /* Any other register associated with the mode. */
205 unsigned int x_reg;
207 /* Any expression. */
208 expressionS exp;
209 } op_type;
211 static expressionS *da_operand;
212 static expressionS *imm_operand;
214 int reg[16];
215 int the_cc;
216 int the_ctrl;
217 int the_flags;
218 int the_interrupt;
220 static char *whatreg PARAMS ((int *, char *));
221 static char *parse_reg PARAMS ((char *, int *, unsigned int *));
222 static char *parse_exp PARAMS ((char *, expressionS *));
223 static char *checkfor PARAMS ((char *, char));
224 static void regword PARAMS ((int, char *));
225 static void regaddr PARAMS ((int, char *));
226 static void get_ctrl_operand
227 PARAMS ((char **, struct z8k_op *, unsigned int));
228 static void get_flags_operand
229 PARAMS ((char **, struct z8k_op *, unsigned int));
230 static void get_interrupt_operand
231 PARAMS ((char **, struct z8k_op *, unsigned int));
232 static void get_cc_operand
233 PARAMS ((char **, struct z8k_op *, unsigned int));
234 static void get_operand
235 PARAMS ((char **, struct z8k_op *, unsigned int));
236 static char *get_operands
237 PARAMS ((const opcode_entry_type *, char *, op_type *));
238 static opcode_entry_type *get_specific
239 PARAMS ((opcode_entry_type *, op_type *));
240 static void newfix
241 PARAMS ((int, int, int, expressionS *));
242 static char *apply_fix
243 PARAMS ((char *, int, expressionS *, int));
244 static void build_bytes
245 PARAMS ((opcode_entry_type *, struct z8k_op *));
247 static char *
248 whatreg (reg, src)
249 int *reg;
250 char *src;
252 if (ISDIGIT (src[1]))
254 *reg = (src[0] - '0') * 10 + src[1] - '0';
255 return src + 2;
257 else
259 *reg = (src[0] - '0');
260 return src + 1;
264 /* Parse operands
266 rh0-rh7, rl0-rl7
267 r0-r15
268 rr0-rr14
269 rq0--rq12
270 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
271 r0l,r0h,..r7l,r7h
272 @WREG
273 @WREG+
274 @-WREG
275 #const
278 /* Try to parse a reg name. Return a pointer to the first character
279 in SRC after the reg name. */
281 static char *
282 parse_reg (src, mode, reg)
283 char *src;
284 int *mode;
285 unsigned int *reg;
287 char *res = 0;
288 char regno;
290 if (src[0] == 's' && src[1] == 'p' && (src[2] == 0 || src[2] == ','))
292 if (segmented_mode)
294 *mode = CLASS_REG_LONG;
295 *reg = 14;
297 else
299 *mode = CLASS_REG_WORD;
300 *reg = 15;
302 return src + 2;
304 if (src[0] == 'r')
306 if (src[1] == 'r')
308 if (src[2] < '0' || src[2] > '9')
309 return res; /* Assume no register name but a label starting with 'rr'. */
310 *mode = CLASS_REG_LONG;
311 res = whatreg (reg, src + 2);
312 regno = *reg;
313 if (regno > 14)
314 as_bad (_("register rr%d out of range"), regno);
315 if (regno & 1)
316 as_bad (_("register rr%d does not exist"), regno);
318 else if (src[1] == 'h')
320 if (src[2] < '0' || src[2] > '9')
321 return res; /* Assume no register name but a label starting with 'rh'. */
322 *mode = CLASS_REG_BYTE;
323 res = whatreg (reg, src + 2);
324 regno = *reg;
325 if (regno > 7)
326 as_bad (_("register rh%d out of range"), regno);
328 else if (src[1] == 'l')
330 if (src[2] < '0' || src[2] > '9')
331 return res; /* Assume no register name but a label starting with 'rl'. */
332 *mode = CLASS_REG_BYTE;
333 res = whatreg (reg, src + 2);
334 regno = *reg;
335 if (regno > 7)
336 as_bad (_("register rl%d out of range"), regno);
337 *reg += 8;
339 else if (src[1] == 'q')
341 if (src[2] < '0' || src[2] > '9')
342 return res; /* Assume no register name but a label starting with 'rq'. */
343 *mode = CLASS_REG_QUAD;
344 res = whatreg (reg, src + 2);
345 regno = *reg;
346 if (regno > 12)
347 as_bad (_("register rq%d out of range"), regno);
348 if (regno & 3)
349 as_bad (_("register rq%d does not exist"), regno);
351 else
353 if (src[1] < '0' || src[1] > '9')
354 return res; /* Assume no register name but a label starting with 'r'. */
355 *mode = CLASS_REG_WORD;
356 res = whatreg (reg, src + 1);
357 regno = *reg;
358 if (regno > 15)
359 as_bad (_("register r%d out of range"), regno);
362 return res;
365 static char *
366 parse_exp (s, op)
367 char *s;
368 expressionS *op;
370 char *save = input_line_pointer;
371 char *new;
373 input_line_pointer = s;
374 expression (op);
375 if (op->X_op == O_absent)
376 as_bad (_("missing operand"));
377 new = input_line_pointer;
378 input_line_pointer = save;
379 return new;
382 /* The many forms of operand:
384 <rb>
386 <rr>
387 <rq>
389 #exp
391 exp(r)
392 r(#exp)
393 r(r)
396 static char *
397 checkfor (ptr, what)
398 char *ptr;
399 char what;
401 if (*ptr == what)
402 ptr++;
403 else
404 as_bad (_("expected %c"), what);
406 return ptr;
409 /* Make sure the mode supplied is the size of a word. */
411 static void
412 regword (mode, string)
413 int mode;
414 char *string;
416 int ok;
418 ok = CLASS_REG_WORD;
419 if (ok != mode)
421 as_bad (_("register is wrong size for a word %s"), string);
425 /* Make sure the mode supplied is the size of an address. */
427 static void
428 regaddr (mode, string)
429 int mode;
430 char *string;
432 int ok;
434 ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
435 if (ok != mode)
437 as_bad (_("register is wrong size for address %s"), string);
441 struct ctrl_names {
442 int value;
443 char *name;
446 struct ctrl_names ctrl_table[] = {
447 { 0x2, "fcw" },
448 { 0x3, "refresh" },
449 { 0x4, "psapseg" },
450 { 0x5, "psapoff" },
451 { 0x5, "psap" },
452 { 0x6, "nspseg" },
453 { 0x7, "nspoff" },
454 { 0x7, "nsp" },
455 { 0 , 0 }
458 static void
459 get_ctrl_operand (ptr, mode, dst)
460 char **ptr;
461 struct z8k_op *mode;
462 unsigned int dst ATTRIBUTE_UNUSED;
464 char *src = *ptr;
465 int i;
467 while (*src == ' ')
468 src++;
470 mode->mode = CLASS_CTRL;
471 for (i = 0; ctrl_table[i].name; i++)
473 int j;
475 for (j = 0; ctrl_table[i].name[j]; j++)
477 if (ctrl_table[i].name[j] != src[j])
478 goto fail;
480 the_ctrl = ctrl_table[i].value;
481 *ptr = src + j;
482 return;
483 fail:
486 the_ctrl = 0;
487 return;
490 struct flag_names {
491 int value;
492 char *name;
496 struct flag_names flag_table[] = {
497 { 0x1, "p" },
498 { 0x1, "v" },
499 { 0x2, "s" },
500 { 0x4, "z" },
501 { 0x8, "c" },
502 { 0x0, "+" },
503 { 0, 0 }
506 static void
507 get_flags_operand (ptr, mode, dst)
508 char **ptr;
509 struct z8k_op *mode;
510 unsigned int dst ATTRIBUTE_UNUSED;
512 char *src = *ptr;
513 int i;
514 int j;
516 while (*src == ' ')
517 src++;
519 mode->mode = CLASS_FLAGS;
520 the_flags = 0;
521 for (j = 0; j <= 9; j++)
523 if (!src[j])
524 goto done;
525 for (i = 0; flag_table[i].name; i++)
527 if (flag_table[i].name[0] == src[j])
529 the_flags = the_flags | flag_table[i].value;
530 goto match;
533 goto done;
534 match:
537 done:
538 *ptr = src + j;
539 return;
542 struct interrupt_names {
543 int value;
544 char *name;
548 struct interrupt_names intr_table[] = {
549 { 0x1, "nvi" },
550 { 0x2, "vi" },
551 { 0x3, "both" },
552 { 0x3, "all" },
553 { 0, 0 }
556 static void
557 get_interrupt_operand (ptr, mode, dst)
558 char **ptr;
559 struct z8k_op *mode;
560 unsigned int dst ATTRIBUTE_UNUSED;
562 char *src = *ptr;
563 int i;
565 while (*src == ' ')
566 src++;
568 mode->mode = CLASS_IMM;
569 for (i = 0; intr_table[i].name; i++)
571 int j;
573 for (j = 0; intr_table[i].name[j]; j++)
575 if (intr_table[i].name[j] != src[j])
576 goto fail;
578 the_interrupt = intr_table[i].value;
579 *ptr = src + j;
580 return;
581 fail:
584 /* No interrupt type specified, opcode won't do anything. */
585 as_warn (_("opcode has no effect"));
586 the_interrupt = 0x0;
587 return;
590 struct cc_names {
591 int value;
592 char *name;
595 struct cc_names table[] = {
596 { 0x0, "f" },
597 { 0x1, "lt" },
598 { 0x2, "le" },
599 { 0x3, "ule" },
600 { 0x4, "ov" },
601 { 0x4, "pe" },
602 { 0x5, "mi" },
603 { 0x6, "eq" },
604 { 0x6, "z" },
605 { 0x7, "c" },
606 { 0x7, "ult" },
607 { 0x8, "t" },
608 { 0x9, "ge" },
609 { 0xa, "gt" },
610 { 0xb, "ugt" },
611 { 0xc, "nov" },
612 { 0xc, "po" },
613 { 0xd, "pl" },
614 { 0xe, "ne" },
615 { 0xe, "nz" },
616 { 0xf, "nc" },
617 { 0xf, "uge" },
618 { 0 , 0 }
621 static void
622 get_cc_operand (ptr, mode, dst)
623 char **ptr;
624 struct z8k_op *mode;
625 unsigned int dst ATTRIBUTE_UNUSED;
627 char *src = *ptr;
628 int i;
630 while (*src == ' ')
631 src++;
633 mode->mode = CLASS_CC;
634 for (i = 0; table[i].name; i++)
636 int j;
638 for (j = 0; table[i].name[j]; j++)
640 if (table[i].name[j] != src[j])
641 goto fail;
643 the_cc = table[i].value;
644 *ptr = src + j;
645 return;
646 fail:
649 the_cc = 0x8;
652 static void
653 get_operand (ptr, mode, dst)
654 char **ptr;
655 struct z8k_op *mode;
656 unsigned int dst ATTRIBUTE_UNUSED;
658 char *src = *ptr;
659 char *end;
661 mode->mode = 0;
663 while (*src == ' ')
664 src++;
665 if (*src == '#')
667 mode->mode = CLASS_IMM;
668 imm_operand = &(mode->exp);
669 src = parse_exp (src + 1, &(mode->exp));
671 else if (*src == '@')
673 mode->mode = CLASS_IR;
674 src = parse_reg (src + 1, &mode->regsize, &mode->reg);
676 else
678 int regn;
680 end = parse_reg (src, &mode->mode, &regn);
682 if (end)
684 int nw, nr;
686 src = end;
687 if (*src == '(')
689 src++;
690 end = parse_reg (src, &nw, &nr);
691 if (end)
693 /* Got Ra(Rb). */
694 src = end;
696 if (*src != ')')
697 as_bad (_("Missing ) in ra(rb)"));
698 else
699 src++;
701 regaddr (mode->mode, "ra(rb) ra");
702 #if 0
703 regword (mode->mode, "ra(rb) rb");
704 #endif
705 mode->mode = CLASS_BX;
706 mode->reg = regn;
707 mode->x_reg = nr;
708 reg[ARG_RX] = nr;
710 else
712 /* Got Ra(disp). */
713 if (*src == '#')
714 src++;
715 src = parse_exp (src, &(mode->exp));
716 src = checkfor (src, ')');
717 mode->mode = CLASS_BA;
718 mode->reg = regn;
719 mode->x_reg = 0;
720 imm_operand = &(mode->exp);
723 else
725 mode->reg = regn;
726 mode->x_reg = 0;
729 else
731 /* No initial reg. */
732 src = parse_exp (src, &(mode->exp));
733 if (*src == '(')
735 src++;
736 end = parse_reg (src, &(mode->mode), &regn);
737 regword (mode->mode, "addr(Ra) ra");
738 mode->mode = CLASS_X;
739 mode->reg = regn;
740 mode->x_reg = 0;
741 da_operand = &(mode->exp);
742 src = checkfor (end, ')');
744 else
746 /* Just an address. */
747 mode->mode = CLASS_DA;
748 mode->reg = 0;
749 mode->x_reg = 0;
750 da_operand = &(mode->exp);
754 *ptr = src;
757 static char *
758 get_operands (opcode, op_end, operand)
759 const opcode_entry_type *opcode;
760 char *op_end;
761 op_type *operand;
763 char *ptr = op_end;
764 char *savptr;
766 ptr++;
767 switch (opcode->noperands)
769 case 0:
770 operand[0].mode = 0;
771 operand[1].mode = 0;
772 break;
774 case 1:
775 if (opcode->arg_info[0] == CLASS_CC)
776 get_cc_operand (&ptr, operand + 0, 0);
778 else if (opcode->arg_info[0] == CLASS_FLAGS)
779 get_flags_operand (&ptr, operand + 0, 0);
781 else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2)))
782 get_interrupt_operand (&ptr, operand + 0, 0);
784 else
785 get_operand (&ptr, operand + 0, 0);
787 operand[1].mode = 0;
788 break;
790 case 2:
791 savptr = ptr;
792 if (opcode->arg_info[0] == CLASS_CC)
793 get_cc_operand (&ptr, operand + 0, 0);
795 else if (opcode->arg_info[0] == CLASS_CTRL)
797 get_ctrl_operand (&ptr, operand + 0, 0);
799 if (the_ctrl == 0)
801 ptr = savptr;
802 get_operand (&ptr, operand + 0, 0);
804 if (ptr == 0)
805 return NULL;
806 if (*ptr == ',')
807 ptr++;
808 get_ctrl_operand (&ptr, operand + 1, 1);
809 return ptr;
812 else
813 get_operand (&ptr, operand + 0, 0);
815 if (ptr == 0)
816 return NULL;
817 if (*ptr == ',')
818 ptr++;
819 get_operand (&ptr, operand + 1, 1);
820 break;
822 case 3:
823 get_operand (&ptr, operand + 0, 0);
824 if (*ptr == ',')
825 ptr++;
826 get_operand (&ptr, operand + 1, 1);
827 if (*ptr == ',')
828 ptr++;
829 get_operand (&ptr, operand + 2, 2);
830 break;
832 case 4:
833 get_operand (&ptr, operand + 0, 0);
834 if (*ptr == ',')
835 ptr++;
836 get_operand (&ptr, operand + 1, 1);
837 if (*ptr == ',')
838 ptr++;
839 get_operand (&ptr, operand + 2, 2);
840 if (*ptr == ',')
841 ptr++;
842 get_cc_operand (&ptr, operand + 3, 3);
843 break;
845 default:
846 abort ();
849 return ptr;
852 /* Passed a pointer to a list of opcodes which use different
853 addressing modes. Return the opcode which matches the opcodes
854 provided. */
856 static opcode_entry_type *
857 get_specific (opcode, operands)
858 opcode_entry_type *opcode;
859 op_type *operands;
861 opcode_entry_type *this_try = opcode;
862 int found = 0;
863 unsigned int noperands = opcode->noperands;
865 int this_index = opcode->idx;
867 while (this_index == opcode->idx && !found)
869 unsigned int i;
871 this_try = opcode++;
872 for (i = 0; i < noperands; i++)
874 unsigned int mode = operands[i].mode;
876 if (((mode & CLASS_MASK) == CLASS_IR) && ((this_try->arg_info[i] & CLASS_MASK) == CLASS_IRO))
878 mode = operands[i].mode = (operands[i].mode & ~CLASS_MASK) | CLASS_IRO;
881 if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
883 /* It could be a pc rel operand, if this is a da mode
884 and we like disps, then insert it. */
886 if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
888 /* This is the case. */
889 operands[i].mode = CLASS_DISP;
891 else if (mode == CLASS_BA && this_try->arg_info[i])
893 /* Can't think of a way to turn what we've been
894 given into something that's OK. */
895 goto fail;
897 else if (this_try->arg_info[i] & CLASS_PR)
899 if (mode == CLASS_REG_LONG && segmented_mode)
901 /* OK. */
903 else if (mode == CLASS_REG_WORD && !segmented_mode)
905 /* OK. */
907 else
908 goto fail;
910 else
911 goto fail;
913 switch (mode & CLASS_MASK)
915 default:
916 break;
917 case CLASS_IRO:
918 if (operands[i].regsize != CLASS_REG_WORD)
919 as_bad (_("invalid indirect register size"));
920 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
921 break;
922 case CLASS_IR:
923 if ((segmented_mode && operands[i].regsize != CLASS_REG_LONG)
924 || (!segmented_mode && operands[i].regsize != CLASS_REG_WORD))
925 as_bad (_("invalid indirect register size"));
926 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
927 break;
928 case CLASS_X:
929 case CLASS_BA:
930 case CLASS_BX:
931 case CLASS_DISP:
932 case CLASS_REG:
933 case CLASS_REG_WORD:
934 case CLASS_REG_BYTE:
935 case CLASS_REG_QUAD:
936 case CLASS_REG_LONG:
937 case CLASS_REGN0:
938 reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
939 break;
943 found = 1;
944 fail:
947 if (found)
948 return this_try;
949 else
950 return 0;
953 #if 0 /* Not used. */
954 static void
955 check_operand (operand, width, string)
956 struct z8k_op *operand;
957 unsigned int width;
958 char *string;
960 if (operand->exp.X_add_symbol == 0
961 && operand->exp.X_op_symbol == 0)
964 /* No symbol involved, let's look at offset, it's dangerous if
965 any of the high bits are not 0 or ff's, find out by oring or
966 anding with the width and seeing if the answer is 0 or all
967 fs. */
968 if ((operand->exp.X_add_number & ~width) != 0 &&
969 (operand->exp.X_add_number | width) != (~0))
971 as_warn (_("operand %s0x%x out of range"),
972 string, operand->exp.X_add_number);
977 #endif
979 static char buffer[20];
981 static void
982 newfix (ptr, type, size, operand)
983 int ptr;
984 int type;
985 int size; /* nibbles. */
986 expressionS *operand;
988 if (operand->X_add_symbol
989 || operand->X_op_symbol
990 || operand->X_add_number)
992 fix_new_exp (frag_now,
993 ptr,
994 size / 2,
995 operand,
997 type);
1001 static char *
1002 apply_fix (ptr, type, operand, size)
1003 char *ptr;
1004 int type;
1005 expressionS *operand;
1006 int size; /* nibbles. */
1008 long n = operand->X_add_number;
1010 newfix ((ptr - buffer) / 2, type, size + 1, operand);
1011 switch (size)
1013 case 8: /* 8 nibbles == 32 bits. */
1014 *ptr++ = n >> 28;
1015 *ptr++ = n >> 24;
1016 *ptr++ = n >> 20;
1017 *ptr++ = n >> 16;
1018 case 4: /* 4 nibbles == 16 bits. */
1019 *ptr++ = n >> 12;
1020 *ptr++ = n >> 8;
1021 case 2:
1022 *ptr++ = n >> 4;
1023 case 1:
1024 *ptr++ = n >> 0;
1025 break;
1027 return ptr;
1030 /* Now we know what sort of opcodes it is. Let's build the bytes. */
1032 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
1034 static void
1035 build_bytes (this_try, operand)
1036 opcode_entry_type *this_try;
1037 struct z8k_op *operand ATTRIBUTE_UNUSED;
1039 char *output_ptr = buffer;
1040 int c;
1041 int nibble;
1042 unsigned int *class_ptr;
1044 frag_wane (frag_now);
1045 frag_new (0);
1047 memset (buffer, 0, sizeof (buffer));
1048 class_ptr = this_try->byte_info;
1050 for (nibble = 0; (c = *class_ptr++); nibble++)
1053 switch (c & CLASS_MASK)
1055 default:
1056 abort ();
1058 case CLASS_ADDRESS:
1059 /* Direct address, we don't cope with the SS mode right now. */
1060 if (segmented_mode)
1062 /* da_operand->X_add_number |= 0x80000000; -- Now set at relocation time. */
1063 output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1065 else
1067 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1069 da_operand = 0;
1070 break;
1071 case CLASS_DISP8:
1072 /* pc rel 8 bit */
1073 output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1074 da_operand = 0;
1075 break;
1077 case CLASS_0DISP7:
1078 /* pc rel 7 bit */
1079 *output_ptr = 0;
1080 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1081 da_operand = 0;
1082 break;
1084 case CLASS_1DISP7:
1085 /* pc rel 7 bit */
1086 *output_ptr = 0x80;
1087 output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1088 output_ptr[-2] = 0x8;
1089 da_operand = 0;
1090 break;
1092 case CLASS_BIT_1OR2:
1093 *output_ptr = c & 0xf;
1094 if (imm_operand)
1096 if (imm_operand->X_add_number == 2)
1097 *output_ptr |= 2;
1098 else if (imm_operand->X_add_number != 1)
1099 as_bad (_("immediate must be 1 or 2"));
1101 else
1102 as_bad (_("immediate 1 or 2 expected"));
1103 output_ptr++;
1104 break;
1105 case CLASS_CC:
1106 *output_ptr++ = the_cc;
1107 break;
1108 case CLASS_0CCC:
1109 *output_ptr++ = the_ctrl;
1110 break;
1111 case CLASS_1CCC:
1112 *output_ptr++ = the_ctrl | 0x8;
1113 break;
1114 case CLASS_00II:
1115 *output_ptr++ = (~the_interrupt & 0x3);
1116 break;
1117 case CLASS_01II:
1118 *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1119 break;
1120 case CLASS_FLAGS:
1121 *output_ptr++ = the_flags;
1122 break;
1123 case CLASS_IGNORE:
1124 case CLASS_BIT:
1125 *output_ptr++ = c & 0xf;
1126 break;
1127 case CLASS_REGN0:
1128 if (reg[c & 0xf] == 0)
1129 as_bad (_("can't use R0 here"));
1130 /* Fall through. */
1131 case CLASS_REG:
1132 case CLASS_REG_BYTE:
1133 case CLASS_REG_WORD:
1134 case CLASS_REG_LONG:
1135 case CLASS_REG_QUAD:
1136 /* Insert bit mattern of right reg. */
1137 *output_ptr++ = reg[c & 0xf];
1138 break;
1139 case CLASS_DISP:
1140 switch (c & ARG_MASK)
1142 case ARG_DISP12:
1143 output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4);
1144 break;
1145 case ARG_DISP16:
1146 output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4);
1147 break;
1148 default:
1149 output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1151 da_operand = 0;
1152 break;
1154 case CLASS_IMM:
1156 switch (c & ARG_MASK)
1158 case ARG_NIM4:
1159 if (imm_operand->X_add_number > 15)
1161 as_bad (_("immediate value out of range"));
1163 imm_operand->X_add_number = -imm_operand->X_add_number;
1164 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1165 break;
1166 /*case ARG_IMMNMINUS1: not used. */
1167 case ARG_IMM4M1:
1168 imm_operand->X_add_number--;
1169 /* Drop through. */
1170 case ARG_IMM4:
1171 if (imm_operand->X_add_number > 15)
1173 as_bad (_("immediate value out of range"));
1175 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1176 break;
1177 case ARG_NIM8:
1178 imm_operand->X_add_number = -imm_operand->X_add_number;
1179 /* Drop through. */
1180 case ARG_IMM8:
1181 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1182 break;
1183 case ARG_IMM16:
1184 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1185 break;
1186 case ARG_IMM32:
1187 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1188 break;
1189 default:
1190 abort ();
1196 /* Copy from the nibble buffer into the frag. */
1198 int length = (output_ptr - buffer) / 2;
1199 char *src = buffer;
1200 char *fragp = frag_more (length);
1202 while (src < output_ptr)
1204 *fragp = (src[0] << 4) | src[1];
1205 src += 2;
1206 fragp++;
1211 /* This is the guts of the machine-dependent assembler. STR points to a
1212 machine dependent instruction. This function is supposed to emit
1213 the frags/bytes it assembles to. */
1215 void
1216 md_assemble (str)
1217 char *str;
1219 char c;
1220 char *op_start;
1221 char *op_end;
1222 struct z8k_op operand[3];
1223 opcode_entry_type *opcode;
1224 opcode_entry_type *prev_opcode;
1226 /* Drop leading whitespace. */
1227 while (*str == ' ')
1228 str++;
1230 /* Find the op code end. */
1231 for (op_start = op_end = str;
1232 *op_end != 0 && *op_end != ' ';
1233 op_end++)
1236 if (op_end == op_start)
1238 as_bad (_("can't find opcode "));
1240 c = *op_end;
1242 *op_end = 0;
1244 opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start);
1246 if (opcode == NULL)
1248 as_bad (_("unknown opcode"));
1249 return;
1252 if (opcode->opcode == 250)
1254 pseudo_typeS *p;
1255 char oc;
1256 char *old = input_line_pointer;
1257 *op_end = c;
1259 /* Was really a pseudo op. */
1261 input_line_pointer = op_end;
1263 oc = *old;
1264 *old = '\n';
1265 while (*input_line_pointer == ' ')
1266 input_line_pointer++;
1267 p = (pseudo_typeS *) (opcode->func);
1269 (p->poc_handler) (p->poc_val);
1270 input_line_pointer = old;
1271 *old = oc;
1273 else
1275 char *new_input_line_pointer;
1277 new_input_line_pointer = get_operands (opcode, op_end, operand);
1278 if (new_input_line_pointer)
1279 input_line_pointer = new_input_line_pointer;
1280 prev_opcode = opcode; /* XXX is this used ?? */
1282 opcode = get_specific (opcode, operand);
1284 if (opcode == 0)
1286 /* Couldn't find an opcode which matched the operands. */
1287 char *where = frag_more (2);
1289 where[0] = 0x0;
1290 where[1] = 0x0;
1292 as_bad (_("Can't find opcode to match operands"));
1293 return;
1296 build_bytes (opcode, operand);
1300 void
1301 tc_crawl_symbol_chain (headers)
1302 object_headers *headers ATTRIBUTE_UNUSED;
1304 printf (_("call to tc_crawl_symbol_chain \n"));
1307 /* We have no need to default values of symbols. */
1309 symbolS *
1310 md_undefined_symbol (name)
1311 char *name ATTRIBUTE_UNUSED;
1313 return 0;
1316 void
1317 tc_headers_hook (headers)
1318 object_headers *headers ATTRIBUTE_UNUSED;
1320 printf (_("call to tc_headers_hook \n"));
1323 /* Various routines to kill one day. */
1324 /* Equal to MAX_PRECISION in atof-ieee.c. */
1325 #define MAX_LITTLENUMS 6
1327 /* Turn a string in input_line_pointer into a floating point constant
1328 of type TYPE, and store the appropriate bytes in *LITP. The number
1329 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1330 returned, or NULL on OK. */
1332 char *
1333 md_atof (type, litP, sizeP)
1334 char type;
1335 char *litP;
1336 int *sizeP;
1338 int prec;
1339 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1340 LITTLENUM_TYPE *wordP;
1341 char *t;
1343 switch (type)
1345 case 'f':
1346 case 'F':
1347 case 's':
1348 case 'S':
1349 prec = 2;
1350 break;
1352 case 'd':
1353 case 'D':
1354 case 'r':
1355 case 'R':
1356 prec = 4;
1357 break;
1359 case 'x':
1360 case 'X':
1361 prec = 6;
1362 break;
1364 case 'p':
1365 case 'P':
1366 prec = 6;
1367 break;
1369 default:
1370 *sizeP = 0;
1371 return _("Bad call to MD_ATOF()");
1373 t = atof_ieee (input_line_pointer, type, words);
1374 if (t)
1375 input_line_pointer = t;
1377 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1378 for (wordP = words; prec--;)
1380 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1381 litP += sizeof (LITTLENUM_TYPE);
1383 return 0;
1386 const char *md_shortopts = "z:";
1388 struct option md_longopts[] =
1390 #define OPTION_RELAX (OPTION_MD_BASE)
1391 {"linkrelax", no_argument, NULL, OPTION_RELAX},
1392 {NULL, no_argument, NULL, 0}
1395 size_t md_longopts_size = sizeof (md_longopts);
1398 md_parse_option (c, arg)
1399 int c;
1400 char *arg;
1402 switch (c)
1404 case 'z':
1405 if (!strcmp (arg, "8001"))
1406 s_segm (1);
1407 else if (!strcmp (arg, "8002"))
1408 s_segm (0);
1409 else
1411 as_bad (_("invalid architecture -z%s"), arg);
1412 return 0;
1414 z8k_target_from_cmdline = 1;
1415 break;
1417 case OPTION_RELAX:
1418 linkrelax = 1;
1419 break;
1421 default:
1422 return 0;
1425 return 1;
1428 void
1429 md_show_usage (stream)
1430 FILE *stream;
1432 fprintf (stream, _("\
1433 Z8K options:\n\
1434 -z8001 generate segmented code\n\
1435 -z8002 generate unsegmented code\n\
1436 -linkrelax create linker relaxable code\n"));
1439 void
1440 md_convert_frag (headers, seg, fragP)
1441 object_headers *headers ATTRIBUTE_UNUSED;
1442 segT seg ATTRIBUTE_UNUSED;
1443 fragS *fragP ATTRIBUTE_UNUSED;
1445 printf (_("call to md_convert_frag\n"));
1446 abort ();
1449 valueT
1450 md_section_align (seg, size)
1451 segT seg;
1452 valueT size;
1454 return ((size + (1 << section_alignment[(int) seg]) - 1)
1455 & (-1 << section_alignment[(int) seg]));
1458 void
1459 md_apply_fix3 (fixP, valP, segment)
1460 fixS * fixP;
1461 valueT * valP;
1462 segT segment ATTRIBUTE_UNUSED;
1464 long val = * (long *) valP;
1465 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1467 switch (fixP->fx_r_type)
1469 case R_IMM4L:
1470 buf[0] = (buf[0] & 0xf0) | (val & 0xf);
1471 break;
1473 case R_JR:
1474 val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size;
1475 if (val & 1)
1476 as_bad (_("cannot branch to odd address"));
1477 val /= 2;
1478 if (val > 127 || val < -128)
1479 as_bad (_("relative jump out of range"));
1480 *buf++ = val;
1481 fixP->fx_no_overflow = 1;
1482 break;
1484 case R_DISP7:
1485 val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size;
1486 if (val & 1)
1487 as_bad (_("cannot branch to odd address"));
1488 val /= 2;
1489 if (val > 0 || val < -128)
1490 as_bad (_("relative jump out of range"));
1491 *buf = (*buf & 0x80) | (val & 0x7f);
1492 fixP->fx_no_overflow = 1;
1493 break;
1495 case R_CALLR:
1496 if (val > 8191 || val < -8192)
1497 as_bad (_("relative call out of range"));
1498 val = -val;
1499 *buf = (*buf & 0xf0) | ((val >> 8) & 0xf);
1500 buf++;
1501 *buf++ = val & 0xff;
1502 break;
1504 case R_IMM8:
1505 *buf++ = val;
1506 break;
1508 case R_IMM16:
1509 *buf++ = (val >> 8);
1510 *buf++ = val;
1511 break;
1513 case R_IMM32:
1514 *buf++ = (val >> 24);
1515 *buf++ = (val >> 16);
1516 *buf++ = (val >> 8);
1517 *buf++ = val;
1518 break;
1520 case R_REL16:
1521 val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size;
1522 if (val > 32767 || val < -32768)
1523 as_bad (_("relative address out of range"));
1524 *buf++ = (val >> 8);
1525 *buf++ = val;
1526 fixP->fx_no_overflow = 1;
1527 break;
1529 #if 0
1530 case R_DA | R_SEG:
1531 *buf++ = (val >> 16);
1532 *buf++ = 0x00;
1533 *buf++ = (val >> 8);
1534 *buf++ = val;
1535 break;
1536 #endif
1538 case 0:
1539 md_number_to_chars (buf, val, fixP->fx_size);
1540 break;
1542 default:
1543 printf(_("md_apply_fix3: unknown r_type 0x%x\n"), fixP->fx_r_type);
1544 abort ();
1547 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1548 fixP->fx_done = 1;
1552 md_estimate_size_before_relax (fragP, segment_type)
1553 register fragS *fragP ATTRIBUTE_UNUSED;
1554 register segT segment_type ATTRIBUTE_UNUSED;
1556 printf (_("call to md_estimate_size_before_relax\n"));
1557 abort ();
1560 /* Put number into target byte order. */
1562 void
1563 md_number_to_chars (ptr, use, nbytes)
1564 char *ptr;
1565 valueT use;
1566 int nbytes;
1568 number_to_chars_bigendian (ptr, use, nbytes);
1571 long
1572 md_pcrel_from (fixP)
1573 fixS *fixP ATTRIBUTE_UNUSED;
1575 abort ();
1578 void
1579 tc_coff_symbol_emit_hook (s)
1580 symbolS *s ATTRIBUTE_UNUSED;
1584 void
1585 tc_reloc_mangle (fix_ptr, intr, base)
1586 fixS *fix_ptr;
1587 struct internal_reloc *intr;
1588 bfd_vma base;
1591 symbolS *symbol_ptr;
1593 if (fix_ptr->fx_addsy
1594 && fix_ptr->fx_subsy)
1596 symbolS *add = fix_ptr->fx_addsy;
1597 symbolS *sub = fix_ptr->fx_subsy;
1599 if (S_GET_SEGMENT (add) != S_GET_SEGMENT (sub))
1600 as_bad (_("Can't subtract symbols in different sections %s %s"),
1601 S_GET_NAME (add), S_GET_NAME (sub));
1602 else
1604 int diff = S_GET_VALUE (add) - S_GET_VALUE (sub);
1606 fix_ptr->fx_addsy = 0;
1607 fix_ptr->fx_subsy = 0;
1608 fix_ptr->fx_offset += diff;
1611 symbol_ptr = fix_ptr->fx_addsy;
1613 /* If this relocation is attached to a symbol then it's ok
1614 to output it. */
1615 if (fix_ptr->fx_r_type == 0)
1617 /* cons likes to create reloc32's whatever the size of the reloc. */
1618 switch (fix_ptr->fx_size)
1620 case 2:
1621 intr->r_type = R_IMM16;
1622 break;
1623 case 1:
1624 intr->r_type = R_IMM8;
1625 break;
1626 case 4:
1627 intr->r_type = R_IMM32;
1628 break;
1629 default:
1630 abort ();
1633 else
1634 intr->r_type = fix_ptr->fx_r_type;
1636 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1637 intr->r_offset = fix_ptr->fx_offset;
1639 if (symbol_ptr)
1640 intr->r_symndx = symbol_ptr->sy_number;
1641 else
1642 intr->r_symndx = -1;