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