OSDN Git Service

Locale changes from Bruno Haible <haible@clisp.cons.org>.
[pf3gnuchains/pf3gnuchains3x.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
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 "safe-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' && (src[2] == 0 || src[2] == ','))
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           if (src[2] < '0' || src[2] > '9')
281             return res;  /* Assume no register name but a label starting with 'rr'.  */
282           *mode = CLASS_REG_LONG;
283           res = whatreg (reg, src + 2);
284           regno = *reg;
285           if (regno > 14)
286             as_warn (_("register rr%d, out of range."), regno);
287         }
288       else if (src[1] == 'h')
289         {
290           if (src[2] < '0' || src[2] > '9')
291             return res;  /* Assume no register name but a label starting with 'rh'.  */
292           *mode = CLASS_REG_BYTE;
293           res = whatreg (reg, src + 2);
294           regno = *reg;
295           if (regno > 7)
296             as_warn (_("register rh%d, out of range."), regno);
297         }
298       else if (src[1] == 'l')
299         {
300           if (src[2] < '0' || src[2] > '9')
301             return res;  /* Assume no register name but a label starting with 'rl'.  */
302           *mode = CLASS_REG_BYTE;
303           res = whatreg (reg, src + 2);
304           regno = *reg;
305           if (regno > 7)
306             as_warn (_("register rl%d, out of range."), regno);
307           *reg += 8;
308         }
309       else if (src[1] == 'q')
310         {
311           if (src[2] < '0' || src[2] > '9')
312             return res;  /* Assume no register name but a label starting with 'rq'.  */
313           *mode = CLASS_REG_QUAD;
314           res = whatreg (reg, src + 2);
315           regno = *reg;
316           if (regno > 12)
317             as_warn (_("register rq%d, out of range."), regno);
318         }
319       else
320         {
321           if (src[1] < '0' || src[1] > '9')
322             return res;  /* Assume no register name but a label starting with 'r'.  */
323           *mode = CLASS_REG_WORD;
324           res = whatreg (reg, src + 1);
325           regno = *reg;
326           if (regno > 15)
327             as_warn (_("register r%d, out of range."), regno);
328         }
329     }
330   return res;
331 }
332
333 char *
334 parse_exp (s, op)
335      char *s;
336      expressionS *op;
337 {
338   char *save = input_line_pointer;
339   char *new;
340
341   input_line_pointer = s;
342   expression (op);
343   if (op->X_op == O_absent)
344     as_bad (_("missing operand"));
345   new = input_line_pointer;
346   input_line_pointer = save;
347   return new;
348 }
349
350 /* The many forms of operand:
351
352    <rb>
353    <r>
354    <rr>
355    <rq>
356    @r
357    #exp
358    exp
359    exp(r)
360    r(#exp)
361    r(r)
362    */
363
364 static char *
365 checkfor (ptr, what)
366      char *ptr;
367      char what;
368 {
369   if (*ptr == what)
370     ptr++;
371   else
372     as_bad (_("expected %c"), what);
373
374   return ptr;
375 }
376
377 /* Make sure the mode supplied is the size of a word.  */
378
379 static void
380 regword (mode, string)
381      int mode;
382      char *string;
383 {
384   int ok;
385
386   ok = CLASS_REG_WORD;
387   if (ok != mode)
388     {
389       as_bad (_("register is wrong size for a word %s"), string);
390     }
391 }
392
393 /* Make sure the mode supplied is the size of an address.  */
394
395 static void
396 regaddr (mode, string)
397      int mode;
398      char *string;
399 {
400   int ok;
401
402   ok = segmented_mode ? CLASS_REG_LONG : CLASS_REG_WORD;
403   if (ok != mode)
404     {
405       as_bad (_("register is wrong size for address %s"), string);
406     }
407 }
408
409 struct ctrl_names {
410   int value;
411   char *name;
412 };
413
414 struct ctrl_names ctrl_table[] = {
415   { 0x2, "fcw" },
416   { 0x3, "refresh" },
417   { 0x4, "psapseg" },
418   { 0x5, "psapoff" },
419   { 0x5, "psap" },
420   { 0x6, "nspseg" },
421   { 0x7, "nspoff" },
422   { 0x7, "nsp" },
423   { 0  , 0 }
424 };
425
426 static void
427 get_ctrl_operand (ptr, mode, dst)
428      char **ptr;
429      struct z8k_op *mode;
430      unsigned int dst ATTRIBUTE_UNUSED;
431 {
432   char *src = *ptr;
433   int i;
434
435   while (*src == ' ')
436     src++;
437
438   mode->mode = CLASS_CTRL;
439   for (i = 0; ctrl_table[i].name; i++)
440     {
441       int j;
442
443       for (j = 0; ctrl_table[i].name[j]; j++)
444         {
445           if (ctrl_table[i].name[j] != src[j])
446             goto fail;
447         }
448       the_ctrl = ctrl_table[i].value;
449       *ptr = src + j;
450       return;
451     fail:
452       ;
453     }
454   the_ctrl = 0;
455   return;
456 }
457
458 struct flag_names {
459   int value;
460   char *name;
461
462 };
463
464 struct flag_names flag_table[] = {
465   { 0x1, "p" },
466   { 0x1, "v" },
467   { 0x2, "s" },
468   { 0x4, "z" },
469   { 0x8, "c" },
470   { 0x0, "+" },
471   { 0, 0 }
472 };
473
474 static void
475 get_flags_operand (ptr, mode, dst)
476      char **ptr;
477      struct z8k_op *mode;
478      unsigned int dst ATTRIBUTE_UNUSED;
479 {
480   char *src = *ptr;
481   int i;
482   int j;
483
484   while (*src == ' ')
485     src++;
486
487   mode->mode = CLASS_FLAGS;
488   the_flags = 0;
489   for (j = 0; j <= 9; j++)
490     {
491       if (!src[j])
492         goto done;
493       for (i = 0; flag_table[i].name; i++)
494         {
495           if (flag_table[i].name[0] == src[j])
496             {
497               the_flags = the_flags | flag_table[i].value;
498               goto match;
499             }
500         }
501       goto done;
502     match:
503       ;
504     }
505  done:
506   *ptr = src + j;
507   return;
508 }
509
510 struct interrupt_names {
511   int value;
512   char *name;
513
514 };
515
516 struct interrupt_names intr_table[] = {
517   { 0x1, "nvi" },
518   { 0x2, "vi" },
519   { 0x3, "both" },
520   { 0x3, "all" },
521   { 0, 0 }
522 };
523
524 static void
525 get_interrupt_operand (ptr, mode, dst)
526      char **ptr;
527      struct z8k_op *mode;
528      unsigned int dst ATTRIBUTE_UNUSED;
529 {
530   char *src = *ptr;
531   int i;
532
533   while (*src == ' ')
534     src++;
535
536   mode->mode = CLASS_IMM;
537   for (i = 0; intr_table[i].name; i++)
538     {
539       int j;
540
541       for (j = 0; intr_table[i].name[j]; j++)
542         {
543           if (intr_table[i].name[j] != src[j])
544             goto fail;
545         }
546       the_interrupt = intr_table[i].value;
547       *ptr = src + j;
548       return;
549     fail:
550       ;
551     }
552   the_interrupt = 0x0;
553   return;
554 }
555
556 struct cc_names {
557   int value;
558   char *name;
559
560 };
561
562 struct cc_names table[] = {
563   { 0x0, "f" },
564   { 0x1, "lt" },
565   { 0x2, "le" },
566   { 0x3, "ule" },
567   { 0x4, "ov" },
568   { 0x4, "pe" },
569   { 0x5, "mi" },
570   { 0x6, "eq" },
571   { 0x6, "z" },
572   { 0x7, "c" },
573   { 0x7, "ult" },
574   { 0x8, "t" },
575   { 0x9, "ge" },
576   { 0xa, "gt" },
577   { 0xb, "ugt" },
578   { 0xc, "nov" },
579   { 0xc, "po" },
580   { 0xd, "pl" },
581   { 0xe, "ne" },
582   { 0xe, "nz" },
583   { 0xf, "nc" },
584   { 0xf, "uge" },
585   { 0  ,  0 }
586 };
587
588 static void
589 get_cc_operand (ptr, mode, dst)
590      char **ptr;
591      struct z8k_op *mode;
592      unsigned int dst ATTRIBUTE_UNUSED;
593 {
594   char *src = *ptr;
595   int i;
596
597   while (*src == ' ')
598     src++;
599
600   mode->mode = CLASS_CC;
601   for (i = 0; table[i].name; i++)
602     {
603       int j;
604
605       for (j = 0; table[i].name[j]; j++)
606         {
607           if (table[i].name[j] != src[j])
608             goto fail;
609         }
610       the_cc = table[i].value;
611       *ptr = src + j;
612       return;
613     fail:
614       ;
615     }
616   the_cc = 0x8;
617 }
618
619 static void
620 get_operand (ptr, mode, dst)
621      char **ptr;
622      struct z8k_op *mode;
623      unsigned int dst ATTRIBUTE_UNUSED;
624 {
625   char *src = *ptr;
626   char *end;
627
628   mode->mode = 0;
629
630   while (*src == ' ')
631     src++;
632   if (*src == '#')
633     {
634       mode->mode = CLASS_IMM;
635       imm_operand = &(mode->exp);
636       src = parse_exp (src + 1, &(mode->exp));
637     }
638   else if (*src == '@')
639     {
640       int d;
641
642       mode->mode = CLASS_IR;
643       src = parse_reg (src + 1, &d, &mode->reg);
644     }
645   else
646     {
647       int regn;
648
649       end = parse_reg (src, &mode->mode, &regn);
650
651       if (end)
652         {
653           int nw, nr;
654
655           src = end;
656           if (*src == '(')
657             {
658               src++;
659               end = parse_reg (src, &nw, &nr);
660               if (end)
661                 {
662                   /* Got Ra(Rb).  */
663                   src = end;
664
665                   if (*src != ')')
666                     as_bad (_("Missing ) in ra(rb)"));
667                   else
668                     src++;
669
670                   regaddr (mode->mode, "ra(rb) ra");
671 #if 0
672                   regword (mode->mode, "ra(rb) rb");
673 #endif
674                   mode->mode = CLASS_BX;
675                   mode->reg = regn;
676                   mode->x_reg = nr;
677                   reg[ARG_RX] = nr;
678                 }
679               else
680                 {
681                   /* Got Ra(disp).  */
682                   if (*src == '#')
683                     src++;
684                   src = parse_exp (src, &(mode->exp));
685                   src = checkfor (src, ')');
686                   mode->mode = CLASS_BA;
687                   mode->reg = regn;
688                   mode->x_reg = 0;
689                   imm_operand = &(mode->exp);
690                 }
691             }
692           else
693             {
694               mode->reg = regn;
695               mode->x_reg = 0;
696             }
697         }
698       else
699         {
700           /* No initial reg.  */
701           src = parse_exp (src, &(mode->exp));
702           if (*src == '(')
703             {
704               src++;
705               end = parse_reg (src, &(mode->mode), &regn);
706               regword (mode->mode, "addr(Ra) ra");
707               mode->mode = CLASS_X;
708               mode->reg = regn;
709               mode->x_reg = 0;
710               da_operand = &(mode->exp);
711               src = checkfor (end, ')');
712             }
713           else
714             {
715               /* Just an address.  */
716               mode->mode = CLASS_DA;
717               mode->reg = 0;
718               mode->x_reg = 0;
719               da_operand = &(mode->exp);
720             }
721         }
722     }
723   *ptr = src;
724 }
725
726 static char *
727 get_operands (opcode, op_end, operand)
728      opcode_entry_type *opcode;
729      char *op_end;
730      op_type *operand;
731 {
732   char *ptr = op_end;
733   char *savptr;
734
735   switch (opcode->noperands)
736     {
737     case 0:
738       operand[0].mode = 0;
739       operand[1].mode = 0;
740       break;
741
742     case 1:
743       ptr++;
744       if (opcode->arg_info[0] == CLASS_CC)
745         {
746           get_cc_operand (&ptr, operand + 0, 0);
747         }
748       else if (opcode->arg_info[0] == CLASS_FLAGS)
749         {
750           get_flags_operand (&ptr, operand + 0, 0);
751         }
752       else if (opcode->arg_info[0] == (CLASS_IMM + (ARG_IMM2)))
753         {
754           get_interrupt_operand (&ptr, operand + 0, 0);
755         }
756       else
757         {
758           get_operand (&ptr, operand + 0, 0);
759         }
760       operand[1].mode = 0;
761       break;
762
763     case 2:
764       ptr++;
765       savptr = ptr;
766       if (opcode->arg_info[0] == CLASS_CC)
767         {
768           get_cc_operand (&ptr, operand + 0, 0);
769         }
770       else if (opcode->arg_info[0] == CLASS_CTRL)
771         {
772           get_ctrl_operand (&ptr, operand + 0, 0);
773           if (the_ctrl == 0)
774             {
775               ptr = savptr;
776               get_operand (&ptr, operand + 0, 0);
777               if (ptr == 0)
778                 return NULL;
779               if (*ptr == ',')
780                 ptr++;
781               get_ctrl_operand (&ptr, operand + 1, 1);
782               return ptr;
783             }
784         }
785       else
786         {
787           get_operand (&ptr, operand + 0, 0);
788         }
789       if (ptr == 0)
790         return NULL;
791       if (*ptr == ',')
792         ptr++;
793       get_operand (&ptr, operand + 1, 1);
794       break;
795
796     case 3:
797       ptr++;
798       get_operand (&ptr, operand + 0, 0);
799       if (*ptr == ',')
800         ptr++;
801       get_operand (&ptr, operand + 1, 1);
802       if (*ptr == ',')
803         ptr++;
804       get_operand (&ptr, operand + 2, 2);
805       break;
806
807     case 4:
808       ptr++;
809       get_operand (&ptr, operand + 0, 0);
810       if (*ptr == ',')
811         ptr++;
812       get_operand (&ptr, operand + 1, 1);
813       if (*ptr == ',')
814         ptr++;
815       get_operand (&ptr, operand + 2, 2);
816       if (*ptr == ',')
817         ptr++;
818       get_cc_operand (&ptr, operand + 3, 3);
819       break;
820
821     default:
822       abort ();
823     }
824
825   return ptr;
826 }
827
828 /* Passed a pointer to a list of opcodes which use different
829    addressing modes.  Return the opcode which matches the opcodes
830    provided.  */
831
832 static opcode_entry_type *
833 get_specific (opcode, operands)
834      opcode_entry_type *opcode;
835      op_type *operands;
836
837 {
838   opcode_entry_type *this_try = opcode;
839   int found = 0;
840   unsigned int noperands = opcode->noperands;
841
842   int this_index = opcode->idx;
843
844   while (this_index == opcode->idx && !found)
845     {
846       unsigned int i;
847
848       this_try = opcode++;
849       for (i = 0; i < noperands; i++)
850         {
851           unsigned int mode = operands[i].mode;
852
853           if ((mode & CLASS_MASK) != (this_try->arg_info[i] & CLASS_MASK))
854             {
855               /* It could be an pc rel operand, if this is a da mode
856                  and we like disps, then insert it.  */
857
858               if (mode == CLASS_DA && this_try->arg_info[i] == CLASS_DISP)
859                 {
860                   /* This is the case.  */
861                   operands[i].mode = CLASS_DISP;
862                 }
863               else if (mode == CLASS_BA && this_try->arg_info[i])
864                 {
865                   /* Can't think of a way to turn what we've been
866                      given into something that's OK.  */
867                   goto fail;
868                 }
869               else if (this_try->arg_info[i] & CLASS_PR)
870                 {
871                   if (mode == CLASS_REG_LONG && segmented_mode)
872                     {
873                       /* OK.  */
874                     }
875                   else if (mode == CLASS_REG_WORD && !segmented_mode)
876                     {
877                       /* OK.  */
878                     }
879                   else
880                     goto fail;
881                 }
882               else
883                 goto fail;
884             }
885           switch (mode & CLASS_MASK)
886             {
887             default:
888               break;
889             case CLASS_X:
890             case CLASS_IR:
891             case CLASS_BA:
892             case CLASS_BX:
893             case CLASS_DISP:
894             case CLASS_REG:
895             case CLASS_REG_WORD:
896             case CLASS_REG_BYTE:
897             case CLASS_REG_QUAD:
898             case CLASS_REG_LONG:
899             case CLASS_REGN0:
900               reg[this_try->arg_info[i] & ARG_MASK] = operands[i].reg;
901               break;
902             }
903         }
904
905       found = 1;
906     fail:
907       ;
908     }
909   if (found)
910     return this_try;
911   else
912     return 0;
913 }
914
915 #if 0 /* Not used.  */
916 static void
917 check_operand (operand, width, string)
918      struct z8k_op *operand;
919      unsigned int width;
920      char *string;
921 {
922   if (operand->exp.X_add_symbol == 0
923       && operand->exp.X_op_symbol == 0)
924     {
925
926       /* No symbol involved, let's look at offset, it's dangerous if
927          any of the high bits are not 0 or ff's, find out by oring or
928          anding with the width and seeing if the answer is 0 or all
929          fs.  */
930       if ((operand->exp.X_add_number & ~width) != 0 &&
931           (operand->exp.X_add_number | width) != (~0))
932         {
933           as_warn (_("operand %s0x%x out of range."),
934                    string, operand->exp.X_add_number);
935         }
936     }
937
938 }
939 #endif
940
941 static char buffer[20];
942
943 static void
944 newfix (ptr, type, operand)
945      int ptr;
946      int type;
947      expressionS *operand;
948 {
949   if (operand->X_add_symbol
950       || operand->X_op_symbol
951       || operand->X_add_number)
952     {
953       fix_new_exp (frag_now,
954                    ptr,
955                    1,
956                    operand,
957                    0,
958                    type);
959     }
960 }
961
962 static char *
963 apply_fix (ptr, type, operand, size)
964      char *ptr;
965      int type;
966      expressionS *operand;
967      int size;
968 {
969   int n = operand->X_add_number;
970
971   newfix ((ptr - buffer) / 2, type, operand);
972   switch (size)
973     {
974     case 8:                     /* 8 nibbles == 32 bits.  */
975       *ptr++ = n >> 28;
976       *ptr++ = n >> 24;
977       *ptr++ = n >> 20;
978       *ptr++ = n >> 16;
979     case 4:                     /* 4 nibbles == 16 bits.  */
980       *ptr++ = n >> 12;
981       *ptr++ = n >> 8;
982     case 2:
983       *ptr++ = n >> 4;
984     case 1:
985       *ptr++ = n >> 0;
986       break;
987     }
988   return ptr;
989 }
990
991 /* Now we know what sort of opcodes it is.  Let's build the bytes.  */
992
993 #define INSERT(x,y) *x++ = y>>24; *x++ = y>> 16; *x++=y>>8; *x++ =y;
994
995 static void
996 build_bytes (this_try, operand)
997      opcode_entry_type *this_try;
998      struct z8k_op *operand ATTRIBUTE_UNUSED;
999 {
1000   char *output_ptr = buffer;
1001   int c;
1002   int nib;
1003   int nibble;
1004   unsigned int *class_ptr;
1005
1006   frag_wane (frag_now);
1007   frag_new (0);
1008
1009   memset (buffer, 20, 0);
1010   class_ptr = this_try->byte_info;
1011
1012   for (nibble = 0; (c = *class_ptr++); nibble++)
1013     {
1014
1015       switch (c & CLASS_MASK)
1016         {
1017         default:
1018           abort ();
1019
1020         case CLASS_ADDRESS:
1021           /* Direct address, we don't cope with the SS mode right now.  */
1022           if (segmented_mode)
1023             {
1024               /* da_operand->X_add_number |= 0x80000000;  --  Now set at relocation time.  */
1025               output_ptr = apply_fix (output_ptr, R_IMM32, da_operand, 8);
1026             }
1027           else
1028             {
1029               output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1030             }
1031           da_operand = 0;
1032           break;
1033         case CLASS_DISP8:
1034           /* pc rel 8 bit  */
1035           output_ptr = apply_fix (output_ptr, R_JR, da_operand, 2);
1036           da_operand = 0;
1037           break;
1038
1039         case CLASS_0DISP7:
1040           /* pc rel 7 bit  */
1041           *output_ptr = 0;
1042           output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1043           da_operand = 0;
1044           break;
1045
1046         case CLASS_1DISP7:
1047           /* pc rel 7 bit  */
1048           *output_ptr = 0x80;
1049           output_ptr = apply_fix (output_ptr, R_DISP7, da_operand, 2);
1050           output_ptr[-2] = 0x8;
1051           da_operand = 0;
1052           break;
1053
1054         case CLASS_BIT_1OR2:
1055           *output_ptr = c & 0xf;
1056           if (imm_operand)
1057             {
1058               if (imm_operand->X_add_number == 2)
1059                 *output_ptr |= 2;
1060               else if (imm_operand->X_add_number != 1)
1061                 as_bad (_("immediate must be 1 or 2"));
1062             }
1063           else
1064             as_bad (_("immediate 1 or 2 expected"));
1065           output_ptr++;
1066           break;
1067         case CLASS_CC:
1068           *output_ptr++ = the_cc;
1069           break;
1070         case CLASS_0CCC:
1071           *output_ptr++ = the_ctrl;
1072           break;
1073         case CLASS_1CCC:
1074           *output_ptr++ = the_ctrl | 0x8;
1075           break;
1076         case CLASS_00II:
1077           *output_ptr++ = (~the_interrupt & 0x3);
1078           break;
1079         case CLASS_01II:
1080           *output_ptr++ = (~the_interrupt & 0x3) | 0x4;
1081           break;
1082         case CLASS_FLAGS:
1083           *output_ptr++ = the_flags;
1084           break;
1085         case CLASS_BIT:
1086           *output_ptr++ = c & 0xf;
1087           break;
1088         case CLASS_REGN0:
1089           if (reg[c & 0xf] == 0)
1090             as_bad (_("can't use R0 here"));
1091           /* Fall through.  */
1092         case CLASS_REG:
1093         case CLASS_REG_BYTE:
1094         case CLASS_REG_WORD:
1095         case CLASS_REG_LONG:
1096         case CLASS_REG_QUAD:
1097           /* Insert bit mattern of right reg.  */
1098           *output_ptr++ = reg[c & 0xf];
1099           break;
1100         case CLASS_DISP:
1101           switch (c & ARG_MASK)
1102             {
1103             case ARG_DISP12:
1104               output_ptr = apply_fix (output_ptr, R_CALLR, da_operand, 4);
1105               break;
1106             case ARG_DISP16:
1107               output_ptr = apply_fix (output_ptr, R_REL16, da_operand, 4);
1108               break;
1109             default:
1110               output_ptr = apply_fix (output_ptr, R_IMM16, da_operand, 4);
1111             }
1112           da_operand = 0;
1113           break;
1114
1115         case CLASS_IMM:
1116           {
1117             nib = 0;
1118             switch (c & ARG_MASK)
1119               {
1120               case ARG_IMM4:
1121                 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1122                 break;
1123               case ARG_IMM4M1:
1124                 imm_operand->X_add_number--;
1125                 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1126                 break;
1127               case ARG_IMMNMINUS1:
1128                 imm_operand->X_add_number--;
1129                 output_ptr = apply_fix (output_ptr, R_IMM4L, imm_operand, 1);
1130                 break;
1131               case ARG_NIM8:
1132                 imm_operand->X_add_number = -imm_operand->X_add_number;
1133               case ARG_IMM8:
1134                 output_ptr = apply_fix (output_ptr, R_IMM8, imm_operand, 2);
1135                 break;
1136               case ARG_IMM16:
1137                 output_ptr = apply_fix (output_ptr, R_IMM16, imm_operand, 4);
1138                 break;
1139
1140               case ARG_IMM32:
1141                 output_ptr = apply_fix (output_ptr, R_IMM32, imm_operand, 8);
1142                 break;
1143
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 (str)
1172      char *str;
1173 {
1174   char c;
1175   char *op_start;
1176   char *op_end;
1177   struct z8k_op operand[3];
1178   opcode_entry_type *opcode;
1179   opcode_entry_type *prev_opcode;
1180
1181   /* Drop leading whitespace.  */
1182   while (*str == ' ')
1183     str++;
1184
1185   /* Find the op code end.  */
1186   for (op_start = op_end = str;
1187        *op_end != 0 && *op_end != ' ';
1188        op_end++)
1189     ;
1190
1191   if (op_end == op_start)
1192     {
1193       as_bad (_("can't find opcode "));
1194     }
1195   c = *op_end;
1196
1197   *op_end = 0;
1198
1199   opcode = (opcode_entry_type *) hash_find (opcode_hash_control, op_start);
1200
1201   if (opcode == NULL)
1202     {
1203       as_bad (_("unknown opcode"));
1204       return;
1205     }
1206
1207   if (opcode->opcode == 250)
1208     {
1209       /* Was really a pseudo op.  */
1210
1211       pseudo_typeS *p;
1212       char oc;
1213
1214       char *old = input_line_pointer;
1215       *op_end = c;
1216
1217       input_line_pointer = op_end;
1218
1219       oc = *old;
1220       *old = '\n';
1221       while (*input_line_pointer == ' ')
1222         input_line_pointer++;
1223       p = (pseudo_typeS *) (opcode->func);
1224
1225       (p->poc_handler) (p->poc_val);
1226       input_line_pointer = old;
1227       *old = oc;
1228     }
1229   else
1230     {
1231       input_line_pointer = get_operands (opcode, op_end, operand);
1232       prev_opcode = opcode;
1233
1234       opcode = get_specific (opcode, operand);
1235
1236       if (opcode == 0)
1237         {
1238           /* Couldn't find an opcode which matched the operands.  */
1239           char *where = frag_more (2);
1240
1241           where[0] = 0x0;
1242           where[1] = 0x0;
1243
1244           as_bad (_("Can't find opcode to match operands"));
1245           return;
1246         }
1247
1248       build_bytes (opcode, operand);
1249     }
1250 }
1251
1252 void
1253 tc_crawl_symbol_chain (headers)
1254      object_headers *headers ATTRIBUTE_UNUSED;
1255 {
1256   printf (_("call to tc_crawl_symbol_chain \n"));
1257 }
1258
1259 symbolS *
1260 md_undefined_symbol (name)
1261      char *name ATTRIBUTE_UNUSED;
1262 {
1263   return 0;
1264 }
1265
1266 void
1267 tc_headers_hook (headers)
1268      object_headers *headers ATTRIBUTE_UNUSED;
1269 {
1270   printf (_("call to tc_headers_hook \n"));
1271 }
1272
1273 /* Various routines to kill one day.  */
1274 /* Equal to MAX_PRECISION in atof-ieee.c.  */
1275 #define MAX_LITTLENUMS 6
1276
1277 /* Turn a string in input_line_pointer into a floating point constant
1278    of type TYPE, and store the appropriate bytes in *LITP.  The number
1279    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
1280    returned, or NULL on OK.  */
1281
1282 char *
1283 md_atof (type, litP, sizeP)
1284      char type;
1285      char *litP;
1286      int *sizeP;
1287 {
1288   int prec;
1289   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1290   LITTLENUM_TYPE *wordP;
1291   char *t;
1292   char *atof_ieee ();
1293
1294   switch (type)
1295     {
1296     case 'f':
1297     case 'F':
1298     case 's':
1299     case 'S':
1300       prec = 2;
1301       break;
1302
1303     case 'd':
1304     case 'D':
1305     case 'r':
1306     case 'R':
1307       prec = 4;
1308       break;
1309
1310     case 'x':
1311     case 'X':
1312       prec = 6;
1313       break;
1314
1315     case 'p':
1316     case 'P':
1317       prec = 6;
1318       break;
1319
1320     default:
1321       *sizeP = 0;
1322       return _("Bad call to MD_ATOF()");
1323     }
1324   t = atof_ieee (input_line_pointer, type, words);
1325   if (t)
1326     input_line_pointer = t;
1327
1328   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1329   for (wordP = words; prec--;)
1330     {
1331       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1332       litP += sizeof (LITTLENUM_TYPE);
1333     }
1334   return 0;
1335 }
1336 \f
1337 CONST char *md_shortopts = "z:";
1338
1339 struct option md_longopts[] = {
1340   {NULL, no_argument, NULL, 0}
1341 };
1342
1343 size_t md_longopts_size = sizeof (md_longopts);
1344
1345 int
1346 md_parse_option (c, arg)
1347      int c;
1348      char *arg;
1349 {
1350   switch (c)
1351     {
1352     case 'z':
1353       if (!strcmp (arg, "8001"))
1354         s_segm ();
1355       else if (!strcmp (arg, "8002"))
1356         s_unseg ();
1357       else
1358         {
1359           as_bad (_("invalid architecture -z%s"), arg);
1360           return 0;
1361         }
1362       break;
1363
1364     default:
1365       return 0;
1366     }
1367
1368   return 1;
1369 }
1370
1371 void
1372 md_show_usage (stream)
1373      FILE *stream;
1374 {
1375   fprintf (stream, _("\
1376 Z8K options:\n\
1377 -z8001                  generate segmented code\n\
1378 -z8002                  generate unsegmented code\n"));
1379 }
1380 \f
1381 void
1382 tc_aout_fix_to_chars ()
1383 {
1384   printf (_("call to tc_aout_fix_to_chars \n"));
1385   abort ();
1386 }
1387
1388 void
1389 md_convert_frag (headers, seg, fragP)
1390      object_headers *headers ATTRIBUTE_UNUSED;
1391      segT seg ATTRIBUTE_UNUSED;
1392      fragS *fragP ATTRIBUTE_UNUSED;
1393 {
1394   printf (_("call to md_convert_frag \n"));
1395   abort ();
1396 }
1397
1398 valueT
1399 md_section_align (seg, size)
1400      segT seg;
1401      valueT size;
1402 {
1403   return ((size + (1 << section_alignment[(int) seg]) - 1)
1404           & (-1 << section_alignment[(int) seg]));
1405
1406 }
1407
1408 void
1409 md_apply_fix (fixP, val)
1410      fixS *fixP;
1411      long val;
1412 {
1413   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1414
1415   switch (fixP->fx_r_type)
1416     {
1417     case R_IMM4L:
1418       buf[0] = (buf[0] & 0xf0) | ((buf[0] + val) & 0xf);
1419       break;
1420
1421     case R_JR:
1422
1423       *buf++ = val;
1424 #if 0
1425       if (val != 0)
1426         abort ();
1427 #endif
1428       break;
1429
1430     case R_DISP7:
1431
1432       *buf++ += val;
1433 #if 0
1434       if (val != 0)
1435         abort ();
1436 #endif
1437       break;
1438
1439     case R_IMM8:
1440       buf[0] += val;
1441       break;
1442     case R_IMM16:
1443       *buf++ = (val >> 8);
1444       *buf++ = val;
1445       break;
1446     case R_IMM32:
1447       *buf++ = (val >> 24);
1448       *buf++ = (val >> 16);
1449       *buf++ = (val >> 8);
1450       *buf++ = val;
1451       break;
1452 #if 0
1453     case R_DA | R_SEG:
1454       *buf++ = (val >> 16);
1455       *buf++ = 0x00;
1456       *buf++ = (val >> 8);
1457       *buf++ = val;
1458       break;
1459 #endif
1460
1461     case 0:
1462       md_number_to_chars (buf, val, fixP->fx_size);
1463       break;
1464
1465     default:
1466       abort ();
1467     }
1468 }
1469
1470 int
1471 md_estimate_size_before_relax (fragP, segment_type)
1472      register fragS *fragP ATTRIBUTE_UNUSED;
1473      register segT segment_type ATTRIBUTE_UNUSED;
1474 {
1475   printf (_("call tomd_estimate_size_before_relax \n"));
1476   abort ();
1477 }
1478
1479 /* Put number into target byte order.  */
1480
1481 void
1482 md_number_to_chars (ptr, use, nbytes)
1483      char *ptr;
1484      valueT use;
1485      int nbytes;
1486 {
1487   number_to_chars_bigendian (ptr, use, nbytes);
1488 }
1489
1490 long
1491 md_pcrel_from (fixP)
1492      fixS *fixP ATTRIBUTE_UNUSED;
1493 {
1494   abort ();
1495 }
1496
1497 void
1498 tc_coff_symbol_emit_hook (s)
1499      symbolS *s ATTRIBUTE_UNUSED;
1500 {
1501 }
1502
1503 void
1504 tc_reloc_mangle (fix_ptr, intr, base)
1505      fixS *fix_ptr;
1506      struct internal_reloc *intr;
1507      bfd_vma base;
1508
1509 {
1510   symbolS *symbol_ptr;
1511
1512   if (fix_ptr->fx_addsy
1513       && fix_ptr->fx_subsy)
1514     {
1515       symbolS *add = fix_ptr->fx_addsy;
1516       symbolS *sub = fix_ptr->fx_subsy;
1517
1518       if (S_GET_SEGMENT (add) != S_GET_SEGMENT (sub))
1519         as_bad (_("Can't subtract symbols in different sections %s %s"),
1520                 S_GET_NAME (add), S_GET_NAME (sub));
1521       else
1522         {
1523           int diff = S_GET_VALUE (add) - S_GET_VALUE (sub);
1524
1525           fix_ptr->fx_addsy = 0;
1526           fix_ptr->fx_subsy = 0;
1527           fix_ptr->fx_offset += diff;
1528         }
1529     }
1530   symbol_ptr = fix_ptr->fx_addsy;
1531
1532   /* If this relocation is attached to a symbol then it's ok
1533      to output it.  */
1534   if (fix_ptr->fx_r_type == 0)
1535     {
1536       /* cons likes to create reloc32's whatever the size of the reloc.  */
1537       switch (fix_ptr->fx_size)
1538         {
1539         case 2:
1540           intr->r_type = R_IMM16;
1541           break;
1542         case 1:
1543           intr->r_type = R_IMM8;
1544           break;
1545         case 4:
1546           intr->r_type = R_IMM32;
1547           break;
1548         default:
1549           abort ();
1550         }
1551     }
1552   else
1553     intr->r_type = fix_ptr->fx_r_type;
1554
1555   intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1556   intr->r_offset = fix_ptr->fx_offset;
1557
1558   if (symbol_ptr)
1559     intr->r_symndx = symbol_ptr->sy_number;
1560   else
1561     intr->r_symndx = -1;
1562 }