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