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