Remove duplicate definitions of the md_atof() function
[platform/upstream/binutils.git] / gas / config / tc-hppa.c
1 /* tc-hppa.c -- Assemble for the PA
2    Copyright 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to the Free
19    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20    02110-1301, USA.  */
21
22 /* HP PA-RISC support was contributed by the Center for Software Science
23    at the University of Utah.  */
24
25 #include "as.h"
26 #include "safe-ctype.h"
27 #include "subsegs.h"
28 #include "dw2gencfi.h"
29
30 #include "bfd/libhppa.h"
31
32 /* Be careful, this file includes data *declarations*.  */
33 #include "opcode/hppa.h"
34
35 #if defined (OBJ_ELF) && defined (OBJ_SOM)
36 error only one of OBJ_ELF and OBJ_SOM can be defined
37 #endif
38
39 /* If we are using ELF, then we probably can support dwarf2 debug
40    records.  Furthermore, if we are supporting dwarf2 debug records,
41    then we want to use the assembler support for compact line numbers.  */
42 #ifdef OBJ_ELF
43 #include "dwarf2dbg.h"
44
45 /* A "convenient" place to put object file dependencies which do
46    not need to be seen outside of tc-hppa.c.  */
47
48 /* Object file formats specify relocation types.  */
49 typedef enum elf_hppa_reloc_type reloc_type;
50
51 /* Object file formats specify BFD symbol types.  */
52 typedef elf_symbol_type obj_symbol_type;
53 #define symbol_arg_reloc_info(sym)\
54   (((obj_symbol_type *) symbol_get_bfdsym (sym))->tc_data.hppa_arg_reloc)
55
56 #if TARGET_ARCH_SIZE == 64
57 /* How to generate a relocation.  */
58 #define hppa_gen_reloc_type _bfd_elf64_hppa_gen_reloc_type
59 #define elf_hppa_reloc_final_type elf64_hppa_reloc_final_type
60 #else
61 #define hppa_gen_reloc_type _bfd_elf32_hppa_gen_reloc_type
62 #define elf_hppa_reloc_final_type elf32_hppa_reloc_final_type
63 #endif
64
65 /* ELF objects can have versions, but apparently do not have anywhere
66    to store a copyright string.  */
67 #define obj_version obj_elf_version
68 #define obj_copyright obj_elf_version
69
70 #define UNWIND_SECTION_NAME ".PARISC.unwind"
71 #endif /* OBJ_ELF */
72
73 #ifdef OBJ_SOM
74 /* Names of various debugging spaces/subspaces.  */
75 #define GDB_DEBUG_SPACE_NAME "$GDB_DEBUG$"
76 #define GDB_STRINGS_SUBSPACE_NAME "$GDB_STRINGS$"
77 #define GDB_SYMBOLS_SUBSPACE_NAME "$GDB_SYMBOLS$"
78 #define UNWIND_SECTION_NAME "$UNWIND$"
79
80 /* Object file formats specify relocation types.  */
81 typedef int reloc_type;
82
83 /* SOM objects can have both a version string and a copyright string.  */
84 #define obj_version obj_som_version
85 #define obj_copyright obj_som_copyright
86
87 /* How to generate a relocation.  */
88 #define hppa_gen_reloc_type hppa_som_gen_reloc_type
89
90 /* Object file formats specify BFD symbol types.  */
91 typedef som_symbol_type obj_symbol_type;
92 #define symbol_arg_reloc_info(sym)\
93   (((obj_symbol_type *) symbol_get_bfdsym (sym))->tc_data.ap.hppa_arg_reloc)
94
95 /* This apparently isn't in older versions of hpux reloc.h.  */
96 #ifndef R_DLT_REL
97 #define R_DLT_REL 0x78
98 #endif
99
100 #ifndef R_N0SEL
101 #define R_N0SEL 0xd8
102 #endif
103
104 #ifndef R_N1SEL
105 #define R_N1SEL 0xd9
106 #endif
107 #endif /* OBJ_SOM */
108
109 #if TARGET_ARCH_SIZE == 64
110 #define DEFAULT_LEVEL 25
111 #else
112 #define DEFAULT_LEVEL 10
113 #endif
114
115 /* Various structures and types used internally in tc-hppa.c.  */
116
117 /* Unwind table and descriptor.  FIXME: Sync this with GDB version.  */
118
119 struct unwind_desc
120   {
121     unsigned int cannot_unwind:1;
122     unsigned int millicode:1;
123     unsigned int millicode_save_rest:1;
124     unsigned int region_desc:2;
125     unsigned int save_sr:2;
126     unsigned int entry_fr:4;
127     unsigned int entry_gr:5;
128     unsigned int args_stored:1;
129     unsigned int call_fr:5;
130     unsigned int call_gr:5;
131     unsigned int save_sp:1;
132     unsigned int save_rp:1;
133     unsigned int save_rp_in_frame:1;
134     unsigned int extn_ptr_defined:1;
135     unsigned int cleanup_defined:1;
136
137     unsigned int hpe_interrupt_marker:1;
138     unsigned int hpux_interrupt_marker:1;
139     unsigned int reserved:3;
140     unsigned int frame_size:27;
141   };
142
143 /* We can't rely on compilers placing bitfields in any particular
144    place, so use these macros when dumping unwind descriptors to
145    object files.  */
146 #define UNWIND_LOW32(U) \
147   (((U)->cannot_unwind << 31)           \
148    | ((U)->millicode << 30)             \
149    | ((U)->millicode_save_rest << 29)   \
150    | ((U)->region_desc << 27)           \
151    | ((U)->save_sr << 25)               \
152    | ((U)->entry_fr << 21)              \
153    | ((U)->entry_gr << 16)              \
154    | ((U)->args_stored << 15)           \
155    | ((U)->call_fr << 10)               \
156    | ((U)->call_gr << 5)                \
157    | ((U)->save_sp << 4)                \
158    | ((U)->save_rp << 3)                \
159    | ((U)->save_rp_in_frame << 2)       \
160    | ((U)->extn_ptr_defined << 1)       \
161    | ((U)->cleanup_defined << 0))
162
163 #define UNWIND_HIGH32(U) \
164   (((U)->hpe_interrupt_marker << 31)    \
165    | ((U)->hpux_interrupt_marker << 30) \
166    | ((U)->frame_size << 0))
167
168 struct unwind_table
169   {
170     /* Starting and ending offsets of the region described by
171        descriptor.  */
172     unsigned int start_offset;
173     unsigned int end_offset;
174     struct unwind_desc descriptor;
175   };
176
177 /* This structure is used by the .callinfo, .enter, .leave pseudo-ops to
178    control the entry and exit code they generate. It is also used in
179    creation of the correct stack unwind descriptors.
180
181    NOTE:  GAS does not support .enter and .leave for the generation of
182    prologues and epilogues.  FIXME.
183
184    The fields in structure roughly correspond to the arguments available on the
185    .callinfo pseudo-op.  */
186
187 struct call_info
188   {
189     /* The unwind descriptor being built.  */
190     struct unwind_table ci_unwind;
191
192     /* Name of this function.  */
193     symbolS *start_symbol;
194
195     /* (temporary) symbol used to mark the end of this function.  */
196     symbolS *end_symbol;
197
198     /* Next entry in the chain.  */
199     struct call_info *ci_next;
200   };
201
202 /* Operand formats for FP instructions.   Note not all FP instructions
203    allow all four formats to be used (for example fmpysub only allows
204    SGL and DBL).  */
205 typedef enum
206   {
207     SGL, DBL, ILLEGAL_FMT, QUAD, W, UW, DW, UDW, QW, UQW
208   }
209 fp_operand_format;
210
211 /* This fully describes the symbol types which may be attached to
212    an EXPORT or IMPORT directive.  Only SOM uses this formation
213    (ELF has no need for it).  */
214 typedef enum
215   {
216     SYMBOL_TYPE_UNKNOWN,
217     SYMBOL_TYPE_ABSOLUTE,
218     SYMBOL_TYPE_CODE,
219     SYMBOL_TYPE_DATA,
220     SYMBOL_TYPE_ENTRY,
221     SYMBOL_TYPE_MILLICODE,
222     SYMBOL_TYPE_PLABEL,
223     SYMBOL_TYPE_PRI_PROG,
224     SYMBOL_TYPE_SEC_PROG,
225   }
226 pa_symbol_type;
227
228 /* This structure contains information needed to assemble
229    individual instructions.  */
230 struct pa_it
231   {
232     /* Holds the opcode after parsing by pa_ip.  */
233     unsigned long opcode;
234
235     /* Holds an expression associated with the current instruction.  */
236     expressionS exp;
237
238     /* Does this instruction use PC-relative addressing.  */
239     int pcrel;
240
241     /* Floating point formats for operand1 and operand2.  */
242     fp_operand_format fpof1;
243     fp_operand_format fpof2;
244
245     /* Whether or not we saw a truncation request on an fcnv insn.  */
246     int trunc;
247
248     /* Holds the field selector for this instruction
249        (for example L%, LR%, etc).  */
250     long field_selector;
251
252     /* Holds any argument relocation bits associated with this
253        instruction.  (instruction should be some sort of call).  */
254     unsigned int arg_reloc;
255
256     /* The format specification for this instruction.  */
257     int format;
258
259     /* The relocation (if any) associated with this instruction.  */
260     reloc_type reloc;
261   };
262
263 /* PA-89 floating point registers are arranged like this:
264
265    +--------------+--------------+
266    |   0 or 16L   |  16 or 16R   |
267    +--------------+--------------+
268    |   1 or 17L   |  17 or 17R   |
269    +--------------+--------------+
270    |              |              |
271
272    .              .              .
273    .              .              .
274    .              .              .
275
276    |              |              |
277    +--------------+--------------+
278    |  14 or 30L   |  30 or 30R   |
279    +--------------+--------------+
280    |  15 or 31L   |  31 or 31R   |
281    +--------------+--------------+  */
282
283 /* Additional information needed to build argument relocation stubs.  */
284 struct call_desc
285   {
286     /* The argument relocation specification.  */
287     unsigned int arg_reloc;
288
289     /* Number of arguments.  */
290     unsigned int arg_count;
291   };
292
293 #ifdef OBJ_SOM
294 /* This structure defines an entry in the subspace dictionary
295    chain.  */
296
297 struct subspace_dictionary_chain
298   {
299     /* Nonzero if this space has been defined by the user code.  */
300     unsigned int ssd_defined;
301
302     /* Name of this subspace.  */
303     char *ssd_name;
304
305     /* GAS segment and subsegment associated with this subspace.  */
306     asection *ssd_seg;
307     int ssd_subseg;
308
309     /* Next space in the subspace dictionary chain.  */
310     struct subspace_dictionary_chain *ssd_next;
311   };
312
313 typedef struct subspace_dictionary_chain ssd_chain_struct;
314
315 /* This structure defines an entry in the subspace dictionary
316    chain.  */
317
318 struct space_dictionary_chain
319   {
320     /* Nonzero if this space has been defined by the user code or
321        as a default space.  */
322     unsigned int sd_defined;
323
324     /* Nonzero if this spaces has been defined by the user code.  */
325     unsigned int sd_user_defined;
326
327     /* The space number (or index).  */
328     unsigned int sd_spnum;
329
330     /* The name of this subspace.  */
331     char *sd_name;
332
333     /* GAS segment to which this subspace corresponds.  */
334     asection *sd_seg;
335
336     /* Current subsegment number being used.  */
337     int sd_last_subseg;
338
339     /* The chain of subspaces contained within this space.  */
340     ssd_chain_struct *sd_subspaces;
341
342     /* The next entry in the space dictionary chain.  */
343     struct space_dictionary_chain *sd_next;
344   };
345
346 typedef struct space_dictionary_chain sd_chain_struct;
347
348 /* This structure defines attributes of the default subspace
349    dictionary entries.  */
350
351 struct default_subspace_dict
352   {
353     /* Name of the subspace.  */
354     char *name;
355
356     /* FIXME.  Is this still needed?  */
357     char defined;
358
359     /* Nonzero if this subspace is loadable.  */
360     char loadable;
361
362     /* Nonzero if this subspace contains only code.  */
363     char code_only;
364
365     /* Nonzero if this is a comdat subspace.  */
366     char comdat;
367
368     /* Nonzero if this is a common subspace.  */
369     char common;
370
371     /* Nonzero if this is a common subspace which allows symbols
372        to be multiply defined.  */
373     char dup_common;
374
375     /* Nonzero if this subspace should be zero filled.  */
376     char zero;
377
378     /* Sort key for this subspace.  */
379     unsigned char sort;
380
381     /* Access control bits for this subspace.  Can represent RWX access
382        as well as privilege level changes for gateways.  */
383     int access;
384
385     /* Index of containing space.  */
386     int space_index;
387
388     /* Alignment (in bytes) of this subspace.  */
389     int alignment;
390
391     /* Quadrant within space where this subspace should be loaded.  */
392     int quadrant;
393
394     /* An index into the default spaces array.  */
395     int def_space_index;
396
397     /* Subsegment associated with this subspace.  */
398     subsegT subsegment;
399   };
400
401 /* This structure defines attributes of the default space
402    dictionary entries.  */
403
404 struct default_space_dict
405   {
406     /* Name of the space.  */
407     char *name;
408
409     /* Space number.  It is possible to identify spaces within
410        assembly code numerically!  */
411     int spnum;
412
413     /* Nonzero if this space is loadable.  */
414     char loadable;
415
416     /* Nonzero if this space is "defined".  FIXME is still needed */
417     char defined;
418
419     /* Nonzero if this space can not be shared.  */
420     char private;
421
422     /* Sort key for this space.  */
423     unsigned char sort;
424
425     /* Segment associated with this space.  */
426     asection *segment;
427   };
428 #endif
429
430 /* Structure for previous label tracking.  Needed so that alignments,
431    callinfo declarations, etc can be easily attached to a particular
432    label.  */
433 typedef struct label_symbol_struct
434   {
435     struct symbol *lss_label;
436 #ifdef OBJ_SOM
437     sd_chain_struct *lss_space;
438 #endif
439 #ifdef OBJ_ELF
440     segT lss_segment;
441 #endif
442     struct label_symbol_struct *lss_next;
443   }
444 label_symbol_struct;
445
446 /* Extra information needed to perform fixups (relocations) on the PA.  */
447 struct hppa_fix_struct
448   {
449     /* The field selector.  */
450     enum hppa_reloc_field_selector_type_alt fx_r_field;
451
452     /* Type of fixup.  */
453     int fx_r_type;
454
455     /* Format of fixup.  */
456     int fx_r_format;
457
458     /* Argument relocation bits.  */
459     unsigned int fx_arg_reloc;
460
461     /* The segment this fixup appears in.  */
462     segT segment;
463   };
464
465 /* Structure to hold information about predefined registers.  */
466
467 struct pd_reg
468   {
469     char *name;
470     int value;
471   };
472
473 /* This structure defines the mapping from a FP condition string
474    to a condition number which can be recorded in an instruction.  */
475 struct fp_cond_map
476   {
477     char *string;
478     int cond;
479   };
480
481 /* This structure defines a mapping from a field selector
482    string to a field selector type.  */
483 struct selector_entry
484   {
485     char *prefix;
486     int field_selector;
487   };
488
489 /* Prototypes for functions local to tc-hppa.c.  */
490
491 #ifdef OBJ_SOM
492 static void pa_check_current_space_and_subspace (void);
493 #endif
494
495 #if !(defined (OBJ_ELF) && (defined (TE_LINUX) || defined (TE_NetBSD)))
496 static void pa_text (int);
497 static void pa_data (int);
498 static void pa_comm (int);
499 #endif
500 #ifdef OBJ_SOM
501 static int exact_log2 (int);
502 static void pa_compiler (int);
503 static void pa_align (int);
504 static void pa_space (int);
505 static void pa_spnum (int);
506 static void pa_subspace (int);
507 static sd_chain_struct *create_new_space (char *, int, int,
508                                                   int, int, int,
509                                                   asection *, int);
510 static ssd_chain_struct *create_new_subspace (sd_chain_struct *,
511                                                       char *, int, int,
512                                                       int, int, int, int,
513                                                       int, int, int, int,
514                                                       int, asection *);
515 static ssd_chain_struct *update_subspace (sd_chain_struct *,
516                                                   char *, int, int, int,
517                                                   int, int, int, int,
518                                                   int, int, int, int,
519                                                   asection *);
520 static sd_chain_struct *is_defined_space (char *);
521 static ssd_chain_struct *is_defined_subspace (char *);
522 static sd_chain_struct *pa_segment_to_space (asection *);
523 static ssd_chain_struct *pa_subsegment_to_subspace (asection *,
524                                                             subsegT);
525 static sd_chain_struct *pa_find_space_by_number (int);
526 static unsigned int pa_subspace_start (sd_chain_struct *, int);
527 static sd_chain_struct *pa_parse_space_stmt (char *, int);
528 #endif
529
530 /* File and globally scoped variable declarations.  */
531
532 #ifdef OBJ_SOM
533 /* Root and final entry in the space chain.  */
534 static sd_chain_struct *space_dict_root;
535 static sd_chain_struct *space_dict_last;
536
537 /* The current space and subspace.  */
538 static sd_chain_struct *current_space;
539 static ssd_chain_struct *current_subspace;
540 #endif
541
542 /* Root of the call_info chain.  */
543 static struct call_info *call_info_root;
544
545 /* The last call_info (for functions) structure
546    seen so it can be associated with fixups and
547    function labels.  */
548 static struct call_info *last_call_info;
549
550 /* The last call description (for actual calls).  */
551 static struct call_desc last_call_desc;
552
553 /* handle of the OPCODE hash table */
554 static struct hash_control *op_hash = NULL;
555
556 /* These characters can be suffixes of opcode names and they may be
557    followed by meaningful whitespace.  We don't include `,' and `!'
558    as they never appear followed by meaningful whitespace.  */
559 const char hppa_symbol_chars[] = "*?=<>";
560
561 /* This array holds the chars that only start a comment at the beginning of
562    a line.  If the line seems to have the form '# 123 filename'
563    .line and .file directives will appear in the pre-processed output.
564
565    Note that input_file.c hand checks for '#' at the beginning of the
566    first line of the input file.  This is because the compiler outputs
567    #NO_APP at the beginning of its output.
568
569    Also note that C style comments will always work.  */
570 const char line_comment_chars[] = "#";
571
572 /* This array holds the chars that always start a comment.  If the
573    pre-processor is disabled, these aren't very useful.  */
574 const char comment_chars[] = ";";
575
576 /* This array holds the characters which act as line separators.  */
577 const char line_separator_chars[] = "!";
578
579 /* Chars that can be used to separate mant from exp in floating point nums.  */
580 const char EXP_CHARS[] = "eE";
581
582 /* Chars that mean this number is a floating point constant.
583    As in 0f12.456 or 0d1.2345e12.
584
585    Be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
586    changed in read.c.  Ideally it shouldn't have to know about it
587    at all, but nothing is ideal around here.  */
588 const char FLT_CHARS[] = "rRsSfFdDxXpP";
589
590 static struct pa_it the_insn;
591
592 /* Points to the end of an expression just parsed by get_expression
593    and friends.  FIXME.  This shouldn't be handled with a file-global
594    variable.  */
595 static char *expr_end;
596
597 /* Nonzero if a .callinfo appeared within the current procedure.  */
598 static int callinfo_found;
599
600 /* Nonzero if the assembler is currently within a .entry/.exit pair.  */
601 static int within_entry_exit;
602
603 /* Nonzero if the assembler is currently within a procedure definition.  */
604 static int within_procedure;
605
606 /* Handle on structure which keep track of the last symbol
607    seen in each subspace.  */
608 static label_symbol_struct *label_symbols_rootp = NULL;
609
610 /* Holds the last field selector.  */
611 static int hppa_field_selector;
612
613 /* Nonzero when strict matching is enabled.  Zero otherwise.
614
615    Each opcode in the table has a flag which indicates whether or
616    not strict matching should be enabled for that instruction.
617
618    Mainly, strict causes errors to be ignored when a match failure
619    occurs.  However, it also affects the parsing of register fields
620    by pa_parse_number.  */
621 static int strict;
622
623 /* pa_parse_number returns values in `pa_number'.  Mostly
624    pa_parse_number is used to return a register number, with floating
625    point registers being numbered from FP_REG_BASE upwards.
626    The bit specified with FP_REG_RSEL is set if the floating point
627    register has a `r' suffix.  */
628 #define FP_REG_BASE 64
629 #define FP_REG_RSEL 128
630 static int pa_number;
631
632 #ifdef OBJ_SOM
633 /* A dummy bfd symbol so that all relocations have symbols of some kind.  */
634 static symbolS *dummy_symbol;
635 #endif
636
637 /* Nonzero if errors are to be printed.  */
638 static int print_errors = 1;
639
640 /* List of registers that are pre-defined:
641
642    Each general register has one predefined name of the form
643    %r<REGNUM> which has the value <REGNUM>.
644
645    Space and control registers are handled in a similar manner,
646    but use %sr<REGNUM> and %cr<REGNUM> as their predefined names.
647
648    Likewise for the floating point registers, but of the form
649    %fr<REGNUM>.  Floating point registers have additional predefined
650    names with 'L' and 'R' suffixes (e.g. %fr19L, %fr19R) which
651    again have the value <REGNUM>.
652
653    Many registers also have synonyms:
654
655    %r26 - %r23 have %arg0 - %arg3 as synonyms
656    %r28 - %r29 have %ret0 - %ret1 as synonyms
657    %fr4 - %fr7 have %farg0 - %farg3 as synonyms
658    %r30 has %sp as a synonym
659    %r27 has %dp as a synonym
660    %r2  has %rp as a synonym
661
662    Almost every control register has a synonym; they are not listed
663    here for brevity.
664
665    The table is sorted. Suitable for searching by a binary search.  */
666
667 static const struct pd_reg pre_defined_registers[] =
668 {
669   {"%arg0",  26},
670   {"%arg1",  25},
671   {"%arg2",  24},
672   {"%arg3",  23},
673   {"%cr0",    0},
674   {"%cr10",  10},
675   {"%cr11",  11},
676   {"%cr12",  12},
677   {"%cr13",  13},
678   {"%cr14",  14},
679   {"%cr15",  15},
680   {"%cr16",  16},
681   {"%cr17",  17},
682   {"%cr18",  18},
683   {"%cr19",  19},
684   {"%cr20",  20},
685   {"%cr21",  21},
686   {"%cr22",  22},
687   {"%cr23",  23},
688   {"%cr24",  24},
689   {"%cr25",  25},
690   {"%cr26",  26},
691   {"%cr27",  27},
692   {"%cr28",  28},
693   {"%cr29",  29},
694   {"%cr30",  30},
695   {"%cr31",  31},
696   {"%cr8",    8},
697   {"%cr9",    9},
698   {"%dp",    27},
699   {"%eiem",  15},
700   {"%eirr",  23},
701   {"%farg0",  4 + FP_REG_BASE},
702   {"%farg1",  5 + FP_REG_BASE},
703   {"%farg2",  6 + FP_REG_BASE},
704   {"%farg3",  7 + FP_REG_BASE},
705   {"%fr0",    0 + FP_REG_BASE},
706   {"%fr0l",   0 + FP_REG_BASE},
707   {"%fr0r",   0 + FP_REG_BASE + FP_REG_RSEL},
708   {"%fr1",    1 + FP_REG_BASE},
709   {"%fr10",  10 + FP_REG_BASE},
710   {"%fr10l", 10 + FP_REG_BASE},
711   {"%fr10r", 10 + FP_REG_BASE + FP_REG_RSEL},
712   {"%fr11",  11 + FP_REG_BASE},
713   {"%fr11l", 11 + FP_REG_BASE},
714   {"%fr11r", 11 + FP_REG_BASE + FP_REG_RSEL},
715   {"%fr12",  12 + FP_REG_BASE},
716   {"%fr12l", 12 + FP_REG_BASE},
717   {"%fr12r", 12 + FP_REG_BASE + FP_REG_RSEL},
718   {"%fr13",  13 + FP_REG_BASE},
719   {"%fr13l", 13 + FP_REG_BASE},
720   {"%fr13r", 13 + FP_REG_BASE + FP_REG_RSEL},
721   {"%fr14",  14 + FP_REG_BASE},
722   {"%fr14l", 14 + FP_REG_BASE},
723   {"%fr14r", 14 + FP_REG_BASE + FP_REG_RSEL},
724   {"%fr15",  15 + FP_REG_BASE},
725   {"%fr15l", 15 + FP_REG_BASE},
726   {"%fr15r", 15 + FP_REG_BASE + FP_REG_RSEL},
727   {"%fr16",  16 + FP_REG_BASE},
728   {"%fr16l", 16 + FP_REG_BASE},
729   {"%fr16r", 16 + FP_REG_BASE + FP_REG_RSEL},
730   {"%fr17",  17 + FP_REG_BASE},
731   {"%fr17l", 17 + FP_REG_BASE},
732   {"%fr17r", 17 + FP_REG_BASE + FP_REG_RSEL},
733   {"%fr18",  18 + FP_REG_BASE},
734   {"%fr18l", 18 + FP_REG_BASE},
735   {"%fr18r", 18 + FP_REG_BASE + FP_REG_RSEL},
736   {"%fr19",  19 + FP_REG_BASE},
737   {"%fr19l", 19 + FP_REG_BASE},
738   {"%fr19r", 19 + FP_REG_BASE + FP_REG_RSEL},
739   {"%fr1l",   1 + FP_REG_BASE},
740   {"%fr1r",   1 + FP_REG_BASE + FP_REG_RSEL},
741   {"%fr2",    2 + FP_REG_BASE},
742   {"%fr20",  20 + FP_REG_BASE},
743   {"%fr20l", 20 + FP_REG_BASE},
744   {"%fr20r", 20 + FP_REG_BASE + FP_REG_RSEL},
745   {"%fr21",  21 + FP_REG_BASE},
746   {"%fr21l", 21 + FP_REG_BASE},
747   {"%fr21r", 21 + FP_REG_BASE + FP_REG_RSEL},
748   {"%fr22",  22 + FP_REG_BASE},
749   {"%fr22l", 22 + FP_REG_BASE},
750   {"%fr22r", 22 + FP_REG_BASE + FP_REG_RSEL},
751   {"%fr23",  23 + FP_REG_BASE},
752   {"%fr23l", 23 + FP_REG_BASE},
753   {"%fr23r", 23 + FP_REG_BASE + FP_REG_RSEL},
754   {"%fr24",  24 + FP_REG_BASE},
755   {"%fr24l", 24 + FP_REG_BASE},
756   {"%fr24r", 24 + FP_REG_BASE + FP_REG_RSEL},
757   {"%fr25",  25 + FP_REG_BASE},
758   {"%fr25l", 25 + FP_REG_BASE},
759   {"%fr25r", 25 + FP_REG_BASE + FP_REG_RSEL},
760   {"%fr26",  26 + FP_REG_BASE},
761   {"%fr26l", 26 + FP_REG_BASE},
762   {"%fr26r", 26 + FP_REG_BASE + FP_REG_RSEL},
763   {"%fr27",  27 + FP_REG_BASE},
764   {"%fr27l", 27 + FP_REG_BASE},
765   {"%fr27r", 27 + FP_REG_BASE + FP_REG_RSEL},
766   {"%fr28",  28 + FP_REG_BASE},
767   {"%fr28l", 28 + FP_REG_BASE},
768   {"%fr28r", 28 + FP_REG_BASE + FP_REG_RSEL},
769   {"%fr29",  29 + FP_REG_BASE},
770   {"%fr29l", 29 + FP_REG_BASE},
771   {"%fr29r", 29 + FP_REG_BASE + FP_REG_RSEL},
772   {"%fr2l",   2 + FP_REG_BASE},
773   {"%fr2r",   2 + FP_REG_BASE + FP_REG_RSEL},
774   {"%fr3",    3 + FP_REG_BASE},
775   {"%fr30",  30 + FP_REG_BASE},
776   {"%fr30l", 30 + FP_REG_BASE},
777   {"%fr30r", 30 + FP_REG_BASE + FP_REG_RSEL},
778   {"%fr31",  31 + FP_REG_BASE},
779   {"%fr31l", 31 + FP_REG_BASE},
780   {"%fr31r", 31 + FP_REG_BASE + FP_REG_RSEL},
781   {"%fr3l",   3 + FP_REG_BASE},
782   {"%fr3r",   3 + FP_REG_BASE + FP_REG_RSEL},
783   {"%fr4",    4 + FP_REG_BASE},
784   {"%fr4l",   4 + FP_REG_BASE},
785   {"%fr4r",   4 + FP_REG_BASE + FP_REG_RSEL},
786   {"%fr5",    5 + FP_REG_BASE},
787   {"%fr5l",   5 + FP_REG_BASE},
788   {"%fr5r",   5 + FP_REG_BASE + FP_REG_RSEL},
789   {"%fr6",    6 + FP_REG_BASE},
790   {"%fr6l",   6 + FP_REG_BASE},
791   {"%fr6r",   6 + FP_REG_BASE + FP_REG_RSEL},
792   {"%fr7",    7 + FP_REG_BASE},
793   {"%fr7l",   7 + FP_REG_BASE},
794   {"%fr7r",   7 + FP_REG_BASE + FP_REG_RSEL},
795   {"%fr8",    8 + FP_REG_BASE},
796   {"%fr8l",   8 + FP_REG_BASE},
797   {"%fr8r",   8 + FP_REG_BASE + FP_REG_RSEL},
798   {"%fr9",    9 + FP_REG_BASE},
799   {"%fr9l",   9 + FP_REG_BASE},
800   {"%fr9r",   9 + FP_REG_BASE + FP_REG_RSEL},
801   {"%fret",   4},
802   {"%hta",   25},
803   {"%iir",   19},
804   {"%ior",   21},
805   {"%ipsw",  22},
806   {"%isr",   20},
807   {"%itmr",  16},
808   {"%iva",   14},
809 #if TARGET_ARCH_SIZE == 64
810   {"%mrp",    2},
811 #else
812   {"%mrp",   31},
813 #endif
814   {"%pcoq",  18},
815   {"%pcsq",  17},
816   {"%pidr1",  8},
817   {"%pidr2",  9},
818   {"%pidr3", 12},
819   {"%pidr4", 13},
820   {"%ppda",  24},
821   {"%r0",     0},
822   {"%r1",     1},
823   {"%r10",   10},
824   {"%r11",   11},
825   {"%r12",   12},
826   {"%r13",   13},
827   {"%r14",   14},
828   {"%r15",   15},
829   {"%r16",   16},
830   {"%r17",   17},
831   {"%r18",   18},
832   {"%r19",   19},
833   {"%r2",     2},
834   {"%r20",   20},
835   {"%r21",   21},
836   {"%r22",   22},
837   {"%r23",   23},
838   {"%r24",   24},
839   {"%r25",   25},
840   {"%r26",   26},
841   {"%r27",   27},
842   {"%r28",   28},
843   {"%r29",   29},
844   {"%r3",     3},
845   {"%r30",   30},
846   {"%r31",   31},
847   {"%r4",     4},
848   {"%r5",     5},
849   {"%r6",     6},
850   {"%r7",     7},
851   {"%r8",     8},
852   {"%r9",     9},
853   {"%rctr",   0},
854   {"%ret0",  28},
855   {"%ret1",  29},
856   {"%rp",     2},
857   {"%sar",   11},
858   {"%sp",    30},
859   {"%sr0",    0},
860   {"%sr1",    1},
861   {"%sr2",    2},
862   {"%sr3",    3},
863   {"%sr4",    4},
864   {"%sr5",    5},
865   {"%sr6",    6},
866   {"%sr7",    7},
867   {"%t1",    22},
868   {"%t2",    21},
869   {"%t3",    20},
870   {"%t4",    19},
871   {"%tf1",   11},
872   {"%tf2",   10},
873   {"%tf3",    9},
874   {"%tf4",    8},
875   {"%tr0",   24},
876   {"%tr1",   25},
877   {"%tr2",   26},
878   {"%tr3",   27},
879   {"%tr4",   28},
880   {"%tr5",   29},
881   {"%tr6",   30},
882   {"%tr7",   31}
883 };
884
885 /* This table is sorted by order of the length of the string. This is
886    so we check for <> before we check for <. If we had a <> and checked
887    for < first, we would get a false match.  */
888 static const struct fp_cond_map fp_cond_map[] =
889 {
890   {"false?", 0},
891   {"false", 1},
892   {"true?", 30},
893   {"true", 31},
894   {"!<=>", 3},
895   {"!?>=", 8},
896   {"!?<=", 16},
897   {"!<>", 7},
898   {"!>=", 11},
899   {"!?>", 12},
900   {"?<=", 14},
901   {"!<=", 19},
902   {"!?<", 20},
903   {"?>=", 22},
904   {"!?=", 24},
905   {"!=t", 27},
906   {"<=>", 29},
907   {"=t", 5},
908   {"?=", 6},
909   {"?<", 10},
910   {"<=", 13},
911   {"!>", 15},
912   {"?>", 18},
913   {">=", 21},
914   {"!<", 23},
915   {"<>", 25},
916   {"!=", 26},
917   {"!?", 28},
918   {"?", 2},
919   {"=", 4},
920   {"<", 9},
921   {">", 17}
922 };
923
924 static const struct selector_entry selector_table[] =
925 {
926   {"f", e_fsel},
927   {"l", e_lsel},
928   {"ld", e_ldsel},
929   {"lp", e_lpsel},
930   {"lr", e_lrsel},
931   {"ls", e_lssel},
932   {"lt", e_ltsel},
933   {"ltp", e_ltpsel},
934   {"n", e_nsel},
935   {"nl", e_nlsel},
936   {"nlr", e_nlrsel},
937   {"p", e_psel},
938   {"r", e_rsel},
939   {"rd", e_rdsel},
940   {"rp", e_rpsel},
941   {"rr", e_rrsel},
942   {"rs", e_rssel},
943   {"rt", e_rtsel},
944   {"rtp", e_rtpsel},
945   {"t", e_tsel},
946 };
947
948 #ifdef OBJ_SOM
949 /* default space and subspace dictionaries */
950
951 #define GDB_SYMBOLS          GDB_SYMBOLS_SUBSPACE_NAME
952 #define GDB_STRINGS          GDB_STRINGS_SUBSPACE_NAME
953
954 /* pre-defined subsegments (subspaces) for the HPPA.  */
955 #define SUBSEG_CODE   0
956 #define SUBSEG_LIT    1
957 #define SUBSEG_MILLI  2
958 #define SUBSEG_DATA   0
959 #define SUBSEG_BSS    2
960 #define SUBSEG_UNWIND 3
961 #define SUBSEG_GDB_STRINGS 0
962 #define SUBSEG_GDB_SYMBOLS 1
963
964 static struct default_subspace_dict pa_def_subspaces[] =
965 {
966   {"$CODE$", 1, 1, 1, 0, 0, 0, 0, 24, 0x2c, 0, 8, 0, 0, SUBSEG_CODE},
967   {"$DATA$", 1, 1, 0, 0, 0, 0, 0, 24, 0x1f, 1, 8, 1, 1, SUBSEG_DATA},
968   {"$LIT$", 1, 1, 0, 0, 0, 0, 0, 16, 0x2c, 0, 8, 0, 0, SUBSEG_LIT},
969   {"$MILLICODE$", 1, 1, 0, 0, 0, 0, 0, 8, 0x2c, 0, 8, 0, 0, SUBSEG_MILLI},
970   {"$BSS$", 1, 1, 0, 0, 0, 0, 1, 80, 0x1f, 1, 8, 1, 1, SUBSEG_BSS},
971   {NULL, 0, 1, 0, 0, 0, 0, 0, 255, 0x1f, 0, 4, 0, 0, 0}
972 };
973
974 static struct default_space_dict pa_def_spaces[] =
975 {
976   {"$TEXT$", 0, 1, 1, 0, 8, ASEC_NULL},
977   {"$PRIVATE$", 1, 1, 1, 1, 16, ASEC_NULL},
978   {NULL, 0, 0, 0, 0, 0, ASEC_NULL}
979 };
980
981 /* Misc local definitions used by the assembler.  */
982
983 /* These macros are used to maintain spaces/subspaces.  */
984 #define SPACE_DEFINED(space_chain)      (space_chain)->sd_defined
985 #define SPACE_USER_DEFINED(space_chain) (space_chain)->sd_user_defined
986 #define SPACE_SPNUM(space_chain)        (space_chain)->sd_spnum
987 #define SPACE_NAME(space_chain)         (space_chain)->sd_name
988
989 #define SUBSPACE_DEFINED(ss_chain)      (ss_chain)->ssd_defined
990 #define SUBSPACE_NAME(ss_chain)         (ss_chain)->ssd_name
991 #endif
992
993 /* Return nonzero if the string pointed to by S potentially represents
994    a right or left half of a FP register  */
995 #define IS_R_SELECT(S)   (*(S) == 'R' || *(S) == 'r')
996 #define IS_L_SELECT(S)   (*(S) == 'L' || *(S) == 'l')
997
998 /* Insert FIELD into OPCODE starting at bit START.  Continue pa_ip
999    main loop after insertion.  */
1000
1001 #define INSERT_FIELD_AND_CONTINUE(OPCODE, FIELD, START) \
1002   { \
1003     ((OPCODE) |= (FIELD) << (START)); \
1004     continue; \
1005   }
1006
1007 /* Simple range checking for FIELD against HIGH and LOW bounds.
1008    IGNORE is used to suppress the error message.  */
1009
1010 #define CHECK_FIELD(FIELD, HIGH, LOW, IGNORE) \
1011   { \
1012     if ((FIELD) > (HIGH) || (FIELD) < (LOW)) \
1013       { \
1014         if (! IGNORE) \
1015           as_bad (_("Field out of range [%d..%d] (%d)."), (LOW), (HIGH), \
1016                   (int) (FIELD));\
1017         break; \
1018       } \
1019   }
1020
1021 /* Variant of CHECK_FIELD for use in md_apply_fix and other places where
1022    the current file and line number are not valid.  */
1023
1024 #define CHECK_FIELD_WHERE(FIELD, HIGH, LOW, FILENAME, LINE) \
1025   { \
1026     if ((FIELD) > (HIGH) || (FIELD) < (LOW)) \
1027       { \
1028         as_bad_where ((FILENAME), (LINE), \
1029                       _("Field out of range [%d..%d] (%d)."), (LOW), (HIGH), \
1030                       (int) (FIELD));\
1031         break; \
1032       } \
1033   }
1034
1035 /* Simple alignment checking for FIELD against ALIGN (a power of two).
1036    IGNORE is used to suppress the error message.  */
1037
1038 #define CHECK_ALIGN(FIELD, ALIGN, IGNORE) \
1039   { \
1040     if ((FIELD) & ((ALIGN) - 1)) \
1041       { \
1042         if (! IGNORE) \
1043           as_bad (_("Field not properly aligned [%d] (%d)."), (ALIGN), \
1044                   (int) (FIELD));\
1045         break; \
1046       } \
1047   }
1048
1049 #define is_DP_relative(exp)                     \
1050   ((exp).X_op == O_subtract                     \
1051    && strcmp (S_GET_NAME ((exp).X_op_symbol), "$global$") == 0)
1052
1053 #define is_PC_relative(exp)                     \
1054   ((exp).X_op == O_subtract                     \
1055    && strcmp (S_GET_NAME ((exp).X_op_symbol), "$PIC_pcrel$0") == 0)
1056
1057 #define is_tls_gdidx(exp)                       \
1058   ((exp).X_op == O_subtract                     \
1059    && strcmp (S_GET_NAME ((exp).X_op_symbol), "$tls_gdidx$") == 0)
1060
1061 #define is_tls_ldidx(exp)                       \
1062   ((exp).X_op == O_subtract                     \
1063    && strcmp (S_GET_NAME ((exp).X_op_symbol), "$tls_ldidx$") == 0)
1064
1065 #define is_tls_dtpoff(exp)                      \
1066   ((exp).X_op == O_subtract                     \
1067    && strcmp (S_GET_NAME ((exp).X_op_symbol), "$tls_dtpoff$") == 0)
1068
1069 #define is_tls_ieoff(exp)                       \
1070   ((exp).X_op == O_subtract                     \
1071    && strcmp (S_GET_NAME ((exp).X_op_symbol), "$tls_ieoff$") == 0)
1072
1073 #define is_tls_leoff(exp)                       \
1074   ((exp).X_op == O_subtract                     \
1075    && strcmp (S_GET_NAME ((exp).X_op_symbol), "$tls_leoff$") == 0)
1076
1077 /* We need some complex handling for stabs (sym1 - sym2).  Luckily, we'll
1078    always be able to reduce the expression to a constant, so we don't
1079    need real complex handling yet.  */
1080 #define is_complex(exp)                         \
1081   ((exp).X_op != O_constant && (exp).X_op != O_symbol)
1082
1083 /* Actual functions to implement the PA specific code for the assembler.  */
1084
1085 /* Called before writing the object file.  Make sure entry/exit and
1086    proc/procend pairs match.  */
1087
1088 void
1089 pa_check_eof (void)
1090 {
1091   if (within_entry_exit)
1092     as_fatal (_("Missing .exit\n"));
1093
1094   if (within_procedure)
1095     as_fatal (_("Missing .procend\n"));
1096 }
1097
1098 /* Returns a pointer to the label_symbol_struct for the current space.
1099    or NULL if no label_symbol_struct exists for the current space.  */
1100
1101 static label_symbol_struct *
1102 pa_get_label (void)
1103 {
1104   label_symbol_struct *label_chain;
1105
1106   for (label_chain = label_symbols_rootp;
1107        label_chain;
1108        label_chain = label_chain->lss_next)
1109     {
1110 #ifdef OBJ_SOM
1111     if (current_space == label_chain->lss_space && label_chain->lss_label)
1112       return label_chain;
1113 #endif
1114 #ifdef OBJ_ELF
1115     if (now_seg == label_chain->lss_segment && label_chain->lss_label)
1116       return label_chain;
1117 #endif
1118     }
1119
1120   return NULL;
1121 }
1122
1123 /* Defines a label for the current space.  If one is already defined,
1124    this function will replace it with the new label.  */
1125
1126 void
1127 pa_define_label (symbolS *symbol)
1128 {
1129   label_symbol_struct *label_chain = pa_get_label ();
1130
1131   if (label_chain)
1132     label_chain->lss_label = symbol;
1133   else
1134     {
1135       /* Create a new label entry and add it to the head of the chain.  */
1136       label_chain = xmalloc (sizeof (label_symbol_struct));
1137       label_chain->lss_label = symbol;
1138 #ifdef OBJ_SOM
1139       label_chain->lss_space = current_space;
1140 #endif
1141 #ifdef OBJ_ELF
1142       label_chain->lss_segment = now_seg;
1143 #endif
1144       label_chain->lss_next = NULL;
1145
1146       if (label_symbols_rootp)
1147         label_chain->lss_next = label_symbols_rootp;
1148
1149       label_symbols_rootp = label_chain;
1150     }
1151
1152 #ifdef OBJ_ELF
1153   dwarf2_emit_label (symbol);
1154 #endif
1155 }
1156
1157 /* Removes a label definition for the current space.
1158    If there is no label_symbol_struct entry, then no action is taken.  */
1159
1160 static void
1161 pa_undefine_label (void)
1162 {
1163   label_symbol_struct *label_chain;
1164   label_symbol_struct *prev_label_chain = NULL;
1165
1166   for (label_chain = label_symbols_rootp;
1167        label_chain;
1168        label_chain = label_chain->lss_next)
1169     {
1170       if (1
1171 #ifdef OBJ_SOM
1172           && current_space == label_chain->lss_space && label_chain->lss_label
1173 #endif
1174 #ifdef OBJ_ELF
1175           && now_seg == label_chain->lss_segment && label_chain->lss_label
1176 #endif
1177           )
1178         {
1179           /* Remove the label from the chain and free its memory.  */
1180           if (prev_label_chain)
1181             prev_label_chain->lss_next = label_chain->lss_next;
1182           else
1183             label_symbols_rootp = label_chain->lss_next;
1184
1185           free (label_chain);
1186           break;
1187         }
1188       prev_label_chain = label_chain;
1189     }
1190 }
1191
1192 /* An HPPA-specific version of fix_new.  This is required because the HPPA
1193    code needs to keep track of some extra stuff.  Each call to fix_new_hppa
1194    results in the creation of an instance of an hppa_fix_struct.  An
1195    hppa_fix_struct stores the extra information along with a pointer to the
1196    original fixS.  This is attached to the original fixup via the
1197    tc_fix_data field.  */
1198
1199 static void
1200 fix_new_hppa (fragS *frag,
1201               int where,
1202               int size,
1203               symbolS *add_symbol,
1204               offsetT offset,
1205               expressionS *exp,
1206               int pcrel,
1207               bfd_reloc_code_real_type r_type,
1208               enum hppa_reloc_field_selector_type_alt r_field,
1209               int r_format,
1210               unsigned int arg_reloc,
1211               int unwind_bits ATTRIBUTE_UNUSED)
1212 {
1213   fixS *new_fix;
1214   struct hppa_fix_struct *hppa_fix = obstack_alloc (&notes, sizeof (struct hppa_fix_struct));
1215
1216   if (exp != NULL)
1217     new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
1218   else
1219     new_fix = fix_new (frag, where, size, add_symbol, offset, pcrel, r_type);
1220   new_fix->tc_fix_data = (void *) hppa_fix;
1221   hppa_fix->fx_r_type = r_type;
1222   hppa_fix->fx_r_field = r_field;
1223   hppa_fix->fx_r_format = r_format;
1224   hppa_fix->fx_arg_reloc = arg_reloc;
1225   hppa_fix->segment = now_seg;
1226 #ifdef OBJ_SOM
1227   if (r_type == R_ENTRY || r_type == R_EXIT)
1228     new_fix->fx_offset = unwind_bits;
1229 #endif
1230
1231   /* foo-$global$ is used to access non-automatic storage.  $global$
1232      is really just a marker and has served its purpose, so eliminate
1233      it now so as not to confuse write.c.  Ditto for $PIC_pcrel$0.  */
1234   if (new_fix->fx_subsy
1235       && (strcmp (S_GET_NAME (new_fix->fx_subsy), "$global$") == 0
1236           || strcmp (S_GET_NAME (new_fix->fx_subsy), "$PIC_pcrel$0") == 0
1237           || strcmp (S_GET_NAME (new_fix->fx_subsy), "$tls_gdidx$") == 0
1238           || strcmp (S_GET_NAME (new_fix->fx_subsy), "$tls_ldidx$") == 0
1239           || strcmp (S_GET_NAME (new_fix->fx_subsy), "$tls_dtpoff$") == 0
1240           || strcmp (S_GET_NAME (new_fix->fx_subsy), "$tls_ieoff$") == 0
1241           || strcmp (S_GET_NAME (new_fix->fx_subsy), "$tls_leoff$") == 0))
1242     new_fix->fx_subsy = NULL;
1243 }
1244
1245 /* This fix_new is called by cons via TC_CONS_FIX_NEW.
1246    hppa_field_selector is set by the parse_cons_expression_hppa.  */
1247
1248 void
1249 cons_fix_new_hppa (fragS *frag, int where, int size, expressionS *exp)
1250 {
1251   unsigned int rel_type;
1252
1253   /* Get a base relocation type.  */
1254   if (is_DP_relative (*exp))
1255     rel_type = R_HPPA_GOTOFF;
1256   else if (is_PC_relative (*exp))
1257     rel_type = R_HPPA_PCREL_CALL;
1258 #ifdef OBJ_ELF
1259   else if (is_tls_gdidx (*exp))
1260     rel_type = R_PARISC_TLS_GD21L;
1261   else if (is_tls_ldidx (*exp))
1262     rel_type = R_PARISC_TLS_LDM21L;
1263   else if (is_tls_dtpoff (*exp))
1264     rel_type = R_PARISC_TLS_LDO21L;
1265   else if (is_tls_ieoff (*exp))
1266     rel_type = R_PARISC_TLS_IE21L;
1267   else if (is_tls_leoff (*exp))
1268     rel_type = R_PARISC_TLS_LE21L;
1269 #endif
1270   else if (is_complex (*exp))
1271     rel_type = R_HPPA_COMPLEX;
1272   else
1273     rel_type = R_HPPA;
1274
1275   if (hppa_field_selector != e_psel && hppa_field_selector != e_fsel)
1276     {
1277       as_warn (_("Invalid field selector.  Assuming F%%."));
1278       hppa_field_selector = e_fsel;
1279     }
1280
1281   fix_new_hppa (frag, where, size,
1282                 (symbolS *) NULL, (offsetT) 0, exp, 0, rel_type,
1283                 hppa_field_selector, size * 8, 0, 0);
1284
1285   /* Reset field selector to its default state.  */
1286   hppa_field_selector = 0;
1287 }
1288
1289 /* Mark (via expr_end) the end of an expression (I think).  FIXME.  */
1290
1291 static void
1292 get_expression (char *str)
1293 {
1294   char *save_in;
1295   asection *seg;
1296
1297   save_in = input_line_pointer;
1298   input_line_pointer = str;
1299   seg = expression (&the_insn.exp);
1300   if (!(seg == absolute_section
1301         || seg == undefined_section
1302         || SEG_NORMAL (seg)))
1303     {
1304       as_warn (_("Bad segment in expression."));
1305       expr_end = input_line_pointer;
1306       input_line_pointer = save_in;
1307       return;
1308     }
1309   expr_end = input_line_pointer;
1310   input_line_pointer = save_in;
1311 }
1312
1313 /* Parse a PA nullification completer (,n).  Return nonzero if the
1314    completer was found; return zero if no completer was found.  */
1315
1316 static int
1317 pa_parse_nullif (char **s)
1318 {
1319   int nullif;
1320
1321   nullif = 0;
1322   if (**s == ',')
1323     {
1324       *s = *s + 1;
1325       if (strncasecmp (*s, "n", 1) == 0)
1326         nullif = 1;
1327       else
1328         {
1329           as_bad (_("Invalid Nullification: (%c)"), **s);
1330           nullif = 0;
1331         }
1332       *s = *s + 1;
1333     }
1334
1335   return nullif;
1336 }
1337
1338 char *
1339 md_atof (int type, char *litP, int *sizeP)
1340 {
1341   return ieee_md_atof (type, litP, sizeP, TRUE);
1342 }
1343
1344 /* Write out big-endian.  */
1345
1346 void
1347 md_number_to_chars (char *buf, valueT val, int n)
1348 {
1349   number_to_chars_bigendian (buf, val, n);
1350 }
1351
1352 /* Translate internal representation of relocation info to BFD target
1353    format.  */
1354
1355 arelent **
1356 tc_gen_reloc (asection *section, fixS *fixp)
1357 {
1358   arelent *reloc;
1359   struct hppa_fix_struct *hppa_fixp;
1360   static arelent *no_relocs = NULL;
1361   arelent **relocs;
1362   reloc_type **codes;
1363   reloc_type code;
1364   int n_relocs;
1365   int i;
1366
1367   hppa_fixp = (struct hppa_fix_struct *) fixp->tc_fix_data;
1368   if (fixp->fx_addsy == 0)
1369     return &no_relocs;
1370
1371   assert (hppa_fixp != 0);
1372   assert (section != 0);
1373
1374   reloc = xmalloc (sizeof (arelent));
1375
1376   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1377   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1378   codes = hppa_gen_reloc_type (stdoutput,
1379                                fixp->fx_r_type,
1380                                hppa_fixp->fx_r_format,
1381                                hppa_fixp->fx_r_field,
1382                                fixp->fx_subsy != NULL,
1383                                symbol_get_bfdsym (fixp->fx_addsy));
1384
1385   if (codes == NULL)
1386     {
1387       as_bad_where (fixp->fx_file, fixp->fx_line, _("Cannot handle fixup"));
1388       abort ();
1389     }
1390
1391   for (n_relocs = 0; codes[n_relocs]; n_relocs++)
1392     ;
1393
1394   relocs = xmalloc (sizeof (arelent *) * n_relocs + 1);
1395   reloc = xmalloc (sizeof (arelent) * n_relocs);
1396   for (i = 0; i < n_relocs; i++)
1397     relocs[i] = &reloc[i];
1398
1399   relocs[n_relocs] = NULL;
1400
1401 #ifdef OBJ_ELF
1402   switch (fixp->fx_r_type)
1403     {
1404     default:
1405       assert (n_relocs == 1);
1406
1407       code = *codes[0];
1408
1409       /* Now, do any processing that is dependent on the relocation type.  */
1410       switch (code)
1411         {
1412         case R_PARISC_DLTREL21L:
1413         case R_PARISC_DLTREL14R:
1414         case R_PARISC_DLTREL14F:
1415         case R_PARISC_PLABEL32:
1416         case R_PARISC_PLABEL21L:
1417         case R_PARISC_PLABEL14R:
1418           /* For plabel relocations, the addend of the
1419              relocation should be either 0 (no static link) or 2
1420              (static link required).  This adjustment is done in
1421              bfd/elf32-hppa.c:elf32_hppa_relocate_section.
1422
1423              We also slam a zero addend into the DLT relative relocs;
1424              it doesn't make a lot of sense to use any addend since
1425              it gets you a different (eg unknown) DLT entry.  */
1426           reloc->addend = 0;
1427           break;
1428
1429 #ifdef ELF_ARG_RELOC
1430         case R_PARISC_PCREL17R:
1431         case R_PARISC_PCREL17F:
1432         case R_PARISC_PCREL17C:
1433         case R_PARISC_DIR17R:
1434         case R_PARISC_DIR17F:
1435         case R_PARISC_PCREL21L:
1436         case R_PARISC_DIR21L:
1437           reloc->addend = HPPA_R_ADDEND (hppa_fixp->fx_arg_reloc,
1438                                          fixp->fx_offset);
1439           break;
1440 #endif
1441
1442         case R_PARISC_DIR32:
1443           /* Facilitate hand-crafted unwind info.  */
1444           if (strcmp (section->name, UNWIND_SECTION_NAME) == 0)
1445             code = R_PARISC_SEGREL32;
1446           /* Fall thru */
1447
1448         default:
1449           reloc->addend = fixp->fx_offset;
1450           break;
1451         }
1452
1453       reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1454       *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1455       reloc->howto = bfd_reloc_type_lookup (stdoutput,
1456                                             (bfd_reloc_code_real_type) code);
1457       reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1458
1459       assert (reloc->howto && (unsigned int) code == reloc->howto->type);
1460       break;
1461     }
1462 #else /* OBJ_SOM */
1463
1464   /* Walk over reach relocation returned by the BFD backend.  */
1465   for (i = 0; i < n_relocs; i++)
1466     {
1467       code = *codes[i];
1468
1469       relocs[i]->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1470       *relocs[i]->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1471       relocs[i]->howto =
1472         bfd_reloc_type_lookup (stdoutput,
1473                                (bfd_reloc_code_real_type) code);
1474       relocs[i]->address = fixp->fx_frag->fr_address + fixp->fx_where;
1475
1476       switch (code)
1477         {
1478         case R_COMP2:
1479           /* The only time we ever use a R_COMP2 fixup is for the difference
1480              of two symbols.  With that in mind we fill in all four
1481              relocs now and break out of the loop.  */
1482           assert (i == 1);
1483           relocs[0]->sym_ptr_ptr
1484             = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
1485           relocs[0]->howto
1486             = bfd_reloc_type_lookup (stdoutput,
1487                                      (bfd_reloc_code_real_type) *codes[0]);
1488           relocs[0]->address = fixp->fx_frag->fr_address + fixp->fx_where;
1489           relocs[0]->addend = 0;
1490           relocs[1]->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1491           *relocs[1]->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1492           relocs[1]->howto
1493             = bfd_reloc_type_lookup (stdoutput,
1494                                      (bfd_reloc_code_real_type) *codes[1]);
1495           relocs[1]->address = fixp->fx_frag->fr_address + fixp->fx_where;
1496           relocs[1]->addend = 0;
1497           relocs[2]->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1498           *relocs[2]->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
1499           relocs[2]->howto
1500             = bfd_reloc_type_lookup (stdoutput,
1501                                      (bfd_reloc_code_real_type) *codes[2]);
1502           relocs[2]->address = fixp->fx_frag->fr_address + fixp->fx_where;
1503           relocs[2]->addend = 0;
1504           relocs[3]->sym_ptr_ptr
1505             = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
1506           relocs[3]->howto
1507             = bfd_reloc_type_lookup (stdoutput,
1508                                      (bfd_reloc_code_real_type) *codes[3]);
1509           relocs[3]->address = fixp->fx_frag->fr_address + fixp->fx_where;
1510           relocs[3]->addend = 0;
1511           relocs[4]->sym_ptr_ptr
1512             = (asymbol **) bfd_abs_section_ptr->symbol_ptr_ptr;
1513           relocs[4]->howto
1514             = bfd_reloc_type_lookup (stdoutput,
1515                                      (bfd_reloc_code_real_type) *codes[4]);
1516           relocs[4]->address = fixp->fx_frag->fr_address + fixp->fx_where;
1517           relocs[4]->addend = 0;
1518           goto done;
1519         case R_PCREL_CALL:
1520         case R_ABS_CALL:
1521           relocs[i]->addend = HPPA_R_ADDEND (hppa_fixp->fx_arg_reloc, 0);
1522           break;
1523
1524         case R_DLT_REL:
1525         case R_DATA_PLABEL:
1526         case R_CODE_PLABEL:
1527           /* For plabel relocations, the addend of the
1528              relocation should be either 0 (no static link) or 2
1529              (static link required).
1530
1531              FIXME: We always assume no static link!
1532
1533              We also slam a zero addend into the DLT relative relocs;
1534              it doesn't make a lot of sense to use any addend since
1535              it gets you a different (eg unknown) DLT entry.  */
1536           relocs[i]->addend = 0;
1537           break;
1538
1539         case R_N_MODE:
1540         case R_S_MODE:
1541         case R_D_MODE:
1542         case R_R_MODE:
1543         case R_FSEL:
1544         case R_LSEL:
1545         case R_RSEL:
1546         case R_BEGIN_BRTAB:
1547         case R_END_BRTAB:
1548         case R_BEGIN_TRY:
1549         case R_N0SEL:
1550         case R_N1SEL:
1551           /* There is no symbol or addend associated with these fixups.  */
1552           relocs[i]->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1553           *relocs[i]->sym_ptr_ptr = symbol_get_bfdsym (dummy_symbol);
1554           relocs[i]->addend = 0;
1555           break;
1556
1557         case R_END_TRY:
1558         case R_ENTRY:
1559         case R_EXIT:
1560           /* There is no symbol associated with these fixups.  */
1561           relocs[i]->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
1562           *relocs[i]->sym_ptr_ptr = symbol_get_bfdsym (dummy_symbol);
1563           relocs[i]->addend = fixp->fx_offset;
1564           break;
1565
1566         default:
1567           relocs[i]->addend = fixp->fx_offset;
1568         }
1569     }
1570
1571  done:
1572 #endif
1573
1574   return relocs;
1575 }
1576
1577 /* Process any machine dependent frag types.  */
1578
1579 void
1580 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
1581                  asection *sec ATTRIBUTE_UNUSED,
1582                  fragS *fragP)
1583 {
1584   unsigned int address;
1585
1586   if (fragP->fr_type == rs_machine_dependent)
1587     {
1588       switch ((int) fragP->fr_subtype)
1589         {
1590         case 0:
1591           fragP->fr_type = rs_fill;
1592           know (fragP->fr_var == 1);
1593           know (fragP->fr_next);
1594           address = fragP->fr_address + fragP->fr_fix;
1595           if (address % fragP->fr_offset)
1596             {
1597               fragP->fr_offset =
1598                 fragP->fr_next->fr_address
1599                 - fragP->fr_address
1600                 - fragP->fr_fix;
1601             }
1602           else
1603             fragP->fr_offset = 0;
1604           break;
1605         }
1606     }
1607 }
1608
1609 /* Round up a section size to the appropriate boundary.  */
1610
1611 valueT
1612 md_section_align (asection *segment, valueT size)
1613 {
1614   int align = bfd_get_section_alignment (stdoutput, segment);
1615   int align2 = (1 << align) - 1;
1616
1617   return (size + align2) & ~align2;
1618 }
1619
1620 /* Return the approximate size of a frag before relaxation has occurred.  */
1621
1622 int
1623 md_estimate_size_before_relax (fragS *fragP, asection *segment ATTRIBUTE_UNUSED)
1624 {
1625   int size;
1626
1627   size = 0;
1628
1629   while ((fragP->fr_fix + size) % fragP->fr_offset)
1630     size++;
1631
1632   return size;
1633 }
1634 \f
1635 #ifdef OBJ_ELF
1636 # ifdef WARN_COMMENTS
1637 const char *md_shortopts = "Vc";
1638 # else
1639 const char *md_shortopts = "V";
1640 # endif
1641 #else
1642 # ifdef WARN_COMMENTS
1643 const char *md_shortopts = "c";
1644 # else
1645 const char *md_shortopts = "";
1646 # endif
1647 #endif
1648
1649 struct option md_longopts[] =
1650 {
1651 #ifdef WARN_COMMENTS
1652   {"warn-comment", no_argument, NULL, 'c'},
1653 #endif
1654   {NULL, no_argument, NULL, 0}
1655 };
1656 size_t md_longopts_size = sizeof (md_longopts);
1657
1658 int
1659 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
1660 {
1661   switch (c)
1662     {
1663     default:
1664       return 0;
1665
1666 #ifdef OBJ_ELF
1667     case 'V':
1668       print_version_id ();
1669       break;
1670 #endif
1671 #ifdef WARN_COMMENTS
1672     case 'c':
1673       warn_comment = 1;
1674       break;
1675 #endif
1676     }
1677
1678   return 1;
1679 }
1680
1681 void
1682 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
1683 {
1684 #ifdef OBJ_ELF
1685   fprintf (stream, _("\
1686   -Q                      ignored\n"));
1687 #endif
1688 #ifdef WARN_COMMENTS
1689   fprintf (stream, _("\
1690   -c                      print a warning if a comment is found\n"));
1691 #endif
1692 }
1693 \f
1694 /* We have no need to default values of symbols.  */
1695
1696 symbolS *
1697 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1698 {
1699   return NULL;
1700 }
1701
1702 #if defined (OBJ_SOM) || defined (ELF_ARG_RELOC)
1703 #define nonzero_dibits(x) \
1704   ((x) | (((x) & 0x55555555) << 1) | (((x) & 0xAAAAAAAA) >> 1))
1705 #define arg_reloc_stub_needed(CALLER, CALLEE) \
1706   (((CALLER) ^ (CALLEE)) & nonzero_dibits (CALLER) & nonzero_dibits (CALLEE))
1707 #else
1708 #define arg_reloc_stub_needed(CALLER, CALLEE) 0
1709 #endif
1710
1711 /* Apply a fixup to an instruction.  */
1712
1713 void
1714 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
1715 {
1716   char *fixpos;
1717   struct hppa_fix_struct *hppa_fixP;
1718   offsetT new_val;
1719   int insn, val, fmt;
1720
1721   /* SOM uses R_HPPA_ENTRY and R_HPPA_EXIT relocations which can
1722      never be "applied" (they are just markers).  Likewise for
1723      R_HPPA_BEGIN_BRTAB and R_HPPA_END_BRTAB.  */
1724 #ifdef OBJ_SOM
1725   if (fixP->fx_r_type == R_HPPA_ENTRY
1726       || fixP->fx_r_type == R_HPPA_EXIT
1727       || fixP->fx_r_type == R_HPPA_BEGIN_BRTAB
1728       || fixP->fx_r_type == R_HPPA_END_BRTAB
1729       || fixP->fx_r_type == R_HPPA_BEGIN_TRY)
1730     return;
1731
1732   /* Disgusting.  We must set fx_offset ourselves -- R_HPPA_END_TRY
1733      fixups are considered not adjustable, which in turn causes
1734      adjust_reloc_syms to not set fx_offset.  Ugh.  */
1735   if (fixP->fx_r_type == R_HPPA_END_TRY)
1736     {
1737       fixP->fx_offset = * valP;
1738       return;
1739     }
1740 #endif
1741 #ifdef OBJ_ELF
1742   if (fixP->fx_r_type == (int) R_PARISC_GNU_VTENTRY
1743       || fixP->fx_r_type == (int) R_PARISC_GNU_VTINHERIT)
1744     return;
1745 #endif
1746
1747   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1748     fixP->fx_done = 1;
1749
1750   /* There should be a HPPA specific fixup associated with the GAS fixup.  */
1751   hppa_fixP = (struct hppa_fix_struct *) fixP->tc_fix_data;
1752   if (hppa_fixP == NULL)
1753     {
1754       as_bad_where (fixP->fx_file, fixP->fx_line,
1755                     _("no hppa_fixup entry for fixup type 0x%x"),
1756                     fixP->fx_r_type);
1757       return;
1758     }
1759
1760   fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
1761
1762   if (fixP->fx_size != 4 || hppa_fixP->fx_r_format == 32)
1763     {
1764       /* Handle constant output. */
1765       number_to_chars_bigendian (fixpos, *valP, fixP->fx_size);
1766       return;
1767     }
1768
1769   insn = bfd_get_32 (stdoutput, fixpos);
1770   fmt = bfd_hppa_insn2fmt (stdoutput, insn);
1771
1772   /* If there is a symbol associated with this fixup, then it's something
1773      which will need a SOM relocation (except for some PC-relative relocs).
1774      In such cases we should treat the "val" or "addend" as zero since it
1775      will be added in as needed from fx_offset in tc_gen_reloc.  */
1776   if ((fixP->fx_addsy != NULL
1777        || fixP->fx_r_type == (int) R_HPPA_NONE)
1778 #ifdef OBJ_SOM
1779       && fmt != 32
1780 #endif
1781       )
1782     new_val = ((fmt == 12 || fmt == 17 || fmt == 22) ? 8 : 0);
1783 #ifdef OBJ_SOM
1784   /* These field selectors imply that we do not want an addend.  */
1785   else if (hppa_fixP->fx_r_field == e_psel
1786            || hppa_fixP->fx_r_field == e_rpsel
1787            || hppa_fixP->fx_r_field == e_lpsel
1788            || hppa_fixP->fx_r_field == e_tsel
1789            || hppa_fixP->fx_r_field == e_rtsel
1790            || hppa_fixP->fx_r_field == e_ltsel)
1791     new_val = ((fmt == 12 || fmt == 17 || fmt == 22) ? 8 : 0);
1792 #endif
1793   else
1794     new_val = hppa_field_adjust (* valP, 0, hppa_fixP->fx_r_field);
1795
1796   /* Handle pc-relative exceptions from above.  */
1797   if ((fmt == 12 || fmt == 17 || fmt == 22)
1798       && fixP->fx_addsy
1799       && fixP->fx_pcrel
1800       && !arg_reloc_stub_needed (symbol_arg_reloc_info (fixP->fx_addsy),
1801                                  hppa_fixP->fx_arg_reloc)
1802 #ifdef OBJ_ELF
1803       && (* valP - 8 + 8192 < 16384
1804           || (fmt == 17 && * valP - 8 + 262144 < 524288)
1805           || (fmt == 22 && * valP - 8 + 8388608 < 16777216))
1806 #endif
1807 #ifdef OBJ_SOM
1808       && (* valP - 8 + 262144 < 524288
1809           || (fmt == 22 && * valP - 8 + 8388608 < 16777216))
1810 #endif
1811       && !S_IS_EXTERNAL (fixP->fx_addsy)
1812       && !S_IS_WEAK (fixP->fx_addsy)
1813       && S_GET_SEGMENT (fixP->fx_addsy) == hppa_fixP->segment
1814       && !(fixP->fx_subsy
1815            && S_GET_SEGMENT (fixP->fx_subsy) != hppa_fixP->segment))
1816     {
1817       new_val = hppa_field_adjust (* valP, 0, hppa_fixP->fx_r_field);
1818     }
1819
1820   switch (fmt)
1821     {
1822     case 10:
1823       CHECK_FIELD_WHERE (new_val, 8191, -8192,
1824                          fixP->fx_file, fixP->fx_line);
1825       val = new_val;
1826
1827       insn = (insn & ~ 0x3ff1) | (((val & 0x1ff8) << 1)
1828                                   | ((val & 0x2000) >> 13));
1829       break;
1830     case -11:
1831       CHECK_FIELD_WHERE (new_val, 8191, -8192,
1832                          fixP->fx_file, fixP->fx_line);
1833       val = new_val;
1834
1835       insn = (insn & ~ 0x3ff9) | (((val & 0x1ffc) << 1)
1836                                   | ((val & 0x2000) >> 13));
1837       break;
1838       /* Handle all opcodes with the 'j' operand type.  */
1839     case 14:
1840       CHECK_FIELD_WHERE (new_val, 8191, -8192,
1841                          fixP->fx_file, fixP->fx_line);
1842       val = new_val;
1843
1844       insn = ((insn & ~ 0x3fff) | low_sign_unext (val, 14));
1845       break;
1846
1847       /* Handle all opcodes with the 'k' operand type.  */
1848     case 21:
1849       CHECK_FIELD_WHERE (new_val, 1048575, -1048576,
1850                          fixP->fx_file, fixP->fx_line);
1851       val = new_val;
1852
1853       insn = (insn & ~ 0x1fffff) | re_assemble_21 (val);
1854       break;
1855
1856       /* Handle all the opcodes with the 'i' operand type.  */
1857     case 11:
1858       CHECK_FIELD_WHERE (new_val, 1023, -1024,
1859                          fixP->fx_file, fixP->fx_line);
1860       val = new_val;
1861
1862       insn = (insn & ~ 0x7ff) | low_sign_unext (val, 11);
1863       break;
1864
1865       /* Handle all the opcodes with the 'w' operand type.  */
1866     case 12:
1867       CHECK_FIELD_WHERE (new_val - 8, 8191, -8192,
1868                          fixP->fx_file, fixP->fx_line);
1869       val = new_val - 8;
1870
1871       insn = (insn & ~ 0x1ffd) | re_assemble_12 (val >> 2);
1872       break;
1873
1874       /* Handle some of the opcodes with the 'W' operand type.  */
1875     case 17:
1876       {
1877         offsetT distance = * valP;
1878
1879         /* If this is an absolute branch (ie no link) with an out of
1880            range target, then we want to complain.  */
1881         if (fixP->fx_r_type == (int) R_HPPA_PCREL_CALL
1882             && (insn & 0xffe00000) == 0xe8000000)
1883           CHECK_FIELD_WHERE (distance - 8, 262143, -262144,
1884                              fixP->fx_file, fixP->fx_line);
1885
1886         CHECK_FIELD_WHERE (new_val - 8, 262143, -262144,
1887                            fixP->fx_file, fixP->fx_line);
1888         val = new_val - 8;
1889
1890         insn = (insn & ~ 0x1f1ffd) | re_assemble_17 (val >> 2);
1891         break;
1892       }
1893
1894     case 22:
1895       {
1896         offsetT distance = * valP;
1897
1898         /* If this is an absolute branch (ie no link) with an out of
1899            range target, then we want to complain.  */
1900         if (fixP->fx_r_type == (int) R_HPPA_PCREL_CALL
1901             && (insn & 0xffe00000) == 0xe8000000)
1902           CHECK_FIELD_WHERE (distance - 8, 8388607, -8388608,
1903                              fixP->fx_file, fixP->fx_line);
1904
1905         CHECK_FIELD_WHERE (new_val - 8, 8388607, -8388608,
1906                            fixP->fx_file, fixP->fx_line);
1907         val = new_val - 8;
1908
1909         insn = (insn & ~ 0x3ff1ffd) | re_assemble_22 (val >> 2);
1910         break;
1911       }
1912
1913     case -10:
1914       val = new_val;
1915       insn = (insn & ~ 0xfff1) | re_assemble_16 (val & -8);
1916       break;
1917
1918     case -16:
1919       val = new_val;
1920       insn = (insn & ~ 0xfff9) | re_assemble_16 (val & -4);
1921       break;
1922
1923     case 16:
1924       val = new_val;
1925       insn = (insn & ~ 0xffff) | re_assemble_16 (val);
1926       break;
1927
1928     case 32:
1929       insn = new_val;
1930       break;
1931
1932     default:
1933       as_bad_where (fixP->fx_file, fixP->fx_line,
1934                     _("Unknown relocation encountered in md_apply_fix."));
1935       return;
1936     }
1937
1938 #ifdef OBJ_ELF
1939   switch (fixP->fx_r_type)
1940     {
1941       case R_PARISC_TLS_GD21L:
1942       case R_PARISC_TLS_GD14R:
1943       case R_PARISC_TLS_LDM21L:
1944       case R_PARISC_TLS_LDM14R:
1945       case R_PARISC_TLS_LE21L:
1946       case R_PARISC_TLS_LE14R:
1947       case R_PARISC_TLS_IE21L:
1948       case R_PARISC_TLS_IE14R:
1949         if (fixP->fx_addsy)
1950           S_SET_THREAD_LOCAL (fixP->fx_addsy);
1951         break;
1952       default:
1953         break;
1954     }
1955 #endif
1956
1957   /* Insert the relocation.  */
1958   bfd_put_32 (stdoutput, insn, fixpos);
1959 }
1960
1961 /* Exactly what point is a PC-relative offset relative TO?
1962    On the PA, they're relative to the address of the offset.  */
1963
1964 long
1965 md_pcrel_from (fixS *fixP)
1966 {
1967   return fixP->fx_where + fixP->fx_frag->fr_address;
1968 }
1969
1970 /* Return nonzero if the input line pointer is at the end of
1971    a statement.  */
1972
1973 static int
1974 is_end_of_statement (void)
1975 {
1976   return ((*input_line_pointer == '\n')
1977           || (*input_line_pointer == ';')
1978           || (*input_line_pointer == '!'));
1979 }
1980
1981 #define REG_NAME_CNT    (sizeof (pre_defined_registers) / sizeof (struct pd_reg))
1982
1983 /* Given NAME, find the register number associated with that name, return
1984    the integer value associated with the given name or -1 on failure.  */
1985
1986 static int
1987 reg_name_search (char *name)
1988 {
1989   int middle, low, high;
1990   int cmp;
1991
1992   low = 0;
1993   high = REG_NAME_CNT - 1;
1994
1995   do
1996     {
1997       middle = (low + high) / 2;
1998       cmp = strcasecmp (name, pre_defined_registers[middle].name);
1999       if (cmp < 0)
2000         high = middle - 1;
2001       else if (cmp > 0)
2002         low = middle + 1;
2003       else
2004         return pre_defined_registers[middle].value;
2005     }
2006   while (low <= high);
2007
2008   return -1;
2009 }
2010
2011 /* Read a number from S.  The number might come in one of many forms,
2012    the most common will be a hex or decimal constant, but it could be
2013    a pre-defined register (Yuk!), or an absolute symbol.
2014
2015    Return 1 on success or 0 on failure.  If STRICT, then a missing
2016    register prefix will cause a failure.  The number itself is
2017    returned in `pa_number'.
2018
2019    IS_FLOAT indicates that a PA-89 FP register number should be
2020    parsed;  A `l' or `r' suffix is checked for if but 2 of IS_FLOAT is
2021    not set.
2022
2023    pa_parse_number can not handle negative constants and will fail
2024    horribly if it is passed such a constant.  */
2025
2026 static int
2027 pa_parse_number (char **s, int is_float)
2028 {
2029   int num;
2030   char *name;
2031   char c;
2032   symbolS *sym;
2033   int status;
2034   char *p = *s;
2035   bfd_boolean have_prefix;
2036
2037   /* Skip whitespace before the number.  */
2038   while (*p == ' ' || *p == '\t')
2039     p = p + 1;
2040
2041   pa_number = -1;
2042   have_prefix = 0;
2043   num = 0;
2044   if (!strict && ISDIGIT (*p))
2045     {
2046       /* Looks like a number.  */
2047
2048       if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X'))
2049         {
2050           /* The number is specified in hex.  */
2051           p += 2;
2052           while (ISDIGIT (*p) || ((*p >= 'a') && (*p <= 'f'))
2053                  || ((*p >= 'A') && (*p <= 'F')))
2054             {
2055               if (ISDIGIT (*p))
2056                 num = num * 16 + *p - '0';
2057               else if (*p >= 'a' && *p <= 'f')
2058                 num = num * 16 + *p - 'a' + 10;
2059               else
2060                 num = num * 16 + *p - 'A' + 10;
2061               ++p;
2062             }
2063         }
2064       else
2065         {
2066           /* The number is specified in decimal.  */
2067           while (ISDIGIT (*p))
2068             {
2069               num = num * 10 + *p - '0';
2070               ++p;
2071             }
2072         }
2073
2074       pa_number = num;
2075
2076       /* Check for a `l' or `r' suffix.  */
2077       if (is_float)
2078         {
2079           pa_number += FP_REG_BASE;
2080           if (! (is_float & 2))
2081             {
2082               if (IS_R_SELECT (p))
2083                 {
2084                   pa_number += FP_REG_RSEL;
2085                   ++p;
2086                 }
2087               else if (IS_L_SELECT (p))
2088                 {
2089                   ++p;
2090                 }
2091             }
2092         }
2093     }
2094   else if (*p == '%')
2095     {
2096       /* The number might be a predefined register.  */
2097       have_prefix = 1;
2098       name = p;
2099       p++;
2100       c = *p;
2101       /* Tege hack: Special case for general registers as the general
2102          code makes a binary search with case translation, and is VERY
2103          slow.  */
2104       if (c == 'r')
2105         {
2106           p++;
2107           if (*p == 'e' && *(p + 1) == 't'
2108               && (*(p + 2) == '0' || *(p + 2) == '1'))
2109             {
2110               p += 2;
2111               num = *p - '0' + 28;
2112               p++;
2113             }
2114           else if (*p == 'p')
2115             {
2116               num = 2;
2117               p++;
2118             }
2119           else if (!ISDIGIT (*p))
2120             {
2121               if (print_errors)
2122                 as_bad (_("Undefined register: '%s'."), name);
2123               num = -1;
2124             }
2125           else
2126             {
2127               do
2128                 num = num * 10 + *p++ - '0';
2129               while (ISDIGIT (*p));
2130             }
2131         }
2132       else
2133         {
2134           /* Do a normal register search.  */
2135           while (is_part_of_name (c))
2136             {
2137               p = p + 1;
2138               c = *p;
2139             }
2140           *p = 0;
2141           status = reg_name_search (name);
2142           if (status >= 0)
2143             num = status;
2144           else
2145             {
2146               if (print_errors)
2147                 as_bad (_("Undefined register: '%s'."), name);
2148               num = -1;
2149             }
2150           *p = c;
2151         }
2152
2153       pa_number = num;
2154     }
2155   else
2156     {
2157       /* And finally, it could be a symbol in the absolute section which
2158          is effectively a constant, or a register alias symbol.  */
2159       name = p;
2160       c = *p;
2161       while (is_part_of_name (c))
2162         {
2163           p = p + 1;
2164           c = *p;
2165         }
2166       *p = 0;
2167       if ((sym = symbol_find (name)) != NULL)
2168         {
2169           if (S_GET_SEGMENT (sym) == reg_section)
2170             {
2171               num = S_GET_VALUE (sym);
2172               /* Well, we don't really have one, but we do have a
2173                  register, so...  */
2174               have_prefix = TRUE;
2175             }
2176           else if (S_GET_SEGMENT (sym) == &bfd_abs_section)
2177             num = S_GET_VALUE (sym);
2178           else if (!strict)
2179             {
2180               if (print_errors)
2181                 as_bad (_("Non-absolute symbol: '%s'."), name);
2182               num = -1;
2183             }
2184         }
2185       else if (!strict)
2186         {
2187           /* There is where we'd come for an undefined symbol
2188              or for an empty string.  For an empty string we
2189              will return zero.  That's a concession made for
2190              compatibility with the braindamaged HP assemblers.  */
2191           if (*name == 0)
2192             num = 0;
2193           else
2194             {
2195               if (print_errors)
2196                 as_bad (_("Undefined absolute constant: '%s'."), name);
2197               num = -1;
2198             }
2199         }
2200       *p = c;
2201
2202       pa_number = num;
2203     }
2204
2205   if (!strict || have_prefix)
2206     {
2207       *s = p;
2208       return 1;
2209     }
2210   return 0;
2211 }
2212
2213 /* Return nonzero if the given INSN and L/R information will require
2214    a new PA-1.1 opcode.  */
2215
2216 static int
2217 need_pa11_opcode (void)
2218 {
2219   if ((pa_number & FP_REG_RSEL) != 0
2220       && !(the_insn.fpof1 == DBL && the_insn.fpof2 == DBL))
2221     {
2222       /* If this instruction is specific to a particular architecture,
2223          then set a new architecture.  */
2224       if (bfd_get_mach (stdoutput) < pa11)
2225         {
2226           if (!bfd_set_arch_mach (stdoutput, bfd_arch_hppa, pa11))
2227             as_warn (_("could not update architecture and machine"));
2228         }
2229       return TRUE;
2230     }
2231   else
2232     return FALSE;
2233 }
2234
2235 /* Parse a condition for a fcmp instruction.  Return the numerical
2236    code associated with the condition.  */
2237
2238 static int
2239 pa_parse_fp_cmp_cond (char **s)
2240 {
2241   int cond, i;
2242
2243   cond = 0;
2244
2245   for (i = 0; i < 32; i++)
2246     {
2247       if (strncasecmp (*s, fp_cond_map[i].string,
2248                        strlen (fp_cond_map[i].string)) == 0)
2249         {
2250           cond = fp_cond_map[i].cond;
2251           *s += strlen (fp_cond_map[i].string);
2252           /* If not a complete match, back up the input string and
2253              report an error.  */
2254           if (**s != ' ' && **s != '\t')
2255             {
2256               *s -= strlen (fp_cond_map[i].string);
2257               break;
2258             }
2259           while (**s == ' ' || **s == '\t')
2260             *s = *s + 1;
2261           return cond;
2262         }
2263     }
2264
2265   as_bad (_("Invalid FP Compare Condition: %s"), *s);
2266
2267   /* Advance over the bogus completer.  */
2268   while (**s != ',' && **s != ' ' && **s != '\t')
2269     *s += 1;
2270
2271   return 0;
2272 }
2273
2274 /* Parse a graphics test complete for ftest.  */
2275
2276 static int
2277 pa_parse_ftest_gfx_completer (char **s)
2278 {
2279   int value;
2280
2281   value = 0;
2282   if (strncasecmp (*s, "acc8", 4) == 0)
2283     {
2284       value = 5;
2285       *s += 4;
2286     }
2287   else if (strncasecmp (*s, "acc6", 4) == 0)
2288     {
2289       value = 9;
2290       *s += 4;
2291     }
2292   else if (strncasecmp (*s, "acc4", 4) == 0)
2293     {
2294       value = 13;
2295       *s += 4;
2296     }
2297   else if (strncasecmp (*s, "acc2", 4) == 0)
2298     {
2299       value = 17;
2300       *s += 4;
2301     }
2302   else if (strncasecmp (*s, "acc", 3) == 0)
2303     {
2304       value = 1;
2305       *s += 3;
2306     }
2307   else if (strncasecmp (*s, "rej8", 4) == 0)
2308     {
2309       value = 6;
2310       *s += 4;
2311     }
2312   else if (strncasecmp (*s, "rej", 3) == 0)
2313     {
2314       value = 2;
2315       *s += 3;
2316     }
2317   else
2318     {
2319       value = 0;
2320       as_bad (_("Invalid FTEST completer: %s"), *s);
2321     }
2322
2323   return value;
2324 }
2325
2326 /* Parse an FP operand format completer returning the completer
2327    type.  */
2328
2329 static fp_operand_format
2330 pa_parse_fp_cnv_format (char **s)
2331 {
2332   int format;
2333
2334   format = SGL;
2335   if (**s == ',')
2336     {
2337       *s += 1;
2338       if (strncasecmp (*s, "sgl", 3) == 0)
2339         {
2340           format = SGL;
2341           *s += 4;
2342         }
2343       else if (strncasecmp (*s, "dbl", 3) == 0)
2344         {
2345           format = DBL;
2346           *s += 4;
2347         }
2348       else if (strncasecmp (*s, "quad", 4) == 0)
2349         {
2350           format = QUAD;
2351           *s += 5;
2352         }
2353       else if (strncasecmp (*s, "w", 1) == 0)
2354         {
2355           format = W;
2356           *s += 2;
2357         }
2358       else if (strncasecmp (*s, "uw", 2) == 0)
2359         {
2360           format = UW;
2361           *s += 3;
2362         }
2363       else if (strncasecmp (*s, "dw", 2) == 0)
2364         {
2365           format = DW;
2366           *s += 3;
2367         }
2368       else if (strncasecmp (*s, "udw", 3) == 0)
2369         {
2370           format = UDW;
2371           *s += 4;
2372         }
2373       else if (strncasecmp (*s, "qw", 2) == 0)
2374         {
2375           format = QW;
2376           *s += 3;
2377         }
2378       else if (strncasecmp (*s, "uqw", 3) == 0)
2379         {
2380           format = UQW;
2381           *s += 4;
2382         }
2383       else
2384         {
2385           format = ILLEGAL_FMT;
2386           as_bad (_("Invalid FP Operand Format: %3s"), *s);
2387         }
2388     }
2389
2390   return format;
2391 }
2392
2393 /* Parse an FP operand format completer returning the completer
2394    type.  */
2395
2396 static fp_operand_format
2397 pa_parse_fp_format (char **s)
2398 {
2399   int format;
2400
2401   format = SGL;
2402   if (**s == ',')
2403     {
2404       *s += 1;
2405       if (strncasecmp (*s, "sgl", 3) == 0)
2406         {
2407           format = SGL;
2408           *s += 4;
2409         }
2410       else if (strncasecmp (*s, "dbl", 3) == 0)
2411         {
2412           format = DBL;
2413           *s += 4;
2414         }
2415       else if (strncasecmp (*s, "quad", 4) == 0)
2416         {
2417           format = QUAD;
2418           *s += 5;
2419         }
2420       else
2421         {
2422           format = ILLEGAL_FMT;
2423           as_bad (_("Invalid FP Operand Format: %3s"), *s);
2424         }
2425     }
2426
2427   return format;
2428 }
2429
2430 /* Convert from a selector string into a selector type.  */
2431
2432 static int
2433 pa_chk_field_selector (char **str)
2434 {
2435   int middle, low, high;
2436   int cmp;
2437   char name[4];
2438
2439   /* Read past any whitespace.  */
2440   /* FIXME: should we read past newlines and formfeeds??? */
2441   while (**str == ' ' || **str == '\t' || **str == '\n' || **str == '\f')
2442     *str = *str + 1;
2443
2444   if ((*str)[1] == '\'' || (*str)[1] == '%')
2445     name[0] = TOLOWER ((*str)[0]),
2446     name[1] = 0;
2447   else if ((*str)[2] == '\'' || (*str)[2] == '%')
2448     name[0] = TOLOWER ((*str)[0]),
2449     name[1] = TOLOWER ((*str)[1]),
2450     name[2] = 0;
2451   else if ((*str)[3] == '\'' || (*str)[3] == '%')
2452     name[0] = TOLOWER ((*str)[0]),
2453     name[1] = TOLOWER ((*str)[1]),
2454     name[2] = TOLOWER ((*str)[2]),
2455     name[3] = 0;
2456   else
2457     return e_fsel;
2458
2459   low = 0;
2460   high = sizeof (selector_table) / sizeof (struct selector_entry) - 1;
2461
2462   do
2463     {
2464       middle = (low + high) / 2;
2465       cmp = strcmp (name, selector_table[middle].prefix);
2466       if (cmp < 0)
2467         high = middle - 1;
2468       else if (cmp > 0)
2469         low = middle + 1;
2470       else
2471         {
2472           *str += strlen (name) + 1;
2473 #ifndef OBJ_SOM
2474           if (selector_table[middle].field_selector == e_nsel)
2475             return e_fsel;
2476 #endif
2477           return selector_table[middle].field_selector;
2478         }
2479     }
2480   while (low <= high);
2481
2482   return e_fsel;
2483 }
2484
2485 /* Parse a .byte, .word, .long expression for the HPPA.  Called by
2486    cons via the TC_PARSE_CONS_EXPRESSION macro.  */
2487
2488 void
2489 parse_cons_expression_hppa (expressionS *exp)
2490 {
2491   hppa_field_selector = pa_chk_field_selector (&input_line_pointer);
2492   expression (exp);
2493 }
2494
2495 /* Evaluate an absolute expression EXP which may be modified by
2496    the selector FIELD_SELECTOR.  Return the value of the expression.  */
2497 static int
2498 evaluate_absolute (struct pa_it *insn)
2499 {
2500   offsetT value;
2501   expressionS exp;
2502   int field_selector = insn->field_selector;
2503
2504   exp = insn->exp;
2505   value = exp.X_add_number;
2506
2507   return hppa_field_adjust (0, value, field_selector);
2508 }
2509
2510 /* Mark (via expr_end) the end of an absolute expression.  FIXME.  */
2511
2512 static int
2513 pa_get_absolute_expression (struct pa_it *insn, char **strp)
2514 {
2515   char *save_in;
2516
2517   insn->field_selector = pa_chk_field_selector (strp);
2518   save_in = input_line_pointer;
2519   input_line_pointer = *strp;
2520   expression (&insn->exp);
2521   /* This is not perfect, but is a huge improvement over doing nothing.
2522
2523      The PA assembly syntax is ambiguous in a variety of ways.  Consider
2524      this string "4 %r5"  Is that the number 4 followed by the register
2525      r5, or is that 4 MOD r5?
2526
2527      If we get a modulo expression when looking for an absolute, we try
2528      again cutting off the input string at the first whitespace character.  */
2529   if (insn->exp.X_op == O_modulus)
2530     {
2531       char *s, c;
2532       int retval;
2533
2534       input_line_pointer = *strp;
2535       s = *strp;
2536       while (*s != ',' && *s != ' ' && *s != '\t')
2537         s++;
2538
2539       c = *s;
2540       *s = 0;
2541
2542       retval = pa_get_absolute_expression (insn, strp);
2543
2544       input_line_pointer = save_in;
2545       *s = c;
2546       return evaluate_absolute (insn);
2547     }
2548   /* When in strict mode we have a non-match, fix up the pointers
2549      and return to our caller.  */
2550   if (insn->exp.X_op != O_constant && strict)
2551     {
2552       expr_end = input_line_pointer;
2553       input_line_pointer = save_in;
2554       return 0;
2555     }
2556   if (insn->exp.X_op != O_constant)
2557     {
2558       as_bad (_("Bad segment (should be absolute)."));
2559       expr_end = input_line_pointer;
2560       input_line_pointer = save_in;
2561       return 0;
2562     }
2563   expr_end = input_line_pointer;
2564   input_line_pointer = save_in;
2565   return evaluate_absolute (insn);
2566 }
2567
2568 /* Given an argument location specification return the associated
2569    argument location number.  */
2570
2571 static unsigned int
2572 pa_build_arg_reloc (char *type_name)
2573 {
2574
2575   if (strncasecmp (type_name, "no", 2) == 0)
2576     return 0;
2577   if (strncasecmp (type_name, "gr", 2) == 0)
2578     return 1;
2579   else if (strncasecmp (type_name, "fr", 2) == 0)
2580     return 2;
2581   else if (strncasecmp (type_name, "fu", 2) == 0)
2582     return 3;
2583   else
2584     as_bad (_("Invalid argument location: %s\n"), type_name);
2585
2586   return 0;
2587 }
2588
2589 /* Encode and return an argument relocation specification for
2590    the given register in the location specified by arg_reloc.  */
2591
2592 static unsigned int
2593 pa_align_arg_reloc (unsigned int reg, unsigned int arg_reloc)
2594 {
2595   unsigned int new_reloc;
2596
2597   new_reloc = arg_reloc;
2598   switch (reg)
2599     {
2600     case 0:
2601       new_reloc <<= 8;
2602       break;
2603     case 1:
2604       new_reloc <<= 6;
2605       break;
2606     case 2:
2607       new_reloc <<= 4;
2608       break;
2609     case 3:
2610       new_reloc <<= 2;
2611       break;
2612     default:
2613       as_bad (_("Invalid argument description: %d"), reg);
2614     }
2615
2616   return new_reloc;
2617 }
2618
2619 /* Parse a non-negated compare/subtract completer returning the
2620    number (for encoding in instructions) of the given completer.  */
2621
2622 static int
2623 pa_parse_nonneg_cmpsub_cmpltr (char **s)
2624 {
2625   int cmpltr;
2626   char *name = *s + 1;
2627   char c;
2628   char *save_s = *s;
2629   int nullify = 0;
2630
2631   cmpltr = 0;
2632   if (**s == ',')
2633     {
2634       *s += 1;
2635       while (**s != ',' && **s != ' ' && **s != '\t')
2636         *s += 1;
2637       c = **s;
2638       **s = 0x00;
2639
2640       if (strcmp (name, "=") == 0)
2641         {
2642           cmpltr = 1;
2643         }
2644       else if (strcmp (name, "<") == 0)
2645         {
2646           cmpltr = 2;
2647         }
2648       else if (strcmp (name, "<=") == 0)
2649         {
2650           cmpltr = 3;
2651         }
2652       else if (strcmp (name, "<<") == 0)
2653         {
2654           cmpltr = 4;
2655         }
2656       else if (strcmp (name, "<<=") == 0)
2657         {
2658           cmpltr = 5;
2659         }
2660       else if (strcasecmp (name, "sv") == 0)
2661         {
2662           cmpltr = 6;
2663         }
2664       else if (strcasecmp (name, "od") == 0)
2665         {
2666           cmpltr = 7;
2667         }
2668       /* If we have something like addb,n then there is no condition
2669          completer.  */
2670       else if (strcasecmp (name, "n") == 0)
2671         {
2672           cmpltr = 0;
2673           nullify = 1;
2674         }
2675       else
2676         {
2677           cmpltr = -1;
2678         }
2679       **s = c;
2680     }
2681
2682   /* Reset pointers if this was really a ,n for a branch instruction.  */
2683   if (nullify)
2684     *s = save_s;
2685
2686   return cmpltr;
2687 }
2688
2689 /* Parse a negated compare/subtract completer returning the
2690    number (for encoding in instructions) of the given completer.  */
2691
2692 static int
2693 pa_parse_neg_cmpsub_cmpltr (char **s)
2694 {
2695   int cmpltr;
2696   char *name = *s + 1;
2697   char c;
2698   char *save_s = *s;
2699   int nullify = 0;
2700
2701   cmpltr = 0;
2702   if (**s == ',')
2703     {
2704       *s += 1;
2705       while (**s != ',' && **s != ' ' && **s != '\t')
2706         *s += 1;
2707       c = **s;
2708       **s = 0x00;
2709
2710       if (strcasecmp (name, "tr") == 0)
2711         {
2712           cmpltr = 0;
2713         }
2714       else if (strcmp (name, "<>") == 0)
2715         {
2716           cmpltr = 1;
2717         }
2718       else if (strcmp (name, ">=") == 0)
2719         {
2720           cmpltr = 2;
2721         }
2722       else if (strcmp (name, ">") == 0)
2723         {
2724           cmpltr = 3;
2725         }
2726       else if (strcmp (name, ">>=") == 0)
2727         {
2728           cmpltr = 4;
2729         }
2730       else if (strcmp (name, ">>") == 0)
2731         {
2732           cmpltr = 5;
2733         }
2734       else if (strcasecmp (name, "nsv") == 0)
2735         {
2736           cmpltr = 6;
2737         }
2738       else if (strcasecmp (name, "ev") == 0)
2739         {
2740           cmpltr = 7;
2741         }
2742       /* If we have something like addb,n then there is no condition
2743          completer.  */
2744       else if (strcasecmp (name, "n") == 0)
2745         {
2746           cmpltr = 0;
2747           nullify = 1;
2748         }
2749       else
2750         {
2751           cmpltr = -1;
2752         }
2753       **s = c;
2754     }
2755
2756   /* Reset pointers if this was really a ,n for a branch instruction.  */
2757   if (nullify)
2758     *s = save_s;
2759
2760   return cmpltr;
2761 }
2762
2763 /* Parse a 64 bit compare and branch completer returning the number (for
2764    encoding in instructions) of the given completer.
2765
2766    Nonnegated comparisons are returned as 0-7, negated comparisons are
2767    returned as 8-15.  */
2768
2769 static int
2770 pa_parse_cmpb_64_cmpltr (char **s)
2771 {
2772   int cmpltr;
2773   char *name = *s + 1;
2774   char c;
2775
2776   cmpltr = -1;
2777   if (**s == ',')
2778     {
2779       *s += 1;
2780       while (**s != ',' && **s != ' ' && **s != '\t')
2781         *s += 1;
2782       c = **s;
2783       **s = 0x00;
2784
2785       if (strcmp (name, "*") == 0)
2786         {
2787           cmpltr = 0;
2788         }
2789       else if (strcmp (name, "*=") == 0)
2790         {
2791           cmpltr = 1;
2792         }
2793       else if (strcmp (name, "*<") == 0)
2794         {
2795           cmpltr = 2;
2796         }
2797       else if (strcmp (name, "*<=") == 0)
2798         {
2799           cmpltr = 3;
2800         }
2801       else if (strcmp (name, "*<<") == 0)
2802         {
2803           cmpltr = 4;
2804         }
2805       else if (strcmp (name, "*<<=") == 0)
2806         {
2807           cmpltr = 5;
2808         }
2809       else if (strcasecmp (name, "*sv") == 0)
2810         {
2811           cmpltr = 6;
2812         }
2813       else if (strcasecmp (name, "*od") == 0)
2814         {
2815           cmpltr = 7;
2816         }
2817       else if (strcasecmp (name, "*tr") == 0)
2818         {
2819           cmpltr = 8;
2820         }
2821       else if (strcmp (name, "*<>") == 0)
2822         {
2823           cmpltr = 9;
2824         }
2825       else if (strcmp (name, "*>=") == 0)
2826         {
2827           cmpltr = 10;
2828         }
2829       else if (strcmp (name, "*>") == 0)
2830         {
2831           cmpltr = 11;
2832         }
2833       else if (strcmp (name, "*>>=") == 0)
2834         {
2835           cmpltr = 12;
2836         }
2837       else if (strcmp (name, "*>>") == 0)
2838         {
2839           cmpltr = 13;
2840         }
2841       else if (strcasecmp (name, "*nsv") == 0)
2842         {
2843           cmpltr = 14;
2844         }
2845       else if (strcasecmp (name, "*ev") == 0)
2846         {
2847           cmpltr = 15;
2848         }
2849       else
2850         {
2851           cmpltr = -1;
2852         }
2853       **s = c;
2854     }
2855
2856   return cmpltr;
2857 }
2858
2859 /* Parse a 64 bit compare immediate and branch completer returning the number
2860    (for encoding in instructions) of the given completer.  */
2861
2862 static int
2863 pa_parse_cmpib_64_cmpltr (char **s)
2864 {
2865   int cmpltr;
2866   char *name = *s + 1;
2867   char c;
2868
2869   cmpltr = -1;
2870   if (**s == ',')
2871     {
2872       *s += 1;
2873       while (**s != ',' && **s != ' ' && **s != '\t')
2874         *s += 1;
2875       c = **s;
2876       **s = 0x00;
2877
2878       if (strcmp (name, "*<<") == 0)
2879         {
2880           cmpltr = 0;
2881         }
2882       else if (strcmp (name, "*=") == 0)
2883         {
2884           cmpltr = 1;
2885         }
2886       else if (strcmp (name, "*<") == 0)
2887         {
2888           cmpltr = 2;
2889         }
2890       else if (strcmp (name, "*<=") == 0)
2891         {
2892           cmpltr = 3;
2893         }
2894       else if (strcmp (name, "*>>=") == 0)
2895         {
2896           cmpltr = 4;
2897         }
2898       else if (strcmp (name, "*<>") == 0)
2899         {
2900           cmpltr = 5;
2901         }
2902       else if (strcasecmp (name, "*>=") == 0)
2903         {
2904           cmpltr = 6;
2905         }
2906       else if (strcasecmp (name, "*>") == 0)
2907         {
2908           cmpltr = 7;
2909         }
2910       else
2911         {
2912           cmpltr = -1;
2913         }
2914       **s = c;
2915     }
2916
2917   return cmpltr;
2918 }
2919
2920 /* Parse a non-negated addition completer returning the number
2921    (for encoding in instructions) of the given completer.  */
2922
2923 static int
2924 pa_parse_nonneg_add_cmpltr (char **s)
2925 {
2926   int cmpltr;
2927   char *name = *s + 1;
2928   char c;
2929   char *save_s = *s;
2930   int nullify = 0;
2931
2932   cmpltr = 0;
2933   if (**s == ',')
2934     {
2935       *s += 1;
2936       while (**s != ',' && **s != ' ' && **s != '\t')
2937         *s += 1;
2938       c = **s;
2939       **s = 0x00;
2940       if (strcmp (name, "=") == 0)
2941         {
2942           cmpltr = 1;
2943         }
2944       else if (strcmp (name, "<") == 0)
2945         {
2946           cmpltr = 2;
2947         }
2948       else if (strcmp (name, "<=") == 0)
2949         {
2950           cmpltr = 3;
2951         }
2952       else if (strcasecmp (name, "nuv") == 0)
2953         {
2954           cmpltr = 4;
2955         }
2956       else if (strcasecmp (name, "znv") == 0)
2957         {
2958           cmpltr = 5;
2959         }
2960       else if (strcasecmp (name, "sv") == 0)
2961         {
2962           cmpltr = 6;
2963         }
2964       else if (strcasecmp (name, "od") == 0)
2965         {
2966           cmpltr = 7;
2967         }
2968       /* If we have something like addb,n then there is no condition
2969          completer.  */
2970       else if (strcasecmp (name, "n") == 0)
2971         {
2972           cmpltr = 0;
2973           nullify = 1;
2974         }
2975       else
2976         {
2977           cmpltr = -1;
2978         }
2979       **s = c;
2980     }
2981
2982   /* Reset pointers if this was really a ,n for a branch instruction.  */
2983   if (nullify)
2984     *s = save_s;
2985
2986   return cmpltr;
2987 }
2988
2989 /* Parse a negated addition completer returning the number
2990    (for encoding in instructions) of the given completer.  */
2991
2992 static int
2993 pa_parse_neg_add_cmpltr (char **s)
2994 {
2995   int cmpltr;
2996   char *name = *s + 1;
2997   char c;
2998   char *save_s = *s;
2999   int nullify = 0;
3000
3001   cmpltr = 0;
3002   if (**s == ',')
3003     {
3004       *s += 1;
3005       while (**s != ',' && **s != ' ' && **s != '\t')
3006         *s += 1;
3007       c = **s;
3008       **s = 0x00;
3009       if (strcasecmp (name, "tr") == 0)
3010         {
3011           cmpltr = 0;
3012         }
3013       else if (strcmp (name, "<>") == 0)
3014         {
3015           cmpltr = 1;
3016         }
3017       else if (strcmp (name, ">=") == 0)
3018         {
3019           cmpltr = 2;
3020         }
3021       else if (strcmp (name, ">") == 0)
3022         {
3023           cmpltr = 3;
3024         }
3025       else if (strcasecmp (name, "uv") == 0)
3026         {
3027           cmpltr = 4;
3028         }
3029       else if (strcasecmp (name, "vnz") == 0)
3030         {
3031           cmpltr = 5;
3032         }
3033       else if (strcasecmp (name, "nsv") == 0)
3034         {
3035           cmpltr = 6;
3036         }
3037       else if (strcasecmp (name, "ev") == 0)
3038         {
3039           cmpltr = 7;
3040         }
3041       /* If we have something like addb,n then there is no condition
3042          completer.  */
3043       else if (strcasecmp (name, "n") == 0)
3044         {
3045           cmpltr = 0;
3046           nullify = 1;
3047         }
3048       else
3049         {
3050           cmpltr = -1;
3051         }
3052       **s = c;
3053     }
3054
3055   /* Reset pointers if this was really a ,n for a branch instruction.  */
3056   if (nullify)
3057     *s = save_s;
3058
3059   return cmpltr;
3060 }
3061
3062 /* Parse a 64 bit wide mode add and branch completer returning the number (for
3063    encoding in instructions) of the given completer.  */
3064
3065 static int
3066 pa_parse_addb_64_cmpltr (char **s)
3067 {
3068   int cmpltr;
3069   char *name = *s + 1;
3070   char c;
3071   char *save_s = *s;
3072   int nullify = 0;
3073
3074   cmpltr = 0;
3075   if (**s == ',')
3076     {
3077       *s += 1;
3078       while (**s != ',' && **s != ' ' && **s != '\t')
3079         *s += 1;
3080       c = **s;
3081       **s = 0x00;
3082       if (strcmp (name, "=") == 0)
3083         {
3084           cmpltr = 1;
3085         }
3086       else if (strcmp (name, "<") == 0)
3087         {
3088           cmpltr = 2;
3089         }
3090       else if (strcmp (name, "<=") == 0)
3091         {
3092           cmpltr = 3;
3093         }
3094       else if (strcasecmp (name, "nuv") == 0)
3095         {
3096           cmpltr = 4;
3097         }
3098       else if (strcasecmp (name, "*=") == 0)
3099         {
3100           cmpltr = 5;
3101         }
3102       else if (strcasecmp (name, "*<") == 0)
3103         {
3104           cmpltr = 6;
3105         }
3106       else if (strcasecmp (name, "*<=") == 0)
3107         {
3108           cmpltr = 7;
3109         }
3110       else if (strcmp (name, "tr") == 0)
3111         {
3112           cmpltr = 8;
3113         }
3114       else if (strcmp (name, "<>") == 0)
3115         {
3116           cmpltr = 9;
3117         }
3118       else if (strcmp (name, ">=") == 0)
3119         {
3120           cmpltr = 10;
3121         }
3122       else if (strcmp (name, ">") == 0)
3123         {
3124           cmpltr = 11;
3125         }
3126       else if (strcasecmp (name, "uv") == 0)
3127         {
3128           cmpltr = 12;
3129         }
3130       else if (strcasecmp (name, "*<>") == 0)
3131         {
3132           cmpltr = 13;
3133         }
3134       else if (strcasecmp (name, "*>=") == 0)
3135         {
3136           cmpltr = 14;
3137         }
3138       else if (strcasecmp (name, "*>") == 0)
3139         {
3140           cmpltr = 15;
3141         }
3142       /* If we have something like addb,n then there is no condition
3143          completer.  */
3144       else if (strcasecmp (name, "n") == 0)
3145         {
3146           cmpltr = 0;
3147           nullify = 1;
3148         }
3149       else
3150         {
3151           cmpltr = -1;
3152         }
3153       **s = c;
3154     }
3155
3156   /* Reset pointers if this was really a ,n for a branch instruction.  */
3157   if (nullify)
3158     *s = save_s;
3159
3160   return cmpltr;
3161 }
3162
3163 /* Do the real work for assembling a single instruction.  Store results
3164    into the global "the_insn" variable.  */
3165
3166 static void
3167 pa_ip (char *str)
3168 {
3169   char *error_message = "";
3170   char *s, c, *argstart, *name, *save_s;
3171   const char *args;
3172   int match = FALSE;
3173   int comma = 0;
3174   int cmpltr, nullif, flag, cond, num;
3175   unsigned long opcode;
3176   struct pa_opcode *insn;
3177
3178 #ifdef OBJ_SOM
3179   /* We must have a valid space and subspace.  */
3180   pa_check_current_space_and_subspace ();
3181 #endif
3182
3183   /* Convert everything up to the first whitespace character into lower
3184      case.  */
3185   for (s = str; *s != ' ' && *s != '\t' && *s != '\n' && *s != '\0'; s++)
3186     *s = TOLOWER (*s);
3187
3188   /* Skip to something interesting.  */
3189   for (s = str;
3190        ISUPPER (*s) || ISLOWER (*s) || (*s >= '0' && *s <= '3');
3191        ++s)
3192     ;
3193
3194   switch (*s)
3195     {
3196
3197     case '\0':
3198       break;
3199
3200     case ',':
3201       comma = 1;
3202
3203       /*FALLTHROUGH */
3204
3205     case ' ':
3206       *s++ = '\0';
3207       break;
3208
3209     default:
3210       as_bad (_("Unknown opcode: `%s'"), str);
3211       return;
3212     }
3213
3214   /* Look up the opcode in the has table.  */
3215   if ((insn = (struct pa_opcode *) hash_find (op_hash, str)) == NULL)
3216     {
3217       as_bad ("Unknown opcode: `%s'", str);
3218       return;
3219     }
3220
3221   if (comma)
3222     *--s = ',';
3223
3224   /* Mark the location where arguments for the instruction start, then
3225      start processing them.  */
3226   argstart = s;
3227   for (;;)
3228     {
3229       /* Do some initialization.  */
3230       opcode = insn->match;
3231       strict = (insn->flags & FLAG_STRICT);
3232       memset (&the_insn, 0, sizeof (the_insn));
3233
3234       the_insn.reloc = R_HPPA_NONE;
3235
3236       if (insn->arch >= pa20
3237           && bfd_get_mach (stdoutput) < insn->arch)
3238         goto failed;
3239
3240       /* Build the opcode, checking as we go to make
3241          sure that the operands match.  */
3242       for (args = insn->args;; ++args)
3243         {
3244           /* Absorb white space in instruction.  */
3245           while (*s == ' ' || *s == '\t')
3246             s++;
3247
3248           switch (*args)
3249             {
3250             /* End of arguments.  */
3251             case '\0':
3252               if (*s == '\0')
3253                 match = TRUE;
3254               break;
3255
3256             case '+':
3257               if (*s == '+')
3258                 {
3259                   ++s;
3260                   continue;
3261                 }
3262               if (*s == '-')
3263                 continue;
3264               break;
3265
3266             /* These must match exactly.  */
3267             case '(':
3268             case ')':
3269             case ',':
3270             case ' ':
3271               if (*s++ == *args)
3272                 continue;
3273               break;
3274
3275             /* Handle a 5 bit register or control register field at 10.  */
3276             case 'b':
3277             case '^':
3278               if (!pa_parse_number (&s, 0))
3279                 break;
3280               num = pa_number;
3281               CHECK_FIELD (num, 31, 0, 0);
3282               INSERT_FIELD_AND_CONTINUE (opcode, num, 21);
3283
3284             /* Handle %sar or %cr11.  No bits get set, we just verify that it
3285                is there.  */
3286             case '!':
3287               /* Skip whitespace before register.  */
3288               while (*s == ' ' || *s == '\t')
3289                 s = s + 1;
3290
3291               if (!strncasecmp (s, "%sar", 4))
3292                 {
3293                   s += 4;
3294                   continue;
3295                 }
3296               else if (!strncasecmp (s, "%cr11", 5))
3297                 {
3298                   s += 5;
3299                   continue;
3300                 }
3301               break;
3302
3303             /* Handle a 5 bit register field at 15.  */
3304             case 'x':
3305               if (!pa_parse_number (&s, 0))
3306                 break;
3307               num = pa_number;
3308               CHECK_FIELD (num, 31, 0, 0);
3309               INSERT_FIELD_AND_CONTINUE (opcode, num, 16);
3310
3311             /* Handle a 5 bit register field at 31.  */
3312             case 't':
3313               if (!pa_parse_number (&s, 0))
3314                 break;
3315               num = pa_number;
3316               CHECK_FIELD (num, 31, 0, 0);
3317               INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
3318
3319             /* Handle a 5 bit register field at 10 and 15.  */
3320             case 'a':
3321               if (!pa_parse_number (&s, 0))
3322                 break;
3323               num = pa_number;
3324               CHECK_FIELD (num, 31, 0, 0);
3325               opcode |= num << 16;
3326               INSERT_FIELD_AND_CONTINUE (opcode, num, 21);
3327
3328             /* Handle a 5 bit field length at 31.  */
3329             case 'T':
3330               num = pa_get_absolute_expression (&the_insn, &s);
3331               if (strict && the_insn.exp.X_op != O_constant)
3332                 break;
3333               s = expr_end;
3334               CHECK_FIELD (num, 32, 1, 0);
3335               INSERT_FIELD_AND_CONTINUE (opcode, 32 - num, 0);
3336
3337             /* Handle a 5 bit immediate at 15.  */
3338             case '5':
3339               num = pa_get_absolute_expression (&the_insn, &s);
3340               if (strict && the_insn.exp.X_op != O_constant)
3341                 break;
3342               s = expr_end;
3343               /* When in strict mode, we want to just reject this
3344                  match instead of giving an out of range error.  */
3345               CHECK_FIELD (num, 15, -16, strict);
3346               num = low_sign_unext (num, 5);
3347               INSERT_FIELD_AND_CONTINUE (opcode, num, 16);
3348
3349             /* Handle a 5 bit immediate at 31.  */
3350             case 'V':
3351               num = pa_get_absolute_expression (&the_insn, &s);
3352               if (strict && the_insn.exp.X_op != O_constant)
3353                 break;
3354               s = expr_end;
3355               /* When in strict mode, we want to just reject this
3356                  match instead of giving an out of range error.  */
3357               CHECK_FIELD (num, 15, -16, strict);
3358               num = low_sign_unext (num, 5);
3359               INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
3360
3361             /* Handle an unsigned 5 bit immediate at 31.  */
3362             case 'r':
3363               num = pa_get_absolute_expression (&the_insn, &s);
3364               if (strict && the_insn.exp.X_op != O_constant)
3365                 break;
3366               s = expr_end;
3367               CHECK_FIELD (num, 31, 0, strict);
3368               INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
3369
3370             /* Handle an unsigned 5 bit immediate at 15.  */
3371             case 'R':
3372               num = pa_get_absolute_expression (&the_insn, &s);
3373               if (strict && the_insn.exp.X_op != O_constant)
3374                 break;
3375               s = expr_end;
3376               CHECK_FIELD (num, 31, 0, strict);
3377               INSERT_FIELD_AND_CONTINUE (opcode, num, 16);
3378
3379             /* Handle an unsigned 10 bit immediate at 15.  */
3380             case 'U':
3381               num = pa_get_absolute_expression (&the_insn, &s);
3382               if (strict && the_insn.exp.X_op != O_constant)
3383                 break;
3384               s = expr_end;
3385               CHECK_FIELD (num, 1023, 0, strict);
3386               INSERT_FIELD_AND_CONTINUE (opcode, num, 16);
3387
3388             /* Handle a 2 bit space identifier at 17.  */
3389             case 's':
3390               if (!pa_parse_number (&s, 0))
3391                 break;
3392               num = pa_number;
3393               CHECK_FIELD (num, 3, 0, 1);
3394               INSERT_FIELD_AND_CONTINUE (opcode, num, 14);
3395
3396             /* Handle a 3 bit space identifier at 18.  */
3397             case 'S':
3398               if (!pa_parse_number (&s, 0))
3399                 break;
3400               num = pa_number;
3401               CHECK_FIELD (num, 7, 0, 1);
3402               opcode |= re_assemble_3 (num);
3403               continue;
3404
3405             /* Handle all completers.  */
3406             case 'c':
3407               switch (*++args)
3408                 {
3409
3410                 /* Handle a completer for an indexing load or store.  */
3411                 case 'X':
3412                 case 'x':
3413                   {
3414                     int uu = 0;
3415                     int m = 0;
3416                     int i = 0;
3417                     while (*s == ',' && i < 2)
3418                       {
3419                         s++;
3420                         if (strncasecmp (s, "sm", 2) == 0)
3421                           {
3422                             uu = 1;
3423                             m = 1;
3424                             s++;
3425                             i++;
3426                           }
3427                         else if (strncasecmp (s, "m", 1) == 0)
3428                           m = 1;
3429                         else if ((strncasecmp (s, "s ", 2) == 0)
3430                                  || (strncasecmp (s, "s,", 2) == 0))
3431                           uu = 1;
3432                         else if (strict)
3433                           {
3434                             /* This is a match failure.  */
3435                             s--;
3436                             break;
3437                           }
3438                         else
3439                           as_bad (_("Invalid Indexed Load Completer."));
3440                         s++;
3441                         i++;
3442                       }
3443                     if (i > 2)
3444                       as_bad (_("Invalid Indexed Load Completer Syntax."));
3445                     opcode |= m << 5;
3446                     INSERT_FIELD_AND_CONTINUE (opcode, uu, 13);
3447                   }
3448
3449                 /* Handle a short load/store completer.  */
3450                 case 'M':
3451                 case 'm':
3452                 case 'q':
3453                 case 'J':
3454                 case 'e':
3455                   {
3456                     int a = 0;
3457                     int m = 0;
3458                     if (*s == ',')
3459                       {
3460                         s++;
3461                         if (strncasecmp (s, "ma", 2) == 0)
3462                           {
3463                             a = 0;
3464                             m = 1;
3465                             s += 2;
3466                           }
3467                         else if (strncasecmp (s, "mb", 2) == 0)
3468                           {
3469                             a = 1;
3470                             m = 1;
3471                             s += 2;
3472                           }
3473                         else if (strict)
3474                           /* This is a match failure.  */
3475                           s--;
3476                         else
3477                           {
3478                             as_bad (_("Invalid Short Load/Store Completer."));
3479                             s += 2;
3480                           }
3481                       }
3482                     /* If we did not get a ma/mb completer, then we do not
3483                        consider this a positive match for 'ce'.  */
3484                     else if (*args == 'e')
3485                       break;
3486
3487                    /* 'J', 'm', 'M' and 'q' are the same, except for where they
3488                        encode the before/after field.  */
3489                    if (*args == 'm' || *args == 'M')
3490                       {
3491                         opcode |= m << 5;
3492                         INSERT_FIELD_AND_CONTINUE (opcode, a, 13);
3493                       }
3494                     else if (*args == 'q')
3495                       {
3496                         opcode |= m << 3;
3497                         INSERT_FIELD_AND_CONTINUE (opcode, a, 2);
3498                       }
3499                     else if (*args == 'J')
3500                       {
3501                         /* M bit is explicit in the major opcode.  */
3502                         INSERT_FIELD_AND_CONTINUE (opcode, a, 2);
3503                       }
3504                     else if (*args == 'e')
3505                       {
3506                         /* Stash the ma/mb flag temporarily in the
3507                            instruction.  We will use (and remove it)
3508                            later when handling 'J', 'K', '<' & '>'.  */
3509                         opcode |= a;
3510                         continue;
3511                       }
3512                   }
3513
3514                 /* Handle a stbys completer.  */
3515                 case 'A':
3516                 case 's':
3517                   {
3518                     int a = 0;
3519                     int m = 0;
3520                     int i = 0;
3521                     while (*s == ',' && i < 2)
3522                       {
3523                         s++;
3524                         if (strncasecmp (s, "m", 1) == 0)
3525                           m = 1;
3526                         else if ((strncasecmp (s, "b ", 2) == 0)
3527                                  || (strncasecmp (s, "b,", 2) == 0))
3528                           a = 0;
3529                         else if (strncasecmp (s, "e", 1) == 0)
3530                           a = 1;
3531                         /* In strict mode, this is a match failure.  */
3532                         else if (strict)
3533                           {
3534                             s--;
3535                             break;
3536                           }
3537                         else
3538                           as_bad (_("Invalid Store Bytes Short Completer"));
3539                         s++;
3540                         i++;
3541                       }
3542                     if (i > 2)
3543                       as_bad (_("Invalid Store Bytes Short Completer"));
3544                     opcode |= m << 5;
3545                     INSERT_FIELD_AND_CONTINUE (opcode, a, 13);
3546                   }
3547
3548                 /* Handle load cache hint completer.  */
3549                 case 'c':
3550                   cmpltr = 0;
3551                   if (!strncmp (s, ",sl", 3))
3552                     {
3553                       s += 3;
3554                       cmpltr = 2;
3555                     }
3556                   INSERT_FIELD_AND_CONTINUE (opcode, cmpltr, 10);
3557
3558                 /* Handle store cache hint completer.  */
3559                 case 'C':
3560                   cmpltr = 0;
3561                   if (!strncmp (s, ",sl", 3))
3562                     {
3563                       s += 3;
3564                       cmpltr = 2;
3565                     }
3566                   else if (!strncmp (s, ",bc", 3))
3567                     {
3568                       s += 3;
3569                       cmpltr = 1;
3570                     }
3571                   INSERT_FIELD_AND_CONTINUE (opcode, cmpltr, 10);
3572
3573                 /* Handle load and clear cache hint completer.  */
3574                 case 'd':
3575                   cmpltr = 0;
3576                   if (!strncmp (s, ",co", 3))
3577                     {
3578                       s += 3;
3579                       cmpltr = 1;
3580                     }
3581                   INSERT_FIELD_AND_CONTINUE (opcode, cmpltr, 10);
3582
3583                 /* Handle load ordering completer.  */
3584                 case 'o':
3585                   if (strncmp (s, ",o", 2) != 0)
3586                     break;
3587                   s += 2;
3588                   continue;
3589
3590                 /* Handle a branch gate completer.  */
3591                 case 'g':
3592                   if (strncasecmp (s, ",gate", 5) != 0)
3593                     break;
3594                   s += 5;
3595                   continue;
3596
3597                 /* Handle a branch link and push completer.  */
3598                 case 'p':
3599                   if (strncasecmp (s, ",l,push", 7) != 0)
3600                     break;
3601                   s += 7;
3602                   continue;
3603
3604                 /* Handle a branch link completer.  */
3605                 case 'l':
3606                   if (strncasecmp (s, ",l", 2) != 0)
3607                     break;
3608                   s += 2;
3609                   continue;
3610
3611                 /* Handle a branch pop completer.  */
3612                 case 'P':
3613                   if (strncasecmp (s, ",pop", 4) != 0)
3614                     break;
3615                   s += 4;
3616                   continue;
3617
3618                 /* Handle a local processor completer.  */
3619                 case 'L':
3620                   if (strncasecmp (s, ",l", 2) != 0)
3621                     break;
3622                   s += 2;
3623                   continue;
3624
3625                 /* Handle a PROBE read/write completer.  */
3626                 case 'w':
3627                   flag = 0;
3628                   if (!strncasecmp (s, ",w", 2))
3629                     {
3630                       flag = 1;
3631                       s += 2;
3632                     }
3633                   else if (!strncasecmp (s, ",r", 2))
3634                     {
3635                       flag = 0;
3636                       s += 2;
3637                     }
3638
3639                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 6);
3640
3641                 /* Handle MFCTL wide completer.  */
3642                 case 'W':
3643                   if (strncasecmp (s, ",w", 2) != 0)
3644                     break;
3645                   s += 2;
3646                   continue;
3647
3648                 /* Handle an RFI restore completer.  */
3649                 case 'r':
3650                   flag = 0;
3651                   if (!strncasecmp (s, ",r", 2))
3652                     {
3653                       flag = 5;
3654                       s += 2;
3655                     }
3656
3657                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 5);
3658
3659                 /* Handle a system control completer.  */
3660                 case 'Z':
3661                   if (*s == ',' && (*(s + 1) == 'm' || *(s + 1) == 'M'))
3662                     {
3663                       flag = 1;
3664                       s += 2;
3665                     }
3666                   else
3667                     flag = 0;
3668
3669                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 5);
3670
3671                 /* Handle intermediate/final completer for DCOR.  */
3672                 case 'i':
3673                   flag = 0;
3674                   if (!strncasecmp (s, ",i", 2))
3675                     {
3676                       flag = 1;
3677                       s += 2;
3678                     }
3679
3680                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 6);
3681
3682                 /* Handle zero/sign extension completer.  */
3683                 case 'z':
3684                   flag = 1;
3685                   if (!strncasecmp (s, ",z", 2))
3686                     {
3687                       flag = 0;
3688                       s += 2;
3689                     }
3690
3691                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 10);
3692
3693                 /* Handle add completer.  */
3694                 case 'a':
3695                   flag = 1;
3696                   if (!strncasecmp (s, ",l", 2))
3697                     {
3698                       flag = 2;
3699                       s += 2;
3700                     }
3701                   else if (!strncasecmp (s, ",tsv", 4))
3702                     {
3703                       flag = 3;
3704                       s += 4;
3705                     }
3706
3707                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 10);
3708
3709                 /* Handle 64 bit carry for ADD.  */
3710                 case 'Y':
3711                   flag = 0;
3712                   if (!strncasecmp (s, ",dc,tsv", 7) ||
3713                       !strncasecmp (s, ",tsv,dc", 7))
3714                     {
3715                       flag = 1;
3716                       s += 7;
3717                     }
3718                   else if (!strncasecmp (s, ",dc", 3))
3719                     {
3720                       flag = 0;
3721                       s += 3;
3722                     }
3723                   else
3724                     break;
3725
3726                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 11);
3727
3728                 /* Handle 32 bit carry for ADD.  */
3729                 case 'y':
3730                   flag = 0;
3731                   if (!strncasecmp (s, ",c,tsv", 6) ||
3732                       !strncasecmp (s, ",tsv,c", 6))
3733                     {
3734                       flag = 1;
3735                       s += 6;
3736                     }
3737                   else if (!strncasecmp (s, ",c", 2))
3738                     {
3739                       flag = 0;
3740                       s += 2;
3741                     }
3742                   else
3743                     break;
3744
3745                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 11);
3746
3747                 /* Handle trap on signed overflow.  */
3748                 case 'v':
3749                   flag = 0;
3750                   if (!strncasecmp (s, ",tsv", 4))
3751                     {
3752                       flag = 1;
3753                       s += 4;
3754                     }
3755
3756                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 11);
3757
3758                 /* Handle trap on condition and overflow.  */
3759                 case 't':
3760                   flag = 0;
3761                   if (!strncasecmp (s, ",tc,tsv", 7) ||
3762                       !strncasecmp (s, ",tsv,tc", 7))
3763                     {
3764                       flag = 1;
3765                       s += 7;
3766                     }
3767                   else if (!strncasecmp (s, ",tc", 3))
3768                     {
3769                       flag = 0;
3770                       s += 3;
3771                     }
3772                   else
3773                     break;
3774
3775                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 11);
3776
3777                 /* Handle 64 bit borrow for SUB.  */
3778                 case 'B':
3779                   flag = 0;
3780                   if (!strncasecmp (s, ",db,tsv", 7) ||
3781                       !strncasecmp (s, ",tsv,db", 7))
3782                     {
3783                       flag = 1;
3784                       s += 7;
3785                     }
3786                   else if (!strncasecmp (s, ",db", 3))
3787                     {
3788                       flag = 0;
3789                       s += 3;
3790                     }
3791                   else
3792                     break;
3793
3794                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 11);
3795
3796                 /* Handle 32 bit borrow for SUB.  */
3797                 case 'b':
3798                   flag = 0;
3799                   if (!strncasecmp (s, ",b,tsv", 6) ||
3800                       !strncasecmp (s, ",tsv,b", 6))
3801                     {
3802                       flag = 1;
3803                       s += 6;
3804                     }
3805                   else if (!strncasecmp (s, ",b", 2))
3806                     {
3807                       flag = 0;
3808                       s += 2;
3809                     }
3810                   else
3811                     break;
3812
3813                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 11);
3814
3815                 /* Handle trap condition completer for UADDCM.  */
3816                 case 'T':
3817                   flag = 0;
3818                   if (!strncasecmp (s, ",tc", 3))
3819                     {
3820                       flag = 1;
3821                       s += 3;
3822                     }
3823
3824                   INSERT_FIELD_AND_CONTINUE (opcode, flag, 6);
3825
3826                 /* Handle signed/unsigned at 21.  */
3827                 case 'S':
3828                   {
3829                     int sign = 1;
3830                     if (strncasecmp (s, ",s", 2) == 0)
3831                       {
3832                         sign = 1;
3833                         s += 2;
3834                       }
3835                     else if (strncasecmp (s, ",u", 2) == 0)
3836                       {
3837                         sign = 0;
3838                         s += 2;
3839                       }
3840
3841                     INSERT_FIELD_AND_CONTINUE (opcode, sign, 10);
3842                   }
3843
3844                 /* Handle left/right combination at 17:18.  */
3845                 case 'h':
3846                   if (*s++ == ',')
3847                     {
3848                       int lr = 0;
3849                       if (*s == 'r')
3850                         lr = 2;
3851                       else if (*s == 'l')
3852                         lr = 0;
3853                       else
3854                         as_bad (_("Invalid left/right combination completer"));
3855
3856                       s++;
3857                       INSERT_FIELD_AND_CONTINUE (opcode, lr, 13);
3858                     }
3859                   else
3860                     as_bad (_("Invalid left/right combination completer"));
3861                   break;
3862
3863                 /* Handle saturation at 24:25.  */
3864                 case 'H':
3865                   {
3866                     int sat = 3;
3867                     if (strncasecmp (s, ",ss", 3) == 0)
3868                       {
3869                         sat = 1;
3870                         s += 3;
3871                       }
3872                     else if (strncasecmp (s, ",us", 3) == 0)
3873                       {
3874                         sat = 0;
3875                         s += 3;
3876                       }
3877
3878                     INSERT_FIELD_AND_CONTINUE (opcode, sat, 6);
3879                   }
3880
3881                 /* Handle permutation completer.  */
3882                 case '*':
3883                   if (*s++ == ',')
3884                     {
3885                       int permloc[4];
3886                       int perm = 0;
3887                       int i = 0;
3888                       permloc[0] = 13;
3889                       permloc[1] = 10;
3890                       permloc[2] = 8;
3891                       permloc[3] = 6;
3892                       for (; i < 4; i++)
3893                         {
3894                           switch (*s++)
3895                             {
3896                             case '0':
3897                               perm = 0;
3898                               break;
3899                             case '1':
3900                               perm = 1;
3901                               break;
3902                             case '2':
3903                               perm = 2;
3904                               break;
3905                             case '3':
3906                               perm = 3;
3907                               break;
3908                             default:
3909                               as_bad (_("Invalid permutation completer"));
3910                             }
3911                           opcode |= perm << permloc[i];
3912                         }
3913                       continue;
3914                     }
3915                   else
3916                     as_bad (_("Invalid permutation completer"));
3917                   break;
3918
3919                 default:
3920                   abort ();
3921                 }
3922               break;
3923
3924             /* Handle all conditions.  */
3925             case '?':
3926               {
3927                 args++;
3928                 switch (*args)
3929                   {
3930                   /* Handle FP compare conditions.  */
3931                   case 'f':
3932                     cond = pa_parse_fp_cmp_cond (&s);
3933                     INSERT_FIELD_AND_CONTINUE (opcode, cond, 0);
3934
3935                   /* Handle an add condition.  */
3936                   case 'A':
3937                   case 'a':
3938                     cmpltr = 0;
3939                     flag = 0;
3940                     if (*s == ',')
3941                       {
3942                         s++;
3943
3944                         /* 64 bit conditions.  */
3945                         if (*args == 'A')
3946                           {
3947                             if (*s == '*')
3948                               s++;
3949                             else
3950                               break;
3951                           }
3952                         else if (*s == '*')
3953                           break;
3954
3955                         name = s;
3956                         while (*s != ',' && *s != ' ' && *s != '\t')
3957                           s += 1;
3958                         c = *s;
3959                         *s = 0x00;
3960                         if (strcmp (name, "=") == 0)
3961                           cmpltr = 1;
3962                         else if (strcmp (name, "<") == 0)
3963                           cmpltr = 2;
3964                         else if (strcmp (name, "<=") == 0)
3965                           cmpltr = 3;
3966                         else if (strcasecmp (name, "nuv") == 0)
3967                           cmpltr = 4;
3968                         else if (strcasecmp (name, "znv") == 0)
3969                           cmpltr = 5;
3970                         else if (strcasecmp (name, "sv") == 0)
3971                           cmpltr = 6;
3972                         else if (strcasecmp (name, "od") == 0)
3973                           cmpltr = 7;
3974                         else if (strcasecmp (name, "tr") == 0)
3975                           {
3976                             cmpltr = 0;
3977                             flag = 1;
3978                           }
3979                         else if (strcmp (name, "<>") == 0)
3980                           {
3981                             cmpltr = 1;
3982                             flag = 1;
3983                           }
3984                         else if (strcmp (name, ">=") == 0)
3985                           {
3986                             cmpltr = 2;
3987                             flag = 1;
3988                           }
3989                         else if (strcmp (name, ">") == 0)
3990                           {
3991                             cmpltr = 3;
3992                             flag = 1;
3993                           }
3994                         else if (strcasecmp (name, "uv") == 0)
3995                           {
3996                             cmpltr = 4;
3997                             flag = 1;
3998                           }
3999                         else if (strcasecmp (name, "vnz") == 0)
4000                           {
4001                             cmpltr = 5;
4002                             flag = 1;
4003                           }
4004                         else if (strcasecmp (name, "nsv") == 0)
4005                           {
4006                             cmpltr = 6;
4007                             flag = 1;
4008                           }
4009                         else if (strcasecmp (name, "ev") == 0)
4010                           {
4011                             cmpltr = 7;
4012                             flag = 1;
4013                           }
4014                         /* ",*" is a valid condition.  */
4015                         else if (*args == 'a' || *name)
4016                           as_bad (_("Invalid Add Condition: %s"), name);
4017                         *s = c;
4018                       }
4019                     opcode |= cmpltr << 13;
4020                     INSERT_FIELD_AND_CONTINUE (opcode, flag, 12);
4021
4022                   /* Handle non-negated add and branch condition.  */
4023                   case 'd':
4024                     cmpltr = pa_parse_nonneg_add_cmpltr (&s);
4025                     if (cmpltr < 0)
4026                       {
4027                         as_bad (_("Invalid Add and Branch Condition"));
4028                         cmpltr = 0;
4029                       }
4030                     INSERT_FIELD_AND_CONTINUE (opcode, cmpltr, 13);
4031
4032                   /* Handle 64 bit wide-mode add and branch condition.  */
4033                   case 'W':
4034                     cmpltr = pa_parse_addb_64_cmpltr (&s);
4035                     if (cmpltr < 0)
4036                       {
4037                         as_bad (_("Invalid Add and Branch Condition"));
4038                         cmpltr = 0;
4039                       }
4040                     else
4041                       {
4042                         /* Negated condition requires an opcode change.  */
4043                         opcode |= (cmpltr & 8) << 24;
4044                       }
4045                     INSERT_FIELD_AND_CONTINUE (opcode, cmpltr & 7, 13);
4046
4047                   /* Handle a negated or non-negated add and branch
4048                      condition.  */
4049                   case '@':
4050                     save_s = s;
4051                     cmpltr = pa_parse_nonneg_add_cmpltr (&s);
4052                     if (cmpltr < 0)
4053                       {
4054                         s = save_s;
4055                         cmpltr = pa_parse_neg_add_cmpltr (&s);
4056                         if (cmpltr < 0)
4057                           {
4058                             as_bad (_("Invalid Compare/Subtract Condition"));
4059                             cmpltr = 0;
4060                           }
4061                         else
4062                           {
4063                             /* Negated condition requires an opcode change.  */
4064                             opcode |= 1 << 27;
4065                           }
4066                       }
4067                     INSERT_FIELD_AND_CONTINUE (opcode, cmpltr, 13);
4068
4069                   /* Handle branch on bit conditions.  */
4070                   case 'B':
4071                   case 'b':
4072                     cmpltr = 0;
4073                     if (*s == ',')
4074                       {
4075                         s++;
4076
4077                         if (*args == 'B')
4078                           {
4079                             if (*s == '*')
4080                               s++;
4081                             else
4082                               break;
4083                           }
4084                         else if (*s == '*')
4085                           break;
4086
4087                         if (strncmp (s, "<", 1) == 0)
4088                           {
4089                             cmpltr = 0;
4090                             s++;
4091                           }
4092                         else if (strncmp (s, ">=", 2) == 0)
4093                           {
4094                             cmpltr = 1;
4095                             s += 2;
4096                           }
4097                         else
4098                           as_bad (_("Invalid Bit Branch Condition: %c"), *s);
4099                       }
4100                     INSERT_FIELD_AND_CONTINUE (opcode, cmpltr, 15);
4101
4102                   /* Handle a compare/subtract condition.  */
4103                   case 'S':
4104                   case 's':
4105                     cmpltr = 0;
4106                     flag = 0;
4107                     if (*s == ',')
4108                       {
4109                         s++;
4110
4111                         /* 64 bit conditions.  */
4112                         if (*args == 'S')
4113                           {
4114                             if (*s == '*')
4115                               s++;
4116                             else
4117                               break;
4118                           }
4119                         else if (*s == '*')
4120                           break;
4121
4122                         name = s;
4123                         while (*s != ',' && *s != ' ' && *s != '\t')
4124                           s += 1;
4125                         c = *s;
4126                         *s = 0x00;
4127                         if (strcmp (name, "=") == 0)
4128                           cmpltr = 1;
4129                         else if (strcmp (name, "<") == 0)
4130                           cmpltr = 2;
4131                         else if (strcmp (name, "<=") == 0)
4132                           cmpltr = 3;
4133                         else if (strcasecmp (name, "<<") == 0)
4134                           cmpltr = 4;
4135                         else if (strcasecmp (name, "<<=") == 0)
4136                           cmpltr = 5;
4137                         else if (strcasecmp (name, "sv") == 0)
4138                           cmpltr = 6;
4139                         else if (strcasecmp (name, "od") == 0)
4140                           cmpltr = 7;
4141                         else if (strcasecmp (name, "tr") == 0)
4142                           {
4143                             cmpltr = 0;
4144                             flag = 1;
4145                           }
4146                         else if (strcmp (name, "<>") == 0)
4147                           {
4148                             cmpltr = 1;
4149                             flag = 1;
4150                           }
4151                         else if (strcmp (name, ">=") == 0)
4152                           {
4153                             cmpltr = 2;
4154                             flag = 1;
4155                           }
4156                         else if (strcmp (name, ">") == 0)
4157                           {
4158                             cmpltr = 3;
4159                             flag = 1;
4160                           }
4161                         else if (strcasecmp (name, ">>=") == 0)
4162                           {
4163                             cmpltr = 4;
4164                             flag = 1;
4165                           }
4166                         else if (strcasecmp (name, ">>") == 0)
4167                           {
4168                             cmpltr = 5;
4169                             flag = 1;
4170                           }
4171                         else if (strcasecmp (name, "nsv") == 0)
4172                           {
4173                             cmpltr = 6;
4174                             flag = 1;
4175                           }
4176                         else if (strcasecmp (name, "ev") == 0)
4177                           {
4178                             cmpltr = 7;
4179                             flag = 1;
4180                           }
4181                         /* ",*" is a valid condition.  */
4182                         else if (*args != 'S' || *name)
4183                           as_bad (_("Invalid Compare/Subtract Condition: %s"),
4184                                   name);
4185                         *s = c;
4186                       }
4187                     opcode |= cmpltr << 13;
4188                     INSERT_FIELD_AND_CONTINUE (opcode, flag, 12);
4189
4190                   /* Handle a non-negated compare condition.  */
4191                   case 't':
4192                     cmpltr = pa_parse_nonneg_cmpsub_cmpltr (&s);
4193                     if (cmpltr < 0)
4194                       {
4195                         as_bad (_("Invalid Compare/Subtract Condition"));
4196                         cmpltr = 0;
4197                       }
4198                     INSERT_FIELD_AND_CONTINUE (opcode, cmpltr, 13);
4199
4200                   /* Handle a 32 bit compare and branch condition.  */
4201                   case 'n':
4202                     save_s = s;
4203                     cmpltr = pa_parse_nonneg_cmpsub_cmpltr (&s);
4204                     if (cmpltr < 0)
4205                       {
4206                         s = save_s;
4207                         cmpltr = pa_parse_neg_cmpsub_cmpltr (&s);
4208                         if (cmpltr < 0)
4209                           {
4210                             as_bad (_("Invalid Compare and Branch Condition"));
4211                             cmpltr = 0;
4212                           }
4213                         else
4214                           {
4215                             /* Negated condition requires an opcode change.  */
4216                             opcode |= 1 << 27;
4217                           }
4218                       }
4219
4220                     INSERT_FIELD_AND_CONTINUE (opcode, cmpltr, 13);
4221
4222                   /* Handle a 64 bit compare and branch condition.  */
4223                   case 'N':
4224                     cmpltr = pa_parse_cmpb_64_cmpltr (&s);
4225                     if (cmpltr >= 0)
4226                       {
4227                         /* Negated condition requires an opcode change.  */
4228                         opcode |= (cmpltr & 8) << 26;
4229                       }
4230                     else
4231                       /* Not a 64 bit cond.  Give 32 bit a chance.  */
4232                       break;
4233
4234                     INSERT_FIELD_AND_CONTINUE (opcode, cmpltr & 7, 13);
4235
4236                   /* Handle a 64 bit cmpib condition.  */
4237                   case 'Q':
4238                     cmpltr = pa_parse_cmpib_64_cmpltr (&s);
4239                     if (cmpltr < 0)
4240                       /* Not a 64 bit cond.  Give 32 bit a chance.  */
4241                       break;
4242
4243                     INSERT_FIELD_AND_CONTINUE (opcode, cmpltr, 13);
4244
4245                     /* Handle a logical instruction condition.  */
4246                   case 'L':
4247                   case 'l':
4248                     cmpltr = 0;
4249                     flag = 0;
4250                     if (*s == ',')
4251                       {
4252                         s++;
4253
4254                         /* 64 bit conditions.  */
4255                         if (*args == 'L')
4256                           {
4257                             if (*s == '*')
4258                               s++;
4259                             else
4260                               break;
4261                           }
4262                         else if (*s == '*')
4263                           break;
4264
4265                         name = s;
4266                         while (*s != ',' && *s != ' ' && *s != '\t')
4267                           s += 1;
4268                         c = *s;
4269                         *s = 0x00;
4270
4271                         if (strcmp (name, "=") == 0)
4272                           cmpltr = 1;
4273                         else if (strcmp (name, "<") == 0)
4274                           cmpltr = 2;
4275                         else if (strcmp (name, "<=") == 0)
4276                           cmpltr = 3;
4277                         else if (strcasecmp (name, "od") == 0)
4278                           cmpltr = 7;
4279                         else if (strcasecmp (name, "tr") == 0)
4280                           {
4281                             cmpltr = 0;
4282                             flag = 1;
4283                           }
4284                         else if (strcmp (name, "<>") == 0)
4285                           {
4286                             cmpltr = 1;
4287                             flag = 1;
4288                           }
4289                         else if (strcmp (name, ">=") == 0)
4290                           {
4291                             cmpltr = 2;
4292                             flag = 1;
4293                           }
4294                         else if (strcmp (name, ">") == 0)
4295                           {
4296                             cmpltr = 3;
4297                             flag = 1;
4298                           }
4299                         else if (strcasecmp (name, "ev") == 0)
4300                           {
4301                             cmpltr = 7;
4302                             flag = 1;
4303                           }
4304                         /* ",*" is a valid condition.  */
4305                         else if (*args != 'L' || *name)
4306                           as_bad (_("Invalid Logical Instruction Condition."));
4307                         *s = c;
4308                       }
4309                     opcode |= cmpltr << 13;
4310                     INSERT_FIELD_AND_CONTINUE (opcode, flag, 12);
4311
4312                   /* Handle a shift/extract/deposit condition.  */
4313                   case 'X':
4314                   case 'x':
4315                   case 'y':
4316                     cmpltr = 0;
4317                     if (*s == ',')
4318                       {
4319                         save_s = s++;
4320
4321                         /* 64 bit conditions.  */
4322                         if (*args == 'X')
4323                           {
4324                             if (*s == '*')
4325                               s++;
4326                             else
4327                               break;
4328                           }
4329                         else if (*s == '*')
4330                           break;
4331
4332                         name = s;
4333                         while (*s != ',' && *s != ' ' && *s != '\t')
4334                           s += 1;
4335                         c = *s;
4336                         *s = 0x00;
4337                         if (strcmp (name, "=") == 0)
4338                           cmpltr = 1;
4339                         else if (strcmp (name, "<") == 0)
4340                           cmpltr = 2;
4341                         else if (strcasecmp (name, "od") == 0)
4342                           cmpltr = 3;
4343                         else if (strcasecmp (name, "tr") == 0)
4344                           cmpltr = 4;
4345                         else if (strcmp (name, "<>") == 0)
4346                           cmpltr = 5;
4347                         else if (strcmp (name, ">=") == 0)
4348                           cmpltr = 6;
4349                         else if (strcasecmp (name, "ev") == 0)
4350                           cmpltr = 7;
4351                         /* Handle movb,n.  Put things back the way they were.
4352                            This includes moving s back to where it started.  */
4353                         else if (strcasecmp (name, "n") == 0 && *args == 'y')
4354                           {
4355                             *s = c;
4356                             s = save_s;
4357                             continue;
4358                           }
4359                         /* ",*" is a valid condition.  */
4360                         else if (*args != 'X' || *name)
4361                           as_bad (_("Invalid Shift/Extract/Deposit Condition."));
4362                         *s = c;
4363                       }
4364                     INSERT_FIELD_AND_CONTINUE (opcode, cmpltr, 13);
4365
4366                   /* Handle a unit instruction condition.  */
4367                   case 'U':
4368                   case 'u':
4369                     cmpltr = 0;
4370                     flag = 0;
4371                     if (*s == ',')
4372                       {
4373                         s++;
4374
4375                         /* 64 bit conditions.  */
4376                         if (*args == 'U')
4377                           {
4378                             if (*s == '*')
4379                               s++;
4380                             else
4381                               break;
4382                           }
4383                         else if (*s == '*')
4384                           break;
4385
4386                         if (strncasecmp (s, "sbz", 3) == 0)
4387                           {
4388                             cmpltr = 2;
4389                             s += 3;
4390                           }
4391                         else if (strncasecmp (s, "shz", 3) == 0)
4392                           {
4393                             cmpltr = 3;
4394                             s += 3;
4395                           }
4396                         else if (strncasecmp (s, "sdc", 3) == 0)
4397                           {
4398                             cmpltr = 4;
4399                             s += 3;
4400                           }
4401                         else if (strncasecmp (s, "sbc", 3) == 0)
4402                           {
4403                             cmpltr = 6;
4404                             s += 3;
4405                           }
4406                         else if (strncasecmp (s, "shc", 3) == 0)
4407                           {
4408                             cmpltr = 7;
4409                             s += 3;
4410                           }
4411                         else if (strncasecmp (s, "tr", 2) == 0)
4412                           {
4413                             cmpltr = 0;
4414                             flag = 1;
4415                             s += 2;
4416                           }
4417                         else if (strncasecmp (s, "nbz", 3) == 0)
4418                           {
4419                             cmpltr = 2;
4420                             flag = 1;
4421                             s += 3;
4422                           }
4423                         else if (strncasecmp (s, "nhz", 3) == 0)
4424                           {
4425                             cmpltr = 3;
4426                             flag = 1;
4427                             s += 3;
4428                           }
4429                         else if (strncasecmp (s, "ndc", 3) == 0)
4430                           {
4431                             cmpltr = 4;
4432                             flag = 1;
4433                             s += 3;
4434                           }
4435                         else if (strncasecmp (s, "nbc", 3) == 0)
4436                           {
4437                             cmpltr = 6;
4438                             flag = 1;
4439                             s += 3;
4440                           }
4441                         else if (strncasecmp (s, "nhc", 3) == 0)
4442                           {
4443                             cmpltr = 7;
4444                             flag = 1;
4445                             s += 3;
4446                           }
4447                         else if (strncasecmp (s, "swz", 3) == 0)
4448                           {
4449                             cmpltr = 1;
4450                             flag = 0;
4451                             s += 3;
4452                           }
4453                         else if (strncasecmp (s, "swc", 3) == 0)
4454                           {
4455                             cmpltr = 5;
4456                             flag = 0;
4457                             s += 3;
4458                           }
4459                         else if (strncasecmp (s, "nwz", 3) == 0)
4460                           {
4461                             cmpltr = 1;
4462                             flag = 1;
4463                             s += 3;
4464                           }
4465                         else if (strncasecmp (s, "nwc", 3) == 0)
4466                           {
4467                             cmpltr = 5;
4468                             flag = 1;
4469                             s += 3;
4470                           }
4471                         /* ",*" is a valid condition.  */
4472                         else if (*args != 'U' || (*s != ' ' && *s != '\t'))
4473                           as_bad (_("Invalid Unit Instruction Condition."));
4474                       }
4475                     opcode |= cmpltr << 13;
4476                     INSERT_FIELD_AND_CONTINUE (opcode, flag, 12);
4477
4478                   default:
4479                     abort ();
4480                   }
4481                 break;
4482               }
4483
4484             /* Handle a nullification completer for branch instructions.  */
4485             case 'n':
4486               nullif = pa_parse_nullif (&s);
4487               INSERT_FIELD_AND_CONTINUE (opcode, nullif, 1);
4488
4489             /* Handle a nullification completer for copr and spop insns.  */
4490             case 'N':
4491               nullif = pa_parse_nullif (&s);
4492               INSERT_FIELD_AND_CONTINUE (opcode, nullif, 5);
4493
4494             /* Handle ,%r2 completer for new syntax branches.  */
4495             case 'L':
4496               if (*s == ',' && strncasecmp (s + 1, "%r2", 3) == 0)
4497                 s += 4;
4498               else if (*s == ',' && strncasecmp (s + 1, "%rp", 3) == 0)
4499                 s += 4;
4500               else
4501                 break;
4502               continue;
4503
4504             /* Handle 3 bit entry into the fp compare array.   Valid values
4505                are 0..6 inclusive.  */
4506             case 'h':
4507               get_expression (s);
4508               s = expr_end;
4509               if (the_insn.exp.X_op == O_constant)
4510                 {
4511                   num = evaluate_absolute (&the_insn);
4512                   CHECK_FIELD (num, 6, 0, 0);
4513                   num++;
4514                   INSERT_FIELD_AND_CONTINUE (opcode, num, 13);
4515                 }
4516               else
4517                 break;
4518
4519             /* Handle 3 bit entry into the fp compare array.   Valid values
4520                are 0..6 inclusive.  */
4521             case 'm':
4522               get_expression (s);
4523               if (the_insn.exp.X_op == O_constant)
4524                 {
4525                   s = expr_end;
4526                   num = evaluate_absolute (&the_insn);
4527                   CHECK_FIELD (num, 6, 0, 0);
4528                   num = (num + 1) ^ 1;
4529                   INSERT_FIELD_AND_CONTINUE (opcode, num, 13);
4530                 }
4531               else
4532                 break;
4533
4534             /* Handle graphics test completers for ftest */
4535             case '=':
4536               {
4537                 num = pa_parse_ftest_gfx_completer (&s);
4538                 INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
4539               }
4540
4541             /* Handle a 11 bit immediate at 31.  */
4542             case 'i':
4543               the_insn.field_selector = pa_chk_field_selector (&s);
4544               get_expression (s);
4545               s = expr_end;
4546               if (the_insn.exp.X_op == O_constant)
4547                 {
4548                   num = evaluate_absolute (&the_insn);
4549                   CHECK_FIELD (num, 1023, -1024, 0);
4550                   num = low_sign_unext (num, 11);
4551                   INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
4552                 }
4553               else
4554                 {
4555                   if (is_DP_relative (the_insn.exp))
4556                     the_insn.reloc = R_HPPA_GOTOFF;
4557                   else if (is_PC_relative (the_insn.exp))
4558                     the_insn.reloc = R_HPPA_PCREL_CALL;
4559 #ifdef OBJ_ELF
4560                   else if (is_tls_gdidx (the_insn.exp))
4561                     the_insn.reloc = R_PARISC_TLS_GD21L;
4562                   else if (is_tls_ldidx (the_insn.exp))
4563                     the_insn.reloc = R_PARISC_TLS_LDM21L;
4564                   else if (is_tls_dtpoff (the_insn.exp))
4565                     the_insn.reloc = R_PARISC_TLS_LDO21L;
4566                   else if (is_tls_ieoff (the_insn.exp))
4567                     the_insn.reloc = R_PARISC_TLS_IE21L;
4568                   else if (is_tls_leoff (the_insn.exp))
4569                     the_insn.reloc = R_PARISC_TLS_LE21L;
4570 #endif
4571                   else
4572                     the_insn.reloc = R_HPPA;
4573                   the_insn.format = 11;
4574                   continue;
4575                 }
4576
4577             /* Handle a 14 bit immediate at 31.  */
4578             case 'J':
4579               the_insn.field_selector = pa_chk_field_selector (&s);
4580               get_expression (s);
4581               s = expr_end;
4582               if (the_insn.exp.X_op == O_constant)
4583                 {
4584                   int mb;
4585
4586                   /* XXX the completer stored away tidbits of information
4587                      for us to extract.  We need a cleaner way to do this.
4588                      Now that we have lots of letters again, it would be
4589                      good to rethink this.  */
4590                   mb = opcode & 1;
4591                   opcode -= mb;
4592                   num = evaluate_absolute (&the_insn);
4593                   if (mb != (num < 0))
4594                     break;
4595                   CHECK_FIELD (num, 8191, -8192, 0);
4596                   num = low_sign_unext (num, 14);
4597                   INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
4598                 }
4599               break;
4600
4601             /* Handle a 14 bit immediate at 31.  */
4602             case 'K':
4603               the_insn.field_selector = pa_chk_field_selector (&s);
4604               get_expression (s);
4605               s = expr_end;
4606               if (the_insn.exp.X_op == O_constant)
4607                 {
4608                   int mb;
4609
4610                   mb = opcode & 1;
4611                   opcode -= mb;
4612                   num = evaluate_absolute (&the_insn);
4613                   if (mb == (num < 0))
4614                     break;
4615                   if (num % 4)
4616                     break;
4617                   CHECK_FIELD (num, 8191, -8192, 0);
4618                   num = low_sign_unext (num, 14);
4619                   INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
4620                 }
4621               break;
4622
4623             /* Handle a 16 bit immediate at 31.  */
4624             case '<':
4625               the_insn.field_selector = pa_chk_field_selector (&s);
4626               get_expression (s);
4627               s = expr_end;
4628               if (the_insn.exp.X_op == O_constant)
4629                 {
4630                   int mb;
4631
4632                   mb = opcode & 1;
4633                   opcode -= mb;
4634                   num = evaluate_absolute (&the_insn);
4635                   if (mb != (num < 0))
4636                     break;
4637                   CHECK_FIELD (num, 32767, -32768, 0);
4638                   num = re_assemble_16 (num);
4639                   INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
4640                 }
4641               break;
4642
4643             /* Handle a 16 bit immediate at 31.  */
4644             case '>':
4645               the_insn.field_selector = pa_chk_field_selector (&s);
4646               get_expression (s);
4647               s = expr_end;
4648               if (the_insn.exp.X_op == O_constant)
4649                 {
4650                   int mb;
4651
4652                   mb = opcode & 1;
4653                   opcode -= mb;
4654                   num = evaluate_absolute (&the_insn);
4655                   if (mb == (num < 0))
4656                     break;
4657                   if (num % 4)
4658                     break;
4659                   CHECK_FIELD (num, 32767, -32768, 0);
4660                   num = re_assemble_16 (num);
4661                   INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
4662                 }
4663               break;
4664
4665             /* Handle 14 bit immediate, shifted left three times.  */
4666             case '#':
4667               if (bfd_get_mach (stdoutput) != pa20)
4668                 break;
4669               the_insn.field_selector = pa_chk_field_selector (&s);
4670               get_expression (s);
4671               s = expr_end;
4672               if (the_insn.exp.X_op == O_constant)
4673                 {
4674                   num = evaluate_absolute (&the_insn);
4675                   if (num & 0x7)
4676                     break;
4677                   CHECK_FIELD (num, 8191, -8192, 0);
4678                   if (num < 0)
4679                     opcode |= 1;
4680                   num &= 0x1fff;
4681                   num >>= 3;
4682                   INSERT_FIELD_AND_CONTINUE (opcode, num, 4);
4683                 }
4684               else
4685                 {
4686                   if (is_DP_relative (the_insn.exp))
4687                     the_insn.reloc = R_HPPA_GOTOFF;
4688                   else if (is_PC_relative (the_insn.exp))
4689                     the_insn.reloc = R_HPPA_PCREL_CALL;
4690 #ifdef OBJ_ELF
4691                   else if (is_tls_gdidx (the_insn.exp))
4692                     the_insn.reloc = R_PARISC_TLS_GD21L;
4693                   else if (is_tls_ldidx (the_insn.exp))
4694                     the_insn.reloc = R_PARISC_TLS_LDM21L;
4695                   else if (is_tls_dtpoff (the_insn.exp))
4696                     the_insn.reloc = R_PARISC_TLS_LDO21L;
4697                   else if (is_tls_ieoff (the_insn.exp))
4698                     the_insn.reloc = R_PARISC_TLS_IE21L;
4699                   else if (is_tls_leoff (the_insn.exp))
4700                     the_insn.reloc = R_PARISC_TLS_LE21L;
4701 #endif
4702                   else
4703                     the_insn.reloc = R_HPPA;
4704                   the_insn.format = 14;
4705                   continue;
4706                 }
4707               break;
4708
4709             /* Handle 14 bit immediate, shifted left twice.  */
4710             case 'd':
4711               the_insn.field_selector = pa_chk_field_selector (&s);
4712               get_expression (s);
4713               s = expr_end;
4714               if (the_insn.exp.X_op == O_constant)
4715                 {
4716                   num = evaluate_absolute (&the_insn);
4717                   if (num & 0x3)
4718                     break;
4719                   CHECK_FIELD (num, 8191, -8192, 0);
4720                   if (num < 0)
4721                     opcode |= 1;
4722                   num &= 0x1fff;
4723                   num >>= 2;
4724                   INSERT_FIELD_AND_CONTINUE (opcode, num, 3);
4725                 }
4726               else
4727                 {
4728                   if (is_DP_relative (the_insn.exp))
4729                     the_insn.reloc = R_HPPA_GOTOFF;
4730                   else if (is_PC_relative (the_insn.exp))
4731                     the_insn.reloc = R_HPPA_PCREL_CALL;
4732 #ifdef OBJ_ELF
4733                   else if (is_tls_gdidx (the_insn.exp))
4734                     the_insn.reloc = R_PARISC_TLS_GD21L;
4735                   else if (is_tls_ldidx (the_insn.exp))
4736                     the_insn.reloc = R_PARISC_TLS_LDM21L;
4737                   else if (is_tls_dtpoff (the_insn.exp))
4738                     the_insn.reloc = R_PARISC_TLS_LDO21L;
4739                   else if (is_tls_ieoff (the_insn.exp))
4740                     the_insn.reloc = R_PARISC_TLS_IE21L;
4741                   else if (is_tls_leoff (the_insn.exp))
4742                     the_insn.reloc = R_PARISC_TLS_LE21L;
4743 #endif
4744                   else
4745                     the_insn.reloc = R_HPPA;
4746                   the_insn.format = 14;
4747                   continue;
4748                 }
4749
4750             /* Handle a 14 bit immediate at 31.  */
4751             case 'j':
4752               the_insn.field_selector = pa_chk_field_selector (&s);
4753               get_expression (s);
4754               s = expr_end;
4755               if (the_insn.exp.X_op == O_constant)
4756                 {
4757                   num = evaluate_absolute (&the_insn);
4758                   CHECK_FIELD (num, 8191, -8192, 0);
4759                   num = low_sign_unext (num, 14);
4760                   INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
4761                 }
4762               else
4763                 {
4764                   if (is_DP_relative (the_insn.exp))
4765                     the_insn.reloc = R_HPPA_GOTOFF;
4766                   else if (is_PC_relative (the_insn.exp))
4767                     the_insn.reloc = R_HPPA_PCREL_CALL;
4768 #ifdef OBJ_ELF
4769                   else if (is_tls_gdidx (the_insn.exp))
4770                     the_insn.reloc = R_PARISC_TLS_GD21L;
4771                   else if (is_tls_ldidx (the_insn.exp))
4772                     the_insn.reloc = R_PARISC_TLS_LDM21L;
4773                   else if (is_tls_dtpoff (the_insn.exp))
4774                     the_insn.reloc = R_PARISC_TLS_LDO21L;
4775                   else if (is_tls_ieoff (the_insn.exp))
4776                     the_insn.reloc = R_PARISC_TLS_IE21L;
4777                   else if (is_tls_leoff (the_insn.exp))
4778                     the_insn.reloc = R_PARISC_TLS_LE21L;
4779 #endif
4780                   else
4781                     the_insn.reloc = R_HPPA;
4782                   the_insn.format = 14;
4783                   continue;
4784                 }
4785
4786             /* Handle a 21 bit immediate at 31.  */
4787             case 'k':
4788               the_insn.field_selector = pa_chk_field_selector (&s);
4789               get_expression (s);
4790               s = expr_end;
4791               if (the_insn.exp.X_op == O_constant)
4792                 {
4793                   num = evaluate_absolute (&the_insn);
4794                   CHECK_FIELD (num >> 11, 1048575, -1048576, 0);
4795                   opcode |= re_assemble_21 (num);
4796                   continue;
4797                 }
4798               else
4799                 {
4800                   if (is_DP_relative (the_insn.exp))
4801                     the_insn.reloc = R_HPPA_GOTOFF;
4802                   else if (is_PC_relative (the_insn.exp))
4803                     the_insn.reloc = R_HPPA_PCREL_CALL;
4804 #ifdef OBJ_ELF
4805                   else if (is_tls_gdidx (the_insn.exp))
4806                     the_insn.reloc = R_PARISC_TLS_GD21L;
4807                   else if (is_tls_ldidx (the_insn.exp))
4808                     the_insn.reloc = R_PARISC_TLS_LDM21L;
4809                   else if (is_tls_dtpoff (the_insn.exp))
4810                     the_insn.reloc = R_PARISC_TLS_LDO21L;
4811                   else if (is_tls_ieoff (the_insn.exp))
4812                     the_insn.reloc = R_PARISC_TLS_IE21L;
4813                   else if (is_tls_leoff (the_insn.exp))
4814                     the_insn.reloc = R_PARISC_TLS_LE21L;
4815 #endif
4816                   else
4817                     the_insn.reloc = R_HPPA;
4818                   the_insn.format = 21;
4819                   continue;
4820                 }
4821
4822             /* Handle a 16 bit immediate at 31 (PA 2.0 wide mode only).  */
4823             case 'l':
4824               the_insn.field_selector = pa_chk_field_selector (&s);
4825               get_expression (s);
4826               s = expr_end;
4827               if (the_insn.exp.X_op == O_constant)
4828                 {
4829                   num = evaluate_absolute (&the_insn);
4830                   CHECK_FIELD (num, 32767, -32768, 0);
4831                   opcode |= re_assemble_16 (num);
4832                   continue;
4833                 }
4834               else
4835                 {
4836                   /* ??? Is this valid for wide mode?  */
4837                   if (is_DP_relative (the_insn.exp))
4838                     the_insn.reloc = R_HPPA_GOTOFF;
4839                   else if (is_PC_relative (the_insn.exp))
4840                     the_insn.reloc = R_HPPA_PCREL_CALL;
4841 #ifdef OBJ_ELF
4842                   else if (is_tls_gdidx (the_insn.exp))
4843                     the_insn.reloc = R_PARISC_TLS_GD21L;
4844                   else if (is_tls_ldidx (the_insn.exp))
4845                     the_insn.reloc = R_PARISC_TLS_LDM21L;
4846                   else if (is_tls_dtpoff (the_insn.exp))
4847                     the_insn.reloc = R_PARISC_TLS_LDO21L;
4848                   else if (is_tls_ieoff (the_insn.exp))
4849                     the_insn.reloc = R_PARISC_TLS_IE21L;
4850                   else if (is_tls_leoff (the_insn.exp))
4851                     the_insn.reloc = R_PARISC_TLS_LE21L;
4852 #endif
4853                   else
4854                     the_insn.reloc = R_HPPA;
4855                   the_insn.format = 14;
4856                   continue;
4857                 }
4858
4859             /* Handle a word-aligned 16-bit imm. at 31 (PA2.0 wide).  */
4860             case 'y':
4861               the_insn.field_selector = pa_chk_field_selector (&s);
4862               get_expression (s);
4863               s = expr_end;
4864               if (the_insn.exp.X_op == O_constant)
4865                 {
4866                   num = evaluate_absolute (&the_insn);
4867                   CHECK_FIELD (num, 32767, -32768, 0);
4868                   CHECK_ALIGN (num, 4, 0);
4869                   opcode |= re_assemble_16 (num);
4870                   continue;
4871                 }
4872               else
4873                 {
4874                   /* ??? Is this valid for wide mode?  */
4875                   if (is_DP_relative (the_insn.exp))
4876                     the_insn.reloc = R_HPPA_GOTOFF;
4877                   else if (is_PC_relative (the_insn.exp))
4878                     the_insn.reloc = R_HPPA_PCREL_CALL;
4879 #ifdef OBJ_ELF
4880                   else if (is_tls_gdidx (the_insn.exp))
4881                     the_insn.reloc = R_PARISC_TLS_GD21L;
4882                   else if (is_tls_ldidx (the_insn.exp))
4883                     the_insn.reloc = R_PARISC_TLS_LDM21L;
4884                   else if (is_tls_dtpoff (the_insn.exp))
4885                     the_insn.reloc = R_PARISC_TLS_LDO21L;
4886                   else if (is_tls_ieoff (the_insn.exp))
4887                     the_insn.reloc = R_PARISC_TLS_IE21L;
4888                   else if (is_tls_leoff (the_insn.exp))
4889                     the_insn.reloc = R_PARISC_TLS_LE21L;
4890 #endif
4891                   else
4892                     the_insn.reloc = R_HPPA;
4893                   the_insn.format = 14;
4894                   continue;
4895                 }
4896
4897             /* Handle a dword-aligned 16-bit imm. at 31 (PA2.0 wide).  */
4898             case '&':
4899               the_insn.field_selector = pa_chk_field_selector (&s);
4900               get_expression (s);
4901               s = expr_end;
4902               if (the_insn.exp.X_op == O_constant)
4903                 {
4904                   num = evaluate_absolute (&the_insn);
4905                   CHECK_FIELD (num, 32767, -32768, 0);
4906                   CHECK_ALIGN (num, 8, 0);
4907                   opcode |= re_assemble_16 (num);
4908                   continue;
4909                 }
4910               else
4911                 {
4912                   /* ??? Is this valid for wide mode?  */
4913                   if (is_DP_relative (the_insn.exp))
4914                     the_insn.reloc = R_HPPA_GOTOFF;
4915                   else if (is_PC_relative (the_insn.exp))
4916                     the_insn.reloc = R_HPPA_PCREL_CALL;
4917 #ifdef OBJ_ELF
4918                   else if (is_tls_gdidx (the_insn.exp))
4919                     the_insn.reloc = R_PARISC_TLS_GD21L;
4920                   else if (is_tls_ldidx (the_insn.exp))
4921                     the_insn.reloc = R_PARISC_TLS_LDM21L;
4922                   else if (is_tls_dtpoff (the_insn.exp))
4923                     the_insn.reloc = R_PARISC_TLS_LDO21L;
4924                   else if (is_tls_ieoff (the_insn.exp))
4925                     the_insn.reloc = R_PARISC_TLS_IE21L;
4926                   else if (is_tls_leoff (the_insn.exp))
4927                     the_insn.reloc = R_PARISC_TLS_LE21L;
4928 #endif
4929                   else
4930                     the_insn.reloc = R_HPPA;
4931                   the_insn.format = 14;
4932                   continue;
4933                 }
4934
4935             /* Handle a 12 bit branch displacement.  */
4936             case 'w':
4937               the_insn.field_selector = pa_chk_field_selector (&s);
4938               get_expression (s);
4939               s = expr_end;
4940               the_insn.pcrel = 1;
4941               if (!the_insn.exp.X_add_symbol
4942                   || !strcmp (S_GET_NAME (the_insn.exp.X_add_symbol),
4943                               FAKE_LABEL_NAME))
4944                 {
4945                   num = evaluate_absolute (&the_insn);
4946                   if (num % 4)
4947                     {
4948                       as_bad (_("Branch to unaligned address"));
4949                       break;
4950                     }
4951                   if (the_insn.exp.X_add_symbol)
4952                     num -= 8;
4953                   CHECK_FIELD (num, 8191, -8192, 0);
4954                   opcode |= re_assemble_12 (num >> 2);
4955                   continue;
4956                 }
4957               else
4958                 {
4959                   the_insn.reloc = R_HPPA_PCREL_CALL;
4960                   the_insn.format = 12;
4961                   the_insn.arg_reloc = last_call_desc.arg_reloc;
4962                   memset (&last_call_desc, 0, sizeof (struct call_desc));
4963                   s = expr_end;
4964                   continue;
4965                 }
4966
4967             /* Handle a 17 bit branch displacement.  */
4968             case 'W':
4969               the_insn.field_selector = pa_chk_field_selector (&s);
4970               get_expression (s);
4971               s = expr_end;
4972               the_insn.pcrel = 1;
4973               if (!the_insn.exp.X_add_symbol
4974                   || !strcmp (S_GET_NAME (the_insn.exp.X_add_symbol),
4975                               FAKE_LABEL_NAME))
4976                 {
4977                   num = evaluate_absolute (&the_insn);
4978                   if (num % 4)
4979                     {
4980                       as_bad (_("Branch to unaligned address"));
4981                       break;
4982                     }
4983                   if (the_insn.exp.X_add_symbol)
4984                     num -= 8;
4985                   CHECK_FIELD (num, 262143, -262144, 0);
4986                   opcode |= re_assemble_17 (num >> 2);
4987                   continue;
4988                 }
4989               else
4990                 {
4991                   the_insn.reloc = R_HPPA_PCREL_CALL;
4992                   the_insn.format = 17;
4993                   the_insn.arg_reloc = last_call_desc.arg_reloc;
4994                   memset (&last_call_desc, 0, sizeof (struct call_desc));
4995                   continue;
4996                 }
4997
4998             /* Handle a 22 bit branch displacement.  */
4999             case 'X':
5000               the_insn.field_selector = pa_chk_field_selector (&s);
5001               get_expression (s);
5002               s = expr_end;
5003               the_insn.pcrel = 1;
5004               if (!the_insn.exp.X_add_symbol
5005                   || !strcmp (S_GET_NAME (the_insn.exp.X_add_symbol),
5006                               FAKE_LABEL_NAME))
5007                 {
5008                   num = evaluate_absolute (&the_insn);
5009                   if (num % 4)
5010                     {
5011                       as_bad (_("Branch to unaligned address"));
5012                       break;
5013                     }
5014                   if (the_insn.exp.X_add_symbol)
5015                     num -= 8;
5016                   CHECK_FIELD (num, 8388607, -8388608, 0);
5017                   opcode |= re_assemble_22 (num >> 2);
5018                 }
5019               else
5020                 {
5021                   the_insn.reloc = R_HPPA_PCREL_CALL;
5022                   the_insn.format = 22;
5023                   the_insn.arg_reloc = last_call_desc.arg_reloc;
5024                   memset (&last_call_desc, 0, sizeof (struct call_desc));
5025                   continue;
5026                 }
5027
5028             /* Handle an absolute 17 bit branch target.  */
5029             case 'z':
5030               the_insn.field_selector = pa_chk_field_selector (&s);
5031               get_expression (s);
5032               s = expr_end;
5033               the_insn.pcrel = 0;
5034               if (!the_insn.exp.X_add_symbol
5035                   || !strcmp (S_GET_NAME (the_insn.exp.X_add_symbol),
5036                               FAKE_LABEL_NAME))
5037                 {
5038                   num = evaluate_absolute (&the_insn);
5039                   if (num % 4)
5040                     {
5041                       as_bad (_("Branch to unaligned address"));
5042                       break;
5043                     }
5044                   if (the_insn.exp.X_add_symbol)
5045                     num -= 8;
5046                   CHECK_FIELD (num, 262143, -262144, 0);
5047                   opcode |= re_assemble_17 (num >> 2);
5048                   continue;
5049                 }
5050               else
5051                 {
5052                   the_insn.reloc = R_HPPA_ABS_CALL;
5053                   the_insn.format = 17;
5054                   the_insn.arg_reloc = last_call_desc.arg_reloc;
5055                   memset (&last_call_desc, 0, sizeof (struct call_desc));
5056                   continue;
5057                 }
5058
5059             /* Handle '%r1' implicit operand of addil instruction.  */
5060             case 'Z':
5061               if (*s == ',' && *(s + 1) == '%' && *(s + 3) == '1'
5062                   && (*(s + 2) == 'r' || *(s + 2) == 'R'))
5063                 {
5064                   s += 4;
5065                   continue;
5066                 }
5067               else
5068                 break;
5069
5070             /* Handle '%sr0,%r31' implicit operand of be,l instruction.  */
5071             case 'Y':
5072               if (strncasecmp (s, "%sr0,%r31", 9) != 0)
5073                 break;
5074               s += 9;
5075               continue;
5076
5077             /* Handle immediate value of 0 for ordered load/store instructions.  */
5078             case '@':
5079               if (*s != '0')
5080                 break;
5081               s++;
5082               continue;
5083
5084             /* Handle a 2 bit shift count at 25.  */
5085             case '.':
5086               num = pa_get_absolute_expression (&the_insn, &s);
5087               if (strict && the_insn.exp.X_op != O_constant)
5088                 break;
5089               s = expr_end;
5090               CHECK_FIELD (num, 3, 1, strict);
5091               INSERT_FIELD_AND_CONTINUE (opcode, num, 6);
5092
5093             /* Handle a 4 bit shift count at 25.  */
5094             case '*':
5095               num = pa_get_absolute_expression (&the_insn, &s);
5096               if (strict && the_insn.exp.X_op != O_constant)
5097                 break;
5098               s = expr_end;
5099               CHECK_FIELD (num, 15, 0, strict);
5100               INSERT_FIELD_AND_CONTINUE (opcode, num, 6);
5101
5102             /* Handle a 5 bit shift count at 26.  */
5103             case 'p':
5104               num = pa_get_absolute_expression (&the_insn, &s);
5105               if (strict && the_insn.exp.X_op != O_constant)
5106                 break;
5107               s = expr_end;
5108               CHECK_FIELD (num, 31, 0, strict);
5109               INSERT_FIELD_AND_CONTINUE (opcode, 31 - num, 5);
5110
5111             /* Handle a 6 bit shift count at 20,22:26.  */
5112             case '~':
5113               num = pa_get_absolute_expression (&the_insn, &s);
5114               if (strict && the_insn.exp.X_op != O_constant)
5115                 break;
5116               s = expr_end;
5117               CHECK_FIELD (num, 63, 0, strict);
5118               num = 63 - num;
5119               opcode |= (num & 0x20) << 6;
5120               INSERT_FIELD_AND_CONTINUE (opcode, num & 0x1f, 5);
5121
5122             /* Handle a 6 bit field length at 23,27:31.  */
5123             case '%':
5124               flag = 0;
5125               num = pa_get_absolute_expression (&the_insn, &s);
5126               if (strict && the_insn.exp.X_op != O_constant)
5127                 break;
5128               s = expr_end;
5129               CHECK_FIELD (num, 64, 1, strict);
5130               num--;
5131               opcode |= (num & 0x20) << 3;
5132               num = 31 - (num & 0x1f);
5133               INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
5134
5135             /* Handle a 6 bit field length at 19,27:31.  */
5136             case '|':
5137               num = pa_get_absolute_expression (&the_insn, &s);
5138               if (strict && the_insn.exp.X_op != O_constant)
5139                 break;
5140               s = expr_end;
5141               CHECK_FIELD (num, 64, 1, strict);
5142               num--;
5143               opcode |= (num & 0x20) << 7;
5144               num = 31 - (num & 0x1f);
5145               INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
5146
5147             /* Handle a 5 bit bit position at 26.  */
5148             case 'P':
5149               num = pa_get_absolute_expression (&the_insn, &s);
5150               if (strict && the_insn.exp.X_op != O_constant)
5151                 break;
5152               s = expr_end;
5153               CHECK_FIELD (num, 31, 0, strict);
5154               INSERT_FIELD_AND_CONTINUE (opcode, num, 5);
5155
5156             /* Handle a 6 bit bit position at 20,22:26.  */
5157             case 'q':
5158               num = pa_get_absolute_expression (&the_insn, &s);
5159               if (strict && the_insn.exp.X_op != O_constant)
5160                 break;
5161               s = expr_end;
5162               CHECK_FIELD (num, 63, 0, strict);
5163               opcode |= (num & 0x20) << 6;
5164               INSERT_FIELD_AND_CONTINUE (opcode, num & 0x1f, 5);
5165
5166             /* Handle a 5 bit immediate at 10 with 'd' as the complement
5167                of the high bit of the immediate.  */
5168             case 'B':
5169               num = pa_get_absolute_expression (&the_insn, &s);
5170               if (strict && the_insn.exp.X_op != O_constant)
5171                 break;
5172               s = expr_end;
5173               CHECK_FIELD (num, 63, 0, strict);
5174               if (num & 0x20)
5175                 ;
5176               else
5177                 opcode |= (1 << 13);
5178               INSERT_FIELD_AND_CONTINUE (opcode, num & 0x1f, 21);
5179
5180             /* Handle a 5 bit immediate at 10.  */
5181             case 'Q':
5182               num = pa_get_absolute_expression (&the_insn, &s);
5183               if (strict && the_insn.exp.X_op != O_constant)
5184                 break;
5185               s = expr_end;
5186               CHECK_FIELD (num, 31, 0, strict);
5187               INSERT_FIELD_AND_CONTINUE (opcode, num, 21);
5188
5189             /* Handle a 9 bit immediate at 28.  */
5190             case '$':
5191               num = pa_get_absolute_expression (&the_insn, &s);
5192               if (strict && the_insn.exp.X_op != O_constant)
5193                 break;
5194               s = expr_end;
5195               CHECK_FIELD (num, 511, 1, strict);
5196               INSERT_FIELD_AND_CONTINUE (opcode, num, 3);
5197
5198             /* Handle a 13 bit immediate at 18.  */
5199             case 'A':
5200               num = pa_get_absolute_expression (&the_insn, &s);
5201               if (strict && the_insn.exp.X_op != O_constant)
5202                 break;
5203               s = expr_end;
5204               CHECK_FIELD (num, 8191, 0, strict);
5205               INSERT_FIELD_AND_CONTINUE (opcode, num, 13);
5206
5207             /* Handle a 26 bit immediate at 31.  */
5208             case 'D':
5209               num = pa_get_absolute_expression (&the_insn, &s);
5210               if (strict && the_insn.exp.X_op != O_constant)
5211                 break;
5212               s = expr_end;
5213               CHECK_FIELD (num, 67108863, 0, strict);
5214               INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
5215
5216             /* Handle a 3 bit SFU identifier at 25.  */
5217             case 'v':
5218               if (*s++ != ',')
5219                 as_bad (_("Invalid SFU identifier"));
5220               num = pa_get_absolute_expression (&the_insn, &s);
5221               if (strict && the_insn.exp.X_op != O_constant)
5222                 break;
5223               s = expr_end;
5224               CHECK_FIELD (num, 7, 0, strict);
5225               INSERT_FIELD_AND_CONTINUE (opcode, num, 6);
5226
5227             /* Handle a 20 bit SOP field for spop0.  */
5228             case 'O':
5229               num = pa_get_absolute_expression (&the_insn, &s);
5230               if (strict && the_insn.exp.X_op != O_constant)
5231                 break;
5232               s = expr_end;
5233               CHECK_FIELD (num, 1048575, 0, strict);
5234               num = (num & 0x1f) | ((num & 0x000fffe0) << 6);
5235               INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
5236
5237             /* Handle a 15bit SOP field for spop1.  */
5238             case 'o':
5239               num = pa_get_absolute_expression (&the_insn, &s);
5240               if (strict && the_insn.exp.X_op != O_constant)
5241                 break;
5242               s = expr_end;
5243               CHECK_FIELD (num, 32767, 0, strict);
5244               INSERT_FIELD_AND_CONTINUE (opcode, num, 11);
5245
5246             /* Handle a 10bit SOP field for spop3.  */
5247             case '0':
5248               num = pa_get_absolute_expression (&the_insn, &s);
5249               if (strict && the_insn.exp.X_op != O_constant)
5250                 break;
5251               s = expr_end;
5252               CHECK_FIELD (num, 1023, 0, strict);
5253               num = (num & 0x1f) | ((num & 0x000003e0) << 6);
5254               INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
5255
5256             /* Handle a 15 bit SOP field for spop2.  */
5257             case '1':
5258               num = pa_get_absolute_expression (&the_insn, &s);
5259               if (strict && the_insn.exp.X_op != O_constant)
5260                 break;
5261               s = expr_end;
5262               CHECK_FIELD (num, 32767, 0, strict);
5263               num = (num & 0x1f) | ((num & 0x00007fe0) << 6);
5264               INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
5265
5266             /* Handle a 3-bit co-processor ID field.  */
5267             case 'u':
5268               if (*s++ != ',')
5269                 as_bad (_("Invalid COPR identifier"));
5270               num = pa_get_absolute_expression (&the_insn, &s);
5271               if (strict && the_insn.exp.X_op != O_constant)
5272                 break;
5273               s = expr_end;
5274               CHECK_FIELD (num, 7, 0, strict);
5275               INSERT_FIELD_AND_CONTINUE (opcode, num, 6);
5276
5277             /* Handle a 22bit SOP field for copr.  */
5278             case '2':
5279               num = pa_get_absolute_expression (&the_insn, &s);
5280               if (strict && the_insn.exp.X_op != O_constant)
5281                 break;
5282               s = expr_end;
5283               CHECK_FIELD (num, 4194303, 0, strict);
5284               num = (num & 0x1f) | ((num & 0x003fffe0) << 4);
5285               INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
5286
5287             /* Handle a source FP operand format completer.  */
5288             case '{':
5289               if (*s == ',' && *(s+1) == 't')
5290                 {
5291                   the_insn.trunc = 1;
5292                   s += 2;
5293                 }
5294               else
5295                 the_insn.trunc = 0;
5296               flag = pa_parse_fp_cnv_format (&s);
5297               the_insn.fpof1 = flag;
5298               if (flag == W || flag == UW)
5299                 flag = SGL;
5300               if (flag == DW || flag == UDW)
5301                 flag = DBL;
5302               if (flag == QW || flag == UQW)
5303                 flag = QUAD;
5304               INSERT_FIELD_AND_CONTINUE (opcode, flag, 11);
5305
5306             /* Handle a destination FP operand format completer.  */
5307             case '_':
5308               /* pa_parse_format needs the ',' prefix.  */
5309               s--;
5310               flag = pa_parse_fp_cnv_format (&s);
5311               the_insn.fpof2 = flag;
5312               if (flag == W || flag == UW)
5313                 flag = SGL;
5314               if (flag == DW || flag == UDW)
5315                 flag = DBL;
5316               if (flag == QW || flag == UQW)
5317                 flag = QUAD;
5318               opcode |= flag << 13;
5319               if (the_insn.fpof1 == SGL
5320                   || the_insn.fpof1 == DBL
5321                   || the_insn.fpof1 == QUAD)
5322                 {
5323                   if (the_insn.fpof2 == SGL
5324                       || the_insn.fpof2 == DBL
5325                       || the_insn.fpof2 == QUAD)
5326                     flag = 0;
5327                   else if (the_insn.fpof2 == W
5328                       || the_insn.fpof2 == DW
5329                       || the_insn.fpof2 == QW)
5330                     flag = 2;
5331                   else if (the_insn.fpof2 == UW
5332                       || the_insn.fpof2 == UDW
5333                       || the_insn.fpof2 == UQW)
5334                     flag = 6;
5335                   else
5336                     abort ();
5337                 }
5338               else if (the_insn.fpof1 == W
5339                        || the_insn.fpof1 == DW
5340                        || the_insn.fpof1 == QW)
5341                 {
5342                   if (the_insn.fpof2 == SGL
5343                       || the_insn.fpof2 == DBL
5344                       || the_insn.fpof2 == QUAD)
5345                     flag = 1;
5346                   else
5347                     abort ();
5348                 }
5349               else if (the_insn.fpof1 == UW
5350                        || the_insn.fpof1 == UDW
5351                        || the_insn.fpof1 == UQW)
5352                 {
5353                   if (the_insn.fpof2 == SGL
5354                       || the_insn.fpof2 == DBL
5355                       || the_insn.fpof2 == QUAD)
5356                     flag = 5;
5357                   else
5358                     abort ();
5359                 }
5360               flag |= the_insn.trunc;
5361               INSERT_FIELD_AND_CONTINUE (opcode, flag, 15);
5362
5363             /* Handle a source FP operand format completer.  */
5364             case 'F':
5365               flag = pa_parse_fp_format (&s);
5366               the_insn.fpof1 = flag;
5367               INSERT_FIELD_AND_CONTINUE (opcode, flag, 11);
5368
5369             /* Handle a destination FP operand format completer.  */
5370             case 'G':
5371               /* pa_parse_format needs the ',' prefix.  */
5372               s--;
5373               flag = pa_parse_fp_format (&s);
5374               the_insn.fpof2 = flag;
5375               INSERT_FIELD_AND_CONTINUE (opcode, flag, 13);
5376
5377             /* Handle a source FP operand format completer at 20.  */
5378             case 'I':
5379               flag = pa_parse_fp_format (&s);
5380               the_insn.fpof1 = flag;
5381               INSERT_FIELD_AND_CONTINUE (opcode, flag, 11);
5382
5383             /* Handle a floating point operand format at 26.
5384                Only allows single and double precision.  */
5385             case 'H':
5386               flag = pa_parse_fp_format (&s);
5387               switch (flag)
5388                 {
5389                 case SGL:
5390                   opcode |= 0x20;
5391                 case DBL:
5392                   the_insn.fpof1 = flag;
5393                   continue;
5394
5395                 case QUAD:
5396                 case ILLEGAL_FMT:
5397                 default:
5398                   as_bad (_("Invalid Floating Point Operand Format."));
5399                 }
5400               break;
5401
5402             /* Handle all floating point registers.  */
5403             case 'f':
5404               switch (*++args)
5405                 {
5406                 /* Float target register.  */
5407                 case 't':
5408                   if (!pa_parse_number (&s, 3))
5409                     break;
5410                   num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5411                   CHECK_FIELD (num, 31, 0, 0);
5412                   INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
5413
5414                 /* Float target register with L/R selection.  */
5415                 case 'T':
5416                   {
5417                     if (!pa_parse_number (&s, 1))
5418                       break;
5419                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5420                     CHECK_FIELD (num, 31, 0, 0);
5421                     opcode |= num;
5422
5423                     /* 0x30 opcodes are FP arithmetic operation opcodes
5424                        and need to be turned into 0x38 opcodes.  This
5425                        is not necessary for loads/stores.  */
5426                     if (need_pa11_opcode ()
5427                         && ((opcode & 0xfc000000) == 0x30000000))
5428                       opcode |= 1 << 27;
5429
5430                     opcode |= (pa_number & FP_REG_RSEL ? 1 << 6 : 0);
5431                     continue;
5432                   }
5433
5434                 /* Float operand 1.  */
5435                 case 'a':
5436                   {
5437                     if (!pa_parse_number (&s, 1))
5438                       break;
5439                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5440                     CHECK_FIELD (num, 31, 0, 0);
5441                     opcode |= num << 21;
5442                     if (need_pa11_opcode ())
5443                       {
5444                         opcode |= (pa_number & FP_REG_RSEL ? 1 << 7 : 0);
5445                         opcode |= 1 << 27;
5446                       }
5447                     continue;
5448                   }
5449
5450                 /* Float operand 1 with L/R selection.  */
5451                 case 'X':
5452                 case 'A':
5453                   {
5454                     if (!pa_parse_number (&s, 1))
5455                       break;
5456                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5457                     CHECK_FIELD (num, 31, 0, 0);
5458                     opcode |= num << 21;
5459                     opcode |= (pa_number & FP_REG_RSEL ? 1 << 7 : 0);
5460                     continue;
5461                   }
5462
5463                 /* Float operand 2.  */
5464                 case 'b':
5465                   {
5466                     if (!pa_parse_number (&s, 1))
5467                       break;
5468                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5469                     CHECK_FIELD (num, 31, 0, 0);
5470                     opcode |= num << 16;
5471                     if (need_pa11_opcode ())
5472                       {
5473                         opcode |= (pa_number & FP_REG_RSEL ? 1 << 12 : 0);
5474                         opcode |= 1 << 27;
5475                       }
5476                     continue;
5477                   }
5478
5479                 /* Float operand 2 with L/R selection.  */
5480                 case 'B':
5481                   {
5482                     if (!pa_parse_number (&s, 1))
5483                       break;
5484                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5485                     CHECK_FIELD (num, 31, 0, 0);
5486                     opcode |= num << 16;
5487                     opcode |= (pa_number & FP_REG_RSEL ? 1 << 12 : 0);
5488                     continue;
5489                   }
5490
5491                 /* Float operand 3 for fmpyfadd, fmpynfadd.  */
5492                 case 'C':
5493                   {
5494                     if (!pa_parse_number (&s, 1))
5495                       break;
5496                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5497                     CHECK_FIELD (num, 31, 0, 0);
5498                     opcode |= (num & 0x1c) << 11;
5499                     opcode |= (num & 0x03) << 9;
5500                     opcode |= (pa_number & FP_REG_RSEL ? 1 << 8 : 0);
5501                     continue;
5502                   }
5503
5504                 /* Float mult operand 1 for fmpyadd, fmpysub */
5505                 case 'i':
5506                   {
5507                     if (!pa_parse_number (&s, 1))
5508                       break;
5509                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5510                     CHECK_FIELD (num, 31, 0, 0);
5511                     if (the_insn.fpof1 == SGL)
5512                       {
5513                         if (num < 16)
5514                           {
5515                             as_bad  (_("Invalid register for single precision fmpyadd or fmpysub"));
5516                             break;
5517                           }
5518                         num &= 0xF;
5519                         num |= (pa_number & FP_REG_RSEL ? 1 << 4 : 0);
5520                       }
5521                     INSERT_FIELD_AND_CONTINUE (opcode, num, 21);
5522                   }
5523
5524                 /* Float mult operand 2 for fmpyadd, fmpysub */
5525                 case 'j':
5526                   {
5527                     if (!pa_parse_number (&s, 1))
5528                       break;
5529                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5530                     CHECK_FIELD (num, 31, 0, 0);
5531                     if (the_insn.fpof1 == SGL)
5532                       {
5533                         if (num < 16)
5534                           {
5535                             as_bad  (_("Invalid register for single precision fmpyadd or fmpysub"));
5536                             break;
5537                           }
5538                         num &= 0xF;
5539                         num |= (pa_number & FP_REG_RSEL ? 1 << 4 : 0);
5540                       }
5541                     INSERT_FIELD_AND_CONTINUE (opcode, num, 16);
5542                   }
5543
5544                 /* Float mult target for fmpyadd, fmpysub */
5545                 case 'k':
5546                   {
5547                     if (!pa_parse_number (&s, 1))
5548                       break;
5549                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5550                     CHECK_FIELD (num, 31, 0, 0);
5551                     if (the_insn.fpof1 == SGL)
5552                       {
5553                         if (num < 16)
5554                           {
5555                             as_bad  (_("Invalid register for single precision fmpyadd or fmpysub"));
5556                             break;
5557                           }
5558                         num &= 0xF;
5559                         num |= (pa_number & FP_REG_RSEL ? 1 << 4 : 0);
5560                       }
5561                     INSERT_FIELD_AND_CONTINUE (opcode, num, 0);
5562                   }
5563
5564                 /* Float add operand 1 for fmpyadd, fmpysub */
5565                 case 'l':
5566                   {
5567                     if (!pa_parse_number (&s, 1))
5568                       break;
5569                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5570                     CHECK_FIELD (num, 31, 0, 0);
5571                     if (the_insn.fpof1 == SGL)
5572                       {
5573                         if (num < 16)
5574                           {
5575                             as_bad  (_("Invalid register for single precision fmpyadd or fmpysub"));
5576                             break;
5577                           }
5578                         num &= 0xF;
5579                         num |= (pa_number & FP_REG_RSEL ? 1 << 4 : 0);
5580                       }
5581                     INSERT_FIELD_AND_CONTINUE (opcode, num, 6);
5582                   }
5583
5584                 /* Float add target for fmpyadd, fmpysub */
5585                 case 'm':
5586                   {
5587                     if (!pa_parse_number (&s, 1))
5588                       break;
5589                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5590                     CHECK_FIELD (num, 31, 0, 0);
5591                     if (the_insn.fpof1 == SGL)
5592                       {
5593                         if (num < 16)
5594                           {
5595                             as_bad  (_("Invalid register for single precision fmpyadd or fmpysub"));
5596                             break;
5597                           }
5598                         num &= 0xF;
5599                         num |= (pa_number & FP_REG_RSEL ? 1 << 4 : 0);
5600                       }
5601                     INSERT_FIELD_AND_CONTINUE (opcode, num, 11);
5602                   }
5603
5604                 /* Handle L/R register halves like 'x'.  */
5605                 case 'E':
5606                 case 'e':
5607                   {
5608                     if (!pa_parse_number (&s, 1))
5609                       break;
5610                     num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5611                     CHECK_FIELD (num, 31, 0, 0);
5612                     opcode |= num << 16;
5613                     if (need_pa11_opcode ())
5614                       {
5615                         opcode |= (pa_number & FP_REG_RSEL ? 1 << 1 : 0);
5616                       }
5617                     continue;
5618                   }
5619
5620                 /* Float target register (PA 2.0 wide).  */
5621                 case 'x':
5622                   if (!pa_parse_number (&s, 3))
5623                     break;
5624                   num = (pa_number & ~FP_REG_RSEL) - FP_REG_BASE;
5625                   CHECK_FIELD (num, 31, 0, 0);
5626                   INSERT_FIELD_AND_CONTINUE (opcode, num, 16);
5627
5628                 default:
5629                   abort ();
5630                 }
5631               break;
5632
5633             default:
5634               abort ();
5635             }
5636           break;
5637         }
5638
5639       /* If this instruction is specific to a particular architecture,
5640          then set a new architecture.  This automatic promotion crud is
5641          for compatibility with HP's old assemblers only.  */
5642       if (match == TRUE
5643           && bfd_get_mach (stdoutput) < insn->arch
5644           && !bfd_set_arch_mach (stdoutput, bfd_arch_hppa, insn->arch))
5645         {
5646           as_warn (_("could not update architecture and machine"));
5647           match = FALSE;
5648         }
5649
5650  failed:
5651       /* Check if the args matched.  */
5652       if (!match)
5653         {
5654           if (&insn[1] - pa_opcodes < (int) NUMOPCODES
5655               && !strcmp (insn->name, insn[1].name))
5656             {
5657               ++insn;
5658               s = argstart;
5659               continue;
5660             }
5661           else
5662             {
5663               as_bad (_("Invalid operands %s"), error_message);
5664               return;
5665             }
5666         }
5667       break;
5668     }
5669
5670   the_insn.opcode = opcode;
5671 }
5672
5673 /* Assemble a single instruction storing it into a frag.  */
5674
5675 void
5676 md_assemble (char *str)
5677 {
5678   char *to;
5679
5680   /* The had better be something to assemble.  */
5681   assert (str);
5682
5683   /* If we are within a procedure definition, make sure we've
5684      defined a label for the procedure; handle case where the
5685      label was defined after the .PROC directive.
5686
5687      Note there's not need to diddle with the segment or fragment
5688      for the label symbol in this case.  We have already switched
5689      into the new $CODE$ subspace at this point.  */
5690   if (within_procedure && last_call_info->start_symbol == NULL)
5691     {
5692       label_symbol_struct *label_symbol = pa_get_label ();
5693
5694       if (label_symbol)
5695         {
5696           if (label_symbol->lss_label)
5697             {
5698               last_call_info->start_symbol = label_symbol->lss_label;
5699               symbol_get_bfdsym (label_symbol->lss_label)->flags
5700                 |= BSF_FUNCTION;
5701 #ifdef OBJ_SOM
5702               /* Also handle allocation of a fixup to hold the unwind
5703                  information when the label appears after the proc/procend.  */
5704               if (within_entry_exit)
5705                 {
5706                   char *where;
5707                   unsigned int u;
5708
5709                   where = frag_more (0);
5710                   u = UNWIND_LOW32 (&last_call_info->ci_unwind.descriptor);
5711                   fix_new_hppa (frag_now, where - frag_now->fr_literal, 0,
5712                                 NULL, (offsetT) 0, NULL,
5713                                 0, R_HPPA_ENTRY, e_fsel, 0, 0, u);
5714                 }
5715 #endif
5716             }
5717           else
5718             as_bad (_("Missing function name for .PROC (corrupted label chain)"));
5719         }
5720       else
5721         as_bad (_("Missing function name for .PROC"));
5722     }
5723
5724   /* Assemble the instruction.  Results are saved into "the_insn".  */
5725   pa_ip (str);
5726
5727   /* Get somewhere to put the assembled instruction.  */
5728   to = frag_more (4);
5729
5730   /* Output the opcode.  */
5731   md_number_to_chars (to, the_insn.opcode, 4);
5732
5733   /* If necessary output more stuff.  */
5734   if (the_insn.reloc != R_HPPA_NONE)
5735     fix_new_hppa (frag_now, (to - frag_now->fr_literal), 4, NULL,
5736                   (offsetT) 0, &the_insn.exp, the_insn.pcrel,
5737                   the_insn.reloc, the_insn.field_selector,
5738                   the_insn.format, the_insn.arg_reloc, 0);
5739
5740 #ifdef OBJ_ELF
5741   dwarf2_emit_insn (4);
5742 #endif
5743 }
5744
5745 #ifdef OBJ_SOM
5746 /* Handle an alignment directive.  Special so that we can update the
5747    alignment of the subspace if necessary.  */
5748 static void
5749 pa_align (int bytes)
5750 {
5751   /* We must have a valid space and subspace.  */
5752   pa_check_current_space_and_subspace ();
5753
5754   /* Let the generic gas code do most of the work.  */
5755   s_align_bytes (bytes);
5756
5757   /* If bytes is a power of 2, then update the current subspace's
5758      alignment if necessary.  */
5759   if (exact_log2 (bytes) != -1)
5760     record_alignment (current_subspace->ssd_seg, exact_log2 (bytes));
5761 }
5762 #endif
5763
5764 /* Handle a .BLOCK type pseudo-op.  */
5765
5766 static void
5767 pa_block (int z ATTRIBUTE_UNUSED)
5768 {
5769   unsigned int temp_size;
5770
5771 #ifdef OBJ_SOM
5772   /* We must have a valid space and subspace.  */
5773   pa_check_current_space_and_subspace ();
5774 #endif
5775
5776   temp_size = get_absolute_expression ();
5777
5778   if (temp_size > 0x3FFFFFFF)
5779     {
5780       as_bad (_("Argument to .BLOCK/.BLOCKZ must be between 0 and 0x3fffffff"));
5781       temp_size = 0;
5782     }
5783   else
5784     {
5785       /* Always fill with zeros, that's what the HP assembler does.  */
5786       char *p = frag_var (rs_fill, 1, 1, 0, NULL, temp_size, NULL);
5787       *p = 0;
5788     }
5789
5790   pa_undefine_label ();
5791   demand_empty_rest_of_line ();
5792 }
5793
5794 /* Handle a .begin_brtab and .end_brtab pseudo-op.  */
5795
5796 static void
5797 pa_brtab (int begin ATTRIBUTE_UNUSED)
5798 {
5799
5800 #ifdef OBJ_SOM
5801   /* The BRTAB relocations are only available in SOM (to denote
5802      the beginning and end of branch tables).  */
5803   char *where = frag_more (0);
5804
5805   fix_new_hppa (frag_now, where - frag_now->fr_literal, 0,
5806                 NULL, (offsetT) 0, NULL,
5807                 0, begin ? R_HPPA_BEGIN_BRTAB : R_HPPA_END_BRTAB,
5808                 e_fsel, 0, 0, 0);
5809 #endif
5810
5811   demand_empty_rest_of_line ();
5812 }
5813
5814 /* Handle a .begin_try and .end_try pseudo-op.  */
5815
5816 static void
5817 pa_try (int begin ATTRIBUTE_UNUSED)
5818 {
5819 #ifdef OBJ_SOM
5820   expressionS exp;
5821   char *where = frag_more (0);
5822
5823   if (! begin)
5824     expression (&exp);
5825
5826   /* The TRY relocations are only available in SOM (to denote
5827      the beginning and end of exception handling regions).  */
5828
5829   fix_new_hppa (frag_now, where - frag_now->fr_literal, 0,
5830                 NULL, (offsetT) 0, begin ? NULL : &exp,
5831                 0, begin ? R_HPPA_BEGIN_TRY : R_HPPA_END_TRY,
5832                 e_fsel, 0, 0, 0);
5833 #endif
5834
5835   demand_empty_rest_of_line ();
5836 }
5837
5838 /* Do the dirty work of building a call descriptor which describes
5839    where the caller placed arguments to a function call.  */
5840
5841 static void
5842 pa_call_args (struct call_desc *call_desc)
5843 {
5844   char *name, c, *p;
5845   unsigned int temp, arg_reloc;
5846
5847   while (!is_end_of_statement ())
5848     {
5849       name = input_line_pointer;
5850       c = get_symbol_end ();
5851       /* Process a source argument.  */
5852       if ((strncasecmp (name, "argw", 4) == 0))
5853         {
5854           temp = atoi (name + 4);
5855           p = input_line_pointer;
5856           *p = c;
5857           input_line_pointer++;
5858           name = input_line_pointer;
5859           c = get_symbol_end ();
5860           arg_reloc = pa_build_arg_reloc (name);
5861           call_desc->arg_reloc |= pa_align_arg_reloc (temp, arg_reloc);
5862         }
5863       /* Process a return value.  */
5864       else if ((strncasecmp (name, "rtnval", 6) == 0))
5865         {
5866           p = input_line_pointer;
5867           *p = c;
5868           input_line_pointer++;
5869           name = input_line_pointer;
5870           c = get_symbol_end ();
5871           arg_reloc = pa_build_arg_reloc (name);
5872           call_desc->arg_reloc |= (arg_reloc & 0x3);
5873         }
5874       else
5875         {
5876           as_bad (_("Invalid .CALL argument: %s"), name);
5877         }
5878       p = input_line_pointer;
5879       *p = c;
5880       if (!is_end_of_statement ())
5881         input_line_pointer++;
5882     }
5883 }
5884
5885 /* Handle a .CALL pseudo-op.  This involves storing away information
5886    about where arguments are to be found so the linker can detect
5887    (and correct) argument location mismatches between caller and callee.  */
5888
5889 static void
5890 pa_call (int unused ATTRIBUTE_UNUSED)
5891 {
5892 #ifdef OBJ_SOM
5893   /* We must have a valid space and subspace.  */
5894   pa_check_current_space_and_subspace ();
5895 #endif
5896
5897   pa_call_args (&last_call_desc);
5898   demand_empty_rest_of_line ();
5899 }
5900
5901 /* Return TRUE if FRAG1 and FRAG2 are the same.  */
5902
5903 static bfd_boolean
5904 is_same_frag (fragS *frag1, fragS *frag2)
5905 {
5906
5907   if (frag1 == NULL)
5908     return FALSE;
5909   else if (frag2 == NULL)
5910     return FALSE;
5911   else if (frag1 == frag2)
5912     return TRUE;
5913   else if (frag2->fr_type == rs_fill && frag2->fr_fix == 0)
5914     return (is_same_frag (frag1, frag2->fr_next));
5915   else
5916     return FALSE;
5917 }
5918
5919 #ifdef OBJ_ELF
5920 /* Build an entry in the UNWIND subspace from the given function
5921    attributes in CALL_INFO.  This is not needed for SOM as using
5922    R_ENTRY and R_EXIT relocations allow the linker to handle building
5923    of the unwind spaces.  */
5924
5925 static void
5926 pa_build_unwind_subspace (struct call_info *call_info)
5927 {
5928   asection *seg, *save_seg;
5929   subsegT save_subseg;
5930   unsigned int unwind;
5931   int reloc;
5932   char *p;
5933
5934   if ((bfd_get_section_flags (stdoutput, now_seg)
5935        & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
5936       != (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
5937     return;
5938
5939   reloc = R_PARISC_SEGREL32;
5940   save_seg = now_seg;
5941   save_subseg = now_subseg;
5942   /* Get into the right seg/subseg.  This may involve creating
5943      the seg the first time through.  Make sure to have the
5944      old seg/subseg so that we can reset things when we are done.  */
5945   seg = bfd_get_section_by_name (stdoutput, UNWIND_SECTION_NAME);
5946   if (seg == ASEC_NULL)
5947     {
5948       seg = subseg_new (UNWIND_SECTION_NAME, 0);
5949       bfd_set_section_flags (stdoutput, seg,
5950                              SEC_READONLY | SEC_HAS_CONTENTS
5951                              | SEC_LOAD | SEC_RELOC | SEC_ALLOC | SEC_DATA);
5952       bfd_set_section_alignment (stdoutput, seg, 2);
5953     }
5954
5955   subseg_set (seg, 0);
5956
5957   /* Get some space to hold relocation information for the unwind
5958      descriptor.  */
5959   p = frag_more (16);
5960
5961   /* Relocation info. for start offset of the function.  */
5962   md_number_to_chars (p, 0, 4);
5963   fix_new_hppa (frag_now, p - frag_now->fr_literal, 4,
5964                 call_info->start_symbol, (offsetT) 0,
5965                 (expressionS *) NULL, 0, reloc,
5966                 e_fsel, 32, 0, 0);
5967
5968   /* Relocation info. for end offset of the function.
5969
5970      Because we allow reductions of 32bit relocations for ELF, this will be
5971      reduced to section_sym + offset which avoids putting the temporary
5972      symbol into the symbol table.  It (should) end up giving the same
5973      value as call_info->start_symbol + function size once the linker is
5974      finished with its work.  */
5975   md_number_to_chars (p + 4, 0, 4);
5976   fix_new_hppa (frag_now, p + 4 - frag_now->fr_literal, 4,
5977                 call_info->end_symbol, (offsetT) 0,
5978                 (expressionS *) NULL, 0, reloc,
5979                 e_fsel, 32, 0, 0);
5980
5981   /* Dump the descriptor.  */
5982   unwind = UNWIND_LOW32 (&call_info->ci_unwind.descriptor);
5983   md_number_to_chars (p + 8, unwind, 4);
5984
5985   unwind = UNWIND_HIGH32 (&call_info->ci_unwind.descriptor);
5986   md_number_to_chars (p + 12, unwind, 4);
5987
5988   /* Return back to the original segment/subsegment.  */
5989   subseg_set (save_seg, save_subseg);
5990 }
5991 #endif
5992
5993 /* Process a .CALLINFO pseudo-op.  This information is used later
5994    to build unwind descriptors and maybe one day to support
5995    .ENTER and .LEAVE.  */
5996
5997 static void
5998 pa_callinfo (int unused ATTRIBUTE_UNUSED)
5999 {
6000   char *name, c, *p;
6001   int temp;
6002
6003 #ifdef OBJ_SOM
6004   /* We must have a valid space and subspace.  */
6005   pa_check_current_space_and_subspace ();
6006 #endif
6007
6008   /* .CALLINFO must appear within a procedure definition.  */
6009   if (!within_procedure)
6010     as_bad (_(".callinfo is not within a procedure definition"));
6011
6012   /* Mark the fact that we found the .CALLINFO for the
6013      current procedure.  */
6014   callinfo_found = TRUE;
6015
6016   /* Iterate over the .CALLINFO arguments.  */
6017   while (!is_end_of_statement ())
6018     {
6019       name = input_line_pointer;
6020       c = get_symbol_end ();
6021       /* Frame size specification.  */
6022       if ((strncasecmp (name, "frame", 5) == 0))
6023         {
6024           p = input_line_pointer;
6025           *p = c;
6026           input_line_pointer++;
6027           temp = get_absolute_expression ();
6028           if ((temp & 0x3) != 0)
6029             {
6030               as_bad (_("FRAME parameter must be a multiple of 8: %d\n"), temp);
6031               temp = 0;
6032             }
6033
6034           /* callinfo is in bytes and unwind_desc is in 8 byte units.  */
6035           last_call_info->ci_unwind.descriptor.frame_size = temp / 8;
6036
6037         }
6038       /* Entry register (GR, GR and SR) specifications.  */
6039       else if ((strncasecmp (name, "entry_gr", 8) == 0))
6040         {
6041           p = input_line_pointer;
6042           *p = c;
6043           input_line_pointer++;
6044           temp = get_absolute_expression ();
6045           /* The HP assembler accepts 19 as the high bound for ENTRY_GR
6046              even though %r19 is caller saved.  I think this is a bug in
6047              the HP assembler, and we are not going to emulate it.  */
6048           if (temp < 3 || temp > 18)
6049             as_bad (_("Value for ENTRY_GR must be in the range 3..18\n"));
6050           last_call_info->ci_unwind.descriptor.entry_gr = temp - 2;
6051         }
6052       else if ((strncasecmp (name, "entry_fr", 8) == 0))
6053         {
6054           p = input_line_pointer;
6055           *p = c;
6056           input_line_pointer++;
6057           temp = get_absolute_expression ();
6058           /* Similarly the HP assembler takes 31 as the high bound even
6059              though %fr21 is the last callee saved floating point register.  */
6060           if (temp < 12 || temp > 21)
6061             as_bad (_("Value for ENTRY_FR must be in the range 12..21\n"));
6062           last_call_info->ci_unwind.descriptor.entry_fr = temp - 11;
6063         }
6064       else if ((strncasecmp (name, "entry_sr", 8) == 0))
6065         {
6066           p = input_line_pointer;
6067           *p = c;
6068           input_line_pointer++;
6069           temp = get_absolute_expression ();
6070           if (temp != 3)
6071             as_bad (_("Value for ENTRY_SR must be 3\n"));
6072         }
6073       /* Note whether or not this function performs any calls.  */
6074       else if ((strncasecmp (name, "calls", 5) == 0) ||
6075                (strncasecmp (name, "caller", 6) == 0))
6076         {
6077           p = input_line_pointer;
6078           *p = c;
6079         }
6080       else if ((strncasecmp (name, "no_calls", 8) == 0))
6081         {
6082           p = input_line_pointer;
6083           *p = c;
6084         }
6085       /* Should RP be saved into the stack.  */
6086       else if ((strncasecmp (name, "save_rp", 7) == 0))
6087         {
6088           p = input_line_pointer;
6089           *p = c;
6090           last_call_info->ci_unwind.descriptor.save_rp = 1;
6091         }
6092       /* Likewise for SP.  */
6093       else if ((strncasecmp (name, "save_sp", 7) == 0))
6094         {
6095           p = input_line_pointer;
6096           *p = c;
6097           last_call_info->ci_unwind.descriptor.save_sp = 1;
6098         }
6099       /* Is this an unwindable procedure.  If so mark it so
6100          in the unwind descriptor.  */
6101       else if ((strncasecmp (name, "no_unwind", 9) == 0))
6102         {
6103           p = input_line_pointer;
6104           *p = c;
6105           last_call_info->ci_unwind.descriptor.cannot_unwind = 1;
6106         }
6107       /* Is this an interrupt routine.  If so mark it in the
6108          unwind descriptor.  */
6109       else if ((strncasecmp (name, "hpux_int", 7) == 0))
6110         {
6111           p = input_line_pointer;
6112           *p = c;
6113           last_call_info->ci_unwind.descriptor.hpux_interrupt_marker = 1;
6114         }
6115       /* Is this a millicode routine.  "millicode" isn't in my
6116          assembler manual, but my copy is old.  The HP assembler
6117          accepts it, and there's a place in the unwind descriptor
6118          to drop the information, so we'll accept it too.  */
6119       else if ((strncasecmp (name, "millicode", 9) == 0))
6120         {
6121           p = input_line_pointer;
6122           *p = c;
6123           last_call_info->ci_unwind.descriptor.millicode = 1;
6124         }
6125       else
6126         {
6127           as_bad (_("Invalid .CALLINFO argument: %s"), name);
6128           *input_line_pointer = c;
6129         }
6130       if (!is_end_of_statement ())
6131         input_line_pointer++;
6132     }
6133
6134   demand_empty_rest_of_line ();
6135 }
6136
6137 #if !(defined (OBJ_ELF) && (defined (TE_LINUX) || defined (TE_NetBSD)))
6138 /* Switch to the text space.  Like s_text, but delete our
6139    label when finished.  */
6140
6141 static void
6142 pa_text (int unused ATTRIBUTE_UNUSED)
6143 {
6144 #ifdef OBJ_SOM
6145   current_space = is_defined_space ("$TEXT$");
6146   current_subspace
6147     = pa_subsegment_to_subspace (current_space->sd_seg, 0);
6148 #endif
6149
6150   s_text (0);
6151   pa_undefine_label ();
6152 }
6153
6154 /* Switch to the data space.  As usual delete our label.  */
6155
6156 static void
6157 pa_data (int unused ATTRIBUTE_UNUSED)
6158 {
6159 #ifdef OBJ_SOM
6160   current_space = is_defined_space ("$PRIVATE$");
6161   current_subspace
6162     = pa_subsegment_to_subspace (current_space->sd_seg, 0);
6163 #endif
6164   s_data (0);
6165   pa_undefine_label ();
6166 }
6167
6168 /* This is different than the standard GAS s_comm(). On HP9000/800 machines,
6169    the .comm pseudo-op has the following symtax:
6170
6171    <label> .comm <length>
6172
6173    where <label> is optional and is a symbol whose address will be the start of
6174    a block of memory <length> bytes long. <length> must be an absolute
6175    expression.  <length> bytes will be allocated in the current space
6176    and subspace.
6177
6178    Also note the label may not even be on the same line as the .comm.
6179
6180    This difference in syntax means the colon function will be called
6181    on the symbol before we arrive in pa_comm.  colon will set a number
6182    of attributes of the symbol that need to be fixed here.  In particular
6183    the value, section pointer, fragment pointer, flags, etc.  What
6184    a pain.
6185
6186    This also makes error detection all but impossible.  */
6187
6188 static void
6189 pa_comm (int unused ATTRIBUTE_UNUSED)
6190 {
6191   unsigned int size;
6192   symbolS *symbol;
6193   label_symbol_struct *label_symbol = pa_get_label ();
6194
6195   if (label_symbol)
6196     symbol = label_symbol->lss_label;
6197   else
6198     symbol = NULL;
6199
6200   SKIP_WHITESPACE ();
6201   size = get_absolute_expression ();
6202
6203   if (symbol)
6204     {
6205       symbol_get_bfdsym (symbol)->flags |= BSF_OBJECT;
6206       S_SET_VALUE (symbol, size);
6207       S_SET_SEGMENT (symbol, bfd_com_section_ptr);
6208       S_SET_EXTERNAL (symbol);
6209
6210       /* colon() has already set the frag to the current location in the
6211          current subspace; we need to reset the fragment to the zero address
6212          fragment.  We also need to reset the segment pointer.  */
6213       symbol_set_frag (symbol, &zero_address_frag);
6214     }
6215   demand_empty_rest_of_line ();
6216 }
6217 #endif /* !(defined (OBJ_ELF) && (defined (TE_LINUX) || defined (TE_NetBSD))) */
6218
6219 /* Process a .END pseudo-op.  */
6220
6221 static void
6222 pa_end (int unused ATTRIBUTE_UNUSED)
6223 {
6224   demand_empty_rest_of_line ();
6225 }
6226
6227 /* Process a .ENTER pseudo-op.  This is not supported.  */
6228
6229 static void
6230 pa_enter (int unused ATTRIBUTE_UNUSED)
6231 {
6232 #ifdef OBJ_SOM
6233   /* We must have a valid space and subspace.  */
6234   pa_check_current_space_and_subspace ();
6235 #endif
6236
6237   as_bad (_("The .ENTER pseudo-op is not supported"));
6238   demand_empty_rest_of_line ();
6239 }
6240
6241 /* Process a .ENTRY pseudo-op.  .ENTRY marks the beginning of the
6242    procedure.  */
6243
6244 static void
6245 pa_entry (int unused ATTRIBUTE_UNUSED)
6246 {
6247 #ifdef OBJ_SOM
6248   /* We must have a valid space and subspace.  */
6249   pa_check_current_space_and_subspace ();
6250 #endif
6251
6252   if (!within_procedure)
6253     as_bad (_("Misplaced .entry. Ignored."));
6254   else
6255     {
6256       if (!callinfo_found)
6257         as_bad (_("Missing .callinfo."));
6258     }
6259   demand_empty_rest_of_line ();
6260   within_entry_exit = TRUE;
6261
6262 #ifdef OBJ_SOM
6263   /* SOM defers building of unwind descriptors until the link phase.
6264      The assembler is responsible for creating an R_ENTRY relocation
6265      to mark the beginning of a region and hold the unwind bits, and
6266      for creating an R_EXIT relocation to mark the end of the region.
6267
6268      FIXME.  ELF should be using the same conventions!  The problem
6269      is an unwind requires too much relocation space.  Hmmm.  Maybe
6270      if we split the unwind bits up between the relocations which
6271      denote the entry and exit points.  */
6272   if (last_call_info->start_symbol != NULL)
6273     {
6274       char *where;
6275       unsigned int u;
6276
6277       where = frag_more (0);
6278       u = UNWIND_LOW32 (&last_call_info->ci_unwind.descriptor);
6279       fix_new_hppa (frag_now, where - frag_now->fr_literal, 0,
6280                     NULL, (offsetT) 0, NULL,
6281                     0, R_HPPA_ENTRY, e_fsel, 0, 0, u);
6282     }
6283 #endif
6284 }
6285
6286 /* Silly nonsense for pa_equ.  The only half-sensible use for this is
6287    being able to subtract two register symbols that specify a range of
6288    registers, to get the size of the range.  */
6289 static int fudge_reg_expressions;
6290
6291 int
6292 hppa_force_reg_syms_absolute (expressionS *resultP,
6293                               operatorT op ATTRIBUTE_UNUSED,
6294                               expressionS *rightP)
6295 {
6296   if (fudge_reg_expressions
6297       && rightP->X_op == O_register
6298       && resultP->X_op == O_register)
6299     {
6300       rightP->X_op = O_constant;
6301       resultP->X_op = O_constant;
6302     }
6303   return 0;  /* Continue normal expr handling.  */
6304 }
6305
6306 /* Handle a .EQU pseudo-op.  */
6307
6308 static void
6309 pa_equ (int reg)
6310 {
6311   label_symbol_struct *label_symbol = pa_get_label ();
6312   symbolS *symbol;
6313
6314   if (label_symbol)
6315     {
6316       symbol = label_symbol->lss_label;
6317       if (reg)
6318         {
6319           strict = 1;
6320           if (!pa_parse_number (&input_line_pointer, 0))
6321             as_bad (_(".REG expression must be a register"));
6322           S_SET_VALUE (symbol, pa_number);
6323           S_SET_SEGMENT (symbol, reg_section);
6324         }
6325       else
6326         {
6327           expressionS exp;
6328           segT seg;
6329
6330           fudge_reg_expressions = 1;
6331           seg = expression (&exp);
6332           fudge_reg_expressions = 0;
6333           if (exp.X_op != O_constant
6334               && exp.X_op != O_register)
6335             {
6336               if (exp.X_op != O_absent)
6337                 as_bad (_("bad or irreducible absolute expression; zero assumed"));
6338               exp.X_add_number = 0;
6339               seg = absolute_section;
6340             }
6341           S_SET_VALUE (symbol, (unsigned int) exp.X_add_number);
6342           S_SET_SEGMENT (symbol, seg);
6343         }
6344     }
6345   else
6346     {
6347       if (reg)
6348         as_bad (_(".REG must use a label"));
6349       else
6350         as_bad (_(".EQU must use a label"));
6351     }
6352
6353   pa_undefine_label ();
6354   demand_empty_rest_of_line ();
6355 }
6356
6357 #ifdef OBJ_ELF
6358 /* Mark the end of a function so that it's possible to compute
6359    the size of the function in elf_hppa_final_processing.  */
6360
6361 static void
6362 hppa_elf_mark_end_of_function (void)
6363 {
6364   /* ELF does not have EXIT relocations.  All we do is create a
6365      temporary symbol marking the end of the function.  */
6366   char *name;
6367
6368   if (last_call_info == NULL || last_call_info->start_symbol == NULL)
6369     {
6370       /* We have already warned about a missing label,
6371          or other problems.  */
6372       return;
6373     }
6374
6375   name = xmalloc (strlen ("L$\001end_")
6376                   + strlen (S_GET_NAME (last_call_info->start_symbol))
6377                   + 1);
6378   if (name)
6379     {
6380       symbolS *symbolP;
6381
6382       strcpy (name, "L$\001end_");
6383       strcat (name, S_GET_NAME (last_call_info->start_symbol));
6384
6385       /* If we have a .exit followed by a .procend, then the
6386          symbol will have already been defined.  */
6387       symbolP = symbol_find (name);
6388       if (symbolP)
6389         {
6390           /* The symbol has already been defined!  This can
6391              happen if we have a .exit followed by a .procend.
6392
6393              This is *not* an error.  All we want to do is free
6394              the memory we just allocated for the name and continue.  */
6395           xfree (name);
6396         }
6397       else
6398         {
6399           /* symbol value should be the offset of the
6400              last instruction of the function */
6401           symbolP = symbol_new (name, now_seg, (valueT) (frag_now_fix () - 4),
6402                                 frag_now);
6403
6404           assert (symbolP);
6405           S_CLEAR_EXTERNAL (symbolP);
6406           symbol_table_insert (symbolP);
6407         }
6408
6409       if (symbolP)
6410         last_call_info->end_symbol = symbolP;
6411       else
6412         as_bad (_("Symbol '%s' could not be created."), name);
6413
6414     }
6415   else
6416     as_bad (_("No memory for symbol name."));
6417 }
6418 #endif
6419
6420 /* Helper function.  Does processing for the end of a function.  This
6421    usually involves creating some relocations or building special
6422    symbols to mark the end of the function.  */
6423
6424 static void
6425 process_exit (void)
6426 {
6427   char *where;
6428
6429   where = frag_more (0);
6430
6431 #ifdef OBJ_ELF
6432   /* Mark the end of the function, stuff away the location of the frag
6433      for the end of the function, and finally call pa_build_unwind_subspace
6434      to add an entry in the unwind table.  */
6435   hppa_elf_mark_end_of_function ();
6436   pa_build_unwind_subspace (last_call_info);
6437 #else
6438   /* SOM defers building of unwind descriptors until the link phase.
6439      The assembler is responsible for creating an R_ENTRY relocation
6440      to mark the beginning of a region and hold the unwind bits, and
6441      for creating an R_EXIT relocation to mark the end of the region.
6442
6443      FIXME.  ELF should be using the same conventions!  The problem
6444      is an unwind requires too much relocation space.  Hmmm.  Maybe
6445      if we split the unwind bits up between the relocations which
6446      denote the entry and exit points.  */
6447   fix_new_hppa (frag_now, where - frag_now->fr_literal, 0,
6448                 NULL, (offsetT) 0,
6449                 NULL, 0, R_HPPA_EXIT, e_fsel, 0, 0,
6450                 UNWIND_HIGH32 (&last_call_info->ci_unwind.descriptor));
6451 #endif
6452 }
6453
6454 /* Process a .EXIT pseudo-op.  */
6455
6456 static void
6457 pa_exit (int unused ATTRIBUTE_UNUSED)
6458 {
6459 #ifdef OBJ_SOM
6460   /* We must have a valid space and subspace.  */
6461   pa_check_current_space_and_subspace ();
6462 #endif
6463
6464   if (!within_procedure)
6465     as_bad (_(".EXIT must appear within a procedure"));
6466   else
6467     {
6468       if (!callinfo_found)
6469         as_bad (_("Missing .callinfo"));
6470       else
6471         {
6472           if (!within_entry_exit)
6473             as_bad (_("No .ENTRY for this .EXIT"));
6474           else
6475             {
6476               within_entry_exit = FALSE;
6477               process_exit ();
6478             }
6479         }
6480     }
6481   demand_empty_rest_of_line ();
6482 }
6483
6484 /* Helper function to process arguments to a .EXPORT pseudo-op.  */
6485
6486 static void
6487 pa_type_args (symbolS *symbolP, int is_export)
6488 {
6489   char *name, c, *p;
6490   unsigned int temp, arg_reloc;
6491   pa_symbol_type type = SYMBOL_TYPE_UNKNOWN;
6492   asymbol *bfdsym = symbol_get_bfdsym (symbolP);
6493
6494   if (strncasecmp (input_line_pointer, "absolute", 8) == 0)
6495     {
6496       input_line_pointer += 8;
6497       bfdsym->flags &= ~BSF_FUNCTION;
6498       S_SET_SEGMENT (symbolP, bfd_abs_section_ptr);
6499       type = SYMBOL_TYPE_ABSOLUTE;
6500     }
6501   else if (strncasecmp (input_line_pointer, "code", 4) == 0)
6502     {
6503       input_line_pointer += 4;
6504       /* IMPORTing/EXPORTing CODE types for functions is meaningless for SOM,
6505          instead one should be IMPORTing/EXPORTing ENTRY types.
6506
6507          Complain if one tries to EXPORT a CODE type since that's never
6508          done.  Both GCC and HP C still try to IMPORT CODE types, so
6509          silently fix them to be ENTRY types.  */
6510       if (S_IS_FUNCTION (symbolP))
6511         {
6512           if (is_export)
6513             as_tsktsk (_("Using ENTRY rather than CODE in export directive for %s"),
6514                        S_GET_NAME (symbolP));
6515
6516           bfdsym->flags |= BSF_FUNCTION;
6517           type = SYMBOL_TYPE_ENTRY;
6518         }
6519       else
6520         {
6521           bfdsym->flags &= ~BSF_FUNCTION;
6522           type = SYMBOL_TYPE_CODE;
6523         }
6524     }
6525   else if (strncasecmp (input_line_pointer, "data", 4) == 0)
6526     {
6527       input_line_pointer += 4;
6528       bfdsym->flags &= ~BSF_FUNCTION;
6529       bfdsym->flags |= BSF_OBJECT;
6530       type = SYMBOL_TYPE_DATA;
6531     }
6532   else if ((strncasecmp (input_line_pointer, "entry", 5) == 0))
6533     {
6534       input_line_pointer += 5;
6535       bfdsym->flags |= BSF_FUNCTION;
6536       type = SYMBOL_TYPE_ENTRY;
6537     }
6538   else if (strncasecmp (input_line_pointer, "millicode", 9) == 0)
6539     {
6540       input_line_pointer += 9;
6541       bfdsym->flags |= BSF_FUNCTION;
6542 #ifdef OBJ_ELF
6543       {
6544         elf_symbol_type *elfsym = (elf_symbol_type *) bfdsym;
6545         elfsym->internal_elf_sym.st_info =
6546           ELF_ST_INFO (ELF_ST_BIND (elfsym->internal_elf_sym.st_info),
6547                        STT_PARISC_MILLI);
6548       }
6549 #endif
6550       type = SYMBOL_TYPE_MILLICODE;
6551     }
6552   else if (strncasecmp (input_line_pointer, "plabel", 6) == 0)
6553     {
6554       input_line_pointer += 6;
6555       bfdsym->flags &= ~BSF_FUNCTION;
6556       type = SYMBOL_TYPE_PLABEL;
6557     }
6558   else if (strncasecmp (input_line_pointer, "pri_prog", 8) == 0)
6559     {
6560       input_line_pointer += 8;
6561       bfdsym->flags |= BSF_FUNCTION;
6562       type = SYMBOL_TYPE_PRI_PROG;
6563     }
6564   else if (strncasecmp (input_line_pointer, "sec_prog", 8) == 0)
6565     {
6566       input_line_pointer += 8;
6567       bfdsym->flags |= BSF_FUNCTION;
6568       type = SYMBOL_TYPE_SEC_PROG;
6569     }
6570
6571   /* SOM requires much more information about symbol types
6572      than BFD understands.  This is how we get this information
6573      to the SOM BFD backend.  */
6574 #ifdef obj_set_symbol_type
6575   obj_set_symbol_type (bfdsym, (int) type);
6576 #endif
6577
6578   /* Now that the type of the exported symbol has been handled,
6579      handle any argument relocation information.  */
6580   while (!is_end_of_statement ())
6581     {
6582       if (*input_line_pointer == ',')
6583         input_line_pointer++;
6584       name = input_line_pointer;
6585       c = get_symbol_end ();
6586       /* Argument sources.  */
6587       if ((strncasecmp (name, "argw", 4) == 0))
6588         {
6589           p = input_line_pointer;
6590           *p = c;
6591           input_line_pointer++;
6592           temp = atoi (name + 4);
6593           name = input_line_pointer;
6594           c = get_symbol_end ();
6595           arg_reloc = pa_align_arg_reloc (temp, pa_build_arg_reloc (name));
6596 #if defined (OBJ_SOM) || defined (ELF_ARG_RELOC)
6597           symbol_arg_reloc_info (symbolP) |= arg_reloc;
6598 #endif
6599           *input_line_pointer = c;
6600         }
6601       /* The return value.  */
6602       else if ((strncasecmp (name, "rtnval", 6)) == 0)
6603         {
6604           p = input_line_pointer;
6605           *p = c;
6606           input_line_pointer++;
6607           name = input_line_pointer;
6608           c = get_symbol_end ();
6609           arg_reloc = pa_build_arg_reloc (name);
6610 #if defined (OBJ_SOM) || defined (ELF_ARG_RELOC)
6611           symbol_arg_reloc_info (symbolP) |= arg_reloc;
6612 #endif
6613           *input_line_pointer = c;
6614         }
6615       /* Privilege level.  */
6616       else if ((strncasecmp (name, "priv_lev", 8)) == 0)
6617         {
6618           p = input_line_pointer;
6619           *p = c;
6620           input_line_pointer++;
6621           temp = atoi (input_line_pointer);
6622 #ifdef OBJ_SOM
6623           ((obj_symbol_type *) bfdsym)->tc_data.ap.hppa_priv_level = temp;
6624 #endif
6625           c = get_symbol_end ();
6626           *input_line_pointer = c;
6627         }
6628       else
6629         {
6630           as_bad (_("Undefined .EXPORT/.IMPORT argument (ignored): %s"), name);
6631           p = input_line_pointer;
6632           *p = c;
6633         }
6634       if (!is_end_of_statement ())
6635         input_line_pointer++;
6636     }
6637 }
6638
6639 /* Process a .EXPORT directive.  This makes functions external
6640    and provides information such as argument relocation entries
6641    to callers.  */
6642
6643 static void
6644 pa_export (int unused ATTRIBUTE_UNUSED)
6645 {
6646   char *name, c, *p;
6647   symbolS *symbol;
6648
6649   name = input_line_pointer;
6650   c = get_symbol_end ();
6651   /* Make sure the given symbol exists.  */
6652   if ((symbol = symbol_find_or_make (name)) == NULL)
6653     {
6654       as_bad (_("Cannot define export symbol: %s\n"), name);
6655       p = input_line_pointer;
6656       *p = c;
6657       input_line_pointer++;
6658     }
6659   else
6660     {
6661       /* OK.  Set the external bits and process argument relocations.
6662          For the HP, weak and global are not mutually exclusive.
6663          S_SET_EXTERNAL will not set BSF_GLOBAL if WEAK is set.
6664          Call S_SET_EXTERNAL to get the other processing.  Manually
6665          set BSF_GLOBAL when we get back.  */
6666       S_SET_EXTERNAL (symbol);
6667       symbol_get_bfdsym (symbol)->flags |= BSF_GLOBAL;
6668       p = input_line_pointer;
6669       *p = c;
6670       if (!is_end_of_statement ())
6671         {
6672           input_line_pointer++;
6673           pa_type_args (symbol, 1);
6674         }
6675     }
6676
6677   demand_empty_rest_of_line ();
6678 }
6679
6680 /* Handle an .IMPORT pseudo-op.  Any symbol referenced in a given
6681    assembly file must either be defined in the assembly file, or
6682    explicitly IMPORTED from another.  */
6683
6684 static void
6685 pa_import (int unused ATTRIBUTE_UNUSED)
6686 {
6687   char *name, c, *p;
6688   symbolS *symbol;
6689
6690   name = input_line_pointer;
6691   c = get_symbol_end ();
6692
6693   symbol = symbol_find (name);
6694   /* Ugh.  We might be importing a symbol defined earlier in the file,
6695      in which case all the code below will really screw things up
6696      (set the wrong segment, symbol flags & type, etc).  */
6697   if (symbol == NULL || !S_IS_DEFINED (symbol))
6698     {
6699       symbol = symbol_find_or_make (name);
6700       p = input_line_pointer;
6701       *p = c;
6702
6703       if (!is_end_of_statement ())
6704         {
6705           input_line_pointer++;
6706           pa_type_args (symbol, 0);
6707         }
6708       else
6709         {
6710           /* Sigh.  To be compatible with the HP assembler and to help
6711              poorly written assembly code, we assign a type based on
6712              the current segment.  Note only BSF_FUNCTION really
6713              matters, we do not need to set the full SYMBOL_TYPE_* info.  */
6714           if (now_seg == text_section)
6715             symbol_get_bfdsym (symbol)->flags |= BSF_FUNCTION;
6716
6717           /* If the section is undefined, then the symbol is undefined
6718              Since this is an import, leave the section undefined.  */
6719           S_SET_SEGMENT (symbol, bfd_und_section_ptr);
6720         }
6721     }
6722   else
6723     {
6724       /* The symbol was already defined.  Just eat everything up to
6725          the end of the current statement.  */
6726       while (!is_end_of_statement ())
6727         input_line_pointer++;
6728     }
6729
6730   demand_empty_rest_of_line ();
6731 }
6732
6733 /* Handle a .LABEL pseudo-op.  */
6734
6735 static void
6736 pa_label (int unused ATTRIBUTE_UNUSED)
6737 {
6738   char *name, c, *p;
6739
6740   name = input_line_pointer;
6741   c = get_symbol_end ();
6742
6743   if (strlen (name) > 0)
6744     {
6745       colon (name);
6746       p = input_line_pointer;
6747       *p = c;
6748     }
6749   else
6750     {
6751       as_warn (_("Missing label name on .LABEL"));
6752     }
6753
6754   if (!is_end_of_statement ())
6755     {
6756       as_warn (_("extra .LABEL arguments ignored."));
6757       ignore_rest_of_line ();
6758     }
6759   demand_empty_rest_of_line ();
6760 }
6761
6762 /* Handle a .LEAVE pseudo-op.  This is not supported yet.  */
6763
6764 static void
6765 pa_leave (int unused ATTRIBUTE_UNUSED)
6766 {
6767 #ifdef OBJ_SOM
6768   /* We must have a valid space and subspace.  */
6769   pa_check_current_space_and_subspace ();
6770 #endif
6771
6772   as_bad (_("The .LEAVE pseudo-op is not supported"));
6773   demand_empty_rest_of_line ();
6774 }
6775
6776 /* Handle a .LEVEL pseudo-op.  */
6777
6778 static void
6779 pa_level (int unused ATTRIBUTE_UNUSED)
6780 {
6781   char *level;
6782
6783   level = input_line_pointer;
6784   if (strncmp (level, "1.0", 3) == 0)
6785     {
6786       input_line_pointer += 3;
6787       if (!bfd_set_arch_mach (stdoutput, bfd_arch_hppa, 10))
6788         as_warn (_("could not set architecture and machine"));
6789     }
6790   else if (strncmp (level, "1.1", 3) == 0)
6791     {
6792       input_line_pointer += 3;
6793       if (!bfd_set_arch_mach (stdoutput, bfd_arch_hppa, 11))
6794         as_warn (_("could not set architecture and machine"));
6795     }
6796   else if (strncmp (level, "2.0w", 4) == 0)
6797     {
6798       input_line_pointer += 4;
6799       if (!bfd_set_arch_mach (stdoutput, bfd_arch_hppa, 25))
6800         as_warn (_("could not set architecture and machine"));
6801     }
6802   else if (strncmp (level, "2.0", 3) == 0)
6803     {
6804       input_line_pointer += 3;
6805       if (!bfd_set_arch_mach (stdoutput, bfd_arch_hppa, 20))
6806         as_warn (_("could not set architecture and machine"));
6807     }
6808   else
6809     {
6810       as_bad (_("Unrecognized .LEVEL argument\n"));
6811       ignore_rest_of_line ();
6812     }
6813   demand_empty_rest_of_line ();
6814 }
6815
6816 /* Handle a .ORIGIN pseudo-op.  */
6817
6818 static void
6819 pa_origin (int unused ATTRIBUTE_UNUSED)
6820 {
6821 #ifdef OBJ_SOM
6822   /* We must have a valid space and subspace.  */
6823   pa_check_current_space_and_subspace ();
6824 #endif
6825
6826   s_org (0);
6827   pa_undefine_label ();
6828 }
6829
6830 /* Handle a .PARAM pseudo-op.  This is much like a .EXPORT, except it
6831    is for static functions.  FIXME.  Should share more code with .EXPORT.  */
6832
6833 static void
6834 pa_param (int unused ATTRIBUTE_UNUSED)
6835 {
6836   char *name, c, *p;
6837   symbolS *symbol;
6838
6839   name = input_line_pointer;
6840   c = get_symbol_end ();
6841
6842   if ((symbol = symbol_find_or_make (name)) == NULL)
6843     {
6844       as_bad (_("Cannot define static symbol: %s\n"), name);
6845       p = input_line_pointer;
6846       *p = c;
6847       input_line_pointer++;
6848     }
6849   else
6850     {
6851       S_CLEAR_EXTERNAL (symbol);
6852       p = input_line_pointer;
6853       *p = c;
6854       if (!is_end_of_statement ())
6855         {
6856           input_line_pointer++;
6857           pa_type_args (symbol, 0);
6858         }
6859     }
6860
6861   demand_empty_rest_of_line ();
6862 }
6863
6864 /* Handle a .PROC pseudo-op.  It is used to mark the beginning
6865    of a procedure from a syntactical point of view.  */
6866
6867 static void
6868 pa_proc (int unused ATTRIBUTE_UNUSED)
6869 {
6870   struct call_info *call_info;
6871
6872 #ifdef OBJ_SOM
6873   /* We must have a valid space and subspace.  */
6874   pa_check_current_space_and_subspace ();
6875 #endif
6876
6877   if (within_procedure)
6878     as_fatal (_("Nested procedures"));
6879
6880   /* Reset global variables for new procedure.  */
6881   callinfo_found = FALSE;
6882   within_procedure = TRUE;
6883
6884   /* Create another call_info structure.  */
6885   call_info = xmalloc (sizeof (struct call_info));
6886
6887   if (!call_info)
6888     as_fatal (_("Cannot allocate unwind descriptor\n"));
6889
6890   memset (call_info, 0, sizeof (struct call_info));
6891
6892   call_info->ci_next = NULL;
6893
6894   if (call_info_root == NULL)
6895     {
6896       call_info_root = call_info;
6897       last_call_info = call_info;
6898     }
6899   else
6900     {
6901       last_call_info->ci_next = call_info;
6902       last_call_info = call_info;
6903     }
6904
6905   /* set up defaults on call_info structure */
6906
6907   call_info->ci_unwind.descriptor.cannot_unwind = 0;
6908   call_info->ci_unwind.descriptor.region_desc = 1;
6909   call_info->ci_unwind.descriptor.hpux_interrupt_marker = 0;
6910
6911   /* If we got a .PROC pseudo-op, we know that the function is defined
6912      locally.  Make sure it gets into the symbol table.  */
6913   {
6914     label_symbol_struct *label_symbol = pa_get_label ();
6915
6916     if (label_symbol)
6917       {
6918         if (label_symbol->lss_label)
6919           {
6920             last_call_info->start_symbol = label_symbol->lss_label;
6921             symbol_get_bfdsym (label_symbol->lss_label)->flags |= BSF_FUNCTION;
6922           }
6923         else
6924           as_bad (_("Missing function name for .PROC (corrupted label chain)"));
6925       }
6926     else
6927       last_call_info->start_symbol = NULL;
6928   }
6929
6930   demand_empty_rest_of_line ();
6931 }
6932
6933 /* Process the syntactical end of a procedure.  Make sure all the
6934    appropriate pseudo-ops were found within the procedure.  */
6935
6936 static void
6937 pa_procend (int unused ATTRIBUTE_UNUSED)
6938 {
6939 #ifdef OBJ_SOM
6940   /* We must have a valid space and subspace.  */
6941   pa_check_current_space_and_subspace ();
6942 #endif
6943
6944   /* If we are within a procedure definition, make sure we've
6945      defined a label for the procedure; handle case where the
6946      label was defined after the .PROC directive.
6947
6948      Note there's not need to diddle with the segment or fragment
6949      for the label symbol in this case.  We have already switched
6950      into the new $CODE$ subspace at this point.  */
6951   if (within_procedure && last_call_info->start_symbol == NULL)
6952     {
6953       label_symbol_struct *label_symbol = pa_get_label ();
6954
6955       if (label_symbol)
6956         {
6957           if (label_symbol->lss_label)
6958             {
6959               last_call_info->start_symbol = label_symbol->lss_label;
6960               symbol_get_bfdsym (label_symbol->lss_label)->flags
6961                 |= BSF_FUNCTION;
6962 #ifdef OBJ_SOM
6963               /* Also handle allocation of a fixup to hold the unwind
6964                  information when the label appears after the proc/procend.  */
6965               if (within_entry_exit)
6966                 {
6967                   char *where;
6968                   unsigned int u;
6969
6970                   where = frag_more (0);
6971                   u = UNWIND_LOW32 (&last_call_info->ci_unwind.descriptor);
6972                   fix_new_hppa (frag_now, where - frag_now->fr_literal, 0,
6973                                 NULL, (offsetT) 0, NULL,
6974                                 0, R_HPPA_ENTRY, e_fsel, 0, 0, u);
6975                 }
6976 #endif
6977             }
6978           else
6979             as_bad (_("Missing function name for .PROC (corrupted label chain)"));
6980         }
6981       else
6982         as_bad (_("Missing function name for .PROC"));
6983     }
6984
6985   if (!within_procedure)
6986     as_bad (_("misplaced .procend"));
6987
6988   if (!callinfo_found)
6989     as_bad (_("Missing .callinfo for this procedure"));
6990
6991   if (within_entry_exit)
6992     as_bad (_("Missing .EXIT for a .ENTRY"));
6993
6994 #ifdef OBJ_ELF
6995   /* ELF needs to mark the end of each function so that it can compute
6996      the size of the function (apparently its needed in the symbol table).  */
6997   hppa_elf_mark_end_of_function ();
6998 #endif
6999
7000   within_procedure = FALSE;
7001   demand_empty_rest_of_line ();
7002   pa_undefine_label ();
7003 }
7004
7005 #ifdef OBJ_SOM
7006 /* If VALUE is an exact power of two between zero and 2^31, then
7007    return log2 (VALUE).  Else return -1.  */
7008
7009 static int
7010 exact_log2 (int value)
7011 {
7012   int shift = 0;
7013
7014   while ((1 << shift) != value && shift < 32)
7015     shift++;
7016
7017   if (shift >= 32)
7018     return -1;
7019   else
7020     return shift;
7021 }
7022
7023 /* Check to make sure we have a valid space and subspace.  */
7024
7025 static void
7026 pa_check_current_space_and_subspace (void)
7027 {
7028   if (current_space == NULL)
7029     as_fatal (_("Not in a space.\n"));
7030
7031   if (current_subspace == NULL)
7032     as_fatal (_("Not in a subspace.\n"));
7033 }
7034
7035 /* Parse the parameters to a .SPACE directive; if CREATE_FLAG is nonzero,
7036    then create a new space entry to hold the information specified
7037    by the parameters to the .SPACE directive.  */
7038
7039 static sd_chain_struct *
7040 pa_parse_space_stmt (char *space_name, int create_flag)
7041 {
7042   char *name, *ptemp, c;
7043   char loadable, defined, private, sort;
7044   int spnum;
7045   asection *seg = NULL;
7046   sd_chain_struct *space;
7047
7048   /* Load default values.  */
7049   spnum = 0;
7050   sort = 0;
7051   loadable = TRUE;
7052   defined = TRUE;
7053   private = FALSE;
7054   if (strcmp (space_name, "$TEXT$") == 0)
7055     {
7056       seg = pa_def_spaces[0].segment;
7057       defined = pa_def_spaces[0].defined;
7058       private = pa_def_spaces[0].private;
7059       sort = pa_def_spaces[0].sort;
7060       spnum = pa_def_spaces[0].spnum;
7061     }
7062   else if (strcmp (space_name, "$PRIVATE$") == 0)
7063     {
7064       seg = pa_def_spaces[1].segment;
7065       defined = pa_def_spaces[1].defined;
7066       private = pa_def_spaces[1].private;
7067       sort = pa_def_spaces[1].sort;
7068       spnum = pa_def_spaces[1].spnum;
7069     }
7070
7071   if (!is_end_of_statement ())
7072     {
7073       print_errors = FALSE;
7074       ptemp = input_line_pointer + 1;
7075       /* First see if the space was specified as a number rather than
7076          as a name.  According to the PA assembly manual the rest of
7077          the line should be ignored.  */
7078       strict = 0;
7079       pa_parse_number (&ptemp, 0);
7080       if (pa_number >= 0)
7081         {
7082           spnum = pa_number;
7083           input_line_pointer = ptemp;
7084         }
7085       else
7086         {
7087           while (!is_end_of_statement ())
7088             {
7089               input_line_pointer++;
7090               name = input_line_pointer;
7091               c = get_symbol_end ();
7092               if ((strncasecmp (name, "spnum", 5) == 0))
7093                 {
7094                   *input_line_pointer = c;
7095                   input_line_pointer++;
7096                   spnum = get_absolute_expression ();
7097                 }
7098               else if ((strncasecmp (name, "sort", 4) == 0))
7099                 {
7100                   *input_line_pointer = c;
7101                   input_line_pointer++;
7102                   sort = get_absolute_expression ();
7103                 }
7104               else if ((strncasecmp (name, "unloadable", 10) == 0))
7105                 {
7106                   *input_line_pointer = c;
7107                   loadable = FALSE;
7108                 }
7109               else if ((strncasecmp (name, "notdefined", 10) == 0))
7110                 {
7111                   *input_line_pointer = c;
7112                   defined = FALSE;
7113                 }
7114               else if ((strncasecmp (name, "private", 7) == 0))
7115                 {
7116                   *input_line_pointer = c;
7117                   private = TRUE;
7118                 }
7119               else
7120                 {
7121                   as_bad (_("Invalid .SPACE argument"));
7122                   *input_line_pointer = c;
7123                   if (!is_end_of_statement ())
7124                     input_line_pointer++;
7125                 }
7126             }
7127         }
7128       print_errors = TRUE;
7129     }
7130
7131   if (create_flag && seg == NULL)
7132     seg = subseg_new (space_name, 0);
7133
7134   /* If create_flag is nonzero, then create the new space with
7135      the attributes computed above.  Else set the values in
7136      an already existing space -- this can only happen for
7137      the first occurrence of a built-in space.  */
7138   if (create_flag)
7139     space = create_new_space (space_name, spnum, loadable, defined,
7140                               private, sort, seg, 1);
7141   else
7142     {
7143       space = is_defined_space (space_name);
7144       SPACE_SPNUM (space) = spnum;
7145       SPACE_DEFINED (space) = defined & 1;
7146       SPACE_USER_DEFINED (space) = 1;
7147     }
7148
7149 #ifdef obj_set_section_attributes
7150   obj_set_section_attributes (seg, defined, private, sort, spnum);
7151 #endif
7152
7153   return space;
7154 }
7155
7156 /* Handle a .SPACE pseudo-op; this switches the current space to the
7157    given space, creating the new space if necessary.  */
7158
7159 static void
7160 pa_space (int unused ATTRIBUTE_UNUSED)
7161 {
7162   char *name, c, *space_name, *save_s;
7163   sd_chain_struct *sd_chain;
7164
7165   if (within_procedure)
7166     {
7167       as_bad (_("Can\'t change spaces within a procedure definition. Ignored"));
7168       ignore_rest_of_line ();
7169     }
7170   else
7171     {
7172       /* Check for some of the predefined spaces.   FIXME: most of the code
7173          below is repeated several times, can we extract the common parts
7174          and place them into a subroutine or something similar?  */
7175       /* FIXME Is this (and the next IF stmt) really right?
7176          What if INPUT_LINE_POINTER points to "$TEXT$FOO"?  */
7177       if (strncmp (input_line_pointer, "$TEXT$", 6) == 0)
7178         {
7179           input_line_pointer += 6;
7180           sd_chain = is_defined_space ("$TEXT$");
7181           if (sd_chain == NULL)
7182             sd_chain = pa_parse_space_stmt ("$TEXT$", 1);
7183           else if (SPACE_USER_DEFINED (sd_chain) == 0)
7184             sd_chain = pa_parse_space_stmt ("$TEXT$", 0);
7185
7186           current_space = sd_chain;
7187           subseg_set (text_section, sd_chain->sd_last_subseg);
7188           current_subspace
7189             = pa_subsegment_to_subspace (text_section,
7190                                          sd_chain->sd_last_subseg);
7191           demand_empty_rest_of_line ();
7192           return;
7193         }
7194       if (strncmp (input_line_pointer, "$PRIVATE$", 9) == 0)
7195         {
7196           input_line_pointer += 9;
7197           sd_chain = is_defined_space ("$PRIVATE$");
7198           if (sd_chain == NULL)
7199             sd_chain = pa_parse_space_stmt ("$PRIVATE$", 1);
7200           else if (SPACE_USER_DEFINED (sd_chain) == 0)
7201             sd_chain = pa_parse_space_stmt ("$PRIVATE$", 0);
7202
7203           current_space = sd_chain;
7204           subseg_set (data_section, sd_chain->sd_last_subseg);
7205           current_subspace
7206             = pa_subsegment_to_subspace (data_section,
7207                                          sd_chain->sd_last_subseg);
7208           demand_empty_rest_of_line ();
7209           return;
7210         }
7211       if (!strncasecmp (input_line_pointer,
7212                         GDB_DEBUG_SPACE_NAME,
7213                         strlen (GDB_DEBUG_SPACE_NAME)))
7214         {
7215           input_line_pointer += strlen (GDB_DEBUG_SPACE_NAME);
7216           sd_chain = is_defined_space (GDB_DEBUG_SPACE_NAME);
7217           if (sd_chain == NULL)
7218             sd_chain = pa_parse_space_stmt (GDB_DEBUG_SPACE_NAME, 1);
7219           else if (SPACE_USER_DEFINED (sd_chain) == 0)
7220             sd_chain = pa_parse_space_stmt (GDB_DEBUG_SPACE_NAME, 0);
7221
7222           current_space = sd_chain;
7223
7224           {
7225             asection *gdb_section
7226             = bfd_make_section_old_way (stdoutput, GDB_DEBUG_SPACE_NAME);
7227
7228             subseg_set (gdb_section, sd_chain->sd_last_subseg);
7229             current_subspace
7230               = pa_subsegment_to_subspace (gdb_section,
7231                                            sd_chain->sd_last_subseg);
7232           }
7233           demand_empty_rest_of_line ();
7234           return;
7235         }
7236
7237       /* It could be a space specified by number.  */
7238       print_errors = 0;
7239       save_s = input_line_pointer;
7240       strict = 0;
7241       pa_parse_number (&input_line_pointer, 0);
7242       if (pa_number >= 0)
7243         {
7244           if ((sd_chain = pa_find_space_by_number (pa_number)))
7245             {
7246               current_space = sd_chain;
7247
7248               subseg_set (sd_chain->sd_seg, sd_chain->sd_last_subseg);
7249               current_subspace
7250                 = pa_subsegment_to_subspace (sd_chain->sd_seg,
7251                                              sd_chain->sd_last_subseg);
7252               demand_empty_rest_of_line ();
7253               return;
7254             }
7255         }
7256
7257       /* Not a number, attempt to create a new space.  */
7258       print_errors = 1;
7259       input_line_pointer = save_s;
7260       name = input_line_pointer;
7261       c = get_symbol_end ();
7262       space_name = xmalloc (strlen (name) + 1);
7263       strcpy (space_name, name);
7264       *input_line_pointer = c;
7265
7266       sd_chain = pa_parse_space_stmt (space_name, 1);
7267       current_space = sd_chain;
7268
7269       subseg_set (sd_chain->sd_seg, sd_chain->sd_last_subseg);
7270       current_subspace = pa_subsegment_to_subspace (sd_chain->sd_seg,
7271                                                   sd_chain->sd_last_subseg);
7272       demand_empty_rest_of_line ();
7273     }
7274 }
7275
7276 /* Switch to a new space.  (I think).  FIXME.  */
7277
7278 static void
7279 pa_spnum (int unused ATTRIBUTE_UNUSED)
7280 {
7281   char *name;
7282   char c;
7283   char *p;
7284   sd_chain_struct *space;
7285
7286   name = input_line_pointer;
7287   c = get_symbol_end ();
7288   space = is_defined_space (name);
7289   if (space)
7290     {
7291       p = frag_more (4);
7292       md_number_to_chars (p, SPACE_SPNUM (space), 4);
7293     }
7294   else
7295     as_warn (_("Undefined space: '%s' Assuming space number = 0."), name);
7296
7297   *input_line_pointer = c;
7298   demand_empty_rest_of_line ();
7299 }
7300
7301 /* Handle a .SUBSPACE pseudo-op; this switches the current subspace to the
7302    given subspace, creating the new subspace if necessary.
7303
7304    FIXME.  Should mirror pa_space more closely, in particular how
7305    they're broken up into subroutines.  */
7306
7307 static void
7308 pa_subspace (int create_new)
7309 {
7310   char *name, *ss_name, c;
7311   char loadable, code_only, comdat, common, dup_common, zero, sort;
7312   int i, access, space_index, alignment, quadrant, applicable, flags;
7313   sd_chain_struct *space;
7314   ssd_chain_struct *ssd;
7315   asection *section;
7316
7317   if (current_space == NULL)
7318     as_fatal (_("Must be in a space before changing or declaring subspaces.\n"));
7319
7320   if (within_procedure)
7321     {
7322       as_bad (_("Can\'t change subspaces within a procedure definition. Ignored"));
7323       ignore_rest_of_line ();
7324     }
7325   else
7326     {
7327       name = input_line_pointer;
7328       c = get_symbol_end ();
7329       ss_name = xmalloc (strlen (name) + 1);
7330       strcpy (ss_name, name);
7331       *input_line_pointer = c;
7332
7333       /* Load default values.  */
7334       sort = 0;
7335       access = 0x7f;
7336       loadable = 1;
7337       comdat = 0;
7338       common = 0;
7339       dup_common = 0;
7340       code_only = 0;
7341       zero = 0;
7342       space_index = ~0;
7343       alignment = 1;
7344       quadrant = 0;
7345
7346       space = current_space;
7347       if (create_new)
7348         ssd = NULL;
7349       else
7350         ssd = is_defined_subspace (ss_name);
7351       /* Allow user to override the builtin attributes of subspaces.  But
7352          only allow the attributes to be changed once!  */
7353       if (ssd && SUBSPACE_DEFINED (ssd))
7354         {
7355           subseg_set (ssd->ssd_seg, ssd->ssd_subseg);
7356           current_subspace = ssd;
7357           if (!is_end_of_statement ())
7358             as_warn (_("Parameters of an existing subspace can\'t be modified"));
7359           demand_empty_rest_of_line ();
7360           return;
7361         }
7362       else
7363         {
7364           /* A new subspace.  Load default values if it matches one of
7365              the builtin subspaces.  */
7366           i = 0;
7367           while (pa_def_subspaces[i].name)
7368             {
7369               if (strcasecmp (pa_def_subspaces[i].name, ss_name) == 0)
7370                 {
7371                   loadable = pa_def_subspaces[i].loadable;
7372                   comdat = pa_def_subspaces[i].comdat;
7373                   common = pa_def_subspaces[i].common;
7374                   dup_common = pa_def_subspaces[i].dup_common;
7375                   code_only = pa_def_subspaces[i].code_only;
7376                   zero = pa_def_subspaces[i].zero;
7377                   space_index = pa_def_subspaces[i].space_index;
7378                   alignment = pa_def_subspaces[i].alignment;
7379                   quadrant = pa_def_subspaces[i].quadrant;
7380                   access = pa_def_subspaces[i].access;
7381                   sort = pa_def_subspaces[i].sort;
7382                   break;
7383                 }
7384               i++;
7385             }
7386         }
7387
7388       /* We should be working with a new subspace now.  Fill in
7389          any information as specified by the user.  */
7390       if (!is_end_of_statement ())
7391         {
7392           input_line_pointer++;
7393           while (!is_end_of_statement ())
7394             {
7395               name = input_line_pointer;
7396               c = get_symbol_end ();
7397               if ((strncasecmp (name, "quad", 4) == 0))
7398                 {
7399                   *input_line_pointer = c;
7400                   input_line_pointer++;
7401                   quadrant = get_absolute_expression ();
7402                 }
7403               else if ((strncasecmp (name, "align", 5) == 0))
7404                 {
7405                   *input_line_pointer = c;
7406                   input_line_pointer++;
7407                   alignment = get_absolute_expression ();
7408                   if (exact_log2 (alignment) == -1)
7409                     {
7410                       as_bad (_("Alignment must be a power of 2"));
7411                       alignment = 1;
7412                     }
7413                 }
7414               else if ((strncasecmp (name, "access", 6) == 0))
7415                 {
7416                   *input_line_pointer = c;
7417                   input_line_pointer++;
7418                   access = get_absolute_expression ();
7419                 }
7420               else if ((strncasecmp (name, "sort", 4) == 0))
7421                 {
7422                   *input_line_pointer = c;
7423                   input_line_pointer++;
7424                   sort = get_absolute_expression ();
7425                 }
7426               else if ((strncasecmp (name, "code_only", 9) == 0))
7427                 {
7428                   *input_line_pointer = c;
7429                   code_only = 1;
7430                 }
7431               else if ((strncasecmp (name, "unloadable", 10) == 0))
7432                 {
7433                   *input_line_pointer = c;
7434                   loadable = 0;
7435                 }
7436               else if ((strncasecmp (name, "comdat", 6) == 0))
7437                 {
7438                   *input_line_pointer = c;
7439                   comdat = 1;
7440                 }
7441               else if ((strncasecmp (name, "common", 6) == 0))
7442                 {
7443                   *input_line_pointer = c;
7444                   common = 1;
7445                 }
7446               else if ((strncasecmp (name, "dup_comm", 8) == 0))
7447                 {
7448                   *input_line_pointer = c;
7449                   dup_common = 1;
7450                 }
7451               else if ((strncasecmp (name, "zero", 4) == 0))
7452                 {
7453                   *input_line_pointer = c;
7454                   zero = 1;
7455                 }
7456               else if ((strncasecmp (name, "first", 5) == 0))
7457                 as_bad (_("FIRST not supported as a .SUBSPACE argument"));
7458               else
7459                 as_bad (_("Invalid .SUBSPACE argument"));
7460               if (!is_end_of_statement ())
7461                 input_line_pointer++;
7462             }
7463         }
7464
7465       /* Compute a reasonable set of BFD flags based on the information
7466          in the .subspace directive.  */
7467       applicable = bfd_applicable_section_flags (stdoutput);
7468       flags = 0;
7469       if (loadable)
7470         flags |= (SEC_ALLOC | SEC_LOAD);
7471       if (code_only)
7472         flags |= SEC_CODE;
7473
7474       /* These flags are used to implement various flavors of initialized
7475          common.  The SOM linker discards duplicate subspaces when they
7476          have the same "key" symbol name.  This support is more like
7477          GNU linkonce than BFD common.  Further, pc-relative relocations
7478          are converted to section relative relocations in BFD common
7479          sections.  This complicates the handling of relocations in
7480          common sections containing text and isn't currently supported
7481          correctly in the SOM BFD backend.  */
7482       if (comdat || common || dup_common)
7483         flags |= SEC_LINK_ONCE;
7484
7485       flags |= SEC_RELOC | SEC_HAS_CONTENTS;
7486
7487       /* This is a zero-filled subspace (eg BSS).  */
7488       if (zero)
7489         flags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
7490
7491       applicable &= flags;
7492
7493       /* If this is an existing subspace, then we want to use the
7494          segment already associated with the subspace.
7495
7496          FIXME NOW!  ELF BFD doesn't appear to be ready to deal with
7497          lots of sections.  It might be a problem in the PA ELF
7498          code, I do not know yet.  For now avoid creating anything
7499          but the "standard" sections for ELF.  */
7500       if (create_new)
7501         section = subseg_force_new (ss_name, 0);
7502       else if (ssd)
7503         section = ssd->ssd_seg;
7504       else
7505         section = subseg_new (ss_name, 0);
7506
7507       if (zero)
7508         seg_info (section)->bss = 1;
7509
7510       /* Now set the flags.  */
7511       bfd_set_section_flags (stdoutput, section, applicable);
7512
7513       /* Record any alignment request for this section.  */
7514       record_alignment (section, exact_log2 (alignment));
7515
7516       /* Set the starting offset for this section.  */
7517       bfd_set_section_vma (stdoutput, section,
7518                            pa_subspace_start (space, quadrant));
7519
7520       /* Now that all the flags are set, update an existing subspace,
7521          or create a new one.  */
7522       if (ssd)
7523
7524         current_subspace = update_subspace (space, ss_name, loadable,
7525                                             code_only, comdat, common,
7526                                             dup_common, sort, zero, access,
7527                                             space_index, alignment, quadrant,
7528                                             section);
7529       else
7530         current_subspace = create_new_subspace (space, ss_name, loadable,
7531                                                 code_only, comdat, common,
7532                                                 dup_common, zero, sort,
7533                                                 access, space_index,
7534                                                 alignment, quadrant, section);
7535
7536       demand_empty_rest_of_line ();
7537       current_subspace->ssd_seg = section;
7538       subseg_set (current_subspace->ssd_seg, current_subspace->ssd_subseg);
7539     }
7540   SUBSPACE_DEFINED (current_subspace) = 1;
7541 }
7542
7543 /* Create default space and subspace dictionaries.  */
7544
7545 static void
7546 pa_spaces_begin (void)
7547 {
7548   int i;
7549
7550   space_dict_root = NULL;
7551   space_dict_last = NULL;
7552
7553   i = 0;
7554   while (pa_def_spaces[i].name)
7555     {
7556       char *name;
7557
7558       /* Pick the right name to use for the new section.  */
7559       name = pa_def_spaces[i].name;
7560
7561       pa_def_spaces[i].segment = subseg_new (name, 0);
7562       create_new_space (pa_def_spaces[i].name, pa_def_spaces[i].spnum,
7563                         pa_def_spaces[i].loadable, pa_def_spaces[i].defined,
7564                         pa_def_spaces[i].private, pa_def_spaces[i].sort,
7565                         pa_def_spaces[i].segment, 0);
7566       i++;
7567     }
7568
7569   i = 0;
7570   while (pa_def_subspaces[i].name)
7571     {
7572       char *name;
7573       int applicable, subsegment;
7574       asection *segment = NULL;
7575       sd_chain_struct *space;
7576
7577       /* Pick the right name for the new section and pick the right
7578          subsegment number.  */
7579       name = pa_def_subspaces[i].name;
7580       subsegment = 0;
7581
7582       /* Create the new section.  */
7583       segment = subseg_new (name, subsegment);
7584
7585       /* For SOM we want to replace the standard .text, .data, and .bss
7586          sections with our own.   We also want to set BFD flags for
7587          all the built-in subspaces.  */
7588       if (!strcmp (pa_def_subspaces[i].name, "$CODE$"))
7589         {
7590           text_section = segment;
7591           applicable = bfd_applicable_section_flags (stdoutput);
7592           bfd_set_section_flags (stdoutput, segment,
7593                                  applicable & (SEC_ALLOC | SEC_LOAD
7594                                                | SEC_RELOC | SEC_CODE
7595                                                | SEC_READONLY
7596                                                | SEC_HAS_CONTENTS));
7597         }
7598       else if (!strcmp (pa_def_subspaces[i].name, "$DATA$"))
7599         {
7600           data_section = segment;
7601           applicable = bfd_applicable_section_flags (stdoutput);
7602           bfd_set_section_flags (stdoutput, segment,
7603                                  applicable & (SEC_ALLOC | SEC_LOAD
7604                                                | SEC_RELOC
7605                                                | SEC_HAS_CONTENTS));
7606
7607         }
7608       else if (!strcmp (pa_def_subspaces[i].name, "$BSS$"))
7609         {
7610           bss_section = segment;
7611           applicable = bfd_applicable_section_flags (stdoutput);
7612           bfd_set_section_flags (stdoutput, segment,
7613                                  applicable & SEC_ALLOC);
7614         }
7615       else if (!strcmp (pa_def_subspaces[i].name, "$LIT$"))
7616         {
7617           applicable = bfd_applicable_section_flags (stdoutput);
7618           bfd_set_section_flags (stdoutput, segment,
7619                                  applicable & (SEC_ALLOC | SEC_LOAD
7620                                                | SEC_RELOC
7621                                                | SEC_READONLY
7622                                                | SEC_HAS_CONTENTS));
7623         }
7624       else if (!strcmp (pa_def_subspaces[i].name, "$MILLICODE$"))
7625         {
7626           applicable = bfd_applicable_section_flags (stdoutput);
7627           bfd_set_section_flags (stdoutput, segment,
7628                                  applicable & (SEC_ALLOC | SEC_LOAD
7629                                                | SEC_RELOC
7630                                                | SEC_READONLY
7631                                                | SEC_HAS_CONTENTS));
7632         }
7633       else if (!strcmp (pa_def_subspaces[i].name, "$UNWIND$"))
7634         {
7635           applicable = bfd_applicable_section_flags (stdoutput);
7636           bfd_set_section_flags (stdoutput, segment,
7637                                  applicable & (SEC_ALLOC | SEC_LOAD
7638                                                | SEC_RELOC
7639                                                | SEC_READONLY
7640                                                | SEC_HAS_CONTENTS));
7641         }
7642
7643       /* Find the space associated with this subspace.  */
7644       space = pa_segment_to_space (pa_def_spaces[pa_def_subspaces[i].
7645                                                  def_space_index].segment);
7646       if (space == NULL)
7647         {
7648           as_fatal (_("Internal error: Unable to find containing space for %s."),
7649                     pa_def_subspaces[i].name);
7650         }
7651
7652       create_new_subspace (space, name,
7653                            pa_def_subspaces[i].loadable,
7654                            pa_def_subspaces[i].code_only,
7655                            pa_def_subspaces[i].comdat,
7656                            pa_def_subspaces[i].common,
7657                            pa_def_subspaces[i].dup_common,
7658                            pa_def_subspaces[i].zero,
7659                            pa_def_subspaces[i].sort,
7660                            pa_def_subspaces[i].access,
7661                            pa_def_subspaces[i].space_index,
7662                            pa_def_subspaces[i].alignment,
7663                            pa_def_subspaces[i].quadrant,
7664                            segment);
7665       i++;
7666     }
7667 }
7668
7669 /* Create a new space NAME, with the appropriate flags as defined
7670    by the given parameters.  */
7671
7672 static sd_chain_struct *
7673 create_new_space (char *name,
7674                   int spnum,
7675                   int loadable ATTRIBUTE_UNUSED,
7676                   int defined,
7677                   int private,
7678                   int sort,
7679                   asection *seg,
7680                   int user_defined)
7681 {
7682   sd_chain_struct *chain_entry;
7683
7684   chain_entry = xmalloc (sizeof (sd_chain_struct));
7685   if (!chain_entry)
7686     as_fatal (_("Out of memory: could not allocate new space chain entry: %s\n"),
7687               name);
7688
7689   SPACE_NAME (chain_entry) = xmalloc (strlen (name) + 1);
7690   strcpy (SPACE_NAME (chain_entry), name);
7691   SPACE_DEFINED (chain_entry) = defined;
7692   SPACE_USER_DEFINED (chain_entry) = user_defined;
7693   SPACE_SPNUM (chain_entry) = spnum;
7694
7695   chain_entry->sd_seg = seg;
7696   chain_entry->sd_last_subseg = -1;
7697   chain_entry->sd_subspaces = NULL;
7698   chain_entry->sd_next = NULL;
7699
7700   /* Find spot for the new space based on its sort key.  */
7701   if (!space_dict_last)
7702     space_dict_last = chain_entry;
7703
7704   if (space_dict_root == NULL)
7705     space_dict_root = chain_entry;
7706   else
7707     {
7708       sd_chain_struct *chain_pointer;
7709       sd_chain_struct *prev_chain_pointer;
7710
7711       chain_pointer = space_dict_root;
7712       prev_chain_pointer = NULL;
7713
7714       while (chain_pointer)
7715         {
7716           prev_chain_pointer = chain_pointer;
7717           chain_pointer = chain_pointer->sd_next;
7718         }
7719
7720       /* At this point we've found the correct place to add the new
7721          entry.  So add it and update the linked lists as appropriate.  */
7722       if (prev_chain_pointer)
7723         {
7724           chain_entry->sd_next = chain_pointer;
7725           prev_chain_pointer->sd_next = chain_entry;
7726         }
7727       else
7728         {
7729           space_dict_root = chain_entry;
7730           chain_entry->sd_next = chain_pointer;
7731         }
7732
7733       if (chain_entry->sd_next == NULL)
7734         space_dict_last = chain_entry;
7735     }
7736
7737   /* This is here to catch predefined spaces which do not get
7738      modified by the user's input.  Another call is found at
7739      the bottom of pa_parse_space_stmt to handle cases where
7740      the user modifies a predefined space.  */
7741 #ifdef obj_set_section_attributes
7742   obj_set_section_attributes (seg, defined, private, sort, spnum);
7743 #endif
7744
7745   return chain_entry;
7746 }
7747
7748 /* Create a new subspace NAME, with the appropriate flags as defined
7749    by the given parameters.
7750
7751    Add the new subspace to the subspace dictionary chain in numerical
7752    order as defined by the SORT entries.  */
7753
7754 static ssd_chain_struct *
7755 create_new_subspace (sd_chain_struct *space,
7756                      char *name,
7757                      int loadable ATTRIBUTE_UNUSED,
7758                      int code_only ATTRIBUTE_UNUSED,
7759                      int comdat,
7760                      int common,
7761                      int dup_common,
7762                      int is_zero ATTRIBUTE_UNUSED,
7763                      int sort,
7764                      int access,
7765                      int space_index ATTRIBUTE_UNUSED,
7766                      int alignment ATTRIBUTE_UNUSED,
7767                      int quadrant,
7768                      asection *seg)
7769 {
7770   ssd_chain_struct *chain_entry;
7771
7772   chain_entry = xmalloc (sizeof (ssd_chain_struct));
7773   if (!chain_entry)
7774     as_fatal (_("Out of memory: could not allocate new subspace chain entry: %s\n"), name);
7775
7776   SUBSPACE_NAME (chain_entry) = xmalloc (strlen (name) + 1);
7777   strcpy (SUBSPACE_NAME (chain_entry), name);
7778
7779   /* Initialize subspace_defined.  When we hit a .subspace directive
7780      we'll set it to 1 which "locks-in" the subspace attributes.  */
7781   SUBSPACE_DEFINED (chain_entry) = 0;
7782
7783   chain_entry->ssd_subseg = 0;
7784   chain_entry->ssd_seg = seg;
7785   chain_entry->ssd_next = NULL;
7786
7787   /* Find spot for the new subspace based on its sort key.  */
7788   if (space->sd_subspaces == NULL)
7789     space->sd_subspaces = chain_entry;
7790   else
7791     {
7792       ssd_chain_struct *chain_pointer;
7793       ssd_chain_struct *prev_chain_pointer;
7794
7795       chain_pointer = space->sd_subspaces;
7796       prev_chain_pointer = NULL;
7797
7798       while (chain_pointer)
7799         {
7800           prev_chain_pointer = chain_pointer;
7801           chain_pointer = chain_pointer->ssd_next;
7802         }
7803
7804       /* Now we have somewhere to put the new entry.  Insert it and update
7805          the links.  */
7806       if (prev_chain_pointer)
7807         {
7808           chain_entry->ssd_next = chain_pointer;
7809           prev_chain_pointer->ssd_next = chain_entry;
7810         }
7811       else
7812         {
7813           space->sd_subspaces = chain_entry;
7814           chain_entry->ssd_next = chain_pointer;
7815         }
7816     }
7817
7818 #ifdef obj_set_subsection_attributes
7819   obj_set_subsection_attributes (seg, space->sd_seg, access, sort,
7820                                  quadrant, comdat, common, dup_common);
7821 #endif
7822
7823   return chain_entry;
7824 }
7825
7826 /* Update the information for the given subspace based upon the
7827    various arguments.   Return the modified subspace chain entry.  */
7828
7829 static ssd_chain_struct *
7830 update_subspace (sd_chain_struct *space,
7831                  char *name,
7832                  int loadable ATTRIBUTE_UNUSED,
7833                  int code_only ATTRIBUTE_UNUSED,
7834                  int comdat,
7835                  int common,
7836                  int dup_common,
7837                  int sort,
7838                  int zero ATTRIBUTE_UNUSED,
7839                  int access,
7840                  int space_index ATTRIBUTE_UNUSED,
7841                  int alignment ATTRIBUTE_UNUSED,
7842                  int quadrant,
7843                  asection *section)
7844 {
7845   ssd_chain_struct *chain_entry;
7846
7847   chain_entry = is_defined_subspace (name);
7848
7849 #ifdef obj_set_subsection_attributes
7850   obj_set_subsection_attributes (section, space->sd_seg, access, sort,
7851                                  quadrant, comdat, common, dup_common);
7852 #endif
7853
7854   return chain_entry;
7855 }
7856
7857 /* Return the space chain entry for the space with the name NAME or
7858    NULL if no such space exists.  */
7859
7860 static sd_chain_struct *
7861 is_defined_space (char *name)
7862 {
7863   sd_chain_struct *chain_pointer;
7864
7865   for (chain_pointer = space_dict_root;
7866        chain_pointer;
7867        chain_pointer = chain_pointer->sd_next)
7868     if (strcmp (SPACE_NAME (chain_pointer), name) == 0)
7869       return chain_pointer;
7870
7871   /* No mapping from segment to space was found.  Return NULL.  */
7872   return NULL;
7873 }
7874
7875 /* Find and return the space associated with the given seg.  If no mapping
7876    from the given seg to a space is found, then return NULL.
7877
7878    Unlike subspaces, the number of spaces is not expected to grow much,
7879    so a linear exhaustive search is OK here.  */
7880
7881 static sd_chain_struct *
7882 pa_segment_to_space (asection *seg)
7883 {
7884   sd_chain_struct *space_chain;
7885
7886   /* Walk through each space looking for the correct mapping.  */
7887   for (space_chain = space_dict_root;
7888        space_chain;
7889        space_chain = space_chain->sd_next)
7890     if (space_chain->sd_seg == seg)
7891       return space_chain;
7892
7893   /* Mapping was not found.  Return NULL.  */
7894   return NULL;
7895 }
7896
7897 /* Return the first space chain entry for the subspace with the name
7898    NAME or NULL if no such subspace exists.
7899
7900    When there are multiple subspaces with the same name, switching to
7901    the first (i.e., default) subspace is preferable in most situations.
7902    For example, it wouldn't be desirable to merge COMDAT data with non
7903    COMDAT data.
7904
7905    Uses a linear search through all the spaces and subspaces, this may
7906    not be appropriate if we ever being placing each function in its
7907    own subspace.  */
7908
7909 static ssd_chain_struct *
7910 is_defined_subspace (char *name)
7911 {
7912   sd_chain_struct *space_chain;
7913   ssd_chain_struct *subspace_chain;
7914
7915   /* Walk through each space.  */
7916   for (space_chain = space_dict_root;
7917        space_chain;
7918        space_chain = space_chain->sd_next)
7919     {
7920       /* Walk through each subspace looking for a name which matches.  */
7921       for (subspace_chain = space_chain->sd_subspaces;
7922            subspace_chain;
7923            subspace_chain = subspace_chain->ssd_next)
7924         if (strcmp (SUBSPACE_NAME (subspace_chain), name) == 0)
7925           return subspace_chain;
7926     }
7927
7928   /* Subspace wasn't found.  Return NULL.  */
7929   return NULL;
7930 }
7931
7932 /* Find and return the subspace associated with the given seg.  If no
7933    mapping from the given seg to a subspace is found, then return NULL.
7934
7935    If we ever put each procedure/function within its own subspace
7936    (to make life easier on the compiler and linker), then this will have
7937    to become more efficient.  */
7938
7939 static ssd_chain_struct *
7940 pa_subsegment_to_subspace (asection *seg, subsegT subseg)
7941 {
7942   sd_chain_struct *space_chain;
7943   ssd_chain_struct *subspace_chain;
7944
7945   /* Walk through each space.  */
7946   for (space_chain = space_dict_root;
7947        space_chain;
7948        space_chain = space_chain->sd_next)
7949     {
7950       if (space_chain->sd_seg == seg)
7951         {
7952           /* Walk through each subspace within each space looking for
7953              the correct mapping.  */
7954           for (subspace_chain = space_chain->sd_subspaces;
7955                subspace_chain;
7956                subspace_chain = subspace_chain->ssd_next)
7957             if (subspace_chain->ssd_subseg == (int) subseg)
7958               return subspace_chain;
7959         }
7960     }
7961
7962   /* No mapping from subsegment to subspace found.  Return NULL.  */
7963   return NULL;
7964 }
7965
7966 /* Given a number, try and find a space with the name number.
7967
7968    Return a pointer to a space dictionary chain entry for the space
7969    that was found or NULL on failure.  */
7970
7971 static sd_chain_struct *
7972 pa_find_space_by_number (int number)
7973 {
7974   sd_chain_struct *space_chain;
7975
7976   for (space_chain = space_dict_root;
7977        space_chain;
7978        space_chain = space_chain->sd_next)
7979     {
7980       if (SPACE_SPNUM (space_chain) == (unsigned int) number)
7981         return space_chain;
7982     }
7983
7984   /* No appropriate space found.  Return NULL.  */
7985   return NULL;
7986 }
7987
7988 /* Return the starting address for the given subspace.  If the starting
7989    address is unknown then return zero.  */
7990
7991 static unsigned int
7992 pa_subspace_start (sd_chain_struct *space, int quadrant)
7993 {
7994   /* FIXME.  Assumes everyone puts read/write data at 0x4000000, this
7995      is not correct for the PA OSF1 port.  */
7996   if ((strcmp (SPACE_NAME (space), "$PRIVATE$") == 0) && quadrant == 1)
7997     return 0x40000000;
7998   else if (space->sd_seg == data_section && quadrant == 1)
7999     return 0x40000000;
8000   else
8001     return 0;
8002   return 0;
8003 }
8004 #endif
8005
8006 /* Helper function for pa_stringer.  Used to find the end of
8007    a string.  */
8008
8009 static unsigned int
8010 pa_stringer_aux (char *s)
8011 {
8012   unsigned int c = *s & CHAR_MASK;
8013
8014   switch (c)
8015     {
8016     case '\"':
8017       c = NOT_A_CHAR;
8018       break;
8019     default:
8020       break;
8021     }
8022   return c;
8023 }
8024
8025 /* Handle a .STRING type pseudo-op.  */
8026
8027 static void
8028 pa_stringer (int append_zero)
8029 {
8030   char *s, num_buf[4];
8031   unsigned int c;
8032   int i;
8033
8034   /* Preprocess the string to handle PA-specific escape sequences.
8035      For example, \xDD where DD is a hexadecimal number should be
8036      changed to \OOO where OOO is an octal number.  */
8037
8038 #ifdef OBJ_SOM
8039   /* We must have a valid space and subspace.  */
8040   pa_check_current_space_and_subspace ();
8041 #endif
8042
8043   /* Skip the opening quote.  */
8044   s = input_line_pointer + 1;
8045
8046   while (is_a_char (c = pa_stringer_aux (s++)))
8047     {
8048       if (c == '\\')
8049         {
8050           c = *s;
8051           switch (c)
8052             {
8053               /* Handle \x<num>.  */
8054             case 'x':
8055               {
8056                 unsigned int number;
8057                 int num_digit;
8058                 char dg;
8059                 char *s_start = s;
8060
8061                 /* Get past the 'x'.  */
8062                 s++;
8063                 for (num_digit = 0, number = 0, dg = *s;
8064                      num_digit < 2
8065                      && (ISDIGIT (dg) || (dg >= 'a' && dg <= 'f')
8066                          || (dg >= 'A' && dg <= 'F'));
8067                      num_digit++)
8068                   {
8069                     if (ISDIGIT (dg))
8070                       number = number * 16 + dg - '0';
8071                     else if (dg >= 'a' && dg <= 'f')
8072                       number = number * 16 + dg - 'a' + 10;
8073                     else
8074                       number = number * 16 + dg - 'A' + 10;
8075
8076                     s++;
8077                     dg = *s;
8078                   }
8079                 if (num_digit > 0)
8080                   {
8081                     switch (num_digit)
8082                       {
8083                       case 1:
8084                         sprintf (num_buf, "%02o", number);
8085                         break;
8086                       case 2:
8087                         sprintf (num_buf, "%03o", number);
8088                         break;
8089                       }
8090                     for (i = 0; i <= num_digit; i++)
8091                       s_start[i] = num_buf[i];
8092                   }
8093                 break;
8094               }
8095             /* This might be a "\"", skip over the escaped char.  */
8096             default:
8097               s++;
8098               break;
8099             }
8100         }
8101     }
8102   stringer (8 + append_zero);
8103   pa_undefine_label ();
8104 }
8105
8106 /* Handle a .VERSION pseudo-op.  */
8107
8108 static void
8109 pa_version (int unused ATTRIBUTE_UNUSED)
8110 {
8111   obj_version (0);
8112   pa_undefine_label ();
8113 }
8114
8115 #ifdef OBJ_SOM
8116
8117 /* Handle a .COMPILER pseudo-op.  */
8118
8119 static void
8120 pa_compiler (int unused ATTRIBUTE_UNUSED)
8121 {
8122   obj_som_compiler (0);
8123   pa_undefine_label ();
8124 }
8125
8126 #endif
8127
8128 /* Handle a .COPYRIGHT pseudo-op.  */
8129
8130 static void
8131 pa_copyright (int unused ATTRIBUTE_UNUSED)
8132 {
8133   obj_copyright (0);
8134   pa_undefine_label ();
8135 }
8136
8137 /* Just like a normal cons, but when finished we have to undefine
8138    the latest space label.  */
8139
8140 static void
8141 pa_cons (int nbytes)
8142 {
8143   cons (nbytes);
8144   pa_undefine_label ();
8145 }
8146
8147 /* Like float_cons, but we need to undefine our label.  */
8148
8149 static void
8150 pa_float_cons (int float_type)
8151 {
8152   float_cons (float_type);
8153   pa_undefine_label ();
8154 }
8155
8156 /* Like s_fill, but delete our label when finished.  */
8157
8158 static void
8159 pa_fill (int unused ATTRIBUTE_UNUSED)
8160 {
8161 #ifdef OBJ_SOM
8162   /* We must have a valid space and subspace.  */
8163   pa_check_current_space_and_subspace ();
8164 #endif
8165
8166   s_fill (0);
8167   pa_undefine_label ();
8168 }
8169
8170 /* Like lcomm, but delete our label when finished.  */
8171
8172 static void
8173 pa_lcomm (int needs_align)
8174 {
8175 #ifdef OBJ_SOM
8176   /* We must have a valid space and subspace.  */
8177   pa_check_current_space_and_subspace ();
8178 #endif
8179
8180   s_lcomm (needs_align);
8181   pa_undefine_label ();
8182 }
8183
8184 /* Like lsym, but delete our label when finished.  */
8185
8186 static void
8187 pa_lsym (int unused ATTRIBUTE_UNUSED)
8188 {
8189 #ifdef OBJ_SOM
8190   /* We must have a valid space and subspace.  */
8191   pa_check_current_space_and_subspace ();
8192 #endif
8193
8194   s_lsym (0);
8195   pa_undefine_label ();
8196 }
8197
8198 /* This function is called once, at assembler startup time.  It should
8199    set up all the tables, etc. that the MD part of the assembler will need.  */
8200
8201 void
8202 md_begin (void)
8203 {
8204   const char *retval = NULL;
8205   int lose = 0;
8206   unsigned int i = 0;
8207
8208   last_call_info = NULL;
8209   call_info_root = NULL;
8210
8211   /* Set the default machine type.  */
8212   if (!bfd_set_arch_mach (stdoutput, bfd_arch_hppa, DEFAULT_LEVEL))
8213     as_warn (_("could not set architecture and machine"));
8214
8215   /* Folding of text and data segments fails miserably on the PA.
8216      Warn user and disable "-R" option.  */
8217   if (flag_readonly_data_in_text)
8218     {
8219       as_warn (_("-R option not supported on this target."));
8220       flag_readonly_data_in_text = 0;
8221     }
8222
8223 #ifdef OBJ_SOM
8224   pa_spaces_begin ();
8225 #endif
8226
8227   op_hash = hash_new ();
8228
8229   while (i < NUMOPCODES)
8230     {
8231       const char *name = pa_opcodes[i].name;
8232
8233       retval = hash_insert (op_hash, name, (struct pa_opcode *) &pa_opcodes[i]);
8234       if (retval != NULL && *retval != '\0')
8235         {
8236           as_fatal (_("Internal error: can't hash `%s': %s\n"), name, retval);
8237           lose = 1;
8238         }
8239
8240       do
8241         {
8242           if ((pa_opcodes[i].match & pa_opcodes[i].mask)
8243               != pa_opcodes[i].match)
8244             {
8245               fprintf (stderr, _("internal error: losing opcode: `%s' \"%s\"\n"),
8246                        pa_opcodes[i].name, pa_opcodes[i].args);
8247               lose = 1;
8248             }
8249           ++i;
8250         }
8251       while (i < NUMOPCODES && !strcmp (pa_opcodes[i].name, name));
8252     }
8253
8254   if (lose)
8255     as_fatal (_("Broken assembler.  No assembly attempted."));
8256
8257 #ifdef OBJ_SOM
8258   /* SOM will change text_section.  To make sure we never put
8259      anything into the old one switch to the new one now.  */
8260   subseg_set (text_section, 0);
8261 #endif
8262
8263 #ifdef OBJ_SOM
8264   dummy_symbol = symbol_find_or_make ("L$dummy");
8265   S_SET_SEGMENT (dummy_symbol, text_section);
8266   /* Force the symbol to be converted to a real symbol.  */
8267   (void) symbol_get_bfdsym (dummy_symbol);
8268 #endif
8269 }
8270
8271 /* On the PA relocations which involve function symbols must not be
8272    adjusted.  This so that the linker can know when/how to create argument
8273    relocation stubs for indirect calls and calls to static functions.
8274
8275    "T" field selectors create DLT relative fixups for accessing
8276    globals and statics in PIC code; each DLT relative fixup creates
8277    an entry in the DLT table.  The entries contain the address of
8278    the final target (eg accessing "foo" would create a DLT entry
8279    with the address of "foo").
8280
8281    Unfortunately, the HP linker doesn't take into account any addend
8282    when generating the DLT; so accessing $LIT$+8 puts the address of
8283    $LIT$ into the DLT rather than the address of $LIT$+8.
8284
8285    The end result is we can't perform relocation symbol reductions for
8286    any fixup which creates entries in the DLT (eg they use "T" field
8287    selectors).
8288
8289    ??? Reject reductions involving symbols with external scope; such
8290    reductions make life a living hell for object file editors.  */
8291
8292 int
8293 hppa_fix_adjustable (fixS *fixp)
8294 {
8295 #ifdef OBJ_ELF
8296   reloc_type code;
8297 #endif
8298   struct hppa_fix_struct *hppa_fix;
8299
8300   hppa_fix = (struct hppa_fix_struct *) fixp->tc_fix_data;
8301
8302 #ifdef OBJ_ELF
8303   /* LR/RR selectors are implicitly used for a number of different relocation
8304      types.  We must ensure that none of these types are adjusted (see below)
8305      even if they occur with a different selector.  */
8306   code = elf_hppa_reloc_final_type (stdoutput, fixp->fx_r_type,
8307                                     hppa_fix->fx_r_format,
8308                                     hppa_fix->fx_r_field);
8309
8310   switch (code)
8311     {
8312     /* Relocation types which use e_lrsel.  */
8313     case R_PARISC_DIR21L:
8314     case R_PARISC_DLTREL21L:
8315     case R_PARISC_DPREL21L:
8316     case R_PARISC_PLTOFF21L:
8317
8318     /* Relocation types which use e_rrsel.  */
8319     case R_PARISC_DIR14R:
8320     case R_PARISC_DIR14DR:
8321     case R_PARISC_DIR14WR:
8322     case R_PARISC_DIR17R:
8323     case R_PARISC_DLTREL14R:
8324     case R_PARISC_DLTREL14DR:
8325     case R_PARISC_DLTREL14WR:
8326     case R_PARISC_DPREL14R:
8327     case R_PARISC_DPREL14DR:
8328     case R_PARISC_DPREL14WR:
8329     case R_PARISC_PLTOFF14R:
8330     case R_PARISC_PLTOFF14DR:
8331     case R_PARISC_PLTOFF14WR:
8332
8333     /* Other types that we reject for reduction.  */
8334     case R_PARISC_GNU_VTENTRY:
8335     case R_PARISC_GNU_VTINHERIT:
8336       return 0;
8337     default:
8338       break;
8339     }
8340 #endif
8341
8342   /* Reject reductions of symbols in sym1-sym2 expressions when
8343      the fixup will occur in a CODE subspace.
8344
8345      XXX FIXME: Long term we probably want to reject all of these;
8346      for example reducing in the debug section would lose if we ever
8347      supported using the optimizing hp linker.  */
8348   if (fixp->fx_addsy
8349       && fixp->fx_subsy
8350       && (hppa_fix->segment->flags & SEC_CODE))
8351     return 0;
8352
8353   /* We can't adjust any relocs that use LR% and RR% field selectors.
8354
8355      If a symbol is reduced to a section symbol, the assembler will
8356      adjust the addend unless the symbol happens to reside right at
8357      the start of the section.  Additionally, the linker has no choice
8358      but to manipulate the addends when coalescing input sections for
8359      "ld -r".  Since an LR% field selector is defined to round the
8360      addend, we can't change the addend without risking that a LR% and
8361      it's corresponding (possible multiple) RR% field will no longer
8362      sum to the right value.
8363
8364      eg. Suppose we have
8365      .          ldil    LR%foo+0,%r21
8366      .          ldw     RR%foo+0(%r21),%r26
8367      .          ldw     RR%foo+4(%r21),%r25
8368
8369      If foo is at address 4092 (decimal) in section `sect', then after
8370      reducing to the section symbol we get
8371      .                  LR%sect+4092 == (L%sect)+0
8372      .                  RR%sect+4092 == (R%sect)+4092
8373      .                  RR%sect+4096 == (R%sect)-4096
8374      and the last address loses because rounding the addend to 8k
8375      multiples takes us up to 8192 with an offset of -4096.
8376
8377      In cases where the LR% expression is identical to the RR% one we
8378      will never have a problem, but is so happens that gcc rounds
8379      addends involved in LR% field selectors to work around a HP
8380      linker bug.  ie. We often have addresses like the last case
8381      above where the LR% expression is offset from the RR% one.  */
8382
8383   if (hppa_fix->fx_r_field == e_lrsel
8384       || hppa_fix->fx_r_field == e_rrsel
8385       || hppa_fix->fx_r_field == e_nlrsel)
8386     return 0;
8387
8388   /* Reject reductions of symbols in DLT relative relocs,
8389      relocations with plabels.  */
8390   if (hppa_fix->fx_r_field == e_tsel
8391       || hppa_fix->fx_r_field == e_ltsel
8392       || hppa_fix->fx_r_field == e_rtsel
8393       || hppa_fix->fx_r_field == e_psel
8394       || hppa_fix->fx_r_field == e_rpsel
8395       || hppa_fix->fx_r_field == e_lpsel)
8396     return 0;
8397
8398   /* Reject absolute calls (jumps).  */
8399   if (hppa_fix->fx_r_type == R_HPPA_ABS_CALL)
8400     return 0;
8401
8402   /* Reject reductions of function symbols.  */
8403   if (fixp->fx_addsy != 0 && S_IS_FUNCTION (fixp->fx_addsy))
8404     return 0;
8405
8406   return 1;
8407 }
8408
8409 /* Return nonzero if the fixup in FIXP will require a relocation,
8410    even it if appears that the fixup could be completely handled
8411    within GAS.  */
8412
8413 int
8414 hppa_force_relocation (struct fix *fixp)
8415 {
8416   struct hppa_fix_struct *hppa_fixp;
8417
8418   hppa_fixp = (struct hppa_fix_struct *) fixp->tc_fix_data;
8419 #ifdef OBJ_SOM
8420   if (fixp->fx_r_type == (int) R_HPPA_ENTRY
8421       || fixp->fx_r_type == (int) R_HPPA_EXIT
8422       || fixp->fx_r_type == (int) R_HPPA_BEGIN_BRTAB
8423       || fixp->fx_r_type == (int) R_HPPA_END_BRTAB
8424       || fixp->fx_r_type == (int) R_HPPA_BEGIN_TRY
8425       || fixp->fx_r_type == (int) R_HPPA_END_TRY
8426       || (fixp->fx_addsy != NULL && fixp->fx_subsy != NULL
8427           && (hppa_fixp->segment->flags & SEC_CODE) != 0))
8428     return 1;
8429 #endif
8430 #ifdef OBJ_ELF
8431   if (fixp->fx_r_type == (int) R_PARISC_GNU_VTINHERIT
8432       || fixp->fx_r_type == (int) R_PARISC_GNU_VTENTRY)
8433     return 1;
8434 #endif
8435
8436   assert (fixp->fx_addsy != NULL);
8437
8438   /* Ensure we emit a relocation for global symbols so that dynamic
8439      linking works.  */
8440   if (S_FORCE_RELOC (fixp->fx_addsy, 1))
8441     return 1;
8442
8443   /* It is necessary to force PC-relative calls/jumps to have a relocation
8444      entry if they're going to need either an argument relocation or long
8445      call stub.  */
8446   if (fixp->fx_pcrel
8447       && arg_reloc_stub_needed (symbol_arg_reloc_info (fixp->fx_addsy),
8448                                 hppa_fixp->fx_arg_reloc))
8449     return 1;
8450
8451   /* Now check to see if we're going to need a long-branch stub.  */
8452   if (fixp->fx_r_type == (int) R_HPPA_PCREL_CALL)
8453     {
8454       long pc = md_pcrel_from (fixp);
8455       valueT distance, min_stub_distance;
8456
8457       distance = fixp->fx_offset + S_GET_VALUE (fixp->fx_addsy) - pc - 8;
8458
8459       /* Distance to the closest possible stub.  This will detect most
8460          but not all circumstances where a stub will not work.  */
8461       min_stub_distance = pc + 16;
8462 #ifdef OBJ_SOM
8463       if (last_call_info != NULL)
8464         min_stub_distance -= S_GET_VALUE (last_call_info->start_symbol);
8465 #endif
8466
8467       if ((distance + 8388608 >= 16777216
8468            && min_stub_distance <= 8388608)
8469           || (hppa_fixp->fx_r_format == 17
8470               && distance + 262144 >= 524288
8471               && min_stub_distance <= 262144)
8472           || (hppa_fixp->fx_r_format == 12
8473               && distance + 8192 >= 16384
8474               && min_stub_distance <= 8192)
8475           )
8476         return 1;
8477     }
8478
8479   if (fixp->fx_r_type == (int) R_HPPA_ABS_CALL)
8480     return 1;
8481
8482   /* No need (yet) to force another relocations to be emitted.  */
8483   return 0;
8484 }
8485
8486 /* Now for some ELF specific code.  FIXME.  */
8487 #ifdef OBJ_ELF
8488 /* For ELF, this function serves one purpose:  to setup the st_size
8489    field of STT_FUNC symbols.  To do this, we need to scan the
8490    call_info structure list, determining st_size in by taking the
8491    difference in the address of the beginning/end marker symbols.  */
8492
8493 void
8494 elf_hppa_final_processing (void)
8495 {
8496   struct call_info *call_info_pointer;
8497
8498   for (call_info_pointer = call_info_root;
8499        call_info_pointer;
8500        call_info_pointer = call_info_pointer->ci_next)
8501     {
8502       elf_symbol_type *esym
8503         = ((elf_symbol_type *)
8504            symbol_get_bfdsym (call_info_pointer->start_symbol));
8505       esym->internal_elf_sym.st_size =
8506         S_GET_VALUE (call_info_pointer->end_symbol)
8507         - S_GET_VALUE (call_info_pointer->start_symbol) + 4;
8508     }
8509 }
8510
8511 static void
8512 pa_vtable_entry (int ignore ATTRIBUTE_UNUSED)
8513 {
8514   struct fix *new_fix;
8515
8516   new_fix = obj_elf_vtable_entry (0);
8517
8518   if (new_fix)
8519     {
8520       struct hppa_fix_struct * hppa_fix = obstack_alloc (&notes, sizeof (struct hppa_fix_struct));
8521
8522       hppa_fix->fx_r_type = R_HPPA;
8523       hppa_fix->fx_r_field = e_fsel;
8524       hppa_fix->fx_r_format = 32;
8525       hppa_fix->fx_arg_reloc = 0;
8526       hppa_fix->segment = now_seg;
8527       new_fix->tc_fix_data = (void *) hppa_fix;
8528       new_fix->fx_r_type = (int) R_PARISC_GNU_VTENTRY;
8529     }
8530 }
8531
8532 static void
8533 pa_vtable_inherit (int ignore ATTRIBUTE_UNUSED)
8534 {
8535   struct fix *new_fix;
8536
8537   new_fix = obj_elf_vtable_inherit (0);
8538
8539   if (new_fix)
8540     {
8541       struct hppa_fix_struct * hppa_fix = obstack_alloc (&notes, sizeof (struct hppa_fix_struct));
8542
8543       hppa_fix->fx_r_type = R_HPPA;
8544       hppa_fix->fx_r_field = e_fsel;
8545       hppa_fix->fx_r_format = 32;
8546       hppa_fix->fx_arg_reloc = 0;
8547       hppa_fix->segment = now_seg;
8548       new_fix->tc_fix_data = (void *) hppa_fix;
8549       new_fix->fx_r_type = (int) R_PARISC_GNU_VTINHERIT;
8550     }
8551 }
8552 #endif
8553
8554 /* Table of pseudo ops for the PA.  FIXME -- how many of these
8555    are now redundant with the overall GAS and the object file
8556    dependent tables?  */
8557 const pseudo_typeS md_pseudo_table[] =
8558 {
8559   /* align pseudo-ops on the PA specify the actual alignment requested,
8560      not the log2 of the requested alignment.  */
8561 #ifdef OBJ_SOM
8562   {"align", pa_align, 8},
8563 #endif
8564 #ifdef OBJ_ELF
8565   {"align", s_align_bytes, 8},
8566 #endif
8567   {"begin_brtab", pa_brtab, 1},
8568   {"begin_try", pa_try, 1},
8569   {"block", pa_block, 1},
8570   {"blockz", pa_block, 0},
8571   {"byte", pa_cons, 1},
8572   {"call", pa_call, 0},
8573   {"callinfo", pa_callinfo, 0},
8574 #if defined (OBJ_ELF) && (defined (TE_LINUX) || defined (TE_NetBSD))
8575   {"code", obj_elf_text, 0},
8576 #else
8577   {"code", pa_text, 0},
8578   {"comm", pa_comm, 0},
8579 #endif
8580 #ifdef OBJ_SOM
8581   {"compiler", pa_compiler, 0},
8582 #endif
8583   {"copyright", pa_copyright, 0},
8584 #if !(defined (OBJ_ELF) && (defined (TE_LINUX) || defined (TE_NetBSD)))
8585   {"data", pa_data, 0},
8586 #endif
8587   {"double", pa_float_cons, 'd'},
8588   {"dword", pa_cons, 8},
8589   {"end", pa_end, 0},
8590   {"end_brtab", pa_brtab, 0},
8591 #if !(defined (OBJ_ELF) && (defined (TE_LINUX) || defined (TE_NetBSD)))
8592   {"end_try", pa_try, 0},
8593 #endif
8594   {"enter", pa_enter, 0},
8595   {"entry", pa_entry, 0},
8596   {"equ", pa_equ, 0},
8597   {"exit", pa_exit, 0},
8598   {"export", pa_export, 0},
8599   {"fill", pa_fill, 0},
8600   {"float", pa_float_cons, 'f'},
8601   {"half", pa_cons, 2},
8602   {"import", pa_import, 0},
8603   {"int", pa_cons, 4},
8604   {"label", pa_label, 0},
8605   {"lcomm", pa_lcomm, 0},
8606   {"leave", pa_leave, 0},
8607   {"level", pa_level, 0},
8608   {"long", pa_cons, 4},
8609   {"lsym", pa_lsym, 0},
8610 #ifdef OBJ_SOM
8611   {"nsubspa", pa_subspace, 1},
8612 #endif
8613   {"octa", pa_cons, 16},
8614   {"org", pa_origin, 0},
8615   {"origin", pa_origin, 0},
8616   {"param", pa_param, 0},
8617   {"proc", pa_proc, 0},
8618   {"procend", pa_procend, 0},
8619   {"quad", pa_cons, 8},
8620   {"reg", pa_equ, 1},
8621   {"short", pa_cons, 2},
8622   {"single", pa_float_cons, 'f'},
8623 #ifdef OBJ_SOM
8624   {"space", pa_space, 0},
8625   {"spnum", pa_spnum, 0},
8626 #endif
8627   {"string", pa_stringer, 0},
8628   {"stringz", pa_stringer, 1},
8629 #ifdef OBJ_SOM
8630   {"subspa", pa_subspace, 0},
8631 #endif
8632 #if !(defined (OBJ_ELF) && (defined (TE_LINUX) || defined (TE_NetBSD)))
8633   {"text", pa_text, 0},
8634 #endif
8635   {"version", pa_version, 0},
8636 #ifdef OBJ_ELF
8637   {"vtable_entry", pa_vtable_entry, 0},
8638   {"vtable_inherit", pa_vtable_inherit, 0},
8639 #endif
8640   {"word", pa_cons, 4},
8641   {NULL, 0, 0}
8642 };
8643
8644 #ifdef OBJ_ELF
8645 void
8646 hppa_cfi_frame_initial_instructions (void)
8647 {
8648   cfi_add_CFA_def_cfa (30, 0);
8649 }
8650
8651 int
8652 hppa_regname_to_dw2regnum (char *regname)
8653 {
8654   unsigned int regnum = -1;
8655   unsigned int i;
8656   const char *p;
8657   char *q;
8658   static struct { char *name; int dw2regnum; } regnames[] =
8659     {
8660       { "sp", 30 }, { "rp", 2 },
8661     };
8662
8663   for (i = 0; i < ARRAY_SIZE (regnames); ++i)
8664     if (strcmp (regnames[i].name, regname) == 0)
8665       return regnames[i].dw2regnum;
8666
8667   if (regname[0] == 'r')
8668     {
8669       p = regname + 1;
8670       regnum = strtoul (p, &q, 10);
8671       if (p == q || *q || regnum >= 32)
8672         return -1;
8673     }
8674   else if (regname[0] == 'f' && regname[1] == 'r')
8675     {
8676       p = regname + 2;
8677       regnum = strtoul (p, &q, 10);
8678       if (p == q || *q || regnum <= 4 || regnum >= 32)
8679         return -1;
8680       regnum += 32 - 4;
8681     }
8682   return regnum;
8683 }
8684 #endif