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