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