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