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