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