OSDN Git Service

* Makefile.am (ALL_MACHINES): Add cpu-tilegx.lo and cpu-tilepro.lo.
[pf3gnuchains/pf3gnuchains4x.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                   if (require_symbol)
564                     {
565                       as_bad (_("Operator may only be applied to symbols."));
566                     }
567
568                   /* HACK: We used X_md to mark this symbol as a fake wrapper
569                      around a real expression. To unwrap it, we just grab its
570                      value here.  */
571                   operand_exp = &operand_exp->X_add_symbol->sy_value;
572                 }
573               else
574                 {
575                   /* The value of this expression is an actual symbol, so
576                      turn that into an expression.  */
577                   memset (&subexp, 0, sizeof subexp);
578                   subexp.X_op = O_symbol;
579                   subexp.X_add_symbol = operand_exp->X_add_symbol;
580                   operand_exp = &subexp;
581                 }
582             }
583
584           /* Create a fixup to handle this later. */
585           fixP = fix_new_exp (frag_now,
586                               bundle_start - frag_now->fr_literal,
587                               (operand->num_bits + 7) >> 3,
588                               operand_exp,
589                               is_pc_relative,
590                               reloc);
591           fixP->tc_fix_data = operand;
592
593           /* Don't do overflow checking if we are applying a function like
594              ha16.  */
595           fixP->fx_no_overflow |= use_subexp;
596         }
597     }
598   return bits;
599 }
600
601
602 /* Detects and complains if two instructions in current_bundle write
603    to the same register, either implicitly or explicitly, or if a
604    read-only register is written.  */
605 static void
606 check_illegal_reg_writes (void)
607 {
608   BFD_HOST_U_64_BIT all_regs_written = 0;
609   int j;
610
611   for (j = 0; j < current_bundle_index; j++)
612     {
613       const struct tilepro_instruction *instr = &current_bundle[j];
614       int k;
615       BFD_HOST_U_64_BIT regs =
616         ((BFD_HOST_U_64_BIT)1) << instr->opcode->implicitly_written_register;
617       BFD_HOST_U_64_BIT conflict;
618
619       for (k = 0; k < instr->opcode->num_operands; k++)
620         {
621           const struct tilepro_operand *operand =
622             &tilepro_operands[instr->opcode->operands[instr->pipe][k]];
623
624           if (operand->is_dest_reg)
625             {
626               int regno = instr->operand_values[k].X_add_number;
627               BFD_HOST_U_64_BIT mask = ((BFD_HOST_U_64_BIT)1) << regno;
628
629               if ((mask & (  (((BFD_HOST_U_64_BIT)1) << TREG_IDN1)
630                              | (((BFD_HOST_U_64_BIT)1) << TREG_UDN1)
631                              | (((BFD_HOST_U_64_BIT)1) << TREG_UDN2)
632                              | (((BFD_HOST_U_64_BIT)1) << TREG_UDN3))) != 0
633                   && !allow_suspicious_bundles)
634                 {
635                   as_bad (_("Writes to register '%s' are not allowed."),
636                           tilepro_register_names[regno]);
637                 }
638
639               regs |= mask;
640             }
641         }
642
643       /* Writing to the zero register doesn't count.  */
644       regs &= ~(((BFD_HOST_U_64_BIT)1) << TREG_ZERO);
645
646       conflict = all_regs_written & regs;
647       if (conflict != 0 && !allow_suspicious_bundles)
648         {
649           /* Find which register caused the conflict.  */
650           const char *conflicting_reg_name = "???";
651           int i;
652
653           for (i = 0; i < TILEPRO_NUM_REGISTERS; i++)
654             {
655               if (((conflict >> i) & 1) != 0)
656                 {
657                   conflicting_reg_name = tilepro_register_names[i];
658                   break;
659                 }
660             }
661
662           as_bad (_("Two instructions in the same bundle both write "
663                     "to register %s, which is not allowed."),
664                   conflicting_reg_name);
665         }
666
667       all_regs_written |= regs;
668     }
669 }
670
671
672 static void
673 tilepro_flush_bundle (void)
674 {
675   unsigned i;
676   int j, addr_mod;
677   unsigned compatible_pipes;
678   const struct bundle_template *match;
679   char *f;
680
681   inside_bundle = 0;
682
683   switch (current_bundle_index)
684     {
685     case 0:
686       /* No instructions.  */
687       return;
688     case 1:
689       if (current_bundle[0].opcode->can_bundle)
690         {
691           /* Simplify later logic by adding an explicit fnop.  */
692           prepend_nop_to_bundle (TILEPRO_OPC_FNOP);
693         }
694       else
695         {
696           /* This instruction cannot be bundled with anything else.
697              Prepend an explicit 'nop', rather than an 'fnop', because
698              fnops can be replaced by later binary-processing tools
699              while nops cannot.  */
700           prepend_nop_to_bundle (TILEPRO_OPC_NOP);
701         }
702       break;
703     default:
704       if (!allow_suspicious_bundles)
705         {
706           /* Make sure all instructions can be bundled with other
707              instructions.  */
708           const struct tilepro_opcode *cannot_bundle = NULL;
709           bfd_boolean seen_non_nop = FALSE;
710
711           for (j = 0; j < current_bundle_index; j++)
712             {
713               const struct tilepro_opcode *op = current_bundle[j].opcode;
714
715               if (!op->can_bundle && cannot_bundle == NULL)
716                 cannot_bundle = op;
717               else if (op->mnemonic != TILEPRO_OPC_NOP
718                        && op->mnemonic != TILEPRO_OPC_INFO
719                        && op->mnemonic != TILEPRO_OPC_INFOL)
720                 seen_non_nop = TRUE;
721             }
722
723           if (cannot_bundle != NULL && seen_non_nop)
724             {
725               current_bundle_index = 0;
726               as_bad (_("'%s' may not be bundled with other instructions."),
727                       cannot_bundle->name);
728               return;
729             }
730         }
731       break;
732     }
733
734   compatible_pipes =
735     BUNDLE_TEMPLATE_MASK(current_bundle[0].opcode->pipes,
736                          current_bundle[1].opcode->pipes,
737                          (current_bundle_index == 3
738                           ? current_bundle[2].opcode->pipes
739                           : (1 << NO_PIPELINE)));
740
741   /* Find a template that works, if any.  */
742   match = NULL;
743   for (i = 0; i < sizeof bundle_templates / sizeof bundle_templates[0]; i++)
744     {
745       const struct bundle_template *b = &bundle_templates[i];
746       if ((b->pipe_mask & compatible_pipes) == b->pipe_mask)
747         {
748           match = b;
749           break;
750         }
751     }
752
753   if (match == NULL)
754     {
755       current_bundle_index = 0;
756       as_bad (_("Invalid combination of instructions for bundle."));
757       return;
758     }
759
760   /* If the section seems to have no alignment set yet, go ahead and
761      make it large enough to hold code.  */
762   if (bfd_get_section_alignment (stdoutput, now_seg) == 0)
763     bfd_set_section_alignment (stdoutput, now_seg,
764                                TILEPRO_LOG2_BUNDLE_ALIGNMENT_IN_BYTES);
765
766   for (j = 0; j < current_bundle_index; j++)
767     current_bundle[j].pipe = match->pipe[j];
768
769   if (current_bundle_index == 2 && !tilepro_is_x_pipeline(match->pipe[0]))
770     {
771       /* We are in Y mode with only two instructions, so add an FNOP.  */
772       prepend_nop_to_bundle (TILEPRO_OPC_FNOP);
773
774       /* Figure out what pipe the fnop must be in via arithmetic.
775        * p0 + p1 + p2 must sum to the sum of TILEPRO_PIPELINE_Y[012].  */
776       current_bundle[0].pipe =
777         (tilepro_pipeline)((TILEPRO_PIPELINE_Y0
778                             + TILEPRO_PIPELINE_Y1
779                             + TILEPRO_PIPELINE_Y2) -
780                            (current_bundle[1].pipe + current_bundle[2].pipe));
781     }
782
783   check_illegal_reg_writes ();
784
785   f = frag_more (TILEPRO_BUNDLE_SIZE_IN_BYTES);
786
787   /* Check to see if this bundle is at an offset that is a multiple of 8-bytes
788      from the start of the frag.  */
789   addr_mod = frag_now_fix () & (TILEPRO_BUNDLE_ALIGNMENT_IN_BYTES - 1);
790   if (frag_now->has_code && frag_now->insn_addr != addr_mod)
791     as_bad (_("instruction address is not a multiple of 8"));
792   frag_now->insn_addr = addr_mod;
793   frag_now->has_code = 1;
794
795   tilepro_bundle_bits bits = 0;
796   for (j = 0; j < current_bundle_index; j++)
797     {
798       struct tilepro_instruction *instr = &current_bundle[j];
799       tilepro_pipeline pipeline = instr->pipe;
800       const struct tilepro_opcode *opcode = instr->opcode;
801
802       bits |= emit_tilepro_instruction (opcode->fixed_bit_values[pipeline],
803                                         opcode->num_operands,
804                                         &opcode->operands[pipeline][0],
805                                         instr->operand_values,
806                                         f);
807     }
808
809   number_to_chars_littleendian (f, (unsigned int)bits, 4);
810   number_to_chars_littleendian (f + 4, (unsigned int)(bits >> 32), 4);
811   current_bundle_index = 0;
812
813   /* Emit DWARF2 debugging information.  */
814   dwarf2_emit_insn (TILEPRO_BUNDLE_SIZE_IN_BYTES);
815 }
816
817
818 /* Extend the expression parser to handle hi16(label), etc.
819    as well as SPR names when in the context of parsing an SPR.  */
820 int
821 tilepro_parse_name (char *name, expressionS *e, char *nextcharP)
822 {
823   operatorT op = O_illegal;
824
825   if (parsing_spr)
826     {
827       void *val = hash_find (spr_hash, name);
828       if (val == NULL)
829         return 0;
830
831       memset (e, 0, sizeof *e);
832       e->X_op = O_constant;
833       e->X_add_number = ((const struct tilepro_spr *)val)->number;
834       return 1;
835     }
836
837   if (*nextcharP != '(')
838     {
839       /* hi16, etc. not followed by a paren is just a label with that
840          name.  */
841       return 0;
842     }
843   else
844     {
845       /* Look up the operator in our table.  */
846       void *val = hash_find (special_operator_hash, name);
847       if (val == 0)
848         return 0;
849       op = (operatorT)(long)val;
850     }
851
852   /* Restore old '(' and skip it.  */
853   *input_line_pointer = '(';
854   ++input_line_pointer;
855
856   expression (e);
857
858   if (*input_line_pointer != ')')
859     {
860       as_bad (_("Missing ')'"));
861       *nextcharP = *input_line_pointer;
862       return 0;
863     }
864   /* Skip ')'.  */
865   ++input_line_pointer;
866
867   if (e->X_op == O_register || e->X_op == O_absent)
868     {
869       as_bad (_("Invalid expression."));
870       e->X_op = O_constant;
871       e->X_add_number = 0;
872     }
873   else
874     {
875       /* Wrap subexpression with a unary operator.  */
876       symbolS *sym = make_expr_symbol (e);
877
878       if (sym != e->X_add_symbol)
879         {
880           /* HACK: mark this symbol as a temporary wrapper around a proper
881              expression, so we can unwrap it later once we have communicated
882              the relocation type.  */
883           sym->sy_value.X_md = 1;
884         }
885
886       memset (e, 0, sizeof *e);
887       e->X_op = op;
888       e->X_add_symbol = sym;
889       e->X_add_number = 0;
890     }
891
892   *nextcharP = *input_line_pointer;
893   return 1;
894 }
895
896
897 /* Parses an expression which must be a register name.  */
898
899 static void
900 parse_reg_expression (expressionS* expression)
901 {
902   /* Zero everything to make sure we don't miss any flags.  */
903   memset (expression, 0, sizeof *expression);
904
905   char* regname = input_line_pointer;
906   char terminating_char = get_symbol_end ();
907
908   void* pval = hash_find (main_reg_hash, regname);
909
910   if (pval == NULL)
911     as_bad (_("Expected register, got '%s'."), regname);
912
913   int regno_and_flags = (int)(size_t)pval;
914   int regno = EXTRACT_REGNO(regno_and_flags);
915
916   if ((regno_and_flags & NONCANONICAL_REG_NAME_FLAG)
917       && require_canonical_reg_names)
918     as_warn (_("Found use of non-canonical register name %s; "
919                "use %s instead."),
920                regname, tilepro_register_names[regno]);
921
922   /* Restore the old character following the register name.  */
923   *input_line_pointer = terminating_char;
924
925   /* Fill in the expression fields to indicate it's a register.  */
926   expression->X_op = O_register;
927   expression->X_add_number = regno;
928 }
929
930
931 /* Parses and type-checks comma-separated operands in input_line_pointer.  */
932 static void
933 parse_operands (const char *opcode_name,
934                 const unsigned char *operands,
935                 int num_operands,
936                 expressionS *operand_values)
937 {
938   int i;
939
940   memset (operand_values, 0, num_operands * sizeof operand_values[0]);
941
942   SKIP_WHITESPACE ();
943   for (i = 0; i < num_operands; i++)
944     {
945       tilepro_operand_type type = tilepro_operands[operands[i]].type;
946
947       SKIP_WHITESPACE ();
948
949       if (type == TILEPRO_OP_TYPE_REGISTER)
950         {
951           parse_reg_expression (&operand_values[i]);
952         }
953       else if (*input_line_pointer == '}')
954         {
955           operand_values[i].X_op = O_absent;
956         }
957       else if (type == TILEPRO_OP_TYPE_SPR)
958         {
959           /* Modify the expression parser to add SPRs to the namespace.  */
960           parsing_spr = 1;
961           expression (&operand_values[i]);
962           parsing_spr = 0;
963         }
964       else
965         {
966           expression (&operand_values[i]);
967         }
968
969       SKIP_WHITESPACE ();
970
971       if (i + 1 < num_operands)
972         {
973           int separator = (unsigned char)*input_line_pointer++;
974
975           if (is_end_of_line[separator] || (separator == '}'))
976             {
977               as_bad (_("Too few operands to '%s'."), opcode_name);
978               return;
979             }
980           else if (separator != ',')
981             {
982               as_bad (_("Unexpected character '%c' after operand %d to %s."),
983                       (char)separator, i + 1, opcode_name);
984               return;
985             }
986         }
987
988       /* Arbitrarily use the first valid pipe to get the operand type,
989          since they are all the same.  */
990       switch (tilepro_operands[operands[i]].type)
991         {
992         case TILEPRO_OP_TYPE_REGISTER:
993           /* Handled in parse_reg_expression already.  */
994           break;
995         case TILEPRO_OP_TYPE_SPR:
996           /* Fall through  */
997         case TILEPRO_OP_TYPE_IMMEDIATE:
998           /* Fall through  */
999         case TILEPRO_OP_TYPE_ADDRESS:
1000           if (   operand_values[i].X_op == O_register
1001               || operand_values[i].X_op == O_illegal
1002               || operand_values[i].X_op == O_absent)
1003             as_bad (_("Expected immediate expression"));
1004           break;
1005         default:
1006           abort ();
1007         }
1008     }
1009
1010   if (!is_end_of_line[(unsigned char)*input_line_pointer])
1011     {
1012       switch (*input_line_pointer)
1013         {
1014         case '}':
1015           if (!inside_bundle)
1016             as_bad (_("Found '}' when not bundling."));
1017           ++input_line_pointer;
1018           inside_bundle = 0;
1019           demand_empty_rest_of_line ();
1020           break;
1021
1022         case ',':
1023           as_bad (_("Too many operands"));
1024           break;
1025
1026         default:
1027           /* Use default error for unrecognized garbage.  */
1028           demand_empty_rest_of_line ();
1029           break;
1030         }
1031     }
1032 }
1033
1034
1035 /* This is the guts of the machine-dependent assembler.  STR points to a
1036    machine dependent instruction.  This function is supposed to emit
1037    the frags/bytes it assembles to.  */
1038 void
1039 md_assemble (char *str)
1040 {
1041   char old_char;
1042   size_t opname_len;
1043   char *old_input_line_pointer;
1044   const struct tilepro_opcode *op;
1045   int first_pipe;
1046
1047   /* Split off the opcode and look it up.  */
1048   opname_len = strcspn (str, " {}");
1049   old_char = str[opname_len];
1050   str[opname_len] = '\0';
1051
1052   op = hash_find(op_hash, str);
1053   str[opname_len] = old_char;
1054   if (op == NULL)
1055     {
1056       as_bad (_("Unknown opcode `%.*s'."), (int)opname_len, str);
1057       return;
1058     }
1059
1060   /* Prepare to parse the operands.  */
1061   old_input_line_pointer = input_line_pointer;
1062   input_line_pointer = str + opname_len;
1063   SKIP_WHITESPACE ();
1064
1065   if (current_bundle_index == TILEPRO_MAX_INSTRUCTIONS_PER_BUNDLE)
1066     {
1067       as_bad (_("Too many instructions for bundle."));
1068       tilepro_flush_bundle ();
1069     }
1070
1071   /* Make sure we have room for the upcoming bundle before we
1072      create any fixups. Otherwise if we have to switch to a new
1073      frag the fixup dot_value fields will be wrong.  */
1074   frag_grow (TILEPRO_BUNDLE_SIZE_IN_BYTES);
1075
1076   /* Find a valid pipe for this opcode. */
1077   for (first_pipe = 0; (op->pipes & (1 << first_pipe)) == 0; first_pipe++)
1078     ;
1079
1080   /* Call the function that assembles this instruction.  */
1081   current_bundle[current_bundle_index].opcode = op;
1082   parse_operands (op->name,
1083                   &op->operands[first_pipe][0],
1084                   op->num_operands,
1085                   current_bundle[current_bundle_index].operand_values);
1086   ++current_bundle_index;
1087
1088   /* Restore the saved value of input_line_pointer.  */
1089   input_line_pointer = old_input_line_pointer;
1090
1091   /* If we weren't inside curly braces, go ahead and emit
1092      this lone instruction as a bundle right now.  */
1093   if (!inside_bundle)
1094     tilepro_flush_bundle ();
1095 }
1096
1097 static void
1098 s_require_canonical_reg_names (int require)
1099 {
1100   demand_empty_rest_of_line ();
1101   require_canonical_reg_names = require;
1102 }
1103
1104 static void
1105 s_allow_suspicious_bundles (int allow)
1106 {
1107   demand_empty_rest_of_line ();
1108   allow_suspicious_bundles = allow;
1109 }
1110
1111 const pseudo_typeS md_pseudo_table[] =
1112 {
1113   {"align", s_align_bytes, 0},  /* Defaulting is invalid (0).  */
1114   {"word", cons, 4},
1115   {"require_canonical_reg_names", s_require_canonical_reg_names, 1 },
1116   {"no_require_canonical_reg_names", s_require_canonical_reg_names, 0 },
1117   {"allow_suspicious_bundles", s_allow_suspicious_bundles, 1 },
1118   {"no_allow_suspicious_bundles", s_allow_suspicious_bundles, 0 },
1119   { NULL, 0, 0 }
1120 };
1121
1122 /* Equal to MAX_PRECISION in atof-ieee.c  */
1123 #define MAX_LITTLENUMS 6
1124
1125 /* Turn the string pointed to by litP into a floating point constant
1126    of type TYPE, and emit the appropriate bytes.  The number of
1127    LITTLENUMS emitted is stored in *SIZEP.  An error message is
1128    returned, or NULL on OK.  */
1129
1130 char *
1131 md_atof (int type, char *litP, int *sizeP)
1132 {
1133   int prec;
1134   LITTLENUM_TYPE words[MAX_LITTLENUMS];
1135   LITTLENUM_TYPE *wordP;
1136   char *t;
1137
1138   switch (type)
1139     {
1140     case 'f':
1141     case 'F':
1142       prec = 2;
1143       break;
1144
1145     case 'd':
1146     case 'D':
1147       prec = 4;
1148       break;
1149
1150     default:
1151       *sizeP = 0;
1152       return _("Bad call to md_atof ()");
1153     }
1154   t = atof_ieee (input_line_pointer, type, words);
1155   if (t)
1156     input_line_pointer = t;
1157
1158   *sizeP = prec * sizeof (LITTLENUM_TYPE);
1159   /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
1160      the bigendian 386.  */
1161   for (wordP = words + prec - 1; prec--;)
1162     {
1163       md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
1164       litP += sizeof (LITTLENUM_TYPE);
1165     }
1166   return 0;
1167 }
1168
1169
1170 /* We have no need to default values of symbols.  */
1171
1172 symbolS *
1173 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1174 {
1175   return NULL;
1176 }
1177
1178
1179 void
1180 tilepro_cons_fix_new (fragS *frag,
1181                       int where,
1182                       int nbytes,
1183                       expressionS *exp)
1184 {
1185   expressionS subexp;
1186   bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
1187   int no_overflow = 0;
1188   fixS *fixP;
1189
1190   /* See if it's one of our special functions.  */
1191   switch (exp->X_op)
1192     {
1193     case O_lo16:
1194       reloc = BFD_RELOC_LO16;
1195       no_overflow = 1;
1196       break;
1197     case O_hi16:
1198       reloc = BFD_RELOC_HI16;
1199       no_overflow = 1;
1200       break;
1201     case O_ha16:
1202       reloc = BFD_RELOC_HI16_S;
1203       no_overflow = 1;
1204       break;
1205
1206     default:
1207       /* Do nothing.  */
1208       break;
1209     }
1210
1211   if (reloc != BFD_RELOC_NONE)
1212     {
1213       if (nbytes != 2)
1214         {
1215           as_bad (_("This operator only produces two byte values."));
1216           nbytes = 2;
1217         }
1218
1219       memset (&subexp, 0, sizeof subexp);
1220       subexp.X_op = O_symbol;
1221       subexp.X_add_symbol = exp->X_add_symbol;
1222       exp = &subexp;
1223     }
1224   else
1225     {
1226       switch (nbytes)
1227         {
1228         case 1:
1229           reloc = BFD_RELOC_8;
1230           break;
1231         case 2:
1232           reloc = BFD_RELOC_16;
1233           break;
1234         case 4:
1235           reloc = BFD_RELOC_32;
1236           break;
1237         case 8:
1238           reloc = BFD_RELOC_64;
1239           break;
1240         default:
1241           as_bad (_("unsupported BFD relocation size %d"), nbytes);
1242           reloc = BFD_RELOC_32;
1243           break;
1244         }
1245     }
1246
1247   fixP = fix_new_exp (frag, where, nbytes, exp, 0, reloc);
1248   fixP->tc_fix_data = NULL;
1249   fixP->fx_no_overflow |= no_overflow;
1250 }
1251
1252
1253 void
1254 md_apply_fix (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
1255 {
1256   const struct tilepro_operand *operand;
1257   valueT value = *valP;
1258   char *p;
1259
1260   /* Leave these for the linker.  */
1261   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1262       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1263     return;
1264
1265   if (fixP->fx_subsy != (symbolS *) NULL)
1266     {
1267       /* We can't actually support subtracting a symbol.  */
1268       as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
1269     }
1270
1271   /* Correct relocation types for pc-relativeness.  */
1272   switch (fixP->fx_r_type)
1273     {
1274 #define FIX_PCREL(rtype)                        \
1275       case rtype:                               \
1276         if (fixP->fx_pcrel)                     \
1277           fixP->fx_r_type = rtype##_PCREL;      \
1278       break;                                    \
1279                                                 \
1280     case rtype##_PCREL:                         \
1281       if (!fixP->fx_pcrel)                      \
1282         fixP->fx_r_type = rtype;                \
1283       break
1284
1285       FIX_PCREL (BFD_RELOC_8);
1286       FIX_PCREL (BFD_RELOC_16);
1287       FIX_PCREL (BFD_RELOC_32);
1288       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0);
1289       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1);
1290       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_LO);
1291       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_LO);
1292       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HI);
1293       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HI);
1294       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X0_HA);
1295       FIX_PCREL (BFD_RELOC_TILEPRO_IMM16_X1_HA);
1296
1297 #undef FIX_PCREL
1298
1299     default:
1300       /* Do nothing */
1301       break;
1302     }
1303
1304   if (fixP->fx_addsy != NULL)
1305     {
1306 #ifdef OBJ_ELF
1307       switch (fixP->fx_r_type)
1308         {
1309         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD:
1310         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD:
1311         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE:
1312         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE:
1313         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_LO:
1314         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_LO:
1315         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_LO:
1316         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_LO:
1317         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HI:
1318         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HI:
1319         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HI:
1320         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HI:
1321         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HA:
1322         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HA:
1323         case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HA:
1324         case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HA:
1325         case BFD_RELOC_TILEPRO_TLS_DTPMOD32:
1326         case BFD_RELOC_TILEPRO_TLS_DTPOFF32:
1327         case BFD_RELOC_TILEPRO_TLS_TPOFF32:
1328           S_SET_THREAD_LOCAL (fixP->fx_addsy);
1329           break;
1330
1331         default:
1332           /* Do nothing */
1333           break;
1334         }
1335 #endif
1336       return;
1337     }
1338
1339   /* Apply lo16, hi16, ha16, etc. munging. */
1340   switch (fixP->fx_r_type)
1341     {
1342     case BFD_RELOC_TILEPRO_IMM16_X0_GOT:
1343     case BFD_RELOC_TILEPRO_IMM16_X1_GOT:
1344       *valP = value = apply_special_operator (O_got, value);
1345       break;
1346
1347     case BFD_RELOC_TILEPRO_IMM16_X0_GOT_LO:
1348     case BFD_RELOC_TILEPRO_IMM16_X1_GOT_LO:
1349       *valP = value = apply_special_operator (O_got_lo16, value);
1350       break;
1351
1352     case BFD_RELOC_TILEPRO_IMM16_X0_GOT_HI:
1353     case BFD_RELOC_TILEPRO_IMM16_X1_GOT_HI:
1354       *valP = value = apply_special_operator (O_got_hi16, value);
1355       break;
1356
1357     case BFD_RELOC_TILEPRO_IMM16_X0_GOT_HA:
1358     case BFD_RELOC_TILEPRO_IMM16_X1_GOT_HA:
1359       *valP = value = apply_special_operator (O_got_ha16, value);
1360       break;
1361
1362     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD:
1363     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD:
1364       *valP = value = apply_special_operator (O_tls_gd, value);
1365       break;
1366
1367     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE:
1368     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE:
1369       *valP = value = apply_special_operator (O_tls_ie, value);
1370       break;
1371
1372     case BFD_RELOC_LO16:
1373     case BFD_RELOC_TILEPRO_IMM16_X0_LO:
1374     case BFD_RELOC_TILEPRO_IMM16_X1_LO:
1375     case BFD_RELOC_TILEPRO_IMM16_X0_LO_PCREL:
1376     case BFD_RELOC_TILEPRO_IMM16_X1_LO_PCREL:
1377       *valP = value = apply_special_operator (O_lo16, value);
1378       break;
1379
1380     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_LO:
1381     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_LO:
1382       *valP = value = apply_special_operator (O_tls_gd_lo16, value);
1383       break;
1384
1385     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_LO:
1386     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_LO:
1387       *valP = value = apply_special_operator (O_tls_ie_lo16, value);
1388       break;
1389
1390     case BFD_RELOC_HI16:
1391     case BFD_RELOC_TILEPRO_IMM16_X0_HI:
1392     case BFD_RELOC_TILEPRO_IMM16_X1_HI:
1393     case BFD_RELOC_TILEPRO_IMM16_X0_HI_PCREL:
1394     case BFD_RELOC_TILEPRO_IMM16_X1_HI_PCREL:
1395       *valP = value = apply_special_operator (O_hi16, value);
1396       break;
1397
1398     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HI:
1399     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HI:
1400       *valP = value = apply_special_operator (O_tls_gd_hi16, value);
1401       break;
1402
1403     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HI:
1404     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HI:
1405       *valP = value = apply_special_operator (O_tls_ie_hi16, value);
1406       break;
1407
1408     case BFD_RELOC_HI16_S:
1409     case BFD_RELOC_TILEPRO_IMM16_X0_HA:
1410     case BFD_RELOC_TILEPRO_IMM16_X1_HA:
1411     case BFD_RELOC_TILEPRO_IMM16_X0_HA_PCREL:
1412     case BFD_RELOC_TILEPRO_IMM16_X1_HA_PCREL:
1413       *valP = value = apply_special_operator (O_ha16, value);
1414       break;
1415
1416     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_GD_HA:
1417     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_GD_HA:
1418       *valP = value = apply_special_operator (O_tls_gd_ha16, value);
1419       break;
1420
1421     case BFD_RELOC_TILEPRO_IMM16_X0_TLS_IE_HA:
1422     case BFD_RELOC_TILEPRO_IMM16_X1_TLS_IE_HA:
1423       *valP = value = apply_special_operator (O_tls_ie_ha16, value);
1424       break;
1425
1426     default:
1427       /* Do nothing  */
1428       break;
1429     }
1430
1431   p = fixP->fx_frag->fr_literal + fixP->fx_where;
1432
1433   operand = fixP->tc_fix_data;
1434   if (operand != NULL)
1435     {
1436       /* It's an instruction operand.  */
1437       tilepro_bundle_bits bits =
1438         insert_operand (0, operand, value, fixP->fx_file, fixP->fx_line);
1439
1440       /* Note that we might either be writing out bits for a bundle or a
1441          static network instruction, which are different sizes, so it's
1442          important to stop touching memory once we run out of bits.  ORing in
1443          values is OK since we know the existing bits for this operand are
1444          zero.  */
1445       for (; bits != 0; bits >>= 8)
1446         *p++ |= (char)bits;
1447     }
1448   else
1449     {
1450       /* Some other kind of relocation.  */
1451       switch (fixP->fx_r_type)
1452         {
1453         case BFD_RELOC_8:
1454         case BFD_RELOC_8_PCREL:
1455           md_number_to_chars (p, value, 1);
1456           break;
1457
1458         case BFD_RELOC_16:
1459         case BFD_RELOC_16_PCREL:
1460           md_number_to_chars (p, value, 2);
1461           break;
1462
1463         case BFD_RELOC_32:
1464         case BFD_RELOC_32_PCREL:
1465           md_number_to_chars (p, value, 4);
1466           break;
1467
1468         default:
1469           /* Leave it for the linker.  */
1470           return;
1471         }
1472     }
1473
1474   fixP->fx_done = 1;
1475 }
1476
1477
1478 /* Generate the BFD reloc to be stuck in the object file from the
1479    fixup used internally in the assembler.  */
1480
1481 arelent *
1482 tc_gen_reloc (asection *sec ATTRIBUTE_UNUSED, fixS *fixp)
1483 {
1484   arelent *reloc;
1485
1486   reloc = (arelent *) xmalloc (sizeof (arelent));
1487   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1488   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1489   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1490
1491   /* Make sure none of our internal relocations make it this far.
1492      They'd better have been fully resolved by this point.  */
1493   gas_assert ((int) fixp->fx_r_type > 0);
1494
1495   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1496   if (reloc->howto == NULL)
1497     {
1498       as_bad_where (fixp->fx_file, fixp->fx_line,
1499                     _("cannot represent `%s' relocation in object file"),
1500                     bfd_get_reloc_code_name (fixp->fx_r_type));
1501       return NULL;
1502     }
1503
1504   if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
1505     {
1506       as_fatal (_("internal error? cannot generate `%s' relocation (%d, %d)"),
1507                 bfd_get_reloc_code_name (fixp->fx_r_type),
1508                 fixp->fx_pcrel, reloc->howto->pc_relative);
1509     }
1510   gas_assert (!fixp->fx_pcrel == !reloc->howto->pc_relative);
1511
1512   reloc->addend = fixp->fx_offset;
1513
1514   return reloc;
1515 }
1516
1517
1518 /* The location from which a PC relative jump should be calculated,
1519    given a PC relative reloc.  */
1520
1521 long
1522 md_pcrel_from (fixS *fixP)
1523 {
1524   return fixP->fx_frag->fr_address + fixP->fx_where;
1525 }
1526
1527
1528 /* Return 1 if it's OK to adjust a reloc by replacing the symbol with
1529    a section symbol plus some offset.  */
1530 int
1531 tilepro_fix_adjustable (fixS *fix)
1532 {
1533   /* Prevent all adjustments to global symbols  */
1534   if (S_IS_EXTERNAL (fix->fx_addsy) || S_IS_WEAK (fix->fx_addsy))
1535     return 0;
1536
1537   return 1;
1538 }
1539
1540
1541 int
1542 tilepro_unrecognized_line (int ch)
1543 {
1544   switch (ch)
1545     {
1546     case '{':
1547       if (inside_bundle)
1548         {
1549           as_bad (_("Found '{' when already bundling."));
1550         }
1551       else
1552         {
1553           inside_bundle = 1;
1554           current_bundle_index = 0;
1555         }
1556       return 1;
1557
1558     case '}':
1559       if (!inside_bundle)
1560         {
1561           as_bad (_("Found '}' when not bundling."));
1562         }
1563       else
1564         {
1565           tilepro_flush_bundle ();
1566         }
1567
1568       /* Allow '{' to follow on the same line.  We also allow ";;", but that
1569          happens automatically because ';' is an end of line marker.  */
1570       SKIP_WHITESPACE ();
1571       if (input_line_pointer[0] == '{')
1572         {
1573           input_line_pointer++;
1574           return tilepro_unrecognized_line ('{');
1575         }
1576
1577       demand_empty_rest_of_line ();
1578       return 1;
1579
1580     default:
1581       break;
1582     }
1583
1584   /* Not a valid line.  */
1585   return 0;
1586 }
1587
1588
1589 /* This is called from HANDLE_ALIGN in write.c.  Fill in the contents
1590    of an rs_align_code fragment.  */
1591
1592 void
1593 tilepro_handle_align (fragS *fragp)
1594 {
1595   int bytes, fix;
1596   char *p;
1597
1598   if (fragp->fr_type != rs_align_code)
1599     return;
1600
1601   bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
1602   p = fragp->fr_literal + fragp->fr_fix;
1603   fix = 0;
1604
1605   /* Determine the bits for NOP.  */
1606   const struct tilepro_opcode *nop_opcode =
1607     &tilepro_opcodes[TILEPRO_OPC_NOP];
1608   tilepro_bundle_bits nop =
1609     (  nop_opcode->fixed_bit_values[TILEPRO_PIPELINE_X0]
1610        | nop_opcode->fixed_bit_values[TILEPRO_PIPELINE_X1]);
1611
1612   if ((bytes & (TILEPRO_BUNDLE_SIZE_IN_BYTES - 1)) != 0)
1613     {
1614       fix = bytes & (TILEPRO_BUNDLE_SIZE_IN_BYTES - 1);
1615       memset (p, 0, fix);
1616       p += fix;
1617       bytes -= fix;
1618     }
1619
1620   number_to_chars_littleendian (p, (unsigned int)nop, 4);
1621   number_to_chars_littleendian (p + 4, (unsigned int)(nop >> 32), 4);
1622   fragp->fr_fix += fix;
1623   fragp->fr_var = TILEPRO_BUNDLE_SIZE_IN_BYTES;
1624 }
1625
1626 /* Standard calling conventions leave the CFA at SP on entry.  */
1627 void
1628 tilepro_cfi_frame_initial_instructions (void)
1629 {
1630   cfi_add_CFA_def_cfa_register (54);
1631 }
1632
1633 int
1634 tc_tilepro_regname_to_dw2regnum (char *regname)
1635 {
1636   int i;
1637
1638   for (i = 0; i < TILEPRO_NUM_REGISTERS; i++)
1639     {
1640       if (!strcmp (regname, tilepro_register_names[i]))
1641         return i;
1642     }
1643
1644   return -1;
1645 }