Back to index

cell-binutils  2.17cvs20070401
tc-ia64.c
Go to the documentation of this file.
00001 /* tc-ia64.c -- Assembler for the HP/Intel IA-64 architecture.
00002    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
00003    Free Software Foundation, Inc.
00004    Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
00005 
00006    This file is part of GAS, the GNU Assembler.
00007 
00008    GAS is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2, or (at your option)
00011    any later version.
00012 
00013    GAS is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with GAS; see the file COPYING.  If not, write to
00020    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
00021    Boston, MA 02110-1301, USA.  */
00022 
00023 /*
00024   TODO:
00025 
00026   - optional operands
00027   - directives:
00028        .eb
00029        .estate
00030        .lb
00031        .popsection
00032        .previous
00033        .psr
00034        .pushsection
00035   - labels are wrong if automatic alignment is introduced
00036     (e.g., checkout the second real10 definition in test-data.s)
00037   - DV-related stuff:
00038        <reg>.safe_across_calls and any other DV-related directives I don't
00039          have documentation for.
00040        verify mod-sched-brs reads/writes are checked/marked (and other
00041        notes)
00042 
00043  */
00044 
00045 #include "as.h"
00046 #include "safe-ctype.h"
00047 #include "dwarf2dbg.h"
00048 #include "subsegs.h"
00049 
00050 #include "opcode/ia64.h"
00051 
00052 #include "elf/ia64.h"
00053 
00054 #ifdef HAVE_LIMITS_H
00055 #include <limits.h>
00056 #endif
00057 
00058 #define NELEMS(a)    ((int) (sizeof (a)/sizeof ((a)[0])))
00059 
00060 /* Some systems define MIN in, e.g., param.h.  */
00061 #undef MIN
00062 #define MIN(a,b)     ((a) < (b) ? (a) : (b))
00063 
00064 #define NUM_SLOTS    4
00065 #define PREV_SLOT    md.slot[(md.curr_slot + NUM_SLOTS - 1) % NUM_SLOTS]
00066 #define CURR_SLOT    md.slot[md.curr_slot]
00067 
00068 #define O_pseudo_fixup (O_max + 1)
00069 
00070 enum special_section
00071   {
00072     /* IA-64 ABI section pseudo-ops.  */
00073     SPECIAL_SECTION_BSS = 0,
00074     SPECIAL_SECTION_SBSS,
00075     SPECIAL_SECTION_SDATA,
00076     SPECIAL_SECTION_RODATA,
00077     SPECIAL_SECTION_COMMENT,
00078     SPECIAL_SECTION_UNWIND,
00079     SPECIAL_SECTION_UNWIND_INFO,
00080     /* HPUX specific section pseudo-ops.  */
00081     SPECIAL_SECTION_INIT_ARRAY,
00082     SPECIAL_SECTION_FINI_ARRAY,
00083   };
00084 
00085 enum reloc_func
00086   {
00087     FUNC_DTP_MODULE,
00088     FUNC_DTP_RELATIVE,
00089     FUNC_FPTR_RELATIVE,
00090     FUNC_GP_RELATIVE,
00091     FUNC_LT_RELATIVE,
00092     FUNC_LT_RELATIVE_X,
00093     FUNC_PC_RELATIVE,
00094     FUNC_PLT_RELATIVE,
00095     FUNC_SEC_RELATIVE,
00096     FUNC_SEG_RELATIVE,
00097     FUNC_TP_RELATIVE,
00098     FUNC_LTV_RELATIVE,
00099     FUNC_LT_FPTR_RELATIVE,
00100     FUNC_LT_DTP_MODULE,
00101     FUNC_LT_DTP_RELATIVE,
00102     FUNC_LT_TP_RELATIVE,
00103     FUNC_IPLT_RELOC,
00104   };
00105 
00106 enum reg_symbol
00107   {
00108     REG_GR    = 0,
00109     REG_FR    = (REG_GR + 128),
00110     REG_AR    = (REG_FR + 128),
00111     REG_CR    = (REG_AR + 128),
00112     REG_P     = (REG_CR + 128),
00113     REG_BR    = (REG_P  + 64),
00114     REG_IP    = (REG_BR + 8),
00115     REG_CFM,
00116     REG_PR,
00117     REG_PR_ROT,
00118     REG_PSR,
00119     REG_PSR_L,
00120     REG_PSR_UM,
00121     /* The following are pseudo-registers for use by gas only.  */
00122     IND_CPUID,
00123     IND_DBR,
00124     IND_DTR,
00125     IND_ITR,
00126     IND_IBR,
00127     IND_MSR,
00128     IND_PKR,
00129     IND_PMC,
00130     IND_PMD,
00131     IND_RR,
00132     /* The following pseudo-registers are used for unwind directives only:  */
00133     REG_PSP,
00134     REG_PRIUNAT,
00135     REG_NUM
00136   };
00137 
00138 enum dynreg_type
00139   {
00140     DYNREG_GR = 0,   /* dynamic general purpose register */
00141     DYNREG_FR,              /* dynamic floating point register */
00142     DYNREG_PR,              /* dynamic predicate register */
00143     DYNREG_NUM_TYPES
00144   };
00145 
00146 enum operand_match_result
00147   {
00148     OPERAND_MATCH,
00149     OPERAND_OUT_OF_RANGE,
00150     OPERAND_MISMATCH
00151   };
00152 
00153 /* On the ia64, we can't know the address of a text label until the
00154    instructions are packed into a bundle.  To handle this, we keep
00155    track of the list of labels that appear in front of each
00156    instruction.  */
00157 struct label_fix
00158 {
00159   struct label_fix *next;
00160   struct symbol *sym;
00161   bfd_boolean dw2_mark_labels;
00162 };
00163 
00164 /* This is the endianness of the current section.  */
00165 extern int target_big_endian;
00166 
00167 /* This is the default endianness.  */
00168 static int default_big_endian = TARGET_BYTES_BIG_ENDIAN;
00169 
00170 void (*ia64_number_to_chars) PARAMS ((char *, valueT, int));
00171 
00172 static void ia64_float_to_chars_bigendian
00173   PARAMS ((char *, LITTLENUM_TYPE *, int));
00174 static void ia64_float_to_chars_littleendian
00175   PARAMS ((char *, LITTLENUM_TYPE *, int));
00176 static void (*ia64_float_to_chars)
00177   PARAMS ((char *, LITTLENUM_TYPE *, int));
00178 
00179 static struct hash_control *alias_hash;
00180 static struct hash_control *alias_name_hash;
00181 static struct hash_control *secalias_hash;
00182 static struct hash_control *secalias_name_hash;
00183 
00184 /* List of chars besides those in app.c:symbol_chars that can start an
00185    operand.  Used to prevent the scrubber eating vital white-space.  */
00186 const char ia64_symbol_chars[] = "@?";
00187 
00188 /* Characters which always start a comment.  */
00189 const char comment_chars[] = "";
00190 
00191 /* Characters which start a comment at the beginning of a line.  */
00192 const char line_comment_chars[] = "#";
00193 
00194 /* Characters which may be used to separate multiple commands on a
00195    single line.  */
00196 const char line_separator_chars[] = ";{}";
00197 
00198 /* Characters which are used to indicate an exponent in a floating
00199    point number.  */
00200 const char EXP_CHARS[] = "eE";
00201 
00202 /* Characters which mean that a number is a floating point constant,
00203    as in 0d1.0.  */
00204 const char FLT_CHARS[] = "rRsSfFdDxXpP";
00205 
00206 /* ia64-specific option processing:  */
00207 
00208 const char *md_shortopts = "m:N:x::";
00209 
00210 struct option md_longopts[] =
00211   {
00212 #define OPTION_MCONSTANT_GP (OPTION_MD_BASE + 1)
00213     {"mconstant-gp", no_argument, NULL, OPTION_MCONSTANT_GP},
00214 #define OPTION_MAUTO_PIC (OPTION_MD_BASE + 2)
00215     {"mauto-pic", no_argument, NULL, OPTION_MAUTO_PIC}
00216   };
00217 
00218 size_t md_longopts_size = sizeof (md_longopts);
00219 
00220 static struct
00221   {
00222     struct hash_control *pseudo_hash;     /* pseudo opcode hash table */
00223     struct hash_control *reg_hash; /* register name hash table */
00224     struct hash_control *dynreg_hash;     /* dynamic register hash table */
00225     struct hash_control *const_hash;      /* constant hash table */
00226     struct hash_control *entry_hash;    /* code entry hint hash table */
00227 
00228     /* If X_op is != O_absent, the registername for the instruction's
00229        qualifying predicate.  If NULL, p0 is assumed for instructions
00230        that are predicatable.  */
00231     expressionS qp;
00232 
00233     /* Optimize for which CPU.  */
00234     enum
00235       {
00236        itanium1,
00237        itanium2
00238       } tune;
00239 
00240     /* What to do when hint.b is used.  */
00241     enum
00242       {
00243        hint_b_error,
00244        hint_b_warning,
00245        hint_b_ok
00246       } hint_b;
00247 
00248     unsigned int
00249       manual_bundling : 1,
00250       debug_dv: 1,
00251       detect_dv: 1,
00252       explicit_mode : 1,            /* which mode we're in */
00253       default_explicit_mode : 1,    /* which mode is the default */
00254       mode_explicitly_set : 1,      /* was the current mode explicitly set? */
00255       auto_align : 1,
00256       keep_pending_output : 1;
00257 
00258     /* What to do when something is wrong with unwind directives.  */
00259     enum
00260       {
00261        unwind_check_warning,
00262        unwind_check_error
00263       } unwind_check;
00264 
00265     /* Each bundle consists of up to three instructions.  We keep
00266        track of four most recent instructions so we can correctly set
00267        the end_of_insn_group for the last instruction in a bundle.  */
00268     int curr_slot;
00269     int num_slots_in_use;
00270     struct slot
00271       {
00272        unsigned int
00273          end_of_insn_group : 1,
00274          manual_bundling_on : 1,
00275          manual_bundling_off : 1,
00276          loc_directive_seen : 1;
00277        signed char user_template;  /* user-selected template, if any */
00278        unsigned char qp_regno;            /* qualifying predicate */
00279        /* This duplicates a good fraction of "struct fix" but we
00280           can't use a "struct fix" instead since we can't call
00281           fix_new_exp() until we know the address of the instruction.  */
00282        int num_fixups;
00283        struct insn_fix
00284          {
00285            bfd_reloc_code_real_type code;
00286            enum ia64_opnd opnd;    /* type of operand in need of fix */
00287            unsigned int is_pcrel : 1;     /* is operand pc-relative? */
00288            expressionS expr;              /* the value to be inserted */
00289          }
00290        fixup[2];                   /* at most two fixups per insn */
00291        struct ia64_opcode *idesc;
00292        struct label_fix *label_fixups;
00293        struct label_fix *tag_fixups;
00294        struct unw_rec_list *unwind_record;       /* Unwind directive.  */
00295        expressionS opnd[6];
00296        char *src_file;
00297        unsigned int src_line;
00298        struct dwarf2_line_info debug_line;
00299       }
00300     slot[NUM_SLOTS];
00301 
00302     segT last_text_seg;
00303 
00304     struct dynreg
00305       {
00306        struct dynreg *next;        /* next dynamic register */
00307        const char *name;
00308        unsigned short base;        /* the base register number */
00309        unsigned short num_regs;    /* # of registers in this set */
00310       }
00311     *dynreg[DYNREG_NUM_TYPES], in, loc, out, rot;
00312 
00313     flagword flags;                /* ELF-header flags */
00314 
00315     struct mem_offset {
00316       unsigned hint:1;              /* is this hint currently valid? */
00317       bfd_vma offset;               /* mem.offset offset */
00318       bfd_vma base;                 /* mem.offset base */
00319     } mem_offset;
00320 
00321     int path;                       /* number of alt. entry points seen */
00322     const char **entry_labels;      /* labels of all alternate paths in
00323                                    the current DV-checking block.  */
00324     int maxpaths;                   /* size currently allocated for
00325                                    entry_labels */
00326 
00327     int pointer_size;       /* size in bytes of a pointer */
00328     int pointer_size_shift; /* shift size of a pointer for alignment */
00329 
00330     symbolS *indregsym[IND_RR - IND_CPUID + 1];
00331   }
00332 md;
00333 
00334 /* These are not const, because they are modified to MMI for non-itanium1
00335    targets below.  */
00336 /* MFI bundle of nops.  */
00337 static unsigned char le_nop[16] =
00338 {
00339   0x0c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
00340   0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00
00341 };
00342 /* MFI bundle of nops with stop-bit.  */
00343 static unsigned char le_nop_stop[16] =
00344 {
00345   0x0d, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
00346   0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00
00347 };
00348 
00349 /* application registers:  */
00350 
00351 #define AR_K0        0
00352 #define AR_K7        7
00353 #define AR_RSC              16
00354 #define AR_BSP              17
00355 #define AR_BSPSTORE  18
00356 #define AR_RNAT             19
00357 #define AR_FCR              21
00358 #define AR_EFLAG     24
00359 #define AR_CSD              25
00360 #define AR_SSD              26
00361 #define AR_CFLG             27
00362 #define AR_FSR              28
00363 #define AR_FIR              29
00364 #define AR_FDR              30
00365 #define AR_CCV              32
00366 #define AR_UNAT             36
00367 #define AR_FPSR             40
00368 #define AR_ITC              44
00369 #define AR_PFS              64
00370 #define AR_LC        65
00371 #define AR_EC        66
00372 
00373 static const struct
00374   {
00375     const char *name;
00376     unsigned int regnum;
00377   }
00378 ar[] =
00379   {
00380     {"ar.k0",        AR_K0},              {"ar.k1",     AR_K0 + 1},
00381     {"ar.k2",        AR_K0 + 2},   {"ar.k3",     AR_K0 + 3},
00382     {"ar.k4",        AR_K0 + 4},   {"ar.k5",     AR_K0 + 5},
00383     {"ar.k6",        AR_K0 + 6},   {"ar.k7",     AR_K7},
00384     {"ar.rsc",              AR_RSC},      {"ar.bsp",    AR_BSP},
00385     {"ar.bspstore",  AR_BSPSTORE}, {"ar.rnat",   AR_RNAT},
00386     {"ar.fcr",              AR_FCR},      {"ar.eflag",  AR_EFLAG},
00387     {"ar.csd",              AR_CSD},      {"ar.ssd",    AR_SSD},
00388     {"ar.cflg",             AR_CFLG},     {"ar.fsr",    AR_FSR},
00389     {"ar.fir",              AR_FIR},      {"ar.fdr",    AR_FDR},
00390     {"ar.ccv",              AR_CCV},      {"ar.unat",   AR_UNAT},
00391     {"ar.fpsr",             AR_FPSR},     {"ar.itc",    AR_ITC},
00392     {"ar.pfs",              AR_PFS},      {"ar.lc",     AR_LC},
00393     {"ar.ec",        AR_EC},
00394   };
00395 
00396 /* control registers:  */
00397 
00398 #define CR_DCR           0
00399 #define CR_ITM           1
00400 #define CR_IVA           2
00401 #define CR_PTA           8
00402 #define CR_GPTA          9
00403 #define CR_IPSR         16
00404 #define CR_ISR          17
00405 #define CR_IIP          19
00406 #define CR_IFA          20
00407 #define CR_ITIR         21
00408 #define CR_IIPA         22
00409 #define CR_IFS          23
00410 #define CR_IIM          24
00411 #define CR_IHA          25
00412 #define CR_LID          64
00413 #define CR_IVR          65
00414 #define CR_TPR          66
00415 #define CR_EOI          67
00416 #define CR_IRR0         68
00417 #define CR_IRR3         71
00418 #define CR_ITV          72
00419 #define CR_PMV          73
00420 #define CR_CMCV         74
00421 #define CR_LRR0         80
00422 #define CR_LRR1         81
00423 
00424 static const struct
00425   {
00426     const char *name;
00427     unsigned int regnum;
00428   }
00429 cr[] =
00430   {
00431     {"cr.dcr",       CR_DCR},
00432     {"cr.itm",       CR_ITM},
00433     {"cr.iva",       CR_IVA},
00434     {"cr.pta",       CR_PTA},
00435     {"cr.gpta",      CR_GPTA},
00436     {"cr.ipsr",      CR_IPSR},
00437     {"cr.isr",       CR_ISR},
00438     {"cr.iip",       CR_IIP},
00439     {"cr.ifa",       CR_IFA},
00440     {"cr.itir",      CR_ITIR},
00441     {"cr.iipa",      CR_IIPA},
00442     {"cr.ifs",       CR_IFS},
00443     {"cr.iim",       CR_IIM},
00444     {"cr.iha",       CR_IHA},
00445     {"cr.lid",       CR_LID},
00446     {"cr.ivr",       CR_IVR},
00447     {"cr.tpr",       CR_TPR},
00448     {"cr.eoi",       CR_EOI},
00449     {"cr.irr0",      CR_IRR0},
00450     {"cr.irr1",      CR_IRR0 + 1},
00451     {"cr.irr2",      CR_IRR0 + 2},
00452     {"cr.irr3",      CR_IRR3},
00453     {"cr.itv",       CR_ITV},
00454     {"cr.pmv",       CR_PMV},
00455     {"cr.cmcv",      CR_CMCV},
00456     {"cr.lrr0",      CR_LRR0},
00457     {"cr.lrr1",      CR_LRR1}
00458   };
00459 
00460 #define PSR_MFL         4
00461 #define PSR_IC          13
00462 #define PSR_DFL         18
00463 #define PSR_CPL         32
00464 
00465 static const struct const_desc
00466   {
00467     const char *name;
00468     valueT value;
00469   }
00470 const_bits[] =
00471   {
00472     /* PSR constant masks:  */
00473 
00474     /* 0: reserved */
00475     {"psr.be",       ((valueT) 1) << 1},
00476     {"psr.up",       ((valueT) 1) << 2},
00477     {"psr.ac",       ((valueT) 1) << 3},
00478     {"psr.mfl",      ((valueT) 1) << 4},
00479     {"psr.mfh",      ((valueT) 1) << 5},
00480     /* 6-12: reserved */
00481     {"psr.ic",       ((valueT) 1) << 13},
00482     {"psr.i", ((valueT) 1) << 14},
00483     {"psr.pk",       ((valueT) 1) << 15},
00484     /* 16: reserved */
00485     {"psr.dt",       ((valueT) 1) << 17},
00486     {"psr.dfl",      ((valueT) 1) << 18},
00487     {"psr.dfh",      ((valueT) 1) << 19},
00488     {"psr.sp",       ((valueT) 1) << 20},
00489     {"psr.pp",       ((valueT) 1) << 21},
00490     {"psr.di",       ((valueT) 1) << 22},
00491     {"psr.si",       ((valueT) 1) << 23},
00492     {"psr.db",       ((valueT) 1) << 24},
00493     {"psr.lp",       ((valueT) 1) << 25},
00494     {"psr.tb",       ((valueT) 1) << 26},
00495     {"psr.rt",       ((valueT) 1) << 27},
00496     /* 28-31: reserved */
00497     /* 32-33: cpl (current privilege level) */
00498     {"psr.is",       ((valueT) 1) << 34},
00499     {"psr.mc",       ((valueT) 1) << 35},
00500     {"psr.it",       ((valueT) 1) << 36},
00501     {"psr.id",       ((valueT) 1) << 37},
00502     {"psr.da",       ((valueT) 1) << 38},
00503     {"psr.dd",       ((valueT) 1) << 39},
00504     {"psr.ss",       ((valueT) 1) << 40},
00505     /* 41-42: ri (restart instruction) */
00506     {"psr.ed",       ((valueT) 1) << 43},
00507     {"psr.bn",       ((valueT) 1) << 44},
00508   };
00509 
00510 /* indirect register-sets/memory:  */
00511 
00512 static const struct
00513   {
00514     const char *name;
00515     unsigned int regnum;
00516   }
00517 indirect_reg[] =
00518   {
00519     { "CPUID",       IND_CPUID },
00520     { "cpuid",       IND_CPUID },
00521     { "dbr",  IND_DBR },
00522     { "dtr",  IND_DTR },
00523     { "itr",  IND_ITR },
00524     { "ibr",  IND_IBR },
00525     { "msr",  IND_MSR },
00526     { "pkr",  IND_PKR },
00527     { "pmc",  IND_PMC },
00528     { "pmd",  IND_PMD },
00529     { "rr",   IND_RR },
00530   };
00531 
00532 /* Pseudo functions used to indicate relocation types (these functions
00533    start with an at sign (@).  */
00534 static struct
00535   {
00536     const char *name;
00537     enum pseudo_type
00538       {
00539        PSEUDO_FUNC_NONE,
00540        PSEUDO_FUNC_RELOC,
00541        PSEUDO_FUNC_CONST,
00542        PSEUDO_FUNC_REG,
00543        PSEUDO_FUNC_FLOAT
00544       }
00545     type;
00546     union
00547       {
00548        unsigned long ival;
00549        symbolS *sym;
00550       }
00551     u;
00552   }
00553 pseudo_func[] =
00554   {
00555     /* reloc pseudo functions (these must come first!):  */
00556     { "dtpmod",      PSEUDO_FUNC_RELOC, { 0 } },
00557     { "dtprel",      PSEUDO_FUNC_RELOC, { 0 } },
00558     { "fptr", PSEUDO_FUNC_RELOC, { 0 } },
00559     { "gprel",       PSEUDO_FUNC_RELOC, { 0 } },
00560     { "ltoff",       PSEUDO_FUNC_RELOC, { 0 } },
00561     { "ltoffx",      PSEUDO_FUNC_RELOC, { 0 } },
00562     { "pcrel",       PSEUDO_FUNC_RELOC, { 0 } },
00563     { "pltoff",      PSEUDO_FUNC_RELOC, { 0 } },
00564     { "secrel",      PSEUDO_FUNC_RELOC, { 0 } },
00565     { "segrel",      PSEUDO_FUNC_RELOC, { 0 } },
00566     { "tprel",       PSEUDO_FUNC_RELOC, { 0 } },
00567     { "ltv",  PSEUDO_FUNC_RELOC, { 0 } },
00568     { NULL, 0, { 0 } },     /* placeholder for FUNC_LT_FPTR_RELATIVE */
00569     { NULL, 0, { 0 } },     /* placeholder for FUNC_LT_DTP_MODULE */
00570     { NULL, 0, { 0 } },     /* placeholder for FUNC_LT_DTP_RELATIVE */
00571     { NULL, 0, { 0 } },     /* placeholder for FUNC_LT_TP_RELATIVE */
00572     { "iplt", PSEUDO_FUNC_RELOC, { 0 } },
00573 
00574     /* mbtype4 constants:  */
00575     { "alt",  PSEUDO_FUNC_CONST, { 0xa } },
00576     { "brcst",       PSEUDO_FUNC_CONST, { 0x0 } },
00577     { "mix",  PSEUDO_FUNC_CONST, { 0x8 } },
00578     { "rev",  PSEUDO_FUNC_CONST, { 0xb } },
00579     { "shuf", PSEUDO_FUNC_CONST, { 0x9 } },
00580 
00581     /* fclass constants:  */
00582     { "nat",  PSEUDO_FUNC_CONST, { 0x100 } },
00583     { "qnan", PSEUDO_FUNC_CONST, { 0x080 } },
00584     { "snan", PSEUDO_FUNC_CONST, { 0x040 } },
00585     { "pos",  PSEUDO_FUNC_CONST, { 0x001 } },
00586     { "neg",  PSEUDO_FUNC_CONST, { 0x002 } },
00587     { "zero", PSEUDO_FUNC_CONST, { 0x004 } },
00588     { "unorm",       PSEUDO_FUNC_CONST, { 0x008 } },
00589     { "norm", PSEUDO_FUNC_CONST, { 0x010 } },
00590     { "inf",  PSEUDO_FUNC_CONST, { 0x020 } },
00591 
00592     { "natval",      PSEUDO_FUNC_CONST, { 0x100 } }, /* old usage */
00593 
00594     /* hint constants: */
00595     { "pause",       PSEUDO_FUNC_CONST, { 0x0 } },
00596 
00597     /* unwind-related constants:  */
00598     { "svr4", PSEUDO_FUNC_CONST,   { ELFOSABI_NONE } },
00599     { "hpux", PSEUDO_FUNC_CONST,   { ELFOSABI_HPUX } },
00600     { "nt",   PSEUDO_FUNC_CONST,   { 2 } },             /* conflicts w/ELFOSABI_NETBSD */
00601     { "linux",       PSEUDO_FUNC_CONST,   { ELFOSABI_LINUX } },
00602     { "freebsd", PSEUDO_FUNC_CONST,       { ELFOSABI_FREEBSD } },
00603     { "openvms", PSEUDO_FUNC_CONST,       { ELFOSABI_OPENVMS } },
00604     { "nsk",  PSEUDO_FUNC_CONST,   { ELFOSABI_NSK } },
00605 
00606     /* unwind-related registers:  */
00607     { "priunat",PSEUDO_FUNC_REG, { REG_PRIUNAT } }
00608   };
00609 
00610 /* 41-bit nop opcodes (one per unit):  */
00611 static const bfd_vma nop[IA64_NUM_UNITS] =
00612   {
00613     0x0000000000LL,  /* NIL => break 0 */
00614     0x0008000000LL,  /* I-unit nop */
00615     0x0008000000LL,  /* M-unit nop */
00616     0x4000000000LL,  /* B-unit nop */
00617     0x0008000000LL,  /* F-unit nop */
00618     0x0000000000LL,  /* L-"unit" nop immediate */
00619     0x0008000000LL,  /* X-unit nop */
00620   };
00621 
00622 /* Can't be `const' as it's passed to input routines (which have the
00623    habit of setting temporary sentinels.  */
00624 static char special_section_name[][20] =
00625   {
00626     {".bss"}, {".sbss"}, {".sdata"}, {".rodata"}, {".comment"},
00627     {".IA_64.unwind"}, {".IA_64.unwind_info"},
00628     {".init_array"}, {".fini_array"}
00629   };
00630 
00631 /* The best template for a particular sequence of up to three
00632    instructions:  */
00633 #define N     IA64_NUM_TYPES
00634 static unsigned char best_template[N][N][N];
00635 #undef N
00636 
00637 /* Resource dependencies currently in effect */
00638 static struct rsrc {
00639   int depind;                       /* dependency index */
00640   const struct ia64_dependency *dependency; /* actual dependency */
00641   unsigned specific:1,              /* is this a specific bit/regno? */
00642     link_to_qp_branch:1;           /* will a branch on the same QP clear it?*/
00643   int index;                        /* specific regno/bit within dependency */
00644   int note;                         /* optional qualifying note (0 if none) */
00645 #define STATE_NONE 0
00646 #define STATE_STOP 1
00647 #define STATE_SRLZ 2
00648   int insn_srlz;                    /* current insn serialization state */
00649   int data_srlz;                    /* current data serialization state */
00650   int qp_regno;                     /* qualifying predicate for this usage */
00651   char *file;                       /* what file marked this dependency */
00652   unsigned int line;                /* what line marked this dependency */
00653   struct mem_offset mem_offset;     /* optional memory offset hint */
00654   enum { CMP_NONE, CMP_OR, CMP_AND } cmp_type; /* OR or AND compare? */
00655   int path;                         /* corresponding code entry index */
00656 } *regdeps = NULL;
00657 static int regdepslen = 0;
00658 static int regdepstotlen = 0;
00659 static const char *dv_mode[] = { "RAW", "WAW", "WAR" };
00660 static const char *dv_sem[] = { "none", "implied", "impliedf",
00661                             "data", "instr", "specific", "stop", "other" };
00662 static const char *dv_cmp_type[] = { "none", "OR", "AND" };
00663 
00664 /* Current state of PR mutexation */
00665 static struct qpmutex {
00666   valueT prmask;
00667   int path;
00668 } *qp_mutexes = NULL;          /* QP mutex bitmasks */
00669 static int qp_mutexeslen = 0;
00670 static int qp_mutexestotlen = 0;
00671 static valueT qp_safe_across_calls = 0;
00672 
00673 /* Current state of PR implications */
00674 static struct qp_imply {
00675   unsigned p1:6;
00676   unsigned p2:6;
00677   unsigned p2_branched:1;
00678   int path;
00679 } *qp_implies = NULL;
00680 static int qp_implieslen = 0;
00681 static int qp_impliestotlen = 0;
00682 
00683 /* Keep track of static GR values so that indirect register usage can
00684    sometimes be tracked.  */
00685 static struct gr {
00686   unsigned known:1;
00687   int path;
00688   valueT value;
00689 } gr_values[128] = {
00690   {
00691     1,
00692 #ifdef INT_MAX
00693     INT_MAX,
00694 #else
00695     (((1 << (8 * sizeof(gr_values->path) - 2)) - 1) << 1) + 1,
00696 #endif
00697     0
00698   }
00699 };
00700 
00701 /* Remember the alignment frag.  */
00702 static fragS *align_frag;
00703 
00704 /* These are the routines required to output the various types of
00705    unwind records.  */
00706 
00707 /* A slot_number is a frag address plus the slot index (0-2).  We use the
00708    frag address here so that if there is a section switch in the middle of
00709    a function, then instructions emitted to a different section are not
00710    counted.  Since there may be more than one frag for a function, this
00711    means we also need to keep track of which frag this address belongs to
00712    so we can compute inter-frag distances.  This also nicely solves the
00713    problem with nops emitted for align directives, which can't easily be
00714    counted, but can easily be derived from frag sizes.  */
00715 
00716 typedef struct unw_rec_list {
00717   unwind_record r;
00718   unsigned long slot_number;
00719   fragS *slot_frag;
00720   struct unw_rec_list *next;
00721 } unw_rec_list;
00722 
00723 #define SLOT_NUM_NOT_SET        (unsigned)-1
00724 
00725 /* Linked list of saved prologue counts.  A very poor
00726    implementation of a map from label numbers to prologue counts.  */
00727 typedef struct label_prologue_count
00728 {
00729   struct label_prologue_count *next;
00730   unsigned long label_number;
00731   unsigned int prologue_count;
00732 } label_prologue_count;
00733 
00734 typedef struct proc_pending
00735 {
00736   symbolS *sym;
00737   struct proc_pending *next;
00738 } proc_pending;
00739 
00740 static struct
00741 {
00742   /* Maintain a list of unwind entries for the current function.  */
00743   unw_rec_list *list;
00744   unw_rec_list *tail;
00745 
00746   /* Any unwind entires that should be attached to the current slot
00747      that an insn is being constructed for.  */
00748   unw_rec_list *current_entry;
00749 
00750   /* These are used to create the unwind table entry for this function.  */
00751   proc_pending proc_pending;
00752   symbolS *info;            /* pointer to unwind info */
00753   symbolS *personality_routine;
00754   segT saved_text_seg;
00755   subsegT saved_text_subseg;
00756   unsigned int force_unwind_entry : 1;    /* force generation of unwind entry? */
00757 
00758   /* TRUE if processing unwind directives in a prologue region.  */
00759   unsigned int prologue : 1;
00760   unsigned int prologue_mask : 4;
00761   unsigned int prologue_gr : 7;
00762   unsigned int body : 1;
00763   unsigned int insn : 1;
00764   unsigned int prologue_count;     /* number of .prologues seen so far */
00765   /* Prologue counts at previous .label_state directives.  */
00766   struct label_prologue_count * saved_prologue_counts;
00767 
00768   /* List of split up .save-s.  */
00769   unw_p_record *pending_saves;
00770 } unwind;
00771 
00772 /* The input value is a negated offset from psp, and specifies an address
00773    psp - offset.  The encoded value is psp + 16 - (4 * offset).  Thus we
00774    must add 16 and divide by 4 to get the encoded value.  */
00775 
00776 #define ENCODED_PSP_OFFSET(OFFSET) (((OFFSET) + 16) / 4)
00777 
00778 typedef void (*vbyte_func) PARAMS ((int, char *, char *));
00779 
00780 /* Forward declarations:  */
00781 static void set_section PARAMS ((char *name));
00782 static unsigned int set_regstack PARAMS ((unsigned int, unsigned int,
00783                                      unsigned int, unsigned int));
00784 static void dot_align (int);
00785 static void dot_radix PARAMS ((int));
00786 static void dot_special_section PARAMS ((int));
00787 static void dot_proc PARAMS ((int));
00788 static void dot_fframe PARAMS ((int));
00789 static void dot_vframe PARAMS ((int));
00790 static void dot_vframesp PARAMS ((int));
00791 static void dot_save PARAMS ((int));
00792 static void dot_restore PARAMS ((int));
00793 static void dot_restorereg PARAMS ((int));
00794 static void dot_handlerdata  PARAMS ((int));
00795 static void dot_unwentry PARAMS ((int));
00796 static void dot_altrp PARAMS ((int));
00797 static void dot_savemem PARAMS ((int));
00798 static void dot_saveg PARAMS ((int));
00799 static void dot_savef PARAMS ((int));
00800 static void dot_saveb PARAMS ((int));
00801 static void dot_savegf PARAMS ((int));
00802 static void dot_spill PARAMS ((int));
00803 static void dot_spillreg PARAMS ((int));
00804 static void dot_spillmem PARAMS ((int));
00805 static void dot_label_state PARAMS ((int));
00806 static void dot_copy_state PARAMS ((int));
00807 static void dot_unwabi PARAMS ((int));
00808 static void dot_personality PARAMS ((int));
00809 static void dot_body PARAMS ((int));
00810 static void dot_prologue PARAMS ((int));
00811 static void dot_endp PARAMS ((int));
00812 static void dot_template PARAMS ((int));
00813 static void dot_regstk PARAMS ((int));
00814 static void dot_rot PARAMS ((int));
00815 static void dot_byteorder PARAMS ((int));
00816 static void dot_psr PARAMS ((int));
00817 static void dot_alias PARAMS ((int));
00818 static void dot_ln PARAMS ((int));
00819 static void cross_section PARAMS ((int ref, void (*cons) PARAMS((int)), int ua));
00820 static void dot_xdata PARAMS ((int));
00821 static void stmt_float_cons PARAMS ((int));
00822 static void stmt_cons_ua PARAMS ((int));
00823 static void dot_xfloat_cons PARAMS ((int));
00824 static void dot_xstringer PARAMS ((int));
00825 static void dot_xdata_ua PARAMS ((int));
00826 static void dot_xfloat_cons_ua PARAMS ((int));
00827 static void print_prmask PARAMS ((valueT mask));
00828 static void dot_pred_rel PARAMS ((int));
00829 static void dot_reg_val PARAMS ((int));
00830 static void dot_serialize PARAMS ((int));
00831 static void dot_dv_mode PARAMS ((int));
00832 static void dot_entry PARAMS ((int));
00833 static void dot_mem_offset PARAMS ((int));
00834 static void add_unwind_entry PARAMS((unw_rec_list *, int));
00835 static symbolS *declare_register PARAMS ((const char *name, unsigned int regnum));
00836 static void declare_register_set PARAMS ((const char *, unsigned int, unsigned int));
00837 static unsigned int operand_width PARAMS ((enum ia64_opnd));
00838 static enum operand_match_result operand_match PARAMS ((const struct ia64_opcode *idesc,
00839                                                  int index,
00840                                                  expressionS *e));
00841 static int parse_operand PARAMS ((expressionS *, int));
00842 static struct ia64_opcode * parse_operands PARAMS ((struct ia64_opcode *));
00843 static void build_insn PARAMS ((struct slot *, bfd_vma *));
00844 static void emit_one_bundle PARAMS ((void));
00845 static void fix_insn PARAMS ((fixS *, const struct ia64_operand *, valueT));
00846 static bfd_reloc_code_real_type ia64_gen_real_reloc_type PARAMS ((struct symbol *sym,
00847                                                           bfd_reloc_code_real_type r_type));
00848 static void insn_group_break PARAMS ((int, int, int));
00849 static void mark_resource PARAMS ((struct ia64_opcode *, const struct ia64_dependency *,
00850                                struct rsrc *, int depind, int path));
00851 static void add_qp_mutex PARAMS((valueT mask));
00852 static void add_qp_imply PARAMS((int p1, int p2));
00853 static void clear_qp_branch_flag PARAMS((valueT mask));
00854 static void clear_qp_mutex PARAMS((valueT mask));
00855 static void clear_qp_implies PARAMS((valueT p1_mask, valueT p2_mask));
00856 static int has_suffix_p PARAMS((const char *, const char *));
00857 static void clear_register_values PARAMS ((void));
00858 static void print_dependency PARAMS ((const char *action, int depind));
00859 static void instruction_serialization PARAMS ((void));
00860 static void data_serialization PARAMS ((void));
00861 static void remove_marked_resource PARAMS ((struct rsrc *));
00862 static int is_conditional_branch PARAMS ((struct ia64_opcode *));
00863 static int is_taken_branch PARAMS ((struct ia64_opcode *));
00864 static int is_interruption_or_rfi PARAMS ((struct ia64_opcode *));
00865 static int depends_on PARAMS ((int, struct ia64_opcode *));
00866 static int specify_resource PARAMS ((const struct ia64_dependency *,
00867                                  struct ia64_opcode *, int, struct rsrc [], int, int));
00868 static int check_dv PARAMS((struct ia64_opcode *idesc));
00869 static void check_dependencies PARAMS((struct ia64_opcode *));
00870 static void mark_resources PARAMS((struct ia64_opcode *));
00871 static void update_dependencies PARAMS((struct ia64_opcode *));
00872 static void note_register_values PARAMS((struct ia64_opcode *));
00873 static int qp_mutex PARAMS ((int, int, int));
00874 static int resources_match PARAMS ((struct rsrc *, struct ia64_opcode *, int, int, int));
00875 static void output_vbyte_mem PARAMS ((int, char *, char *));
00876 static void count_output PARAMS ((int, char *, char *));
00877 static void output_R1_format PARAMS ((vbyte_func, unw_record_type, int));
00878 static void output_R2_format PARAMS ((vbyte_func, int, int, unsigned long));
00879 static void output_R3_format PARAMS ((vbyte_func, unw_record_type, unsigned long));
00880 static void output_P1_format PARAMS ((vbyte_func, int));
00881 static void output_P2_format PARAMS ((vbyte_func, int, int));
00882 static void output_P3_format PARAMS ((vbyte_func, unw_record_type, int));
00883 static void output_P4_format PARAMS ((vbyte_func, unsigned char *, unsigned long));
00884 static void output_P5_format PARAMS ((vbyte_func, int, unsigned long));
00885 static void output_P6_format PARAMS ((vbyte_func, unw_record_type, int));
00886 static void output_P7_format PARAMS ((vbyte_func, unw_record_type, unsigned long, unsigned long));
00887 static void output_P8_format PARAMS ((vbyte_func, unw_record_type, unsigned long));
00888 static void output_P9_format PARAMS ((vbyte_func, int, int));
00889 static void output_P10_format PARAMS ((vbyte_func, int, int));
00890 static void output_B1_format PARAMS ((vbyte_func, unw_record_type, unsigned long));
00891 static void output_B2_format PARAMS ((vbyte_func, unsigned long, unsigned long));
00892 static void output_B3_format PARAMS ((vbyte_func, unsigned long, unsigned long));
00893 static void output_B4_format PARAMS ((vbyte_func, unw_record_type, unsigned long));
00894 static char format_ab_reg PARAMS ((int, int));
00895 static void output_X1_format PARAMS ((vbyte_func, unw_record_type, int, int, unsigned long,
00896                                   unsigned long));
00897 static void output_X2_format PARAMS ((vbyte_func, int, int, int, int, int, unsigned long));
00898 static void output_X3_format PARAMS ((vbyte_func, unw_record_type, int, int, int, unsigned long,
00899                                   unsigned long));
00900 static void output_X4_format PARAMS ((vbyte_func, int, int, int, int, int, int, unsigned long));
00901 static unw_rec_list *output_endp PARAMS ((void));
00902 static unw_rec_list *output_prologue PARAMS ((void));
00903 static unw_rec_list *output_prologue_gr PARAMS ((unsigned int, unsigned int));
00904 static unw_rec_list *output_body PARAMS ((void));
00905 static unw_rec_list *output_mem_stack_f PARAMS ((unsigned int));
00906 static unw_rec_list *output_mem_stack_v PARAMS ((void));
00907 static unw_rec_list *output_psp_gr PARAMS ((unsigned int));
00908 static unw_rec_list *output_psp_sprel PARAMS ((unsigned int));
00909 static unw_rec_list *output_rp_when PARAMS ((void));
00910 static unw_rec_list *output_rp_gr PARAMS ((unsigned int));
00911 static unw_rec_list *output_rp_br PARAMS ((unsigned int));
00912 static unw_rec_list *output_rp_psprel PARAMS ((unsigned int));
00913 static unw_rec_list *output_rp_sprel PARAMS ((unsigned int));
00914 static unw_rec_list *output_pfs_when PARAMS ((void));
00915 static unw_rec_list *output_pfs_gr PARAMS ((unsigned int));
00916 static unw_rec_list *output_pfs_psprel PARAMS ((unsigned int));
00917 static unw_rec_list *output_pfs_sprel PARAMS ((unsigned int));
00918 static unw_rec_list *output_preds_when PARAMS ((void));
00919 static unw_rec_list *output_preds_gr PARAMS ((unsigned int));
00920 static unw_rec_list *output_preds_psprel PARAMS ((unsigned int));
00921 static unw_rec_list *output_preds_sprel PARAMS ((unsigned int));
00922 static unw_rec_list *output_fr_mem PARAMS ((unsigned int));
00923 static unw_rec_list *output_frgr_mem PARAMS ((unsigned int, unsigned int));
00924 static unw_rec_list *output_gr_gr PARAMS ((unsigned int, unsigned int));
00925 static unw_rec_list *output_gr_mem PARAMS ((unsigned int));
00926 static unw_rec_list *output_br_mem PARAMS ((unsigned int));
00927 static unw_rec_list *output_br_gr PARAMS ((unsigned int, unsigned int));
00928 static unw_rec_list *output_spill_base PARAMS ((unsigned int));
00929 static unw_rec_list *output_unat_when PARAMS ((void));
00930 static unw_rec_list *output_unat_gr PARAMS ((unsigned int));
00931 static unw_rec_list *output_unat_psprel PARAMS ((unsigned int));
00932 static unw_rec_list *output_unat_sprel PARAMS ((unsigned int));
00933 static unw_rec_list *output_lc_when PARAMS ((void));
00934 static unw_rec_list *output_lc_gr PARAMS ((unsigned int));
00935 static unw_rec_list *output_lc_psprel PARAMS ((unsigned int));
00936 static unw_rec_list *output_lc_sprel PARAMS ((unsigned int));
00937 static unw_rec_list *output_fpsr_when PARAMS ((void));
00938 static unw_rec_list *output_fpsr_gr PARAMS ((unsigned int));
00939 static unw_rec_list *output_fpsr_psprel PARAMS ((unsigned int));
00940 static unw_rec_list *output_fpsr_sprel PARAMS ((unsigned int));
00941 static unw_rec_list *output_priunat_when_gr PARAMS ((void));
00942 static unw_rec_list *output_priunat_when_mem PARAMS ((void));
00943 static unw_rec_list *output_priunat_gr PARAMS ((unsigned int));
00944 static unw_rec_list *output_priunat_psprel PARAMS ((unsigned int));
00945 static unw_rec_list *output_priunat_sprel PARAMS ((unsigned int));
00946 static unw_rec_list *output_bsp_when PARAMS ((void));
00947 static unw_rec_list *output_bsp_gr PARAMS ((unsigned int));
00948 static unw_rec_list *output_bsp_psprel PARAMS ((unsigned int));
00949 static unw_rec_list *output_bsp_sprel PARAMS ((unsigned int));
00950 static unw_rec_list *output_bspstore_when PARAMS ((void));
00951 static unw_rec_list *output_bspstore_gr PARAMS ((unsigned int));
00952 static unw_rec_list *output_bspstore_psprel PARAMS ((unsigned int));
00953 static unw_rec_list *output_bspstore_sprel PARAMS ((unsigned int));
00954 static unw_rec_list *output_rnat_when PARAMS ((void));
00955 static unw_rec_list *output_rnat_gr PARAMS ((unsigned int));
00956 static unw_rec_list *output_rnat_psprel PARAMS ((unsigned int));
00957 static unw_rec_list *output_rnat_sprel PARAMS ((unsigned int));
00958 static unw_rec_list *output_unwabi PARAMS ((unsigned long, unsigned long));
00959 static unw_rec_list *output_epilogue PARAMS ((unsigned long));
00960 static unw_rec_list *output_label_state PARAMS ((unsigned long));
00961 static unw_rec_list *output_copy_state PARAMS ((unsigned long));
00962 static unw_rec_list *output_spill_psprel PARAMS ((unsigned int, unsigned int, unsigned int,
00963                                               unsigned int));
00964 static unw_rec_list *output_spill_sprel PARAMS ((unsigned int, unsigned int, unsigned int,
00965                                              unsigned int));
00966 static unw_rec_list *output_spill_reg PARAMS ((unsigned int, unsigned int, unsigned int,
00967                                            unsigned int, unsigned int));
00968 static void process_one_record PARAMS ((unw_rec_list *, vbyte_func));
00969 static void process_unw_records PARAMS ((unw_rec_list *, vbyte_func));
00970 static int calc_record_size PARAMS ((unw_rec_list *));
00971 static void set_imask PARAMS ((unw_rec_list *, unsigned long, unsigned long, unsigned int));
00972 static unsigned long slot_index PARAMS ((unsigned long, fragS *,
00973                                     unsigned long, fragS *,
00974                                     int));
00975 static unw_rec_list *optimize_unw_records PARAMS ((unw_rec_list *));
00976 static void fixup_unw_records PARAMS ((unw_rec_list *, int));
00977 static int parse_predicate_and_operand PARAMS ((expressionS *, unsigned *, const char *));
00978 static void convert_expr_to_ab_reg PARAMS ((const expressionS *, unsigned int *, unsigned int *, const char *, int));
00979 static void convert_expr_to_xy_reg PARAMS ((const expressionS *, unsigned int *, unsigned int *, const char *, int));
00980 static unsigned int get_saved_prologue_count PARAMS ((unsigned long));
00981 static void save_prologue_count PARAMS ((unsigned long, unsigned int));
00982 static void free_saved_prologue_counts PARAMS ((void));
00983 
00984 /* Determine if application register REGNUM resides only in the integer
00985    unit (as opposed to the memory unit).  */
00986 static int
00987 ar_is_only_in_integer_unit (int reg)
00988 {
00989   reg -= REG_AR;
00990   return reg >= 64 && reg <= 111;
00991 }
00992 
00993 /* Determine if application register REGNUM resides only in the memory 
00994    unit (as opposed to the integer unit).  */
00995 static int
00996 ar_is_only_in_memory_unit (int reg)
00997 {
00998   reg -= REG_AR;
00999   return reg >= 0 && reg <= 47;
01000 }
01001 
01002 /* Switch to section NAME and create section if necessary.  It's
01003    rather ugly that we have to manipulate input_line_pointer but I
01004    don't see any other way to accomplish the same thing without
01005    changing obj-elf.c (which may be the Right Thing, in the end).  */
01006 static void
01007 set_section (name)
01008      char *name;
01009 {
01010   char *saved_input_line_pointer;
01011 
01012   saved_input_line_pointer = input_line_pointer;
01013   input_line_pointer = name;
01014   obj_elf_section (0);
01015   input_line_pointer = saved_input_line_pointer;
01016 }
01017 
01018 /* Map 's' to SHF_IA_64_SHORT.  */
01019 
01020 int
01021 ia64_elf_section_letter (letter, ptr_msg)
01022      int letter;
01023      char **ptr_msg;
01024 {
01025   if (letter == 's')
01026     return SHF_IA_64_SHORT;
01027   else if (letter == 'o')
01028     return SHF_LINK_ORDER;
01029 
01030   *ptr_msg = _("Bad .section directive: want a,o,s,w,x,M,S,G,T in string");
01031   return -1;
01032 }
01033 
01034 /* Map SHF_IA_64_SHORT to SEC_SMALL_DATA.  */
01035 
01036 flagword
01037 ia64_elf_section_flags (flags, attr, type)
01038      flagword flags;
01039      int attr, type ATTRIBUTE_UNUSED;
01040 {
01041   if (attr & SHF_IA_64_SHORT)
01042     flags |= SEC_SMALL_DATA;
01043   return flags;
01044 }
01045 
01046 int
01047 ia64_elf_section_type (str, len)
01048      const char *str;
01049      size_t len;
01050 {
01051 #define STREQ(s) ((len == sizeof (s) - 1) && (strncmp (str, s, sizeof (s) - 1) == 0))
01052 
01053   if (STREQ (ELF_STRING_ia64_unwind_info))
01054     return SHT_PROGBITS;
01055 
01056   if (STREQ (ELF_STRING_ia64_unwind_info_once))
01057     return SHT_PROGBITS;
01058 
01059   if (STREQ (ELF_STRING_ia64_unwind))
01060     return SHT_IA_64_UNWIND;
01061 
01062   if (STREQ (ELF_STRING_ia64_unwind_once))
01063     return SHT_IA_64_UNWIND;
01064 
01065   if (STREQ ("unwind"))
01066     return SHT_IA_64_UNWIND;
01067 
01068   return -1;
01069 #undef STREQ
01070 }
01071 
01072 static unsigned int
01073 set_regstack (ins, locs, outs, rots)
01074      unsigned int ins, locs, outs, rots;
01075 {
01076   /* Size of frame.  */
01077   unsigned int sof;
01078 
01079   sof = ins + locs + outs;
01080   if (sof > 96)
01081     {
01082       as_bad ("Size of frame exceeds maximum of 96 registers");
01083       return 0;
01084     }
01085   if (rots > sof)
01086     {
01087       as_warn ("Size of rotating registers exceeds frame size");
01088       return 0;
01089     }
01090   md.in.base = REG_GR + 32;
01091   md.loc.base = md.in.base + ins;
01092   md.out.base = md.loc.base + locs;
01093 
01094   md.in.num_regs  = ins;
01095   md.loc.num_regs = locs;
01096   md.out.num_regs = outs;
01097   md.rot.num_regs = rots;
01098   return sof;
01099 }
01100 
01101 void
01102 ia64_flush_insns ()
01103 {
01104   struct label_fix *lfix;
01105   segT saved_seg;
01106   subsegT saved_subseg;
01107   unw_rec_list *ptr;
01108   bfd_boolean mark;
01109 
01110   if (!md.last_text_seg)
01111     return;
01112 
01113   saved_seg = now_seg;
01114   saved_subseg = now_subseg;
01115 
01116   subseg_set (md.last_text_seg, 0);
01117 
01118   while (md.num_slots_in_use > 0)
01119     emit_one_bundle ();            /* force out queued instructions */
01120 
01121   /* In case there are labels following the last instruction, resolve
01122      those now.  */
01123   mark = FALSE;
01124   for (lfix = CURR_SLOT.label_fixups; lfix; lfix = lfix->next)
01125     {
01126       symbol_set_value_now (lfix->sym);
01127       mark |= lfix->dw2_mark_labels;
01128     }
01129   if (mark)
01130     {
01131       dwarf2_where (&CURR_SLOT.debug_line);
01132       CURR_SLOT.debug_line.flags |= DWARF2_FLAG_BASIC_BLOCK;
01133       dwarf2_gen_line_info (frag_now_fix (), &CURR_SLOT.debug_line);
01134     }
01135   CURR_SLOT.label_fixups = 0;
01136 
01137   for (lfix = CURR_SLOT.tag_fixups; lfix; lfix = lfix->next)
01138     symbol_set_value_now (lfix->sym);
01139   CURR_SLOT.tag_fixups = 0;
01140 
01141   /* In case there are unwind directives following the last instruction,
01142      resolve those now.  We only handle prologue, body, and endp directives
01143      here.  Give an error for others.  */
01144   for (ptr = unwind.current_entry; ptr; ptr = ptr->next)
01145     {
01146       switch (ptr->r.type)
01147        {
01148        case prologue:
01149        case prologue_gr:
01150        case body:
01151        case endp:
01152          ptr->slot_number = (unsigned long) frag_more (0);
01153          ptr->slot_frag = frag_now;
01154          break;
01155 
01156          /* Allow any record which doesn't have a "t" field (i.e.,
01157             doesn't relate to a particular instruction).  */
01158        case unwabi:
01159        case br_gr:
01160        case copy_state:
01161        case fr_mem:
01162        case frgr_mem:
01163        case gr_gr:
01164        case gr_mem:
01165        case label_state:
01166        case rp_br:
01167        case spill_base:
01168        case spill_mask:
01169          /* nothing */
01170          break;
01171 
01172        default:
01173          as_bad (_("Unwind directive not followed by an instruction."));
01174          break;
01175        }
01176     }
01177   unwind.current_entry = NULL;
01178 
01179   subseg_set (saved_seg, saved_subseg);
01180 
01181   if (md.qp.X_op == O_register)
01182     as_bad ("qualifying predicate not followed by instruction");
01183 }
01184 
01185 static void
01186 ia64_do_align (int nbytes)
01187 {
01188   char *saved_input_line_pointer = input_line_pointer;
01189 
01190   input_line_pointer = "";
01191   s_align_bytes (nbytes);
01192   input_line_pointer = saved_input_line_pointer;
01193 }
01194 
01195 void
01196 ia64_cons_align (nbytes)
01197      int nbytes;
01198 {
01199   if (md.auto_align)
01200     {
01201       char *saved_input_line_pointer = input_line_pointer;
01202       input_line_pointer = "";
01203       s_align_bytes (nbytes);
01204       input_line_pointer = saved_input_line_pointer;
01205     }
01206 }
01207 
01208 /* Output COUNT bytes to a memory location.  */
01209 static char *vbyte_mem_ptr = NULL;
01210 
01211 void
01212 output_vbyte_mem (count, ptr, comment)
01213      int count;
01214      char *ptr;
01215      char *comment ATTRIBUTE_UNUSED;
01216 {
01217   int x;
01218   if (vbyte_mem_ptr == NULL)
01219     abort ();
01220 
01221   if (count == 0)
01222     return;
01223   for (x = 0; x < count; x++)
01224     *(vbyte_mem_ptr++) = ptr[x];
01225 }
01226 
01227 /* Count the number of bytes required for records.  */
01228 static int vbyte_count = 0;
01229 void
01230 count_output (count, ptr, comment)
01231      int count;
01232      char *ptr ATTRIBUTE_UNUSED;
01233      char *comment ATTRIBUTE_UNUSED;
01234 {
01235   vbyte_count += count;
01236 }
01237 
01238 static void
01239 output_R1_format (f, rtype, rlen)
01240      vbyte_func f;
01241      unw_record_type rtype;
01242      int rlen;
01243 {
01244   int r = 0;
01245   char byte;
01246   if (rlen > 0x1f)
01247     {
01248       output_R3_format (f, rtype, rlen);
01249       return;
01250     }
01251 
01252   if (rtype == body)
01253     r = 1;
01254   else if (rtype != prologue)
01255     as_bad ("record type is not valid");
01256 
01257   byte = UNW_R1 | (r << 5) | (rlen & 0x1f);
01258   (*f) (1, &byte, NULL);
01259 }
01260 
01261 static void
01262 output_R2_format (f, mask, grsave, rlen)
01263      vbyte_func f;
01264      int mask, grsave;
01265      unsigned long rlen;
01266 {
01267   char bytes[20];
01268   int count = 2;
01269   mask = (mask & 0x0f);
01270   grsave = (grsave & 0x7f);
01271 
01272   bytes[0] = (UNW_R2 | (mask >> 1));
01273   bytes[1] = (((mask & 0x01) << 7) | grsave);
01274   count += output_leb128 (bytes + 2, rlen, 0);
01275   (*f) (count, bytes, NULL);
01276 }
01277 
01278 static void
01279 output_R3_format (f, rtype, rlen)
01280      vbyte_func f;
01281      unw_record_type rtype;
01282      unsigned long rlen;
01283 {
01284   int r = 0, count;
01285   char bytes[20];
01286   if (rlen <= 0x1f)
01287     {
01288       output_R1_format (f, rtype, rlen);
01289       return;
01290     }
01291 
01292   if (rtype == body)
01293     r = 1;
01294   else if (rtype != prologue)
01295     as_bad ("record type is not valid");
01296   bytes[0] = (UNW_R3 | r);
01297   count = output_leb128 (bytes + 1, rlen, 0);
01298   (*f) (count + 1, bytes, NULL);
01299 }
01300 
01301 static void
01302 output_P1_format (f, brmask)
01303      vbyte_func f;
01304      int brmask;
01305 {
01306   char byte;
01307   byte = UNW_P1 | (brmask & 0x1f);
01308   (*f) (1, &byte, NULL);
01309 }
01310 
01311 static void
01312 output_P2_format (f, brmask, gr)
01313      vbyte_func f;
01314      int brmask;
01315      int gr;
01316 {
01317   char bytes[2];
01318   brmask = (brmask & 0x1f);
01319   bytes[0] = UNW_P2 | (brmask >> 1);
01320   bytes[1] = (((brmask & 1) << 7) | gr);
01321   (*f) (2, bytes, NULL);
01322 }
01323 
01324 static void
01325 output_P3_format (f, rtype, reg)
01326      vbyte_func f;
01327      unw_record_type rtype;
01328      int reg;
01329 {
01330   char bytes[2];
01331   int r = 0;
01332   reg = (reg & 0x7f);
01333   switch (rtype)
01334     {
01335     case psp_gr:
01336       r = 0;
01337       break;
01338     case rp_gr:
01339       r = 1;
01340       break;
01341     case pfs_gr:
01342       r = 2;
01343       break;
01344     case preds_gr:
01345       r = 3;
01346       break;
01347     case unat_gr:
01348       r = 4;
01349       break;
01350     case lc_gr:
01351       r = 5;
01352       break;
01353     case rp_br:
01354       r = 6;
01355       break;
01356     case rnat_gr:
01357       r = 7;
01358       break;
01359     case bsp_gr:
01360       r = 8;
01361       break;
01362     case bspstore_gr:
01363       r = 9;
01364       break;
01365     case fpsr_gr:
01366       r = 10;
01367       break;
01368     case priunat_gr:
01369       r = 11;
01370       break;
01371     default:
01372       as_bad ("Invalid record type for P3 format.");
01373     }
01374   bytes[0] = (UNW_P3 | (r >> 1));
01375   bytes[1] = (((r & 1) << 7) | reg);
01376   (*f) (2, bytes, NULL);
01377 }
01378 
01379 static void
01380 output_P4_format (f, imask, imask_size)
01381      vbyte_func f;
01382      unsigned char *imask;
01383      unsigned long imask_size;
01384 {
01385   imask[0] = UNW_P4;
01386   (*f) (imask_size, (char *) imask, NULL);
01387 }
01388 
01389 static void
01390 output_P5_format (f, grmask, frmask)
01391      vbyte_func f;
01392      int grmask;
01393      unsigned long frmask;
01394 {
01395   char bytes[4];
01396   grmask = (grmask & 0x0f);
01397 
01398   bytes[0] = UNW_P5;
01399   bytes[1] = ((grmask << 4) | ((frmask & 0x000f0000) >> 16));
01400   bytes[2] = ((frmask & 0x0000ff00) >> 8);
01401   bytes[3] = (frmask & 0x000000ff);
01402   (*f) (4, bytes, NULL);
01403 }
01404 
01405 static void
01406 output_P6_format (f, rtype, rmask)
01407      vbyte_func f;
01408      unw_record_type rtype;
01409      int rmask;
01410 {
01411   char byte;
01412   int r = 0;
01413 
01414   if (rtype == gr_mem)
01415     r = 1;
01416   else if (rtype != fr_mem)
01417     as_bad ("Invalid record type for format P6");
01418   byte = (UNW_P6 | (r << 4) | (rmask & 0x0f));
01419   (*f) (1, &byte, NULL);
01420 }
01421 
01422 static void
01423 output_P7_format (f, rtype, w1, w2)
01424      vbyte_func f;
01425      unw_record_type rtype;
01426      unsigned long w1;
01427      unsigned long w2;
01428 {
01429   char bytes[20];
01430   int count = 1;
01431   int r = 0;
01432   count += output_leb128 (bytes + 1, w1, 0);
01433   switch (rtype)
01434     {
01435     case mem_stack_f:
01436       r = 0;
01437       count += output_leb128 (bytes + count, w2 >> 4, 0);
01438       break;
01439     case mem_stack_v:
01440       r = 1;
01441       break;
01442     case spill_base:
01443       r = 2;
01444       break;
01445     case psp_sprel:
01446       r = 3;
01447       break;
01448     case rp_when:
01449       r = 4;
01450       break;
01451     case rp_psprel:
01452       r = 5;
01453       break;
01454     case pfs_when:
01455       r = 6;
01456       break;
01457     case pfs_psprel:
01458       r = 7;
01459       break;
01460     case preds_when:
01461       r = 8;
01462       break;
01463     case preds_psprel:
01464       r = 9;
01465       break;
01466     case lc_when:
01467       r = 10;
01468       break;
01469     case lc_psprel:
01470       r = 11;
01471       break;
01472     case unat_when:
01473       r = 12;
01474       break;
01475     case unat_psprel:
01476       r = 13;
01477       break;
01478     case fpsr_when:
01479       r = 14;
01480       break;
01481     case fpsr_psprel:
01482       r = 15;
01483       break;
01484     default:
01485       break;
01486     }
01487   bytes[0] = (UNW_P7 | r);
01488   (*f) (count, bytes, NULL);
01489 }
01490 
01491 static void
01492 output_P8_format (f, rtype, t)
01493      vbyte_func f;
01494      unw_record_type rtype;
01495      unsigned long t;
01496 {
01497   char bytes[20];
01498   int r = 0;
01499   int count = 2;
01500   bytes[0] = UNW_P8;
01501   switch (rtype)
01502     {
01503     case rp_sprel:
01504       r = 1;
01505       break;
01506     case pfs_sprel:
01507       r = 2;
01508       break;
01509     case preds_sprel:
01510       r = 3;
01511       break;
01512     case lc_sprel:
01513       r = 4;
01514       break;
01515     case unat_sprel:
01516       r = 5;
01517       break;
01518     case fpsr_sprel:
01519       r = 6;
01520       break;
01521     case bsp_when:
01522       r = 7;
01523       break;
01524     case bsp_psprel:
01525       r = 8;
01526       break;
01527     case bsp_sprel:
01528       r = 9;
01529       break;
01530     case bspstore_when:
01531       r = 10;
01532       break;
01533     case bspstore_psprel:
01534       r = 11;
01535       break;
01536     case bspstore_sprel:
01537       r = 12;
01538       break;
01539     case rnat_when:
01540       r = 13;
01541       break;
01542     case rnat_psprel:
01543       r = 14;
01544       break;
01545     case rnat_sprel:
01546       r = 15;
01547       break;
01548     case priunat_when_gr:
01549       r = 16;
01550       break;
01551     case priunat_psprel:
01552       r = 17;
01553       break;
01554     case priunat_sprel:
01555       r = 18;
01556       break;
01557     case priunat_when_mem:
01558       r = 19;
01559       break;
01560     default:
01561       break;
01562     }
01563   bytes[1] = r;
01564   count += output_leb128 (bytes + 2, t, 0);
01565   (*f) (count, bytes, NULL);
01566 }
01567 
01568 static void
01569 output_P9_format (f, grmask, gr)
01570      vbyte_func f;
01571      int grmask;
01572      int gr;
01573 {
01574   char bytes[3];
01575   bytes[0] = UNW_P9;
01576   bytes[1] = (grmask & 0x0f);
01577   bytes[2] = (gr & 0x7f);
01578   (*f) (3, bytes, NULL);
01579 }
01580 
01581 static void
01582 output_P10_format (f, abi, context)
01583      vbyte_func f;
01584      int abi;
01585      int context;
01586 {
01587   char bytes[3];
01588   bytes[0] = UNW_P10;
01589   bytes[1] = (abi & 0xff);
01590   bytes[2] = (context & 0xff);
01591   (*f) (3, bytes, NULL);
01592 }
01593 
01594 static void
01595 output_B1_format (f, rtype, label)
01596      vbyte_func f;
01597      unw_record_type rtype;
01598      unsigned long label;
01599 {
01600   char byte;
01601   int r = 0;
01602   if (label > 0x1f)
01603     {
01604       output_B4_format (f, rtype, label);
01605       return;
01606     }
01607   if (rtype == copy_state)
01608     r = 1;
01609   else if (rtype != label_state)
01610     as_bad ("Invalid record type for format B1");
01611 
01612   byte = (UNW_B1 | (r << 5) | (label & 0x1f));
01613   (*f) (1, &byte, NULL);
01614 }
01615 
01616 static void
01617 output_B2_format (f, ecount, t)
01618      vbyte_func f;
01619      unsigned long ecount;
01620      unsigned long t;
01621 {
01622   char bytes[20];
01623   int count = 1;
01624   if (ecount > 0x1f)
01625     {
01626       output_B3_format (f, ecount, t);
01627       return;
01628     }
01629   bytes[0] = (UNW_B2 | (ecount & 0x1f));
01630   count += output_leb128 (bytes + 1, t, 0);
01631   (*f) (count, bytes, NULL);
01632 }
01633 
01634 static void
01635 output_B3_format (f, ecount, t)
01636      vbyte_func f;
01637      unsigned long ecount;
01638      unsigned long t;
01639 {
01640   char bytes[20];
01641   int count = 1;
01642   if (ecount <= 0x1f)
01643     {
01644       output_B2_format (f, ecount, t);
01645       return;
01646     }
01647   bytes[0] = UNW_B3;
01648   count += output_leb128 (bytes + 1, t, 0);
01649   count += output_leb128 (bytes + count, ecount, 0);
01650   (*f) (count, bytes, NULL);
01651 }
01652 
01653 static void
01654 output_B4_format (f, rtype, label)
01655      vbyte_func f;
01656      unw_record_type rtype;
01657      unsigned long label;
01658 {
01659   char bytes[20];
01660   int r = 0;
01661   int count = 1;
01662   if (label <= 0x1f)
01663     {
01664       output_B1_format (f, rtype, label);
01665       return;
01666     }
01667 
01668   if (rtype == copy_state)
01669     r = 1;
01670   else if (rtype != label_state)
01671     as_bad ("Invalid record type for format B1");
01672 
01673   bytes[0] = (UNW_B4 | (r << 3));
01674   count += output_leb128 (bytes + 1, label, 0);
01675   (*f) (count, bytes, NULL);
01676 }
01677 
01678 static char
01679 format_ab_reg (ab, reg)
01680      int ab;
01681      int reg;
01682 {
01683   int ret;
01684   ab = (ab & 3);
01685   reg = (reg & 0x1f);
01686   ret = (ab << 5) | reg;
01687   return ret;
01688 }
01689 
01690 static void
01691 output_X1_format (f, rtype, ab, reg, t, w1)
01692      vbyte_func f;
01693      unw_record_type rtype;
01694      int ab, reg;
01695      unsigned long t;
01696      unsigned long w1;
01697 {
01698   char bytes[20];
01699   int r = 0;
01700   int count = 2;
01701   bytes[0] = UNW_X1;
01702 
01703   if (rtype == spill_sprel)
01704     r = 1;
01705   else if (rtype != spill_psprel)
01706     as_bad ("Invalid record type for format X1");
01707   bytes[1] = ((r << 7) | format_ab_reg (ab, reg));
01708   count += output_leb128 (bytes + 2, t, 0);
01709   count += output_leb128 (bytes + count, w1, 0);
01710   (*f) (count, bytes, NULL);
01711 }
01712 
01713 static void
01714 output_X2_format (f, ab, reg, x, y, treg, t)
01715      vbyte_func f;
01716      int ab, reg;
01717      int x, y, treg;
01718      unsigned long t;
01719 {
01720   char bytes[20];
01721   int count = 3;
01722   bytes[0] = UNW_X2;
01723   bytes[1] = (((x & 1) << 7) | format_ab_reg (ab, reg));
01724   bytes[2] = (((y & 1) << 7) | (treg & 0x7f));
01725   count += output_leb128 (bytes + 3, t, 0);
01726   (*f) (count, bytes, NULL);
01727 }
01728 
01729 static void
01730 output_X3_format (f, rtype, qp, ab, reg, t, w1)
01731      vbyte_func f;
01732      unw_record_type rtype;
01733      int qp;
01734      int ab, reg;
01735      unsigned long t;
01736      unsigned long w1;
01737 {
01738   char bytes[20];
01739   int r = 0;
01740   int count = 3;
01741   bytes[0] = UNW_X3;
01742 
01743   if (rtype == spill_sprel_p)
01744     r = 1;
01745   else if (rtype != spill_psprel_p)
01746     as_bad ("Invalid record type for format X3");
01747   bytes[1] = ((r << 7) | (qp & 0x3f));
01748   bytes[2] = format_ab_reg (ab, reg);
01749   count += output_leb128 (bytes + 3, t, 0);
01750   count += output_leb128 (bytes + count, w1, 0);
01751   (*f) (count, bytes, NULL);
01752 }
01753 
01754 static void
01755 output_X4_format (f, qp, ab, reg, x, y, treg, t)
01756      vbyte_func f;
01757      int qp;
01758      int ab, reg;
01759      int x, y, treg;
01760      unsigned long t;
01761 {
01762   char bytes[20];
01763   int count = 4;
01764   bytes[0] = UNW_X4;
01765   bytes[1] = (qp & 0x3f);
01766   bytes[2] = (((x & 1) << 7) | format_ab_reg (ab, reg));
01767   bytes[3] = (((y & 1) << 7) | (treg & 0x7f));
01768   count += output_leb128 (bytes + 4, t, 0);
01769   (*f) (count, bytes, NULL);
01770 }
01771 
01772 /* This function checks whether there are any outstanding .save-s and
01773    discards them if so.  */
01774 
01775 static void
01776 check_pending_save (void)
01777 {
01778   if (unwind.pending_saves)
01779     {
01780       unw_rec_list *cur, *prev;
01781 
01782       as_warn ("Previous .save incomplete");
01783       for (cur = unwind.list, prev = NULL; cur; )
01784        if (&cur->r.record.p == unwind.pending_saves)
01785          {
01786            if (prev)
01787              prev->next = cur->next;
01788            else
01789              unwind.list = cur->next;
01790            if (cur == unwind.tail)
01791              unwind.tail = prev;
01792            if (cur == unwind.current_entry)
01793              unwind.current_entry = cur->next;
01794            /* Don't free the first discarded record, it's being used as
01795               terminator for (currently) br_gr and gr_gr processing, and
01796               also prevents leaving a dangling pointer to it in its
01797               predecessor.  */
01798            cur->r.record.p.grmask = 0;
01799            cur->r.record.p.brmask = 0;
01800            cur->r.record.p.frmask = 0;
01801            prev = cur->r.record.p.next;
01802            cur->r.record.p.next = NULL;
01803            cur = prev;
01804            break;
01805          }
01806        else
01807          {
01808            prev = cur;
01809            cur = cur->next;
01810          }
01811       while (cur)
01812        {
01813          prev = cur;
01814          cur = cur->r.record.p.next;
01815          free (prev);
01816        }
01817       unwind.pending_saves = NULL;
01818     }
01819 }
01820 
01821 /* This function allocates a record list structure, and initializes fields.  */
01822 
01823 static unw_rec_list *
01824 alloc_record (unw_record_type t)
01825 {
01826   unw_rec_list *ptr;
01827   ptr = xmalloc (sizeof (*ptr));
01828   memset (ptr, 0, sizeof (*ptr));
01829   ptr->slot_number = SLOT_NUM_NOT_SET;
01830   ptr->r.type = t;
01831   return ptr;
01832 }
01833 
01834 /* Dummy unwind record used for calculating the length of the last prologue or
01835    body region.  */
01836 
01837 static unw_rec_list *
01838 output_endp ()
01839 {
01840   unw_rec_list *ptr = alloc_record (endp);
01841   return ptr;
01842 }
01843 
01844 static unw_rec_list *
01845 output_prologue ()
01846 {
01847   unw_rec_list *ptr = alloc_record (prologue);
01848   memset (&ptr->r.record.r.mask, 0, sizeof (ptr->r.record.r.mask));
01849   return ptr;
01850 }
01851 
01852 static unw_rec_list *
01853 output_prologue_gr (saved_mask, reg)
01854      unsigned int saved_mask;
01855      unsigned int reg;
01856 {
01857   unw_rec_list *ptr = alloc_record (prologue_gr);
01858   memset (&ptr->r.record.r.mask, 0, sizeof (ptr->r.record.r.mask));
01859   ptr->r.record.r.grmask = saved_mask;
01860   ptr->r.record.r.grsave = reg;
01861   return ptr;
01862 }
01863 
01864 static unw_rec_list *
01865 output_body ()
01866 {
01867   unw_rec_list *ptr = alloc_record (body);
01868   return ptr;
01869 }
01870 
01871 static unw_rec_list *
01872 output_mem_stack_f (size)
01873      unsigned int size;
01874 {
01875   unw_rec_list *ptr = alloc_record (mem_stack_f);
01876   ptr->r.record.p.size = size;
01877   return ptr;
01878 }
01879 
01880 static unw_rec_list *
01881 output_mem_stack_v ()
01882 {
01883   unw_rec_list *ptr = alloc_record (mem_stack_v);
01884   return ptr;
01885 }
01886 
01887 static unw_rec_list *
01888 output_psp_gr (gr)
01889      unsigned int gr;
01890 {
01891   unw_rec_list *ptr = alloc_record (psp_gr);
01892   ptr->r.record.p.r.gr = gr;
01893   return ptr;
01894 }
01895 
01896 static unw_rec_list *
01897 output_psp_sprel (offset)
01898      unsigned int offset;
01899 {
01900   unw_rec_list *ptr = alloc_record (psp_sprel);
01901   ptr->r.record.p.off.sp = offset / 4;
01902   return ptr;
01903 }
01904 
01905 static unw_rec_list *
01906 output_rp_when ()
01907 {
01908   unw_rec_list *ptr = alloc_record (rp_when);
01909   return ptr;
01910 }
01911 
01912 static unw_rec_list *
01913 output_rp_gr (gr)
01914      unsigned int gr;
01915 {
01916   unw_rec_list *ptr = alloc_record (rp_gr);
01917   ptr->r.record.p.r.gr = gr;
01918   return ptr;
01919 }
01920 
01921 static unw_rec_list *
01922 output_rp_br (br)
01923      unsigned int br;
01924 {
01925   unw_rec_list *ptr = alloc_record (rp_br);
01926   ptr->r.record.p.r.br = br;
01927   return ptr;
01928 }
01929 
01930 static unw_rec_list *
01931 output_rp_psprel (offset)
01932      unsigned int offset;
01933 {
01934   unw_rec_list *ptr = alloc_record (rp_psprel);
01935   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
01936   return ptr;
01937 }
01938 
01939 static unw_rec_list *
01940 output_rp_sprel (offset)
01941      unsigned int offset;
01942 {
01943   unw_rec_list *ptr = alloc_record (rp_sprel);
01944   ptr->r.record.p.off.sp = offset / 4;
01945   return ptr;
01946 }
01947 
01948 static unw_rec_list *
01949 output_pfs_when ()
01950 {
01951   unw_rec_list *ptr = alloc_record (pfs_when);
01952   return ptr;
01953 }
01954 
01955 static unw_rec_list *
01956 output_pfs_gr (gr)
01957      unsigned int gr;
01958 {
01959   unw_rec_list *ptr = alloc_record (pfs_gr);
01960   ptr->r.record.p.r.gr = gr;
01961   return ptr;
01962 }
01963 
01964 static unw_rec_list *
01965 output_pfs_psprel (offset)
01966      unsigned int offset;
01967 {
01968   unw_rec_list *ptr = alloc_record (pfs_psprel);
01969   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
01970   return ptr;
01971 }
01972 
01973 static unw_rec_list *
01974 output_pfs_sprel (offset)
01975      unsigned int offset;
01976 {
01977   unw_rec_list *ptr = alloc_record (pfs_sprel);
01978   ptr->r.record.p.off.sp = offset / 4;
01979   return ptr;
01980 }
01981 
01982 static unw_rec_list *
01983 output_preds_when ()
01984 {
01985   unw_rec_list *ptr = alloc_record (preds_when);
01986   return ptr;
01987 }
01988 
01989 static unw_rec_list *
01990 output_preds_gr (gr)
01991      unsigned int gr;
01992 {
01993   unw_rec_list *ptr = alloc_record (preds_gr);
01994   ptr->r.record.p.r.gr = gr;
01995   return ptr;
01996 }
01997 
01998 static unw_rec_list *
01999 output_preds_psprel (offset)
02000      unsigned int offset;
02001 {
02002   unw_rec_list *ptr = alloc_record (preds_psprel);
02003   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
02004   return ptr;
02005 }
02006 
02007 static unw_rec_list *
02008 output_preds_sprel (offset)
02009      unsigned int offset;
02010 {
02011   unw_rec_list *ptr = alloc_record (preds_sprel);
02012   ptr->r.record.p.off.sp = offset / 4;
02013   return ptr;
02014 }
02015 
02016 static unw_rec_list *
02017 output_fr_mem (mask)
02018      unsigned int mask;
02019 {
02020   unw_rec_list *ptr = alloc_record (fr_mem);
02021   unw_rec_list *cur = ptr;
02022 
02023   ptr->r.record.p.frmask = mask;
02024   unwind.pending_saves = &ptr->r.record.p;
02025   for (;;)
02026     {
02027       unw_rec_list *prev = cur;
02028 
02029       /* Clear least significant set bit.  */
02030       mask &= ~(mask & (~mask + 1));
02031       if (!mask)
02032        return ptr;
02033       cur = alloc_record (fr_mem);
02034       cur->r.record.p.frmask = mask;
02035       /* Retain only least significant bit.  */
02036       prev->r.record.p.frmask ^= mask;
02037       prev->r.record.p.next = cur;
02038     }
02039 }
02040 
02041 static unw_rec_list *
02042 output_frgr_mem (gr_mask, fr_mask)
02043      unsigned int gr_mask;
02044      unsigned int fr_mask;
02045 {
02046   unw_rec_list *ptr = alloc_record (frgr_mem);
02047   unw_rec_list *cur = ptr;
02048 
02049   unwind.pending_saves = &cur->r.record.p;
02050   cur->r.record.p.frmask = fr_mask;
02051   while (fr_mask)
02052     {
02053       unw_rec_list *prev = cur;
02054 
02055       /* Clear least significant set bit.  */
02056       fr_mask &= ~(fr_mask & (~fr_mask + 1));
02057       if (!gr_mask && !fr_mask)
02058        return ptr;
02059       cur = alloc_record (frgr_mem);
02060       cur->r.record.p.frmask = fr_mask;
02061       /* Retain only least significant bit.  */
02062       prev->r.record.p.frmask ^= fr_mask;
02063       prev->r.record.p.next = cur;
02064     }
02065   cur->r.record.p.grmask = gr_mask;
02066   for (;;)
02067     {
02068       unw_rec_list *prev = cur;
02069 
02070       /* Clear least significant set bit.  */
02071       gr_mask &= ~(gr_mask & (~gr_mask + 1));
02072       if (!gr_mask)
02073        return ptr;
02074       cur = alloc_record (frgr_mem);
02075       cur->r.record.p.grmask = gr_mask;
02076       /* Retain only least significant bit.  */
02077       prev->r.record.p.grmask ^= gr_mask;
02078       prev->r.record.p.next = cur;
02079     }
02080 }
02081 
02082 static unw_rec_list *
02083 output_gr_gr (mask, reg)
02084      unsigned int mask;
02085      unsigned int reg;
02086 {
02087   unw_rec_list *ptr = alloc_record (gr_gr);
02088   unw_rec_list *cur = ptr;
02089 
02090   ptr->r.record.p.grmask = mask;
02091   ptr->r.record.p.r.gr = reg;
02092   unwind.pending_saves = &ptr->r.record.p;
02093   for (;;)
02094     {
02095       unw_rec_list *prev = cur;
02096 
02097       /* Clear least significant set bit.  */
02098       mask &= ~(mask & (~mask + 1));
02099       if (!mask)
02100        return ptr;
02101       cur = alloc_record (gr_gr);
02102       cur->r.record.p.grmask = mask;
02103       /* Indicate this record shouldn't be output.  */
02104       cur->r.record.p.r.gr = REG_NUM;
02105       /* Retain only least significant bit.  */
02106       prev->r.record.p.grmask ^= mask;
02107       prev->r.record.p.next = cur;
02108     }
02109 }
02110 
02111 static unw_rec_list *
02112 output_gr_mem (mask)
02113      unsigned int mask;
02114 {
02115   unw_rec_list *ptr = alloc_record (gr_mem);
02116   unw_rec_list *cur = ptr;
02117 
02118   ptr->r.record.p.grmask = mask;
02119   unwind.pending_saves = &ptr->r.record.p;
02120   for (;;)
02121     {
02122       unw_rec_list *prev = cur;
02123 
02124       /* Clear least significant set bit.  */
02125       mask &= ~(mask & (~mask + 1));
02126       if (!mask)
02127        return ptr;
02128       cur = alloc_record (gr_mem);
02129       cur->r.record.p.grmask = mask;
02130       /* Retain only least significant bit.  */
02131       prev->r.record.p.grmask ^= mask;
02132       prev->r.record.p.next = cur;
02133     }
02134 }
02135 
02136 static unw_rec_list *
02137 output_br_mem (unsigned int mask)
02138 {
02139   unw_rec_list *ptr = alloc_record (br_mem);
02140   unw_rec_list *cur = ptr;
02141 
02142   ptr->r.record.p.brmask = mask;
02143   unwind.pending_saves = &ptr->r.record.p;
02144   for (;;)
02145     {
02146       unw_rec_list *prev = cur;
02147 
02148       /* Clear least significant set bit.  */
02149       mask &= ~(mask & (~mask + 1));
02150       if (!mask)
02151        return ptr;
02152       cur = alloc_record (br_mem);
02153       cur->r.record.p.brmask = mask;
02154       /* Retain only least significant bit.  */
02155       prev->r.record.p.brmask ^= mask;
02156       prev->r.record.p.next = cur;
02157     }
02158 }
02159 
02160 static unw_rec_list *
02161 output_br_gr (mask, reg)
02162      unsigned int mask;
02163      unsigned int reg;
02164 {
02165   unw_rec_list *ptr = alloc_record (br_gr);
02166   unw_rec_list *cur = ptr;
02167 
02168   ptr->r.record.p.brmask = mask;
02169   ptr->r.record.p.r.gr = reg;
02170   unwind.pending_saves = &ptr->r.record.p;
02171   for (;;)
02172     {
02173       unw_rec_list *prev = cur;
02174 
02175       /* Clear least significant set bit.  */
02176       mask &= ~(mask & (~mask + 1));
02177       if (!mask)
02178        return ptr;
02179       cur = alloc_record (br_gr);
02180       cur->r.record.p.brmask = mask;
02181       /* Indicate this record shouldn't be output.  */
02182       cur->r.record.p.r.gr = REG_NUM;
02183       /* Retain only least significant bit.  */
02184       prev->r.record.p.brmask ^= mask;
02185       prev->r.record.p.next = cur;
02186     }
02187 }
02188 
02189 static unw_rec_list *
02190 output_spill_base (offset)
02191      unsigned int offset;
02192 {
02193   unw_rec_list *ptr = alloc_record (spill_base);
02194   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
02195   return ptr;
02196 }
02197 
02198 static unw_rec_list *
02199 output_unat_when ()
02200 {
02201   unw_rec_list *ptr = alloc_record (unat_when);
02202   return ptr;
02203 }
02204 
02205 static unw_rec_list *
02206 output_unat_gr (gr)
02207      unsigned int gr;
02208 {
02209   unw_rec_list *ptr = alloc_record (unat_gr);
02210   ptr->r.record.p.r.gr = gr;
02211   return ptr;
02212 }
02213 
02214 static unw_rec_list *
02215 output_unat_psprel (offset)
02216      unsigned int offset;
02217 {
02218   unw_rec_list *ptr = alloc_record (unat_psprel);
02219   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
02220   return ptr;
02221 }
02222 
02223 static unw_rec_list *
02224 output_unat_sprel (offset)
02225      unsigned int offset;
02226 {
02227   unw_rec_list *ptr = alloc_record (unat_sprel);
02228   ptr->r.record.p.off.sp = offset / 4;
02229   return ptr;
02230 }
02231 
02232 static unw_rec_list *
02233 output_lc_when ()
02234 {
02235   unw_rec_list *ptr = alloc_record (lc_when);
02236   return ptr;
02237 }
02238 
02239 static unw_rec_list *
02240 output_lc_gr (gr)
02241      unsigned int gr;
02242 {
02243   unw_rec_list *ptr = alloc_record (lc_gr);
02244   ptr->r.record.p.r.gr = gr;
02245   return ptr;
02246 }
02247 
02248 static unw_rec_list *
02249 output_lc_psprel (offset)
02250      unsigned int offset;
02251 {
02252   unw_rec_list *ptr = alloc_record (lc_psprel);
02253   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
02254   return ptr;
02255 }
02256 
02257 static unw_rec_list *
02258 output_lc_sprel (offset)
02259      unsigned int offset;
02260 {
02261   unw_rec_list *ptr = alloc_record (lc_sprel);
02262   ptr->r.record.p.off.sp = offset / 4;
02263   return ptr;
02264 }
02265 
02266 static unw_rec_list *
02267 output_fpsr_when ()
02268 {
02269   unw_rec_list *ptr = alloc_record (fpsr_when);
02270   return ptr;
02271 }
02272 
02273 static unw_rec_list *
02274 output_fpsr_gr (gr)
02275      unsigned int gr;
02276 {
02277   unw_rec_list *ptr = alloc_record (fpsr_gr);
02278   ptr->r.record.p.r.gr = gr;
02279   return ptr;
02280 }
02281 
02282 static unw_rec_list *
02283 output_fpsr_psprel (offset)
02284      unsigned int offset;
02285 {
02286   unw_rec_list *ptr = alloc_record (fpsr_psprel);
02287   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
02288   return ptr;
02289 }
02290 
02291 static unw_rec_list *
02292 output_fpsr_sprel (offset)
02293      unsigned int offset;
02294 {
02295   unw_rec_list *ptr = alloc_record (fpsr_sprel);
02296   ptr->r.record.p.off.sp = offset / 4;
02297   return ptr;
02298 }
02299 
02300 static unw_rec_list *
02301 output_priunat_when_gr ()
02302 {
02303   unw_rec_list *ptr = alloc_record (priunat_when_gr);
02304   return ptr;
02305 }
02306 
02307 static unw_rec_list *
02308 output_priunat_when_mem ()
02309 {
02310   unw_rec_list *ptr = alloc_record (priunat_when_mem);
02311   return ptr;
02312 }
02313 
02314 static unw_rec_list *
02315 output_priunat_gr (gr)
02316      unsigned int gr;
02317 {
02318   unw_rec_list *ptr = alloc_record (priunat_gr);
02319   ptr->r.record.p.r.gr = gr;
02320   return ptr;
02321 }
02322 
02323 static unw_rec_list *
02324 output_priunat_psprel (offset)
02325      unsigned int offset;
02326 {
02327   unw_rec_list *ptr = alloc_record (priunat_psprel);
02328   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
02329   return ptr;
02330 }
02331 
02332 static unw_rec_list *
02333 output_priunat_sprel (offset)
02334      unsigned int offset;
02335 {
02336   unw_rec_list *ptr = alloc_record (priunat_sprel);
02337   ptr->r.record.p.off.sp = offset / 4;
02338   return ptr;
02339 }
02340 
02341 static unw_rec_list *
02342 output_bsp_when ()
02343 {
02344   unw_rec_list *ptr = alloc_record (bsp_when);
02345   return ptr;
02346 }
02347 
02348 static unw_rec_list *
02349 output_bsp_gr (gr)
02350      unsigned int gr;
02351 {
02352   unw_rec_list *ptr = alloc_record (bsp_gr);
02353   ptr->r.record.p.r.gr = gr;
02354   return ptr;
02355 }
02356 
02357 static unw_rec_list *
02358 output_bsp_psprel (offset)
02359      unsigned int offset;
02360 {
02361   unw_rec_list *ptr = alloc_record (bsp_psprel);
02362   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
02363   return ptr;
02364 }
02365 
02366 static unw_rec_list *
02367 output_bsp_sprel (offset)
02368      unsigned int offset;
02369 {
02370   unw_rec_list *ptr = alloc_record (bsp_sprel);
02371   ptr->r.record.p.off.sp = offset / 4;
02372   return ptr;
02373 }
02374 
02375 static unw_rec_list *
02376 output_bspstore_when ()
02377 {
02378   unw_rec_list *ptr = alloc_record (bspstore_when);
02379   return ptr;
02380 }
02381 
02382 static unw_rec_list *
02383 output_bspstore_gr (gr)
02384      unsigned int gr;
02385 {
02386   unw_rec_list *ptr = alloc_record (bspstore_gr);
02387   ptr->r.record.p.r.gr = gr;
02388   return ptr;
02389 }
02390 
02391 static unw_rec_list *
02392 output_bspstore_psprel (offset)
02393      unsigned int offset;
02394 {
02395   unw_rec_list *ptr = alloc_record (bspstore_psprel);
02396   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
02397   return ptr;
02398 }
02399 
02400 static unw_rec_list *
02401 output_bspstore_sprel (offset)
02402      unsigned int offset;
02403 {
02404   unw_rec_list *ptr = alloc_record (bspstore_sprel);
02405   ptr->r.record.p.off.sp = offset / 4;
02406   return ptr;
02407 }
02408 
02409 static unw_rec_list *
02410 output_rnat_when ()
02411 {
02412   unw_rec_list *ptr = alloc_record (rnat_when);
02413   return ptr;
02414 }
02415 
02416 static unw_rec_list *
02417 output_rnat_gr (gr)
02418      unsigned int gr;
02419 {
02420   unw_rec_list *ptr = alloc_record (rnat_gr);
02421   ptr->r.record.p.r.gr = gr;
02422   return ptr;
02423 }
02424 
02425 static unw_rec_list *
02426 output_rnat_psprel (offset)
02427      unsigned int offset;
02428 {
02429   unw_rec_list *ptr = alloc_record (rnat_psprel);
02430   ptr->r.record.p.off.psp = ENCODED_PSP_OFFSET (offset);
02431   return ptr;
02432 }
02433 
02434 static unw_rec_list *
02435 output_rnat_sprel (offset)
02436      unsigned int offset;
02437 {
02438   unw_rec_list *ptr = alloc_record (rnat_sprel);
02439   ptr->r.record.p.off.sp = offset / 4;
02440   return ptr;
02441 }
02442 
02443 static unw_rec_list *
02444 output_unwabi (abi, context)
02445      unsigned long abi;
02446      unsigned long context;
02447 {
02448   unw_rec_list *ptr = alloc_record (unwabi);
02449   ptr->r.record.p.abi = abi;
02450   ptr->r.record.p.context = context;
02451   return ptr;
02452 }
02453 
02454 static unw_rec_list *
02455 output_epilogue (unsigned long ecount)
02456 {
02457   unw_rec_list *ptr = alloc_record (epilogue);
02458   ptr->r.record.b.ecount = ecount;
02459   return ptr;
02460 }
02461 
02462 static unw_rec_list *
02463 output_label_state (unsigned long label)
02464 {
02465   unw_rec_list *ptr = alloc_record (label_state);
02466   ptr->r.record.b.label = label;
02467   return ptr;
02468 }
02469 
02470 static unw_rec_list *
02471 output_copy_state (unsigned long label)
02472 {
02473   unw_rec_list *ptr = alloc_record (copy_state);
02474   ptr->r.record.b.label = label;
02475   return ptr;
02476 }
02477 
02478 static unw_rec_list *
02479 output_spill_psprel (ab, reg, offset, predicate)
02480      unsigned int ab;
02481      unsigned int reg;
02482      unsigned int offset;
02483      unsigned int predicate;
02484 {
02485   unw_rec_list *ptr = alloc_record (predicate ? spill_psprel_p : spill_psprel);
02486   ptr->r.record.x.ab = ab;
02487   ptr->r.record.x.reg = reg;
02488   ptr->r.record.x.where.pspoff = ENCODED_PSP_OFFSET (offset);
02489   ptr->r.record.x.qp = predicate;
02490   return ptr;
02491 }
02492 
02493 static unw_rec_list *
02494 output_spill_sprel (ab, reg, offset, predicate)
02495      unsigned int ab;
02496      unsigned int reg;
02497      unsigned int offset;
02498      unsigned int predicate;
02499 {
02500   unw_rec_list *ptr = alloc_record (predicate ? spill_sprel_p : spill_sprel);
02501   ptr->r.record.x.ab = ab;
02502   ptr->r.record.x.reg = reg;
02503   ptr->r.record.x.where.spoff = offset / 4;
02504   ptr->r.record.x.qp = predicate;
02505   return ptr;
02506 }
02507 
02508 static unw_rec_list *
02509 output_spill_reg (ab, reg, targ_reg, xy, predicate)
02510      unsigned int ab;
02511      unsigned int reg;
02512      unsigned int targ_reg;
02513      unsigned int xy;
02514      unsigned int predicate;
02515 {
02516   unw_rec_list *ptr = alloc_record (predicate ? spill_reg_p : spill_reg);
02517   ptr->r.record.x.ab = ab;
02518   ptr->r.record.x.reg = reg;
02519   ptr->r.record.x.where.reg = targ_reg;
02520   ptr->r.record.x.xy = xy;
02521   ptr->r.record.x.qp = predicate;
02522   return ptr;
02523 }
02524 
02525 /* Given a unw_rec_list process the correct format with the
02526    specified function.  */
02527 
02528 static void
02529 process_one_record (ptr, f)
02530      unw_rec_list *ptr;
02531      vbyte_func f;
02532 {
02533   unsigned int fr_mask, gr_mask;
02534 
02535   switch (ptr->r.type)
02536     {
02537       /* This is a dummy record that takes up no space in the output.  */
02538     case endp:
02539       break;
02540 
02541     case gr_mem:
02542     case fr_mem:
02543     case br_mem:
02544     case frgr_mem:
02545       /* These are taken care of by prologue/prologue_gr.  */
02546       break;
02547 
02548     case prologue_gr:
02549     case prologue:
02550       if (ptr->r.type == prologue_gr)
02551        output_R2_format (f, ptr->r.record.r.grmask,
02552                        ptr->r.record.r.grsave, ptr->r.record.r.rlen);
02553       else
02554        output_R1_format (f, ptr->r.type, ptr->r.record.r.rlen);
02555 
02556       /* Output descriptor(s) for union of register spills (if any).  */
02557       gr_mask = ptr->r.record.r.mask.gr_mem;
02558       fr_mask = ptr->r.record.r.mask.fr_mem;
02559       if (fr_mask)
02560        {
02561          if ((fr_mask & ~0xfUL) == 0)
02562            output_P6_format (f, fr_mem, fr_mask);
02563          else
02564            {
02565              output_P5_format (f, gr_mask, fr_mask);
02566              gr_mask = 0;
02567            }
02568        }
02569       if (gr_mask)
02570        output_P6_format (f, gr_mem, gr_mask);
02571       if (ptr->r.record.r.mask.br_mem)
02572        output_P1_format (f, ptr->r.record.r.mask.br_mem);
02573 
02574       /* output imask descriptor if necessary:  */
02575       if (ptr->r.record.r.mask.i)
02576        output_P4_format (f, ptr->r.record.r.mask.i,
02577                        ptr->r.record.r.imask_size);
02578       break;
02579 
02580     case body:
02581       output_R1_format (f, ptr->r.type, ptr->r.record.r.rlen);
02582       break;
02583     case mem_stack_f:
02584     case mem_stack_v:
02585       output_P7_format (f, ptr->r.type, ptr->r.record.p.t,
02586                      ptr->r.record.p.size);
02587       break;
02588     case psp_gr:
02589     case rp_gr:
02590     case pfs_gr:
02591     case preds_gr:
02592     case unat_gr:
02593     case lc_gr:
02594     case fpsr_gr:
02595     case priunat_gr:
02596     case bsp_gr:
02597     case bspstore_gr:
02598     case rnat_gr:
02599       output_P3_format (f, ptr->r.type, ptr->r.record.p.r.gr);
02600       break;
02601     case rp_br:
02602       output_P3_format (f, rp_br, ptr->r.record.p.r.br);
02603       break;
02604     case psp_sprel:
02605       output_P7_format (f, psp_sprel, ptr->r.record.p.off.sp, 0);
02606       break;
02607     case rp_when:
02608     case pfs_when:
02609     case preds_when:
02610     case unat_when:
02611     case lc_when:
02612     case fpsr_when:
02613       output_P7_format (f, ptr->r.type, ptr->r.record.p.t, 0);
02614       break;
02615     case rp_psprel:
02616     case pfs_psprel:
02617     case preds_psprel:
02618     case unat_psprel:
02619     case lc_psprel:
02620     case fpsr_psprel:
02621     case spill_base:
02622       output_P7_format (f, ptr->r.type, ptr->r.record.p.off.psp, 0);
02623       break;
02624     case rp_sprel:
02625     case pfs_sprel:
02626     case preds_sprel:
02627     case unat_sprel:
02628     case lc_sprel:
02629     case fpsr_sprel:
02630     case priunat_sprel:
02631     case bsp_sprel:
02632     case bspstore_sprel:
02633     case rnat_sprel:
02634       output_P8_format (f, ptr->r.type, ptr->r.record.p.off.sp);
02635       break;
02636     case gr_gr:
02637       if (ptr->r.record.p.r.gr < REG_NUM)
02638        {
02639          const unw_rec_list *cur = ptr;
02640 
02641          gr_mask = cur->r.record.p.grmask;
02642          while ((cur = cur->r.record.p.next) != NULL)
02643            gr_mask |= cur->r.record.p.grmask;
02644          output_P9_format (f, gr_mask, ptr->r.record.p.r.gr);
02645        }
02646       break;
02647     case br_gr:
02648       if (ptr->r.record.p.r.gr < REG_NUM)
02649        {
02650          const unw_rec_list *cur = ptr;
02651 
02652          gr_mask = cur->r.record.p.brmask;
02653          while ((cur = cur->r.record.p.next) != NULL)
02654            gr_mask |= cur->r.record.p.brmask;
02655          output_P2_format (f, gr_mask, ptr->r.record.p.r.gr);
02656        }
02657       break;
02658     case spill_mask:
02659       as_bad ("spill_mask record unimplemented.");
02660       break;
02661     case priunat_when_gr:
02662     case priunat_when_mem:
02663     case bsp_when:
02664     case bspstore_when:
02665     case rnat_when:
02666       output_P8_format (f, ptr->r.type, ptr->r.record.p.t);
02667       break;
02668     case priunat_psprel:
02669     case bsp_psprel:
02670     case bspstore_psprel:
02671     case rnat_psprel:
02672       output_P8_format (f, ptr->r.type, ptr->r.record.p.off.psp);
02673       break;
02674     case unwabi:
02675       output_P10_format (f, ptr->r.record.p.abi, ptr->r.record.p.context);
02676       break;
02677     case epilogue:
02678       output_B3_format (f, ptr->r.record.b.ecount, ptr->r.record.b.t);
02679       break;
02680     case label_state:
02681     case copy_state:
02682       output_B4_format (f, ptr->r.type, ptr->r.record.b.label);
02683       break;
02684     case spill_psprel:
02685       output_X1_format (f, ptr->r.type, ptr->r.record.x.ab,
02686                      ptr->r.record.x.reg, ptr->r.record.x.t,
02687                      ptr->r.record.x.where.pspoff);
02688       break;
02689     case spill_sprel:
02690       output_X1_format (f, ptr->r.type, ptr->r.record.x.ab,
02691                      ptr->r.record.x.reg, ptr->r.record.x.t,
02692                      ptr->r.record.x.where.spoff);
02693       break;
02694     case spill_reg:
02695       output_X2_format (f, ptr->r.record.x.ab, ptr->r.record.x.reg,
02696                      ptr->r.record.x.xy >> 1, ptr->r.record.x.xy,
02697                      ptr->r.record.x.where.reg, ptr->r.record.x.t);
02698       break;
02699     case spill_psprel_p:
02700       output_X3_format (f, ptr->r.type, ptr->r.record.x.qp,
02701                      ptr->r.record.x.ab, ptr->r.record.x.reg,
02702                      ptr->r.record.x.t, ptr->r.record.x.where.pspoff);
02703       break;
02704     case spill_sprel_p:
02705       output_X3_format (f, ptr->r.type, ptr->r.record.x.qp,
02706                      ptr->r.record.x.ab, ptr->r.record.x.reg,
02707                      ptr->r.record.x.t, ptr->r.record.x.where.spoff);
02708       break;
02709     case spill_reg_p:
02710       output_X4_format (f, ptr->r.record.x.qp, ptr->r.record.x.ab,
02711                      ptr->r.record.x.reg, ptr->r.record.x.xy >> 1,
02712                      ptr->r.record.x.xy, ptr->r.record.x.where.reg,
02713                      ptr->r.record.x.t);
02714       break;
02715     default:
02716       as_bad ("record_type_not_valid");
02717       break;
02718     }
02719 }
02720 
02721 /* Given a unw_rec_list list, process all the records with
02722    the specified function.  */
02723 static void
02724 process_unw_records (list, f)
02725      unw_rec_list *list;
02726      vbyte_func f;
02727 {
02728   unw_rec_list *ptr;
02729   for (ptr = list; ptr; ptr = ptr->next)
02730     process_one_record (ptr, f);
02731 }
02732 
02733 /* Determine the size of a record list in bytes.  */
02734 static int
02735 calc_record_size (list)
02736      unw_rec_list *list;
02737 {
02738   vbyte_count = 0;
02739   process_unw_records (list, count_output);
02740   return vbyte_count;
02741 }
02742 
02743 /* Return the number of bits set in the input value.
02744    Perhaps this has a better place...  */
02745 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
02746 # define popcount __builtin_popcount
02747 #else
02748 static int
02749 popcount (unsigned x)
02750 {
02751   static const unsigned char popcnt[16] =
02752     {
02753       0, 1, 1, 2,
02754       1, 2, 2, 3,
02755       1, 2, 2, 3,
02756       2, 3, 3, 4
02757     };
02758 
02759   if (x < NELEMS (popcnt))
02760     return popcnt[x];
02761   return popcnt[x % NELEMS (popcnt)] + popcount (x / NELEMS (popcnt));
02762 }
02763 #endif
02764 
02765 /* Update IMASK bitmask to reflect the fact that one or more registers
02766    of type TYPE are saved starting at instruction with index T.  If N
02767    bits are set in REGMASK, it is assumed that instructions T through
02768    T+N-1 save these registers.
02769 
02770    TYPE values:
02771        0: no save
02772        1: instruction saves next fp reg
02773        2: instruction saves next general reg
02774        3: instruction saves next branch reg */
02775 static void
02776 set_imask (region, regmask, t, type)
02777      unw_rec_list *region;
02778      unsigned long regmask;
02779      unsigned long t;
02780      unsigned int type;
02781 {
02782   unsigned char *imask;
02783   unsigned long imask_size;
02784   unsigned int i;
02785   int pos;
02786 
02787   imask = region->r.record.r.mask.i;
02788   imask_size = region->r.record.r.imask_size;
02789   if (!imask)
02790     {
02791       imask_size = (region->r.record.r.rlen * 2 + 7) / 8 + 1;
02792       imask = xmalloc (imask_size);
02793       memset (imask, 0, imask_size);
02794 
02795       region->r.record.r.imask_size = imask_size;
02796       region->r.record.r.mask.i = imask;
02797     }
02798 
02799   i = (t / 4) + 1;
02800   pos = 2 * (3 - t % 4);
02801   while (regmask)
02802     {
02803       if (i >= imask_size)
02804        {
02805          as_bad ("Ignoring attempt to spill beyond end of region");
02806          return;
02807        }
02808 
02809       imask[i] |= (type & 0x3) << pos;
02810 
02811       regmask &= (regmask - 1);
02812       pos -= 2;
02813       if (pos < 0)
02814        {
02815          pos = 0;
02816          ++i;
02817        }
02818     }
02819 }
02820 
02821 /* Return the number of instruction slots from FIRST_ADDR to SLOT_ADDR.
02822    SLOT_FRAG is the frag containing SLOT_ADDR, and FIRST_FRAG is the frag
02823    containing FIRST_ADDR.  If BEFORE_RELAX, then we use worst-case estimates
02824    for frag sizes.  */
02825 
02826 unsigned long
02827 slot_index (slot_addr, slot_frag, first_addr, first_frag, before_relax)
02828      unsigned long slot_addr;
02829      fragS *slot_frag;
02830      unsigned long first_addr;
02831      fragS *first_frag;
02832      int before_relax;
02833 {
02834   unsigned long index = 0;
02835 
02836   /* First time we are called, the initial address and frag are invalid.  */
02837   if (first_addr == 0)
02838     return 0;
02839 
02840   /* If the two addresses are in different frags, then we need to add in
02841      the remaining size of this frag, and then the entire size of intermediate
02842      frags.  */
02843   while (slot_frag != first_frag)
02844     {
02845       unsigned long start_addr = (unsigned long) &first_frag->fr_literal;
02846 
02847       if (! before_relax)
02848        {
02849          /* We can get the final addresses only during and after
02850             relaxation.  */
02851          if (first_frag->fr_next && first_frag->fr_next->fr_address)
02852            index += 3 * ((first_frag->fr_next->fr_address
02853                         - first_frag->fr_address
02854                           - first_frag->fr_fix) >> 4);
02855        }
02856       else
02857        /* We don't know what the final addresses will be. We try our
02858           best to estimate.  */
02859        switch (first_frag->fr_type)
02860          {
02861          default:
02862            break;
02863 
02864          case rs_space:
02865            as_fatal ("only constant space allocation is supported");
02866            break;
02867 
02868          case rs_align:
02869          case rs_align_code:
02870          case rs_align_test:
02871            /* Take alignment into account.  Assume the worst case
02872               before relaxation.  */
02873            index += 3 * ((1 << first_frag->fr_offset) >> 4);
02874            break;
02875 
02876          case rs_org:
02877            if (first_frag->fr_symbol)
02878              {
02879               as_fatal ("only constant offsets are supported");
02880               break;
02881              }
02882          case rs_fill:
02883            index += 3 * (first_frag->fr_offset >> 4);
02884            break;
02885          }
02886 
02887       /* Add in the full size of the frag converted to instruction slots.  */
02888       index += 3 * (first_frag->fr_fix >> 4);
02889       /* Subtract away the initial part before first_addr.  */
02890       index -= (3 * ((first_addr >> 4) - (start_addr >> 4))
02891               + ((first_addr & 0x3) - (start_addr & 0x3)));
02892 
02893       /* Move to the beginning of the next frag.  */
02894       first_frag = first_frag->fr_next;
02895       first_addr = (unsigned long) &first_frag->fr_literal;
02896 
02897       /* This can happen if there is section switching in the middle of a
02898         function, causing the frag chain for the function to be broken.
02899         It is too difficult to recover safely from this problem, so we just
02900         exit with an error.  */
02901       if (first_frag == NULL)
02902        as_fatal ("Section switching in code is not supported.");
02903     }
02904 
02905   /* Add in the used part of the last frag.  */
02906   index += (3 * ((slot_addr >> 4) - (first_addr >> 4))
02907            + ((slot_addr & 0x3) - (first_addr & 0x3)));
02908   return index;
02909 }
02910 
02911 /* Optimize unwind record directives.  */
02912 
02913 static unw_rec_list *
02914 optimize_unw_records (list)
02915      unw_rec_list *list;
02916 {
02917   if (!list)
02918     return NULL;
02919 
02920   /* If the only unwind record is ".prologue" or ".prologue" followed
02921      by ".body", then we can optimize the unwind directives away.  */
02922   if (list->r.type == prologue
02923       && (list->next->r.type == endp
02924          || (list->next->r.type == body && list->next->next->r.type == endp)))
02925     return NULL;
02926 
02927   return list;
02928 }
02929 
02930 /* Given a complete record list, process any records which have
02931    unresolved fields, (ie length counts for a prologue).  After
02932    this has been run, all necessary information should be available
02933    within each record to generate an image.  */
02934 
02935 static void
02936 fixup_unw_records (list, before_relax)
02937      unw_rec_list *list;
02938      int before_relax;
02939 {
02940   unw_rec_list *ptr, *region = 0;
02941   unsigned long first_addr = 0, rlen = 0, t;
02942   fragS *first_frag = 0;
02943 
02944   for (ptr = list; ptr; ptr = ptr->next)
02945     {
02946       if (ptr->slot_number == SLOT_NUM_NOT_SET)
02947        as_bad (" Insn slot not set in unwind record.");
02948       t = slot_index (ptr->slot_number, ptr->slot_frag,
02949                     first_addr, first_frag, before_relax);
02950       switch (ptr->r.type)
02951        {
02952        case prologue:
02953        case prologue_gr:
02954        case body:
02955          {
02956            unw_rec_list *last;
02957            int size;
02958            unsigned long last_addr = 0;
02959            fragS *last_frag = NULL;
02960 
02961            first_addr = ptr->slot_number;
02962            first_frag = ptr->slot_frag;
02963            /* Find either the next body/prologue start, or the end of
02964               the function, and determine the size of the region.  */
02965            for (last = ptr->next; last != NULL; last = last->next)
02966              if (last->r.type == prologue || last->r.type == prologue_gr
02967                 || last->r.type == body || last->r.type == endp)
02968               {
02969                 last_addr = last->slot_number;
02970                 last_frag = last->slot_frag;
02971                 break;
02972               }
02973            size = slot_index (last_addr, last_frag, first_addr, first_frag,
02974                             before_relax);
02975            rlen = ptr->r.record.r.rlen = size;
02976            if (ptr->r.type == body)
02977              /* End of region.  */
02978              region = 0;
02979            else
02980              region = ptr;
02981            break;
02982          }
02983        case epilogue:
02984          if (t < rlen)
02985            ptr->r.record.b.t = rlen - 1 - t;
02986          else
02987            /* This happens when a memory-stack-less procedure uses a
02988               ".restore sp" directive at the end of a region to pop
02989               the frame state.  */
02990            ptr->r.record.b.t = 0;
02991          break;
02992 
02993        case mem_stack_f:
02994        case mem_stack_v:
02995        case rp_when:
02996        case pfs_when:
02997        case preds_when:
02998        case unat_when:
02999        case lc_when:
03000        case fpsr_when:
03001        case priunat_when_gr:
03002        case priunat_when_mem:
03003        case bsp_when:
03004        case bspstore_when:
03005        case rnat_when:
03006          ptr->r.record.p.t = t;
03007          break;
03008 
03009        case spill_reg:
03010        case spill_sprel:
03011        case spill_psprel:
03012        case spill_reg_p:
03013        case spill_sprel_p:
03014        case spill_psprel_p:
03015          ptr->r.record.x.t = t;
03016          break;
03017 
03018        case frgr_mem:
03019          if (!region)
03020            {
03021              as_bad ("frgr_mem record before region record!");
03022              return;
03023            }
03024          region->r.record.r.mask.fr_mem |= ptr->r.record.p.frmask;
03025          region->r.record.r.mask.gr_mem |= ptr->r.record.p.grmask;
03026          set_imask (region, ptr->r.record.p.frmask, t, 1);
03027          set_imask (region, ptr->r.record.p.grmask, t, 2);
03028          break;
03029        case fr_mem:
03030          if (!region)
03031            {
03032              as_bad ("fr_mem record before region record!");
03033              return;
03034            }
03035          region->r.record.r.mask.fr_mem |= ptr->r.record.p.frmask;
03036          set_imask (region, ptr->r.record.p.frmask, t, 1);
03037          break;
03038        case gr_mem:
03039          if (!region)
03040            {
03041              as_bad ("gr_mem record before region record!");
03042              return;
03043            }
03044          region->r.record.r.mask.gr_mem |= ptr->r.record.p.grmask;
03045          set_imask (region, ptr->r.record.p.grmask, t, 2);
03046          break;
03047        case br_mem:
03048          if (!region)
03049            {
03050              as_bad ("br_mem record before region record!");
03051              return;
03052            }
03053          region->r.record.r.mask.br_mem |= ptr->r.record.p.brmask;
03054          set_imask (region, ptr->r.record.p.brmask, t, 3);
03055          break;
03056 
03057        case gr_gr:
03058          if (!region)
03059            {
03060              as_bad ("gr_gr record before region record!");
03061              return;
03062            }
03063          set_imask (region, ptr->r.record.p.grmask, t, 2);
03064          break;
03065        case br_gr:
03066          if (!region)
03067            {
03068              as_bad ("br_gr record before region record!");
03069              return;
03070            }
03071          set_imask (region, ptr->r.record.p.brmask, t, 3);
03072          break;
03073 
03074        default:
03075          break;
03076        }
03077     }
03078 }
03079 
03080 /* Estimate the size of a frag before relaxing.  We only have one type of frag
03081    to handle here, which is the unwind info frag.  */
03082 
03083 int
03084 ia64_estimate_size_before_relax (fragS *frag,
03085                              asection *segtype ATTRIBUTE_UNUSED)
03086 {
03087   unw_rec_list *list;
03088   int len, size, pad;
03089 
03090   /* ??? This code is identical to the first part of ia64_convert_frag.  */
03091   list = (unw_rec_list *) frag->fr_opcode;
03092   fixup_unw_records (list, 0);
03093 
03094   len = calc_record_size (list);
03095   /* pad to pointer-size boundary.  */
03096   pad = len % md.pointer_size;
03097   if (pad != 0)
03098     len += md.pointer_size - pad;
03099   /* Add 8 for the header.  */
03100   size = len + 8;
03101   /* Add a pointer for the personality offset.  */
03102   if (frag->fr_offset)
03103     size += md.pointer_size;
03104 
03105   /* fr_var carries the max_chars that we created the fragment with.
03106      We must, of course, have allocated enough memory earlier.  */
03107   assert (frag->fr_var >= size);
03108 
03109   return frag->fr_fix + size;
03110 }
03111 
03112 /* This function converts a rs_machine_dependent variant frag into a
03113   normal fill frag with the unwind image from the the record list.  */
03114 void
03115 ia64_convert_frag (fragS *frag)
03116 {
03117   unw_rec_list *list;
03118   int len, size, pad;
03119   valueT flag_value;
03120 
03121   /* ??? This code is identical to ia64_estimate_size_before_relax.  */
03122   list = (unw_rec_list *) frag->fr_opcode;
03123   fixup_unw_records (list, 0);
03124 
03125   len = calc_record_size (list);
03126   /* pad to pointer-size boundary.  */
03127   pad = len % md.pointer_size;
03128   if (pad != 0)
03129     len += md.pointer_size - pad;
03130   /* Add 8 for the header.  */
03131   size = len + 8;
03132   /* Add a pointer for the personality offset.  */
03133   if (frag->fr_offset)
03134     size += md.pointer_size;
03135 
03136   /* fr_var carries the max_chars that we created the fragment with.
03137      We must, of course, have allocated enough memory earlier.  */
03138   assert (frag->fr_var >= size);
03139 
03140   /* Initialize the header area. fr_offset is initialized with
03141      unwind.personality_routine.  */
03142   if (frag->fr_offset)
03143     {
03144       if (md.flags & EF_IA_64_ABI64)
03145        flag_value = (bfd_vma) 3 << 32;
03146       else
03147        /* 32-bit unwind info block.  */
03148        flag_value = (bfd_vma) 0x1003 << 32;
03149     }
03150   else
03151     flag_value = 0;
03152 
03153  md_number_to_chars (frag->fr_literal,
03154                    (((bfd_vma) 1 << 48) /* Version.  */
03155                     | flag_value        /* U & E handler flags.  */
03156                     | (len / md.pointer_size)), /* Length.  */
03157                    8);
03158 
03159   /* Skip the header.  */
03160   vbyte_mem_ptr = frag->fr_literal + 8;
03161   process_unw_records (list, output_vbyte_mem);
03162 
03163   /* Fill the padding bytes with zeros.  */
03164   if (pad != 0)
03165     md_number_to_chars (frag->fr_literal + len + 8 - md.pointer_size + pad, 0,
03166                      md.pointer_size - pad);
03167 
03168   frag->fr_fix += size;
03169   frag->fr_type = rs_fill;
03170   frag->fr_var = 0;
03171   frag->fr_offset = 0;
03172 }
03173 
03174 static int
03175 parse_predicate_and_operand (e, qp, po)
03176      expressionS * e;
03177      unsigned * qp;
03178      const char * po;
03179 {
03180   int sep = parse_operand (e, ',');
03181 
03182   *qp = e->X_add_number - REG_P;
03183   if (e->X_op != O_register || *qp > 63)
03184     {
03185       as_bad ("First operand to .%s must be a predicate", po);
03186       *qp = 0;
03187     }
03188   else if (*qp == 0)
03189     as_warn ("Pointless use of p0 as first operand to .%s", po);
03190   if (sep == ',')
03191     sep = parse_operand (e, ',');
03192   else
03193     e->X_op = O_absent;
03194   return sep;
03195 }
03196 
03197 static void
03198 convert_expr_to_ab_reg (e, ab, regp, po, n)
03199      const expressionS *e;
03200      unsigned int *ab;
03201      unsigned int *regp;
03202      const char * po;
03203      int n;
03204 {
03205   unsigned int reg = e->X_add_number;
03206 
03207   *ab = *regp = 0; /* Anything valid is good here.  */
03208 
03209   if (e->X_op != O_register)
03210     reg = REG_GR; /* Anything invalid is good here.  */
03211 
03212   if (reg >= (REG_GR + 4) && reg <= (REG_GR + 7))
03213     {
03214       *ab = 0;
03215       *regp = reg - REG_GR;
03216     }
03217   else if ((reg >= (REG_FR + 2) && reg <= (REG_FR + 5))
03218           || (reg >= (REG_FR + 16) && reg <= (REG_FR + 31)))
03219     {
03220       *ab = 1;
03221       *regp = reg - REG_FR;
03222     }
03223   else if (reg >= (REG_BR + 1) && reg <= (REG_BR + 5))
03224     {
03225       *ab = 2;
03226       *regp = reg - REG_BR;
03227     }
03228   else
03229     {
03230       *ab = 3;
03231       switch (reg)
03232        {
03233        case REG_PR:         *regp =  0; break;
03234        case REG_PSP:        *regp =  1; break;
03235        case REG_PRIUNAT:    *regp =  2; break;
03236        case REG_BR + 0:     *regp =  3; break;
03237        case REG_AR + AR_BSP:       *regp =  4; break;
03238        case REG_AR + AR_BSPSTORE: *regp = 5; break;
03239        case REG_AR + AR_RNAT:      *regp =  6; break;
03240        case REG_AR + AR_UNAT:      *regp =  7; break;
03241        case REG_AR + AR_FPSR:      *regp =  8; break;
03242        case REG_AR + AR_PFS:       *regp =  9; break;
03243        case REG_AR + AR_LC: *regp = 10; break;
03244 
03245        default:
03246          as_bad ("Operand %d to .%s must be a preserved register", n, po);
03247          break;
03248        }
03249     }
03250 }
03251 
03252 static void
03253 convert_expr_to_xy_reg (e, xy, regp, po, n)
03254      const expressionS *e;
03255      unsigned int *xy;
03256      unsigned int *regp;
03257      const char * po;
03258      int n;
03259 {
03260   unsigned int reg = e->X_add_number;
03261 
03262   *xy = *regp = 0; /* Anything valid is good here.  */
03263 
03264   if (e->X_op != O_register)
03265     reg = REG_GR; /* Anything invalid is good here.  */
03266 
03267   if (reg >= (REG_GR + 1) && reg <= (REG_GR + 127))
03268     {
03269       *xy = 0;
03270       *regp = reg - REG_GR;
03271     }
03272   else if (reg >= (REG_FR + 2) && reg <= (REG_FR + 127))
03273     {
03274       *xy = 1;
03275       *regp = reg - REG_FR;
03276     }
03277   else if (reg >= REG_BR && reg <= (REG_BR + 7))
03278     {
03279       *xy = 2;
03280       *regp = reg - REG_BR;
03281     }
03282   else
03283     as_bad ("Operand %d to .%s must be a writable register", n, po);
03284 }
03285 
03286 static void
03287 dot_align (int arg)
03288 {
03289   /* The current frag is an alignment frag.  */
03290   align_frag = frag_now;
03291   s_align_bytes (arg);
03292 }
03293 
03294 static void
03295 dot_radix (dummy)
03296      int dummy ATTRIBUTE_UNUSED;
03297 {
03298   char *radix;
03299   int ch;
03300 
03301   SKIP_WHITESPACE ();
03302 
03303   if (is_it_end_of_statement ())
03304     return;
03305   radix = input_line_pointer;
03306   ch = get_symbol_end ();
03307   ia64_canonicalize_symbol_name (radix);
03308   if (strcasecmp (radix, "C"))
03309     as_bad ("Radix `%s' unsupported or invalid", radix);
03310   *input_line_pointer = ch;
03311   demand_empty_rest_of_line ();
03312 }
03313 
03314 /* Helper function for .loc directives.  If the assembler is not generating
03315    line number info, then we need to remember which instructions have a .loc
03316    directive, and only call dwarf2_gen_line_info for those instructions.  */
03317 
03318 static void
03319 dot_loc (int x)
03320 {
03321   CURR_SLOT.loc_directive_seen = 1;
03322   dwarf2_directive_loc (x);
03323 }
03324 
03325 /* .sbss, .bss etc. are macros that expand into ".section SECNAME".  */
03326 static void
03327 dot_special_section (which)
03328      int which;
03329 {
03330   set_section ((char *) special_section_name[which]);
03331 }
03332 
03333 /* Return -1 for warning and 0 for error.  */
03334 
03335 static int
03336 unwind_diagnostic (const char * region, const char *directive)
03337 {
03338   if (md.unwind_check == unwind_check_warning)
03339     {
03340       as_warn (".%s outside of %s", directive, region);
03341       return -1;
03342     }
03343   else
03344     {
03345       as_bad (".%s outside of %s", directive, region);
03346       ignore_rest_of_line ();
03347       return 0;
03348     }
03349 }
03350 
03351 /* Return 1 if a directive is in a procedure, -1 if a directive isn't in
03352    a procedure but the unwind directive check is set to warning, 0 if
03353    a directive isn't in a procedure and the unwind directive check is set
03354    to error.  */
03355 
03356 static int
03357 in_procedure (const char *directive)
03358 {
03359   if (unwind.proc_pending.sym
03360       && (!unwind.saved_text_seg || strcmp (directive, "endp") == 0))
03361     return 1;
03362   return unwind_diagnostic ("procedure", directive);
03363 }
03364 
03365 /* Return 1 if a directive is in a prologue, -1 if a directive isn't in
03366    a prologue but the unwind directive check is set to warning, 0 if
03367    a directive isn't in a prologue and the unwind directive check is set
03368    to error.  */
03369 
03370 static int
03371 in_prologue (const char *directive)
03372 {
03373   int in = in_procedure (directive);
03374 
03375   if (in > 0 && !unwind.prologue)
03376     in = unwind_diagnostic ("prologue", directive);
03377   check_pending_save ();
03378   return in;
03379 }
03380 
03381 /* Return 1 if a directive is in a body, -1 if a directive isn't in
03382    a body but the unwind directive check is set to warning, 0 if
03383    a directive isn't in a body and the unwind directive check is set
03384    to error.  */
03385 
03386 static int
03387 in_body (const char *directive)
03388 {
03389   int in = in_procedure (directive);
03390 
03391   if (in > 0 && !unwind.body)
03392     in = unwind_diagnostic ("body region", directive);
03393   return in;
03394 }
03395 
03396 static void
03397 add_unwind_entry (ptr, sep)
03398      unw_rec_list *ptr;
03399      int sep;
03400 {
03401   if (ptr)
03402     {
03403       if (unwind.tail)
03404        unwind.tail->next = ptr;
03405       else
03406        unwind.list = ptr;
03407       unwind.tail = ptr;
03408 
03409       /* The current entry can in fact be a chain of unwind entries.  */
03410       if (unwind.current_entry == NULL)
03411        unwind.current_entry = ptr;
03412     }
03413 
03414   /* The current entry can in fact be a chain of unwind entries.  */
03415   if (unwind.current_entry == NULL)
03416     unwind.current_entry = ptr;
03417 
03418   if (sep == ',')
03419     {
03420       /* Parse a tag permitted for the current directive.  */
03421       int ch;
03422 
03423       SKIP_WHITESPACE ();
03424       ch = get_symbol_end ();
03425       /* FIXME: For now, just issue a warning that this isn't implemented.  */
03426       {
03427        static int warned;
03428 
03429        if (!warned)
03430          {
03431            warned = 1;
03432            as_warn ("Tags on unwind pseudo-ops aren't supported, yet");
03433          }
03434       }
03435       *input_line_pointer = ch;
03436     }
03437   if (sep != NOT_A_CHAR)
03438     demand_empty_rest_of_line ();
03439 }
03440 
03441 static void
03442 dot_fframe (dummy)
03443      int dummy ATTRIBUTE_UNUSED;
03444 {
03445   expressionS e;
03446   int sep;
03447 
03448   if (!in_prologue ("fframe"))
03449     return;
03450 
03451   sep = parse_operand (&e, ',');
03452 
03453   if (e.X_op != O_constant)
03454     {
03455       as_bad ("First operand to .fframe must be a constant");
03456       e.X_add_number = 0;
03457     }
03458   add_unwind_entry (output_mem_stack_f (e.X_add_number), sep);
03459 }
03460 
03461 static void
03462 dot_vframe (dummy)
03463      int dummy ATTRIBUTE_UNUSED;
03464 {
03465   expressionS e;
03466   unsigned reg;
03467   int sep;
03468 
03469   if (!in_prologue ("vframe"))
03470     return;
03471 
03472   sep = parse_operand (&e, ',');
03473   reg = e.X_add_number - REG_GR;
03474   if (e.X_op != O_register || reg > 127)
03475     {
03476       as_bad ("First operand to .vframe must be a general register");
03477       reg = 0;
03478     }
03479   add_unwind_entry (output_mem_stack_v (), sep);
03480   if (! (unwind.prologue_mask & 2))
03481     add_unwind_entry (output_psp_gr (reg), NOT_A_CHAR);
03482   else if (reg != unwind.prologue_gr
03483                 + (unsigned) popcount (unwind.prologue_mask & (-2 << 1)))
03484     as_warn ("Operand of .vframe contradicts .prologue");
03485 }
03486 
03487 static void
03488 dot_vframesp (psp)
03489      int psp;
03490 {
03491   expressionS e;
03492   int sep;
03493 
03494   if (psp)
03495     as_warn (".vframepsp is meaningless, assuming .vframesp was meant");
03496 
03497   if (!in_prologue ("vframesp"))
03498     return;
03499 
03500   sep = parse_operand (&e, ',');
03501   if (e.X_op != O_constant)
03502     {
03503       as_bad ("Operand to .vframesp must be a constant (sp-relative offset)");
03504       e.X_add_number = 0;
03505     }
03506   add_unwind_entry (output_mem_stack_v (), sep);
03507   add_unwind_entry (output_psp_sprel (e.X_add_number), NOT_A_CHAR);
03508 }
03509 
03510 static void
03511 dot_save (dummy)
03512      int dummy ATTRIBUTE_UNUSED;
03513 {
03514   expressionS e1, e2;
03515   unsigned reg1, reg2;
03516   int sep;
03517 
03518   if (!in_prologue ("save"))
03519     return;
03520 
03521   sep = parse_operand (&e1, ',');
03522   if (sep == ',')
03523     sep = parse_operand (&e2, ',');
03524   else
03525     e2.X_op = O_absent;
03526 
03527   reg1 = e1.X_add_number;
03528   /* Make sure its a valid ar.xxx reg, OR its br0, aka 'rp'.  */
03529   if (e1.X_op != O_register)
03530     {
03531       as_bad ("First operand to .save not a register");
03532       reg1 = REG_PR; /* Anything valid is good here.  */
03533     }
03534   reg2 = e2.X_add_number - REG_GR;
03535   if (e2.X_op != O_register || reg2 > 127)
03536     {
03537       as_bad ("Second operand to .save not a valid register");
03538       reg2 = 0;
03539     }
03540   switch (reg1)
03541     {
03542     case REG_AR + AR_BSP:
03543       add_unwind_entry (output_bsp_when (), sep);
03544       add_unwind_entry (output_bsp_gr (reg2), NOT_A_CHAR);
03545       break;
03546     case REG_AR + AR_BSPSTORE:
03547       add_unwind_entry (output_bspstore_when (), sep);
03548       add_unwind_entry (output_bspstore_gr (reg2), NOT_A_CHAR);
03549       break;
03550     case REG_AR + AR_RNAT:
03551       add_unwind_entry (output_rnat_when (), sep);
03552       add_unwind_entry (output_rnat_gr (reg2), NOT_A_CHAR);
03553       break;
03554     case REG_AR + AR_UNAT:
03555       add_unwind_entry (output_unat_when (), sep);
03556       add_unwind_entry (output_unat_gr (reg2), NOT_A_CHAR);
03557       break;
03558     case REG_AR + AR_FPSR:
03559       add_unwind_entry (output_fpsr_when (), sep);
03560       add_unwind_entry (output_fpsr_gr (reg2), NOT_A_CHAR);
03561       break;
03562     case REG_AR + AR_PFS:
03563       add_unwind_entry (output_pfs_when (), sep);
03564       if (! (unwind.prologue_mask & 4))
03565        add_unwind_entry (output_pfs_gr (reg2), NOT_A_CHAR);
03566       else if (reg2 != unwind.prologue_gr
03567                      + (unsigned) popcount (unwind.prologue_mask & (-4 << 1)))
03568        as_warn ("Second operand of .save contradicts .prologue");
03569       break;
03570     case REG_AR + AR_LC:
03571       add_unwind_entry (output_lc_when (), sep);
03572       add_unwind_entry (output_lc_gr (reg2), NOT_A_CHAR);
03573       break;
03574     case REG_BR:
03575       add_unwind_entry (output_rp_when (), sep);
03576       if (! (unwind.prologue_mask & 8))
03577        add_unwind_entry (output_rp_gr (reg2), NOT_A_CHAR);
03578       else if (reg2 != unwind.prologue_gr)
03579        as_warn ("Second operand of .save contradicts .prologue");
03580       break;
03581     case REG_PR:
03582       add_unwind_entry (output_preds_when (), sep);
03583       if (! (unwind.prologue_mask & 1))
03584        add_unwind_entry (output_preds_gr (reg2), NOT_A_CHAR);
03585       else if (reg2 != unwind.prologue_gr
03586                      + (unsigned) popcount (unwind.prologue_mask & (-1 << 1)))
03587        as_warn ("Second operand of .save contradicts .prologue");
03588       break;
03589     case REG_PRIUNAT:
03590       add_unwind_entry (output_priunat_when_gr (), sep);
03591       add_unwind_entry (output_priunat_gr (reg2), NOT_A_CHAR);
03592       break;
03593     default:
03594       as_bad ("First operand to .save not a valid register");
03595       add_unwind_entry (NULL, sep);
03596       break;
03597     }
03598 }
03599 
03600 static void
03601 dot_restore (dummy)
03602      int dummy ATTRIBUTE_UNUSED;
03603 {
03604   expressionS e1;
03605   unsigned long ecount;     /* # of _additional_ regions to pop */
03606   int sep;
03607 
03608   if (!in_body ("restore"))
03609     return;
03610 
03611   sep = parse_operand (&e1, ',');
03612   if (e1.X_op != O_register || e1.X_add_number != REG_GR + 12)
03613     as_bad ("First operand to .restore must be stack pointer (sp)");
03614 
03615   if (sep == ',')
03616     {
03617       expressionS e2;
03618 
03619       sep = parse_operand (&e2, ',');
03620       if (e2.X_op != O_constant || e2.X_add_number < 0)
03621        {
03622          as_bad ("Second operand to .restore must be a constant >= 0");
03623          e2.X_add_number = 0;
03624        }
03625       ecount = e2.X_add_number;
03626     }
03627   else
03628     ecount = unwind.prologue_count - 1;
03629 
03630   if (ecount >= unwind.prologue_count)
03631     {
03632       as_bad ("Epilogue count of %lu exceeds number of nested prologues (%u)",
03633              ecount + 1, unwind.prologue_count);
03634       ecount = 0;
03635     }
03636 
03637   add_unwind_entry (output_epilogue (ecount), sep);
03638 
03639   if (ecount < unwind.prologue_count)
03640     unwind.prologue_count -= ecount + 1;
03641   else
03642     unwind.prologue_count = 0;
03643 }
03644 
03645 static void
03646 dot_restorereg (pred)
03647      int pred;
03648 {
03649   unsigned int qp, ab, reg;
03650   expressionS e;
03651   int sep;
03652   const char * const po = pred ? "restorereg.p" : "restorereg";
03653 
03654   if (!in_procedure (po))
03655     return;
03656 
03657   if (pred)
03658     sep = parse_predicate_and_operand (&e, &qp, po);
03659   else
03660     {
03661       sep = parse_operand (&e, ',');
03662       qp = 0;
03663     }
03664   convert_expr_to_ab_reg (&e, &ab, &reg, po, 1 + pred);
03665 
03666   add_unwind_entry (output_spill_reg (ab, reg, 0, 0, qp), sep);
03667 }
03668 
03669 static char *special_linkonce_name[] =
03670   {
03671     ".gnu.linkonce.ia64unw.", ".gnu.linkonce.ia64unwi."
03672   };
03673 
03674 static void
03675 start_unwind_section (const segT text_seg, int sec_index)
03676 {
03677   /*
03678     Use a slightly ugly scheme to derive the unwind section names from
03679     the text section name:
03680 
03681     text sect.  unwind table sect.
03682     name:       name:                      comments:
03683     ----------  -----------------          --------------------------------
03684     .text       .IA_64.unwind
03685     .text.foo   .IA_64.unwind.text.foo
03686     .foo        .IA_64.unwind.foo
03687     .gnu.linkonce.t.foo
03688               .gnu.linkonce.ia64unw.foo
03689     _info       .IA_64.unwind_info         gas issues error message (ditto)
03690     _infoFOO    .IA_64.unwind_infoFOO      gas issues error message (ditto)
03691 
03692     This mapping is done so that:
03693 
03694        (a) An object file with unwind info only in .text will use
03695            unwind section names .IA_64.unwind and .IA_64.unwind_info.
03696            This follows the letter of the ABI and also ensures backwards
03697            compatibility with older toolchains.
03698 
03699        (b) An object file with unwind info in multiple text sections
03700            will use separate unwind sections for each text section.
03701            This allows us to properly set the "sh_info" and "sh_link"
03702            fields in SHT_IA_64_UNWIND as required by the ABI and also
03703            lets GNU ld support programs with multiple segments
03704            containing unwind info (as might be the case for certain
03705            embedded applications).
03706 
03707        (c) An error is issued if there would be a name clash.
03708   */
03709 
03710   const char *text_name, *sec_text_name;
03711   char *sec_name;
03712   const char *prefix = special_section_name [sec_index];
03713   const char *suffix;
03714   size_t prefix_len, suffix_len, sec_name_len;
03715 
03716   sec_text_name = segment_name (text_seg);
03717   text_name = sec_text_name;
03718   if (strncmp (text_name, "_info", 5) == 0)
03719     {
03720       as_bad ("Illegal section name `%s' (causes unwind section name clash)",
03721              text_name);
03722       ignore_rest_of_line ();
03723       return;
03724     }
03725   if (strcmp (text_name, ".text") == 0)
03726     text_name = "";
03727 
03728   /* Build the unwind section name by appending the (possibly stripped)
03729      text section name to the unwind prefix.  */
03730   suffix = text_name;
03731   if (strncmp (text_name, ".gnu.linkonce.t.",
03732               sizeof (".gnu.linkonce.t.") - 1) == 0)
03733     {
03734       prefix = special_linkonce_name [sec_index - SPECIAL_SECTION_UNWIND];
03735       suffix += sizeof (".gnu.linkonce.t.") - 1;
03736     }
03737 
03738   prefix_len = strlen (prefix);
03739   suffix_len = strlen (suffix);
03740   sec_name_len = prefix_len + suffix_len;
03741   sec_name = alloca (sec_name_len + 1);
03742   memcpy (sec_name, prefix, prefix_len);
03743   memcpy (sec_name + prefix_len, suffix, suffix_len);
03744   sec_name [sec_name_len] = '\0';
03745 
03746   /* Handle COMDAT group.  */
03747   if ((text_seg->flags & SEC_LINK_ONCE) != 0
03748       && (elf_section_flags (text_seg) & SHF_GROUP) != 0)
03749     {
03750       char *section;
03751       size_t len, group_name_len;
03752       const char *group_name = elf_group_name (text_seg);
03753 
03754       if (group_name == NULL)
03755        {
03756          as_bad ("Group section `%s' has no group signature",
03757                 sec_text_name);
03758          ignore_rest_of_line ();
03759          return;
03760        }
03761       /* We have to construct a fake section directive. */
03762       group_name_len = strlen (group_name);
03763       len = (sec_name_len
03764             + 16                   /* ,"aG",@progbits,  */
03765             + group_name_len              /* ,group_name  */
03766             + 7);                  /* ,comdat  */
03767 
03768       section = alloca (len + 1);
03769       memcpy (section, sec_name, sec_name_len);
03770       memcpy (section + sec_name_len, ",\"aG\",@progbits,", 16);
03771       memcpy (section + sec_name_len + 16, group_name, group_name_len);
03772       memcpy (section + len - 7, ",comdat", 7);
03773       section [len] = '\0';
03774       set_section (section);
03775     }
03776   else
03777     {
03778       set_section (sec_name);
03779       bfd_set_section_flags (stdoutput, now_seg,
03780                           SEC_LOAD | SEC_ALLOC | SEC_READONLY);
03781     }
03782 
03783   elf_linked_to_section (now_seg) = text_seg;
03784 }
03785 
03786 static void
03787 generate_unwind_image (const segT text_seg)
03788 {
03789   int size, pad;
03790   unw_rec_list *list;
03791 
03792   /* Mark the end of the unwind info, so that we can compute the size of the
03793      last unwind region.  */
03794   add_unwind_entry (output_endp (), NOT_A_CHAR);
03795 
03796   /* Force out pending instructions, to make sure all unwind records have
03797      a valid slot_number field.  */
03798   ia64_flush_insns ();
03799 
03800   /* Generate the unwind record.  */
03801   list = optimize_unw_records (unwind.list);
03802   fixup_unw_records (list, 1);
03803   size = calc_record_size (list);
03804 
03805   if (size > 0 || unwind.force_unwind_entry)
03806     {
03807       unwind.force_unwind_entry = 0;
03808       /* pad to pointer-size boundary.  */
03809       pad = size % md.pointer_size;
03810       if (pad != 0)
03811        size += md.pointer_size - pad;
03812       /* Add 8 for the header.  */
03813       size += 8;
03814       /* Add a pointer for the personality offset.  */
03815       if (unwind.personality_routine)
03816        size += md.pointer_size;
03817     }
03818 
03819   /* If there are unwind records, switch sections, and output the info.  */
03820   if (size != 0)
03821     {
03822       expressionS exp;
03823       bfd_reloc_code_real_type reloc;
03824 
03825       start_unwind_section (text_seg, SPECIAL_SECTION_UNWIND_INFO);
03826 
03827       /* Make sure the section has 4 byte alignment for ILP32 and
03828         8 byte alignment for LP64.  */
03829       frag_align (md.pointer_size_shift, 0, 0);
03830       record_alignment (now_seg, md.pointer_size_shift);
03831 
03832       /* Set expression which points to start of unwind descriptor area.  */
03833       unwind.info = expr_build_dot ();
03834       
03835       frag_var (rs_machine_dependent, size, size, 0, 0,
03836               (offsetT) (long) unwind.personality_routine,
03837               (char *) list);
03838 
03839       /* Add the personality address to the image.  */
03840       if (unwind.personality_routine != 0)
03841        {
03842          exp.X_op = O_symbol;
03843          exp.X_add_symbol = unwind.personality_routine;
03844          exp.X_add_number = 0;
03845 
03846          if (md.flags & EF_IA_64_BE)
03847            {
03848              if (md.flags & EF_IA_64_ABI64)
03849               reloc = BFD_RELOC_IA64_LTOFF_FPTR64MSB;
03850              else
03851               reloc = BFD_RELOC_IA64_LTOFF_FPTR32MSB;
03852            }
03853          else
03854            {
03855              if (md.flags & EF_IA_64_ABI64)
03856               reloc = BFD_RELOC_IA64_LTOFF_FPTR64LSB;
03857              else
03858               reloc = BFD_RELOC_IA64_LTOFF_FPTR32LSB;
03859            }
03860 
03861          fix_new_exp (frag_now, frag_now_fix () - md.pointer_size,
03862                      md.pointer_size, &exp, 0, reloc);
03863          unwind.personality_routine = 0;
03864        }
03865     }
03866 
03867   free_saved_prologue_counts ();
03868   unwind.list = unwind.tail = unwind.current_entry = NULL;
03869 }
03870 
03871 static void
03872 dot_handlerdata (dummy)
03873      int dummy ATTRIBUTE_UNUSED;
03874 {
03875   if (!in_procedure ("handlerdata"))
03876     return;
03877   unwind.force_unwind_entry = 1;
03878 
03879   /* Remember which segment we're in so we can switch back after .endp */
03880   unwind.saved_text_seg = now_seg;
03881   unwind.saved_text_subseg = now_subseg;
03882 
03883   /* Generate unwind info into unwind-info section and then leave that
03884      section as the currently active one so dataXX directives go into
03885      the language specific data area of the unwind info block.  */
03886   generate_unwind_image (now_seg);
03887   demand_empty_rest_of_line ();
03888 }
03889 
03890 static void
03891 dot_unwentry (dummy)
03892      int dummy ATTRIBUTE_UNUSED;
03893 {
03894   if (!in_procedure ("unwentry"))
03895     return;
03896   unwind.force_unwind_entry = 1;
03897   demand_empty_rest_of_line ();
03898 }
03899 
03900 static void
03901 dot_altrp (dummy)
03902      int dummy ATTRIBUTE_UNUSED;
03903 {
03904   expressionS e;
03905   unsigned reg;
03906 
03907   if (!in_prologue ("altrp"))
03908     return;
03909 
03910   parse_operand (&e, 0);
03911   reg = e.X_add_number - REG_BR;
03912   if (e.X_op != O_register || reg > 7)
03913     {
03914       as_bad ("First operand to .altrp not a valid branch register");
03915       reg = 0;
03916     }
03917   add_unwind_entry (output_rp_br (reg), 0);
03918 }
03919 
03920 static void
03921 dot_savemem (psprel)
03922      int psprel;
03923 {
03924   expressionS e1, e2;
03925   int sep;
03926   int reg1, val;
03927   const char * const po = psprel ? "savepsp" : "savesp";
03928 
03929   if (!in_prologue (po))
03930     return;
03931 
03932   sep = parse_operand (&e1, ',');
03933   if (sep == ',')
03934     sep = parse_operand (&e2, ',');
03935   else
03936     e2.X_op = O_absent;
03937 
03938   reg1 = e1.X_add_number;
03939   val = e2.X_add_number;
03940 
03941   /* Make sure its a valid ar.xxx reg, OR its br0, aka 'rp'.  */
03942   if (e1.X_op != O_register)
03943     {
03944       as_bad ("First operand to .%s not a register", po);
03945       reg1 = REG_PR; /* Anything valid is good here.  */
03946     }
03947   if (e2.X_op != O_constant)
03948     {
03949       as_bad ("Second operand to .%s not a constant", po);
03950       val = 0;
03951     }
03952 
03953   switch (reg1)
03954     {
03955     case REG_AR + AR_BSP:
03956       add_unwind_entry (output_bsp_when (), sep);
03957       add_unwind_entry ((psprel
03958                       ? output_bsp_psprel
03959                       : output_bsp_sprel) (val), NOT_A_CHAR);
03960       break;
03961     case REG_AR + AR_BSPSTORE:
03962       add_unwind_entry (output_bspstore_when (), sep);
03963       add_unwind_entry ((psprel
03964                       ? output_bspstore_psprel
03965                       : output_bspstore_sprel) (val), NOT_A_CHAR);
03966       break;
03967     case REG_AR + AR_RNAT:
03968       add_unwind_entry (output_rnat_when (), sep);
03969       add_unwind_entry ((psprel
03970                       ? output_rnat_psprel
03971                       : output_rnat_sprel) (val), NOT_A_CHAR);
03972       break;
03973     case REG_AR + AR_UNAT:
03974       add_unwind_entry (output_unat_when (), sep);
03975       add_unwind_entry ((psprel
03976                       ? output_unat_psprel
03977                       : output_unat_sprel) (val), NOT_A_CHAR);
03978       break;
03979     case REG_AR + AR_FPSR:
03980       add_unwind_entry (output_fpsr_when (), sep);
03981       add_unwind_entry ((psprel
03982                       ? output_fpsr_psprel
03983                       : output_fpsr_sprel) (val), NOT_A_CHAR);
03984       break;
03985     case REG_AR + AR_PFS:
03986       add_unwind_entry (output_pfs_when (), sep);
03987       add_unwind_entry ((psprel
03988                       ? output_pfs_psprel
03989                       : output_pfs_sprel) (val), NOT_A_CHAR);
03990       break;
03991     case REG_AR + AR_LC:
03992       add_unwind_entry (output_lc_when (), sep);
03993       add_unwind_entry ((psprel
03994                       ? output_lc_psprel
03995                       : output_lc_sprel) (val), NOT_A_CHAR);
03996       break;
03997     case REG_BR:
03998       add_unwind_entry (output_rp_when (), sep);
03999       add_unwind_entry ((psprel
04000                       ? output_rp_psprel
04001                       : output_rp_sprel) (val), NOT_A_CHAR);
04002       break;
04003     case REG_PR:
04004       add_unwind_entry (output_preds_when (), sep);
04005       add_unwind_entry ((psprel
04006                       ? output_preds_psprel
04007                       : output_preds_sprel) (val), NOT_A_CHAR);
04008       break;
04009     case REG_PRIUNAT:
04010       add_unwind_entry (output_priunat_when_mem (), sep);
04011       add_unwind_entry ((psprel
04012                       ? output_priunat_psprel
04013                       : output_priunat_sprel) (val), NOT_A_CHAR);
04014       break;
04015     default:
04016       as_bad ("First operand to .%s not a valid register", po);
04017       add_unwind_entry (NULL, sep);
04018       break;
04019     }
04020 }
04021 
04022 static void
04023 dot_saveg (dummy)
04024      int dummy ATTRIBUTE_UNUSED;
04025 {
04026   expressionS e;
04027   unsigned grmask;
04028   int sep;
04029 
04030   if (!in_prologue ("save.g"))
04031     return;
04032 
04033   sep = parse_operand (&e, ',');
04034 
04035   grmask = e.X_add_number;
04036   if (e.X_op != O_constant
04037       || e.X_add_number <= 0
04038       || e.X_add_number > 0xf)
04039     {
04040       as_bad ("First operand to .save.g must be a positive 4-bit constant");
04041       grmask = 0;
04042     }
04043 
04044   if (sep == ',')
04045     {
04046       unsigned reg;
04047       int n = popcount (grmask);
04048 
04049       parse_operand (&e, 0);
04050       reg = e.X_add_number - REG_GR;
04051       if (e.X_op != O_register || reg > 127)
04052        {
04053          as_bad ("Second operand to .save.g must be a general register");
04054          reg = 0;
04055        }
04056       else if (reg > 128U - n)
04057        {
04058          as_bad ("Second operand to .save.g must be the first of %d general registers", n);
04059          reg = 0;
04060        }
04061       add_unwind_entry (output_gr_gr (grmask, reg), 0);
04062     }
04063   else
04064     add_unwind_entry (output_gr_mem (grmask), 0);
04065 }
04066 
04067 static void
04068 dot_savef (dummy)
04069      int dummy ATTRIBUTE_UNUSED;
04070 {
04071   expressionS e;
04072 
04073   if (!in_prologue ("save.f"))
04074     return;
04075 
04076   parse_operand (&e, 0);
04077 
04078   if (e.X_op != O_constant
04079       || e.X_add_number <= 0
04080       || e.X_add_number > 0xfffff)
04081     {
04082       as_bad ("Operand to .save.f must be a positive 20-bit constant");
04083       e.X_add_number = 0;
04084     }
04085   add_unwind_entry (output_fr_mem (e.X_add_number), 0);
04086 }
04087 
04088 static void
04089 dot_saveb (dummy)
04090      int dummy ATTRIBUTE_UNUSED;
04091 {
04092   expressionS e;
04093   unsigned brmask;
04094   int sep;
04095 
04096   if (!in_prologue ("save.b"))
04097     return;
04098 
04099   sep = parse_operand (&e, ',');
04100 
04101   brmask = e.X_add_number;
04102   if (e.X_op != O_constant
04103       || e.X_add_number <= 0
04104       || e.X_add_number > 0x1f)
04105     {
04106       as_bad ("First operand to .save.b must be a positive 5-bit constant");
04107       brmask = 0;
04108     }
04109 
04110   if (sep == ',')
04111     {
04112       unsigned reg;
04113       int n = popcount (brmask);
04114 
04115       parse_operand (&e, 0);
04116       reg = e.X_add_number - REG_GR;
04117       if (e.X_op != O_register || reg > 127)
04118        {
04119          as_bad ("Second operand to .save.b must be a general register");
04120          reg = 0;
04121        }
04122       else if (reg > 128U - n)
04123        {
04124          as_bad ("Second operand to .save.b must be the first of %d general registers", n);
04125          reg = 0;
04126        }
04127       add_unwind_entry (output_br_gr (brmask, reg), 0);
04128     }
04129   else
04130     add_unwind_entry (output_br_mem (brmask), 0);
04131 }
04132 
04133 static void
04134 dot_savegf (dummy)
04135      int dummy ATTRIBUTE_UNUSED;
04136 {
04137   expressionS e1, e2;
04138 
04139   if (!in_prologue ("save.gf"))
04140     return;
04141 
04142   if (parse_operand (&e1, ',') == ',')
04143     parse_operand (&e2, 0);
04144   else
04145     e2.X_op = O_absent;
04146 
04147   if (e1.X_op != O_constant
04148       || e1.X_add_number < 0
04149       || e1.X_add_number > 0xf)
04150     {
04151       as_bad ("First operand to .save.gf must be a non-negative 4-bit constant");
04152       e1.X_op = O_absent;
04153       e1.X_add_number = 0;
04154     }
04155   if (e2.X_op != O_constant
04156       || e2.X_add_number < 0
04157       || e2.X_add_number > 0xfffff)
04158     {
04159       as_bad ("Second operand to .save.gf must be a non-negative 20-bit constant");
04160       e2.X_op = O_absent;
04161       e2.X_add_number = 0;
04162     }
04163   if (e1.X_op == O_constant
04164       && e2.X_op == O_constant
04165       && e1.X_add_number == 0
04166       && e2.X_add_number == 0)
04167     as_bad ("Operands to .save.gf may not be both zero");
04168 
04169   add_unwind_entry (output_frgr_mem (e1.X_add_number, e2.X_add_number), 0);
04170 }
04171 
04172 static void
04173 dot_spill (dummy)
04174      int dummy ATTRIBUTE_UNUSED;
04175 {
04176   expressionS e;
04177 
04178   if (!in_prologue ("spill"))
04179     return;
04180 
04181   parse_operand (&e, 0);
04182 
04183   if (e.X_op != O_constant)
04184     {
04185       as_bad ("Operand to .spill must be a constant");
04186       e.X_add_number = 0;
04187     }
04188   add_unwind_entry (output_spill_base (e.X_add_number), 0);
04189 }
04190 
04191 static void
04192 dot_spillreg (pred)
04193      int pred;
04194 {
04195   int sep;
04196   unsigned int qp, ab, xy, reg, treg;
04197   expressionS e;
04198   const char * const po = pred ? "spillreg.p" : "spillreg";
04199 
04200   if (!in_procedure (po))
04201     return;
04202 
04203   if (pred)
04204     sep = parse_predicate_and_operand (&e, &qp, po);
04205   else
04206     {
04207       sep = parse_operand (&e, ',');
04208       qp = 0;
04209     }
04210   convert_expr_to_ab_reg (&e, &ab, &reg, po, 1 + pred);
04211 
04212   if (sep == ',')
04213     sep = parse_operand (&e, ',');
04214   else
04215     e.X_op = O_absent;
04216   convert_expr_to_xy_reg (&e, &xy, &treg, po, 2 + pred);
04217 
04218   add_unwind_entry (output_spill_reg (ab, reg, treg, xy, qp), sep);
04219 }
04220 
04221 static void
04222 dot_spillmem (psprel)
04223      int psprel;
04224 {
04225   expressionS e;
04226   int pred = (psprel < 0), sep;
04227   unsigned int qp, ab, reg;
04228   const char * po;
04229 
04230   if (pred)
04231     {
04232       psprel = ~psprel;
04233       po = psprel ? "spillpsp.p" : "spillsp.p";
04234     }
04235   else
04236     po = psprel ? "spillpsp" : "spillsp";
04237 
04238   if (!in_procedure (po))
04239     return;
04240 
04241   if (pred)
04242     sep = parse_predicate_and_operand (&e, &qp, po);
04243   else
04244     {
04245       sep = parse_operand (&e, ',');
04246       qp = 0;
04247     }
04248   convert_expr_to_ab_reg (&e, &ab, &reg, po, 1 + pred);
04249 
04250   if (sep == ',')
04251     sep = parse_operand (&e, ',');
04252   else
04253     e.X_op = O_absent;
04254   if (e.X_op != O_constant)
04255     {
04256       as_bad ("Operand %d to .%s must be a constant", 2 + pred, po);
04257       e.X_add_number = 0;
04258     }
04259 
04260   if (psprel)
04261     add_unwind_entry (output_spill_psprel (ab, reg, e.X_add_number, qp), sep);
04262   else
04263     add_unwind_entry (output_spill_sprel (ab, reg, e.X_add_number, qp), sep);
04264 }
04265 
04266 static unsigned int
04267 get_saved_prologue_count (lbl)
04268      unsigned long lbl;
04269 {
04270   label_prologue_count *lpc = unwind.saved_prologue_counts;
04271 
04272   while (lpc != NULL && lpc->label_number != lbl)
04273     lpc = lpc->next;
04274 
04275   if (lpc != NULL)
04276     return lpc->prologue_count;
04277 
04278   as_bad ("Missing .label_state %ld", lbl);
04279   return 1;
04280 }
04281 
04282 static void
04283 save_prologue_count (lbl, count)
04284      unsigned long lbl;
04285      unsigned int count;
04286 {
04287   label_prologue_count *lpc = unwind.saved_prologue_counts;
04288 
04289   while (lpc != NULL && lpc->label_number != lbl)
04290     lpc = lpc->next;
04291 
04292   if (lpc != NULL)
04293     lpc->prologue_count = count;
04294   else
04295     {
04296       label_prologue_count *new_lpc = xmalloc (sizeof (* new_lpc));
04297 
04298       new_lpc->next = unwind.saved_prologue_counts;
04299       new_lpc->label_number = lbl;
04300       new_lpc->prologue_count = count;
04301       unwind.saved_prologue_counts = new_lpc;
04302     }
04303 }
04304 
04305 static void
04306 free_saved_prologue_counts ()
04307 {
04308   label_prologue_count *lpc = unwind.saved_prologue_counts;
04309   label_prologue_count *next;
04310 
04311   while (lpc != NULL)
04312     {
04313       next = lpc->next;
04314       free (lpc);
04315       lpc = next;
04316     }
04317 
04318   unwind.saved_prologue_counts = NULL;
04319 }
04320 
04321 static void
04322 dot_label_state (dummy)
04323      int dummy ATTRIBUTE_UNUSED;
04324 {
04325   expressionS e;
04326 
04327   if (!in_body ("label_state"))
04328     return;
04329 
04330   parse_operand (&e, 0);
04331   if (e.X_op == O_constant)
04332     save_prologue_count (e.X_add_number, unwind.prologue_count);
04333   else
04334     {
04335       as_bad ("Operand to .label_state must be a constant");
04336       e.X_add_number = 0;
04337     }
04338   add_unwind_entry (output_label_state (e.X_add_number), 0);
04339 }
04340 
04341 static void
04342 dot_copy_state (dummy)
04343      int dummy ATTRIBUTE_UNUSED;
04344 {
04345   expressionS e;
04346 
04347   if (!in_body ("copy_state"))
04348     return;
04349 
04350   parse_operand (&e, 0);
04351   if (e.X_op == O_constant)
04352     unwind.prologue_count = get_saved_prologue_count (e.X_add_number);
04353   else
04354     {
04355       as_bad ("Operand to .copy_state must be a constant");
04356       e.X_add_number = 0;
04357     }
04358   add_unwind_entry (output_copy_state (e.X_add_number), 0);
04359 }
04360 
04361 static void
04362 dot_unwabi (dummy)
04363      int dummy ATTRIBUTE_UNUSED;
04364 {
04365   expressionS e1, e2;
04366   unsigned char sep;
04367 
04368   if (!in_prologue ("unwabi"))
04369     return;
04370 
04371   sep = parse_operand (&e1, ',');
04372   if (sep == ',')
04373     parse_operand (&e2, 0);
04374   else
04375     e2.X_op = O_absent;
04376 
04377   if (e1.X_op != O_constant)
04378     {
04379       as_bad ("First operand to .unwabi must be a constant");
04380       e1.X_add_number = 0;
04381     }
04382 
04383   if (e2.X_op != O_constant)
04384     {
04385       as_bad ("Second operand to .unwabi must be a constant");
04386       e2.X_add_number = 0;
04387     }
04388 
04389   add_unwind_entry (output_unwabi (e1.X_add_number, e2.X_add_number), 0);
04390 }
04391 
04392 static void
04393 dot_personality (dummy)
04394      int dummy ATTRIBUTE_UNUSED;
04395 {
04396   char *name, *p, c;
04397   if (!in_procedure ("personality"))
04398     return;
04399   SKIP_WHITESPACE ();
04400   name = input_line_pointer;
04401   c = get_symbol_end ();
04402   p = input_line_pointer;
04403   unwind.personality_routine = symbol_find_or_make (name);
04404   unwind.force_unwind_entry = 1;
04405   *p = c;
04406   SKIP_WHITESPACE ();
04407   demand_empty_rest_of_line ();
04408 }
04409 
04410 static void
04411 dot_proc (dummy)
04412      int dummy ATTRIBUTE_UNUSED;
04413 {
04414   char *name, *p, c;
04415   symbolS *sym;
04416   proc_pending *pending, *last_pending;
04417 
04418   if (unwind.proc_pending.sym)
04419     {
04420       (md.unwind_check == unwind_check_warning
04421        ? as_warn
04422        : as_bad) ("Missing .endp after previous .proc");
04423       while (unwind.proc_pending.next)
04424        {
04425          pending = unwind.proc_pending.next;
04426          unwind.proc_pending.next = pending->next;
04427          free (pending);
04428        }
04429     }
04430   last_pending = NULL;
04431 
04432   /* Parse names of main and alternate entry points and mark them as
04433      function symbols:  */
04434   while (1)
04435     {
04436       SKIP_WHITESPACE ();
04437       name = input_line_pointer;
04438       c = get_symbol_end ();
04439       p = input_line_pointer;
04440       if (!*name)
04441        as_bad ("Empty argument of .proc");
04442       else
04443        {
04444          sym = symbol_find_or_make (name);
04445          if (S_IS_DEFINED (sym))
04446            as_bad ("`%s' was already defined", name);
04447          else if (!last_pending)
04448            {
04449              unwind.proc_pending.sym = sym;
04450              last_pending = &unwind.proc_pending;
04451            }
04452          else
04453            {
04454              pending = xmalloc (sizeof (*pending));
04455              pending->sym = sym;
04456              last_pending = last_pending->next = pending;
04457            }
04458          symbol_get_bfdsym (sym)->flags |= BSF_FUNCTION;
04459        }
04460       *p = c;
04461       SKIP_WHITESPACE ();
04462       if (*input_line_pointer != ',')
04463        break;
04464       ++input_line_pointer;
04465     }
04466   if (!last_pending)
04467     {
04468       unwind.proc_pending.sym = expr_build_dot ();
04469       last_pending = &unwind.proc_pending;
04470     }
04471   last_pending->next = NULL;
04472   demand_empty_rest_of_line ();
04473   ia64_do_align (16);
04474 
04475   unwind.prologue = 0;
04476   unwind.prologue_count = 0;
04477   unwind.body = 0;
04478   unwind.insn = 0;
04479   unwind.list = unwind.tail = unwind.current_entry = NULL;
04480   unwind.personality_routine = 0;
04481 }
04482 
04483 static void
04484 dot_body (dummy)
04485      int dummy ATTRIBUTE_UNUSED;
04486 {
04487   if (!in_procedure ("body"))
04488     return;
04489   if (!unwind.prologue && !unwind.body && unwind.insn)
04490     as_warn ("Initial .body should precede any instructions");
04491   check_pending_save ();
04492 
04493   unwind.prologue = 0;
04494   unwind.prologue_mask = 0;
04495   unwind.body = 1;
04496 
04497   add_unwind_entry (output_body (), 0);
04498 }
04499 
04500 static void
04501 dot_prologue (dummy)
04502      int dummy ATTRIBUTE_UNUSED;
04503 {
04504   unsigned mask = 0, grsave = 0;
04505 
04506   if (!in_procedure ("prologue"))
04507     return;
04508   if (unwind.prologue)
04509     {
04510       as_bad (".prologue within prologue");
04511       ignore_rest_of_line ();
04512       return;
04513     }
04514   if (!unwind.body && unwind.insn)
04515     as_warn ("Initial .prologue should precede any instructions");
04516 
04517   if (!is_it_end_of_statement ())
04518     {
04519       expressionS e;
04520       int n, sep = parse_operand (&e, ',');
04521 
04522       if (e.X_op != O_constant
04523          || e.X_add_number < 0
04524          || e.X_add_number > 0xf)
04525        as_bad ("First operand to .prologue must be a positive 4-bit constant");
04526       else if (e.X_add_number == 0)
04527        as_warn ("Pointless use of zero first operand to .prologue");
04528       else
04529        mask = e.X_add_number;
04530        n = popcount (mask);
04531 
04532       if (sep == ',')
04533        parse_operand (&e, 0);
04534       else
04535        e.X_op = O_absent;
04536       if (e.X_op == O_constant
04537          && e.X_add_number >= 0
04538          && e.X_add_number < 128)
04539        {
04540          if (md.unwind_check == unwind_check_error)
04541            as_warn ("Using a constant as second operand to .prologue is deprecated");
04542          grsave = e.X_add_number;
04543        }
04544       else if (e.X_op != O_register
04545               || (grsave = e.X_add_number - REG_GR) > 127)
04546        {
04547          as_bad ("Second operand to .prologue must be a general register");
04548          grsave = 0;
04549        }
04550       else if (grsave > 128U - n)
04551        {
04552          as_bad ("Second operand to .prologue must be the first of %d general registers", n);
04553          grsave = 0;
04554        }
04555 
04556     }
04557 
04558   if (mask)
04559     add_unwind_entry (output_prologue_gr (mask, grsave), 0);
04560   else
04561     add_unwind_entry (output_prologue (), 0);
04562 
04563   unwind.prologue = 1;
04564   unwind.prologue_mask = mask;
04565   unwind.prologue_gr = grsave;
04566   unwind.body = 0;
04567   ++unwind.prologue_count;
04568 }
04569 
04570 static void
04571 dot_endp (dummy)
04572      int dummy ATTRIBUTE_UNUSED;
04573 {
04574   expressionS e;
04575   int bytes_per_address;
04576   long where;
04577   segT saved_seg;
04578   subsegT saved_subseg;
04579   proc_pending *pending;
04580   int unwind_check = md.unwind_check;
04581 
04582   md.unwind_check = unwind_check_error;
04583   if (!in_procedure ("endp"))
04584     return;
04585   md.unwind_check = unwind_check;
04586 
04587   if (unwind.saved_text_seg)
04588     {
04589       saved_seg = unwind.saved_text_seg;
04590       saved_subseg = unwind.saved_text_subseg;
04591       unwind.saved_text_seg = NULL;
04592     }
04593   else
04594     {
04595       saved_seg = now_seg;
04596       saved_subseg = now_subseg;
04597     }
04598 
04599   insn_group_break (1, 0, 0);
04600 
04601   /* If there wasn't a .handlerdata, we haven't generated an image yet.  */
04602   if (!unwind.info)
04603     generate_unwind_image (saved_seg);
04604 
04605   if (unwind.info || unwind.force_unwind_entry)
04606     {
04607       symbolS *proc_end;
04608 
04609       subseg_set (md.last_text_seg, 0);
04610       proc_end = expr_build_dot ();
04611 
04612       start_unwind_section (saved_seg, SPECIAL_SECTION_UNWIND);
04613 
04614       /* Make sure that section has 4 byte alignment for ILP32 and
04615          8 byte alignment for LP64.  */
04616       record_alignment (now_seg, md.pointer_size_shift);
04617 
04618       /* Need space for 3 pointers for procedure start, procedure end,
04619         and unwind info.  */
04620       memset (frag_more (3 * md.pointer_size), 0, 3 * md.pointer_size);
04621       where = frag_now_fix () - (3 * md.pointer_size);
04622       bytes_per_address = bfd_arch_bits_per_address (stdoutput) / 8;
04623 
04624       /* Issue the values of  a) Proc Begin, b) Proc End, c) Unwind Record.  */
04625       e.X_op = O_pseudo_fixup;
04626       e.X_op_symbol = pseudo_func[FUNC_SEG_RELATIVE].u.sym;
04627       e.X_add_number = 0;
04628       if (!S_IS_LOCAL (unwind.proc_pending.sym)
04629          && S_IS_DEFINED (unwind.proc_pending.sym))
04630        e.X_add_symbol = symbol_temp_new (S_GET_SEGMENT (unwind.proc_pending.sym),
04631                                      S_GET_VALUE (unwind.proc_pending.sym),
04632                                      symbol_get_frag (unwind.proc_pending.sym));
04633       else
04634        e.X_add_symbol = unwind.proc_pending.sym;
04635       ia64_cons_fix_new (frag_now, where, bytes_per_address, &e);
04636 
04637       e.X_op = O_pseudo_fixup;
04638       e.X_op_symbol = pseudo_func[FUNC_SEG_RELATIVE].u.sym;
04639       e.X_add_number = 0;
04640       e.X_add_symbol = proc_end;
04641       ia64_cons_fix_new (frag_now, where + bytes_per_address,
04642                       bytes_per_address, &e);
04643 
04644       if (unwind.info)
04645        {
04646          e.X_op = O_pseudo_fixup;
04647          e.X_op_symbol = pseudo_func[FUNC_SEG_RELATIVE].u.sym;
04648          e.X_add_number = 0;
04649          e.X_add_symbol = unwind.info;
04650          ia64_cons_fix_new (frag_now, where + (bytes_per_address * 2),
04651                           bytes_per_address, &e);
04652        }
04653     }
04654   subseg_set (saved_seg, saved_subseg);
04655 
04656   /* Set symbol sizes.  */
04657   pending = &unwind.proc_pending;
04658   if (S_GET_NAME (pending->sym))
04659     {
04660       do
04661        {
04662          symbolS *sym = pending->sym;
04663 
04664          if (!S_IS_DEFINED (sym))
04665            as_bad ("`%s' was not defined within procedure", S_GET_NAME (sym));
04666          else if (S_GET_SIZE (sym) == 0
04667                  && symbol_get_obj (sym)->size == NULL)
04668            {
04669              fragS *frag = symbol_get_frag (sym);
04670 
04671              if (frag)
04672               {
04673                 if (frag == frag_now && SEG_NORMAL (now_seg))
04674                   S_SET_SIZE (sym, frag_now_fix () - S_GET_VALUE (sym));
04675                 else
04676                   {
04677                     symbol_get_obj (sym)->size =
04678                      (expressionS *) xmalloc (sizeof (expressionS));
04679                     symbol_get_obj (sym)->size->X_op = O_subtract;
04680                     symbol_get_obj (sym)->size->X_add_symbol
04681                      = symbol_new (FAKE_LABEL_NAME, now_seg,
04682                                   frag_now_fix (), frag_now);
04683                     symbol_get_obj (sym)->size->X_op_symbol = sym;
04684                     symbol_get_obj (sym)->size->X_add_number = 0;
04685                   }
04686               }
04687            }
04688        } while ((pending = pending->next) != NULL);
04689     }
04690 
04691   /* Parse names of main and alternate entry points.  */
04692   while (1)
04693     {
04694       char *name, *p, c;
04695 
04696       SKIP_WHITESPACE ();
04697       name = input_line_pointer;
04698       c = get_symbol_end ();
04699       p = input_line_pointer;
04700       if (!*name)
04701        (md.unwind_check == unwind_check_warning
04702         ? as_warn
04703         : as_bad) ("Empty argument of .endp");
04704       else
04705        {
04706          symbolS *sym = symbol_find (name);
04707 
04708          for (pending = &unwind.proc_pending; pending; pending = pending->next)
04709            {
04710              if (sym == pending->sym)
04711               {
04712                 pending->sym = NULL;
04713                 break;
04714               }
04715            }
04716          if (!sym || !pending)
04717            as_warn ("`%s' was not specified with previous .proc", name);
04718        }
04719       *p = c;
04720       SKIP_WHITESPACE ();
04721       if (*input_line_pointer != ',')
04722        break;
04723       ++input_line_pointer;
04724     }
04725   demand_empty_rest_of_line ();
04726 
04727   /* Deliberately only checking for the main entry point here; the
04728      language spec even says all arguments to .endp are ignored.  */
04729   if (unwind.proc_pending.sym
04730       && S_GET_NAME (unwind.proc_pending.sym)
04731       && strcmp (S_GET_NAME (unwind.proc_pending.sym), FAKE_LABEL_NAME))
04732     as_warn ("`%s' should be an operand to this .endp",
04733             S_GET_NAME (unwind.proc_pending.sym));
04734   while (unwind.proc_pending.next)
04735     {
04736       pending = unwind.proc_pending.next;
04737       unwind.proc_pending.next = pending->next;
04738       free (pending);
04739     }
04740   unwind.proc_pending.sym = unwind.info = NULL;
04741 }
04742 
04743 static void
04744 dot_template (template)
04745      int template;
04746 {
04747   CURR_SLOT.user_template = template;
04748 }
04749 
04750 static void
04751 dot_regstk (dummy)
04752      int dummy ATTRIBUTE_UNUSED;
04753 {
04754   int ins, locs, outs, rots;
04755 
04756   if (is_it_end_of_statement ())
04757     ins = locs = outs = rots = 0;
04758   else
04759     {
04760       ins = get_absolute_expression ();
04761       if (*input_line_pointer++ != ',')
04762        goto err;
04763       locs = get_absolute_expression ();
04764       if (*input_line_pointer++ != ',')
04765        goto err;
04766       outs = get_absolute_expression ();
04767       if (*input_line_pointer++ != ',')
04768        goto err;
04769       rots = get_absolute_expression ();
04770     }
04771   set_regstack (ins, locs, outs, rots);
04772   return;
04773 
04774  err:
04775   as_bad ("Comma expected");
04776   ignore_rest_of_line ();
04777 }
04778 
04779 static void
04780 dot_rot (type)
04781      int type;
04782 {
04783   offsetT num_regs;
04784   valueT num_alloced = 0;
04785   struct dynreg **drpp, *dr;
04786   int ch, base_reg = 0;
04787   char *name, *start;
04788   size_t len;
04789 
04790   switch (type)
04791     {
04792     case DYNREG_GR: base_reg = REG_GR + 32; break;
04793     case DYNREG_FR: base_reg = REG_FR + 32; break;
04794     case DYNREG_PR: base_reg = REG_P + 16; break;
04795     default: break;
04796     }
04797 
04798   /* First, remove existing names from hash table.  */
04799   for (dr = md.dynreg[type]; dr && dr->num_regs; dr = dr->next)
04800     {
04801       hash_delete (md.dynreg_hash, dr->name);
04802       /* FIXME: Free dr->name.  */
04803       dr->num_regs = 0;
04804     }
04805 
04806   drpp = &md.dynreg[type];
04807   while (1)
04808     {
04809       start = input_line_pointer;
04810       ch = get_symbol_end ();
04811       len = strlen (ia64_canonicalize_symbol_name (start));
04812       *input_line_pointer = ch;
04813 
04814       SKIP_WHITESPACE ();
04815       if (*input_line_pointer != '[')
04816        {
04817          as_bad ("Expected '['");
04818          goto err;
04819        }
04820       ++input_line_pointer; /* skip '[' */
04821 
04822       num_regs = get_absolute_expression ();
04823 
04824       if (*input_line_pointer++ != ']')
04825        {
04826          as_bad ("Expected ']'");
04827          goto err;
04828        }
04829       if (num_regs <= 0)
04830        {
04831          as_bad ("Number of elements must be positive");
04832          goto err;
04833        }
04834       SKIP_WHITESPACE ();
04835 
04836       num_alloced += num_regs;
04837       switch (type)
04838        {
04839        case DYNREG_GR:
04840          if (num_alloced > md.rot.num_regs)
04841            {
04842              as_bad ("Used more than the declared %d rotating registers",
04843                     md.rot.num_regs);
04844              goto err;
04845            }
04846          break;
04847        case DYNREG_FR:
04848          if (num_alloced > 96)
04849            {
04850              as_bad ("Used more than the available 96 rotating registers");
04851              goto err;
04852            }
04853          break;
04854        case DYNREG_PR:
04855          if (num_alloced > 48)
04856            {
04857              as_bad ("Used more than the available 48 rotating registers");
04858              goto err;
04859            }
04860          break;
04861 
04862        default:
04863          break;
04864        }
04865 
04866       if (!*drpp)
04867        {
04868          *drpp = obstack_alloc (&notes, sizeof (*dr));
04869          memset (*drpp, 0, sizeof (*dr));
04870        }
04871 
04872       name = obstack_alloc (&notes, len + 1);
04873       memcpy (name, start, len);
04874       name[len] = '\0';
04875 
04876       dr = *drpp;
04877       dr->name = name;
04878       dr->num_regs = num_regs;
04879       dr->base = base_reg;
04880       drpp = &dr->next;
04881       base_reg += num_regs;
04882 
04883       if (hash_insert (md.dynreg_hash, name, dr))
04884        {
04885          as_bad ("Attempt to redefine register set `%s'", name);
04886          obstack_free (&notes, name);
04887          goto err;
04888        }
04889 
04890       if (*input_line_pointer != ',')
04891        break;
04892       ++input_line_pointer; /* skip comma */
04893       SKIP_WHITESPACE ();
04894     }
04895   demand_empty_rest_of_line ();
04896   return;
04897 
04898  err:
04899   ignore_rest_of_line ();
04900 }
04901 
04902 static void
04903 dot_byteorder (byteorder)
04904      int byteorder;
04905 {
04906   segment_info_type *seginfo = seg_info (now_seg);
04907 
04908   if (byteorder == -1)
04909     {
04910       if (seginfo->tc_segment_info_data.endian == 0)
04911        seginfo->tc_segment_info_data.endian = default_big_endian ? 1 : 2;
04912       byteorder = seginfo->tc_segment_info_data.endian == 1;
04913     }
04914   else
04915     seginfo->tc_segment_info_data.endian = byteorder ? 1 : 2;
04916 
04917   if (target_big_endian != byteorder)
04918     {
04919       target_big_endian = byteorder;
04920       if (target_big_endian)
04921        {
04922          ia64_number_to_chars = number_to_chars_bigendian;
04923          ia64_float_to_chars = ia64_float_to_chars_bigendian;
04924        }
04925       else
04926        {
04927          ia64_number_to_chars = number_to_chars_littleendian;
04928          ia64_float_to_chars = ia64_float_to_chars_littleendian;
04929        }
04930     }
04931 }
04932 
04933 static void
04934 dot_psr (dummy)
04935      int dummy ATTRIBUTE_UNUSED;
04936 {
04937   char *option;
04938   int ch;
04939 
04940   while (1)
04941     {
04942       option = input_line_pointer;
04943       ch = get_symbol_end ();
04944       if (strcmp (option, "lsb") == 0)
04945        md.flags &= ~EF_IA_64_BE;
04946       else if (strcmp (option, "msb") == 0)
04947        md.flags |= EF_IA_64_BE;
04948       else if (strcmp (option, "abi32") == 0)
04949        md.flags &= ~EF_IA_64_ABI64;
04950       else if (strcmp (option, "abi64") == 0)
04951        md.flags |= EF_IA_64_ABI64;
04952       else
04953        as_bad ("Unknown psr option `%s'", option);
04954       *input_line_pointer = ch;
04955 
04956       SKIP_WHITESPACE ();
04957       if (*input_line_pointer != ',')
04958        break;
04959 
04960       ++input_line_pointer;
04961       SKIP_WHITESPACE ();
04962     }
04963   demand_empty_rest_of_line ();
04964 }
04965 
04966 static void
04967 dot_ln (dummy)
04968      int dummy ATTRIBUTE_UNUSED;
04969 {
04970   new_logical_line (0, get_absolute_expression ());
04971   demand_empty_rest_of_line ();
04972 }
04973 
04974 static void
04975 cross_section (ref, cons, ua)
04976      int ref;
04977      void (*cons) PARAMS((int));
04978      int ua;
04979 {
04980   char *start, *end;
04981   int saved_auto_align;
04982   unsigned int section_count;
04983 
04984   SKIP_WHITESPACE ();
04985   start = input_line_pointer;
04986   if (*start == '"')
04987     {
04988       int len;
04989       char *name;
04990 
04991       name = demand_copy_C_string (&len);
04992       obstack_free(&notes, name);
04993       if (!name)
04994        {
04995          ignore_rest_of_line ();
04996          return;
04997        }
04998     }
04999   else
05000     {
05001       char c = get_symbol_end ();
05002 
05003       if (input_line_pointer == start)
05004        {
05005          as_bad ("Missing section name");
05006          ignore_rest_of_line ();
05007          return;
05008        }
05009       *input_line_pointer = c;
05010     }
05011   end = input_line_pointer;
05012   SKIP_WHITESPACE ();
05013   if (*input_line_pointer != ',')
05014     {
05015       as_bad ("Comma expected after section name");
05016       ignore_rest_of_line ();
05017       return;
05018     }
05019   *end = '\0';
05020   end = input_line_pointer + 1;           /* skip comma */
05021   input_line_pointer = start;
05022   md.keep_pending_output = 1;
05023   section_count = bfd_count_sections(stdoutput);
05024   obj_elf_section (0);
05025   if (section_count != bfd_count_sections(stdoutput))
05026     as_warn ("Creating sections with .xdataN/.xrealN/.xstringZ is deprecated.");
05027   input_line_pointer = end;
05028   saved_auto_align = md.auto_align;
05029   if (ua)
05030     md.auto_align = 0;
05031   (*cons) (ref);
05032   if (ua)
05033     md.auto_align = saved_auto_align;
05034   obj_elf_previous (0);
05035   md.keep_pending_output = 0;
05036 }
05037 
05038 static void
05039 dot_xdata (size)
05040      int size;
05041 {
05042   cross_section (size, cons, 0);
05043 }
05044 
05045 /* Why doesn't float_cons() call md_cons_align() the way cons() does?  */
05046 
05047 static void
05048 stmt_float_cons (kind)
05049      int kind;
05050 {
05051   size_t alignment;
05052 
05053   switch (kind)
05054     {
05055     case 'd':
05056       alignment = 8;
05057       break;
05058 
05059     case 'x':
05060     case 'X':
05061       alignment = 16;
05062       break;
05063 
05064     case 'f':
05065     default:
05066       alignment = 4;
05067       break;
05068     }
05069   ia64_do_align (alignment);
05070   float_cons (kind);
05071 }
05072 
05073 static void
05074 stmt_cons_ua (size)
05075      int size;
05076 {
05077   int saved_auto_align = md.auto_align;
05078 
05079   md.auto_align = 0;
05080   cons (size);
05081   md.auto_align = saved_auto_align;
05082 }
05083 
05084 static void
05085 dot_xfloat_cons (kind)
05086      int kind;
05087 {
05088   cross_section (kind, stmt_float_cons, 0);
05089 }
05090 
05091 static void
05092 dot_xstringer (zero)
05093      int zero;
05094 {
05095   cross_section (zero, stringer, 0);
05096 }
05097 
05098 static void
05099 dot_xdata_ua (size)
05100      int size;
05101 {
05102   cross_section (size, cons, 1);
05103 }
05104 
05105 static void
05106 dot_xfloat_cons_ua (kind)
05107      int kind;
05108 {
05109   cross_section (kind, float_cons, 1);
05110 }
05111 
05112 /* .reg.val <regname>,value */
05113 
05114 static void
05115 dot_reg_val (dummy)
05116      int dummy ATTRIBUTE_UNUSED;
05117 {
05118   expressionS reg;
05119 
05120   expression_and_evaluate (&reg);
05121   if (reg.X_op != O_register)
05122     {
05123       as_bad (_("Register name expected"));
05124       ignore_rest_of_line ();
05125     }
05126   else if (*input_line_pointer++ != ',')
05127     {
05128       as_bad (_("Comma expected"));
05129       ignore_rest_of_line ();
05130     }
05131   else
05132     {
05133       valueT value = get_absolute_expression ();
05134       int regno = reg.X_add_number;
05135       if (regno <= REG_GR || regno > REG_GR + 127)
05136        as_warn (_("Register value annotation ignored"));
05137       else
05138        {
05139          gr_values[regno - REG_GR].known = 1;
05140          gr_values[regno - REG_GR].value = value;
05141          gr_values[regno - REG_GR].path = md.path;
05142        }
05143     }
05144   demand_empty_rest_of_line ();
05145 }
05146 
05147 /*
05148   .serialize.data
05149   .serialize.instruction
05150  */
05151 static void
05152 dot_serialize (type)
05153      int type;
05154 {
05155   insn_group_break (0, 0, 0);
05156   if (type)
05157     instruction_serialization ();
05158   else
05159     data_serialization ();
05160   insn_group_break (0, 0, 0);
05161   demand_empty_rest_of_line ();
05162 }
05163 
05164 /* select dv checking mode
05165    .auto
05166    .explicit
05167    .default
05168 
05169    A stop is inserted when changing modes
05170  */
05171 
05172 static void
05173 dot_dv_mode (type)
05174      int type;
05175 {
05176   if (md.manual_bundling)
05177     as_warn (_("Directive invalid within a bundle"));
05178 
05179   if (type == 'E' || type == 'A')
05180     md.mode_explicitly_set = 0;
05181   else
05182     md.mode_explicitly_set = 1;
05183 
05184   md.detect_dv = 1;
05185   switch (type)
05186     {
05187     case 'A':
05188     case 'a':
05189       if (md.explicit_mode)
05190        insn_group_break (1, 0, 0);
05191       md.explicit_mode = 0;
05192       break;
05193     case 'E':
05194     case 'e':
05195       if (!md.explicit_mode)
05196        insn_group_break (1, 0, 0);
05197       md.explicit_mode = 1;
05198       break;
05199     default:
05200     case 'd':
05201       if (md.explicit_mode != md.default_explicit_mode)
05202        insn_group_break (1, 0, 0);
05203       md.explicit_mode = md.default_explicit_mode;
05204       md.mode_explicitly_set = 0;
05205       break;
05206     }
05207 }
05208 
05209 static void
05210 print_prmask (mask)
05211      valueT mask;
05212 {
05213   int regno;
05214   char *comma = "";
05215   for (regno = 0; regno < 64; regno++)
05216     {
05217       if (mask & ((valueT) 1 << regno))
05218        {
05219          fprintf (stderr, "%s p%d", comma, regno);
05220          comma = ",";
05221        }
05222     }
05223 }
05224 
05225 /*
05226   .pred.rel.clear [p1 [,p2 [,...]]]     (also .pred.rel "clear" or @clear)
05227   .pred.rel.imply p1, p2                (also .pred.rel "imply" or @imply)
05228   .pred.rel.mutex p1, p2 [,...]         (also .pred.rel "mutex" or @mutex)
05229   .pred.safe_across_calls p1 [, p2 [,...]]
05230  */
05231 
05232 static void
05233 dot_pred_rel (type)
05234      int type;
05235 {
05236   valueT mask = 0;
05237   int count = 0;
05238   int p1 = -1, p2 = -1;
05239 
05240   if (type == 0)
05241     {
05242       if (*input_line_pointer == '"')
05243        {
05244          int len;
05245          char *form = demand_copy_C_string (&len);
05246 
05247          if (strcmp (form, "mutex") == 0)
05248            type = 'm';
05249          else if (strcmp (form, "clear") == 0)
05250            type = 'c';
05251          else if (strcmp (form, "imply") == 0)
05252            type = 'i';
05253          obstack_free (&notes, form);
05254        }
05255       else if (*input_line_pointer == '@')
05256        {
05257          char *form = ++input_line_pointer;
05258          char c = get_symbol_end();
05259 
05260          if (strcmp (form, "mutex") == 0)
05261            type = 'm';
05262          else if (strcmp (form, "clear") == 0)
05263            type = 'c';
05264          else if (strcmp (form, "imply") == 0)
05265            type = 'i';
05266          *input_line_pointer = c;
05267        }
05268       else
05269        {
05270          as_bad (_("Missing predicate relation type"));
05271          ignore_rest_of_line ();
05272          return;
05273        }
05274       if (type == 0)
05275        {
05276          as_bad (_("Unrecognized predicate relation type"));
05277          ignore_rest_of_line ();
05278          return;
05279        }
05280       if (*input_line_pointer == ',')
05281        ++input_line_pointer;
05282       SKIP_WHITESPACE ();
05283     }
05284 
05285   SKIP_WHITESPACE ();
05286   while (1)
05287     {
05288       valueT bits = 1;
05289       int regno;
05290       expressionS pr, *pr1, *pr2;
05291 
05292       expression_and_evaluate (&pr);
05293       if (pr.X_op == O_register
05294          && pr.X_add_number >= REG_P
05295          && pr.X_add_number <= REG_P + 63)
05296        {
05297          regno = pr.X_add_number - REG_P;
05298          bits <<= regno;
05299          count++;
05300          if (p1 == -1)
05301            p1 = regno;
05302          else if (p2 == -1)
05303            p2 = regno;
05304        }
05305       else if (type != 'i'
05306          && pr.X_op == O_subtract
05307          && (pr1 = symbol_get_value_expression (pr.X_add_symbol))
05308          && pr1->X_op == O_register
05309          && pr1->X_add_number >= REG_P
05310          && pr1->X_add_number <= REG_P + 63
05311          && (pr2 = symbol_get_value_expression (pr.X_op_symbol))
05312          && pr2->X_op == O_register
05313          && pr2->X_add_number >= REG_P
05314          && pr2->X_add_number <= REG_P + 63)
05315        {
05316          /* It's a range.  */
05317          int stop;
05318 
05319          regno = pr1->X_add_number - REG_P;
05320          stop = pr2->X_add_number - REG_P;
05321          if (regno >= stop)
05322            {
05323              as_bad (_("Bad register range"));
05324              ignore_rest_of_line ();
05325              return;
05326            }
05327          bits = ((bits << stop) << 1) - (bits << regno);
05328          count += stop - regno + 1;
05329        }
05330       else
05331        {
05332          as_bad (_("Predicate register expected"));
05333          ignore_rest_of_line ();
05334          return;
05335        }
05336       if (mask & bits)
05337        as_warn (_("Duplicate predicate register ignored"));
05338       mask |= bits;
05339       if (*input_line_pointer != ',')
05340        break;
05341       ++input_line_pointer;
05342       SKIP_WHITESPACE ();
05343     }
05344 
05345   switch (type)
05346     {
05347     case 'c':
05348       if (count == 0)
05349        mask = ~(valueT) 0;
05350       clear_qp_mutex (mask);
05351       clear_qp_implies (mask, (valueT) 0);
05352       break;
05353     case 'i':
05354       if (count != 2 || p1 == -1 || p2 == -1)
05355        as_bad (_("Predicate source and target required"));
05356       else if (p1 == 0 || p2 == 0)
05357        as_bad (_("Use of p0 is not valid in this context"));
05358       else
05359        add_qp_imply (p1, p2);
05360       break;
05361     case 'm':
05362       if (count < 2)
05363        {
05364          as_bad (_("At least two PR arguments expected"));
05365          break;
05366        }
05367       else if (mask & 1)
05368        {
05369          as_bad (_("Use of p0 is not valid in this context"));
05370          break;
05371        }
05372       add_qp_mutex (mask);
05373       break;
05374     case 's':
05375       /* note that we don't override any existing relations */
05376       if (count == 0)
05377        {
05378          as_bad (_("At least one PR argument expected"));
05379          break;
05380        }
05381       if (md.debug_dv)
05382        {
05383          fprintf (stderr, "Safe across calls: ");
05384          print_prmask (mask);
05385          fprintf (stderr, "\n");
05386        }
05387       qp_safe_across_calls = mask;
05388       break;
05389     }
05390   demand_empty_rest_of_line ();
05391 }
05392 
05393 /* .entry label [, label [, ...]]
05394    Hint to DV code that the given labels are to be considered entry points.
05395    Otherwise, only global labels are considered entry points.  */
05396 
05397 static void
05398 dot_entry (dummy)
05399      int dummy ATTRIBUTE_UNUSED;
05400 {
05401   const char *err;
05402   char *name;
05403   int c;
05404   symbolS *symbolP;
05405 
05406   do
05407     {
05408       name = input_line_pointer;
05409       c = get_symbol_end ();
05410       symbolP = symbol_find_or_make (name);
05411 
05412       err = hash_insert (md.entry_hash, S_GET_NAME (symbolP), (PTR) symbolP);
05413       if (err)
05414        as_fatal (_("Inserting \"%s\" into entry hint table failed: %s"),
05415                 name, err);
05416 
05417       *input_line_pointer = c;
05418       SKIP_WHITESPACE ();
05419       c = *input_line_pointer;
05420       if (c == ',')
05421        {
05422          input_line_pointer++;
05423          SKIP_WHITESPACE ();
05424          if (*input_line_pointer == '\n')
05425            c = '\n';
05426        }
05427     }
05428   while (c == ',');
05429 
05430   demand_empty_rest_of_line ();
05431 }
05432 
05433 /* .mem.offset offset, base
05434    "base" is used to distinguish between offsets from a different base.  */
05435 
05436 static void
05437 dot_mem_offset (dummy)
05438   int dummy ATTRIBUTE_UNUSED;
05439 {
05440   md.mem_offset.hint = 1;
05441   md.mem_offset.offset = get_absolute_expression ();
05442   if (*input_line_pointer != ',')
05443     {
05444       as_bad (_("Comma expected"));
05445       ignore_rest_of_line ();
05446       return;
05447     }
05448   ++input_line_pointer;
05449   md.mem_offset.base = get_absolute_expression ();
05450   demand_empty_rest_of_line ();
05451 }
05452 
05453 /* ia64-specific pseudo-ops:  */
05454 const pseudo_typeS md_pseudo_table[] =
05455   {
05456     { "radix", dot_radix, 0 },
05457     { "lcomm", s_lcomm_bytes, 1 },
05458     { "loc", dot_loc, 0 },
05459     { "bss", dot_special_section, SPECIAL_SECTION_BSS },
05460     { "sbss", dot_special_section, SPECIAL_SECTION_SBSS },
05461     { "sdata", dot_special_section, SPECIAL_SECTION_SDATA },
05462     { "rodata", dot_special_section, SPECIAL_SECTION_RODATA },
05463     { "comment", dot_special_section, SPECIAL_SECTION_COMMENT },
05464     { "ia_64.unwind", dot_special_section, SPECIAL_SECTION_UNWIND },
05465     { "ia_64.unwind_info", dot_special_section, SPECIAL_SECTION_UNWIND_INFO },
05466     { "init_array", dot_special_section, SPECIAL_SECTION_INIT_ARRAY },
05467     { "fini_array", dot_special_section, SPECIAL_SECTION_FINI_ARRAY },
05468     { "proc", dot_proc, 0 },
05469     { "body", dot_body, 0 },
05470     { "prologue", dot_prologue, 0 },
05471     { "endp", dot_endp, 0 },
05472 
05473     { "fframe", dot_fframe, 0 },
05474     { "vframe", dot_vframe, 0 },
05475     { "vframesp", dot_vframesp, 0 },
05476     { "vframepsp", dot_vframesp, 1 },
05477     { "save", dot_save, 0 },
05478     { "restore", dot_restore, 0 },
05479     { "restorereg", dot_restorereg, 0 },
05480     { "restorereg.p", dot_restorereg, 1 },
05481     { "handlerdata", dot_handlerdata, 0 },
05482     { "unwentry", dot_unwentry, 0 },
05483     { "altrp", dot_altrp, 0 },
05484     { "savesp", dot_savemem, 0 },
05485     { "savepsp", dot_savemem, 1 },
05486     { "save.g", dot_saveg, 0 },
05487     { "save.f", dot_savef, 0 },
05488     { "save.b", dot_saveb, 0 },
05489     { "save.gf", dot_savegf, 0 },
05490     { "spill", dot_spill, 0 },
05491     { "spillreg", dot_spillreg, 0 },
05492     { "spillsp", dot_spillmem, 0 },
05493     { "spillpsp", dot_spillmem, 1 },
05494     { "spillreg.p", dot_spillreg, 1 },
05495     { "spillsp.p", dot_spillmem, ~0 },
05496     { "spillpsp.p", dot_spillmem, ~1 },
05497     { "label_state", dot_label_state, 0 },
05498     { "copy_state", dot_copy_state, 0 },
05499     { "unwabi", dot_unwabi, 0 },
05500     { "personality", dot_personality, 0 },
05501     { "mii", dot_template, 0x0 },
05502     { "mli", dot_template, 0x2 }, /* old format, for compatibility */
05503     { "mlx", dot_template, 0x2 },
05504     { "mmi", dot_template, 0x4 },
05505     { "mfi", dot_template, 0x6 },
05506     { "mmf", dot_template, 0x7 },
05507     { "mib", dot_template, 0x8 },
05508     { "mbb", dot_template, 0x9 },
05509     { "bbb", dot_template, 0xb },
05510     { "mmb", dot_template, 0xc },
05511     { "mfb", dot_template, 0xe },
05512     { "align", dot_align, 0 },
05513     { "regstk", dot_regstk, 0 },
05514     { "rotr", dot_rot, DYNREG_GR },
05515     { "rotf", dot_rot, DYNREG_FR },
05516     { "rotp", dot_rot, DYNREG_PR },
05517     { "lsb", dot_byteorder, 0 },
05518     { "msb", dot_byteorder, 1 },
05519     { "psr", dot_psr, 0 },
05520     { "alias", dot_alias, 0 },
05521     { "secalias", dot_alias, 1 },
05522     { "ln", dot_ln, 0 },           /* source line info (for debugging) */
05523 
05524     { "xdata1", dot_xdata, 1 },
05525     { "xdata2", dot_xdata, 2 },
05526     { "xdata4", dot_xdata, 4 },
05527     { "xdata8", dot_xdata, 8 },
05528     { "xdata16", dot_xdata, 16 },
05529     { "xreal4", dot_xfloat_cons, 'f' },
05530     { "xreal8", dot_xfloat_cons, 'd' },
05531     { "xreal10", dot_xfloat_cons, 'x' },
05532     { "xreal16", dot_xfloat_cons, 'X' },
05533     { "xstring", dot_xstringer, 0 },
05534     { "xstringz", dot_xstringer, 1 },
05535 
05536     /* unaligned versions:  */
05537     { "xdata2.ua", dot_xdata_ua, 2 },
05538     { "xdata4.ua", dot_xdata_ua, 4 },
05539     { "xdata8.ua", dot_xdata_ua, 8 },
05540     { "xdata16.ua", dot_xdata_ua, 16 },
05541     { "xreal4.ua", dot_xfloat_cons_ua, 'f' },
05542     { "xreal8.ua", dot_xfloat_cons_ua, 'd' },
05543     { "xreal10.ua", dot_xfloat_cons_ua, 'x' },
05544     { "xreal16.ua", dot_xfloat_cons_ua, 'X' },
05545 
05546     /* annotations/DV checking support */
05547     { "entry", dot_entry, 0 },
05548     { "mem.offset", dot_mem_offset, 0 },
05549     { "pred.rel", dot_pred_rel, 0 },
05550     { "pred.rel.clear", dot_pred_rel, 'c' },
05551     { "pred.rel.imply", dot_pred_rel, 'i' },
05552     { "pred.rel.mutex", dot_pred_rel, 'm' },
05553     { "pred.safe_across_calls", dot_pred_rel, 's' },
05554     { "reg.val", dot_reg_val, 0 },
05555     { "serialize.data", dot_serialize, 0 },
05556     { "serialize.instruction", dot_serialize, 1 },
05557     { "auto", dot_dv_mode, 'a' },
05558     { "explicit", dot_dv_mode, 'e' },
05559     { "default", dot_dv_mode, 'd' },
05560 
05561     /* ??? These are needed to make gas/testsuite/gas/elf/ehopt.s work.
05562        IA-64 aligns data allocation pseudo-ops by default, so we have to
05563        tell it that these ones are supposed to be unaligned.  Long term,
05564        should rewrite so that only IA-64 specific data allocation pseudo-ops
05565        are aligned by default.  */
05566     {"2byte", stmt_cons_ua, 2},
05567     {"4byte", stmt_cons_ua, 4},
05568     {"8byte", stmt_cons_ua, 8},
05569 
05570     { NULL, 0, 0 }
05571   };
05572 
05573 static const struct pseudo_opcode
05574   {
05575     const char *name;
05576     void (*handler) (int);
05577     int arg;
05578   }
05579 pseudo_opcode[] =
05580   {
05581     /* these are more like pseudo-ops, but don't start with a dot */
05582     { "data1", cons, 1 },
05583     { "data2", cons, 2 },
05584     { "data4", cons, 4 },
05585     { "data8", cons, 8 },
05586     { "data16", cons, 16 },
05587     { "real4", stmt_float_cons, 'f' },
05588     { "real8", stmt_float_cons, 'd' },
05589     { "real10", stmt_float_cons, 'x' },
05590     { "real16", stmt_float_cons, 'X' },
05591     { "string", stringer, 0 },
05592     { "stringz", stringer, 1 },
05593 
05594     /* unaligned versions:  */
05595     { "data2.ua", stmt_cons_ua, 2 },
05596     { "data4.ua", stmt_cons_ua, 4 },
05597     { "data8.ua", stmt_cons_ua, 8 },
05598     { "data16.ua", stmt_cons_ua, 16 },
05599     { "real4.ua", float_cons, 'f' },
05600     { "real8.ua", float_cons, 'd' },
05601     { "real10.ua", float_cons, 'x' },
05602     { "real16.ua", float_cons, 'X' },
05603   };
05604 
05605 /* Declare a register by creating a symbol for it and entering it in
05606    the symbol table.  */
05607 
05608 static symbolS *
05609 declare_register (name, regnum)
05610      const char *name;
05611      unsigned int regnum;
05612 {
05613   const char *err;
05614   symbolS *sym;
05615 
05616   sym = symbol_create (name, reg_section, regnum, &zero_address_frag);
05617 
05618   err = hash_insert (md.reg_hash, S_GET_NAME (sym), (PTR) sym);
05619   if (err)
05620     as_fatal ("Inserting \"%s\" into register table failed: %s",
05621              name, err);
05622 
05623   return sym;
05624 }
05625 
05626 static void
05627 declare_register_set (prefix, num_regs, base_regnum)
05628      const char *prefix;
05629      unsigned int num_regs;
05630      unsigned int base_regnum;
05631 {
05632   char name[8];
05633   unsigned int i;
05634 
05635   for (i = 0; i < num_regs; ++i)
05636     {
05637       snprintf (name, sizeof (name), "%s%u", prefix, i);
05638       declare_register (name, base_regnum + i);
05639     }
05640 }
05641 
05642 static unsigned int
05643 operand_width (opnd)
05644      enum ia64_opnd opnd;
05645 {
05646   const struct ia64_operand *odesc = &elf64_ia64_operands[opnd];
05647   unsigned int bits = 0;
05648   int i;
05649 
05650   bits = 0;
05651   for (i = 0; i < NELEMS (odesc->field) && odesc->field[i].bits; ++i)
05652     bits += odesc->field[i].bits;
05653 
05654   return bits;
05655 }
05656 
05657 static enum operand_match_result
05658 operand_match (idesc, index, e)
05659      const struct ia64_opcode *idesc;
05660      int index;
05661      expressionS *e;
05662 {
05663   enum ia64_opnd opnd = idesc->operands[index];
05664   int bits, relocatable = 0;
05665   struct insn_fix *fix;
05666   bfd_signed_vma val;
05667 
05668   switch (opnd)
05669     {
05670       /* constants:  */
05671 
05672     case IA64_OPND_AR_CCV:
05673       if (e->X_op == O_register && e->X_add_number == REG_AR + 32)
05674        return OPERAND_MATCH;
05675       break;
05676 
05677     case IA64_OPND_AR_CSD:
05678       if (e->X_op == O_register && e->X_add_number == REG_AR + 25)
05679        return OPERAND_MATCH;
05680       break;
05681 
05682     case IA64_OPND_AR_PFS:
05683       if (e->X_op == O_register && e->X_add_number == REG_AR + 64)
05684        return OPERAND_MATCH;
05685       break;
05686 
05687     case IA64_OPND_GR0:
05688       if (e->X_op == O_register && e->X_add_number == REG_GR + 0)
05689        return OPERAND_MATCH;
05690       break;
05691 
05692     case IA64_OPND_IP:
05693       if (e->X_op == O_register && e->X_add_number == REG_IP)
05694        return OPERAND_MATCH;
05695       break;
05696 
05697     case IA64_OPND_PR:
05698       if (e->X_op == O_register && e->X_add_number == REG_PR)
05699        return OPERAND_MATCH;
05700       break;
05701 
05702     case IA64_OPND_PR_ROT:
05703       if (e->X_op == O_register && e->X_add_number == REG_PR_ROT)
05704        return OPERAND_MATCH;
05705       break;
05706 
05707     case IA64_OPND_PSR:
05708       if (e->X_op == O_register && e->X_add_number == REG_PSR)
05709        return OPERAND_MATCH;
05710       break;
05711 
05712     case IA64_OPND_PSR_L:
05713       if (e->X_op == O_register && e->X_add_number == REG_PSR_L)
05714        return OPERAND_MATCH;
05715       break;
05716 
05717     case IA64_OPND_PSR_UM:
05718       if (e->X_op == O_register && e->X_add_number == REG_PSR_UM)
05719        return OPERAND_MATCH;
05720       break;
05721 
05722     case IA64_OPND_C1:
05723       if (e->X_op == O_constant)
05724        {
05725          if (e->X_add_number == 1)
05726            return OPERAND_MATCH;
05727          else
05728            return OPERAND_OUT_OF_RANGE;
05729        }
05730       break;
05731 
05732     case IA64_OPND_C8:
05733       if (e->X_op == O_constant)
05734        {
05735          if (e->X_add_number == 8)
05736            return OPERAND_MATCH;
05737          else
05738            return OPERAND_OUT_OF_RANGE;
05739        }
05740       break;
05741 
05742     case IA64_OPND_C16:
05743       if (e->X_op == O_constant)
05744        {
05745          if (e->X_add_number == 16)
05746            return OPERAND_MATCH;
05747          else
05748            return OPERAND_OUT_OF_RANGE;
05749        }
05750       break;
05751 
05752       /* register operands:  */
05753 
05754     case IA64_OPND_AR3:
05755       if (e->X_op == O_register && e->X_add_number >= REG_AR
05756          && e->X_add_number < REG_AR + 128)
05757        return OPERAND_MATCH;
05758       break;
05759 
05760     case IA64_OPND_B1:
05761     case IA64_OPND_B2:
05762       if (e->X_op == O_register && e->X_add_number >= REG_BR
05763          && e->X_add_number < REG_BR + 8)
05764        return OPERAND_MATCH;
05765       break;
05766 
05767     case IA64_OPND_CR3:
05768       if (e->X_op == O_register && e->X_add_number >= REG_CR
05769          && e->X_add_number < REG_CR + 128)
05770        return OPERAND_MATCH;
05771       break;
05772 
05773     case IA64_OPND_F1:
05774     case IA64_OPND_F2:
05775     case IA64_OPND_F3:
05776     case IA64_OPND_F4:
05777       if (e->X_op == O_register && e->X_add_number >= REG_FR
05778          && e->X_add_number < REG_FR + 128)
05779        return OPERAND_MATCH;
05780       break;
05781 
05782     case IA64_OPND_P1:
05783     case IA64_OPND_P2:
05784       if (e->X_op == O_register && e->X_add_number >= REG_P
05785          && e->X_add_number < REG_P + 64)
05786        return OPERAND_MATCH;
05787       break;
05788 
05789     case IA64_OPND_R1:
05790     case IA64_OPND_R2:
05791     case IA64_OPND_R3:
05792       if (e->X_op == O_register && e->X_add_number >= REG_GR
05793          && e->X_add_number < REG_GR + 128)
05794        return OPERAND_MATCH;
05795       break;
05796 
05797     case IA64_OPND_R3_2:
05798       if (e->X_op == O_register && e->X_add_number >= REG_GR)
05799        {
05800          if (e->X_add_number < REG_GR + 4)
05801            return OPERAND_MATCH;
05802          else if (e->X_add_number < REG_GR + 128)
05803            return OPERAND_OUT_OF_RANGE;
05804        }
05805       break;
05806 
05807       /* indirect operands:  */
05808     case IA64_OPND_CPUID_R3:
05809     case IA64_OPND_DBR_R3:
05810     case IA64_OPND_DTR_R3:
05811     case IA64_OPND_ITR_R3:
05812     case IA64_OPND_IBR_R3:
05813     case IA64_OPND_MSR_R3:
05814     case IA64_OPND_PKR_R3:
05815     case IA64_OPND_PMC_R3:
05816     case IA64_OPND_PMD_R3:
05817     case IA64_OPND_RR_R3:
05818       if (e->X_op == O_index && e->X_op_symbol
05819          && (S_GET_VALUE (e->X_op_symbol) - IND_CPUID
05820              == opnd - IA64_OPND_CPUID_R3))
05821        return OPERAND_MATCH;
05822       break;
05823 
05824     case IA64_OPND_MR3:
05825       if (e->X_op == O_index && !e->X_op_symbol)
05826        return OPERAND_MATCH;
05827       break;
05828 
05829       /* immediate operands:  */
05830     case IA64_OPND_CNT2a:
05831     case IA64_OPND_LEN4:
05832     case IA64_OPND_LEN6:
05833       bits = operand_width (idesc->operands[index]);
05834       if (e->X_op == O_constant)
05835        {
05836          if ((bfd_vma) (e->X_add_number - 1) < ((bfd_vma) 1 << bits))
05837            return OPERAND_MATCH;
05838          else
05839            return OPERAND_OUT_OF_RANGE;
05840        }
05841       break;
05842 
05843     case IA64_OPND_CNT2b:
05844       if (e->X_op == O_constant)
05845        {
05846          if ((bfd_vma) (e->X_add_number - 1) < 3)
05847            return OPERAND_MATCH;
05848          else
05849            return OPERAND_OUT_OF_RANGE;
05850        }
05851       break;
05852 
05853     case IA64_OPND_CNT2c:
05854       val = e->X_add_number;
05855       if (e->X_op == O_constant)
05856        {
05857          if ((val == 0 || val == 7 || val == 15 || val == 16))
05858            return OPERAND_MATCH;
05859          else
05860            return OPERAND_OUT_OF_RANGE;
05861        }
05862       break;
05863 
05864     case IA64_OPND_SOR:
05865       /* SOR must be an integer multiple of 8 */
05866       if (e->X_op == O_constant && e->X_add_number & 0x7)
05867        return OPERAND_OUT_OF_RANGE;
05868     case IA64_OPND_SOF:
05869     case IA64_OPND_SOL:
05870       if (e->X_op == O_constant)
05871        {
05872          if ((bfd_vma) e->X_add_number <= 96)
05873            return OPERAND_MATCH;
05874          else
05875            return OPERAND_OUT_OF_RANGE;
05876        }
05877       break;
05878 
05879     case IA64_OPND_IMMU62:
05880       if (e->X_op == O_constant)
05881        {
05882          if ((bfd_vma) e->X_add_number < ((bfd_vma) 1 << 62))
05883            return OPERAND_MATCH;
05884          else
05885            return OPERAND_OUT_OF_RANGE;
05886        }
05887       else
05888        {
05889          /* FIXME -- need 62-bit relocation type */
05890          as_bad (_("62-bit relocation not yet implemented"));
05891        }
05892       break;
05893 
05894     case IA64_OPND_IMMU64:
05895       if (e->X_op == O_symbol || e->X_op == O_pseudo_fixup
05896          || e->X_op == O_subtract)
05897        {
05898          fix = CURR_SLOT.fixup + CURR_SLOT.num_fixups;
05899          fix->code = BFD_RELOC_IA64_IMM64;
05900          if (e->X_op != O_subtract)
05901            {
05902              fix->code = ia64_gen_real_reloc_type (e->X_op_symbol, fix->code);
05903              if (e->X_op == O_pseudo_fixup)
05904               e->X_op = O_symbol;
05905            }
05906 
05907          fix->opnd = idesc->operands[index];
05908          fix->expr = *e;
05909          fix->is_pcrel = 0;
05910          ++CURR_SLOT.num_fixups;
05911          return OPERAND_MATCH;
05912        }
05913       else if (e->X_op == O_constant)
05914        return OPERAND_MATCH;
05915       break;
05916 
05917     case IA64_OPND_IMMU5b:
05918       if (e->X_op == O_constant)
05919        {
05920          val = e->X_add_number;
05921          if (val >= 32 && val <= 63)
05922            return OPERAND_MATCH;
05923          else
05924            return OPERAND_OUT_OF_RANGE;
05925        }
05926       break;
05927 
05928     case IA64_OPND_CCNT5:
05929     case IA64_OPND_CNT5:
05930     case IA64_OPND_CNT6:
05931     case IA64_OPND_CPOS6a:
05932     case IA64_OPND_CPOS6b:
05933     case IA64_OPND_CPOS6c:
05934     case IA64_OPND_IMMU2:
05935     case IA64_OPND_IMMU7a:
05936     case IA64_OPND_IMMU7b:
05937     case IA64_OPND_IMMU21:
05938     case IA64_OPND_IMMU24:
05939     case IA64_OPND_MBTYPE4:
05940     case IA64_OPND_MHTYPE8:
05941     case IA64_OPND_POS6:
05942       bits = operand_width (idesc->operands[index]);
05943       if (e->X_op == O_constant)
05944        {
05945          if ((bfd_vma) e->X_add_number < ((bfd_vma) 1 << bits))
05946            return OPERAND_MATCH;
05947          else
05948            return OPERAND_OUT_OF_RANGE;
05949        }
05950       break;
05951 
05952     case IA64_OPND_IMMU9:
05953       bits = operand_width (idesc->operands[index]);
05954       if (e->X_op == O_constant)
05955        {
05956          if ((bfd_vma) e->X_add_number < ((bfd_vma) 1 << bits))
05957            {
05958              int lobits = e->X_add_number & 0x3;
05959              if (((bfd_vma) e->X_add_number & 0x3C) != 0 && lobits == 0)
05960               e->X_add_number |= (bfd_vma) 0x3;
05961              return OPERAND_MATCH;
05962            }
05963          else
05964            return OPERAND_OUT_OF_RANGE;
05965        }
05966       break;
05967 
05968     case IA64_OPND_IMM44:
05969       /* least 16 bits must be zero */
05970       if ((e->X_add_number & 0xffff) != 0)
05971        /* XXX technically, this is wrong: we should not be issuing warning
05972           messages until we're sure this instruction pattern is going to
05973           be used! */
05974        as_warn (_("lower 16 bits of mask ignored"));
05975 
05976       if (e->X_op == O_constant)
05977        {
05978          if (((e->X_add_number >= 0
05979               && (bfd_vma) e->X_add_number < ((bfd_vma) 1 << 44))
05980               || (e->X_add_number < 0
05981                  && (bfd_vma) -e->X_add_number <= ((bfd_vma) 1 << 44))))
05982            {
05983              /* sign-extend */
05984              if (e->X_add_number >= 0
05985                 && (e->X_add_number & ((bfd_vma) 1 << 43)) != 0)
05986               {
05987                 e->X_add_number |= ~(((bfd_vma) 1 << 44) - 1);
05988               }
05989              return OPERAND_MATCH;
05990            }
05991          else
05992            return OPERAND_OUT_OF_RANGE;
05993        }
05994       break;
05995 
05996     case IA64_OPND_IMM17:
05997       /* bit 0 is a don't care (pr0 is hardwired to 1) */
05998       if (e->X_op == O_constant)
05999        {
06000          if (((e->X_add_number >= 0
06001               && (bfd_vma) e->X_add_number < ((bfd_vma) 1 << 17))
06002               || (e->X_add_number < 0
06003                  && (bfd_vma) -e->X_add_number <= ((bfd_vma) 1 << 17))))
06004            {
06005              /* sign-extend */
06006              if (e->X_add_number >= 0
06007                 && (e->X_add_number & ((bfd_vma) 1 << 16)) != 0)
06008               {
06009                 e->X_add_number |= ~(((bfd_vma) 1 << 17) - 1);
06010               }
06011              return OPERAND_MATCH;
06012            }
06013          else
06014            return OPERAND_OUT_OF_RANGE;
06015        }
06016       break;
06017 
06018     case IA64_OPND_IMM14:
06019     case IA64_OPND_IMM22:
06020       relocatable = 1;
06021     case IA64_OPND_IMM1:
06022     case IA64_OPND_IMM8:
06023     case IA64_OPND_IMM8U4:
06024     case IA64_OPND_IMM8M1:
06025     case IA64_OPND_IMM8M1U4:
06026     case IA64_OPND_IMM8M1U8:
06027     case IA64_OPND_IMM9a:
06028     case IA64_OPND_IMM9b:
06029       bits = operand_width (idesc->operands[index]);
06030       if (relocatable && (e->X_op == O_symbol
06031                        || e->X_op == O_subtract
06032                        || e->X_op == O_pseudo_fixup))
06033        {
06034          fix = CURR_SLOT.fixup + CURR_SLOT.num_fixups;
06035 
06036          if (idesc->operands[index] == IA64_OPND_IMM14)
06037            fix->code = BFD_RELOC_IA64_IMM14;
06038          else
06039            fix->code = BFD_RELOC_IA64_IMM22;
06040 
06041          if (e->X_op != O_subtract)
06042            {
06043              fix->code = ia64_gen_real_reloc_type (e->X_op_symbol, fix->code);
06044              if (e->X_op == O_pseudo_fixup)
06045               e->X_op = O_symbol;
06046            }
06047 
06048          fix->opnd = idesc->operands[index];
06049          fix->expr = *e;
06050          fix->is_pcrel = 0;
06051          ++CURR_SLOT.num_fixups;
06052          return OPERAND_MATCH;
06053        }
06054       else if (e->X_op != O_constant
06055               && ! (e->X_op == O_big && opnd == IA64_OPND_IMM8M1U8))
06056        return OPERAND_MISMATCH;
06057 
06058       if (opnd == IA64_OPND_IMM8M1U4)
06059        {
06060          /* Zero is not valid for unsigned compares that take an adjusted
06061             constant immediate range.  */
06062          if (e->X_add_number == 0)
06063            return OPERAND_OUT_OF_RANGE;
06064 
06065          /* Sign-extend 32-bit unsigned numbers, so that the following range
06066             checks will work.  */
06067          val = e->X_add_number;
06068          if (((val & (~(bfd_vma) 0 << 32)) == 0)
06069              && ((val & ((bfd_vma) 1 << 31)) != 0))
06070            val = ((val << 32) >> 32);
06071 
06072          /* Check for 0x100000000.  This is valid because
06073             0x100000000-1 is the same as ((uint32_t) -1).  */
06074          if (val == ((bfd_signed_vma) 1 << 32))
06075            return OPERAND_MATCH;
06076 
06077          val = val - 1;
06078        }
06079       else if (opnd == IA64_OPND_IMM8M1U8)
06080        {
06081          /* Zero is not valid for unsigned compares that take an adjusted
06082             constant immediate range.  */
06083          if (e->X_add_number == 0)
06084            return OPERAND_OUT_OF_RANGE;
06085 
06086          /* Check for 0x10000000000000000.  */
06087          if (e->X_op == O_big)
06088            {
06089              if (generic_bignum[0] == 0
06090                 && generic_bignum[1] == 0
06091                 && generic_bignum[2] == 0
06092                 && generic_bignum[3] == 0
06093                 && generic_bignum[4] == 1)
06094               return OPERAND_MATCH;
06095              else
06096               return OPERAND_OUT_OF_RANGE;
06097            }
06098          else
06099            val = e->X_add_number - 1;
06100        }
06101       else if (opnd == IA64_OPND_IMM8M1)
06102        val = e->X_add_number - 1;
06103       else if (opnd == IA64_OPND_IMM8U4)
06104        {
06105          /* Sign-extend 32-bit unsigned numbers, so that the following range
06106             checks will work.  */
06107          val = e->X_add_number;
06108          if (((val & (~(bfd_vma) 0 << 32)) == 0)
06109              && ((val & ((bfd_vma) 1 << 31)) != 0))
06110            val = ((val << 32) >> 32);
06111        }
06112       else
06113        val = e->X_add_number;
06114 
06115       if ((val >= 0 && (bfd_vma) val < ((bfd_vma) 1 << (bits - 1)))
06116          || (val < 0 && (bfd_vma) -val <= ((bfd_vma) 1 << (bits - 1))))
06117        return OPERAND_MATCH;
06118       else
06119        return OPERAND_OUT_OF_RANGE;
06120 
06121     case IA64_OPND_INC3:
06122       /* +/- 1, 4, 8, 16 */
06123       val = e->X_add_number;
06124       if (val < 0)
06125        val = -val;
06126       if (e->X_op == O_constant)
06127        {
06128          if ((val == 1 || val == 4 || val == 8 || val == 16))
06129            return OPERAND_MATCH;
06130          else
06131            return OPERAND_OUT_OF_RANGE;
06132        }
06133       break;
06134 
06135     case IA64_OPND_TGT25:
06136     case IA64_OPND_TGT25b:
06137     case IA64_OPND_TGT25c:
06138     case IA64_OPND_TGT64:
06139       if (e->X_op == O_symbol)
06140        {
06141          fix = CURR_SLOT.fixup + CURR_SLOT.num_fixups;
06142          if (opnd == IA64_OPND_TGT25)
06143            fix->code = BFD_RELOC_IA64_PCREL21F;
06144          else if (opnd == IA64_OPND_TGT25b)
06145            fix->code = BFD_RELOC_IA64_PCREL21M;
06146          else if (opnd == IA64_OPND_TGT25c)
06147            fix->code = BFD_RELOC_IA64_PCREL21B;
06148          else if (opnd == IA64_OPND_TGT64)
06149            fix->code = BFD_RELOC_IA64_PCREL60B;
06150          else
06151            abort ();
06152 
06153          fix->code = ia64_gen_real_reloc_type (e->X_op_symbol, fix->code);
06154          fix->opnd = idesc->operands[index];
06155          fix->expr = *e;
06156          fix->is_pcrel = 1;
06157          ++CURR_SLOT.num_fixups;
06158          return OPERAND_MATCH;
06159        }
06160     case IA64_OPND_TAG13:
06161     case IA64_OPND_TAG13b:
06162       switch (e->X_op)
06163        {
06164        case O_constant:
06165          return OPERAND_MATCH;
06166 
06167        case O_symbol:
06168          fix = CURR_SLOT.fixup + CURR_SLOT.num_fixups;
06169          /* There are no external relocs for TAG13/TAG13b fields, so we
06170             create a dummy reloc.  This will not live past md_apply_fix.  */
06171          fix->code = BFD_RELOC_UNUSED;
06172          fix->code = ia64_gen_real_reloc_type (e->X_op_symbol, fix->code);
06173          fix->opnd = idesc->operands[index];
06174          fix->expr = *e;
06175          fix->is_pcrel = 1;
06176          ++CURR_SLOT.num_fixups;
06177          return OPERAND_MATCH;
06178 
06179        default:
06180          break;
06181        }
06182       break;
06183 
06184     case IA64_OPND_LDXMOV:
06185       fix = CURR_SLOT.fixup + CURR_SLOT.num_fixups;
06186       fix->code = BFD_RELOC_IA64_LDXMOV;
06187       fix->opnd = idesc->operands[index];
06188       fix->expr = *e;
06189       fix->is_pcrel = 0;
06190       ++CURR_SLOT.num_fixups;
06191       return OPERAND_MATCH;
06192 
06193     default:
06194       break;
06195     }
06196   return OPERAND_MISMATCH;
06197 }
06198 
06199 static int
06200 parse_operand (e, more)
06201      expressionS *e;
06202      int more;
06203 {
06204   int sep = '\0';
06205 
06206   memset (e, 0, sizeof (*e));
06207   e->X_op = O_absent;
06208   SKIP_WHITESPACE ();
06209   expression_and_evaluate (e);
06210   sep = *input_line_pointer;
06211   if (more && (sep == ',' || sep == more))
06212     ++input_line_pointer;
06213   return sep;
06214 }
06215 
06216 /* Returns the next entry in the opcode table that matches the one in
06217    IDESC, and frees the entry in IDESC.  If no matching entry is
06218    found, NULL is returned instead.  */
06219 
06220 static struct ia64_opcode *
06221 get_next_opcode (struct ia64_opcode *idesc)
06222 {
06223   struct ia64_opcode *next = ia64_find_next_opcode (idesc);
06224   ia64_free_opcode (idesc);
06225   return next;
06226 }
06227 
06228 /* Parse the operands for the opcode and find the opcode variant that
06229    matches the specified operands, or NULL if no match is possible.  */
06230 
06231 static struct ia64_opcode *
06232 parse_operands (idesc)
06233      struct ia64_opcode *idesc;
06234 {
06235   int i = 0, highest_unmatched_operand, num_operands = 0, num_outputs = 0;
06236   int error_pos, out_of_range_pos, curr_out_of_range_pos, sep = 0;
06237   int reg1, reg2;
06238   char reg_class;
06239   enum ia64_opnd expected_operand = IA64_OPND_NIL;
06240   enum operand_match_result result;
06241   char mnemonic[129];
06242   char *first_arg = 0, *end, *saved_input_pointer;
06243   unsigned int sof;
06244 
06245   assert (strlen (idesc->name) <= 128);
06246 
06247   strcpy (mnemonic, idesc->name);
06248   if (idesc->operands[2] == IA64_OPND_SOF
06249       || idesc->operands[1] == IA64_OPND_SOF)
06250     {
06251       /* To make the common idiom "alloc loc?=ar.pfs,0,1,0,0" work, we
06252         can't parse the first operand until we have parsed the
06253         remaining operands of the "alloc" instruction.  */
06254       SKIP_WHITESPACE ();
06255       first_arg = input_line_pointer;
06256       end = strchr (input_line_pointer, '=');
06257       if (!end)
06258        {
06259          as_bad ("Expected separator `='");
06260          return 0;
06261        }
06262       input_line_pointer = end + 1;
06263       ++i;
06264       ++num_outputs;
06265     }
06266 
06267   for (; ; ++i)
06268     {
06269       if (i < NELEMS (CURR_SLOT.opnd)) 
06270        {
06271          sep = parse_operand (CURR_SLOT.opnd + i, '=');
06272          if (CURR_SLOT.opnd[i].X_op == O_absent)
06273            break;
06274        }
06275       else
06276        {
06277          expressionS dummy;
06278 
06279          sep = parse_operand (&dummy, '=');
06280          if (dummy.X_op == O_absent)
06281            break;
06282        }
06283 
06284       ++num_operands;
06285 
06286       if (sep != '=' && sep != ',')
06287        break;
06288 
06289       if (sep == '=')
06290        {
06291          if (num_outputs > 0)
06292            as_bad ("Duplicate equal sign (=) in instruction");
06293          else
06294            num_outputs = i + 1;
06295        }
06296     }
06297   if (sep != '\0')
06298     {
06299       as_bad ("Illegal operand separator `%c'", sep);
06300       return 0;
06301     }
06302 
06303   if (idesc->operands[2] == IA64_OPND_SOF
06304       || idesc->operands[1] == IA64_OPND_SOF)
06305     {
06306       /* Map alloc r1=ar.pfs,i,l,o,r to alloc r1=ar.pfs,(i+l+o),(i+l),r.
06307         Note, however, that due to that mapping operand numbers in error
06308         messages for any of the constant operands will not be correct.  */
06309       know (strcmp (idesc->name, "alloc") == 0);
06310       /* The first operand hasn't been parsed/initialized, yet (but
06311         num_operands intentionally doesn't account for that).  */
06312       i = num_operands > 4 ? 2 : 1;
06313 #define FORCE_CONST(n) (CURR_SLOT.opnd[n].X_op == O_constant \
06314                      ? CURR_SLOT.opnd[n].X_add_number \
06315                      : 0)
06316       sof = set_regstack (FORCE_CONST(i),
06317                        FORCE_CONST(i + 1),
06318                        FORCE_CONST(i + 2),
06319                        FORCE_CONST(i + 3));
06320 #undef FORCE_CONST
06321 
06322       /* now we can parse the first arg:  */
06323       saved_input_pointer = input_line_pointer;
06324       input_line_pointer = first_arg;
06325       sep = parse_operand (CURR_SLOT.opnd + 0, '=');
06326       if (sep != '=')
06327        --num_outputs;       /* force error */
06328       input_line_pointer = saved_input_pointer;
06329 
06330       CURR_SLOT.opnd[i].X_add_number = sof;
06331       if (CURR_SLOT.opnd[i + 1].X_op == O_constant
06332          && CURR_SLOT.opnd[i + 2].X_op == O_constant)
06333        CURR_SLOT.opnd[i + 1].X_add_number
06334          = sof - CURR_SLOT.opnd[i + 2].X_add_number;
06335       else
06336        CURR_SLOT.opnd[i + 1].X_op = O_illegal;
06337       CURR_SLOT.opnd[i + 2] = CURR_SLOT.opnd[i + 3];
06338     }
06339 
06340   highest_unmatched_operand = -4;
06341   curr_out_of_range_pos = -1;
06342   error_pos = 0;
06343   for (; idesc; idesc = get_next_opcode (idesc))
06344     {
06345       if (num_outputs != idesc->num_outputs)
06346        continue;            /* mismatch in # of outputs */
06347       if (highest_unmatched_operand < 0)
06348        highest_unmatched_operand |= 1;
06349       if (num_operands > NELEMS (idesc->operands)
06350          || (num_operands < NELEMS (idesc->operands)
06351           && idesc->operands[num_operands])
06352          || (num_operands > 0 && !idesc->operands[num_operands - 1]))
06353        continue;            /* mismatch in number of arguments */
06354       if (highest_unmatched_operand < 0)
06355        highest_unmatched_operand |= 2;
06356 
06357       CURR_SLOT.num_fixups = 0;
06358 
06359       /* Try to match all operands.  If we see an out-of-range operand,
06360         then continue trying to match the rest of the operands, since if
06361         the rest match, then this idesc will give the best error message.  */
06362 
06363       out_of_range_pos = -1;
06364       for (i = 0; i < num_operands && idesc->operands[i]; ++i)
06365        {
06366          result = operand_match (idesc, i, CURR_SLOT.opnd + i);
06367          if (result != OPERAND_MATCH)
06368            {
06369              if (result != OPERAND_OUT_OF_RANGE)
06370               break;
06371              if (out_of_range_pos < 0)
06372               /* remember position of the first out-of-range operand: */
06373               out_of_range_pos = i;
06374            }
06375        }
06376 
06377       /* If we did not match all operands, or if at least one operand was
06378         out-of-range, then this idesc does not match.  Keep track of which
06379         idesc matched the most operands before failing.  If we have two
06380         idescs that failed at the same position, and one had an out-of-range
06381         operand, then prefer the out-of-range operand.  Thus if we have
06382         "add r0=0x1000000,r1" we get an error saying the constant is out
06383         of range instead of an error saying that the constant should have been
06384         a register.  */
06385 
06386       if (i != num_operands || out_of_range_pos >= 0)
06387        {
06388          if (i > highest_unmatched_operand
06389              || (i == highest_unmatched_operand
06390                 && out_of_range_pos > curr_out_of_range_pos))
06391            {
06392              highest_unmatched_operand = i;
06393              if (out_of_range_pos >= 0)
06394               {
06395                 expected_operand = idesc->operands[out_of_range_pos];
06396                 error_pos = out_of_range_pos;
06397               }
06398              else
06399               {
06400                 expected_operand = idesc->operands[i];
06401                 error_pos = i;
06402               }
06403              curr_out_of_range_pos = out_of_range_pos;
06404            }
06405          continue;
06406        }
06407 
06408       break;
06409     }
06410   if (!idesc)
06411     {
06412       if (expected_operand)
06413        as_bad ("Operand %u of `%s' should be %s",
06414               error_pos + 1, mnemonic,
06415               elf64_ia64_operands[expected_operand].desc);
06416       else if (highest_unmatched_operand < 0 && !(highest_unmatched_operand & 1))
06417        as_bad ("Wrong number of output operands");
06418       else if (highest_unmatched_operand < 0 && !(highest_unmatched_operand & 2))
06419        as_bad ("Wrong number of input operands");
06420       else
06421        as_bad ("Operand mismatch");
06422       return 0;
06423     }
06424 
06425   /* Check that the instruction doesn't use
06426      - r0, f0, or f1 as output operands
06427      - the same predicate twice as output operands
06428      - r0 as address of a base update load or store
06429      - the same GR as output and address of a base update load
06430      - two even- or two odd-numbered FRs as output operands of a floating
06431        point parallel load.
06432      At most two (conflicting) output (or output-like) operands can exist,
06433      (floating point parallel loads have three outputs, but the base register,
06434      if updated, cannot conflict with the actual outputs).  */
06435   reg2 = reg1 = -1;
06436   for (i = 0; i < num_operands; ++i)
06437     {
06438       int regno = 0;
06439 
06440       reg_class = 0;
06441       switch (idesc->operands[i])
06442        {
06443        case IA64_OPND_R1:
06444        case IA64_OPND_R2:
06445        case IA64_OPND_R3:
06446          if (i < num_outputs)
06447            {
06448              if (CURR_SLOT.opnd[i].X_add_number == REG_GR)
06449               reg_class = 'r';
06450              else if (reg1 < 0)
06451               reg1 = CURR_SLOT.opnd[i].X_add_number;
06452              else if (reg2 < 0)
06453               reg2 = CURR_SLOT.opnd[i].X_add_number;
06454            }
06455          break;
06456        case IA64_OPND_P1:
06457        case IA64_OPND_P2:
06458          if (i < num_outputs)
06459            {
06460              if (reg1 < 0)
06461               reg1 = CURR_SLOT.opnd[i].X_add_number;
06462              else if (reg2 < 0)
06463               reg2 = CURR_SLOT.opnd[i].X_add_number;
06464            }
06465          break;
06466        case IA64_OPND_F1:
06467        case IA64_OPND_F2:
06468        case IA64_OPND_F3:
06469        case IA64_OPND_F4:
06470          if (i < num_outputs)
06471            {
06472              if (CURR_SLOT.opnd[i].X_add_number >= REG_FR
06473                 && CURR_SLOT.opnd[i].X_add_number <= REG_FR + 1)
06474               {
06475                 reg_class = 'f';
06476                 regno = CURR_SLOT.opnd[i].X_add_number - REG_FR;
06477               }
06478              else if (reg1 < 0)
06479               reg1 = CURR_SLOT.opnd[i].X_add_number;
06480              else if (reg2 < 0)
06481               reg2 = CURR_SLOT.opnd[i].X_add_number;
06482            }
06483          break;
06484        case IA64_OPND_MR3:
06485          if (idesc->flags & IA64_OPCODE_POSTINC)
06486            {
06487              if (CURR_SLOT.opnd[i].X_add_number == REG_GR)
06488               reg_class = 'm';
06489              else if (reg1 < 0)
06490               reg1 = CURR_SLOT.opnd[i].X_add_number;
06491              else if (reg2 < 0)
06492               reg2 = CURR_SLOT.opnd[i].X_add_number;
06493            }
06494          break;
06495        default:
06496          break;
06497        }
06498       switch (reg_class)
06499        {
06500        case 0:
06501          break;
06502        default:
06503          as_warn ("Invalid use of `%c%d' as output operand", reg_class, regno);
06504          break;
06505        case 'm':
06506          as_warn ("Invalid use of `r%d' as base update address operand", regno);
06507          break;
06508        }
06509     }
06510   if (reg1 == reg2)
06511     {
06512       if (reg1 >= REG_GR && reg1 <= REG_GR + 127)
06513        {
06514          reg1 -= REG_GR;
06515          reg_class = 'r';
06516        }
06517       else if (reg1 >= REG_P && reg1 <= REG_P + 63)
06518        {
06519          reg1 -= REG_P;
06520          reg_class = 'p';
06521        }
06522       else if (reg1 >= REG_FR && reg1 <= REG_FR + 127)
06523        {
06524          reg1 -= REG_FR;
06525          reg_class = 'f';
06526        }
06527       else
06528        reg_class = 0;
06529       if (reg_class)
06530        as_warn ("Invalid duplicate use of `%c%d'", reg_class, reg1);
06531     }
06532   else if (((reg1 >= REG_FR && reg1 <= REG_FR + 31
06533             && reg2 >= REG_FR && reg2 <= REG_FR + 31)
06534            || (reg1 >= REG_FR + 32 && reg1 <= REG_FR + 127
06535             && reg2 >= REG_FR + 32 && reg2 <= REG_FR + 127))
06536           && ! ((reg1 ^ reg2) & 1))
06537     as_warn ("Invalid simultaneous use of `f%d' and `f%d'",
06538             reg1 - REG_FR, reg2 - REG_FR);
06539   else if ((reg1 >= REG_FR && reg1 <= REG_FR + 31
06540            && reg2 >= REG_FR + 32 && reg2 <= REG_FR + 127)
06541           || (reg1 >= REG_FR + 32 && reg1 <= REG_FR + 127
06542            && reg2 >= REG_FR && reg2 <= REG_FR + 31))
06543     as_warn ("Dangerous simultaneous use of `f%d' and `f%d'",
06544             reg1 - REG_FR, reg2 - REG_FR);
06545   return idesc;
06546 }
06547 
06548 static void
06549 build_insn (slot, insnp)
06550      struct slot *slot;
06551      bfd_vma *insnp;
06552 {
06553   const struct ia64_operand *odesc, *o2desc;
06554   struct ia64_opcode *idesc = slot->idesc;
06555   bfd_vma insn;
06556   bfd_signed_vma val;
06557   const char *err;
06558   int i;
06559 
06560   insn = idesc->opcode | slot->qp_regno;
06561 
06562   for (i = 0; i < NELEMS (idesc->operands) && idesc->operands[i]; ++i)
06563     {
06564       if (slot->opnd[i].X_op == O_register
06565          || slot->opnd[i].X_op == O_constant
06566          || slot->opnd[i].X_op == O_index)
06567        val = slot->opnd[i].X_add_number;
06568       else if (slot->opnd[i].X_op == O_big)
06569        {
06570          /* This must be the value 0x10000000000000000.  */
06571          assert (idesc->operands[i] == IA64_OPND_IMM8M1U8);
06572          val = 0;
06573        }
06574       else
06575        val = 0;
06576 
06577       switch (idesc->operands[i])
06578        {
06579        case IA64_OPND_IMMU64:
06580          *insnp++ = (val >> 22) & 0x1ffffffffffLL;
06581          insn |= (((val & 0x7f) << 13) | (((val >> 7) & 0x1ff) << 27)
06582                  | (((val >> 16) & 0x1f) << 22) | (((val >> 21) & 0x1) << 21)
06583                  | (((val >> 63) & 0x1) << 36));
06584          continue;
06585 
06586        case IA64_OPND_IMMU62:
06587          val &= 0x3fffffffffffffffULL;
06588          if (val != slot->opnd[i].X_add_number)
06589            as_warn (_("Value truncated to 62 bits"));
06590          *insnp++ = (val >> 21) & 0x1ffffffffffLL;
06591          insn |= (((val & 0xfffff) << 6) | (((val >> 20) & 0x1) << 36));
06592          continue;
06593 
06594        case IA64_OPND_TGT64:
06595          val >>= 4;
06596          *insnp++ = ((val >> 20) & 0x7fffffffffLL) << 2;
06597          insn |= ((((val >> 59) & 0x1) << 36)
06598                  | (((val >> 0) & 0xfffff) << 13));
06599          continue;
06600 
06601        case IA64_OPND_AR3:
06602          val -= REG_AR;
06603          break;
06604 
06605        case IA64_OPND_B1:
06606        case IA64_OPND_B2:
06607          val -= REG_BR;
06608          break;
06609 
06610        case IA64_OPND_CR3:
06611          val -= REG_CR;
06612          break;
06613 
06614        case IA64_OPND_F1:
06615        case IA64_OPND_F2:
06616        case IA64_OPND_F3:
06617        case IA64_OPND_F4:
06618          val -= REG_FR;
06619          break;
06620 
06621        case IA64_OPND_P1:
06622        case IA64_OPND_P2:
06623          val -= REG_P;
06624          break;
06625 
06626        case IA64_OPND_R1:
06627        case IA64_OPND_R2:
06628        case IA64_OPND_R3:
06629        case IA64_OPND_R3_2:
06630        case IA64_OPND_CPUID_R3:
06631        case IA64_OPND_DBR_R3:
06632        case IA64_OPND_DTR_R3:
06633        case IA64_OPND_ITR_R3:
06634        case IA64_OPND_IBR_R3:
06635        case IA64_OPND_MR3:
06636        case IA64_OPND_MSR_R3:
06637        case IA64_OPND_PKR_R3:
06638        case IA64_OPND_PMC_R3:
06639        case IA64_OPND_PMD_R3:
06640        case IA64_OPND_RR_R3:
06641          val -= REG_GR;
06642          break;
06643 
06644        default:
06645          break;
06646        }
06647 
06648       odesc = elf64_ia64_operands + idesc->operands[i];
06649       err = (*odesc->insert) (odesc, val, &insn);
06650       if (err)
06651        as_bad_where (slot->src_file, slot->src_line,
06652                     "Bad operand value: %s", err);
06653       if (idesc->flags & IA64_OPCODE_PSEUDO)
06654        {
06655          if ((idesc->flags & IA64_OPCODE_F2_EQ_F3)
06656              && odesc == elf64_ia64_operands + IA64_OPND_F3)
06657            {
06658              o2desc = elf64_ia64_operands + IA64_OPND_F2;
06659              (*o2desc->insert) (o2desc, val, &insn);
06660            }
06661          if ((idesc->flags & IA64_OPCODE_LEN_EQ_64MCNT)
06662              && (odesc == elf64_ia64_operands + IA64_OPND_CPOS6a
06663                 || odesc == elf64_ia64_operands + IA64_OPND_POS6))
06664            {
06665              o2desc = elf64_ia64_operands + IA64_OPND_LEN6;
06666              (*o2desc->insert) (o2desc, 64 - val, &insn);
06667            }
06668        }
06669     }
06670   *insnp = insn;
06671 }
06672 
06673 static void
06674 emit_one_bundle ()
06675 {
06676   int manual_bundling_off = 0, manual_bundling = 0;
06677   enum ia64_unit required_unit, insn_unit = 0;
06678   enum ia64_insn_type type[3], insn_type;
06679   unsigned int template, orig_template;
06680   bfd_vma insn[3] = { -1, -1, -1 };
06681   struct ia64_opcode *idesc;
06682   int end_of_insn_group = 0, user_template = -1;
06683   int n, i, j, first, curr, last_slot;
06684   bfd_vma t0 = 0, t1 = 0;
06685   struct label_fix *lfix;
06686   bfd_boolean mark_label;
06687   struct insn_fix *ifix;
06688   char mnemonic[16];
06689   fixS *fix;
06690   char *f;
06691   int addr_mod;
06692 
06693   first = (md.curr_slot + NUM_SLOTS - md.num_slots_in_use) % NUM_SLOTS;
06694   know (first >= 0 & first < NUM_SLOTS);
06695   n = MIN (3, md.num_slots_in_use);
06696 
06697   /* Determine template: user user_template if specified, best match
06698      otherwise:  */
06699 
06700   if (md.slot[first].user_template >= 0)
06701     user_template = template = md.slot[first].user_template;
06702   else
06703     {
06704       /* Auto select appropriate template.  */
06705       memset (type, 0, sizeof (type));
06706       curr = first;
06707       for (i = 0; i < n; ++i)
06708        {
06709          if (md.slot[curr].label_fixups && i != 0)
06710            break;
06711          type[i] = md.slot[curr].idesc->type;
06712          curr = (curr + 1) % NUM_SLOTS;
06713        }
06714       template = best_template[type[0]][type[1]][type[2]];
06715     }
06716 
06717   /* initialize instructions with appropriate nops:  */
06718   for (i = 0; i < 3; ++i)
06719     insn[i] = nop[ia64_templ_desc[template].exec_unit[i]];
06720 
06721   f = frag_more (16);
06722 
06723   /* Check to see if this bundle is at an offset that is a multiple of 16-bytes
06724      from the start of the frag.  */
06725   addr_mod = frag_now_fix () & 15;
06726   if (frag_now->has_code && frag_now->insn_addr != addr_mod)
06727     as_bad (_("instruction address is not a multiple of 16"));
06728   frag_now->insn_addr = addr_mod;
06729   frag_now->has_code = 1;
06730 
06731   /* now fill in slots with as many insns as possible:  */
06732   curr = first;
06733   idesc = md.slot[curr].idesc;
06734   end_of_insn_group = 0;
06735   last_slot = -1;
06736   for (i = 0; i < 3 && md.num_slots_in_use > 0; ++i)
06737     {
06738       /* If we have unwind records, we may need to update some now.  */
06739       unw_rec_list *ptr = md.slot[curr].unwind_record;
06740       unw_rec_list *end_ptr = NULL;
06741 
06742       if (ptr)
06743        {
06744          /* Find the last prologue/body record in the list for the current
06745             insn, and set the slot number for all records up to that point.
06746             This needs to be done now, because prologue/body records refer to
06747             the current point, not the point after the instruction has been
06748             issued.  This matters because there may have been nops emitted
06749             meanwhile.  Any non-prologue non-body record followed by a
06750             prologue/body record must also refer to the current point.  */
06751          unw_rec_list *last_ptr;
06752 
06753          for (j = 1; end_ptr == NULL && j < md.num_slots_in_use; ++j)
06754            end_ptr = md.slot[(curr + j) % NUM_SLOTS].unwind_record;
06755          for (last_ptr = NULL; ptr != end_ptr; ptr = ptr->next)
06756            if (ptr->r.type == prologue || ptr->r.type == prologue_gr
06757               || ptr->r.type == body)
06758              last_ptr = ptr;
06759          if (last_ptr)
06760            {
06761              /* Make last_ptr point one after the last prologue/body
06762                record.  */
06763              last_ptr = last_ptr->next;
06764              for (ptr = md.slot[curr].unwind_record; ptr != last_ptr;
06765                  ptr = ptr->next)
06766               {
06767                 ptr->slot_number = (unsigned long) f + i;
06768                 ptr->slot_frag = frag_now;
06769               }
06770              /* Remove the initialized records, so that we won't accidentally
06771                update them again if we insert a nop and continue.  */
06772              md.slot[curr].unwind_record = last_ptr;
06773            }
06774        }
06775 
06776       manual_bundling_off = md.slot[curr].manual_bundling_off;
06777       if (md.slot[curr].manual_bundling_on)
06778        {
06779          if (curr == first)
06780            manual_bundling = 1;
06781          else
06782          break; /* Need to start a new bundle.  */
06783        }
06784 
06785       /* If this instruction specifies a template, then it must be the first
06786         instruction of a bundle.  */
06787       if (curr != first && md.slot[curr].user_template >= 0)
06788        break;
06789 
06790       if (idesc->flags & IA64_OPCODE_SLOT2)
06791        {
06792          if (manual_bundling && !manual_bundling_off)
06793            {
06794              as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
06795                          "`%s' must be last in bundle", idesc->name);
06796              if (i < 2)
06797               manual_bundling = -1; /* Suppress meaningless post-loop errors.  */
06798            }
06799          i = 2;
06800        }
06801       if (idesc->flags & IA64_OPCODE_LAST)
06802        {
06803          int required_slot;
06804          unsigned int required_template;
06805 
06806          /* If we need a stop bit after an M slot, our only choice is
06807             template 5 (M;;MI).  If we need a stop bit after a B
06808             slot, our only choice is to place it at the end of the
06809             bundle, because the only available templates are MIB,
06810             MBB, BBB, MMB, and MFB.  We don't handle anything other
06811             than M and B slots because these are the only kind of
06812             instructions that can have the IA64_OPCODE_LAST bit set.  */
06813          required_template = template;
06814          switch (idesc->type)
06815            {
06816            case IA64_TYPE_M:
06817              required_slot = 0;
06818              required_template = 5;
06819              break;
06820 
06821            case IA64_TYPE_B:
06822              required_slot = 2;
06823              break;
06824 
06825            default:
06826              as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
06827                          "Internal error: don't know how to force %s to end"
06828                          "of instruction group", idesc->name);
06829              required_slot = i;
06830              break;
06831            }
06832          if (manual_bundling
06833              && (i > required_slot
06834                 || (required_slot == 2 && !manual_bundling_off)
06835                 || (user_template >= 0
06836                     /* Changing from MMI to M;MI is OK.  */
06837                     && (template ^ required_template) > 1)))
06838            {
06839              as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
06840                          "`%s' must be last in instruction group",
06841                          idesc->name);
06842              if (i < 2 && required_slot == 2 && !manual_bundling_off)
06843               manual_bundling = -1; /* Suppress meaningless post-loop errors.  */
06844            }
06845          if (required_slot < i)
06846            /* Can't fit this instruction.  */
06847            break;
06848 
06849          i = required_slot;
06850          if (required_template != template)
06851            {
06852              /* If we switch the template, we need to reset the NOPs
06853                 after slot i.  The slot-types of the instructions ahead
06854                 of i never change, so we don't need to worry about
06855                 changing NOPs in front of this slot.  */
06856              for (j = i; j < 3; ++j)
06857                insn[j] = nop[ia64_templ_desc[required_template].exec_unit[j]];
06858 
06859              /* We just picked a template that includes the stop bit in the
06860                middle, so we don't need another one emitted later.  */
06861              md.slot[curr].end_of_insn_group = 0;
06862            }
06863          template = required_template;
06864        }
06865       if (curr != first && md.slot[curr].label_fixups)
06866        {
06867          if (manual_bundling)
06868            {
06869              as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
06870                        "Label must be first in a bundle");
06871              manual_bundling = -1; /* Suppress meaningless post-loop errors.  */
06872            }
06873          /* This insn must go into the first slot of a bundle.  */
06874          break;
06875        }
06876 
06877       if (end_of_insn_group && md.num_slots_in_use >= 1)
06878        {
06879          /* We need an instruction group boundary in the middle of a
06880             bundle.  See if we can switch to an other template with
06881             an appropriate boundary.  */
06882 
06883          orig_template = template;
06884          if (i == 1 && (user_template == 4
06885                       || (user_template < 0
06886                           && (ia64_templ_desc[template].exec_unit[0]
06887                              == IA64_UNIT_M))))
06888            {
06889              template = 5;
06890              end_of_insn_group = 0;
06891            }
06892          else if (i == 2 && (user_template == 0
06893                            || (user_template < 0
06894                               && (ia64_templ_desc[template].exec_unit[1]
06895                                   == IA64_UNIT_I)))
06896                  /* This test makes sure we don't switch the template if
06897                     the next instruction is one that needs to be first in
06898                     an instruction group.  Since all those instructions are
06899                     in the M group, there is no way such an instruction can
06900                     fit in this bundle even if we switch the template.  The
06901                     reason we have to check for this is that otherwise we
06902                     may end up generating "MI;;I M.." which has the deadly
06903                     effect that the second M instruction is no longer the
06904                     first in the group! --davidm 99/12/16  */
06905                  && (idesc->flags & IA64_OPCODE_FIRST) == 0)
06906            {
06907              template = 1;
06908              end_of_insn_group = 0;
06909            }
06910          else if (i == 1
06911                  && user_template == 0
06912                  && !(idesc->flags & IA64_OPCODE_FIRST))
06913            /* Use the next slot.  */
06914            continue;
06915          else if (curr != first)
06916            /* can't fit this insn */
06917            break;
06918 
06919          if (template != orig_template)
06920            /* if we switch the template, we need to reset the NOPs
06921               after slot i.  The slot-types of the instructions ahead
06922               of i never change, so we don't need to worry about
06923               changing NOPs in front of this slot.  */
06924            for (j = i; j < 3; ++j)
06925              insn[j] = nop[ia64_templ_desc[template].exec_unit[j]];
06926        }
06927       required_unit = ia64_templ_desc[template].exec_unit[i];
06928 
06929       /* resolve dynamic opcodes such as "break", "hint", and "nop":  */
06930       if (idesc->type == IA64_TYPE_DYN)
06931        {
06932          enum ia64_opnd opnd1, opnd2;
06933 
06934          if ((strcmp (idesc->name, "nop") == 0)
06935              || (strcmp (idesc->name, "break") == 0))
06936            insn_unit = required_unit;
06937          else if (strcmp (idesc->name, "hint") == 0)
06938            {
06939              insn_unit = required_unit;
06940              if (required_unit == IA64_UNIT_B)
06941               {
06942                 switch (md.hint_b)
06943                   {
06944                   case hint_b_ok:
06945                     break;
06946                   case hint_b_warning:
06947                     as_warn ("hint in B unit may be treated as nop");
06948                     break;
06949                   case hint_b_error:
06950                     /* When manual bundling is off and there is no
06951                       user template, we choose a different unit so
06952                       that hint won't go into the current slot. We
06953                       will fill the current bundle with nops and
06954                       try to put hint into the next bundle.  */
06955                     if (!manual_bundling && user_template < 0)
06956                      insn_unit = IA64_UNIT_I;
06957                     else
06958                      as_bad ("hint in B unit can't be used");
06959                     break;
06960                   }
06961               }
06962            }
06963          else if (strcmp (idesc->name, "chk.s") == 0
06964              || strcmp (idesc->name, "mov") == 0)
06965            {
06966              insn_unit = IA64_UNIT_M;
06967              if (required_unit == IA64_UNIT_I
06968                 || (required_unit == IA64_UNIT_F && template == 6))
06969               insn_unit = IA64_UNIT_I;
06970            }
06971          else
06972            as_fatal ("emit_one_bundle: unexpected dynamic op");
06973 
06974          snprintf (mnemonic, sizeof (mnemonic), "%s.%c",
06975                   idesc->name, "?imbfxx"[insn_unit]);
06976          opnd1 = idesc->operands[0];
06977          opnd2 = idesc->operands[1];
06978          ia64_free_opcode (idesc);
06979          idesc = ia64_find_opcode (mnemonic);
06980          /* moves to/from ARs have collisions */
06981          if (opnd1 == IA64_OPND_AR3 || opnd2 == IA64_OPND_AR3)
06982            {
06983              while (idesc != NULL
06984                    && (idesc->operands[0] != opnd1
06985                       || idesc->operands[1] != opnd2))
06986               idesc = get_next_opcode (idesc);
06987            }
06988          md.slot[curr].idesc = idesc;
06989        }
06990       else
06991        {
06992          insn_type = idesc->type;
06993          insn_unit = IA64_UNIT_NIL;
06994          switch (insn_type)
06995            {
06996            case IA64_TYPE_A:
06997              if (required_unit == IA64_UNIT_I || required_unit == IA64_UNIT_M)
06998               insn_unit = required_unit;
06999              break;
07000            case IA64_TYPE_X: insn_unit = IA64_UNIT_L; break;
07001            case IA64_TYPE_I: insn_unit = IA64_UNIT_I; break;
07002            case IA64_TYPE_M: insn_unit = IA64_UNIT_M; break;
07003            case IA64_TYPE_B: insn_unit = IA64_UNIT_B; break;
07004            case IA64_TYPE_F: insn_unit = IA64_UNIT_F; break;
07005            default:                              break;
07006            }
07007        }
07008 
07009       if (insn_unit != required_unit)
07010        continue;            /* Try next slot.  */
07011 
07012       /* Now is a good time to fix up the labels for this insn.  */
07013       mark_label = FALSE;
07014       for (lfix = md.slot[curr].label_fixups; lfix; lfix = lfix->next)
07015        {
07016          S_SET_VALUE (lfix->sym, frag_now_fix () - 16);
07017          symbol_set_frag (lfix->sym, frag_now);
07018          mark_label |= lfix->dw2_mark_labels;
07019        }
07020       for (lfix = md.slot[curr].tag_fixups; lfix; lfix = lfix->next)
07021        {
07022          S_SET_VALUE (lfix->sym, frag_now_fix () - 16 + i);
07023          symbol_set_frag (lfix->sym, frag_now);
07024        }
07025 
07026       if (debug_type == DEBUG_DWARF2
07027          || md.slot[curr].loc_directive_seen
07028          || mark_label)
07029        {
07030          bfd_vma addr = frag_now->fr_address + frag_now_fix () - 16 + i;
07031 
07032          md.slot[curr].loc_directive_seen = 0;
07033          if (mark_label)
07034            md.slot[curr].debug_line.flags |= DWARF2_FLAG_BASIC_BLOCK;
07035 
07036          dwarf2_gen_line_info (addr, &md.slot[curr].debug_line);
07037        }
07038 
07039       build_insn (md.slot + curr, insn + i);
07040 
07041       ptr = md.slot[curr].unwind_record;
07042       if (ptr)
07043        {
07044          /* Set slot numbers for all remaining unwind records belonging to the
07045             current insn.  There can not be any prologue/body unwind records
07046             here.  */
07047          for (; ptr != end_ptr; ptr = ptr->next)
07048            {
07049              ptr->slot_number = (unsigned long) f + i;
07050              ptr->slot_frag = frag_now;
07051            }
07052          md.slot[curr].unwind_record = NULL;
07053        }
07054 
07055       if (required_unit == IA64_UNIT_L)
07056        {
07057          know (i == 1);
07058          /* skip one slot for long/X-unit instructions */
07059          ++i;
07060        }
07061       --md.num_slots_in_use;
07062       last_slot = i;
07063 
07064       for (j = 0; j < md.slot[curr].num_fixups; ++j)
07065        {
07066          ifix = md.slot[curr].fixup + j;
07067          fix = fix_new_exp (frag_now, frag_now_fix () - 16 + i, 8,
07068                           &ifix->expr, ifix->is_pcrel, ifix->code);
07069          fix->tc_fix_data.opnd = ifix->opnd;
07070          fix->fx_file = md.slot[curr].src_file;
07071          fix->fx_line = md.slot[curr].src_line;
07072        }
07073 
07074       end_of_insn_group = md.slot[curr].end_of_insn_group;
07075 
07076       /* clear slot:  */
07077       ia64_free_opcode (md.slot[curr].idesc);
07078       memset (md.slot + curr, 0, sizeof (md.slot[curr]));
07079       md.slot[curr].user_template = -1;
07080 
07081       if (manual_bundling_off)
07082        {
07083          manual_bundling = 0;
07084          break;
07085        }
07086       curr = (curr + 1) % NUM_SLOTS;
07087       idesc = md.slot[curr].idesc;
07088     }
07089 
07090   /* A user template was specified, but the first following instruction did
07091      not fit.  This can happen with or without manual bundling.  */
07092   if (md.num_slots_in_use > 0 && last_slot < 0)
07093     {
07094       as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
07095                   "`%s' does not fit into %s template",
07096                   idesc->name, ia64_templ_desc[template].name);
07097       /* Drop first insn so we don't livelock.  */
07098       --md.num_slots_in_use;
07099       know (curr == first);
07100       ia64_free_opcode (md.slot[curr].idesc);
07101       memset (md.slot + curr, 0, sizeof (md.slot[curr]));
07102       md.slot[curr].user_template = -1;
07103     }
07104   else if (manual_bundling > 0)
07105     {
07106       if (md.num_slots_in_use > 0)
07107        {
07108          if (last_slot >= 2)
07109            as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
07110                        "`%s' does not fit into bundle", idesc->name);
07111          else
07112            {
07113              const char *where;
07114 
07115              if (template == 2)
07116               where = "X slot";
07117              else if (last_slot == 0)
07118               where = "slots 2 or 3";
07119              else
07120               where = "slot 3";
07121              as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
07122                          "`%s' can't go in %s of %s template",
07123                          idesc->name, where, ia64_templ_desc[template].name);
07124            }
07125        }
07126       else
07127        as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
07128                     "Missing '}' at end of file");
07129     }
07130        
07131   know (md.num_slots_in_use < NUM_SLOTS);
07132 
07133   t0 = end_of_insn_group | (template << 1) | (insn[0] << 5) | (insn[1] << 46);
07134   t1 = ((insn[1] >> 18) & 0x7fffff) | (insn[2] << 23);
07135 
07136   number_to_chars_littleendian (f + 0, t0, 8);
07137   number_to_chars_littleendian (f + 8, t1, 8);
07138 }
07139 
07140 int
07141 md_parse_option (c, arg)
07142      int c;
07143      char *arg;
07144 {
07145 
07146   switch (c)
07147     {
07148     /* Switches from the Intel assembler.  */
07149     case 'm':
07150       if (strcmp (arg, "ilp64") == 0
07151          || strcmp (arg, "lp64") == 0
07152          || strcmp (arg, "p64") == 0)
07153        {
07154          md.flags |= EF_IA_64_ABI64;
07155        }
07156       else if (strcmp (arg, "ilp32") == 0)
07157        {
07158          md.flags &= ~EF_IA_64_ABI64;
07159        }
07160       else if (strcmp (arg, "le") == 0)
07161        {
07162          md.flags &= ~EF_IA_64_BE;
07163          default_big_endian = 0;
07164        }
07165       else if (strcmp (arg, "be") == 0)
07166        {
07167          md.flags |= EF_IA_64_BE;
07168          default_big_endian = 1;
07169        }
07170       else if (strncmp (arg, "unwind-check=", 13) == 0)
07171        {
07172          arg += 13;
07173          if (strcmp (arg, "warning") == 0)
07174            md.unwind_check = unwind_check_warning;
07175          else if (strcmp (arg, "error") == 0)
07176            md.unwind_check = unwind_check_error;
07177          else
07178            return 0;
07179        }
07180       else if (strncmp (arg, "hint.b=", 7) == 0)
07181        {
07182          arg += 7;
07183          if (strcmp (arg, "ok") == 0)
07184            md.hint_b = hint_b_ok;
07185          else if (strcmp (arg, "warning") == 0)
07186            md.hint_b = hint_b_warning;
07187          else if (strcmp (arg, "error") == 0)
07188            md.hint_b = hint_b_error;
07189          else
07190            return 0;
07191        }
07192       else if (strncmp (arg, "tune=", 5) == 0)
07193        {
07194          arg += 5;
07195          if (strcmp (arg, "itanium1") == 0)
07196            md.tune = itanium1;
07197          else if (strcmp (arg, "itanium2") == 0)
07198            md.tune = itanium2;
07199          else
07200            return 0;
07201        }
07202       else
07203        return 0;
07204       break;
07205 
07206     case 'N':
07207       if (strcmp (arg, "so") == 0)
07208        {
07209          /* Suppress signon message.  */
07210        }
07211       else if (strcmp (arg, "pi") == 0)
07212        {
07213          /* Reject privileged instructions.  FIXME */
07214        }
07215       else if (strcmp (arg, "us") == 0)
07216        {
07217          /* Allow union of signed and unsigned range.  FIXME */
07218        }
07219       else if (strcmp (arg, "close_fcalls") == 0)
07220        {
07221          /* Do not resolve global function calls.  */
07222        }
07223       else
07224        return 0;
07225       break;
07226 
07227     case 'C':
07228       /* temp[="prefix"]  Insert temporary labels into the object file
07229                        symbol table prefixed by "prefix".
07230                        Default prefix is ":temp:".
07231        */
07232       break;
07233 
07234     case 'a':
07235       /* indirect=<tgt>     Assume unannotated indirect branches behavior
07236                      according to <tgt> --
07237                      exit:  branch out from the current context (default)
07238                      labels:       all labels in context may be branch targets
07239        */
07240       if (strncmp (arg, "indirect=", 9) != 0)
07241         return 0;
07242       break;
07243 
07244     case 'x':
07245       /* -X conflicts with an ignored option, use -x instead */
07246       md.detect_dv = 1;
07247       if (!arg || strcmp (arg, "explicit") == 0)
07248        {
07249          /* set default mode to explicit */
07250          md.default_explicit_mode = 1;
07251          break;
07252        }
07253       else if (strcmp (arg, "auto") == 0)
07254        {
07255          md.default_explicit_mode = 0;
07256        }
07257       else if (strcmp (arg, "none") == 0)
07258        {
07259          md.detect_dv = 0;
07260        }
07261       else if (strcmp (arg, "debug") == 0)
07262        {
07263          md.debug_dv = 1;
07264        }
07265       else if (strcmp (arg, "debugx") == 0)
07266        {
07267          md.default_explicit_mode = 1;
07268          md.debug_dv = 1;
07269        }
07270       else if (strcmp (arg, "debugn") == 0)
07271        {
07272          md.debug_dv = 1;
07273          md.detect_dv = 0;
07274        }
07275       else
07276        {
07277          as_bad (_("Unrecognized option '-x%s'"), arg);
07278        }
07279       break;
07280 
07281     case 'S':
07282       /* nops        Print nops statistics.  */
07283       break;
07284 
07285     /* GNU specific switches for gcc.  */
07286     case OPTION_MCONSTANT_GP:
07287       md.flags |= EF_IA_64_CONS_GP;
07288       break;
07289 
07290     case OPTION_MAUTO_PIC:
07291       md.flags |= EF_IA_64_NOFUNCDESC_CONS_GP;
07292       break;
07293 
07294     default:
07295       return 0;
07296     }
07297 
07298   return 1;
07299 }
07300 
07301 void
07302 md_show_usage (stream)
07303      FILE *stream;
07304 {
07305   fputs (_("\
07306 IA-64 options:\n\
07307   --mconstant-gp       mark output file as using the constant-GP model\n\
07308                        (sets ELF header flag EF_IA_64_CONS_GP)\n\
07309   --mauto-pic          mark output file as using the constant-GP model\n\
07310                        without function descriptors (sets ELF header flag\n\
07311                        EF_IA_64_NOFUNCDESC_CONS_GP)\n\
07312   -milp32|-milp64|-mlp64|-mp64     select data model (default -mlp64)\n\
07313   -mle | -mbe          select little- or big-endian byte order (default -mle)\n\
07314   -mtune=[itanium1|itanium2]\n\
07315                        tune for a specific CPU (default -mtune=itanium2)\n\
07316   -munwind-check=[warning|error]\n\
07317                        unwind directive check (default -munwind-check=warning)\n\
07318   -mhint.b=[ok|warning|error]\n\
07319                        hint.b check (default -mhint.b=error)\n\
07320   -x | -xexplicit      turn on dependency violation checking\n\
07321   -xauto               automagically remove dependency violations (default)\n\
07322   -xnone               turn off dependency violation checking\n\
07323   -xdebug              debug dependency violation checker\n\
07324   -xdebugn             debug dependency violation checker but turn off\n\
07325                        dependency violation checking\n\
07326   -xdebugx             debug dependency violation checker and turn on\n\
07327                        dependency violation checking\n"),
07328        stream);
07329 }
07330 
07331 void
07332 ia64_after_parse_args ()
07333 {
07334   if (debug_type == DEBUG_STABS)
07335     as_fatal (_("--gstabs is not supported for ia64"));
07336 }
07337 
07338 /* Return true if TYPE fits in TEMPL at SLOT.  */
07339 
07340 static int
07341 match (int templ, int type, int slot)
07342 {
07343   enum ia64_unit unit;
07344   int result;
07345 
07346   unit = ia64_templ_desc[templ].exec_unit[slot];
07347   switch (type)
07348     {
07349     case IA64_TYPE_DYN: result = 1; break; /* for nop and break */
07350     case IA64_TYPE_A:
07351       result = (unit == IA64_UNIT_I || unit == IA64_UNIT_M);
07352       break;
07353     case IA64_TYPE_X:       result = (unit == IA64_UNIT_L); break;
07354     case IA64_TYPE_I:       result = (unit == IA64_UNIT_I); break;
07355     case IA64_TYPE_M:       result = (unit == IA64_UNIT_M); break;
07356     case IA64_TYPE_B:       result = (unit == IA64_UNIT_B); break;
07357     case IA64_TYPE_F:       result = (unit == IA64_UNIT_F); break;
07358     default:         result = 0; break;
07359     }
07360   return result;
07361 }
07362 
07363 /* For Itanium 1, add a bit of extra goodness if a nop of type F or B would fit
07364    in TEMPL at SLOT.  For Itanium 2, add a bit of extra goodness if a nop of
07365    type M or I would fit in TEMPL at SLOT.  */
07366 
07367 static inline int
07368 extra_goodness (int templ, int slot)
07369 {
07370   switch (md.tune)
07371     {
07372     case itanium1:
07373       if (slot == 1 && match (templ, IA64_TYPE_F, slot))
07374        return 2;
07375       else if (slot == 2 && match (templ, IA64_TYPE_B, slot))
07376        return 1;
07377       else
07378        return 0;
07379       break;
07380     case itanium2:
07381       if (match (templ, IA64_TYPE_M, slot)
07382          || match (templ, IA64_TYPE_I, slot))
07383        /* Favor M- and I-unit NOPs.  We definitely want to avoid
07384           F-unit and B-unit may cause split-issue or less-than-optimal
07385           branch-prediction.  */
07386        return 2;
07387       else
07388        return 0;
07389       break;
07390     default:
07391       abort ();
07392       return 0;
07393     }
07394 }
07395 
07396 /* This function is called once, at assembler startup time.  It sets
07397    up all the tables, etc. that the MD part of the assembler will need
07398    that can be determined before arguments are parsed.  */
07399 void
07400 md_begin ()
07401 {
07402   int i, j, k, t, goodness, best, ok;
07403   const char *err;
07404   char name[8];
07405 
07406   md.auto_align = 1;
07407   md.explicit_mode = md.default_explicit_mode;
07408 
07409   bfd_set_section_alignment (stdoutput, text_section, 4);
07410 
07411   /* Make sure function pointers get initialized.  */
07412   target_big_endian = -1;
07413   dot_byteorder (default_big_endian);
07414 
07415   alias_hash = hash_new ();
07416   alias_name_hash = hash_new ();
07417   secalias_hash = hash_new ();
07418   secalias_name_hash = hash_new ();
07419 
07420   pseudo_func[FUNC_DTP_MODULE].u.sym =
07421     symbol_new (".<dtpmod>", undefined_section, FUNC_DTP_MODULE,
07422               &zero_address_frag);
07423 
07424   pseudo_func[FUNC_DTP_RELATIVE].u.sym =
07425     symbol_new (".<dtprel>", undefined_section, FUNC_DTP_RELATIVE,
07426               &zero_address_frag);
07427 
07428   pseudo_func[FUNC_FPTR_RELATIVE].u.sym =
07429     symbol_new (".<fptr>", undefined_section, FUNC_FPTR_RELATIVE,
07430               &zero_address_frag);
07431 
07432   pseudo_func[FUNC_GP_RELATIVE].u.sym =
07433     symbol_new (".<gprel>", undefined_section, FUNC_GP_RELATIVE,
07434               &zero_address_frag);
07435 
07436   pseudo_func[FUNC_LT_RELATIVE].u.sym =
07437     symbol_new (".<ltoff>", undefined_section, FUNC_LT_RELATIVE,
07438               &zero_address_frag);
07439 
07440   pseudo_func[FUNC_LT_RELATIVE_X].u.sym =
07441     symbol_new (".<ltoffx>", undefined_secti