OSDN Git Service

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