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