Home | History | Annotate | Line # | Download | only in config
tc-spu.c revision 1.1
      1 /* spu.c -- Assembler for the IBM Synergistic Processing Unit (SPU)
      2 
      3    Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
      4 
      5    This file is part of GAS, the GNU Assembler.
      6 
      7    GAS is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3, or (at your option)
     10    any later version.
     11 
     12    GAS is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with GAS; see the file COPYING.  If not, write to the Free
     19    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
     20    02110-1301, USA.  */
     21 
     22 #include "as.h"
     23 #include "safe-ctype.h"
     24 #include "subsegs.h"
     25 #include "dwarf2dbg.h"
     26 
     27 const struct spu_opcode spu_opcodes[] = {
     28 #define APUOP(TAG,MACFORMAT,OPCODE,MNEMONIC,ASMFORMAT,DEP,PIPE) \
     29 	{ MACFORMAT, (OPCODE) << (32-11), MNEMONIC, ASMFORMAT },
     30 #define APUOPFB(TAG,MACFORMAT,OPCODE,FB,MNEMONIC,ASMFORMAT,DEP,PIPE) \
     31 	{ MACFORMAT, ((OPCODE) << (32-11)) | ((FB) << (32-18)), MNEMONIC, ASMFORMAT },
     32 #include "opcode/spu-insns.h"
     33 #undef APUOP
     34 #undef APUOPFB
     35 };
     36 
     37 static const int spu_num_opcodes =
     38   sizeof (spu_opcodes) / sizeof (spu_opcodes[0]);
     39 
     40 #define MAX_RELOCS 2
     41 
     42 struct spu_insn
     43 {
     44   unsigned int opcode;
     45   expressionS exp[MAX_RELOCS];
     46   int reloc_arg[MAX_RELOCS];
     47   bfd_reloc_code_real_type reloc[MAX_RELOCS];
     48   enum spu_insns tag;
     49 };
     50 
     51 static const char *get_imm (const char *param, struct spu_insn *insn, int arg);
     52 static const char *get_reg (const char *param, struct spu_insn *insn, int arg,
     53 			    int accept_expr);
     54 static int calcop (struct spu_opcode *format, const char *param,
     55 		   struct spu_insn *insn);
     56 static void spu_cons (int);
     57 
     58 extern char *myname;
     59 static struct hash_control *op_hash = NULL;
     60 
     61 /* These bits should be turned off in the first address of every segment */
     62 int md_seg_align = 7;
     63 
     64 /* These chars start a comment anywhere in a source file (except inside
     65    another comment */
     66 const char comment_chars[] = "#";
     67 
     68 /* These chars only start a comment at the beginning of a line.  */
     69 const char line_comment_chars[] = "#";
     70 
     71 /* gods own line continuation char */
     72 const char line_separator_chars[] = ";";
     73 
     74 /* Chars that can be used to separate mant from exp in floating point nums */
     75 const char EXP_CHARS[] = "eE";
     76 
     77 /* Chars that mean this number is a floating point constant */
     78 /* as in 0f123.456 */
     79 /* or    0H1.234E-12 (see exp chars above) */
     80 const char FLT_CHARS[] = "dDfF";
     81 
     82 const pseudo_typeS md_pseudo_table[] =
     83 {
     84   {"align", s_align_ptwo, 4},
     85   {"bss", s_lcomm_bytes, 1},
     86   {"def", s_set, 0},
     87   {"dfloat", float_cons, 'd'},
     88   {"ffloat", float_cons, 'f'},
     89   {"global", s_globl, 0},
     90   {"half", cons, 2},
     91   {"int", spu_cons, 4},
     92   {"long", spu_cons, 4},
     93   {"quad", spu_cons, 8},
     94   {"string", stringer, 8 + 1},
     95   {"word", spu_cons, 4},
     96   /* Force set to be treated as an instruction.  */
     97   {"set", NULL, 0},
     98   {".set", s_set, 0},
     99   /* Likewise for eqv.  */
    100   {"eqv", NULL, 0},
    101   {".eqv", s_set, -1},
    102   {"file", (void (*) (int)) dwarf2_directive_file, 0 },
    103   {"loc", dwarf2_directive_loc, 0},
    104   {0,0,0}
    105 };
    106 
    107 void
    108 md_begin (void)
    109 {
    110   const char *retval = NULL;
    111   int i;
    112 
    113   /* initialize hash table */
    114 
    115   op_hash = hash_new ();
    116 
    117   /* loop until you see the end of the list */
    118 
    119   for (i = 0; i < spu_num_opcodes; i++)
    120     {
    121       /* hash each mnemonic and record its position */
    122 
    123       retval = hash_insert (op_hash, spu_opcodes[i].mnemonic,
    124 			    (void *) &spu_opcodes[i]);
    125 
    126       if (retval != NULL && strcmp (retval, "exists") != 0)
    127 	as_fatal (_("Can't hash instruction '%s':%s"),
    128 		  spu_opcodes[i].mnemonic, retval);
    129     }
    130 }
    131 
    132 const char *md_shortopts = "";
    134 struct option md_longopts[] = {
    135 #define OPTION_APUASM (OPTION_MD_BASE)
    136   {"apuasm", no_argument, NULL, OPTION_APUASM},
    137 #define OPTION_DD2 (OPTION_MD_BASE+1)
    138   {"mdd2.0", no_argument, NULL, OPTION_DD2},
    139 #define OPTION_DD1 (OPTION_MD_BASE+2)
    140   {"mdd1.0", no_argument, NULL, OPTION_DD1},
    141 #define OPTION_DD3 (OPTION_MD_BASE+3)
    142   {"mdd3.0", no_argument, NULL, OPTION_DD3},
    143   { NULL, no_argument, NULL, 0 }
    144 };
    145 size_t md_longopts_size = sizeof (md_longopts);
    146 
    147 /* When set (by -apuasm) our assembler emulates the behaviour of apuasm.
    148  * e.g. don't add bias to float conversion and don't right shift
    149  * immediate values. */
    150 static int emulate_apuasm;
    151 
    152 /* Use the dd2.0 instructions set.  The only differences are some new
    153  * register names and the orx insn */
    154 static int use_dd2 = 1;
    155 
    156 int
    157 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
    158 {
    159   switch (c)
    160     {
    161     case OPTION_APUASM:
    162       emulate_apuasm = 1;
    163       break;
    164     case OPTION_DD3:
    165       use_dd2 = 1;
    166       break;
    167     case OPTION_DD2:
    168       use_dd2 = 1;
    169       break;
    170     case OPTION_DD1:
    171       use_dd2 = 0;
    172       break;
    173     default:
    174       return 0;
    175     }
    176   return 1;
    177 }
    178 
    179 void
    180 md_show_usage (FILE *stream)
    181 {
    182   fputs (_("\
    183 SPU options:\n\
    184   --apuasm		  emulate behaviour of apuasm\n"),
    185 	stream);
    186 }
    187 
    188 
    190 struct arg_encode {
    191   int size;
    192   int pos;
    193   int rshift;
    194   int lo, hi;
    195   int wlo, whi;
    196   bfd_reloc_code_real_type reloc;
    197 };
    198 
    199 static struct arg_encode arg_encode[A_MAX] = {
    200   {  7,  0, 0,       0,    127,    0,   -1,  0 }, /* A_T */
    201   {  7,  7, 0,       0,    127,    0,   -1,  0 }, /* A_A */
    202   {  7, 14, 0,       0,    127,    0,   -1,  0 }, /* A_B */
    203   {  7, 21, 0,       0,    127,    0,   -1,  0 }, /* A_C */
    204   {  7,  7, 0,       0,    127,    0,   -1,  0 }, /* A_S */
    205   {  7,  7, 0,       0,    127,    0,   -1,  0 }, /* A_H */
    206   {  0,  0, 0,       0,     -1,    0,   -1,  0 }, /* A_P */
    207   {  7, 14, 0,       0,     -1,    0,   -1,  BFD_RELOC_SPU_IMM7 }, /* A_S3 */
    208   {  7, 14, 0,     -32,     31,  -31,    0,  BFD_RELOC_SPU_IMM7 }, /* A_S6 */
    209   {  7, 14, 0,       0,     -1,    0,   -1,  BFD_RELOC_SPU_IMM7 }, /* A_S7N */
    210   {  7, 14, 0,     -64,     63,  -63,    0,  BFD_RELOC_SPU_IMM7 }, /* A_S7 */
    211   {  8, 14, 0,       0,    127,    0,   -1,  BFD_RELOC_SPU_IMM8 }, /* A_U7A */
    212   {  8, 14, 0,       0,    127,    0,   -1,  BFD_RELOC_SPU_IMM8 }, /* A_U7B */
    213   { 10, 14, 0,    -512,    511, -128,  255,  BFD_RELOC_SPU_IMM10 }, /* A_S10B */
    214   { 10, 14, 0,    -512,    511,    0,   -1,  BFD_RELOC_SPU_IMM10 }, /* A_S10 */
    215   {  2, 23, 9,   -1024,   1023,    0,   -1,  BFD_RELOC_SPU_PCREL9a }, /* A_S11 */
    216   {  2, 14, 9,   -1024,   1023,    0,   -1,  BFD_RELOC_SPU_PCREL9b }, /* A_S11I */
    217   { 10, 14, 4,   -8192,   8191,    0,   -1,  BFD_RELOC_SPU_IMM10W }, /* A_S14 */
    218   { 16,  7, 0,  -32768,  32767,    0,   -1,  BFD_RELOC_SPU_IMM16 }, /* A_S16 */
    219   { 16,  7, 2, -131072, 262143,    0,   -1,  BFD_RELOC_SPU_IMM16W }, /* A_S18 */
    220   { 16,  7, 2, -262144, 262143,    0,   -1,  BFD_RELOC_SPU_PCREL16 }, /* A_R18 */
    221   {  7, 14, 0,       0,     -1,    0,   -1,  BFD_RELOC_SPU_IMM7 }, /* A_U3 */
    222   {  7, 14, 0,       0,    127,    0,   31,  BFD_RELOC_SPU_IMM7 }, /* A_U5 */
    223   {  7, 14, 0,       0,    127,    0,   63,  BFD_RELOC_SPU_IMM7 }, /* A_U6 */
    224   {  7, 14, 0,       0,     -1,    0,   -1,  BFD_RELOC_SPU_IMM7 }, /* A_U7 */
    225   { 14,  0, 0,       0,  16383,    0,   -1,  0 }, /* A_U14 */
    226   { 16,  7, 0,  -32768,  65535,    0,   -1,  BFD_RELOC_SPU_IMM16 }, /* A_X16 */
    227   { 18,  7, 0,       0, 262143,    0,   -1,  BFD_RELOC_SPU_IMM18 }, /* A_U18 */
    228 };
    229 
    230 /* Some flags for handling errors.  This is very hackish and added after
    231  * the fact. */
    232 static int syntax_error_arg;
    233 static const char *syntax_error_param;
    234 static int syntax_reg;
    235 
    236 static char *
    237 insn_fmt_string (struct spu_opcode *format)
    238 {
    239   static char buf[64];
    240   int len = 0;
    241   int i;
    242 
    243   len += sprintf (&buf[len], "%s\t", format->mnemonic);
    244   for (i = 1; i <= format->arg[0]; i++)
    245     {
    246       int arg = format->arg[i];
    247       char *exp;
    248       if (i > 1 && arg != A_P && format->arg[i-1] != A_P)
    249 	buf[len++] =  ',';
    250       if (arg == A_P)
    251 	exp = "(";
    252       else if (arg < A_P)
    253 	exp = i == syntax_error_arg ? "REG" : "reg";
    254       else
    255 	exp = i == syntax_error_arg ? "IMM" : "imm";
    256       len += sprintf (&buf[len], "%s", exp);
    257       if (i > 1 && format->arg[i-1] == A_P)
    258 	buf[len++] =  ')';
    259     }
    260   buf[len] = 0;
    261   return buf;
    262 }
    263 
    264 void
    265 md_assemble (char *op)
    266 {
    267   char *param, *thisfrag;
    268   char c;
    269   struct spu_opcode *format;
    270   struct spu_insn insn;
    271   int i;
    272 
    273   assert (op);
    274 
    275   /* skip over instruction to find parameters */
    276 
    277   for (param = op; *param != 0 && !ISSPACE (*param); param++)
    278     ;
    279   c = *param;
    280   *param = 0;
    281 
    282   if (c != 0 && c != '\n')
    283     param++;
    284 
    285   /* try to find the instruction in the hash table */
    286 
    287   if ((format = (struct spu_opcode *) hash_find (op_hash, op)) == NULL)
    288     {
    289       as_bad (_("Invalid mnemonic '%s'"), op);
    290       return;
    291     }
    292 
    293   if (!use_dd2 && strcmp (format->mnemonic, "orx") == 0)
    294     {
    295       as_bad (_("'%s' is only available in DD2.0 or higher."), op);
    296       return;
    297     }
    298 
    299   while (1)
    300     {
    301       /* try parsing this instruction into insn */
    302       for (i = 0; i < MAX_RELOCS; i++)
    303 	{
    304 	  insn.exp[i].X_add_symbol = 0;
    305 	  insn.exp[i].X_op_symbol = 0;
    306 	  insn.exp[i].X_add_number = 0;
    307 	  insn.exp[i].X_op = O_illegal;
    308 	  insn.reloc_arg[i] = -1;
    309 	  insn.reloc[i] = BFD_RELOC_NONE;
    310 	}
    311       insn.opcode = format->opcode;
    312       insn.tag = (enum spu_insns) (format - spu_opcodes);
    313 
    314       syntax_error_arg = 0;
    315       syntax_error_param = 0;
    316       syntax_reg = 0;
    317       if (calcop (format, param, &insn))
    318 	break;
    319 
    320       /* if it doesn't parse try the next instruction */
    321       if (!strcmp (format[0].mnemonic, format[1].mnemonic))
    322 	format++;
    323       else
    324 	{
    325 	  int parg = format[0].arg[syntax_error_arg-1];
    326 
    327 	  as_fatal (_("Error in argument %d.  Expecting:  \"%s\""),
    328 		    syntax_error_arg - (parg == A_P),
    329 		    insn_fmt_string (format));
    330 	  return;
    331 	}
    332     }
    333 
    334   if ((syntax_reg & 4)
    335       && ! (insn.tag == M_RDCH
    336 	    || insn.tag == M_RCHCNT
    337 	    || insn.tag == M_WRCH))
    338     as_warn (_("Mixing register syntax, with and without '$'."));
    339   if (syntax_error_param)
    340     {
    341       const char *d = syntax_error_param;
    342       while (*d != '$')
    343 	d--;
    344       as_warn (_("Treating '%-*s' as a symbol."), (int)(syntax_error_param - d), d);
    345     }
    346 
    347   /* grow the current frag and plop in the opcode */
    348 
    349   thisfrag = frag_more (4);
    350   md_number_to_chars (thisfrag, insn.opcode, 4);
    351 
    352   /* if this instruction requires labels mark it for later */
    353 
    354   for (i = 0; i < MAX_RELOCS; i++)
    355     if (insn.reloc_arg[i] >= 0)
    356       {
    357         fixS *fixP;
    358         bfd_reloc_code_real_type reloc = insn.reloc[i];
    359 	int pcrel = 0;
    360 
    361 	if (reloc == BFD_RELOC_SPU_PCREL9a
    362 	    || reloc == BFD_RELOC_SPU_PCREL9b
    363 	    || reloc == BFD_RELOC_SPU_PCREL16)
    364 	  pcrel = 1;
    365 	fixP = fix_new_exp (frag_now,
    366 			    thisfrag - frag_now->fr_literal,
    367 			    4,
    368 			    &insn.exp[i],
    369 			    pcrel,
    370 			    reloc);
    371 	fixP->tc_fix_data.arg_format = insn.reloc_arg[i];
    372 	fixP->tc_fix_data.insn_tag = insn.tag;
    373       }
    374   dwarf2_emit_insn (4);
    375 }
    376 
    377 static int
    378 calcop (struct spu_opcode *format, const char *param, struct spu_insn *insn)
    379 {
    380   int i;
    381   int paren = 0;
    382   int arg;
    383 
    384   for (i = 1; i <= format->arg[0]; i++)
    385     {
    386       arg = format->arg[i];
    387       syntax_error_arg = i;
    388 
    389       while (ISSPACE (*param))
    390         param++;
    391       if (*param == 0 || *param == ',')
    392 	return 0;
    393       if (arg < A_P)
    394         param = get_reg (param, insn, arg, 1);
    395       else if (arg > A_P)
    396         param = get_imm (param, insn, arg);
    397       else if (arg == A_P)
    398 	{
    399 	  paren++;
    400 	  if ('(' != *param++)
    401 	    return 0;
    402 	}
    403 
    404       if (!param)
    405 	return 0;
    406 
    407       while (ISSPACE (*param))
    408         param++;
    409 
    410       if (arg != A_P && paren)
    411 	{
    412 	  paren--;
    413 	  if (')' != *param++)
    414 	    return 0;
    415 	}
    416       else if (i < format->arg[0]
    417 	       && format->arg[i] != A_P
    418 	       && format->arg[i+1] != A_P)
    419 	{
    420 	  if (',' != *param++)
    421 	    {
    422 	      syntax_error_arg++;
    423 	      return 0;
    424 	    }
    425 	}
    426     }
    427   while (ISSPACE (*param))
    428     param++;
    429   return !paren && (*param == 0 || *param == '\n');
    430 }
    431 
    432 struct reg_name {
    433     unsigned int regno;
    434     unsigned int length;
    435     char name[32];
    436 };
    437 
    438 #define REG_NAME(NO,NM) { NO, sizeof (NM) - 1, NM }
    439 
    440 static struct reg_name reg_name[] = {
    441   REG_NAME (0, "lr"),  /* link register */
    442   REG_NAME (1, "sp"),  /* stack pointer */
    443   REG_NAME (0, "rp"),  /* link register */
    444   REG_NAME (127, "fp"),  /* frame pointer */
    445 };
    446 
    447 static struct reg_name sp_reg_name[] = {
    448 };
    449 
    450 static struct reg_name ch_reg_name[] = {
    451   REG_NAME (  0, "SPU_RdEventStat"),
    452   REG_NAME (  1, "SPU_WrEventMask"),
    453   REG_NAME (  2, "SPU_WrEventAck"),
    454   REG_NAME (  3, "SPU_RdSigNotify1"),
    455   REG_NAME (  4, "SPU_RdSigNotify2"),
    456   REG_NAME (  7, "SPU_WrDec"),
    457   REG_NAME (  8, "SPU_RdDec"),
    458   REG_NAME ( 11, "SPU_RdEventMask"), /* DD2.0 only */
    459   REG_NAME ( 13, "SPU_RdMachStat"),
    460   REG_NAME ( 14, "SPU_WrSRR0"),
    461   REG_NAME ( 15, "SPU_RdSRR0"),
    462   REG_NAME ( 28, "SPU_WrOutMbox"),
    463   REG_NAME ( 29, "SPU_RdInMbox"),
    464   REG_NAME ( 30, "SPU_WrOutIntrMbox"),
    465   REG_NAME (  9, "MFC_WrMSSyncReq"),
    466   REG_NAME ( 12, "MFC_RdTagMask"),   /* DD2.0 only */
    467   REG_NAME ( 16, "MFC_LSA"),
    468   REG_NAME ( 17, "MFC_EAH"),
    469   REG_NAME ( 18, "MFC_EAL"),
    470   REG_NAME ( 19, "MFC_Size"),
    471   REG_NAME ( 20, "MFC_TagID"),
    472   REG_NAME ( 21, "MFC_Cmd"),
    473   REG_NAME ( 22, "MFC_WrTagMask"),
    474   REG_NAME ( 23, "MFC_WrTagUpdate"),
    475   REG_NAME ( 24, "MFC_RdTagStat"),
    476   REG_NAME ( 25, "MFC_RdListStallStat"),
    477   REG_NAME ( 26, "MFC_WrListStallAck"),
    478   REG_NAME ( 27, "MFC_RdAtomicStat"),
    479 };
    480 #undef REG_NAME
    481 
    482 static const char *
    483 get_reg (const char *param, struct spu_insn *insn, int arg, int accept_expr)
    484 {
    485   unsigned regno;
    486   int saw_prefix = 0;
    487 
    488   if (*param == '$')
    489     {
    490       saw_prefix = 1;
    491       param++;
    492     }
    493 
    494   if (arg == A_H) /* Channel */
    495     {
    496       if ((param[0] == 'c' || param[0] == 'C')
    497 	  && (param[1] == 'h' || param[1] == 'H')
    498 	  && ISDIGIT (param[2]))
    499         param += 2;
    500     }
    501   else if (arg == A_S) /* Special purpose register */
    502     {
    503       if ((param[0] == 's' || param[0] == 'S')
    504 	  && (param[1] == 'p' || param[1] == 'P')
    505 	  && ISDIGIT (param[2]))
    506         param += 2;
    507     }
    508 
    509   if (ISDIGIT (*param))
    510     {
    511       regno = 0;
    512       while (ISDIGIT (*param))
    513 	regno = regno * 10 + *param++ - '0';
    514     }
    515   else
    516     {
    517       struct reg_name *rn;
    518       unsigned int i, n, l = 0;
    519 
    520       if (arg == A_H) /* Channel */
    521 	{
    522 	  rn = ch_reg_name;
    523 	  n = sizeof (ch_reg_name) / sizeof (*ch_reg_name);
    524 	}
    525       else if (arg == A_S) /* Special purpose register */
    526 	{
    527 	  rn = sp_reg_name;
    528 	  n = sizeof (sp_reg_name) / sizeof (*sp_reg_name);
    529 	}
    530       else
    531 	{
    532 	  rn = reg_name;
    533 	  n = sizeof (reg_name) / sizeof (*reg_name);
    534 	}
    535       regno = 128;
    536       for (i = 0; i < n; i++)
    537 	if (rn[i].length > l
    538 	    && 0 == strncasecmp (param, rn[i].name, rn[i].length))
    539           {
    540 	    l = rn[i].length;
    541             regno = rn[i].regno;
    542           }
    543       param += l;
    544     }
    545 
    546   if (!use_dd2
    547       && arg == A_H)
    548     {
    549       if (regno == 11)
    550 	as_bad (_("'SPU_RdEventMask' (channel 11) is only available in DD2.0 or higher."));
    551       else if (regno == 12)
    552 	as_bad (_("'MFC_RdTagMask' (channel 12) is only available in DD2.0 or higher."));
    553     }
    554 
    555   if (regno < 128)
    556     {
    557       insn->opcode |= regno << arg_encode[arg].pos;
    558       if ((!saw_prefix && syntax_reg == 1)
    559 	  || (saw_prefix && syntax_reg == 2))
    560 	syntax_reg |= 4;
    561       syntax_reg |= saw_prefix ? 1 : 2;
    562       return param;
    563     }
    564 
    565   if (accept_expr)
    566     {
    567       char *save_ptr;
    568       expressionS ex;
    569       save_ptr = input_line_pointer;
    570       input_line_pointer = (char *)param;
    571       expression (&ex);
    572       param = input_line_pointer;
    573       input_line_pointer = save_ptr;
    574       if (ex.X_op == O_register || ex.X_op == O_constant)
    575 	{
    576 	  insn->opcode |= ex.X_add_number << arg_encode[arg].pos;
    577 	  return param;
    578 	}
    579     }
    580   return 0;
    581 }
    582 
    583 static const char *
    584 get_imm (const char *param, struct spu_insn *insn, int arg)
    585 {
    586   int val;
    587   char *save_ptr;
    588   int low = 0, high = 0;
    589   int reloc_i = insn->reloc_arg[0] >= 0 ? 1 : 0;
    590 
    591   if (strncasecmp (param, "%lo(", 4) == 0)
    592     {
    593       param += 3;
    594       low = 1;
    595       as_warn (_("Using old style, %%lo(expr), please change to PPC style, expr@l."));
    596     }
    597   else if (strncasecmp (param, "%hi(", 4) == 0)
    598     {
    599       param += 3;
    600       high = 1;
    601       as_warn (_("Using old style, %%hi(expr), please change to PPC style, expr@h."));
    602     }
    603   else if (strncasecmp (param, "%pic(", 5) == 0)
    604     {
    605       /* Currently we expect %pic(expr) == expr, so do nothing here.
    606 	 i.e. for code loaded at address 0 $toc will be 0.  */
    607       param += 4;
    608     }
    609 
    610   if (*param == '$')
    611     {
    612       /* Symbols can start with $, but if this symbol matches a register
    613 	 name, it's probably a mistake.  The only way to avoid this
    614 	 warning is to rename the symbol.  */
    615       struct spu_insn tmp_insn;
    616       const char *np = get_reg (param, &tmp_insn, arg, 0);
    617 
    618       if (np)
    619 	syntax_error_param = np;
    620     }
    621 
    622   save_ptr = input_line_pointer;
    623   input_line_pointer = (char *) param;
    624   expression (&insn->exp[reloc_i]);
    625   param = input_line_pointer;
    626   input_line_pointer = save_ptr;
    627 
    628   /* Similar to ppc_elf_suffix in tc-ppc.c.  We have so few cases to
    629      handle we do it inlined here. */
    630   if (param[0] == '@' && !ISALNUM (param[2]) && param[2] != '@')
    631     {
    632       if (param[1] == 'h' || param[1] == 'H')
    633 	{
    634 	  high = 1;
    635 	  param += 2;
    636 	}
    637       else if (param[1] == 'l' || param[1] == 'L')
    638 	{
    639 	  low = 1;
    640 	  param += 2;
    641 	}
    642     }
    643 
    644   if (insn->exp[reloc_i].X_op == O_constant)
    645     {
    646       val = insn->exp[reloc_i].X_add_number;
    647 
    648       if (emulate_apuasm)
    649 	{
    650 	  /* Convert the value to a format we expect. */
    651           val <<= arg_encode[arg].rshift;
    652 	  if (arg == A_U7A)
    653 	    val = 173 - val;
    654 	  else if (arg == A_U7B)
    655 	    val = 155 - val;
    656 	}
    657 
    658       if (high)
    659 	val = val >> 16;
    660       else if (low)
    661 	val = val & 0xffff;
    662 
    663       /* Warn about out of range expressions. */
    664       {
    665 	int hi = arg_encode[arg].hi;
    666 	int lo = arg_encode[arg].lo;
    667 	int whi = arg_encode[arg].whi;
    668 	int wlo = arg_encode[arg].wlo;
    669 
    670 	if (hi > lo && (val < lo || val > hi))
    671 	  as_fatal (_("Constant expression %d out of range, [%d, %d]."),
    672 		    val, lo, hi);
    673 	else if (whi > wlo && (val < wlo || val > whi))
    674 	  as_warn (_("Constant expression %d out of range, [%d, %d]."),
    675 		   val, wlo, whi);
    676       }
    677 
    678       if (arg == A_U7A)
    679         val = 173 - val;
    680       else if (arg == A_U7B)
    681         val = 155 - val;
    682 
    683       /* Branch hints have a split encoding.  Do the bottom part. */
    684       if (arg == A_S11 || arg == A_S11I)
    685 	insn->opcode |= ((val >> 2) & 0x7f);
    686 
    687       insn->opcode |= (((val >> arg_encode[arg].rshift)
    688 			& ((1 << arg_encode[arg].size) - 1))
    689 		       << arg_encode[arg].pos);
    690     }
    691   else
    692     {
    693       insn->reloc_arg[reloc_i] = arg;
    694       if (high)
    695 	insn->reloc[reloc_i] = BFD_RELOC_SPU_HI16;
    696       else if (low)
    697 	insn->reloc[reloc_i] = BFD_RELOC_SPU_LO16;
    698       else
    699 	insn->reloc[reloc_i] = arg_encode[arg].reloc;
    700     }
    701 
    702   return param;
    703 }
    704 
    705 char *
    706 md_atof (int type, char *litP, int *sizeP)
    707 {
    708   return ieee_md_atof (type, litP, sizeP, TRUE);
    709 }
    710 
    711 #ifndef WORKING_DOT_WORD
    712 int md_short_jump_size = 4;
    713 
    714 void
    715 md_create_short_jump (char *ptr,
    716 		      addressT from_addr ATTRIBUTE_UNUSED,
    717 		      addressT to_addr ATTRIBUTE_UNUSED,
    718 		      fragS *frag,
    719 		      symbolS *to_symbol)
    720 {
    721   ptr[0] = (char) 0xc0;
    722   ptr[1] = 0x00;
    723   ptr[2] = 0x00;
    724   ptr[3] = 0x00;
    725   fix_new (frag,
    726 	   ptr - frag->fr_literal,
    727 	   4,
    728 	   to_symbol,
    729 	   (offsetT) 0,
    730 	   0,
    731 	   BFD_RELOC_SPU_PCREL16);
    732 }
    733 
    734 int md_long_jump_size = 4;
    735 
    736 void
    737 md_create_long_jump (char *ptr,
    738 		     addressT from_addr ATTRIBUTE_UNUSED,
    739 		     addressT to_addr ATTRIBUTE_UNUSED,
    740 		     fragS *frag,
    741 		     symbolS *to_symbol)
    742 {
    743   ptr[0] = (char) 0xc0;
    744   ptr[1] = 0x00;
    745   ptr[2] = 0x00;
    746   ptr[3] = 0x00;
    747   fix_new (frag,
    748 	   ptr - frag->fr_literal,
    749 	   4,
    750 	   to_symbol,
    751 	   (offsetT) 0,
    752 	   0,
    753 	   BFD_RELOC_SPU_PCREL16);
    754 }
    755 #endif
    756 
    757 /* Support @ppu on symbols referenced in .int/.long/.word/.quad.  */
    758 static void
    759 spu_cons (int nbytes)
    760 {
    761   expressionS exp;
    762 
    763   if (is_it_end_of_statement ())
    764     {
    765       demand_empty_rest_of_line ();
    766       return;
    767     }
    768 
    769   do
    770     {
    771       deferred_expression (&exp);
    772       if ((exp.X_op == O_symbol
    773 	   || exp.X_op == O_constant)
    774 	  && strncasecmp (input_line_pointer, "@ppu", 4) == 0)
    775 	{
    776 	  char *p = frag_more (nbytes);
    777 	  enum bfd_reloc_code_real reloc;
    778 
    779 	  /* Check for identifier@suffix+constant.  */
    780 	  input_line_pointer += 4;
    781 	  if (*input_line_pointer == '-' || *input_line_pointer == '+')
    782 	    {
    783 	      expressionS new_exp;
    784 
    785 	      expression (&new_exp);
    786 	      if (new_exp.X_op == O_constant)
    787 		exp.X_add_number += new_exp.X_add_number;
    788 	    }
    789 
    790 	  reloc = nbytes == 4 ? BFD_RELOC_SPU_PPU32 : BFD_RELOC_SPU_PPU64;
    791 	  fix_new_exp (frag_now, p - frag_now->fr_literal, nbytes,
    792 		       &exp, 0, reloc);
    793 	}
    794       else
    795 	emit_expr (&exp, nbytes);
    796     }
    797   while (*input_line_pointer++ == ',');
    798 
    799   /* Put terminator back into stream.  */
    800   input_line_pointer--;
    801   demand_empty_rest_of_line ();
    802 }
    803 
    804 int
    805 md_estimate_size_before_relax (fragS *fragP ATTRIBUTE_UNUSED,
    806 			       segT segment_type ATTRIBUTE_UNUSED)
    807 {
    808   as_fatal (_("Relaxation should never occur"));
    809   return -1;
    810 }
    811 
    812 /* If while processing a fixup, a reloc really needs to be created,
    813    then it is done here.  */
    814 
    815 arelent *
    816 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
    817 {
    818   arelent *reloc;
    819   reloc = (arelent *) xmalloc (sizeof (arelent));
    820   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
    821   if (fixp->fx_addsy)
    822     *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
    823   else if (fixp->fx_subsy)
    824     *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
    825   else
    826     abort ();
    827   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
    828   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
    829   if (reloc->howto == (reloc_howto_type *) NULL)
    830     {
    831       as_bad_where (fixp->fx_file, fixp->fx_line,
    832 		    _("reloc %d not supported by object file format"),
    833 		    (int) fixp->fx_r_type);
    834       return NULL;
    835     }
    836   reloc->addend = fixp->fx_addnumber;
    837   return reloc;
    838 }
    839 
    840 /* Round up a section's size to the appropriate boundary.  */
    841 
    842 valueT
    843 md_section_align (segT seg, valueT size)
    844 {
    845   int align = bfd_get_section_alignment (stdoutput, seg);
    846   valueT mask = ((valueT) 1 << align) - 1;
    847 
    848   return (size + mask) & ~mask;
    849 }
    850 
    851 /* Where a PC relative offset is calculated from.  On the spu they
    852    are calculated from the beginning of the branch instruction.  */
    853 
    854 long
    855 md_pcrel_from (fixS *fixp)
    856 {
    857   return fixp->fx_frag->fr_address + fixp->fx_where;
    858 }
    859 
    860 /* Fill in rs_align_code fragments.  */
    861 
    862 void
    863 spu_handle_align (fragS *fragp)
    864 {
    865   static const unsigned char nop_pattern[8] = {
    866     0x40, 0x20, 0x00, 0x00, /* even nop */
    867     0x00, 0x20, 0x00, 0x00, /* odd  nop */
    868   };
    869 
    870   int bytes;
    871   char *p;
    872 
    873   if (fragp->fr_type != rs_align_code)
    874     return;
    875 
    876   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
    877   p = fragp->fr_literal + fragp->fr_fix;
    878 
    879   if (bytes & 3)
    880     {
    881       int fix = bytes & 3;
    882       memset (p, 0, fix);
    883       p += fix;
    884       bytes -= fix;
    885       fragp->fr_fix += fix;
    886     }
    887   if (bytes & 4)
    888     {
    889       memcpy (p, &nop_pattern[4], 4);
    890       p += 4;
    891       bytes -= 4;
    892       fragp->fr_fix += 4;
    893     }
    894 
    895   memcpy (p, nop_pattern, 8);
    896   fragp->fr_var = 8;
    897 }
    898 
    899 void
    900 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
    901 {
    902   unsigned int res;
    903   valueT val = *valP;
    904   char *place = fixP->fx_where + fixP->fx_frag->fr_literal;
    905 
    906   if (fixP->fx_subsy != (symbolS *) NULL)
    907     {
    908       /* We can't actually support subtracting a symbol.  */
    909       as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
    910     }
    911 
    912   if (fixP->fx_addsy != NULL)
    913     {
    914       if (fixP->fx_pcrel)
    915 	{
    916 	  /* Hack around bfd_install_relocation brain damage.  */
    917 	  val += fixP->fx_frag->fr_address + fixP->fx_where;
    918 
    919 	  switch (fixP->fx_r_type)
    920 	    {
    921 	    case BFD_RELOC_32:
    922 	      fixP->fx_r_type = BFD_RELOC_32_PCREL;
    923 	      break;
    924 
    925 	    case BFD_RELOC_SPU_PCREL16:
    926 	    case BFD_RELOC_SPU_PCREL9a:
    927 	    case BFD_RELOC_SPU_PCREL9b:
    928 	    case BFD_RELOC_32_PCREL:
    929 	      break;
    930 
    931 	    default:
    932 	      as_bad_where (fixP->fx_file, fixP->fx_line,
    933 			    _("expression too complex"));
    934 	      break;
    935 	    }
    936 	}
    937     }
    938 
    939   fixP->fx_addnumber = val;
    940 
    941   if (fixP->fx_r_type == BFD_RELOC_SPU_PPU32
    942       || fixP->fx_r_type == BFD_RELOC_SPU_PPU64)
    943     return;
    944 
    945   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
    946     {
    947       fixP->fx_done = 1;
    948       res = 0;
    949       if (fixP->tc_fix_data.arg_format > A_P)
    950 	{
    951 	  int hi = arg_encode[fixP->tc_fix_data.arg_format].hi;
    952 	  int lo = arg_encode[fixP->tc_fix_data.arg_format].lo;
    953 	  if (hi > lo && ((offsetT) val < lo || (offsetT) val > hi))
    954 	    as_bad_where (fixP->fx_file, fixP->fx_line,
    955 			  "Relocation doesn't fit. (relocation value = 0x%lx)",
    956 			  (long) val);
    957 	}
    958 
    959       switch (fixP->fx_r_type)
    960         {
    961         case BFD_RELOC_8:
    962 	  md_number_to_chars (place, val, 1);
    963 	  return;
    964 
    965         case BFD_RELOC_16:
    966 	  md_number_to_chars (place, val, 2);
    967 	  return;
    968 
    969         case BFD_RELOC_32:
    970 	case BFD_RELOC_32_PCREL:
    971 	  md_number_to_chars (place, val, 4);
    972 	  return;
    973 
    974         case BFD_RELOC_64:
    975 	  md_number_to_chars (place, val, 8);
    976 	  return;
    977 
    978         case BFD_RELOC_SPU_IMM7:
    979           res = (val & 0x7f) << 14;
    980           break;
    981 
    982         case BFD_RELOC_SPU_IMM8:
    983           res = (val & 0xff) << 14;
    984           break;
    985 
    986         case BFD_RELOC_SPU_IMM10:
    987           res = (val & 0x3ff) << 14;
    988           break;
    989 
    990         case BFD_RELOC_SPU_IMM10W:
    991           res = (val & 0x3ff0) << 10;
    992           break;
    993 
    994         case BFD_RELOC_SPU_IMM16:
    995           res = (val & 0xffff) << 7;
    996           break;
    997 
    998         case BFD_RELOC_SPU_IMM16W:
    999           res = (val & 0x3fffc) << 5;
   1000           break;
   1001 
   1002         case BFD_RELOC_SPU_IMM18:
   1003           res = (val & 0x3ffff) << 7;
   1004           break;
   1005 
   1006         case BFD_RELOC_SPU_PCREL9a:
   1007           res = ((val & 0x1fc) >> 2) | ((val & 0x600) << 14);
   1008           break;
   1009 
   1010         case BFD_RELOC_SPU_PCREL9b:
   1011           res = ((val & 0x1fc) >> 2) | ((val & 0x600) << 5);
   1012           break;
   1013 
   1014         case BFD_RELOC_SPU_PCREL16:
   1015           res = (val & 0x3fffc) << 5;
   1016           break;
   1017 
   1018 	case BFD_RELOC_SPU_HI16:
   1019 	  res = (val >> 9) & 0x7fff80;
   1020 	  break;
   1021 
   1022 	case BFD_RELOC_SPU_LO16:
   1023 	  res = (val << 7) & 0x7fff80;
   1024 	  break;
   1025 
   1026         default:
   1027           as_bad_where (fixP->fx_file, fixP->fx_line,
   1028                         _("reloc %d not supported by object file format"),
   1029                         (int) fixP->fx_r_type);
   1030         }
   1031 
   1032       if (res != 0)
   1033         {
   1034           place[0] |= (res >> 24) & 0xff;
   1035           place[1] |= (res >> 16) & 0xff;
   1036           place[2] |= (res >> 8) & 0xff;
   1037           place[3] |= (res) & 0xff;
   1038         }
   1039     }
   1040 }
   1041