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