Fixes the TILE-Gx/TILEPro port of gas to deal with relocations of
[external/binutils.git] / gas / config / tc-tilepro.c
1 /* tc-tilepro.c -- Assemble for a TILEPro chip.
2    Copyright 2011 Free Software Foundation, Inc.
3
4    This file is part of GAS, the GNU Assembler.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20
21 #include "as.h"
22 #include "struc-symbol.h"
23 #include "subsegs.h"
24
25 #include "elf/tilepro.h"
26 #include "opcode/tilepro.h"
27
28 #include "dwarf2dbg.h"
29 #include "dw2gencfi.h"
30
31 #include "safe-ctype.h"
32
33
34 /* Special registers. */
35 #define TREG_IDN0     57
36 #define TREG_IDN1     58
37 #define TREG_UDN0     59
38 #define TREG_UDN1     60
39 #define TREG_UDN2     61
40 #define TREG_UDN3     62
41 #define TREG_ZERO     63
42
43
44 /* Generic assembler global variables which must be defined by all
45    targets.  */
46
47 /* Characters which always start a comment.  */
48 const char comment_chars[] = "#";
49
50 /* Characters which start a comment at the beginning of a line.  */
51 const char line_comment_chars[] = "#";
52
53 /* Characters which may be used to separate multiple commands on a
54    single line.  */
55 const char line_separator_chars[] = ";";
56
57 /* Characters which are used to indicate an exponent in a floating
58    point number.  */
59 const char EXP_CHARS[] = "eE";
60
61 /* Characters which mean that a number is a floating point constant,
62    as in 0d1.0.  */
63 const char FLT_CHARS[] = "rRsSfFdDxXpP";
64
65 const char *md_shortopts = "VQ:";
66
67 struct option md_longopts[] =
68 {
69   {NULL, no_argument, NULL, 0}
70 };
71
72 size_t md_longopts_size = sizeof (md_longopts);
73
74 int
75 md_parse_option (int c, char *arg ATTRIBUTE_UNUSED)
76 {
77   switch (c)
78     {
79       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
80          should be emitted or not.  FIXME: Not implemented.  */
81     case 'Q':
82       break;
83
84       /* -V: SVR4 argument to print version ID.  */
85     case 'V':
86       print_version_id ();
87       break;
88
89     default:
90       return 0;
91     }
92
93   return 1;
94 }
95
96 void
97 md_show_usage (FILE *stream)
98 {
99   fprintf (stream, _("\
100   -Q                      ignored\n\
101   -V                      print assembler version number\n"));
102 }
103
104 /* Extra expression types.  */
105
106 #define O_lo16        O_md1
107 #define O_hi16        O_md2
108 #define O_ha16        O_md3
109 #define O_got         O_md4
110 #define O_got_lo16    O_md5
111 #define O_got_hi16    O_md6
112 #define O_got_ha16    O_md7
113 #define O_plt         O_md8
114 #define O_tls_gd      O_md9
115 #define O_tls_gd_lo16 O_md10
116 #define O_tls_gd_hi16 O_md11
117 #define O_tls_gd_ha16 O_md12
118 #define O_tls_ie      O_md13
119 #define O_tls_ie_lo16 O_md14
120 #define O_tls_ie_hi16 O_md15
121 #define O_tls_ie_ha16 O_md16
122
123 static struct hash_control *special_operator_hash;
124
125 /* Hash tables for instruction mnemonic lookup.  */
126 static struct hash_control *op_hash;
127
128 /* Hash table for spr lookup.  */
129 static struct hash_control *spr_hash;
130
131 /* True temporarily while parsing an SPR expression. This changes the
132  * namespace to include SPR names.  */
133 static int parsing_spr;
134
135 /* Are we currently inside `{ ... }'?  */
136 static int inside_bundle;
137
138 struct tilepro_instruction
139 {
140   const struct tilepro_opcode *opcode;
141   tilepro_pipeline pipe;
142   expressionS operand_values[TILEPRO_MAX_OPERANDS];
143 };
144
145 /* This keeps track of the current bundle being built up.  */
146 static struct tilepro_instruction
147 current_bundle[TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE];
148
149 /* Index in current_bundle for the next instruction to parse.  */
150 static int current_bundle_index;
151
152 /* Allow 'r63' in addition to 'zero', etc. Normally we disallow this as
153    'zero' is not a real register, so using it accidentally would be a
154    nasty bug. For other registers, such as 'sp', code using multiple names
155    for the same physical register is excessively confusing.
156
157    The '.require_canonical_reg_names' pseudo-op turns this error on,
158    and the '.no_require_canonical_reg_names' pseudo-op turns this off.
159    By default the error is on.  */
160 static int require_canonical_reg_names;
161
162 /* Allow bundles that do undefined or suspicious things like write
163    two different values to the same register at the same time.
164
165    The '.no_allow_suspicious_bundles' pseudo-op turns this error on,
166    and the '.allow_suspicious_bundles' pseudo-op turns this off.  */
167 static int allow_suspicious_bundles;
168
169
170 /* A hash table of main processor registers, mapping each register name
171    to its index.
172
173    Furthermore, if the register number is greater than the number
174    of registers for that processor, the user used an illegal alias
175    for that register (e.g. r63 instead of zero), so we should generate
176    a warning. The attempted register number can be found by clearing
177    NONCANONICAL_REG_NAME_FLAG.  */
178 static struct hash_control *main_reg_hash;
179
180
181 /* We cannot unambiguously store a 0 in a hash table and look it up,
182    so we OR in this flag to every canonical register.  */
183 #define CANONICAL_REG_NAME_FLAG    0x1000
184
185 /* By default we disallow register aliases like r63, but we record
186    them in the hash table in case the .no_require_canonical_reg_names
187    directive is used. Noncanonical names have this value added to them.  */
188 #define NONCANONICAL_REG_NAME_FLAG 0x2000
189
190 /* Discards flags for register hash table entries and returns the
191    reg number.  */
192 #define EXTRACT_REGNO(p) ((p) & 63)
193
194 /* This function is called once, at assembler startup time.  It should
195    set up all the tables, etc., that the MD part of the assembler will
196    need.  */
197 void
198 md_begin (void)
199 {
200   const struct tilepro_opcode *op;
201   int i;
202
203   /* Guarantee text section is aligned.  */
204   bfd_set_section_alignment (stdoutput, text_section,
205                              TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES);
206
207   require_canonical_reg_names = 1;
208   allow_suspicious_bundles = 0;
209   current_bundle_index = 0;
210   inside_bundle = 0;
211
212   /* Initialize special operator hash table.  */
213   special_operator_hash = hash_new ();
214 #define INSERT_SPECIAL_OP(name)                                 \
215   hash_insert (special_operator_hash, #name, (void *)O_##name)
216
217   INSERT_SPECIAL_OP(lo16);
218   INSERT_SPECIAL_OP(hi16);
219   INSERT_SPECIAL_OP(ha16);
220   INSERT_SPECIAL_OP(got);
221   INSERT_SPECIAL_OP(got_lo16);
222   INSERT_SPECIAL_OP(got_hi16);
223   INSERT_SPECIAL_OP(got_ha16);
224   INSERT_SPECIAL_OP(plt);
225   INSERT_SPECIAL_OP(tls_gd);
226   INSERT_SPECIAL_OP(tls_gd_lo16);
227   INSERT_SPECIAL_OP(tls_gd_hi16);
228   INSERT_SPECIAL_OP(tls_gd_ha16);
229   INSERT_SPECIAL_OP(tls_ie);
230   INSERT_SPECIAL_OP(tls_ie_lo16);
231   INSERT_SPECIAL_OP(tls_ie_hi16);
232   INSERT_SPECIAL_OP(tls_ie_ha16);
233 #undef INSERT_SPECIAL_OP
234
235   /* Initialize op_hash hash table.  */
236   op_hash = hash_new ();
237   for (op = &tilepro_opcodes[0]; op->name != NULL; op++)
238     {
239       const char *hash_err = hash_insert (op_hash, op->name, (void *)op);
240       if (hash_err != NULL)
241         {
242           as_fatal (_("Internal Error:  Can't hash %s: %s"),
243                     op->name, hash_err);
244         }
245     }
246
247   /* Initialize the spr hash table.  */
248   parsing_spr = 0;
249   spr_hash = hash_new ();
250   for (i = 0; i < tilepro_num_sprs; i++)
251     hash_insert (spr_hash, tilepro_sprs[i].name,
252                  (void *) &tilepro_sprs[i]);
253
254   /* Set up the main_reg_hash table. We use this instead of
255    * creating a symbol in the register section to avoid ambiguities
256    * with labels that have the same names as registers.  */
257   main_reg_hash = hash_new ();
258   for (i = 0; i < TILEPRO_NUM_REGISTERS; i++)
259     {
260       char buf[64];
261
262       hash_insert (main_reg_hash, tilepro_register_names[i],
263                    (void *) (long)(i | CANONICAL_REG_NAME_FLAG));
264
265       /* See if we should insert a noncanonical alias, like r63.  */
266       sprintf (buf, "r%d", i);
267       if (strcmp (buf, tilepro_register_names[i]) != 0)
268         hash_insert (main_reg_hash, xstrdup (buf),
269                      (void *) (long)(i | NONCANONICAL_REG_NAME_FLAG));
270     }
271
272   /* Insert obsolete backwards-compatibility register names.  */
273   hash_insert (main_reg_hash, "io0",
274                (void *) (long) (TREG_IDN0 | CANONICAL_REG_NAME_FLAG));
275   hash_insert (main_reg_hash, "io1",
276                (void *) (long) (TREG_IDN1 | CANONICAL_REG_NAME_FLAG));
277   hash_insert (main_reg_hash, "us0",
278                (void *) (long) (TREG_UDN0 | CANONICAL_REG_NAME_FLAG));
279   hash_insert (main_reg_hash, "us1",
280                (void *) (long) (TREG_UDN1 | CANONICAL_REG_NAME_FLAG));
281   hash_insert (main_reg_hash, "us2",
282                (void *) (long) (TREG_UDN2 | CANONICAL_REG_NAME_FLAG));
283   hash_insert (main_reg_hash, "us3",
284                (void *) (long) (TREG_UDN3 | CANONICAL_REG_NAME_FLAG));
285
286 }
287
288
289 #define BUNDLE_TEMPLATE_MASK(p0, p1, p2) \
290   ((p0) | ((p1) << 8) | ((p2) << 16))
291 #define BUNDLE_TEMPLATE(p0, p1, p2) \
292   { { (p0), (p1), (p2) }, \
293      BUNDLE_TEMPLATE_MASK(1 << (p0), 1 << (p1), (1 << (p2))) \
294   }
295
296 #define NO_PIPELINE TILEPRO_NUM_PIPELINE_ENCODINGS
297
298 struct bundle_template
299 {
300   tilepro_pipeline pipe[TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE];
301   unsigned int pipe_mask;
302 };
303
304 static const struct bundle_template bundle_templates[] =
305 {
306   /* In Y format we must always have something in Y2, since it has
307    * no fnop, so this conveys that Y2 must always be used.  */
308   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y0, TILEPRO_PIPELINE_Y2, NO_PIPELINE),
309   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y1, TILEPRO_PIPELINE_Y2, NO_PIPELINE),
310   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y0, NO_PIPELINE),
311   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y1, NO_PIPELINE),
312
313   /* Y format has three instructions.  */
314   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y0, TILEPRO_PIPELINE_Y1, TILEPRO_PIPELINE_Y2),
315   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y0, TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y1),
316   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y1, TILEPRO_PIPELINE_Y0, TILEPRO_PIPELINE_Y2),
317   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y1, TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y0),
318   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y0, TILEPRO_PIPELINE_Y1),
319   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_Y2, TILEPRO_PIPELINE_Y1, TILEPRO_PIPELINE_Y0),
320
321   /* X format has only two instructions.  */
322   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_X0, TILEPRO_PIPELINE_X1, NO_PIPELINE),
323   BUNDLE_TEMPLATE(TILEPRO_PIPELINE_X1, TILEPRO_PIPELINE_X0, NO_PIPELINE)
324 };
325
326
327 static void
328 prepend_nop_to_bundle (tilepro_mnemonic mnemonic)
329 {
330   memmove (&current_bundle[1], &current_bundle[0],
331            current_bundle_index * sizeof current_bundle[0]);
332   current_bundle[0].opcode = &tilepro_opcodes[mnemonic];
333   ++current_bundle_index;
334 }
335
336
337 static tilepro_bundle_bits
338 insert_operand (tilepro_bundle_bits bits,
339                 const struct tilepro_operand *operand,
340                 int operand_value,
341                 char *file,
342                 unsigned lineno)
343 {
344   /* Range-check the immediate.  */
345   int num_bits = operand->num_bits;
346
347   operand_value >>= operand->rightshift;
348
349   if (bfd_check_overflow (operand->is_signed
350                           ? complain_overflow_signed
351                           : complain_overflow_unsigned,
352                           num_bits,
353                           0,
354                           bfd_arch_bits_per_address (stdoutput),
355                           operand_value)
356       != bfd_reloc_ok)
357     {
358       offsetT min, max;
359       if (operand->is_signed)
360         {
361           min = -(1 << (num_bits - 1));
362           max = (1 << (num_bits - 1)) - 1;
363         }
364       else
365         {
366           min = 0;
367           max = (1 << num_bits) - 1;
368         }
369       as_bad_value_out_of_range (_("operand"), operand_value, min, max,
370                                  file, lineno);
371     }
372
373   /* Write out the bits for the immediate.  */
374   return bits | operand->insert (operand_value);
375 }
376
377
378 static int
379 apply_special_operator (operatorT op, int num)
380 {
381   switch (op)
382     {
383     case O_lo16:
384     case O_got:
385     case O_got_lo16:
386     case O_tls_gd:
387     case O_tls_gd_lo16:
388     case O_tls_ie:
389     case O_tls_ie_lo16:
390       return (signed short)num;
391
392     case O_hi16:
393     case O_got_hi16:
394     case O_tls_gd_hi16:
395     case O_tls_ie_hi16:
396       return (signed short)(num >> 16);
397
398     case O_ha16:
399     case O_got_ha16:
400     case O_tls_gd_ha16:
401     case O_tls_ie_ha16:
402       return (signed short)((num + 0x8000) >> 16);
403
404     default:
405       abort ();
406     }
407 }
408
409
410 static tilepro_bundle_bits
411 emit_tilepro_instruction (tilepro_bundle_bits bits,
412                           int num_operands,
413                           const unsigned char *operands,
414                           expressionS *operand_values,
415                           char *bundle_start)
416 {
417   int i;
418
419   for (i = 0; i < num_operands; i++)
420     {
421       const struct tilepro_operand *operand =
422         &tilepro_operands[operands[i]];
423       expressionS *operand_exp = &operand_values[i];
424       int is_pc_relative = operand->is_pc_relative;
425
426       if (operand_exp->X_op == O_register
427           || (operand_exp->X_op == O_constant && !is_pc_relative))
428         {
429           /* We know what the bits are right now, so insert them.  */
430           bits = insert_operand (bits, operand, operand_exp->X_add_number,
431                                  NULL, 0);
432         }
433       else
434         {
435           bfd_reloc_code_real_type reloc = operand->default_reloc;
436           expressionS subexp;
437           int die = 0, use_subexp = 0, require_symbol = 0;
438           fixS *fixP;
439
440           /* Take an expression like hi16(x) and turn it into x with
441              a different reloc type.  */
442           switch (operand_exp->X_op)
443             {
444 #define HANDLE_OP16(suffix)                                     \
445               switch (reloc)                                    \
446                 {                                               \
447                 case BFD_RELOC_TILEPRO_IMM16_X0:                \
448                   reloc = BFD_RELOC_TILEPRO_IMM16_X0_##suffix;  \
449                   break;                                        \
450                 case BFD_RELOC_TILEPRO_IMM16_X1:                \
451                   reloc = BFD_RELOC_TILEPRO_IMM16_X1_##suffix;  \
452                   break;                                        \
453                 default:                                        \
454                   die = 1;                                      \
455                   break;                                        \
456                 }                                               \
457               use_subexp = 1
458
459             case O_lo16:
460               HANDLE_OP16 (LO);
461               break;
462
463             case O_hi16:
464               HANDLE_OP16 (HI);
465               break;
466
467             case O_ha16:
468               HANDLE_OP16 (HA);
469               break;
470
471             case O_got:
472               HANDLE_OP16 (GOT);
473               require_symbol = 1;
474               break;
475
476             case O_got_lo16:
477               HANDLE_OP16 (GOT_LO);
478               require_symbol = 1;
479               break;
480
481             case O_got_hi16:
482               HANDLE_OP16 (GOT_HI);
483               require_symbol = 1;
484               break;
485
486             case O_got_ha16:
487               HANDLE_OP16 (GOT_HA);
488               require_symbol = 1;
489               break;
490
491             case O_tls_gd:
492               HANDLE_OP16 (TLS_GD);
493               require_symbol = 1;
494               break;
495
496             case O_tls_gd_lo16:
497               HANDLE_OP16 (TLS_GD_LO);
498               require_symbol = 1;
499               break;
500
501             case O_tls_gd_hi16:
502               HANDLE_OP16 (TLS_GD_HI);
503               require_symbol = 1;
504               break;
505
506             case O_tls_gd_ha16:
507               HANDLE_OP16 (TLS_GD_HA);
508               require_symbol = 1;
509               break;
510
511             case O_tls_ie:
512               HANDLE_OP16 (TLS_IE);
513               require_symbol = 1;
514               break;
515
516             case O_tls_ie_lo16:
517               HANDLE_OP16 (TLS_IE_LO);
518               require_symbol = 1;
519               break;
520
521             case O_tls_ie_hi16:
522               HANDLE_OP16 (TLS_IE_HI);
523               require_symbol = 1;
524               break;
525
526             case O_tls_ie_ha16:
527               HANDLE_OP16 (TLS_IE_HA);
528               require_symbol = 1;
529               break;
530
531 #undef HANDLE_OP16
532
533             case O_plt:
534               switch (reloc)
535                 {
536                 case BFD_RELOC_TILEPRO_JOFFLONG_X1:
537                   reloc = BFD_RELOC_TILEPRO_JOFFLONG_X1_PLT;
538                   break;
539                 default:
540                   die = 1;
541                   break;
542                 }
543               use_subexp = 1;
544               require_symbol = 1;
545               break;
546
547             default:
548               /* Do nothing.  */
549               break;
550             }
551
552           if (die)
553             {
554               as_bad (_("Invalid operator for operand."));
555             }
556           else if (use_subexp)
557             {
558               /* Now that we've changed the reloc, change ha16(x) into x,
559                  etc.  */
560
561               if (operand_exp->X_add_symbol->sy_value.X_md)
562                 {
563                   /* HACK: We used X_md to mark this symbol as a fake wrapper
564                      around a real expression. To unwrap it, we just grab its
565                      value here.  */
566                   operand_exp = &operand_exp->X_add_symbol->sy_value;
567
568                   if (require_symbol)
569                     {
570                       /* Look at the expression, and reject it if it's not a
571                          plain symbol.  */
572                       if (operand_exp->X_op != O_symbol
573                           || operand_exp->X_add_number != 0)
574                         as_bad (_("Operator may only be applied to symbols."));
575                     }
576                 }
577               else
578                 {
579                   /* The value of this expression is an actual symbol, so
580                      turn that into an expression.  */
581                   memset (&subexp, 0, sizeof subexp);
582                   subexp.X_op = O_symbol;
583                   subexp.X_add_symbol = operand_exp->X_add_symbol;
584                   operand_exp = &subexp;
585                 }
586             }
587
588           /* Create a fixup to handle this later. */
589           fixP = fix_new_exp (frag_now,
590                               bundle_start - frag_now->fr_literal,
591                               (operand->num_bits + 7) >> 3,
592                               operand_exp,
593                               is_pc_relative,
594                               reloc);
595           fixP->tc_fix_data = operand;
596
597           /* Don't do overflow checking if we are applying a function like
598              ha16.  */
599           fixP->fx_no_overflow |= use_subexp;
600         }
601     }
602   return bits;
603 }
604
605
606 /* Detects and complains if two instructions in current_bundle write
607    to the same register, either implicitly or explicitly, or if a
608    read-only register is written.  */
609 static void
610 check_illegal_reg_writes (void)
611 {
612   BFD_HOST_U_64_BIT all_regs_written = 0;
613   int j;
614
615   for (j = 0; j < current_bundle_index; j++)
616     {
617       const struct tilepro_instruction *instr = &current_bundle[j];
618       int k;
619       BFD_HOST_U_64_BIT regs =
620         ((BFD_HOST_U_64_BIT)1) << instr->opcode->implicitly_written_register;
621       BFD_HOST_U_64_BIT conflict;
622
623       for (k = 0; k < instr->opcode->num_operands; k++)
624         {
625           const struct tilepro_operand *operand =
626             &tilepro_operands[instr->opcode->operands[instr->pipe][k]];
627
628           if (operand->is_dest_reg)
629             {
630               int regno = instr->operand_values[k].X_add_number;
631               BFD_HOST_U_64_BIT mask = ((BFD_HOST_U_64_BIT)1) << regno;
632
633               if ((mask & (  (((BFD_HOST_U_64_BIT)1) << TREG_IDN1)
634                              | (((BFD_HOST_U_64_BIT)1) << TREG_UDN1)
635                              | (((BFD_HOST_U_64_BIT)1) << TREG_UDN2)
636                              | (((BFD_HOST_U_64_BIT)1) << TREG_UDN3))) != 0
637                   && !allow_suspicious_bundles)
638                 {
639                   as_bad (_("Writes to register '%s' are not allowed."),
640                           tilepro_register_names[regno]);
641                 }
642
643               regs |= mask;
644             }
645         }
646
647       /* Writing to the zero register doesn't count.  */
648       regs &= ~(((BFD_HOST_U_64_BIT)1) << TREG_ZERO);
649
650       conflict = all_regs_written & regs;
651       if (conflict != 0 && !allow_suspicious_bundles)
652         {
653           /* Find which register caused the conflict.  */
654           const char *conflicting_reg_name = "???";
655           int i;
656
657           for (i = 0; i < TILEPRO_NUM_REGISTERS; i++)
658             {
659               if (((conflict >> i) & 1) != 0)
660                 {
661                   conflicting_reg_name = tilepro_register_names[i];
662                   break;
663                 }
664             }
665
666           as_bad (_("Two instructions in the same bundle both write "
667                     "to register %s, which is not allowed."),
668                   conflicting_reg_name);
669         }
670
671       all_regs_written |= regs;
672     }
673 }
674
675
676 static void
677 tilepro_flush_bundle (void)
678 {
679   unsigned i;
680   int j, addr_mod;
681   unsigned compatible_pipes;
682   const struct bundle_template *match;
683   char *f;
684
685   inside_bundle = 0;
686
687   switch (current_bundle_index)
688     {
689     case 0:
690       /* No instructions.  */
691       return;
692     case 1:
693       if (current_bundle[0].opcode->can_bundle)
694         {
695           /* Simplify later logic by adding an explicit fnop.  */
696           prepend_nop_to_bundle (TILEPRO_OPC_FNOP);
697         }
698       else
699         {
700           /* This instruction cannot be bundled with anything else.
701              Prepend an explicit 'nop', rather than an 'fnop', because
702              fnops can be replaced by later binary-processing tools
703              while nops cannot.  */
704           prepend_nop_to_bundle (TILEPRO_OPC_NOP);
705         }
706       break;
707     default:
708       if (!allow_suspicious_bundles)
709         {
710           /* Make sure all instructions can be bundled with other
711              instructions.  */
712           const struct tilepro_opcode *cannot_bundle = NULL;
713           bfd_boolean seen_non_nop = FALSE;
714
715           for (j = 0; j < current_bundle_index; j++)
716             {
717               const struct tilepro_opcode *op = current_bundle[j].opcode;
718
719               if (!op->can_bundle && cannot_bundle == NULL)
720                 cannot_bundle = op;
721               else if (op->mnemonic != TILEPRO_OPC_NOP
722                        && op->mnemonic != TILEPRO_OPC_INFO
723                        && op->mnemonic != TILEPRO_OPC_INFOL)
724                 seen_non_nop = TRUE;
725             }
726
727           if (cannot_bundle != NULL && seen_non_nop)
728             {
729               current_bundle_index = 0;
730               as_bad (_("'%s' may not be bundled with other instructions."),
731                       cannot_bundle->name);
732               return;
733             }
734         }
735       break;
736     }
737
738   compatible_pipes =
739     BUNDLE_TEMPLATE_MASK(current_bundle[0].opcode->pipes,
740                          current_bundle[1].opcode->pipes,
741                          (current_bundle_index == 3
742                           ? current_bundle[2].opcode->pipes
743                           : (1 << NO_PIPELINE)));
744
745   /* Find a template that works, if any.  */
746   match = NULL;
747   for (i = 0; i < sizeof bundle_templates / sizeof bundle_templates[0]; i++)
748     {
749       const struct bundle_template *b = &bundle_templates[i];
750       if ((b->pipe_mask & compatible_pipes) == b->pipe_mask)
751         {
752           match = b;
753           break;
754         }
755     }
756
757   if (match == NULL)
758     {
759       current_bundle_index = 0;
760       as_bad (_("Invalid combination of instructions for bundle."));
761       return;
762     }
763
764   /* If the section seems to have no alignment set yet, go ahead and
765      make it large enough to hold code.  */
766   if (bfd_get_section_alignment (stdoutput, now_seg) == 0)
767     bfd_set_section_alignment (stdoutput, now_seg,
768                                TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES);
769
770   for (j = 0; j < current_bundle_index; j++)
771     current_bundle[j].pipe = match->pipe[j];
772
773   if (current_bundle_index == 2 && !tilepro_is_x_pipeline(match->pipe[0]))
774     {
775       /* We are in Y mode with only two instructions, so add an FNOP.  */
776       prepend_nop_to_bundle (TILEPRO_OPC_FNOP);
777
778       /* Figure out what pipe the fnop must be in via arithmetic.
779        * p0 + p1 + p2 must sum to the sum of TILEPRO_PIPELINE_Y[012].  */
780       current_bundle[0].pipe =
781         (tilepro_pipeline)((TILEPRO_PIPELINE_Y0
782                             + TILEPRO_PIPELINE_Y1
783                             + TILEPRO_PIPELINE_Y2) -
784                            (current_bundle[1].pipe + current_bundle[2].pipe));
785     }
786
787   check_illegal_reg_writes ();
788
789   f = frag_more (TILEPRO_BUNDLE_SIZE_IN_BYTES);
790
791   /* Check to see if this bundle is at an offset that is a multiple of 8-bytes
792      from the start of the frag.  */
793   addr_mod = frag_now_fix () & (TILEPRO_BUNDLE_ALIGNMENT_IN_BYTES - 1);
794   if (frag_now->has_code && frag_now->insn_addr != addr_mod)
795     as_bad (_("instruction address is not a multiple of 8"));
796   frag_now->insn_addr = addr_mod;
797   frag_now->has_code = 1;
798
799   tilepro_bundle_bits bits = 0;
800   for (j = 0; j < current_bundle_index; j++)
801     {
802       struct tilepro_instruction *instr = &current_bundle[j];
803       tilepro_pipeline pipeline = instr->pipe;
804       const struct tilepro_opcode *opcode = instr->opcode;
805
806       bits |= emit_tilepro_instruction (opcode->fixed_bit_values[pipeline],
807                                         opcode->num_operands,
808                                         &opcode->operands[pipeline][0],
809                                         instr->operand_values,
810                                         f);
811     }
812
813   number_to_chars_littleendian (f, (unsigned int)bits, 4);
814   number_to_chars_littleendian (f + 4, (unsigned int)(bits >> 32), 4);
815   current_bundle_index = 0;
816
817   /* Emit DWARF2 debugging information.  */
818   dwarf2_emit_insn (TILEPRO_BUNDLE_SIZE_IN_BYTES);
819 }
820
821
822 /* Extend the expression parser to handle hi16(label), etc.
823    as well as SPR names when in the context of parsing an SPR.  */
824 int
825 tilepro_parse_name (char *name, expressionS *e, char *nextcharP)
826 {
827   operatorT op = O_illegal;
828
829   if (parsing_spr)
830     {
831       void *val = hash_find (spr_hash, name);
832       if (val == NULL)
833         return 0;
834
835       memset (e, 0, sizeof *e);
836       e->X_op = O_constant;
837       e->X_add_number = ((const struct tilepro_spr *)val)->number;
838       return 1;
839     }
840
841   if (*nextcharP != '(')
842     {
843       /* hi16, etc. not followed by a paren is just a label with that
844          name.  */
845       return 0;
846     }
847   else
848     {
849       /* Look up the operator in our table.  */
850       void *val = hash_find (special_operator_hash, name);
851       if (val == 0)
852         return 0;
853       op = (operatorT)(long)val;
854     }
855
856   /* Restore old '(' and skip it.  */
857   *input_line_pointer = '(';
858   ++input_line_pointer;
859
860   expression (e);
861
862   if (*input_line_pointer != ')')
863     {
864       as_bad (_("Missing ')'"));
865       *nextcharP = *input_line_pointer;
866       return 0;
867     }
868   /* Skip ')'.  */
869   ++input_line_pointer;
870
871   if (e->X_op == O_register || e->X_op == O_absent)
872     {
873       as_bad (_("Invalid expression."));
874       e->X_op = O_constant;
875       e->X_add_number = 0;
876     }
877   else
878     {
879       /* Wrap subexpression with a unary operator.  */
880       symbolS *sym = make_expr_symbol (e);
881
882       if (sym != e->X_add_symbol)
883         {
884           /* HACK: mark this symbol as a temporary wrapper around a proper
885              expression, so we can unwrap it later once we have communicated
886              the relocation type.  */
887           sym->sy_value.X_md = 1;
888         }
889
890       memset (e, 0, sizeof *e);
891       e->X_op = op;
892       e->X_add_symbol = sym;
893       e->X_add_number = 0;
894     }
895
896   *nextcharP = *input_line_pointer;
897   return 1;
898 }
899
900
901 /* Parses an expression which must be a register name.  */
902
903 static void
904 parse_reg_expression (expressionS* expression)
905 {
906   /* Zero everything to make sure we don't miss any flags.  */
907   memset (expression, 0, sizeof *expression);
908
909   char* regname = input_line_pointer;
910   char terminating_char = get_symbol_end ();
911
912   void* pval = hash_find (main_reg_hash, regname);
913
914   if (pval == NULL)
915     as_bad (_("Expected register, got '%s'."), regname);
916
917   int regno_and_flags = (int)(size_t)pval;
918   int regno = EXTRACT_REGNO(regno_and_flags);
919
920   if ((regno_and_flags & NONCANONICAL_REG_NAME_FLAG)
921       && require_canonical_reg_names)
922     as_warn (_("Found use of non-canonical register name %s; "
923                "use %s instead."),
924                regname, tilepro_register_names[regno]);
925
926   /* Restore the old character following the register name.  */
927   *input_line_pointer = terminating_char;
928
929   /* Fill in the expression fields to indicate it's a register.  */
930   expression->X_op = O_register;
931   expression->X_add_number = regno;
932 }
933
934
935 /* Parses and type-checks comma-separated operands in input_line_pointer.  */
936 static void
937 parse_operands (const char *opcode_name,
938                 const unsigned char *operands,
939                 int num_operands,
940                 expressionS *operand_values)
941 {
942   int i;
943
944   memset (operand_values, 0, num_operands * sizeof operand_values[0]);
945
946   SKIP_WHITESPACE ();
947   for (i = 0; i < num_operands; i++)
948     {
949       tilepro_operand_type type = tilepro_operands[operands[i]].type;
950
951       SKIP_WHITESPACE ();
952
953       if (type == TILEPRO_OP_TYPE_REGISTER)
954         {
955           parse_reg_expression (&operand_values[i]);
956         }
957       else if (*input_line_pointer == '}')
958         {
959           operand_values[i].X_op = O_absent;
960         }
961       else if (type == TILEPRO_OP_TYPE_SPR)
962         {
963           /* Modify the expression parser to add SPRs to the namespace.  */
964           parsing_spr = 1;
965           expression (&operand_values[i]);
966           parsing_spr = 0;
967         }
968       else
969         {
970           expression (&operand_values[i]);
971         }
972
973       SKIP_WHITESPACE ();
974
975       if (i + 1 < num_operands)
976         {
977           int separator = (unsigned char)*input_line_pointer++;
978
979           if (is_end_of_line[separator] || (separator == '}'))
980             {
981               as_bad (_("Too few operands to '%s'."), opcode_name);
982               return;
983             }
984           else if (separator != ',')
985             {
986               as_bad (_("Unexpected character '%c' after operand %d to %s."),
987                       (char)separator, i + 1, opcode_name);
988               return;
989             }
990         }
991
992       /* Arbitrarily use the first valid pipe to get the operand type,
993          since they are all the same.  */
994       switch (tilepro_operands[operands[i]].type)
995         {
996         case TILEPRO_OP_TYPE_REGISTER:
997           /* Handled in parse_reg_expression already.  */
998           break;
999         case TILEPRO_OP_TYPE_SPR:
1000           /* Fall through  */
1001         case TILEPRO_OP_TYPE_IMMEDIATE:
1002           /* Fall through  */
1003         case TILEPRO_OP_TYPE_ADDRESS:
1004           if (   operand_values[i].X_op == O_register
1005               || operand_values[i].X_op == O_illegal
1006               || operand_values[i].X_op == O_absent)
1007             as_bad (_("Expected immediate expression"));
1008           break;
1009         default:
1010           abort ();
1011         }
1012     }
1013
1014   if (!is_end_of_line[(unsigned char)*input_line_pointer])
1015     {
1016       switch (*input_line_pointer)
1017         {
1018         case '}':
1019           if (!inside_bundle)
1020             as_bad (_("Found '}' when not bundling."));
1021           ++input_line_pointer;
1022           inside_bundle = 0;
1023           demand_empty_rest_of_line ();
1024           break;
1025
1026         case ',':
1027           as_bad (_("Too many operands"));
1028           break;
1029
1030         default:
1031           /* Use default error for unrecognized garbage.  */
1032           demand_empty_rest_of_line ();
1033           break;
1034         }
1035     }
1036 }
1037
1038
1039 /* This is the guts of the machine-dependent assembler.  STR points to a
1040    machine dependent instruction.  This function is supposed to emit
1041    the frags/bytes it assembles to.  */
1042 void
1043 md_assemble (char *str)
1044 {
1045   char old_char;
1046   size_t opname_len;
1047   char *old_input_line_pointer;
1048   const struct tilepro_opcode *op;
1049   int first_pipe;
1050
1051   /* Split off the opcode and look it up.  */
1052   opname_len = strcspn (str, " {}");
1053   old_char = str[opname_len];
1054   str[opname_len] = '\0';
1055
1056   op = hash_find(op_hash, str);
1057   str[opname_len] = old_char;
1058   if (op == NULL)
1059     {
1060       as_bad (_("Unknown opcode `%.*s'."), (int)opname_len, str);
1061       return;
1062     }
1063
1064   /* Prepare to parse the operands.  */
1065   old_input_line_pointer = input_line_pointer;
1066   input_line_pointer = str + opname_len;
1067   SKIP_WHITESPACE ();
1068
1069   if (current_bundle_index == TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE)
1070     {
1071       as_bad (_("Too many instructions for bundle."));
1072       tilepro_flush_bundle ();
1073     }
1074
1075   /* Make sure we have room for the upcoming bundle before we
1076      create any fixups. Otherwise if we have to switch to a new
1077      frag the fixup dot_value fields will be wrong.  */
1078   frag_grow (TILEPRO_BUNDLE_SIZE_IN_BYTES);
1079
1080   /* Find a valid pipe for this opcode. */
1081   for (first_pipe = 0; (op->pipes & (1 << first_pipe)) == 0; first_pipe++)
1082     ;
1083
1084   /* Call the function that assembles this instruction.  */
1085   current_bundle[current_bundle_index].opcode = op;
1086   parse_operands (op->name,
1087                   &op->operands[first_pipe][0],
1088                   op->num_operands,
1089                   current_bundle[current_bundle_index].operand_values);
1090   ++current_bundle_index;
1091
1092   /* Restore the saved value of input_line_pointer.  */
1093   input_line_pointer = old_input_line_pointer;
1094
1095   /* If we weren't inside curly braces, go ahead and emit
1096      this lone instruction as a bundle right now.  */
1097   if (!inside_bundle)
1098     tilepro_flush_bundle ();
1099 }
1100
1101 static void
1102 s_require_canonical_reg_names (int require)
1103 {
1104   demand_empty_rest_of_line ();
1105   require_canonical_reg_names = require;
1106 }
1107
1108 static void
1109 s_allow_suspicious_bundles (int allow)
1110 {
1111   demand_empty_rest_of_line ();
1112   allow_suspicious_bundles = allow;
1113 }
1114
1115 const pseudo_typeS md_pseudo_table[] =
1116 {
1117   {"align", s_align_bytes, 0},  /* Defaulting is invalid (0).  */
1118   {"word", cons, 4},
1119   {"require_canonical_reg_names", s_require_canonical_reg_names, 1 },
1120   {"no_require_canonical_reg_names", s_require_canonical_reg_names, 0 },
1121   {"allow_suspicious_bundles", s_allow_suspicious_bundles, 1 },
1122   {"no_allow_suspicious_bundles", s_allow_suspicious_bundles, 0 },
1123   { NULL, 0, 0 }
1124 };
1125
1126 /* Equal to MAX_PRECISION in atof-ieee.c  */
1127 #define MAX_LITTLENUMS 6
1128
1129 /* Turn the string pointed to by litP into a floating point constant
1130    of type TYPE, and emit the appropriate bytes.  The number of
1131    LITTLENUMS emitted is stored in *SIZEP.  An error message is
1132    returned, or NULL on OK.  */
1133
1134 char *
1135 md_atof (int type, char *litP, int *sizeP)
1136 {
1137   int prec;
1138   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1139   LITTLENUM_TYPE *wordP;
1140   char *t;
1141
1142   switch (type)
1143     {
1144     case 'f':
1145     case 'F':
1146       prec = 2;
1147       break;
1148
1149     case 'd':
1150     case 'D':
1151       prec = 4;
1152       break;
1153
1154     default:
1155       *sizeP = 0;
1156       return _("Bad call to md_atof ()");
1157     }
1158   t = atof_ieee (input_line_pointer, type, words);
1159   if (t)
1160     input_line_pointer = t;
1161
1162   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1163   /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
1164      the bigendian 386.  */
1165   for (wordP = words + prec - 1; prec--;)
1166     {
1167       md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
1168       litP += sizeof (LITTLENUM_TYPE);
1169     }
1170   return 0;
1171 }
1172
1173
1174 /* We have no need to default values of symbols.  */
1175
1176 symbolS *
1177 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1178 {
1179   return NULL;
1180 }
1181
1182
1183 void
1184 tilepro_cons_fix_new (fragS *frag,
1185                       int where,
1186                       int nbytes,
1187                       expressionS *exp)
1188 {
1189   expressionS subexp;
1190   bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
1191   int no_overflow = 0;
1192   fixS *fixP;
1193
1194   /* See if it's one of our special functions.  */
1195   switch (exp->X_op)
1196     {
1197     case O_lo16:
1198       reloc = BFD_RELOC_LO16;
1199       no_overflow = 1;
1200       break;
1201     case O_hi16:
1202       reloc = BFD_RELOC_HI16;
1203       no_overflow = 1;
1204       break;
1205     case O_ha16:
1206       reloc = BFD_RELOC_HI16_S;
1207       no_overflow = 1;
1208       break;
1209
1210     default:
1211       /* Do nothing.  */
1212       break;
1213     }
1214
1215   if (reloc != BFD_RELOC_NONE)
1216     {
1217       if (nbytes != 2)
1218         {
1219           as_bad (_("This operator only produces two byte values."));
1220           nbytes = 2;
1221         }
1222
1223       memset (&subexp, 0, sizeof subexp);
1224       subexp.X_op = O_symbol;
1225       subexp.X_add_symbol = exp->X_add_symbol;
1226       exp = &subexp;
1227     }
1228   else
1229     {
1230       switch (nbytes)
1231         {
1232         case 1:
1233           reloc = BFD_RELOC_8;
1234           break;
1235         case 2:
1236           reloc = BFD_RELOC_16;
1237           break;
1238         case 4:
1239           reloc = BFD_RELOC_32;
1240           break;
1241         case 8:
1242           reloc = BFD_RELOC_64;
1243           break;
1244         default:
1245           as_bad (_("unsupported BFD relocation size %d"), nbytes);
1246           reloc = BFD_RELOC_32;
1247           break;
1248         }
1249     }
1250
1251   fixP = fix_new_exp (frag, where, nbytes, exp, 0, reloc);
1252   fixP->tc_fix_data = NULL;
1253   fixP->fx_no_overflow |= no_overflow;
1254 }
1255
1256
1257 void
1258 md_apply_fix (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
1259 {
1260   const struct tilepro_operand *operand;
1261   valueT value = *valP;
1262   char *p;
1263
1264   /* Leave these for the linker.  */
1265   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1266       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1267     return;
1268
1269   if (fixP->fx_subsy != (symbolS *) NULL)
1270     {
1271       /* We can't actually support subtracting a symbol.  */
1272       as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1273     }
1274
1275   /* Correct relocation types for pc-relativeness.  */
1276   switch (fixP->fx_r_type)
1277     {
1278 #define FIX_PCREL(rtype)                        \
1279       case rtype:                               \
1280         if (fixP->fx_pcrel)                     \
1281           fixP->fx_r_type = rtype##_PCREL;      \
1282       break;                                    \
1283                                                 \
1284     case rtype##_PCREL:                         \
1285       if (!fixP->fx_pcrel)                      \
1286         fixP->fx_r_type = rtype;                \
1287       break
1288
1289       FIX_PCREL (BFD_RELOC_8);
1290       FIX_PCREL (BFD_RELOC_16);
1291       FIX_PCREL (BFD_RELOC_32);
1292       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0);
1293       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1);
1294       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_LO);
1295       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_LO);
1296       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HI);
1297       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HI);
1298       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HA);
1299       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HA);
1300
1301 #undef FIX_PCREL
1302
1303     default:
1304       /* Do nothing */
1305       break;
1306     }
1307
1308   if (fixP->fx_addsy != NULL)
1309     {
1310 #ifdef OBJ_ELF
1311       switch (fixP->fx_r_type)
1312         {
1313         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD:
1314         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD:
1315         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE:
1316         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE:
1317         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_LO:
1318         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_LO:
1319         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_LO:
1320         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_LO:
1321         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HI:
1322         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HI:
1323         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HI:
1324         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HI:
1325         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HA:
1326         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HA:
1327         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HA:
1328         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HA:
1329         case BFD_RELOC_TILEPRO_TLS_DTPMOD32:
1330         case BFD_RELOC_TILEPRO_TLS_DTPOFF32:
1331         case BFD_RELOC_TILEPRO_TLS_TPOFF32:
1332           S_SET_THREAD_LOCAL (fixP->fx_addsy);
1333           break;
1334
1335         default:
1336           /* Do nothing */
1337           break;
1338         }
1339 #endif
1340       return;
1341     }
1342
1343   /* Apply lo16, hi16, ha16, etc. munging. */
1344   switch (fixP->fx_r_type)
1345     {
1346     case BFD_RELOC_TILEPRO_IMM16_X0_GOT:
1347     case BFD_RELOC_TILEPRO_IMM16_X1_GOT:
1348       *valP = value = apply_special_operator (O_got, value);
1349       break;
1350
1351     case BFD_RELOC_TILEPRO_IMM16_X0_GOT_LO:
1352     case BFD_RELOC_TILEPRO_IMM16_X1_GOT_LO:
1353       *valP = value = apply_special_operator (O_got_lo16, value);
1354       break;
1355
1356     case BFD_RELOC_TILEPRO_IMM16_X0_GOT_HI:
1357     case BFD_RELOC_TILEPRO_IMM16_X1_GOT_HI:
1358       *valP = value = apply_special_operator (O_got_hi16, value);
1359       break;
1360
1361     case BFD_RELOC_TILEPRO_IMM16_X0_GOT_HA:
1362     case BFD_RELOC_TILEPRO_IMM16_X1_GOT_HA:
1363       *valP = value = apply_special_operator (O_got_ha16, value);
1364       break;
1365
1366     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD:
1367     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD:
1368       *valP = value = apply_special_operator (O_tls_gd, value);
1369       break;
1370
1371     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE:
1372     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE:
1373       *valP = value = apply_special_operator (O_tls_ie, value);
1374       break;
1375
1376     case BFD_RELOC_LO16:
1377     case BFD_RELOC_TILEPRO_IMM16_X0_LO:
1378     case BFD_RELOC_TILEPRO_IMM16_X1_LO:
1379     case BFD_RELOC_TILEPRO_IMM16_X0_LO_PCREL:
1380     case BFD_RELOC_TILEPRO_IMM16_X1_LO_PCREL:
1381       *valP = value = apply_special_operator (O_lo16, value);
1382       break;
1383
1384     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_LO:
1385     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_LO:
1386       *valP = value = apply_special_operator (O_tls_gd_lo16, value);
1387       break;
1388
1389     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_LO:
1390     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_LO:
1391       *valP = value = apply_special_operator (O_tls_ie_lo16, value);
1392       break;
1393
1394     case BFD_RELOC_HI16:
1395     case BFD_RELOC_TILEPRO_IMM16_X0_HI:
1396     case BFD_RELOC_TILEPRO_IMM16_X1_HI:
1397     case BFD_RELOC_TILEPRO_IMM16_X0_HI_PCREL:
1398     case BFD_RELOC_TILEPRO_IMM16_X1_HI_PCREL:
1399       *valP = value = apply_special_operator (O_hi16, value);
1400       break;
1401
1402     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HI:
1403     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HI:
1404       *valP = value = apply_special_operator (O_tls_gd_hi16, value);
1405       break;
1406
1407     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HI:
1408     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HI:
1409       *valP = value = apply_special_operator (O_tls_ie_hi16, value);
1410       break;
1411
1412     case BFD_RELOC_HI16_S:
1413     case BFD_RELOC_TILEPRO_IMM16_X0_HA:
1414     case BFD_RELOC_TILEPRO_IMM16_X1_HA:
1415     case BFD_RELOC_TILEPRO_IMM16_X0_HA_PCREL:
1416     case BFD_RELOC_TILEPRO_IMM16_X1_HA_PCREL:
1417       *valP = value = apply_special_operator (O_ha16, value);
1418       break;
1419
1420     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HA:
1421     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HA:
1422       *valP = value = apply_special_operator (O_tls_gd_ha16, value);
1423       break;
1424
1425     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HA:
1426     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HA:
1427       *valP = value = apply_special_operator (O_tls_ie_ha16, value);
1428       break;
1429
1430     default:
1431       /* Do nothing  */
1432       break;
1433     }
1434
1435   p = fixP->fx_frag->fr_literal + fixP->fx_where;
1436
1437   operand = fixP->tc_fix_data;
1438   if (operand != NULL)
1439     {
1440       /* It's an instruction operand.  */
1441       tilepro_bundle_bits bits =
1442         insert_operand (0, operand, value, fixP->fx_file, fixP->fx_line);
1443
1444       /* Note that we might either be writing out bits for a bundle or a
1445          static network instruction, which are different sizes, so it's
1446          important to stop touching memory once we run out of bits.  ORing in
1447          values is OK since we know the existing bits for this operand are
1448          zero.  */
1449       for (; bits != 0; bits >>= 8)
1450         *p++ |= (char)bits;
1451     }
1452   else
1453     {
1454       /* Some other kind of relocation.  */
1455       switch (fixP->fx_r_type)
1456         {
1457         case BFD_RELOC_8:
1458         case BFD_RELOC_8_PCREL:
1459           md_number_to_chars (p, value, 1);
1460           break;
1461
1462         case BFD_RELOC_16:
1463         case BFD_RELOC_16_PCREL:
1464           md_number_to_chars (p, value, 2);
1465           break;
1466
1467         case BFD_RELOC_32:
1468         case BFD_RELOC_32_PCREL:
1469           md_number_to_chars (p, value, 4);
1470           break;
1471
1472         default:
1473           /* Leave it for the linker.  */
1474           return;
1475         }
1476     }
1477
1478   fixP->fx_done = 1;
1479 }
1480
1481
1482 /* Generate the BFD reloc to be stuck in the object file from the
1483    fixup used internally in the assembler.  */
1484
1485 arelent *
1486 tc_gen_reloc (asection *sec ATTRIBUTE_UNUSED, fixS *fixp)
1487 {
1488   arelent *reloc;
1489
1490   reloc = (arelent *) xmalloc (sizeof (arelent));
1491   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1492   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1493   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1494
1495   /* Make sure none of our internal relocations make it this far.
1496      They'd better have been fully resolved by this point.  */
1497   gas_assert ((int) fixp->fx_r_type > 0);
1498
1499   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1500   if (reloc->howto == NULL)
1501     {
1502       as_bad_where (fixp->fx_file, fixp->fx_line,
1503                     _("cannot represent `%s' relocation in object file"),
1504                     bfd_get_reloc_code_name (fixp->fx_r_type));
1505       return NULL;
1506     }
1507
1508   if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
1509     {
1510       as_fatal (_("internal error? cannot generate `%s' relocation (%d, %d)"),
1511                 bfd_get_reloc_code_name (fixp->fx_r_type),
1512                 fixp->fx_pcrel, reloc->howto->pc_relative);
1513     }
1514   gas_assert (!fixp->fx_pcrel == !reloc->howto->pc_relative);
1515
1516   reloc->addend = fixp->fx_offset;
1517
1518   return reloc;
1519 }
1520
1521
1522 /* The location from which a PC relative jump should be calculated,
1523    given a PC relative reloc.  */
1524
1525 long
1526 md_pcrel_from (fixS *fixP)
1527 {
1528   return fixP->fx_frag->fr_address + fixP->fx_where;
1529 }
1530
1531
1532 /* Return 1 if it's OK to adjust a reloc by replacing the symbol with
1533    a section symbol plus some offset.  */
1534 int
1535 tilepro_fix_adjustable (fixS *fix)
1536 {
1537   /* Prevent all adjustments to global symbols  */
1538   if (S_IS_EXTERNAL (fix->fx_addsy) || S_IS_WEAK (fix->fx_addsy))
1539     return 0;
1540
1541   return 1;
1542 }
1543
1544
1545 int
1546 tilepro_unrecognized_line (int ch)
1547 {
1548   switch (ch)
1549     {
1550     case '{':
1551       if (inside_bundle)
1552         {
1553           as_bad (_("Found '{' when already bundling."));
1554         }
1555       else
1556         {
1557           inside_bundle = 1;
1558           current_bundle_index = 0;
1559         }
1560       return 1;
1561
1562     case '}':
1563       if (!inside_bundle)
1564         {
1565           as_bad (_("Found '}' when not bundling."));
1566         }
1567       else
1568         {
1569           tilepro_flush_bundle ();
1570         }
1571
1572       /* Allow '{' to follow on the same line.  We also allow ";;", but that
1573          happens automatically because ';' is an end of line marker.  */
1574       SKIP_WHITESPACE ();
1575       if (input_line_pointer[0] == '{')
1576         {
1577           input_line_pointer++;
1578           return tilepro_unrecognized_line ('{');
1579         }
1580
1581       demand_empty_rest_of_line ();
1582       return 1;
1583
1584     default:
1585       break;
1586     }
1587
1588   /* Not a valid line.  */
1589   return 0;
1590 }
1591
1592
1593 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
1594    of an rs_align_code fragment.  */
1595
1596 void
1597 tilepro_handle_align (fragS *fragp)
1598 {
1599   int bytes, fix;
1600   char *p;
1601
1602   if (fragp->fr_type != rs_align_code)
1603     return;
1604
1605   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
1606   p = fragp->fr_literal + fragp->fr_fix;
1607   fix = 0;
1608
1609   /* Determine the bits for NOP.  */
1610   const struct tilepro_opcode *nop_opcode =
1611     &tilepro_opcodes[TILEPRO_OPC_NOP];
1612   tilepro_bundle_bits nop =
1613     (  nop_opcode->fixed_bit_values[TILEPRO_PIPELINE_X0]
1614        | nop_opcode->fixed_bit_values[TILEPRO_PIPELINE_X1]);
1615
1616   if ((bytes & (TILEPRO_BUNDLE_SIZE_IN_BYTES - 1)) != 0)
1617     {
1618       fix = bytes & (TILEPRO_BUNDLE_SIZE_IN_BYTES - 1);
1619       memset (p, 0, fix);
1620       p += fix;
1621       bytes -= fix;
1622     }
1623
1624   number_to_chars_littleendian (p, (unsigned int)nop, 4);
1625   number_to_chars_littleendian (p + 4, (unsigned int)(nop >> 32), 4);
1626   fragp->fr_fix += fix;
1627   fragp->fr_var = TILEPRO_BUNDLE_SIZE_IN_BYTES;
1628 }
1629
1630 /* Standard calling conventions leave the CFA at SP on entry.  */
1631 void
1632 tilepro_cfi_frame_initial_instructions (void)
1633 {
1634   cfi_add_CFA_def_cfa_register (54);
1635 }
1636
1637 int
1638 tc_tilepro_regname_to_dw2regnum (char *regname)
1639 {
1640   int i;
1641
1642   for (i = 0; i < TILEPRO_NUM_REGISTERS; i++)
1643     {
1644       if (!strcmp (regname, tilepro_register_names[i]))
1645         return i;
1646     }
1647
1648   return -1;
1649 }