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