]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-tic6x.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / gas / config / tc-tic6x.c
1 /* TI C6X assembler.
2 Copyright (C) 2010-2021 Free Software Foundation, Inc.
3 Contributed by Joseph Myers <joseph@codesourcery.com>
4 Bernd Schmidt <bernds@codesourcery.com>
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "as.h"
24 #include "dwarf2dbg.h"
25 #include "dw2gencfi.h"
26 #include "safe-ctype.h"
27 #include "subsegs.h"
28 #include "opcode/tic6x.h"
29 #include "elf/tic6x.h"
30 #include "elf32-tic6x.h"
31
32 /* Truncate and sign-extend at 32 bits, so that building on a 64-bit
33 host gives identical results to a 32-bit host. */
34 #define TRUNC(X) ((valueT) (X) & 0xffffffffU)
35 #define SEXT(X) ((TRUNC (X) ^ 0x80000000U) - 0x80000000U)
36
37 #define streq(a, b) (strcmp (a, b) == 0)
38
39 /* Stuff for .scomm symbols. */
40 static segT sbss_section;
41 static asection scom_section;
42 static asymbol scom_symbol;
43
44 const char comment_chars[] = ";";
45 const char line_comment_chars[] = "#*;";
46 const char line_separator_chars[] = "@";
47
48 const char EXP_CHARS[] = "eE";
49 const char FLT_CHARS[] = "dDfF";
50
51 const char *md_shortopts = "";
52
53 enum
54 {
55 OPTION_MARCH = OPTION_MD_BASE,
56 OPTION_MBIG_ENDIAN,
57 OPTION_MLITTLE_ENDIAN,
58 OPTION_MDSBT,
59 OPTION_MNO_DSBT,
60 OPTION_MPID,
61 OPTION_MPIC,
62 OPTION_MNO_PIC,
63 OPTION_MGENERATE_REL
64 };
65
66 struct option md_longopts[] =
67 {
68 { "march", required_argument, NULL, OPTION_MARCH },
69 { "mbig-endian", no_argument, NULL, OPTION_MBIG_ENDIAN },
70 { "mlittle-endian", no_argument, NULL, OPTION_MLITTLE_ENDIAN },
71 { "mdsbt", no_argument, NULL, OPTION_MDSBT },
72 { "mno-dsbt", no_argument, NULL, OPTION_MNO_DSBT },
73 { "mpid", required_argument, NULL, OPTION_MPID },
74 { "mpic", no_argument, NULL, OPTION_MPIC },
75 { "mno-pic", no_argument, NULL, OPTION_MNO_PIC },
76 { "mgenerate-rel", no_argument, NULL, OPTION_MGENERATE_REL },
77 { NULL, no_argument, NULL, 0 }
78 };
79 size_t md_longopts_size = sizeof (md_longopts);
80
81 /* The instructions enabled based only on the selected architecture
82 (all instructions, if no architecture specified). */
83 static unsigned short tic6x_arch_enable = (TIC6X_INSN_C62X
84 | TIC6X_INSN_C64X
85 | TIC6X_INSN_C64XP
86 | TIC6X_INSN_C67X
87 | TIC6X_INSN_C67XP
88 | TIC6X_INSN_C674X);
89
90 /* The instructions enabled based on the current set of features
91 (architecture, as modified by other options). */
92 static unsigned short tic6x_features;
93
94 /* The architecture attribute value, or C6XABI_Tag_ISA_none if
95 not yet set. */
96 static int tic6x_arch_attribute = C6XABI_Tag_ISA_none;
97
98 /* Whether any instructions at all have been seen. Once any
99 instructions have been seen, architecture attributes merge into the
100 previous attribute value rather than replacing it. */
101 static bfd_boolean tic6x_seen_insns = FALSE;
102
103 /* The number of registers in each register file supported by the
104 current architecture. */
105 static unsigned int tic6x_num_registers;
106
107 /* Whether predication on A0 is possible. */
108 static bfd_boolean tic6x_predicate_a0;
109
110 /* Whether execute packets can cross fetch packet boundaries. */
111 static bfd_boolean tic6x_can_cross_fp_boundary;
112
113 /* Whether there are constraints on simultaneous reads and writes of
114 40-bit data. */
115 static bfd_boolean tic6x_long_data_constraints;
116
117 /* Whether compact instructions are available. */
118 static bfd_boolean tic6x_compact_insns;
119
120 /* Whether to generate RELA relocations. */
121 static bfd_boolean tic6x_generate_rela = TRUE;
122
123 /* Whether the code uses DSBT addressing. */
124 static bfd_boolean tic6x_dsbt;
125
126 /* Types of position-independent data (attribute values for
127 Tag_ABI_PID). */
128 typedef enum
129 {
130 tic6x_pid_no = 0,
131 tic6x_pid_near = 1,
132 tic6x_pid_far = 2
133 } tic6x_pid_type;
134
135 /* The type of data addressing used in this code. */
136 static tic6x_pid_type tic6x_pid;
137
138 /* Whether the code uses position-independent code. */
139 static bfd_boolean tic6x_pic;
140
141 /* Table of supported architecture variants. */
142 typedef struct
143 {
144 const char *arch;
145 int attr;
146 unsigned short features;
147 } tic6x_arch_table;
148 static const tic6x_arch_table tic6x_arches[] =
149 {
150 { "c62x", C6XABI_Tag_ISA_C62X, TIC6X_INSN_C62X },
151 { "c64x", C6XABI_Tag_ISA_C64X, TIC6X_INSN_C62X | TIC6X_INSN_C64X },
152 { "c64x+", C6XABI_Tag_ISA_C64XP, (TIC6X_INSN_C62X
153 | TIC6X_INSN_C64X
154 | TIC6X_INSN_C64XP) },
155 { "c67x", C6XABI_Tag_ISA_C67X, TIC6X_INSN_C62X | TIC6X_INSN_C67X },
156 { "c67x+", C6XABI_Tag_ISA_C67XP, (TIC6X_INSN_C62X
157 | TIC6X_INSN_C67X
158 | TIC6X_INSN_C67XP) },
159 { "c674x", C6XABI_Tag_ISA_C674X, (TIC6X_INSN_C62X
160 | TIC6X_INSN_C64X
161 | TIC6X_INSN_C64XP
162 | TIC6X_INSN_C67X
163 | TIC6X_INSN_C67XP
164 | TIC6X_INSN_C674X) }
165 };
166
167 /* Caller saved register encodings. The standard frame layout uses this
168 order, starting from the highest address. There must be
169 TIC6X_NUM_UNWIND_REGS values. */
170 enum
171 {
172 UNWIND_A15,
173 UNWIND_B15,
174 UNWIND_B14,
175 UNWIND_B13,
176 UNWIND_B12,
177 UNWIND_B11,
178 UNWIND_B10,
179 UNWIND_B3,
180 UNWIND_A14,
181 UNWIND_A13,
182 UNWIND_A12,
183 UNWIND_A11,
184 UNWIND_A10
185 };
186
187 static void tic6x_output_unwinding (bfd_boolean need_extab);
188
189 /* Return the frame unwind state for the current function, allocating
190 as necessary. */
191
192 static tic6x_unwind_info *tic6x_get_unwind (void)
193 {
194 tic6x_unwind_info *unwind;
195
196 unwind = seg_info (now_seg)->tc_segment_info_data.unwind;
197 if (unwind)
198 return unwind;
199
200 unwind = seg_info (now_seg)->tc_segment_info_data.text_unwind;
201 if (unwind)
202 return unwind;
203
204 unwind =XNEW (tic6x_unwind_info);
205 seg_info (now_seg)->tc_segment_info_data.unwind = unwind;
206 memset (unwind, 0, sizeof (*unwind));
207 return unwind;
208 }
209
210 /* Update the selected architecture based on ARCH, giving an error if
211 ARCH is an invalid value. Does not call tic6x_update_features; the
212 caller must do that if necessary. */
213
214 static void
215 tic6x_use_arch (const char *arch)
216 {
217 unsigned int i;
218
219 for (i = 0; i < ARRAY_SIZE (tic6x_arches); i++)
220 if (strcmp (arch, tic6x_arches[i].arch) == 0)
221 {
222 tic6x_arch_enable = tic6x_arches[i].features;
223 if (tic6x_seen_insns)
224 tic6x_arch_attribute
225 = elf32_tic6x_merge_arch_attributes (tic6x_arch_attribute,
226 tic6x_arches[i].attr);
227 else
228 tic6x_arch_attribute = tic6x_arches[i].attr;
229 return;
230 }
231
232 as_bad (_("unknown architecture '%s'"), arch);
233 }
234
235 /* Table of supported -mpid arguments. */
236 typedef struct
237 {
238 const char *arg;
239 tic6x_pid_type attr;
240 } tic6x_pid_type_table;
241 static const tic6x_pid_type_table tic6x_pid_types[] =
242 {
243 { "no", tic6x_pid_no },
244 { "near", tic6x_pid_near },
245 { "far", tic6x_pid_far }
246 };
247
248 /* Handle -mpid=ARG. */
249
250 static void
251 tic6x_use_pid (const char *arg)
252 {
253 unsigned int i;
254
255 for (i = 0; i < ARRAY_SIZE (tic6x_pid_types); i++)
256 if (strcmp (arg, tic6x_pid_types[i].arg) == 0)
257 {
258 tic6x_pid = tic6x_pid_types[i].attr;
259 return;
260 }
261
262 as_bad (_("unknown -mpid= argument '%s'"), arg);
263 }
264
265 /* Parse a target-specific option. */
266
267 int
268 md_parse_option (int c, const char *arg)
269 {
270 switch (c)
271 {
272 case OPTION_MARCH:
273 tic6x_use_arch (arg);
274 break;
275
276 case OPTION_MBIG_ENDIAN:
277 target_big_endian = 1;
278 break;
279
280 case OPTION_MLITTLE_ENDIAN:
281 target_big_endian = 0;
282 break;
283
284 case OPTION_MDSBT:
285 tic6x_dsbt = 1;
286 break;
287
288 case OPTION_MNO_DSBT:
289 tic6x_dsbt = 0;
290 break;
291
292 case OPTION_MPID:
293 tic6x_use_pid (arg);
294 break;
295
296 case OPTION_MPIC:
297 tic6x_pic = 1;
298 break;
299
300 case OPTION_MNO_PIC:
301 tic6x_pic = 0;
302 break;
303
304 case OPTION_MGENERATE_REL:
305 tic6x_generate_rela = FALSE;
306 break;
307
308 default:
309 return 0;
310 }
311 return 1;
312 }
313
314 void
315 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
316 {
317 unsigned int i;
318
319 fputc ('\n', stream);
320 fprintf (stream, _("TMS320C6000 options:\n"));
321 fprintf (stream, _(" -march=ARCH enable instructions from architecture ARCH\n"));
322 fprintf (stream, _(" -mbig-endian generate big-endian code\n"));
323 fprintf (stream, _(" -mlittle-endian generate little-endian code\n"));
324 fprintf (stream, _(" -mdsbt code uses DSBT addressing\n"));
325 fprintf (stream, _(" -mno-dsbt code does not use DSBT addressing\n"));
326 fprintf (stream, _(" -mpid=no code uses position-dependent data addressing\n"));
327 fprintf (stream, _(" -mpid=near code uses position-independent data addressing,\n"
328 " GOT accesses use near DP addressing\n"));
329 fprintf (stream, _(" -mpid=far code uses position-independent data addressing,\n"
330 " GOT accesses use far DP addressing\n"));
331 fprintf (stream, _(" -mpic code addressing is position-independent\n"));
332 fprintf (stream, _(" -mno-pic code addressing is position-dependent\n"));
333 /* -mgenerate-rel is only for testsuite use and is deliberately
334 undocumented. */
335
336 fputc ('\n', stream);
337 fprintf (stream, _("Supported ARCH values are:"));
338 for (i = 0; i < ARRAY_SIZE (tic6x_arches); i++)
339 fprintf (stream, " %s", tic6x_arches[i].arch);
340 fputc ('\n', stream);
341 }
342
343 /* Update enabled features based on the current architecture and
344 related settings. */
345 static void
346 tic6x_update_features (void)
347 {
348 tic6x_features = tic6x_arch_enable;
349
350 tic6x_num_registers
351 = (tic6x_arch_enable & (TIC6X_INSN_C64X | TIC6X_INSN_C67XP)) ? 32 : 16;
352
353 tic6x_predicate_a0 = (tic6x_arch_enable & TIC6X_INSN_C64X) ? TRUE : FALSE;
354
355 tic6x_can_cross_fp_boundary
356 = (tic6x_arch_enable
357 & (TIC6X_INSN_C64X | TIC6X_INSN_C67XP)) ? TRUE : FALSE;
358
359 tic6x_long_data_constraints
360 = (tic6x_arch_enable & TIC6X_INSN_C64X) ? FALSE : TRUE;
361
362 tic6x_compact_insns = (tic6x_arch_enable & TIC6X_INSN_C64XP) ? TRUE : FALSE;
363 }
364
365 /* Do configuration after all options have been parsed. */
366
367 void
368 tic6x_after_parse_args (void)
369 {
370 tic6x_update_features ();
371 }
372
373 /* Parse a .cantunwind directive. */
374 static void
375 s_tic6x_cantunwind (int ignored ATTRIBUTE_UNUSED)
376 {
377 tic6x_unwind_info *unwind = tic6x_get_unwind ();
378
379 /* GCC sometimes spits out superfluous .cantunwind directives, so ignore
380 them. */
381 if (unwind->data_bytes == 0)
382 return;
383
384 if (unwind->data_bytes != -1)
385 {
386 as_bad (_("unexpected .cantunwind directive"));
387 return;
388 }
389
390 demand_empty_rest_of_line ();
391
392 if (unwind->personality_routine || unwind->personality_index != -1)
393 as_bad (_("personality routine specified for cantunwind frame"));
394
395 unwind->personality_index = -2;
396 }
397
398 /* Parse a .handlerdata directive. */
399 static void
400 s_tic6x_handlerdata (int ignored ATTRIBUTE_UNUSED)
401 {
402 tic6x_unwind_info *unwind = tic6x_get_unwind ();
403
404 if (!unwind->saved_seg)
405 {
406 as_bad (_("unexpected .handlerdata directive"));
407 return;
408 }
409
410 if (unwind->table_entry || unwind->personality_index == -2)
411 {
412 as_bad (_("duplicate .handlerdata directive"));
413 return;
414 }
415
416 if (unwind->personality_index == -1 && unwind->personality_routine == NULL)
417 {
418 as_bad (_("personality routine required before .handlerdata directive"));
419 return;
420 }
421
422 tic6x_output_unwinding (TRUE);
423 }
424
425 /* Parse a .endp directive. */
426 static void
427 s_tic6x_endp (int ignored ATTRIBUTE_UNUSED)
428 {
429 tic6x_unwind_info *unwind = tic6x_get_unwind ();
430
431 if (unwind->data_bytes != 0)
432 {
433 /* Output a .exidx entry if we have not already done so.
434 Then switch back to the text section. */
435 if (!unwind->table_entry)
436 tic6x_output_unwinding (FALSE);
437
438 subseg_set (unwind->saved_seg, unwind->saved_subseg);
439 }
440
441 unwind->saved_seg = NULL;
442 unwind->table_entry = NULL;
443 unwind->data_bytes = 0;
444 }
445
446 /* Parse a .personalityindex directive. */
447 static void
448 s_tic6x_personalityindex (int ignored ATTRIBUTE_UNUSED)
449 {
450 tic6x_unwind_info *unwind = tic6x_get_unwind ();
451 expressionS exp;
452
453 if (unwind->personality_routine || unwind->personality_index != -1)
454 as_bad (_("duplicate .personalityindex directive"));
455
456 expression (&exp);
457
458 if (exp.X_op != O_constant
459 || exp.X_add_number < 0 || exp.X_add_number > 15)
460 {
461 as_bad (_("bad personality routine number"));
462 ignore_rest_of_line ();
463 return;
464 }
465
466 unwind->personality_index = exp.X_add_number;
467
468 demand_empty_rest_of_line ();
469 }
470
471 static void
472 s_tic6x_personality (int ignored ATTRIBUTE_UNUSED)
473 {
474 char *name, c;
475 tic6x_unwind_info *unwind = tic6x_get_unwind ();
476
477 if (unwind->personality_routine || unwind->personality_index != -1)
478 as_bad (_("duplicate .personality directive"));
479
480 c = get_symbol_name (&name);
481 unwind->personality_routine = symbol_find_or_make (name);
482 (void) restore_line_pointer (c);
483 demand_empty_rest_of_line ();
484 }
485
486 /* Parse a .arch directive. */
487 static void
488 s_tic6x_arch (int ignored ATTRIBUTE_UNUSED)
489 {
490 char c;
491 char *arch;
492
493 arch = input_line_pointer;
494 while (*input_line_pointer && !ISSPACE (*input_line_pointer))
495 input_line_pointer++;
496 c = *input_line_pointer;
497 *input_line_pointer = 0;
498
499 tic6x_use_arch (arch);
500 tic6x_update_features ();
501 *input_line_pointer = c;
502 demand_empty_rest_of_line ();
503 }
504
505 /* Parse a .ehtype directive. */
506
507 static void
508 s_tic6x_ehtype (int ignored ATTRIBUTE_UNUSED)
509 {
510 expressionS exp;
511 char *p;
512
513 #ifdef md_flush_pending_output
514 md_flush_pending_output ();
515 #endif
516
517 if (is_it_end_of_statement ())
518 {
519 demand_empty_rest_of_line ();
520 return;
521 }
522
523 #ifdef md_cons_align
524 md_cons_align (4);
525 #endif
526
527
528 expression (&exp);
529
530 if (exp.X_op != O_symbol)
531 {
532 as_bad (_("expected symbol"));
533 return;
534 }
535
536 p = frag_more (4);
537 memset (p, 0, 4);
538 fix_new_exp (frag_now, p - frag_now->fr_literal, 4,
539 &exp, 0, BFD_RELOC_C6000_EHTYPE);
540
541 demand_empty_rest_of_line ();
542 }
543
544 /* Parse a .nocmp directive. */
545
546 static void
547 s_tic6x_nocmp (int ignored ATTRIBUTE_UNUSED)
548 {
549 seg_info (now_seg)->tc_segment_info_data.nocmp = TRUE;
550 demand_empty_rest_of_line ();
551 }
552
553 /* .scomm pseudo-op handler.
554
555 This is a new pseudo-op to handle putting objects in .scommon.
556 By doing this the linker won't need to do any work,
557 and more importantly it removes the implicit -G arg necessary to
558 correctly link the object file. */
559
560 static void
561 s_tic6x_scomm (int ignore ATTRIBUTE_UNUSED)
562 {
563 char *name;
564 char c;
565 char *p;
566 offsetT size;
567 symbolS *symbolP;
568 offsetT align;
569 int align2;
570
571 c = get_symbol_name (&name);
572
573 /* Just after name is now '\0'. */
574 p = input_line_pointer;
575 (void) restore_line_pointer (c);
576 SKIP_WHITESPACE ();
577 if (*input_line_pointer != ',')
578 {
579 as_bad (_("expected comma after symbol name"));
580 ignore_rest_of_line ();
581 return;
582 }
583
584 /* Skip ','. */
585 input_line_pointer++;
586 if ((size = get_absolute_expression ()) < 0)
587 {
588 /* xgettext:c-format */
589 as_warn (_("invalid length for .scomm directive"));
590 ignore_rest_of_line ();
591 return;
592 }
593
594 /* The third argument to .scomm is the alignment. */
595 if (*input_line_pointer != ',')
596 align = 8;
597 else
598 {
599 ++input_line_pointer;
600 align = get_absolute_expression ();
601 if (align <= 0)
602 {
603 as_warn (_("alignment is not a positive number"));
604 align = 8;
605 }
606 }
607
608 /* Convert to a power of 2 alignment. */
609 if (align)
610 {
611 for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2)
612 continue;
613 if (align != 1)
614 {
615 as_bad (_("alignment is not a power of 2"));
616 ignore_rest_of_line ();
617 return;
618 }
619 }
620 else
621 align2 = 0;
622
623 *p = 0;
624 symbolP = symbol_find_or_make (name);
625 *p = c;
626
627 if (S_IS_DEFINED (symbolP))
628 {
629 /* xgettext:c-format */
630 as_bad (_("attempt to re-define symbol `%s'"),
631 S_GET_NAME (symbolP));
632 ignore_rest_of_line ();
633 return;
634 }
635
636 if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
637 {
638 /* xgettext:c-format */
639 as_bad (_("attempt to redefine `%s' with a different length"),
640 S_GET_NAME (symbolP));
641
642 ignore_rest_of_line ();
643 return;
644 }
645
646 if (symbol_get_obj (symbolP)->local)
647 {
648 segT old_sec = now_seg;
649 int old_subsec = now_subseg;
650 char *pfrag;
651
652 record_alignment (sbss_section, align2);
653 subseg_set (sbss_section, 0);
654
655 if (align2)
656 frag_align (align2, 0, 0);
657
658 if (S_GET_SEGMENT (symbolP) == sbss_section)
659 symbol_get_frag (symbolP)->fr_symbol = 0;
660
661 symbol_set_frag (symbolP, frag_now);
662
663 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
664 (char *) 0);
665 *pfrag = 0;
666 S_SET_SIZE (symbolP, size);
667 S_SET_SEGMENT (symbolP, sbss_section);
668 S_CLEAR_EXTERNAL (symbolP);
669 subseg_set (old_sec, old_subsec);
670 }
671 else
672 {
673 S_SET_VALUE (symbolP, (valueT) size);
674 S_SET_ALIGN (symbolP, 1 << align2);
675 S_SET_EXTERNAL (symbolP);
676 S_SET_SEGMENT (symbolP, &scom_section);
677 }
678
679 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
680
681 demand_empty_rest_of_line ();
682 }
683
684 /* Track for each attribute whether it has been set explicitly (and so
685 should not have a default value set by the assembler). */
686 static bfd_boolean tic6x_attributes_set_explicitly[NUM_KNOWN_OBJ_ATTRIBUTES];
687
688 /* Parse a .c6xabi_attribute directive. */
689
690 static void
691 s_tic6x_c6xabi_attribute (int ignored ATTRIBUTE_UNUSED)
692 {
693 int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
694
695 if (tag < NUM_KNOWN_OBJ_ATTRIBUTES)
696 tic6x_attributes_set_explicitly[tag] = TRUE;
697 }
698
699 typedef struct
700 {
701 const char *name;
702 int tag;
703 } tic6x_attribute_table;
704
705 static const tic6x_attribute_table tic6x_attributes[] =
706 {
707 #define TAG(tag, value) { #tag, tag },
708 #include "elf/tic6x-attrs.h"
709 #undef TAG
710 };
711
712 /* Convert an attribute name to a number. */
713
714 int
715 tic6x_convert_symbolic_attribute (const char *name)
716 {
717 unsigned int i;
718
719 for (i = 0; i < ARRAY_SIZE (tic6x_attributes); i++)
720 if (strcmp (name, tic6x_attributes[i].name) == 0)
721 return tic6x_attributes[i].tag;
722
723 return -1;
724 }
725
726 const pseudo_typeS md_pseudo_table[] =
727 {
728 { "arch", s_tic6x_arch, 0 },
729 { "c6xabi_attribute", s_tic6x_c6xabi_attribute, 0 },
730 { "nocmp", s_tic6x_nocmp, 0 },
731 { "scomm", s_tic6x_scomm, 0 },
732 { "word", cons, 4 },
733 { "ehtype", s_tic6x_ehtype, 0 },
734 { "endp", s_tic6x_endp, 0 },
735 { "handlerdata", s_tic6x_handlerdata, 0 },
736 { "personalityindex", s_tic6x_personalityindex, 0 },
737 { "personality", s_tic6x_personality, 0 },
738 { "cantunwind", s_tic6x_cantunwind, 0 },
739 { 0, 0, 0 }
740 };
741
742 /* Hash table of opcodes. For each opcode name, this stores a pointer
743 to a tic6x_opcode_list listing (in an arbitrary order) all opcode
744 table entries with that name. */
745 static htab_t opcode_hash;
746
747 /* Initialize the assembler (called once at assembler startup). */
748
749 void
750 md_begin (void)
751 {
752 tic6x_opcode_id id;
753 flagword applicable;
754 segT seg;
755 subsegT subseg;
756
757 bfd_set_arch_mach (stdoutput, TARGET_ARCH, 0);
758
759 /* Insert opcodes into the hash table. */
760 opcode_hash = str_htab_create ();
761 for (id = 0; id < tic6x_opcode_max; id++)
762 {
763 tic6x_opcode_list *opc = XNEW (tic6x_opcode_list);
764
765 opc->id = id;
766 opc->next = str_hash_find (opcode_hash, tic6x_opcode_table[id].name);
767 str_hash_insert (opcode_hash, tic6x_opcode_table[id].name, opc, 1);
768 }
769
770 /* Save the current subseg so we can restore it [it's the default one and
771 we don't want the initial section to be .sbss]. */
772 seg = now_seg;
773 subseg = now_subseg;
774
775 /* The sbss section is for local .scomm symbols. */
776 sbss_section = subseg_new (".bss", 0);
777 seg_info (sbss_section)->bss = 1;
778
779 /* This is copied from perform_an_assembly_pass. */
780 applicable = bfd_applicable_section_flags (stdoutput);
781 bfd_set_section_flags (sbss_section, applicable & SEC_ALLOC);
782
783 subseg_set (seg, subseg);
784
785 /* We must construct a fake section similar to bfd_com_section
786 but with the name .scommon. */
787 scom_section = *bfd_com_section_ptr;
788 scom_section.name = ".scommon";
789 scom_section.output_section = & scom_section;
790 scom_section.symbol = & scom_symbol;
791 scom_section.symbol_ptr_ptr = & scom_section.symbol;
792 scom_symbol = * bfd_com_section_ptr->symbol;
793 scom_symbol.name = ".scommon";
794 scom_symbol.section = & scom_section;
795 }
796
797 /* Whether the current line being parsed had the "||" parallel bars. */
798 static bfd_boolean tic6x_line_parallel;
799
800 /* Whether the current line being parsed started "||^" to indicate an
801 SPMASKed parallel instruction. */
802 static bfd_boolean tic6x_line_spmask;
803
804 /* If the current line being parsed had an instruction predicate, the
805 creg value for that predicate (which must be nonzero); otherwise
806 0. */
807 static unsigned int tic6x_line_creg;
808
809 /* If the current line being parsed had an instruction predicate, the
810 z value for that predicate; otherwise 0. */
811 static unsigned int tic6x_line_z;
812
813 /* Return 1 (updating input_line_pointer as appropriate) if the line
814 starting with C (immediately before input_line_pointer) starts with
815 pre-opcode text appropriate for this target, 0 otherwise. */
816
817 int
818 tic6x_unrecognized_line (int c)
819 {
820 char *p, *endp;
821 unsigned int z;
822 bfd_boolean areg;
823 bfd_boolean bad_predicate;
824
825 switch (c)
826 {
827 case '|':
828 if (input_line_pointer[0] == '|')
829 {
830 if (input_line_pointer[1] == '^')
831 {
832 tic6x_line_spmask = TRUE;
833 input_line_pointer += 2;
834 }
835 else
836 input_line_pointer += 1;
837 if (tic6x_line_parallel)
838 as_bad (_("multiple '||' on same line"));
839 tic6x_line_parallel = TRUE;
840 if (tic6x_line_creg)
841 as_bad (_("'||' after predicate"));
842 return 1;
843 }
844 return 0;
845
846 case '[':
847 /* If it doesn't look like a predicate at all, just return 0.
848 If it looks like one but not a valid one, give a better
849 error. */
850 p = input_line_pointer;
851 while (*p != ']' && !is_end_of_line[(unsigned char) *p])
852 p++;
853 if (*p != ']')
854 return 0;
855 endp = p + 1;
856 p = input_line_pointer;
857 z = 0;
858 bad_predicate = FALSE;
859 if (*p == '!')
860 {
861 z = 1;
862 p++;
863 }
864 if (*p == 'A' || *p == 'a')
865 areg = TRUE;
866 else if (*p == 'B' || *p == 'b')
867 areg = FALSE;
868 else
869 {
870 areg = TRUE; /* Avoid uninitialized warning. */
871 bad_predicate = TRUE;
872 }
873 if (!bad_predicate)
874 {
875 p++;
876 if (*p != '0' && *p != '1' && *p != '2')
877 bad_predicate = TRUE;
878 else if (p[1] != ']')
879 bad_predicate = TRUE;
880 else
881 input_line_pointer = p + 2;
882 }
883
884 if (tic6x_line_creg)
885 as_bad (_("multiple predicates on same line"));
886
887 if (bad_predicate)
888 {
889 char ctmp = *endp;
890 *endp = 0;
891 as_bad (_("bad predicate '%s'"), input_line_pointer - 1);
892 *endp = ctmp;
893 input_line_pointer = endp;
894 return 1;
895 }
896
897 switch (*p)
898 {
899 case '0':
900 tic6x_line_creg = (areg ? 6 : 1);
901 if (areg && !tic6x_predicate_a0)
902 as_bad (_("predication on A0 not supported on this architecture"));
903 break;
904
905 case '1':
906 tic6x_line_creg = (areg ? 4 : 2);
907 break;
908
909 case '2':
910 tic6x_line_creg = (areg ? 5 : 3);
911 break;
912
913 default:
914 abort ();
915 }
916
917 tic6x_line_z = z;
918 return 1;
919
920 default:
921 return 0;
922 }
923 }
924
925 /* Do any target-specific handling of a label required. */
926
927 void
928 tic6x_frob_label (symbolS *sym)
929 {
930 segment_info_type *si;
931 tic6x_label_list *list;
932
933 if (tic6x_line_parallel)
934 {
935 as_bad (_("label after '||'"));
936 tic6x_line_parallel = FALSE;
937 tic6x_line_spmask = FALSE;
938 }
939 if (tic6x_line_creg)
940 {
941 as_bad (_("label after predicate"));
942 tic6x_line_creg = 0;
943 tic6x_line_z = 0;
944 }
945
946 si = seg_info (now_seg);
947 list = si->tc_segment_info_data.label_list;
948 si->tc_segment_info_data.label_list = XNEW (tic6x_label_list);
949 si->tc_segment_info_data.label_list->next = list;
950 si->tc_segment_info_data.label_list->label = sym;
951
952 /* Defining tc_frob_label overrides the ELF definition of
953 obj_frob_label, so we need to apply its effects here. */
954 dwarf2_emit_label (sym);
955 }
956
957 /* At end-of-line, give errors for start-of-line decorations that
958 needed an instruction but were not followed by one. */
959
960 static void
961 tic6x_end_of_line (void)
962 {
963 if (tic6x_line_parallel)
964 {
965 as_bad (_("'||' not followed by instruction"));
966 tic6x_line_parallel = FALSE;
967 tic6x_line_spmask = FALSE;
968 }
969 if (tic6x_line_creg)
970 {
971 as_bad (_("predicate not followed by instruction"));
972 tic6x_line_creg = 0;
973 tic6x_line_z = 0;
974 }
975 }
976
977 /* Do any target-specific handling of the start of a logical line. */
978
979 void
980 tic6x_start_line_hook (void)
981 {
982 tic6x_end_of_line ();
983 }
984
985 /* Do target-specific handling immediately after an input file from
986 the command line, and any other inputs it includes, have been
987 read. */
988
989 void
990 tic6x_cleanup (void)
991 {
992 tic6x_end_of_line ();
993 }
994
995 /* Do target-specific initialization after arguments have been
996 processed and the output file created. */
997
998 void
999 tic6x_init_after_args (void)
1000 {
1001 elf32_tic6x_set_use_rela_p (stdoutput, tic6x_generate_rela);
1002 }
1003
1004 /* Free LIST of labels (possibly NULL). */
1005
1006 static void
1007 tic6x_free_label_list (tic6x_label_list *list)
1008 {
1009 while (list)
1010 {
1011 tic6x_label_list *old = list;
1012
1013 list = list->next;
1014 free (old);
1015 }
1016 }
1017
1018 /* Handle a data alignment of N bytes. */
1019
1020 void
1021 tic6x_cons_align (int n ATTRIBUTE_UNUSED)
1022 {
1023 segment_info_type *seginfo = seg_info (now_seg);
1024
1025 /* Data means there is no current execute packet, and that any label
1026 applies to that data rather than a subsequent instruction. */
1027 tic6x_free_label_list (seginfo->tc_segment_info_data.label_list);
1028 seginfo->tc_segment_info_data.label_list = NULL;
1029 seginfo->tc_segment_info_data.execute_packet_frag = NULL;
1030 seginfo->tc_segment_info_data.last_insn_lsb = NULL;
1031 seginfo->tc_segment_info_data.spmask_addr = NULL;
1032 seginfo->tc_segment_info_data.func_units_used = 0;
1033 }
1034
1035 /* Handle an alignment directive. Return TRUE if the
1036 machine-independent frag generation should be skipped. */
1037
1038 bfd_boolean
1039 tic6x_do_align (int n, char *fill, int len ATTRIBUTE_UNUSED, int max)
1040 {
1041 /* Given code alignments of 4, 8, 16 or 32 bytes, we try to handle
1042 them in the md_end pass by inserting NOPs in parallel with
1043 previous instructions. We only do this in sections containing
1044 nothing but instructions. Code alignments of 1 or 2 bytes have
1045 no effect in such sections (but we record them with
1046 machine-dependent frags anyway so they can be skipped or
1047 converted to machine-independent), while those of more than 64
1048 bytes cannot reliably be handled in this way. */
1049 if (n > 0
1050 && max >= 0
1051 && max < (1 << n)
1052 && !need_pass_2
1053 && fill == NULL
1054 && subseg_text_p (now_seg))
1055 {
1056 fragS *align_frag;
1057 char *p;
1058
1059 if (n > 5)
1060 return FALSE;
1061
1062 /* Machine-independent code would generate a frag here, but we
1063 wish to handle it in a machine-dependent way. */
1064 if (frag_now_fix () != 0)
1065 {
1066 if (frag_now->fr_type != rs_machine_dependent)
1067 frag_wane (frag_now);
1068
1069 frag_new (0);
1070 }
1071 frag_grow (32);
1072 align_frag = frag_now;
1073 p = frag_var (rs_machine_dependent, 32, 32, max, NULL, n, NULL);
1074 /* This must be the same as the frag to which a pointer was just
1075 saved. */
1076 if (p != align_frag->fr_literal)
1077 abort ();
1078 align_frag->tc_frag_data.is_insns = FALSE;
1079 return TRUE;
1080 }
1081 else
1082 return FALSE;
1083 }
1084
1085 /* Types of operand for parsing purposes. These are used as bit-masks
1086 to tell tic6x_parse_operand what forms of operand are
1087 permitted. */
1088 #define TIC6X_OP_EXP 0x0001u
1089 #define TIC6X_OP_REG 0x0002u
1090 #define TIC6X_OP_REGPAIR 0x0004u
1091 #define TIC6X_OP_IRP 0x0008u
1092 #define TIC6X_OP_NRP 0x0010u
1093 /* With TIC6X_OP_MEM_NOUNREG, the contents of a () offset are always
1094 interpreted as an expression, which may be a symbol with the same
1095 name as a register that ends up being implicitly DP-relative. With
1096 TIC6X_OP_MEM_UNREG, the contents of a () offset are interpreted as
1097 a register if they match one, and failing that as an expression,
1098 which must be constant. */
1099 #define TIC6X_OP_MEM_NOUNREG 0x0020u
1100 #define TIC6X_OP_MEM_UNREG 0x0040u
1101 #define TIC6X_OP_CTRL 0x0080u
1102 #define TIC6X_OP_FUNC_UNIT 0x0100u
1103
1104 /* A register or register pair read by the assembler. */
1105 typedef struct
1106 {
1107 /* The side the register is on (1 or 2). */
1108 unsigned int side;
1109 /* The register number (0 to 31). */
1110 unsigned int num;
1111 } tic6x_register;
1112
1113 /* Types of modification of a base address. */
1114 typedef enum
1115 {
1116 tic6x_mem_mod_none,
1117 tic6x_mem_mod_plus,
1118 tic6x_mem_mod_minus,
1119 tic6x_mem_mod_preinc,
1120 tic6x_mem_mod_predec,
1121 tic6x_mem_mod_postinc,
1122 tic6x_mem_mod_postdec
1123 } tic6x_mem_mod;
1124
1125 /* Scaled [] or unscaled () nature of an offset. */
1126 typedef enum
1127 {
1128 tic6x_offset_none,
1129 tic6x_offset_scaled,
1130 tic6x_offset_unscaled
1131 } tic6x_mem_scaling;
1132
1133 /* A memory operand read by the assembler. */
1134 typedef struct
1135 {
1136 /* The base register. */
1137 tic6x_register base_reg;
1138 /* How the base register is modified. */
1139 tic6x_mem_mod mod;
1140 /* Whether there is an offset (required with plain "+" and "-"), and
1141 whether it is scaled or unscaled if so. */
1142 tic6x_mem_scaling scaled;
1143 /* Whether the offset is a register (TRUE) or an expression
1144 (FALSE). */
1145 bfd_boolean offset_is_reg;
1146 /* The offset. */
1147 union
1148 {
1149 expressionS exp;
1150 tic6x_register reg;
1151 } offset;
1152 } tic6x_mem_ref;
1153
1154 /* A functional unit in SPMASK operands read by the assembler. */
1155 typedef struct
1156 {
1157 /* The basic unit. */
1158 tic6x_func_unit_base base;
1159 /* The side (1 or 2). */
1160 unsigned int side;
1161 } tic6x_func_unit_operand;
1162
1163 /* An operand read by the assembler. */
1164 typedef struct
1165 {
1166 /* The syntactic form of the operand, as one of the bit-masks
1167 above. */
1168 unsigned int form;
1169 /* The operand value. */
1170 union
1171 {
1172 /* An expression: TIC6X_OP_EXP. */
1173 expressionS exp;
1174 /* A register: TIC6X_OP_REG, TIC6X_OP_REGPAIR. */
1175 tic6x_register reg;
1176 /* A memory reference: TIC6X_OP_MEM_NOUNREG,
1177 TIC6X_OP_MEM_UNREG. */
1178 tic6x_mem_ref mem;
1179 /* A control register: TIC6X_OP_CTRL. */
1180 tic6x_ctrl_id ctrl;
1181 /* A functional unit: TIC6X_OP_FUNC_UNIT. */
1182 tic6x_func_unit_operand func_unit;
1183 } value;
1184 } tic6x_operand;
1185
1186 #define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
1187
1188 /* Parse a register operand, or part of an operand, starting at *P.
1189 If syntactically OK (including that the number is in the range 0 to
1190 31, but not necessarily in range for this architecture), return
1191 TRUE, putting the register side and number in *REG and update *P to
1192 point immediately after the register number; otherwise return FALSE
1193 without changing *P (but possibly changing *REG). Do not print any
1194 diagnostics. */
1195
1196 static bfd_boolean
1197 tic6x_parse_register (char **p, tic6x_register *reg)
1198 {
1199 char *r = *p;
1200
1201 switch (*r)
1202 {
1203 case 'a':
1204 case 'A':
1205 reg->side = 1;
1206 break;
1207
1208 case 'b':
1209 case 'B':
1210 reg->side = 2;
1211 break;
1212
1213 default:
1214 return FALSE;
1215 }
1216 r++;
1217
1218 if (*r >= '0' && *r <= '9')
1219 {
1220 reg->num = *r - '0';
1221 r++;
1222 }
1223 else
1224 return FALSE;
1225
1226 if (reg->num > 0 && *r >= '0' && *r <= '9')
1227 {
1228 reg->num = reg->num * 10 + (*r - '0');
1229 r++;
1230 }
1231
1232 if (*r >= '0' && *r <= '9')
1233 return FALSE;
1234
1235 if (reg->num >= 32)
1236 return FALSE;
1237 *p = r;
1238 return TRUE;
1239 }
1240
1241 /* Parse the initial two characters of a functional unit name starting
1242 at *P. If OK, set *BASE and *SIDE and return TRUE; otherwise,
1243 return FALSE. */
1244
1245 static bfd_boolean
1246 tic6x_parse_func_unit_base (char *p, tic6x_func_unit_base *base,
1247 unsigned int *side)
1248 {
1249 bfd_boolean good_func_unit = TRUE;
1250 tic6x_func_unit_base maybe_base = tic6x_func_unit_nfu;
1251 unsigned int maybe_side = 0;
1252
1253 switch (p[0])
1254 {
1255 case 'd':
1256 case 'D':
1257 maybe_base = tic6x_func_unit_d;
1258 break;
1259
1260 case 'l':
1261 case 'L':
1262 maybe_base = tic6x_func_unit_l;
1263 break;
1264
1265 case 'm':
1266 case 'M':
1267 maybe_base = tic6x_func_unit_m;
1268 break;
1269
1270 case 's':
1271 case 'S':
1272 maybe_base = tic6x_func_unit_s;
1273 break;
1274
1275 default:
1276 good_func_unit = FALSE;
1277 break;
1278 }
1279
1280 if (good_func_unit)
1281 switch (p[1])
1282 {
1283 case '1':
1284 maybe_side = 1;
1285 break;
1286
1287 case '2':
1288 maybe_side = 2;
1289 break;
1290
1291 default:
1292 good_func_unit = FALSE;
1293 break;
1294 }
1295
1296 if (good_func_unit)
1297 {
1298 *base = maybe_base;
1299 *side = maybe_side;
1300 }
1301
1302 return good_func_unit;
1303 }
1304
1305 /* Parse an operand starting at *P. If the operand parses OK, return
1306 TRUE and store the value in *OP; otherwise return FALSE (possibly
1307 changing *OP). In any case, update *P to point to the following
1308 comma or end of line. The possible operand forms are given by
1309 OP_FORMS. For diagnostics, this is operand OPNO of an opcode
1310 starting at STR, length OPC_LEN. */
1311
1312 static bfd_boolean
1313 tic6x_parse_operand (char **p, tic6x_operand *op, unsigned int op_forms,
1314 char *str, int opc_len, unsigned int opno)
1315 {
1316 bfd_boolean operand_parsed = FALSE;
1317 char *q = *p;
1318
1319 if ((op_forms & (TIC6X_OP_MEM_NOUNREG | TIC6X_OP_MEM_UNREG))
1320 == (TIC6X_OP_MEM_NOUNREG | TIC6X_OP_MEM_UNREG))
1321 abort ();
1322
1323 /* Check for functional unit names for SPMASK and SPMASKR. */
1324 if (!operand_parsed && (op_forms & TIC6X_OP_FUNC_UNIT))
1325 {
1326 tic6x_func_unit_base base = tic6x_func_unit_nfu;
1327 unsigned int side = 0;
1328
1329 if (tic6x_parse_func_unit_base (q, &base, &side))
1330 {
1331 char *rq = q + 2;
1332
1333 skip_whitespace (rq);
1334 if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
1335 {
1336 op->form = TIC6X_OP_FUNC_UNIT;
1337 op->value.func_unit.base = base;
1338 op->value.func_unit.side = side;
1339 operand_parsed = TRUE;
1340 q = rq;
1341 }
1342 }
1343 }
1344
1345 /* Check for literal "irp". */
1346 if (!operand_parsed && (op_forms & TIC6X_OP_IRP))
1347 {
1348 if ((q[0] == 'i' || q[0] == 'I')
1349 && (q[1] == 'r' || q[1] == 'R')
1350 && (q[2] == 'p' || q[2] == 'P'))
1351 {
1352 char *rq = q + 3;
1353
1354 skip_whitespace (rq);
1355 if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
1356 {
1357 op->form = TIC6X_OP_IRP;
1358 operand_parsed = TRUE;
1359 q = rq;
1360 }
1361 }
1362 }
1363
1364 /* Check for literal "nrp". */
1365 if (!operand_parsed && (op_forms & TIC6X_OP_NRP))
1366 {
1367 if ((q[0] == 'n' || q[0] == 'N')
1368 && (q[1] == 'r' || q[1] == 'R')
1369 && (q[2] == 'p' || q[2] == 'P'))
1370 {
1371 char *rq = q + 3;
1372
1373 skip_whitespace (rq);
1374 if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
1375 {
1376 op->form = TIC6X_OP_NRP;
1377 operand_parsed = TRUE;
1378 q = rq;
1379 }
1380 }
1381 }
1382
1383 /* Check for control register names. */
1384 if (!operand_parsed && (op_forms & TIC6X_OP_CTRL))
1385 {
1386 tic6x_ctrl_id crid;
1387
1388 for (crid = 0; crid < tic6x_ctrl_max; crid++)
1389 {
1390 size_t len = strlen (tic6x_ctrl_table[crid].name);
1391
1392 if (strncasecmp (tic6x_ctrl_table[crid].name, q, len) == 0)
1393 {
1394 char *rq = q + len;
1395
1396 skip_whitespace (rq);
1397 if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
1398 {
1399 op->form = TIC6X_OP_CTRL;
1400 op->value.ctrl = crid;
1401 operand_parsed = TRUE;
1402 q = rq;
1403 if (!(tic6x_ctrl_table[crid].isa_variants & tic6x_features))
1404 as_bad (_("control register '%s' not supported "
1405 "on this architecture"),
1406 tic6x_ctrl_table[crid].name);
1407 }
1408 }
1409 }
1410 }
1411
1412 /* See if this looks like a memory reference. */
1413 if (!operand_parsed
1414 && (op_forms & (TIC6X_OP_MEM_NOUNREG | TIC6X_OP_MEM_UNREG)))
1415 {
1416 bfd_boolean mem_ok = TRUE;
1417 char *mq = q;
1418 tic6x_mem_mod mem_mod = tic6x_mem_mod_none;
1419 tic6x_register base_reg;
1420 bfd_boolean require_offset, permit_offset;
1421 tic6x_mem_scaling scaled;
1422 bfd_boolean offset_is_reg;
1423 expressionS offset_exp;
1424 tic6x_register offset_reg;
1425
1426 if (*mq == '*')
1427 mq++;
1428 else
1429 mem_ok = FALSE;
1430
1431 if (mem_ok)
1432 {
1433 skip_whitespace (mq);
1434 switch (*mq)
1435 {
1436 case '+':
1437 if (mq[1] == '+')
1438 {
1439 mem_mod = tic6x_mem_mod_preinc;
1440 mq += 2;
1441 }
1442 else
1443 {
1444 mem_mod = tic6x_mem_mod_plus;
1445 mq++;
1446 }
1447 break;
1448
1449 case '-':
1450 if (mq[1] == '-')
1451 {
1452 mem_mod = tic6x_mem_mod_predec;
1453 mq += 2;
1454 }
1455 else
1456 {
1457 mem_mod = tic6x_mem_mod_minus;
1458 mq++;
1459 }
1460 break;
1461
1462 default:
1463 break;
1464 }
1465 }
1466
1467 if (mem_ok)
1468 {
1469 skip_whitespace (mq);
1470 mem_ok = tic6x_parse_register (&mq, &base_reg);
1471 }
1472
1473 if (mem_ok && mem_mod == tic6x_mem_mod_none)
1474 {
1475 skip_whitespace (mq);
1476 if (mq[0] == '+' && mq[1] == '+')
1477 {
1478 mem_mod = tic6x_mem_mod_postinc;
1479 mq += 2;
1480 }
1481 else if (mq[0] == '-' && mq[1] == '-')
1482 {
1483 mem_mod = tic6x_mem_mod_postdec;
1484 mq += 2;
1485 }
1486 }
1487
1488 if (mem_mod == tic6x_mem_mod_none)
1489 permit_offset = FALSE;
1490 else
1491 permit_offset = TRUE;
1492 if (mem_mod == tic6x_mem_mod_plus || mem_mod == tic6x_mem_mod_minus)
1493 require_offset = TRUE;
1494 else
1495 require_offset = FALSE;
1496 scaled = tic6x_offset_none;
1497 offset_is_reg = FALSE;
1498
1499 if (mem_ok && permit_offset)
1500 {
1501 char endc = 0;
1502
1503 skip_whitespace (mq);
1504 switch (*mq)
1505 {
1506 case '[':
1507 scaled = tic6x_offset_scaled;
1508 mq++;
1509 endc = ']';
1510 break;
1511
1512 case '(':
1513 scaled = tic6x_offset_unscaled;
1514 mq++;
1515 endc = ')';
1516 break;
1517
1518 default:
1519 break;
1520 }
1521 if (scaled != tic6x_offset_none)
1522 {
1523 skip_whitespace (mq);
1524 if (scaled == tic6x_offset_scaled
1525 || (op_forms & TIC6X_OP_MEM_UNREG))
1526 {
1527 bfd_boolean reg_ok;
1528 char *rq = mq;
1529
1530 reg_ok = tic6x_parse_register (&rq, &offset_reg);
1531 if (reg_ok)
1532 {
1533 skip_whitespace (rq);
1534 if (*rq == endc)
1535 {
1536 mq = rq;
1537 offset_is_reg = TRUE;
1538 }
1539 }
1540 }
1541 if (!offset_is_reg)
1542 {
1543 char *save_input_line_pointer;
1544
1545 save_input_line_pointer = input_line_pointer;
1546 input_line_pointer = mq;
1547 expression (&offset_exp);
1548 mq = input_line_pointer;
1549 input_line_pointer = save_input_line_pointer;
1550 }
1551 skip_whitespace (mq);
1552 if (*mq == endc)
1553 mq++;
1554 else
1555 mem_ok = FALSE;
1556 }
1557 }
1558
1559 if (mem_ok && require_offset && scaled == tic6x_offset_none)
1560 mem_ok = FALSE;
1561
1562 if (mem_ok)
1563 {
1564 skip_whitespace (mq);
1565 if (!is_end_of_line[(unsigned char) *mq] && *mq != ',')
1566 mem_ok = FALSE;
1567 }
1568
1569 if (mem_ok)
1570 {
1571 op->form = op_forms & (TIC6X_OP_MEM_NOUNREG | TIC6X_OP_MEM_UNREG);
1572 op->value.mem.base_reg = base_reg;
1573 op->value.mem.mod = mem_mod;
1574 op->value.mem.scaled = scaled;
1575 op->value.mem.offset_is_reg = offset_is_reg;
1576 if (offset_is_reg)
1577 op->value.mem.offset.reg = offset_reg;
1578 else
1579 op->value.mem.offset.exp = offset_exp;
1580 operand_parsed = TRUE;
1581 q = mq;
1582 if (base_reg.num >= tic6x_num_registers)
1583 as_bad (_("register number %u not supported on this architecture"),
1584 base_reg.num);
1585 if (offset_is_reg && offset_reg.num >= tic6x_num_registers)
1586 as_bad (_("register number %u not supported on this architecture"),
1587 offset_reg.num);
1588 }
1589 }
1590
1591 /* See if this looks like a register or register pair. */
1592 if (!operand_parsed && (op_forms & (TIC6X_OP_REG | TIC6X_OP_REGPAIR)))
1593 {
1594 tic6x_register first_reg, second_reg;
1595 bfd_boolean reg_ok;
1596 char *rq = q;
1597
1598 reg_ok = tic6x_parse_register (&rq, &first_reg);
1599
1600 if (reg_ok)
1601 {
1602 if (*rq == ':' && (op_forms & TIC6X_OP_REGPAIR))
1603 {
1604 rq++;
1605 reg_ok = tic6x_parse_register (&rq, &second_reg);
1606 if (reg_ok)
1607 {
1608 skip_whitespace (rq);
1609 if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
1610 {
1611 if ((second_reg.num & 1)
1612 || (first_reg.num != second_reg.num + 1)
1613 || (first_reg.side != second_reg.side))
1614 as_bad (_("register pair for operand %u of '%.*s'"
1615 " not a valid even/odd pair"), opno,
1616 opc_len, str);
1617 op->form = TIC6X_OP_REGPAIR;
1618 op->value.reg = second_reg;
1619 operand_parsed = TRUE;
1620 q = rq;
1621 }
1622 }
1623 }
1624 else if (op_forms & TIC6X_OP_REG)
1625 {
1626 skip_whitespace (rq);
1627 if (is_end_of_line[(unsigned char) *rq] || *rq == ',')
1628 {
1629 op->form = TIC6X_OP_REG;
1630 op->value.reg = first_reg;
1631 operand_parsed = TRUE;
1632 q = rq;
1633 }
1634 }
1635 }
1636 if (operand_parsed)
1637 {
1638 if (first_reg.num >= tic6x_num_registers)
1639 as_bad (_("register number %u not supported on this architecture"),
1640 first_reg.num);
1641 if (op->form == TIC6X_OP_REGPAIR
1642 && second_reg.num >= tic6x_num_registers)
1643 as_bad (_("register number %u not supported on this architecture"),
1644 second_reg.num);
1645 }
1646 }
1647
1648 /* Otherwise, parse it as an expression. */
1649 if (!operand_parsed && (op_forms & TIC6X_OP_EXP))
1650 {
1651 char *save_input_line_pointer;
1652
1653 save_input_line_pointer = input_line_pointer;
1654 input_line_pointer = q;
1655 op->form = TIC6X_OP_EXP;
1656 expression (&op->value.exp);
1657 q = input_line_pointer;
1658 input_line_pointer = save_input_line_pointer;
1659 operand_parsed = TRUE;
1660 }
1661
1662 if (operand_parsed)
1663 {
1664 /* Now the operand has been parsed, there must be nothing more
1665 before the comma or end of line. */
1666 skip_whitespace (q);
1667 if (!is_end_of_line[(unsigned char) *q] && *q != ',')
1668 {
1669 operand_parsed = FALSE;
1670 as_bad (_("junk after operand %u of '%.*s'"), opno,
1671 opc_len, str);
1672 while (!is_end_of_line[(unsigned char) *q] && *q != ',')
1673 q++;
1674 }
1675 }
1676 else
1677 {
1678 /* This could not be parsed as any acceptable form of
1679 operand. */
1680 switch (op_forms)
1681 {
1682 case TIC6X_OP_REG | TIC6X_OP_REGPAIR:
1683 as_bad (_("bad register or register pair for operand %u of '%.*s'"),
1684 opno, opc_len, str);
1685 break;
1686
1687 case TIC6X_OP_REG | TIC6X_OP_CTRL:
1688 case TIC6X_OP_REG:
1689 as_bad (_("bad register for operand %u of '%.*s'"),
1690 opno, opc_len, str);
1691 break;
1692
1693 case TIC6X_OP_REGPAIR:
1694 as_bad (_("bad register pair for operand %u of '%.*s'"),
1695 opno, opc_len, str);
1696 break;
1697
1698 case TIC6X_OP_FUNC_UNIT:
1699 as_bad (_("bad functional unit for operand %u of '%.*s'"),
1700 opno, opc_len, str);
1701 break;
1702
1703 default:
1704 as_bad (_("bad operand %u of '%.*s'"),
1705 opno, opc_len, str);
1706 break;
1707
1708 }
1709 while (!is_end_of_line[(unsigned char) *q] && *q != ',')
1710 q++;
1711 }
1712 *p = q;
1713 return operand_parsed;
1714 }
1715
1716 /* Table of assembler operators and associated O_* values. */
1717 typedef struct
1718 {
1719 const char *name;
1720 operatorT op;
1721 } tic6x_operator_table;
1722 static const tic6x_operator_table tic6x_operators[] = {
1723 #define O_dsbt_index O_md1
1724 { "dsbt_index", O_dsbt_index },
1725 #define O_got O_md2
1726 { "got", O_got },
1727 #define O_dpr_got O_md3
1728 { "dpr_got", O_dpr_got },
1729 #define O_dpr_byte O_md4
1730 { "dpr_byte", O_dpr_byte },
1731 #define O_dpr_hword O_md5
1732 { "dpr_hword", O_dpr_hword },
1733 #define O_dpr_word O_md6
1734 { "dpr_word", O_dpr_word },
1735 #define O_pcr_offset O_md7
1736 { "pcr_offset", O_pcr_offset }
1737 };
1738
1739 /* Parse a name in some machine-specific way. Used on C6X to handle
1740 assembler operators. */
1741
1742 int
1743 tic6x_parse_name (const char *name, expressionS *exprP,
1744 enum expr_mode mode ATTRIBUTE_UNUSED, char *nextchar)
1745 {
1746 char *p = input_line_pointer;
1747 char c, *name_start, *name_end;
1748 const char *inner_name;
1749 unsigned int i;
1750 operatorT op = O_illegal;
1751 symbolS *sym, *op_sym = NULL;
1752
1753 if (*name != '$')
1754 return 0;
1755
1756 for (i = 0; i < ARRAY_SIZE (tic6x_operators); i++)
1757 if (strcasecmp (name + 1, tic6x_operators[i].name) == 0)
1758 {
1759 op = tic6x_operators[i].op;
1760 break;
1761 }
1762
1763 if (op == O_illegal)
1764 return 0;
1765
1766 *input_line_pointer = *nextchar;
1767 skip_whitespace (p);
1768
1769 if (*p != '(')
1770 {
1771 *input_line_pointer = 0;
1772 return 0;
1773 }
1774 p++;
1775 skip_whitespace (p);
1776
1777 if (!is_name_beginner (*p))
1778 {
1779 *input_line_pointer = 0;
1780 return 0;
1781 }
1782
1783 name_start = p;
1784 p++;
1785 while (is_part_of_name (*p))
1786 p++;
1787 name_end = p;
1788 skip_whitespace (p);
1789
1790 if (op == O_pcr_offset)
1791 {
1792 char *op_name_start, *op_name_end;
1793
1794 if (*p != ',')
1795 {
1796 *input_line_pointer = 0;
1797 return 0;
1798 }
1799 p++;
1800 skip_whitespace (p);
1801
1802 if (!is_name_beginner (*p))
1803 {
1804 *input_line_pointer = 0;
1805 return 0;
1806 }
1807
1808 op_name_start = p;
1809 p++;
1810 while (is_part_of_name (*p))
1811 p++;
1812 op_name_end = p;
1813 skip_whitespace (p);
1814
1815 c = *op_name_end;
1816 *op_name_end = 0;
1817 op_sym = symbol_find_or_make (op_name_start);
1818 *op_name_end = c;
1819 }
1820
1821 if (*p != ')')
1822 {
1823 *input_line_pointer = 0;
1824 return 0;
1825 }
1826
1827 input_line_pointer = p + 1;
1828 *nextchar = *input_line_pointer;
1829 *input_line_pointer = 0;
1830
1831 c = *name_end;
1832 *name_end = 0;
1833 inner_name = name_start;
1834 if (op == O_dsbt_index && strcmp (inner_name, "__c6xabi_DSBT_BASE") != 0)
1835 {
1836 as_bad (_("$DSBT_INDEX must be used with __c6xabi_DSBT_BASE"));
1837 inner_name = "__c6xabi_DSBT_BASE";
1838 }
1839 sym = symbol_find_or_make (inner_name);
1840 *name_end = c;
1841
1842 exprP->X_op = op;
1843 exprP->X_add_symbol = sym;
1844 exprP->X_add_number = 0;
1845 exprP->X_op_symbol = op_sym;
1846 exprP->X_md = 0;
1847
1848 return 1;
1849 }
1850
1851 /* Create a fixup for an expression. Same arguments as fix_new_exp,
1852 plus FIX_ADDA which is TRUE for ADDA instructions (to indicate that
1853 fixes resolving to constants should have those constants implicitly
1854 shifted) and FALSE otherwise, but look for C6X-specific expression
1855 types and adjust the relocations or give errors accordingly. */
1856
1857 static void
1858 tic6x_fix_new_exp (fragS *frag, int where, int size, expressionS *exp,
1859 int pcrel, bfd_reloc_code_real_type r_type,
1860 bfd_boolean fix_adda)
1861 {
1862 bfd_reloc_code_real_type new_reloc = BFD_RELOC_UNUSED;
1863 symbolS *subsy = NULL;
1864 fixS *fix;
1865
1866 switch (exp->X_op)
1867 {
1868 case O_dsbt_index:
1869 switch (r_type)
1870 {
1871 case BFD_RELOC_C6000_SBR_U15_W:
1872 new_reloc = BFD_RELOC_C6000_DSBT_INDEX;
1873 break;
1874
1875 default:
1876 as_bad (_("$DSBT_INDEX not supported in this context"));
1877 return;
1878 }
1879 break;
1880
1881 case O_got:
1882 switch (r_type)
1883 {
1884 case BFD_RELOC_C6000_SBR_U15_W:
1885 new_reloc = BFD_RELOC_C6000_SBR_GOT_U15_W;
1886 break;
1887
1888 default:
1889 as_bad (_("$GOT not supported in this context"));
1890 return;
1891 }
1892 break;
1893
1894 case O_dpr_got:
1895 switch (r_type)
1896 {
1897 case BFD_RELOC_C6000_ABS_L16:
1898 new_reloc = BFD_RELOC_C6000_SBR_GOT_L16_W;
1899 break;
1900
1901 case BFD_RELOC_C6000_ABS_H16:
1902 new_reloc = BFD_RELOC_C6000_SBR_GOT_H16_W;
1903 break;
1904
1905 default:
1906 as_bad (_("$DPR_GOT not supported in this context"));
1907 return;
1908 }
1909 break;
1910
1911 case O_dpr_byte:
1912 switch (r_type)
1913 {
1914 case BFD_RELOC_C6000_ABS_S16:
1915 new_reloc = BFD_RELOC_C6000_SBR_S16;
1916 break;
1917
1918 case BFD_RELOC_C6000_ABS_L16:
1919 new_reloc = BFD_RELOC_C6000_SBR_L16_B;
1920 break;
1921
1922 case BFD_RELOC_C6000_ABS_H16:
1923 new_reloc = BFD_RELOC_C6000_SBR_H16_B;
1924 break;
1925
1926 default:
1927 as_bad (_("$DPR_BYTE not supported in this context"));
1928 return;
1929 }
1930 break;
1931
1932 case O_dpr_hword:
1933 switch (r_type)
1934 {
1935 case BFD_RELOC_C6000_ABS_L16:
1936 new_reloc = BFD_RELOC_C6000_SBR_L16_H;
1937 break;
1938
1939 case BFD_RELOC_C6000_ABS_H16:
1940 new_reloc = BFD_RELOC_C6000_SBR_H16_H;
1941 break;
1942
1943 default:
1944 as_bad (_("$DPR_HWORD not supported in this context"));
1945 return;
1946 }
1947 break;
1948
1949 case O_dpr_word:
1950 switch (r_type)
1951 {
1952 case BFD_RELOC_C6000_ABS_L16:
1953 new_reloc = BFD_RELOC_C6000_SBR_L16_W;
1954 break;
1955
1956 case BFD_RELOC_C6000_ABS_H16:
1957 new_reloc = BFD_RELOC_C6000_SBR_H16_W;
1958 break;
1959
1960 default:
1961 as_bad (_("$DPR_WORD not supported in this context"));
1962 return;
1963 }
1964 break;
1965
1966 case O_pcr_offset:
1967 subsy = exp->X_op_symbol;
1968 switch (r_type)
1969 {
1970 case BFD_RELOC_C6000_ABS_S16:
1971 case BFD_RELOC_C6000_ABS_L16:
1972 new_reloc = BFD_RELOC_C6000_PCR_L16;
1973 break;
1974
1975 case BFD_RELOC_C6000_ABS_H16:
1976 new_reloc = BFD_RELOC_C6000_PCR_H16;
1977 break;
1978
1979 default:
1980 as_bad (_("$PCR_OFFSET not supported in this context"));
1981 return;
1982 }
1983 break;
1984
1985 case O_symbol:
1986 break;
1987
1988 default:
1989 if (pcrel)
1990 {
1991 as_bad (_("invalid PC-relative operand"));
1992 return;
1993 }
1994 break;
1995 }
1996
1997 if (new_reloc == BFD_RELOC_UNUSED)
1998 fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
1999 else
2000 fix = fix_new (frag, where, size, exp->X_add_symbol, exp->X_add_number,
2001 pcrel, new_reloc);
2002 fix->tc_fix_data.fix_subsy = subsy;
2003 fix->tc_fix_data.fix_adda = fix_adda;
2004 }
2005
2006 /* Generate a fix for a constant (.word etc.). Needed to ensure these
2007 go through the error checking in tic6x_fix_new_exp. */
2008
2009 void
2010 tic6x_cons_fix_new (fragS *frag, int where, int size, expressionS *exp,
2011 bfd_reloc_code_real_type r_type)
2012 {
2013 switch (size)
2014 {
2015 case 1:
2016 r_type = BFD_RELOC_8;
2017 break;
2018
2019 case 2:
2020 r_type = BFD_RELOC_16;
2021 break;
2022
2023 case 4:
2024 r_type = BFD_RELOC_32;
2025 break;
2026
2027 default:
2028 as_bad (_("no %d-byte relocations available"), size);
2029 return;
2030 }
2031
2032 tic6x_fix_new_exp (frag, where, size, exp, 0, r_type, FALSE);
2033 }
2034
2035 /* Initialize target-specific fix data. */
2036
2037 void
2038 tic6x_init_fix_data (fixS *fixP)
2039 {
2040 fixP->tc_fix_data.fix_adda = FALSE;
2041 fixP->tc_fix_data.fix_subsy = NULL;
2042 }
2043
2044 /* Return true if the fix can be handled by GAS, false if it must
2045 be passed through to the linker. */
2046
2047 bfd_boolean
2048 tic6x_fix_adjustable (fixS *fixP)
2049 {
2050 switch (fixP->fx_r_type)
2051 {
2052 /* Adjust_reloc_syms doesn't know about the GOT. */
2053 case BFD_RELOC_C6000_SBR_GOT_U15_W:
2054 case BFD_RELOC_C6000_SBR_GOT_H16_W:
2055 case BFD_RELOC_C6000_SBR_GOT_L16_W:
2056 case BFD_RELOC_C6000_EHTYPE:
2057 return 0;
2058
2059 case BFD_RELOC_C6000_PREL31:
2060 return 0;
2061
2062 case BFD_RELOC_C6000_PCR_H16:
2063 case BFD_RELOC_C6000_PCR_L16:
2064 return 0;
2065
2066 default:
2067 return 1;
2068 }
2069 }
2070
2071 /* Given the fine-grained form of an operand, return the coarse
2072 (bit-mask) form. */
2073
2074 static unsigned int
2075 tic6x_coarse_operand_form (tic6x_operand_form form)
2076 {
2077 switch (form)
2078 {
2079 case tic6x_operand_asm_const:
2080 case tic6x_operand_link_const:
2081 return TIC6X_OP_EXP;
2082
2083 case tic6x_operand_reg:
2084 case tic6x_operand_xreg:
2085 case tic6x_operand_dreg:
2086 case tic6x_operand_areg:
2087 case tic6x_operand_retreg:
2088 return TIC6X_OP_REG;
2089
2090 case tic6x_operand_regpair:
2091 case tic6x_operand_xregpair:
2092 case tic6x_operand_dregpair:
2093 return TIC6X_OP_REGPAIR;
2094
2095 case tic6x_operand_irp:
2096 return TIC6X_OP_IRP;
2097
2098 case tic6x_operand_nrp:
2099 return TIC6X_OP_NRP;
2100
2101 case tic6x_operand_ctrl:
2102 return TIC6X_OP_CTRL;
2103
2104 case tic6x_operand_mem_short:
2105 case tic6x_operand_mem_long:
2106 case tic6x_operand_mem_deref:
2107 return TIC6X_OP_MEM_NOUNREG;
2108
2109 case tic6x_operand_mem_ndw:
2110 return TIC6X_OP_MEM_UNREG;
2111
2112 case tic6x_operand_func_unit:
2113 return TIC6X_OP_FUNC_UNIT;
2114
2115 default:
2116 abort ();
2117 }
2118 }
2119
2120 /* How an operand may match or not match a desired form. If different
2121 instruction alternatives fail in different ways, the first failure
2122 in this list determines the diagnostic. */
2123 typedef enum
2124 {
2125 /* Matches. */
2126 tic6x_match_matches,
2127 /* Bad coarse form. */
2128 tic6x_match_coarse,
2129 /* Not constant. */
2130 tic6x_match_non_const,
2131 /* Register on wrong side. */
2132 tic6x_match_wrong_side,
2133 /* Not a valid address register. */
2134 tic6x_match_bad_address,
2135 /* Not a valid return address register. */
2136 tic6x_match_bad_return,
2137 /* Control register not readable. */
2138 tic6x_match_ctrl_write_only,
2139 /* Control register not writable. */
2140 tic6x_match_ctrl_read_only,
2141 /* Not a valid memory reference for this instruction. */
2142 tic6x_match_bad_mem
2143 } tic6x_operand_match;
2144
2145 /* Return whether an operand matches the given fine-grained form and
2146 read/write usage, and, if it does not match, how it fails to match.
2147 The main functional unit side is SIDE; the cross-path side is CROSS
2148 (the same as SIDE if a cross path not used); the data side is
2149 DATA_SIDE. */
2150 static tic6x_operand_match
2151 tic6x_operand_matches_form (const tic6x_operand *op, tic6x_operand_form form,
2152 tic6x_rw rw, unsigned int side, unsigned int cross,
2153 unsigned int data_side)
2154 {
2155 unsigned int coarse = tic6x_coarse_operand_form (form);
2156
2157 if (coarse != op->form)
2158 return tic6x_match_coarse;
2159
2160 switch (form)
2161 {
2162 case tic6x_operand_asm_const:
2163 if (op->value.exp.X_op == O_constant)
2164 return tic6x_match_matches;
2165 else
2166 return tic6x_match_non_const;
2167
2168 case tic6x_operand_link_const:
2169 case tic6x_operand_irp:
2170 case tic6x_operand_nrp:
2171 case tic6x_operand_func_unit:
2172 /* All expressions are link-time constants, although there may
2173 not be relocations to express them in the output file. "irp"
2174 and "nrp" are unique operand values. All parsed functional
2175 unit names are valid. */
2176 return tic6x_match_matches;
2177
2178 case tic6x_operand_reg:
2179 case tic6x_operand_regpair:
2180 if (op->value.reg.side == side)
2181 return tic6x_match_matches;
2182 else
2183 return tic6x_match_wrong_side;
2184
2185 case tic6x_operand_xreg:
2186 case tic6x_operand_xregpair:
2187 if (op->value.reg.side == cross)
2188 return tic6x_match_matches;
2189 else
2190 return tic6x_match_wrong_side;
2191
2192 case tic6x_operand_dreg:
2193 case tic6x_operand_dregpair:
2194 if (op->value.reg.side == data_side)
2195 return tic6x_match_matches;
2196 else
2197 return tic6x_match_wrong_side;
2198
2199 case tic6x_operand_areg:
2200 if (op->value.reg.side != cross)
2201 return tic6x_match_wrong_side;
2202 else if (op->value.reg.side == 2
2203 && (op->value.reg.num == 14 || op->value.reg.num == 15))
2204 return tic6x_match_matches;
2205 else
2206 return tic6x_match_bad_address;
2207
2208 case tic6x_operand_retreg:
2209 if (op->value.reg.side != side)
2210 return tic6x_match_wrong_side;
2211 else if (op->value.reg.num != 3)
2212 return tic6x_match_bad_return;
2213 else
2214 return tic6x_match_matches;
2215
2216 case tic6x_operand_ctrl:
2217 switch (rw)
2218 {
2219 case tic6x_rw_read:
2220 if (tic6x_ctrl_table[op->value.ctrl].rw == tic6x_rw_read
2221 || tic6x_ctrl_table[op->value.ctrl].rw == tic6x_rw_read_write)
2222 return tic6x_match_matches;
2223 else
2224 return tic6x_match_ctrl_write_only;
2225
2226 case tic6x_rw_write:
2227 if (tic6x_ctrl_table[op->value.ctrl].rw == tic6x_rw_write
2228 || tic6x_ctrl_table[op->value.ctrl].rw == tic6x_rw_read_write)
2229 return tic6x_match_matches;
2230 else
2231 return tic6x_match_ctrl_read_only;
2232
2233 default:
2234 abort ();
2235 }
2236
2237 case tic6x_operand_mem_deref:
2238 if (op->value.mem.mod != tic6x_mem_mod_none)
2239 return tic6x_match_bad_mem;
2240 else if (op->value.mem.scaled != tic6x_offset_none)
2241 abort ();
2242 else if (op->value.mem.base_reg.side != side)
2243 return tic6x_match_bad_mem;
2244 else
2245 return tic6x_match_matches;
2246
2247 case tic6x_operand_mem_short:
2248 case tic6x_operand_mem_ndw:
2249 if (op->value.mem.base_reg.side != side)
2250 return tic6x_match_bad_mem;
2251 if (op->value.mem.mod == tic6x_mem_mod_none)
2252 {
2253 if (op->value.mem.scaled != tic6x_offset_none)
2254 abort ();
2255 return tic6x_match_matches;
2256 }
2257 if (op->value.mem.scaled == tic6x_offset_none)
2258 {
2259 if (op->value.mem.mod == tic6x_mem_mod_plus
2260 || op->value.mem.mod == tic6x_mem_mod_minus)
2261 abort ();
2262 return tic6x_match_matches;
2263 }
2264 if (op->value.mem.offset_is_reg)
2265 {
2266 if (op->value.mem.scaled == tic6x_offset_unscaled
2267 && form != tic6x_operand_mem_ndw)
2268 abort ();
2269 if (op->value.mem.offset.reg.side == side)
2270 return tic6x_match_matches;
2271 else
2272 return tic6x_match_bad_mem;
2273 }
2274 else
2275 {
2276 if (op->value.mem.offset.exp.X_op == O_constant)
2277 return tic6x_match_matches;
2278 else
2279 return tic6x_match_bad_mem;
2280 }
2281
2282 case tic6x_operand_mem_long:
2283 if (op->value.mem.base_reg.side == 2
2284 && (op->value.mem.base_reg.num == 14
2285 || op->value.mem.base_reg.num == 15))
2286 {
2287 switch (op->value.mem.mod)
2288 {
2289 case tic6x_mem_mod_none:
2290 if (op->value.mem.scaled != tic6x_offset_none)
2291 abort ();
2292 return tic6x_match_matches;
2293
2294 case tic6x_mem_mod_plus:
2295 if (op->value.mem.scaled == tic6x_offset_none)
2296 abort ();
2297 if (op->value.mem.offset_is_reg)
2298 return tic6x_match_bad_mem;
2299 else if (op->value.mem.scaled == tic6x_offset_scaled
2300 && op->value.mem.offset.exp.X_op != O_constant)
2301 return tic6x_match_bad_mem;
2302 else
2303 return tic6x_match_matches;
2304
2305 case tic6x_mem_mod_minus:
2306 case tic6x_mem_mod_preinc:
2307 case tic6x_mem_mod_predec:
2308 case tic6x_mem_mod_postinc:
2309 case tic6x_mem_mod_postdec:
2310 return tic6x_match_bad_mem;
2311
2312 default:
2313 abort ();
2314 }
2315
2316 }
2317 else
2318 return tic6x_match_bad_mem;
2319
2320 default:
2321 abort ();
2322 }
2323 }
2324
2325 /* Return the number of bits shift used with DP-relative coding method
2326 CODING. */
2327
2328 static unsigned int
2329 tic6x_dpr_shift (tic6x_coding_method coding)
2330 {
2331 switch (coding)
2332 {
2333 case tic6x_coding_ulcst_dpr_byte:
2334 return 0;
2335
2336 case tic6x_coding_ulcst_dpr_half:
2337 return 1;
2338
2339 case tic6x_coding_ulcst_dpr_word:
2340 return 2;
2341
2342 default:
2343 abort ();
2344 }
2345 }
2346
2347 /* Return the relocation used with DP-relative coding method
2348 CODING. */
2349
2350 static bfd_reloc_code_real_type
2351 tic6x_dpr_reloc (tic6x_coding_method coding)
2352 {
2353 switch (coding)
2354 {
2355 case tic6x_coding_ulcst_dpr_byte:
2356 return BFD_RELOC_C6000_SBR_U15_B;
2357
2358 case tic6x_coding_ulcst_dpr_half:
2359 return BFD_RELOC_C6000_SBR_U15_H;
2360
2361 case tic6x_coding_ulcst_dpr_word:
2362 return BFD_RELOC_C6000_SBR_U15_W;
2363
2364 default:
2365 abort ();
2366 }
2367 }
2368
2369 /* Given a memory reference *MEM_REF as originally parsed, fill in
2370 defaults for missing offsets. */
2371
2372 static void
2373 tic6x_default_mem_ref (tic6x_mem_ref *mem_ref)
2374 {
2375 switch (mem_ref->mod)
2376 {
2377 case tic6x_mem_mod_none:
2378 if (mem_ref->scaled != tic6x_offset_none)
2379 abort ();
2380 mem_ref->mod = tic6x_mem_mod_plus;
2381 mem_ref->scaled = tic6x_offset_unscaled;
2382 mem_ref->offset_is_reg = FALSE;
2383 memset (&mem_ref->offset.exp, 0, sizeof mem_ref->offset.exp);
2384 mem_ref->offset.exp.X_op = O_constant;
2385 mem_ref->offset.exp.X_add_number = 0;
2386 mem_ref->offset.exp.X_unsigned = 0;
2387 break;
2388
2389 case tic6x_mem_mod_plus:
2390 case tic6x_mem_mod_minus:
2391 if (mem_ref->scaled == tic6x_offset_none)
2392 abort ();
2393 break;
2394
2395 case tic6x_mem_mod_preinc:
2396 case tic6x_mem_mod_predec:
2397 case tic6x_mem_mod_postinc:
2398 case tic6x_mem_mod_postdec:
2399 if (mem_ref->scaled != tic6x_offset_none)
2400 break;
2401 mem_ref->scaled = tic6x_offset_scaled;
2402 mem_ref->offset_is_reg = FALSE;
2403 memset (&mem_ref->offset.exp, 0, sizeof mem_ref->offset.exp);
2404 mem_ref->offset.exp.X_op = O_constant;
2405 mem_ref->offset.exp.X_add_number = 1;
2406 mem_ref->offset.exp.X_unsigned = 0;
2407 break;
2408
2409 default:
2410 abort ();
2411 }
2412 }
2413
2414 /* Return the encoding in the 8-bit field of an SPMASK or SPMASKR
2415 instruction of the specified UNIT, side SIDE. */
2416
2417 static unsigned int
2418 tic6x_encode_spmask (tic6x_func_unit_base unit, unsigned int side)
2419 {
2420 switch (unit)
2421 {
2422 case tic6x_func_unit_l:
2423 return 1 << (side - 1);
2424
2425 case tic6x_func_unit_s:
2426 return 1 << (side + 1);
2427
2428 case tic6x_func_unit_d:
2429 return 1 << (side + 3);
2430
2431 case tic6x_func_unit_m:
2432 return 1 << (side + 5);
2433
2434 default:
2435 abort ();
2436 }
2437 }
2438
2439 /* Try to encode the instruction with opcode number ID and operands
2440 OPERANDS (number NUM_OPERANDS), creg value THIS_LINE_CREG and z
2441 value THIS_LINE_Z; FUNC_UNIT_SIDE, FUNC_UNIT_CROSS and
2442 FUNC_UNIT_DATA_SIDE describe the functional unit specification;
2443 SPLOOP_II is the ii value from the previous SPLOOP-family
2444 instruction, or 0 if not in such a loop; the only possible problems
2445 are operands being out of range (they already match the
2446 fine-grained form), and inappropriate predication. If this
2447 succeeds, return the encoding and set *OK to TRUE; otherwise return
2448 0 and set *OK to FALSE. If a fix is needed, set *FIX_NEEDED to
2449 true and fill in *FIX_EXP, *FIX_PCREL, *FX_R_TYPE and *FIX_ADDA.
2450 Print error messages for failure if PRINT_ERRORS is TRUE; the
2451 opcode starts at STR and has length OPC_LEN. */
2452
2453 static unsigned int
2454 tic6x_try_encode (tic6x_opcode_id id, tic6x_operand *operands,
2455 unsigned int num_operands, unsigned int this_line_creg,
2456 unsigned int this_line_z, unsigned int func_unit_side,
2457 unsigned int func_unit_cross,
2458 unsigned int func_unit_data_side, int sploop_ii,
2459 expressionS **fix_exp, int *fix_pcrel,
2460 bfd_reloc_code_real_type *fx_r_type, bfd_boolean *fix_adda,
2461 bfd_boolean *fix_needed, bfd_boolean *ok,
2462 bfd_boolean print_errors, char *str, int opc_len)
2463 {
2464 const tic6x_opcode *opct;
2465 const tic6x_insn_format *fmt;
2466 unsigned int opcode_value;
2467 unsigned int fld;
2468
2469 opct = &tic6x_opcode_table[id];
2470 fmt = &tic6x_insn_format_table[opct->format];
2471 opcode_value = fmt->cst_bits;
2472
2473 for (fld = 0; fld < opct->num_fixed_fields; fld++)
2474 {
2475 if (opct->fixed_fields[fld].min_val == opct->fixed_fields[fld].max_val)
2476 {
2477 const tic6x_insn_field *fldd;
2478 fldd = tic6x_field_from_fmt (fmt, opct->fixed_fields[fld].field_id);
2479 if (fldd == NULL)
2480 abort ();
2481 opcode_value |= opct->fixed_fields[fld].min_val << fldd->bitfields[0].low_pos;
2482 }
2483 }
2484
2485 for (fld = 0; fld < opct->num_variable_fields; fld++)
2486 {
2487 const tic6x_insn_field *fldd;
2488 unsigned int value;
2489 unsigned int opno;
2490 unsigned int ffld;
2491 offsetT sign_value;
2492 unsigned int bits;
2493 unsigned int fcyc_bits;
2494 expressionS *expp;
2495 expressionS ucexp;
2496 tic6x_mem_ref mem;
2497
2498 fldd = tic6x_field_from_fmt (fmt, opct->variable_fields[fld].field_id);
2499 if (fldd == NULL)
2500 abort ();
2501 opno = opct->variable_fields[fld].operand_num;
2502 switch (opct->variable_fields[fld].coding_method)
2503 {
2504 case tic6x_coding_ucst:
2505 if (operands[opno].form != TIC6X_OP_EXP)
2506 abort ();
2507 if (operands[opno].value.exp.X_op != O_constant)
2508 abort ();
2509 ucexp = operands[opno].value.exp;
2510 unsigned_constant:
2511 if (ucexp.X_add_number < 0
2512 || ucexp.X_add_number >= (1 << fldd->bitfields[0].width))
2513 {
2514 if (print_errors)
2515 as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
2516 opc_len, str);
2517 *ok = FALSE;
2518 return 0;
2519 }
2520 value = ucexp.X_add_number;
2521 break;
2522
2523 case tic6x_coding_scst:
2524 if (operands[opno].form != TIC6X_OP_EXP)
2525 abort ();
2526 if (operands[opno].value.exp.X_op != O_constant)
2527 {
2528 value = 0;
2529 /* Opcode table should not permit non-constants without
2530 a known relocation for them. */
2531 if (fldd->bitfields[0].low_pos != 7 || fldd->bitfields[0].width != 16)
2532 abort ();
2533 *fix_needed = TRUE;
2534 *fix_exp = &operands[opno].value.exp;
2535 *fix_pcrel = 0;
2536 *fx_r_type = BFD_RELOC_C6000_ABS_S16;
2537 *fix_adda = FALSE;
2538 break;
2539 }
2540 sign_value = SEXT (operands[opno].value.exp.X_add_number);
2541 signed_constant:
2542 if (sign_value < -(1 << (fldd->bitfields[0].width - 1))
2543 || (sign_value >= (1 << (fldd->bitfields[0].width - 1))))
2544 {
2545 if (print_errors)
2546 as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
2547 opc_len, str);
2548 *ok = FALSE;
2549 return 0;
2550 }
2551 value = sign_value + (1 << (fldd->bitfields[0].width - 1));
2552 value ^= (1 << (fldd->bitfields[0].width - 1));
2553 break;
2554
2555 case tic6x_coding_ucst_minus_one:
2556 if (operands[opno].form != TIC6X_OP_EXP)
2557 abort ();
2558 if (operands[opno].value.exp.X_op != O_constant)
2559 abort ();
2560 if (operands[opno].value.exp.X_add_number <= 0
2561 || operands[opno].value.exp.X_add_number > (1 << fldd->bitfields[0].width))
2562 {
2563 if (print_errors)
2564 as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
2565 opc_len, str);
2566 *ok = FALSE;
2567 return 0;
2568 }
2569 value = operands[opno].value.exp.X_add_number - 1;
2570 break;
2571
2572 case tic6x_coding_scst_negate:
2573 if (operands[opno].form != TIC6X_OP_EXP)
2574 abort ();
2575 if (operands[opno].value.exp.X_op != O_constant)
2576 abort ();
2577 sign_value = SEXT (-operands[opno].value.exp.X_add_number);
2578 goto signed_constant;
2579
2580 case tic6x_coding_ulcst_dpr_byte:
2581 case tic6x_coding_ulcst_dpr_half:
2582 case tic6x_coding_ulcst_dpr_word:
2583 bits = tic6x_dpr_shift (opct->variable_fields[fld].coding_method);
2584 switch (operands[opno].form)
2585 {
2586 case TIC6X_OP_EXP:
2587 if (operands[opno].value.exp.X_op == O_constant)
2588 {
2589 ucexp = operands[opno].value.exp;
2590 goto unsigned_constant;
2591 }
2592 expp = &operands[opno].value.exp;
2593 break;
2594
2595 case TIC6X_OP_MEM_NOUNREG:
2596 mem = operands[opno].value.mem;
2597 tic6x_default_mem_ref (&mem);
2598 if (mem.offset_is_reg)
2599 abort ();
2600 if (mem.offset.exp.X_op == O_constant)
2601 {
2602 ucexp = mem.offset.exp;
2603 if (mem.scaled == tic6x_offset_unscaled)
2604 {
2605 if (ucexp.X_add_number & ((1 << bits) - 1))
2606 {
2607 if (print_errors)
2608 as_bad (_("offset in operand %u of '%.*s' not "
2609 "divisible by %u"), opno + 1, opc_len,
2610 str, 1u << bits);
2611 *ok = FALSE;
2612 return 0;
2613 }
2614 ucexp.X_add_number >>= bits;
2615 }
2616 goto unsigned_constant;
2617 }
2618 if (mem.scaled != tic6x_offset_unscaled)
2619 abort ();
2620 if (operands[opno].value.mem.mod == tic6x_mem_mod_none
2621 || operands[opno].value.mem.scaled != tic6x_offset_unscaled
2622 || operands[opno].value.mem.offset_is_reg)
2623 abort ();
2624 expp = &operands[opno].value.mem.offset.exp;
2625 break;
2626
2627 default:
2628 abort ();
2629 }
2630 value = 0;
2631 /* Opcode table should not use this encoding without a known
2632 relocation. */
2633 if (fldd->bitfields[0].low_pos != 8 || fldd->bitfields[0].width != 15)
2634 abort ();
2635 /* We do not check for offset divisibility here; such a
2636 check is not needed at this point to encode the value,
2637 and if there is eventually a problem it will be detected
2638 either in md_apply_fix or at link time. */
2639 *fix_needed = TRUE;
2640 *fix_exp = expp;
2641 *fix_pcrel = 0;
2642 *fx_r_type
2643 = tic6x_dpr_reloc (opct->variable_fields[fld].coding_method);
2644 if (operands[opno].form == TIC6X_OP_EXP)
2645 *fix_adda = TRUE;
2646 else
2647 *fix_adda = FALSE;
2648 break;
2649
2650 case tic6x_coding_lcst_low16:
2651 if (operands[opno].form != TIC6X_OP_EXP)
2652 abort ();
2653 if (operands[opno].value.exp.X_op == O_constant)
2654 value = operands[opno].value.exp.X_add_number & 0xffff;
2655 else
2656 {
2657 value = 0;
2658 /* Opcode table should not use this encoding without a
2659 known relocation. */
2660 if (fldd->bitfields[0].low_pos != 7 || fldd->bitfields[0].width != 16)
2661 abort ();
2662 *fix_needed = TRUE;
2663 *fix_exp = &operands[opno].value.exp;
2664 *fix_pcrel = 0;
2665 *fx_r_type = BFD_RELOC_C6000_ABS_L16;
2666 *fix_adda = FALSE;
2667 }
2668 break;
2669
2670 case tic6x_coding_lcst_high16:
2671 if (operands[opno].form != TIC6X_OP_EXP)
2672 abort ();
2673 if (operands[opno].value.exp.X_op == O_constant)
2674 value = (operands[opno].value.exp.X_add_number >> 16) & 0xffff;
2675 else
2676 {
2677 value = 0;
2678 /* Opcode table should not use this encoding without a
2679 known relocation. */
2680 if (fldd->bitfields[0].low_pos != 7 || fldd->bitfields[0].width != 16)
2681 abort ();
2682 *fix_needed = TRUE;
2683 *fix_exp = &operands[opno].value.exp;
2684 *fix_pcrel = 0;
2685 *fx_r_type = BFD_RELOC_C6000_ABS_H16;
2686 *fix_adda = FALSE;
2687 }
2688 break;
2689
2690 case tic6x_coding_pcrel:
2691 case tic6x_coding_pcrel_half:
2692 if (operands[opno].form != TIC6X_OP_EXP)
2693 abort ();
2694 value = 0;
2695 *fix_needed = TRUE;
2696 *fix_exp = &operands[opno].value.exp;
2697 *fix_pcrel = 1;
2698 if (fldd->bitfields[0].low_pos == 7 && fldd->bitfields[0].width == 21)
2699 *fx_r_type = BFD_RELOC_C6000_PCR_S21;
2700 else if (fldd->bitfields[0].low_pos == 16 && fldd->bitfields[0].width == 12)
2701 *fx_r_type = BFD_RELOC_C6000_PCR_S12;
2702 else if (fldd->bitfields[0].low_pos == 13 && fldd->bitfields[0].width == 10)
2703 *fx_r_type = BFD_RELOC_C6000_PCR_S10;
2704 else if (fldd->bitfields[0].low_pos == 16 && fldd->bitfields[0].width == 7)
2705 *fx_r_type = BFD_RELOC_C6000_PCR_S7;
2706 else
2707 /* Opcode table should not use this encoding without a
2708 known relocation. */
2709 abort ();
2710 *fix_adda = FALSE;
2711 break;
2712
2713 case tic6x_coding_regpair_lsb:
2714 switch (operands[opno].form)
2715 {
2716 case TIC6X_OP_REGPAIR:
2717 value = operands[opno].value.reg.num;
2718 break;
2719
2720 default:
2721 abort ();
2722 }
2723 break;
2724
2725 case tic6x_coding_regpair_msb:
2726 switch (operands[opno].form)
2727 {
2728 case TIC6X_OP_REGPAIR:
2729 value = operands[opno].value.reg.num + 1;
2730 break;
2731
2732 default:
2733 abort ();
2734 }
2735 break;
2736
2737 case tic6x_coding_reg:
2738 switch (operands[opno].form)
2739 {
2740 case TIC6X_OP_REG:
2741 case TIC6X_OP_REGPAIR:
2742 value = operands[opno].value.reg.num;
2743 break;
2744
2745 case TIC6X_OP_MEM_NOUNREG:
2746 case TIC6X_OP_MEM_UNREG:
2747 value = operands[opno].value.mem.base_reg.num;
2748 break;
2749
2750 default:
2751 abort ();
2752 }
2753 break;
2754
2755 case tic6x_coding_areg:
2756 switch (operands[opno].form)
2757 {
2758 case TIC6X_OP_REG:
2759 value = (operands[opno].value.reg.num == 15 ? 1 : 0);
2760 break;
2761
2762 case TIC6X_OP_MEM_NOUNREG:
2763 value = (operands[opno].value.mem.base_reg.num == 15 ? 1 : 0);
2764 break;
2765
2766 default:
2767 abort ();
2768 }
2769 break;
2770
2771 case tic6x_coding_crlo:
2772 if (operands[opno].form != TIC6X_OP_CTRL)
2773 abort ();
2774 value = tic6x_ctrl_table[operands[opno].value.ctrl].crlo;
2775 break;
2776
2777 case tic6x_coding_crhi:
2778 if (operands[opno].form != TIC6X_OP_CTRL)
2779 abort ();
2780 value = 0;
2781 break;
2782
2783 case tic6x_coding_reg_shift:
2784 if (operands[opno].form != TIC6X_OP_REGPAIR)
2785 abort ();
2786 value = operands[opno].value.reg.num >> 1;
2787 break;
2788
2789 case tic6x_coding_mem_offset:
2790 if (operands[opno].form != TIC6X_OP_MEM_NOUNREG)
2791 abort ();
2792 mem = operands[opno].value.mem;
2793 tic6x_default_mem_ref (&mem);
2794 if (mem.offset_is_reg)
2795 {
2796 if (mem.scaled != tic6x_offset_scaled)
2797 abort ();
2798 value = mem.offset.reg.num;
2799 }
2800 else
2801 {
2802 int scale;
2803
2804 if (mem.offset.exp.X_op != O_constant)
2805 abort ();
2806 switch (mem.scaled)
2807 {
2808 case tic6x_offset_scaled:
2809 scale = 1;
2810 break;
2811
2812 case tic6x_offset_unscaled:
2813 scale = opct->operand_info[opno].size;
2814 if (scale != 1 && scale != 2 && scale != 4 && scale != 8)
2815 abort ();
2816 break;
2817
2818 default:
2819 abort ();
2820 }
2821 if (mem.offset.exp.X_add_number < 0
2822 || mem.offset.exp.X_add_number >= (1 << fldd->bitfields[0].width) * scale)
2823 {
2824 if (print_errors)
2825 as_bad (_("offset in operand %u of '%.*s' out of range"),
2826 opno + 1, opc_len, str);
2827 *ok = FALSE;
2828 return 0;
2829 }
2830 if (mem.offset.exp.X_add_number % scale)
2831 {
2832 if (print_errors)
2833 as_bad (_("offset in operand %u of '%.*s' not "
2834 "divisible by %u"),
2835 opno + 1, opc_len, str, scale);
2836 *ok = FALSE;
2837 return 0;
2838 }
2839 value = mem.offset.exp.X_add_number / scale;
2840 }
2841 break;
2842
2843 case tic6x_coding_mem_offset_noscale:
2844 if (operands[opno].form != TIC6X_OP_MEM_UNREG)
2845 abort ();
2846 mem = operands[opno].value.mem;
2847 tic6x_default_mem_ref (&mem);
2848 if (mem.offset_is_reg)
2849 value = mem.offset.reg.num;
2850 else
2851 {
2852 if (mem.offset.exp.X_op != O_constant)
2853 abort ();
2854 if (mem.offset.exp.X_add_number < 0
2855 || mem.offset.exp.X_add_number >= (1 << fldd->bitfields[0].width))
2856 {
2857 if (print_errors)
2858 as_bad (_("offset in operand %u of '%.*s' out of range"),
2859 opno + 1, opc_len, str);
2860 *ok = FALSE;
2861 return 0;
2862 }
2863 value = mem.offset.exp.X_add_number;
2864 }
2865 break;
2866
2867 case tic6x_coding_mem_mode:
2868 if (operands[opno].form != TIC6X_OP_MEM_NOUNREG
2869 && operands[opno].form != TIC6X_OP_MEM_UNREG)
2870 abort ();
2871 mem = operands[opno].value.mem;
2872 tic6x_default_mem_ref (&mem);
2873 switch (mem.mod)
2874 {
2875 case tic6x_mem_mod_plus:
2876 value = 1;
2877 break;
2878
2879 case tic6x_mem_mod_minus:
2880 value = 0;
2881 break;
2882
2883 case tic6x_mem_mod_preinc:
2884 value = 9;
2885 break;
2886
2887 case tic6x_mem_mod_predec:
2888 value = 8;
2889 break;
2890
2891 case tic6x_mem_mod_postinc:
2892 value = 11;
2893 break;
2894
2895 case tic6x_mem_mod_postdec:
2896 value = 10;
2897 break;
2898
2899 default:
2900 abort ();
2901 }
2902 value += (mem.offset_is_reg ? 4 : 0);
2903 break;
2904
2905 case tic6x_coding_scaled:
2906 if (operands[opno].form != TIC6X_OP_MEM_UNREG)
2907 abort ();
2908 mem = operands[opno].value.mem;
2909 tic6x_default_mem_ref (&mem);
2910 switch (mem.scaled)
2911 {
2912 case tic6x_offset_unscaled:
2913 value = 0;
2914 break;
2915
2916 case tic6x_offset_scaled:
2917 value = 1;
2918 break;
2919
2920 default:
2921 abort ();
2922 }
2923 break;
2924
2925 case tic6x_coding_spmask:
2926 /* The position of such a field is hardcoded in the handling
2927 of "||^". */
2928 if (fldd->bitfields[0].low_pos != 18)
2929 abort ();
2930 value = 0;
2931 for (opno = 0; opno < num_operands; opno++)
2932 {
2933 unsigned int v;
2934
2935 v = tic6x_encode_spmask (operands[opno].value.func_unit.base,
2936 operands[opno].value.func_unit.side);
2937 if (value & v)
2938 {
2939 if (print_errors)
2940 as_bad (_("functional unit already masked for operand "
2941 "%u of '%.*s'"), opno + 1, opc_len, str);
2942 *ok = FALSE;
2943 return 0;
2944 }
2945 value |= v;
2946 }
2947 break;
2948
2949 case tic6x_coding_reg_unused:
2950 /* This is a placeholder; correct handling goes along with
2951 resource constraint checks. */
2952 value = 0;
2953 break;
2954
2955 case tic6x_coding_fstg:
2956 case tic6x_coding_fcyc:
2957 if (operands[opno].form != TIC6X_OP_EXP)
2958 abort ();
2959 if (operands[opno].value.exp.X_op != O_constant)
2960 abort ();
2961 if (!sploop_ii)
2962 {
2963 if (print_errors)
2964 as_bad (_("'%.*s' instruction not in a software "
2965 "pipelined loop"),
2966 opc_len, str);
2967 *ok = FALSE;
2968 return 0;
2969 }
2970
2971 if (sploop_ii <= 1)
2972 fcyc_bits = 0;
2973 else if (sploop_ii <= 2)
2974 fcyc_bits = 1;
2975 else if (sploop_ii <= 4)
2976 fcyc_bits = 2;
2977 else if (sploop_ii <= 8)
2978 fcyc_bits = 3;
2979 else if (sploop_ii <= 14)
2980 fcyc_bits = 4;
2981 else
2982 abort ();
2983 if (fcyc_bits > fldd->bitfields[0].width)
2984 abort ();
2985
2986 if (opct->variable_fields[fld].coding_method == tic6x_coding_fstg)
2987 {
2988 int i, t;
2989 if (operands[opno].value.exp.X_add_number < 0
2990 || (operands[opno].value.exp.X_add_number
2991 >= (1 << (fldd->bitfields[0].width - fcyc_bits))))
2992 {
2993 if (print_errors)
2994 as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
2995 opc_len, str);
2996 *ok = FALSE;
2997 return 0;
2998 }
2999 value = operands[opno].value.exp.X_add_number;
3000 for (t = 0, i = fcyc_bits; i < fldd->bitfields[0].width; i++)
3001 {
3002 t = (t << 1) | (value & 1);
3003 value >>= 1;
3004 }
3005 value = t << fcyc_bits;
3006 }
3007 else
3008 {
3009 if (operands[opno].value.exp.X_add_number < 0
3010 || (operands[opno].value.exp.X_add_number >= sploop_ii))
3011 {
3012 if (print_errors)
3013 as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
3014 opc_len, str);
3015 *ok = FALSE;
3016 return 0;
3017 }
3018 value = operands[opno].value.exp.X_add_number;
3019 }
3020 break;
3021
3022 case tic6x_coding_fu:
3023 value = func_unit_side == 2 ? 1 : 0;
3024 break;
3025
3026 case tic6x_coding_data_fu:
3027 value = func_unit_data_side == 2 ? 1 : 0;
3028 break;
3029
3030 case tic6x_coding_xpath:
3031 value = func_unit_cross;
3032 break;
3033
3034 default:
3035 abort ();
3036 }
3037
3038 for (ffld = 0; ffld < opct->num_fixed_fields; ffld++)
3039 if ((opct->fixed_fields[ffld].field_id
3040 == opct->variable_fields[fld].field_id)
3041 && (value < opct->fixed_fields[ffld].min_val
3042 || value > opct->fixed_fields[ffld].max_val))
3043 {
3044 if (print_errors)
3045 as_bad (_("operand %u of '%.*s' out of range"), opno + 1,
3046 opc_len, str);
3047 *ok = FALSE;
3048 return 0;
3049 }
3050
3051 opcode_value |= value << fldd->bitfields[0].low_pos;
3052 }
3053
3054 if (this_line_creg)
3055 {
3056 const tic6x_insn_field *creg;
3057 const tic6x_insn_field *z;
3058
3059 creg = tic6x_field_from_fmt (fmt, tic6x_field_creg);
3060 if (creg == NULL)
3061 {
3062 if (print_errors)
3063 as_bad (_("instruction '%.*s' cannot be predicated"),
3064 opc_len, str);
3065 *ok = FALSE;
3066 return 0;
3067 }
3068 z = tic6x_field_from_fmt (fmt, tic6x_field_z);
3069 /* If there is a creg field, there must be a z field; otherwise
3070 there is an error in the format table. */
3071 if (z == NULL)
3072 abort ();
3073
3074 opcode_value |= this_line_creg << creg->bitfields[0].low_pos;
3075 opcode_value |= this_line_z << z->bitfields[0].low_pos;
3076 }
3077
3078 *ok = TRUE;
3079 return opcode_value;
3080 }
3081
3082 /* Convert the target integer stored in N bytes in BUF to a host
3083 integer, returning that value. */
3084
3085 static valueT
3086 md_chars_to_number (char *buf, int n)
3087 {
3088 valueT result = 0;
3089 unsigned char *p = (unsigned char *) buf;
3090
3091 if (target_big_endian)
3092 {
3093 while (n--)
3094 {
3095 result <<= 8;
3096 result |= (*p++ & 0xff);
3097 }
3098 }
3099 else
3100 {
3101 while (n--)
3102 {
3103 result <<= 8;
3104 result |= (p[n] & 0xff);
3105 }
3106 }
3107
3108 return result;
3109 }
3110
3111 /* Assemble the instruction starting at STR (an opcode, with the
3112 opcode name all-lowercase). */
3113
3114 void
3115 md_assemble (char *str)
3116 {
3117 char *p;
3118 int opc_len;
3119 bfd_boolean this_line_parallel;
3120 bfd_boolean this_line_spmask;
3121 unsigned int this_line_creg;
3122 unsigned int this_line_z;
3123 tic6x_label_list *this_insn_label_list;
3124 segment_info_type *seginfo;
3125 tic6x_opcode_list *opc_list, *opc;
3126 tic6x_func_unit_base func_unit_base = tic6x_func_unit_nfu;
3127 unsigned int func_unit_side = 0;
3128 unsigned int func_unit_cross = 0;
3129 unsigned int cross_side = 0;
3130 unsigned int func_unit_data_side = 0;
3131 unsigned int max_matching_opcodes, num_matching_opcodes;
3132 tic6x_opcode_id *opcm = NULL;
3133 unsigned int opc_rank[TIC6X_NUM_PREFER];
3134 const tic6x_opcode *opct = NULL;
3135 int min_rank, try_rank, max_rank;
3136 bfd_boolean num_operands_permitted[TIC6X_MAX_SOURCE_OPERANDS + 1]
3137 = { FALSE };
3138 unsigned int operand_forms[TIC6X_MAX_SOURCE_OPERANDS] = { 0 };
3139 tic6x_operand operands[TIC6X_MAX_SOURCE_OPERANDS];
3140 unsigned int max_num_operands;
3141 unsigned int num_operands_read;
3142 bfd_boolean ok_this_arch, ok_this_fu, ok_this_arch_fu;
3143 bfd_boolean bad_operands = FALSE;
3144 unsigned int opcode_value;
3145 bfd_boolean encoded_ok;
3146 bfd_boolean fix_needed = FALSE;
3147 expressionS *fix_exp = NULL;
3148 int fix_pcrel = 0;
3149 bfd_reloc_code_real_type fx_r_type = BFD_RELOC_UNUSED;
3150 bfd_boolean fix_adda = FALSE;
3151 fragS *insn_frag;
3152 char *output;
3153
3154 p = str;
3155 while (*p && !is_end_of_line[(unsigned char) *p] && *p != ' ')
3156 p++;
3157
3158 /* This function should only have been called when there is actually
3159 an instruction to assemble. */
3160 if (p == str)
3161 abort ();
3162
3163 /* Now an instruction has been seen, architecture attributes from
3164 .arch directives merge with rather than overriding the previous
3165 value. */
3166 tic6x_seen_insns = TRUE;
3167 /* If no .arch directives or -march options have been seen, we are
3168 assessing instruction validity based on the C674X default, so set
3169 the attribute accordingly. */
3170 if (tic6x_arch_attribute == C6XABI_Tag_ISA_none)
3171 tic6x_arch_attribute = C6XABI_Tag_ISA_C674X;
3172
3173 /* Reset global settings for parallel bars and predicates now to
3174 avoid extra errors if there are problems with this opcode. */
3175 this_line_parallel = tic6x_line_parallel;
3176 this_line_spmask = tic6x_line_spmask;
3177 this_line_creg = tic6x_line_creg;
3178 this_line_z = tic6x_line_z;
3179 tic6x_line_parallel = FALSE;
3180 tic6x_line_spmask = FALSE;
3181 tic6x_line_creg = 0;
3182 tic6x_line_z = 0;
3183 seginfo = seg_info (now_seg);
3184 this_insn_label_list = seginfo->tc_segment_info_data.label_list;
3185 seginfo->tc_segment_info_data.label_list = NULL;
3186
3187 opc_list = str_hash_find_n (opcode_hash, str, p - str);
3188 if (opc_list == NULL)
3189 {
3190 char c = *p;
3191 *p = 0;
3192 as_bad (_("unknown opcode '%s'"), str);
3193 *p = c;
3194 return;
3195 }
3196
3197 opc_len = p - str;
3198 skip_whitespace (p);
3199
3200 /* See if there is something that looks like a functional unit
3201 specifier. */
3202 if (*p == '.')
3203 {
3204 bfd_boolean good_func_unit;
3205 tic6x_func_unit_base maybe_base = tic6x_func_unit_nfu;
3206 unsigned int maybe_side = 0;
3207 unsigned int maybe_cross = 0;
3208 unsigned int maybe_data_side = 0;
3209
3210 good_func_unit = tic6x_parse_func_unit_base (p + 1, &maybe_base,
3211 &maybe_side);
3212
3213 if (good_func_unit)
3214 {
3215 if (p[3] == ' ' || is_end_of_line[(unsigned char) p[3]])
3216 p += 3;
3217 else if ((p[3] == 'x' || p[3] == 'X')
3218 && (p[4] == ' ' || is_end_of_line[(unsigned char) p[4]]))
3219 {
3220 maybe_cross = 1;
3221 p += 4;
3222 }
3223 else if (maybe_base == tic6x_func_unit_d
3224 && (p[3] == 't' || p[3] == 'T')
3225 && (p[4] == '1' || p[4] == '2')
3226 && (p[5] == ' ' || is_end_of_line[(unsigned char) p[5]]))
3227 {
3228 maybe_data_side = p[4] - '0';
3229 p += 5;
3230 }
3231 else
3232 good_func_unit = FALSE;
3233 }
3234
3235 if (good_func_unit)
3236 {
3237 func_unit_base = maybe_base;
3238 func_unit_side = maybe_side;
3239 func_unit_cross = maybe_cross;
3240 cross_side = (func_unit_cross ? 3 - func_unit_side : func_unit_side);
3241 func_unit_data_side = maybe_data_side;
3242 }
3243
3244 skip_whitespace (p);
3245 }
3246
3247 /* Determine which entries in the opcode table match, and the
3248 associated permitted forms of operands. */
3249 max_matching_opcodes = 0;
3250 for (opc = opc_list; opc; opc = opc->next)
3251 max_matching_opcodes++;
3252 num_matching_opcodes = 0;
3253 opcm = XNEWVEC (tic6x_opcode_id, max_matching_opcodes);
3254 max_num_operands = 0;
3255 ok_this_arch = FALSE;
3256 ok_this_fu = FALSE;
3257 ok_this_arch_fu = FALSE;
3258 for (opc = opc_list; opc; opc = opc->next)
3259 {
3260 unsigned int num_operands;
3261 unsigned int i;
3262 bfd_boolean this_opc_arch_ok = TRUE;
3263 bfd_boolean this_opc_fu_ok = TRUE;
3264
3265 if (tic6x_insn_format_table[tic6x_opcode_table[opc->id].format].num_bits
3266 != 32)
3267 continue;
3268 if (!(tic6x_opcode_table[opc->id].isa_variants & tic6x_features))
3269 this_opc_arch_ok = FALSE;
3270 if (tic6x_opcode_table[opc->id].func_unit != func_unit_base)
3271 this_opc_fu_ok = FALSE;
3272 if (func_unit_side == 1
3273 && (tic6x_opcode_table[opc->id].flags & TIC6X_FLAG_SIDE_B_ONLY))
3274 this_opc_fu_ok = FALSE;
3275 if (func_unit_cross
3276 && (tic6x_opcode_table[opc->id].flags & TIC6X_FLAG_NO_CROSS))
3277 this_opc_fu_ok = FALSE;
3278 if (!func_unit_data_side
3279 && (tic6x_opcode_table[opc->id].flags
3280 & (TIC6X_FLAG_LOAD | TIC6X_FLAG_STORE)))
3281 this_opc_fu_ok = FALSE;
3282 if (func_unit_data_side
3283 && !(tic6x_opcode_table[opc->id].flags
3284 & (TIC6X_FLAG_LOAD | TIC6X_FLAG_STORE)))
3285 this_opc_fu_ok = FALSE;
3286 if (func_unit_data_side == 1
3287 && (tic6x_opcode_table[opc->id].flags & TIC6X_FLAG_SIDE_T2_ONLY))
3288 this_opc_fu_ok = FALSE;
3289 if (this_opc_arch_ok)
3290 ok_this_arch = TRUE;
3291 if (this_opc_fu_ok)
3292 ok_this_fu = TRUE;
3293 if (!this_opc_arch_ok || !this_opc_fu_ok)
3294 continue;
3295 ok_this_arch_fu = TRUE;
3296 opcm[num_matching_opcodes] = opc->id;
3297 num_matching_opcodes++;
3298 num_operands = tic6x_opcode_table[opc->id].num_operands;
3299
3300 if (tic6x_opcode_table[opc->id].flags & TIC6X_FLAG_SPMASK)
3301 {
3302 if (num_operands != 1
3303 || (tic6x_opcode_table[opc->id].operand_info[0].form
3304 != tic6x_operand_func_unit))
3305 abort ();
3306 num_operands = 8;
3307 for (i = 0; i < num_operands; i++)
3308 {
3309 operand_forms[i]
3310 |= tic6x_coarse_operand_form (tic6x_operand_func_unit);
3311 num_operands_permitted[i] = TRUE;
3312 }
3313 }
3314 else
3315 {
3316 for (i = 0; i < num_operands; i++)
3317 {
3318 tic6x_operand_form f
3319 = tic6x_opcode_table[opc->id].operand_info[i].form;
3320
3321 operand_forms[i] |= tic6x_coarse_operand_form (f);
3322 }
3323 }
3324 num_operands_permitted[num_operands] = TRUE;
3325 if (num_operands > max_num_operands)
3326 max_num_operands = num_operands;
3327 }
3328
3329 if (!ok_this_arch)
3330 {
3331 as_bad (_("'%.*s' instruction not supported on this architecture"),
3332 opc_len, str);
3333 free (opcm);
3334 return;
3335 }
3336
3337 if (!ok_this_fu)
3338 {
3339 as_bad (_("'%.*s' instruction not supported on this functional unit"),
3340 opc_len, str);
3341 free (opcm);
3342 return;
3343 }
3344
3345 if (!ok_this_arch_fu)
3346 {
3347 as_bad (_("'%.*s' instruction not supported on this functional unit"
3348 " for this architecture"),
3349 opc_len, str);
3350 free (opcm);
3351 return;
3352 }
3353
3354 /* If there were no instructions matching the above availability
3355 checks, we should now have given an error and returned. */
3356 if (num_matching_opcodes == 0)
3357 abort ();
3358
3359 num_operands_read = 0;
3360 while (TRUE)
3361 {
3362 skip_whitespace (p);
3363 if (is_end_of_line[(unsigned char) *p])
3364 {
3365 if (num_operands_read > 0)
3366 {
3367 as_bad (_("missing operand after comma"));
3368 bad_operands = TRUE;
3369 }
3370 break;
3371 }
3372
3373 if (max_num_operands == 0)
3374 {
3375 as_bad (_("too many operands to '%.*s'"), opc_len, str);
3376 bad_operands = TRUE;
3377 break;
3378 }
3379
3380 if (!tic6x_parse_operand (&p, &operands[num_operands_read],
3381 operand_forms[num_operands_read], str, opc_len,
3382 num_operands_read + 1))
3383 bad_operands = TRUE;
3384 num_operands_read++;
3385
3386 if (is_end_of_line[(unsigned char) *p])
3387 break;
3388 else if (*p == ',')
3389 {
3390 p++;
3391 if (num_operands_read == max_num_operands)
3392 {
3393 as_bad (_("too many operands to '%.*s'"), opc_len, str);
3394 bad_operands = TRUE;
3395 break;
3396 }
3397 continue;
3398 }
3399 else
3400 /* Operand parsing should consume whole operands. */
3401 abort ();
3402 }
3403
3404 if (!bad_operands && !num_operands_permitted[num_operands_read])
3405 {
3406 as_bad (_("bad number of operands to '%.*s'"), opc_len, str);
3407 bad_operands = TRUE;
3408 }
3409
3410 if (!bad_operands)
3411 {
3412 /* Each operand is of the right syntactic form for some opcode
3413 choice, and the number of operands is valid. Check that each
3414 operand is OK in detail for some opcode choice with the right
3415 number of operands. */
3416 unsigned int i;
3417
3418 for (i = 0; i < num_operands_read; i++)
3419 {
3420 bfd_boolean coarse_ok = FALSE;
3421 bfd_boolean fine_ok = FALSE;
3422 tic6x_operand_match fine_failure = tic6x_match_matches;
3423 unsigned int j;
3424
3425 for (j = 0; j < num_matching_opcodes; j++)
3426 {
3427 tic6x_operand_form f;
3428 tic6x_rw rw;
3429 unsigned int cf;
3430 tic6x_operand_match this_fine_failure;
3431
3432 if (tic6x_opcode_table[opcm[j]].flags & TIC6X_FLAG_SPMASK)
3433 {
3434 f = tic6x_operand_func_unit;
3435 rw = tic6x_rw_none;
3436 }
3437 else
3438 {
3439 if (tic6x_opcode_table[opcm[j]].num_operands
3440 != num_operands_read)
3441 continue;
3442
3443 f = tic6x_opcode_table[opcm[j]].operand_info[i].form;
3444 rw = tic6x_opcode_table[opcm[j]].operand_info[i].rw;
3445 }
3446 cf = tic6x_coarse_operand_form (f);
3447
3448 if (operands[i].form != cf)
3449 continue;
3450
3451 coarse_ok = TRUE;
3452 this_fine_failure
3453 = tic6x_operand_matches_form (&operands[i], f, rw,
3454 func_unit_side,
3455 cross_side,
3456 func_unit_data_side);
3457 if (this_fine_failure == tic6x_match_matches)
3458 {
3459 fine_ok = TRUE;
3460 break;
3461 }
3462 if (fine_failure == tic6x_match_matches
3463 || fine_failure > this_fine_failure)
3464 fine_failure = this_fine_failure;
3465 }
3466
3467 /* No instructions should have operand syntactic forms only
3468 acceptable with certain numbers of operands, so no
3469 diagnostic for this case. */
3470 if (!coarse_ok)
3471 abort ();
3472
3473 if (!fine_ok)
3474 {
3475 switch (fine_failure)
3476 {
3477 case tic6x_match_non_const:
3478 as_bad (_("operand %u of '%.*s' not constant"),
3479 i + 1, opc_len, str);
3480 break;
3481
3482 case tic6x_match_wrong_side:
3483 as_bad (_("operand %u of '%.*s' on wrong side"),
3484 i + 1, opc_len, str);
3485 break;
3486
3487 case tic6x_match_bad_return:
3488 as_bad (_("operand %u of '%.*s' not a valid return "
3489 "address register"),
3490 i + 1, opc_len, str);
3491 break;
3492
3493 case tic6x_match_ctrl_write_only:
3494 as_bad (_("operand %u of '%.*s' is write-only"),
3495 i + 1, opc_len, str);
3496 break;
3497
3498 case tic6x_match_ctrl_read_only:
3499 as_bad (_("operand %u of '%.*s' is read-only"),
3500 i + 1, opc_len, str);
3501 break;
3502
3503 case tic6x_match_bad_mem:
3504 as_bad (_("operand %u of '%.*s' not a valid memory "
3505 "reference"),
3506 i + 1, opc_len, str);
3507 break;
3508
3509 case tic6x_match_bad_address:
3510 as_bad (_("operand %u of '%.*s' not a valid base "
3511 "address register"),
3512 i + 1, opc_len, str);
3513 break;
3514
3515 default:
3516 abort ();
3517 }
3518 bad_operands = TRUE;
3519 break;
3520 }
3521 }
3522 }
3523
3524 if (!bad_operands)
3525 {
3526 /* Each operand is OK for some opcode choice, and the number of
3527 operands is valid. Check whether there is an opcode choice
3528 for which all operands are simultaneously valid. */
3529 unsigned int i;
3530 bfd_boolean found_match = FALSE;
3531
3532 for (i = 0; i < TIC6X_NUM_PREFER; i++)
3533 opc_rank[i] = (unsigned int) -1;
3534
3535 min_rank = TIC6X_NUM_PREFER - 1;
3536 max_rank = 0;
3537
3538 for (i = 0; i < num_matching_opcodes; i++)
3539 {
3540 unsigned int j;
3541 bfd_boolean this_matches = TRUE;
3542
3543 if (!(tic6x_opcode_table[opcm[i]].flags & TIC6X_FLAG_SPMASK)
3544 && tic6x_opcode_table[opcm[i]].num_operands != num_operands_read)
3545 continue;
3546
3547 for (j = 0; j < num_operands_read; j++)
3548 {
3549 tic6x_operand_form f;
3550 tic6x_rw rw;
3551
3552 if (tic6x_opcode_table[opcm[i]].flags & TIC6X_FLAG_SPMASK)
3553 {
3554 f = tic6x_operand_func_unit;
3555 rw = tic6x_rw_none;
3556 }
3557 else
3558 {
3559 f = tic6x_opcode_table[opcm[i]].operand_info[j].form;
3560 rw = tic6x_opcode_table[opcm[i]].operand_info[j].rw;
3561 }
3562 if (tic6x_operand_matches_form (&operands[j], f, rw,
3563 func_unit_side,
3564 cross_side,
3565 func_unit_data_side)
3566 != tic6x_match_matches)
3567 {
3568 this_matches = FALSE;
3569 break;
3570 }
3571 }
3572
3573 if (this_matches)
3574 {
3575 int rank = TIC6X_PREFER_VAL (tic6x_opcode_table[opcm[i]].flags);
3576
3577 if (rank < min_rank)
3578 min_rank = rank;
3579 if (rank > max_rank)
3580 max_rank = rank;
3581
3582 if (opc_rank[rank] == (unsigned int) -1)
3583 opc_rank[rank] = i;
3584 else
3585 /* The opcode table should provide a total ordering
3586 for all cases where multiple matches may get
3587 here. */
3588 abort ();
3589
3590 found_match = TRUE;
3591 }
3592 }
3593
3594 if (!found_match)
3595 {
3596 as_bad (_("bad operand combination for '%.*s'"), opc_len, str);
3597 bad_operands = TRUE;
3598 }
3599 }
3600
3601 if (bad_operands)
3602 {
3603 free (opcm);
3604 return;
3605 }
3606
3607 opcode_value = 0;
3608 encoded_ok = FALSE;
3609 for (try_rank = max_rank; try_rank >= min_rank; try_rank--)
3610 {
3611 fix_needed = FALSE;
3612
3613 if (opc_rank[try_rank] == (unsigned int) -1)
3614 continue;
3615
3616 opcode_value = tic6x_try_encode (opcm[opc_rank[try_rank]], operands,
3617 num_operands_read, this_line_creg,
3618 this_line_z, func_unit_side,
3619 func_unit_cross, func_unit_data_side,
3620 seginfo->tc_segment_info_data.sploop_ii,
3621 &fix_exp, &fix_pcrel, &fx_r_type,
3622 &fix_adda, &fix_needed, &encoded_ok,
3623 (try_rank == min_rank ? TRUE : FALSE),
3624 str, opc_len);
3625 if (encoded_ok)
3626 {
3627 opct = &tic6x_opcode_table[opcm[opc_rank[try_rank]]];
3628 break;
3629 }
3630 }
3631
3632 free (opcm);
3633
3634 if (!encoded_ok)
3635 return;
3636
3637 if (this_line_parallel)
3638 {
3639 insn_frag = seginfo->tc_segment_info_data.execute_packet_frag;
3640 if (insn_frag == NULL)
3641 {
3642 as_bad (_("parallel instruction not following another instruction"));
3643 return;
3644 }
3645
3646 if (insn_frag->fr_fix >= 32)
3647 {
3648 as_bad (_("too many instructions in execute packet"));
3649 return;
3650 }
3651
3652 if (this_insn_label_list != NULL)
3653 as_bad (_("label not at start of execute packet"));
3654
3655 if (opct->flags & TIC6X_FLAG_FIRST)
3656 as_bad (_("'%.*s' instruction not at start of execute packet"),
3657 opc_len, str);
3658
3659 *seginfo->tc_segment_info_data.last_insn_lsb |= 0x1;
3660 output = insn_frag->fr_literal + insn_frag->fr_fix;
3661 }
3662 else
3663 {
3664 tic6x_label_list *l;
3665
3666 seginfo->tc_segment_info_data.spmask_addr = NULL;
3667 seginfo->tc_segment_info_data.func_units_used = 0;
3668
3669 /* Start a new frag for this execute packet. */
3670 if (frag_now_fix () != 0)
3671 {
3672 if (frag_now->fr_type != rs_machine_dependent)
3673 frag_wane (frag_now);
3674
3675 frag_new (0);
3676 }
3677 frag_grow (32);
3678 insn_frag = seginfo->tc_segment_info_data.execute_packet_frag = frag_now;
3679 for (l = this_insn_label_list; l; l = l->next)
3680 {
3681 symbol_set_frag (l->label, frag_now);
3682 S_SET_VALUE (l->label, 0);
3683 S_SET_SEGMENT (l->label, now_seg);
3684 }
3685 tic6x_free_label_list (this_insn_label_list);
3686 dwarf2_emit_insn (0);
3687 output = frag_var (rs_machine_dependent, 32, 32, 0, NULL, 0, NULL);
3688 /* This must be the same as the frag to which a pointer was just
3689 saved. */
3690 if (output != insn_frag->fr_literal)
3691 abort ();
3692 insn_frag->tc_frag_data.is_insns = TRUE;
3693 insn_frag->tc_frag_data.can_cross_fp_boundary
3694 = tic6x_can_cross_fp_boundary;
3695 }
3696
3697 if (func_unit_base != tic6x_func_unit_nfu)
3698 {
3699 unsigned int func_unit_enc;
3700
3701 func_unit_enc = tic6x_encode_spmask (func_unit_base, func_unit_side);
3702
3703 if (seginfo->tc_segment_info_data.func_units_used & func_unit_enc)
3704 as_bad (_("functional unit already used in this execute packet"));
3705
3706 seginfo->tc_segment_info_data.func_units_used |= func_unit_enc;
3707 }
3708
3709 if (opct->flags & TIC6X_FLAG_SPLOOP)
3710 {
3711 if (seginfo->tc_segment_info_data.sploop_ii)
3712 as_bad (_("nested software pipelined loop"));
3713 if (num_operands_read != 1
3714 || operands[0].form != TIC6X_OP_EXP
3715 || operands[0].value.exp.X_op != O_constant)
3716 abort ();
3717 seginfo->tc_segment_info_data.sploop_ii
3718 = operands[0].value.exp.X_add_number;
3719 }
3720 else if (opct->flags & TIC6X_FLAG_SPKERNEL)
3721 {
3722 if (!seginfo->tc_segment_info_data.sploop_ii)
3723 as_bad (_("'%.*s' instruction not in a software pipelined loop"),
3724 opc_len, str);
3725 seginfo->tc_segment_info_data.sploop_ii = 0;
3726 }
3727
3728 if (this_line_spmask)
3729 {
3730 if (seginfo->tc_segment_info_data.spmask_addr == NULL)
3731 as_bad (_("'||^' without previous SPMASK"));
3732 else if (func_unit_base == tic6x_func_unit_nfu)
3733 as_bad (_("cannot mask instruction using no functional unit"));
3734 else
3735 {
3736 unsigned int spmask_opcode;
3737 unsigned int mask_bit;
3738
3739 spmask_opcode
3740 = md_chars_to_number (seginfo->tc_segment_info_data.spmask_addr,
3741 4);
3742 mask_bit = tic6x_encode_spmask (func_unit_base, func_unit_side);
3743 mask_bit <<= 18;
3744 if (spmask_opcode & mask_bit)
3745 as_bad (_("functional unit already masked"));
3746 spmask_opcode |= mask_bit;
3747 md_number_to_chars (seginfo->tc_segment_info_data.spmask_addr,
3748 spmask_opcode, 4);
3749 }
3750 }
3751
3752 record_alignment (now_seg, 5);
3753 md_number_to_chars (output, opcode_value, 4);
3754 if (fix_needed)
3755 tic6x_fix_new_exp (insn_frag, output - insn_frag->fr_literal, 4, fix_exp,
3756 fix_pcrel, fx_r_type, fix_adda);
3757 insn_frag->fr_fix += 4;
3758 insn_frag->fr_var -= 4;
3759 seginfo->tc_segment_info_data.last_insn_lsb
3760 = (target_big_endian ? output + 3 : output);
3761 if (opct->flags & TIC6X_FLAG_SPMASK)
3762 seginfo->tc_segment_info_data.spmask_addr = output;
3763 }
3764
3765 /* Modify NEWVAL (32-bit) by inserting VALUE, shifted right by SHIFT
3766 and the least significant BITS bits taken, at position POS. */
3767 #define MODIFY_VALUE(NEWVAL, VALUE, SHIFT, POS, BITS) \
3768 do { \
3769 (NEWVAL) &= 0xffffffffU & ~(((1U << (BITS)) - 1) << (POS)); \
3770 (NEWVAL) |= (((VALUE) >> (SHIFT)) & ((1U << (BITS)) - 1)) << (POS); \
3771 } while (0)
3772
3773 /* Apply a fixup to the object file. */
3774
3775 void
3776 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
3777 {
3778 valueT value = *valP;
3779 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3780
3781 value = SEXT (value);
3782 *valP = value;
3783
3784 fixP->fx_offset = SEXT (fixP->fx_offset);
3785
3786 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
3787 fixP->fx_done = 1;
3788
3789 /* We do our own overflow checks. */
3790 fixP->fx_no_overflow = 1;
3791
3792 switch (fixP->fx_r_type)
3793 {
3794 case BFD_RELOC_NONE:
3795 case BFD_RELOC_C6000_EHTYPE:
3796 /* Force output to the object file. */
3797 fixP->fx_done = 0;
3798 break;
3799
3800 case BFD_RELOC_32:
3801 if (fixP->fx_done || !seg->use_rela_p)
3802 md_number_to_chars (buf, value, 4);
3803 break;
3804
3805 case BFD_RELOC_16:
3806 if (fixP->fx_done || !seg->use_rela_p)
3807 {
3808 if (value + 0x8000 > 0xffff + 0x8000)
3809 as_bad_where (fixP->fx_file, fixP->fx_line,
3810 _("value too large for 2-byte field"));
3811 md_number_to_chars (buf, value, 2);
3812 }
3813 break;
3814
3815 case BFD_RELOC_8:
3816 if (fixP->fx_done || !seg->use_rela_p)
3817 {
3818 if (value + 0x80 > 0xff + 0x80)
3819 as_bad_where (fixP->fx_file, fixP->fx_line,
3820 _("value too large for 1-byte field"));
3821 *buf = value;
3822 }
3823 break;
3824
3825 case BFD_RELOC_C6000_ABS_S16:
3826 case BFD_RELOC_C6000_ABS_L16:
3827 case BFD_RELOC_C6000_SBR_S16:
3828 case BFD_RELOC_C6000_SBR_L16_B:
3829 case BFD_RELOC_C6000_SBR_L16_H:
3830 case BFD_RELOC_C6000_SBR_L16_W:
3831 case BFD_RELOC_C6000_SBR_GOT_L16_W:
3832 if (fixP->fx_done || !seg->use_rela_p)
3833 {
3834 valueT newval = md_chars_to_number (buf, 4);
3835 int shift;
3836
3837 switch (fixP->fx_r_type)
3838 {
3839 case BFD_RELOC_C6000_SBR_L16_H:
3840 shift = 1;
3841 break;
3842
3843 case BFD_RELOC_C6000_SBR_L16_W:
3844 case BFD_RELOC_C6000_SBR_GOT_L16_W:
3845 shift = 2;
3846 break;
3847
3848 default:
3849 shift = 0;
3850 break;
3851 }
3852
3853 MODIFY_VALUE (newval, value, shift, 7, 16);
3854 if ((value + 0x8000 > 0x7fff + 0x8000)
3855 && (fixP->fx_r_type == BFD_RELOC_C6000_ABS_S16
3856 || fixP->fx_r_type == BFD_RELOC_C6000_SBR_S16))
3857 as_bad_where (fixP->fx_file, fixP->fx_line,
3858 _("immediate offset out of range"));
3859
3860 md_number_to_chars (buf, newval, 4);
3861 }
3862 if (fixP->fx_done
3863 && fixP->fx_r_type != BFD_RELOC_C6000_ABS_S16
3864 && fixP->fx_r_type != BFD_RELOC_C6000_ABS_L16)
3865 abort ();
3866 break;
3867
3868 case BFD_RELOC_C6000_ABS_H16:
3869 case BFD_RELOC_C6000_SBR_H16_B:
3870 case BFD_RELOC_C6000_SBR_H16_H:
3871 case BFD_RELOC_C6000_SBR_H16_W:
3872 case BFD_RELOC_C6000_SBR_GOT_H16_W:
3873 if (fixP->fx_done || !seg->use_rela_p)
3874 {
3875 valueT newval = md_chars_to_number (buf, 4);
3876 int shift;
3877
3878 switch (fixP->fx_r_type)
3879 {
3880 case BFD_RELOC_C6000_SBR_H16_H:
3881 shift = 17;
3882 break;
3883
3884 case BFD_RELOC_C6000_SBR_H16_W:
3885 case BFD_RELOC_C6000_SBR_GOT_H16_W:
3886 shift = 18;
3887 break;
3888
3889 default:
3890 shift = 16;
3891 break;
3892 }
3893
3894 MODIFY_VALUE (newval, value, shift, 7, 16);
3895
3896 md_number_to_chars (buf, newval, 4);
3897 }
3898 if (fixP->fx_done && fixP->fx_r_type != BFD_RELOC_C6000_ABS_H16)
3899 abort ();
3900 break;
3901
3902 case BFD_RELOC_C6000_PCR_H16:
3903 case BFD_RELOC_C6000_PCR_L16:
3904 if (fixP->fx_done || !seg->use_rela_p)
3905 {
3906 valueT newval = md_chars_to_number (buf, 4);
3907 int shift = fixP->fx_r_type == BFD_RELOC_C6000_PCR_H16 ? 16 : 0;
3908
3909 MODIFY_VALUE (newval, value, shift, 7, 16);
3910
3911 md_number_to_chars (buf, newval, 4);
3912 }
3913 break;
3914
3915 case BFD_RELOC_C6000_SBR_U15_B:
3916 if (fixP->fx_done || !seg->use_rela_p)
3917 {
3918 valueT newval = md_chars_to_number (buf, 4);
3919
3920 MODIFY_VALUE (newval, value, 0, 8, 15);
3921 if (value > 0x7fff)
3922 as_bad_where (fixP->fx_file, fixP->fx_line,
3923 _("immediate offset out of range"));
3924
3925 md_number_to_chars (buf, newval, 4);
3926 }
3927 break;
3928
3929 case BFD_RELOC_C6000_SBR_U15_H:
3930 if (fixP->fx_done || !seg->use_rela_p)
3931 {
3932 valueT newval = md_chars_to_number (buf, 4);
3933
3934 /* Constant ADDA operands, processed as constant when the
3935 instruction is parsed, are encoded as-is rather than
3936 shifted. If the operand of an ADDA instruction is now
3937 constant (for example, the difference between two labels
3938 found after the instruction), ensure it is encoded the
3939 same way it would have been if the constant value had
3940 been known when the instruction was parsed. */
3941 if (fixP->tc_fix_data.fix_adda && fixP->fx_done)
3942 value <<= 1;
3943
3944 MODIFY_VALUE (newval, value, 1, 8, 15);
3945 if (value & 1)
3946 as_bad_where (fixP->fx_file, fixP->fx_line,
3947 _("immediate offset not 2-byte-aligned"));
3948 if (value > 0xfffe)
3949 as_bad_where (fixP->fx_file, fixP->fx_line,
3950 _("immediate offset out of range"));
3951
3952 md_number_to_chars (buf, newval, 4);
3953 }
3954 break;
3955
3956 case BFD_RELOC_C6000_SBR_U15_W:
3957 case BFD_RELOC_C6000_SBR_GOT_U15_W:
3958 if (fixP->fx_done || !seg->use_rela_p)
3959 {
3960 valueT newval = md_chars_to_number (buf, 4);
3961
3962 /* Constant ADDA operands, processed as constant when the
3963 instruction is parsed, are encoded as-is rather than
3964 shifted. If the operand of an ADDA instruction is now
3965 constant (for example, the difference between two labels
3966 found after the instruction), ensure it is encoded the
3967 same way it would have been if the constant value had
3968 been known when the instruction was parsed. */
3969 if (fixP->tc_fix_data.fix_adda && fixP->fx_done)
3970 value <<= 2;
3971
3972 MODIFY_VALUE (newval, value, 2, 8, 15);
3973 if (value & 3)
3974 as_bad_where (fixP->fx_file, fixP->fx_line,
3975 _("immediate offset not 4-byte-aligned"));
3976 if (value > 0x1fffc)
3977 as_bad_where (fixP->fx_file, fixP->fx_line,
3978 _("immediate offset out of range"));
3979
3980 md_number_to_chars (buf, newval, 4);
3981 }
3982 if (fixP->fx_done && fixP->fx_r_type != BFD_RELOC_C6000_SBR_U15_W)
3983 abort ();
3984 break;
3985
3986 case BFD_RELOC_C6000_DSBT_INDEX:
3987 if (value != 0)
3988 as_bad_where (fixP->fx_file, fixP->fx_line,
3989 _("addend used with $DSBT_INDEX"));
3990 if (fixP->fx_done)
3991 abort ();
3992 break;
3993
3994 case BFD_RELOC_C6000_PCR_S21:
3995 if (fixP->fx_done || !seg->use_rela_p)
3996 {
3997 valueT newval = md_chars_to_number (buf, 4);
3998
3999 MODIFY_VALUE (newval, value, 2, 7, 21);
4000
4001 if (value & 3)
4002 as_bad_where (fixP->fx_file, fixP->fx_line,
4003 _("PC-relative offset not 4-byte-aligned"));
4004 if (value + 0x400000 > 0x3ffffc + 0x400000)
4005 as_bad_where (fixP->fx_file, fixP->fx_line,
4006 _("PC-relative offset out of range"));
4007
4008 md_number_to_chars (buf, newval, 4);
4009 }
4010 break;
4011
4012 case BFD_RELOC_C6000_PCR_S12:
4013 if (fixP->fx_done || !seg->use_rela_p)
4014 {
4015 valueT newval = md_chars_to_number (buf, 4);
4016
4017 MODIFY_VALUE (newval, value, 2, 16, 12);
4018
4019 if (value & 3)
4020 as_bad_where (fixP->fx_file, fixP->fx_line,
4021 _("PC-relative offset not 4-byte-aligned"));
4022 if (value + 0x2000 > 0x1ffc + 0x2000)
4023 as_bad_where (fixP->fx_file, fixP->fx_line,
4024 _("PC-relative offset out of range"));
4025
4026 md_number_to_chars (buf, newval, 4);
4027 }
4028 break;
4029
4030 case BFD_RELOC_C6000_PCR_S10:
4031 if (fixP->fx_done || !seg->use_rela_p)
4032 {
4033 valueT newval = md_chars_to_number (buf, 4);
4034
4035 MODIFY_VALUE (newval, value, 2, 13, 10);
4036
4037 if (value & 3)
4038 as_bad_where (fixP->fx_file, fixP->fx_line,
4039 _("PC-relative offset not 4-byte-aligned"));
4040 if (value + 0x800 > 0x7fc + 0x800)
4041 as_bad_where (fixP->fx_file, fixP->fx_line,
4042 _("PC-relative offset out of range"));
4043
4044 md_number_to_chars (buf, newval, 4);
4045 }
4046 break;
4047
4048 case BFD_RELOC_C6000_PCR_S7:
4049 if (fixP->fx_done || !seg->use_rela_p)
4050 {
4051 valueT newval = md_chars_to_number (buf, 4);
4052
4053 MODIFY_VALUE (newval, value, 2, 16, 7);
4054
4055 if (value & 3)
4056 as_bad_where (fixP->fx_file, fixP->fx_line,
4057 _("PC-relative offset not 4-byte-aligned"));
4058 if (value + 0x100 > 0xfc + 0x100)
4059 as_bad_where (fixP->fx_file, fixP->fx_line,
4060 _("PC-relative offset out of range"));
4061
4062 md_number_to_chars (buf, newval, 4);
4063 }
4064 break;
4065
4066 case BFD_RELOC_C6000_PREL31:
4067 /* Force output to the object file. */
4068 fixP->fx_done = 0;
4069 break;
4070
4071 default:
4072 abort ();
4073 }
4074 }
4075
4076 /* Convert a floating-point number to target (IEEE) format. */
4077
4078 const char *
4079 md_atof (int type, char *litP, int *sizeP)
4080 {
4081 return ieee_md_atof (type, litP, sizeP, target_big_endian);
4082 }
4083
4084 /* Adjust the frags in SECTION (see tic6x_end). */
4085
4086 static void
4087 tic6x_adjust_section (bfd *abfd ATTRIBUTE_UNUSED, segT section,
4088 void *dummy ATTRIBUTE_UNUSED)
4089 {
4090 segment_info_type *info;
4091 frchainS *frchp;
4092 fragS *fragp;
4093 bfd_boolean have_code = FALSE;
4094 bfd_boolean have_non_code = FALSE;
4095
4096 info = seg_info (section);
4097 if (info == NULL)
4098 return;
4099
4100 for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
4101 for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
4102 switch (fragp->fr_type)
4103 {
4104 case rs_machine_dependent:
4105 if (fragp->tc_frag_data.is_insns)
4106 have_code = TRUE;
4107 break;
4108
4109 case rs_dummy:
4110 case rs_fill:
4111 if (fragp->fr_fix > 0)
4112 have_non_code = TRUE;
4113 break;
4114
4115 default:
4116 have_non_code = TRUE;
4117 break;
4118 }
4119
4120 /* Process alignment requirements in a code-only section. */
4121 if (have_code && !have_non_code)
4122 {
4123 /* If we need to insert an odd number of instructions to meet an
4124 alignment requirement, there must have been an odd number of
4125 instructions since the last 8-byte-aligned execute packet
4126 boundary. So there must have been an execute packet with an
4127 odd number (and so a number fewer than 8) of instructions
4128 into which we can insert a NOP without breaking any previous
4129 alignments.
4130
4131 If then we need to insert a number 2 mod 4 of instructions,
4132 the number of instructions since the last 16-byte-aligned
4133 execute packet boundary must be 2 mod 4. So between that
4134 boundary and the following 8-byte-aligned boundary there must
4135 either be at least one execute packet with 2-mod-4
4136 instructions, or at least two with an odd number of
4137 instructions; again, greedily inserting NOPs as soon as
4138 possible suffices to meet the alignment requirement.
4139
4140 If then we need to insert 4 instructions, we look between the
4141 last 32-byte-aligned boundary and the following
4142 16-byte-aligned boundary. The sizes of the execute packets
4143 in this range total 4 instructions mod 8, so again there is
4144 room for greedy insertion of NOPs to meet the alignment
4145 requirement, and before any intermediate point with 8-byte
4146 (2-instruction) alignment requirement the sizes of execute
4147 packets (and so the room for NOPs) will total 2 instructions
4148 mod 4 so greedy insertion will not break such alignments.
4149
4150 So we can always meet these alignment requirements by
4151 inserting NOPs in parallel with existing execute packets, and
4152 by induction the approach described above inserts the minimum
4153 number of such NOPs. */
4154
4155 /* The number of NOPs we are currently looking to insert, if we
4156 have gone back to insert NOPs. */
4157 unsigned int want_insert = 0;
4158
4159 /* Out of that number, the number inserted so far in the current
4160 stage of the above algorithm. */
4161 unsigned int want_insert_done_so_far = 0;
4162
4163 /* The position mod 32 at the start of the current frag. */
4164 unsigned int pos = 0;
4165
4166 /* The locations in the frag chain of the most recent frags at
4167 the start of which there is the given alignment. */
4168 frchainS *frchp_last32, *frchp_last16, *frchp_last8;
4169 fragS *fragp_last32, *fragp_last16, *fragp_last8;
4170 unsigned int pos_last32, pos_last16, pos_last8;
4171
4172 frchp_last32 = frchp_last16 = frchp_last8 = info->frchainP;
4173 fragp_last32 = fragp_last16 = fragp_last8 = info->frchainP->frch_root;
4174 pos_last32 = pos_last16 = pos_last8 = 0;
4175
4176 for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
4177 for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
4178 look_at_frag:
4179 {
4180 bfd_boolean go_back = FALSE;
4181 frchainS *frchp_next;
4182 fragS *fragp_next;
4183
4184 if (fragp->fr_type != rs_machine_dependent)
4185 continue;
4186
4187 if (fragp->tc_frag_data.is_insns
4188 && pos + fragp->fr_fix > 32
4189 && !fragp->tc_frag_data.can_cross_fp_boundary)
4190 {
4191 /* As described above, we should always have met an
4192 alignment requirement by the time we come back to
4193 it. */
4194 if (want_insert)
4195 abort ();
4196
4197 if (pos & 3)
4198 abort ();
4199 want_insert = (32 - pos) >> 2;
4200 if (want_insert > 7)
4201 abort ();
4202 want_insert_done_so_far = 0;
4203 go_back = TRUE;
4204 }
4205
4206 if (!fragp->tc_frag_data.is_insns)
4207 {
4208 unsigned int would_insert_bytes;
4209
4210 if (!(pos & ((1 << fragp->fr_offset) - 1)))
4211 /* This alignment requirement is already met. */
4212 continue;
4213
4214 /* As described above, we should always have met an
4215 alignment requirement by the time we come back to
4216 it. */
4217 if (want_insert)
4218 abort ();
4219
4220 /* We may not be able to meet this requirement within
4221 the given number of characters. */
4222 would_insert_bytes
4223 = ((1 << fragp->fr_offset)
4224 - (pos & ((1 << fragp->fr_offset) - 1)));
4225
4226 if (fragp->fr_subtype != 0
4227 && would_insert_bytes > fragp->fr_subtype)
4228 continue;
4229
4230 /* An unmet alignment must be 8, 16 or 32 bytes;
4231 smaller ones must always be met within code-only
4232 sections and larger ones cause the section not to
4233 be code-only. */
4234 if (fragp->fr_offset != 3
4235 && fragp->fr_offset != 4
4236 && fragp->fr_offset != 5)
4237 abort ();
4238
4239 if (would_insert_bytes & 3)
4240 abort ();
4241 want_insert = would_insert_bytes >> 2;
4242 if (want_insert > 7)
4243 abort ();
4244 want_insert_done_so_far = 0;
4245 go_back = TRUE;
4246 }
4247 else if (want_insert && !go_back)
4248 {
4249 unsigned int num_insns = fragp->fr_fix >> 2;
4250 unsigned int max_poss_nops = 8 - num_insns;
4251
4252 if (max_poss_nops)
4253 {
4254 unsigned int cur_want_nops, max_want_nops, do_nops, i;
4255
4256 if (want_insert & 1)
4257 cur_want_nops = 1;
4258 else if (want_insert & 2)
4259 cur_want_nops = 2;
4260 else if (want_insert & 4)
4261 cur_want_nops = 4;
4262 else
4263 abort ();
4264
4265 max_want_nops = cur_want_nops - want_insert_done_so_far;
4266
4267 do_nops = (max_poss_nops < max_want_nops
4268 ? max_poss_nops
4269 : max_want_nops);
4270 for (i = 0; i < do_nops; i++)
4271 {
4272 md_number_to_chars (fragp->fr_literal + fragp->fr_fix,
4273 0, 4);
4274 if (target_big_endian)
4275 fragp->fr_literal[fragp->fr_fix - 1] |= 0x1;
4276 else
4277 fragp->fr_literal[fragp->fr_fix - 4] |= 0x1;
4278 fragp->fr_fix += 4;
4279 fragp->fr_var -= 4;
4280 }
4281 want_insert_done_so_far += do_nops;
4282 if (want_insert_done_so_far == cur_want_nops)
4283 {
4284 want_insert -= want_insert_done_so_far;
4285 want_insert_done_so_far = 0;
4286 if (want_insert)
4287 go_back = TRUE;
4288 }
4289 }
4290 }
4291 if (go_back)
4292 {
4293 if (want_insert & 1)
4294 {
4295 frchp = frchp_last8;
4296 fragp = fragp_last8;
4297 pos = pos_last8;
4298 }
4299 else if (want_insert & 2)
4300 {
4301 frchp = frchp_last8 = frchp_last16;
4302 fragp = fragp_last8 = fragp_last16;
4303 pos = pos_last8 = pos_last16;
4304 }
4305 else if (want_insert & 4)
4306 {
4307 frchp = frchp_last8 = frchp_last16 = frchp_last32;
4308 fragp = fragp_last8 = fragp_last16 = fragp_last32;
4309 pos = pos_last8 = pos_last16 = pos_last32;
4310 }
4311 else
4312 abort ();
4313
4314 goto look_at_frag;
4315 }
4316
4317 /* Update current position for moving past a code
4318 frag. */
4319 pos += fragp->fr_fix;
4320 pos &= 31;
4321 frchp_next = frchp;
4322 fragp_next = fragp->fr_next;
4323 if (fragp_next == NULL)
4324 {
4325 frchp_next = frchp->frch_next;
4326 if (frchp_next != NULL)
4327 fragp_next = frchp_next->frch_root;
4328 }
4329 if (!(pos & 7))
4330 {
4331 frchp_last8 = frchp_next;
4332 fragp_last8 = fragp_next;
4333 pos_last8 = pos;
4334 }
4335 if (!(pos & 15))
4336 {
4337 frchp_last16 = frchp_next;
4338 fragp_last16 = fragp_next;
4339 pos_last16 = pos;
4340 }
4341 if (!(pos & 31))
4342 {
4343 frchp_last32 = frchp_next;
4344 fragp_last32 = fragp_next;
4345 pos_last32 = pos;
4346 }
4347 }
4348 }
4349
4350 /* Now convert the machine-dependent frags to machine-independent
4351 ones. */
4352 for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
4353 for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
4354 {
4355 if (fragp->fr_type == rs_machine_dependent)
4356 {
4357 if (fragp->tc_frag_data.is_insns)
4358 frag_wane (fragp);
4359 else
4360 {
4361 fragp->fr_type = rs_align_code;
4362 fragp->fr_var = 1;
4363 *fragp->fr_literal = 0;
4364 }
4365 }
4366 }
4367 }
4368
4369 /* Initialize the machine-dependent parts of a frag. */
4370
4371 void
4372 tic6x_frag_init (fragS *fragp)
4373 {
4374 fragp->tc_frag_data.is_insns = FALSE;
4375 fragp->tc_frag_data.can_cross_fp_boundary = FALSE;
4376 }
4377
4378 /* Set an attribute if it has not already been set by the user. */
4379
4380 static void
4381 tic6x_set_attribute_int (int tag, int value)
4382 {
4383 if (tag < 1
4384 || tag >= NUM_KNOWN_OBJ_ATTRIBUTES)
4385 abort ();
4386 if (!tic6x_attributes_set_explicitly[tag])
4387 bfd_elf_add_proc_attr_int (stdoutput, tag, value);
4388 }
4389
4390 /* Set object attributes deduced from the input file and command line
4391 rather than given explicitly. */
4392 static void
4393 tic6x_set_attributes (void)
4394 {
4395 if (tic6x_arch_attribute == C6XABI_Tag_ISA_none)
4396 tic6x_arch_attribute = C6XABI_Tag_ISA_C674X;
4397
4398 tic6x_set_attribute_int (Tag_ISA, tic6x_arch_attribute);
4399 tic6x_set_attribute_int (Tag_ABI_DSBT, tic6x_dsbt);
4400 tic6x_set_attribute_int (Tag_ABI_PID, tic6x_pid);
4401 tic6x_set_attribute_int (Tag_ABI_PIC, tic6x_pic);
4402 }
4403
4404 /* Do machine-dependent manipulations of the frag chains after all
4405 input has been read and before the machine-independent sizing and
4406 relaxing. */
4407
4408 void
4409 tic6x_end (void)
4410 {
4411 /* Set object attributes at this point if not explicitly set. */
4412 tic6x_set_attributes ();
4413
4414 /* Meeting alignment requirements may require inserting NOPs in
4415 parallel in execute packets earlier in the segment. Future
4416 16-bit instruction generation involves whole-segment optimization
4417 to determine the best choice and ordering of 32-bit or 16-bit
4418 instructions. This doesn't fit will in the general relaxation
4419 framework, so handle alignment and 16-bit instruction generation
4420 here. */
4421 bfd_map_over_sections (stdoutput, tic6x_adjust_section, NULL);
4422 }
4423
4424 /* No machine-dependent frags at this stage; all converted in
4425 tic6x_end. */
4426
4427 void
4428 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
4429 fragS *fragp ATTRIBUTE_UNUSED)
4430 {
4431 abort ();
4432 }
4433
4434 /* No machine-dependent frags at this stage; all converted in
4435 tic6x_end. */
4436
4437 int
4438 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
4439 segT seg ATTRIBUTE_UNUSED)
4440 {
4441 abort ();
4442 }
4443
4444 /* Put a number into target byte order. */
4445
4446 void
4447 md_number_to_chars (char *buf, valueT val, int n)
4448 {
4449 if (target_big_endian)
4450 number_to_chars_bigendian (buf, val, n);
4451 else
4452 number_to_chars_littleendian (buf, val, n);
4453 }
4454
4455 /* Machine-dependent operand parsing not currently needed. */
4456
4457 void
4458 md_operand (expressionS *op ATTRIBUTE_UNUSED)
4459 {
4460 }
4461
4462 /* PC-relative operands are relative to the start of the fetch
4463 packet. */
4464
4465 long
4466 tic6x_pcrel_from_section (fixS *fixp, segT sec)
4467 {
4468 if (fixp->fx_addsy != NULL
4469 && (!S_IS_DEFINED (fixp->fx_addsy)
4470 || S_GET_SEGMENT (fixp->fx_addsy) != sec))
4471 return 0;
4472 return (fixp->fx_where + fixp->fx_frag->fr_address) & ~(long) 0x1f;
4473 }
4474
4475 /* Round up a section size to the appropriate boundary. */
4476
4477 valueT
4478 md_section_align (segT segment ATTRIBUTE_UNUSED,
4479 valueT size)
4480 {
4481 /* Round up section sizes to ensure that text sections consist of
4482 whole fetch packets. */
4483 int align = bfd_section_alignment (segment);
4484 return ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4485 }
4486
4487 /* No special undefined symbol handling needed for now. */
4488
4489 symbolS *
4490 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
4491 {
4492 return NULL;
4493 }
4494
4495 /* Translate internal representation of relocation info to BFD target
4496 format. */
4497
4498 arelent *
4499 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
4500 {
4501 arelent *reloc;
4502 asymbol *symbol;
4503 bfd_reloc_code_real_type r_type;
4504
4505 reloc = XNEW (arelent);
4506 reloc->sym_ptr_ptr = XNEW (asymbol *);
4507 symbol = symbol_get_bfdsym (fixp->fx_addsy);
4508 *reloc->sym_ptr_ptr = symbol;
4509 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
4510 reloc->addend = (tic6x_generate_rela ? fixp->fx_offset : 0);
4511 r_type = fixp->fx_r_type;
4512 reloc->howto = bfd_reloc_type_lookup (stdoutput, r_type);
4513
4514 if (reloc->howto == NULL)
4515 {
4516 as_bad_where (fixp->fx_file, fixp->fx_line,
4517 _("Cannot represent relocation type %s"),
4518 bfd_get_reloc_code_name (r_type));
4519 return NULL;
4520 }
4521
4522 /* Correct for adjustments bfd_install_relocation will make. */
4523 if (reloc->howto->pcrel_offset && reloc->howto->partial_inplace)
4524 {
4525 reloc->addend += reloc->address;
4526 if (!bfd_is_com_section (bfd_asymbol_section (symbol)))
4527 reloc->addend -= symbol->value;
4528 }
4529 if (r_type == BFD_RELOC_C6000_PCR_H16
4530 || r_type == BFD_RELOC_C6000_PCR_L16)
4531 {
4532 symbolS *t = fixp->tc_fix_data.fix_subsy;
4533 segT sub_symbol_segment;
4534
4535 resolve_symbol_value (t);
4536 sub_symbol_segment = S_GET_SEGMENT (t);
4537 if (sub_symbol_segment == undefined_section)
4538 as_bad_where (fixp->fx_file, fixp->fx_line,
4539 _("undefined symbol %s in PCR relocation"),
4540 S_GET_NAME (t));
4541 else
4542 {
4543 reloc->addend = reloc->address & ~0x1F;
4544 reloc->addend -= S_GET_VALUE (t);
4545 }
4546 }
4547 return reloc;
4548 }
4549
4550 /* Convert REGNAME to a DWARF-2 register number. */
4551
4552 int
4553 tic6x_regname_to_dw2regnum (char *regname)
4554 {
4555 bfd_boolean reg_ok;
4556 tic6x_register reg;
4557 char *rq = regname;
4558
4559 reg_ok = tic6x_parse_register (&rq, &reg);
4560
4561 if (!reg_ok)
4562 return -1;
4563
4564 switch (reg.side)
4565 {
4566 case 1: /* A regs. */
4567 if (reg.num < 16)
4568 return reg.num;
4569 else if (reg.num < 32)
4570 return (reg.num - 16) + 37;
4571 else
4572 return -1;
4573
4574 case 2: /* B regs. */
4575 if (reg.num < 16)
4576 return reg.num + 16;
4577 else if (reg.num < 32)
4578 return (reg.num - 16) + 53;
4579 else
4580 return -1;
4581
4582 default:
4583 return -1;
4584 }
4585 }
4586
4587 /* Initialize the DWARF-2 unwind information for this procedure. */
4588
4589 void
4590 tic6x_frame_initial_instructions (void)
4591 {
4592 /* CFA is initial stack pointer (B15). */
4593 cfi_add_CFA_def_cfa (31, 0);
4594 }
4595
4596 /* Start an exception table entry. If idx is nonzero this is an index table
4597 entry. */
4598
4599 static void
4600 tic6x_start_unwind_section (const segT text_seg, int idx)
4601 {
4602 tic6x_unwind_info *unwind = tic6x_get_unwind ();
4603 const char * text_name;
4604 const char * prefix;
4605 const char * prefix_once;
4606 struct elf_section_match match;
4607 size_t prefix_len;
4608 size_t text_len;
4609 char * sec_name;
4610 size_t sec_name_len;
4611 int type;
4612 int flags;
4613 int linkonce;
4614
4615 if (idx)
4616 {
4617 prefix = ELF_STRING_C6000_unwind;
4618 prefix_once = ELF_STRING_C6000_unwind_once;
4619 type = SHT_C6000_UNWIND;
4620 }
4621 else
4622 {
4623 prefix = ELF_STRING_C6000_unwind_info;
4624 prefix_once = ELF_STRING_C6000_unwind_info_once;
4625 type = SHT_PROGBITS;
4626 }
4627
4628 text_name = segment_name (text_seg);
4629 if (streq (text_name, ".text"))
4630 text_name = "";
4631
4632 if (strncmp (text_name, ".gnu.linkonce.t.",
4633 strlen (".gnu.linkonce.t.")) == 0)
4634 {
4635 prefix = prefix_once;
4636 text_name += strlen (".gnu.linkonce.t.");
4637 }
4638
4639 prefix_len = strlen (prefix);
4640 text_len = strlen (text_name);
4641 sec_name_len = prefix_len + text_len;
4642 sec_name = XNEWVEC (char, sec_name_len + 1);
4643 memcpy (sec_name, prefix, prefix_len);
4644 memcpy (sec_name + prefix_len, text_name, text_len);
4645 sec_name[prefix_len + text_len] = '\0';
4646
4647 flags = SHF_ALLOC;
4648 linkonce = 0;
4649 memset (&match, 0, sizeof (match));
4650
4651 /* Handle COMDAT group. */
4652 if (prefix != prefix_once && (text_seg->flags & SEC_LINK_ONCE) != 0)
4653 {
4654 match.group_name = elf_group_name (text_seg);
4655 if (match.group_name == NULL)
4656 {
4657 as_bad (_("group section `%s' has no group signature"),
4658 segment_name (text_seg));
4659 ignore_rest_of_line ();
4660 return;
4661 }
4662 flags |= SHF_GROUP;
4663 linkonce = 1;
4664 }
4665
4666 obj_elf_change_section (sec_name, type, flags, 0, &match,
4667 linkonce, 0);
4668
4669 /* Set the section link for index tables. */
4670 if (idx)
4671 elf_linked_to_section (now_seg) = text_seg;
4672
4673 seg_info (now_seg)->tc_segment_info_data.text_unwind = unwind;
4674 }
4675
4676
4677 static const int
4678 tic6x_unwind_frame_regs[TIC6X_NUM_UNWIND_REGS] =
4679 /* A15 B15 B14 B13 B12 B11 B10 B3 A14 A13 A12 A11 A10. */
4680 { 15, 31, 30, 29, 28, 27, 26, 19, 14, 13, 12, 11, 10 };
4681
4682 /* Register save offsets for __c6xabi_push_rts. */
4683 static const int
4684 tic6x_pop_rts_offset_little[TIC6X_NUM_UNWIND_REGS] =
4685 /* A15 B15 B14 B13 B12 B11 B10 B3 A14 A13 A12 A11 A10. */
4686 { -1, 1, 0, -3, -4, -7, -8,-11, -2, -5, -6, -9,-10};
4687
4688 static const int
4689 tic6x_pop_rts_offset_big[TIC6X_NUM_UNWIND_REGS] =
4690 /* A15 B15 B14 B13 B12 B11 B10 B3 A14 A13 A12 A11 A10. */
4691 { -2, 1, 0, -4, -3, -8, -7,-12, -1, -6, -5,-10, -9};
4692
4693 /* Map from dwarf register number to unwind frame register number. */
4694 static int
4695 tic6x_unwind_reg_from_dwarf (int dwarf)
4696 {
4697 int reg;
4698
4699 for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
4700 {
4701 if (tic6x_unwind_frame_regs[reg] == dwarf)
4702 return reg;
4703 }
4704
4705 return -1;
4706 }
4707
4708 /* Unwinding bytecode definitions. */
4709 #define UNWIND_OP_ADD_SP 0x00
4710 #define UNWIND_OP_ADD_SP2 0xd2
4711 #define UNWIND_OP2_POP 0x8000
4712 #define UNWIND_OP2_POP_COMPACT 0xa000
4713 #define UNWIND_OP_POP_REG 0xc0
4714 #define UNWIND_OP_MV_FP 0xd0
4715 #define UNWIND_OP_POP_RTS 0xd1
4716 #define UNWIND_OP_RET 0xe0
4717
4718 /* Maximum stack adjustment for __c6xabi_unwind_cpp_pr3/4 */
4719 #define MAX_COMPACT_SP_OFFSET (0x7f << 3)
4720
4721 static void
4722 tic6x_flush_unwind_word (valueT data)
4723 {
4724 tic6x_unwind_info *unwind = tic6x_get_unwind ();
4725 char *ptr;
4726
4727 /* Create EXTAB entry if it does not exist. */
4728 if (unwind->table_entry == NULL)
4729 {
4730 tic6x_start_unwind_section (unwind->saved_seg, 0);
4731 frag_align (2, 0, 0);
4732 record_alignment (now_seg, 2);
4733 unwind->table_entry = expr_build_dot ();
4734 ptr = frag_more (4);
4735 unwind->frag_start = ptr;
4736 }
4737 else
4738 {
4739 /* Append additional word of data. */
4740 ptr = frag_more (4);
4741 }
4742
4743 md_number_to_chars (ptr, data, 4);
4744 }
4745
4746 /* Add a single byte of unwinding data. */
4747
4748 static void
4749 tic6x_unwind_byte (int byte)
4750 {
4751 tic6x_unwind_info *unwind = tic6x_get_unwind ();
4752
4753 unwind->data_bytes++;
4754 /* Only flush the first word after we know multiple words are required. */
4755 if (unwind->data_bytes == 5)
4756 {
4757 if (unwind->personality_index == -1)
4758 {
4759 /* At this point we know we are too big for pr0. */
4760 unwind->personality_index = 1;
4761 tic6x_flush_unwind_word (0x81000000 | ((unwind->data >> 8) & 0xffff));
4762 unwind->data = ((unwind->data & 0xff) << 8) | byte;
4763 unwind->data_bytes++;
4764 }
4765 else
4766 {
4767 tic6x_flush_unwind_word (unwind->data);
4768 unwind->data = byte;
4769 }
4770 }
4771 else
4772 {
4773 unwind->data = (unwind->data << 8) | byte;
4774 if ((unwind->data_bytes & 3) == 0 && unwind->data_bytes > 4)
4775 {
4776 tic6x_flush_unwind_word (unwind->data);
4777 unwind->data = 0;
4778 }
4779 }
4780 }
4781
4782 /* Add a two-byte unwinding opcode. */
4783 static void
4784 tic6x_unwind_2byte (int bytes)
4785 {
4786 tic6x_unwind_byte (bytes >> 8);
4787 tic6x_unwind_byte (bytes & 0xff);
4788 }
4789
4790 static void
4791 tic6x_unwind_uleb (offsetT offset)
4792 {
4793 while (offset > 0x7f)
4794 {
4795 tic6x_unwind_byte ((offset & 0x7f) | 0x80);
4796 offset >>= 7;
4797 }
4798 tic6x_unwind_byte (offset);
4799 }
4800
4801 void
4802 tic6x_cfi_startproc (void)
4803 {
4804 tic6x_unwind_info *unwind = tic6x_get_unwind ();
4805
4806 unwind->personality_index = -1;
4807 unwind->personality_routine = NULL;
4808 if (unwind->table_entry)
4809 as_bad (_("missing .endp before .cfi_startproc"));
4810
4811 unwind->table_entry = NULL;
4812 unwind->data_bytes = -1;
4813 }
4814
4815 static void
4816 tic6x_output_exidx_entry (void)
4817 {
4818 char *ptr;
4819 long where;
4820 unsigned int marked_pr_dependency;
4821 segT old_seg;
4822 subsegT old_subseg;
4823 tic6x_unwind_info *unwind = tic6x_get_unwind ();
4824
4825 old_seg = now_seg;
4826 old_subseg = now_subseg;
4827
4828 /* Add index table entry. This is two words. */
4829 tic6x_start_unwind_section (unwind->saved_seg, 1);
4830 frag_align (2, 0, 0);
4831 record_alignment (now_seg, 2);
4832
4833 ptr = frag_more (8);
4834 memset (ptr, 0, 8);
4835 where = frag_now_fix () - 8;
4836
4837 /* Self relative offset of the function start. */
4838 fix_new (frag_now, where, 4, unwind->function_start, 0, 1,
4839 BFD_RELOC_C6000_PREL31);
4840
4841 /* Indicate dependency on ABI-defined personality routines to the
4842 linker, if it hasn't been done already. */
4843 marked_pr_dependency
4844 = seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency;
4845 if (unwind->personality_index >= 0 && unwind->personality_index < 5
4846 && !(marked_pr_dependency & (1 << unwind->personality_index)))
4847 {
4848 static const char *const name[] =
4849 {
4850 "__c6xabi_unwind_cpp_pr0",
4851 "__c6xabi_unwind_cpp_pr1",
4852 "__c6xabi_unwind_cpp_pr2",
4853 "__c6xabi_unwind_cpp_pr3",
4854 "__c6xabi_unwind_cpp_pr4"
4855 };
4856 symbolS *pr = symbol_find_or_make (name[unwind->personality_index]);
4857 fix_new (frag_now, where, 0, pr, 0, 1, BFD_RELOC_NONE);
4858 seg_info (now_seg)->tc_segment_info_data.marked_pr_dependency
4859 |= 1 << unwind->personality_index;
4860 }
4861
4862 if (unwind->table_entry)
4863 {
4864 /* Self relative offset of the table entry. */
4865 fix_new (frag_now, where + 4, 4, unwind->table_entry, 0, 1,
4866 BFD_RELOC_C6000_PREL31);
4867 }
4868 else
4869 {
4870 /* Inline exception table entry. */
4871 md_number_to_chars (ptr + 4, unwind->data, 4);
4872 }
4873
4874 /* Restore the original section. */
4875 subseg_set (old_seg, old_subseg);
4876 }
4877
4878 static void
4879 tic6x_output_unwinding (bfd_boolean need_extab)
4880 {
4881 tic6x_unwind_info *unwind = tic6x_get_unwind ();
4882 unsigned safe_mask = unwind->safe_mask;
4883 unsigned compact_mask = unwind->compact_mask;
4884 unsigned reg_saved_mask = unwind->reg_saved_mask;
4885 offsetT cfa_offset = unwind->cfa_offset;
4886 long where;
4887 int reg;
4888
4889 if (unwind->personality_index == -2)
4890 {
4891 /* Function can not be unwound. */
4892 unwind->data = 1;
4893 tic6x_output_exidx_entry ();
4894 return;
4895 }
4896
4897 if (unwind->personality_index == -1 && unwind->personality_routine == NULL)
4898 {
4899 /* Auto-select a personality routine if none specified. */
4900 if (reg_saved_mask || cfa_offset >= MAX_COMPACT_SP_OFFSET)
4901 unwind->personality_index = -1;
4902 else if (safe_mask)
4903 unwind->personality_index = 3;
4904 else
4905 unwind->personality_index = 4;
4906 }
4907
4908 /* Calculate unwinding opcodes, and emit to EXTAB if necessary. */
4909 unwind->table_entry = NULL;
4910 if (unwind->personality_index == 3 || unwind->personality_index == 4)
4911 {
4912 if (cfa_offset >= MAX_COMPACT_SP_OFFSET)
4913 {
4914 as_bad (_("stack pointer offset too large for personality routine"));
4915 return;
4916 }
4917 if (reg_saved_mask
4918 || (unwind->personality_index == 3 && compact_mask != 0)
4919 || (unwind->personality_index == 4 && safe_mask != 0))
4920 {
4921 as_bad (_("stack frame layout does not match personality routine"));
4922 return;
4923 }
4924
4925 unwind->data = (1u << 31) | (unwind->personality_index << 24);
4926 if (unwind->cfa_reg == 15)
4927 unwind->data |= 0x7f << 17;
4928 else
4929 unwind->data |= cfa_offset << (17 - 3);
4930
4931 if (unwind->personality_index == 3)
4932 unwind->data |= safe_mask << 4;
4933 else
4934 unwind->data |= compact_mask << 4;
4935 unwind->data |= unwind->return_reg;
4936 unwind->data_bytes = 4;
4937 }
4938 else
4939 {
4940 if (unwind->personality_routine)
4941 {
4942 unwind->data = 0;
4943 unwind->data_bytes = 5;
4944 tic6x_flush_unwind_word (0);
4945 /* First word is personality routine. */
4946 where = frag_now_fix () - 4;
4947 fix_new (frag_now, where, 4, unwind->personality_routine, 0, 1,
4948 BFD_RELOC_C6000_PREL31);
4949 }
4950 else if (unwind->personality_index > 0)
4951 {
4952 unwind->data = 0x8000 | (unwind->personality_index << 8);
4953 unwind->data_bytes = 2;
4954 }
4955 else /* pr0 or undecided */
4956 {
4957 unwind->data = 0x80;
4958 unwind->data_bytes = 1;
4959 }
4960
4961 if (unwind->return_reg != UNWIND_B3)
4962 {
4963 tic6x_unwind_byte (UNWIND_OP_RET | unwind->return_reg);
4964 }
4965
4966 if (unwind->cfa_reg == 15)
4967 {
4968 tic6x_unwind_byte (UNWIND_OP_MV_FP);
4969 }
4970 else if (cfa_offset != 0)
4971 {
4972 cfa_offset >>= 3;
4973 if (cfa_offset > 0x80)
4974 {
4975 tic6x_unwind_byte (UNWIND_OP_ADD_SP2);
4976 tic6x_unwind_uleb (cfa_offset - 0x81);
4977 }
4978 else if (cfa_offset > 0x40)
4979 {
4980 tic6x_unwind_byte (UNWIND_OP_ADD_SP | 0x3f);
4981 tic6x_unwind_byte (UNWIND_OP_ADD_SP | (cfa_offset - 0x40));
4982 }
4983 else
4984 {
4985 tic6x_unwind_byte (UNWIND_OP_ADD_SP | (cfa_offset - 1));
4986 }
4987 }
4988
4989 if (safe_mask)
4990 tic6x_unwind_2byte (UNWIND_OP2_POP | unwind->safe_mask);
4991 else if (unwind->pop_rts)
4992 tic6x_unwind_byte (UNWIND_OP_POP_RTS);
4993 else if (compact_mask)
4994 tic6x_unwind_2byte (UNWIND_OP2_POP_COMPACT | unwind->compact_mask);
4995 else if (reg_saved_mask)
4996 {
4997 offsetT cur_offset;
4998 int val;
4999 int last_val;
5000
5001 tic6x_unwind_byte (UNWIND_OP_POP_REG | unwind->saved_reg_count);
5002 last_val = 0;
5003 for (cur_offset = 0; unwind->saved_reg_count > 0; cur_offset -= 4)
5004 {
5005 val = 0xf;
5006 for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
5007 {
5008 if (!unwind->reg_saved[reg])
5009 continue;
5010
5011 if (unwind->reg_offset[reg] == cur_offset)
5012 {
5013 unwind->saved_reg_count--;
5014 val = reg;
5015 break;
5016 }
5017 }
5018 if ((cur_offset & 4) == 4)
5019 tic6x_unwind_byte ((last_val << 4) | val);
5020 else
5021 last_val = val;
5022 }
5023 if ((cur_offset & 4) == 4)
5024 tic6x_unwind_byte ((last_val << 4) | 0xf);
5025 }
5026
5027 /* Pad with RETURN opcodes. */
5028 while ((unwind->data_bytes & 3) != 0)
5029 tic6x_unwind_byte (UNWIND_OP_RET | UNWIND_B3);
5030
5031 if (unwind->personality_index == -1 && unwind->personality_routine == NULL)
5032 unwind->personality_index = 0;
5033 }
5034
5035 /* Force creation of an EXTAB entry if an LSDA is required. */
5036 if (need_extab && !unwind->table_entry)
5037 {
5038 if (unwind->data_bytes != 4)
5039 abort ();
5040
5041 tic6x_flush_unwind_word (unwind->data);
5042 }
5043 else if (unwind->table_entry && !need_extab)
5044 {
5045 /* Add an empty descriptor if there is no user-specified data. */
5046 char *ptr = frag_more (4);
5047 md_number_to_chars (ptr, 0, 4);
5048 }
5049
5050 /* Fill in length of unwinding bytecode. */
5051 if (unwind->table_entry)
5052 {
5053 valueT tmp;
5054 if (unwind->data_bytes > 0x400)
5055 as_bad (_("too many unwinding instructions"));
5056
5057 if (unwind->personality_index == -1)
5058 {
5059 tmp = md_chars_to_number (unwind->frag_start + 4, 4);
5060 tmp |= (valueT) ((unwind->data_bytes - 8) >> 2) << 24;
5061 md_number_to_chars (unwind->frag_start + 4, tmp, 4);
5062 }
5063 else if (unwind->personality_index == 1 || unwind->personality_index == 2)
5064 {
5065 tmp = md_chars_to_number (unwind->frag_start, 4);
5066 tmp |= ((unwind->data_bytes - 4) >> 2) << 16;
5067 md_number_to_chars (unwind->frag_start, tmp, 4);
5068 }
5069 }
5070 tic6x_output_exidx_entry ();
5071 }
5072
5073 /* FIXME: This will get horribly confused if cfi directives are emitted for
5074 function epilogue. */
5075 void
5076 tic6x_cfi_endproc (struct fde_entry *fde)
5077 {
5078 tic6x_unwind_info *unwind = tic6x_get_unwind ();
5079 struct cfi_insn_data *insn;
5080 int reg;
5081 unsigned safe_mask = 0;
5082 unsigned compact_mask = 0;
5083 unsigned reg_saved_mask = 0;
5084 offsetT cfa_offset = 0;
5085 offsetT save_offset = 0;
5086
5087 unwind->cfa_reg = 31;
5088 unwind->return_reg = UNWIND_B3;
5089 unwind->saved_reg_count = 0;
5090 unwind->pop_rts = FALSE;
5091
5092 unwind->saved_seg = now_seg;
5093 unwind->saved_subseg = now_subseg;
5094
5095 for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
5096 unwind->reg_saved[reg] = FALSE;
5097
5098 /* Scan FDE instructions to build up stack frame layout. */
5099 for (insn = fde->data; insn; insn = insn->next)
5100 {
5101 switch (insn->insn)
5102 {
5103 case DW_CFA_advance_loc:
5104 break;
5105
5106 case DW_CFA_def_cfa:
5107 unwind->cfa_reg = insn->u.ri.reg;
5108 cfa_offset = insn->u.ri.offset;
5109 break;
5110
5111 case DW_CFA_def_cfa_register:
5112 unwind->cfa_reg = insn->u.r;
5113 break;
5114
5115 case DW_CFA_def_cfa_offset:
5116 cfa_offset = insn->u.i;
5117 break;
5118
5119 case DW_CFA_undefined:
5120 case DW_CFA_same_value:
5121 reg = tic6x_unwind_reg_from_dwarf (insn->u.r);
5122 if (reg >= 0)
5123 unwind->reg_saved[reg] = FALSE;
5124 break;
5125
5126 case DW_CFA_offset:
5127 reg = tic6x_unwind_reg_from_dwarf (insn->u.ri.reg);
5128 if (reg < 0)
5129 {
5130 as_bad (_("unable to generate unwinding opcode for reg %d"),
5131 insn->u.ri.reg);
5132 return;
5133 }
5134 unwind->reg_saved[reg] = TRUE;
5135 unwind->reg_offset[reg] = insn->u.ri.offset;
5136 if (insn->u.ri.reg == UNWIND_B3)
5137 unwind->return_reg = UNWIND_B3;
5138 break;
5139
5140 case DW_CFA_register:
5141 if (insn->u.rr.reg1 != 19)
5142 {
5143 as_bad (_("unable to generate unwinding opcode for reg %d"),
5144 insn->u.rr.reg1);
5145 return;
5146 }
5147
5148 reg = tic6x_unwind_reg_from_dwarf (insn->u.rr.reg2);
5149 if (reg < 0)
5150 {
5151 as_bad (_("unable to generate unwinding opcode for reg %d"),
5152 insn->u.rr.reg2);
5153 return;
5154 }
5155
5156 unwind->return_reg = reg;
5157 unwind->reg_saved[UNWIND_B3] = FALSE;
5158 if (unwind->reg_saved[reg])
5159 {
5160 as_bad (_("unable to restore return address from "
5161 "previously restored reg"));
5162 return;
5163 }
5164 break;
5165
5166 case DW_CFA_restore:
5167 case DW_CFA_remember_state:
5168 case DW_CFA_restore_state:
5169 case DW_CFA_GNU_window_save:
5170 case CFI_escape:
5171 case CFI_val_encoded_addr:
5172 as_bad (_("unhandled CFA insn for unwinding (%d)"), insn->insn);
5173 break;
5174
5175 default:
5176 abort ();
5177 }
5178 }
5179
5180 if (unwind->cfa_reg != 15 && unwind->cfa_reg != 31)
5181 {
5182 as_bad (_("unable to generate unwinding opcode for frame pointer reg %d"),
5183 unwind->cfa_reg);
5184 return;
5185 }
5186
5187 if (unwind->cfa_reg == 15)
5188 {
5189 if (cfa_offset != 0)
5190 {
5191 as_bad (_("unable to generate unwinding opcode for "
5192 "frame pointer offset"));
5193 return;
5194 }
5195 }
5196 else
5197 {
5198 if ((cfa_offset & 7) != 0)
5199 {
5200 as_bad (_("unwound stack pointer not doubleword aligned"));
5201 return;
5202 }
5203 }
5204
5205 for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
5206 {
5207 if (unwind->reg_saved[reg])
5208 reg_saved_mask |= 1 << (TIC6X_NUM_UNWIND_REGS - (reg + 1));
5209 }
5210
5211 /* Check for standard "safe debug" frame layout */
5212 if (reg_saved_mask)
5213 {
5214 save_offset = 0;
5215 for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
5216 {
5217 if (!unwind->reg_saved[reg])
5218 continue;
5219
5220 if (target_big_endian
5221 && reg < TIC6X_NUM_UNWIND_REGS - 1
5222 && unwind->reg_saved[reg + 1]
5223 && tic6x_unwind_frame_regs[reg]
5224 == tic6x_unwind_frame_regs[reg + 1] + 1
5225 && (tic6x_unwind_frame_regs[reg] & 1) == 1
5226 && (save_offset & 4) == 4)
5227 {
5228 /* Swapped pair */
5229 if (save_offset != unwind->reg_offset[reg + 1]
5230 || save_offset - 4 != unwind->reg_offset[reg])
5231 break;
5232 save_offset -= 8;
5233 reg++;
5234 }
5235 else
5236 {
5237 if (save_offset != unwind->reg_offset[reg])
5238 break;
5239 save_offset -= 4;
5240 }
5241 }
5242 if (reg == TIC6X_NUM_UNWIND_REGS)
5243 {
5244 safe_mask = reg_saved_mask;
5245 reg_saved_mask = 0;
5246 }
5247 }
5248
5249 /* Check for compact frame layout. */
5250 if (reg_saved_mask)
5251 {
5252 save_offset = 0;
5253 for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
5254 {
5255 int reg2;
5256
5257 if (!unwind->reg_saved[reg])
5258 continue;
5259
5260 if (reg < TIC6X_NUM_UNWIND_REGS - 1)
5261 {
5262 reg2 = reg + 1;
5263
5264 if (!unwind->reg_saved[reg2]
5265 || tic6x_unwind_frame_regs[reg]
5266 != tic6x_unwind_frame_regs[reg2] + 1
5267 || (tic6x_unwind_frame_regs[reg2] & 1) != 0
5268 || save_offset == 0)
5269 reg2 = -1;
5270 }
5271 else
5272 reg2 = -1;
5273
5274 if (reg2 >= 0)
5275 {
5276 int high_offset;
5277 if (target_big_endian)
5278 high_offset = 4; /* lower address = positive stack offset. */
5279 else
5280 high_offset = 0;
5281
5282 if (save_offset + 4 - high_offset != unwind->reg_offset[reg]
5283 || save_offset + high_offset != unwind->reg_offset[reg2])
5284 {
5285 break;
5286 }
5287 reg++;
5288 }
5289 else
5290 {
5291 if (save_offset != unwind->reg_offset[reg])
5292 break;
5293 }
5294 save_offset -= 8;
5295 }
5296
5297 if (reg == TIC6X_NUM_UNWIND_REGS)
5298 {
5299 compact_mask = reg_saved_mask;
5300 reg_saved_mask = 0;
5301 }
5302 }
5303
5304 /* Check for __c6xabi_pop_rts format */
5305 if (reg_saved_mask == 0x17ff)
5306 {
5307 const int *pop_rts_offset = target_big_endian
5308 ? tic6x_pop_rts_offset_big
5309 : tic6x_pop_rts_offset_little;
5310
5311 save_offset = 0;
5312 for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
5313 {
5314 if (reg == UNWIND_B15)
5315 continue;
5316
5317 if (unwind->reg_offset[reg] != pop_rts_offset[reg] * 4)
5318 break;
5319 }
5320
5321 if (reg == TIC6X_NUM_UNWIND_REGS)
5322 {
5323 unwind->pop_rts = TRUE;
5324 reg_saved_mask = 0;
5325 }
5326 }
5327 /* If all else fails then describe the frame manually. */
5328 if (reg_saved_mask)
5329 {
5330 save_offset = 0;
5331
5332 for (reg = 0; reg < TIC6X_NUM_UNWIND_REGS; reg++)
5333 {
5334 if (!unwind->reg_saved[reg])
5335 continue;
5336
5337 unwind->saved_reg_count++;
5338 /* Encoding uses 4 bits per word, so size of unwinding opcode data
5339 limits the save area size. The exact cap will be figured out
5340 later due to overflow, the 0x800 here is just a quick sanity
5341 check to weed out obviously excessive offsets. */
5342 if (unwind->reg_offset[reg] > 0 || unwind->reg_offset[reg] < -0x800
5343 || (unwind->reg_offset[reg] & 3) != 0)
5344 {
5345 as_bad (_("stack frame layout too complex for unwinder"));
5346 return;
5347 }
5348
5349 if (unwind->reg_offset[reg] < save_offset)
5350 save_offset = unwind->reg_offset[reg] - 4;
5351 }
5352 }
5353
5354 /* Align to 8-byte boundary (stack grows towards negative offsets). */
5355 save_offset &= ~7;
5356
5357 if (unwind->cfa_reg == 31 && !reg_saved_mask)
5358 {
5359 cfa_offset += save_offset;
5360 if (cfa_offset < 0)
5361 {
5362 as_bad (_("unwound frame has negative size"));
5363 return;
5364 }
5365 }
5366
5367 unwind->safe_mask = safe_mask;
5368 unwind->compact_mask = compact_mask;
5369 unwind->reg_saved_mask = reg_saved_mask;
5370 unwind->cfa_offset = cfa_offset;
5371 unwind->function_start = fde->start_address;
5372 }