* config/atof-ieee.c, config/obj-coff.c, config/obj-elf.c,
[external/binutils.git] / gas / config / tc-z8k.c
1 /* tc-z8k.c -- Assemble code for the Zilog Z800n
2    Copyright 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001, 2002, 2003,
3    2005 Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
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.
11
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.
16
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.  */
21
22 /* Written By Steve Chamberlain <sac@cygnus.com>.  */
23
24 #define DEFINE_TABLE
25 #include <stdio.h>
26
27 #include "as.h"
28 #include "bfd.h"
29 #include "safe-ctype.h"
30 #include "opcodes/z8k-opc.h"
31
32 const char comment_chars[] = "!";
33 const char line_comment_chars[] = "#";
34 const char line_separator_chars[] = ";";
35
36 extern int machine;
37 extern int coff_flags;
38 int segmented_mode;
39 const int md_reloc_size;
40
41 /* This is non-zero if target was set from the command line.  */
42 static int z8k_target_from_cmdline;
43
44 static void
45 s_segm (int segm)
46 {
47   if (segm)
48     {
49       segmented_mode = 1;
50       machine = bfd_mach_z8001;
51       coff_flags = F_Z8001;
52     }
53   else
54     {
55       segmented_mode = 0;
56       machine = bfd_mach_z8002;
57       coff_flags = F_Z8002;
58     }
59 }
60
61 static void
62 even (int ignore ATTRIBUTE_UNUSED)
63 {
64   frag_align (1, 0, 0);
65   record_alignment (now_seg, 1);
66 }
67
68 static int
69 tohex (int c)
70 {
71   if (ISDIGIT (c))
72     return c - '0';
73   if (ISLOWER (c))
74     return c - 'a' + 10;
75   return c - 'A' + 10;
76 }
77
78 static void
79 sval (int ignore ATTRIBUTE_UNUSED)
80 {
81   SKIP_WHITESPACE ();
82   if (*input_line_pointer == '\'')
83     {
84       int c;
85       input_line_pointer++;
86       c = *input_line_pointer++;
87       while (c != '\'')
88         {
89           if (c == '%')
90             {
91               c = (tohex (input_line_pointer[0]) << 4)
92                 | tohex (input_line_pointer[1]);
93               input_line_pointer += 2;
94             }
95           FRAG_APPEND_1_CHAR (c);
96           c = *input_line_pointer++;
97         }
98       demand_empty_rest_of_line ();
99     }
100 }
101
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
107    */
108
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},
121
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}
136 };
137
138 const char EXP_CHARS[] = "eE";
139
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";
144
145 /* Opcode mnemonics.  */
146 static struct hash_control *opcode_hash_control;
147
148 void
149 md_begin (void)
150 {
151   const opcode_entry_type *opcode;
152   int idx = -1;
153
154   opcode_hash_control = hash_new ();
155
156   for (opcode = z8k_table; opcode->name; opcode++)
157     {
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;
162     }
163
164   /* Default to z8002.  */
165   if (! z8k_target_from_cmdline)
166     s_segm (0);
167
168   /* Insert the pseudo ops, too.  */
169   for (idx = 0; md_pseudo_table[idx].poc_name; idx++)
170     {
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);
177     }
178 }
179
180 typedef struct z8k_op {
181   /* CLASS_REG_xxx.  */
182   int regsize;
183
184   /* 0 .. 15.  */
185   unsigned int reg;
186
187   int mode;
188
189   /* Any other register associated with the mode.  */
190   unsigned int x_reg;
191
192   /* Any expression.  */
193   expressionS exp;
194 } op_type;
195
196 static expressionS *da_operand;
197 static expressionS *imm_operand;
198
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;
204
205 static char *
206 whatreg (int *reg, char *src)
207 {
208   if (ISDIGIT (src[1]))
209     {
210       *reg = (src[0] - '0') * 10 + src[1] - '0';
211       return src + 2;
212     }
213   else
214     {
215       *reg = (src[0] - '0');
216       return src + 1;
217     }
218 }
219
220 /* Parse operands
221
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
232 */
233
234 /* Try to parse a reg name.  Return a pointer to the first character
235    in SRC after the reg name.  */
236
237 static char *
238 parse_reg (char *src, int *mode, unsigned int *reg)
239 {
240   char *res = 0;
241   char regno;
242
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] == ','))
247     {
248       if (segmented_mode)
249         {
250           *mode = CLASS_REG_LONG;
251           *reg = 14;
252         }
253       else
254         {
255           *mode = CLASS_REG_WORD;
256           *reg = 15;
257         }
258       return src + 2;
259     }
260
261   if (src[0] == 'r' || src[0] == 'R')
262     {
263       if (src[1] == 'r' || src[1] == 'R')
264         {
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);
274         }
275       else if (src[1] == 'h' || src[1] == 'H')
276         {
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);
284         }
285       else if (src[1] == 'l' || src[1] == 'L')
286         {
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;
295         }
296       else if (src[1] == 'q' || src[1] == 'Q')
297         {
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);
307         }
308       else
309         {
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);
317         }
318     }
319   return res;
320 }
321
322 static char *
323 parse_exp (char *s, expressionS *op)
324 {
325   char *save = input_line_pointer;
326   char *new;
327
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;
335 }
336
337 /* The many forms of operand:
338
339    <rb>
340    <r>
341    <rr>
342    <rq>
343    @r
344    #exp
345    exp
346    exp(r)
347    r(#exp)
348    r(r)
349    */
350
351 static char *
352 checkfor (char *ptr, char what)
353 {
354   if (*ptr == what)
355     ptr++;
356   else
357     as_bad (_("expected %c"), what);
358
359   return ptr;
360 }
361
362 /* Make sure the mode supplied is the size of a word.  */
363
364 static void
365 regword (int mode, char *string)
366 {
367   int ok;
368
369   ok = CLASS_REG_WORD;
370   if (ok != mode)
371     {
372       as_bad (_("register is wrong size for a word %s"), string);
373     }
374 }
375
376 /* Make sure the mode supplied is the size of an address.  */
377
378 static void
379 regaddr (int mode, char *string)
380 {
381   int ok;
382
383   ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
384   if (ok != mode)
385     {
386       as_bad (_("register is wrong size for address %s"), string);
387     }
388 }
389
390 struct ctrl_names {
391   int value;
392   char *name;
393 };
394
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 }
406 };
407
408 static void
409 get_ctrl_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
410 {
411   char *src = *ptr;
412   int i, l;
413
414   while (*src == ' ')
415     src++;
416
417   mode->mode = CLASS_CTRL;
418   for (i = 0; ctrl_table[i].name; i++)
419     {
420       l = strlen (ctrl_table[i].name);
421       if (! strncasecmp (ctrl_table[i].name, src, l))
422         {
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;
428         }
429     }
430   the_ctrl = 0;
431 }
432
433 struct flag_names {
434   int value;
435   char *name;
436 };
437
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 }
447 };
448
449 static void
450 get_flags_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
451 {
452   char *src = *ptr;
453   char c;
454   int i;
455   int j;
456
457   while (*src == ' ')
458     src++;
459
460   mode->mode = CLASS_FLAGS;
461   the_flags = 0;
462   for (j = 0; j <= 9; j++)
463     {
464       if (!src[j])
465         goto done;
466       c = TOUPPER(src[j]);
467       for (i = 0; flag_table[i].name; i++)
468         {
469           if (flag_table[i].name[0] == c)
470             {
471               the_flags = the_flags | flag_table[i].value;
472               goto match;
473             }
474         }
475       goto done;
476     match:
477       ;
478     }
479  done:
480   *ptr = src + j;
481 }
482
483 struct interrupt_names {
484   int value;
485   char *name;
486 };
487
488 static struct interrupt_names intr_table[] = {
489   { 0x1, "nvi" },
490   { 0x2, "vi" },
491   { 0x3, "both" },
492   { 0x3, "all" },
493   { 0, 0 }
494 };
495
496 static void
497 get_interrupt_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
498 {
499   char *src = *ptr;
500   int i, l;
501
502   while (*src == ' ')
503     src++;
504
505   mode->mode = CLASS_IMM;
506   the_interrupt = 0;
507
508   while (*src)
509     {
510       for (i = 0; intr_table[i].name; i++)
511         {
512           l = strlen (intr_table[i].name);
513           if (! strncasecmp (intr_table[i].name, src, l))
514             {
515               the_interrupt |= intr_table[i].value;
516               if (*(src + l) && *(src + l) != ',')
517                 {
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;
524                 }
525               src += l;
526               if (! *src)
527                 {
528                   *ptr = src;
529                   return;
530                 }
531             }
532         }
533       if (*src == ',')
534         src++;
535       else
536         {
537           *ptr = src;
538           goto invalid;
539         }
540     }
541
542   /* No interrupt type specified, opcode won't do anything.  */
543   as_warn (_("opcode has no effect"));
544   the_interrupt = 0x0;
545 }
546
547 struct cc_names {
548   int value;
549   char *name;
550 };
551
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 }
584 };
585
586 static void
587 get_cc_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
588 {
589   char *src = *ptr;
590   int i, l;
591
592   while (*src == ' ')
593     src++;
594
595   mode->mode = CLASS_CC;
596   for (i = 0; table[i].name; i++)
597     {
598       l = strlen (table[i].name);
599       if (! strncasecmp (table[i].name, src, l))
600         {
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;
606         }
607     }
608   the_cc = 0x8;  /* Not recognizing the cc defaults to t.  (Assuming no cc present.)  */
609 }
610
611 static void
612 get_operand (char **ptr, struct z8k_op *mode, unsigned int dst ATTRIBUTE_UNUSED)
613 {
614   char *src = *ptr;
615   char *end;
616
617   mode->mode = 0;
618
619   while (*src == ' ')
620     src++;
621   if (*src == '#')
622     {
623       mode->mode = CLASS_IMM;
624       imm_operand = &(mode->exp);
625       src = parse_exp (src + 1, &(mode->exp));
626     }
627   else if (*src == '@')
628     {
629       mode->mode = CLASS_IR;
630       src = parse_reg (src + 1, &mode->regsize, &mode->reg);
631     }
632   else
633     {
634       int regn;
635
636       end = parse_reg (src, &mode->mode, &regn);
637
638       if (end)
639         {
640           int nw, nr;
641
642           src = end;
643           if (*src == '(')
644             {
645               src++;
646               end = parse_reg (src, &nw, &nr);
647               if (end)
648                 {
649                   /* Got Ra(Rb).  */
650                   src = end;
651
652                   if (*src != ')')
653                     as_bad (_("Missing ) in ra(rb)"));
654                   else
655                     src++;
656
657                   regaddr (mode->mode, "ra(rb) ra");
658                   mode->mode = CLASS_BX;
659                   mode->reg = regn;
660                   mode->x_reg = nr;
661                   reg[ARG_RX] = nr;
662                 }
663               else
664                 {
665                   /* Got Ra(disp).  */
666                   if (*src == '#')
667                     src++;
668                   src = parse_exp (src, &(mode->exp));
669                   src = checkfor (src, ')');
670                   mode->mode = CLASS_BA;
671                   mode->reg = regn;
672                   mode->x_reg = 0;
673                   imm_operand = &(mode->exp);
674                 }
675             }
676           else
677             {
678               mode->reg = regn;
679               mode->x_reg = 0;
680             }
681         }
682       else
683         {
684           /* No initial reg.  */
685           src = parse_exp (src, &(mode->exp));
686           if (*src == '(')
687             {
688               src++;
689               end = parse_reg (src, &(mode->mode), &regn);
690               regword (mode->mode, "addr(Ra) ra");
691               mode->mode = CLASS_X;
692               mode->reg = regn;
693               mode->x_reg = 0;
694               da_operand = &(mode->exp);
695               src = checkfor (end, ')');
696             }
697           else
698             {
699               /* Just an address.  */
700               mode->mode = CLASS_DA;
701               mode->reg = 0;
702               mode->x_reg = 0;
703               da_operand = &(mode->exp);
704             }
705         }
706     }
707   *ptr = src;
708 }
709
710 static char *
711 get_operands (const opcode_entry_type *opcode, char *op_end, op_type *operand)
712 {
713   char *ptr = op_end;
714   char *savptr;
715
716   switch (opcode->noperands)
717     {
718     case 0:
719       operand[0].mode = 0;
720       operand[1].mode = 0;
721       while (*ptr == ' ')
722         ptr++;
723       break;
724
725     case 1:
726       if (opcode->arg_info[0] == CLASS_CC)
727         {
728           get_cc_operand (&ptr, operand + 0, 0);
729           while (*ptr == ' ')
730             ptr++;
731           if (*ptr && ! is_end_of_line[(unsigned char) *ptr])
732             {
733               as_bad (_("invalid condition code '%s'"), ptr);
734               while (*ptr && ! is_end_of_line[(unsigned char) *ptr])
735                 ptr++;   /* Consume rest of line.  */
736             }
737         }
738       else if (opcode->arg_info[0] == CLASS_FLAGS)
739         {
740           get_flags_operand (&ptr, operand + 0, 0);
741           while (*ptr == ' ')
742             ptr++;
743           if (*ptr && ! is_end_of_line[(unsigned char) *ptr])
744             {
745               as_bad (_("invalid flag '%s'"), ptr);
746               while (*ptr && ! is_end_of_line[(unsigned char) *ptr])
747                 ptr++;   /* Consume rest of line.  */
748             }
749         }
750       else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2)))
751         get_interrupt_operand (&ptr, operand + 0, 0);
752       else
753         get_operand (&ptr, operand + 0, 0);
754
755       operand[1].mode = 0;
756       break;
757
758     case 2:
759       savptr = ptr;
760       if (opcode->arg_info[0] == CLASS_CC)
761         {
762           get_cc_operand (&ptr, operand + 0, 0);
763           while (*ptr == ' ')
764             ptr++;
765           if (*ptr != ',' && strchr (ptr + 1, ','))
766             {
767               savptr = ptr;
768               while (*ptr != ',')
769                 ptr++;
770               *ptr = 0;
771               ptr++;
772               as_bad (_("invalid condition code '%s'"), savptr);
773             }
774         }
775       else if (opcode->arg_info[0] == CLASS_CTRL)
776         {
777           get_ctrl_operand (&ptr, operand + 0, 0);
778
779           if (the_ctrl == 0)
780             {
781               ptr = savptr;
782               get_operand (&ptr, operand + 0, 0);
783
784               if (ptr == 0)
785                 return NULL;
786               if (*ptr == ',')
787                 ptr++;
788               get_ctrl_operand (&ptr, operand + 1, 1);
789               if (the_ctrl == 0)
790                 return NULL;
791               return ptr;
792             }
793         }
794       else
795         get_operand (&ptr, operand + 0, 0);
796
797       if (ptr == 0)
798         return NULL;
799       if (*ptr == ',')
800         ptr++;
801       get_operand (&ptr, operand + 1, 1);
802       break;
803
804     case 3:
805       get_operand (&ptr, operand + 0, 0);
806       if (*ptr == ',')
807         ptr++;
808       get_operand (&ptr, operand + 1, 1);
809       if (*ptr == ',')
810         ptr++;
811       get_operand (&ptr, operand + 2, 2);
812       break;
813
814     case 4:
815       get_operand (&ptr, operand + 0, 0);
816       if (*ptr == ',')
817         ptr++;
818       get_operand (&ptr, operand + 1, 1);
819       if (*ptr == ',')
820         ptr++;
821       get_operand (&ptr, operand + 2, 2);
822       if (*ptr == ',')
823         ptr++;
824       get_cc_operand (&ptr, operand + 3, 3);
825       break;
826
827     default:
828       abort ();
829     }
830
831   return ptr;
832 }
833
834 /* Passed a pointer to a list of opcodes which use different
835    addressing modes.  Return the opcode which matches the opcodes
836    provided.  */
837
838 static opcode_entry_type *
839 get_specific (opcode_entry_type *opcode, op_type *operands)
840 {
841   opcode_entry_type *this_try = opcode;
842   int found = 0;
843   unsigned int noperands = opcode->noperands;
844
845   int this_index = opcode->idx;
846
847   while (this_index == opcode->idx && !found)
848     {
849       unsigned int i;
850
851       this_try = opcode++;
852       for (i = 0; i < noperands; i++)
853         {
854           unsigned int mode = operands[i].mode;
855
856           if (((mode & CLASS_MASK) == CLASS_IR) && ((this_try->arg_info[i] & CLASS_MASK) == CLASS_IRO))
857             {
858               mode = operands[i].mode = (operands[i].mode & ~CLASS_MASK) | CLASS_IRO;
859             }
860
861           if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
862             {
863               /* It could be a pc rel operand, if this is a da mode
864                  and we like disps, then insert it.  */
865
866               if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
867                 {
868                   /* This is the case.  */
869                   operands[i].mode = CLASS_DISP;
870                 }
871               else if (mode == CLASS_BA && this_try->arg_info[i])
872                 {
873                   /* Can't think of a way to turn what we've been
874                      given into something that's OK.  */
875                   goto fail;
876                 }
877               else if (this_try->arg_info[i] & CLASS_PR)
878                 {
879                   if (mode == CLASS_REG_LONG && segmented_mode)
880                     {
881                       /* OK.  */
882                     }
883                   else if (mode == CLASS_REG_WORD && !segmented_mode)
884                     {
885                       /* OK.  */
886                     }
887                   else
888                     goto fail;
889                 }
890               else
891                 goto fail;
892             }
893           switch (mode & CLASS_MASK)
894             {
895             default:
896               break;
897             case CLASS_IRO:
898               if (operands[i].regsize != CLASS_REG_WORD)
899                 as_bad (_("invalid indirect register size"));
900               reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
901               break;
902             case CLASS_IR:
903               if ((segmented_mode && operands[i].regsize != CLASS_REG_LONG)
904                   || (!segmented_mode && operands[i].regsize != CLASS_REG_WORD))
905                 as_bad (_("invalid indirect register size"));
906               reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
907               break;
908             case CLASS_X:
909             case CLASS_BA:
910             case CLASS_BX:
911             case CLASS_DISP:
912             case CLASS_REG:
913             case CLASS_REG_WORD:
914             case CLASS_REG_BYTE:
915             case CLASS_REG_QUAD:
916             case CLASS_REG_LONG:
917             case CLASS_REGN0:
918               reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
919               break;
920             case CLASS_CTRL:
921               if (this_try->opcode == OPC_ldctlb && the_ctrl != 1)
922                 as_bad (_("invalid control register name"));
923               break;
924             }
925         }
926
927       found = 1;
928     fail:
929       ;
930     }
931   if (found)
932     return this_try;
933   else
934     return 0;
935 }
936
937 static char buffer[20];
938
939 static void
940 newfix (int ptr, int type, int size, expressionS *operand)
941 {
942   int is_pcrel = 0;
943
944   /* size is in nibbles.  */
945
946   if (operand->X_add_symbol
947       || operand->X_op_symbol
948       || operand->X_add_number)
949     {
950       switch(type)
951         {
952         case R_JR:
953         case R_DISP7:
954         case R_CALLR:
955           is_pcrel = 1;
956         }
957       fix_new_exp (frag_now,
958                    ptr,
959                    size / 2,
960                    operand,
961                    is_pcrel,
962                    type);
963     }
964 }
965
966 static char *
967 apply_fix (char *ptr, int type, expressionS *operand, int size)
968 {
969   long n = operand->X_add_number;
970
971   /* size is in nibbles.  */
972
973   newfix ((ptr - buffer) / 2, type, size + 1, operand);
974   switch (size)
975     {
976     case 8:                     /* 8 nibbles == 32 bits.  */
977       *ptr++ = n >> 28;
978       *ptr++ = n >> 24;
979       *ptr++ = n >> 20;
980       *ptr++ = n >> 16;
981     case 4:                     /* 4 nibbles == 16 bits.  */
982       *ptr++ = n >> 12;
983       *ptr++ = n >> 8;
984     case 2:
985       *ptr++ = n >> 4;
986     case 1:
987       *ptr++ = n >> 0;
988       break;
989     }
990   return ptr;
991 }
992
993 /* Now we know what sort of opcodes it is.  Let's build the bytes.  */
994
995 static void
996 build_bytes (opcode_entry_type *this_try, struct z8k_op *operand ATTRIBUTE_UNUSED)
997 {
998   char *output_ptr = buffer;
999   int c;
1000   int nibble;
1001   unsigned int *class_ptr;
1002
1003   frag_wane (frag_now);
1004   frag_new (0);
1005
1006   memset (buffer, 0, sizeof (buffer));
1007   class_ptr = this_try->byte_info;
1008
1009   for (nibble = 0; (c = *class_ptr++); nibble++)
1010     {
1011
1012       switch (c & CLASS_MASK)
1013         {
1014         default:
1015           abort ();
1016
1017         case CLASS_ADDRESS:
1018           /* Direct address, we don't cope with the SS mode right now.  */
1019           if (segmented_mode)
1020             {
1021               /* da_operand->X_add_number |= 0x80000000;  --  Now set at relocation time.  */
1022               output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1023             }
1024           else
1025             {
1026               output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1027             }
1028           da_operand = 0;
1029           break;
1030         case CLASS_DISP8:
1031           /* pc rel 8 bit  */
1032           output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1033           da_operand = 0;
1034           break;
1035
1036         case CLASS_0DISP7:
1037           /* pc rel 7 bit  */
1038           *output_ptr = 0;
1039           output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1040           da_operand = 0;
1041           break;
1042
1043         case CLASS_1DISP7:
1044           /* pc rel 7 bit  */
1045           *output_ptr = 0x80;
1046           output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1047           output_ptr[-2] = 0x8;
1048           da_operand = 0;
1049           break;
1050
1051         case CLASS_BIT_1OR2:
1052           *output_ptr = c & 0xf;
1053           if (imm_operand)
1054             {
1055               if (imm_operand->X_add_number == 2)
1056                 *output_ptr |= 2;
1057               else if (imm_operand->X_add_number != 1)
1058                 as_bad (_("immediate must be 1 or 2"));
1059             }
1060           else
1061             as_bad (_("immediate 1 or 2 expected"));
1062           output_ptr++;
1063           break;
1064         case CLASS_CC:
1065           *output_ptr++ = the_cc;
1066           break;
1067         case CLASS_0CCC:
1068           if (the_ctrl < 2 || the_ctrl > 7)
1069             as_bad (_("invalid control register name"));
1070           *output_ptr++ = the_ctrl;
1071           break;
1072         case CLASS_1CCC:
1073           if (the_ctrl < 2 || the_ctrl > 7)
1074             as_bad (_("invalid control register name"));
1075           *output_ptr++ = the_ctrl | 0x8;
1076           break;
1077         case CLASS_00II:
1078           *output_ptr++ = (~the_interrupt & 0x3);
1079           break;
1080         case CLASS_01II:
1081           *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1082           break;
1083         case CLASS_FLAGS:
1084           *output_ptr++ = the_flags;
1085           break;
1086         case CLASS_IGNORE:
1087         case CLASS_BIT:
1088           *output_ptr++ = c & 0xf;
1089           break;
1090         case CLASS_REGN0:
1091           if (reg[c & 0xf] == 0)
1092             as_bad (_("can't use R0 here"));
1093           /* Fall through.  */
1094         case CLASS_REG:
1095         case CLASS_REG_BYTE:
1096         case CLASS_REG_WORD:
1097         case CLASS_REG_LONG:
1098         case CLASS_REG_QUAD:
1099           /* Insert bit mattern of right reg.  */
1100           *output_ptr++ = reg[c & 0xf];
1101           break;
1102         case CLASS_DISP:
1103           switch (c & ARG_MASK)
1104             {
1105             case ARG_DISP12:
1106               output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4);
1107               break;
1108             case ARG_DISP16:
1109               output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4);
1110               break;
1111             default:
1112               output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1113             }
1114           da_operand = 0;
1115           break;
1116
1117         case CLASS_IMM:
1118           {
1119             switch (c & ARG_MASK)
1120               {
1121               case ARG_NIM4:
1122                 if (imm_operand->X_add_number > 15)
1123                   {
1124                     as_bad (_("immediate value out of range"));
1125                   }
1126                 imm_operand->X_add_number = -imm_operand->X_add_number;
1127                 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1128                 break;
1129               /*case ARG_IMMNMINUS1: not used.  */
1130               case ARG_IMM4M1:
1131                 imm_operand->X_add_number--;
1132                 /* Drop through.  */
1133               case ARG_IMM4:
1134                 if (imm_operand->X_add_number > 15)
1135                   {
1136                     as_bad (_("immediate value out of range"));
1137                   }
1138                 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1139                 break;
1140               case ARG_NIM8:
1141                 imm_operand->X_add_number = -imm_operand->X_add_number;
1142                 /* Drop through.  */
1143               case ARG_IMM8:
1144                 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1145                 break;
1146               case ARG_IMM16:
1147                 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1148                 break;
1149               case ARG_IMM32:
1150                 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1151                 break;
1152               default:
1153                 abort ();
1154               }
1155           }
1156         }
1157     }
1158
1159   /* Copy from the nibble buffer into the frag.  */
1160   {
1161     int length = (output_ptr - buffer) / 2;
1162     char *src = buffer;
1163     char *fragp = frag_more (length);
1164
1165     while (src < output_ptr)
1166       {
1167         *fragp = (src[0] << 4) | src[1];
1168         src += 2;
1169         fragp++;
1170       }
1171   }
1172 }
1173
1174 /* This is the guts of the machine-dependent assembler.  STR points to a
1175    machine dependent instruction.  This function is supposed to emit
1176    the frags/bytes it assembles to.  */
1177
1178 void
1179 md_assemble (char *str)
1180 {
1181   char c;
1182   char *op_start;
1183   char *op_end;
1184   struct z8k_op operand[3];
1185   opcode_entry_type *opcode;
1186
1187   /* Drop leading whitespace.  */
1188   while (*str == ' ')
1189     str++;
1190
1191   /* Find the op code end.  */
1192   for (op_start = op_end = str;
1193        *op_end != 0 && *op_end != ' ' && ! is_end_of_line[(unsigned char) *op_end];
1194        op_end++)
1195     ;
1196
1197   if (op_end == op_start)
1198     {
1199       as_bad (_("can't find opcode "));
1200     }
1201   c = *op_end;
1202
1203   *op_end = 0;  /* Zero-terminate op code string for hash_find() call.  */
1204
1205   opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start);
1206
1207   if (opcode == NULL)
1208     {
1209       as_bad (_("unknown opcode"));
1210       return;
1211     }
1212
1213   *op_end = c;  /* Restore original string.  */
1214
1215   if (opcode->opcode == 250)
1216     {
1217       pseudo_typeS *p;
1218       char oc;
1219       char *old = input_line_pointer;
1220
1221       /* Was really a pseudo op.  */
1222
1223       input_line_pointer = op_end;
1224
1225       oc = *old;
1226       *old = '\n';
1227       while (*input_line_pointer == ' ')
1228         input_line_pointer++;
1229       p = (pseudo_typeS *) (opcode->func);
1230
1231       (p->poc_handler) (p->poc_val);
1232       input_line_pointer = old;
1233       *old = oc;
1234     }
1235   else
1236     {
1237       char *new_input_line_pointer;
1238
1239       new_input_line_pointer = get_operands (opcode, op_end, operand);
1240       if (new_input_line_pointer)
1241         {
1242           input_line_pointer = new_input_line_pointer;
1243           opcode = get_specific (opcode, operand);
1244         }
1245
1246       if (new_input_line_pointer == NULL || opcode == NULL)
1247         {
1248           /* Couldn't find an opcode which matched the operands.  */
1249           char *where = frag_more (2);
1250
1251           where[0] = 0x0;
1252           where[1] = 0x0;
1253
1254           as_bad (_("Can't find opcode to match operands"));
1255           return;
1256         }
1257
1258       build_bytes (opcode, operand);
1259     }
1260 }
1261
1262 void
1263 tc_crawl_symbol_chain (object_headers *headers ATTRIBUTE_UNUSED)
1264 {
1265   printf (_("call to tc_crawl_symbol_chain \n"));
1266 }
1267
1268 /* We have no need to default values of symbols.  */
1269
1270 symbolS *
1271 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1272 {
1273   return 0;
1274 }
1275
1276 void
1277 tc_headers_hook (object_headers *headers ATTRIBUTE_UNUSED)
1278 {
1279   printf (_("call to tc_headers_hook \n"));
1280 }
1281
1282 /* Various routines to kill one day.  */
1283 /* Equal to MAX_PRECISION in atof-ieee.c.  */
1284 #define MAX_LITTLENUMS 6
1285
1286 /* Turn a string in input_line_pointer into a floating point constant
1287    of type TYPE, and store the appropriate bytes in *LITP.  The number
1288    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1289    returned, or NULL on OK.  */
1290
1291 char *
1292 md_atof (int type, char *litP, int *sizeP)
1293 {
1294   int prec;
1295   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1296   LITTLENUM_TYPE *wordP;
1297   char *t;
1298
1299   switch (type)
1300     {
1301     case 'f':
1302     case 'F':
1303     case 's':
1304     case 'S':
1305       prec = 2;
1306       break;
1307
1308     case 'd':
1309     case 'D':
1310     case 'r':
1311     case 'R':
1312       prec = 4;
1313       break;
1314
1315     case 'x':
1316     case 'X':
1317       prec = 6;
1318       break;
1319
1320     case 'p':
1321     case 'P':
1322       prec = 6;
1323       break;
1324
1325     default:
1326       *sizeP = 0;
1327       return _("Bad call to MD_ATOF()");
1328     }
1329   t = atof_ieee (input_line_pointer, type, words);
1330   if (t)
1331     input_line_pointer = t;
1332
1333   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1334   for (wordP = words; prec--;)
1335     {
1336       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1337       litP += sizeof (LITTLENUM_TYPE);
1338     }
1339   return 0;
1340 }
1341 \f
1342 const char *md_shortopts = "z:";
1343
1344 struct option md_longopts[] =
1345   {
1346 #define OPTION_RELAX  (OPTION_MD_BASE)
1347     {"linkrelax", no_argument, NULL, OPTION_RELAX},
1348     {NULL, no_argument, NULL, 0}
1349   };
1350
1351 size_t md_longopts_size = sizeof (md_longopts);
1352
1353 int
1354 md_parse_option (int c, char *arg)
1355 {
1356   switch (c)
1357     {
1358     case 'z':
1359       if (!strcmp (arg, "8001"))
1360         s_segm (1);
1361       else if (!strcmp (arg, "8002"))
1362         s_segm (0);
1363       else
1364         {
1365           as_bad (_("invalid architecture -z%s"), arg);
1366           return 0;
1367         }
1368       z8k_target_from_cmdline = 1;
1369       break;
1370
1371     case OPTION_RELAX:
1372       linkrelax = 1;
1373       break;
1374
1375     default:
1376       return 0;
1377     }
1378
1379   return 1;
1380 }
1381
1382 void
1383 md_show_usage (FILE *stream)
1384 {
1385   fprintf (stream, _("\
1386  Z8K options:\n\
1387   -z8001                  generate segmented code\n\
1388   -z8002                  generate unsegmented code\n\
1389   -linkrelax              create linker relaxable code\n"));
1390 }
1391 \f
1392 void
1393 md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED,
1394                  segT seg ATTRIBUTE_UNUSED,
1395                  fragS *fragP ATTRIBUTE_UNUSED)
1396 {
1397   printf (_("call to md_convert_frag\n"));
1398   abort ();
1399 }
1400
1401 valueT
1402 md_section_align (segT seg, valueT size)
1403 {
1404   return ((size + (1 << section_alignment[(int) seg]) - 1)
1405           & (-1 << section_alignment[(int) seg]));
1406 }
1407
1408 /* Attempt to simplify or eliminate a fixup. To indicate that a fixup
1409    has been eliminated, set fix->fx_done. If fix->fx_addsy is non-NULL,
1410    we will have to generate a reloc entry.  */
1411 void
1412 md_apply_fix3 (fixS *fixP, valueT *valP, segT segment ATTRIBUTE_UNUSED)
1413 {
1414   long val = * (long *) valP;
1415   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1416
1417   switch (fixP->fx_r_type)
1418     {
1419     case R_IMM4L:
1420       buf[0] = (buf[0] & 0xf0) | (val & 0xf);
1421       break;
1422
1423     case R_JR:
1424       if (fixP->fx_addsy)
1425         {
1426           fixP->fx_no_overflow = 1;
1427           fixP->fx_done = 0;
1428         }
1429       else
1430         {
1431           if (val & 1)
1432             as_bad_where (fixP->fx_file, fixP->fx_line,
1433                           _("cannot branch to odd address"));
1434           val /= 2;
1435           if (val > 127 || val < -128)
1436             as_bad_where (fixP->fx_file, fixP->fx_line,
1437                           _("relative jump out of range"));
1438           *buf++ = val;
1439           fixP->fx_no_overflow = 1;
1440           fixP->fx_done = 1;
1441         }
1442       break;
1443
1444     case R_DISP7:
1445       if (fixP->fx_addsy)
1446         {
1447           fixP->fx_no_overflow = 1;
1448           fixP->fx_done = 0;
1449         }
1450       else
1451         {
1452           if (val & 1)
1453             as_bad_where (fixP->fx_file, fixP->fx_line,
1454                           _("cannot branch to odd address"));
1455           val /= 2;
1456           if (val > 0 || val < -127)
1457             as_bad_where (fixP->fx_file, fixP->fx_line,
1458                           _("relative jump out of range"));
1459           *buf = (*buf & 0x80) | (-val & 0x7f);
1460           fixP->fx_no_overflow = 1;
1461           fixP->fx_done = 1;
1462         }
1463       break;
1464
1465     case R_CALLR:
1466       if (fixP->fx_addsy)
1467         {
1468           fixP->fx_no_overflow = 1;
1469           fixP->fx_done = 0;
1470         }
1471       else
1472         {
1473           if (val & 1)
1474             as_bad_where (fixP->fx_file, fixP->fx_line,
1475                           _("cannot branch to odd address"));
1476           if (val > 4096 || val < -4095)
1477             as_bad_where (fixP->fx_file, fixP->fx_line,
1478                           _("relative call out of range"));
1479           val = -val / 2;
1480           *buf = (*buf & 0xf0) | ((val >> 8) & 0xf);
1481           buf++;
1482           *buf++ = val & 0xff;
1483           fixP->fx_no_overflow = 1;
1484           fixP->fx_done = 1;
1485         }
1486       break;
1487
1488     case R_IMM8:
1489       *buf++ = val;
1490       break;
1491
1492     case R_IMM16:
1493       *buf++ = (val >> 8);
1494       *buf++ = val;
1495       break;
1496
1497     case R_IMM32:
1498       *buf++ = (val >> 24);
1499       *buf++ = (val >> 16);
1500       *buf++ = (val >> 8);
1501       *buf++ = val;
1502       break;
1503
1504     case R_REL16:
1505       val = val - fixP->fx_frag->fr_address + fixP->fx_where - fixP->fx_size;
1506       if (val > 32767 || val < -32768)
1507         as_bad_where (fixP->fx_file, fixP->fx_line,
1508                       _("relative address out of range"));
1509       *buf++ = (val >> 8);
1510       *buf++ = val;
1511       fixP->fx_no_overflow = 1;
1512       break;
1513
1514     case 0:
1515       md_number_to_chars (buf, val, fixP->fx_size);
1516       break;
1517
1518     default:
1519       printf(_("md_apply_fix3: unknown r_type 0x%x\n"), fixP->fx_r_type);
1520       abort ();
1521     }
1522
1523   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1524     fixP->fx_done = 1;
1525 }
1526
1527 int
1528 md_estimate_size_before_relax (fragS *fragP ATTRIBUTE_UNUSED,
1529                                segT segment_type ATTRIBUTE_UNUSED)
1530 {
1531   printf (_("call to md_estimate_size_before_relax\n"));
1532   abort ();
1533 }
1534
1535 /* Put number into target byte order.  */
1536
1537 void
1538 md_number_to_chars (char *ptr, valueT use, int nbytes)
1539 {
1540   number_to_chars_bigendian (ptr, use, nbytes);
1541 }
1542
1543 /* On the Z8000, a PC-relative offset is relative to the address of the
1544    instruction plus its size.  */
1545 long
1546 md_pcrel_from (fixS *fixP)
1547 {
1548   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
1549 }
1550
1551 void
1552 tc_coff_symbol_emit_hook (symbolS *s ATTRIBUTE_UNUSED)
1553 {
1554 }
1555
1556 void
1557 tc_reloc_mangle (fixS *fix_ptr, struct internal_reloc *intr, bfd_vma base)
1558 {
1559   symbolS *symbol_ptr;
1560
1561   if (fix_ptr->fx_addsy
1562       && fix_ptr->fx_subsy)
1563     {
1564       symbolS *add = fix_ptr->fx_addsy;
1565       symbolS *sub = fix_ptr->fx_subsy;
1566
1567       if (S_GET_SEGMENT (add) != S_GET_SEGMENT (sub))
1568         as_bad (_("Can't subtract symbols in different sections %s %s"),
1569                 S_GET_NAME (add), S_GET_NAME (sub));
1570       else
1571         {
1572           int diff = S_GET_VALUE (add) - S_GET_VALUE (sub);
1573
1574           fix_ptr->fx_addsy = 0;
1575           fix_ptr->fx_subsy = 0;
1576           fix_ptr->fx_offset += diff;
1577         }
1578     }
1579   symbol_ptr = fix_ptr->fx_addsy;
1580
1581   /* If this relocation is attached to a symbol then it's ok
1582      to output it.  */
1583   if (fix_ptr->fx_r_type == 0)
1584     {
1585       /* cons likes to create reloc32's whatever the size of the reloc.  */
1586       switch (fix_ptr->fx_size)
1587         {
1588         case 2:
1589           intr->r_type = R_IMM16;
1590           break;
1591         case 1:
1592           intr->r_type = R_IMM8;
1593           break;
1594         case 4:
1595           intr->r_type = R_IMM32;
1596           break;
1597         default:
1598           abort ();
1599         }
1600     }
1601   else
1602     intr->r_type = fix_ptr->fx_r_type;
1603
1604   intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1605   intr->r_offset = fix_ptr->fx_offset;
1606
1607   if (symbol_ptr)
1608     intr->r_symndx = symbol_ptr->sy_number;
1609   else
1610     intr->r_symndx = -1;
1611 }