1/* tc-i370.c -- Assembler for the IBM 360/370/390 instruction set.
2   Loosely based on the ppc files by Linas Vepstas <linas@linas.org> 1998, 99
3   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4   2004, 2005, 2006, 2007, 2009, 2010  Free Software Foundation, Inc.
5   Written by Ian Lance Taylor, Cygnus Support.
6
7   This file is part of GAS, the GNU Assembler.
8
9   GAS is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 3, or (at your option)
12   any later version.
13
14   GAS is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19   You should have received a copy of the GNU General Public License
20   along with GAS; see the file COPYING.  If not, write to the Free
21   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22   02110-1301, USA.  */
23
24/* This assembler implements a very hacked version of an elf-like thing
25   that gcc emits (when gcc is suitably hacked).  To make it behave more
26   HLASM-like, try turning on the -M or --mri flag (as there are various
27   similarities between HLASM and the MRI assemblers, such as section
28   names, lack of leading . in pseudo-ops, DC and DS, etc.  */
29
30#include "as.h"
31#include "safe-ctype.h"
32#include "subsegs.h"
33#include "struc-symbol.h"
34
35#include "opcode/i370.h"
36
37#ifdef OBJ_ELF
38#include "elf/i370.h"
39#endif
40
41/* This is the assembler for the System/390 Architecture.  */
42
43/* Tell the main code what the endianness is.  */
44extern int target_big_endian;
45
46
47/* Generic assembler global variables which must be defined by all
48   targets.  */
49
50#ifdef OBJ_ELF
51/* This string holds the chars that always start a comment.  If the
52   pre-processor is disabled, these aren't very useful.  The macro
53   tc_comment_chars points to this.  We use this, rather than the
54   usual comment_chars, so that we can switch for Solaris conventions.  */
55static const char i370_eabi_comment_chars[] = "#";
56
57const char *i370_comment_chars = i370_eabi_comment_chars;
58#else
59const char comment_chars[] = "#";
60#endif
61
62/* Characters which start a comment at the beginning of a line.  */
63const char line_comment_chars[] = "#*";
64
65/* Characters which may be used to separate multiple commands on a
66   single line.  */
67const char line_separator_chars[] = ";";
68
69/* Characters which are used to indicate an exponent in a floating
70   point number.  */
71const char EXP_CHARS[] = "eE";
72
73/* Characters which mean that a number is a floating point constant,
74   as in 0d1.0.  */
75const char FLT_CHARS[] = "dD";
76
77void
78md_show_usage (FILE *stream)
79{
80  fprintf (stream, "\
81S/370 options: (these have not yet been tested and may not work) \n\
82-u        		ignored\n\
83-mregnames        	Allow symbolic names for registers\n\
84-mno-regnames        	Do not allow symbolic names for registers\n");
85#ifdef OBJ_ELF
86  fprintf (stream, "\
87-mrelocatable        	support for GCC's -mrelocatble option\n\
88-mrelocatable-lib       support for GCC's -mrelocatble-lib option\n\
89-V        		print assembler version number\n");
90#endif
91}
92
93/* Whether to use user friendly register names.  */
94#define TARGET_REG_NAMES_P TRUE
95
96static bfd_boolean reg_names_p = TARGET_REG_NAMES_P;
97
98
99/* Predefined register names if -mregnames
100   In general, there are lots of them, in an attempt to be compatible
101   with a number of assemblers.  */
102
103/* Structure to hold information about predefined registers.  */
104struct pd_reg
105  {
106    char *name;
107    int value;
108  };
109
110/* List of registers that are pre-defined:
111
112   Each general register has predefined names of the form:
113   1. r<reg_num> which has the value <reg_num>.
114   2. r.<reg_num> which has the value <reg_num>.
115
116   Each floating point register has predefined names of the form:
117   1. f<reg_num> which has the value <reg_num>.
118   2. f.<reg_num> which has the value <reg_num>.
119
120   There are only four floating point registers, and these are
121   commonly labelled 0,2,4 and 6.  Thus, there is no f1, f3, etc.
122
123   There are individual registers as well:
124   rbase or r.base has the value  3  (base register)
125   rpgt or r.pgt   has the value  4  (page origin table pointer)
126   rarg or r.arg   has the value 11  (argument pointer)
127   rtca or r.tca   has the value 12  (table of contents pointer)
128   rtoc or r.toc   has the value 12  (table of contents pointer)
129   sp or r.sp      has the value 13  (stack pointer)
130   dsa or r.dsa    has the value 13  (stack pointer)
131   lr              has the value 14  (link reg)
132
133   The table is sorted. Suitable for searching by a binary search.  */
134
135static const struct pd_reg pre_defined_registers[] =
136{
137  { "arg", 11 },   /* Argument Pointer.  */
138  { "base", 3 },   /* Base Reg.  */
139
140  { "f.0", 0 },    /* Floating point registers.  */
141  { "f.2", 2 },
142  { "f.4", 4 },
143  { "f.6", 6 },
144
145  { "f0", 0 },
146  { "f2", 2 },
147  { "f4", 4 },
148  { "f6", 6 },
149
150  { "dsa",13 },    /* Stack pointer.  */
151  { "lr", 14 },    /* Link Register.  */
152  { "pgt", 4 },    /* Page Origin Table Pointer.  */
153
154  { "r.0", 0 },    /* General Purpose Registers.  */
155  { "r.1", 1 },
156  { "r.10", 10 },
157  { "r.11", 11 },
158  { "r.12", 12 },
159  { "r.13", 13 },
160  { "r.14", 14 },
161  { "r.15", 15 },
162  { "r.2", 2 },
163  { "r.3", 3 },
164  { "r.4", 4 },
165  { "r.5", 5 },
166  { "r.6", 6 },
167  { "r.7", 7 },
168  { "r.8", 8 },
169  { "r.9", 9 },
170
171  { "r.arg", 11 },  /* Argument Pointer.  */
172  { "r.base", 3 },  /* Base Reg.  */
173  { "r.dsa", 13 },  /* Stack Pointer.  */
174  { "r.pgt", 4 },   /* Page Origin Table Pointer.  */
175  { "r.sp", 13 },   /* Stack Pointer.  */
176
177  { "r.tca", 12 },  /* Pointer to the table of contents.  */
178  { "r.toc", 12 },  /* Pointer to the table of contents.  */
179
180  { "r0", 0 },      /* More general purpose registers.  */
181  { "r1", 1 },
182  { "r10", 10 },
183  { "r11", 11 },
184  { "r12", 12 },
185  { "r13", 13 },
186  { "r14", 14 },
187  { "r15", 15 },
188  { "r2", 2 },
189  { "r3", 3 },
190  { "r4", 4 },
191  { "r5", 5 },
192  { "r6", 6 },
193  { "r7", 7 },
194  { "r8", 8 },
195  { "r9", 9 },
196
197  { "rbase", 3 },  /* Base Reg.  */
198
199  { "rtca", 12 },  /* Pointer to the table of contents.  */
200  { "rtoc", 12 },  /* Pointer to the table of contents.  */
201
202  { "sp", 13 },   /* Stack Pointer.  */
203
204};
205
206#define REG_NAME_CNT        (sizeof (pre_defined_registers) / sizeof (struct pd_reg))
207
208/* Given NAME, find the register number associated with that name, return
209   the integer value associated with the given name or -1 on failure.  */
210
211static int
212reg_name_search (const struct pd_reg *regs,
213		 int regcount,
214		 const char *name)
215{
216  int middle, low, high;
217  int cmp;
218
219  low = 0;
220  high = regcount - 1;
221
222  do
223    {
224      middle = (low + high) / 2;
225      cmp = strcasecmp (name, regs[middle].name);
226      if (cmp < 0)
227        high = middle - 1;
228      else if (cmp > 0)
229        low = middle + 1;
230      else
231        return regs[middle].value;
232    }
233  while (low <= high);
234
235  return -1;
236}
237
238/* Summary of register_name().
239
240   in:        Input_line_pointer points to 1st char of operand.
241
242   out:        An expressionS.
243        The operand may have been a register: in this case, X_op == O_register,
244        X_add_number is set to the register number, and truth is returned.
245          Input_line_pointer->(next non-blank) char after operand, or is in its
246        original state.  */
247
248static bfd_boolean
249register_name (expressionS *expressionP)
250{
251  int reg_number;
252  char *name;
253  char *start;
254  char c;
255
256  /* Find the spelling of the operand.  */
257  start = name = input_line_pointer;
258  if (name[0] == '%' && ISALPHA (name[1]))
259    name = ++input_line_pointer;
260
261  else if (!reg_names_p)
262    return FALSE;
263
264  while (' ' == *name)
265    name = ++input_line_pointer;
266
267  /* If it's a number, treat it as a number.  If it's alpha, look to
268     see if it's in the register table.  */
269  if (!ISALPHA (name[0]))
270    reg_number = get_single_number ();
271  else
272    {
273      c = get_symbol_end ();
274      reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
275
276      /* Put back the delimiting char.  */
277      *input_line_pointer = c;
278    }
279
280  /* If numeric, make sure its not out of bounds.  */
281  if ((0 <= reg_number) && (16 >= reg_number))
282    {
283      expressionP->X_op = O_register;
284      expressionP->X_add_number = reg_number;
285
286      /* Make the rest nice.  */
287      expressionP->X_add_symbol = NULL;
288      expressionP->X_op_symbol = NULL;
289      return TRUE;
290    }
291
292  /* Reset the line as if we had not done anything.  */
293  input_line_pointer = start;
294  return FALSE;
295}
296
297/* Local variables.  */
298
299/* The type of processor we are assembling for.  This is one or more
300   of the I370_OPCODE flags defined in opcode/i370.h.  */
301static int i370_cpu = 0;
302
303/* The base register to use for opcode with optional operands.
304   We define two of these: "text" and "other".  Normally, "text"
305   would get used in the .text section for branches, while "other"
306   gets used in the .data section for address constants.
307
308   The idea of a second base register in a different section
309   is foreign to the usual HLASM-style semantics; however, it
310   allows us to provide support for dynamically loaded libraries,
311   by allowing us to place address constants in a section other
312   than the text section. The "other" section need not be the
313   .data section, it can be any section that isn't the .text section.
314
315   Note that HLASM defines a multiple, concurrent .using semantic
316   that we do not: in calculating offsets, it uses either the most
317   recent .using directive, or the one with the smallest displacement.
318   This allows HLASM to support a quasi-block-scope-like behaviour.
319   Handy for people writing assembly by hand ... but not supported
320   by us.  */
321static int i370_using_text_regno = -1;
322static int i370_using_other_regno = -1;
323
324/* The base address for address literals.  */
325static expressionS i370_using_text_baseaddr;
326static expressionS i370_using_other_baseaddr;
327
328/* the "other" section, used only for syntax error detection.  */
329static segT i370_other_section = undefined_section;
330
331/* Opcode hash table.  */
332static struct hash_control *i370_hash;
333
334/* Macro hash table.  */
335static struct hash_control *i370_macro_hash;
336
337#ifdef OBJ_ELF
338/* What type of shared library support to use.  */
339static enum { SHLIB_NONE, SHLIB_PIC, SHILB_MRELOCATABLE } shlib = SHLIB_NONE;
340#endif
341
342/* Flags to set in the elf header.  */
343static flagword i370_flags = 0;
344
345#ifndef WORKING_DOT_WORD
346int md_short_jump_size = 4;
347int md_long_jump_size = 4;
348#endif
349
350#ifdef OBJ_ELF
351const char *md_shortopts = "l:um:K:VQ:";
352#else
353const char *md_shortopts = "um:";
354#endif
355struct option md_longopts[] =
356{
357  {NULL, no_argument, NULL, 0}
358};
359size_t md_longopts_size = sizeof (md_longopts);
360
361int
362md_parse_option (int c, char *arg)
363{
364  switch (c)
365    {
366    case 'u':
367      /* -u means that any undefined symbols should be treated as
368         external, which is the default for gas anyhow.  */
369      break;
370
371#ifdef OBJ_ELF
372    case 'K':
373      /* Recognize -K PIC */
374      if (strcmp (arg, "PIC") == 0 || strcmp (arg, "pic") == 0)
375        {
376          shlib = SHLIB_PIC;
377          i370_flags |= EF_I370_RELOCATABLE_LIB;
378        }
379      else
380        return 0;
381
382      break;
383#endif
384
385    case 'm':
386
387      /* -m360 mean to assemble for the ancient 360 architecture.  */
388      if (strcmp (arg, "360") == 0 || strcmp (arg, "i360") == 0)
389	i370_cpu = I370_OPCODE_360;
390      /* -mxa means to assemble for the IBM 370 XA.  */
391      else if (strcmp (arg, "xa") == 0)
392	i370_cpu = I370_OPCODE_370_XA;
393      /* -many means to assemble for any architecture (370/XA).  */
394      else if (strcmp (arg, "any") == 0)
395	i370_cpu = I370_OPCODE_370;
396
397      else if (strcmp (arg, "regnames") == 0)
398	reg_names_p = TRUE;
399
400      else if (strcmp (arg, "no-regnames") == 0)
401	reg_names_p = FALSE;
402
403#ifdef OBJ_ELF
404      /* -mrelocatable/-mrelocatable-lib -- warn about
405	 initializations that require relocation.  */
406      else if (strcmp (arg, "relocatable") == 0)
407        {
408          shlib = SHILB_MRELOCATABLE;
409          i370_flags |= EF_I370_RELOCATABLE;
410        }
411      else if (strcmp (arg, "relocatable-lib") == 0)
412        {
413          shlib = SHILB_MRELOCATABLE;
414          i370_flags |= EF_I370_RELOCATABLE_LIB;
415        }
416#endif
417      else
418        {
419          as_bad (_("invalid switch -m%s"), arg);
420          return 0;
421        }
422      break;
423
424#ifdef OBJ_ELF
425      /* -V: SVR4 argument to print version ID.  */
426    case 'V':
427      print_version_id ();
428      break;
429
430      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
431         should be emitted or not.  FIXME: Not implemented.  */
432    case 'Q':
433      break;
434
435#endif
436
437    default:
438      return 0;
439    }
440
441  return 1;
442}
443
444
445/* Set i370_cpu if it is not already set.
446   Currently defaults to the reasonable superset;
447   but can be made more fine grained if desred.  */
448
449static void
450i370_set_cpu (void)
451{
452  const char *default_os  = TARGET_OS;
453  const char *default_cpu = TARGET_CPU;
454
455  /* Override with the superset for the moment.  */
456  i370_cpu = I370_OPCODE_ESA390_SUPERSET;
457  if (i370_cpu == 0)
458    {
459      if (strcmp (default_cpu, "i360") == 0)
460        i370_cpu = I370_OPCODE_360;
461      else if (strcmp (default_cpu, "i370") == 0)
462        i370_cpu = I370_OPCODE_370;
463      else if (strcmp (default_cpu, "XA") == 0)
464        i370_cpu = I370_OPCODE_370_XA;
465      else
466        as_fatal ("Unknown default cpu = %s, os = %s", default_cpu, default_os);
467    }
468}
469
470/* Figure out the BFD architecture to use.
471   FIXME: specify the different 370 architectures.  */
472
473enum bfd_architecture
474i370_arch (void)
475{
476   return bfd_arch_i370;
477}
478
479/* This function is called when the assembler starts up.  It is called
480   after the options have been parsed and the output file has been
481   opened.  */
482
483void
484md_begin (void)
485{
486  const struct i370_opcode *op;
487  const struct i370_opcode *op_end;
488  const struct i370_macro *macro;
489  const struct i370_macro *macro_end;
490  bfd_boolean dup_insn = FALSE;
491
492  i370_set_cpu ();
493
494#ifdef OBJ_ELF
495  /* Set the ELF flags if desired.  */
496  if (i370_flags)
497    bfd_set_private_flags (stdoutput, i370_flags);
498#endif
499
500  /* Insert the opcodes into a hash table.  */
501  i370_hash = hash_new ();
502
503   op_end = i370_opcodes + i370_num_opcodes;
504   for (op = i370_opcodes; op < op_end; op++)
505     {
506       know ((op->opcode.i[0] & op->mask.i[0]) == op->opcode.i[0]
507	     && (op->opcode.i[1] & op->mask.i[1]) == op->opcode.i[1]);
508
509       if ((op->flags & i370_cpu) != 0)
510         {
511           const char *retval;
512
513           retval = hash_insert (i370_hash, op->name, (void *) op);
514           if (retval != (const char *) NULL)
515             {
516               as_bad (_("Internal assembler error for instruction %s"), op->name);
517               dup_insn = TRUE;
518             }
519         }
520     }
521
522  /* Insert the macros into a hash table.  */
523  i370_macro_hash = hash_new ();
524
525  macro_end = i370_macros + i370_num_macros;
526  for (macro = i370_macros; macro < macro_end; macro++)
527    {
528      if ((macro->flags & i370_cpu) != 0)
529        {
530          const char *retval;
531
532          retval = hash_insert (i370_macro_hash, macro->name, (void *) macro);
533          if (retval != (const char *) NULL)
534            {
535              as_bad (_("Internal assembler error for macro %s"), macro->name);
536              dup_insn = TRUE;
537            }
538        }
539    }
540
541  if (dup_insn)
542    abort ();
543}
544
545/* Insert an operand value into an instruction.  */
546
547static i370_insn_t
548i370_insert_operand (i370_insn_t insn,
549		     const struct i370_operand *operand,
550		     offsetT val)
551{
552  if (operand->insert)
553    {
554      const char *errmsg;
555
556      /* Used for 48-bit insn's.  */
557      errmsg = NULL;
558      insn = (*operand->insert) (insn, (long) val, &errmsg);
559      if (errmsg)
560        as_bad ("%s", errmsg);
561    }
562  else
563    /* This is used only for 16, 32 bit insn's.  */
564    insn.i[0] |= (((long) val & ((1 << operand->bits) - 1))
565		  << operand->shift);
566
567  return insn;
568}
569
570
571#ifdef OBJ_ELF
572/* Parse @got, etc. and return the desired relocation.
573   Currently, i370 does not support (don't really need to support) any
574   of these fancier markups ... for example, no one is going to
575   write 'L 6,=V(bogus)@got' it just doesn't make sense (at least to me).
576   So basically, we could get away with this routine returning
577   BFD_RELOC_UNUSED in all circumstances.  However, I'll leave
578   in for now in case someone ambitious finds a good use for this stuff ...
579   this routine was pretty much just copied from the powerpc code ...  */
580
581static bfd_reloc_code_real_type
582i370_elf_suffix (char **str_p, expressionS *exp_p)
583{
584  struct map_bfd
585  {
586    char *string;
587    int length;
588    bfd_reloc_code_real_type reloc;
589  };
590
591  char ident[20];
592  char *str = *str_p;
593  char *str2;
594  int ch;
595  int len;
596  struct map_bfd *ptr;
597
598#define MAP(str,reloc) { str, sizeof (str) - 1, reloc }
599
600  static struct map_bfd mapping[] =
601  {
602    /* warnings with -mrelocatable.  */
603    MAP ("fixup",	BFD_RELOC_CTOR),
604    { (char *)0, 0,	BFD_RELOC_UNUSED }
605  };
606
607  if (*str++ != '@')
608    return BFD_RELOC_UNUSED;
609
610  for (ch = *str, str2 = ident;
611       (str2 < ident + sizeof (ident) - 1
612        && (ISALNUM (ch) || ch == '@'));
613       ch = *++str)
614    *str2++ = TOLOWER (ch);
615
616  *str2 = '\0';
617  len = str2 - ident;
618
619  ch = ident[0];
620  for (ptr = &mapping[0]; ptr->length > 0; ptr++)
621    if (ch == ptr->string[0]
622        && len == ptr->length
623        && memcmp (ident, ptr->string, ptr->length) == 0)
624      {
625        if (exp_p->X_add_number != 0
626            && (ptr->reloc == BFD_RELOC_16_GOTOFF
627        	|| ptr->reloc == BFD_RELOC_LO16_GOTOFF
628        	|| ptr->reloc == BFD_RELOC_HI16_GOTOFF
629        	|| ptr->reloc == BFD_RELOC_HI16_S_GOTOFF))
630          as_warn (_("identifier+constant@got means identifier@got+constant"));
631
632        /* Now check for identifier@suffix+constant */
633        if (*str == '-' || *str == '+')
634          {
635            char *orig_line = input_line_pointer;
636            expressionS new_exp;
637
638            input_line_pointer = str;
639            expression (&new_exp);
640            if (new_exp.X_op == O_constant)
641              {
642        	exp_p->X_add_number += new_exp.X_add_number;
643        	str = input_line_pointer;
644              }
645
646            if (&input_line_pointer != str_p)
647              input_line_pointer = orig_line;
648          }
649
650        *str_p = str;
651        return ptr->reloc;
652      }
653
654  return BFD_RELOC_UNUSED;
655}
656
657/* Like normal .long/.short/.word, except support @got, etc.
658   Clobbers input_line_pointer, checks end-of-line.  */
659
660static void
661i370_elf_cons (int nbytes)   /* 1=.byte, 2=.word, 4=.long.  */
662{
663  expressionS exp;
664  bfd_reloc_code_real_type reloc;
665
666  if (is_it_end_of_statement ())
667    {
668      demand_empty_rest_of_line ();
669      return;
670    }
671
672  do
673    {
674      expression (&exp);
675
676      if (exp.X_op == O_symbol
677          && *input_line_pointer == '@'
678          && (reloc = i370_elf_suffix (&input_line_pointer, &exp)) != BFD_RELOC_UNUSED)
679        {
680          reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
681          int size = bfd_get_reloc_size (reloc_howto);
682
683          if (size > nbytes)
684            as_bad (_("%s relocations do not fit in %d bytes\n"),
685		    reloc_howto->name, nbytes);
686          else
687            {
688              char *p = frag_more ((int) nbytes);
689              int offset = nbytes - size;
690
691              fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size, &exp, 0, reloc);
692            }
693        }
694      else
695        emit_expr (&exp, (unsigned int) nbytes);
696    }
697  while (*input_line_pointer++ == ',');
698
699  input_line_pointer--;        	/* Put terminator back into stream.  */
700  demand_empty_rest_of_line ();
701}
702
703
704/* ASCII to EBCDIC conversion table.  */
705static unsigned char ascebc[256] =
706{
707 /*00  NL    SH    SX    EX    ET    NQ    AK    BL */
708     0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F,
709 /*08  BS    HT    LF    VT    FF    CR    SO    SI */
710     0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
711 /*10  DL    D1    D2    D3    D4    NK    SN    EB */
712     0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26,
713 /*18  CN    EM    SB    EC    FS    GS    RS    US */
714     0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F,
715 /*20  SP     !     "     #     $     %     &     ' */
716     0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D,
717 /*28   (     )     *     +     ,     -    .      / */
718     0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
719 /*30   0     1     2     3     4     5     6     7 */
720     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
721 /*38   8     9     :     ;     <     =     >     ? */
722     0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
723 /*40   @     A     B     C     D     E     F     G */
724     0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
725 /*48   H     I     J     K     L     M     N     O */
726     0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
727 /*50   P     Q     R     S     T     U     V     W */
728     0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
729 /*58   X     Y     Z     [     \     ]     ^     _ */
730     0xE7, 0xE8, 0xE9, 0xAD, 0xE0, 0xBD, 0x5F, 0x6D,
731 /*60   `     a     b     c     d     e     f     g */
732     0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
733 /*68   h     i     j     k     l     m     n     o */
734     0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
735 /*70   p     q     r     s     t     u     v     w */
736     0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
737 /*78   x     y     z     {     |     }     ~    DL */
738     0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
739     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
740     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
741     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
742     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
743     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
744     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
745     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
746     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
747     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
748     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
749     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
750     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
751     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
752     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
753     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
754     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0xFF
755};
756
757/* EBCDIC to ASCII conversion table.  */
758unsigned char ebcasc[256] =
759{
760 /*00  NU    SH    SX    EX    PF    HT    LC    DL */
761     0x00, 0x01, 0x02, 0x03, 0x00, 0x09, 0x00, 0x7F,
762 /*08              SM    VT    FF    CR    SO    SI */
763     0x00, 0x00, 0x00, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
764 /*10  DE    D1    D2    TM    RS    NL    BS    IL */
765     0x10, 0x11, 0x12, 0x13, 0x14, 0x0A, 0x08, 0x00,
766 /*18  CN    EM    CC    C1    FS    GS    RS    US */
767     0x18, 0x19, 0x00, 0x00, 0x1C, 0x1D, 0x1E, 0x1F,
768 /*20  DS    SS    FS          BP    LF    EB    EC */
769     0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x17, 0x1B,
770 /*28              SM    C2    EQ    AK    BL       */
771     0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07, 0x00,
772 /*30              SY          PN    RS    UC    ET */
773     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
774 /*38                    C3    D4    NK          SU */
775     0x00, 0x00, 0x00, 0x00, 0x14, 0x15, 0x00, 0x1A,
776 /*40  SP                                           */
777     0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
778 /*48                     .     <     (     +     | */
779     0x00, 0x00, 0x00, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
780 /*50   &                                           */
781     0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
782 /*58               !     $     *     )     ;     ^ */
783     0x00, 0x00, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0x5E,
784 /*60   -     /                                     */
785     0x2D, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
786 /*68                     ,     %     _     >     ? */
787     0x00, 0x00, 0x00, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
788 /*70                                               */
789     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
790 /*78         `     :     #     @     '     =     " */
791     0x00, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
792 /*80         a     b     c     d     e     f     g */
793     0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
794 /*88   h     i           {                         */
795     0x68, 0x69, 0x00, 0x7B, 0x00, 0x00, 0x00, 0x00,
796 /*90         j     k     l     m     n     o     p */
797     0x00, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
798 /*98   q     r           }                         */
799     0x71, 0x72, 0x00, 0x7D, 0x00, 0x00, 0x00, 0x00,
800 /*A0         ~     s     t     u     v     w     x */
801     0x00, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
802 /*A8   y     z                       [             */
803     0x79, 0x7A, 0x00, 0x00, 0x00, 0x5B, 0x00, 0x00,
804 /*B0                                               */
805     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
806 /*B8                                 ]             */
807     0x00, 0x00, 0x00, 0x00, 0x00, 0x5D, 0x00, 0x00,
808 /*C0   {     A     B     C     D     E     F     G */
809     0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
810 /*C8   H     I                                     */
811     0x48, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
812 /*D0   }     J     K     L     M     N     O     P */
813     0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
814 /*D8   Q     R                                     */
815     0x51, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
816 /*E0   \           S     T     U     V     W     X */
817     0x5C, 0x00, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
818 /*E8   Y     Z                                     */
819     0x59, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
820 /*F0   0     1     2     3     4     5     6     7 */
821     0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
822 /*F8   8     9                                     */
823     0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF
824};
825
826/* EBCDIC translation tables needed for 3270 support.  */
827
828static void
829i370_ebcdic (int unused ATTRIBUTE_UNUSED)
830{
831  char *p, *end;
832  char delim = 0;
833  size_t nbytes;
834
835  nbytes = strlen (input_line_pointer);
836  end = input_line_pointer + nbytes;
837  while ('\r' == *end) end --;
838  while ('\n' == *end) end --;
839
840  delim = *input_line_pointer;
841  if (('\'' == delim) || ('\"' == delim))
842    {
843      input_line_pointer ++;
844      end = rindex (input_line_pointer, delim);
845    }
846
847  if (end > input_line_pointer)
848    {
849      nbytes = end - input_line_pointer +1;
850      p = frag_more (nbytes);
851      while (end > input_line_pointer)
852	{
853	  *p = ascebc [(unsigned char) (*input_line_pointer)];
854	  ++p; ++input_line_pointer;
855	}
856      *p = '\0';
857    }
858  if (delim == *input_line_pointer) ++input_line_pointer;
859}
860
861
862/* Stub out a couple of routines.  */
863
864static void
865i370_rmode (int unused ATTRIBUTE_UNUSED)
866{
867  as_tsktsk ("rmode ignored");
868}
869
870static void
871i370_dsect (int sect)
872{
873  char *save_line = input_line_pointer;
874  static char section[] = ".data\n";
875
876  /* Just pretend this is .section .data.  */
877  input_line_pointer = section;
878  obj_elf_section (sect);
879
880  input_line_pointer = save_line;
881}
882
883static void
884i370_csect (int unused ATTRIBUTE_UNUSED)
885{
886  as_tsktsk ("csect not supported");
887}
888
889
890/* DC Define Const  is only partially supported.
891   For samplecode on what to do, look at i370_elf_cons() above.
892   This code handles pseudoops of the style
893   DC   D'3.141592653'   # in sysv4, .double 3.14159265
894   DC   F'1'             # in sysv4, .long   1.  */
895
896static void
897i370_dc (int unused ATTRIBUTE_UNUSED)
898{
899  char * p, tmp[50];
900  int nbytes=0;
901  expressionS exp;
902  char type=0;
903  char * clse;
904
905  if (is_it_end_of_statement ())
906    {
907      demand_empty_rest_of_line ();
908      return;
909    }
910
911  /* Figure out the size.  */
912  type = *input_line_pointer++;
913  switch (type)
914    {
915    case 'H':  /* 16-bit */
916      nbytes = 2;
917      break;
918    case 'E':  /* 32-bit */
919    case 'F':  /* 32-bit */
920      nbytes = 4;
921      break;
922    case 'D':  /* 64-bit */
923      nbytes = 8;
924      break;
925    default:
926      as_bad (_("unsupported DC type"));
927      return;
928    }
929
930  /* Get rid of pesky quotes.  */
931  if ('\'' == *input_line_pointer)
932    {
933      ++input_line_pointer;
934      clse = strchr (input_line_pointer, '\'');
935      if (clse)
936	*clse= ' ';
937      else
938	as_bad (_("missing end-quote"));
939    }
940
941  if ('\"' == *input_line_pointer)
942    {
943      ++input_line_pointer;
944      clse = strchr (input_line_pointer, '\"');
945      if (clse)
946	*clse= ' ';
947      else
948	as_bad (_("missing end-quote"));
949    }
950
951  switch (type)
952    {
953    case 'H':  /* 16-bit */
954    case 'F':  /* 32-bit */
955      expression (&exp);
956      emit_expr (&exp, nbytes);
957      break;
958    case 'E':  /* 32-bit */
959      type = 'f';
960    case 'D':  /* 64-bit */
961      md_atof (type, tmp, &nbytes);
962      p = frag_more (nbytes);
963      memcpy (p, tmp, nbytes);
964      break;
965    default:
966      as_bad (_("unsupported DC type"));
967      return;
968    }
969
970  demand_empty_rest_of_line ();
971}
972
973
974/* Provide minimal support for DS Define Storage.  */
975
976static void
977i370_ds (int unused ATTRIBUTE_UNUSED)
978{
979  /* DS 0H or DS 0F or DS 0D.  */
980  if ('0' == *input_line_pointer)
981    {
982      int alignment = 0;  /* Left shift 1 << align.  */
983      input_line_pointer ++;
984      switch (*input_line_pointer++)
985	{
986	case 'H':  /* 16-bit */
987	  alignment = 1;
988	  break;
989	case 'F':  /* 32-bit */
990	  alignment = 2;
991	  break;
992	case 'D':  /* 64-bit */
993	  alignment = 3;
994	  break;
995	default:
996	  as_bad (_("unsupported alignment"));
997	  return;
998	}
999      frag_align (alignment, 0, 0);
1000      record_alignment (now_seg, alignment);
1001    }
1002  else
1003    as_bad (_("this DS form not yet supported"));
1004}
1005
1006/* Solaris pseudo op to change to the .rodata section.  */
1007
1008static void
1009i370_elf_rdata (int sect)
1010{
1011  char *save_line = input_line_pointer;
1012  static char section[] = ".rodata\n";
1013
1014  /* Just pretend this is .section .rodata.  */
1015  input_line_pointer = section;
1016  obj_elf_section (sect);
1017
1018  input_line_pointer = save_line;
1019}
1020
1021/* Pseudo op to make file scope bss items.  */
1022
1023static void
1024i370_elf_lcomm (int unused ATTRIBUTE_UNUSED)
1025{
1026  char *name;
1027  char c;
1028  char *p;
1029  offsetT size;
1030  symbolS *symbolP;
1031  offsetT align;
1032  segT old_sec;
1033  int old_subsec;
1034  char *pfrag;
1035  int align2;
1036
1037  name = input_line_pointer;
1038  c = get_symbol_end ();
1039
1040  /* Just after name is now '\0'.  */
1041  p = input_line_pointer;
1042  *p = c;
1043  SKIP_WHITESPACE ();
1044  if (*input_line_pointer != ',')
1045    {
1046      as_bad (_("Expected comma after symbol-name: rest of line ignored."));
1047      ignore_rest_of_line ();
1048      return;
1049    }
1050
1051  /* Skip ','.  */
1052  input_line_pointer++;
1053  if ((size = get_absolute_expression ()) < 0)
1054    {
1055      as_warn (_(".COMMon length (%ld.) <0! Ignored."), (long) size);
1056      ignore_rest_of_line ();
1057      return;
1058    }
1059
1060  /* The third argument to .lcomm is the alignment.  */
1061  if (*input_line_pointer != ',')
1062    align = 8;
1063  else
1064    {
1065      ++input_line_pointer;
1066      align = get_absolute_expression ();
1067      if (align <= 0)
1068        {
1069          as_warn (_("ignoring bad alignment"));
1070          align = 8;
1071        }
1072    }
1073
1074  *p = 0;
1075  symbolP = symbol_find_or_make (name);
1076  *p = c;
1077
1078  if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
1079    {
1080      as_bad (_("Ignoring attempt to re-define symbol `%s'."),
1081              S_GET_NAME (symbolP));
1082      ignore_rest_of_line ();
1083      return;
1084    }
1085
1086  if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
1087    {
1088      as_bad (_("Length of .lcomm \"%s\" is already %ld. Not changed to %ld."),
1089              S_GET_NAME (symbolP),
1090              (long) S_GET_VALUE (symbolP),
1091              (long) size);
1092
1093      ignore_rest_of_line ();
1094      return;
1095    }
1096
1097  /* Allocate_bss:  */
1098  old_sec = now_seg;
1099  old_subsec = now_subseg;
1100  if (align)
1101    {
1102      /* Convert to a power of 2 alignment.  */
1103      for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2)
1104	;
1105      if (align != 1)
1106        {
1107          as_bad (_("Common alignment not a power of 2"));
1108          ignore_rest_of_line ();
1109          return;
1110        }
1111    }
1112  else
1113    align2 = 0;
1114
1115  record_alignment (bss_section, align2);
1116  subseg_set (bss_section, 0);
1117  if (align2)
1118    frag_align (align2, 0, 0);
1119  if (S_GET_SEGMENT (symbolP) == bss_section)
1120    symbol_get_frag (symbolP)->fr_symbol = 0;
1121  symbol_set_frag (symbolP, frag_now);
1122  pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
1123        	    (char *) 0);
1124  *pfrag = 0;
1125  S_SET_SIZE (symbolP, size);
1126  S_SET_SEGMENT (symbolP, bss_section);
1127  subseg_set (old_sec, old_subsec);
1128  demand_empty_rest_of_line ();
1129}
1130
1131/* Validate any relocations emitted for -mrelocatable, possibly adding
1132   fixups for word relocations in writable segments, so we can adjust
1133   them at runtime.  */
1134
1135static void
1136i370_elf_validate_fix (fixS *fixp, segT seg)
1137{
1138  if (fixp->fx_done || fixp->fx_pcrel)
1139    return;
1140
1141  switch (shlib)
1142    {
1143    case SHLIB_NONE:
1144    case SHLIB_PIC:
1145      return;
1146
1147    case SHILB_MRELOCATABLE:
1148      if (fixp->fx_r_type <= BFD_RELOC_UNUSED
1149          && fixp->fx_r_type != BFD_RELOC_16_GOTOFF
1150          && fixp->fx_r_type != BFD_RELOC_HI16_GOTOFF
1151          && fixp->fx_r_type != BFD_RELOC_LO16_GOTOFF
1152          && fixp->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
1153          && fixp->fx_r_type != BFD_RELOC_32_BASEREL
1154          && fixp->fx_r_type != BFD_RELOC_LO16_BASEREL
1155          && fixp->fx_r_type != BFD_RELOC_HI16_BASEREL
1156          && fixp->fx_r_type != BFD_RELOC_HI16_S_BASEREL
1157          && strcmp (segment_name (seg), ".got2") != 0
1158          && strcmp (segment_name (seg), ".dtors") != 0
1159          && strcmp (segment_name (seg), ".ctors") != 0
1160          && strcmp (segment_name (seg), ".fixup") != 0
1161          && strcmp (segment_name (seg), ".stab") != 0
1162          && strcmp (segment_name (seg), ".gcc_except_table") != 0
1163          && strcmp (segment_name (seg), ".ex_shared") != 0)
1164        {
1165          if ((seg->flags & (SEC_READONLY | SEC_CODE)) != 0
1166              || fixp->fx_r_type != BFD_RELOC_CTOR)
1167	    as_bad_where (fixp->fx_file, fixp->fx_line,
1168			  "Relocation cannot be done when using -mrelocatable");
1169        }
1170      return;
1171    default:
1172      break;
1173    }
1174}
1175#endif /* OBJ_ELF */
1176
1177
1178#define LITERAL_POOL_SUPPORT
1179#ifdef LITERAL_POOL_SUPPORT
1180/* Provide support for literal pools within the text section.
1181   Loosely based on similar code from tc-arm.c.
1182   We will use four symbols to locate four parts of the literal pool.
1183   These four sections contain 64,32,16 and 8-bit constants; we use
1184   four sections so that all memory access can be appropriately aligned.
1185   That is, we want to avoid mixing these together so that we don't
1186   waste space padding out to alignments.  The four pointers
1187   longlong_poolP, word_poolP, etc. point to a symbol labeling the
1188   start of each pool part.
1189
1190   lit_pool_num increments from zero to infinity and uniquely id's
1191     -- its used to generate the *_poolP symbol name.  */
1192
1193#define MAX_LITERAL_POOL_SIZE 1024
1194
1195typedef struct literalS
1196{
1197  struct expressionS  exp;
1198  char * sym_name;
1199  char size;  /* 1,2,4 or 8 */
1200  short offset;
1201} literalT;
1202
1203literalT literals[MAX_LITERAL_POOL_SIZE];
1204int next_literal_pool_place = 0; /* Next free entry in the pool.  */
1205
1206static symbolS *longlong_poolP = NULL;   /* 64-bit pool entries.  */
1207static symbolS *word_poolP = NULL;       /* 32-bit pool entries.  */
1208static symbolS *short_poolP = NULL;      /* 16-bit pool entries.  */
1209static symbolS *byte_poolP = NULL;       /* 8-bit  pool entries.  */
1210
1211static int lit_pool_num = 1;
1212
1213/* Create a new, empty symbol.  */
1214static symbolS *
1215symbol_make_empty (void)
1216{
1217  return symbol_create (FAKE_LABEL_NAME, undefined_section,
1218  			(valueT) 0, &zero_address_frag);
1219}
1220
1221/* Make the first argument an address-relative expression
1222   by subtracting the second argument.  */
1223
1224static void
1225i370_make_relative (expressionS *exx, expressionS *baseaddr)
1226{
1227  if (O_constant == baseaddr->X_op)
1228    {
1229       exx->X_op = O_symbol;
1230       exx->X_add_number -= baseaddr->X_add_number;
1231    }
1232  else if (O_symbol == baseaddr->X_op)
1233    {
1234       exx->X_op = O_subtract;
1235       exx->X_op_symbol = baseaddr->X_add_symbol;
1236       exx->X_add_number -= baseaddr->X_add_number;
1237    }
1238  else if (O_uminus == baseaddr->X_op)
1239    {
1240       exx->X_op = O_add;
1241       exx->X_op_symbol = baseaddr->X_add_symbol;
1242       exx->X_add_number += baseaddr->X_add_number;
1243    }
1244  else
1245    as_bad (_("Missing or bad .using directive"));
1246}
1247/* Add an expression to the literal pool.  */
1248
1249static  void
1250add_to_lit_pool (expressionS *exx, char *name, int sz)
1251{
1252  int lit_count = 0;
1253  int offset_in_pool = 0;
1254
1255  /* Start a new pool, if necessary.  */
1256  if (8 == sz && NULL == longlong_poolP)
1257    longlong_poolP = symbol_make_empty ();
1258  else if (4 == sz && NULL == word_poolP)
1259    word_poolP = symbol_make_empty ();
1260  else if (2 == sz && NULL == short_poolP)
1261    short_poolP = symbol_make_empty ();
1262  else if (1 == sz && NULL == byte_poolP)
1263    byte_poolP = symbol_make_empty ();
1264
1265  /* Check if this literal value is already in the pool.
1266     FIXME: We should probably be checking expressions
1267            of type O_symbol as well.
1268     FIXME: This is probably(certainly?) broken for O_big,
1269            which includes 64-bit long-longs.  */
1270  while (lit_count < next_literal_pool_place)
1271    {
1272      if (exx->X_op == O_constant
1273          && literals[lit_count].exp.X_op == exx->X_op
1274          && literals[lit_count].exp.X_add_number == exx->X_add_number
1275          && literals[lit_count].exp.X_unsigned == exx->X_unsigned
1276          && literals[lit_count].size == sz)
1277        break;
1278      else if (literals[lit_count].sym_name
1279	       && name
1280	       && !strcmp (name, literals[lit_count].sym_name))
1281        break;
1282      if (sz == literals[lit_count].size)
1283	offset_in_pool += sz;
1284      lit_count ++;
1285    }
1286
1287  if (lit_count == next_literal_pool_place) /* new entry */
1288    {
1289      if (next_literal_pool_place > MAX_LITERAL_POOL_SIZE)
1290	as_bad (_("Literal Pool Overflow"));
1291
1292      literals[next_literal_pool_place].exp = *exx;
1293      literals[next_literal_pool_place].size = sz;
1294      literals[next_literal_pool_place].offset = offset_in_pool;
1295      if (name)
1296	literals[next_literal_pool_place].sym_name = strdup (name);
1297      else
1298	literals[next_literal_pool_place].sym_name = NULL;
1299      next_literal_pool_place++;
1300    }
1301
1302  /* ???_poolP points to the beginning of the literal pool.
1303     X_add_number is the offset from the beginning of the
1304     literal pool to this expr minus the location of the most
1305     recent .using directive.  Thus, the grand total value of the
1306     expression is the distance from .using to the literal.  */
1307  if (8 == sz)
1308    exx->X_add_symbol = longlong_poolP;
1309  else if (4 == sz)
1310    exx->X_add_symbol = word_poolP;
1311  else if (2 == sz)
1312    exx->X_add_symbol = short_poolP;
1313  else if (1 == sz)
1314    exx->X_add_symbol = byte_poolP;
1315  exx->X_add_number = offset_in_pool;
1316  exx->X_op_symbol = NULL;
1317
1318  /* If the user has set up a base reg in another section,
1319     use that; otherwise use the text section.  */
1320  if (0 < i370_using_other_regno)
1321    i370_make_relative (exx, &i370_using_other_baseaddr);
1322  else
1323    i370_make_relative (exx, &i370_using_text_baseaddr);
1324}
1325
1326/* The symbol setup for the literal pool is done in two steps.  First,
1327   a symbol that represents the start of the literal pool is created,
1328   above, in the add_to_pool() routine. This sym ???_poolP.
1329   However, we don't know what fragment its in until a bit later.
1330   So we defer the frag_now thing, and the symbol name, until .ltorg time.  */
1331
1332/* Can't use symbol_new here, so have to create a symbol and then at
1333   a later date assign it a value. Thats what these functions do.  */
1334
1335static void
1336symbol_locate (symbolS *symbolP,
1337	       const char *name,	/* It is copied, the caller can modify.  */
1338	       segT segment,		/* Segment identifier (SEG_<something>).  */
1339	       valueT valu,		/* Symbol value.  */
1340	       fragS *frag)		/* Associated fragment.  */
1341{
1342  size_t name_length;
1343  char *preserved_copy_of_name;
1344
1345  name_length = strlen (name) + 1;      /* +1 for \0 */
1346  obstack_grow (&notes, name, name_length);
1347  preserved_copy_of_name = obstack_finish (&notes);
1348
1349  S_SET_NAME (symbolP, preserved_copy_of_name);
1350
1351  S_SET_SEGMENT (symbolP, segment);
1352  S_SET_VALUE (symbolP, valu);
1353  symbol_clear_list_pointers (symbolP);
1354
1355  symbol_set_frag (symbolP, frag);
1356
1357  /* Link to end of symbol chain.  */
1358  {
1359    extern int symbol_table_frozen;
1360
1361    if (symbol_table_frozen)
1362      abort ();
1363  }
1364
1365  symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
1366
1367  obj_symbol_new_hook (symbolP);
1368
1369#ifdef tc_symbol_new_hook
1370  tc_symbol_new_hook (symbolP);
1371#endif
1372
1373#define DEBUG_SYMS
1374#ifdef DEBUG_SYMS
1375  verify_symbol_chain(symbol_rootP, symbol_lastP);
1376#endif /* DEBUG_SYMS */
1377}
1378
1379/* i370_addr_offset() will convert operand expressions
1380   that appear to be absolute into thier base-register
1381   relative form.  These expressions come in two types:
1382
1383   (1) of the form "* + const" * where "*" means
1384   relative offset since the last using
1385   i.e. "*" means ".-using_baseaddr"
1386
1387   (2) labels, which are never absolute, but are always
1388   relative to the last "using".  Anything with an alpha
1389   character is considered to be a label (since symbols
1390   can never be operands), and since we've already handled
1391   register operands. For example, "BL .L33" branch low
1392   to .L33 RX form insn frequently terminates for-loops.  */
1393
1394static bfd_boolean
1395i370_addr_offset (expressionS *exx)
1396{
1397  char *dot, *lab;
1398  int islabel = 0;
1399  int all_digits = 0;
1400
1401  /* Search for a label; anything with an alpha char will do.
1402     Local labels consist of N digits followed by either b or f.  */
1403  lab = input_line_pointer;
1404  while (*lab && (',' != *lab) && ('(' != *lab))
1405    {
1406      if (ISDIGIT (*lab))
1407	all_digits = 1;
1408      else if (ISALPHA (*lab))
1409	{
1410	  if (!all_digits)
1411	    {
1412	      islabel = 1;
1413	      break;
1414	    }
1415	  else if (('f' == *lab) || ('b' == *lab))
1416	    {
1417	      islabel = 1;
1418	      break;
1419	    }
1420	  if (all_digits)
1421	    break;
1422	}
1423      else if ('.' != *lab)
1424        break;
1425      ++lab;
1426    }
1427
1428  /* See if operand has a * in it.  */
1429  dot = strchr (input_line_pointer, '*');
1430
1431  if (!dot && !islabel)
1432    return FALSE;
1433
1434  /* Replace * with . and let expr munch on it.  */
1435  if (dot)
1436    *dot = '.';
1437  expression (exx);
1438
1439  /* OK, now we have to subtract the "using" location.
1440     Normally branches appear in the text section only.  */
1441  if (0 == strncmp (now_seg->name, ".text", 5) || 0 > i370_using_other_regno)
1442    i370_make_relative (exx, &i370_using_text_baseaddr);
1443  else
1444    i370_make_relative (exx, &i370_using_other_baseaddr);
1445
1446  /* Put the * back.  */
1447  if (dot)
1448    *dot = '*';
1449
1450  return TRUE;
1451}
1452
1453/* Handle address constants of various sorts.  */
1454/* The currently supported types are
1455      =A(some_symb)
1456      =V(some_extern)
1457      =X'deadbeef'    hexadecimal
1458      =F'1234'        32-bit const int
1459      =H'1234'        16-bit const int.  */
1460
1461static bfd_boolean
1462i370_addr_cons (expressionS *exp)
1463{
1464  char *name;
1465  char *sym_name, delim;
1466  int name_len;
1467  int hex_len = 0;
1468  int cons_len = 0;
1469
1470  name = input_line_pointer;
1471  sym_name = input_line_pointer;
1472  /* Find the spelling of the operand.  */
1473  if (name[0] == '=' && ISALPHA (name[1]))
1474    name = ++input_line_pointer;
1475  else
1476    return FALSE;
1477
1478  switch (name[0])
1479    {
1480    case 'A': /* A == address-of.  */
1481    case 'V': /* V == extern.  */
1482      ++input_line_pointer;
1483      expression (exp);
1484
1485      /* We use a simple string name to collapse together
1486         multiple refrences to the same address literal.  */
1487      name_len = strcspn (sym_name, ", ");
1488      delim = *(sym_name + name_len);
1489      *(sym_name + name_len) = 0x0;
1490      add_to_lit_pool (exp, sym_name, 4);
1491      *(sym_name + name_len) = delim;
1492
1493      break;
1494    case 'H':
1495    case 'F':
1496    case 'X':
1497    case 'E':  /* Single-precision float point.  */
1498    case 'D':  /* Double-precision float point.  */
1499
1500      /* H == 16-bit fixed-point const; expression must be const.  */
1501      /* F == fixed-point const; expression must be const.  */
1502      /* X == fixed-point const; expression must be const.  */
1503      if ('H' == name[0]) cons_len = 2;
1504      else if ('F' == name[0]) cons_len = 4;
1505      else if ('X' == name[0]) cons_len = -1;
1506      else if ('E' == name[0]) cons_len = 4;
1507      else if ('D' == name[0]) cons_len = 8;
1508
1509      /* Extract length, if it is present;
1510	 FIXME: assume single-digit length.  */
1511      if ('L' == name[1])
1512	{
1513	  /* Should work for ASCII and EBCDIC.  */
1514	  cons_len = name[2] - '0';
1515	  input_line_pointer += 2;
1516	}
1517
1518      ++input_line_pointer;
1519
1520      /* Get rid of pesky quotes.  */
1521      if ('\'' == *input_line_pointer)
1522	{
1523	  char * clse;
1524
1525	  ++input_line_pointer;
1526	  clse = strchr (input_line_pointer, '\'');
1527	  if (clse)
1528	    *clse= ' ';
1529	  else
1530	    as_bad (_("missing end-quote"));
1531	}
1532      if ('\"' == *input_line_pointer)
1533	{
1534	  char * clse;
1535
1536	  ++input_line_pointer;
1537	  clse = strchr (input_line_pointer, '\"');
1538	  if (clse)
1539	    *clse= ' ';
1540	  else
1541	    as_bad (_("missing end-quote"));
1542	}
1543      if (('X' == name[0]) || ('E' == name[0]) || ('D' == name[0]))
1544	{
1545	  char tmp[50];
1546	  char *save;
1547
1548	  /* The length of hex constants is specified directly with L,
1549	     or implied through the number of hex digits. For example:
1550	     =X'AB'       one byte
1551	     =X'abcd'     two bytes
1552	     =X'000000AB' four bytes
1553	     =XL4'AB'     four bytes, left-padded withn zero.  */
1554	  if (('X' == name[0]) && (0 > cons_len))
1555	    {
1556	      save = input_line_pointer;
1557	      while (*save)
1558		{
1559		  if (ISXDIGIT (*save))
1560		    hex_len++;
1561		  save++;
1562		}
1563	      cons_len = (hex_len+1) /2;
1564	    }
1565	  /* I believe this works even for =XL8'dada0000beeebaaa'
1566	     which should parse out to X_op == O_big
1567	     Note that floats and doubles get represented as
1568	     0d3.14159265358979  or 0f 2.7.  */
1569	  tmp[0] = '0';
1570	  tmp[1] = name[0];
1571	  tmp[2] = 0;
1572	  strcat (tmp, input_line_pointer);
1573	  save = input_line_pointer;
1574	  input_line_pointer = tmp;
1575	  expression (exp);
1576	  input_line_pointer = save + (input_line_pointer-tmp-2);
1577
1578	  /* Fix up lengths for floats and doubles.  */
1579	  if (O_big == exp->X_op)
1580	    exp->X_add_number = cons_len / CHARS_PER_LITTLENUM;
1581	}
1582      else
1583	expression (exp);
1584
1585      /* O_big occurs when more than 4 bytes worth gets parsed.  */
1586      if ((exp->X_op != O_constant) && (exp->X_op != O_big))
1587	{
1588	  as_bad (_("expression not a constant"));
1589	  return FALSE;
1590	}
1591      add_to_lit_pool (exp, 0x0, cons_len);
1592      break;
1593
1594    default:
1595      as_bad (_("Unknown/unsupported address literal type"));
1596      return FALSE;
1597    }
1598
1599  return TRUE;
1600}
1601
1602
1603/* Dump the contents of the literal pool that we've accumulated so far.
1604   This aligns the pool to the size of the largest literal in the pool.  */
1605
1606static void
1607i370_ltorg (int ignore ATTRIBUTE_UNUSED)
1608{
1609  int litsize;
1610  int lit_count = 0;
1611  int biggest_literal_size = 0;
1612  int biggest_align = 0;
1613  char pool_name[20];
1614
1615  if (strncmp (now_seg->name, ".text", 5))
1616    {
1617      if (i370_other_section == undefined_section)
1618	as_bad (_(".ltorg without prior .using in section %s"),
1619		now_seg->name);
1620
1621      if (i370_other_section != now_seg)
1622	as_bad (_(".ltorg in section %s paired to .using in section %s"),
1623		now_seg->name, i370_other_section->name);
1624    }
1625
1626  if (! longlong_poolP
1627      && ! word_poolP
1628      && ! short_poolP
1629      && ! byte_poolP)
1630    /* Nothing to do.  */
1631    return;
1632
1633  /* Find largest literal .. 2 4 or 8.  */
1634  lit_count = 0;
1635  while (lit_count < next_literal_pool_place)
1636    {
1637      if (biggest_literal_size < literals[lit_count].size)
1638	biggest_literal_size = literals[lit_count].size;
1639      lit_count ++;
1640    }
1641  if (1 == biggest_literal_size) biggest_align = 0;
1642  else if (2 == biggest_literal_size) biggest_align = 1;
1643  else if (4 == biggest_literal_size) biggest_align = 2;
1644  else if (8 == biggest_literal_size) biggest_align = 3;
1645  else as_bad (_("bad alignment of %d bytes in literal pool"), biggest_literal_size);
1646  if (0 == biggest_align) biggest_align = 1;
1647
1648  /* Align pool for short, word, double word accesses.  */
1649  frag_align (biggest_align, 0, 0);
1650  record_alignment (now_seg, biggest_align);
1651
1652  /* Note that the gas listing will print only the first five
1653     entries in the pool .... wonder how to make it print more.  */
1654  /* Output largest literals first, then the smaller ones.  */
1655  for (litsize=8; litsize; litsize /=2)
1656    {
1657      symbolS *current_poolP = NULL;
1658      switch (litsize)
1659	{
1660	case 8:
1661	  current_poolP = longlong_poolP; break;
1662	case 4:
1663	  current_poolP = word_poolP; break;
1664	case 2:
1665	  current_poolP = short_poolP; break;
1666	case 1:
1667	  current_poolP = byte_poolP; break;
1668	default:
1669	  as_bad (_("bad literal size\n"));
1670	}
1671      if (NULL == current_poolP)
1672	continue;
1673      sprintf (pool_name, ".LITP%01d%06d", litsize, lit_pool_num);
1674      symbol_locate (current_poolP, pool_name, now_seg,
1675		     (valueT) frag_now_fix (), frag_now);
1676      symbol_table_insert (current_poolP);
1677
1678      lit_count = 0;
1679      while (lit_count < next_literal_pool_place)
1680	{
1681	  if (litsize == literals[lit_count].size)
1682	    {
1683#define EMIT_ADDR_CONS_SYMBOLS
1684#ifdef EMIT_ADDR_CONS_SYMBOLS
1685	      /* Create a bogus symbol, add it to the pool ...
1686	         For the most part, I think this is a useless exercise,
1687	         except that having these symbol names in the objects
1688	         is vaguely useful for debugging.  */
1689	      if (literals[lit_count].sym_name)
1690		{
1691		  symbolS * symP = symbol_make_empty ();
1692		  symbol_locate (symP, literals[lit_count].sym_name, now_seg,
1693				 (valueT) frag_now_fix (), frag_now);
1694		  symbol_table_insert (symP);
1695		}
1696#endif /* EMIT_ADDR_CONS_SYMBOLS */
1697
1698	      emit_expr (&(literals[lit_count].exp), literals[lit_count].size);
1699	    }
1700	  lit_count ++;
1701	}
1702    }
1703
1704  next_literal_pool_place = 0;
1705  longlong_poolP = NULL;
1706  word_poolP = NULL;
1707  short_poolP = NULL;
1708  byte_poolP = NULL;
1709  lit_pool_num++;
1710}
1711
1712#endif /* LITERAL_POOL_SUPPORT */
1713
1714
1715/* Add support for the HLASM-like USING directive to indicate
1716   the base register to use ...  we don't support the full
1717   hlasm semantics for this ... we merely pluck a base address
1718   and a register number out.  We print a warning if using is
1719   called multiple times.  I suppose we should check to see
1720   if the regno is valid.  */
1721
1722static void
1723i370_using (int ignore ATTRIBUTE_UNUSED)
1724{
1725  expressionS ex, baseaddr;
1726  int iregno;
1727  char *star;
1728
1729  /* If "*" appears in a using, it means "."
1730     replace it with "." so that expr doesn't get confused.  */
1731  star = strchr (input_line_pointer, '*');
1732  if (star)
1733    *star = '.';
1734
1735  /* The first arg to using will usually be ".", but it can
1736     be a more complex expression too.  */
1737  expression (&baseaddr);
1738  if (star)
1739    *star = '*';
1740  if (O_constant != baseaddr.X_op
1741      && O_symbol != baseaddr.X_op
1742      && O_uminus != baseaddr.X_op)
1743    as_bad (_(".using: base address expression illegal or too complex"));
1744
1745  if (*input_line_pointer != '\0') ++input_line_pointer;
1746
1747  /* The second arg to using had better be a register.  */
1748  register_name (&ex);
1749  demand_empty_rest_of_line ();
1750  iregno = ex.X_add_number;
1751
1752  if (0 == strncmp (now_seg->name, ".text", 5))
1753    {
1754      i370_using_text_baseaddr = baseaddr;
1755      i370_using_text_regno = iregno;
1756    }
1757  else
1758    {
1759      i370_using_other_baseaddr = baseaddr;
1760      i370_using_other_regno = iregno;
1761      i370_other_section = now_seg;
1762    }
1763}
1764
1765static void
1766i370_drop (int ignore ATTRIBUTE_UNUSED)
1767{
1768  expressionS ex;
1769  int iregno;
1770
1771  register_name (&ex);
1772  demand_empty_rest_of_line ();
1773  iregno = ex.X_add_number;
1774
1775  if (0 == strncmp (now_seg->name, ".text", 5))
1776    {
1777      if (iregno != i370_using_text_regno)
1778	as_bad (_("droping register %d in section %s does not match using register %d"),
1779		iregno, now_seg->name, i370_using_text_regno);
1780
1781      i370_using_text_regno = -1;
1782      i370_using_text_baseaddr.X_op = O_absent;
1783    }
1784  else
1785    {
1786      if (iregno != i370_using_other_regno)
1787	as_bad (_("droping register %d in section %s does not match using register %d"),
1788		iregno, now_seg->name, i370_using_other_regno);
1789
1790      if (i370_other_section != now_seg)
1791	as_bad (_("droping register %d in section %s previously used in section %s"),
1792		iregno, now_seg->name, i370_other_section->name);
1793
1794      i370_using_other_regno = -1;
1795      i370_using_other_baseaddr.X_op = O_absent;
1796      i370_other_section = undefined_section;
1797    }
1798}
1799
1800
1801/* We need to keep a list of fixups.  We can't simply generate them as
1802   we go, because that would require us to first create the frag, and
1803   that would screw up references to ``.''.  */
1804
1805struct i370_fixup
1806{
1807  expressionS exp;
1808  int opindex;
1809  bfd_reloc_code_real_type reloc;
1810};
1811
1812#define MAX_INSN_FIXUPS 5
1813
1814/* Handle a macro.  Gather all the operands, transform them as
1815   described by the macro, and call md_assemble recursively.  All the
1816   operands are separated by commas; we don't accept parentheses
1817   around operands here.  */
1818
1819static void
1820i370_macro (char *str, const struct i370_macro *macro)
1821{
1822  char *operands[10];
1823  unsigned int count;
1824  char *s;
1825  unsigned int len;
1826  const char *format;
1827  int arg;
1828  char *send;
1829  char *complete;
1830
1831  /* Gather the users operands into the operands array.  */
1832  count = 0;
1833  s = str;
1834  while (1)
1835    {
1836      if (count >= sizeof operands / sizeof operands[0])
1837        break;
1838      operands[count++] = s;
1839      s = strchr (s, ',');
1840      if (s == (char *) NULL)
1841        break;
1842      *s++ = '\0';
1843    }
1844
1845  if (count != macro->operands)
1846    {
1847      as_bad (_("wrong number of operands"));
1848      return;
1849    }
1850
1851  /* Work out how large the string must be (the size is unbounded
1852     because it includes user input).  */
1853  len = 0;
1854  format = macro->format;
1855  while (*format != '\0')
1856    {
1857      if (*format != '%')
1858        {
1859          ++len;
1860          ++format;
1861        }
1862      else
1863        {
1864          arg = strtol (format + 1, &send, 10);
1865          know (send != format && arg >= 0 && (unsigned) arg < count);
1866          len += strlen (operands[arg]);
1867          format = send;
1868        }
1869    }
1870
1871  /* Put the string together.  */
1872  complete = s = alloca (len + 1);
1873  format = macro->format;
1874  while (*format != '\0')
1875    {
1876      if (*format != '%')
1877        *s++ = *format++;
1878      else
1879        {
1880          arg = strtol (format + 1, &send, 10);
1881          strcpy (s, operands[arg]);
1882          s += strlen (s);
1883          format = send;
1884        }
1885    }
1886  *s = '\0';
1887
1888  /* Assemble the constructed instruction.  */
1889  md_assemble (complete);
1890}
1891
1892/* This routine is called for each instruction to be assembled.  */
1893
1894void
1895md_assemble (char *str)
1896{
1897  char *s;
1898  const struct i370_opcode *opcode;
1899  i370_insn_t insn;
1900  const unsigned char *opindex_ptr;
1901  int have_optional_index, have_optional_basereg, have_optional_reg;
1902  int skip_optional_index, skip_optional_basereg, skip_optional_reg;
1903  int use_text=0, use_other=0;
1904  int off_by_one;
1905  struct i370_fixup fixups[MAX_INSN_FIXUPS];
1906  int fc;
1907  char *f;
1908  int i;
1909#ifdef OBJ_ELF
1910  bfd_reloc_code_real_type reloc;
1911#endif
1912
1913  /* Get the opcode.  */
1914  for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
1915    ;
1916  if (*s != '\0')
1917    *s++ = '\0';
1918
1919  /* Look up the opcode in the hash table.  */
1920  opcode = (const struct i370_opcode *) hash_find (i370_hash, str);
1921  if (opcode == (const struct i370_opcode *) NULL)
1922    {
1923      const struct i370_macro *macro;
1924
1925      gas_assert (i370_macro_hash);
1926      macro = (const struct i370_macro *) hash_find (i370_macro_hash, str);
1927      if (macro == (const struct i370_macro *) NULL)
1928        as_bad (_("Unrecognized opcode: `%s'"), str);
1929      else
1930	i370_macro (s, macro);
1931
1932      return;
1933    }
1934
1935  insn = opcode->opcode;
1936
1937  str = s;
1938  while (ISSPACE (*str))
1939    ++str;
1940
1941  /* I370 operands are either expressions or address constants.
1942     Many operand types are optional.  The optional operands
1943     are always surrounded by parens, and are used to denote the base
1944     register ... e.g. "A R1, D2" or "A R1, D2(,B2) as opposed to
1945     the fully-formed "A R1, D2(X2,B2)".  Note also the = sign,
1946     such as A R1,=A(i) where the address-of operator =A implies
1947     use of both a base register, and a missing index register.
1948
1949     So, before we start seriously parsing the operands, we check
1950     to see if we have an optional operand, and, if we do, we count
1951     the number of commas to see which operand should be omitted.  */
1952
1953  have_optional_index = have_optional_basereg = have_optional_reg = 0;
1954  for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1955    {
1956      const struct i370_operand *operand;
1957
1958      operand = &i370_operands[*opindex_ptr];
1959      if ((operand->flags & I370_OPERAND_INDEX) != 0)
1960	have_optional_index = 1;
1961      if ((operand->flags & I370_OPERAND_BASE) != 0)
1962	have_optional_basereg = 1;
1963      if ((operand->flags & I370_OPERAND_OPTIONAL) != 0)
1964	have_optional_reg = 1;
1965    }
1966
1967  skip_optional_index = skip_optional_basereg = skip_optional_reg = 0;
1968  if (have_optional_index || have_optional_basereg)
1969    {
1970      unsigned int opcount, nwanted;
1971
1972      /* There is an optional operand.  Count the number of
1973	 commas and open-parens in the input line.  */
1974      if (*str == '\0')
1975	opcount = 0;
1976      else
1977	{
1978	  opcount = 1;
1979	  s = str;
1980	  while ((s = strpbrk (s, ",(=")) != (char *) NULL)
1981	    {
1982	      ++opcount;
1983	      ++s;
1984	      if (',' == *s) ++s;  /* avoid counting things like (, */
1985	      if ('=' == *s) { ++s; --opcount; }
1986	    }
1987	}
1988
1989      /* If there are fewer operands in the line then are called
1990	 for by the instruction, we want to skip the optional
1991	 operand.  */
1992      nwanted = strlen ((char *) opcode->operands);
1993      if (have_optional_index)
1994	{
1995	  if (opcount < nwanted)
1996	    skip_optional_index = 1;
1997	  if (have_optional_basereg && ((opcount+1) < nwanted))
1998	    skip_optional_basereg = 1;
1999	  if (have_optional_reg && ((opcount+1) < nwanted))
2000	    skip_optional_reg = 1;
2001	}
2002      else
2003	{
2004	  if (have_optional_basereg && (opcount < nwanted))
2005	    skip_optional_basereg = 1;
2006	  if (have_optional_reg && (opcount < nwanted))
2007	    skip_optional_reg = 1;
2008	}
2009    }
2010
2011  /* Perform some off-by-one hacks on the length field of certain instructions.
2012     Its such a shame to have to do this, but the problem is that HLASM got
2013     defined so that the lengths differ by one from the actual machine instructions.
2014     this code should probably be moved to a special inster-operand routine.
2015     Sigh. Affected instructions are Compare Logical, Move and Exclusive OR
2016     hack alert -- aren't *all* SS instructions affected ??  */
2017  off_by_one = 0;
2018  if (0 == strcasecmp ("CLC", opcode->name)
2019      || 0 == strcasecmp ("ED", opcode->name)
2020      || 0 == strcasecmp ("EDMK", opcode->name)
2021      || 0 == strcasecmp ("MVC", opcode->name)
2022      || 0 == strcasecmp ("MVCIN", opcode->name)
2023      || 0 == strcasecmp ("MVN", opcode->name)
2024      || 0 == strcasecmp ("MVZ", opcode->name)
2025      || 0 == strcasecmp ("NC", opcode->name)
2026      || 0 == strcasecmp ("OC", opcode->name)
2027      || 0 == strcasecmp ("XC", opcode->name))
2028    off_by_one = 1;
2029
2030  /* Gather the operands.  */
2031  fc = 0;
2032  for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
2033    {
2034      const struct i370_operand *operand;
2035      char *hold;
2036      expressionS ex;
2037
2038      operand = &i370_operands[*opindex_ptr];
2039
2040      /* If this is an index operand, and we are skipping it,
2041	 just insert a zero.  */
2042      if (skip_optional_index &&
2043	  ((operand->flags & I370_OPERAND_INDEX) != 0))
2044        {
2045          insn = i370_insert_operand (insn, operand, 0);
2046          continue;
2047        }
2048
2049      /* If this is the base operand, and we are skipping it,
2050	 just insert the current using basreg.  */
2051      if (skip_optional_basereg &&
2052          ((operand->flags & I370_OPERAND_BASE) != 0))
2053        {
2054          int basereg = -1;
2055          if (use_text)
2056            {
2057              if (0 == strncmp (now_seg->name, ".text", 5)
2058		  || 0 > i370_using_other_regno)
2059		basereg = i370_using_text_regno;
2060              else
2061		basereg = i370_using_other_regno;
2062            }
2063          else if (use_other)
2064            {
2065              if (0 > i370_using_other_regno)
2066		basereg = i370_using_text_regno;
2067              else
2068		basereg = i370_using_other_regno;
2069            }
2070          if (0 > basereg)
2071	    as_bad (_("not using any base register"));
2072
2073          insn = i370_insert_operand (insn, operand, basereg);
2074          continue;
2075        }
2076
2077      /* If this is an optional operand, and we are skipping it,
2078	 Use zero (since a non-zero value would denote a register)  */
2079      if (skip_optional_reg
2080	  && ((operand->flags & I370_OPERAND_OPTIONAL) != 0))
2081        {
2082          insn = i370_insert_operand (insn, operand, 0);
2083          continue;
2084        }
2085
2086      /* Gather the operand.  */
2087      hold = input_line_pointer;
2088      input_line_pointer = str;
2089
2090      /* Register names are only allowed where there are registers.  */
2091      if ((operand->flags & I370_OPERAND_GPR) != 0)
2092        {
2093          /* Quickie hack to get past things like (,r13).  */
2094          if (skip_optional_index && (',' == *input_line_pointer))
2095            {
2096              *input_line_pointer = ' ';
2097              input_line_pointer ++;
2098            }
2099
2100          if (! register_name (&ex))
2101	    as_bad (_("expecting a register for operand %d"),
2102		    (int) (opindex_ptr - opcode->operands + 1));
2103        }
2104
2105      /* Check for an address constant expression.  */
2106      /* We will put PSW-relative addresses in the text section,
2107         and address literals in the .data (or other) section.  */
2108      else if (i370_addr_cons (&ex))
2109	use_other = 1;
2110      else if (i370_addr_offset (&ex))
2111	use_text = 1;
2112      else expression (&ex);
2113
2114      str = input_line_pointer;
2115      input_line_pointer = hold;
2116
2117      /* Perform some off-by-one hacks on the length field of certain instructions.
2118         Its such a shame to have to do this, but the problem is that HLASM got
2119         defined so that the programmer specifies a length that is one greater
2120         than what the machine instruction wants.  Sigh.  */
2121      if (off_by_one && (0 == strcasecmp ("SS L", operand->name)))
2122	ex.X_add_number --;
2123
2124      if (ex.X_op == O_illegal)
2125        as_bad (_("illegal operand"));
2126      else if (ex.X_op == O_absent)
2127        as_bad (_("missing operand"));
2128      else if (ex.X_op == O_register)
2129	insn = i370_insert_operand (insn, operand, ex.X_add_number);
2130      else if (ex.X_op == O_constant)
2131        {
2132#ifdef OBJ_ELF
2133          /* Allow @HA, @L, @H on constants.
2134             Well actually, no we don't; there really don't make sense
2135             (at least not to me) for the i370.  However, this code is
2136             left here for any dubious future expansion reasons.  */
2137          char *orig_str = str;
2138
2139          if ((reloc = i370_elf_suffix (&str, &ex)) != BFD_RELOC_UNUSED)
2140            switch (reloc)
2141              {
2142              default:
2143        	str = orig_str;
2144        	break;
2145
2146              case BFD_RELOC_LO16:
2147        	/* X_unsigned is the default, so if the user has done
2148                   something which cleared it, we always produce a
2149                   signed value.  */
2150		ex.X_add_number = (((ex.X_add_number & 0xffff)
2151				    ^ 0x8000)
2152				   - 0x8000);
2153        	break;
2154
2155              case BFD_RELOC_HI16:
2156        	ex.X_add_number = (ex.X_add_number >> 16) & 0xffff;
2157        	break;
2158
2159              case BFD_RELOC_HI16_S:
2160        	ex.X_add_number = (((ex.X_add_number >> 16) & 0xffff)
2161        			   + ((ex.X_add_number >> 15) & 1));
2162        	break;
2163              }
2164#endif
2165          insn = i370_insert_operand (insn, operand, ex.X_add_number);
2166        }
2167#ifdef OBJ_ELF
2168      else if ((reloc = i370_elf_suffix (&str, &ex)) != BFD_RELOC_UNUSED)
2169        {
2170          as_tsktsk ("md_assemble(): suffixed relocations not supported\n");
2171
2172          /* We need to generate a fixup for this expression.  */
2173          if (fc >= MAX_INSN_FIXUPS)
2174            as_fatal ("too many fixups");
2175          fixups[fc].exp = ex;
2176          fixups[fc].opindex = 0;
2177          fixups[fc].reloc = reloc;
2178          ++fc;
2179        }
2180#endif /* OBJ_ELF */
2181      else
2182        {
2183          /* We need to generate a fixup for this expression.  */
2184          /* Typically, the expression will just be a symbol ...
2185               printf ("insn %s needs fixup for %s \n",
2186                    opcode->name, ex.X_add_symbol->bsym->name);  */
2187
2188          if (fc >= MAX_INSN_FIXUPS)
2189            as_fatal ("too many fixups");
2190          fixups[fc].exp = ex;
2191          fixups[fc].opindex = *opindex_ptr;
2192          fixups[fc].reloc = BFD_RELOC_UNUSED;
2193          ++fc;
2194        }
2195
2196      /* Skip over delimiter (close paren, or comma).  */
2197      if ((')' == *str) && (',' == *(str+1)))
2198	++str;
2199      if (*str != '\0')
2200	++str;
2201    }
2202
2203  while (ISSPACE (*str))
2204    ++str;
2205
2206  if (*str != '\0')
2207    as_bad (_("junk at end of line: `%s'"), str);
2208
2209  /* Write out the instruction.  */
2210  f = frag_more (opcode->len);
2211  if (4 >= opcode->len)
2212    md_number_to_chars (f, insn.i[0], opcode->len);
2213  else
2214    {
2215      md_number_to_chars (f, insn.i[0], 4);
2216
2217      if (6 == opcode->len)
2218	md_number_to_chars ((f + 4), ((insn.i[1])>>16), 2);
2219      else
2220	{
2221	  /* Not used --- don't have any 8 byte instructions.  */
2222	  as_bad (_("Internal Error: bad instruction length"));
2223	  md_number_to_chars ((f + 4), insn.i[1], opcode->len -4);
2224	}
2225    }
2226
2227  /* Create any fixups.  At this point we do not use a
2228     bfd_reloc_code_real_type, but instead just use the
2229     BFD_RELOC_UNUSED plus the operand index.  This lets us easily
2230     handle fixups for any operand type, although that is admittedly
2231     not a very exciting feature.  We pick a BFD reloc type in
2232     md_apply_fix.  */
2233  for (i = 0; i < fc; i++)
2234    {
2235      const struct i370_operand *operand;
2236
2237      operand = &i370_operands[fixups[i].opindex];
2238      if (fixups[i].reloc != BFD_RELOC_UNUSED)
2239	{
2240	  reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
2241	  int size;
2242	  fixS *fixP;
2243
2244	  if (!reloc_howto)
2245	    abort ();
2246
2247	  size = bfd_get_reloc_size (reloc_howto);
2248
2249	  if (size < 1 || size > 4)
2250	    abort ();
2251
2252	  printf (" gwana doo fixup %d \n", i);
2253	  fixP = fix_new_exp (frag_now, f - frag_now->fr_literal, size,
2254         		      &fixups[i].exp, reloc_howto->pc_relative,
2255         		      fixups[i].reloc);
2256
2257	  /* Turn off complaints that the addend is too large for things like
2258	     foo+100000@ha.  */
2259	  switch (fixups[i].reloc)
2260	    {
2261	    case BFD_RELOC_16_GOTOFF:
2262	    case BFD_RELOC_LO16:
2263	    case BFD_RELOC_HI16:
2264	    case BFD_RELOC_HI16_S:
2265	      fixP->fx_no_overflow = 1;
2266	      break;
2267	    default:
2268	      break;
2269	    }
2270	}
2271      else
2272	{
2273	  fix_new_exp (frag_now, f - frag_now->fr_literal, opcode->len,
2274		       &fixups[i].exp,
2275		       (operand->flags & I370_OPERAND_RELATIVE) != 0,
2276		       ((bfd_reloc_code_real_type)
2277			(fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
2278	}
2279    }
2280}
2281
2282
2283/* Pseudo-op handling.  */
2284
2285/* The .byte pseudo-op.  This is similar to the normal .byte
2286   pseudo-op, but it can also take a single ASCII string.  */
2287
2288static void
2289i370_byte (int ignore ATTRIBUTE_UNUSED)
2290{
2291  if (*input_line_pointer != '\"')
2292    {
2293      cons (1);
2294      return;
2295    }
2296
2297  /* Gather characters.  A real double quote is doubled.  Unusual
2298     characters are not permitted.  */
2299  ++input_line_pointer;
2300  while (1)
2301    {
2302      char c;
2303
2304      c = *input_line_pointer++;
2305
2306      if (c == '\"')
2307        {
2308        if (*input_line_pointer != '\"')
2309            break;
2310          ++input_line_pointer;
2311        }
2312
2313      FRAG_APPEND_1_CHAR (c);
2314    }
2315
2316  demand_empty_rest_of_line ();
2317}
2318
2319/* The .tc pseudo-op.  This is used when generating XCOFF and ELF.
2320   This takes two or more arguments.
2321
2322   When generating XCOFF output, the first argument is the name to
2323   give to this location in the toc; this will be a symbol with class
2324   TC.  The rest of the arguments are 4 byte values to actually put at
2325   this location in the TOC; often there is just one more argument, a
2326   relocatable symbol reference.
2327
2328   When not generating XCOFF output, the arguments are the same, but
2329   the first argument is simply ignored.  */
2330
2331static void
2332i370_tc (int ignore ATTRIBUTE_UNUSED)
2333{
2334
2335  /* Skip the TOC symbol name.  */
2336  while (is_part_of_name (*input_line_pointer)
2337         || *input_line_pointer == '['
2338         || *input_line_pointer == ']'
2339         || *input_line_pointer == '{'
2340         || *input_line_pointer == '}')
2341    ++input_line_pointer;
2342
2343  /* Align to a four byte boundary.  */
2344  frag_align (2, 0, 0);
2345  record_alignment (now_seg, 2);
2346
2347  if (*input_line_pointer != ',')
2348    demand_empty_rest_of_line ();
2349  else
2350    {
2351      ++input_line_pointer;
2352      cons (4);
2353    }
2354}
2355
2356char *
2357md_atof (int type, char *litp, int *sizep)
2358{
2359  /* 360/370/390 have two float formats: an old, funky 360 single-precision
2360     format, and the ieee format.  Support only the ieee format.  */
2361  return ieee_md_atof (type, litp, sizep, TRUE);
2362}
2363
2364/* Write a value out to the object file, using the appropriate
2365   endianness.  */
2366
2367void
2368md_number_to_chars (char *buf, valueT val, int n)
2369{
2370  number_to_chars_bigendian (buf, val, n);
2371}
2372
2373/* Align a section (I don't know why this is machine dependent).  */
2374
2375valueT
2376md_section_align (asection *seg, valueT addr)
2377{
2378  int align = bfd_get_section_alignment (stdoutput, seg);
2379
2380  return (addr + (1 << align) - 1) & (-1 << align);
2381}
2382
2383/* We don't have any form of relaxing.  */
2384
2385int
2386md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
2387			       asection *seg ATTRIBUTE_UNUSED)
2388{
2389  abort ();
2390  return 0;
2391}
2392
2393/* Convert a machine dependent frag.  We never generate these.  */
2394
2395void
2396md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
2397		 asection *sec ATTRIBUTE_UNUSED,
2398		 fragS *fragp ATTRIBUTE_UNUSED)
2399{
2400  abort ();
2401}
2402
2403/* We have no need to default values of symbols.  */
2404
2405symbolS *
2406md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
2407{
2408  return 0;
2409}
2410
2411/* Functions concerning relocs.  */
2412
2413/* The location from which a PC relative jump should be calculated,
2414   given a PC relative reloc.  */
2415
2416long
2417md_pcrel_from_section (fixS *fixp, segT sec ATTRIBUTE_UNUSED)
2418{
2419  return fixp->fx_frag->fr_address + fixp->fx_where;
2420}
2421
2422/* Apply a fixup to the object code.  This is called for all the
2423   fixups we generated by the call to fix_new_exp, above.  In the call
2424   above we used a reloc code which was the largest legal reloc code
2425   plus the operand index.  Here we undo that to recover the operand
2426   index.  At this point all symbol values should be fully resolved,
2427   and we attempt to completely resolve the reloc.  If we can not do
2428   that, we determine the correct reloc code and put it back in the
2429   fixup.
2430
2431   See gas/cgen.c for more sample code and explanations of what's
2432   going on here.  */
2433
2434void
2435md_apply_fix (fixS *fixP, valueT * valP, segT seg)
2436{
2437  valueT value = * valP;
2438
2439  if (fixP->fx_addsy != NULL)
2440    {
2441#ifdef DEBUG
2442      printf ("\nmd_apply_fix: symbol %s at 0x%x (%s:%d) val=0x%x addend=0x%x\n",
2443	      S_GET_NAME (fixP->fx_addsy),
2444	      fixP->fx_frag->fr_address + fixP->fx_where,
2445	      fixP->fx_file, fixP->fx_line,
2446	      S_GET_VALUE (fixP->fx_addsy), value);
2447#endif
2448    }
2449  else
2450    fixP->fx_done = 1;
2451
2452  /* Apply fixups to operands.  Note that there should be no relocations
2453     for any operands, since no instruction ever takes an operand
2454     that requires reloc.  */
2455  if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
2456    {
2457      int opindex;
2458      const struct i370_operand *operand;
2459      char *where;
2460      i370_insn_t insn;
2461
2462      opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
2463
2464      operand = &i370_operands[opindex];
2465
2466#ifdef DEBUG
2467      printf ("\nmd_apply_fix: fixup operand %s at 0x%x in %s:%d addend=0x%x\n",
2468	      operand->name,
2469	      fixP->fx_frag->fr_address + fixP->fx_where,
2470	      fixP->fx_file, fixP->fx_line,
2471	      value);
2472#endif
2473      /* Fetch the instruction, insert the fully resolved operand
2474         value, and stuff the instruction back again.
2475         fisxp->fx_size is the length of the instruction.  */
2476      where = fixP->fx_frag->fr_literal + fixP->fx_where;
2477      insn.i[0] = bfd_getb32 ((unsigned char *) where);
2478
2479      if (6 <= fixP->fx_size)
2480	/* Deal with 48-bit insn's.  */
2481	insn.i[1] = bfd_getb32 (((unsigned char *) where)+4);
2482
2483      insn = i370_insert_operand (insn, operand, (offsetT) value);
2484      bfd_putb32 ((bfd_vma) insn.i[0], (unsigned char *) where);
2485
2486      if (6 <= fixP->fx_size)
2487	/* Deal with 48-bit insn's.  */
2488	bfd_putb32 ((bfd_vma) insn.i[1], (((unsigned char *) where)+4));
2489
2490      /* We are done, right? right !!  */
2491      fixP->fx_done = 1;
2492      if (fixP->fx_done)
2493	/* Nothing else to do here.  */
2494	return;
2495
2496      /* Determine a BFD reloc value based on the operand information.
2497	 We are only prepared to turn a few of the operands into
2498	 relocs.  In fact, we support *zero* operand relocations ...
2499	 Why?  Because we are not expecting the compiler to generate
2500	 any operands that need relocation.  Due to the 12-bit naturew of
2501	 i370 addressing, this would be unusual.  */
2502        {
2503          char *sfile;
2504          unsigned int sline;
2505
2506          /* Use expr_symbol_where to see if this is an expression
2507             symbol.  */
2508          if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
2509            as_bad_where (fixP->fx_file, fixP->fx_line,
2510        		  "unresolved expression that must be resolved");
2511          else
2512            as_bad_where (fixP->fx_file, fixP->fx_line,
2513        		  "unsupported relocation type");
2514          fixP->fx_done = 1;
2515          return;
2516        }
2517    }
2518  else
2519    {
2520      /* We branch to here if the fixup is not to a symbol that
2521         appears in an instruction operand, but is rather some
2522         declared storage.  */
2523#ifdef OBJ_ELF
2524      i370_elf_validate_fix (fixP, seg);
2525#endif
2526#ifdef DEBUG
2527      printf ("md_apply_fix: reloc case %d in segment  %s %s:%d\n",
2528	      fixP->fx_r_type, segment_name (seg), fixP->fx_file, fixP->fx_line);
2529      printf ("\tcurrent fixup value is 0x%x \n", value);
2530#endif
2531      switch (fixP->fx_r_type)
2532        {
2533        case BFD_RELOC_32:
2534        case BFD_RELOC_CTOR:
2535          if (fixP->fx_pcrel)
2536            fixP->fx_r_type = BFD_RELOC_32_PCREL;
2537	  /* Fall through.  */
2538
2539        case BFD_RELOC_RVA:
2540        case BFD_RELOC_32_PCREL:
2541        case BFD_RELOC_32_BASEREL:
2542#ifdef DEBUG
2543          printf ("\t32 bit relocation at 0x%x\n",
2544		  fixP->fx_frag->fr_address + fixP->fx_where);
2545#endif
2546          md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
2547        		      value, 4);
2548          break;
2549
2550        case BFD_RELOC_LO16:
2551        case BFD_RELOC_16:
2552          if (fixP->fx_pcrel)
2553            as_bad_where (fixP->fx_file, fixP->fx_line,
2554        		  "cannot emit PC relative %s relocation%s%s",
2555        		  bfd_get_reloc_code_name (fixP->fx_r_type),
2556        		  fixP->fx_addsy != NULL ? " against " : "",
2557        		  (fixP->fx_addsy != NULL
2558        		   ? S_GET_NAME (fixP->fx_addsy)
2559        		   : ""));
2560
2561          md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
2562        		      value, 2);
2563          break;
2564
2565          /* This case happens when you write, for example,
2566             lis %r3,(L1-L2)@ha
2567             where L1 and L2 are defined later.  */
2568        case BFD_RELOC_HI16:
2569          if (fixP->fx_pcrel)
2570            abort ();
2571          md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
2572        		      value >> 16, 2);
2573          break;
2574        case BFD_RELOC_HI16_S:
2575          if (fixP->fx_pcrel)
2576            abort ();
2577          md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
2578        		      (value + 0x8000) >> 16, 2);
2579          break;
2580
2581        case BFD_RELOC_8:
2582          if (fixP->fx_pcrel)
2583            abort ();
2584
2585          md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
2586        		      value, 1);
2587          break;
2588
2589        default:
2590          fprintf (stderr,
2591        	  "Gas failure, reloc value %d\n", fixP->fx_r_type);
2592          fflush (stderr);
2593          abort ();
2594        }
2595    }
2596
2597  fixP->fx_addnumber = value;
2598}
2599
2600/* Generate a reloc for a fixup.  */
2601
2602arelent *
2603tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
2604{
2605  arelent *reloc;
2606
2607  reloc = xmalloc (sizeof (arelent));
2608
2609  reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
2610  *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2611  reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2612  reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2613  if (reloc->howto == (reloc_howto_type *) NULL)
2614    {
2615      as_bad_where (fixp->fx_file, fixp->fx_line,
2616        	    "reloc %d not supported by object file format", (int)fixp->fx_r_type);
2617      return NULL;
2618    }
2619  reloc->addend = fixp->fx_addnumber;
2620
2621#ifdef DEBUG
2622  printf ("\ngen_reloc(): sym %s (%s:%d) at addr 0x%x addend=0x%x\n",
2623	  fixp->fx_addsy->bsym->name,
2624	  fixp->fx_file, fixp->fx_line,
2625	  reloc->address, reloc->addend);
2626#endif
2627
2628  return reloc;
2629}
2630
2631/* The target specific pseudo-ops which we support.  */
2632
2633const pseudo_typeS md_pseudo_table[] =
2634{
2635  /* Pseudo-ops which must be overridden.  */
2636  { "byte",     i370_byte,	0 },
2637
2638  { "dc",       i370_dc,	0 },
2639  { "ds",       i370_ds,	0 },
2640  { "rmode",    i370_rmode,	0 },
2641  { "csect",    i370_csect,	0 },
2642  { "dsect",    i370_dsect,	0 },
2643
2644  /* enable ebcdic strings e.g. for 3270 support */
2645  { "ebcdic",   i370_ebcdic,	0 },
2646
2647#ifdef OBJ_ELF
2648  { "long",     i370_elf_cons,	4 },
2649  { "word",     i370_elf_cons,	4 },
2650  { "short",    i370_elf_cons,	2 },
2651  { "rdata",    i370_elf_rdata,	0 },
2652  { "rodata",   i370_elf_rdata,	0 },
2653  { "lcomm",    i370_elf_lcomm,	0 },
2654#endif
2655
2656  /* This pseudo-op is used even when not generating XCOFF output.  */
2657  { "tc",       i370_tc,	0 },
2658
2659  /* dump the literal pool */
2660  { "ltorg",    i370_ltorg,	0 },
2661
2662  /* support the hlasm-style USING directive */
2663  { "using",    i370_using,	0 },
2664  { "drop",     i370_drop,	0 },
2665
2666  { NULL,       NULL,		0 }
2667};
2668