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