Port gas/config/* to str_htab.
[deliverable/binutils-gdb.git] / gas / config / tc-tic54x.c
1 /* tc-tic54x.c -- Assembly code for the Texas Instruments TMS320C54X
2 Copyright (C) 1999-2020 Free Software Foundation, Inc.
3 Contributed by Timothy Wall (twall@cygnus.com)
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
22 /* Texas Instruments TMS320C54X machine specific gas.
23 Written by Timothy Wall (twall@alum.mit.edu).
24
25 Valuable things to do:
26 Pipeline conflict warnings
27 We encode/decode "ld #_label, dp" differently in relocatable files
28 This means we're not compatible with TI output containing those
29 expressions. We store the upper nine bits; TI stores the lower nine
30 bits. How they recover the original upper nine bits is beyond me.
31
32 Tests to add to expect testsuite:
33 '=' and '==' with .if, .elseif, and .break
34
35 Incompatibilities (mostly trivial):
36 We don't allow '''
37 We fill text section with zeroes instead of "nop"s
38 We don't convert '' or "" to a single instance
39 We don't convert '' to '\0'
40 We don't allow strings with .byte/.half/.short/.long
41 Probably details of the subsym stuff are different
42 TI sets labels to be data type 4 (T_INT); GAS uses T_NULL.
43
44 COFF1 limits section names to 8 characters.
45 Some of the default behavior changed from COFF1 to COFF2. */
46
47 #include "as.h"
48 #include <limits.h>
49 #include "safe-ctype.h"
50 #include "sb.h"
51 #include "macro.h"
52 #include "subsegs.h"
53 #include "opcode/tic54x.h"
54 #include "obj-coff.h"
55 #include <math.h>
56
57
58 static struct stag
59 {
60 symbolS *sym; /* Symbol for this stag; value is offset. */
61 const char *name; /* Shortcut to symbol name. */
62 bfd_vma size; /* Size of struct/union. */
63 int current_bitfield_offset; /* Temporary for tracking fields. */
64 int is_union;
65 struct stag_field /* List of fields. */
66 {
67 const char *name;
68 bfd_vma offset; /* Of start of this field. */
69 int bitfield_offset; /* Of start of this field. */
70 struct stag *stag; /* If field is struct/union. */
71 struct stag_field *next;
72 } *field;
73 /* For nesting; used only in stag construction. */
74 struct stag *inner; /* Enclosed .struct. */
75 struct stag *outer; /* Enclosing .struct. */
76 } *current_stag = NULL;
77
78 #define MAX_LINE 256 /* Lines longer than this are truncated by TI's asm. */
79
80 typedef struct _tic54x_insn
81 {
82 const insn_template *tm; /* Opcode template. */
83
84 char mnemonic[MAX_LINE]; /* Opcode name/mnemonic. */
85 char parmnemonic[MAX_LINE]; /* 2nd mnemonic of parallel insn. */
86
87 int opcount;
88 struct opstruct
89 {
90 char buf[MAX_LINE];
91 enum optype type;
92 expressionS exp;
93 } operands[MAX_OPERANDS];
94
95 int paropcount;
96 struct opstruct paroperands[MAX_OPERANDS];
97
98 int is_lkaddr;
99 int lkoperand;
100 int words; /* Size of insn in 16-bit words. */
101 int using_default_dst; /* Do we need to explicitly set an
102 omitted OP_DST operand? */
103 struct
104 {
105 unsigned short word; /* Final encoded opcode data. */
106 int unresolved;
107 int r_nchars; /* Relocation size. */
108 bfd_reloc_code_real_type r_type; /* Relocation type. */
109 expressionS addr_expr; /* Storage for unresolved expressions. */
110 } opcode[3];
111 } tic54x_insn;
112
113 enum cpu_version
114 {
115 VNONE = 0, V541 = 1, V542 = 2, V543 = 3, V545 = 5, V548 = 8, V549 = 9,
116 V545LP = 15, V546LP = 16
117 };
118
119 enum address_mode
120 {
121 c_mode, /* 16-bit addresses. */
122 far_mode /* >16-bit addresses. */
123 };
124
125 static segT stag_saved_seg;
126 static subsegT stag_saved_subseg;
127
128 const char comment_chars[] = ";";
129 const char line_comment_chars[] = ";*#"; /* At column zero only. */
130 const char line_separator_chars[] = ""; /* Not permitted. */
131
132 int emitting_long = 0;
133
134 /* Characters which indicate that this is a floating point constant. */
135 const char FLT_CHARS[] = "fF";
136
137 /* Characters that can be used to separate mantissa from exp in FP
138 nums. */
139 const char EXP_CHARS[] = "eE";
140
141 const char *md_shortopts = "";
142
143 #define OPTION_ADDRESS_MODE (OPTION_MD_BASE)
144 #define OPTION_CPU_VERSION (OPTION_ADDRESS_MODE + 1)
145 #define OPTION_COFF_VERSION (OPTION_CPU_VERSION + 1)
146 #define OPTION_STDERR_TO_FILE (OPTION_COFF_VERSION + 1)
147
148 struct option md_longopts[] =
149 {
150 { "mfar-mode", no_argument, NULL, OPTION_ADDRESS_MODE },
151 { "mf", no_argument, NULL, OPTION_ADDRESS_MODE },
152 { "mcpu", required_argument, NULL, OPTION_CPU_VERSION },
153 { "merrors-to-file", required_argument, NULL, OPTION_STDERR_TO_FILE },
154 { "me", required_argument, NULL, OPTION_STDERR_TO_FILE },
155 { NULL, no_argument, NULL, 0},
156 };
157
158 size_t md_longopts_size = sizeof (md_longopts);
159
160 static int assembly_begun = 0;
161 /* Addressing mode is not entirely implemented; the latest rev of the Other
162 assembler doesn't seem to make any distinction whatsoever; all relocations
163 are stored as extended relocations. Older versions used REL16 vs RELEXT16,
164 but now it seems all relocations are RELEXT16. We use all RELEXT16.
165
166 The cpu version is kind of a waste of time as well. There is one
167 instruction (RND) for LP devices only, and several for devices with
168 extended addressing only. We include it for compatibility. */
169 static enum address_mode amode = c_mode;
170 static enum cpu_version cpu = VNONE;
171
172 /* Include string substitutions in listing? */
173 static int listing_sslist = 0;
174
175 /* Did we do subsym substitutions on the line? */
176 static int substitution_line = 0;
177
178 /* Last label seen. */
179 static symbolS *last_label_seen = NULL;
180
181 /* This ensures that all new labels are unique. */
182 static int local_label_id;
183
184 static htab_t subsym_recurse_hash; /* Prevent infinite recurse. */
185 static htab_t math_hash; /* Built-in math functions. */
186 /* Allow maximum levels of macro nesting; level 0 is the main substitution
187 symbol table. The other assembler only does 32 levels, so there! */
188 #define MAX_SUBSYM_HASH 100
189 static htab_t subsym_hash[MAX_SUBSYM_HASH];
190
191 /* Keep track of local labels so we can substitute them before GAS sees them
192 since macros use their own 'namespace' for local labels, use a separate hash
193
194 We do our own local label handling 'cuz it's subtly different from the
195 stock GAS handling.
196
197 We use our own macro nesting counter, since GAS overloads it when expanding
198 other things (like conditionals and repeat loops). */
199 static int macro_level = 0;
200 static htab_t local_label_hash[100];
201 /* Keep track of struct/union tags. */
202 static htab_t stag_hash;
203 static htab_t op_hash;
204 static htab_t parop_hash;
205 static htab_t reg_hash;
206 static htab_t mmreg_hash;
207 static htab_t cc_hash;
208 static htab_t cc2_hash;
209 static htab_t cc3_hash;
210 static htab_t sbit_hash;
211 static htab_t misc_symbol_hash;
212
213 /* Only word (et al.), align, or conditionals are allowed within
214 .struct/.union. */
215 #define ILLEGAL_WITHIN_STRUCT() \
216 do \
217 if (current_stag != NULL) \
218 { \
219 as_bad (_("pseudo-op illegal within .struct/.union")); \
220 return; \
221 } \
222 while (0)
223
224
225 static void subsym_create_or_replace (char *, char *);
226 static char *subsym_lookup (char *, int);
227 static char *subsym_substitute (char *, int);
228
229
230 void
231 md_show_usage (FILE *stream)
232 {
233 fprintf (stream, _("C54x-specific command line options:\n"));
234 fprintf (stream, _("-mfar-mode | -mf Use extended addressing\n"));
235 fprintf (stream, _("-mcpu=<CPU version> Specify the CPU version\n"));
236 fprintf (stream, _("-merrors-to-file <filename>\n"));
237 fprintf (stream, _("-me <filename> Redirect errors to a file\n"));
238 }
239
240 /* Output a single character (upper octet is zero). */
241
242 static void
243 tic54x_emit_char (char c)
244 {
245 expressionS expn;
246
247 expn.X_op = O_constant;
248 expn.X_add_number = c;
249 emit_expr (&expn, 2);
250 }
251
252 /* Walk backwards in the frag chain. */
253
254 static fragS *
255 frag_prev (fragS *frag, segT seg)
256 {
257 segment_info_type *seginfo = seg_info (seg);
258 fragS *fragp;
259
260 for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
261 if (fragp->fr_next == frag)
262 return fragp;
263
264 return NULL;
265 }
266
267 static fragS *
268 bit_offset_frag (fragS *frag, segT seg)
269 {
270 while (frag != NULL)
271 {
272 if (frag->fr_fix == 0
273 && frag->fr_opcode == NULL
274 && frag->tc_frag_data == 0)
275 frag = frag_prev (frag, seg);
276 else
277 return frag;
278 }
279 return NULL;
280 }
281
282 /* Return the number of bits allocated in the most recent word, or zero if
283 none. .field/.space/.bes may leave words partially allocated. */
284
285 static int
286 frag_bit_offset (fragS *frag, segT seg)
287 {
288 frag = bit_offset_frag (frag, seg);
289
290 if (frag)
291 return frag->fr_opcode != NULL ? -1 : frag->tc_frag_data;
292
293 return 0;
294 }
295
296 /* Read an expression from a C string; returns a pointer past the end of the
297 expression. */
298
299 static char *
300 parse_expression (char *str, expressionS *expn)
301 {
302 char *s;
303 char *tmp;
304
305 tmp = input_line_pointer; /* Save line pointer. */
306 input_line_pointer = str;
307 expression (expn);
308 s = input_line_pointer;
309 input_line_pointer = tmp; /* Restore line pointer. */
310 return s; /* Return pointer to where parsing stopped. */
311 }
312
313 /* .asg "character-string"|character-string, symbol
314
315 .eval is the only pseudo-op allowed to perform arithmetic on substitution
316 symbols. all other use of symbols defined with .asg are currently
317 unsupported. */
318
319 static void
320 tic54x_asg (int x ATTRIBUTE_UNUSED)
321 {
322 int c;
323 char *name;
324 char *str;
325 int quoted = *input_line_pointer == '"';
326
327 ILLEGAL_WITHIN_STRUCT ();
328
329 if (quoted)
330 {
331 int len;
332 str = demand_copy_C_string (&len);
333 c = *input_line_pointer;
334 }
335 else
336 {
337 str = input_line_pointer;
338 while ((c = *input_line_pointer) != ',')
339 {
340 if (is_end_of_line[(unsigned char) c])
341 break;
342 ++input_line_pointer;
343 }
344 *input_line_pointer = 0;
345 }
346 if (c != ',')
347 {
348 as_bad (_("Comma and symbol expected for '.asg STRING, SYMBOL'"));
349 ignore_rest_of_line ();
350 return;
351 }
352
353 ++input_line_pointer;
354 c = get_symbol_name (&name); /* Get terminator. */
355 if (!ISALPHA (*name))
356 {
357 as_bad (_("symbols assigned with .asg must begin with a letter"));
358 ignore_rest_of_line ();
359 return;
360 }
361
362 str = xstrdup (str);
363 name = xstrdup (name);
364 subsym_create_or_replace (name, str);
365 (void) restore_line_pointer (c);
366 demand_empty_rest_of_line ();
367 }
368
369 /* .eval expression, symbol
370 There's something screwy about this. The other assembler sometimes does and
371 sometimes doesn't substitute symbols defined with .eval.
372 We'll put the symbols into the subsym table as well as the normal symbol
373 table, since that's what works best. */
374
375 static void
376 tic54x_eval (int x ATTRIBUTE_UNUSED)
377 {
378 char c;
379 int value;
380 char *name;
381 symbolS *symbolP;
382 char valuestr[32], *tmp;
383 int quoted;
384
385 ILLEGAL_WITHIN_STRUCT ();
386
387 SKIP_WHITESPACE ();
388
389 quoted = *input_line_pointer == '"';
390 if (quoted)
391 ++input_line_pointer;
392 value = get_absolute_expression ();
393 if (quoted)
394 {
395 if (*input_line_pointer != '"')
396 {
397 as_bad (_("Unterminated string after absolute expression"));
398 ignore_rest_of_line ();
399 return;
400 }
401 ++input_line_pointer;
402 }
403 if (*input_line_pointer++ != ',')
404 {
405 as_bad (_("Comma and symbol expected for '.eval EXPR, SYMBOL'"));
406 ignore_rest_of_line ();
407 return;
408 }
409 c = get_symbol_name (&name); /* Get terminator. */
410 name = xstrdup (name);
411 (void) restore_line_pointer (c);
412
413 if (!ISALPHA (*name))
414 {
415 as_bad (_("symbols assigned with .eval must begin with a letter"));
416 ignore_rest_of_line ();
417 return;
418 }
419 symbolP = symbol_new (name, absolute_section,
420 (valueT) value, &zero_address_frag);
421 SF_SET_LOCAL (symbolP);
422 symbol_table_insert (symbolP);
423
424 /* The "other" assembler sometimes doesn't put .eval's in the subsym table
425 But since there's not written rule as to when, don't even bother trying
426 to match their behavior. */
427 sprintf (valuestr, "%d", value);
428 tmp = xstrdup (valuestr);
429 subsym_create_or_replace (name, tmp);
430
431 demand_empty_rest_of_line ();
432 }
433
434 /* .bss symbol, size [, [blocking flag] [, alignment flag]
435
436 alignment is to a longword boundary; blocking is to 128-word boundary.
437
438 1) if there is a hole in memory, this directive should attempt to fill it
439 (not yet implemented).
440
441 2) if the blocking flag is not set, allocate at the current SPC
442 otherwise, check to see if the current SPC plus the space to be
443 allocated crosses the page boundary (128 words).
444 if there's not enough space, create a hole and align with the next page
445 boundary.
446 (not yet implemented). */
447
448 static void
449 tic54x_bss (int x ATTRIBUTE_UNUSED)
450 {
451 char c;
452 char *name;
453 char *p;
454 int words;
455 segT current_seg;
456 subsegT current_subseg;
457 symbolS *symbolP;
458 int block = 0;
459 int align = 0;
460
461 ILLEGAL_WITHIN_STRUCT ();
462
463 current_seg = now_seg; /* Save current seg. */
464 current_subseg = now_subseg; /* Save current subseg. */
465
466 c = get_symbol_name (&name); /* Get terminator. */
467 if (c == '"')
468 c = * ++ input_line_pointer;
469 if (c != ',')
470 {
471 as_bad (_(".bss size argument missing\n"));
472 ignore_rest_of_line ();
473 return;
474 }
475
476 ++input_line_pointer;
477 words = get_absolute_expression ();
478 if (words < 0)
479 {
480 as_bad (_(".bss size %d < 0!"), words);
481 ignore_rest_of_line ();
482 return;
483 }
484
485 if (*input_line_pointer == ',')
486 {
487 /* The blocking flag may be missing. */
488 ++input_line_pointer;
489 if (*input_line_pointer != ',')
490 block = get_absolute_expression ();
491 else
492 block = 0;
493
494 if (*input_line_pointer == ',')
495 {
496 ++input_line_pointer;
497 align = get_absolute_expression ();
498 }
499 else
500 align = 0;
501 }
502 else
503 block = align = 0;
504
505 subseg_set (bss_section, 0);
506 symbolP = symbol_find_or_make (name);
507
508 if (S_GET_SEGMENT (symbolP) == bss_section)
509 symbol_get_frag (symbolP)->fr_symbol = (symbolS *) NULL;
510
511 symbol_set_frag (symbolP, frag_now);
512 p = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
513 (offsetT) (words * OCTETS_PER_BYTE), (char *) 0);
514 *p = 0; /* Fill char. */
515
516 S_SET_SEGMENT (symbolP, bss_section);
517
518 /* The symbol may already have been created with a preceding
519 ".globl" directive -- be careful not to step on storage class
520 in that case. Otherwise, set it to static. */
521 if (S_GET_STORAGE_CLASS (symbolP) != C_EXT)
522 S_SET_STORAGE_CLASS (symbolP, C_STAT);
523
524 if (align)
525 {
526 /* s_align eats end of line; restore it */
527 s_align_bytes (4);
528 --input_line_pointer;
529 }
530
531 if (block)
532 bss_section->flags |= SEC_TIC54X_BLOCK;
533
534 subseg_set (current_seg, current_subseg); /* Restore current seg. */
535 demand_empty_rest_of_line ();
536 }
537
538 static void
539 stag_add_field_symbols (struct stag *stag,
540 const char *path,
541 bfd_vma base_offset,
542 symbolS *rootsym,
543 const char *root_stag_name)
544 {
545 char * prefix;
546 struct stag_field *field = stag->field;
547
548 /* Construct a symbol for every field contained within this structure
549 including fields within structure fields. */
550 prefix = concat (path, *path ? "." : "", NULL);
551
552 while (field != NULL)
553 {
554 char *name = concat (prefix, field->name, NULL);
555
556 if (rootsym == NULL)
557 {
558 symbolS *sym;
559 sym = symbol_new (name, absolute_section,
560 (field->stag ? field->offset :
561 (valueT) (base_offset + field->offset)),
562 &zero_address_frag);
563 SF_SET_LOCAL (sym);
564 symbol_table_insert (sym);
565 }
566 else
567 {
568 char *replacement;
569
570 replacement = concat (S_GET_NAME (rootsym), "+", root_stag_name,
571 name + strlen (S_GET_NAME (rootsym)), NULL);
572 str_hash_insert (subsym_hash[0], name, replacement);
573 }
574
575 /* Recurse if the field is a structure.
576 Note the field offset is relative to the outermost struct. */
577 if (field->stag != NULL)
578 stag_add_field_symbols (field->stag, name,
579 field->offset,
580 rootsym, root_stag_name);
581 field = field->next;
582 free (name);
583 }
584 free (prefix);
585 }
586
587 /* Keep track of stag fields so that when structures are nested we can add the
588 complete dereferencing symbols to the symbol table. */
589
590 static void
591 stag_add_field (struct stag *parent,
592 const char *name,
593 bfd_vma offset,
594 struct stag *stag)
595 {
596 struct stag_field *sfield = XCNEW (struct stag_field);
597
598 sfield->name = xstrdup (name);
599 sfield->offset = offset;
600 sfield->bitfield_offset = parent->current_bitfield_offset;
601 sfield->stag = stag;
602 if (parent->field == NULL)
603 parent->field = sfield;
604 else
605 {
606 struct stag_field *sf = parent->field;
607 while (sf->next != NULL)
608 sf = sf->next;
609 sf->next = sfield;
610 }
611 /* Only create a symbol for this field if the parent has no name. */
612 if (!strncmp (".fake", parent->name, 5))
613 {
614 symbolS *sym = symbol_new (name, absolute_section,
615 (valueT) offset, &zero_address_frag);
616 SF_SET_LOCAL (sym);
617 symbol_table_insert (sym);
618 }
619 }
620
621 /* [STAG] .struct [OFFSET]
622 Start defining structure offsets (symbols in absolute section). */
623
624 static void
625 tic54x_struct (int arg)
626 {
627 int start_offset = 0;
628 int is_union = arg;
629
630 if (!current_stag)
631 {
632 /* Starting a new struct, switch to absolute section. */
633 stag_saved_seg = now_seg;
634 stag_saved_subseg = now_subseg;
635 subseg_set (absolute_section, 0);
636 }
637 /* Align the current pointer. */
638 else if (current_stag->current_bitfield_offset != 0)
639 {
640 ++abs_section_offset;
641 current_stag->current_bitfield_offset = 0;
642 }
643
644 /* Offset expression is only meaningful for global .structs. */
645 if (!is_union)
646 {
647 /* Offset is ignored in inner structs. */
648 SKIP_WHITESPACE ();
649 if (!is_end_of_line[(unsigned char) *input_line_pointer])
650 start_offset = get_absolute_expression ();
651 else
652 start_offset = 0;
653 }
654
655 if (current_stag)
656 {
657 /* Nesting, link to outer one. */
658 current_stag->inner = XCNEW (struct stag);
659 current_stag->inner->outer = current_stag;
660 current_stag = current_stag->inner;
661 if (start_offset)
662 as_warn (_("Offset on nested structures is ignored"));
663 start_offset = abs_section_offset;
664 }
665 else
666 {
667 current_stag = XCNEW (struct stag);
668 abs_section_offset = start_offset;
669 }
670 current_stag->is_union = is_union;
671
672 if (line_label == NULL)
673 {
674 static int struct_count = 0;
675 char fake[] = ".fake_stagNNNNNNN";
676 sprintf (fake, ".fake_stag%d", struct_count++);
677 current_stag->sym = symbol_new (fake, absolute_section,
678 (valueT) abs_section_offset,
679 &zero_address_frag);
680 }
681 else
682 {
683 char * label = xstrdup (S_GET_NAME (line_label));
684 current_stag->sym = symbol_new (label,
685 absolute_section,
686 (valueT) abs_section_offset,
687 &zero_address_frag);
688 free (label);
689 }
690 current_stag->name = S_GET_NAME (current_stag->sym);
691 SF_SET_LOCAL (current_stag->sym);
692 /* Nested .structs don't go into the symbol table. */
693 if (current_stag->outer == NULL)
694 symbol_table_insert (current_stag->sym);
695
696 line_label = NULL;
697 }
698
699 /* [LABEL] .endstruct
700 finish defining structure offsets; optional LABEL's value will be the size
701 of the structure. */
702
703 static void
704 tic54x_endstruct (int is_union)
705 {
706 int size;
707 const char *path =
708 !strncmp (current_stag->name, ".fake", 5) ? "" : current_stag->name;
709
710 if (!current_stag || current_stag->is_union != is_union)
711 {
712 as_bad (_(".end%s without preceding .%s"),
713 is_union ? "union" : "struct",
714 is_union ? "union" : "struct");
715 ignore_rest_of_line ();
716 return;
717 }
718
719 /* Align end of structures. */
720 if (current_stag->current_bitfield_offset)
721 {
722 ++abs_section_offset;
723 current_stag->current_bitfield_offset = 0;
724 }
725
726 if (current_stag->is_union)
727 size = current_stag->size;
728 else
729 size = abs_section_offset - S_GET_VALUE (current_stag->sym);
730 if (line_label != NULL)
731 {
732 S_SET_VALUE (line_label, size);
733 symbol_table_insert (line_label);
734 line_label = NULL;
735 }
736
737 /* Union size has already been calculated. */
738 if (!current_stag->is_union)
739 current_stag->size = size;
740 /* Nested .structs don't get put in the stag table. */
741 if (current_stag->outer == NULL)
742 {
743 str_hash_insert (stag_hash, current_stag->name, current_stag);
744 stag_add_field_symbols (current_stag, path,
745 S_GET_VALUE (current_stag->sym),
746 NULL, NULL);
747 }
748 current_stag = current_stag->outer;
749
750 /* If this is a nested .struct/.union, add it as a field to the enclosing
751 one. otherwise, restore the section we were in. */
752 if (current_stag != NULL)
753 {
754 stag_add_field (current_stag, current_stag->inner->name,
755 S_GET_VALUE (current_stag->inner->sym),
756 current_stag->inner);
757 }
758 else
759 subseg_set (stag_saved_seg, stag_saved_subseg);
760 }
761
762 /* [LABEL] .tag STAG
763 Reference a structure within a structure, as a sized field with an optional
764 label.
765 If used outside of a .struct/.endstruct, overlays the given structure
766 format on the existing allocated space. */
767
768 static void
769 tic54x_tag (int ignore ATTRIBUTE_UNUSED)
770 {
771 char *name;
772 int c = get_symbol_name (&name);
773 struct stag *stag = (struct stag *) str_hash_find (stag_hash, name);
774
775 if (!stag)
776 {
777 if (*name)
778 as_bad (_("Unrecognized struct/union tag '%s'"), name);
779 else
780 as_bad (_(".tag requires a structure tag"));
781 ignore_rest_of_line ();
782 return;
783 }
784 if (line_label == NULL)
785 {
786 as_bad (_("Label required for .tag"));
787 ignore_rest_of_line ();
788 return;
789 }
790 else
791 {
792 char * label;
793
794 label = xstrdup (S_GET_NAME (line_label));
795 if (current_stag != NULL)
796 stag_add_field (current_stag, label,
797 abs_section_offset - S_GET_VALUE (current_stag->sym),
798 stag);
799 else
800 {
801 symbolS *sym = symbol_find (label);
802
803 if (!sym)
804 {
805 as_bad (_(".tag target '%s' undefined"), label);
806 ignore_rest_of_line ();
807 free (label);
808 return;
809 }
810 stag_add_field_symbols (stag, S_GET_NAME (sym),
811 S_GET_VALUE (stag->sym), sym, stag->name);
812 }
813 free (label);
814 }
815
816 /* Bump by the struct size, but only if we're within a .struct section. */
817 if (current_stag != NULL && !current_stag->is_union)
818 abs_section_offset += stag->size;
819
820 (void) restore_line_pointer (c);
821 demand_empty_rest_of_line ();
822 line_label = NULL;
823 }
824
825 /* Handle all .byte, .char, .double, .field, .float, .half, .int, .long,
826 .short, .string, .ubyte, .uchar, .uhalf, .uint, .ulong, .ushort, .uword,
827 and .word. */
828
829 static void
830 tic54x_struct_field (int type)
831 {
832 int size;
833 int count = 1;
834 int new_bitfield_offset = 0;
835 int field_align = current_stag->current_bitfield_offset != 0;
836 int longword_align = 0;
837
838 SKIP_WHITESPACE ();
839 if (!is_end_of_line[(unsigned char) *input_line_pointer])
840 count = get_absolute_expression ();
841
842 switch (type)
843 {
844 case 'b':
845 case 'B':
846 case 'c':
847 case 'C':
848 case 'h':
849 case 'H':
850 case 'i':
851 case 'I':
852 case 's':
853 case 'S':
854 case 'w':
855 case 'W':
856 case '*': /* String. */
857 size = 1;
858 break;
859 case 'f':
860 case 'l':
861 case 'L':
862 longword_align = 1;
863 size = 2;
864 break;
865 case '.': /* Bitfield. */
866 size = 0;
867 if (count < 1 || count > 32)
868 {
869 as_bad (_(".field count '%d' out of range (1 <= X <= 32)"), count);
870 ignore_rest_of_line ();
871 return;
872 }
873 if (current_stag->current_bitfield_offset + count > 16)
874 {
875 /* Set the appropriate size and new field offset. */
876 if (count == 32)
877 {
878 size = 2;
879 count = 1;
880 }
881 else if (count > 16)
882 {
883 size = 1;
884 count = 1;
885 new_bitfield_offset = count - 16;
886 }
887 else
888 new_bitfield_offset = count;
889 }
890 else
891 {
892 field_align = 0;
893 new_bitfield_offset = current_stag->current_bitfield_offset + count;
894 }
895 break;
896 default:
897 as_bad (_("Unrecognized field type '%c'"), type);
898 ignore_rest_of_line ();
899 return;
900 }
901
902 if (field_align)
903 {
904 /* Align to the actual starting position of the field. */
905 current_stag->current_bitfield_offset = 0;
906 ++abs_section_offset;
907 }
908 /* Align to longword boundary. */
909 if (longword_align && (abs_section_offset & 0x1))
910 ++abs_section_offset;
911
912 if (line_label == NULL)
913 {
914 static int fieldno = 0;
915 char fake[] = ".fake_fieldNNNNN";
916
917 sprintf (fake, ".fake_field%d", fieldno++);
918 stag_add_field (current_stag, fake,
919 abs_section_offset - S_GET_VALUE (current_stag->sym),
920 NULL);
921 }
922 else
923 {
924 char * label;
925
926 label = xstrdup (S_GET_NAME (line_label));
927 stag_add_field (current_stag, label,
928 abs_section_offset - S_GET_VALUE (current_stag->sym),
929 NULL);
930 free (label);
931 }
932
933 if (current_stag->is_union)
934 {
935 /* Note we treat the element as if it were an array of COUNT. */
936 if (current_stag->size < (unsigned) size * count)
937 current_stag->size = size * count;
938 }
939 else
940 {
941 abs_section_offset += (unsigned) size * count;
942 current_stag->current_bitfield_offset = new_bitfield_offset;
943 }
944 line_label = NULL;
945 }
946
947 /* Handle .byte, .word. .int, .long and all variants. */
948
949 static void
950 tic54x_cons (int type)
951 {
952 unsigned int c;
953 int octets;
954
955 /* If we're within a .struct construct, don't actually allocate space. */
956 if (current_stag != NULL)
957 {
958 tic54x_struct_field (type);
959 return;
960 }
961
962 #ifdef md_flush_pending_output
963 md_flush_pending_output ();
964 #endif
965
966 generate_lineno_debug ();
967
968 /* Align long words to long word boundaries (4 octets). */
969 if (type == 'l' || type == 'L')
970 {
971 frag_align (2, 0, 2);
972 /* If there's a label, assign it to the first allocated word. */
973 if (line_label != NULL)
974 {
975 symbol_set_frag (line_label, frag_now);
976 S_SET_VALUE (line_label, frag_now_fix ());
977 }
978 }
979
980 switch (type)
981 {
982 case 'l':
983 case 'L':
984 case 'x':
985 octets = 4;
986 break;
987 case 'b':
988 case 'B':
989 case 'c':
990 case 'C':
991 octets = 1;
992 break;
993 default:
994 octets = 2;
995 break;
996 }
997
998 do
999 {
1000 if (*input_line_pointer == '"')
1001 {
1002 input_line_pointer++;
1003 while (is_a_char (c = next_char_of_string ()))
1004 tic54x_emit_char (c);
1005 know (input_line_pointer[-1] == '\"');
1006 }
1007 else
1008 {
1009 expressionS expn;
1010
1011 input_line_pointer = parse_expression (input_line_pointer, &expn);
1012 if (expn.X_op == O_constant)
1013 {
1014 offsetT value = expn.X_add_number;
1015 /* Truncate overflows. */
1016 switch (octets)
1017 {
1018 case 1:
1019 if ((value > 0 && value > 0xFF)
1020 || (value < 0 && value < - 0x100))
1021 as_warn (_("Overflow in expression, truncated to 8 bits"));
1022 break;
1023 case 2:
1024 if ((value > 0 && value > 0xFFFF)
1025 || (value < 0 && value < - 0x10000))
1026 as_warn (_("Overflow in expression, truncated to 16 bits"));
1027 break;
1028 }
1029 }
1030 if (expn.X_op != O_constant && octets < 2)
1031 {
1032 /* Disallow .byte with a non constant expression that will
1033 require relocation. */
1034 as_bad (_("Relocatable values require at least WORD storage"));
1035 ignore_rest_of_line ();
1036 return;
1037 }
1038
1039 if (expn.X_op != O_constant
1040 && amode == c_mode
1041 && octets == 4)
1042 {
1043 /* FIXME -- at one point TI tools used to output REL16
1044 relocations, but I don't think the latest tools do at all
1045 The current tools output extended relocations regardless of
1046 the addressing mode (I actually think that ".c_mode" is
1047 totally ignored in the latest tools). */
1048 amode = far_mode;
1049 emitting_long = 1;
1050 emit_expr (&expn, 4);
1051 emitting_long = 0;
1052 amode = c_mode;
1053 }
1054 else
1055 {
1056 emitting_long = octets == 4;
1057 emit_expr (&expn, (octets == 1) ? 2 : octets);
1058 emitting_long = 0;
1059 }
1060 }
1061 }
1062 while (*input_line_pointer++ == ',');
1063
1064 input_line_pointer--; /* Put terminator back into stream. */
1065 demand_empty_rest_of_line ();
1066 }
1067
1068 /* .global <symbol>[,...,<symbolN>]
1069 .def <symbol>[,...,<symbolN>]
1070 .ref <symbol>[,...,<symbolN>]
1071
1072 These all identify global symbols.
1073
1074 .def means the symbol is defined in the current module and can be accessed
1075 by other files. The symbol should be placed in the symbol table.
1076
1077 .ref means the symbol is used in the current module but defined in another
1078 module. The linker is to resolve this symbol's definition at link time.
1079
1080 .global should act as a .ref or .def, as needed.
1081
1082 global, def and ref all have symbol storage classes of C_EXT.
1083
1084 I can't identify any difference in how the "other" c54x assembler treats
1085 these, so we ignore the type here. */
1086
1087 void
1088 tic54x_global (int type)
1089 {
1090 char *name;
1091 int c;
1092 symbolS *symbolP;
1093
1094 if (type == 'r')
1095 as_warn (_("Use of .def/.ref is deprecated. Use .global instead"));
1096
1097 ILLEGAL_WITHIN_STRUCT ();
1098
1099 do
1100 {
1101 c = get_symbol_name (&name);
1102 symbolP = symbol_find_or_make (name);
1103 c = restore_line_pointer (c);
1104
1105 S_SET_STORAGE_CLASS (symbolP, C_EXT);
1106 if (c == ',')
1107 {
1108 input_line_pointer++;
1109 if (is_end_of_line[(unsigned char) *input_line_pointer])
1110 c = *input_line_pointer;
1111 }
1112 }
1113 while (c == ',');
1114
1115 demand_empty_rest_of_line ();
1116 }
1117
1118 /* Remove the symbol from the local label hash lookup. */
1119
1120 static int
1121 tic54x_remove_local_label (void **slot, void *arg ATTRIBUTE_UNUSED)
1122 {
1123 string_tuple_t *tuple = *((string_tuple_t **) slot);
1124 void *elem = str_hash_find (local_label_hash[macro_level], tuple->key);
1125 str_hash_delete (local_label_hash[macro_level], tuple->key);
1126 free (elem);
1127 return 0;
1128 }
1129
1130 /* Reset all local labels. */
1131
1132 static void
1133 tic54x_clear_local_labels (int ignored ATTRIBUTE_UNUSED)
1134 {
1135 htab_traverse (local_label_hash[macro_level], tic54x_remove_local_label, NULL);
1136 }
1137
1138 /* .text
1139 .data
1140 .sect "section name"
1141
1142 Initialized section
1143 make sure local labels get cleared when changing sections
1144
1145 ARG is 't' for text, 'd' for data, or '*' for a named section
1146
1147 For compatibility, '*' sections are SEC_CODE if instructions are
1148 encountered, or SEC_DATA if not.
1149 */
1150
1151 static void
1152 tic54x_sect (int arg)
1153 {
1154 ILLEGAL_WITHIN_STRUCT ();
1155
1156 /* Local labels are cleared when changing sections. */
1157 tic54x_clear_local_labels (0);
1158
1159 if (arg == 't')
1160 s_text (0);
1161 else if (arg == 'd')
1162 s_data (0);
1163 else
1164 {
1165 char *name = NULL;
1166 int len;
1167 /* Make sure all named initialized sections flagged properly. If we
1168 encounter instructions, we'll flag it with SEC_CODE as well. */
1169 const char *flags = ",\"w\"\n";
1170
1171 /* If there are quotes, remove them. */
1172 if (*input_line_pointer == '"')
1173 {
1174 name = demand_copy_C_string (&len);
1175 demand_empty_rest_of_line ();
1176 name = concat (name, flags, (char *) NULL);
1177 }
1178 else
1179 {
1180 int c;
1181
1182 c = get_symbol_name (&name);
1183 name = concat (name, flags, (char *) NULL);
1184 (void) restore_line_pointer (c);
1185 demand_empty_rest_of_line ();
1186 }
1187
1188 input_scrub_insert_line (name);
1189 obj_coff_section (0);
1190
1191 /* If there was a line label, make sure that it gets assigned the proper
1192 section. This is for compatibility, even though the actual behavior
1193 is not explicitly defined. For consistency, we make .sect behave
1194 like .usect, since that is probably what people expect. */
1195 if (line_label != NULL)
1196 {
1197 S_SET_SEGMENT (line_label, now_seg);
1198 symbol_set_frag (line_label, frag_now);
1199 S_SET_VALUE (line_label, frag_now_fix ());
1200 if (S_GET_STORAGE_CLASS (line_label) != C_EXT)
1201 S_SET_STORAGE_CLASS (line_label, C_LABEL);
1202 }
1203 }
1204 }
1205
1206 /* [symbol] .space space_in_bits
1207 [symbol] .bes space_in_bits
1208 BES puts the symbol at the *last* word allocated
1209
1210 cribbed from s_space. */
1211
1212 static void
1213 tic54x_space (int arg)
1214 {
1215 expressionS expn;
1216 char *p = 0;
1217 int octets = 0;
1218 long words;
1219 int bits_per_byte = (OCTETS_PER_BYTE * 8);
1220 int bit_offset = 0;
1221 symbolS *label = line_label;
1222 int bes = arg;
1223
1224 ILLEGAL_WITHIN_STRUCT ();
1225
1226 #ifdef md_flush_pending_output
1227 md_flush_pending_output ();
1228 #endif
1229
1230 /* Read the bit count. */
1231 expression (&expn);
1232
1233 /* Some expressions are unresolvable until later in the assembly pass;
1234 postpone until relaxation/fixup. we also have to postpone if a previous
1235 partial allocation has not been completed yet. */
1236 if (expn.X_op != O_constant || frag_bit_offset (frag_now, now_seg) == -1)
1237 {
1238 struct bit_info *bi = XNEW (struct bit_info);
1239
1240 bi->seg = now_seg;
1241 bi->type = bes;
1242 bi->sym = label;
1243 p = frag_var (rs_machine_dependent,
1244 65536 * 2, 1, (relax_substateT) 0,
1245 make_expr_symbol (&expn), (offsetT) 0,
1246 (char *) bi);
1247 if (p)
1248 *p = 0;
1249
1250 return;
1251 }
1252
1253 /* Reduce the required size by any bit offsets currently left over
1254 from a previous .space/.bes/.field directive. */
1255 bit_offset = frag_now->tc_frag_data;
1256 if (bit_offset != 0 && bit_offset < 16)
1257 {
1258 int spare_bits = bits_per_byte - bit_offset;
1259
1260 if (spare_bits >= expn.X_add_number)
1261 {
1262 /* Don't have to do anything; sufficient bits have already been
1263 allocated; just point the label to the right place. */
1264 if (label != NULL)
1265 {
1266 symbol_set_frag (label, frag_now);
1267 S_SET_VALUE (label, frag_now_fix () - 1);
1268 label = NULL;
1269 }
1270 frag_now->tc_frag_data += expn.X_add_number;
1271 goto getout;
1272 }
1273 expn.X_add_number -= spare_bits;
1274 /* Set the label to point to the first word allocated, which in this
1275 case is the previous word, which was only partially filled. */
1276 if (!bes && label != NULL)
1277 {
1278 symbol_set_frag (label, frag_now);
1279 S_SET_VALUE (label, frag_now_fix () - 1);
1280 label = NULL;
1281 }
1282 }
1283 /* Convert bits to bytes/words and octets, rounding up. */
1284 words = ((expn.X_add_number + bits_per_byte - 1) / bits_per_byte);
1285 /* How many do we have left over? */
1286 bit_offset = expn.X_add_number % bits_per_byte;
1287 octets = words * OCTETS_PER_BYTE;
1288 if (octets < 0)
1289 {
1290 as_warn (_(".space/.bes repeat count is negative, ignored"));
1291 goto getout;
1292 }
1293 else if (octets == 0)
1294 {
1295 as_warn (_(".space/.bes repeat count is zero, ignored"));
1296 goto getout;
1297 }
1298
1299 /* If we are in the absolute section, just bump the offset. */
1300 if (now_seg == absolute_section)
1301 {
1302 abs_section_offset += words;
1303 if (bes && label != NULL)
1304 S_SET_VALUE (label, abs_section_offset - 1);
1305 frag_now->tc_frag_data = bit_offset;
1306 goto getout;
1307 }
1308
1309 if (!need_pass_2)
1310 p = frag_var (rs_fill, 1, 1,
1311 (relax_substateT) 0, (symbolS *) 0,
1312 (offsetT) octets, (char *) 0);
1313
1314 /* Make note of how many bits of this word we've allocated so far. */
1315 frag_now->tc_frag_data = bit_offset;
1316
1317 /* .bes puts label at *last* word allocated. */
1318 if (bes && label != NULL)
1319 {
1320 symbol_set_frag (label, frag_now);
1321 S_SET_VALUE (label, frag_now_fix () - 1);
1322 }
1323
1324 if (p)
1325 *p = 0;
1326
1327 getout:
1328
1329 demand_empty_rest_of_line ();
1330 }
1331
1332 /* [symbol] .usect "section-name", size-in-words
1333 [, [blocking-flag] [, alignment-flag]]
1334
1335 Uninitialized section.
1336 Non-zero blocking means that if the section would cross a page (128-word)
1337 boundary, it will be page-aligned.
1338 Non-zero alignment aligns on a longword boundary.
1339
1340 Has no effect on the current section. */
1341
1342 static void
1343 tic54x_usect (int x ATTRIBUTE_UNUSED)
1344 {
1345 char c;
1346 char *name;
1347 char *section_name;
1348 char *p;
1349 segT seg;
1350 int size, blocking_flag, alignment_flag;
1351 segT current_seg;
1352 subsegT current_subseg;
1353 flagword flags;
1354
1355 ILLEGAL_WITHIN_STRUCT ();
1356
1357 current_seg = now_seg; /* Save current seg. */
1358 current_subseg = now_subseg; /* Save current subseg. */
1359
1360 c = get_symbol_name (&section_name); /* Get terminator. */
1361 name = xstrdup (section_name);
1362 c = restore_line_pointer (c);
1363
1364 if (c == ',')
1365 ++input_line_pointer;
1366 else
1367 {
1368 as_bad (_("Missing size argument"));
1369 ignore_rest_of_line ();
1370 return;
1371 }
1372
1373 size = get_absolute_expression ();
1374
1375 /* Read a possibly present third argument (blocking flag). */
1376 if (*input_line_pointer == ',')
1377 {
1378 ++input_line_pointer;
1379 if (*input_line_pointer != ',')
1380 blocking_flag = get_absolute_expression ();
1381 else
1382 blocking_flag = 0;
1383
1384 /* Read a possibly present fourth argument (alignment flag). */
1385 if (*input_line_pointer == ',')
1386 {
1387 ++input_line_pointer;
1388 alignment_flag = get_absolute_expression ();
1389 }
1390 else
1391 alignment_flag = 0;
1392 }
1393 else
1394 blocking_flag = alignment_flag = 0;
1395
1396 seg = subseg_new (name, 0);
1397 flags = bfd_section_flags (seg) | SEC_ALLOC;
1398
1399 if (alignment_flag)
1400 {
1401 /* s_align eats end of line; restore it. */
1402 s_align_bytes (4);
1403 --input_line_pointer;
1404 }
1405
1406 if (line_label != NULL)
1407 {
1408 S_SET_SEGMENT (line_label, seg);
1409 symbol_set_frag (line_label, frag_now);
1410 S_SET_VALUE (line_label, frag_now_fix ());
1411 /* Set scl to label, since that's what TI does. */
1412 if (S_GET_STORAGE_CLASS (line_label) != C_EXT)
1413 S_SET_STORAGE_CLASS (line_label, C_LABEL);
1414 }
1415
1416 seg_info (seg)->bss = 1; /* Uninitialized data. */
1417
1418 p = frag_var (rs_fill, 1, 1,
1419 (relax_substateT) 0, (symbolS *) line_label,
1420 size * OCTETS_PER_BYTE, (char *) 0);
1421 *p = 0;
1422
1423 if (blocking_flag)
1424 flags |= SEC_TIC54X_BLOCK;
1425
1426 if (!bfd_set_section_flags (seg, flags))
1427 as_warn (_("Error setting flags for \"%s\": %s"), name,
1428 bfd_errmsg (bfd_get_error ()));
1429
1430 subseg_set (current_seg, current_subseg); /* Restore current seg. */
1431 demand_empty_rest_of_line ();
1432 }
1433
1434 static enum cpu_version
1435 lookup_version (const char *ver)
1436 {
1437 enum cpu_version version = VNONE;
1438
1439 if (ver[0] == '5' && ver[1] == '4')
1440 {
1441 if (strlen (ver) == 3
1442 && (ver[2] == '1' || ver[2] == '2' || ver[2] == '3'
1443 || ver[2] == '5' || ver[2] == '8' || ver[2] == '9'))
1444 version = ver[2] - '0';
1445 else if (strlen (ver) == 5
1446 && TOUPPER (ver[3]) == 'L'
1447 && TOUPPER (ver[4]) == 'P'
1448 && (ver[2] == '5' || ver[2] == '6'))
1449 version = ver[2] - '0' + 10;
1450 }
1451
1452 return version;
1453 }
1454
1455 static void
1456 set_cpu (enum cpu_version version)
1457 {
1458 cpu = version;
1459 if (version == V545LP || version == V546LP)
1460 {
1461 symbolS *symbolP = symbol_new ("__allow_lp", absolute_section,
1462 (valueT) 1, &zero_address_frag);
1463 SF_SET_LOCAL (symbolP);
1464 symbol_table_insert (symbolP);
1465 }
1466 }
1467
1468 /* .version cpu-version
1469 cpu-version may be one of the following:
1470 541
1471 542
1472 543
1473 545
1474 545LP
1475 546LP
1476 548
1477 549
1478
1479 This is for compatibility only. It currently has no affect on assembly. */
1480 static int cpu_needs_set = 1;
1481
1482 static void
1483 tic54x_version (int x ATTRIBUTE_UNUSED)
1484 {
1485 enum cpu_version version = VNONE;
1486 enum cpu_version old_version = cpu;
1487 int c;
1488 char *ver;
1489
1490 ILLEGAL_WITHIN_STRUCT ();
1491
1492 SKIP_WHITESPACE ();
1493 ver = input_line_pointer;
1494 while (!is_end_of_line[(unsigned char) *input_line_pointer])
1495 ++input_line_pointer;
1496 c = *input_line_pointer;
1497 *input_line_pointer = 0;
1498
1499 version = lookup_version (ver);
1500
1501 if (cpu != VNONE && cpu != version)
1502 as_warn (_("CPU version has already been set"));
1503
1504 if (version == VNONE)
1505 {
1506 as_bad (_("Unrecognized version '%s'"), ver);
1507 ignore_rest_of_line ();
1508 return;
1509 }
1510 else if (assembly_begun && version != old_version)
1511 {
1512 as_bad (_("Changing of CPU version on the fly not supported"));
1513 ignore_rest_of_line ();
1514 return;
1515 }
1516
1517 set_cpu (version);
1518
1519 *input_line_pointer = c;
1520 demand_empty_rest_of_line ();
1521 }
1522
1523 /* 'f' = float, 'x' = xfloat, 'd' = double, 'l' = ldouble. */
1524
1525 static void
1526 tic54x_float_cons (int type)
1527 {
1528 if (current_stag != 0)
1529 tic54x_struct_field ('f');
1530
1531 #ifdef md_flush_pending_output
1532 md_flush_pending_output ();
1533 #endif
1534
1535 /* Align to long word boundary (4 octets) unless it's ".xfloat". */
1536 if (type != 'x')
1537 {
1538 frag_align (2, 0, 2);
1539 /* If there's a label, assign it to the first allocated word. */
1540 if (line_label != NULL)
1541 {
1542 symbol_set_frag (line_label, frag_now);
1543 S_SET_VALUE (line_label, frag_now_fix ());
1544 }
1545 }
1546
1547 float_cons ('f');
1548 }
1549
1550 /* The argument is capitalized if it should be zero-terminated
1551 's' is normal string with upper 8-bits zero-filled, 'p' is packed.
1552 Code copied from stringer, and slightly modified so that strings are packed
1553 and encoded into the correct octets. */
1554
1555 static void
1556 tic54x_stringer (int type)
1557 {
1558 unsigned int c;
1559 int append_zero = type == 'S' || type == 'P';
1560 int packed = type == 'p' || type == 'P';
1561 int last_char = -1; /* Packed strings need two bytes at a time to encode. */
1562
1563 if (current_stag != NULL)
1564 {
1565 tic54x_struct_field ('*');
1566 return;
1567 }
1568
1569 #ifdef md_flush_pending_output
1570 md_flush_pending_output ();
1571 #endif
1572
1573 c = ','; /* Do loop. */
1574 while (c == ',')
1575 {
1576 SKIP_WHITESPACE ();
1577 switch (*input_line_pointer)
1578 {
1579 default:
1580 {
1581 unsigned short value = get_absolute_expression ();
1582 FRAG_APPEND_1_CHAR ( value & 0xFF);
1583 FRAG_APPEND_1_CHAR ((value >> 8) & 0xFF);
1584 break;
1585 }
1586 case '\"':
1587 ++input_line_pointer; /* -> 1st char of string. */
1588 while (is_a_char (c = next_char_of_string ()))
1589 {
1590 if (!packed)
1591 {
1592 FRAG_APPEND_1_CHAR (c);
1593 FRAG_APPEND_1_CHAR (0);
1594 }
1595 else
1596 {
1597 /* Packed strings are filled MS octet first. */
1598 if (last_char == -1)
1599 last_char = c;
1600 else
1601 {
1602 FRAG_APPEND_1_CHAR (c);
1603 FRAG_APPEND_1_CHAR (last_char);
1604 last_char = -1;
1605 }
1606 }
1607 }
1608 if (append_zero)
1609 {
1610 if (packed && last_char != -1)
1611 {
1612 FRAG_APPEND_1_CHAR (0);
1613 FRAG_APPEND_1_CHAR (last_char);
1614 last_char = -1;
1615 }
1616 else
1617 {
1618 FRAG_APPEND_1_CHAR (0);
1619 FRAG_APPEND_1_CHAR (0);
1620 }
1621 }
1622 know (input_line_pointer[-1] == '\"');
1623 break;
1624 }
1625 SKIP_WHITESPACE ();
1626 c = *input_line_pointer;
1627 if (!is_end_of_line[c])
1628 ++input_line_pointer;
1629 }
1630
1631 /* Finish up any leftover packed string. */
1632 if (packed && last_char != -1)
1633 {
1634 FRAG_APPEND_1_CHAR (0);
1635 FRAG_APPEND_1_CHAR (last_char);
1636 }
1637 demand_empty_rest_of_line ();
1638 }
1639
1640 static void
1641 tic54x_p2align (int arg ATTRIBUTE_UNUSED)
1642 {
1643 as_bad (_("p2align not supported on this target"));
1644 }
1645
1646 static void
1647 tic54x_align_words (int arg)
1648 {
1649 /* Only ".align" with no argument is allowed within .struct/.union. */
1650 int count = arg;
1651
1652 if (!is_end_of_line[(unsigned char) *input_line_pointer])
1653 {
1654 if (arg == 2)
1655 as_warn (_("Argument to .even ignored"));
1656 else
1657 count = get_absolute_expression ();
1658 }
1659
1660 if (current_stag != NULL && arg == 128)
1661 {
1662 if (current_stag->current_bitfield_offset != 0)
1663 {
1664 current_stag->current_bitfield_offset = 0;
1665 ++abs_section_offset;
1666 }
1667 demand_empty_rest_of_line ();
1668 return;
1669 }
1670
1671 ILLEGAL_WITHIN_STRUCT ();
1672
1673 s_align_bytes (count << 1);
1674 }
1675
1676 /* Initialize multiple-bit fields within a single word of memory. */
1677
1678 static void
1679 tic54x_field (int ignore ATTRIBUTE_UNUSED)
1680 {
1681 expressionS expn;
1682 int size = 16;
1683 char *p;
1684 valueT value;
1685 symbolS *label = line_label;
1686
1687 if (current_stag != NULL)
1688 {
1689 tic54x_struct_field ('.');
1690 return;
1691 }
1692
1693 input_line_pointer = parse_expression (input_line_pointer, &expn);
1694
1695 if (*input_line_pointer == ',')
1696 {
1697 ++input_line_pointer;
1698 size = get_absolute_expression ();
1699 if (size < 1 || size > 32)
1700 {
1701 as_bad (_("Invalid field size, must be from 1 to 32"));
1702 ignore_rest_of_line ();
1703 return;
1704 }
1705 }
1706
1707 /* Truncate values to the field width. */
1708 if (expn.X_op != O_constant)
1709 {
1710 /* If the expression value is relocatable, the field size *must*
1711 be 16. */
1712 if (size != 16)
1713 {
1714 as_bad (_("field size must be 16 when value is relocatable"));
1715 ignore_rest_of_line ();
1716 return;
1717 }
1718
1719 frag_now->tc_frag_data = 0;
1720 emit_expr (&expn, 2);
1721 }
1722 else
1723 {
1724 unsigned long fmask = (size == 32) ? 0xFFFFFFFF : (1ul << size) - 1;
1725
1726 value = expn.X_add_number;
1727 expn.X_add_number &= fmask;
1728 if (value != (valueT) expn.X_add_number)
1729 as_warn (_("field value truncated"));
1730 value = expn.X_add_number;
1731 /* Bits are stored MS first. */
1732 while (size >= 16)
1733 {
1734 frag_now->tc_frag_data = 0;
1735 p = frag_more (2);
1736 md_number_to_chars (p, (value >> (size - 16)) & 0xFFFF, 2);
1737 size -= 16;
1738 }
1739 if (size > 0)
1740 {
1741 int bit_offset = frag_bit_offset (frag_now, now_seg);
1742
1743 fragS *alloc_frag = bit_offset_frag (frag_now, now_seg);
1744 if (bit_offset == -1)
1745 {
1746 struct bit_info *bi = XNEW (struct bit_info);
1747 /* We don't know the previous offset at this time, so store the
1748 info we need and figure it out later. */
1749 expressionS size_exp;
1750
1751 size_exp.X_op = O_constant;
1752 size_exp.X_add_number = size;
1753 bi->seg = now_seg;
1754 bi->type = TYPE_FIELD;
1755 bi->value = value;
1756 p = frag_var (rs_machine_dependent,
1757 4, 1, (relax_substateT) 0,
1758 make_expr_symbol (&size_exp), (offsetT) 0,
1759 (char *) bi);
1760 goto getout;
1761 }
1762 else if (bit_offset == 0 || bit_offset + size > 16)
1763 {
1764 /* Align a new field. */
1765 p = frag_more (2);
1766 frag_now->tc_frag_data = 0;
1767 alloc_frag = frag_now;
1768 }
1769 else
1770 {
1771 /* Put the new value entirely within the existing one. */
1772 p = alloc_frag == frag_now ?
1773 frag_now->fr_literal + frag_now_fix_octets () - 2 :
1774 alloc_frag->fr_literal;
1775 if (label != NULL)
1776 {
1777 symbol_set_frag (label, alloc_frag);
1778 if (alloc_frag == frag_now)
1779 S_SET_VALUE (label, frag_now_fix () - 1);
1780 label = NULL;
1781 }
1782 }
1783 value <<= 16 - alloc_frag->tc_frag_data - size;
1784
1785 /* OR in existing value. */
1786 if (alloc_frag->tc_frag_data)
1787 value |= ((unsigned short) p[1] << 8) | p[0];
1788 md_number_to_chars (p, value, 2);
1789 alloc_frag->tc_frag_data += size;
1790 if (alloc_frag->tc_frag_data == 16)
1791 alloc_frag->tc_frag_data = 0;
1792 }
1793 }
1794 getout:
1795 demand_empty_rest_of_line ();
1796 }
1797
1798 /* Ideally, we want to check SEC_LOAD and SEC_HAS_CONTENTS, but those aren't
1799 available yet. seg_info ()->bss is the next best thing. */
1800
1801 static int
1802 tic54x_initialized_section (segT seg)
1803 {
1804 return !seg_info (seg)->bss;
1805 }
1806
1807 /* .clink ["section name"]
1808
1809 Marks the section as conditionally linked (link only if contents are
1810 referenced elsewhere.
1811 Without a name, refers to the current initialized section.
1812 Name is required for uninitialized sections. */
1813
1814 static void
1815 tic54x_clink (int ignored ATTRIBUTE_UNUSED)
1816 {
1817 segT seg = now_seg;
1818
1819 ILLEGAL_WITHIN_STRUCT ();
1820
1821 if (*input_line_pointer == '\"')
1822 {
1823 char *section_name = ++input_line_pointer;
1824 char *name;
1825
1826 while (is_a_char (next_char_of_string ()))
1827 ;
1828 know (input_line_pointer[-1] == '\"');
1829 input_line_pointer[-1] = 0;
1830 name = xstrdup (section_name);
1831
1832 seg = bfd_get_section_by_name (stdoutput, name);
1833 if (seg == NULL)
1834 {
1835 as_bad (_("Unrecognized section '%s'"), section_name);
1836 ignore_rest_of_line ();
1837 return;
1838 }
1839 }
1840 else
1841 {
1842 if (!tic54x_initialized_section (seg))
1843 {
1844 as_bad (_("Current section is uninitialized, "
1845 "section name required for .clink"));
1846 ignore_rest_of_line ();
1847 return;
1848 }
1849 }
1850
1851 seg->flags |= SEC_TIC54X_CLINK;
1852
1853 demand_empty_rest_of_line ();
1854 }
1855
1856 /* Change the default include directory to be the current source file's
1857 directory, instead of the current working directory. If DOT is non-zero,
1858 set to "." instead. */
1859
1860 static void
1861 tic54x_set_default_include (void)
1862 {
1863 char *dir, *tmp = NULL;
1864 const char *curfile;
1865 unsigned lineno;
1866
1867 curfile = as_where (&lineno);
1868 dir = xstrdup (curfile);
1869 tmp = strrchr (dir, '/');
1870 if (tmp != NULL)
1871 {
1872 int len;
1873
1874 *tmp = '\0';
1875 len = strlen (dir);
1876 if (include_dir_count == 0)
1877 {
1878 include_dirs = XNEWVEC (const char *, 1);
1879 include_dir_count = 1;
1880 }
1881 include_dirs[0] = dir;
1882 if (len > include_dir_maxlen)
1883 include_dir_maxlen = len;
1884 }
1885 else if (include_dirs != NULL)
1886 include_dirs[0] = ".";
1887 }
1888
1889 /* .include "filename" | filename
1890 .copy "filename" | filename
1891
1892 FIXME 'include' file should be omitted from any output listing,
1893 'copy' should be included in any output listing
1894 FIXME -- prevent any included files from changing listing (compat only)
1895 FIXME -- need to include source file directory in search path; what's a
1896 good way to do this?
1897
1898 Entering/exiting included/copied file clears all local labels. */
1899
1900 static void
1901 tic54x_include (int ignored ATTRIBUTE_UNUSED)
1902 {
1903 char newblock[] = " .newblock\n";
1904 char *filename;
1905 char *input;
1906 int len, c = -1;
1907
1908 ILLEGAL_WITHIN_STRUCT ();
1909
1910 SKIP_WHITESPACE ();
1911
1912 if (*input_line_pointer == '"')
1913 {
1914 filename = demand_copy_C_string (&len);
1915 demand_empty_rest_of_line ();
1916 }
1917 else
1918 {
1919 filename = input_line_pointer;
1920 while (!is_end_of_line[(unsigned char) *input_line_pointer])
1921 ++input_line_pointer;
1922 c = *input_line_pointer;
1923 *input_line_pointer = '\0';
1924 filename = xstrdup (filename);
1925 *input_line_pointer = c;
1926 demand_empty_rest_of_line ();
1927 }
1928 /* Insert a partial line with the filename (for the sake of s_include)
1929 and a .newblock.
1930 The included file will be inserted before the newblock, so that the
1931 newblock is executed after the included file is processed. */
1932 input = concat ("\"", filename, "\"\n", newblock, (char *) NULL);
1933 input_scrub_insert_line (input);
1934
1935 tic54x_clear_local_labels (0);
1936
1937 tic54x_set_default_include ();
1938
1939 s_include (0);
1940 }
1941
1942 static void
1943 tic54x_message (int type)
1944 {
1945 char *msg;
1946 char c;
1947 int len;
1948
1949 ILLEGAL_WITHIN_STRUCT ();
1950
1951 if (*input_line_pointer == '"')
1952 msg = demand_copy_C_string (&len);
1953 else
1954 {
1955 msg = input_line_pointer;
1956 while (!is_end_of_line[(unsigned char) *input_line_pointer])
1957 ++input_line_pointer;
1958 c = *input_line_pointer;
1959 *input_line_pointer = 0;
1960 msg = xstrdup (msg);
1961 *input_line_pointer = c;
1962 }
1963
1964 switch (type)
1965 {
1966 case 'm':
1967 as_tsktsk ("%s", msg);
1968 break;
1969 case 'w':
1970 as_warn ("%s", msg);
1971 break;
1972 case 'e':
1973 as_bad ("%s", msg);
1974 break;
1975 }
1976
1977 demand_empty_rest_of_line ();
1978 }
1979
1980 /* .label <symbol>
1981 Define a special symbol that refers to the loadtime address rather than the
1982 runtime address within the current section.
1983
1984 This symbol gets a special storage class so that when it is resolved, it is
1985 resolved relative to the load address (lma) of the section rather than the
1986 run address (vma). */
1987
1988 static void
1989 tic54x_label (int ignored ATTRIBUTE_UNUSED)
1990 {
1991 char *name;
1992 symbolS *symbolP;
1993 int c;
1994
1995 ILLEGAL_WITHIN_STRUCT ();
1996
1997 c = get_symbol_name (&name);
1998 symbolP = colon (name);
1999 S_SET_STORAGE_CLASS (symbolP, C_STATLAB);
2000
2001 (void) restore_line_pointer (c);
2002 demand_empty_rest_of_line ();
2003 }
2004
2005 /* .mmregs
2006 Install all memory-mapped register names into the symbol table as
2007 absolute local symbols. */
2008
2009 static void
2010 tic54x_mmregs (int ignored ATTRIBUTE_UNUSED)
2011 {
2012 tic54x_symbol *sym;
2013
2014 ILLEGAL_WITHIN_STRUCT ();
2015
2016 for (sym = (tic54x_symbol *) mmregs; sym->name; sym++)
2017 {
2018 symbolS *symbolP = symbol_new (sym->name, absolute_section,
2019 (valueT) sym->value, &zero_address_frag);
2020 SF_SET_LOCAL (symbolP);
2021 symbol_table_insert (symbolP);
2022 }
2023 }
2024
2025 /* .loop [count]
2026 Count defaults to 1024. */
2027
2028 static void
2029 tic54x_loop (int count)
2030 {
2031 ILLEGAL_WITHIN_STRUCT ();
2032
2033 SKIP_WHITESPACE ();
2034 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2035 count = get_absolute_expression ();
2036
2037 do_repeat ((size_t) count, "LOOP", "ENDLOOP");
2038 }
2039
2040 /* Normally, endloop gets eaten by the preceding loop. */
2041
2042 static void
2043 tic54x_endloop (int ignore ATTRIBUTE_UNUSED)
2044 {
2045 as_bad (_("ENDLOOP without corresponding LOOP"));
2046 ignore_rest_of_line ();
2047 }
2048
2049 /* .break [condition]. */
2050
2051 static void
2052 tic54x_break (int ignore ATTRIBUTE_UNUSED)
2053 {
2054 int cond = 1;
2055
2056 ILLEGAL_WITHIN_STRUCT ();
2057
2058 SKIP_WHITESPACE ();
2059 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2060 cond = get_absolute_expression ();
2061
2062 if (cond)
2063 end_repeat (substitution_line ? 1 : 0);
2064 }
2065
2066 static void
2067 set_address_mode (int mode)
2068 {
2069 amode = mode;
2070 if (mode == far_mode)
2071 {
2072 symbolS *symbolP = symbol_new ("__allow_far", absolute_section,
2073 (valueT) 1, &zero_address_frag);
2074 SF_SET_LOCAL (symbolP);
2075 symbol_table_insert (symbolP);
2076 }
2077 }
2078
2079 static int address_mode_needs_set = 1;
2080
2081 static void
2082 tic54x_address_mode (int mode)
2083 {
2084 if (assembly_begun && amode != (unsigned) mode)
2085 {
2086 as_bad (_("Mixing of normal and extended addressing not supported"));
2087 ignore_rest_of_line ();
2088 return;
2089 }
2090 if (mode == far_mode && cpu != VNONE && cpu != V548 && cpu != V549)
2091 {
2092 as_bad (_("Extended addressing not supported on the specified CPU"));
2093 ignore_rest_of_line ();
2094 return;
2095 }
2096
2097 set_address_mode (mode);
2098 demand_empty_rest_of_line ();
2099 }
2100
2101 /* .sblock "section"|section [,...,"section"|section]
2102 Designate initialized sections for blocking. */
2103
2104 static void
2105 tic54x_sblock (int ignore ATTRIBUTE_UNUSED)
2106 {
2107 int c = ',';
2108
2109 ILLEGAL_WITHIN_STRUCT ();
2110
2111 while (c == ',')
2112 {
2113 segT seg;
2114 char *name;
2115
2116 if (*input_line_pointer == '"')
2117 {
2118 int len;
2119
2120 name = demand_copy_C_string (&len);
2121 }
2122 else
2123 {
2124 char *section_name;
2125
2126 c = get_symbol_name (&section_name);
2127 name = xstrdup (section_name);
2128 (void) restore_line_pointer (c);
2129 }
2130
2131 seg = bfd_get_section_by_name (stdoutput, name);
2132 if (seg == NULL)
2133 {
2134 as_bad (_("Unrecognized section '%s'"), name);
2135 ignore_rest_of_line ();
2136 return;
2137 }
2138 else if (!tic54x_initialized_section (seg))
2139 {
2140 as_bad (_(".sblock may be used for initialized sections only"));
2141 ignore_rest_of_line ();
2142 return;
2143 }
2144 seg->flags |= SEC_TIC54X_BLOCK;
2145
2146 c = *input_line_pointer;
2147 if (!is_end_of_line[(unsigned char) c])
2148 ++input_line_pointer;
2149 }
2150
2151 demand_empty_rest_of_line ();
2152 }
2153
2154 /* symbol .set value
2155 symbol .equ value
2156
2157 value must be defined externals; no forward-referencing allowed
2158 symbols assigned with .set/.equ may not be redefined. */
2159
2160 static void
2161 tic54x_set (int ignore ATTRIBUTE_UNUSED)
2162 {
2163 symbolS *symbolP;
2164 char *name;
2165
2166 ILLEGAL_WITHIN_STRUCT ();
2167
2168 if (!line_label)
2169 {
2170 as_bad (_("Symbol missing for .set/.equ"));
2171 ignore_rest_of_line ();
2172 return;
2173 }
2174 name = xstrdup (S_GET_NAME (line_label));
2175 line_label = NULL;
2176 if ((symbolP = symbol_find (name)) == NULL
2177 && (symbolP = md_undefined_symbol (name)) == NULL)
2178 {
2179 symbolP = symbol_new (name, absolute_section, 0, &zero_address_frag);
2180 S_SET_STORAGE_CLASS (symbolP, C_STAT);
2181 }
2182 free (name);
2183 S_SET_DATA_TYPE (symbolP, T_INT);
2184 S_SET_SEGMENT (symbolP, absolute_section);
2185 symbol_table_insert (symbolP);
2186 pseudo_set (symbolP);
2187 demand_empty_rest_of_line ();
2188 }
2189
2190 /* .fclist
2191 .fcnolist
2192 List false conditional blocks. */
2193
2194 static void
2195 tic54x_fclist (int show)
2196 {
2197 if (show)
2198 listing &= ~LISTING_NOCOND;
2199 else
2200 listing |= LISTING_NOCOND;
2201 demand_empty_rest_of_line ();
2202 }
2203
2204 static void
2205 tic54x_sslist (int show)
2206 {
2207 ILLEGAL_WITHIN_STRUCT ();
2208
2209 listing_sslist = show;
2210 }
2211
2212 /* .var SYM[,...,SYMN]
2213 Define a substitution string to be local to a macro. */
2214
2215 static void
2216 tic54x_var (int ignore ATTRIBUTE_UNUSED)
2217 {
2218 static char empty[] = "";
2219 char *name;
2220 int c;
2221
2222 ILLEGAL_WITHIN_STRUCT ();
2223
2224 if (macro_level == 0)
2225 {
2226 as_bad (_(".var may only be used within a macro definition"));
2227 ignore_rest_of_line ();
2228 return;
2229 }
2230 do
2231 {
2232 if (!ISALPHA (*input_line_pointer))
2233 {
2234 as_bad (_("Substitution symbols must begin with a letter"));
2235 ignore_rest_of_line ();
2236 return;
2237 }
2238 c = get_symbol_name (&name);
2239 /* .var symbols start out with a null string. */
2240 name = xstrdup (name);
2241 str_hash_insert (subsym_hash[macro_level], name, empty);
2242 c = restore_line_pointer (c);
2243 if (c == ',')
2244 {
2245 ++input_line_pointer;
2246 if (is_end_of_line[(unsigned char) *input_line_pointer])
2247 c = *input_line_pointer;
2248 }
2249 }
2250 while (c == ',');
2251
2252 demand_empty_rest_of_line ();
2253 }
2254
2255 /* .mlib <macro library filename>
2256
2257 Macro libraries are archived (standard AR-format) text macro definitions
2258 Expand the file and include it.
2259
2260 FIXME need to try the source file directory as well. */
2261
2262 static void
2263 tic54x_mlib (int ignore ATTRIBUTE_UNUSED)
2264 {
2265 char *filename;
2266 char *path;
2267 int len, i;
2268 bfd *abfd, *mbfd;
2269
2270 ILLEGAL_WITHIN_STRUCT ();
2271
2272 /* Parse the filename. */
2273 if (*input_line_pointer == '"')
2274 {
2275 if ((filename = demand_copy_C_string (&len)) == NULL)
2276 return;
2277 }
2278 else
2279 {
2280 SKIP_WHITESPACE ();
2281 len = 0;
2282 while (!is_end_of_line[(unsigned char) *input_line_pointer]
2283 && !ISSPACE (*input_line_pointer))
2284 {
2285 obstack_1grow (&notes, *input_line_pointer);
2286 ++input_line_pointer;
2287 ++len;
2288 }
2289 obstack_1grow (&notes, '\0');
2290 filename = obstack_finish (&notes);
2291 }
2292 demand_empty_rest_of_line ();
2293
2294 tic54x_set_default_include ();
2295 path = XNEWVEC (char, (unsigned long) len + include_dir_maxlen + 5);
2296
2297 for (i = 0; i < include_dir_count; i++)
2298 {
2299 FILE *try;
2300
2301 strcpy (path, include_dirs[i]);
2302 strcat (path, "/");
2303 strcat (path, filename);
2304 if ((try = fopen (path, "r")) != NULL)
2305 {
2306 fclose (try);
2307 break;
2308 }
2309 }
2310
2311 if (i >= include_dir_count)
2312 {
2313 free (path);
2314 path = filename;
2315 }
2316
2317 /* FIXME: if path is found, malloc'd storage is not freed. Of course, this
2318 happens all over the place, and since the assembler doesn't usually keep
2319 running for a very long time, it really doesn't matter. */
2320 register_dependency (path);
2321
2322 /* Expand all archive entries to temporary files and include them. */
2323 abfd = bfd_openr (path, NULL);
2324 if (!abfd)
2325 {
2326 as_bad (_("can't open macro library file '%s' for reading: %s"),
2327 path, bfd_errmsg (bfd_get_error ()));
2328 ignore_rest_of_line ();
2329 return;
2330 }
2331 if (!bfd_check_format (abfd, bfd_archive))
2332 {
2333 as_bad (_("File '%s' not in macro archive format"), path);
2334 ignore_rest_of_line ();
2335 return;
2336 }
2337
2338 /* Open each BFD as binary (it should be straight ASCII text). */
2339 for (mbfd = bfd_openr_next_archived_file (abfd, NULL);
2340 mbfd != NULL; mbfd = bfd_openr_next_archived_file (abfd, mbfd))
2341 {
2342 /* Get a size at least as big as the archive member. */
2343 bfd_size_type size = bfd_get_size (mbfd);
2344 char *buf = XNEWVEC (char, size);
2345 char *fname = tmpnam (NULL);
2346 FILE *ftmp;
2347
2348 /* We're not sure how big it is, but it will be smaller than "size". */
2349 size = bfd_bread (buf, size, mbfd);
2350
2351 /* Write to a temporary file, then use s_include to include it
2352 a bit of a hack. */
2353 ftmp = fopen (fname, "w+b");
2354 fwrite ((void *) buf, size, 1, ftmp);
2355 if (size == 0 || buf[size - 1] != '\n')
2356 fwrite ("\n", 1, 1, ftmp);
2357 fclose (ftmp);
2358 free (buf);
2359 input_scrub_insert_file (fname);
2360 unlink (fname);
2361 }
2362 }
2363
2364 const pseudo_typeS md_pseudo_table[] =
2365 {
2366 { "algebraic", s_ignore , 0 },
2367 { "align" , tic54x_align_words , 128 },
2368 { "ascii" , tic54x_stringer , 'p' },
2369 { "asciz" , tic54x_stringer , 'P' },
2370 { "even" , tic54x_align_words , 2 },
2371 { "asg" , tic54x_asg , 0 },
2372 { "eval" , tic54x_eval , 0 },
2373 { "bss" , tic54x_bss , 0 },
2374 { "byte" , tic54x_cons , 'b' },
2375 { "ubyte" , tic54x_cons , 'B' },
2376 { "char" , tic54x_cons , 'c' },
2377 { "uchar" , tic54x_cons , 'C' },
2378 { "clink" , tic54x_clink , 0 },
2379 { "c_mode" , tic54x_address_mode , c_mode },
2380 { "copy" , tic54x_include , 'c' },
2381 { "include" , tic54x_include , 'i' },
2382 { "data" , tic54x_sect , 'd' },
2383 { "double" , tic54x_float_cons , 'd' },
2384 { "ldouble" , tic54x_float_cons , 'l' },
2385 { "drlist" , s_ignore , 0 },
2386 { "drnolist" , s_ignore , 0 },
2387 { "emsg" , tic54x_message , 'e' },
2388 { "mmsg" , tic54x_message , 'm' },
2389 { "wmsg" , tic54x_message , 'w' },
2390 { "far_mode" , tic54x_address_mode , far_mode },
2391 { "fclist" , tic54x_fclist , 1 },
2392 { "fcnolist" , tic54x_fclist , 0 },
2393 { "field" , tic54x_field , -1 },
2394 { "float" , tic54x_float_cons , 'f' },
2395 { "xfloat" , tic54x_float_cons , 'x' },
2396 { "global" , tic54x_global , 'g' },
2397 { "def" , tic54x_global , 'd' },
2398 { "ref" , tic54x_global , 'r' },
2399 { "half" , tic54x_cons , 'h' },
2400 { "uhalf" , tic54x_cons , 'H' },
2401 { "short" , tic54x_cons , 's' },
2402 { "ushort" , tic54x_cons , 'S' },
2403 { "if" , s_if , (int) O_ne },
2404 { "elseif" , s_elseif , (int) O_ne },
2405 { "else" , s_else , 0 },
2406 { "endif" , s_endif , 0 },
2407 { "int" , tic54x_cons , 'i' },
2408 { "uint" , tic54x_cons , 'I' },
2409 { "word" , tic54x_cons , 'w' },
2410 { "uword" , tic54x_cons , 'W' },
2411 { "label" , tic54x_label , 0 }, /* Loadtime
2412 address. */
2413 { "length" , s_ignore , 0 },
2414 { "width" , s_ignore , 0 },
2415 { "long" , tic54x_cons , 'l' },
2416 { "ulong" , tic54x_cons , 'L' },
2417 { "xlong" , tic54x_cons , 'x' },
2418 { "loop" , tic54x_loop , 1024 },
2419 { "break" , tic54x_break , 0 },
2420 { "endloop" , tic54x_endloop , 0 },
2421 { "mlib" , tic54x_mlib , 0 },
2422 { "mlist" , s_ignore , 0 },
2423 { "mnolist" , s_ignore , 0 },
2424 { "mmregs" , tic54x_mmregs , 0 },
2425 { "newblock" , tic54x_clear_local_labels, 0 },
2426 { "option" , s_ignore , 0 },
2427 { "p2align" , tic54x_p2align , 0 },
2428 { "sblock" , tic54x_sblock , 0 },
2429 { "sect" , tic54x_sect , '*' },
2430 { "set" , tic54x_set , 0 },
2431 { "equ" , tic54x_set , 0 },
2432 { "space" , tic54x_space , 0 },
2433 { "bes" , tic54x_space , 1 },
2434 { "sslist" , tic54x_sslist , 1 },
2435 { "ssnolist" , tic54x_sslist , 0 },
2436 { "string" , tic54x_stringer , 's' },
2437 { "pstring" , tic54x_stringer , 'p' },
2438 { "struct" , tic54x_struct , 0 },
2439 { "tag" , tic54x_tag , 0 },
2440 { "endstruct", tic54x_endstruct , 0 },
2441 { "tab" , s_ignore , 0 },
2442 { "text" , tic54x_sect , 't' },
2443 { "union" , tic54x_struct , 1 },
2444 { "endunion" , tic54x_endstruct , 1 },
2445 { "usect" , tic54x_usect , 0 },
2446 { "var" , tic54x_var , 0 },
2447 { "version" , tic54x_version , 0 },
2448 {0 , 0 , 0 }
2449 };
2450
2451 int
2452 md_parse_option (int c, const char *arg)
2453 {
2454 switch (c)
2455 {
2456 default:
2457 return 0;
2458 case OPTION_COFF_VERSION:
2459 {
2460 int version = atoi (arg);
2461
2462 if (version != 0 && version != 1 && version != 2)
2463 as_fatal (_("Bad COFF version '%s'"), arg);
2464 /* FIXME -- not yet implemented. */
2465 break;
2466 }
2467 case OPTION_CPU_VERSION:
2468 {
2469 cpu = lookup_version (arg);
2470 cpu_needs_set = 1;
2471 if (cpu == VNONE)
2472 as_fatal (_("Bad CPU version '%s'"), arg);
2473 break;
2474 }
2475 case OPTION_ADDRESS_MODE:
2476 amode = far_mode;
2477 address_mode_needs_set = 1;
2478 break;
2479 case OPTION_STDERR_TO_FILE:
2480 {
2481 const char *filename = arg;
2482 FILE *fp = fopen (filename, "w+");
2483
2484 if (fp == NULL)
2485 as_fatal (_("Can't redirect stderr to the file '%s'"), filename);
2486 fclose (fp);
2487 if ((fp = freopen (filename, "w+", stderr)) == NULL)
2488 as_fatal (_("Can't redirect stderr to the file '%s'"), filename);
2489 break;
2490 }
2491 }
2492
2493 return 1;
2494 }
2495
2496 /* Create a "local" substitution string hash table for a new macro level
2497 Some docs imply that macros have to use .newblock in order to be able
2498 to re-use a local label. We effectively do an automatic .newblock by
2499 deleting the local label hash between macro invocations. */
2500
2501 void
2502 tic54x_macro_start (void)
2503 {
2504 if (++macro_level >= MAX_SUBSYM_HASH)
2505 {
2506 as_fatal (_("Macro nesting is too deep"));
2507 return;
2508 }
2509 subsym_hash[macro_level] = str_htab_create ();
2510 local_label_hash[macro_level] = str_htab_create ();
2511 }
2512
2513 void
2514 tic54x_macro_info (const macro_entry *macro)
2515 {
2516 const formal_entry *entry;
2517
2518 /* Put the formal args into the substitution symbol table. */
2519 for (entry = macro->formals; entry; entry = entry->next)
2520 {
2521 char *name = xstrndup (entry->name.ptr, entry->name.len);
2522 char *value = xstrndup (entry->actual.ptr, entry->actual.len);
2523
2524 name[entry->name.len] = '\0';
2525 value[entry->actual.len] = '\0';
2526 str_hash_insert (subsym_hash[macro_level], name, value);
2527 }
2528 }
2529
2530 /* Get rid of this macro's .var's, arguments, and local labels. */
2531
2532 void
2533 tic54x_macro_end (void)
2534 {
2535 htab_delete (subsym_hash[macro_level]);
2536 subsym_hash[macro_level] = NULL;
2537 htab_delete (local_label_hash[macro_level]);
2538 local_label_hash[macro_level] = NULL;
2539 --macro_level;
2540 }
2541
2542 static int
2543 subsym_symlen (char *a, char *ignore ATTRIBUTE_UNUSED)
2544 {
2545 return strlen (a);
2546 }
2547
2548 /* Compare symbol A to string B. */
2549
2550 static int
2551 subsym_symcmp (char *a, char *b)
2552 {
2553 return strcmp (a, b);
2554 }
2555
2556 /* Return the index of the first occurrence of B in A, or zero if none
2557 assumes b is an integer char value as a string. Index is one-based. */
2558
2559 static int
2560 subsym_firstch (char *a, char *b)
2561 {
2562 int val = atoi (b);
2563 char *tmp = strchr (a, val);
2564
2565 return tmp ? tmp - a + 1 : 0;
2566 }
2567
2568 /* Similar to firstch, but returns index of last occurrence of B in A. */
2569
2570 static int
2571 subsym_lastch (char *a, char *b)
2572 {
2573 int val = atoi (b);
2574 char *tmp = strrchr (a, val);
2575
2576 return tmp ? tmp - a + 1 : 0;
2577 }
2578
2579 /* Returns 1 if string A is defined in the symbol table (NOT the substitution
2580 symbol table). */
2581
2582 static int
2583 subsym_isdefed (char *a, char *ignore ATTRIBUTE_UNUSED)
2584 {
2585 symbolS *symbolP = symbol_find (a);
2586
2587 return symbolP != NULL;
2588 }
2589
2590 /* Assign first member of comma-separated list B (e.g. "1,2,3") to the symbol
2591 A, or zero if B is a null string. Both arguments *must* be substitution
2592 symbols, unsubstituted. */
2593
2594 static int
2595 subsym_ismember (char *sym, char *list)
2596 {
2597 char *elem, *ptr, *listv;
2598
2599 if (!list)
2600 return 0;
2601
2602 listv = subsym_lookup (list, macro_level);
2603 if (!listv)
2604 {
2605 as_bad (_("Undefined substitution symbol '%s'"), list);
2606 ignore_rest_of_line ();
2607 return 0;
2608 }
2609
2610 ptr = elem = xstrdup (listv);
2611 while (*ptr && *ptr != ',')
2612 ++ptr;
2613 *ptr++ = 0;
2614
2615 subsym_create_or_replace (sym, elem);
2616
2617 /* Reassign the list. */
2618 subsym_create_or_replace (list, ptr);
2619
2620 /* Assume this value, docs aren't clear. */
2621 return *list != 0;
2622 }
2623
2624 /* Return zero if not a constant; otherwise:
2625 1 if binary
2626 2 if octal
2627 3 if hexadecimal
2628 4 if character
2629 5 if decimal. */
2630
2631 static int
2632 subsym_iscons (char *a, char *ignore ATTRIBUTE_UNUSED)
2633 {
2634 expressionS expn;
2635
2636 parse_expression (a, &expn);
2637
2638 if (expn.X_op == O_constant)
2639 {
2640 int len = strlen (a);
2641
2642 switch (TOUPPER (a[len - 1]))
2643 {
2644 case 'B':
2645 return 1;
2646 case 'Q':
2647 return 2;
2648 case 'H':
2649 return 3;
2650 case '\'':
2651 return 4;
2652 default:
2653 break;
2654 }
2655 /* No suffix; either octal, hex, or decimal. */
2656 if (*a == '0' && len > 1)
2657 {
2658 if (TOUPPER (a[1]) == 'X')
2659 return 3;
2660 return 2;
2661 }
2662 return 5;
2663 }
2664
2665 return 0;
2666 }
2667
2668 /* Return 1 if A is a valid symbol name. Expects string input. */
2669
2670 static int
2671 subsym_isname (char *a, char *ignore ATTRIBUTE_UNUSED)
2672 {
2673 if (!is_name_beginner (*a))
2674 return 0;
2675 while (*a)
2676 {
2677 if (!is_part_of_name (*a))
2678 return 0;
2679 ++a;
2680 }
2681 return 1;
2682 }
2683
2684 /* Return whether the string is a register; accepts ar0-7, unless .mmregs has
2685 been seen; if so, recognize any memory-mapped register.
2686 Note this does not recognize "A" or "B" accumulators. */
2687
2688 static int
2689 subsym_isreg (char *a, char *ignore ATTRIBUTE_UNUSED)
2690 {
2691 if (str_hash_find (reg_hash, a))
2692 return 1;
2693 if (str_hash_find (mmreg_hash, a))
2694 return 1;
2695 return 0;
2696 }
2697
2698 /* Return the structure size, given the stag. */
2699
2700 static int
2701 subsym_structsz (char *name, char *ignore ATTRIBUTE_UNUSED)
2702 {
2703 struct stag *stag = (struct stag *) str_hash_find (stag_hash, name);
2704
2705 if (stag)
2706 return stag->size;
2707
2708 return 0;
2709 }
2710
2711 /* If anybody actually uses this, they can fix it :)
2712 FIXME I'm not sure what the "reference point" of a structure is. It might
2713 be either the initial offset given .struct, or it may be the offset of the
2714 structure within another structure, or it might be something else
2715 altogether. since the TI assembler doesn't seem to ever do anything but
2716 return zero, we punt and return zero. */
2717
2718 static int
2719 subsym_structacc (char *stag_name ATTRIBUTE_UNUSED,
2720 char *ignore ATTRIBUTE_UNUSED)
2721 {
2722 return 0;
2723 }
2724
2725 static float
2726 math_ceil (float arg1, float ignore ATTRIBUTE_UNUSED)
2727 {
2728 return (float) ceil (arg1);
2729 }
2730
2731 static float
2732 math_cvi (float arg1, float ignore ATTRIBUTE_UNUSED)
2733 {
2734 return (int) arg1;
2735 }
2736
2737 static float
2738 math_floor (float arg1, float ignore ATTRIBUTE_UNUSED)
2739 {
2740 return (float) floor (arg1);
2741 }
2742
2743 static float
2744 math_fmod (float arg1, float arg2)
2745 {
2746 return (int) arg1 % (int) arg2;
2747 }
2748
2749 static float
2750 math_int (float arg1, float ignore ATTRIBUTE_UNUSED)
2751 {
2752 return ((float) ((int) arg1)) == arg1;
2753 }
2754
2755 static float
2756 math_round (float arg1, float ignore ATTRIBUTE_UNUSED)
2757 {
2758 return arg1 > 0 ? (int) (arg1 + 0.5) : (int) (arg1 - 0.5);
2759 }
2760
2761 static float
2762 math_sgn (float arg1, float ignore ATTRIBUTE_UNUSED)
2763 {
2764 return (arg1 < 0) ? -1 : (arg1 ? 1 : 0);
2765 }
2766
2767 static float
2768 math_trunc (float arg1, float ignore ATTRIBUTE_UNUSED)
2769 {
2770 return (int) arg1;
2771 }
2772
2773 static float
2774 math_acos (float arg1, float ignore ATTRIBUTE_UNUSED)
2775 {
2776 return (float) acos (arg1);
2777 }
2778
2779 static float
2780 math_asin (float arg1, float ignore ATTRIBUTE_UNUSED)
2781 {
2782 return (float) asin (arg1);
2783 }
2784
2785 static float
2786 math_atan (float arg1, float ignore ATTRIBUTE_UNUSED)
2787 {
2788 return (float) atan (arg1);
2789 }
2790
2791 static float
2792 math_atan2 (float arg1, float arg2)
2793 {
2794 return (float) atan2 (arg1, arg2);
2795 }
2796
2797 static float
2798 math_cosh (float arg1, float ignore ATTRIBUTE_UNUSED)
2799 {
2800 return (float) cosh (arg1);
2801 }
2802
2803 static float
2804 math_cos (float arg1, float ignore ATTRIBUTE_UNUSED)
2805 {
2806 return (float) cos (arg1);
2807 }
2808
2809 static float
2810 math_cvf (float arg1, float ignore ATTRIBUTE_UNUSED)
2811 {
2812 return (float) arg1;
2813 }
2814
2815 static float
2816 math_exp (float arg1, float ignore ATTRIBUTE_UNUSED)
2817 {
2818 return (float) exp (arg1);
2819 }
2820
2821 static float
2822 math_fabs (float arg1, float ignore ATTRIBUTE_UNUSED)
2823 {
2824 return (float) fabs (arg1);
2825 }
2826
2827 /* expr1 * 2^expr2. */
2828
2829 static float
2830 math_ldexp (float arg1, float arg2)
2831 {
2832 return arg1 * (float) pow (2.0, arg2);
2833 }
2834
2835 static float
2836 math_log10 (float arg1, float ignore ATTRIBUTE_UNUSED)
2837 {
2838 return (float) log10 (arg1);
2839 }
2840
2841 static float
2842 math_log (float arg1, float ignore ATTRIBUTE_UNUSED)
2843 {
2844 return (float) log (arg1);
2845 }
2846
2847 static float
2848 math_max (float arg1, float arg2)
2849 {
2850 return (arg1 > arg2) ? arg1 : arg2;
2851 }
2852
2853 static float
2854 math_min (float arg1, float arg2)
2855 {
2856 return (arg1 < arg2) ? arg1 : arg2;
2857 }
2858
2859 static float
2860 math_pow (float arg1, float arg2)
2861 {
2862 return (float) pow (arg1, arg2);
2863 }
2864
2865 static float
2866 math_sin (float arg1, float ignore ATTRIBUTE_UNUSED)
2867 {
2868 return (float) sin (arg1);
2869 }
2870
2871 static float
2872 math_sinh (float arg1, float ignore ATTRIBUTE_UNUSED)
2873 {
2874 return (float) sinh (arg1);
2875 }
2876
2877 static float
2878 math_sqrt (float arg1, float ignore ATTRIBUTE_UNUSED)
2879 {
2880 return (float) sqrt (arg1);
2881 }
2882
2883 static float
2884 math_tan (float arg1, float ignore ATTRIBUTE_UNUSED)
2885 {
2886 return (float) tan (arg1);
2887 }
2888
2889 static float
2890 math_tanh (float arg1, float ignore ATTRIBUTE_UNUSED)
2891 {
2892 return (float) tanh (arg1);
2893 }
2894
2895 /* Built-in substitution symbol functions and math functions. */
2896 typedef struct
2897 {
2898 const char *name;
2899 int (*proc) (char *, char *);
2900 int nargs;
2901 } subsym_proc_entry;
2902
2903 static const subsym_proc_entry subsym_procs[] =
2904 {
2905 /* Assembler built-in string substitution functions. */
2906 { "$symlen", subsym_symlen, 1, },
2907 { "$symcmp", subsym_symcmp, 2, },
2908 { "$firstch", subsym_firstch, 2, },
2909 { "$lastch", subsym_lastch, 2, },
2910 { "$isdefed", subsym_isdefed, 1, },
2911 { "$ismember", subsym_ismember, 2, },
2912 { "$iscons", subsym_iscons, 1, },
2913 { "$isname", subsym_isname, 1, },
2914 { "$isreg", subsym_isreg, 1, },
2915 { "$structsz", subsym_structsz, 1, },
2916 { "$structacc", subsym_structacc, 1, },
2917 { NULL, NULL, 0 },
2918 };
2919
2920 typedef struct
2921 {
2922 const char *name;
2923 float (*proc) (float, float);
2924 int nargs;
2925 int int_return;
2926 } math_proc_entry;
2927
2928 static const math_proc_entry math_procs[] =
2929 {
2930 /* Integer-returning built-in math functions. */
2931 { "$cvi", math_cvi, 1, 1 },
2932 { "$int", math_int, 1, 1 },
2933 { "$sgn", math_sgn, 1, 1 },
2934
2935 /* Float-returning built-in math functions. */
2936 { "$acos", math_acos, 1, 0 },
2937 { "$asin", math_asin, 1, 0 },
2938 { "$atan", math_atan, 1, 0 },
2939 { "$atan2", math_atan2, 2, 0 },
2940 { "$ceil", math_ceil, 1, 0 },
2941 { "$cosh", math_cosh, 1, 0 },
2942 { "$cos", math_cos, 1, 0 },
2943 { "$cvf", math_cvf, 1, 0 },
2944 { "$exp", math_exp, 1, 0 },
2945 { "$fabs", math_fabs, 1, 0 },
2946 { "$floor", math_floor, 1, 0 },
2947 { "$fmod", math_fmod, 2, 0 },
2948 { "$ldexp", math_ldexp, 2, 0 },
2949 { "$log10", math_log10, 1, 0 },
2950 { "$log", math_log, 1, 0 },
2951 { "$max", math_max, 2, 0 },
2952 { "$min", math_min, 2, 0 },
2953 { "$pow", math_pow, 2, 0 },
2954 { "$round", math_round, 1, 0 },
2955 { "$sin", math_sin, 1, 0 },
2956 { "$sinh", math_sinh, 1, 0 },
2957 { "$sqrt", math_sqrt, 1, 0 },
2958 { "$tan", math_tan, 1, 0 },
2959 { "$tanh", math_tanh, 1, 0 },
2960 { "$trunc", math_trunc, 1, 0 },
2961 { NULL, NULL, 0, 0 },
2962 };
2963
2964 void
2965 md_begin (void)
2966 {
2967 insn_template *tm;
2968 tic54x_symbol *sym;
2969 const subsym_proc_entry *subsym_proc;
2970 const math_proc_entry *math_proc;
2971 char **symname;
2972 char *TIC54X_DIR = getenv ("TIC54X_DIR");
2973 char *A_DIR = TIC54X_DIR ? TIC54X_DIR : getenv ("A_DIR");
2974
2975 local_label_id = 0;
2976
2977 /* Look for A_DIR and add it to the include list. */
2978 if (A_DIR != NULL)
2979 {
2980 char *tmp = xstrdup (A_DIR);
2981
2982 do
2983 {
2984 char *next = strchr (tmp, ';');
2985
2986 if (next)
2987 *next++ = '\0';
2988 add_include_dir (tmp);
2989 tmp = next;
2990 }
2991 while (tmp != NULL);
2992 }
2993
2994 op_hash = str_htab_create ();
2995 for (tm = (insn_template *) tic54x_optab; tm->name; tm++)
2996 {
2997 if (str_hash_find (op_hash, tm->name))
2998 continue;
2999 str_hash_insert (op_hash, tm->name, (char *) tm);
3000 }
3001 parop_hash = str_htab_create ();
3002 for (tm = (insn_template *) tic54x_paroptab; tm->name; tm++)
3003 {
3004 if (str_hash_find (parop_hash, tm->name))
3005 continue;
3006 str_hash_insert (parop_hash, tm->name, (char *) tm);
3007 }
3008 reg_hash = str_htab_create ();
3009 for (sym = (tic54x_symbol *) regs; sym->name; sym++)
3010 {
3011 /* Add basic registers to the symbol table. */
3012 symbolS *symbolP = symbol_new (sym->name, absolute_section,
3013 (valueT) sym->value, &zero_address_frag);
3014 SF_SET_LOCAL (symbolP);
3015 symbol_table_insert (symbolP);
3016 str_hash_insert (reg_hash, sym->name, (char *) sym);
3017 }
3018 for (sym = (tic54x_symbol *) mmregs; sym->name; sym++)
3019 str_hash_insert (reg_hash, sym->name, (char *) sym);
3020 mmreg_hash = str_htab_create ();
3021 for (sym = (tic54x_symbol *) mmregs; sym->name; sym++)
3022 str_hash_insert (mmreg_hash, sym->name, (char *) sym);
3023
3024 cc_hash = str_htab_create ();
3025 for (sym = (tic54x_symbol *) condition_codes; sym->name; sym++)
3026 str_hash_insert (cc_hash, sym->name, (char *) sym);
3027
3028 cc2_hash = str_htab_create ();
3029 for (sym = (tic54x_symbol *) cc2_codes; sym->name; sym++)
3030 str_hash_insert (cc2_hash, sym->name, (char *) sym);
3031
3032 cc3_hash = str_htab_create ();
3033 for (sym = (tic54x_symbol *) cc3_codes; sym->name; sym++)
3034 str_hash_insert (cc3_hash, sym->name, (char *) sym);
3035
3036 sbit_hash = str_htab_create ();
3037 for (sym = (tic54x_symbol *) status_bits; sym->name; sym++)
3038 str_hash_insert (sbit_hash, sym->name, (char *) sym);
3039
3040 misc_symbol_hash = str_htab_create ();
3041 for (symname = (char **) misc_symbols; *symname; symname++)
3042 str_hash_insert (misc_symbol_hash, *symname, *symname);
3043
3044 /* Only the base substitution table and local label table are initialized;
3045 the others (for local macro substitution) get instantiated as needed. */
3046 local_label_hash[0] = str_htab_create ();
3047 subsym_hash[0] = str_htab_create ();
3048 for (subsym_proc = subsym_procs; subsym_proc->name; subsym_proc++)
3049 str_hash_insert (subsym_hash[0], subsym_proc->name,
3050 (char *) subsym_proc);
3051
3052 math_hash = str_htab_create ();
3053 for (math_proc = math_procs; math_proc->name; math_proc++)
3054 {
3055 /* Insert into the main subsym hash for recognition; insert into
3056 the math hash to actually store information. */
3057 str_hash_insert (subsym_hash[0], math_proc->name,
3058 (char *) math_proc);
3059 str_hash_insert (math_hash, math_proc->name,
3060 (char *) math_proc);
3061 }
3062 subsym_recurse_hash = str_htab_create ();
3063 stag_hash = str_htab_create ();
3064 }
3065
3066 static int
3067 is_accumulator (struct opstruct *operand)
3068 {
3069 return strcasecmp (operand->buf, "a") == 0
3070 || strcasecmp (operand->buf, "b") == 0;
3071 }
3072
3073 /* Return the number of operands found, or -1 on error, copying the
3074 operands into the given array and the accompanying expressions into
3075 the next array. */
3076
3077 static int
3078 get_operands (struct opstruct operands[], char *line)
3079 {
3080 char *lptr = line;
3081 int numexp = 0;
3082 int expecting_operand = 0;
3083 int i;
3084
3085 while (numexp < MAX_OPERANDS && !is_end_of_line[(unsigned char) *lptr])
3086 {
3087 int paren_not_balanced = 0;
3088 char *op_start, *op_end;
3089
3090 while (*lptr && ISSPACE (*lptr))
3091 ++lptr;
3092 op_start = lptr;
3093 while (paren_not_balanced || *lptr != ',')
3094 {
3095 if (*lptr == '\0')
3096 {
3097 if (paren_not_balanced)
3098 {
3099 as_bad (_("Unbalanced parenthesis in operand %d"), numexp);
3100 return -1;
3101 }
3102 else
3103 break;
3104 }
3105 if (*lptr == '(')
3106 ++paren_not_balanced;
3107 else if (*lptr == ')')
3108 --paren_not_balanced;
3109 ++lptr;
3110 }
3111 op_end = lptr;
3112 if (op_end != op_start)
3113 {
3114 int len = op_end - op_start;
3115
3116 strncpy (operands[numexp].buf, op_start, len);
3117 operands[numexp].buf[len] = 0;
3118 /* Trim trailing spaces; while the preprocessor gets rid of most,
3119 there are weird usage patterns that can introduce them
3120 (i.e. using strings for macro args). */
3121 while (len > 0 && ISSPACE (operands[numexp].buf[len - 1]))
3122 operands[numexp].buf[--len] = 0;
3123 lptr = op_end;
3124 ++numexp;
3125 }
3126 else
3127 {
3128 if (expecting_operand || *lptr == ',')
3129 {
3130 as_bad (_("Expecting operand after ','"));
3131 return -1;
3132 }
3133 }
3134 if (*lptr == ',')
3135 {
3136 if (*++lptr == '\0')
3137 {
3138 as_bad (_("Expecting operand after ','"));
3139 return -1;
3140 }
3141 expecting_operand = 1;
3142 }
3143 }
3144
3145 while (*lptr && ISSPACE (*lptr++))
3146 ;
3147 if (!is_end_of_line[(unsigned char) *lptr])
3148 {
3149 as_bad (_("Extra junk on line"));
3150 return -1;
3151 }
3152
3153 /* OK, now parse them into expressions. */
3154 for (i = 0; i < numexp; i++)
3155 {
3156 memset (&operands[i].exp, 0, sizeof (operands[i].exp));
3157 if (operands[i].buf[0] == '#')
3158 {
3159 /* Immediate. */
3160 parse_expression (operands[i].buf + 1, &operands[i].exp);
3161 }
3162 else if (operands[i].buf[0] == '@')
3163 {
3164 /* Direct notation. */
3165 parse_expression (operands[i].buf + 1, &operands[i].exp);
3166 }
3167 else if (operands[i].buf[0] == '*')
3168 {
3169 /* Indirect. */
3170 char *paren = strchr (operands[i].buf, '(');
3171
3172 /* Allow immediate syntax in the inner expression. */
3173 if (paren && paren[1] == '#')
3174 *++paren = '(';
3175
3176 /* Pull out the lk expression or SP offset, if present. */
3177 if (paren != NULL)
3178 {
3179 int len = strlen (paren);
3180 char *end = paren + len;
3181 int c;
3182
3183 while (end[-1] != ')')
3184 if (--end <= paren)
3185 {
3186 as_bad (_("Badly formed address expression"));
3187 return -1;
3188 }
3189 c = *end;
3190 *end = '\0';
3191 parse_expression (paren, &operands[i].exp);
3192 *end = c;
3193 }
3194 else
3195 operands[i].exp.X_op = O_absent;
3196 }
3197 else
3198 parse_expression (operands[i].buf, &operands[i].exp);
3199 }
3200
3201 return numexp;
3202 }
3203
3204 /* Predicates for different operand types. */
3205
3206 static int
3207 is_immediate (struct opstruct *operand)
3208 {
3209 return *operand->buf == '#';
3210 }
3211
3212 /* This is distinguished from immediate because some numbers must be constants
3213 and must *not* have the '#' prefix. */
3214
3215 static int
3216 is_absolute (struct opstruct *operand)
3217 {
3218 return operand->exp.X_op == O_constant && !is_immediate (operand);
3219 }
3220
3221 /* Is this an indirect operand? */
3222
3223 static int
3224 is_indirect (struct opstruct *operand)
3225 {
3226 return operand->buf[0] == '*';
3227 }
3228
3229 /* Is this a valid dual-memory operand? */
3230
3231 static int
3232 is_dual (struct opstruct *operand)
3233 {
3234 if (is_indirect (operand) && strncasecmp (operand->buf, "*ar", 3) == 0)
3235 {
3236 char *tmp = operand->buf + 3;
3237 int arf;
3238 int valid_mod;
3239
3240 arf = *tmp++ - '0';
3241 /* Only allow *ARx, *ARx-, *ARx+, or *ARx+0%. */
3242 valid_mod = *tmp == '\0' ||
3243 strcasecmp (tmp, "-") == 0 ||
3244 strcasecmp (tmp, "+") == 0 ||
3245 strcasecmp (tmp, "+0%") == 0;
3246 return arf >= 2 && arf <= 5 && valid_mod;
3247 }
3248 return 0;
3249 }
3250
3251 static int
3252 is_mmreg (struct opstruct *operand)
3253 {
3254 return (is_absolute (operand)
3255 || is_immediate (operand)
3256 || str_hash_find (mmreg_hash, operand->buf) != 0);
3257 }
3258
3259 static int
3260 is_type (struct opstruct *operand, enum optype type)
3261 {
3262 switch (type)
3263 {
3264 case OP_None:
3265 return operand->buf[0] == 0;
3266 case OP_Xmem:
3267 case OP_Ymem:
3268 return is_dual (operand);
3269 case OP_Sind:
3270 return is_indirect (operand);
3271 case OP_xpmad_ms7:
3272 /* This one *must* be immediate. */
3273 return is_immediate (operand);
3274 case OP_xpmad:
3275 case OP_pmad:
3276 case OP_PA:
3277 case OP_dmad:
3278 case OP_Lmem:
3279 case OP_MMR:
3280 return 1;
3281 case OP_Smem:
3282 /* Address may be a numeric, indirect, or an expression. */
3283 return !is_immediate (operand);
3284 case OP_MMRY:
3285 case OP_MMRX:
3286 return is_mmreg (operand);
3287 case OP_SRC:
3288 case OP_SRC1:
3289 case OP_RND:
3290 case OP_DST:
3291 return is_accumulator (operand);
3292 case OP_B:
3293 return is_accumulator (operand) && TOUPPER (operand->buf[0]) == 'B';
3294 case OP_A:
3295 return is_accumulator (operand) && TOUPPER (operand->buf[0]) == 'A';
3296 case OP_ARX:
3297 return strncasecmp ("ar", operand->buf, 2) == 0
3298 && ISDIGIT (operand->buf[2]);
3299 case OP_SBIT:
3300 return str_hash_find (sbit_hash, operand->buf) != 0 || is_absolute (operand);
3301 case OP_CC:
3302 return str_hash_find (cc_hash, operand->buf) != 0;
3303 case OP_CC2:
3304 return str_hash_find (cc2_hash, operand->buf) != 0;
3305 case OP_CC3:
3306 return str_hash_find (cc3_hash, operand->buf) != 0
3307 || is_immediate (operand) || is_absolute (operand);
3308 case OP_16:
3309 return (is_immediate (operand) || is_absolute (operand))
3310 && operand->exp.X_add_number == 16;
3311 case OP_N:
3312 /* Allow st0 or st1 instead of a numeric. */
3313 return is_absolute (operand) || is_immediate (operand) ||
3314 strcasecmp ("st0", operand->buf) == 0 ||
3315 strcasecmp ("st1", operand->buf) == 0;
3316 case OP_12:
3317 case OP_123:
3318 return is_absolute (operand) || is_immediate (operand);
3319 case OP_SHFT:
3320 return (is_immediate (operand) || is_absolute (operand))
3321 && operand->exp.X_add_number >= 0 && operand->exp.X_add_number < 16;
3322 case OP_SHIFT:
3323 /* Let this one catch out-of-range values. */
3324 return (is_immediate (operand) || is_absolute (operand))
3325 && operand->exp.X_add_number != 16;
3326 case OP_BITC:
3327 case OP_031:
3328 case OP_k8:
3329 return is_absolute (operand) || is_immediate (operand);
3330 case OP_k8u:
3331 return is_immediate (operand)
3332 && operand->exp.X_op == O_constant
3333 && operand->exp.X_add_number >= 0
3334 && operand->exp.X_add_number < 256;
3335 case OP_lk:
3336 case OP_lku:
3337 /* Allow anything; assumes opcodes are ordered with Smem operands
3338 versions first. */
3339 return 1;
3340 case OP_k5:
3341 case OP_k3:
3342 case OP_k9:
3343 /* Just make sure it's an integer; check range later. */
3344 return is_immediate (operand);
3345 case OP_T:
3346 return strcasecmp ("t", operand->buf) == 0 ||
3347 strcasecmp ("treg", operand->buf) == 0;
3348 case OP_TS:
3349 return strcasecmp ("ts", operand->buf) == 0;
3350 case OP_ASM:
3351 return strcasecmp ("asm", operand->buf) == 0;
3352 case OP_TRN:
3353 return strcasecmp ("trn", operand->buf) == 0;
3354 case OP_DP:
3355 return strcasecmp ("dp", operand->buf) == 0;
3356 case OP_ARP:
3357 return strcasecmp ("arp", operand->buf) == 0;
3358 default:
3359 return 0;
3360 }
3361 }
3362
3363 static int
3364 operands_match (tic54x_insn *insn,
3365 struct opstruct *operands,
3366 int opcount,
3367 const enum optype *refoptype,
3368 int minops,
3369 int maxops)
3370 {
3371 int op = 0, refop = 0;
3372
3373 if (opcount == 0 && minops == 0)
3374 return 1;
3375
3376 while (op <= maxops && refop <= maxops)
3377 {
3378 while (!is_type (&operands[op], OPTYPE (refoptype[refop])))
3379 {
3380 /* Skip an optional template operand if it doesn't agree
3381 with the current operand. */
3382 if (refoptype[refop] & OPT)
3383 {
3384 ++refop;
3385 --maxops;
3386 if (refop > maxops)
3387 return 0;
3388 }
3389 else
3390 return 0;
3391 }
3392
3393 /* Save the actual operand type for later use. */
3394 operands[op].type = OPTYPE (refoptype[refop]);
3395 ++refop;
3396 ++op;
3397 /* Have we matched them all yet? */
3398 if (op == opcount)
3399 {
3400 while (op < maxops)
3401 {
3402 /* If a later operand is *not* optional, no match. */
3403 if ((refoptype[refop] & OPT) == 0)
3404 return 0;
3405 /* Flag any implicit default OP_DST operands so we know to add
3406 them explicitly when encoding the operand later. */
3407 if (OPTYPE (refoptype[refop]) == OP_DST)
3408 insn->using_default_dst = 1;
3409 ++refop;
3410 ++op;
3411 }
3412
3413 return 1;
3414 }
3415 }
3416
3417 return 0;
3418 }
3419
3420 /* 16-bit direct memory address
3421 Explicit dmad operands are always in last word of insn (usually second
3422 word, but bumped to third if lk addressing is used)
3423
3424 We allow *(dmad) notation because the TI assembler allows it.
3425
3426 XPC_CODE:
3427 0 for 16-bit addresses
3428 1 for full 23-bit addresses
3429 2 for the upper 7 bits of a 23-bit address (LDX). */
3430
3431 static int
3432 encode_dmad (tic54x_insn *insn, struct opstruct *operand, int xpc_code)
3433 {
3434 int op = 1 + insn->is_lkaddr;
3435
3436 /* Only allow *(dmad) expressions; all others are invalid. */
3437 if (is_indirect (operand) && operand->buf[strlen (operand->buf) - 1] != ')')
3438 {
3439 as_bad (_("Invalid dmad syntax '%s'"), operand->buf);
3440 return 0;
3441 }
3442
3443 insn->opcode[op].addr_expr = operand->exp;
3444
3445 if (insn->opcode[op].addr_expr.X_op == O_constant)
3446 {
3447 valueT value = insn->opcode[op].addr_expr.X_add_number;
3448
3449 if (xpc_code == 1)
3450 {
3451 insn->opcode[0].word &= 0xFF80;
3452 insn->opcode[0].word |= (value >> 16) & 0x7F;
3453 insn->opcode[1].word = value & 0xFFFF;
3454 }
3455 else if (xpc_code == 2)
3456 insn->opcode[op].word = (value >> 16) & 0xFFFF;
3457 else
3458 insn->opcode[op].word = value;
3459 }
3460 else
3461 {
3462 /* Do the fixup later; just store the expression. */
3463 insn->opcode[op].word = 0;
3464 insn->opcode[op].r_nchars = 2;
3465
3466 if (amode == c_mode)
3467 insn->opcode[op].r_type = BFD_RELOC_TIC54X_16_OF_23;
3468 else if (xpc_code == 1)
3469 {
3470 /* This relocation spans two words, so adjust accordingly. */
3471 insn->opcode[0].addr_expr = operand->exp;
3472 insn->opcode[0].r_type = BFD_RELOC_TIC54X_23;
3473 insn->opcode[0].r_nchars = 4;
3474 insn->opcode[0].unresolved = 1;
3475 /* It's really 2 words, but we want to stop encoding after the
3476 first, since we must encode both words at once. */
3477 insn->words = 1;
3478 }
3479 else if (xpc_code == 2)
3480 insn->opcode[op].r_type = BFD_RELOC_TIC54X_MS7_OF_23;
3481 else
3482 insn->opcode[op].r_type = BFD_RELOC_TIC54X_16_OF_23;
3483
3484 insn->opcode[op].unresolved = 1;
3485 }
3486
3487 return 1;
3488 }
3489
3490 /* 7-bit direct address encoding. */
3491
3492 static int
3493 encode_address (tic54x_insn *insn, struct opstruct *operand)
3494 {
3495 /* Assumes that dma addresses are *always* in word 0 of the opcode. */
3496 insn->opcode[0].addr_expr = operand->exp;
3497
3498 if (operand->exp.X_op == O_constant)
3499 insn->opcode[0].word |= (operand->exp.X_add_number & 0x7F);
3500 else
3501 {
3502 if (operand->exp.X_op == O_register)
3503 as_bad (_("Use the .mmregs directive to use memory-mapped register names such as '%s'"), operand->buf);
3504 /* Do the fixup later; just store the expression. */
3505 insn->opcode[0].r_nchars = 1;
3506 insn->opcode[0].r_type = BFD_RELOC_TIC54X_PARTLS7;
3507 insn->opcode[0].unresolved = 1;
3508 }
3509
3510 return 1;
3511 }
3512
3513 static int
3514 encode_indirect (tic54x_insn *insn, struct opstruct *operand)
3515 {
3516 int arf;
3517 int mod;
3518
3519 if (insn->is_lkaddr)
3520 {
3521 /* lk addresses always go in the second insn word. */
3522 mod = ((TOUPPER (operand->buf[1]) == 'A') ? 12 :
3523 (operand->buf[1] == '(') ? 15 :
3524 (strchr (operand->buf, '%') != NULL) ? 14 : 13);
3525 arf = ((mod == 12) ? operand->buf[3] - '0' :
3526 (mod == 15) ? 0 : operand->buf[4] - '0');
3527
3528 insn->opcode[1].addr_expr = operand->exp;
3529
3530 if (operand->exp.X_op == O_constant)
3531 insn->opcode[1].word = operand->exp.X_add_number;
3532 else
3533 {
3534 insn->opcode[1].word = 0;
3535 insn->opcode[1].r_nchars = 2;
3536 insn->opcode[1].r_type = BFD_RELOC_TIC54X_16_OF_23;
3537 insn->opcode[1].unresolved = 1;
3538 }
3539 }
3540 else if (strncasecmp (operand->buf, "*sp (", 4) == 0)
3541 {
3542 /* Stack offsets look the same as 7-bit direct addressing. */
3543 return encode_address (insn, operand);
3544 }
3545 else
3546 {
3547 arf = (TOUPPER (operand->buf[1]) == 'A' ?
3548 operand->buf[3] : operand->buf[4]) - '0';
3549
3550 if (operand->buf[1] == '+')
3551 {
3552 mod = 3; /* *+ARx */
3553 if (insn->tm->flags & FL_SMR)
3554 as_warn (_("Address mode *+ARx is write-only. "
3555 "Results of reading are undefined."));
3556 }
3557 else if (operand->buf[4] == '\0')
3558 mod = 0; /* *ARx */
3559 else if (operand->buf[5] == '\0')
3560 mod = (operand->buf[4] == '-' ? 1 : 2); /* *ARx+ / *ARx- */
3561 else if (operand->buf[6] == '\0')
3562 {
3563 if (operand->buf[5] == '0')
3564 mod = (operand->buf[4] == '-' ? 5 : 6); /* *ARx+0 / *ARx-0 */
3565 else
3566 mod = (operand->buf[4] == '-' ? 8 : 10);/* *ARx+% / *ARx-% */
3567 }
3568 else if (TOUPPER (operand->buf[6]) == 'B')
3569 mod = (operand->buf[4] == '-' ? 4 : 7); /* ARx+0B / *ARx-0B */
3570 else if (TOUPPER (operand->buf[6]) == '%')
3571 mod = (operand->buf[4] == '-' ? 9 : 11); /* ARx+0% / *ARx - 0% */
3572 else
3573 {
3574 as_bad (_("Unrecognized indirect address format \"%s\""),
3575 operand->buf);
3576 return 0;
3577 }
3578 }
3579
3580 insn->opcode[0].word |= 0x80 | (mod << 3) | arf;
3581
3582 return 1;
3583 }
3584
3585 static int
3586 encode_integer (tic54x_insn *insn,
3587 struct opstruct *operand,
3588 int which,
3589 int min,
3590 int max,
3591 unsigned short mask)
3592 {
3593 long parse, integer;
3594
3595 insn->opcode[which].addr_expr = operand->exp;
3596
3597 if (operand->exp.X_op == O_constant)
3598 {
3599 parse = operand->exp.X_add_number;
3600 /* Hack -- fixup for 16-bit hex quantities that get converted positive
3601 instead of negative. */
3602 if ((parse & 0x8000) && min == -32768 && max == 32767)
3603 integer = (short) parse;
3604 else
3605 integer = parse;
3606
3607 if (integer >= min && integer <= max)
3608 {
3609 insn->opcode[which].word |= (integer & mask);
3610 return 1;
3611 }
3612 as_bad (_("Operand '%s' out of range (%d <= x <= %d)"),
3613 operand->buf, min, max);
3614 }
3615 else
3616 {
3617 if (insn->opcode[which].addr_expr.X_op == O_constant)
3618 {
3619 insn->opcode[which].word |=
3620 insn->opcode[which].addr_expr.X_add_number & mask;
3621 }
3622 else
3623 {
3624 /* Do the fixup later; just store the expression. */
3625 bfd_reloc_code_real_type rtype =
3626 (mask == 0x1FF ? BFD_RELOC_TIC54X_PARTMS9 :
3627 mask == 0xFFFF ? BFD_RELOC_TIC54X_16_OF_23 :
3628 mask == 0x7F ? BFD_RELOC_TIC54X_PARTLS7 : BFD_RELOC_8);
3629 int size = (mask == 0x1FF || mask == 0xFFFF) ? 2 : 1;
3630
3631 if (rtype == BFD_RELOC_8)
3632 as_bad (_("Error in relocation handling"));
3633
3634 insn->opcode[which].r_nchars = size;
3635 insn->opcode[which].r_type = rtype;
3636 insn->opcode[which].unresolved = 1;
3637 }
3638
3639 return 1;
3640 }
3641
3642 return 0;
3643 }
3644
3645 static int
3646 encode_condition (tic54x_insn *insn, struct opstruct *operand)
3647 {
3648 tic54x_symbol *cc = (tic54x_symbol *) str_hash_find (cc_hash, operand->buf);
3649 if (!cc)
3650 {
3651 as_bad (_("Unrecognized condition code \"%s\""), operand->buf);
3652 return 0;
3653 }
3654 #define CC_GROUP 0x40
3655 #define CC_ACC 0x08
3656 #define CATG_A1 0x07
3657 #define CATG_B1 0x30
3658 #define CATG_A2 0x30
3659 #define CATG_B2 0x0C
3660 #define CATG_C2 0x03
3661 /* Disallow group 1 conditions mixed with group 2 conditions
3662 if group 1, allow only one category A and one category B
3663 if group 2, allow only one each of category A, B, and C. */
3664 if (((insn->opcode[0].word & 0xFF) != 0))
3665 {
3666 if ((insn->opcode[0].word & CC_GROUP) != (cc->value & CC_GROUP))
3667 {
3668 as_bad (_("Condition \"%s\" does not match preceding group"),
3669 operand->buf);
3670 return 0;
3671 }
3672 if (insn->opcode[0].word & CC_GROUP)
3673 {
3674 if ((insn->opcode[0].word & CC_ACC) != (cc->value & CC_ACC))
3675 {
3676 as_bad (_("Condition \"%s\" uses a different accumulator from "
3677 "a preceding condition"),
3678 operand->buf);
3679 return 0;
3680 }
3681 if ((insn->opcode[0].word & CATG_A1) && (cc->value & CATG_A1))
3682 {
3683 as_bad (_("Only one comparison conditional allowed"));
3684 return 0;
3685 }
3686 if ((insn->opcode[0].word & CATG_B1) && (cc->value & CATG_B1))
3687 {
3688 as_bad (_("Only one overflow conditional allowed"));
3689 return 0;
3690 }
3691 }
3692 else if ( ((insn->opcode[0].word & CATG_A2) && (cc->value & CATG_A2))
3693 || ((insn->opcode[0].word & CATG_B2) && (cc->value & CATG_B2))
3694 || ((insn->opcode[0].word & CATG_C2) && (cc->value & CATG_C2)))
3695 {
3696 as_bad (_("Duplicate %s conditional"), operand->buf);
3697 return 0;
3698 }
3699 }
3700
3701 insn->opcode[0].word |= cc->value;
3702 return 1;
3703 }
3704
3705 static int
3706 encode_cc3 (tic54x_insn *insn, struct opstruct *operand)
3707 {
3708 tic54x_symbol *cc3 = (tic54x_symbol *) str_hash_find (cc3_hash, operand->buf);
3709 int value = cc3 ? cc3->value : operand->exp.X_add_number << 8;
3710
3711 if ((value & 0x0300) != value)
3712 {
3713 as_bad (_("Unrecognized condition code \"%s\""), operand->buf);
3714 return 0;
3715 }
3716 insn->opcode[0].word |= value;
3717 return 1;
3718 }
3719
3720 static int
3721 encode_arx (tic54x_insn *insn, struct opstruct *operand)
3722 {
3723 int arf = strlen (operand->buf) >= 3 ? operand->buf[2] - '0' : -1;
3724
3725 if (strncasecmp ("ar", operand->buf, 2) || arf < 0 || arf > 7)
3726 {
3727 as_bad (_("Invalid auxiliary register (use AR0-AR7)"));
3728 return 0;
3729 }
3730 insn->opcode[0].word |= arf;
3731 return 1;
3732 }
3733
3734 static int
3735 encode_cc2 (tic54x_insn *insn, struct opstruct *operand)
3736 {
3737 tic54x_symbol *cc2 = (tic54x_symbol *) str_hash_find (cc2_hash, operand->buf);
3738
3739 if (!cc2)
3740 {
3741 as_bad (_("Unrecognized condition code \"%s\""), operand->buf);
3742 return 0;
3743 }
3744 insn->opcode[0].word |= cc2->value;
3745 return 1;
3746 }
3747
3748 static int
3749 encode_operand (tic54x_insn *insn, enum optype type, struct opstruct *operand)
3750 {
3751 int ext = (insn->tm->flags & FL_EXT) != 0;
3752
3753 if (type == OP_MMR && operand->exp.X_op != O_constant)
3754 {
3755 /* Disallow long-constant addressing for memory-mapped addressing. */
3756 if (insn->is_lkaddr)
3757 {
3758 as_bad (_("lk addressing modes are invalid for memory-mapped "
3759 "register addressing"));
3760 return 0;
3761 }
3762 type = OP_Smem;
3763 /* Warn about *+ARx when used with MMR operands. */
3764 if (strncasecmp (operand->buf, "*+ar", 4) == 0)
3765 {
3766 as_warn (_("Address mode *+ARx is not allowed in memory-mapped "
3767 "register addressing. Resulting behavior is "
3768 "undefined."));
3769 }
3770 }
3771
3772 switch (type)
3773 {
3774 case OP_None:
3775 return 1;
3776 case OP_dmad:
3777 /* 16-bit immediate value. */
3778 return encode_dmad (insn, operand, 0);
3779 case OP_SRC:
3780 if (TOUPPER (*operand->buf) == 'B')
3781 {
3782 insn->opcode[ext ? (1 + insn->is_lkaddr) : 0].word |= (1 << 9);
3783 if (insn->using_default_dst)
3784 insn->opcode[ext ? (1 + insn->is_lkaddr) : 0].word |= (1 << 8);
3785 }
3786 return 1;
3787 case OP_RND:
3788 /* Make sure this agrees with the OP_DST operand. */
3789 if (!((TOUPPER (operand->buf[0]) == 'B') ^
3790 ((insn->opcode[0].word & (1 << 8)) != 0)))
3791 {
3792 as_bad (_("Destination accumulator for each part of this parallel "
3793 "instruction must be different"));
3794 return 0;
3795 }
3796 return 1;
3797 case OP_SRC1:
3798 case OP_DST:
3799 if (TOUPPER (operand->buf[0]) == 'B')
3800 insn->opcode[ext ? (1 + insn->is_lkaddr) : 0].word |= (1 << 8);
3801 return 1;
3802 case OP_Xmem:
3803 case OP_Ymem:
3804 {
3805 int mod = (operand->buf[4] == '\0' ? 0 : /* *arx */
3806 operand->buf[4] == '-' ? 1 : /* *arx- */
3807 operand->buf[5] == '\0' ? 2 : 3); /* *arx+, *arx+0% */
3808 int arf = operand->buf[3] - '0' - 2;
3809 int code = (mod << 2) | arf;
3810 insn->opcode[0].word |= (code << (type == OP_Xmem ? 4 : 0));
3811 return 1;
3812 }
3813 case OP_Lmem:
3814 case OP_Smem:
3815 if (!is_indirect (operand))
3816 return encode_address (insn, operand);
3817 /* Fall through. */
3818 case OP_Sind:
3819 return encode_indirect (insn, operand);
3820 case OP_xpmad_ms7:
3821 return encode_dmad (insn, operand, 2);
3822 case OP_xpmad:
3823 return encode_dmad (insn, operand, 1);
3824 case OP_PA:
3825 case OP_pmad:
3826 return encode_dmad (insn, operand, 0);
3827 case OP_ARX:
3828 return encode_arx (insn, operand);
3829 case OP_MMRX:
3830 case OP_MMRY:
3831 case OP_MMR:
3832 {
3833 int value = operand->exp.X_add_number;
3834
3835 if (type == OP_MMR)
3836 insn->opcode[0].word |= value;
3837 else
3838 {
3839 if (value < 16 || value > 24)
3840 {
3841 as_bad (_("Memory mapped register \"%s\" out of range"),
3842 operand->buf);
3843 return 0;
3844 }
3845 if (type == OP_MMRX)
3846 insn->opcode[0].word |= (value - 16) << 4;
3847 else
3848 insn->opcode[0].word |= (value - 16);
3849 }
3850 return 1;
3851 }
3852 case OP_B:
3853 case OP_A:
3854 return 1;
3855 case OP_SHFT:
3856 return encode_integer (insn, operand, ext + insn->is_lkaddr,
3857 0, 15, 0xF);
3858 case OP_SHIFT:
3859 return encode_integer (insn, operand, ext + insn->is_lkaddr,
3860 -16, 15, 0x1F);
3861 case OP_lk:
3862 return encode_integer (insn, operand, 1 + insn->is_lkaddr,
3863 -32768, 32767, 0xFFFF);
3864 case OP_CC:
3865 return encode_condition (insn, operand);
3866 case OP_CC2:
3867 return encode_cc2 (insn, operand);
3868 case OP_CC3:
3869 return encode_cc3 (insn, operand);
3870 case OP_BITC:
3871 return encode_integer (insn, operand, 0, 0, 15, 0xF);
3872 case OP_k8:
3873 return encode_integer (insn, operand, 0, -128, 127, 0xFF);
3874 case OP_123:
3875 {
3876 int value = operand->exp.X_add_number;
3877 int code;
3878 if (value < 1 || value > 3)
3879 {
3880 as_bad (_("Invalid operand (use 1, 2, or 3)"));
3881 return 0;
3882 }
3883 code = value == 1 ? 0 : value == 2 ? 0x2 : 0x1;
3884 insn->opcode[0].word |= (code << 8);
3885 return 1;
3886 }
3887 case OP_031:
3888 return encode_integer (insn, operand, 0, 0, 31, 0x1F);
3889 case OP_k8u:
3890 return encode_integer (insn, operand, 0, 0, 255, 0xFF);
3891 case OP_lku:
3892 return encode_integer (insn, operand, 1 + insn->is_lkaddr,
3893 0, 65535, 0xFFFF);
3894 case OP_SBIT:
3895 {
3896 tic54x_symbol *sbit = (tic54x_symbol *)
3897 str_hash_find (sbit_hash, operand->buf);
3898 int value = is_absolute (operand) ?
3899 operand->exp.X_add_number : (sbit ? sbit->value : -1);
3900 int reg = 0;
3901
3902 if (insn->opcount == 1)
3903 {
3904 if (!sbit)
3905 {
3906 as_bad (_("A status register or status bit name is required"));
3907 return 0;
3908 }
3909 /* Guess the register based on the status bit; "ovb" is the last
3910 status bit defined for st0. */
3911 if (sbit > (tic54x_symbol *) str_hash_find (sbit_hash, "ovb"))
3912 reg = 1;
3913 }
3914 if (value == -1)
3915 {
3916 as_bad (_("Unrecognized status bit \"%s\""), operand->buf);
3917 return 0;
3918 }
3919 insn->opcode[0].word |= value;
3920 insn->opcode[0].word |= (reg << 9);
3921 return 1;
3922 }
3923 case OP_N:
3924 if (strcasecmp (operand->buf, "st0") == 0
3925 || strcasecmp (operand->buf, "st1") == 0)
3926 {
3927 insn->opcode[0].word |=
3928 ((unsigned short) (operand->buf[2] - '0')) << 9;
3929 return 1;
3930 }
3931 else if (operand->exp.X_op == O_constant
3932 && (operand->exp.X_add_number == 0
3933 || operand->exp.X_add_number == 1))
3934 {
3935 insn->opcode[0].word |=
3936 ((unsigned short) (operand->exp.X_add_number)) << 9;
3937 return 1;
3938 }
3939 as_bad (_("Invalid status register \"%s\""), operand->buf);
3940 return 0;
3941 case OP_k5:
3942 return encode_integer (insn, operand, 0, -16, 15, 0x1F);
3943 case OP_k3:
3944 return encode_integer (insn, operand, 0, 0, 7, 0x7);
3945 case OP_k9:
3946 return encode_integer (insn, operand, 0, 0, 0x1FF, 0x1FF);
3947 case OP_12:
3948 if (operand->exp.X_add_number != 1
3949 && operand->exp.X_add_number != 2)
3950 {
3951 as_bad (_("Operand \"%s\" out of range (use 1 or 2)"), operand->buf);
3952 return 0;
3953 }
3954 insn->opcode[0].word |= (operand->exp.X_add_number - 1) << 9;
3955 return 1;
3956 case OP_16:
3957 case OP_T:
3958 case OP_TS:
3959 case OP_ASM:
3960 case OP_TRN:
3961 case OP_DP:
3962 case OP_ARP:
3963 /* No encoding necessary. */
3964 return 1;
3965 default:
3966 return 0;
3967 }
3968
3969 return 1;
3970 }
3971
3972 static void
3973 emit_insn (tic54x_insn *insn)
3974 {
3975 int i;
3976 flagword oldflags = bfd_section_flags (now_seg);
3977 flagword flags = oldflags | SEC_CODE;
3978
3979 if (!bfd_set_section_flags (now_seg, flags))
3980 as_warn (_("error setting flags for \"%s\": %s"),
3981 bfd_section_name (now_seg),
3982 bfd_errmsg (bfd_get_error ()));
3983
3984 for (i = 0; i < insn->words; i++)
3985 {
3986 int size = (insn->opcode[i].unresolved
3987 && insn->opcode[i].r_type == BFD_RELOC_TIC54X_23) ? 4 : 2;
3988 char *p = frag_more (size);
3989
3990 if (size == 2)
3991 md_number_to_chars (p, (valueT) insn->opcode[i].word, 2);
3992 else
3993 md_number_to_chars (p, (valueT) insn->opcode[i].word << 16, 4);
3994
3995 if (insn->opcode[i].unresolved)
3996 fix_new_exp (frag_now, p - frag_now->fr_literal,
3997 insn->opcode[i].r_nchars, &insn->opcode[i].addr_expr,
3998 FALSE, insn->opcode[i].r_type);
3999 }
4000 }
4001
4002 /* Convert the operand strings into appropriate opcode values
4003 return the total number of words used by the instruction. */
4004
4005 static int
4006 build_insn (tic54x_insn *insn)
4007 {
4008 int i;
4009
4010 /* Only non-parallel instructions support lk addressing. */
4011 if (!(insn->tm->flags & FL_PAR))
4012 {
4013 for (i = 0; i < insn->opcount; i++)
4014 {
4015 if ((OPTYPE (insn->operands[i].type) == OP_Smem
4016 || OPTYPE (insn->operands[i].type) == OP_Lmem
4017 || OPTYPE (insn->operands[i].type) == OP_Sind)
4018 && strchr (insn->operands[i].buf, '(')
4019 /* Don't mistake stack-relative addressing for lk addressing. */
4020 && strncasecmp (insn->operands[i].buf, "*sp (", 4) != 0)
4021 {
4022 insn->is_lkaddr = 1;
4023 insn->lkoperand = i;
4024 break;
4025 }
4026 }
4027 }
4028 insn->words = insn->tm->words + insn->is_lkaddr;
4029
4030 insn->opcode[0].word = insn->tm->opcode;
4031 if (insn->tm->flags & FL_EXT)
4032 insn->opcode[1 + insn->is_lkaddr].word = insn->tm->opcode2;
4033
4034 for (i = 0; i < insn->opcount; i++)
4035 {
4036 enum optype type = insn->operands[i].type;
4037
4038 if (!encode_operand (insn, type, &insn->operands[i]))
4039 return 0;
4040 }
4041 if (insn->tm->flags & FL_PAR)
4042 for (i = 0; i < insn->paropcount; i++)
4043 {
4044 enum optype partype = insn->paroperands[i].type;
4045
4046 if (!encode_operand (insn, partype, &insn->paroperands[i]))
4047 return 0;
4048 }
4049
4050 emit_insn (insn);
4051
4052 return insn->words;
4053 }
4054
4055 static int
4056 optimize_insn (tic54x_insn *insn)
4057 {
4058 /* Optimize some instructions, helping out the brain-dead programmer. */
4059 #define is_zero(op) ((op).exp.X_op == O_constant && (op).exp.X_add_number == 0)
4060 if (strcasecmp (insn->tm->name, "add") == 0)
4061 {
4062 if (insn->opcount > 1
4063 && is_accumulator (&insn->operands[insn->opcount - 2])
4064 && is_accumulator (&insn->operands[insn->opcount - 1])
4065 && strcasecmp (insn->operands[insn->opcount - 2].buf,
4066 insn->operands[insn->opcount - 1].buf) == 0)
4067 {
4068 --insn->opcount;
4069 insn->using_default_dst = 1;
4070 return 1;
4071 }
4072
4073 /* Try to collapse if Xmem and shift count is zero. */
4074 if ((OPTYPE (insn->tm->operand_types[0]) == OP_Xmem
4075 && OPTYPE (insn->tm->operand_types[1]) == OP_SHFT
4076 && is_zero (insn->operands[1]))
4077 /* Or if Smem, shift is zero or absent, and SRC == DST. */
4078 || (OPTYPE (insn->tm->operand_types[0]) == OP_Smem
4079 && OPTYPE (insn->tm->operand_types[1]) == OP_SHIFT
4080 && is_type (&insn->operands[1], OP_SHIFT)
4081 && is_zero (insn->operands[1]) && insn->opcount == 3))
4082 {
4083 insn->operands[1] = insn->operands[2];
4084 insn->opcount = 2;
4085 return 1;
4086 }
4087 }
4088 else if (strcasecmp (insn->tm->name, "ld") == 0)
4089 {
4090 if (insn->opcount == 3 && insn->operands[0].type != OP_SRC)
4091 {
4092 if ((OPTYPE (insn->tm->operand_types[1]) == OP_SHIFT
4093 || OPTYPE (insn->tm->operand_types[1]) == OP_SHFT)
4094 && is_zero (insn->operands[1])
4095 && (OPTYPE (insn->tm->operand_types[0]) != OP_lk
4096 || (insn->operands[0].exp.X_op == O_constant
4097 && insn->operands[0].exp.X_add_number <= 255
4098 && insn->operands[0].exp.X_add_number >= 0)))
4099 {
4100 insn->operands[1] = insn->operands[2];
4101 insn->opcount = 2;
4102 return 1;
4103 }
4104 }
4105 }
4106 else if (strcasecmp (insn->tm->name, "sth") == 0
4107 || strcasecmp (insn->tm->name, "stl") == 0)
4108 {
4109 if ((OPTYPE (insn->tm->operand_types[1]) == OP_SHIFT
4110 || OPTYPE (insn->tm->operand_types[1]) == OP_SHFT)
4111 && is_zero (insn->operands[1]))
4112 {
4113 insn->operands[1] = insn->operands[2];
4114 insn->opcount = 2;
4115 return 1;
4116 }
4117 }
4118 else if (strcasecmp (insn->tm->name, "sub") == 0)
4119 {
4120 if (insn->opcount > 1
4121 && is_accumulator (&insn->operands[insn->opcount - 2])
4122 && is_accumulator (&insn->operands[insn->opcount - 1])
4123 && strcasecmp (insn->operands[insn->opcount - 2].buf,
4124 insn->operands[insn->opcount - 1].buf) == 0)
4125 {
4126 --insn->opcount;
4127 insn->using_default_dst = 1;
4128 return 1;
4129 }
4130
4131 if ( ((OPTYPE (insn->tm->operand_types[0]) == OP_Smem
4132 && OPTYPE (insn->tm->operand_types[1]) == OP_SHIFT)
4133 || (OPTYPE (insn->tm->operand_types[0]) == OP_Xmem
4134 && OPTYPE (insn->tm->operand_types[1]) == OP_SHFT))
4135 && is_zero (insn->operands[1])
4136 && insn->opcount == 3)
4137 {
4138 insn->operands[1] = insn->operands[2];
4139 insn->opcount = 2;
4140 return 1;
4141 }
4142 }
4143 return 0;
4144 }
4145
4146 /* Find a matching template if possible, and get the operand strings. */
4147
4148 static int
4149 tic54x_parse_insn (tic54x_insn *insn, char *line)
4150 {
4151 insn->tm = (insn_template *) str_hash_find (op_hash, insn->mnemonic);
4152 if (!insn->tm)
4153 {
4154 as_bad (_("Unrecognized instruction \"%s\""), insn->mnemonic);
4155 return 0;
4156 }
4157
4158 insn->opcount = get_operands (insn->operands, line);
4159 if (insn->opcount < 0)
4160 return 0;
4161
4162 /* Check each variation of operands for this mnemonic. */
4163 while (insn->tm->name && strcasecmp (insn->tm->name, insn->mnemonic) == 0)
4164 {
4165 if (insn->opcount >= insn->tm->minops
4166 && insn->opcount <= insn->tm->maxops
4167 && operands_match (insn, &insn->operands[0], insn->opcount,
4168 insn->tm->operand_types,
4169 insn->tm->minops, insn->tm->maxops))
4170 {
4171 /* SUCCESS! now try some optimizations. */
4172 if (optimize_insn (insn))
4173 {
4174 insn->tm = (insn_template *) str_hash_find (op_hash,
4175 insn->mnemonic);
4176 continue;
4177 }
4178
4179 return 1;
4180 }
4181 ++(insn->tm);
4182 }
4183 as_bad (_("Unrecognized operand list '%s' for instruction '%s'"),
4184 line, insn->mnemonic);
4185 return 0;
4186 }
4187
4188 /* We set this in start_line_hook, 'cause if we do a line replacement, we
4189 won't be able to see the next line. */
4190 static int parallel_on_next_line_hint = 0;
4191
4192 /* See if this is part of a parallel instruction
4193 Look for a subsequent line starting with "||". */
4194
4195 static int
4196 next_line_shows_parallel (char *next_line)
4197 {
4198 /* Look for the second half. */
4199 while (*next_line != 0 && ISSPACE (*next_line))
4200 ++next_line;
4201
4202 return (next_line[0] == PARALLEL_SEPARATOR
4203 && next_line[1] == PARALLEL_SEPARATOR);
4204 }
4205
4206 static int
4207 tic54x_parse_parallel_insn_firstline (tic54x_insn *insn, char *line)
4208 {
4209 insn->tm = (insn_template *) str_hash_find (parop_hash, insn->mnemonic);
4210 if (!insn->tm)
4211 {
4212 as_bad (_("Unrecognized parallel instruction \"%s\""),
4213 insn->mnemonic);
4214 return 0;
4215 }
4216
4217 while (insn->tm->name && strcasecmp (insn->tm->name,
4218 insn->mnemonic) == 0)
4219 {
4220 insn->opcount = get_operands (insn->operands, line);
4221 if (insn->opcount < 0)
4222 return 0;
4223 if (insn->opcount == 2
4224 && operands_match (insn, &insn->operands[0], insn->opcount,
4225 insn->tm->operand_types, 2, 2))
4226 {
4227 return 1;
4228 }
4229 ++(insn->tm);
4230 }
4231 /* Didn't find a matching parallel; try for a normal insn. */
4232 return 0;
4233 }
4234
4235 /* Parse the second line of a two-line parallel instruction. */
4236
4237 static int
4238 tic54x_parse_parallel_insn_lastline (tic54x_insn *insn, char *line)
4239 {
4240 int valid_mnemonic = 0;
4241
4242 insn->paropcount = get_operands (insn->paroperands, line);
4243 while (insn->tm->name && strcasecmp (insn->tm->name,
4244 insn->mnemonic) == 0)
4245 {
4246 if (strcasecmp (insn->tm->parname, insn->parmnemonic) == 0)
4247 {
4248 valid_mnemonic = 1;
4249
4250 if (insn->paropcount >= insn->tm->minops
4251 && insn->paropcount <= insn->tm->maxops
4252 && operands_match (insn, insn->paroperands,
4253 insn->paropcount,
4254 insn->tm->paroperand_types,
4255 insn->tm->minops, insn->tm->maxops))
4256 return 1;
4257 }
4258 ++(insn->tm);
4259 }
4260 if (valid_mnemonic)
4261 as_bad (_("Invalid operand (s) for parallel instruction \"%s\""),
4262 insn->parmnemonic);
4263 else
4264 as_bad (_("Unrecognized parallel instruction combination \"%s || %s\""),
4265 insn->mnemonic, insn->parmnemonic);
4266
4267 return 0;
4268 }
4269
4270 /* If quotes found, return copy of line up to closing quote;
4271 otherwise up until terminator.
4272 If it's a string, pass as-is; otherwise attempt substitution symbol
4273 replacement on the value. */
4274
4275 static char *
4276 subsym_get_arg (char *line, const char *terminators, char **str, int nosub)
4277 {
4278 char *ptr = line;
4279 char *endp;
4280 int is_string = *line == '"';
4281 int is_char = ISDIGIT (*line);
4282
4283 if (is_char)
4284 {
4285 while (ISDIGIT (*ptr))
4286 ++ptr;
4287 endp = ptr;
4288 *str = xmemdup0 (line, ptr - line);
4289 }
4290 else if (is_string)
4291 {
4292 char *savedp = input_line_pointer;
4293 int len;
4294
4295 input_line_pointer = ptr;
4296 *str = demand_copy_C_string (&len);
4297 endp = input_line_pointer;
4298 input_line_pointer = savedp;
4299
4300 /* Do forced substitutions if requested. */
4301 if (!nosub && **str == ':')
4302 *str = subsym_substitute (*str, 1);
4303 }
4304 else
4305 {
4306 const char *term = terminators;
4307 char *value = NULL;
4308
4309 while (*ptr && *ptr != *term)
4310 {
4311 if (!*term)
4312 {
4313 term = terminators;
4314 ++ptr;
4315 }
4316 else
4317 ++term;
4318 }
4319 endp = ptr;
4320 *str = xmemdup0 (line, ptr - line);
4321 /* Do simple substitution, if available. */
4322 if (!nosub && (value = subsym_lookup (*str, macro_level)) != NULL)
4323 *str = value;
4324 }
4325
4326 return endp;
4327 }
4328
4329 /* Replace the given substitution string.
4330 We start at the innermost macro level, so that existing locals remain local
4331 Note: we're treating macro args identically to .var's; I don't know if
4332 that's compatible w/TI's assembler. */
4333
4334 static void
4335 subsym_create_or_replace (char *name, char *value)
4336 {
4337 int i;
4338
4339 for (i = macro_level; i > 0; i--)
4340 str_hash_insert (subsym_hash[i], name, value);
4341 str_hash_insert (subsym_hash[0], name, value);
4342 }
4343
4344 /* Look up the substitution string replacement for the given symbol.
4345 Start with the innermost macro substitution table given and work
4346 outwards. */
4347
4348 static char *
4349 subsym_lookup (char *name, int nest_level)
4350 {
4351 char *value = str_hash_find (subsym_hash[nest_level], name);
4352
4353 if (value || nest_level == 0)
4354 return value;
4355
4356 return subsym_lookup (name, nest_level - 1);
4357 }
4358
4359 /* Do substitution-symbol replacement on the given line (recursively).
4360 return the argument if no substitution was done
4361
4362 Also look for built-in functions ($func (arg)) and local labels.
4363
4364 If FORCED is set, look for forced substitutions of the form ':SYMBOL:'. */
4365
4366 static char *
4367 subsym_substitute (char *line, int forced)
4368 {
4369 /* For each apparent symbol, see if it's a substitution symbol, and if so,
4370 replace it in the input. */
4371 char *replacement; /* current replacement for LINE. */
4372 char *head; /* Start of line. */
4373 char *ptr; /* Current examination point. */
4374 int changed = 0; /* Did we make a substitution? */
4375 int eval_line = 0; /* Is this line a .eval/.asg statement? */
4376 int eval_symbol = 0; /* Are we in the middle of the symbol for
4377 .eval/.asg? */
4378 char *eval_end = NULL;
4379 int recurse = 1;
4380 int line_conditional = 0;
4381 char *tmp;
4382
4383 /* Work with a copy of the input line. */
4384 replacement = xstrdup (line);
4385
4386 ptr = head = replacement;
4387
4388 /* Flag lines where we might need to replace a single '=' with two;
4389 GAS uses single '=' to assign macro args values, and possibly other
4390 places, so limit what we replace. */
4391 if (strstr (line, ".if")
4392 || strstr (line, ".elseif")
4393 || strstr (line, ".break"))
4394 line_conditional = 1;
4395
4396 /* Watch out for .eval, so that we avoid doing substitution on the
4397 symbol being assigned a value. */
4398 if (strstr (line, ".eval") || strstr (line, ".asg"))
4399 eval_line = 1;
4400
4401 /* If it's a macro definition, don't do substitution on the argument
4402 names. */
4403 if (strstr (line, ".macro"))
4404 return line;
4405
4406 unsigned char current_char;
4407 while (!is_end_of_line[(current_char = * (unsigned char *) ptr)])
4408 {
4409 /* Need to update this since LINE may have been modified. */
4410 if (eval_line)
4411 eval_end = strrchr (ptr, ',');
4412
4413 /* Replace triple double quotes with bounding quote/escapes. */
4414 if (current_char == '"' && ptr[1] == '"' && ptr[2] == '"')
4415 {
4416 ptr[1] = '\\';
4417 tmp = strstr (ptr + 2, "\"\"\"");
4418 if (tmp)
4419 tmp[0] = '\\';
4420 changed = 1;
4421 }
4422
4423 /* Replace a single '=' with a '==';
4424 for compatibility with older code only. */
4425 if (line_conditional && current_char == '=')
4426 {
4427 if (ptr[1] == '=')
4428 {
4429 ptr += 2;
4430 continue;
4431 }
4432 *ptr++ = '\0';
4433 tmp = concat (head, "==", ptr, (char *) NULL);
4434 /* Continue examining after the '=='. */
4435 ptr = tmp + strlen (head) + 2;
4436 free (replacement);
4437 head = replacement = tmp;
4438 changed = 1;
4439 }
4440
4441 /* Flag when we've reached the symbol part of .eval/.asg. */
4442 if (eval_line && ptr >= eval_end)
4443 eval_symbol = 1;
4444
4445 /* For each apparent symbol, see if it's a substitution symbol, and if
4446 so, replace it in the input. */
4447 if ((forced && current_char == ':')
4448 || (!forced && is_name_beginner (current_char)))
4449 {
4450 char *name; /* Symbol to be replaced. */
4451 char *savedp = input_line_pointer;
4452 int c;
4453 char *value = NULL;
4454 char *tail; /* Rest of line after symbol. */
4455
4456 /* Skip the colon. */
4457 if (forced)
4458 ++ptr;
4459
4460 input_line_pointer = ptr;
4461 c = get_symbol_name (&name);
4462 /* '?' is not normally part of a symbol, but it IS part of a local
4463 label. */
4464 if (c == '?')
4465 {
4466 *input_line_pointer++ = c;
4467 c = *input_line_pointer;
4468 *input_line_pointer = '\0';
4469 }
4470 /* Avoid infinite recursion; if a symbol shows up a second time for
4471 substitution, leave it as is. */
4472 if (str_hash_find (subsym_recurse_hash, name) == NULL)
4473 value = subsym_lookup (name, macro_level);
4474 else
4475 as_warn (_("%s symbol recursion stopped at "
4476 "second appearance of '%s'"),
4477 forced ? "Forced substitution" : "Substitution", name);
4478 ptr = tail = input_line_pointer;
4479 input_line_pointer = savedp;
4480
4481 /* Check for local labels; replace them with the appropriate
4482 substitution. */
4483 if ((*name == '$' && ISDIGIT (name[1]) && name[2] == '\0')
4484 || name[strlen (name) - 1] == '?')
4485 {
4486 /* Use an existing identifier for that label if, available, or
4487 create a new, unique identifier. */
4488 value = str_hash_find (local_label_hash[macro_level], name);
4489 if (value == NULL)
4490 {
4491 char digit[11];
4492 char *namecopy = xstrdup (name);
4493
4494 value = strcpy (xmalloc (strlen (name) + sizeof (digit) + 1),
4495 name);
4496 if (*value != '$')
4497 value[strlen (value) - 1] = '\0';
4498 sprintf (digit, ".%d", local_label_id++);
4499 strcat (value, digit);
4500 str_hash_insert (local_label_hash[macro_level], namecopy, value);
4501 }
4502 /* Indicate where to continue looking for substitutions. */
4503 ptr = tail;
4504 }
4505 /* Check for built-in subsym and math functions. */
4506 else if (value != NULL && *name == '$')
4507 {
4508 subsym_proc_entry *entry = (subsym_proc_entry *) value;
4509 math_proc_entry *math_entry = str_hash_find (math_hash, name);
4510 char *arg1, *arg2 = NULL;
4511
4512 *ptr = c;
4513 if (entry == NULL)
4514 {
4515 as_bad (_("Unrecognized substitution symbol function"));
4516 break;
4517 }
4518 else if (*ptr != '(')
4519 {
4520 as_bad (_("Missing '(' after substitution symbol function"));
4521 break;
4522 }
4523 ++ptr;
4524 if (math_entry != NULL)
4525 {
4526 float farg1, farg2 = 0;
4527 volatile float fresult;
4528
4529 farg1 = (float) strtod (ptr, &ptr);
4530 if (math_entry->nargs == 2)
4531 {
4532 if (*ptr++ != ',')
4533 {
4534 as_bad (_("Expecting second argument"));
4535 break;
4536 }
4537 farg2 = (float) strtod (ptr, &ptr);
4538 }
4539 fresult = (*math_entry->proc) (farg1, farg2);
4540 value = XNEWVEC (char, 128);
4541 if (math_entry->int_return)
4542 sprintf (value, "%d", (int) fresult);
4543 else
4544 sprintf (value, "%f", fresult);
4545 if (*ptr++ != ')')
4546 {
4547 as_bad (_("Extra junk in function call, expecting ')'"));
4548 break;
4549 }
4550 /* Don't bother recursing; the replacement isn't a
4551 symbol. */
4552 recurse = 0;
4553 }
4554 else
4555 {
4556 int val;
4557 int arg_type[2] = { *ptr == '"' , 0 };
4558 int ismember = !strcmp (entry->name, "$ismember");
4559
4560 /* Parse one or two args, which must be a substitution
4561 symbol, string or a character-string constant. */
4562 /* For all functions, a string or substitution symbol may be
4563 used, with the following exceptions:
4564 firstch/lastch: 2nd arg must be character constant
4565 ismember: both args must be substitution symbols. */
4566 ptr = subsym_get_arg (ptr, ",)", &arg1, ismember);
4567 if (!arg1)
4568 break;
4569 if (entry->nargs == 2)
4570 {
4571 if (*ptr++ != ',')
4572 {
4573 as_bad (_("Function expects two arguments"));
4574 break;
4575 }
4576 /* Character constants are converted to numerics
4577 by the preprocessor. */
4578 arg_type[1] = (ISDIGIT (*ptr)) ? 2 : (*ptr == '"');
4579 ptr = subsym_get_arg (ptr, ")", &arg2, ismember);
4580 }
4581 /* Args checking. */
4582 if ((!strcmp (entry->name, "$firstch")
4583 || !strcmp (entry->name, "$lastch"))
4584 && arg_type[1] != 2)
4585 {
4586 as_bad (_("Expecting character constant argument"));
4587 break;
4588 }
4589 if (ismember
4590 && (arg_type[0] != 0 || arg_type[1] != 0))
4591 {
4592 as_bad (_("Both arguments must be substitution symbols"));
4593 break;
4594 }
4595 if (*ptr++ != ')')
4596 {
4597 as_bad (_("Extra junk in function call, expecting ')'"));
4598 break;
4599 }
4600 val = (*entry->proc) (arg1, arg2);
4601 value = XNEWVEC (char, 64);
4602 sprintf (value, "%d", val);
4603 }
4604 /* Fix things up to replace the entire expression, not just the
4605 function name. */
4606 tail = ptr;
4607 c = *tail;
4608 }
4609
4610 if (value != NULL && !eval_symbol)
4611 {
4612 /* Replace the symbol with its string replacement and
4613 continue. Recursively replace VALUE until either no
4614 substitutions are performed, or a substitution that has been
4615 previously made is encountered again.
4616
4617 Put the symbol into the recursion hash table so we only
4618 try to replace a symbol once. */
4619 if (recurse)
4620 {
4621 str_hash_insert (subsym_recurse_hash, name, name);
4622 value = subsym_substitute (value, macro_level > 0);
4623 str_hash_delete (subsym_recurse_hash, name);
4624 }
4625
4626 /* Temporarily zero-terminate where the symbol started. */
4627 *name = 0;
4628 if (forced)
4629 {
4630 if (c == '(')
4631 {
4632 /* Subscripted substitution symbol -- use just the
4633 indicated portion of the string; the description
4634 kinda indicates that forced substitution is not
4635 supposed to be recursive, but I'm not sure. */
4636 unsigned beg, len = 1; /* default to a single char */
4637 char *newval = xstrdup (value);
4638
4639 savedp = input_line_pointer;
4640 input_line_pointer = tail + 1;
4641 beg = get_absolute_expression ();
4642 if (beg < 1)
4643 {
4644 as_bad (_("Invalid subscript (use 1 to %d)"),
4645 (int) strlen (value));
4646 break;
4647 }
4648 if (*input_line_pointer == ',')
4649 {
4650 ++input_line_pointer;
4651 len = get_absolute_expression ();
4652 if (beg + len > strlen (value))
4653 {
4654 as_bad (_("Invalid length (use 0 to %d)"),
4655 (int) strlen (value) - beg);
4656 break;
4657 }
4658 }
4659 newval += beg - 1;
4660 newval[len] = 0;
4661 tail = input_line_pointer;
4662 if (*tail++ != ')')
4663 {
4664 as_bad (_("Missing ')' in subscripted substitution "
4665 "symbol expression"));
4666 break;
4667 }
4668 c = *tail;
4669 input_line_pointer = savedp;
4670
4671 value = newval;
4672 }
4673 name[-1] = 0;
4674 }
4675 tmp = xmalloc (strlen (head) + strlen (value) +
4676 strlen (tail + 1) + 2);
4677 strcpy (tmp, head);
4678 strcat (tmp, value);
4679 /* Make sure forced substitutions are properly terminated. */
4680 if (forced)
4681 {
4682 if (c != ':')
4683 {
4684 as_bad (_("Missing forced substitution terminator ':'"));
4685 break;
4686 }
4687 ++tail;
4688 }
4689 else
4690 /* Restore the character after the symbol end. */
4691 *tail = c;
4692 strcat (tmp, tail);
4693 /* Continue examining after the replacement value. */
4694 ptr = tmp + strlen (head) + strlen (value);
4695 free (replacement);
4696 head = replacement = tmp;
4697 changed = 1;
4698 }
4699 else
4700 *ptr = c;
4701 }
4702 else
4703 {
4704 ++ptr;
4705 }
4706 }
4707
4708 if (changed)
4709 return replacement;
4710 else
4711 return line;
4712 }
4713
4714 /* We use this to handle substitution symbols
4715 hijack input_line_pointer, replacing it with our substituted string.
4716
4717 .sslist should enable listing the line after replacements are made...
4718
4719 returns the new buffer limit. */
4720
4721 void
4722 tic54x_start_line_hook (void)
4723 {
4724 char *line, *endp;
4725 char *replacement = NULL;
4726
4727 /* Work with a copy of the input line, including EOL char. */
4728 for (endp = input_line_pointer; *endp != 0; )
4729 if (is_end_of_line[(unsigned char) *endp++])
4730 break;
4731
4732 line = xmemdup0 (input_line_pointer, endp - input_line_pointer);
4733
4734 /* Scan ahead for parallel insns. */
4735 parallel_on_next_line_hint = next_line_shows_parallel (endp);
4736
4737 /* If within a macro, first process forced replacements. */
4738 if (macro_level > 0)
4739 replacement = subsym_substitute (line, 1);
4740 else
4741 replacement = line;
4742 replacement = subsym_substitute (replacement, 0);
4743
4744 if (replacement != line)
4745 {
4746 char *tmp = replacement;
4747 char *comment = strchr (replacement, ';');
4748 char endc = replacement[strlen (replacement) - 1];
4749
4750 /* Clean up the replacement; we'd prefer to have this done by the
4751 standard preprocessing equipment (maybe do_scrub_chars?)
4752 but for now, do a quick-and-dirty. */
4753 if (comment != NULL)
4754 {
4755 comment[0] = endc;
4756 comment[1] = 0;
4757 --comment;
4758 }
4759 else
4760 comment = replacement + strlen (replacement) - 1;
4761
4762 /* Trim trailing whitespace. */
4763 while (ISSPACE (*comment))
4764 {
4765 comment[0] = endc;
4766 comment[1] = 0;
4767 --comment;
4768 }
4769
4770 /* Compact leading whitespace. */
4771 while (ISSPACE (tmp[0]) && ISSPACE (tmp[1]))
4772 ++tmp;
4773
4774 input_line_pointer = endp;
4775 input_scrub_insert_line (tmp);
4776 free (replacement);
4777 free (line);
4778 /* Keep track of whether we've done a substitution. */
4779 substitution_line = 1;
4780 }
4781 else
4782 {
4783 /* No change. */
4784 free (line);
4785 substitution_line = 0;
4786 }
4787 }
4788
4789 /* This is the guts of the machine-dependent assembler. STR points to a
4790 machine dependent instruction. This function is supposed to emit
4791 the frags/bytes it assembles to. */
4792 void
4793 md_assemble (char *line)
4794 {
4795 static int repeat_slot = 0;
4796 static int delay_slots = 0; /* How many delay slots left to fill? */
4797 static int is_parallel = 0;
4798 static tic54x_insn insn;
4799 char *lptr;
4800 char *savedp = input_line_pointer;
4801 int c;
4802
4803 input_line_pointer = line;
4804 c = get_symbol_name (&line);
4805
4806 if (cpu == VNONE)
4807 cpu = V542;
4808 if (address_mode_needs_set)
4809 {
4810 set_address_mode (amode);
4811 address_mode_needs_set = 0;
4812 }
4813 if (cpu_needs_set)
4814 {
4815 set_cpu (cpu);
4816 cpu_needs_set = 0;
4817 }
4818 assembly_begun = 1;
4819
4820 if (is_parallel)
4821 {
4822 is_parallel = 0;
4823
4824 strcpy (insn.parmnemonic, line);
4825 lptr = input_line_pointer;
4826 *lptr = c;
4827 input_line_pointer = savedp;
4828
4829 if (tic54x_parse_parallel_insn_lastline (&insn, lptr))
4830 {
4831 int words = build_insn (&insn);
4832
4833 if (delay_slots != 0)
4834 {
4835 if (words > delay_slots)
4836 {
4837 as_bad (ngettext ("Instruction does not fit in available "
4838 "delay slots (%d-word insn, %d slot left)",
4839 "Instruction does not fit in available "
4840 "delay slots (%d-word insn, %d slots left)",
4841 delay_slots),
4842 words, delay_slots);
4843 delay_slots = 0;
4844 return;
4845 }
4846 delay_slots -= words;
4847 }
4848 }
4849 return;
4850 }
4851
4852 memset (&insn, 0, sizeof (insn));
4853 strcpy (insn.mnemonic, line);
4854 lptr = input_line_pointer;
4855 *lptr = c;
4856 input_line_pointer = savedp;
4857
4858 /* See if this line is part of a parallel instruction; if so, either this
4859 line or the next line will have the "||" specifier preceding the
4860 mnemonic, and we look for it in the parallel insn hash table. */
4861 if (strstr (line, "||") != NULL || parallel_on_next_line_hint)
4862 {
4863 char *tmp = strstr (line, "||");
4864 if (tmp != NULL)
4865 *tmp = '\0';
4866
4867 if (tic54x_parse_parallel_insn_firstline (&insn, lptr))
4868 {
4869 is_parallel = 1;
4870 /* If the parallel part is on the same line, process it now,
4871 otherwise let the assembler pick up the next line for us. */
4872 if (tmp != NULL)
4873 {
4874 while (ISSPACE (tmp[2]))
4875 ++tmp;
4876 md_assemble (tmp + 2);
4877 }
4878 }
4879 else
4880 {
4881 as_bad (_("Unrecognized parallel instruction '%s'"), line);
4882 }
4883 return;
4884 }
4885
4886 if (tic54x_parse_insn (&insn, lptr))
4887 {
4888 int words;
4889
4890 if ((insn.tm->flags & FL_LP)
4891 && cpu != V545LP && cpu != V546LP)
4892 {
4893 as_bad (_("Instruction '%s' requires an LP cpu version"),
4894 insn.tm->name);
4895 return;
4896 }
4897 if ((insn.tm->flags & FL_FAR)
4898 && amode != far_mode)
4899 {
4900 as_bad (_("Instruction '%s' requires far mode addressing"),
4901 insn.tm->name);
4902 return;
4903 }
4904
4905 words = build_insn (&insn);
4906
4907 /* Is this instruction in a delay slot? */
4908 if (delay_slots)
4909 {
4910 if (words > delay_slots)
4911 {
4912 as_warn (ngettext ("Instruction does not fit in available "
4913 "delay slots (%d-word insn, %d slot left). "
4914 "Resulting behavior is undefined.",
4915 "Instruction does not fit in available "
4916 "delay slots (%d-word insn, %d slots left). "
4917 "Resulting behavior is undefined.",
4918 delay_slots),
4919 words, delay_slots);
4920 delay_slots = 0;
4921 return;
4922 }
4923 /* Branches in delay slots are not allowed. */
4924 if (insn.tm->flags & FL_BMASK)
4925 {
4926 as_warn (_("Instructions which cause PC discontinuity are not "
4927 "allowed in a delay slot. "
4928 "Resulting behavior is undefined."));
4929 }
4930 delay_slots -= words;
4931 }
4932
4933 /* Is this instruction the target of a repeat? */
4934 if (repeat_slot)
4935 {
4936 if (insn.tm->flags & FL_NR)
4937 as_warn (_("'%s' is not repeatable. "
4938 "Resulting behavior is undefined."),
4939 insn.tm->name);
4940 else if (insn.is_lkaddr)
4941 as_warn (_("Instructions using long offset modifiers or absolute "
4942 "addresses are not repeatable. "
4943 "Resulting behavior is undefined."));
4944 repeat_slot = 0;
4945 }
4946
4947 /* Make sure we check the target of a repeat instruction. */
4948 if (insn.tm->flags & B_REPEAT)
4949 {
4950 repeat_slot = 1;
4951 /* FIXME -- warn if repeat_slot == 1 at EOF. */
4952 }
4953 /* Make sure we check our delay slots for validity. */
4954 if (insn.tm->flags & FL_DELAY)
4955 {
4956 delay_slots = 2;
4957 /* FIXME -- warn if delay_slots != 0 at EOF. */
4958 }
4959 }
4960 }
4961
4962 /* Do a final adjustment on the symbol table; in this case, make sure we have
4963 a ".file" symbol. */
4964
4965 void
4966 tic54x_adjust_symtab (void)
4967 {
4968 if (symbol_rootP == NULL
4969 || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
4970 {
4971 unsigned lineno;
4972 const char * filename = as_where (&lineno);
4973 c_dot_file_symbol (filename, 0);
4974 }
4975 }
4976
4977 /* In order to get gas to ignore any | chars at the start of a line,
4978 this function returns true if a | is found in a line.
4979 This lets us process parallel instructions, which span two lines. */
4980
4981 int
4982 tic54x_unrecognized_line (int c)
4983 {
4984 return c == PARALLEL_SEPARATOR;
4985 }
4986
4987 /* Watch for local labels of the form $[0-9] and [_a-zA-Z][_a-zA-Z0-9]*?
4988 Encode their names so that only we see them and can map them to the
4989 appropriate places.
4990 FIXME -- obviously this isn't done yet. These locals still show up in the
4991 symbol table. */
4992 void
4993 tic54x_define_label (symbolS *sym)
4994 {
4995 /* Just in case we need this later; note that this is not necessarily the
4996 same thing as line_label...
4997 When aligning or assigning labels to fields, sometimes the label is
4998 assigned other than the address at which the label appears.
4999 FIXME -- is this really needed? I think all the proper label assignment
5000 is done in tic54x_cons. */
5001 last_label_seen = sym;
5002 }
5003
5004 /* Try to parse something that normal parsing failed at. */
5005
5006 symbolS *
5007 tic54x_undefined_symbol (char *name)
5008 {
5009 tic54x_symbol *sym;
5010
5011 /* Not sure how to handle predefined symbols. */
5012 if ((sym = (tic54x_symbol *) str_hash_find (cc_hash, name)) != NULL ||
5013 (sym = (tic54x_symbol *) str_hash_find (cc2_hash, name)) != NULL ||
5014 (sym = (tic54x_symbol *) str_hash_find (cc3_hash, name)) != NULL ||
5015 (sym = (tic54x_symbol *) str_hash_find (misc_symbol_hash, name)) != NULL ||
5016 (sym = (tic54x_symbol *) str_hash_find (sbit_hash, name)) != NULL)
5017 {
5018 return symbol_new (name, reg_section,
5019 (valueT) sym->value,
5020 &zero_address_frag);
5021 }
5022
5023 if ((sym = (tic54x_symbol *) str_hash_find (reg_hash, name)) != NULL ||
5024 (sym = (tic54x_symbol *) str_hash_find (mmreg_hash, name)) != NULL ||
5025 !strcasecmp (name, "a") || !strcasecmp (name, "b"))
5026 {
5027 return symbol_new (name, reg_section,
5028 (valueT) sym ? sym->value : 0,
5029 &zero_address_frag);
5030 }
5031
5032 return NULL;
5033 }
5034
5035 /* Parse a name in an expression before the expression parser takes a stab at
5036 it. */
5037
5038 int
5039 tic54x_parse_name (char *name ATTRIBUTE_UNUSED,
5040 expressionS *expn ATTRIBUTE_UNUSED)
5041 {
5042 return 0;
5043 }
5044
5045 const char *
5046 md_atof (int type, char *literalP, int *sizeP)
5047 {
5048 /* Target data is little-endian, but floats are stored
5049 big-"word"ian. ugh. */
5050 return ieee_md_atof (type, literalP, sizeP, TRUE);
5051 }
5052
5053 arelent *
5054 tc_gen_reloc (asection *section, fixS *fixP)
5055 {
5056 arelent *rel;
5057 bfd_reloc_code_real_type code = fixP->fx_r_type;
5058 asymbol *sym = symbol_get_bfdsym (fixP->fx_addsy);
5059
5060 rel = XNEW (arelent);
5061 rel->sym_ptr_ptr = XNEW (asymbol *);
5062 *rel->sym_ptr_ptr = sym;
5063 /* We assume that all rel->address are host byte offsets. */
5064 rel->address = fixP->fx_frag->fr_address + fixP->fx_where;
5065 rel->address /= OCTETS_PER_BYTE;
5066 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
5067 if (!strcmp (sym->name, section->name))
5068 rel->howto += HOWTO_BANK;
5069
5070 if (!rel->howto)
5071 {
5072 const char *name = S_GET_NAME (fixP->fx_addsy);
5073 if (name == NULL)
5074 name = "<unknown>";
5075 as_fatal ("Cannot generate relocation type for symbol %s, code %s",
5076 name, bfd_get_reloc_code_name (code));
5077 return NULL;
5078 }
5079 return rel;
5080 }
5081
5082 /* Handle cons expressions. */
5083
5084 void
5085 tic54x_cons_fix_new (fragS *frag, int where, int octets, expressionS *expn,
5086 bfd_reloc_code_real_type r)
5087 {
5088 switch (octets)
5089 {
5090 default:
5091 as_bad (_("Unsupported relocation size %d"), octets);
5092 r = BFD_RELOC_TIC54X_16_OF_23;
5093 break;
5094 case 2:
5095 r = BFD_RELOC_TIC54X_16_OF_23;
5096 break;
5097 case 4:
5098 /* TI assembler always uses this, regardless of addressing mode. */
5099 if (emitting_long)
5100 r = BFD_RELOC_TIC54X_23;
5101 else
5102 /* We never want to directly generate this; this is provided for
5103 stabs support only. */
5104 r = BFD_RELOC_32;
5105 break;
5106 }
5107 fix_new_exp (frag, where, octets, expn, 0, r);
5108 }
5109
5110 /* Attempt to simplify or even eliminate a fixup.
5111 To indicate that a fixup has been eliminated, set fixP->fx_done.
5112
5113 If fixp->fx_addsy is non-NULL, we'll have to generate a reloc entry. */
5114
5115 void
5116 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
5117 {
5118 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
5119 valueT val = * valP;
5120
5121 switch (fixP->fx_r_type)
5122 {
5123 default:
5124 as_fatal ("Bad relocation type: 0x%02x", fixP->fx_r_type);
5125 return;
5126 case BFD_RELOC_TIC54X_MS7_OF_23:
5127 val = (val >> 16) & 0x7F;
5128 /* Fall through. */
5129 case BFD_RELOC_TIC54X_16_OF_23:
5130 case BFD_RELOC_16:
5131 bfd_put_16 (stdoutput, val, buf);
5132 /* Indicate what we're actually writing, so that we don't get warnings
5133 about exceeding available space. */
5134 *valP = val & 0xFFFF;
5135 break;
5136 case BFD_RELOC_TIC54X_PARTLS7:
5137 bfd_put_16 (stdoutput,
5138 (bfd_get_16 (stdoutput, buf) & 0xFF80) | (val & 0x7F),
5139 buf);
5140 /* Indicate what we're actually writing, so that we don't get warnings
5141 about exceeding available space. */
5142 *valP = val & 0x7F;
5143 break;
5144 case BFD_RELOC_TIC54X_PARTMS9:
5145 /* TI assembler doesn't shift its encoding for relocatable files, and is
5146 thus incompatible with this implementation's relocatable files. */
5147 bfd_put_16 (stdoutput,
5148 (bfd_get_16 (stdoutput, buf) & 0xFE00) | (val >> 7),
5149 buf);
5150 break;
5151 case BFD_RELOC_32:
5152 case BFD_RELOC_TIC54X_23:
5153 bfd_put_32 (stdoutput,
5154 (bfd_get_32 (stdoutput, buf) & 0xFF800000) | val,
5155 buf);
5156 break;
5157 }
5158
5159 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
5160 fixP->fx_done = 1;
5161 }
5162
5163 /* This is our chance to record section alignment
5164 don't need to do anything here, since BFD does the proper encoding. */
5165
5166 valueT
5167 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT section_size)
5168 {
5169 return section_size;
5170 }
5171
5172 long
5173 md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
5174 {
5175 return 0;
5176 }
5177
5178 /* Mostly little-endian, but longwords (4 octets) get MS word stored
5179 first. */
5180
5181 void
5182 tic54x_number_to_chars (char *buf, valueT val, int n)
5183 {
5184 if (n != 4)
5185 number_to_chars_littleendian (buf, val, n);
5186 else
5187 {
5188 number_to_chars_littleendian (buf , val >> 16 , 2);
5189 number_to_chars_littleendian (buf + 2, val & 0xFFFF, 2);
5190 }
5191 }
5192
5193 int
5194 tic54x_estimate_size_before_relax (fragS *frag ATTRIBUTE_UNUSED,
5195 segT seg ATTRIBUTE_UNUSED)
5196 {
5197 return 0;
5198 }
5199
5200 /* We use this to handle bit allocations which we couldn't handle before due
5201 to symbols being in different frags. return number of octets added. */
5202
5203 int
5204 tic54x_relax_frag (fragS *frag, long stretch ATTRIBUTE_UNUSED)
5205 {
5206 symbolS *sym = frag->fr_symbol;
5207 int growth = 0;
5208 int i;
5209
5210 if (sym != NULL)
5211 {
5212 struct bit_info *bi = (struct bit_info *) frag->fr_opcode;
5213 int bit_offset = frag_bit_offset (frag_prev (frag, bi->seg), bi->seg);
5214 int size = S_GET_VALUE (sym);
5215 fragS *prev_frag = bit_offset_frag (frag_prev (frag, bi->seg), bi->seg);
5216 int available = 16 - bit_offset;
5217
5218 if (symbol_get_frag (sym) != &zero_address_frag
5219 || S_IS_COMMON (sym)
5220 || !S_IS_DEFINED (sym))
5221 as_bad_where (frag->fr_file, frag->fr_line,
5222 _("non-absolute value used with .space/.bes"));
5223
5224 if (size < 0)
5225 {
5226 as_warn (_("negative value ignored in %s"),
5227 bi->type == TYPE_SPACE ? ".space" :
5228 bi->type == TYPE_BES ? ".bes" : ".field");
5229 growth = 0;
5230 frag->tc_frag_data = frag->fr_fix = 0;
5231 return 0;
5232 }
5233
5234 if (bi->type == TYPE_FIELD)
5235 {
5236 /* Bit fields of 16 or larger will have already been handled. */
5237 if (bit_offset != 0 && available >= size)
5238 {
5239 char *p = prev_frag->fr_literal;
5240
5241 valueT value = bi->value;
5242 value <<= available - size;
5243 value |= ((unsigned short) p[1] << 8) | p[0];
5244 md_number_to_chars (p, value, 2);
5245 if ((prev_frag->tc_frag_data += size) == 16)
5246 prev_frag->tc_frag_data = 0;
5247 if (bi->sym)
5248 symbol_set_frag (bi->sym, prev_frag);
5249 /* This frag is no longer used. */
5250 growth = -frag->fr_fix;
5251 frag->fr_fix = 0;
5252 frag->tc_frag_data = 0;
5253 }
5254 else
5255 {
5256 char *p = frag->fr_literal;
5257
5258 valueT value = bi->value << (16 - size);
5259 md_number_to_chars (p, value, 2);
5260 if ((frag->tc_frag_data = size) == 16)
5261 frag->tc_frag_data = 0;
5262 growth = 0;
5263 }
5264 }
5265 else
5266 {
5267 if (bit_offset != 0 && bit_offset < 16)
5268 {
5269 if (available >= size)
5270 {
5271 if ((prev_frag->tc_frag_data += size) == 16)
5272 prev_frag->tc_frag_data = 0;
5273 if (bi->sym)
5274 symbol_set_frag (bi->sym, prev_frag);
5275 /* This frag is no longer used. */
5276 growth = -frag->fr_fix;
5277 frag->fr_fix = 0;
5278 frag->tc_frag_data = 0;
5279 goto getout;
5280 }
5281 if (bi->type == TYPE_SPACE && bi->sym)
5282 symbol_set_frag (bi->sym, prev_frag);
5283 size -= available;
5284 }
5285 growth = (size + 15) / 16 * OCTETS_PER_BYTE - frag->fr_fix;
5286 for (i = 0; i < growth; i++)
5287 frag->fr_literal[i] = 0;
5288 frag->fr_fix = growth;
5289 frag->tc_frag_data = size % 16;
5290 /* Make sure any BES label points to the LAST word allocated. */
5291 if (bi->type == TYPE_BES && bi->sym)
5292 S_SET_VALUE (bi->sym, frag->fr_fix / OCTETS_PER_BYTE - 1);
5293 }
5294 getout:
5295 frag->fr_symbol = 0;
5296 frag->fr_opcode = 0;
5297 free ((void *) bi);
5298 }
5299 return growth;
5300 }
5301
5302 void
5303 tic54x_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
5304 segT seg ATTRIBUTE_UNUSED,
5305 fragS *frag)
5306 {
5307 /* Offset is in bytes. */
5308 frag->fr_offset = (frag->fr_next->fr_address
5309 - frag->fr_address
5310 - frag->fr_fix) / frag->fr_var;
5311 if (frag->fr_offset < 0)
5312 {
5313 as_bad_where (frag->fr_file, frag->fr_line,
5314 _("attempt to .space/.bes backwards? (%ld)"),
5315 (long) frag->fr_offset);
5316 }
5317 frag->fr_type = rs_space;
5318 }
5319
5320 /* We need to avoid having labels defined for certain directives/pseudo-ops
5321 since once the label is defined, it's in the symbol table for good. TI
5322 syntax puts the symbol *before* the pseudo (which is kinda like MRI syntax,
5323 I guess, except I've never seen a definition of MRI syntax).
5324
5325 Don't allow labels to start with '.' */
5326
5327 int
5328 tic54x_start_label (char * label_start, int nul_char, int next_char)
5329 {
5330 char *rest;
5331
5332 /* If within .struct/.union, no auto line labels, please. */
5333 if (current_stag != NULL)
5334 return 0;
5335
5336 /* Disallow labels starting with "." */
5337 if (next_char != ':')
5338 {
5339 if (*label_start == '.')
5340 {
5341 as_bad (_("Invalid label '%s'"), label_start);
5342 return 0;
5343 }
5344 }
5345
5346 if (is_end_of_line[(unsigned char) next_char])
5347 return 1;
5348
5349 rest = input_line_pointer;
5350 if (nul_char == '"')
5351 ++rest;
5352 while (ISSPACE (next_char))
5353 next_char = *++rest;
5354 if (next_char != '.')
5355 return 1;
5356
5357 /* Don't let colon () define a label for any of these... */
5358 return ((strncasecmp (rest, ".tag", 4) != 0 || !ISSPACE (rest[4]))
5359 && (strncasecmp (rest, ".struct", 7) != 0 || !ISSPACE (rest[7]))
5360 && (strncasecmp (rest, ".union", 6) != 0 || !ISSPACE (rest[6]))
5361 && (strncasecmp (rest, ".macro", 6) != 0 || !ISSPACE (rest[6]))
5362 && (strncasecmp (rest, ".set", 4) != 0 || !ISSPACE (rest[4]))
5363 && (strncasecmp (rest, ".equ", 4) != 0 || !ISSPACE (rest[4])));
5364 }
This page took 0.143556 seconds and 4 git commands to generate.