gas/
[external/binutils.git] / gas / config / tc-tic6x.c
1 /* TI C6X assembler.
2    Copyright 2010, 2011
3    Free Software Foundation, Inc.
4    Contributed by Joseph Myers <joseph@codesourcery.com>
5                   Bernd Schmidt  <bernds@codesourcery.com>
6
7    This file is part of GAS, the GNU Assembler.
8
9    GAS is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3, or (at your option)
12    any later version.
13
14    GAS is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with GAS; see the file COPYING.  If not, write to the Free
21    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22    02110-1301, USA.  */
23
24 #include "as.h"
25 #include "dwarf2dbg.h"
26 #include "safe-ctype.h"
27 #include "subsegs.h"
28 #include "opcode/tic6x.h"
29 #include "elf/tic6x.h"
30 #include "elf32-tic6x.h"
31
32 /* Truncate and sign-extend at 32 bits, so that building on a 64-bit
33    host gives identical results to a 32-bit host.  */
34 #define TRUNC(X)        ((valueT) (X) & 0xffffffffU)
35 #define SEXT(X)         ((TRUNC (X) ^ 0x80000000U) - 0x80000000U)
36
37 const char comment_chars[] = ";";
38 const char line_comment_chars[] = "#*;";
39 const char line_separator_chars[] = "@";
40
41 const char EXP_CHARS[] = "eE";
42 const char FLT_CHARS[] = "dDfF";
43
44 const char *md_shortopts = "";
45
46 enum
47   {
48     OPTION_MARCH = OPTION_MD_BASE,
49     OPTION_MBIG_ENDIAN,
50     OPTION_MLITTLE_ENDIAN,
51     OPTION_MDSBT,
52     OPTION_MNO_DSBT,
53     OPTION_MPID,
54     OPTION_MPIC,
55     OPTION_MNO_PIC,
56     OPTION_MGENERATE_REL
57   };
58
59 struct option md_longopts[] =
60   {
61     { "march", required_argument, NULL, OPTION_MARCH },
62     { "mbig-endian", no_argument, NULL, OPTION_MBIG_ENDIAN },
63     { "mlittle-endian", no_argument, NULL, OPTION_MLITTLE_ENDIAN },
64     { "mdsbt", no_argument, NULL, OPTION_MDSBT },
65     { "mno-dsbt", no_argument, NULL, OPTION_MNO_DSBT },
66     { "mpid", required_argument, NULL, OPTION_MPID },
67     { "mpic", no_argument, NULL, OPTION_MPIC },
68     { "mno-pic", no_argument, NULL, OPTION_MNO_PIC },
69     { "mgenerate-rel", no_argument, NULL, OPTION_MGENERATE_REL },
70     { NULL, no_argument, NULL, 0 }
71   };
72 size_t md_longopts_size = sizeof (md_longopts);
73
74 /* The instructions enabled based only on the selected architecture
75    (all instructions, if no architecture specified).  */
76 static unsigned short tic6x_arch_enable = (TIC6X_INSN_C62X
77                                            | TIC6X_INSN_C64X
78                                            | TIC6X_INSN_C64XP
79                                            | TIC6X_INSN_C67X
80                                            | TIC6X_INSN_C67XP
81                                            | TIC6X_INSN_C674X);
82
83 /* The instructions enabled based on the current set of features
84    (architecture, as modified by other options).  */
85 static unsigned short tic6x_features;
86
87 /* The architecture attribute value, or C6XABI_Tag_ISA_none if
88    not yet set.  */
89 static int tic6x_arch_attribute = C6XABI_Tag_ISA_none;
90
91 /* Whether any instructions at all have been seen.  Once any
92    instructions have been seen, architecture attributes merge into the
93    previous attribute value rather than replacing it.  */
94 static bfd_boolean tic6x_seen_insns = FALSE;
95
96 /* The number of registers in each register file supported by the
97    current architecture.  */
98 static unsigned int tic6x_num_registers;
99
100 /* Whether predication on A0 is possible.  */
101 static bfd_boolean tic6x_predicate_a0;
102
103 /* Whether execute packets can cross fetch packet boundaries.  */
104 static bfd_boolean tic6x_can_cross_fp_boundary;
105
106 /* Whether there are constraints on simultaneous reads and writes of
107    40-bit data.  */
108 static bfd_boolean tic6x_long_data_constraints;
109
110 /* Whether compact instructions are available.  */
111 static bfd_boolean tic6x_compact_insns;
112
113 /* Whether to generate RELA relocations.  */
114 static bfd_boolean tic6x_generate_rela = TRUE;
115
116 /* Whether the code uses DSBT addressing.  */
117 static bfd_boolean tic6x_dsbt;
118
119 /* Types of position-independent data (attribute values for
120    Tag_ABI_PID).  */
121 typedef enum
122   {
123     tic6x_pid_no = 0,
124     tic6x_pid_near = 1,
125     tic6x_pid_far = 2
126   } tic6x_pid_type;
127
128 /* The type of data addressing used in this code.  */
129 static tic6x_pid_type tic6x_pid;
130
131 /* Whether the code uses position-independent code.  */
132 static bfd_boolean tic6x_pic;
133
134 /* Table of supported architecture variants.  */
135 typedef struct
136 {
137   const char *arch;
138   int attr;
139   unsigned short features;
140 } tic6x_arch_table;
141 static const tic6x_arch_table tic6x_arches[] =
142   {
143     { "c62x", C6XABI_Tag_ISA_C62X, TIC6X_INSN_C62X },
144     { "c64x", C6XABI_Tag_ISA_C64X, TIC6X_INSN_C62X | TIC6X_INSN_C64X },
145     { "c64x+", C6XABI_Tag_ISA_C64XP, (TIC6X_INSN_C62X
146                                       | TIC6X_INSN_C64X
147                                       | TIC6X_INSN_C64XP) },
148     { "c67x", C6XABI_Tag_ISA_C67X, TIC6X_INSN_C62X | TIC6X_INSN_C67X },
149     { "c67x+", C6XABI_Tag_ISA_C67XP, (TIC6X_INSN_C62X
150                                       | TIC6X_INSN_C67X
151                                       | TIC6X_INSN_C67XP) },
152     { "c674x", C6XABI_Tag_ISA_C674X, (TIC6X_INSN_C62X
153                                       | TIC6X_INSN_C64X
154                                       | TIC6X_INSN_C64XP
155                                       | TIC6X_INSN_C67X
156                                       | TIC6X_INSN_C67XP
157                                       | TIC6X_INSN_C674X) }
158   };
159
160 /* Update the selected architecture based on ARCH, giving an error if
161    ARCH is an invalid value.  Does not call tic6x_update_features; the
162    caller must do that if necessary.  */
163
164 static void
165 tic6x_use_arch (const char *arch)
166 {
167   unsigned int i;
168
169   for (i = 0; i < ARRAY_SIZE (tic6x_arches); i++)
170     if (strcmp (arch, tic6x_arches[i].arch) == 0)
171       {
172         tic6x_arch_enable = tic6x_arches[i].features;
173         if (tic6x_seen_insns)
174           tic6x_arch_attribute
175             = elf32_tic6x_merge_arch_attributes (tic6x_arch_attribute,
176                                                  tic6x_arches[i].attr);
177         else
178           tic6x_arch_attribute = tic6x_arches[i].attr;
179         return;
180       }
181
182   as_bad (_("unknown architecture '%s'"), arch);
183 }
184
185 /* Table of supported -mpid arguments.  */
186 typedef struct
187 {
188   const char *arg;
189   tic6x_pid_type attr;
190 } tic6x_pid_type_table;
191 static const tic6x_pid_type_table tic6x_pid_types[] =
192   {
193     { "no", tic6x_pid_no },
194     { "near", tic6x_pid_near },
195     { "far", tic6x_pid_far }
196   };
197
198 /* Handle -mpid=ARG.  */
199
200 static void
201 tic6x_use_pid (const char *arg)
202 {
203   unsigned int i;
204
205   for (i = 0; i < ARRAY_SIZE (tic6x_pid_types); i++)
206     if (strcmp (arg, tic6x_pid_types[i].arg) == 0)
207       {
208         tic6x_pid = tic6x_pid_types[i].attr;
209         return;
210       }
211
212   as_bad (_("unknown -mpid= argument '%s'"), arg);
213 }
214
215 /* Parse a target-specific option.  */
216
217 int
218 md_parse_option (int c, char *arg)
219 {
220   switch (c)
221     {
222     case OPTION_MARCH:
223       tic6x_use_arch (arg);
224       break;
225
226     case OPTION_MBIG_ENDIAN:
227       target_big_endian = 1;
228       break;
229
230     case OPTION_MLITTLE_ENDIAN:
231       target_big_endian = 0;
232       break;
233
234     case OPTION_MDSBT:
235       tic6x_dsbt = 1;
236       break;
237
238     case OPTION_MNO_DSBT:
239       tic6x_dsbt = 0;
240       break;
241
242     case OPTION_MPID:
243       tic6x_use_pid (arg);
244       break;
245
246     case OPTION_MPIC:
247       tic6x_pic = 1;
248       break;
249
250     case OPTION_MNO_PIC:
251       tic6x_pic = 0;
252       break;
253
254     case OPTION_MGENERATE_REL:
255       tic6x_generate_rela = FALSE;
256       break;
257
258     default:
259       return 0;
260     }
261   return 1;
262 }
263
264 void
265 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
266 {
267   unsigned int i;
268
269   fputc ('\n', stream);
270   fprintf (stream, _("TMS320C6000 options:\n"));
271   fprintf (stream, _("  -march=ARCH             enable instructions from architecture ARCH\n"));
272   fprintf (stream, _("  -mbig-endian            generate big-endian code\n"));
273   fprintf (stream, _("  -mlittle-endian         generate little-endian code\n"));
274   fprintf (stream, _("  -mdsbt                  code uses DSBT addressing\n"));
275   fprintf (stream, _("  -mno-dsbt               code does not use DSBT addressing\n"));
276   fprintf (stream, _("  -mpid=no                code uses position-dependent data addressing\n"));
277   fprintf (stream, _("  -mpid=near              code uses position-independent data addressing,\n"
278                      "                            GOT accesses use near DP addressing\n"));
279   fprintf (stream, _("  -mpid=far               code uses position-independent data addressing,\n"
280                      "                            GOT accesses use far DP addressing\n"));
281   fprintf (stream, _("  -mpic                   code addressing is position-independent\n"));
282   fprintf (stream, _("  -mno-pic                code addressing is position-dependent\n"));
283   /* -mgenerate-rel is only for testsuite use and is deliberately
284       undocumented.  */
285
286   fputc ('\n', stream);
287   fprintf (stream, _("Supported ARCH values are:"));
288   for (i = 0; i < ARRAY_SIZE (tic6x_arches); i++)
289     fprintf (stream, " %s", tic6x_arches[i].arch);
290   fputc ('\n', stream);
291 }
292
293 /* Update enabled features based on the current architecture and
294    related settings.  */
295 static void
296 tic6x_update_features (void)
297 {
298   tic6x_features = tic6x_arch_enable;
299
300   tic6x_num_registers
301     = (tic6x_arch_enable & (TIC6X_INSN_C64X | TIC6X_INSN_C67XP)) ? 32 : 16;
302
303   tic6x_predicate_a0 = (tic6x_arch_enable & TIC6X_INSN_C64X) ? TRUE : FALSE;
304
305   tic6x_can_cross_fp_boundary
306     = (tic6x_arch_enable
307        & (TIC6X_INSN_C64X | TIC6X_INSN_C67XP)) ? TRUE : FALSE;
308
309   tic6x_long_data_constraints
310     = (tic6x_arch_enable & TIC6X_INSN_C64X) ? FALSE : TRUE;
311
312   tic6x_compact_insns = (tic6x_arch_enable & TIC6X_INSN_C64XP) ? TRUE : FALSE;
313 }
314
315 /* Do configuration after all options have been parsed.  */
316
317 void
318 tic6x_after_parse_args (void)
319 {
320   tic6x_update_features ();
321 }
322
323 /* Parse a .arch directive.  */
324
325 static void
326 s_tic6x_arch (int ignored ATTRIBUTE_UNUSED)
327 {
328   char c;
329   char *arch;
330
331   arch = input_line_pointer;
332   while (*input_line_pointer && !ISSPACE (*input_line_pointer))
333     input_line_pointer++;
334   c = *input_line_pointer;
335   *input_line_pointer = 0;
336
337   tic6x_use_arch (arch);
338   tic6x_update_features ();
339   *input_line_pointer = c;
340   demand_empty_rest_of_line ();
341 }
342
343 /* Parse a .nocmp directive.  */
344
345 static void
346 s_tic6x_nocmp (int ignored ATTRIBUTE_UNUSED)
347 {
348   seg_info (now_seg)->tc_segment_info_data.nocmp = TRUE;
349   demand_empty_rest_of_line ();
350 }
351
352 /* Track for each attribute whether it has been set explicitly (and so
353    should not have a default value set by the assembler).  */
354 static bfd_boolean tic6x_attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
355
356 /* Parse a .c6xabi_attribute directive.  */
357
358 static void
359 s_tic6x_c6xabi_attribute (int ignored ATTRIBUTE_UNUSED)
360 {
361   int tag = s_vendor_attribute (OBJ_ATTR_PROC);
362
363   if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
364     tic6x_attributes_set_explicitly[tag] = TRUE;
365 }
366
367 typedef struct
368 {
369   const char *name;
370   int tag;
371 } tic6x_attribute_table;
372
373 static const tic6x_attribute_table tic6x_attributes[] =
374   {
375 #define TAG(tag, value) { #tag, tag },
376 #include "elf/tic6x-attrs.h"
377 #undef TAG
378   };
379
380 /* Convert an attribute name to a number.  */
381
382 int
383 tic6x_convert_symbolic_attribute (const char *name)
384 {
385   unsigned int i;
386
387   for (i = 0; i < ARRAY_SIZE (tic6x_attributes); i++)
388     if (strcmp (name, tic6x_attributes[i].name) == 0)
389       return tic6x_attributes[i].tag;
390
391   return -1;
392 }
393
394 const pseudo_typeS md_pseudo_table[] =
395   {
396     { "arch", s_tic6x_arch, 0 },
397     { "c6xabi_attribute", s_tic6x_c6xabi_attribute, 0 },
398     { "nocmp", s_tic6x_nocmp, 0 },
399     { "word", cons, 4 },
400     { 0, 0, 0 }
401   };
402
403 /* Hash table of opcodes.  For each opcode name, this stores a pointer
404    to a tic6x_opcode_list listing (in an arbitrary order) all opcode
405    table entries with that name.  */
406 static struct hash_control *opcode_hash;
407
408 /* Initialize the assembler (called once at assembler startup).  */
409
410 void
411 md_begin (void)
412 {
413   tic6x_opcode_id id;
414
415   bfd_set_arch_mach (stdoutput, TARGET_ARCH, 0);
416
417   /* Insert opcodes into the hash table.  */
418   opcode_hash = hash_new ();
419   for (id = 0; id < tic6x_opcode_max; id++)
420     {
421       const char *errmsg;
422       tic6x_opcode_list *opc = xmalloc (sizeof (tic6x_opcode_list));
423
424       opc->id = id;
425       opc->next = hash_find (opcode_hash, tic6x_opcode_table[id].name);
426       if ((errmsg = hash_jam (opcode_hash, tic6x_opcode_table[id].name, opc))
427           != NULL)
428         as_fatal ("%s", _(errmsg));
429     }
430 }
431
432 /* Whether the current line being parsed had the "||" parallel bars.  */
433 static bfd_boolean tic6x_line_parallel;
434
435 /* Whether the current line being parsed started "||^" to indicate an
436    SPMASKed parallel instruction.  */
437 static bfd_boolean tic6x_line_spmask;
438
439 /* If the current line being parsed had an instruction predicate, the
440    creg value for that predicate (which must be nonzero); otherwise
441    0.  */
442 static unsigned int tic6x_line_creg;
443
444 /* If the current line being parsed had an instruction predicate, the
445    z value for that predicate; otherwise 0.  */
446 static unsigned int tic6x_line_z;
447
448 /* Return 1 (updating input_line_pointer as appropriate) if the line
449    starting with C (immediately before input_line_pointer) starts with
450    pre-opcode text appropriate for this target, 0 otherwise.  */
451
452 int
453 tic6x_unrecognized_line (int c)
454 {
455   char *p, *endp;
456   unsigned int z;
457   bfd_boolean areg;
458   bfd_boolean bad_predicate;
459
460   switch (c)
461     {
462     case '|':
463       if (input_line_pointer[0] == '|')
464         {
465           if (input_line_pointer[1] == '^')
466             {
467               tic6x_line_spmask = TRUE;
468               input_line_pointer += 2;
469             }
470           else
471             input_line_pointer += 1;
472           if (tic6x_line_parallel)
473             as_bad (_("multiple '||' on same line"));
474           tic6x_line_parallel = TRUE;
475           if (tic6x_line_creg)
476             as_bad (_("'||' after predicate"));
477           return 1;
478         }
479       return 0;
480
481     case '[':
482       /* If it doesn't look like a predicate at all, just return 0.
483          If it looks like one but not a valid one, give a better
484          error.  */
485       p = input_line_pointer;
486       while (*p != ']' && !is_end_of_line[(unsigned char) *p])
487         p++;
488       if (*p != ']')
489         return 0;
490       endp = p + 1;
491       p = input_line_pointer;
492       z = 0;
493       bad_predicate = FALSE;
494       if (*p == '!')
495         {
496           z = 1;
497           p++;
498         }
499       if (*p == 'A' || *p == 'a')
500         areg = TRUE;
501       else if (*p == 'B' || *p == 'b')
502         areg = FALSE;
503       else
504         {
505           areg = TRUE; /* Avoid uninitialized warning.  */
506           bad_predicate = TRUE;
507         }
508       if (!bad_predicate)
509         {
510           p++;
511           if (*p != '0' && *p != '1' && *p != '2')
512             bad_predicate = TRUE;
513           else if (p[1] != ']')
514             bad_predicate = TRUE;
515           else
516             input_line_pointer = p + 2;
517         }
518
519       if (tic6x_line_creg)
520         as_bad (_("multiple predicates on same line"));
521
522       if (bad_predicate)
523         {
524           char ctmp = *endp;
525           *endp = 0;
526           as_bad (_("bad predicate '%s'"), input_line_pointer - 1);
527           *endp = ctmp;
528           input_line_pointer = endp;
529           return 1;
530         }
531
532       switch (*p)
533         {
534         case '0':
535           tic6x_line_creg = (areg ? 6 : 1);
536           if (areg && !tic6x_predicate_a0)
537             as_bad (_("predication on A0 not supported on this architecture"));
538           break;
539
540         case '1':
541           tic6x_line_creg = (areg ? 4 : 2);
542           break;
543
544         case '2':
545           tic6x_line_creg = (areg ? 5 : 3);
546           break;
547
548         default:
549           abort ();
550         }
551
552       tic6x_line_z = z;
553       return 1;
554
555     default:
556       return 0;
557     }
558 }
559
560 /* Do any target-specific handling of a label required.  */
561
562 void
563 tic6x_frob_label (symbolS *sym)
564 {
565   segment_info_type *si;
566   tic6x_label_list *list;
567
568   if (tic6x_line_parallel)
569     {
570       as_bad (_("label after '||'"));
571       tic6x_line_parallel = FALSE;
572       tic6x_line_spmask = FALSE;
573     }
574   if (tic6x_line_creg)
575     {
576       as_bad (_("label after predicate"));
577       tic6x_line_creg = 0;
578       tic6x_line_z = 0;
579     }
580
581   si = seg_info (now_seg);
582   list = si->tc_segment_info_data.label_list;
583   si->tc_segment_info_data.label_list = xmalloc (sizeof (tic6x_label_list));
584   si->tc_segment_info_data.label_list->next = list;
585   si->tc_segment_info_data.label_list->label = sym;
586
587   /* Defining tc_frob_label overrides the ELF definition of
588      obj_frob_label, so we need to apply its effects here.  */
589   dwarf2_emit_label (sym);
590 }
591
592 /* At end-of-line, give errors for start-of-line decorations that
593    needed an instruction but were not followed by one.  */
594
595 static void
596 tic6x_end_of_line (void)
597 {
598   if (tic6x_line_parallel)
599     {
600       as_bad (_("'||' not followed by instruction"));
601       tic6x_line_parallel = FALSE;
602       tic6x_line_spmask = FALSE;
603     }
604   if (tic6x_line_creg)
605     {
606       as_bad (_("predicate not followed by instruction"));
607       tic6x_line_creg = 0;
608       tic6x_line_z = 0;
609     }
610 }
611
612 /* Do any target-specific handling of the start of a logical line.  */
613
614 void
615 tic6x_start_line_hook (void)
616 {
617   tic6x_end_of_line ();
618 }
619
620 /* Do target-specific handling immediately after an input file from
621    the command line, and any other inputs it includes, have been
622    read.  */
623
624 void
625 tic6x_cleanup (void)
626 {
627   tic6x_end_of_line ();
628 }
629
630 /* Do target-specific initialization after arguments have been
631    processed and the output file created.  */
632
633 void
634 tic6x_init_after_args (void)
635 {
636   elf32_tic6x_set_use_rela_p (stdoutput, tic6x_generate_rela);
637 }
638
639 /* Free LIST of labels (possibly NULL).  */
640
641 static void
642 tic6x_free_label_list (tic6x_label_list *list)
643 {
644   while (list)
645     {
646       tic6x_label_list *old = list;
647
648       list = list->next;
649       free (old);
650     }
651 }
652
653 /* Handle a data alignment of N bytes.  */
654
655 void
656 tic6x_cons_align (int n ATTRIBUTE_UNUSED)
657 {
658   segment_info_type *seginfo = seg_info (now_seg);
659
660   /* Data means there is no current execute packet, and that any label
661      applies to that data rather than a subsequent instruction.  */
662   tic6x_free_label_list (seginfo->tc_segment_info_data.label_list);
663   seginfo->tc_segment_info_data.label_list = NULL;
664   seginfo->tc_segment_info_data.execute_packet_frag = NULL;
665   seginfo->tc_segment_info_data.last_insn_lsb = NULL;
666   seginfo->tc_segment_info_data.spmask_addr = NULL;
667   seginfo->tc_segment_info_data.func_units_used = 0;
668 }
669
670 /* Handle an alignment directive.  Return TRUE if the
671    machine-independent frag generation should be skipped.  */
672
673 bfd_boolean
674 tic6x_do_align (int n, char *fill, int len ATTRIBUTE_UNUSED, int max)
675 {
676   /* Given code alignments of 4, 8, 16 or 32 bytes, we try to handle
677      them in the md_end pass by inserting NOPs in parallel with
678      previous instructions.  We only do this in sections containing
679      nothing but instructions.  Code alignments of 1 or 2 bytes have
680      no effect in such sections (but we record them with
681      machine-dependent frags anyway so they can be skipped or
682      converted to machine-independent), while those of more than 64
683      bytes cannot reliably be handled in this way.  */
684   if (n > 0
685       && max >= 0
686       && max < (1 << n)
687       && !need_pass_2
688       && fill == NULL
689       && subseg_text_p (now_seg))
690     {
691       fragS *align_frag;
692       char *p;
693
694       if (n > 5)
695         return FALSE;
696
697       /* Machine-independent code would generate a frag here, but we
698          wish to handle it in a machine-dependent way.  */
699       if (frag_now_fix () != 0)
700         {
701           if (frag_now->fr_type != rs_machine_dependent)
702             frag_wane (frag_now);
703
704           frag_new (0);
705         }
706       frag_grow (32);
707       align_frag = frag_now;
708       p = frag_var (rs_machine_dependent, 32, 32, max, NULL, n, NULL);
709       /* This must be the same as the frag to which a pointer was just
710          saved.  */
711       if (p != align_frag->fr_literal)
712         abort ();
713       align_frag->tc_frag_data.is_insns = FALSE;
714       return TRUE;
715     }
716   else
717     return FALSE;
718 }
719
720 /* Types of operand for parsing purposes.  These are used as bit-masks
721    to tell tic6x_parse_operand what forms of operand are
722    permitted.  */
723 #define TIC6X_OP_EXP            0x0001u
724 #define TIC6X_OP_REG            0x0002u
725 #define TIC6X_OP_REGPAIR        0x0004u
726 #define TIC6X_OP_IRP            0x0008u
727 #define TIC6X_OP_NRP            0x0010u
728 /* With TIC6X_OP_MEM_NOUNREG, the contents of a () offset are always
729    interpreted as an expression, which may be a symbol with the same
730    name as a register that ends up being implicitly DP-relative.  With
731    TIC6X_OP_MEM_UNREG, the contents of a () offset are interpreted as
732    a register if they match one, and failing that as an expression,
733    which must be constant.  */
734 #define TIC6X_OP_MEM_NOUNREG    0x0020u
735 #define TIC6X_OP_MEM_UNREG      0x0040u
736 #define TIC6X_OP_CTRL           0x0080u
737 #define TIC6X_OP_FUNC_UNIT      0x0100u
738
739 /* A register or register pair read by the assembler.  */
740 typedef struct
741 {
742   /* The side the register is on (1 or 2).  */
743   unsigned int side;
744   /* The register number (0 to 31).  */
745   unsigned int num;
746 } tic6x_register;
747
748 /* Types of modification of a base address.  */
749 typedef enum
750   {
751     tic6x_mem_mod_none,
752     tic6x_mem_mod_plus,
753     tic6x_mem_mod_minus,
754     tic6x_mem_mod_preinc,
755     tic6x_mem_mod_predec,
756     tic6x_mem_mod_postinc,
757     tic6x_mem_mod_postdec
758   } tic6x_mem_mod;
759
760 /* Scaled [] or unscaled () nature of an offset.  */
761 typedef enum
762   {
763     tic6x_offset_none,
764     tic6x_offset_scaled,
765     tic6x_offset_unscaled
766   } tic6x_mem_scaling;
767
768 /* A memory operand read by the assembler.  */
769 typedef struct
770 {
771   /* The base register.  */
772   tic6x_register base_reg;
773   /* How the base register is modified.  */
774   tic6x_mem_mod mod;
775   /* Whether there is an offset (required with plain "+" and "-"), and
776      whether it is scaled or unscaled if so.  */
777   tic6x_mem_scaling scaled;
778   /* Whether the offset is a register (TRUE) or an expression
779      (FALSE).  */
780   bfd_boolean offset_is_reg;
781   /* The offset.  */
782   union
783   {
784     expressionS exp;
785     tic6x_register reg;
786   } offset;
787 } tic6x_mem_ref;
788
789 /* A functional unit in SPMASK operands read by the assembler.  */
790 typedef struct
791 {
792   /* The basic unit.  */
793   tic6x_func_unit_base base;
794   /* The side (1 or 2).  */
795   unsigned int side;
796 } tic6x_func_unit_operand;
797
798 /* An operand read by the assembler.  */
799 typedef struct
800 {
801   /* The syntactic form of the operand, as one of the bit-masks
802      above.  */
803   unsigned int form;
804   /* The operand value.  */
805   union
806   {
807     /* An expression: TIC6X_OP_EXP.  */
808     expressionS exp;
809     /* A register: TIC6X_OP_REG, TIC6X_OP_REGPAIR.  */
810     tic6x_register reg;
811     /* A memory reference: TIC6X_OP_MEM_NOUNREG,
812        TIC6X_OP_MEM_UNREG.  */
813     tic6x_mem_ref mem;
814     /* A control register: TIC6X_OP_CTRL.  */
815     tic6x_ctrl_id ctrl;
816     /* A functional unit: TIC6X_OP_FUNC_UNIT.  */
817     tic6x_func_unit_operand func_unit;
818   } value;
819 } tic6x_operand;
820
821 #define skip_whitespace(str)  do { if (*(str) == ' ') ++(str); } while (0)
822
823 /* Parse a register operand, or part of an operand, starting at *P.
824    If syntactically OK (including that the number is in the range 0 to
825    31, but not necessarily in range for this architecture), return
826    TRUE, putting the register side and number in *REG and update *P to
827    point immediately after the register number; otherwise return FALSE
828    without changing *P (but possibly changing *REG).  Do not print any
829    diagnostics.  */
830
831 static bfd_boolean
832 tic6x_parse_register (char **p, tic6x_register *reg)
833 {
834   char *r = *p;
835
836   switch (*r)
837     {
838     case 'a':
839     case 'A':
840       reg->side = 1;
841       break;
842
843     case 'b':
844     case 'B':
845       reg->side = 2;
846       break;
847
848     default:
849       return FALSE;
850     }
851   r++;
852
853   if (*r >= '0' && *r <= '9')
854     {
855       reg->num = *r - '0';
856       r++;
857     }
858   else
859     return FALSE;
860
861   if (reg->num > 0 && *r >= '0' && *r <= '9')
862     {
863       reg->num = reg->num * 10 + (*r - '0');
864       r++;
865     }
866
867   if (*r >= '0' && *r <= '9')
868     return FALSE;
869
870   if (reg->num >= 32)
871     return FALSE;
872   *p = r;
873   return TRUE;
874 }
875
876 /* Parse the initial two characters of a functional unit name starting
877    at *P.  If OK, set *BASE and *SIDE and return TRUE; otherwise,
878    return FALSE.  */
879
880 static bfd_boolean
881 tic6x_parse_func_unit_base (char *p, tic6x_func_unit_base *base,
882                             unsigned int *side)
883 {
884   bfd_boolean good_func_unit = TRUE;
885   tic6x_func_unit_base maybe_base = tic6x_func_unit_nfu;
886   unsigned int maybe_side = 0;
887
888   switch (p[0])
889     {
890     case 'd':
891     case 'D':
892       maybe_base = tic6x_func_unit_d;
893       break;
894
895     case 'l':
896     case 'L':
897       maybe_base = tic6x_func_unit_l;
898       break;
899
900     case 'm':
901     case 'M':
902       maybe_base = tic6x_func_unit_m;
903       break;
904
905     case 's':
906     case 'S':
907       maybe_base = tic6x_func_unit_s;
908       break;
909
910     default:
911       good_func_unit = FALSE;
912       break;
913     }
914
915   if (good_func_unit)
916     switch (p[1])
917       {
918       case '1':
919         maybe_side = 1;
920         break;
921
922       case '2':
923         maybe_side = 2;
924         break;
925
926       default:
927         good_func_unit = FALSE;
928         break;
929       }
930
931   if (good_func_unit)
932     {
933       *base = maybe_base;
934       *side = maybe_side;
935     }
936
937   return good_func_unit;
938 }
939
940 /* Parse an operand starting at *P.  If the operand parses OK, return
941    TRUE and store the value in *OP; otherwise return FALSE (possibly
942    changing *OP).  In any case, update *P to point to the following
943    comma or end of line.  The possible operand forms are given by
944    OP_FORMS.  For diagnostics, this is operand OPNO of an opcode
945    starting at STR, length OPC_LEN.  */
946
947 static bfd_boolean
948 tic6x_parse_operand (char **p, tic6x_operand *op, unsigned int op_forms,
949                      char *str, int opc_len, unsigned int opno)
950 {
951   bfd_boolean operand_parsed = FALSE;
952   char *q = *p;
953
954   if ((op_forms & (TIC6X_OP_MEM_NOUNREG | TIC6X_OP_MEM_UNREG))
955       == (TIC6X_OP_MEM_NOUNREG | TIC6X_OP_MEM_UNREG))
956     abort ();
957
958   /* Check for functional unit names for SPMASK and SPMASKR.  */
959   if (!operand_parsed && (op_forms & TIC6X_OP_FUNC_UNIT))
960     {
961       tic6x_func_unit_base base = tic6x_func_unit_nfu;
962       unsigned int side = 0;
963
964       if (tic6x_parse_func_unit_base (q, &base, &side))
965         {
966           char *rq = q + 2;
967
968           skip_whitespace (rq);
969           if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
970             {
971               op->form = TIC6X_OP_FUNC_UNIT;
972               op->value.func_unit.base = base;
973               op->value.func_unit.side = side;
974               operand_parsed = TRUE;
975               q = rq;
976             }
977         }
978     }
979
980   /* Check for literal "irp".  */
981   if (!operand_parsed && (op_forms & TIC6X_OP_IRP))
982     {
983       if ((q[0] == 'i' || q[0] == 'I')
984           && (q[1] == 'r' || q[1] == 'R')
985           && (q[2] == 'p' || q[2] == 'P'))
986         {
987           char *rq = q + 3;
988
989           skip_whitespace (rq);
990           if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
991             {
992               op->form = TIC6X_OP_IRP;
993               operand_parsed = TRUE;
994               q = rq;
995             }
996         }
997     }
998
999   /* Check for literal "nrp".  */
1000   if (!operand_parsed && (op_forms & TIC6X_OP_NRP))
1001     {
1002       if ((q[0] == 'n' || q[0] == 'N')
1003           && (q[1] == 'r' || q[1] == 'R')
1004           && (q[2] == 'p' || q[2] == 'P'))
1005         {
1006           char *rq = q + 3;
1007
1008           skip_whitespace (rq);
1009           if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
1010             {
1011               op->form = TIC6X_OP_NRP;
1012               operand_parsed = TRUE;
1013               q = rq;
1014             }
1015         }
1016     }
1017
1018   /* Check for control register names.  */
1019   if (!operand_parsed && (op_forms & TIC6X_OP_CTRL))
1020     {
1021       tic6x_ctrl_id crid;
1022
1023       for (crid = 0; crid < tic6x_ctrl_max; crid++)
1024         {
1025           size_t len = strlen (tic6x_ctrl_table[crid].name);
1026
1027           if (strncasecmp (tic6x_ctrl_table[crid].name, q, len) == 0)
1028             {
1029               char *rq = q + len;
1030
1031               skip_whitespace (rq);
1032               if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
1033                 {
1034                   op->form = TIC6X_OP_CTRL;
1035                   op->value.ctrl = crid;
1036                   operand_parsed = TRUE;
1037                   q = rq;
1038                   if (!(tic6x_ctrl_table[crid].isa_variants & tic6x_features))
1039                     as_bad (_("control register '%s' not supported "
1040                               "on this architecture"),
1041                             tic6x_ctrl_table[crid].name);
1042                 }
1043             }
1044         }
1045     }
1046
1047   /* See if this looks like a memory reference.  */
1048   if (!operand_parsed
1049       && (op_forms & (TIC6X_OP_MEM_NOUNREG | TIC6X_OP_MEM_UNREG)))
1050     {
1051       bfd_boolean mem_ok = TRUE;
1052       char *mq = q;
1053       tic6x_mem_mod mem_mod = tic6x_mem_mod_none;
1054       tic6x_register base_reg;
1055       bfd_boolean require_offset, permit_offset;
1056       tic6x_mem_scaling scaled;
1057       bfd_boolean offset_is_reg;
1058       expressionS offset_exp;
1059       tic6x_register offset_reg;
1060
1061       if (*mq == '*')
1062         mq++;
1063       else
1064         mem_ok = FALSE;
1065
1066       if (mem_ok)
1067         {
1068           skip_whitespace (mq);
1069           switch (*mq)
1070             {
1071             case '+':
1072               if (mq[1] == '+')
1073                 {
1074                   mem_mod = tic6x_mem_mod_preinc;
1075                   mq += 2;
1076                 }
1077               else
1078                 {
1079                   mem_mod = tic6x_mem_mod_plus;
1080                   mq++;
1081                 }
1082               break;
1083
1084             case '-':
1085               if (mq[1] == '-')
1086                 {
1087                   mem_mod = tic6x_mem_mod_predec;
1088                   mq += 2;
1089                 }
1090               else
1091                 {
1092                   mem_mod = tic6x_mem_mod_minus;
1093                   mq++;
1094                 }
1095               break;
1096
1097             default:
1098               break;
1099             }
1100         }
1101
1102       if (mem_ok)
1103         {
1104           skip_whitespace (mq);
1105           mem_ok = tic6x_parse_register (&mq, &base_reg);
1106         }
1107
1108       if (mem_ok && mem_mod == tic6x_mem_mod_none)
1109         {
1110           skip_whitespace (mq);
1111           if (mq[0] == '+' && mq[1] == '+')
1112             {
1113               mem_mod = tic6x_mem_mod_postinc;
1114               mq += 2;
1115             }
1116           else if (mq[0] == '-' && mq[1] == '-')
1117             {
1118               mem_mod = tic6x_mem_mod_postdec;
1119               mq += 2;
1120             }
1121         }
1122
1123       if (mem_mod == tic6x_mem_mod_none)
1124         permit_offset = FALSE;
1125       else
1126         permit_offset = TRUE;
1127       if (mem_mod == tic6x_mem_mod_plus || mem_mod == tic6x_mem_mod_minus)
1128         require_offset = TRUE;
1129       else
1130         require_offset = FALSE;
1131       scaled = tic6x_offset_none;
1132       offset_is_reg = FALSE;
1133
1134       if (mem_ok && permit_offset)
1135         {
1136           char endc = 0;
1137
1138           skip_whitespace (mq);
1139           switch (*mq)
1140             {
1141             case '[':
1142               scaled = tic6x_offset_scaled;
1143               mq++;
1144               endc = ']';
1145               break;
1146
1147             case '(':
1148               scaled = tic6x_offset_unscaled;
1149               mq++;
1150               endc = ')';
1151               break;
1152
1153             default:
1154               break;
1155             }
1156           if (scaled != tic6x_offset_none)
1157             {
1158               skip_whitespace (mq);
1159               if (scaled == tic6x_offset_scaled
1160                   || (op_forms & TIC6X_OP_MEM_UNREG))
1161                 {
1162                   bfd_boolean reg_ok;
1163                   char *rq = mq;
1164
1165                   reg_ok = tic6x_parse_register (&rq, &offset_reg);
1166                   if (reg_ok)
1167                     {
1168                       skip_whitespace (rq);
1169                       if (*rq == endc)
1170                         {
1171                           mq = rq;
1172                           offset_is_reg = TRUE;
1173                         }
1174                     }
1175                 }
1176               if (!offset_is_reg)
1177                 {
1178                   char *save_input_line_pointer;
1179
1180                   save_input_line_pointer = input_line_pointer;
1181                   input_line_pointer = mq;
1182                   expression (&offset_exp);
1183                   mq = input_line_pointer;
1184                   input_line_pointer = save_input_line_pointer;
1185                 }
1186               skip_whitespace (mq);
1187               if (*mq == endc)
1188                 mq++;
1189               else
1190                 mem_ok = FALSE;
1191             }
1192         }
1193
1194       if (mem_ok && require_offset && scaled == tic6x_offset_none)
1195         mem_ok = FALSE;
1196
1197       if (mem_ok)
1198         {
1199           skip_whitespace (mq);
1200           if (!is_end_of_line[(unsigned char) *mq] && *mq != ',')
1201             mem_ok = FALSE;
1202         }
1203
1204       if (mem_ok)
1205         {
1206           op->form = op_forms & (TIC6X_OP_MEM_NOUNREG | TIC6X_OP_MEM_UNREG);
1207           op->value.mem.base_reg = base_reg;
1208           op->value.mem.mod = mem_mod;
1209           op->value.mem.scaled = scaled;
1210           op->value.mem.offset_is_reg = offset_is_reg;
1211           if (offset_is_reg)
1212             op->value.mem.offset.reg = offset_reg;
1213           else
1214             op->value.mem.offset.exp = offset_exp;
1215           operand_parsed = TRUE;
1216           q = mq;
1217           if (base_reg.num >= tic6x_num_registers)
1218             as_bad (_("register number %u not supported on this architecture"),
1219                     base_reg.num);
1220           if (offset_is_reg && offset_reg.num >= tic6x_num_registers)
1221             as_bad (_("register number %u not supported on this architecture"),
1222                     offset_reg.num);
1223         }
1224     }
1225
1226   /* See if this looks like a register or register pair.  */
1227   if (!operand_parsed && (op_forms & (TIC6X_OP_REG | TIC6X_OP_REGPAIR)))
1228     {
1229       tic6x_register first_reg, second_reg;
1230       bfd_boolean reg_ok;
1231       char *rq = q;
1232
1233       reg_ok = tic6x_parse_register (&rq, &first_reg);
1234
1235       if (reg_ok)
1236         {
1237           if (*rq == ':' && (op_forms & TIC6X_OP_REGPAIR))
1238             {
1239               rq++;
1240               reg_ok = tic6x_parse_register (&rq, &second_reg);
1241               if (reg_ok)
1242                 {
1243                   skip_whitespace (rq);
1244                   if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
1245                     {
1246                       if ((second_reg.num & 1)
1247                           || (first_reg.num != second_reg.num + 1)
1248                           || (first_reg.side != second_reg.side))
1249                         as_bad (_("register pair for operand %u of '%.*s'"
1250                                   " not a valid even/odd pair"), opno,
1251                                 opc_len, str);
1252                       op->form = TIC6X_OP_REGPAIR;
1253                       op->value.reg = second_reg;
1254                       operand_parsed = TRUE;
1255                       q = rq;
1256                     }
1257                 }
1258             }
1259           else if (op_forms & TIC6X_OP_REG)
1260             {
1261               skip_whitespace (rq);
1262               if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
1263                 {
1264                   op->form = TIC6X_OP_REG;
1265                   op->value.reg = first_reg;
1266                   operand_parsed = TRUE;
1267                   q = rq;
1268                 }
1269             }
1270         }
1271       if (operand_parsed)
1272         {
1273           if (first_reg.num >= tic6x_num_registers)
1274             as_bad (_("register number %u not supported on this architecture"),
1275                     first_reg.num);
1276           if (op->form == TIC6X_OP_REGPAIR
1277               && second_reg.num >= tic6x_num_registers)
1278             as_bad (_("register number %u not supported on this architecture"),
1279                     second_reg.num);
1280         }
1281     }
1282
1283   /* Otherwise, parse it as an expression.  */
1284   if (!operand_parsed && (op_forms & TIC6X_OP_EXP))
1285     {
1286       char *save_input_line_pointer;
1287
1288       save_input_line_pointer = input_line_pointer;
1289       input_line_pointer = q;
1290       op->form = TIC6X_OP_EXP;
1291       expression (&op->value.exp);
1292       q = input_line_pointer;
1293       input_line_pointer = save_input_line_pointer;
1294       operand_parsed = TRUE;
1295     }
1296
1297   if (operand_parsed)
1298     {
1299       /* Now the operand has been parsed, there must be nothing more
1300          before the comma or end of line.  */
1301       skip_whitespace (q);
1302       if (!is_end_of_line[(unsigned char) *q] && *q != ',')
1303         {
1304           operand_parsed = FALSE;
1305           as_bad (_("junk after operand %u of '%.*s'"), opno,
1306                   opc_len, str);
1307           while (!is_end_of_line[(unsigned char) *q] && *q != ',')
1308             q++;
1309         }
1310     }
1311   else
1312     {
1313       /* This could not be parsed as any acceptable form of
1314          operand.  */
1315       switch (op_forms)
1316         {
1317         case TIC6X_OP_REG | TIC6X_OP_REGPAIR:
1318           as_bad (_("bad register or register pair for operand %u of '%.*s'"),
1319                   opno, opc_len, str);
1320           break;
1321
1322         case TIC6X_OP_REG | TIC6X_OP_CTRL:
1323         case TIC6X_OP_REG:
1324           as_bad (_("bad register for operand %u of '%.*s'"),
1325                   opno, opc_len, str);
1326           break;
1327
1328         case TIC6X_OP_REGPAIR:
1329           as_bad (_("bad register pair for operand %u of '%.*s'"),
1330                   opno, opc_len, str);
1331           break;
1332
1333         case TIC6X_OP_FUNC_UNIT:
1334           as_bad (_("bad functional unit for operand %u of '%.*s'"),
1335                   opno, opc_len, str);
1336           break;
1337
1338         default:
1339           as_bad (_("bad operand %u of '%.*s'"),
1340                   opno, opc_len, str);
1341           break;
1342
1343         }
1344       while (!is_end_of_line[(unsigned char) *q] && *q != ',')
1345         q++;
1346     }
1347   *p = q;
1348   return operand_parsed;
1349 }
1350
1351 /* Table of assembler operators and associated O_* values.  */
1352 typedef struct
1353 {
1354   const char *name;
1355   operatorT op;
1356 } tic6x_operator_table;
1357 static const tic6x_operator_table tic6x_operators[] = {
1358 #define O_dsbt_index O_md1
1359   { "dsbt_index", O_dsbt_index },
1360 #define O_got O_md2
1361   { "got", O_got },
1362 #define O_dpr_got O_md3
1363   { "dpr_got", O_dpr_got },
1364 #define O_dpr_byte O_md4
1365   { "dpr_byte", O_dpr_byte },
1366 #define O_dpr_hword O_md5
1367   { "dpr_hword", O_dpr_hword },
1368 #define O_dpr_word O_md6
1369   { "dpr_word", O_dpr_word },
1370 };
1371
1372 /* Parse a name in some machine-specific way.  Used on C6X to handle
1373    assembler operators.  */
1374
1375 int
1376 tic6x_parse_name (const char *name, expressionS *exprP,
1377                   enum expr_mode mode ATTRIBUTE_UNUSED, char *nextchar)
1378 {
1379   char *p = input_line_pointer;
1380   char c, *name_start, *name_end;
1381   const char *inner_name;
1382   unsigned int i;
1383   operatorT op = O_illegal;
1384   symbolS *sym;
1385
1386   if (*name != '$')
1387     return 0;
1388
1389   for (i = 0; i < ARRAY_SIZE (tic6x_operators); i++)
1390     if (strcasecmp (name + 1, tic6x_operators[i].name) == 0)
1391       {
1392         op = tic6x_operators[i].op;
1393         break;
1394       }
1395
1396   if (op == O_illegal)
1397     return 0;
1398
1399   *input_line_pointer = *nextchar;
1400   skip_whitespace (p);
1401
1402   if (*p != '(')
1403     {
1404       *input_line_pointer = 0;
1405       return 0;
1406     }
1407   p++;
1408   skip_whitespace (p);
1409
1410   if (!is_name_beginner (*p))
1411     {
1412       *input_line_pointer = 0;
1413       return 0;
1414     }
1415
1416   name_start = p;
1417   p++;
1418   while (is_part_of_name (*p))
1419     p++;
1420   name_end = p;
1421   skip_whitespace (p);
1422
1423   if (*p != ')')
1424     {
1425       *input_line_pointer = 0;
1426       return 0;
1427     }
1428
1429   input_line_pointer = p + 1;
1430   *nextchar = *input_line_pointer;
1431   *input_line_pointer = 0;
1432
1433   c = *name_end;
1434   *name_end = 0;
1435   inner_name = name_start;
1436   if (op == O_dsbt_index && strcmp (inner_name, "__c6xabi_DSBT_BASE") != 0)
1437     {
1438       as_bad (_("$DSBT_INDEX must be used with __c6xabi_DSBT_BASE"));
1439       inner_name = "__c6xabi_DSBT_BASE";
1440     }
1441   sym = symbol_find_or_make (inner_name);
1442   *name_end = c;
1443
1444   exprP->X_op = op;
1445   exprP->X_add_symbol = sym;
1446   exprP->X_add_number = 0;
1447   exprP->X_op_symbol = NULL;
1448   exprP->X_md = 0;
1449
1450   return 1;
1451 }
1452
1453 /* Create a fixup for an expression.  Same arguments as fix_new_exp,
1454    plus FIX_ADDA which is TRUE for ADDA instructions (to indicate that
1455    fixes resolving to constants should have those constants implicitly
1456    shifted) and FALSE otherwise, but look for C6X-specific expression
1457    types and adjust the relocations or give errors accordingly.  */
1458
1459 static void
1460 tic6x_fix_new_exp (fragS *frag, int where, int size, expressionS *exp,
1461                    int pcrel, bfd_reloc_code_real_type r_type,
1462                    bfd_boolean fix_adda)
1463 {
1464   bfd_reloc_code_real_type new_reloc = BFD_RELOC_UNUSED;
1465   fixS *fix;
1466
1467   switch (exp->X_op)
1468     {
1469     case O_dsbt_index:
1470       switch (r_type)
1471         {
1472         case BFD_RELOC_C6000_SBR_U15_W:
1473           new_reloc = BFD_RELOC_C6000_DSBT_INDEX;
1474           break;
1475
1476         default:
1477           as_bad (_("$DSBT_INDEX not supported in this context"));
1478           return;
1479         }
1480       break;
1481
1482     case O_got:
1483       switch (r_type)
1484         {
1485         case BFD_RELOC_C6000_SBR_U15_W:
1486           new_reloc = BFD_RELOC_C6000_SBR_GOT_U15_W;
1487           break;
1488
1489         default:
1490           as_bad (_("$GOT not supported in this context"));
1491           return;
1492         }
1493       break;
1494
1495     case O_dpr_got:
1496       switch (r_type)
1497         {
1498         case BFD_RELOC_C6000_ABS_L16:
1499           new_reloc = BFD_RELOC_C6000_SBR_GOT_L16_W;
1500           break;
1501
1502         case BFD_RELOC_C6000_ABS_H16:
1503           new_reloc = BFD_RELOC_C6000_SBR_GOT_H16_W;
1504           break;
1505
1506         default:
1507           as_bad (_("$DPR_GOT not supported in this context"));
1508           return;
1509         }
1510       break;
1511
1512     case O_dpr_byte:
1513       switch (r_type)
1514         {
1515         case BFD_RELOC_C6000_ABS_S16:
1516           new_reloc = BFD_RELOC_C6000_SBR_S16;
1517           break;
1518
1519         case BFD_RELOC_C6000_ABS_L16:
1520           new_reloc = BFD_RELOC_C6000_SBR_L16_B;
1521           break;
1522
1523         case BFD_RELOC_C6000_ABS_H16:
1524           new_reloc = BFD_RELOC_C6000_SBR_H16_B;
1525           break;
1526
1527         default:
1528           as_bad (_("$DPR_BYTE not supported in this context"));
1529           return;
1530         }
1531       break;
1532
1533     case O_dpr_hword:
1534       switch (r_type)
1535         {
1536         case BFD_RELOC_C6000_ABS_L16:
1537           new_reloc = BFD_RELOC_C6000_SBR_L16_H;
1538           break;
1539
1540         case BFD_RELOC_C6000_ABS_H16:
1541           new_reloc = BFD_RELOC_C6000_SBR_H16_H;
1542           break;
1543
1544         default:
1545           as_bad (_("$DPR_HWORD not supported in this context"));
1546           return;
1547         }
1548       break;
1549
1550     case O_dpr_word:
1551       switch (r_type)
1552         {
1553         case BFD_RELOC_C6000_ABS_L16:
1554           new_reloc = BFD_RELOC_C6000_SBR_L16_W;
1555           break;
1556
1557         case BFD_RELOC_C6000_ABS_H16:
1558           new_reloc = BFD_RELOC_C6000_SBR_H16_W;
1559           break;
1560
1561         default:
1562           as_bad (_("$DPR_WORD not supported in this context"));
1563           return;
1564         }
1565       break;
1566
1567     case O_symbol:
1568       break;
1569
1570     default:
1571       if (pcrel)
1572         {
1573           as_bad (_("invalid PC-relative operand"));
1574           return;
1575         }
1576       break;
1577     }
1578
1579   if (new_reloc == BFD_RELOC_UNUSED)
1580     fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
1581   else
1582     fix = fix_new (frag, where, size, exp->X_add_symbol, exp->X_add_number,
1583                    pcrel, new_reloc);
1584   fix->tc_fix_data.fix_adda = fix_adda;
1585 }
1586
1587 /* Generate a fix for a constant (.word etc.).  Needed to ensure these
1588    go through the error checking in tic6x_fix_new_exp.  */
1589
1590 void
1591 tic6x_cons_fix_new (fragS *frag, int where, int size, expressionS *exp)
1592 {
1593   bfd_reloc_code_real_type r_type;
1594
1595   switch (size)
1596     {
1597     case 1:
1598       r_type = BFD_RELOC_8;
1599       break;
1600
1601     case 2:
1602       r_type = BFD_RELOC_16;
1603       break;
1604
1605     case 4:
1606       r_type = BFD_RELOC_32;
1607       break;
1608
1609     default:
1610       as_bad (_("no %d-byte relocations available"), size);
1611       return;
1612     }
1613
1614   tic6x_fix_new_exp (frag, where, size, exp, 0, r_type, FALSE);
1615 }
1616
1617 /* Initialize target-specific fix data.  */
1618
1619 void
1620 tic6x_init_fix_data (fixS *fixP)
1621 {
1622   fixP->tc_fix_data.fix_adda = FALSE;
1623 }
1624
1625 /* Return true if the fix can be handled by GAS, false if it must
1626    be passed through to the linker.  */
1627
1628 bfd_boolean
1629 tic6x_fix_adjustable (fixS *fixP)
1630 {
1631   switch (fixP->fx_r_type)
1632     {
1633       /* Adjust_reloc_syms doesn't know about the GOT.  */
1634     case BFD_RELOC_C6000_SBR_GOT_U15_W:
1635     case BFD_RELOC_C6000_SBR_GOT_H16_W:
1636     case BFD_RELOC_C6000_SBR_GOT_L16_W:
1637       return 0;
1638
1639     default:
1640       return 1;
1641     }
1642 }
1643
1644 /* Given the fine-grained form of an operand, return the coarse
1645    (bit-mask) form.  */
1646
1647 static unsigned int
1648 tic6x_coarse_operand_form (tic6x_operand_form form)
1649 {
1650   switch (form)
1651     {
1652     case tic6x_operand_asm_const:
1653     case tic6x_operand_link_const:
1654       return TIC6X_OP_EXP;
1655
1656     case tic6x_operand_reg:
1657     case tic6x_operand_xreg:
1658     case tic6x_operand_dreg:
1659     case tic6x_operand_areg:
1660     case tic6x_operand_retreg:
1661       return TIC6X_OP_REG;
1662
1663     case tic6x_operand_regpair:
1664     case tic6x_operand_xregpair:
1665     case tic6x_operand_dregpair:
1666       return TIC6X_OP_REGPAIR;
1667
1668     case tic6x_operand_irp:
1669       return TIC6X_OP_IRP;
1670
1671     case tic6x_operand_nrp:
1672       return TIC6X_OP_NRP;
1673
1674     case tic6x_operand_ctrl:
1675       return TIC6X_OP_CTRL;
1676
1677     case tic6x_operand_mem_short:
1678     case tic6x_operand_mem_long:
1679     case tic6x_operand_mem_deref:
1680       return TIC6X_OP_MEM_NOUNREG;
1681
1682     case tic6x_operand_mem_ndw:
1683       return TIC6X_OP_MEM_UNREG;
1684
1685     case tic6x_operand_func_unit:
1686       return TIC6X_OP_FUNC_UNIT;
1687
1688     default:
1689       abort ();
1690     }
1691 }
1692
1693 /* How an operand may match or not match a desired form.  If different
1694    instruction alternatives fail in different ways, the first failure
1695    in this list determines the diagnostic.  */
1696 typedef enum
1697   {
1698     /* Matches.  */
1699     tic6x_match_matches,
1700     /* Bad coarse form.  */
1701     tic6x_match_coarse,
1702     /* Not constant.  */
1703     tic6x_match_non_const,
1704     /* Register on wrong side.  */
1705     tic6x_match_wrong_side,
1706     /* Not a valid address register.  */
1707     tic6x_match_bad_address,
1708     /* Not a valid return address register.  */
1709     tic6x_match_bad_return,
1710     /* Control register not readable.  */
1711     tic6x_match_ctrl_write_only,
1712     /* Control register not writable.  */
1713     tic6x_match_ctrl_read_only,
1714     /* Not a valid memory reference for this instruction.  */
1715     tic6x_match_bad_mem
1716   } tic6x_operand_match;
1717
1718 /* Return whether an operand matches the given fine-grained form and
1719    read/write usage, and, if it does not match, how it fails to match.
1720    The main functional unit side is SIDE; the cross-path side is CROSS
1721    (the same as SIDE if a cross path not used); the data side is
1722    DATA_SIDE.  */
1723 static tic6x_operand_match
1724 tic6x_operand_matches_form (const tic6x_operand *op, tic6x_operand_form form,
1725                             tic6x_rw rw, unsigned int side, unsigned int cross,
1726                             unsigned int data_side)
1727 {
1728   unsigned int coarse = tic6x_coarse_operand_form (form);
1729
1730   if (coarse != op->form)
1731     return tic6x_match_coarse;
1732
1733   switch (form)
1734     {
1735     case tic6x_operand_asm_const:
1736       if (op->value.exp.X_op == O_constant)
1737         return tic6x_match_matches;
1738       else
1739         return tic6x_match_non_const;
1740
1741     case tic6x_operand_link_const:
1742     case tic6x_operand_irp:
1743     case tic6x_operand_nrp:
1744     case tic6x_operand_func_unit:
1745       /* All expressions are link-time constants, although there may
1746          not be relocations to express them in the output file.  "irp"
1747          and "nrp" are unique operand values.  All parsed functional
1748          unit names are valid.  */
1749       return tic6x_match_matches;
1750
1751     case tic6x_operand_reg:
1752     case tic6x_operand_regpair:
1753       if (op->value.reg.side == side)
1754         return tic6x_match_matches;
1755       else
1756         return tic6x_match_wrong_side;
1757
1758     case tic6x_operand_xreg:
1759     case tic6x_operand_xregpair:
1760       if (op->value.reg.side == cross)
1761         return tic6x_match_matches;
1762       else
1763         return tic6x_match_wrong_side;
1764
1765     case tic6x_operand_dreg:
1766     case tic6x_operand_dregpair:
1767       if (op->value.reg.side == data_side)
1768         return tic6x_match_matches;
1769       else
1770         return tic6x_match_wrong_side;
1771
1772     case tic6x_operand_areg:
1773       if (op->value.reg.side != cross)
1774         return tic6x_match_wrong_side;
1775       else if (op->value.reg.side == 2
1776                && (op->value.reg.num == 14 || op->value.reg.num == 15))
1777         return tic6x_match_matches;
1778       else
1779         return tic6x_match_bad_address;
1780
1781     case tic6x_operand_retreg:
1782       if (op->value.reg.side != side)
1783         return tic6x_match_wrong_side;
1784       else if (op->value.reg.num != 3)
1785         return tic6x_match_bad_return;
1786       else
1787         return tic6x_match_matches;
1788
1789     case tic6x_operand_ctrl:
1790       switch (rw)
1791         {
1792         case tic6x_rw_read:
1793           if (tic6x_ctrl_table[op->value.ctrl].rw == tic6x_rw_read
1794               || tic6x_ctrl_table[op->value.ctrl].rw == tic6x_rw_read_write)
1795             return tic6x_match_matches;
1796           else
1797             return tic6x_match_ctrl_write_only;
1798
1799         case tic6x_rw_write:
1800           if (tic6x_ctrl_table[op->value.ctrl].rw == tic6x_rw_write
1801               || tic6x_ctrl_table[op->value.ctrl].rw == tic6x_rw_read_write)
1802             return tic6x_match_matches;
1803           else
1804             return tic6x_match_ctrl_read_only;
1805
1806         default:
1807           abort ();
1808         }
1809
1810     case tic6x_operand_mem_deref:
1811       if (op->value.mem.mod != tic6x_mem_mod_none)
1812         return tic6x_match_bad_mem;
1813       else if (op->value.mem.scaled != tic6x_offset_none)
1814         abort ();
1815       else if (op->value.mem.base_reg.side != side)
1816         return tic6x_match_bad_mem;
1817       else
1818         return tic6x_match_matches;
1819
1820     case tic6x_operand_mem_short:
1821     case tic6x_operand_mem_ndw:
1822       if (op->value.mem.base_reg.side != side)
1823         return tic6x_match_bad_mem;
1824       if (op->value.mem.mod == tic6x_mem_mod_none)
1825         {
1826           if (op->value.mem.scaled != tic6x_offset_none)
1827             abort ();
1828           return tic6x_match_matches;
1829         }
1830       if (op->value.mem.scaled == tic6x_offset_none)
1831         {
1832           if (op->value.mem.mod == tic6x_mem_mod_plus
1833               || op->value.mem.mod == tic6x_mem_mod_minus)
1834             abort ();
1835           return tic6x_match_matches;
1836         }
1837       if (op->value.mem.offset_is_reg)
1838         {
1839           if (op->value.mem.scaled == tic6x_offset_unscaled
1840               && form != tic6x_operand_mem_ndw)
1841             abort ();
1842           if (op->value.mem.offset.reg.side == side)
1843             return tic6x_match_matches;
1844           else
1845             return tic6x_match_bad_mem;
1846         }
1847       else
1848         {
1849           if (op->value.mem.offset.exp.X_op == O_constant)
1850             return tic6x_match_matches;
1851           else
1852             return tic6x_match_bad_mem;
1853         }
1854
1855     case tic6x_operand_mem_long:
1856       if (op->value.mem.base_reg.side == 2
1857           && (op->value.mem.base_reg.num == 14
1858               || op->value.mem.base_reg.num == 15))
1859         {
1860           switch (op->value.mem.mod)
1861             {
1862             case tic6x_mem_mod_none:
1863               if (op->value.mem.scaled != tic6x_offset_none)
1864                 abort ();
1865               return tic6x_match_matches;
1866
1867             case tic6x_mem_mod_plus:
1868               if (op->value.mem.scaled == tic6x_offset_none)
1869                 abort ();
1870               if (op->value.mem.offset_is_reg)
1871                 return tic6x_match_bad_mem;
1872               else if (op->value.mem.scaled == tic6x_offset_scaled
1873                        && op->value.mem.offset.exp.X_op != O_constant)
1874                 return tic6x_match_bad_mem;
1875               else
1876                 return tic6x_match_matches;
1877
1878             case tic6x_mem_mod_minus:
1879             case tic6x_mem_mod_preinc:
1880             case tic6x_mem_mod_predec:
1881             case tic6x_mem_mod_postinc:
1882             case tic6x_mem_mod_postdec:
1883               return tic6x_match_bad_mem;
1884
1885             default:
1886               abort ();
1887             }
1888
1889         }
1890       else
1891         return tic6x_match_bad_mem;
1892
1893     default:
1894       abort ();
1895     }
1896 }
1897
1898 /* Return the number of bits shift used with DP-relative coding method
1899    CODING.  */
1900
1901 static unsigned int
1902 tic6x_dpr_shift (tic6x_coding_method coding)
1903 {
1904   switch (coding)
1905     {
1906     case tic6x_coding_ulcst_dpr_byte:
1907       return 0;
1908
1909     case tic6x_coding_ulcst_dpr_half:
1910       return 1;
1911
1912     case tic6x_coding_ulcst_dpr_word:
1913       return 2;
1914
1915     default:
1916       abort ();
1917     }
1918 }
1919
1920 /* Return the relocation used with DP-relative coding method
1921    CODING.  */
1922
1923 static bfd_reloc_code_real_type
1924 tic6x_dpr_reloc (tic6x_coding_method coding)
1925 {
1926   switch (coding)
1927     {
1928     case tic6x_coding_ulcst_dpr_byte:
1929       return BFD_RELOC_C6000_SBR_U15_B;
1930
1931     case tic6x_coding_ulcst_dpr_half:
1932       return BFD_RELOC_C6000_SBR_U15_H;
1933
1934     case tic6x_coding_ulcst_dpr_word:
1935       return BFD_RELOC_C6000_SBR_U15_W;
1936
1937     default:
1938       abort ();
1939     }
1940 }
1941
1942 /* Given a memory reference *MEM_REF as originally parsed, fill in
1943    defaults for missing offsets.  */
1944
1945 static void
1946 tic6x_default_mem_ref (tic6x_mem_ref *mem_ref)
1947 {
1948   switch (mem_ref->mod)
1949     {
1950     case tic6x_mem_mod_none:
1951       if (mem_ref->scaled != tic6x_offset_none)
1952         abort ();
1953       mem_ref->mod = tic6x_mem_mod_plus;
1954       mem_ref->scaled = tic6x_offset_unscaled;
1955       mem_ref->offset_is_reg = FALSE;
1956       memset (&mem_ref->offset.exp, 0, sizeof mem_ref->offset.exp);
1957       mem_ref->offset.exp.X_op = O_constant;
1958       mem_ref->offset.exp.X_add_number = 0;
1959       mem_ref->offset.exp.X_unsigned = 0;
1960       break;
1961
1962     case tic6x_mem_mod_plus:
1963     case tic6x_mem_mod_minus:
1964       if (mem_ref->scaled == tic6x_offset_none)
1965         abort ();
1966       break;
1967
1968     case tic6x_mem_mod_preinc:
1969     case tic6x_mem_mod_predec:
1970     case tic6x_mem_mod_postinc:
1971     case tic6x_mem_mod_postdec:
1972       if (mem_ref->scaled != tic6x_offset_none)
1973         break;
1974       mem_ref->scaled = tic6x_offset_scaled;
1975       mem_ref->offset_is_reg = FALSE;
1976       memset (&mem_ref->offset.exp, 0, sizeof mem_ref->offset.exp);
1977       mem_ref->offset.exp.X_op = O_constant;
1978       mem_ref->offset.exp.X_add_number = 1;
1979       mem_ref->offset.exp.X_unsigned = 0;
1980       break;
1981
1982     default:
1983       abort ();
1984     }
1985 }
1986
1987 /* Return the encoding in the 8-bit field of an SPMASK or SPMASKR
1988    instruction of the specified UNIT, side SIDE.  */
1989
1990 static unsigned int
1991 tic6x_encode_spmask (tic6x_func_unit_base unit, unsigned int side)
1992 {
1993   switch (unit)
1994     {
1995     case tic6x_func_unit_l:
1996       return 1 << (side - 1);
1997
1998     case tic6x_func_unit_s:
1999       return 1 << (side + 1);
2000
2001     case tic6x_func_unit_d:
2002       return 1 << (side + 3);
2003
2004     case tic6x_func_unit_m:
2005       return 1 << (side + 5);
2006
2007     default:
2008       abort ();
2009     }
2010 }
2011
2012 /* Try to encode the instruction with opcode number ID and operands
2013    OPERANDS (number NUM_OPERANDS), creg value THIS_LINE_CREG and z
2014    value THIS_LINE_Z; FUNC_UNIT_SIDE, FUNC_UNIT_CROSS and
2015    FUNC_UNIT_DATA_SIDE describe the functional unit specification;
2016    SPLOOP_II is the ii value from the previous SPLOOP-family
2017    instruction, or 0 if not in such a loop; the only possible problems
2018    are operands being out of range (they already match the
2019    fine-grained form), and inappropriate predication.  If this
2020    succeeds, return the encoding and set *OK to TRUE; otherwise return
2021    0 and set *OK to FALSE.  If a fix is needed, set *FIX_NEEDED to
2022    true and fill in *FIX_EXP, *FIX_PCREL, *FX_R_TYPE and *FIX_ADDA.
2023    Print error messages for failure if PRINT_ERRORS is TRUE; the
2024    opcode starts at STR and has length OPC_LEN.  */
2025
2026 static unsigned int
2027 tic6x_try_encode (tic6x_opcode_id id, tic6x_operand *operands,
2028                   unsigned int num_operands, unsigned int this_line_creg,
2029                   unsigned int this_line_z, unsigned int func_unit_side,
2030                   unsigned int func_unit_cross,
2031                   unsigned int func_unit_data_side, int sploop_ii,
2032                   expressionS **fix_exp, int *fix_pcrel,
2033                   bfd_reloc_code_real_type *fx_r_type, bfd_boolean *fix_adda,
2034                   bfd_boolean *fix_needed, bfd_boolean *ok,
2035                   bfd_boolean print_errors, char *str, int opc_len)
2036 {
2037   const tic6x_opcode *opct;
2038   const tic6x_insn_format *fmt;
2039   unsigned int opcode_value;
2040   unsigned int fld;
2041
2042   opct = &tic6x_opcode_table[id];
2043   fmt = &tic6x_insn_format_table[opct->format];
2044   opcode_value = fmt->cst_bits;
2045
2046   for (fld = 0; fld < opct->num_fixed_fields; fld++)
2047     {
2048       if (opct->fixed_fields[fld].min_val == opct->fixed_fields[fld].max_val)
2049         {
2050           const tic6x_insn_field *fldd;
2051           fldd = tic6x_field_from_fmt (fmt, opct->fixed_fields[fld].field_id);
2052           if (fldd == NULL)
2053             abort ();
2054           opcode_value |= opct->fixed_fields[fld].min_val << fldd->low_pos;
2055         }
2056     }
2057
2058   for (fld = 0; fld < opct->num_variable_fields; fld++)
2059     {
2060       const tic6x_insn_field *fldd;
2061       unsigned int value;
2062       unsigned int opno;
2063       unsigned int ffld;
2064       offsetT sign_value;
2065       unsigned int bits;
2066       unsigned int fcyc_bits;
2067       expressionS *expp;
2068       expressionS ucexp;
2069       tic6x_mem_ref mem;
2070
2071       fldd = tic6x_field_from_fmt (fmt, opct->variable_fields[fld].field_id);
2072       if (fldd == NULL)
2073         abort ();
2074       opno = opct->variable_fields[fld].operand_num;
2075       switch (opct->variable_fields[fld].coding_method)
2076         {
2077         case tic6x_coding_ucst:
2078           if (operands[opno].form != TIC6X_OP_EXP)
2079             abort ();
2080           if (operands[opno].value.exp.X_op != O_constant)
2081             abort ();
2082           ucexp = operands[opno].value.exp;
2083         unsigned_constant:
2084           if (ucexp.X_add_number < 0
2085               || ucexp.X_add_number >= (1 << fldd->width))
2086             {
2087               if (print_errors)
2088                 as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
2089                         opc_len, str);
2090               *ok = FALSE;
2091               return 0;
2092             }
2093           value = ucexp.X_add_number;
2094           break;
2095
2096         case tic6x_coding_scst:
2097           if (operands[opno].form != TIC6X_OP_EXP)
2098             abort ();
2099           if (operands[opno].value.exp.X_op != O_constant)
2100             {
2101               value = 0;
2102               /* Opcode table should not permit non-constants without
2103                  a known relocation for them.  */
2104               if (fldd->low_pos != 7 || fldd->width != 16)
2105                 abort ();
2106               *fix_needed = TRUE;
2107               *fix_exp = &operands[opno].value.exp;
2108               *fix_pcrel = 0;
2109               *fx_r_type = BFD_RELOC_C6000_ABS_S16;
2110               *fix_adda = FALSE;
2111               break;
2112             }
2113           sign_value = SEXT (operands[opno].value.exp.X_add_number);
2114         signed_constant:
2115           if (sign_value < -(1 << (fldd->width - 1))
2116               || (sign_value >= (1 << (fldd->width - 1))))
2117             {
2118               if (print_errors)
2119                 as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
2120                         opc_len, str);
2121               *ok = FALSE;
2122               return 0;
2123             }
2124           value = sign_value + (1 << (fldd->width - 1));
2125           value ^= (1 << (fldd->width - 1));
2126           break;
2127
2128         case tic6x_coding_ucst_minus_one:
2129           if (operands[opno].form != TIC6X_OP_EXP)
2130             abort ();
2131           if (operands[opno].value.exp.X_op != O_constant)
2132             abort ();
2133           if (operands[opno].value.exp.X_add_number <= 0
2134               || operands[opno].value.exp.X_add_number > (1 << fldd->width))
2135             {
2136               if (print_errors)
2137                 as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
2138                         opc_len, str);
2139               *ok = FALSE;
2140               return 0;
2141             }
2142           value = operands[opno].value.exp.X_add_number - 1;
2143           break;
2144
2145         case tic6x_coding_scst_negate:
2146           if (operands[opno].form != TIC6X_OP_EXP)
2147             abort ();
2148           if (operands[opno].value.exp.X_op != O_constant)
2149             abort ();
2150           sign_value = SEXT (-operands[opno].value.exp.X_add_number);
2151           goto signed_constant;
2152
2153         case tic6x_coding_ulcst_dpr_byte:
2154         case tic6x_coding_ulcst_dpr_half:
2155         case tic6x_coding_ulcst_dpr_word:
2156           bits = tic6x_dpr_shift (opct->variable_fields[fld].coding_method);
2157           switch (operands[opno].form)
2158             {
2159             case TIC6X_OP_EXP:
2160               if (operands[opno].value.exp.X_op == O_constant)
2161                 {
2162                   ucexp = operands[opno].value.exp;
2163                   goto unsigned_constant;
2164                 }
2165               expp = &operands[opno].value.exp;
2166               break;
2167
2168             case TIC6X_OP_MEM_NOUNREG:
2169               mem = operands[opno].value.mem;
2170               tic6x_default_mem_ref (&mem);
2171               if (mem.offset_is_reg)
2172                 abort ();
2173               if (mem.offset.exp.X_op == O_constant)
2174                 {
2175                   ucexp = mem.offset.exp;
2176                   if (mem.scaled == tic6x_offset_unscaled)
2177                     {
2178                       if (ucexp.X_add_number & ((1 << bits) - 1))
2179                         {
2180                           if (print_errors)
2181                             as_bad (_("offset in operand %u of '%.*s' not "
2182                                       "divisible by %u"), opno + 1, opc_len,
2183                                     str, 1u << bits);
2184                           *ok = FALSE;
2185                           return 0;
2186                         }
2187                       ucexp.X_add_number >>= bits;
2188                     }
2189                   goto unsigned_constant;
2190                 }
2191               if (mem.scaled != tic6x_offset_unscaled)
2192                 abort ();
2193               if (operands[opno].value.mem.mod == tic6x_mem_mod_none
2194                   || operands[opno].value.mem.scaled != tic6x_offset_unscaled
2195                   || operands[opno].value.mem.offset_is_reg)
2196                 abort ();
2197               expp = &operands[opno].value.mem.offset.exp;
2198               break;
2199
2200             default:
2201               abort ();
2202             }
2203           value = 0;
2204           /* Opcode table should not use this encoding without a known
2205              relocation.  */
2206           if (fldd->low_pos != 8 || fldd->width != 15)
2207             abort ();
2208           /* We do not check for offset divisibility here; such a
2209              check is not needed at this point to encode the value,
2210              and if there is eventually a problem it will be detected
2211              either in md_apply_fix or at link time.  */
2212           *fix_needed = TRUE;
2213           *fix_exp = expp;
2214           *fix_pcrel = 0;
2215           *fx_r_type
2216             = tic6x_dpr_reloc (opct->variable_fields[fld].coding_method);
2217           if (operands[opno].form == TIC6X_OP_EXP)
2218             *fix_adda = TRUE;
2219           else
2220             *fix_adda = FALSE;
2221           break;
2222
2223         case tic6x_coding_lcst_low16:
2224           if (operands[opno].form != TIC6X_OP_EXP)
2225             abort ();
2226           if (operands[opno].value.exp.X_op == O_constant)
2227             value = operands[opno].value.exp.X_add_number & 0xffff;
2228           else
2229             {
2230               value = 0;
2231               /* Opcode table should not use this encoding without a
2232                  known relocation.  */
2233               if (fldd->low_pos != 7 || fldd->width != 16)
2234                 abort ();
2235               *fix_needed = TRUE;
2236               *fix_exp = &operands[opno].value.exp;
2237               *fix_pcrel = 0;
2238               *fx_r_type = BFD_RELOC_C6000_ABS_L16;
2239               *fix_adda = FALSE;
2240             }
2241           break;
2242
2243         case tic6x_coding_lcst_high16:
2244           if (operands[opno].form != TIC6X_OP_EXP)
2245             abort ();
2246           if (operands[opno].value.exp.X_op == O_constant)
2247             value = (operands[opno].value.exp.X_add_number >> 16) & 0xffff;
2248           else
2249             {
2250               value = 0;
2251               /* Opcode table should not use this encoding without a
2252                  known relocation.  */
2253               if (fldd->low_pos != 7 || fldd->width != 16)
2254                 abort ();
2255               *fix_needed = TRUE;
2256               *fix_exp = &operands[opno].value.exp;
2257               *fix_pcrel = 0;
2258               *fx_r_type = BFD_RELOC_C6000_ABS_H16;
2259               *fix_adda = FALSE;
2260             }
2261           break;
2262
2263         case tic6x_coding_pcrel:
2264         case tic6x_coding_pcrel_half:
2265           if (operands[opno].form != TIC6X_OP_EXP)
2266             abort ();
2267           value = 0;
2268           *fix_needed = TRUE;
2269           *fix_exp = &operands[opno].value.exp;
2270           *fix_pcrel = 1;
2271           if (fldd->low_pos == 7 && fldd->width == 21)
2272             *fx_r_type = BFD_RELOC_C6000_PCR_S21;
2273           else if (fldd->low_pos == 16 && fldd->width == 12)
2274             *fx_r_type = BFD_RELOC_C6000_PCR_S12;
2275           else if (fldd->low_pos == 13 && fldd->width == 10)
2276             *fx_r_type = BFD_RELOC_C6000_PCR_S10;
2277           else if (fldd->low_pos == 16 && fldd->width == 7)
2278             *fx_r_type = BFD_RELOC_C6000_PCR_S7;
2279           else
2280             /* Opcode table should not use this encoding without a
2281                known relocation.  */
2282             abort ();
2283           *fix_adda = FALSE;
2284           break;
2285
2286         case tic6x_coding_reg:
2287           switch (operands[opno].form)
2288             {
2289             case TIC6X_OP_REG:
2290             case TIC6X_OP_REGPAIR:
2291               value = operands[opno].value.reg.num;
2292               break;
2293
2294             case TIC6X_OP_MEM_NOUNREG:
2295             case TIC6X_OP_MEM_UNREG:
2296               value = operands[opno].value.mem.base_reg.num;
2297               break;
2298
2299             default:
2300               abort ();
2301             }
2302           break;
2303
2304         case tic6x_coding_areg:
2305           switch (operands[opno].form)
2306             {
2307             case TIC6X_OP_REG:
2308               value = (operands[opno].value.reg.num == 15 ? 1 : 0);
2309               break;
2310
2311             case TIC6X_OP_MEM_NOUNREG:
2312               value = (operands[opno].value.mem.base_reg.num == 15 ? 1 : 0);
2313               break;
2314
2315             default:
2316               abort ();
2317             }
2318           break;
2319
2320         case tic6x_coding_crlo:
2321           if (operands[opno].form != TIC6X_OP_CTRL)
2322             abort ();
2323           value = tic6x_ctrl_table[operands[opno].value.ctrl].crlo;
2324           break;
2325
2326         case tic6x_coding_crhi:
2327           if (operands[opno].form != TIC6X_OP_CTRL)
2328             abort ();
2329           value = 0;
2330           break;
2331
2332         case tic6x_coding_reg_shift:
2333           if (operands[opno].form != TIC6X_OP_REGPAIR)
2334             abort ();
2335           value = operands[opno].value.reg.num >> 1;
2336           break;
2337
2338         case tic6x_coding_mem_offset:
2339           if (operands[opno].form != TIC6X_OP_MEM_NOUNREG)
2340             abort ();
2341           mem = operands[opno].value.mem;
2342           tic6x_default_mem_ref (&mem);
2343           if (mem.offset_is_reg)
2344             {
2345               if (mem.scaled != tic6x_offset_scaled)
2346                 abort ();
2347               value = mem.offset.reg.num;
2348             }
2349           else
2350             {
2351               int scale;
2352
2353               if (mem.offset.exp.X_op != O_constant)
2354                 abort ();
2355               switch (mem.scaled)
2356                 {
2357                 case tic6x_offset_scaled:
2358                   scale = 1;
2359                   break;
2360
2361                 case tic6x_offset_unscaled:
2362                   scale = opct->operand_info[opno].size;
2363                   if (scale != 1 && scale != 2 && scale != 4 && scale != 8)
2364                     abort ();
2365                   break;
2366
2367                 default:
2368                   abort ();
2369                 }
2370               if (mem.offset.exp.X_add_number < 0
2371                   || mem.offset.exp.X_add_number >= (1 << fldd->width) * scale)
2372                 {
2373                   if (print_errors)
2374                     as_bad (_("offset in operand %u of '%.*s' out of range"),
2375                             opno + 1, opc_len, str);
2376                   *ok = FALSE;
2377                   return 0;
2378                 }
2379               if (mem.offset.exp.X_add_number % scale)
2380                 {
2381                   if (print_errors)
2382                     as_bad (_("offset in operand %u of '%.*s' not "
2383                               "divisible by %u"),
2384                             opno + 1, opc_len, str, scale);
2385                   *ok = FALSE;
2386                   return 0;
2387                 }
2388               value = mem.offset.exp.X_add_number / scale;
2389             }
2390           break;
2391
2392         case tic6x_coding_mem_offset_noscale:
2393           if (operands[opno].form != TIC6X_OP_MEM_UNREG)
2394             abort ();
2395           mem = operands[opno].value.mem;
2396           tic6x_default_mem_ref (&mem);
2397           if (mem.offset_is_reg)
2398             value = mem.offset.reg.num;
2399           else
2400             {
2401               if (mem.offset.exp.X_op != O_constant)
2402                 abort ();
2403               if (mem.offset.exp.X_add_number < 0
2404                   || mem.offset.exp.X_add_number >= (1 << fldd->width))
2405                 {
2406                   if (print_errors)
2407                     as_bad (_("offset in operand %u of '%.*s' out of range"),
2408                             opno + 1, opc_len, str);
2409                   *ok = FALSE;
2410                   return 0;
2411                 }
2412               value = mem.offset.exp.X_add_number;
2413             }
2414           break;
2415
2416         case tic6x_coding_mem_mode:
2417           if (operands[opno].form != TIC6X_OP_MEM_NOUNREG
2418               && operands[opno].form != TIC6X_OP_MEM_UNREG)
2419             abort ();
2420           mem = operands[opno].value.mem;
2421           tic6x_default_mem_ref (&mem);
2422           switch (mem.mod)
2423             {
2424             case tic6x_mem_mod_plus:
2425               value = 1;
2426               break;
2427
2428             case tic6x_mem_mod_minus:
2429               value = 0;
2430               break;
2431
2432             case tic6x_mem_mod_preinc:
2433               value = 9;
2434               break;
2435
2436             case tic6x_mem_mod_predec:
2437               value = 8;
2438               break;
2439
2440             case tic6x_mem_mod_postinc:
2441               value = 11;
2442               break;
2443
2444             case tic6x_mem_mod_postdec:
2445               value = 10;
2446               break;
2447
2448             default:
2449               abort ();
2450             }
2451           value += (mem.offset_is_reg ? 4 : 0);
2452           break;
2453
2454         case tic6x_coding_scaled:
2455           if (operands[opno].form != TIC6X_OP_MEM_UNREG)
2456             abort ();
2457           mem = operands[opno].value.mem;
2458           tic6x_default_mem_ref (&mem);
2459           switch (mem.scaled)
2460             {
2461             case tic6x_offset_unscaled:
2462               value = 0;
2463               break;
2464
2465             case tic6x_offset_scaled:
2466               value = 1;
2467               break;
2468
2469             default:
2470               abort ();
2471             }
2472           break;
2473
2474         case tic6x_coding_spmask:
2475           /* The position of such a field is hardcoded in the handling
2476              of "||^".  */
2477           if (fldd->low_pos != 18)
2478             abort ();
2479           value = 0;
2480           for (opno = 0; opno < num_operands; opno++)
2481             {
2482               unsigned int v;
2483
2484               v = tic6x_encode_spmask (operands[opno].value.func_unit.base,
2485                                        operands[opno].value.func_unit.side);
2486               if (value & v)
2487                 {
2488                   if (print_errors)
2489                     as_bad (_("functional unit already masked for operand "
2490                               "%u of '%.*s'"), opno + 1, opc_len, str);
2491                   *ok = FALSE;
2492                   return 0;
2493                 }
2494               value |= v;
2495             }
2496           break;
2497
2498         case tic6x_coding_reg_unused:
2499           /* This is a placeholder; correct handling goes along with
2500              resource constraint checks.  */
2501           value = 0;
2502           break;
2503
2504         case tic6x_coding_fstg:
2505         case tic6x_coding_fcyc:
2506           if (operands[opno].form != TIC6X_OP_EXP)
2507             abort ();
2508           if (operands[opno].value.exp.X_op != O_constant)
2509             abort ();
2510           if (!sploop_ii)
2511             {
2512               if (print_errors)
2513                 as_bad (_("'%.*s' instruction not in a software "
2514                           "pipelined loop"),
2515                         opc_len, str);
2516               *ok = FALSE;
2517               return 0;
2518             }
2519
2520           if (sploop_ii <= 1)
2521             fcyc_bits = 0;
2522           else if (sploop_ii <= 2)
2523             fcyc_bits = 1;
2524           else if (sploop_ii <= 4)
2525             fcyc_bits = 2;
2526           else if (sploop_ii <= 8)
2527             fcyc_bits = 3;
2528           else if (sploop_ii <= 14)
2529             fcyc_bits = 4;
2530           else
2531             abort ();
2532           if (fcyc_bits > fldd->width)
2533             abort ();
2534
2535           if (opct->variable_fields[fld].coding_method == tic6x_coding_fstg)
2536             {
2537               int i, t;
2538               if (operands[opno].value.exp.X_add_number < 0
2539                   || (operands[opno].value.exp.X_add_number
2540                       >= (1 << (fldd->width - fcyc_bits))))
2541                 {
2542                   if (print_errors)
2543                     as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
2544                             opc_len, str);
2545                   *ok = FALSE;
2546                   return 0;
2547                 }
2548               value = operands[opno].value.exp.X_add_number;
2549               for (t = 0, i = fcyc_bits; i < fldd->width; i++)
2550                 {
2551                   t = (t << 1) | (value & 1);
2552                   value >>= 1;
2553                 }
2554               value = t << fcyc_bits;
2555             }
2556           else
2557             {
2558               if (operands[opno].value.exp.X_add_number < 0
2559                   || (operands[opno].value.exp.X_add_number >= sploop_ii))
2560                 {
2561                   if (print_errors)
2562                     as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
2563                             opc_len, str);
2564                   *ok = FALSE;
2565                   return 0;
2566                 }
2567               value = operands[opno].value.exp.X_add_number;
2568             }
2569           break;
2570
2571         case tic6x_coding_fu:
2572           value = func_unit_side == 2 ? 1 : 0;
2573           break;
2574
2575         case tic6x_coding_data_fu:
2576           value = func_unit_data_side == 2 ? 1 : 0;
2577           break;
2578
2579         case tic6x_coding_xpath:
2580           value = func_unit_cross;
2581           break;
2582
2583         default:
2584           abort ();
2585         }
2586
2587       for (ffld = 0; ffld < opct->num_fixed_fields; ffld++)
2588         if ((opct->fixed_fields[ffld].field_id
2589              == opct->variable_fields[fld].field_id)
2590             && (value < opct->fixed_fields[ffld].min_val
2591                 || value > opct->fixed_fields[ffld].max_val))
2592           {
2593             if (print_errors)
2594               as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
2595                       opc_len, str);
2596             *ok = FALSE;
2597             return 0;
2598           }
2599
2600       opcode_value |= value << fldd->low_pos;
2601     }
2602
2603   if (this_line_creg)
2604     {
2605       const tic6x_insn_field *creg;
2606       const tic6x_insn_field *z;
2607
2608       creg = tic6x_field_from_fmt (fmt, tic6x_field_creg);
2609       if (creg == NULL)
2610         {
2611           if (print_errors)
2612             as_bad (_("instruction '%.*s' cannot be predicated"),
2613                     opc_len, str);
2614           *ok = FALSE;
2615           return 0;
2616         }
2617       z = tic6x_field_from_fmt (fmt, tic6x_field_z);
2618       /* If there is a creg field, there must be a z field; otherwise
2619          there is an error in the format table.  */
2620       if (z == NULL)
2621         abort ();
2622
2623       opcode_value |= this_line_creg << creg->low_pos;
2624       opcode_value |= this_line_z << z->low_pos;
2625     }
2626
2627   *ok = TRUE;
2628   return opcode_value;
2629 }
2630
2631 /* Convert the target integer stored in N bytes in BUF to a host
2632    integer, returning that value.  */
2633
2634 static valueT
2635 md_chars_to_number (char *buf, int n)
2636 {
2637   valueT result = 0;
2638   unsigned char *p = (unsigned char *) buf;
2639
2640   if (target_big_endian)
2641     {
2642       while (n--)
2643         {
2644           result <<= 8;
2645           result |= (*p++ & 0xff);
2646         }
2647     }
2648   else
2649     {
2650       while (n--)
2651         {
2652           result <<= 8;
2653           result |= (p[n] & 0xff);
2654         }
2655     }
2656
2657   return result;
2658 }
2659
2660 /* Assemble the instruction starting at STR (an opcode, with the
2661    opcode name all-lowercase).  */
2662
2663 void
2664 md_assemble (char *str)
2665 {
2666   char *p;
2667   int opc_len;
2668   bfd_boolean this_line_parallel;
2669   bfd_boolean this_line_spmask;
2670   unsigned int this_line_creg;
2671   unsigned int this_line_z;
2672   tic6x_label_list *this_insn_label_list;
2673   segment_info_type *seginfo;
2674   tic6x_opcode_list *opc_list, *opc;
2675   tic6x_func_unit_base func_unit_base = tic6x_func_unit_nfu;
2676   unsigned int func_unit_side = 0;
2677   unsigned int func_unit_cross = 0;
2678   unsigned int cross_side = 0;
2679   unsigned int func_unit_data_side = 0;
2680   unsigned int max_matching_opcodes, num_matching_opcodes;
2681   tic6x_opcode_id *opcm = NULL;
2682   unsigned int opc_rank[TIC6X_NUM_PREFER];
2683   const tic6x_opcode *opct = NULL;
2684   int min_rank, try_rank, max_rank;
2685   bfd_boolean num_operands_permitted[TIC6X_MAX_SOURCE_OPERANDS + 1]
2686     = { FALSE };
2687   unsigned int operand_forms[TIC6X_MAX_SOURCE_OPERANDS] = { 0 };
2688   tic6x_operand operands[TIC6X_MAX_SOURCE_OPERANDS];
2689   unsigned int max_num_operands;
2690   unsigned int num_operands_read;
2691   bfd_boolean ok_this_arch, ok_this_fu, ok_this_arch_fu;
2692   bfd_boolean bad_operands = FALSE;
2693   unsigned int opcode_value;
2694   bfd_boolean encoded_ok;
2695   bfd_boolean fix_needed = FALSE;
2696   expressionS *fix_exp = NULL;
2697   int fix_pcrel = 0;
2698   bfd_reloc_code_real_type fx_r_type = BFD_RELOC_UNUSED;
2699   bfd_boolean fix_adda = FALSE;
2700   fragS *insn_frag;
2701   char *output;
2702
2703   p = str;
2704   while (*p && !is_end_of_line[(unsigned char) *p] && *p != ' ')
2705     p++;
2706
2707   /* This function should only have been called when there is actually
2708      an instruction to assemble.  */
2709   if (p == str)
2710     abort ();
2711
2712   /* Now an instruction has been seen, architecture attributes from
2713      .arch directives merge with rather than overriding the previous
2714      value.  */
2715   tic6x_seen_insns = TRUE;
2716   /* If no .arch directives or -march options have been seen, we are
2717      assessing instruction validity based on the C674X default, so set
2718      the attribute accordingly.  */
2719   if (tic6x_arch_attribute == C6XABI_Tag_ISA_none)
2720     tic6x_arch_attribute = C6XABI_Tag_ISA_C674X;
2721
2722   /* Reset global settings for parallel bars and predicates now to
2723      avoid extra errors if there are problems with this opcode.  */
2724   this_line_parallel = tic6x_line_parallel;
2725   this_line_spmask = tic6x_line_spmask;
2726   this_line_creg = tic6x_line_creg;
2727   this_line_z = tic6x_line_z;
2728   tic6x_line_parallel = FALSE;
2729   tic6x_line_spmask = FALSE;
2730   tic6x_line_creg = 0;
2731   tic6x_line_z = 0;
2732   seginfo = seg_info (now_seg);
2733   this_insn_label_list = seginfo->tc_segment_info_data.label_list;
2734   seginfo->tc_segment_info_data.label_list = NULL;
2735
2736   opc_list = hash_find_n (opcode_hash, str, p - str);
2737   if (opc_list == NULL)
2738     {
2739       char c = *p;
2740       *p = 0;
2741       as_bad (_("unknown opcode '%s'"), str);
2742       *p = c;
2743       return;
2744     }
2745
2746   opc_len = p - str;
2747   skip_whitespace (p);
2748
2749   /* See if there is something that looks like a functional unit
2750      specifier.  */
2751   if (*p == '.')
2752     {
2753       bfd_boolean good_func_unit;
2754       tic6x_func_unit_base maybe_base = tic6x_func_unit_nfu;
2755       unsigned int maybe_side = 0;
2756       unsigned int maybe_cross = 0;
2757       unsigned int maybe_data_side = 0;
2758
2759       good_func_unit = tic6x_parse_func_unit_base (p + 1, &maybe_base,
2760                                                    &maybe_side);
2761
2762       if (good_func_unit)
2763         {
2764           if (p[3] == ' ' || is_end_of_line[(unsigned char) p[3]])
2765             p += 3;
2766           else if ((p[3] == 'x' || p[3] == 'X')
2767                    && (p[4] == ' ' || is_end_of_line[(unsigned char) p[4]]))
2768             {
2769               maybe_cross = 1;
2770               p += 4;
2771             }
2772           else if (maybe_base == tic6x_func_unit_d
2773                    && (p[3] == 't' || p[3] == 'T')
2774                    && (p[4] == '1' || p[4] == '2')
2775                    && (p[5] == ' ' || is_end_of_line[(unsigned char) p[5]]))
2776             {
2777               maybe_data_side = p[4] - '0';
2778               p += 5;
2779             }
2780           else
2781             good_func_unit = FALSE;
2782         }
2783
2784       if (good_func_unit)
2785         {
2786           func_unit_base = maybe_base;
2787           func_unit_side = maybe_side;
2788           func_unit_cross = maybe_cross;
2789           cross_side = (func_unit_cross ? 3 - func_unit_side : func_unit_side);
2790           func_unit_data_side = maybe_data_side;
2791         }
2792
2793       skip_whitespace (p);
2794     }
2795
2796   /* Determine which entries in the opcode table match, and the
2797      associated permitted forms of operands.  */
2798   max_matching_opcodes = 0;
2799   for (opc = opc_list; opc; opc = opc->next)
2800     max_matching_opcodes++;
2801   num_matching_opcodes = 0;
2802   opcm = xmalloc (max_matching_opcodes * sizeof (*opcm));
2803   max_num_operands = 0;
2804   ok_this_arch = FALSE;
2805   ok_this_fu = FALSE;
2806   ok_this_arch_fu = FALSE;
2807   for (opc = opc_list; opc; opc = opc->next)
2808     {
2809       unsigned int num_operands;
2810       unsigned int i;
2811       bfd_boolean this_opc_arch_ok = TRUE;
2812       bfd_boolean this_opc_fu_ok = TRUE;
2813
2814       if (tic6x_insn_format_table[tic6x_opcode_table[opc->id].format].num_bits
2815           != 32)
2816         continue;
2817       if (!(tic6x_opcode_table[opc->id].isa_variants & tic6x_features))
2818         this_opc_arch_ok = FALSE;
2819       if (tic6x_opcode_table[opc->id].func_unit != func_unit_base)
2820         this_opc_fu_ok = FALSE;
2821       if (func_unit_side == 1
2822           && (tic6x_opcode_table[opc->id].flags & TIC6X_FLAG_SIDE_B_ONLY))
2823         this_opc_fu_ok = FALSE;
2824       if (func_unit_cross
2825           && (tic6x_opcode_table[opc->id].flags & TIC6X_FLAG_NO_CROSS))
2826         this_opc_fu_ok = FALSE;
2827       if (!func_unit_data_side
2828           && (tic6x_opcode_table[opc->id].flags
2829               & (TIC6X_FLAG_LOAD | TIC6X_FLAG_STORE)))
2830         this_opc_fu_ok = FALSE;
2831       if (func_unit_data_side
2832           && !(tic6x_opcode_table[opc->id].flags
2833                & (TIC6X_FLAG_LOAD | TIC6X_FLAG_STORE)))
2834         this_opc_fu_ok = FALSE;
2835       if (func_unit_data_side == 1
2836           && (tic6x_opcode_table[opc->id].flags & TIC6X_FLAG_SIDE_T2_ONLY))
2837         this_opc_fu_ok = FALSE;
2838       if (this_opc_arch_ok)
2839         ok_this_arch = TRUE;
2840       if (this_opc_fu_ok)
2841         ok_this_fu = TRUE;
2842       if (!this_opc_arch_ok || !this_opc_fu_ok)
2843         continue;
2844       ok_this_arch_fu = TRUE;
2845       opcm[num_matching_opcodes] = opc->id;
2846       num_matching_opcodes++;
2847       num_operands = tic6x_opcode_table[opc->id].num_operands;
2848
2849       if (tic6x_opcode_table[opc->id].flags & TIC6X_FLAG_SPMASK)
2850         {
2851           if (num_operands != 1
2852               || (tic6x_opcode_table[opc->id].operand_info[0].form
2853                   != tic6x_operand_func_unit))
2854             abort ();
2855           num_operands = 8;
2856           for (i = 0; i < num_operands; i++)
2857             {
2858               operand_forms[i]
2859                 |= tic6x_coarse_operand_form (tic6x_operand_func_unit);
2860               num_operands_permitted[i] = TRUE;
2861             }
2862         }
2863       else
2864         {
2865           for (i = 0; i < num_operands; i++)
2866             {
2867               tic6x_operand_form f
2868                 = tic6x_opcode_table[opc->id].operand_info[i].form;
2869
2870               operand_forms[i] |= tic6x_coarse_operand_form (f);
2871             }
2872         }
2873       num_operands_permitted[num_operands] = TRUE;
2874       if (num_operands > max_num_operands)
2875         max_num_operands = num_operands;
2876     }
2877
2878   if (!ok_this_arch)
2879     {
2880       as_bad (_("'%.*s' instruction not supported on this architecture"),
2881               opc_len, str);
2882       free (opcm);
2883       return;
2884     }
2885
2886   if (!ok_this_fu)
2887     {
2888       as_bad (_("'%.*s' instruction not supported on this functional unit"),
2889               opc_len, str);
2890       free (opcm);
2891       return;
2892     }
2893
2894   if (!ok_this_arch_fu)
2895     {
2896       as_bad (_("'%.*s' instruction not supported on this functional unit"
2897                 " for this architecture"),
2898               opc_len, str);
2899       free (opcm);
2900       return;
2901     }
2902
2903   /* If there were no instructions matching the above availability
2904      checks, we should now have given an error and returned.  */
2905   if (num_matching_opcodes == 0)
2906     abort ();
2907
2908   num_operands_read = 0;
2909   while (TRUE)
2910     {
2911       skip_whitespace (p);
2912       if (is_end_of_line[(unsigned char) *p])
2913         {
2914           if (num_operands_read > 0)
2915             {
2916               as_bad (_("missing operand after comma"));
2917               bad_operands = TRUE;
2918             }
2919           break;
2920         }
2921
2922       if (max_num_operands == 0)
2923         {
2924           as_bad (_("too many operands to '%.*s'"), opc_len, str);
2925           bad_operands = TRUE;
2926           break;
2927         }
2928
2929       if (!tic6x_parse_operand (&p, &operands[num_operands_read],
2930                                 operand_forms[num_operands_read], str, opc_len,
2931                                 num_operands_read + 1))
2932         bad_operands = TRUE;
2933       num_operands_read++;
2934
2935       if (is_end_of_line[(unsigned char) *p])
2936         break;
2937       else if (*p == ',')
2938         {
2939           p++;
2940           if (num_operands_read == max_num_operands)
2941             {
2942               as_bad (_("too many operands to '%.*s'"), opc_len, str);
2943               bad_operands = TRUE;
2944               break;
2945             }
2946           continue;
2947         }
2948       else
2949         /* Operand parsing should consume whole operands.  */
2950         abort ();
2951     }
2952
2953   if (!bad_operands && !num_operands_permitted[num_operands_read])
2954     {
2955       as_bad (_("bad number of operands to '%.*s'"), opc_len, str);
2956       bad_operands = TRUE;
2957     }
2958
2959   if (!bad_operands)
2960     {
2961       /* Each operand is of the right syntactic form for some opcode
2962          choice, and the number of operands is valid.  Check that each
2963          operand is OK in detail for some opcode choice with the right
2964          number of operands.  */
2965       unsigned int i;
2966
2967       for (i = 0; i < num_operands_read; i++)
2968         {
2969           bfd_boolean coarse_ok = FALSE;
2970           bfd_boolean fine_ok = FALSE;
2971           tic6x_operand_match fine_failure = tic6x_match_matches;
2972           unsigned int j;
2973
2974           for (j = 0; j < num_matching_opcodes; j++)
2975             {
2976               tic6x_operand_form f;
2977               tic6x_rw rw;
2978               unsigned int cf;
2979               tic6x_operand_match this_fine_failure;
2980
2981               if (tic6x_opcode_table[opcm[j]].flags & TIC6X_FLAG_SPMASK)
2982                 {
2983                   f = tic6x_operand_func_unit;
2984                   rw = tic6x_rw_none;
2985                 }
2986               else
2987                 {
2988                   if (tic6x_opcode_table[opcm[j]].num_operands
2989                       != num_operands_read)
2990                     continue;
2991
2992                   f = tic6x_opcode_table[opcm[j]].operand_info[i].form;
2993                   rw = tic6x_opcode_table[opcm[j]].operand_info[i].rw;
2994                 }
2995               cf = tic6x_coarse_operand_form (f);
2996
2997               if (operands[i].form != cf)
2998                 continue;
2999
3000               coarse_ok = TRUE;
3001               this_fine_failure
3002                 = tic6x_operand_matches_form (&operands[i], f, rw,
3003                                               func_unit_side,
3004                                               cross_side,
3005                                               func_unit_data_side);
3006               if (this_fine_failure == tic6x_match_matches)
3007                 {
3008                   fine_ok = TRUE;
3009                   break;
3010                 }
3011               if (fine_failure == tic6x_match_matches
3012                   || fine_failure > this_fine_failure)
3013                 fine_failure = this_fine_failure;
3014             }
3015
3016           /* No instructions should have operand syntactic forms only
3017              acceptable with certain numbers of operands, so no
3018              diagnostic for this case.  */
3019           if (!coarse_ok)
3020             abort ();
3021
3022           if (!fine_ok)
3023             {
3024               switch (fine_failure)
3025                 {
3026                 case tic6x_match_non_const:
3027                   as_bad (_("operand %u of '%.*s' not constant"),
3028                           i + 1, opc_len, str);
3029                   break;
3030
3031                 case tic6x_match_wrong_side:
3032                   as_bad (_("operand %u of '%.*s' on wrong side"),
3033                           i + 1, opc_len, str);
3034                   break;
3035
3036                 case tic6x_match_bad_return:
3037                   as_bad (_("operand %u of '%.*s' not a valid return "
3038                             "address register"),
3039                           i + 1, opc_len, str);
3040                   break;
3041
3042                 case tic6x_match_ctrl_write_only:
3043                   as_bad (_("operand %u of '%.*s' is write-only"),
3044                           i + 1, opc_len, str);
3045                   break;
3046
3047                 case tic6x_match_ctrl_read_only:
3048                   as_bad (_("operand %u of '%.*s' is read-only"),
3049                           i + 1, opc_len, str);
3050                   break;
3051
3052                 case tic6x_match_bad_mem:
3053                   as_bad (_("operand %u of '%.*s' not a valid memory "
3054                             "reference"),
3055                           i + 1, opc_len, str);
3056                   break;
3057
3058                 case tic6x_match_bad_address:
3059                   as_bad (_("operand %u of '%.*s' not a valid base "
3060                             "address register"),
3061                           i + 1, opc_len, str);
3062                   break;
3063
3064                 default:
3065                   abort ();
3066                 }
3067               bad_operands = TRUE;
3068               break;
3069             }
3070         }
3071     }
3072
3073   if (!bad_operands)
3074     {
3075       /* Each operand is OK for some opcode choice, and the number of
3076          operands is valid.  Check whether there is an opcode choice
3077          for which all operands are simultaneously valid.  */
3078       unsigned int i;
3079       bfd_boolean found_match = FALSE;
3080
3081       for (i = 0; i < TIC6X_NUM_PREFER; i++)
3082         opc_rank[i] = (unsigned int) -1;
3083
3084       min_rank = TIC6X_NUM_PREFER - 1;
3085       max_rank = 0;
3086
3087       for (i = 0; i < num_matching_opcodes; i++)
3088         {
3089           unsigned int j;
3090           bfd_boolean this_matches = TRUE;
3091
3092           if (!(tic6x_opcode_table[opcm[i]].flags & TIC6X_FLAG_SPMASK)
3093               && tic6x_opcode_table[opcm[i]].num_operands != num_operands_read)
3094             continue;
3095
3096           for (j = 0; j < num_operands_read; j++)
3097             {
3098               tic6x_operand_form f;
3099               tic6x_rw rw;
3100
3101               if (tic6x_opcode_table[opcm[i]].flags & TIC6X_FLAG_SPMASK)
3102                 {
3103                   f = tic6x_operand_func_unit;
3104                   rw = tic6x_rw_none;
3105                 }
3106               else
3107                 {
3108                   f = tic6x_opcode_table[opcm[i]].operand_info[j].form;
3109                   rw = tic6x_opcode_table[opcm[i]].operand_info[j].rw;
3110                 }
3111               if (tic6x_operand_matches_form (&operands[j], f, rw,
3112                                               func_unit_side,
3113                                               cross_side,
3114                                               func_unit_data_side)
3115                   != tic6x_match_matches)
3116                 {
3117                   this_matches = FALSE;
3118                   break;
3119                 }
3120             }
3121
3122           if (this_matches)
3123             {
3124               int rank = TIC6X_PREFER_VAL (tic6x_opcode_table[opcm[i]].flags);
3125
3126               if (rank < min_rank)
3127                 min_rank = rank;
3128               if (rank > max_rank)
3129                 max_rank = rank;
3130
3131               if (opc_rank[rank] == (unsigned int) -1)
3132                 opc_rank[rank] = i;
3133               else
3134                 /* The opcode table should provide a total ordering
3135                    for all cases where multiple matches may get
3136                    here.  */
3137                 abort ();
3138
3139               found_match = TRUE;
3140             }
3141         }
3142
3143       if (!found_match)
3144         {
3145           as_bad (_("bad operand combination for '%.*s'"), opc_len, str);
3146           bad_operands = TRUE;
3147         }
3148     }
3149
3150   if (bad_operands)
3151     {
3152       free (opcm);
3153       return;
3154     }
3155
3156   opcode_value = 0;
3157   encoded_ok = FALSE;
3158   for (try_rank = max_rank; try_rank >= min_rank; try_rank--)
3159     {
3160       fix_needed = FALSE;
3161
3162       if (opc_rank[try_rank] == (unsigned int) -1)
3163         continue;
3164
3165       opcode_value = tic6x_try_encode (opcm[opc_rank[try_rank]], operands,
3166                                        num_operands_read, this_line_creg,
3167                                        this_line_z, func_unit_side,
3168                                        func_unit_cross, func_unit_data_side,
3169                                        seginfo->tc_segment_info_data.sploop_ii,
3170                                        &fix_exp, &fix_pcrel, &fx_r_type,
3171                                        &fix_adda, &fix_needed, &encoded_ok,
3172                                        (try_rank == min_rank ? TRUE : FALSE),
3173                                        str, opc_len);
3174       if (encoded_ok)
3175         {
3176           opct = &tic6x_opcode_table[opcm[opc_rank[try_rank]]];
3177           break;
3178         }
3179     }
3180
3181   free (opcm);
3182
3183   if (!encoded_ok)
3184     return;
3185
3186   if (this_line_parallel)
3187     {
3188       insn_frag = seginfo->tc_segment_info_data.execute_packet_frag;
3189       if (insn_frag == NULL)
3190         {
3191           as_bad (_("parallel instruction not following another instruction"));
3192           return;
3193         }
3194
3195       if (insn_frag->fr_fix >= 32)
3196         {
3197           as_bad (_("too many instructions in execute packet"));
3198           return;
3199         }
3200
3201       if (this_insn_label_list != NULL)
3202         as_bad (_("label not at start of execute packet"));
3203
3204       if (opct->flags & TIC6X_FLAG_FIRST)
3205         as_bad (_("'%.*s' instruction not at start of execute packet"),
3206                 opc_len, str);
3207
3208       *seginfo->tc_segment_info_data.last_insn_lsb |= 0x1;
3209       output = insn_frag->fr_literal + insn_frag->fr_fix;
3210     }
3211   else
3212     {
3213       tic6x_label_list *l;
3214
3215       seginfo->tc_segment_info_data.spmask_addr = NULL;
3216       seginfo->tc_segment_info_data.func_units_used = 0;
3217
3218       /* Start a new frag for this execute packet.  */
3219       if (frag_now_fix () != 0)
3220         {
3221           if (frag_now->fr_type != rs_machine_dependent)
3222             frag_wane (frag_now);
3223
3224           frag_new (0);
3225         }
3226       frag_grow (32);
3227       insn_frag = seginfo->tc_segment_info_data.execute_packet_frag = frag_now;
3228       for (l = this_insn_label_list; l; l = l->next)
3229         {
3230           symbol_set_frag (l->label, frag_now);
3231           S_SET_VALUE (l->label, 0);
3232           S_SET_SEGMENT (l->label, now_seg);
3233         }
3234       tic6x_free_label_list (this_insn_label_list);
3235       dwarf2_emit_insn (0);
3236       output = frag_var (rs_machine_dependent, 32, 32, 0, NULL, 0, NULL);
3237       /* This must be the same as the frag to which a pointer was just
3238          saved.  */
3239       if (output != insn_frag->fr_literal)
3240         abort ();
3241       insn_frag->tc_frag_data.is_insns = TRUE;
3242       insn_frag->tc_frag_data.can_cross_fp_boundary
3243         = tic6x_can_cross_fp_boundary;
3244     }
3245
3246   if (func_unit_base != tic6x_func_unit_nfu)
3247     {
3248       unsigned int func_unit_enc;
3249
3250       func_unit_enc = tic6x_encode_spmask (func_unit_base, func_unit_side);
3251
3252       if (seginfo->tc_segment_info_data.func_units_used & func_unit_enc)
3253         as_bad (_("functional unit already used in this execute packet"));
3254
3255       seginfo->tc_segment_info_data.func_units_used |= func_unit_enc;
3256     }
3257
3258   if (opct->flags & TIC6X_FLAG_SPLOOP)
3259     {
3260       if (seginfo->tc_segment_info_data.sploop_ii)
3261         as_bad (_("nested software pipelined loop"));
3262       if (num_operands_read != 1
3263           || operands[0].form != TIC6X_OP_EXP
3264           || operands[0].value.exp.X_op != O_constant)
3265         abort ();
3266       seginfo->tc_segment_info_data.sploop_ii
3267         = operands[0].value.exp.X_add_number;
3268     }
3269   else if (opct->flags & TIC6X_FLAG_SPKERNEL)
3270     {
3271       if (!seginfo->tc_segment_info_data.sploop_ii)
3272         as_bad (_("'%.*s' instruction not in a software pipelined loop"),
3273                 opc_len, str);
3274       seginfo->tc_segment_info_data.sploop_ii = 0;
3275     }
3276
3277   if (this_line_spmask)
3278     {
3279       if (seginfo->tc_segment_info_data.spmask_addr == NULL)
3280         as_bad (_("'||^' without previous SPMASK"));
3281       else if (func_unit_base == tic6x_func_unit_nfu)
3282         as_bad (_("cannot mask instruction using no functional unit"));
3283       else
3284         {
3285           unsigned int spmask_opcode;
3286           unsigned int mask_bit;
3287
3288           spmask_opcode
3289             = md_chars_to_number (seginfo->tc_segment_info_data.spmask_addr,
3290                                   4);
3291           mask_bit = tic6x_encode_spmask (func_unit_base, func_unit_side);
3292           mask_bit <<= 18;
3293           if (spmask_opcode & mask_bit)
3294             as_bad (_("functional unit already masked"));
3295           spmask_opcode |= mask_bit;
3296           md_number_to_chars (seginfo->tc_segment_info_data.spmask_addr,
3297                               spmask_opcode, 4);
3298         }
3299     }
3300
3301   record_alignment (now_seg, 5);
3302   md_number_to_chars (output, opcode_value, 4);
3303   if (fix_needed)
3304     tic6x_fix_new_exp (insn_frag, output - insn_frag->fr_literal, 4, fix_exp,
3305                        fix_pcrel, fx_r_type, fix_adda);
3306   insn_frag->fr_fix += 4;
3307   insn_frag->fr_var -= 4;
3308   seginfo->tc_segment_info_data.last_insn_lsb
3309     = (target_big_endian ? output + 3 : output);
3310   if (opct->flags & TIC6X_FLAG_SPMASK)
3311     seginfo->tc_segment_info_data.spmask_addr = output;
3312 }
3313
3314 /* Modify NEWVAL (32-bit) by inserting VALUE, shifted right by SHIFT
3315    and the least significant BITS bits taken, at position POS.  */
3316 #define MODIFY_VALUE(NEWVAL, VALUE, SHIFT, POS, BITS)                   \
3317   do {                                                                  \
3318     (NEWVAL) &= 0xffffffffU & ~(((1U << (BITS)) - 1) << (POS));         \
3319     (NEWVAL) |= (((VALUE) >> (SHIFT)) & ((1U << (BITS)) - 1)) << (POS); \
3320   } while (0)
3321
3322 /* Apply a fixup to the object file.  */
3323
3324 void
3325 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
3326 {
3327   offsetT value = *valP;
3328   char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3329
3330   value = SEXT (value);
3331   *valP = value;
3332
3333   fixP->fx_offset = SEXT (fixP->fx_offset);
3334
3335   if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
3336     fixP->fx_done = 1;
3337
3338   /* We do our own overflow checks.  */
3339   fixP->fx_no_overflow = 1;
3340
3341   switch (fixP->fx_r_type)
3342     {
3343     case BFD_RELOC_NONE:
3344       /* Force output to the object file.  */
3345       fixP->fx_done = 0;
3346       break;
3347
3348     case BFD_RELOC_32:
3349       if (fixP->fx_done || !seg->use_rela_p)
3350         md_number_to_chars (buf, value, 4);
3351       break;
3352
3353     case BFD_RELOC_16:
3354       if (fixP->fx_done || !seg->use_rela_p)
3355         {
3356           if (value < -0x8000 || value > 0xffff)
3357             as_bad_where (fixP->fx_file, fixP->fx_line,
3358                           _("value too large for 2-byte field"));
3359           md_number_to_chars (buf, value, 2);
3360         }
3361       break;
3362
3363     case BFD_RELOC_8:
3364       if (fixP->fx_done || !seg->use_rela_p)
3365         {
3366           if (value < -0x80 || value > 0xff)
3367             as_bad_where (fixP->fx_file, fixP->fx_line,
3368                           _("value too large for 1-byte field"));
3369           md_number_to_chars (buf, value, 1);
3370         }
3371       break;
3372
3373     case BFD_RELOC_C6000_ABS_S16:
3374     case BFD_RELOC_C6000_ABS_L16:
3375     case BFD_RELOC_C6000_SBR_S16:
3376     case BFD_RELOC_C6000_SBR_L16_B:
3377     case BFD_RELOC_C6000_SBR_L16_H:
3378     case BFD_RELOC_C6000_SBR_L16_W:
3379     case BFD_RELOC_C6000_SBR_GOT_L16_W:
3380       if (fixP->fx_done || !seg->use_rela_p)
3381         {
3382           offsetT newval = md_chars_to_number (buf, 4);
3383           int shift;
3384
3385           switch (fixP->fx_r_type)
3386             {
3387             case BFD_RELOC_C6000_SBR_L16_H:
3388               shift = 1;
3389               break;
3390
3391             case BFD_RELOC_C6000_SBR_L16_W:
3392             case BFD_RELOC_C6000_SBR_GOT_L16_W:
3393               shift = 2;
3394               break;
3395
3396             default:
3397               shift = 0;
3398               break;
3399             }
3400
3401           MODIFY_VALUE (newval, value, shift, 7, 16);
3402           if ((value < -0x8000 || value > 0x7fff)
3403               && (fixP->fx_r_type == BFD_RELOC_C6000_ABS_S16
3404                   || fixP->fx_r_type == BFD_RELOC_C6000_SBR_S16))
3405             as_bad_where (fixP->fx_file, fixP->fx_line,
3406                           _("immediate offset out of range"));
3407
3408           md_number_to_chars (buf, newval, 4);
3409         }
3410       if (fixP->fx_done
3411           && fixP->fx_r_type != BFD_RELOC_C6000_ABS_S16
3412           && fixP->fx_r_type != BFD_RELOC_C6000_ABS_L16)
3413         abort ();
3414       break;
3415
3416     case BFD_RELOC_C6000_ABS_H16:
3417     case BFD_RELOC_C6000_SBR_H16_B:
3418     case BFD_RELOC_C6000_SBR_H16_H:
3419     case BFD_RELOC_C6000_SBR_H16_W:
3420     case BFD_RELOC_C6000_SBR_GOT_H16_W:
3421       if (fixP->fx_done || !seg->use_rela_p)
3422         {
3423           offsetT newval = md_chars_to_number (buf, 4);
3424           int shift;
3425
3426           switch (fixP->fx_r_type)
3427             {
3428             case BFD_RELOC_C6000_SBR_H16_H:
3429               shift = 17;
3430               break;
3431
3432             case BFD_RELOC_C6000_SBR_H16_W:
3433             case BFD_RELOC_C6000_SBR_GOT_H16_W:
3434               shift = 18;
3435               break;
3436
3437             default:
3438               shift = 16;
3439               break;
3440             }
3441
3442           MODIFY_VALUE (newval, value, shift, 7, 16);
3443
3444           md_number_to_chars (buf, newval, 4);
3445         }
3446       if (fixP->fx_done && fixP->fx_r_type != BFD_RELOC_C6000_ABS_H16)
3447         abort ();
3448       break;
3449
3450     case BFD_RELOC_C6000_SBR_U15_B:
3451       if (fixP->fx_done || !seg->use_rela_p)
3452         {
3453           offsetT newval = md_chars_to_number (buf, 4);
3454
3455           MODIFY_VALUE (newval, value, 0, 8, 15);
3456           if (value < 0 || value > 0x7fff)
3457             as_bad_where (fixP->fx_file, fixP->fx_line,
3458                           _("immediate offset out of range"));
3459
3460           md_number_to_chars (buf, newval, 4);
3461         }
3462       break;
3463
3464     case BFD_RELOC_C6000_SBR_U15_H:
3465       if (fixP->fx_done || !seg->use_rela_p)
3466         {
3467           offsetT newval = md_chars_to_number (buf, 4);
3468
3469           /* Constant ADDA operands, processed as constant when the
3470              instruction is parsed, are encoded as-is rather than
3471              shifted.  If the operand of an ADDA instruction is now
3472              constant (for example, the difference between two labels
3473              found after the instruction), ensure it is encoded the
3474              same way it would have been if the constant value had
3475              been known when the instruction was parsed.  */
3476           if (fixP->tc_fix_data.fix_adda && fixP->fx_done)
3477             value <<= 1;
3478
3479           MODIFY_VALUE (newval, value, 1, 8, 15);
3480           if (value & 1)
3481             as_bad_where (fixP->fx_file, fixP->fx_line,
3482                           _("immediate offset not 2-byte-aligned"));
3483           if (value < 0 || value > 0xfffe)
3484             as_bad_where (fixP->fx_file, fixP->fx_line,
3485                           _("immediate offset out of range"));
3486
3487           md_number_to_chars (buf, newval, 4);
3488         }
3489       break;
3490
3491     case BFD_RELOC_C6000_SBR_U15_W:
3492     case BFD_RELOC_C6000_SBR_GOT_U15_W:
3493       if (fixP->fx_done || !seg->use_rela_p)
3494         {
3495           offsetT newval = md_chars_to_number (buf, 4);
3496
3497           /* Constant ADDA operands, processed as constant when the
3498              instruction is parsed, are encoded as-is rather than
3499              shifted.  If the operand of an ADDA instruction is now
3500              constant (for example, the difference between two labels
3501              found after the instruction), ensure it is encoded the
3502              same way it would have been if the constant value had
3503              been known when the instruction was parsed.  */
3504           if (fixP->tc_fix_data.fix_adda && fixP->fx_done)
3505             value <<= 2;
3506
3507           MODIFY_VALUE (newval, value, 2, 8, 15);
3508           if (value & 3)
3509             as_bad_where (fixP->fx_file, fixP->fx_line,
3510                           _("immediate offset not 4-byte-aligned"));
3511           if (value < 0 || value > 0x1fffc)
3512             as_bad_where (fixP->fx_file, fixP->fx_line,
3513                           _("immediate offset out of range"));
3514
3515           md_number_to_chars (buf, newval, 4);
3516         }
3517       if (fixP->fx_done && fixP->fx_r_type != BFD_RELOC_C6000_SBR_U15_W)
3518         abort ();
3519       break;
3520
3521     case BFD_RELOC_C6000_DSBT_INDEX:
3522       if (value != 0)
3523         as_bad_where (fixP->fx_file, fixP->fx_line,
3524                       _("addend used with $DSBT_INDEX"));
3525       if (fixP->fx_done)
3526         abort ();
3527       break;
3528
3529     case BFD_RELOC_C6000_PCR_S21:
3530       if (fixP->fx_done || !seg->use_rela_p)
3531         {
3532           offsetT newval = md_chars_to_number (buf, 4);
3533
3534           MODIFY_VALUE (newval, value, 2, 7, 21);
3535
3536           if (value & 3)
3537             as_bad_where (fixP->fx_file, fixP->fx_line,
3538                           _("PC-relative offset not 4-byte-aligned"));
3539           if (value < -0x400000 || value > 0x3ffffc)
3540             as_bad_where (fixP->fx_file, fixP->fx_line,
3541                           _("PC-relative offset out of range"));
3542
3543           md_number_to_chars (buf, newval, 4);
3544         }
3545       break;
3546
3547     case BFD_RELOC_C6000_PCR_S12:
3548       if (fixP->fx_done || !seg->use_rela_p)
3549         {
3550           offsetT newval = md_chars_to_number (buf, 4);
3551
3552           MODIFY_VALUE (newval, value, 2, 16, 12);
3553
3554           if (value & 3)
3555             as_bad_where (fixP->fx_file, fixP->fx_line,
3556                           _("PC-relative offset not 4-byte-aligned"));
3557           if (value < -0x2000 || value > 0x1ffc)
3558             as_bad_where (fixP->fx_file, fixP->fx_line,
3559                           _("PC-relative offset out of range"));
3560
3561           md_number_to_chars (buf, newval, 4);
3562         }
3563       break;
3564
3565     case BFD_RELOC_C6000_PCR_S10:
3566       if (fixP->fx_done || !seg->use_rela_p)
3567         {
3568           offsetT newval = md_chars_to_number (buf, 4);
3569
3570           MODIFY_VALUE (newval, value, 2, 13, 10);
3571
3572           if (value & 3)
3573             as_bad_where (fixP->fx_file, fixP->fx_line,
3574                           _("PC-relative offset not 4-byte-aligned"));
3575           if (value < -0x800 || value > 0x7fc)
3576             as_bad_where (fixP->fx_file, fixP->fx_line,
3577                           _("PC-relative offset out of range"));
3578
3579           md_number_to_chars (buf, newval, 4);
3580         }
3581       break;
3582
3583     case BFD_RELOC_C6000_PCR_S7:
3584       if (fixP->fx_done || !seg->use_rela_p)
3585         {
3586           offsetT newval = md_chars_to_number (buf, 4);
3587
3588           MODIFY_VALUE (newval, value, 2, 16, 7);
3589
3590           if (value & 3)
3591             as_bad_where (fixP->fx_file, fixP->fx_line,
3592                           _("PC-relative offset not 4-byte-aligned"));
3593           if (value < -0x100 || value > 0xfc)
3594             as_bad_where (fixP->fx_file, fixP->fx_line,
3595                           _("PC-relative offset out of range"));
3596
3597           md_number_to_chars (buf, newval, 4);
3598         }
3599       break;
3600
3601     default:
3602       abort ();
3603     }
3604 }
3605
3606 /* Convert a floating-point number to target (IEEE) format.  */
3607
3608 char *
3609 md_atof (int type, char *litP, int *sizeP)
3610 {
3611   return ieee_md_atof (type, litP, sizeP, target_big_endian);
3612 }
3613
3614 /* Adjust the frags in SECTION (see tic6x_end).  */
3615
3616 static void
3617 tic6x_adjust_section (bfd *abfd ATTRIBUTE_UNUSED, segT section,
3618                       void *dummy ATTRIBUTE_UNUSED)
3619 {
3620   segment_info_type *info;
3621   frchainS *frchp;
3622   fragS *fragp;
3623   bfd_boolean have_code = FALSE;
3624   bfd_boolean have_non_code = FALSE;
3625
3626   info = seg_info (section);
3627   if (info == NULL)
3628     return;
3629
3630   for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
3631     for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
3632       switch (fragp->fr_type)
3633         {
3634         case rs_machine_dependent:
3635           if (fragp->tc_frag_data.is_insns)
3636             have_code = TRUE;
3637           break;
3638
3639         case rs_dummy:
3640         case rs_fill:
3641           if (fragp->fr_fix > 0)
3642             have_non_code = TRUE;
3643           break;
3644
3645         default:
3646           have_non_code = TRUE;
3647           break;
3648         }
3649
3650   /* Process alignment requirements in a code-only section.  */
3651   if (have_code && !have_non_code)
3652     {
3653       /* If we need to insert an odd number of instructions to meet an
3654          alignment requirement, there must have been an odd number of
3655          instructions since the last 8-byte-aligned execute packet
3656          boundary.  So there must have been an execute packet with an
3657          odd number (and so a number fewer than 8) of instructions
3658          into which we can insert a NOP without breaking any previous
3659          alignments.
3660
3661          If then we need to insert a number 2 mod 4 of instructions,
3662          the number of instructions since the last 16-byte-aligned
3663          execute packet boundary must be 2 mod 4.  So between that
3664          boundary and the following 8-byte-aligned boundary there must
3665          either be at least one execute packet with 2-mod-4
3666          instructions, or at least two with an odd number of
3667          instructions; again, greedily inserting NOPs as soon as
3668          possible suffices to meet the alignment requirement.
3669
3670          If then we need to insert 4 instructions, we look between the
3671          last 32-byte-aligned boundary and the following
3672          16-byte-aligned boundary.  The sizes of the execute packets
3673          in this range total 4 instructions mod 8, so again there is
3674          room for greedy insertion of NOPs to meet the alignment
3675          requirement, and before any intermediate point with 8-byte
3676          (2-instruction) alignment requirement the sizes of execute
3677          packets (and so the room for NOPs) will total 2 instructions
3678          mod 4 so greedy insertion will not break such alignments.
3679
3680          So we can always meet these alignment requirements by
3681          inserting NOPs in parallel with existing execute packets, and
3682          by induction the approach described above inserts the minimum
3683          number of such NOPs.  */
3684
3685       /* The number of NOPs we are currently looking to insert, if we
3686          have gone back to insert NOPs.  */
3687       unsigned int want_insert = 0;
3688
3689       /* Out of that number, the number inserted so far in the current
3690          stage of the above algorithm.  */
3691       unsigned int want_insert_done_so_far = 0;
3692
3693       /* The position mod 32 at the start of the current frag.  */
3694       unsigned int pos = 0;
3695
3696       /* The locations in the frag chain of the most recent frags at
3697          the start of which there is the given alignment.  */
3698       frchainS *frchp_last32, *frchp_last16, *frchp_last8;
3699       fragS *fragp_last32, *fragp_last16, *fragp_last8;
3700       unsigned int pos_last32, pos_last16, pos_last8;
3701
3702       frchp_last32 = frchp_last16 = frchp_last8 = info->frchainP;
3703       fragp_last32 = fragp_last16 = fragp_last8 = info->frchainP->frch_root;
3704       pos_last32 = pos_last16 = pos_last8 = 0;
3705
3706       for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
3707         for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
3708         look_at_frag:
3709           {
3710             bfd_boolean go_back = FALSE;
3711             frchainS *frchp_next;
3712             fragS *fragp_next;
3713
3714             if (fragp->fr_type != rs_machine_dependent)
3715               continue;
3716
3717             if (fragp->tc_frag_data.is_insns
3718                 && pos + fragp->fr_fix > 32
3719                 && !fragp->tc_frag_data.can_cross_fp_boundary)
3720               {
3721                 /* As described above, we should always have met an
3722                    alignment requirement by the time we come back to
3723                    it.  */
3724                 if (want_insert)
3725                   abort ();
3726
3727                 if (pos & 3)
3728                   abort ();
3729                 want_insert = (32 - pos) >> 2;
3730                 if (want_insert > 7)
3731                   abort ();
3732                 want_insert_done_so_far = 0;
3733                 go_back = TRUE;
3734               }
3735
3736             if (!fragp->tc_frag_data.is_insns)
3737               {
3738                 unsigned int would_insert_bytes;
3739
3740                 if (!(pos & ((1 << fragp->fr_offset) - 1)))
3741                   /* This alignment requirement is already met.  */
3742                   continue;
3743
3744                 /* As described above, we should always have met an
3745                    alignment requirement by the time we come back to
3746                    it.  */
3747                 if (want_insert)
3748                   abort ();
3749
3750                 /* We may not be able to meet this requirement within
3751                    the given number of characters.  */
3752                 would_insert_bytes
3753                   = ((1 << fragp->fr_offset)
3754                      - (pos & ((1 << fragp->fr_offset) - 1)));
3755
3756                 if (fragp->fr_subtype != 0
3757                     && would_insert_bytes > fragp->fr_subtype)
3758                   continue;
3759
3760                 /* An unmet alignment must be 8, 16 or 32 bytes;
3761                    smaller ones must always be met within code-only
3762                    sections and larger ones cause the section not to
3763                    be code-only.  */
3764                 if (fragp->fr_offset != 3
3765                     && fragp->fr_offset != 4
3766                     && fragp->fr_offset != 5)
3767                   abort ();
3768
3769                 if (would_insert_bytes & 3)
3770                   abort ();
3771                 want_insert = would_insert_bytes >> 2;
3772                 if (want_insert > 7)
3773                   abort ();
3774                 want_insert_done_so_far = 0;
3775                 go_back = TRUE;
3776               }
3777             else if (want_insert && !go_back)
3778               {
3779                 unsigned int num_insns = fragp->fr_fix >> 2;
3780                 unsigned int max_poss_nops = 8 - num_insns;
3781
3782                 if (max_poss_nops)
3783                   {
3784                     unsigned int cur_want_nops, max_want_nops, do_nops, i;
3785
3786                     if (want_insert & 1)
3787                       cur_want_nops = 1;
3788                     else if (want_insert & 2)
3789                       cur_want_nops = 2;
3790                     else if (want_insert & 4)
3791                       cur_want_nops = 4;
3792                     else
3793                       abort ();
3794
3795                     max_want_nops = cur_want_nops - want_insert_done_so_far;
3796
3797                     do_nops = (max_poss_nops < max_want_nops
3798                                ? max_poss_nops
3799                                : max_want_nops);
3800                     for (i = 0; i < do_nops; i++)
3801                       {
3802                         md_number_to_chars (fragp->fr_literal + fragp->fr_fix,
3803                                             0, 4);
3804                         if (target_big_endian)
3805                           fragp->fr_literal[fragp->fr_fix - 1] |= 0x1;
3806                         else
3807                           fragp->fr_literal[fragp->fr_fix - 4] |= 0x1;
3808                         fragp->fr_fix += 4;
3809                         fragp->fr_var -= 4;
3810                       }
3811                     want_insert_done_so_far += do_nops;
3812                     if (want_insert_done_so_far == cur_want_nops)
3813                       {
3814                         want_insert -= want_insert_done_so_far;
3815                         want_insert_done_so_far = 0;
3816                         if (want_insert)
3817                           go_back = TRUE;
3818                       }
3819                   }
3820               }
3821             if (go_back)
3822               {
3823                 if (want_insert & 1)
3824                   {
3825                     frchp = frchp_last8;
3826                     fragp = fragp_last8;
3827                     pos = pos_last8;
3828                   }
3829                 else if (want_insert & 2)
3830                   {
3831                     frchp = frchp_last8 = frchp_last16;
3832                     fragp = fragp_last8 = fragp_last16;
3833                     pos = pos_last8 = pos_last16;
3834                   }
3835                 else if (want_insert & 4)
3836                   {
3837                     frchp = frchp_last8 = frchp_last16 = frchp_last32;
3838                     fragp = fragp_last8 = fragp_last16 = fragp_last32;
3839                     pos = pos_last8 = pos_last16 = pos_last32;
3840                   }
3841                 else
3842                   abort ();
3843
3844                 goto look_at_frag;
3845               }
3846
3847             /* Update current position for moving past a code
3848                frag.  */
3849             pos += fragp->fr_fix;
3850             pos &= 31;
3851             frchp_next = frchp;
3852             fragp_next = fragp->fr_next;
3853             if (fragp_next == NULL)
3854               {
3855                 frchp_next = frchp->frch_next;
3856                 if (frchp_next != NULL)
3857                   fragp_next = frchp_next->frch_root;
3858               }
3859             if (!(pos & 7))
3860               {
3861                 frchp_last8 = frchp_next;
3862                 fragp_last8 = fragp_next;
3863                 pos_last8 = pos;
3864               }
3865             if (!(pos & 15))
3866               {
3867                 frchp_last16 = frchp_next;
3868                 fragp_last16 = fragp_next;
3869                 pos_last16 = pos;
3870               }
3871             if (!(pos & 31))
3872               {
3873                 frchp_last32 = frchp_next;
3874                 fragp_last32 = fragp_next;
3875                 pos_last32 = pos;
3876               }
3877           }
3878     }
3879
3880   /* Now convert the machine-dependent frags to machine-independent
3881      ones.  */
3882   for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
3883     for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
3884       {
3885         if (fragp->fr_type == rs_machine_dependent)
3886           {
3887             if (fragp->tc_frag_data.is_insns)
3888               frag_wane (fragp);
3889             else
3890               {
3891                 fragp->fr_type = rs_align_code;
3892                 fragp->fr_var = 1;
3893                 *fragp->fr_literal = 0;
3894               }
3895           }
3896       }
3897 }
3898
3899 /* Initialize the machine-dependent parts of a frag.  */
3900
3901 void
3902 tic6x_frag_init (fragS *fragp)
3903 {
3904   fragp->tc_frag_data.is_insns = FALSE;
3905   fragp->tc_frag_data.can_cross_fp_boundary = FALSE;
3906 }
3907
3908 /* Set an attribute if it has not already been set by the user.  */
3909
3910 static void
3911 tic6x_set_attribute_int (int tag, int value)
3912 {
3913   if (tag < 1
3914       || tag >= NUM_KNOWN_OBJ_ATTRIBUTES)
3915     abort ();
3916   if (!tic6x_attributes_set_explicitly[tag])
3917     bfd_elf_add_proc_attr_int (stdoutput, tag, value);
3918 }
3919
3920 /* Set object attributes deduced from the input file and command line
3921    rather than given explicitly.  */
3922 static void
3923 tic6x_set_attributes (void)
3924 {
3925   if (tic6x_arch_attribute == C6XABI_Tag_ISA_none)
3926     tic6x_arch_attribute = C6XABI_Tag_ISA_C674X;
3927
3928   tic6x_set_attribute_int (Tag_ISA, tic6x_arch_attribute);
3929   tic6x_set_attribute_int (Tag_ABI_DSBT, tic6x_dsbt);
3930   tic6x_set_attribute_int (Tag_ABI_PID, tic6x_pid);
3931   tic6x_set_attribute_int (Tag_ABI_PIC, tic6x_pic);
3932 }
3933
3934 /* Do machine-dependent manipulations of the frag chains after all
3935    input has been read and before the machine-independent sizing and
3936    relaxing.  */
3937
3938 void
3939 tic6x_end (void)
3940 {
3941   /* Set object attributes at this point if not explicitly set.  */
3942   tic6x_set_attributes ();
3943
3944   /* Meeting alignment requirements may require inserting NOPs in
3945      parallel in execute packets earlier in the segment.  Future
3946      16-bit instruction generation involves whole-segment optimization
3947      to determine the best choice and ordering of 32-bit or 16-bit
3948      instructions.  This doesn't fit will in the general relaxation
3949      framework, so handle alignment and 16-bit instruction generation
3950      here.  */
3951   bfd_map_over_sections (stdoutput, tic6x_adjust_section, NULL);
3952 }
3953
3954 /* No machine-dependent frags at this stage; all converted in
3955    tic6x_end.  */
3956
3957 void
3958 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
3959                  fragS *fragp ATTRIBUTE_UNUSED)
3960 {
3961   abort ();
3962 }
3963
3964 /* No machine-dependent frags at this stage; all converted in
3965    tic6x_end.  */
3966
3967 int
3968 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
3969                                segT seg ATTRIBUTE_UNUSED)
3970 {
3971   abort ();
3972 }
3973
3974 /* Put a number into target byte order.  */
3975
3976 void
3977 md_number_to_chars (char *buf, valueT val, int n)
3978 {
3979   if (target_big_endian)
3980     number_to_chars_bigendian (buf, val, n);
3981   else
3982     number_to_chars_littleendian (buf, val, n);
3983 }
3984
3985 /* Machine-dependent operand parsing not currently needed.  */
3986
3987 void
3988 md_operand (expressionS *op ATTRIBUTE_UNUSED)
3989 {
3990 }
3991
3992 /* PC-relative operands are relative to the start of the fetch
3993    packet.  */
3994
3995 long
3996 tic6x_pcrel_from_section (fixS *fixp, segT sec)
3997 {
3998   if (fixp->fx_addsy != NULL
3999       && (!S_IS_DEFINED (fixp->fx_addsy)
4000           || S_GET_SEGMENT (fixp->fx_addsy) != sec))
4001     return 0;
4002   return (fixp->fx_where + fixp->fx_frag->fr_address) & ~(long) 0x1f;
4003 }
4004
4005 /* Round up a section size to the appropriate boundary.  */
4006
4007 valueT
4008 md_section_align (segT segment ATTRIBUTE_UNUSED,
4009                   valueT size)
4010 {
4011   /* Round up section sizes to ensure that text sections consist of
4012      whole fetch packets.  */
4013   int align = bfd_get_section_alignment (stdoutput, segment);
4014   return ((size + (1 << align) - 1) & ((valueT) -1 << align));
4015 }
4016
4017 /* No special undefined symbol handling needed for now.  */
4018
4019 symbolS *
4020 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
4021 {
4022   return NULL;
4023 }
4024
4025 /* Translate internal representation of relocation info to BFD target
4026    format.  */
4027
4028 arelent *
4029 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
4030 {
4031   arelent *reloc;
4032   bfd_reloc_code_real_type r_type;
4033
4034   reloc = xmalloc (sizeof (arelent));
4035   reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
4036   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
4037   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
4038   reloc->addend = (tic6x_generate_rela ? fixp->fx_offset : 0);
4039   r_type = fixp->fx_r_type;
4040   reloc->howto = bfd_reloc_type_lookup (stdoutput, r_type);
4041
4042   if (reloc->howto == NULL)
4043     {
4044       as_bad_where (fixp->fx_file, fixp->fx_line,
4045                     _("Cannot represent relocation type %s"),
4046                     bfd_get_reloc_code_name (r_type));
4047       return NULL;
4048     }
4049
4050   /* Correct for adjustments bfd_install_relocation will make.  */
4051   if (reloc->howto->pcrel_offset && reloc->howto->partial_inplace)
4052     reloc->addend += reloc->address;
4053
4054   return reloc;
4055 }