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