Home | History | Annotate | Line # | Download | only in config
tc-pru.c revision 1.1.1.2
      1 /* TI PRU assembler.
      2    Copyright (C) 2014-2020 Free Software Foundation, Inc.
      3    Contributed by Dimitar Dimitrov <dimitar (at) dinux.eu>
      4    Based on tc-nios2.c
      5 
      6    This file is part of GAS, the GNU Assembler.
      7 
      8    GAS is free software; you can redistribute it and/or modify
      9    it under the terms of the GNU General Public License as published by
     10    the Free Software Foundation; either version 3, or (at your option)
     11    any later version.
     12 
     13    GAS is distributed in the hope that it will be useful,
     14    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16    GNU General Public License for more details.
     17 
     18    You should have received a copy of the GNU General Public License
     19    along with GAS; see the file COPYING.  If not, write to the Free
     20    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
     21    02110-1301, USA.  */
     22 
     23 #include "as.h"
     24 #include "bfd_stdint.h"
     25 #include "opcode/pru.h"
     26 #include "elf/pru.h"
     27 #include "tc-pru.h"
     28 #include "bfd.h"
     29 #include "dwarf2dbg.h"
     30 #include "subsegs.h"
     31 #include "safe-ctype.h"
     32 #include "dw2gencfi.h"
     33 
     34 #ifndef OBJ_ELF
     35 /* We are not supporting any other target so we throw a compile time error.  */
     36   #error "OBJ_ELF not defined"
     37 #endif
     38 
     39 /* This array holds the chars that always start a comment.  If the
     40    pre-processor is disabled, these aren't very useful.  */
     41 const char comment_chars[] = "#;";
     42 
     43 /* This array holds the chars that only start a comment at the beginning of
     44    a line.  If the line seems to have the form '# 123 filename'
     45    .line and .file directives will appear in the pre-processed output.  */
     46 /* Note that input_file.c hand checks for '#' at the beginning of the
     47    first line of the input file.  This is because the compiler outputs
     48    #NO_APP at the beginning of its output.  */
     49 /* Also note that C style comments are always supported.  */
     50 const char line_comment_chars[] = "#;*";
     51 
     52 /* This array holds machine specific line separator characters.  */
     53 const char line_separator_chars[] = "";
     54 
     55 /* Chars that can be used to separate mant from exp in floating point nums.  */
     56 const char EXP_CHARS[] = "eE";
     57 
     58 /* Chars that mean this number is a floating point constant.
     59    As in 0f12.456
     60    or	 0d1.2345e12  */
     61 const char FLT_CHARS[] = "rRsSfFdDxXpP";
     62 
     63 /* Machine-dependent command-line options.  */
     64 
     65 struct pru_opt_s
     66 {
     67   /* -mno-link-relax / -mlink-relax: generate (or not)
     68      relocations for linker relaxation.  */
     69   bfd_boolean link_relax;
     70 
     71   /* -mno-warn-regname-label: do not output a warning that a label name
     72      matches a register name.  */
     73   bfd_boolean warn_regname_label;
     74 };
     75 
     76 static struct pru_opt_s pru_opt = { TRUE, TRUE };
     77 
     78 const char *md_shortopts = "r";
     79 
     80 enum options
     81 {
     82   OPTION_LINK_RELAX = OPTION_MD_BASE + 1,
     83   OPTION_NO_LINK_RELAX,
     84   OPTION_NO_WARN_REGNAME_LABEL,
     85 };
     86 
     87 struct option md_longopts[] = {
     88   { "mlink-relax",  no_argument, NULL, OPTION_LINK_RELAX  },
     89   { "mno-link-relax",  no_argument, NULL, OPTION_NO_LINK_RELAX  },
     90   { "mno-warn-regname-label",  no_argument, NULL,
     91     OPTION_NO_WARN_REGNAME_LABEL  },
     92   { NULL, no_argument, NULL, 0 }
     93 };
     94 
     95 size_t md_longopts_size = sizeof (md_longopts);
     96 
     97 typedef struct pru_insn_reloc
     98 {
     99   /* Any expression in the instruction is parsed into this field,
    100      which is passed to fix_new_exp () to generate a fixup.  */
    101   expressionS reloc_expression;
    102 
    103   /* The type of the relocation to be applied.  */
    104   bfd_reloc_code_real_type reloc_type;
    105 
    106   /* PC-relative.  */
    107   unsigned int reloc_pcrel;
    108 
    109   /* The next relocation to be applied to the instruction.  */
    110   struct pru_insn_reloc *reloc_next;
    111 } pru_insn_relocS;
    112 
    113 /* This struct is used to hold state when assembling instructions.  */
    114 typedef struct pru_insn_info
    115 {
    116   /* Assembled instruction.  */
    117   unsigned long insn_code;
    118   /* Used for assembling LDI32.  */
    119   unsigned long ldi32_imm32;
    120 
    121   /* Pointer to the relevant bit of the opcode table.  */
    122   const struct pru_opcode *insn_pru_opcode;
    123   /* After parsing ptrs to the tokens in the instruction fill this array
    124      it is terminated with a null pointer (hence the first +1).
    125      The second +1 is because in some parts of the code the opcode
    126      is not counted as a token, but still placed in this array.  */
    127   const char *insn_tokens[PRU_MAX_INSN_TOKENS + 1 + 1];
    128 
    129   /* This holds information used to generate fixups
    130      and eventually relocations if it is not null.  */
    131   pru_insn_relocS *insn_reloc;
    132 } pru_insn_infoS;
    133 
    134 /* Opcode hash table.  */
    135 static struct hash_control *pru_opcode_hash = NULL;
    136 #define pru_opcode_lookup(NAME) \
    137   ((struct pru_opcode *) hash_find (pru_opcode_hash, (NAME)))
    138 
    139 /* Register hash table.  */
    140 static struct hash_control *pru_reg_hash = NULL;
    141 #define pru_reg_lookup(NAME) \
    142   ((struct pru_reg *) hash_find (pru_reg_hash, (NAME)))
    143 
    144 /* The known current alignment of the current section.  */
    145 static int pru_current_align;
    146 static segT pru_current_align_seg;
    147 
    148 static int pru_auto_align_on = 1;
    149 
    150 /* The last seen label in the current section.  This is used to auto-align
    151    labels preceding instructions.  */
    152 static symbolS *pru_last_label;
    153 
    154 
    155 /** Utility routines.  */
    157 /* Function md_chars_to_number takes the sequence of
    158    bytes in buf and returns the corresponding value
    159    in an int.  n must be 1, 2, 4 or 8.  */
    160 static uint64_t
    161 md_chars_to_number (char *buf, int n)
    162 {
    163   int i;
    164   uint64_t val;
    165 
    166   gas_assert (n == 1 || n == 2 || n == 4 || n == 8);
    167 
    168   val = 0;
    169   for (i = 0; i < n; ++i)
    170     val = val | ((buf[i] & 0xff) << 8 * i);
    171   return val;
    172 }
    173 
    174 
    175 /* This function turns a C long int, short int or char
    176    into the series of bytes that represent the number
    177    on the target machine.  */
    178 void
    179 md_number_to_chars (char *buf, valueT val, int n)
    180 {
    181   gas_assert (n == 1 || n == 2 || n == 4 || n == 8);
    182   number_to_chars_littleendian (buf, val, n);
    183 }
    184 
    185 /* Turn a string in input_line_pointer into a floating point constant
    186    of type TYPE, and store the appropriate bytes in *LITP.  The number
    187    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
    188    returned, or NULL on OK.  */
    189 const char *
    190 md_atof (int type, char *litP, int *sizeP)
    191 {
    192   return ieee_md_atof (type, litP, sizeP, FALSE);
    193 }
    194 
    195 /* Return true if STR starts with PREFIX, which should be a string literal.  */
    196 #define strprefix(STR, PREFIX) \
    197   (strncmp ((STR), PREFIX, strlen (PREFIX)) == 0)
    198 
    199 /* nop fill pattern for text section.  */
    200 static char const nop[4] = { 0xe0, 0xe0, 0xe0, 0x12 };
    201 
    202 /* Handles all machine-dependent alignment needs.  */
    203 static void
    204 pru_align (int log_size, const char *pfill, symbolS *label)
    205 {
    206   int align;
    207   long max_alignment = 15;
    208 
    209   /* The front end is prone to changing segments out from under us
    210      temporarily when -g is in effect.  */
    211   int switched_seg_p = (pru_current_align_seg != now_seg);
    212 
    213   align = log_size;
    214   if (align > max_alignment)
    215     {
    216       align = max_alignment;
    217       as_bad (_("Alignment too large: %d assumed"), align);
    218     }
    219   else if (align < 0)
    220     {
    221       as_warn (_("Alignment negative: 0 assumed"));
    222       align = 0;
    223     }
    224 
    225   if (align != 0)
    226     {
    227       if (subseg_text_p (now_seg) && align >= 2)
    228 	{
    229 	  /* First, make sure we're on a four-byte boundary, in case
    230 	     someone has been putting .byte values the text section.  */
    231 	  if (pru_current_align < 2 || switched_seg_p)
    232 	    frag_align (2, 0, 0);
    233 
    234 	  /* Now fill in the alignment pattern.  */
    235 	  if (pfill != NULL)
    236 	    frag_align_pattern (align, pfill, sizeof nop, 0);
    237 	  else
    238 	    frag_align (align, 0, 0);
    239 	}
    240       else
    241 	frag_align (align, 0, 0);
    242 
    243       if (!switched_seg_p)
    244 	pru_current_align = align;
    245 
    246       /* If the last label was in a different section we can't align it.  */
    247       if (label != NULL && !switched_seg_p)
    248 	{
    249 	  symbolS *sym;
    250 	  int label_seen = FALSE;
    251 	  struct frag *old_frag;
    252 	  valueT old_value;
    253 	  valueT new_value;
    254 
    255 	  gas_assert (S_GET_SEGMENT (label) == now_seg);
    256 
    257 	  old_frag = symbol_get_frag (label);
    258 	  old_value = S_GET_VALUE (label);
    259 	  new_value = (valueT) frag_now_fix ();
    260 
    261 	  /* It is possible to have more than one label at a particular
    262 	     address, especially if debugging is enabled, so we must
    263 	     take care to adjust all the labels at this address in this
    264 	     fragment.  To save time we search from the end of the symbol
    265 	     list, backwards, since the symbols we are interested in are
    266 	     almost certainly the ones that were most recently added.
    267 	     Also to save time we stop searching once we have seen at least
    268 	     one matching label, and we encounter a label that is no longer
    269 	     in the target fragment.  Note, this search is guaranteed to
    270 	     find at least one match when sym == label, so no special case
    271 	     code is necessary.  */
    272 	  for (sym = symbol_lastP; sym != NULL; sym = symbol_previous (sym))
    273 	    if (symbol_get_frag (sym) == old_frag
    274 		&& S_GET_VALUE (sym) == old_value)
    275 	      {
    276 		label_seen = TRUE;
    277 		symbol_set_frag (sym, frag_now);
    278 		S_SET_VALUE (sym, new_value);
    279 	      }
    280 	    else if (label_seen && symbol_get_frag (sym) != old_frag)
    281 	      break;
    282 	}
    283       record_alignment (now_seg, align);
    284     }
    285 }
    286 
    287 
    288 /** Support for self-check mode.  */
    290 
    291 /* Mode of the assembler.  */
    292 typedef enum
    293 {
    294   PRU_MODE_ASSEMBLE,		/* Ordinary operation.  */
    295   PRU_MODE_TEST		/* Hidden mode used for self testing.  */
    296 } PRU_MODE;
    297 
    298 static PRU_MODE pru_mode = PRU_MODE_ASSEMBLE;
    299 
    300 /* This function is used to in self-checking mode
    301    to check the assembled instruction.
    302    OPCODE should be the assembled opcode, and exp_opcode
    303    the parsed string representing the expected opcode.  */
    304 
    305 static void
    306 pru_check_assembly (unsigned int opcode, const char *exp_opcode)
    307 {
    308   if (pru_mode == PRU_MODE_TEST)
    309     {
    310       if (exp_opcode == NULL)
    311 	as_bad (_("expecting opcode string in self test mode"));
    312       else if (opcode != strtoul (exp_opcode, NULL, 16))
    313 	as_bad (_("assembly 0x%08x, expected %s"), opcode, exp_opcode);
    314     }
    315 }
    316 
    317 
    318 /** Support for machine-dependent assembler directives.  */
    320 /* Handle the .align pseudo-op.  This aligns to a power of two.  It
    321    also adjusts any current instruction label.  We treat this the same
    322    way the MIPS port does: .align 0 turns off auto alignment.  */
    323 static void
    324 s_pru_align (int ignore ATTRIBUTE_UNUSED)
    325 {
    326   int align;
    327   char fill;
    328   const char *pfill = NULL;
    329   long max_alignment = 15;
    330 
    331   align = get_absolute_expression ();
    332   if (align > max_alignment)
    333     {
    334       align = max_alignment;
    335       as_bad (_("Alignment too large: %d assumed"), align);
    336     }
    337   else if (align < 0)
    338     {
    339       as_warn (_("Alignment negative: 0 assumed"));
    340       align = 0;
    341     }
    342 
    343   if (*input_line_pointer == ',')
    344     {
    345       input_line_pointer++;
    346       fill = get_absolute_expression ();
    347       pfill = (const char *) &fill;
    348     }
    349   else if (subseg_text_p (now_seg))
    350     pfill = (const char *) &nop;
    351   else
    352     {
    353       pfill = NULL;
    354       pru_last_label = NULL;
    355     }
    356 
    357   if (align != 0)
    358     {
    359       pru_auto_align_on = 1;
    360       pru_align (align, pfill, pru_last_label);
    361       pru_last_label = NULL;
    362     }
    363   else
    364     pru_auto_align_on = 0;
    365 
    366   demand_empty_rest_of_line ();
    367 }
    368 
    369 /* Handle the .text pseudo-op.  This is like the usual one, but it
    370    clears the saved last label and resets known alignment.  */
    371 static void
    372 s_pru_text (int i)
    373 {
    374   s_text (i);
    375   pru_last_label = NULL;
    376   pru_current_align = 0;
    377   pru_current_align_seg = now_seg;
    378 }
    379 
    380 /* Handle the .data pseudo-op.  This is like the usual one, but it
    381    clears the saved last label and resets known alignment.  */
    382 static void
    383 s_pru_data (int i)
    384 {
    385   s_data (i);
    386   pru_last_label = NULL;
    387   pru_current_align = 0;
    388   pru_current_align_seg = now_seg;
    389 }
    390 
    391 /* Handle the .section pseudo-op.  This is like the usual one, but it
    392    clears the saved last label and resets known alignment.  */
    393 static void
    394 s_pru_section (int ignore)
    395 {
    396   obj_elf_section (ignore);
    397   pru_last_label = NULL;
    398   pru_current_align = 0;
    399   pru_current_align_seg = now_seg;
    400 }
    401 
    402 /* Explicitly unaligned cons.  */
    403 static void
    404 s_pru_ucons (int nbytes)
    405 {
    406   int hold;
    407   hold = pru_auto_align_on;
    408   pru_auto_align_on = 0;
    409   cons (nbytes);
    410   pru_auto_align_on = hold;
    411 }
    412 
    413 /* .set sets assembler options.  */
    414 static void
    415 s_pru_set (int equiv)
    416 {
    417   char *save = input_line_pointer;
    418   char *directive;
    419   char delim = get_symbol_name (&directive);
    420   char *endline = input_line_pointer;
    421 
    422   (void) restore_line_pointer (delim);
    423 
    424   /* We only want to handle ".set XXX" if the
    425      user has tried ".set XXX, YYY" they are not
    426      trying a directive.  This prevents
    427      us from polluting the name space.  */
    428   SKIP_WHITESPACE ();
    429   if (is_end_of_line[(unsigned char) *input_line_pointer])
    430     {
    431       bfd_boolean done = TRUE;
    432       *endline = 0;
    433 
    434       if (!strcmp (directive, "no_warn_regname_label"))
    435 	  pru_opt.warn_regname_label = FALSE;
    436       else
    437 	done = FALSE;
    438 
    439       if (done)
    440 	{
    441 	  *endline = delim;
    442 	  demand_empty_rest_of_line ();
    443 	  return;
    444 	}
    445     }
    446 
    447   /* If we fall through to here, either we have ".set XXX, YYY"
    448      or we have ".set XXX" where XXX is unknown or we have
    449      a syntax error.  */
    450   input_line_pointer = save;
    451   s_set (equiv);
    452 }
    453 
    454 /* Machine-dependent assembler directives.
    455    Format of each entry is:
    456    { "directive", handler_func, param }	 */
    457 const pseudo_typeS md_pseudo_table[] = {
    458   {"align", s_pru_align, 0},
    459   {"text", s_pru_text, 0},
    460   {"data", s_pru_data, 0},
    461   {"section", s_pru_section, 0},
    462   {"section.s", s_pru_section, 0},
    463   {"sect", s_pru_section, 0},
    464   {"sect.s", s_pru_section, 0},
    465   /* .dword and .half are included for compatibility with MIPS.  */
    466   {"dword", cons, 8},
    467   {"half", cons, 2},
    468   /* PRU native word size is 4 bytes, so we override
    469      the GAS default of 2.  */
    470   {"word", cons, 4},
    471   /* Explicitly unaligned directives.  */
    472   {"2byte", s_pru_ucons, 2},
    473   {"4byte", s_pru_ucons, 4},
    474   {"8byte", s_pru_ucons, 8},
    475   {"16byte", s_pru_ucons, 16},
    476   {"set", s_pru_set, 0},
    477   {NULL, NULL, 0}
    478 };
    479 
    480 
    481 int
    483 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
    484 			       asection *seg ATTRIBUTE_UNUSED)
    485 {
    486   abort ();
    487   return 0;
    488 }
    489 
    490 void
    491 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED,
    492 		 fragS *fragp ATTRIBUTE_UNUSED)
    493 {
    494   abort ();
    495 }
    496 
    497 
    498 static bfd_boolean
    500 relaxable_section (asection *sec)
    501 {
    502   return ((sec->flags & SEC_DEBUGGING) == 0
    503 	  && (sec->flags & SEC_CODE) != 0
    504 	  && (sec->flags & SEC_ALLOC) != 0);
    505 }
    506 
    507 /* Does whatever the xtensa port does.  */
    508 int
    509 pru_validate_fix_sub (fixS *fix)
    510 {
    511   segT add_symbol_segment, sub_symbol_segment;
    512 
    513   /* The difference of two symbols should be resolved by the assembler when
    514      linkrelax is not set.  If the linker may relax the section containing
    515      the symbols, then an Xtensa DIFF relocation must be generated so that
    516      the linker knows to adjust the difference value.  */
    517   if (!linkrelax || fix->fx_addsy == NULL)
    518     return 0;
    519 
    520   /* Make sure both symbols are in the same segment, and that segment is
    521      "normal" and relaxable.  If the segment is not "normal", then the
    522      fix is not valid.  If the segment is not "relaxable", then the fix
    523      should have been handled earlier.  */
    524   add_symbol_segment = S_GET_SEGMENT (fix->fx_addsy);
    525   if (! SEG_NORMAL (add_symbol_segment)
    526       || ! relaxable_section (add_symbol_segment))
    527     return 0;
    528 
    529   sub_symbol_segment = S_GET_SEGMENT (fix->fx_subsy);
    530   return (sub_symbol_segment == add_symbol_segment);
    531 }
    532 
    533 /* TC_FORCE_RELOCATION hook.  */
    534 
    535 /* If linkrelax is turned on, and the symbol to relocate
    536    against is in a relaxable segment, don't compute the value -
    537    generate a relocation instead.  */
    538 int
    539 pru_force_relocation (fixS *fix)
    540 {
    541   if (linkrelax && fix->fx_addsy
    542       && relaxable_section (S_GET_SEGMENT (fix->fx_addsy)))
    543     return 1;
    544 
    545   return generic_force_reloc (fix);
    546 }
    547 
    548 
    549 
    550 /** Fixups and overflow checking.  */
    552 
    553 /* Check a fixup for overflow.  */
    554 static bfd_reloc_status_type
    555 pru_check_overflow (valueT fixup, reloc_howto_type *howto)
    556 {
    557   bfd_reloc_status_type ret;
    558 
    559   ret = bfd_check_overflow (howto->complain_on_overflow,
    560 			    howto->bitsize,
    561 			    howto->rightshift,
    562 			    bfd_get_reloc_size (howto) * 8,
    563 			    fixup);
    564 
    565   return ret;
    566 }
    567 
    568 /* Emit diagnostic for fixup overflow.  */
    569 static void
    570 pru_diagnose_overflow (valueT fixup, reloc_howto_type *howto,
    571 			 fixS *fixP, valueT value)
    572 {
    573   if (fixP->fx_r_type == BFD_RELOC_8
    574       || fixP->fx_r_type == BFD_RELOC_16
    575       || fixP->fx_r_type == BFD_RELOC_32)
    576     /* These relocs are against data, not instructions.  */
    577     as_bad_where (fixP->fx_file, fixP->fx_line,
    578 		  _("immediate value 0x%x truncated to 0x%x"),
    579 		  (unsigned int) fixup,
    580 		  (unsigned int) (~(~(valueT) 0 << howto->bitsize) & fixup));
    581   else
    582     {
    583       /* What opcode is the instruction?  This will determine
    584 	 whether we check for overflow in immediate values
    585 	 and what error message we get.  */
    586       const struct pru_opcode *opcode;
    587       enum overflow_type overflow_msg_type;
    588       unsigned int range_min;
    589       unsigned int range_max;
    590       unsigned int address;
    591       gas_assert (fixP->fx_size == 4);
    592       opcode = pru_find_opcode (value);
    593       gas_assert (opcode);
    594       overflow_msg_type = opcode->overflow_msg;
    595       switch (overflow_msg_type)
    596 	{
    597 	case call_target_overflow:
    598 	  range_min
    599 	    = ((fixP->fx_frag->fr_address + fixP->fx_where) & 0xf0000000);
    600 	  range_max = range_min + 0x0fffffff;
    601 	  address = fixup | range_min;
    602 
    603 	  as_bad_where (fixP->fx_file, fixP->fx_line,
    604 			_("call target address 0x%08x out of range 0x%08x to 0x%08x"),
    605 			address, range_min, range_max);
    606 	  break;
    607 	case qbranch_target_overflow:
    608 	  as_bad_where (fixP->fx_file, fixP->fx_line,
    609 			_("quick branch offset %d out of range %d to %d"),
    610 			(int)fixup, -((1<<9) * 4), (1 << 9) * 4);
    611 	  break;
    612 	case address_offset_overflow:
    613 	  as_bad_where (fixP->fx_file, fixP->fx_line,
    614 			_("%s offset %d out of range %d to %d"),
    615 			opcode->name, (int)fixup, -32768, 32767);
    616 	  break;
    617 	case signed_immed16_overflow:
    618 	  as_bad_where (fixP->fx_file, fixP->fx_line,
    619 			_("immediate value %d out of range %d to %d"),
    620 			(int)fixup, -32768, 32767);
    621 	  break;
    622 	case unsigned_immed32_overflow:
    623 	  as_bad_where (fixP->fx_file, fixP->fx_line,
    624 			_("immediate value %llu out of range %u to %lu"),
    625 			(unsigned long long)fixup, 0, 0xfffffffflu);
    626 	  break;
    627 	case unsigned_immed16_overflow:
    628 	  as_bad_where (fixP->fx_file, fixP->fx_line,
    629 			_("immediate value %u out of range %u to %u"),
    630 			(unsigned int)fixup, 0, 65535);
    631 	  break;
    632 	case unsigned_immed5_overflow:
    633 	  as_bad_where (fixP->fx_file, fixP->fx_line,
    634 			_("immediate value %u out of range %u to %u"),
    635 			(unsigned int)fixup, 0, 31);
    636 	  break;
    637 	default:
    638 	  as_bad_where (fixP->fx_file, fixP->fx_line,
    639 			_("overflow in immediate argument"));
    640 	  break;
    641 	}
    642     }
    643 }
    644 
    645 /* Apply a fixup to the object file.  */
    646 void
    647 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
    648 {
    649   unsigned char *where;
    650   valueT value = *valP;
    651 
    652   /* Assert that the fixup is one we can handle.  */
    653   gas_assert (fixP != NULL && valP != NULL
    654 	      && (fixP->fx_r_type == BFD_RELOC_8
    655 		  || fixP->fx_r_type == BFD_RELOC_16
    656 		  || fixP->fx_r_type == BFD_RELOC_32
    657 		  || fixP->fx_r_type == BFD_RELOC_64
    658 		  || fixP->fx_r_type == BFD_RELOC_PRU_LDI32
    659 		  || fixP->fx_r_type == BFD_RELOC_PRU_U16
    660 		  || fixP->fx_r_type == BFD_RELOC_PRU_U16_PMEMIMM
    661 		  || fixP->fx_r_type == BFD_RELOC_PRU_S10_PCREL
    662 		  || fixP->fx_r_type == BFD_RELOC_PRU_U8_PCREL
    663 		  || fixP->fx_r_type == BFD_RELOC_PRU_32_PMEM
    664 		  || fixP->fx_r_type == BFD_RELOC_PRU_16_PMEM
    665 		  /* Add other relocs here as we generate them.  */
    666 	      ));
    667 
    668   if (fixP->fx_r_type == BFD_RELOC_64)
    669     {
    670       /* We may reach here due to .8byte directives, but we never output
    671 	 BFD_RELOC_64; it must be resolved.  */
    672       if (fixP->fx_addsy != NULL)
    673 	as_bad_where (fixP->fx_file, fixP->fx_line,
    674 		      _("cannot create 64-bit relocation"));
    675       else
    676 	{
    677 	  md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
    678 			      *valP, 8);
    679 	  fixP->fx_done = 1;
    680 	}
    681       return;
    682     }
    683 
    684   /* gas_assert (had_errors () || !fixP->fx_subsy); */
    685 
    686   /* In general, fix instructions with immediate
    687      constants.  But leave LDI32 for the linker,
    688      which is prepared to shorten insns.  */
    689   if (fixP->fx_addsy == (symbolS *) NULL
    690       && fixP->fx_r_type != BFD_RELOC_PRU_LDI32)
    691     fixP->fx_done = 1;
    692 
    693   else if (fixP->fx_pcrel)
    694     {
    695       segT s = S_GET_SEGMENT (fixP->fx_addsy);
    696 
    697       if (s == seg || s == absolute_section)
    698 	{
    699 	  /* Blindly copied from AVR, but I don't understand why
    700 	     this is needed in the first place.  Fail hard to catch
    701 	     when this curious code snippet is utilized.  */
    702 	  as_bad_where (fixP->fx_file, fixP->fx_line,
    703 			_("unexpected PC relative expression"));
    704 	  value += S_GET_VALUE (fixP->fx_addsy);
    705 	  fixP->fx_done = 1;
    706 	}
    707     }
    708   else if (linkrelax && fixP->fx_subsy)
    709     {
    710       /* For a subtraction relocation expression, generate one
    711 	 of the DIFF relocs, with the value being the difference.
    712 	 Note that a sym1 - sym2 expression is adjusted into a
    713 	 section_start_sym + sym4_offset_from_section_start - sym1
    714 	 expression.  fixP->fx_addsy holds the section start symbol,
    715 	 fixP->fx_offset holds sym2's offset, and fixP->fx_subsy
    716 	 holds sym1.  Calculate the current difference and write value,
    717 	 but leave fx_offset as is - during relaxation,
    718 	 fx_offset - value gives sym1's value.  */
    719 
    720       offsetT diffval;	/* valueT is unsigned, so use offsetT.  */
    721 
    722       diffval = S_GET_VALUE (fixP->fx_addsy)
    723 		+ fixP->fx_offset - S_GET_VALUE (fixP->fx_subsy);
    724 
    725       switch (fixP->fx_r_type)
    726 	{
    727 	case BFD_RELOC_8:
    728 	  fixP->fx_r_type = BFD_RELOC_PRU_GNU_DIFF8;
    729 	  break;
    730 	case BFD_RELOC_16:
    731 	  fixP->fx_r_type = BFD_RELOC_PRU_GNU_DIFF16;
    732 	  break;
    733 	case BFD_RELOC_32:
    734 	  fixP->fx_r_type = BFD_RELOC_PRU_GNU_DIFF32;
    735 	  break;
    736 	case BFD_RELOC_PRU_16_PMEM:
    737 	  fixP->fx_r_type = BFD_RELOC_PRU_GNU_DIFF16_PMEM;
    738 	  if (diffval % 4)
    739 	    as_bad_where (fixP->fx_file, fixP->fx_line,
    740 			  _("residual low bits in pmem diff relocation"));
    741 	  diffval /= 4;
    742 	  break;
    743 	case BFD_RELOC_PRU_32_PMEM:
    744 	  fixP->fx_r_type = BFD_RELOC_PRU_GNU_DIFF32_PMEM;
    745 	  if (diffval % 4)
    746 	    as_bad_where (fixP->fx_file, fixP->fx_line,
    747 			  _("residual low bits in pmem diff relocation"));
    748 	  diffval /= 4;
    749 	  break;
    750 	default:
    751 	  as_bad_where (fixP->fx_file, fixP->fx_line,
    752 			_("expression too complex"));
    753 	  break;
    754 	}
    755 
    756       value = *valP = diffval;
    757 
    758       fixP->fx_subsy = NULL;
    759   }
    760   /* We don't actually support subtracting a symbol.  */
    761   if (fixP->fx_subsy != (symbolS *) NULL)
    762     as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
    763 
    764   /* For the DIFF relocs, write the value into the object file while still
    765      keeping fx_done FALSE, as both the difference (recorded in the object file)
    766      and the sym offset (part of fixP) are needed at link relax time.  */
    767   where = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
    768   switch (fixP->fx_r_type)
    769     {
    770     case BFD_RELOC_PRU_GNU_DIFF8:
    771       *where = value;
    772       break;
    773     case BFD_RELOC_PRU_GNU_DIFF16:
    774     case BFD_RELOC_PRU_GNU_DIFF16_PMEM:
    775       bfd_putl16 ((bfd_vma) value, where);
    776       break;
    777     case BFD_RELOC_PRU_GNU_DIFF32:
    778     case BFD_RELOC_PRU_GNU_DIFF32_PMEM:
    779       bfd_putl32 ((bfd_vma) value, where);
    780       break;
    781     default:
    782       break;
    783     }
    784 
    785   if (fixP->fx_done)
    786     /* Fully resolved fixup.  */
    787     {
    788       reloc_howto_type *howto
    789 	= bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
    790 
    791       if (howto == NULL)
    792 	as_bad_where (fixP->fx_file, fixP->fx_line,
    793 		      _("relocation is not supported"));
    794       else
    795 	{
    796 	  valueT fixup = value;
    797 	  uint64_t insn;
    798 	  char *buf;
    799 
    800 	  /* Get the instruction or data to be fixed up.  */
    801 	  buf = fixP->fx_frag->fr_literal + fixP->fx_where;
    802 	  insn = md_chars_to_number (buf, fixP->fx_size);
    803 
    804 	  /* Check for overflow, emitting a diagnostic if necessary.  */
    805 	  if (pru_check_overflow (fixup, howto) != bfd_reloc_ok)
    806 	    pru_diagnose_overflow (fixup, howto, fixP, insn);
    807 
    808 	  /* Apply the right shift.  */
    809 	  fixup = (offsetT) fixup >> howto->rightshift;
    810 
    811 	  /* Truncate the fixup to right size.  */
    812 	  if (howto->bitsize == 0)
    813 	    fixup = 0;
    814 	  else
    815 	    fixup &= ((valueT) 2 << (howto->bitsize - 1)) - 1;
    816 
    817 	  /* Fix up the instruction.  Non-contiguous bitfields need
    818 	     special handling.  */
    819 	  if (fixP->fx_r_type == BFD_RELOC_PRU_LDI32)
    820 	    {
    821 	      /* As the only 64-bit "insn", LDI32 needs special handling. */
    822 	      uint32_t insn1 = insn & 0xffffffff;
    823 	      uint32_t insn2 = insn >> 32;
    824 	      SET_INSN_FIELD (IMM16, insn1, fixup >> 16);
    825 	      SET_INSN_FIELD (IMM16, insn2, fixup & 0xffff);
    826 
    827 	      SET_INSN_FIELD (RDSEL, insn1, RSEL_31_16);
    828 	      SET_INSN_FIELD (RDSEL, insn2, RSEL_15_0);
    829 
    830 	      md_number_to_chars (buf, insn1, 4);
    831 	      md_number_to_chars (buf + 4, insn2, 4);
    832 	    }
    833 	  else
    834 	    {
    835 	      if (fixP->fx_r_type == BFD_RELOC_PRU_S10_PCREL)
    836 		SET_BROFF_URAW (insn, fixup);
    837 	      else
    838 		insn = (insn & ~howto->dst_mask) | (fixup << howto->bitpos);
    839 	      md_number_to_chars (buf, insn, fixP->fx_size);
    840 	    }
    841 	}
    842 
    843       fixP->fx_done = 1;
    844     }
    845 
    846   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
    847     {
    848       fixP->fx_done = 0;
    849       if (fixP->fx_addsy
    850 	  && !S_IS_DEFINED (fixP->fx_addsy) && !S_IS_WEAK (fixP->fx_addsy))
    851 	S_SET_WEAK (fixP->fx_addsy);
    852     }
    853   else if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
    854     fixP->fx_done = 0;
    855 }
    856 
    857 
    858 
    859 /** Instruction parsing support.  */
    861 
    862 /* Creates a new pru_insn_relocS and returns a pointer to it.  */
    863 static pru_insn_relocS *
    864 pru_insn_reloc_new (bfd_reloc_code_real_type reloc_type, unsigned int pcrel)
    865 {
    866   pru_insn_relocS *retval;
    867   retval = XNEW (pru_insn_relocS);
    868   if (retval == NULL)
    869     {
    870       as_bad (_("can't create relocation"));
    871       abort ();
    872     }
    873 
    874   /* Fill out the fields with default values.  */
    875   retval->reloc_next = NULL;
    876   retval->reloc_type = reloc_type;
    877   retval->reloc_pcrel = pcrel;
    878   return retval;
    879 }
    880 
    881 /* Frees up memory previously allocated by pru_insn_reloc_new ().  */
    882 static void
    883 pru_insn_reloc_destroy (pru_insn_relocS *reloc)
    884 {
    885   pru_insn_relocS *next;
    886 
    887   while (reloc)
    888     {
    889       next = reloc->reloc_next;
    890       free (reloc);
    891       reloc = next;
    892     }
    893 }
    894 
    895 /* The various pru_assemble_* functions call this
    896    function to generate an expression from a string representing an expression.
    897    It then tries to evaluate the expression, and if it can, returns its value.
    898    If not, it creates a new pru_insn_relocS and stores the expression and
    899    reloc_type for future use.  */
    900 static unsigned long
    901 pru_assemble_expression (const char *exprstr,
    902 			   pru_insn_infoS *insn,
    903 			   pru_insn_relocS *prev_reloc,
    904 			   bfd_reloc_code_real_type reloc_type,
    905 			   unsigned int pcrel)
    906 {
    907   expressionS *ep;
    908   pru_insn_relocS *reloc;
    909   char *saved_line_ptr;
    910   unsigned short value;
    911 
    912   gas_assert (exprstr != NULL);
    913   gas_assert (insn != NULL);
    914 
    915   /* We use this blank keyword to distinguish register from
    916      label operands.  */
    917   if (strstr (exprstr, "%label") != NULL)
    918     {
    919       exprstr += strlen ("%label") + 1;
    920     }
    921 
    922   /* Check for pmem relocation operator.
    923      Change the relocation type and advance the ptr to the start of
    924      the expression proper.  */
    925   if (strstr (exprstr, "%pmem") != NULL)
    926     {
    927       reloc_type = BFD_RELOC_PRU_U16_PMEMIMM;
    928       exprstr += strlen ("%pmem") + 1;
    929     }
    930 
    931   /* We potentially have a relocation.  */
    932   reloc = pru_insn_reloc_new (reloc_type, pcrel);
    933   if (prev_reloc != NULL)
    934     prev_reloc->reloc_next = reloc;
    935   else
    936     insn->insn_reloc = reloc;
    937 
    938   /* Parse the expression string.  */
    939   ep = &reloc->reloc_expression;
    940   saved_line_ptr = input_line_pointer;
    941   input_line_pointer = (char *) exprstr;
    942   SKIP_WHITESPACE ();
    943   expression (ep);
    944   SKIP_WHITESPACE ();
    945   if (*input_line_pointer)
    946     as_bad (_("trailing garbage after expression: %s"), input_line_pointer);
    947   input_line_pointer = saved_line_ptr;
    948 
    949 
    950   if (ep->X_op == O_illegal || ep->X_op == O_absent)
    951     as_bad (_("expected expression, got %s"), exprstr);
    952 
    953   /* This is redundant as the fixup will put this into
    954      the instruction, but it is included here so that
    955      self-test mode (-r) works.  */
    956   value = 0;
    957   if (pru_mode == PRU_MODE_TEST && ep->X_op == O_constant)
    958     value = ep->X_add_number;
    959 
    960   return (unsigned long) value;
    961 }
    962 
    963 /* Try to parse a non-relocatable expression.  */
    964 static unsigned long
    965 pru_assemble_noreloc_expression (const char *exprstr)
    966 {
    967   expressionS exp;
    968   char *saved_line_ptr;
    969   unsigned long val;
    970 
    971   gas_assert (exprstr != NULL);
    972 
    973   saved_line_ptr = input_line_pointer;
    974   input_line_pointer = (char *) exprstr;
    975   SKIP_WHITESPACE ();
    976   expression (&exp);
    977   SKIP_WHITESPACE ();
    978   if (*input_line_pointer)
    979     as_bad (_("trailing garbage after expression: %s"), input_line_pointer);
    980   input_line_pointer = saved_line_ptr;
    981 
    982   val = 0;
    983   if (exp.X_op != O_constant)
    984     as_bad (_("expected constant expression, got %s"), exprstr);
    985   else
    986     val = exp.X_add_number;
    987 
    988   return val;
    989 }
    990 
    991 /* Argument assemble functions.
    992    All take an instruction argument string, and a pointer
    993    to an instruction opcode.  Upon return the insn_opcode
    994    has the relevant fields filled in to represent the arg
    995    string.  The return value is NULL if successful, or
    996    an error message if an error was detected.  */
    997 
    998 static void
    999 pru_assemble_arg_d (pru_insn_infoS *insn_info, const char *argstr)
   1000 {
   1001   struct pru_reg *dst = pru_reg_lookup (argstr);
   1002 
   1003   if (dst == NULL)
   1004     as_bad (_("unknown register %s"), argstr);
   1005   else
   1006     {
   1007       SET_INSN_FIELD (RD, insn_info->insn_code, dst->index);
   1008       SET_INSN_FIELD (RDSEL, insn_info->insn_code, dst->regsel);
   1009     }
   1010 }
   1011 
   1012 static void
   1013 pru_assemble_arg_D (pru_insn_infoS *insn_info, const char *argstr)
   1014 {
   1015   struct pru_reg *dst;
   1016 
   1017   /* The leading & before an address register is optional.  */
   1018   if (*argstr == '&')
   1019     argstr++;
   1020 
   1021   dst = pru_reg_lookup (argstr);
   1022 
   1023   if (dst == NULL)
   1024     as_bad (_("unknown register %s"), argstr);
   1025   else
   1026     {
   1027       unsigned long rxb = 0;
   1028 
   1029       switch (dst->regsel)
   1030 	{
   1031 	case RSEL_31_0: rxb = 0; break;	/* whole register defaults to .b0  */
   1032 	case RSEL_7_0: rxb = 0; break;
   1033 	case RSEL_15_8: rxb = 1; break;
   1034 	case RSEL_23_16: rxb = 2; break;
   1035 	case RSEL_31_24: rxb = 3; break;
   1036 	default:
   1037 	  as_bad (_("data transfer register cannot be halfword"));
   1038 	}
   1039 
   1040       SET_INSN_FIELD (RD, insn_info->insn_code, dst->index);
   1041       SET_INSN_FIELD (RDB, insn_info->insn_code, rxb);
   1042     }
   1043 }
   1044 
   1045 static void
   1046 pru_assemble_arg_R (pru_insn_infoS *insn_info, const char *argstr)
   1047 {
   1048   struct pru_reg *dst = pru_reg_lookup (argstr);
   1049 
   1050   if (dst == NULL)
   1051     as_bad (_("unknown register %s"), argstr);
   1052   else
   1053     {
   1054       if (dst->regsel != RSEL_31_0)
   1055 	{
   1056 	  as_bad (_("destination register must be full-word"));
   1057 	}
   1058 
   1059       SET_INSN_FIELD (RD, insn_info->insn_code, dst->index);
   1060       SET_INSN_FIELD (RDSEL, insn_info->insn_code, dst->regsel);
   1061     }
   1062 }
   1063 
   1064 static void
   1065 pru_assemble_arg_s (pru_insn_infoS *insn_info, const char *argstr)
   1066 {
   1067   struct pru_reg *src1 = pru_reg_lookup (argstr);
   1068 
   1069   if (src1 == NULL)
   1070     as_bad (_("unknown register %s"), argstr);
   1071   else
   1072     {
   1073       SET_INSN_FIELD (RS1, insn_info->insn_code, src1->index);
   1074       SET_INSN_FIELD (RS1SEL, insn_info->insn_code, src1->regsel);
   1075     }
   1076 }
   1077 
   1078 static void
   1079 pru_assemble_arg_S (pru_insn_infoS *insn_info, const char *argstr)
   1080 {
   1081   struct pru_reg *src1 = pru_reg_lookup (argstr);
   1082 
   1083   if (src1 == NULL)
   1084     as_bad (_("unknown register %s"), argstr);
   1085   else
   1086     {
   1087       if (src1->regsel != RSEL_31_0)
   1088 	as_bad (_("cannot use partial register %s for addressing"), argstr);
   1089       SET_INSN_FIELD (RS1, insn_info->insn_code, src1->index);
   1090     }
   1091 }
   1092 
   1093 static void
   1094 pru_assemble_arg_b (pru_insn_infoS *insn_info, const char *argstr)
   1095 {
   1096   struct pru_reg *src2 = pru_reg_lookup (argstr);
   1097   if (src2 == NULL)
   1098     {
   1099       unsigned long imm8 = pru_assemble_noreloc_expression (argstr);
   1100       if (imm8 >= 0x100)
   1101 	as_bad (_("value %lu is too large for a byte operand"), imm8);
   1102       SET_INSN_FIELD (IMM8, insn_info->insn_code, imm8);
   1103       SET_INSN_FIELD (IO, insn_info->insn_code, 1);
   1104     }
   1105   else
   1106     {
   1107       SET_INSN_FIELD (IO, insn_info->insn_code, 0);
   1108       SET_INSN_FIELD (RS2, insn_info->insn_code, src2->index);
   1109       SET_INSN_FIELD (RS2SEL, insn_info->insn_code, src2->regsel);
   1110     }
   1111 
   1112 }
   1113 
   1114 static void
   1115 pru_assemble_arg_B (pru_insn_infoS *insn_info, const char *argstr)
   1116 {
   1117   struct pru_reg *src2 = pru_reg_lookup (argstr);
   1118   if (src2 == NULL)
   1119     {
   1120       unsigned long imm8;
   1121       imm8 = pru_assemble_noreloc_expression (argstr);
   1122       if (!imm8 || imm8 > 0xff)
   1123 	as_bad (_("loop count constant %ld is out of range [1..%d]"),
   1124 		imm8, 0xff);
   1125       /* Note: HW expects the immediate loop count field
   1126 	 to be one less than the actual loop count.  */
   1127       SET_INSN_FIELD (IMM8, insn_info->insn_code, imm8 - 1);
   1128       SET_INSN_FIELD (IO, insn_info->insn_code, 1);
   1129     }
   1130   else
   1131     {
   1132       SET_INSN_FIELD (IO, insn_info->insn_code, 0);
   1133       SET_INSN_FIELD (RS2, insn_info->insn_code, src2->index);
   1134       SET_INSN_FIELD (RS2SEL, insn_info->insn_code, src2->regsel);
   1135     }
   1136 }
   1137 
   1138 static void
   1139 pru_assemble_arg_i (pru_insn_infoS *insn_info, const char *argstr)
   1140 {
   1141   unsigned long imm32;
   1142 
   1143   /* We must not generate PRU_LDI32 relocation if relaxation is disabled in
   1144      GAS. Consider the following scenario: GAS relaxation is disabled, so
   1145      DIFF* expressions are fixed and not emitted as relocations. Then if LD
   1146      has relaxation enabled, it may shorten LDI32 but will not update
   1147      accordingly the DIFF expressions.  */
   1148   if (pru_opt.link_relax)
   1149     imm32 = pru_assemble_expression (argstr, insn_info,
   1150 				     insn_info->insn_reloc,
   1151 				     BFD_RELOC_PRU_LDI32, 0);
   1152   else
   1153     imm32 = pru_assemble_noreloc_expression (argstr);
   1154 
   1155   /* QUIRK: LDI must clear IO bit high, even though it has immediate arg. */
   1156   SET_INSN_FIELD (IO, insn_info->insn_code, 0);
   1157   SET_INSN_FIELD (RDSEL, insn_info->insn_code, RSEL_31_16);
   1158   SET_INSN_FIELD (IMM16, insn_info->insn_code, imm32 >> 16);
   1159   insn_info->ldi32_imm32 = imm32;
   1160 }
   1161 
   1162 static void
   1163 pru_assemble_arg_j (pru_insn_infoS *insn_info, const char *argstr)
   1164 {
   1165   struct pru_reg *src2 = pru_reg_lookup (argstr);
   1166 
   1167   if (src2 == NULL)
   1168     {
   1169       unsigned long imm16 = pru_assemble_expression (argstr, insn_info,
   1170 						     insn_info->insn_reloc,
   1171 						     BFD_RELOC_PRU_U16_PMEMIMM,
   1172 						     0);
   1173       SET_INSN_FIELD (IMM16, insn_info->insn_code, imm16);
   1174       SET_INSN_FIELD (IO, insn_info->insn_code, 1);
   1175     }
   1176   else
   1177     {
   1178       SET_INSN_FIELD (IO, insn_info->insn_code, 0);
   1179       SET_INSN_FIELD (RS2, insn_info->insn_code, src2->index);
   1180       SET_INSN_FIELD (RS2SEL, insn_info->insn_code, src2->regsel);
   1181     }
   1182 }
   1183 
   1184 static void
   1185 pru_assemble_arg_W (pru_insn_infoS *insn_info, const char *argstr)
   1186 {
   1187   unsigned long imm16 = pru_assemble_expression (argstr, insn_info,
   1188 						 insn_info->insn_reloc,
   1189 						 BFD_RELOC_PRU_U16, 0);
   1190   /* QUIRK: LDI must clear IO bit high, even though it has immediate arg.  */
   1191   SET_INSN_FIELD (IO, insn_info->insn_code, 0);
   1192   SET_INSN_FIELD (IMM16, insn_info->insn_code, imm16);
   1193 }
   1194 
   1195 static void
   1196 pru_assemble_arg_o (pru_insn_infoS *insn_info, const char *argstr)
   1197 {
   1198   unsigned long imm10 = pru_assemble_expression (argstr, insn_info,
   1199 						 insn_info->insn_reloc,
   1200 						 BFD_RELOC_PRU_S10_PCREL, 1);
   1201   SET_BROFF_URAW (insn_info->insn_code, imm10);
   1202 }
   1203 
   1204 static void
   1205 pru_assemble_arg_O (pru_insn_infoS *insn_info, const char *argstr)
   1206 {
   1207   unsigned long imm8 = pru_assemble_expression (argstr, insn_info,
   1208 						insn_info->insn_reloc,
   1209 						BFD_RELOC_PRU_U8_PCREL, 1);
   1210   SET_INSN_FIELD (LOOP_JMPOFFS, insn_info->insn_code, imm8);
   1211 }
   1212 
   1213 static void
   1214 pru_assemble_arg_l (pru_insn_infoS *insn_info, const char *argstr)
   1215 {
   1216   unsigned long burstlen = 0;
   1217   struct pru_reg *blreg = pru_reg_lookup (argstr);
   1218 
   1219   if (blreg == NULL)
   1220     {
   1221       burstlen = pru_assemble_noreloc_expression (argstr);
   1222       if (!burstlen || burstlen > LSSBBO_BYTECOUNT_R0_BITS7_0)
   1223 	as_bad (_("byte count constant %ld is out of range [1..%d]"),
   1224 		burstlen, LSSBBO_BYTECOUNT_R0_BITS7_0);
   1225       burstlen--;
   1226     }
   1227   else
   1228     {
   1229       if (blreg->index != 0)
   1230 	as_bad (_("only r0 can be used as byte count register"));
   1231       else if (blreg->regsel > RSEL_31_24)
   1232 	as_bad (_("only r0.bX byte fields of r0 can be used as byte count"));
   1233       else
   1234 	burstlen = LSSBBO_BYTECOUNT_R0_BITS7_0 + blreg->regsel;
   1235     }
   1236     SET_BURSTLEN (insn_info->insn_code, burstlen);
   1237 }
   1238 
   1239 static void
   1240 pru_assemble_arg_n (pru_insn_infoS *insn_info, const char *argstr)
   1241 {
   1242   unsigned long burstlen = 0;
   1243   struct pru_reg *blreg = pru_reg_lookup (argstr);
   1244 
   1245   if (blreg == NULL)
   1246     {
   1247       burstlen = pru_assemble_noreloc_expression (argstr);
   1248       if (!burstlen || burstlen > LSSBBO_BYTECOUNT_R0_BITS7_0)
   1249 	as_bad (_("byte count constant %ld is out of range [1..%d]"),
   1250 		burstlen, LSSBBO_BYTECOUNT_R0_BITS7_0);
   1251       burstlen--;
   1252     }
   1253   else
   1254     {
   1255       if (blreg->index != 0)
   1256 	as_bad (_("only r0 can be used as byte count register"));
   1257       else if (blreg->regsel > RSEL_31_24)
   1258 	as_bad (_("only r0.bX byte fields of r0 can be used as byte count"));
   1259       else
   1260 	burstlen = LSSBBO_BYTECOUNT_R0_BITS7_0 + blreg->regsel;
   1261     }
   1262     SET_INSN_FIELD (XFR_LENGTH, insn_info->insn_code, burstlen);
   1263 }
   1264 
   1265 static void
   1266 pru_assemble_arg_c (pru_insn_infoS *insn_info, const char *argstr)
   1267 {
   1268   unsigned long cb = pru_assemble_noreloc_expression (argstr);
   1269 
   1270   if (cb > 31)
   1271     as_bad (_("invalid constant table offset %ld"), cb);
   1272   else
   1273     SET_INSN_FIELD (CB, insn_info->insn_code, cb);
   1274 }
   1275 
   1276 static void
   1277 pru_assemble_arg_w (pru_insn_infoS *insn_info, const char *argstr)
   1278 {
   1279   unsigned long wk = pru_assemble_noreloc_expression (argstr);
   1280 
   1281   if (wk != 0 && wk != 1)
   1282     as_bad (_("invalid WakeOnStatus %ld"), wk);
   1283   else
   1284     SET_INSN_FIELD (WAKEONSTATUS, insn_info->insn_code, wk);
   1285 }
   1286 
   1287 static void
   1288 pru_assemble_arg_x (pru_insn_infoS *insn_info, const char *argstr)
   1289 {
   1290   unsigned long wba = pru_assemble_noreloc_expression (argstr);
   1291 
   1292   if (wba > 255)
   1293     as_bad (_("invalid XFR WideBus Address %ld"), wba);
   1294   else
   1295     SET_INSN_FIELD (XFR_WBA, insn_info->insn_code, wba);
   1296 }
   1297 
   1298 /* The function consume_arg takes a pointer into a string
   1299    of instruction tokens (args) and a pointer into a string
   1300    representing the expected sequence of tokens and separators.
   1301    It checks whether the first argument in argstr is of the
   1302    expected type, throwing an error if it is not, and returns
   1303    the pointer argstr.  */
   1304 static char *
   1305 pru_consume_arg (char *argstr, const char *parsestr)
   1306 {
   1307   char *temp;
   1308 
   1309   switch (*parsestr)
   1310     {
   1311     case 'W':
   1312       if (*argstr == '%')
   1313 	{
   1314 	  if (strprefix (argstr, "%pmem") || strprefix (argstr, "%label"))
   1315 	    {
   1316 	      /* We zap the parentheses because we don't want them confused
   1317 		 with separators.  */
   1318 	      temp = strchr (argstr, '(');
   1319 	      if (temp != NULL)
   1320 		*temp = ' ';
   1321 	      temp = strchr (argstr, ')');
   1322 	      if (temp != NULL)
   1323 		*temp = ' ';
   1324 	    }
   1325 	  else
   1326 	    as_bad (_("badly formed expression near %s"), argstr);
   1327 	}
   1328       break;
   1329 
   1330     case 'j':
   1331     case 'o':
   1332     case 'O':
   1333       if (*argstr == '%')
   1334 	{
   1335 	  /* Only 'j' really requires %label for distinguishing registers
   1336 	     from labels, but we include 'o' and 'O' here to avoid
   1337 	     confusing assembler programmers. Thus for completeness all
   1338 	     jump operands can be prefixed with %label.  */
   1339 	  if (strprefix (argstr, "%label"))
   1340 	    {
   1341 	      /* We zap the parentheses because we don't want them confused
   1342 		 with separators.  */
   1343 	      temp = strchr (argstr, '(');
   1344 	      if (temp != NULL)
   1345 		*temp = ' ';
   1346 	      temp = strchr (argstr, ')');
   1347 	      if (temp != NULL)
   1348 		*temp = ' ';
   1349 	    }
   1350 	  else
   1351 	    as_bad (_("badly formed expression near %s"), argstr);
   1352 	}
   1353       break;
   1354 
   1355     case 'b':
   1356     case 'B':
   1357     case 'c':
   1358     case 'd':
   1359     case 'D':
   1360     case 'E':
   1361     case 'i':
   1362     case 's':
   1363     case 'S':
   1364     case 'l':
   1365     case 'n':
   1366     case 'R':
   1367     case 'w':
   1368     case 'x':
   1369       /* We can't have %pmem here.  */
   1370       if (*argstr == '%')
   1371 	as_bad (_("badly formed expression near %s"), argstr);
   1372       break;
   1373     default:
   1374       BAD_CASE (*parsestr);
   1375       break;
   1376     }
   1377 
   1378   return argstr;
   1379 }
   1380 
   1381 /* The function consume_separator takes a pointer into a string
   1382    of instruction tokens (args) and a pointer into a string representing
   1383    the expected sequence of tokens and separators.  It finds the first
   1384    instance of the character pointed to by separator in argstr, and
   1385    returns a pointer to the next element of argstr, which is the
   1386    following token in the sequence.  */
   1387 static char *
   1388 pru_consume_separator (char *argstr, const char *separator)
   1389 {
   1390   char *p;
   1391 
   1392   p = strchr (argstr, *separator);
   1393 
   1394   if (p != NULL)
   1395     *p++ = 0;
   1396   else
   1397     as_bad (_("expecting %c near %s"), *separator, argstr);
   1398   return p;
   1399 }
   1400 
   1401 
   1402 /* The principal argument parsing function which takes a string argstr
   1403    representing the instruction arguments for insn, and extracts the argument
   1404    tokens matching parsestr into parsed_args.  */
   1405 static void
   1406 pru_parse_args (pru_insn_infoS *insn ATTRIBUTE_UNUSED, char *argstr,
   1407 		  const char *parsestr, char **parsed_args)
   1408 {
   1409   char *p;
   1410   char *end = NULL;
   1411   int i;
   1412   p = argstr;
   1413   i = 0;
   1414   bfd_boolean terminate = FALSE;
   1415 
   1416   /* This rest of this function is it too fragile and it mostly works,
   1417      therefore special case this one.  */
   1418   if (*parsestr == 0 && argstr != 0)
   1419     {
   1420       as_bad (_("too many arguments"));
   1421       parsed_args[0] = NULL;
   1422       return;
   1423     }
   1424 
   1425   while (p != NULL && !terminate && i < PRU_MAX_INSN_TOKENS)
   1426     {
   1427       parsed_args[i] = pru_consume_arg (p, parsestr);
   1428       ++parsestr;
   1429       if (*parsestr != '\0')
   1430 	{
   1431 	  p = pru_consume_separator (p, parsestr);
   1432 	  ++parsestr;
   1433 	}
   1434       else
   1435 	{
   1436 	  /* Check that the argument string has no trailing arguments.  */
   1437 	  /* If we've got a %pmem relocation, we've zapped the parens with
   1438 	     spaces.  */
   1439 	  if (strprefix (p, "%pmem") || strprefix (p, "%label"))
   1440 	    end = strpbrk (p, ",");
   1441 	  else
   1442 	    end = strpbrk (p, " ,");
   1443 
   1444 	  if (end != NULL)
   1445 	    as_bad (_("too many arguments"));
   1446 	}
   1447 
   1448       if (*parsestr == '\0' || (p != NULL && *p == '\0'))
   1449 	terminate = TRUE;
   1450       ++i;
   1451     }
   1452 
   1453   parsed_args[i] = NULL;
   1454 
   1455   /* There are no instructions with optional arguments; complain.  */
   1456   if (*parsestr != '\0')
   1457     as_bad (_("missing argument"));
   1458 }
   1459 
   1460 
   1461 /** Assembler output support.  */
   1463 
   1464 /* Output a normal instruction.  */
   1465 static void
   1466 output_insn (pru_insn_infoS *insn)
   1467 {
   1468   char *f;
   1469   pru_insn_relocS *reloc;
   1470 
   1471   f = frag_more (4);
   1472   /* This allocates enough space for the instruction
   1473      and puts it in the current frag.  */
   1474   md_number_to_chars (f, insn->insn_code, 4);
   1475   /* Emit debug info.  */
   1476   dwarf2_emit_insn (4);
   1477   /* Create any fixups to be acted on later.  */
   1478   for (reloc = insn->insn_reloc; reloc != NULL; reloc = reloc->reloc_next)
   1479     fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
   1480 		 &reloc->reloc_expression, reloc->reloc_pcrel,
   1481 		 reloc->reloc_type);
   1482 }
   1483 
   1484 /* Output two LDI instructions from LDI32 macro */
   1485 static void
   1486 output_insn_ldi32 (pru_insn_infoS *insn)
   1487 {
   1488   char *f;
   1489   pru_insn_relocS *reloc;
   1490   unsigned long insn2;
   1491 
   1492   f = frag_more (8);
   1493   SET_INSN_FIELD (IMM16, insn->insn_code, insn->ldi32_imm32 >> 16);
   1494   SET_INSN_FIELD (RDSEL, insn->insn_code, RSEL_31_16);
   1495   md_number_to_chars (f, insn->insn_code, 4);
   1496 
   1497   insn2 = insn->insn_code;
   1498   SET_INSN_FIELD (IMM16, insn2, insn->ldi32_imm32 & 0xffff);
   1499   SET_INSN_FIELD (RDSEL, insn2, RSEL_15_0);
   1500   md_number_to_chars (f + 4, insn2, 4);
   1501 
   1502   /* Emit debug info.  */
   1503   dwarf2_emit_insn (8);
   1504 
   1505   /* Create any fixups to be acted on later.  */
   1506   for (reloc = insn->insn_reloc; reloc != NULL; reloc = reloc->reloc_next)
   1507     fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
   1508 		 &reloc->reloc_expression, reloc->reloc_pcrel,
   1509 		 reloc->reloc_type);
   1510 }
   1511 
   1512 
   1513 /** External interfaces.  */
   1515 
   1516 /* The following functions are called by machine-independent parts of
   1517    the assembler.  */
   1518 int
   1519 md_parse_option (int c, const char *arg ATTRIBUTE_UNUSED)
   1520 {
   1521   switch (c)
   1522     {
   1523     case 'r':
   1524       /* Hidden option for self-test mode.  */
   1525       pru_mode = PRU_MODE_TEST;
   1526       break;
   1527     case OPTION_LINK_RELAX:
   1528       pru_opt.link_relax = TRUE;
   1529       break;
   1530     case OPTION_NO_LINK_RELAX:
   1531       pru_opt.link_relax = FALSE;
   1532       break;
   1533     case OPTION_NO_WARN_REGNAME_LABEL:
   1534       pru_opt.warn_regname_label = FALSE;
   1535       break;
   1536     default:
   1537       return 0;
   1538       break;
   1539     }
   1540 
   1541   return 1;
   1542 }
   1543 
   1544 const char *
   1545 pru_target_format (void)
   1546 {
   1547   return "elf32-pru";
   1548 }
   1549 
   1550 /* Machine-dependent usage message.  */
   1551 void
   1552 md_show_usage (FILE *stream)
   1553 {
   1554   fprintf (stream,
   1555     _("PRU options:\n"
   1556       "  -mlink-relax     generate relocations for linker relaxation (default).\n"
   1557       "  -mno-link-relax  don't generate relocations for linker relaxation.\n"
   1558     ));
   1559 
   1560 }
   1561 
   1562 /* This function is called once, at assembler startup time.
   1563    It should set up all the tables, etc.  that the MD part of the
   1564    assembler will need.  */
   1565 void
   1566 md_begin (void)
   1567 {
   1568   int i;
   1569   const char *inserted;
   1570 
   1571   /* Create and fill a hashtable for the PRU opcodes, registers and
   1572      arguments.  */
   1573   pru_opcode_hash = hash_new ();
   1574   pru_reg_hash = hash_new ();
   1575 
   1576   for (i = 0; i < NUMOPCODES; ++i)
   1577     {
   1578       inserted
   1579 	= hash_insert (pru_opcode_hash, pru_opcodes[i].name,
   1580 		       (PTR) & pru_opcodes[i]);
   1581       if (inserted != NULL)
   1582 	{
   1583 	  fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
   1584 		   pru_opcodes[i].name, inserted);
   1585 	  /* Probably a memory allocation problem?  Give up now.  */
   1586 	  as_fatal (_("Broken assembler.  No assembly attempted."));
   1587 	}
   1588     }
   1589 
   1590   for (i = 0; i < pru_num_regs; ++i)
   1591     {
   1592       inserted
   1593 	= hash_insert (pru_reg_hash, pru_regs[i].name,
   1594 		       (PTR) & pru_regs[i]);
   1595       if (inserted != NULL)
   1596 	{
   1597 	  fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
   1598 		   pru_regs[i].name, inserted);
   1599 	  /* Probably a memory allocation problem?  Give up now.  */
   1600 	  as_fatal (_("Broken assembler.  No assembly attempted."));
   1601 	}
   1602 
   1603     }
   1604 
   1605   linkrelax = pru_opt.link_relax;
   1606   /* Initialize the alignment data.  */
   1607   pru_current_align_seg = now_seg;
   1608   pru_last_label = NULL;
   1609   pru_current_align = 0;
   1610 }
   1611 
   1612 
   1613 /* Assembles a single line of PRU assembly language.  */
   1614 void
   1615 md_assemble (char *op_str)
   1616 {
   1617   char *argstr;
   1618   char *op_strdup = NULL;
   1619   pru_insn_infoS thisinsn;
   1620   pru_insn_infoS *insn = &thisinsn;
   1621 
   1622   /* Make sure we are aligned on a 4-byte boundary.  */
   1623   if (pru_current_align < 2)
   1624     pru_align (2, NULL, pru_last_label);
   1625   else if (pru_current_align > 2)
   1626     pru_current_align = 2;
   1627   pru_last_label = NULL;
   1628 
   1629   /* We don't want to clobber to op_str
   1630      because we want to be able to use it in messages.  */
   1631   op_strdup = strdup (op_str);
   1632   insn->insn_tokens[0] = strtok (op_strdup, " ");
   1633   argstr = strtok (NULL, "");
   1634 
   1635   /* Assemble the opcode.  */
   1636   insn->insn_pru_opcode = pru_opcode_lookup (insn->insn_tokens[0]);
   1637   insn->insn_reloc = NULL;
   1638 
   1639   if (insn->insn_pru_opcode != NULL)
   1640     {
   1641       const char *argsfmt = insn->insn_pru_opcode->args;
   1642       const char **argtk = &insn->insn_tokens[1];
   1643       const char *argp;
   1644 
   1645       /* Set the opcode for the instruction.  */
   1646       insn->insn_code = insn->insn_pru_opcode->match;
   1647 
   1648       if (pru_mode == PRU_MODE_TEST)
   1649 	{
   1650 	  /* Add the "expected" instruction parameter used for validation.  */
   1651 	  argsfmt = malloc (strlen (argsfmt) + 3);
   1652 	  sprintf ((char *)argsfmt, "%s,E", insn->insn_pru_opcode->args);
   1653 	}
   1654       pru_parse_args (insn, argstr, argsfmt,
   1655 		      (char **) &insn->insn_tokens[1]);
   1656 
   1657       for (argp = argsfmt; !had_errors () && *argp && *argtk; ++argp)
   1658 	{
   1659 	  gas_assert (argtk <= &insn->insn_tokens[PRU_MAX_INSN_TOKENS]);
   1660 
   1661 	  switch (*argp)
   1662 	    {
   1663 	    case ',':
   1664 	      continue;
   1665 
   1666 	    case 'd':
   1667 	      pru_assemble_arg_d (insn, *argtk++);
   1668 	      continue;
   1669 	    case 'D':
   1670 	      pru_assemble_arg_D (insn, *argtk++);
   1671 	      continue;
   1672 	    case 'R':
   1673 	      pru_assemble_arg_R (insn, *argtk++);
   1674 	      continue;
   1675 	    case 's':
   1676 	      pru_assemble_arg_s (insn, *argtk++);
   1677 	      continue;
   1678 	    case 'S':
   1679 	      pru_assemble_arg_S (insn, *argtk++);
   1680 	      continue;
   1681 	    case 'b':
   1682 	      pru_assemble_arg_b (insn, *argtk++);
   1683 	      continue;
   1684 	    case 'B':
   1685 	      pru_assemble_arg_B (insn, *argtk++);
   1686 	      continue;
   1687 	    case 'i':
   1688 	      pru_assemble_arg_i (insn, *argtk++);
   1689 	      continue;
   1690 	    case 'j':
   1691 	      pru_assemble_arg_j (insn, *argtk++);
   1692 	      continue;
   1693 	    case 'W':
   1694 	      pru_assemble_arg_W (insn, *argtk++);
   1695 	      continue;
   1696 	    case 'o':
   1697 	      pru_assemble_arg_o (insn, *argtk++);
   1698 	      continue;
   1699 	    case 'O':
   1700 	      pru_assemble_arg_O (insn, *argtk++);
   1701 	      continue;
   1702 	    case 'l':
   1703 	      pru_assemble_arg_l (insn, *argtk++);
   1704 	      continue;
   1705 	    case 'n':
   1706 	      pru_assemble_arg_n (insn, *argtk++);
   1707 	      continue;
   1708 	    case 'c':
   1709 	      pru_assemble_arg_c (insn, *argtk++);
   1710 	      continue;
   1711 	    case 'w':
   1712 	      pru_assemble_arg_w (insn, *argtk++);
   1713 	      continue;
   1714 	    case 'x':
   1715 	      pru_assemble_arg_x (insn, *argtk++);
   1716 	      continue;
   1717 
   1718 	    case 'E':
   1719 	      pru_check_assembly (insn->insn_code, *argtk++);
   1720 	      continue;
   1721 
   1722 	    default:
   1723 	      BAD_CASE (*argp);
   1724 	    }
   1725 	}
   1726 
   1727       if (*argp && !had_errors ())
   1728 	as_bad (_("missing argument"));
   1729 
   1730       if (!had_errors ())
   1731 	{
   1732 	  if (insn->insn_pru_opcode->pinfo & PRU_INSN_LDI32)
   1733 	    {
   1734 	      output_insn_ldi32 (insn);
   1735 	    }
   1736 	  else
   1737 	    {
   1738 	      output_insn (insn);
   1739 	    }
   1740 	}
   1741 
   1742       if (pru_mode == PRU_MODE_TEST)
   1743 	free ((char *)argsfmt);
   1744     }
   1745   else
   1746     /* Unrecognised instruction - error.  */
   1747     as_bad (_("unrecognised instruction %s"), insn->insn_tokens[0]);
   1748 
   1749   /* Don't leak memory.  */
   1750   pru_insn_reloc_destroy (insn->insn_reloc);
   1751   free (op_strdup);
   1752 }
   1753 
   1754 /* Round up section size.  */
   1755 valueT
   1756 md_section_align (asection *seg, valueT addr)
   1757 {
   1758   int align = bfd_section_alignment (seg);
   1759   return ((addr + (1 << align) - 1) & (-((valueT) 1 << align)));
   1760 }
   1761 
   1762 /* Implement tc_fix_adjustable.  */
   1763 int
   1764 pru_fix_adjustable (fixS *fixp)
   1765 {
   1766   if (fixp->fx_addsy == NULL)
   1767     return 1;
   1768 
   1769   /* Prevent all adjustments to global symbols.  */
   1770   if (OUTPUT_FLAVOR == bfd_target_elf_flavour
   1771       && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
   1772     return 0;
   1773 
   1774   if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
   1775       || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
   1776     return 0;
   1777 
   1778   /* Preserve relocations against symbols with function type.  */
   1779   if (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION)
   1780     return 0;
   1781 
   1782   return 1;
   1783 }
   1784 
   1785 /* The function tc_gen_reloc creates a relocation structure for the
   1786    fixup fixp, and returns a pointer to it.  This structure is passed
   1787    to bfd_install_relocation so that it can be written to the object
   1788    file for linking.  */
   1789 arelent *
   1790 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
   1791 {
   1792   arelent *reloc = XNEW (arelent);
   1793   reloc->sym_ptr_ptr = XNEW (asymbol *);
   1794   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
   1795 
   1796   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
   1797   reloc->addend = fixp->fx_offset;  /* fixp->fx_addnumber; */
   1798 
   1799   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
   1800   if (reloc->howto == NULL)
   1801     {
   1802       as_bad_where (fixp->fx_file, fixp->fx_line,
   1803 		    _("can't represent relocation type %s"),
   1804 		    bfd_get_reloc_code_name (fixp->fx_r_type));
   1805 
   1806       /* Set howto to a garbage value so that we can keep going.  */
   1807       reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
   1808       gas_assert (reloc->howto != NULL);
   1809     }
   1810   return reloc;
   1811 }
   1812 
   1813 long
   1814 md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
   1815 {
   1816   return fixP->fx_where + fixP->fx_frag->fr_address;
   1817 }
   1818 
   1819 /* Called just before the assembler exits.  */
   1820 void
   1821 md_end (void)
   1822 {
   1823   hash_die (pru_opcode_hash);
   1824   hash_die (pru_reg_hash);
   1825 }
   1826 
   1827 symbolS *
   1828 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
   1829 {
   1830   return NULL;
   1831 }
   1832 
   1833 /* Implement tc_frob_label.  */
   1834 void
   1835 pru_frob_label (symbolS *lab)
   1836 {
   1837   /* Emit dwarf information.  */
   1838   dwarf2_emit_label (lab);
   1839 
   1840   /* Update the label's address with the current output pointer.  */
   1841   symbol_set_frag (lab, frag_now);
   1842   S_SET_VALUE (lab, (valueT) frag_now_fix ());
   1843 
   1844   /* Record this label for future adjustment after we find out what
   1845      kind of data it references, and the required alignment therewith.  */
   1846   pru_last_label = lab;
   1847 
   1848   if (pru_opt.warn_regname_label && pru_reg_lookup (S_GET_NAME (lab)))
   1849     as_warn (_("Label \"%s\" matches a CPU register name"), S_GET_NAME (lab));
   1850 }
   1851 
   1852 static inline char *
   1853 skip_space (char *s)
   1854 {
   1855   while (*s == ' ' || *s == '\t')
   1856     ++s;
   1857   return s;
   1858 }
   1859 
   1860 /* Parse special CONS expression: pmem (expression).  Idea from AVR.
   1861 
   1862    Used to catch and mark code (program memory) in constant expression
   1863    relocations.  Return non-zero for program memory.  */
   1864 
   1865 int
   1866 pru_parse_cons_expression (expressionS *exp, int nbytes)
   1867 {
   1868   int is_pmem = FALSE;
   1869   char *tmp;
   1870 
   1871   tmp = input_line_pointer = skip_space (input_line_pointer);
   1872 
   1873   if (nbytes == 4 || nbytes == 2)
   1874     {
   1875       const char *pmem_str = "%pmem";
   1876       int len = strlen (pmem_str);
   1877 
   1878       if (strncasecmp (input_line_pointer, pmem_str, len) == 0)
   1879 	{
   1880 	  input_line_pointer = skip_space (input_line_pointer + len);
   1881 
   1882 	  if (*input_line_pointer == '(')
   1883 	    {
   1884 	      input_line_pointer = skip_space (input_line_pointer + 1);
   1885 	      is_pmem = TRUE;
   1886 	      expression (exp);
   1887 
   1888 	      if (*input_line_pointer == ')')
   1889 		++input_line_pointer;
   1890 	      else
   1891 		{
   1892 		  as_bad (_("`)' required"));
   1893 		  is_pmem = FALSE;
   1894 		}
   1895 
   1896 	      return is_pmem;
   1897 	    }
   1898 
   1899 	  input_line_pointer = tmp;
   1900 	}
   1901     }
   1902 
   1903   expression (exp);
   1904 
   1905   return is_pmem;
   1906 }
   1907 
   1908 /* Implement TC_CONS_FIX_NEW.  */
   1909 void
   1910 pru_cons_fix_new (fragS *frag, int where, unsigned int nbytes,
   1911 		    expressionS *exp, const int is_pmem)
   1912 {
   1913   bfd_reloc_code_real_type r;
   1914 
   1915   switch (nbytes | (!!is_pmem << 8))
   1916     {
   1917     case 1 | (0 << 8): r = BFD_RELOC_8; break;
   1918     case 2 | (0 << 8): r = BFD_RELOC_16; break;
   1919     case 4 | (0 << 8): r = BFD_RELOC_32; break;
   1920     case 8 | (0 << 8): r = BFD_RELOC_64; break;
   1921     case 2 | (1 << 8): r = BFD_RELOC_PRU_16_PMEM; break;
   1922     case 4 | (1 << 8): r = BFD_RELOC_PRU_32_PMEM; break;
   1923     default:
   1924       as_bad (_("illegal %s relocation size: %d"),
   1925 	      is_pmem ? "text" : "data", nbytes);
   1926       return;
   1927     }
   1928 
   1929   fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
   1930 }
   1931 
   1932 /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
   1933    register number.  Return the starting HW byte-register number.  */
   1934 
   1935 int
   1936 pru_regname_to_dw2regnum (char *regname)
   1937 {
   1938   static const unsigned int regstart[RSEL_NUM_ITEMS] =
   1939     {
   1940      [RSEL_7_0]	  = 0,
   1941      [RSEL_15_8]  = 1,
   1942      [RSEL_23_16] = 2,
   1943      [RSEL_31_24] = 3,
   1944      [RSEL_15_0]  = 0,
   1945      [RSEL_23_8]  = 1,
   1946      [RSEL_31_16] = 2,
   1947      [RSEL_31_0]  = 0,
   1948     };
   1949 
   1950   struct pru_reg *r = pru_reg_lookup (regname);
   1951 
   1952   if (r == NULL || r->regsel >= RSEL_NUM_ITEMS)
   1953     return -1;
   1954   return r->index * 4 + regstart[r->regsel];
   1955 }
   1956 
   1957 /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
   1958    unwind information for this procedure.  */
   1959 void
   1960 pru_frame_initial_instructions (void)
   1961 {
   1962   const unsigned fp_regno = 4 * 4;
   1963   cfi_add_CFA_def_cfa (fp_regno, 0);
   1964 }
   1965 
   1966 bfd_boolean
   1967 pru_allow_local_subtract (expressionS * left,
   1968 			     expressionS * right,
   1969 			     segT section)
   1970 {
   1971   /* If we are not in relaxation mode, subtraction is OK.  */
   1972   if (!linkrelax)
   1973     return TRUE;
   1974 
   1975   /* If the symbols are not in a code section then they are OK.  */
   1976   if ((section->flags & SEC_CODE) == 0)
   1977     return TRUE;
   1978 
   1979   if (left->X_add_symbol == right->X_add_symbol)
   1980     return TRUE;
   1981 
   1982   /* We have to assume that there may be instructions between the
   1983      two symbols and that relaxation may increase the distance between
   1984      them.  */
   1985   return FALSE;
   1986 }
   1987