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