* write.c (is_dnrange): Stop as soon as the address becomes
[binutils.git] / gas / config / tc-i370.c
blobde9fce785bd078d8acf675006be92a629cb151b1
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 (C) 1994, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* This assembler implements a very hacked version of an elf-like thing
24 * that gcc emits (when gcc is suitably hacked). To make it behave more
25 * HLASM-like, try turning on the -M or --mri flag (as there are various
26 * similarities between HLASM and the MRI assemblers, such as section
27 * names, lack of leading . in pseudo-ops, DC and DS, etc ...
30 #include <stdio.h>
31 #include <ctype.h>
32 #include "as.h"
33 #include "subsegs.h"
34 #include "struc-symbol.h"
36 #include "opcode/i370.h"
38 #ifdef OBJ_ELF
39 #include "elf/i370.h"
40 #endif
42 /* This is the assembler for the System/390 Architecture */
44 /* Tell the main code what the endianness is. */
45 extern int target_big_endian;
48 /* Generic assembler global variables which must be defined by all
49 targets. */
51 #ifdef OBJ_ELF
52 /* This string holds the chars that always start a comment. If the
53 pre-processor is disabled, these aren't very useful. The macro
54 tc_comment_chars points to this. We use this, rather than the
55 usual comment_chars, so that we can switch for Solaris conventions. */
56 static const char i370_eabi_comment_chars[] = "#";
58 const char *i370_comment_chars = i370_eabi_comment_chars;
59 #else
60 const char comment_chars[] = "#";
61 #endif
63 /* Characters which start a comment at the beginning of a line. */
64 const char line_comment_chars[] = "#*";
66 /* Characters which may be used to separate multiple commands on a
67 single line. */
68 const char line_separator_chars[] = ";";
70 /* Characters which are used to indicate an exponent in a floating
71 point number. */
72 const char EXP_CHARS[] = "eE";
74 /* Characters which mean that a number is a floating point constant,
75 as in 0d1.0. */
76 const char FLT_CHARS[] = "dD";
78 void
79 md_show_usage (stream)
80 FILE *stream;
82 fprintf (stream, "\
83 S/370 options: (these have not yet been tested and may not work) \n\
84 -u ignored\n\
85 -mregnames Allow symbolic names for registers\n\
86 -mno-regnames Do not allow symbolic names for registers\n");
87 #ifdef OBJ_ELF
88 fprintf (stream, "\
89 -mrelocatable support for GCC's -mrelocatble option\n\
90 -mrelocatable-lib support for GCC's -mrelocatble-lib option\n\
91 -V print assembler version number\n");
92 #endif
96 static void i370_byte PARAMS ((int));
97 static void i370_tc PARAMS ((int));
98 static void i370_ebcdic PARAMS ((int));
100 static void i370_dc PARAMS ((int));
101 static void i370_ds PARAMS ((int));
102 static void i370_rmode PARAMS ((int));
103 static void i370_csect PARAMS ((int));
104 static void i370_dsect PARAMS ((int));
105 static void i370_ltorg PARAMS ((int));
106 static void i370_using PARAMS ((int));
107 static void i370_drop PARAMS ((int));
108 static void i370_make_relative PARAMS ((expressionS *exp, expressionS *baseaddr));
110 #ifdef OBJ_ELF
111 static bfd_reloc_code_real_type i370_elf_suffix PARAMS ((char **, expressionS *));
112 static void i370_elf_cons PARAMS ((int));
113 static void i370_elf_rdata PARAMS ((int));
114 static void i370_elf_lcomm PARAMS ((int));
115 static void i370_elf_validate_fix PARAMS ((fixS *, segT));
116 #endif
119 /* The target specific pseudo-ops which we support. */
121 const pseudo_typeS md_pseudo_table[] =
123 /* Pseudo-ops which must be overridden. */
124 { "byte", i370_byte, 0 },
126 { "dc", i370_dc, 0 },
127 { "ds", i370_ds, 0 },
128 { "rmode", i370_rmode, 0 },
129 { "csect", i370_csect, 0 },
130 { "dsect", i370_dsect, 0 },
132 /* enable ebcdic strings e.g. for 3270 support */
133 { "ebcdic", i370_ebcdic, 0 },
135 #ifdef OBJ_ELF
136 { "long", i370_elf_cons, 4 },
137 { "word", i370_elf_cons, 4 },
138 { "short", i370_elf_cons, 2 },
139 { "rdata", i370_elf_rdata, 0 },
140 { "rodata", i370_elf_rdata, 0 },
141 { "lcomm", i370_elf_lcomm, 0 },
142 #endif
144 /* This pseudo-op is used even when not generating XCOFF output. */
145 { "tc", i370_tc, 0 },
147 /* dump the literal pool */
148 { "ltorg", i370_ltorg, 0 },
150 /* support the hlasm-style USING directive */
151 { "using", i370_using, 0 },
152 { "drop", i370_drop, 0 },
154 { NULL, NULL, 0 }
157 /* ***************************************************************** */
159 /* Whether to use user friendly register names. */
160 #define TARGET_REG_NAMES_P true
162 static boolean reg_names_p = TARGET_REG_NAMES_P;
164 static boolean register_name PARAMS ((expressionS *));
165 static void i370_set_cpu PARAMS ((void));
166 static i370_insn_t i370_insert_operand
167 PARAMS ((i370_insn_t insn, const struct i370_operand *operand, offsetT val));
168 static void i370_macro PARAMS ((char *str, const struct i370_macro *macro));
170 /* Predefined register names if -mregnames */
171 /* In general, there are lots of them, in an attempt to be compatible */
172 /* with a number of assemblers. */
174 /* Structure to hold information about predefined registers. */
175 struct pd_reg
177 char *name;
178 int value;
181 /* List of registers that are pre-defined:
183 Each general register has predefined names of the form:
184 1. r<reg_num> which has the value <reg_num>.
185 2. r.<reg_num> which has the value <reg_num>.
187 Each floating point register has predefined names of the form:
188 1. f<reg_num> which has the value <reg_num>.
189 2. f.<reg_num> which has the value <reg_num>.
191 There are only four floating point registers, and these are
192 commonly labelled 0,2,4 and 6. Thus, there is no f1, f3, etc.
194 There are individual registers as well:
195 rbase or r.base has the value 3 (base register)
196 rpgt or r.pgt has the value 4 (page origin table pointer)
197 rarg or r.arg has the value 11 (argument pointer)
198 rtca or r.tca has the value 12 (table of contents pointer)
199 rtoc or r.toc has the value 12 (table of contents pointer)
200 sp or r.sp has the value 13 (stack pointer)
201 dsa or r.dsa has the value 13 (stack pointer)
202 lr has the value 14 (link reg)
204 The table is sorted. Suitable for searching by a binary search. */
206 static const struct pd_reg pre_defined_registers[] =
208 { "arg", 11 }, /* Argument Pointer */
209 { "base", 3 }, /* Base Reg */
211 { "f.0", 0 }, /* Floating point registers */
212 { "f.2", 2 },
213 { "f.4", 4 },
214 { "f.6", 6 },
216 { "f0", 0 },
217 { "f2", 2 },
218 { "f4", 4 },
219 { "f6", 6 },
221 { "dsa",13 }, /* stack pointer */
222 { "lr", 14 }, /* Link Register */
223 { "pgt", 4 }, /* Page Origin Table Pointer */
225 { "r.0", 0 }, /* General Purpose Registers */
226 { "r.1", 1 },
227 { "r.10", 10 },
228 { "r.11", 11 },
229 { "r.12", 12 },
230 { "r.13", 13 },
231 { "r.14", 14 },
232 { "r.15", 15 },
233 { "r.2", 2 },
234 { "r.3", 3 },
235 { "r.4", 4 },
236 { "r.5", 5 },
237 { "r.6", 6 },
238 { "r.7", 7 },
239 { "r.8", 8 },
240 { "r.9", 9 },
242 { "r.arg", 11 }, /* Argument Pointer */
243 { "r.base", 3 }, /* Base Reg */
244 { "r.dsa", 13 }, /* Stack Pointer */
245 { "r.pgt", 4 }, /* Page Origin Table Pointer */
246 { "r.sp", 13 }, /* Stack Pointer */
248 { "r.tca", 12 }, /* Pointer to the table of contents */
249 { "r.toc", 12 }, /* Pointer to the table of contents */
251 { "r0", 0 }, /* More general purpose registers */
252 { "r1", 1 },
253 { "r10", 10 },
254 { "r11", 11 },
255 { "r12", 12 },
256 { "r13", 13 },
257 { "r14", 14 },
258 { "r15", 15 },
259 { "r2", 2 },
260 { "r3", 3 },
261 { "r4", 4 },
262 { "r5", 5 },
263 { "r6", 6 },
264 { "r7", 7 },
265 { "r8", 8 },
266 { "r9", 9 },
268 { "rbase", 3 }, /* Base Reg */
270 { "rtca", 12 }, /* Pointer to the table of contents */
271 { "rtoc", 12 }, /* Pointer to the table of contents */
273 { "sp", 13 }, /* Stack Pointer */
277 #define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct pd_reg))
279 /* Given NAME, find the register number associated with that name, return
280 the integer value associated with the given name or -1 on failure. */
282 static int reg_name_search
283 PARAMS ((const struct pd_reg *, int, const char * name));
285 static int
286 reg_name_search (regs, regcount, name)
287 const struct pd_reg *regs;
288 int regcount;
289 const char *name;
291 int middle, low, high;
292 int cmp;
294 low = 0;
295 high = regcount - 1;
299 middle = (low + high) / 2;
300 cmp = strcasecmp (name, regs[middle].name);
301 if (cmp < 0)
302 high = middle - 1;
303 else if (cmp > 0)
304 low = middle + 1;
305 else
306 return regs[middle].value;
308 while (low <= high);
310 return -1;
314 * Summary of register_name().
316 * in: Input_line_pointer points to 1st char of operand.
318 * out: A expressionS.
319 * The operand may have been a register: in this case, X_op == O_register,
320 * X_add_number is set to the register number, and truth is returned.
321 * Input_line_pointer->(next non-blank) char after operand, or is in its
322 * original state.
325 static boolean
326 register_name (expressionP)
327 expressionS *expressionP;
329 int reg_number;
330 char *name;
331 char *start;
332 char c;
334 /* Find the spelling of the operand */
335 start = name = input_line_pointer;
336 if (name[0] == '%' && isalpha (name[1]))
337 name = ++input_line_pointer;
339 else if (!reg_names_p)
340 return false;
342 while (' ' == *name)
343 name = ++input_line_pointer;
345 /* if its a number, treat it as a number */
346 /* if its alpha, look to see if it's in the register table */
347 if (!isalpha (name[0]))
349 reg_number = get_single_number();
350 c = get_symbol_end ();
352 else
354 c = get_symbol_end ();
355 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
358 /* if numeric, make sure its not out of bounds */
359 if ((0 <= reg_number) && (16 >= reg_number))
361 expressionP->X_op = O_register;
362 expressionP->X_add_number = reg_number;
364 /* make the rest nice */
365 expressionP->X_add_symbol = NULL;
366 expressionP->X_op_symbol = NULL;
367 *input_line_pointer = c; /* put back the delimiting char */
368 return true;
371 /* reset the line as if we had not done anything */
372 *input_line_pointer = c; /* put back the delimiting char */
373 input_line_pointer = start; /* reset input_line pointer */
374 return false;
377 /* Local variables. */
379 /* The type of processor we are assembling for. This is one or more
380 of the I370_OPCODE flags defined in opcode/i370.h. */
381 static int i370_cpu = 0;
383 /* The base register to use for opcode with optional operands.
384 * We define two of these: "text" and "other". Normally, "text"
385 * would get used in the .text section for branches, while "other"
386 * gets used in the .data section for address constants.
388 * The idea of a second base register in a different section
389 * is foreign to the usual HLASM-style semantics; however, it
390 * allows us to provide support for dynamically loaded libraries,
391 * by allowing us to place address constants in a section other
392 * than the text section. The "other" section need not be the
393 * .data section, it can be any section that isn't the .text section.
395 * Note that HLASM defines a multiple, concurrent .using semantic
396 * that we do not: in calculating offsets, it uses either the most
397 * recent .using directive, or the one with the smallest displacement.
398 * This allows HLASM to support a quasi-block-scope-like behaviour.
399 * Handy for people writing assembly by hand ... but not supported
400 * by us.
402 static int i370_using_text_regno = -1;
403 static int i370_using_other_regno = -1;
405 /* The base address for address literals */
406 static expressionS i370_using_text_baseaddr;
407 static expressionS i370_using_other_baseaddr;
409 /* the "other" section, used only for syntax error detection */
410 static segT i370_other_section = undefined_section;
412 /* Opcode hash table. */
413 static struct hash_control *i370_hash;
415 /* Macro hash table. */
416 static struct hash_control *i370_macro_hash;
418 #ifdef OBJ_ELF
419 /* What type of shared library support to use */
420 static enum { SHLIB_NONE, SHLIB_PIC, SHILB_MRELOCATABLE } shlib = SHLIB_NONE;
421 #endif
423 /* Flags to set in the elf header */
424 static flagword i370_flags = 0;
426 #ifndef WORKING_DOT_WORD
427 const int md_short_jump_size = 4;
428 const int md_long_jump_size = 4;
429 #endif
431 #ifdef OBJ_ELF
432 CONST char *md_shortopts = "l:um:K:VQ:";
433 #else
434 CONST char *md_shortopts = "um:";
435 #endif
436 struct option md_longopts[] =
438 {NULL, no_argument, NULL, 0}
440 size_t md_longopts_size = sizeof (md_longopts);
443 md_parse_option (c, arg)
444 int c;
445 char *arg;
447 switch (c)
449 case 'u':
450 /* -u means that any undefined symbols should be treated as
451 external, which is the default for gas anyhow. */
452 break;
454 #ifdef OBJ_ELF
455 case 'K':
456 /* Recognize -K PIC */
457 if (strcmp (arg, "PIC") == 0 || strcmp (arg, "pic") == 0)
459 shlib = SHLIB_PIC;
460 i370_flags |= EF_I370_RELOCATABLE_LIB;
462 else
463 return 0;
465 break;
466 #endif
468 case 'm':
470 /* -m360 mean to assemble for the ancient 360 architecture */
471 if (strcmp (arg, "360") == 0 || strcmp (arg, "i360") == 0)
472 i370_cpu = I370_OPCODE_360;
473 /* -mxa means to assemble for the IBM 370 XA */
474 else if (strcmp (arg, "xa") == 0)
475 i370_cpu = I370_OPCODE_370_XA;
476 /* -many means to assemble for any architecture (370/XA). */
477 else if (strcmp (arg, "any") == 0)
478 i370_cpu = I370_OPCODE_370;
480 else if (strcmp (arg, "regnames") == 0)
481 reg_names_p = true;
483 else if (strcmp (arg, "no-regnames") == 0)
484 reg_names_p = false;
486 #ifdef OBJ_ELF
487 /* -mrelocatable/-mrelocatable-lib -- warn about initializations that require relocation */
488 else if (strcmp (arg, "relocatable") == 0)
490 shlib = SHILB_MRELOCATABLE;
491 i370_flags |= EF_I370_RELOCATABLE;
494 else if (strcmp (arg, "relocatable-lib") == 0)
496 shlib = SHILB_MRELOCATABLE;
497 i370_flags |= EF_I370_RELOCATABLE_LIB;
500 #endif
501 else
503 as_bad ("invalid switch -m%s", arg);
504 return 0;
506 break;
508 #ifdef OBJ_ELF
509 /* -V: SVR4 argument to print version ID. */
510 case 'V':
511 print_version_id ();
512 break;
514 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
515 should be emitted or not. FIXME: Not implemented. */
516 case 'Q':
517 break;
519 #endif
521 default:
522 return 0;
525 return 1;
529 /* Set i370_cpu if it is not already set.
530 Currently defaults to the reasonable superset;
531 but can be made more fine grained if desred. */
533 static void
534 i370_set_cpu ()
536 const char *default_os = TARGET_OS;
537 const char *default_cpu = TARGET_CPU;
539 /* override with the superset for the moment. */
540 i370_cpu = I370_OPCODE_ESA390_SUPERSET;
541 if (i370_cpu == 0)
543 if (strcmp (default_cpu, "i360") == 0)
544 i370_cpu = I370_OPCODE_360;
545 else if (strcmp (default_cpu, "i370") == 0)
546 i370_cpu = I370_OPCODE_370;
547 else if (strcmp (default_cpu, "XA") == 0)
548 i370_cpu = I370_OPCODE_370_XA;
549 else
550 as_fatal ("Unknown default cpu = %s, os = %s", default_cpu, default_os);
554 /* Figure out the BFD architecture to use. */
555 /* hack alert -- specify the different 370 architectures */
557 enum bfd_architecture
558 i370_arch ()
560 return bfd_arch_i370;
563 /* This function is called when the assembler starts up. It is called
564 after the options have been parsed and the output file has been
565 opened. */
567 void
568 md_begin ()
570 register const struct i370_opcode *op;
571 const struct i370_opcode *op_end;
572 const struct i370_macro *macro;
573 const struct i370_macro *macro_end;
574 boolean dup_insn = false;
576 i370_set_cpu ();
578 #ifdef OBJ_ELF
579 /* Set the ELF flags if desired. */
580 if (i370_flags)
581 bfd_set_private_flags (stdoutput, i370_flags);
582 #endif
584 /* Insert the opcodes into a hash table. */
585 i370_hash = hash_new ();
587 op_end = i370_opcodes + i370_num_opcodes;
588 for (op = i370_opcodes; op < op_end; op++)
590 know ((op->opcode & op->mask) == op->opcode);
592 if ((op->flags & i370_cpu) != 0)
594 const char *retval;
596 retval = hash_insert (i370_hash, op->name, (PTR) op);
597 if (retval != (const char *) NULL)
599 as_bad ("Internal assembler error for instruction %s", op->name);
600 dup_insn = true;
605 /* Insert the macros into a hash table. */
606 i370_macro_hash = hash_new ();
608 macro_end = i370_macros + i370_num_macros;
609 for (macro = i370_macros; macro < macro_end; macro++)
611 if ((macro->flags & i370_cpu) != 0)
613 const char *retval;
615 retval = hash_insert (i370_macro_hash, macro->name, (PTR) macro);
616 if (retval != (const char *) NULL)
618 as_bad ("Internal assembler error for macro %s", macro->name);
619 dup_insn = true;
624 if (dup_insn)
625 abort ();
628 /* Insert an operand value into an instruction. */
630 static i370_insn_t
631 i370_insert_operand (insn, operand, val)
632 i370_insn_t insn;
633 const struct i370_operand *operand;
634 offsetT val;
636 if (operand->insert)
638 const char *errmsg;
640 /* used for 48-bit insn's */
641 errmsg = NULL;
642 insn = (*operand->insert) (insn, (long) val, &errmsg);
643 if (errmsg)
644 as_bad ("%s", errmsg);
646 else
648 /* this is used only for 16, 32 bit insn's */
649 insn.i[0] |= (((long) val & ((1 << operand->bits) - 1))
650 << operand->shift);
653 return insn;
657 #ifdef OBJ_ELF
658 /* Parse @got, etc. and return the desired relocation.
659 Currently, i370 does not support (don't really need to support) any
660 of these fancier markups ... for example, no one is going to
661 write 'L 6,=V(bogus)@got' it just doesn't make sense (at least to me).
662 So basically, we could get away with this routine returning
663 BFD_RELOC_UNUSED in all circumstances. However, I'll leave
664 in for now in case someone ambitious finds a good use for this stuff ...
665 this routine was pretty much just copied from the powerpc code ... */
666 static bfd_reloc_code_real_type
667 i370_elf_suffix (str_p, exp_p)
668 char **str_p;
669 expressionS *exp_p;
671 struct map_bfd
673 char *string;
674 int length;
675 bfd_reloc_code_real_type reloc;
678 char ident[20];
679 char *str = *str_p;
680 char *str2;
681 int ch;
682 int len;
683 struct map_bfd *ptr;
685 #define MAP(str,reloc) { str, sizeof (str)-1, reloc }
687 static struct map_bfd mapping[] =
689 #if 0
690 MAP ("l", BFD_RELOC_LO16),
691 MAP ("h", BFD_RELOC_HI16),
692 MAP ("ha", BFD_RELOC_HI16_S),
693 #endif
694 /* warnings with -mrelocatable. */
695 MAP ("fixup", BFD_RELOC_CTOR),
696 { (char *)0, 0, BFD_RELOC_UNUSED }
699 if (*str++ != '@')
700 return BFD_RELOC_UNUSED;
702 for (ch = *str, str2 = ident;
703 (str2 < ident + sizeof (ident) - 1
704 && (isalnum (ch) || ch == '@'));
705 ch = *++str)
707 *str2++ = (islower (ch)) ? ch : tolower (ch);
710 *str2 = '\0';
711 len = str2 - ident;
713 ch = ident[0];
714 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
715 if (ch == ptr->string[0]
716 && len == ptr->length
717 && memcmp (ident, ptr->string, ptr->length) == 0)
719 if (exp_p->X_add_number != 0
720 && (ptr->reloc == BFD_RELOC_16_GOTOFF
721 || ptr->reloc == BFD_RELOC_LO16_GOTOFF
722 || ptr->reloc == BFD_RELOC_HI16_GOTOFF
723 || ptr->reloc == BFD_RELOC_HI16_S_GOTOFF))
724 as_warn ("identifier+constant@got means identifier@got+constant");
726 /* Now check for identifier@suffix+constant */
727 if (*str == '-' || *str == '+')
729 char *orig_line = input_line_pointer;
730 expressionS new_exp;
732 input_line_pointer = str;
733 expression (&new_exp);
734 if (new_exp.X_op == O_constant)
736 exp_p->X_add_number += new_exp.X_add_number;
737 str = input_line_pointer;
740 if (&input_line_pointer != str_p)
741 input_line_pointer = orig_line;
744 *str_p = str;
745 return ptr->reloc;
748 return BFD_RELOC_UNUSED;
751 /* Like normal .long/.short/.word, except support @got, etc. */
752 /* clobbers input_line_pointer, checks end-of-line. */
753 static void
754 i370_elf_cons (nbytes)
755 register int nbytes; /* 1=.byte, 2=.word, 4=.long */
757 expressionS exp;
758 bfd_reloc_code_real_type reloc;
760 if (is_it_end_of_statement ())
762 demand_empty_rest_of_line ();
763 return;
768 expression (&exp);
769 if (exp.X_op == O_symbol
770 && *input_line_pointer == '@'
771 && (reloc = i370_elf_suffix (&input_line_pointer, &exp)) != BFD_RELOC_UNUSED)
773 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
774 int size = bfd_get_reloc_size (reloc_howto);
776 if (size > nbytes)
777 as_bad ("%s relocations do not fit in %d bytes\n", reloc_howto->name, nbytes);
779 else
781 register char *p = frag_more ((int) nbytes);
782 int offset = nbytes - size;
784 fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size, &exp, 0, reloc);
787 else
788 emit_expr (&exp, (unsigned int) nbytes);
790 while (*input_line_pointer++ == ',');
792 input_line_pointer--; /* Put terminator back into stream. */
793 demand_empty_rest_of_line ();
797 /* ASCII to EBCDIC conversion table. */
798 static unsigned char ascebc[256] =
800 /*00 NL SH SX EX ET NQ AK BL */
801 0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F,
802 /*08 BS HT LF VT FF CR SO SI */
803 0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
804 /*10 DL D1 D2 D3 D4 NK SN EB */
805 0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26,
806 /*18 CN EM SB EC FS GS RS US */
807 0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F,
808 /*20 SP ! " # $ % & ' */
809 0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D,
810 /*28 ( ) * + , - . / */
811 0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
812 /*30 0 1 2 3 4 5 6 7 */
813 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
814 /*38 8 9 : ; < = > ? */
815 0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
816 /*40 @ A B C D E F G */
817 0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
818 /*48 H I J K L M N O */
819 0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
820 /*50 P Q R S T U V W */
821 0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
822 /*58 X Y Z [ \ ] ^ _ */
823 0xE7, 0xE8, 0xE9, 0xAD, 0xE0, 0xBD, 0x5F, 0x6D,
824 /*60 ` a b c d e f g */
825 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
826 /*68 h i j k l m n o */
827 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
828 /*70 p q r s t u v w */
829 0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
830 /*78 x y z { | } ~ DL */
831 0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
832 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
833 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
834 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
835 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
836 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
837 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
838 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
839 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
840 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
841 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
842 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
843 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
844 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
845 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
846 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
847 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0xFF
850 /* EBCDIC to ASCII conversion table. */
851 unsigned char ebcasc[256] =
853 /*00 NU SH SX EX PF HT LC DL */
854 0x00, 0x01, 0x02, 0x03, 0x00, 0x09, 0x00, 0x7F,
855 /*08 SM VT FF CR SO SI */
856 0x00, 0x00, 0x00, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
857 /*10 DE D1 D2 TM RS NL BS IL */
858 0x10, 0x11, 0x12, 0x13, 0x14, 0x0A, 0x08, 0x00,
859 /*18 CN EM CC C1 FS GS RS US */
860 0x18, 0x19, 0x00, 0x00, 0x1C, 0x1D, 0x1E, 0x1F,
861 /*20 DS SS FS BP LF EB EC */
862 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x17, 0x1B,
863 /*28 SM C2 EQ AK BL */
864 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07, 0x00,
865 /*30 SY PN RS UC ET */
866 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
867 /*38 C3 D4 NK SU */
868 0x00, 0x00, 0x00, 0x00, 0x14, 0x15, 0x00, 0x1A,
869 /*40 SP */
870 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
871 /*48 . < ( + | */
872 0x00, 0x00, 0x00, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
873 /*50 & */
874 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
875 /*58 ! $ * ) ; ^ */
876 0x00, 0x00, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0x5E,
877 /*60 - / */
878 0x2D, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
879 /*68 , % _ > ? */
880 0x00, 0x00, 0x00, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
881 /*70 */
882 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
883 /*78 ` : # @ ' = " */
884 0x00, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
885 /*80 a b c d e f g */
886 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
887 /*88 h i { */
888 0x68, 0x69, 0x00, 0x7B, 0x00, 0x00, 0x00, 0x00,
889 /*90 j k l m n o p */
890 0x00, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
891 /*98 q r } */
892 0x71, 0x72, 0x00, 0x7D, 0x00, 0x00, 0x00, 0x00,
893 /*A0 ~ s t u v w x */
894 0x00, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
895 /*A8 y z [ */
896 0x79, 0x7A, 0x00, 0x00, 0x00, 0x5B, 0x00, 0x00,
897 /*B0 */
898 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
899 /*B8 ] */
900 0x00, 0x00, 0x00, 0x00, 0x00, 0x5D, 0x00, 0x00,
901 /*C0 { A B C D E F G */
902 0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
903 /*C8 H I */
904 0x48, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
905 /*D0 } J K L M N O P */
906 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
907 /*D8 Q R */
908 0x51, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
909 /*E0 \ S T U V W X */
910 0x5C, 0x00, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
911 /*E8 Y Z */
912 0x59, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
913 /*F0 0 1 2 3 4 5 6 7 */
914 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
915 /*F8 8 9 */
916 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF
919 /* ebcdic translation tables needed for 3270 support */
920 static void
921 i370_ebcdic (unused)
922 int unused;
924 char *p, *end;
925 char delim = 0;
926 size_t nbytes;
928 nbytes = strlen (input_line_pointer);
929 end = input_line_pointer + nbytes;
930 while ('\r' == *end) end --;
931 while ('\n' == *end) end --;
933 delim = *input_line_pointer;
934 if (('\'' == delim) || ('\"' == delim)) {
935 input_line_pointer ++;
936 end = rindex (input_line_pointer, delim);
939 if (end > input_line_pointer)
941 nbytes = end - input_line_pointer +1;
942 p = frag_more (nbytes);
943 while (end > input_line_pointer)
945 *p = ascebc [(unsigned char) (*input_line_pointer)];
946 ++p; ++input_line_pointer;
948 *p = '\0';
950 if (delim == *input_line_pointer) ++input_line_pointer;
954 /* stub out a couple of routines */
955 static void
956 i370_rmode (unused)
957 int unused;
959 as_tsktsk ("rmode ignored");
962 static void
963 i370_dsect (sect)
964 int sect;
966 char *save_line = input_line_pointer;
967 static char section[] = ".data\n";
969 /* Just pretend this is .section .data */
970 input_line_pointer = section;
971 obj_elf_section (sect);
973 input_line_pointer = save_line;
976 static void
977 i370_csect (unused)
978 int unused;
980 as_tsktsk ("csect not supported");
984 /* DC Define Const is only partially supported.
985 * For samplecode on what to do, look at i370_elf_cons() above.
986 * This code handles pseudoops of the style
987 * DC D'3.141592653' # in sysv4, .double 3.14159265
988 * DC F'1' # in sysv4, .long 1
990 static void
991 i370_dc(unused)
992 int unused;
994 char * p, tmp[50];
995 int nbytes=0;
996 expressionS exp;
997 char type=0;
999 if (is_it_end_of_statement ())
1001 demand_empty_rest_of_line ();
1002 return;
1005 /* figure out the size */
1006 type = *input_line_pointer++;
1007 switch (type)
1009 case 'H': /* 16-bit */
1010 nbytes = 2;
1011 break;
1012 case 'E': /* 32-bit */
1013 case 'F': /* 32-bit */
1014 nbytes = 4;
1015 break;
1016 case 'D': /* 64-bit */
1017 nbytes = 8;
1018 break;
1019 default:
1020 as_bad ("unsupported DC type");
1021 return;
1024 /* get rid of pesky quotes */
1025 if ('\'' == *input_line_pointer)
1027 char * close;
1028 ++input_line_pointer;
1029 close = strchr (input_line_pointer, '\'');
1030 if (close)
1031 *close= ' ';
1032 else
1033 as_bad ("missing end-quote");
1035 if ('\"' == *input_line_pointer)
1037 char * close;
1038 ++input_line_pointer;
1039 close = strchr (input_line_pointer, '\"');
1040 if (close)
1041 *close= ' ';
1042 else
1043 as_bad ("missing end-quote");
1046 switch (type)
1048 case 'H': /* 16-bit */
1049 case 'F': /* 32-bit */
1050 expression (&exp);
1051 emit_expr (&exp, nbytes);
1052 break;
1053 case 'E': /* 32-bit */
1054 case 'D': /* 64-bit */
1055 md_atof (type, tmp, &nbytes);
1056 p = frag_more (nbytes);
1057 memcpy (p, tmp, nbytes);
1058 break;
1059 default:
1060 as_bad ("unsupported DC type");
1061 return;
1064 demand_empty_rest_of_line ();
1068 /* provide minimal support for DS Define Storage */
1069 static void
1070 i370_ds (unused)
1071 int unused;
1073 /* DS 0H or DS 0F or DS 0D */
1074 if ('0' == *input_line_pointer)
1076 int alignment = 0; /* left shift 1<<align */
1077 input_line_pointer ++;
1078 switch (*input_line_pointer++)
1080 case 'H': /* 16-bit */
1081 alignment = 1;
1082 break;
1083 case 'F': /* 32-bit */
1084 alignment = 2;
1085 break;
1086 case 'D': /* 64-bit */
1087 alignment = 3;
1088 break;
1089 default:
1090 as_bad ("unsupported alignment");
1091 return;
1093 frag_align (alignment, 0, 0);
1094 record_alignment (now_seg, alignment);
1096 else
1098 as_bad ("this DS form not yet supported");
1102 /* Solaris pseudo op to change to the .rodata section. */
1103 static void
1104 i370_elf_rdata (sect)
1105 int sect;
1107 char *save_line = input_line_pointer;
1108 static char section[] = ".rodata\n";
1110 /* Just pretend this is .section .rodata */
1111 input_line_pointer = section;
1112 obj_elf_section (sect);
1114 input_line_pointer = save_line;
1117 /* Pseudo op to make file scope bss items */
1118 static void
1119 i370_elf_lcomm(unused)
1120 int unused;
1122 register char *name;
1123 register char c;
1124 register char *p;
1125 offsetT size;
1126 register symbolS *symbolP;
1127 offsetT align;
1128 segT old_sec;
1129 int old_subsec;
1130 char *pfrag;
1131 int align2;
1133 name = input_line_pointer;
1134 c = get_symbol_end ();
1136 /* just after name is now '\0' */
1137 p = input_line_pointer;
1138 *p = c;
1139 SKIP_WHITESPACE ();
1140 if (*input_line_pointer != ',')
1142 as_bad ("Expected comma after symbol-name: rest of line ignored.");
1143 ignore_rest_of_line ();
1144 return;
1147 input_line_pointer++; /* skip ',' */
1148 if ((size = get_absolute_expression ()) < 0)
1150 as_warn (".COMMon length (%ld.) <0! Ignored.", (long) size);
1151 ignore_rest_of_line ();
1152 return;
1155 /* The third argument to .lcomm is the alignment. */
1156 if (*input_line_pointer != ',')
1157 align = 8;
1158 else
1160 ++input_line_pointer;
1161 align = get_absolute_expression ();
1162 if (align <= 0)
1164 as_warn ("ignoring bad alignment");
1165 align = 8;
1169 *p = 0;
1170 symbolP = symbol_find_or_make (name);
1171 *p = c;
1173 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
1175 as_bad ("Ignoring attempt to re-define symbol `%s'.",
1176 S_GET_NAME (symbolP));
1177 ignore_rest_of_line ();
1178 return;
1181 if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
1183 as_bad ("Length of .lcomm \"%s\" is already %ld. Not changed to %ld.",
1184 S_GET_NAME (symbolP),
1185 (long) S_GET_VALUE (symbolP),
1186 (long) size);
1188 ignore_rest_of_line ();
1189 return;
1192 /* allocate_bss: */
1193 old_sec = now_seg;
1194 old_subsec = now_subseg;
1195 if (align)
1197 /* convert to a power of 2 alignment */
1198 for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2)
1200 if (align != 1)
1202 as_bad ("Common alignment not a power of 2");
1203 ignore_rest_of_line ();
1204 return;
1207 else
1208 align2 = 0;
1210 record_alignment (bss_section, align2);
1211 subseg_set (bss_section, 0);
1212 if (align2)
1213 frag_align (align2, 0, 0);
1214 if (S_GET_SEGMENT (symbolP) == bss_section)
1215 symbol_get_frag (symbolP)->fr_symbol = 0;
1216 symbol_set_frag (symbolP, frag_now);
1217 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
1218 (char *) 0);
1219 *pfrag = 0;
1220 S_SET_SIZE (symbolP, size);
1221 S_SET_SEGMENT (symbolP, bss_section);
1222 subseg_set (old_sec, old_subsec);
1223 demand_empty_rest_of_line ();
1226 /* Validate any relocations emitted for -mrelocatable, possibly adding
1227 fixups for word relocations in writable segments, so we can adjust
1228 them at runtime. */
1229 static void
1230 i370_elf_validate_fix (fixp, seg)
1231 fixS *fixp;
1232 segT seg;
1234 if (fixp->fx_done || fixp->fx_pcrel)
1235 return;
1237 switch (shlib)
1239 case SHLIB_NONE:
1240 case SHLIB_PIC:
1241 return;
1243 case SHILB_MRELOCATABLE:
1244 if (fixp->fx_r_type <= BFD_RELOC_UNUSED
1245 && fixp->fx_r_type != BFD_RELOC_16_GOTOFF
1246 && fixp->fx_r_type != BFD_RELOC_HI16_GOTOFF
1247 && fixp->fx_r_type != BFD_RELOC_LO16_GOTOFF
1248 && fixp->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
1249 && fixp->fx_r_type != BFD_RELOC_32_BASEREL
1250 && fixp->fx_r_type != BFD_RELOC_LO16_BASEREL
1251 && fixp->fx_r_type != BFD_RELOC_HI16_BASEREL
1252 && fixp->fx_r_type != BFD_RELOC_HI16_S_BASEREL
1253 && strcmp (segment_name (seg), ".got2") != 0
1254 && strcmp (segment_name (seg), ".dtors") != 0
1255 && strcmp (segment_name (seg), ".ctors") != 0
1256 && strcmp (segment_name (seg), ".fixup") != 0
1257 && strcmp (segment_name (seg), ".stab") != 0
1258 && strcmp (segment_name (seg), ".gcc_except_table") != 0
1259 && strcmp (segment_name (seg), ".ex_shared") != 0)
1261 if ((seg->flags & (SEC_READONLY | SEC_CODE)) != 0
1262 || fixp->fx_r_type != BFD_RELOC_CTOR)
1264 as_bad_where (fixp->fx_file, fixp->fx_line,
1265 "Relocation cannot be done when using -mrelocatable");
1268 return;
1271 #endif /* OBJ_ELF */
1274 #define LITERAL_POOL_SUPPORT
1275 #ifdef LITERAL_POOL_SUPPORT
1276 /* Provide support for literal pools within the text section. */
1277 /* Loosely based on similar code from tc-arm.c */
1279 * We will use four symbols to locate four parts of the literal pool.
1280 * These four sections contain 64,32,16 and 8-bit constants; we use
1281 * four sections so that all memory access can be appropriately aligned.
1282 * That is, we want to avoid mixing these together so that we don't
1283 * waste space padding out to alignments. The four pointers
1284 * longlong_poolP, word_poolP, etc. point to a symbol labeling the
1285 * start of each pool part.
1287 * lit_pool_num increments from zero to infinity and uniquely id's
1288 * -- its used to generate the *_poolP symbol name.
1291 #define MAX_LITERAL_POOL_SIZE 1024
1293 typedef struct literalS
1295 struct expressionS exp;
1296 char * sym_name;
1297 char size; /* 1,2,4 or 8 */
1298 short offset;
1299 } literalT;
1301 literalT literals[MAX_LITERAL_POOL_SIZE];
1302 int next_literal_pool_place = 0; /* Next free entry in the pool */
1304 static symbolS *longlong_poolP = NULL; /* 64-bit pool entries */
1305 static symbolS *word_poolP = NULL; /* 32-bit pool entries */
1306 static symbolS *short_poolP = NULL; /* 16-bit pool entries */
1307 static symbolS *byte_poolP = NULL; /* 8-bit pool entries */
1309 static int lit_pool_num = 1;
1311 /* create a new, empty symbol */
1312 static symbolS *
1313 symbol_make_empty (void)
1315 return symbol_create (FAKE_LABEL_NAME, undefined_section,
1316 (valueT) 0, &zero_address_frag);
1319 /* add an expression to the literal pool */
1320 static void
1321 add_to_lit_pool (expressionS *exx, char *name, int sz)
1323 int lit_count = 0;
1324 int offset_in_pool = 0;
1326 /* start a new pool, if necessary */
1327 if (8 == sz && NULL == longlong_poolP)
1328 longlong_poolP = symbol_make_empty();
1329 else if (4 == sz && NULL == word_poolP)
1330 word_poolP = symbol_make_empty();
1331 else if (2 == sz && NULL == short_poolP)
1332 short_poolP = symbol_make_empty();
1333 else if (1 == sz && NULL == byte_poolP)
1334 byte_poolP = symbol_make_empty();
1336 /* Check if this literal value is already in the pool: */
1337 /* hack alert -- we should probably be checking expressions
1338 * of type O_symbol as well ... */
1339 /* hack alert XXX this is probably(certainly?) broken for O_big,
1340 * which includes 64-bit long-longs ...
1342 while (lit_count < next_literal_pool_place)
1344 if (exx->X_op == O_constant
1345 && literals[lit_count].exp.X_op == exx->X_op
1346 && literals[lit_count].exp.X_add_number == exx->X_add_number
1347 && literals[lit_count].exp.X_unsigned == exx->X_unsigned
1348 && literals[lit_count].size == sz)
1349 break;
1350 else if (literals[lit_count].sym_name
1351 && name
1352 && !strcmp (name, literals[lit_count].sym_name))
1353 break;
1354 if (sz == literals[lit_count].size)
1355 offset_in_pool += sz;
1356 lit_count ++;
1359 if (lit_count == next_literal_pool_place) /* new entry */
1361 if (next_literal_pool_place > MAX_LITERAL_POOL_SIZE)
1363 as_bad("Literal Pool Overflow");
1366 literals[next_literal_pool_place].exp = *exx;
1367 literals[next_literal_pool_place].size = sz;
1368 literals[next_literal_pool_place].offset = offset_in_pool;
1369 if (name)
1371 literals[next_literal_pool_place].sym_name = strdup (name);
1373 else
1375 literals[next_literal_pool_place].sym_name = NULL;
1377 next_literal_pool_place++;
1380 /* ???_poolP points to the begining of the literal pool.
1381 * X_add_number is the offset from the begining of the
1382 * literal pool to this expr minus the location of the most
1383 * recent .using directive. Thus, the grand total value of the
1384 * expression is the distance from .using to the literal.
1386 if (8 == sz)
1387 exx->X_add_symbol = longlong_poolP;
1388 else if (4 == sz)
1389 exx->X_add_symbol = word_poolP;
1390 else if (2 == sz)
1391 exx->X_add_symbol = short_poolP;
1392 else if (1 == sz)
1393 exx->X_add_symbol = byte_poolP;
1394 exx->X_add_number = offset_in_pool;
1395 exx->X_op_symbol = NULL;
1397 /* If the user has set up a base reg in another section,
1398 * use that; otherwise use the text section. */
1399 if (0 < i370_using_other_regno)
1401 i370_make_relative (exx, &i370_using_other_baseaddr);
1403 else
1405 i370_make_relative (exx, &i370_using_text_baseaddr);
1409 /* The symbol setup for the literal pool is done in two steps. First,
1410 * a symbol that represents the start of the literal pool is created,
1411 * above, in the add_to_pool() routine. This sym ???_poolP.
1412 * However, we don't know what fragment its in until a bit later.
1413 * So we defer the frag_now thing, and the symbol name, until .ltorg time
1416 /* Can't use symbol_new here, so have to create a symbol and then at
1417 a later date assign it a value. Thats what these functions do */
1418 static void
1419 symbol_locate (symbolP, name, segment, valu, frag)
1420 symbolS *symbolP;
1421 CONST char *name; /* It is copied, the caller can modify */
1422 segT segment; /* Segment identifier (SEG_<something>) */
1423 valueT valu; /* Symbol value */
1424 fragS *frag; /* Associated fragment */
1426 size_t name_length;
1427 char *preserved_copy_of_name;
1429 name_length = strlen (name) + 1; /* +1 for \0 */
1430 obstack_grow (&notes, name, name_length);
1431 preserved_copy_of_name = obstack_finish (&notes);
1433 S_SET_NAME (symbolP, preserved_copy_of_name);
1435 S_SET_SEGMENT (symbolP, segment);
1436 S_SET_VALUE (symbolP, valu);
1437 symbol_clear_list_pointers(symbolP);
1439 symbol_set_frag (symbolP, frag);
1442 * Link to end of symbol chain.
1445 extern int symbol_table_frozen;
1446 if (symbol_table_frozen)
1447 abort ();
1450 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
1452 obj_symbol_new_hook (symbolP);
1454 #ifdef tc_symbol_new_hook
1455 tc_symbol_new_hook (symbolP);
1456 #endif
1458 #define DEBUG_SYMS
1459 #ifdef DEBUG_SYMS
1460 verify_symbol_chain(symbol_rootP, symbol_lastP);
1461 #endif /* DEBUG_SYMS */
1464 /* i370_addr_offset() will convert operand expressions
1465 * that appear to be absolute into thier base-register
1466 * relative form. These expressions come in two types:
1468 * (1) of the form "* + const" * where "*" means
1469 * relative offset since the last using
1470 * i.e. "*" means ".-using_baseaddr"
1472 * (2) labels, which are never absolute, but are always
1473 * relative to the last "using". Anything with an alpha
1474 * character is considered to be a label (since symbols
1475 * can never be operands), and since we've already handled
1476 * register operands. For example, "BL .L33" branch low
1477 * to .L33 RX form insn frequently terminates for-loops,
1479 static boolean
1480 i370_addr_offset (expressionS *exx)
1482 char *dot, *lab;
1483 int islabel = 0;
1484 int all_digits = 0;
1486 /* search for a label; anything with an alpha char will do */
1487 /* local labels consist of N digits followed by either b or f */
1488 lab = input_line_pointer;
1489 while (*lab && (',' != *lab) && ('(' != *lab))
1491 if (isdigit(*lab))
1493 all_digits = 1;
1495 else if (isalpha(*lab))
1497 if (!all_digits)
1499 islabel = 1;
1500 break;
1502 else if (('f' == *lab) || ('b' == *lab))
1504 islabel = 1;
1505 break;
1507 if (all_digits)
1508 break;
1510 else if ('.' != *lab)
1511 break;
1512 ++lab;
1515 /* See if operand has a * in it */
1516 dot = strchr (input_line_pointer, '*');
1518 if (!dot && !islabel)
1519 return false;
1521 /* replace * with . and let expr munch on it. */
1522 if (dot)
1523 *dot = '.';
1524 expression (exx);
1526 /* OK, now we have to subtract the "using" location */
1527 /* normally branches appear in the text section only... */
1528 if (0 == strncmp (now_seg->name, ".text", 5) || 0 > i370_using_other_regno)
1530 i370_make_relative (exx, &i370_using_text_baseaddr);
1532 else
1534 i370_make_relative (exx, &i370_using_other_baseaddr);
1537 /* put the * back */
1538 if (dot)
1539 *dot = '*';
1541 return true;
1544 /* handle address constants of various sorts */
1545 /* The currently supported types are
1546 * =A(some_symb)
1547 * =V(some_extern)
1548 * =X'deadbeef' hexadecimal
1549 * =F'1234' 32-bit const int
1550 * =H'1234' 16-bit const int
1552 static boolean
1553 i370_addr_cons (expressionS *exp)
1555 char *name;
1556 char *sym_name, delim;
1557 int name_len;
1558 int hex_len=0;
1559 int cons_len=0;
1561 name = input_line_pointer;
1562 sym_name = input_line_pointer;
1563 /* Find the spelling of the operand */
1564 if (name[0] == '=' && isalpha (name[1]))
1566 name = ++input_line_pointer;
1568 else
1570 return false;
1572 switch (name[0])
1574 case 'A':
1575 case 'V':
1576 /* A == address-of */
1577 /* V == extern */
1578 ++input_line_pointer;
1579 expression (exp);
1581 /* we use a simple string name to collapse together
1582 * multiple refrences to the same address literal
1584 name_len = strcspn (sym_name, ", ");
1585 delim = *(sym_name + name_len);
1586 *(sym_name + name_len) = 0x0;
1587 add_to_lit_pool (exp, sym_name, 4);
1588 *(sym_name + name_len) = delim;
1590 break;
1591 case 'H':
1592 case 'F':
1593 case 'X':
1594 case 'E': /* single-precision float point */
1595 case 'D': /* double-precision float point */
1597 /* H == 16-bit fixed-point const; expression must be const */
1598 /* F == fixed-point const; expression must be const */
1599 /* X == fixed-point const; expression must be const */
1600 if ('H' == name[0]) cons_len = 2;
1601 else if ('F' == name[0]) cons_len = 4;
1602 else if ('X' == name[0]) cons_len = -1;
1603 else if ('E' == name[0]) cons_len = 4;
1604 else if ('D' == name[0]) cons_len = 8;
1606 /* extract length, if it is present; hack alert -- assume single-digit
1607 * length */
1608 if ('L' == name[1])
1610 cons_len = name[2] - '0'; /* should work for ascii and ebcdic */
1611 input_line_pointer += 2;
1614 ++input_line_pointer;
1616 /* get rid of pesky quotes */
1617 if ('\'' == *input_line_pointer)
1619 char * close;
1620 ++input_line_pointer;
1621 close = strchr (input_line_pointer, '\'');
1622 if (close)
1623 *close= ' ';
1624 else
1625 as_bad ("missing end-quote");
1627 if ('\"' == *input_line_pointer)
1629 char * close;
1630 ++input_line_pointer;
1631 close = strchr (input_line_pointer, '\"');
1632 if (close)
1633 *close= ' ';
1634 else
1635 as_bad ("missing end-quote");
1637 if (('X' == name[0]) || ('E' == name[0]) || ('D' == name[0]))
1639 char tmp[50];
1640 char *save;
1642 /* The length of hex constants is specified directly with L,
1643 * or implied through the number of hex digits. For example:
1644 * =X'AB' one byte
1645 * =X'abcd' two bytes
1646 * =X'000000AB' four bytes
1647 * =XL4'AB' four bytes, left-padded withn zero
1649 if (('X' == name[0]) && (0 > cons_len))
1651 save = input_line_pointer;
1652 while (*save)
1654 if (isxdigit(*save))
1655 hex_len++;
1656 save++;
1658 cons_len = (hex_len+1) /2;
1660 /* I beleive this works even for =XL8'dada0000beeebaaa'
1661 * which should parse out to X_op == O_big
1662 * Note that floats and doubles get represented as
1663 * 0d3.14159265358979 or 0f 2.7
1665 tmp[0] = '0';
1666 tmp[1] = name[0];
1667 tmp[2] = 0;
1668 strcat (tmp, input_line_pointer);
1669 save = input_line_pointer;
1670 input_line_pointer = tmp;
1671 expression (exp);
1672 input_line_pointer = save + (input_line_pointer-tmp-2);
1674 /* fix up lengths for floats and doubles */
1675 if (O_big == exp->X_op)
1677 exp->X_add_number = cons_len / CHARS_PER_LITTLENUM;
1680 else
1682 expression (exp);
1684 /* O_big occurs when more than 4 bytes worth gets parsed */
1685 if ((exp->X_op != O_constant) && (exp->X_op != O_big))
1687 as_bad ("expression not a constant");
1688 return false;
1690 add_to_lit_pool (exp, 0x0, cons_len);
1691 break;
1693 default:
1694 as_bad ("Unknown/unsupported address literal type");
1695 return false;
1698 return true;
1702 /* Dump the contents of the literal pool that we've accumulated so far.
1703 * This aligns the pool to the size of the largest literal in the pool.
1706 static void
1707 i370_ltorg (ignore)
1708 int ignore;
1710 int litsize;
1711 int lit_count = 0;
1712 int biggest_literal_size = 0;
1713 int biggest_align = 0;
1714 char pool_name[20];
1716 if (strncmp (now_seg->name, ".text", 5))
1718 if (i370_other_section == undefined_section)
1720 as_bad (".ltorg without prior .using in section %s",
1721 now_seg->name);
1723 if (i370_other_section != now_seg)
1725 as_bad (".ltorg in section %s paired to .using in section %s",
1726 now_seg->name, i370_other_section->name);
1729 if (! longlong_poolP
1730 && ! word_poolP
1731 && ! short_poolP
1732 && ! byte_poolP)
1734 /* Nothing to do */
1735 /* as_tsktsk ("Nothing to put in the pool\n"); */
1736 return;
1739 /* find largest literal .. 2 4 or 8 */
1740 lit_count = 0;
1741 while (lit_count < next_literal_pool_place)
1743 if (biggest_literal_size < literals[lit_count].size)
1744 biggest_literal_size = literals[lit_count].size;
1745 lit_count ++;
1747 if (1 == biggest_literal_size) biggest_align = 0;
1748 else if (2 == biggest_literal_size) biggest_align = 1;
1749 else if (4 == biggest_literal_size) biggest_align = 2;
1750 else if (8 == biggest_literal_size) biggest_align = 3;
1751 else as_bad ("bad alignment of %d bytes in literal pool", biggest_literal_size);
1752 if (0 == biggest_align) biggest_align = 1;
1754 /* Align pool for short, word, double word accesses */
1755 frag_align (biggest_align, 0, 0);
1756 record_alignment (now_seg, biggest_align);
1758 /* Note that the gas listing will print only the first five
1759 * entries in the pool .... wonder how to make it print more ...
1761 /* output largest literals first, then the smaller ones. */
1762 for (litsize=8; litsize; litsize /=2)
1764 symbolS *current_poolP = NULL;
1765 switch (litsize)
1767 case 8:
1768 current_poolP = longlong_poolP; break;
1769 case 4:
1770 current_poolP = word_poolP; break;
1771 case 2:
1772 current_poolP = short_poolP; break;
1773 case 1:
1774 current_poolP = byte_poolP; break;
1775 default:
1776 as_bad ("bad literal size\n");
1778 if (NULL == current_poolP)
1779 continue;
1780 sprintf (pool_name, ".LITP%01d%06d", litsize, lit_pool_num);
1781 symbol_locate (current_poolP, pool_name, now_seg,
1782 (valueT) frag_now_fix (), frag_now);
1783 symbol_table_insert (current_poolP);
1785 lit_count = 0;
1786 while (lit_count < next_literal_pool_place)
1788 if (litsize == literals[lit_count].size)
1790 #define EMIT_ADDR_CONS_SYMBOLS
1791 #ifdef EMIT_ADDR_CONS_SYMBOLS
1792 /* create a bogus symbol, add it to the pool ...
1793 * For the most part, I think this is a useless excercise,
1794 * except that having these symbol names in the objects
1795 * is vaguely useful for debugging ...
1797 if (literals[lit_count].sym_name)
1799 symbolS * symP = symbol_make_empty();
1800 symbol_locate (symP, literals[lit_count].sym_name, now_seg,
1801 (valueT) frag_now_fix (), frag_now);
1802 symbol_table_insert (symP);
1804 #endif /* EMIT_ADDR_CONS_SYMBOLS */
1806 emit_expr (&(literals[lit_count].exp), literals[lit_count].size);
1808 lit_count ++;
1812 next_literal_pool_place = 0;
1813 longlong_poolP = NULL;
1814 word_poolP = NULL;
1815 short_poolP = NULL;
1816 byte_poolP = NULL;
1817 lit_pool_num++;
1820 #endif /* LITERAL_POOL_SUPPORT */
1823 /* add support for the HLASM-like USING directive to indicate
1824 * the base register to use ... we don't support the full
1825 * hlasm semantics for this ... we merely pluck a base address
1826 * and a register number out. We print a warning if using is
1827 * called multiple times. I suppose we should check to see
1828 * if the regno is valid ...
1830 static void
1831 i370_using (ignore)
1832 int ignore;
1834 expressionS ex, baseaddr;
1835 int iregno;
1836 char *star;
1838 /* if "*" appears in a using, it means "." */
1839 /* replace it with "." so that expr doesn't get confused. */
1840 star = strchr (input_line_pointer, '*');
1841 if (star)
1842 *star = '.';
1844 /* the first arg to using will usually be ".", but it can
1845 * be a more complex exprsssion too ... */
1846 expression (&baseaddr);
1847 if (star)
1848 *star = '*';
1849 if (O_constant != baseaddr.X_op
1850 && O_symbol != baseaddr.X_op
1851 && O_uminus != baseaddr.X_op)
1853 as_bad (".using: base address expression illegal or too complex");
1856 if (*input_line_pointer != '\0') ++input_line_pointer;
1858 /* the second arg to using had better be a register */
1859 register_name (&ex);
1860 demand_empty_rest_of_line ();
1861 iregno = ex.X_add_number;
1863 if (0 == strncmp (now_seg->name, ".text", 5))
1865 i370_using_text_baseaddr = baseaddr;
1866 i370_using_text_regno = iregno;
1868 else
1870 i370_using_other_baseaddr = baseaddr;
1871 i370_using_other_regno = iregno;
1872 i370_other_section = now_seg;
1876 static void
1877 i370_drop (ignore)
1878 int ignore;
1880 expressionS ex;
1881 int iregno;
1883 register_name (&ex);
1884 demand_empty_rest_of_line ();
1885 iregno = ex.X_add_number;
1887 if (0 == strncmp (now_seg->name, ".text", 5))
1889 if (iregno != i370_using_text_regno)
1891 as_bad ("droping register %d in section %s does not match using register %d",
1892 iregno, now_seg->name, i370_using_text_regno);
1894 i370_using_text_regno = -1;
1895 i370_using_text_baseaddr.X_op = O_absent;
1897 else
1899 if (iregno != i370_using_other_regno)
1901 as_bad ("droping register %d in section %s does not match using register %d",
1902 iregno, now_seg->name, i370_using_other_regno);
1904 if (i370_other_section != now_seg)
1906 as_bad ("droping register %d in section %s previously used in section %s",
1907 iregno, now_seg->name, i370_other_section->name);
1909 i370_using_other_regno = -1;
1910 i370_using_other_baseaddr.X_op = O_absent;
1911 i370_other_section = undefined_section;
1915 /* Make the first argument an address-relative expression
1916 * by subtracting the second argument.
1918 static void
1919 i370_make_relative (expressionS *exx, expressionS *baseaddr)
1922 if (O_constant == baseaddr->X_op)
1924 exx->X_op = O_symbol;
1925 exx->X_add_number -= baseaddr->X_add_number;
1927 else if (O_symbol == baseaddr->X_op)
1929 exx->X_op = O_subtract;
1930 exx->X_op_symbol = baseaddr->X_add_symbol;
1931 exx->X_add_number -= baseaddr->X_add_number;
1933 else if (O_uminus == baseaddr->X_op)
1935 exx->X_op = O_add;
1936 exx->X_op_symbol = baseaddr->X_add_symbol;
1937 exx->X_add_number += baseaddr->X_add_number;
1939 else
1941 as_bad ("Missing or bad .using directive");
1945 /* We need to keep a list of fixups. We can't simply generate them as
1946 we go, because that would require us to first create the frag, and
1947 that would screw up references to ``.''. */
1949 struct i370_fixup
1951 expressionS exp;
1952 int opindex;
1953 bfd_reloc_code_real_type reloc;
1956 #define MAX_INSN_FIXUPS (5)
1958 /* This routine is called for each instruction to be assembled. */
1960 void
1961 md_assemble (str)
1962 char *str;
1964 char *s, *opcode_str;
1965 const struct i370_opcode *opcode;
1966 i370_insn_t insn;
1967 const unsigned char *opindex_ptr;
1968 int have_optional_index, have_optional_basereg, have_optional_reg;
1969 int skip_optional_index, skip_optional_basereg, skip_optional_reg;
1970 int use_text=0, use_other=0;
1971 int off_by_one;
1972 struct i370_fixup fixups[MAX_INSN_FIXUPS];
1973 int fc;
1974 char *f;
1975 int i;
1976 #ifdef OBJ_ELF
1977 bfd_reloc_code_real_type reloc;
1978 #endif
1980 /* Get the opcode. */
1981 for (s = str; *s != '\0' && ! isspace (*s); s++)
1983 if (*s != '\0')
1984 *s++ = '\0';
1985 opcode_str = str;
1987 /* Look up the opcode in the hash table. */
1988 opcode = (const struct i370_opcode *) hash_find (i370_hash, str);
1989 if (opcode == (const struct i370_opcode *) NULL)
1991 const struct i370_macro *macro;
1993 assert (i370_macro_hash);
1994 macro = (const struct i370_macro *) hash_find (i370_macro_hash, str);
1995 if (macro == (const struct i370_macro *) NULL)
1996 as_bad ("Unrecognized opcode: `%s'", str);
1997 else
1998 i370_macro (s, macro);
2000 return;
2003 insn = opcode->opcode;
2005 str = s;
2006 while (isspace (*str))
2007 ++str;
2009 /* I370 operands are either expressions or address constants.
2010 Many operand types are optional. The optional operands
2011 are always surrounded by parens, and are used to denote the base
2012 register ... e.g. "A R1, D2" or "A R1, D2(,B2) as opposed to
2013 the fully-formed "A R1, D2(X2,B2)". Note also the = sign,
2014 such as A R1,=A(i) where the address-of operator =A implies
2015 use of both a base register, and a missing index register.
2017 So, before we start seriously parsing the operands, we check
2018 to see if we have an optional operand, and, if we do, we count
2019 the number of commas to see which operand should be omitted. */
2021 have_optional_index = have_optional_basereg = have_optional_reg = 0;
2022 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
2024 const struct i370_operand *operand;
2025 operand = &i370_operands[*opindex_ptr];
2026 if ((operand->flags & I370_OPERAND_INDEX) != 0)
2027 have_optional_index = 1;
2028 if ((operand->flags & I370_OPERAND_BASE) != 0)
2029 have_optional_basereg = 1;
2030 if ((operand->flags & I370_OPERAND_OPTIONAL) != 0)
2031 have_optional_reg = 1;
2034 skip_optional_index = skip_optional_basereg = skip_optional_reg = 0;
2035 if (have_optional_index || have_optional_basereg)
2037 unsigned int opcount, nwanted;
2039 /* There is an optional operand. Count the number of
2040 commas and open-parens in the input line. */
2041 if (*str == '\0')
2042 opcount = 0;
2043 else
2045 opcount = 1;
2046 s = str;
2047 while ((s = strpbrk (s, ",(=")) != (char *) NULL)
2049 ++opcount;
2050 ++s;
2051 if (',' == *s) ++s; /* avoid counting things like (, */
2052 if ('=' == *s) { ++s; --opcount; }
2056 /* If there are fewer operands in the line then are called
2057 for by the instruction, we want to skip the optional
2058 operand. */
2059 nwanted = strlen (opcode->operands);
2060 if (have_optional_index)
2062 if (opcount < nwanted)
2063 skip_optional_index = 1;
2064 if (have_optional_basereg && ((opcount+1) < nwanted))
2065 skip_optional_basereg = 1;
2066 if (have_optional_reg && ((opcount+1) < nwanted))
2067 skip_optional_reg = 1;
2069 else
2071 if (have_optional_basereg && (opcount < nwanted))
2072 skip_optional_basereg = 1;
2073 if (have_optional_reg && (opcount < nwanted))
2074 skip_optional_reg = 1;
2078 /* Perform some off-by-one hacks on the length field of certain instructions.
2079 * Its such a shame to have to do this, but the problem is that HLASM got
2080 * defined so that the lengths differ by one from the actual machine instructions.
2081 * this code should probably be moved to a special inster-operand routine.
2082 * Sigh. Affected instructions are Compare Logical, Move and Exclusive OR
2083 * hack alert -- aren't *all* SS instructions affected ??
2085 off_by_one = 0;
2086 if (0 == strcasecmp ("CLC", opcode->name)
2087 || 0 == strcasecmp ("ED", opcode->name)
2088 || 0 == strcasecmp ("EDMK", opcode->name)
2089 || 0 == strcasecmp ("MVC", opcode->name)
2090 || 0 == strcasecmp ("MVCIN", opcode->name)
2091 || 0 == strcasecmp ("MVN", opcode->name)
2092 || 0 == strcasecmp ("MVZ", opcode->name)
2093 || 0 == strcasecmp ("NC", opcode->name)
2094 || 0 == strcasecmp ("OC", opcode->name)
2095 || 0 == strcasecmp ("XC", opcode->name))
2096 off_by_one = 1;
2098 /* Gather the operands. */
2099 fc = 0;
2100 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
2102 const struct i370_operand *operand;
2103 const char *errmsg;
2104 char *hold;
2105 expressionS ex;
2107 operand = &i370_operands[*opindex_ptr];
2108 errmsg = NULL;
2110 /* If this is an index operand, and we are skipping it,
2111 just insert a zero. */
2112 if (skip_optional_index &&
2113 ((operand->flags & I370_OPERAND_INDEX) != 0))
2115 insn = i370_insert_operand (insn, operand, 0);
2116 continue;
2119 /* If this is the base operand, and we are skipping it,
2120 just insert the current using basreg. */
2121 if (skip_optional_basereg &&
2122 ((operand->flags & I370_OPERAND_BASE) != 0))
2124 int basereg = -1;
2125 if (use_text)
2127 if (0 == strncmp (now_seg->name, ".text", 5)
2128 || 0 > i370_using_other_regno)
2130 basereg = i370_using_text_regno;
2132 else
2134 basereg = i370_using_other_regno;
2137 else if (use_other)
2139 if (0 > i370_using_other_regno)
2141 basereg = i370_using_text_regno;
2143 else
2145 basereg = i370_using_other_regno;
2148 if (0 > basereg)
2150 as_bad ("not using any base register");
2152 insn = i370_insert_operand (insn, operand, basereg);
2153 continue;
2156 /* If this is an optional operand, and we are skipping it,
2157 Use zero (since a non-zero value would denote a register) */
2158 if (skip_optional_reg
2159 && ((operand->flags & I370_OPERAND_OPTIONAL) != 0))
2161 insn = i370_insert_operand (insn, operand, 0);
2162 continue;
2165 /* Gather the operand. */
2166 hold = input_line_pointer;
2167 input_line_pointer = str;
2169 /* register names are only allowed where there are registers ... */
2170 if ((operand->flags & I370_OPERAND_GPR) != 0)
2172 /* quickie hack to get past things like (,r13) */
2173 if (skip_optional_index && (',' == *input_line_pointer))
2175 *input_line_pointer = ' ';
2176 input_line_pointer ++;
2178 if (! register_name (&ex))
2180 as_bad ("expecting a register for operand %d",
2181 opindex_ptr - opcode->operands + 1);
2185 /* check for a address constant expression */
2186 /* We will put PSW-relative addresses in the text section,
2187 * and adress literals in the .data (or other) section. */
2188 else if (i370_addr_cons (&ex))
2189 use_other=1;
2190 else if (i370_addr_offset (&ex))
2191 use_text=1;
2192 else expression (&ex);
2194 str = input_line_pointer;
2195 input_line_pointer = hold;
2197 /* perform some off-by-one hacks on the length field of certain instructions.
2198 * Its such a shame to have to do this, but the problem is that HLASM got
2199 * defined so that the programmer specifies a length that is one greater
2200 * than what the machine instruction wants.
2201 * Sigh.
2203 if (off_by_one && (0 == strcasecmp ("SS L", operand->name)))
2205 ex.X_add_number --;
2208 if (ex.X_op == O_illegal)
2209 as_bad ("illegal operand");
2210 else if (ex.X_op == O_absent)
2211 as_bad ("missing operand");
2212 else if (ex.X_op == O_register)
2214 insn = i370_insert_operand (insn, operand, ex.X_add_number);
2216 else if (ex.X_op == O_constant)
2218 #ifdef OBJ_ELF
2219 /* Allow @HA, @L, @H on constants.
2220 * Well actually, no we don't; there really don't make sense
2221 * (at least not to me) for the i370. However, this code is
2222 * left here for any dubious future expansion reasons ... */
2223 char *orig_str = str;
2225 if ((reloc = i370_elf_suffix (&str, &ex)) != BFD_RELOC_UNUSED)
2226 switch (reloc)
2228 default:
2229 str = orig_str;
2230 break;
2232 case BFD_RELOC_LO16:
2233 /* X_unsigned is the default, so if the user has done
2234 something which cleared it, we always produce a
2235 signed value. */
2236 ex.X_add_number = (((ex.X_add_number & 0xffff)
2237 ^ 0x8000)
2238 - 0x8000);
2239 break;
2241 case BFD_RELOC_HI16:
2242 ex.X_add_number = (ex.X_add_number >> 16) & 0xffff;
2243 break;
2245 case BFD_RELOC_HI16_S:
2246 ex.X_add_number = (((ex.X_add_number >> 16) & 0xffff)
2247 + ((ex.X_add_number >> 15) & 1));
2248 break;
2250 #endif
2251 insn = i370_insert_operand (insn, operand, ex.X_add_number);
2253 #ifdef OBJ_ELF
2254 else if ((reloc = i370_elf_suffix (&str, &ex)) != BFD_RELOC_UNUSED)
2256 as_tsktsk ("md_assemble(): suffixed relocations not supported\n");
2258 /* We need to generate a fixup for this expression. */
2259 if (fc >= MAX_INSN_FIXUPS)
2260 as_fatal ("too many fixups");
2261 fixups[fc].exp = ex;
2262 fixups[fc].opindex = 0;
2263 fixups[fc].reloc = reloc;
2264 ++fc;
2266 #endif /* OBJ_ELF */
2268 else
2270 /* We need to generate a fixup for this expression. */
2271 /* Typically, the expression will just be a symbol ...
2272 * printf ("insn %s needs fixup for %s \n",
2273 * opcode->name, ex.X_add_symbol->bsym->name);
2276 if (fc >= MAX_INSN_FIXUPS)
2277 as_fatal ("too many fixups");
2278 fixups[fc].exp = ex;
2279 fixups[fc].opindex = *opindex_ptr;
2280 fixups[fc].reloc = BFD_RELOC_UNUSED;
2281 ++fc;
2284 /* skip over delimiter (close paren, or comma) */
2285 if ((')' == *str) && (',' == *(str+1)))
2286 ++str;
2287 if (*str != '\0')
2288 ++str;
2291 while (isspace (*str))
2292 ++str;
2294 if (*str != '\0')
2295 as_bad ("junk at end of line: `%s'", str);
2297 /* Write out the instruction. */
2298 f = frag_more (opcode->len);
2299 if (4 >= opcode->len)
2301 md_number_to_chars (f, insn.i[0], opcode->len);
2303 else
2305 md_number_to_chars (f, insn.i[0], 4);
2306 if (6 == opcode->len)
2308 md_number_to_chars ((f+4), ((insn.i[1])>>16), 2);
2310 else
2312 /* not used --- don't have any 8 byte instructions */
2313 as_bad ("Internal Error: bad instruction length");
2314 md_number_to_chars ((f+4), insn.i[1], opcode->len -4);
2318 /* Create any fixups. At this point we do not use a
2319 bfd_reloc_code_real_type, but instead just use the
2320 BFD_RELOC_UNUSED plus the operand index. This lets us easily
2321 handle fixups for any operand type, although that is admittedly
2322 not a very exciting feature. We pick a BFD reloc type in
2323 md_apply_fix. */
2324 for (i = 0; i < fc; i++)
2326 const struct i370_operand *operand;
2328 operand = &i370_operands[fixups[i].opindex];
2329 if (fixups[i].reloc != BFD_RELOC_UNUSED)
2331 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
2332 int size;
2333 fixS *fixP;
2335 if (!reloc_howto)
2336 abort ();
2338 size = bfd_get_reloc_size (reloc_howto);
2340 if (size < 1 || size > 4)
2341 abort ();
2343 printf (" gwana doo fixup %d \n", i);
2344 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal, size,
2345 &fixups[i].exp, reloc_howto->pc_relative,
2346 fixups[i].reloc);
2348 /* Turn off complaints that the addend is too large for things like
2349 foo+100000@ha. */
2350 switch (fixups[i].reloc)
2352 case BFD_RELOC_16_GOTOFF:
2353 case BFD_RELOC_LO16:
2354 case BFD_RELOC_HI16:
2355 case BFD_RELOC_HI16_S:
2356 fixP->fx_no_overflow = 1;
2357 break;
2358 default:
2359 break;
2362 else
2364 fix_new_exp (frag_now, f - frag_now->fr_literal, opcode->len,
2365 &fixups[i].exp,
2366 (operand->flags & I370_OPERAND_RELATIVE) != 0,
2367 ((bfd_reloc_code_real_type)
2368 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
2373 /* Handle a macro. Gather all the operands, transform them as
2374 described by the macro, and call md_assemble recursively. All the
2375 operands are separated by commas; we don't accept parentheses
2376 around operands here. */
2378 static void
2379 i370_macro (str, macro)
2380 char *str;
2381 const struct i370_macro *macro;
2383 char *operands[10];
2384 unsigned int count;
2385 char *s;
2386 unsigned int len;
2387 const char *format;
2388 int arg;
2389 char *send;
2390 char *complete;
2392 /* Gather the users operands into the operands array. */
2393 count = 0;
2394 s = str;
2395 while (1)
2397 if (count >= sizeof operands / sizeof operands[0])
2398 break;
2399 operands[count++] = s;
2400 s = strchr (s, ',');
2401 if (s == (char *) NULL)
2402 break;
2403 *s++ = '\0';
2406 if (count != macro->operands)
2408 as_bad ("wrong number of operands");
2409 return;
2412 /* Work out how large the string must be (the size is unbounded
2413 because it includes user input). */
2414 len = 0;
2415 format = macro->format;
2416 while (*format != '\0')
2418 if (*format != '%')
2420 ++len;
2421 ++format;
2423 else
2425 arg = strtol (format + 1, &send, 10);
2426 know (send != format && arg >= 0 && arg < count);
2427 len += strlen (operands[arg]);
2428 format = send;
2432 /* Put the string together. */
2433 complete = s = (char *) alloca (len + 1);
2434 format = macro->format;
2435 while (*format != '\0')
2437 if (*format != '%')
2438 *s++ = *format++;
2439 else
2441 arg = strtol (format + 1, &send, 10);
2442 strcpy (s, operands[arg]);
2443 s += strlen (s);
2444 format = send;
2447 *s = '\0';
2449 /* Assemble the constructed instruction. */
2450 md_assemble (complete);
2453 #ifdef OBJ_ELF
2454 /* For ELF, add support for SHF_EXCLUDE and SHT_ORDERED */
2457 i370_section_letter (letter, ptr_msg)
2458 int letter;
2459 char **ptr_msg;
2461 if (letter == 'e')
2462 return SHF_EXCLUDE;
2464 *ptr_msg = "Bad .section directive: want a,w,x,e in string";
2465 return 0;
2469 i370_section_word (str, len)
2470 char *str;
2471 size_t len;
2473 if (len == 7 && strncmp (str, "exclude", 7) == 0)
2474 return SHF_EXCLUDE;
2476 return -1;
2480 i370_section_type (str, len)
2481 char *str;
2482 size_t len;
2484 if (len == 7 && strncmp (str, "ordered", 7) == 0)
2485 return SHT_ORDERED;
2487 return -1;
2491 i370_section_flags (flags, attr, type)
2492 int flags;
2493 int attr;
2494 int type;
2496 if (type == SHT_ORDERED)
2497 flags |= SEC_ALLOC | SEC_LOAD | SEC_SORT_ENTRIES;
2499 if (attr & SHF_EXCLUDE)
2500 flags |= SEC_EXCLUDE;
2502 return flags;
2504 #endif /* OBJ_ELF */
2507 /* Pseudo-op handling. */
2509 /* The .byte pseudo-op. This is similar to the normal .byte
2510 pseudo-op, but it can also take a single ASCII string. */
2512 static void
2513 i370_byte (ignore)
2514 int ignore;
2516 if (*input_line_pointer != '\"')
2518 cons (1);
2519 return;
2522 /* Gather characters. A real double quote is doubled. Unusual
2523 characters are not permitted. */
2524 ++input_line_pointer;
2525 while (1)
2527 char c;
2529 c = *input_line_pointer++;
2531 if (c == '\"')
2533 if (*input_line_pointer != '\"')
2534 break;
2535 ++input_line_pointer;
2538 FRAG_APPEND_1_CHAR (c);
2541 demand_empty_rest_of_line ();
2544 /* The .tc pseudo-op. This is used when generating XCOFF and ELF.
2545 This takes two or more arguments.
2547 When generating XCOFF output, the first argument is the name to
2548 give to this location in the toc; this will be a symbol with class
2549 TC. The rest of the arguments are 4 byte values to actually put at
2550 this location in the TOC; often there is just one more argument, a
2551 relocateable symbol reference.
2553 When not generating XCOFF output, the arguments are the same, but
2554 the first argument is simply ignored. */
2556 static void
2557 i370_tc (ignore)
2558 int ignore;
2561 /* Skip the TOC symbol name. */
2562 while (is_part_of_name (*input_line_pointer)
2563 || *input_line_pointer == '['
2564 || *input_line_pointer == ']'
2565 || *input_line_pointer == '{'
2566 || *input_line_pointer == '}')
2567 ++input_line_pointer;
2569 /* Align to a four byte boundary. */
2570 frag_align (2, 0, 0);
2571 record_alignment (now_seg, 2);
2573 if (*input_line_pointer != ',')
2574 demand_empty_rest_of_line ();
2575 else
2577 ++input_line_pointer;
2578 cons (4);
2582 /* Turn a string in input_line_pointer into a floating point constant
2583 of type TYPE, and store the appropriate bytes in *LITP. The number
2584 of LITTLENUMS emitted is stored in *SIZEP. An error message is
2585 returned, or NULL on OK. */
2587 char *
2588 md_atof (type, litp, sizep)
2589 int type;
2590 char *litp;
2591 int *sizep;
2593 int prec;
2594 LITTLENUM_TYPE words[4];
2595 char *t;
2596 int i;
2598 switch (type)
2600 case 'f':
2601 case 'E':
2602 type = 'f';
2603 prec = 2;
2604 break;
2606 case 'd':
2607 case 'D':
2608 type = 'd';
2609 prec = 4;
2610 break;
2612 default:
2613 *sizep = 0;
2614 return "bad call to md_atof";
2617 /* 360/370/390 have two float formats: an old, funky 360 single-precision
2618 * format, and the ieee format. Support only the ieee format. */
2619 t = atof_ieee (input_line_pointer, type, words);
2620 if (t)
2621 input_line_pointer = t;
2623 *sizep = prec * 2;
2625 for (i = 0; i < prec; i++)
2627 md_number_to_chars (litp, (valueT) words[i], 2);
2628 litp += 2;
2631 return NULL;
2634 /* Write a value out to the object file, using the appropriate
2635 endianness. */
2637 void
2638 md_number_to_chars (buf, val, n)
2639 char *buf;
2640 valueT val;
2641 int n;
2643 number_to_chars_bigendian (buf, val, n);
2646 /* Align a section (I don't know why this is machine dependent). */
2648 valueT
2649 md_section_align (seg, addr)
2650 asection *seg;
2651 valueT addr;
2653 int align = bfd_get_section_alignment (stdoutput, seg);
2655 return (addr + (1 << align) - 1) & (-1 << align);
2658 /* We don't have any form of relaxing. */
2661 md_estimate_size_before_relax (fragp, seg)
2662 fragS *fragp;
2663 asection *seg;
2665 abort ();
2666 return 0;
2669 /* Convert a machine dependent frag. We never generate these. */
2671 void
2672 md_convert_frag (abfd, sec, fragp)
2673 bfd *abfd;
2674 asection *sec;
2675 fragS *fragp;
2677 abort ();
2680 /* We have no need to default values of symbols. */
2682 symbolS *
2683 md_undefined_symbol (name)
2684 char *name;
2686 return 0;
2689 /* Functions concerning relocs. */
2691 /* The location from which a PC relative jump should be calculated,
2692 given a PC relative reloc. */
2694 long
2695 md_pcrel_from_section (fixp, sec)
2696 fixS *fixp;
2697 segT sec;
2699 return fixp->fx_frag->fr_address + fixp->fx_where;
2702 /* Apply a fixup to the object code. This is called for all the
2703 fixups we generated by the call to fix_new_exp, above. In the call
2704 above we used a reloc code which was the largest legal reloc code
2705 plus the operand index. Here we undo that to recover the operand
2706 index. At this point all symbol values should be fully resolved,
2707 and we attempt to completely resolve the reloc. If we can not do
2708 that, we determine the correct reloc code and put it back in the
2709 fixup.
2711 See gas/cgen.c for more sample code and explanations of what's
2712 going on here ...
2716 md_apply_fix3 (fixp, valuep, seg)
2717 fixS *fixp;
2718 valueT *valuep;
2719 segT seg;
2721 valueT value;
2723 value = *valuep;
2724 if (fixp->fx_addsy != NULL)
2726 /* Notes:
2727 Branches to labels will come in here with fixp->fx_pcrel set to 1
2728 and fixp->fx_subsy not null, and holding the value of the base
2729 (i.e. the value of the .using). These we want to ignore.
2731 'Strong' and 'weak' symbols will come in here with
2732 fixp->fx_pcrel==0, fixp->fx_addsy defined, and
2733 *valuep holding the value of the symbol.
2735 'Strong' symbols will have S_GET_VALUE(fx_addsy) equal to zero,
2736 whereas 'weak' symbols will have S_GET_VALUE(fx_addsy) set to the
2737 symbol value (usually).
2739 We want to subtract S_GET_VALUE(fx_addsy) if it set, and
2740 for all practical purposes, do a fixup with value zero. This
2741 is because the linker/loader, at a later time, will do this
2742 fixup with the correct value. If we fixup now with a value,
2743 it will get double-fixed, leading to garbage.
2745 Note that subsy will also be set for strong/weak symbols
2746 when the user program was compiled with -g. In that case,
2747 subsy will hold the base address (i.e. the .using address).
2750 if (fixp->fx_addsy->sy_used_in_reloc
2751 && S_GET_SEGMENT (fixp->fx_addsy) != absolute_section
2752 && S_GET_SEGMENT (fixp->fx_addsy) != undefined_section
2753 && ! bfd_is_com_section (S_GET_SEGMENT (fixp->fx_addsy)))
2754 value -= S_GET_VALUE (fixp->fx_addsy);
2756 #ifdef DEBUG
2757 printf ("\nmd_apply_fix3: symbol %s at 0x%x (%s:%d) val=0x%x addend=0x%x\n",
2758 S_GET_NAME (fixp->fx_addsy),
2759 fixp->fx_frag->fr_address + fixp->fx_where,
2760 fixp->fx_file, fixp->fx_line,
2761 S_GET_VALUE (fixp->fx_addsy), value);
2762 #endif
2764 else
2766 fixp->fx_done = 1;
2767 return 1;
2770 /* Apply fixups to operands. Note that there should be no relocations
2771 for any operands, since no instruction ever takes an operand
2772 that requires reloc. */
2773 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
2775 int opindex;
2776 const struct i370_operand *operand;
2777 char *where;
2778 i370_insn_t insn;
2780 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
2782 operand = &i370_operands[opindex];
2784 #ifdef DEBUG
2785 printf ("\nmd_apply_fix3: fixup operand %s at 0x%x in %s:%d addend=0x%x\n",
2786 operand->name,
2787 fixp->fx_frag->fr_address + fixp->fx_where,
2788 fixp->fx_file, fixp->fx_line,
2789 value);
2790 #endif
2791 /* Fetch the instruction, insert the fully resolved operand
2792 value, and stuff the instruction back again.
2793 fisxp->fx_size is the length of the instruction. */
2794 where = fixp->fx_frag->fr_literal + fixp->fx_where;
2795 insn.i[0] = bfd_getb32 ((unsigned char *) where);
2796 if (6 <= fixp->fx_size)
2797 { /* deal with 48-bit insn's */
2798 insn.i[1] = bfd_getb32 (((unsigned char *) where)+4);
2800 insn = i370_insert_operand (insn, operand, (offsetT) value);
2801 bfd_putb32 ((bfd_vma) insn.i[0], (unsigned char *) where);
2802 if (6 <= fixp->fx_size)
2803 { /* deal with 48-bit insn's */
2804 bfd_putb32 ((bfd_vma) insn.i[1], (((unsigned char *) where)+4));
2807 /* we are done, right? right !! */
2808 fixp->fx_done = 1;
2809 if (fixp->fx_done)
2811 /* Nothing else to do here. */
2812 return 1;
2815 /* Determine a BFD reloc value based on the operand information.
2816 We are only prepared to turn a few of the operands into
2817 relocs. In fact, we support *zero* operand relocations ...
2818 Why? Because we are not expecting the compiler to generate
2819 any operands that need relocation. Due to the 12-bit naturew of
2820 i370 addressing, this would be unusual. */
2821 #if 0
2822 if ((operand->flags & I370_OPERAND_RELATIVE) != 0
2823 && operand->bits == 12
2824 && operand->shift == 0)
2825 fixp->fx_r_type = BFD_RELOC_I370_D12;
2826 else
2827 #endif
2829 char *sfile;
2830 unsigned int sline;
2832 /* Use expr_symbol_where to see if this is an expression
2833 symbol. */
2834 if (expr_symbol_where (fixp->fx_addsy, &sfile, &sline))
2835 as_bad_where (fixp->fx_file, fixp->fx_line,
2836 "unresolved expression that must be resolved");
2837 else
2838 as_bad_where (fixp->fx_file, fixp->fx_line,
2839 "unsupported relocation type");
2840 fixp->fx_done = 1;
2841 return 1;
2844 else
2846 /* We branch to here if the fixup is not to a symbol that
2847 * appears in an instruction operand, but is rather some
2848 * declared storage.
2850 #ifdef OBJ_ELF
2851 i370_elf_validate_fix (fixp, seg);
2852 #endif
2853 #ifdef DEBUG
2854 printf ("md_apply_fix3: reloc case %d in segment %s %s:%d\n",
2855 fixp->fx_r_type, segment_name (seg), fixp->fx_file, fixp->fx_line);
2856 printf ("\tcurrent fixup value is 0x%x \n", value);
2857 #endif
2858 switch (fixp->fx_r_type)
2860 case BFD_RELOC_32:
2861 case BFD_RELOC_CTOR:
2862 if (fixp->fx_pcrel)
2863 fixp->fx_r_type = BFD_RELOC_32_PCREL;
2864 /* fall through */
2866 case BFD_RELOC_RVA:
2867 case BFD_RELOC_32_PCREL:
2868 case BFD_RELOC_32_BASEREL:
2869 #ifdef DEBUG
2870 printf ("\t32 bit relocation at 0x%x\n",
2871 fixp->fx_frag->fr_address + fixp->fx_where);
2872 #endif
2873 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
2874 value, 4);
2875 break;
2877 case BFD_RELOC_LO16:
2878 case BFD_RELOC_16:
2879 if (fixp->fx_pcrel)
2880 as_bad_where (fixp->fx_file, fixp->fx_line,
2881 "cannot emit PC relative %s relocation%s%s",
2882 bfd_get_reloc_code_name (fixp->fx_r_type),
2883 fixp->fx_addsy != NULL ? " against " : "",
2884 (fixp->fx_addsy != NULL
2885 ? S_GET_NAME (fixp->fx_addsy)
2886 : ""));
2888 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
2889 value, 2);
2890 break;
2892 /* This case happens when you write, for example,
2893 lis %r3,(L1-L2)@ha
2894 where L1 and L2 are defined later. */
2895 case BFD_RELOC_HI16:
2896 if (fixp->fx_pcrel)
2897 abort ();
2898 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
2899 value >> 16, 2);
2900 break;
2901 case BFD_RELOC_HI16_S:
2902 if (fixp->fx_pcrel)
2903 abort ();
2904 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
2905 (value + 0x8000) >> 16, 2);
2906 break;
2908 case BFD_RELOC_8:
2909 if (fixp->fx_pcrel)
2910 abort ();
2912 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
2913 value, 1);
2914 break;
2916 default:
2917 fprintf (stderr,
2918 "Gas failure, reloc value %d\n", fixp->fx_r_type);
2919 fflush(stderr);
2920 abort ();
2924 fixp->fx_addnumber = value;
2926 return 1;
2929 /* Generate a reloc for a fixup. */
2931 arelent *
2932 tc_gen_reloc (seg, fixp)
2933 asection *seg;
2934 fixS *fixp;
2936 arelent *reloc;
2938 reloc = (arelent *) xmalloc (sizeof (arelent));
2940 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2941 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2942 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2943 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2944 if (reloc->howto == (reloc_howto_type *) NULL)
2946 as_bad_where (fixp->fx_file, fixp->fx_line,
2947 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
2948 return NULL;
2950 reloc->addend = fixp->fx_addnumber;
2952 #ifdef DEBUG
2953 printf ("\ngen_reloc(): sym %s (%s:%d) at addr 0x%x addend=0x%x\n",
2954 fixp->fx_addsy->bsym->name,
2955 fixp->fx_file, fixp->fx_line,
2956 reloc->address, reloc->addend);
2957 #endif
2959 return reloc;