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