Oops - omitted by accident from the previous msp430 patch
[deliverable/binutils-gdb.git] / gas / macro.c
CommitLineData
fea17916 1/* macro.c - macro support for gas
2da5c037
AM
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 Free Software Foundation, Inc.
252b5132
RH
4
5 Written by Steve and Judy Chamberlain of Cygnus Support,
6 sac@cygnus.com
7
8 This file is part of GAS, the GNU Assembler.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
252b5132
RH
24
25#include "config.h"
26
fa6e9318 27#ifndef __GNUC__
252b5132
RH
28# if HAVE_ALLOCA_H
29# include <alloca.h>
30# else
31# ifdef _AIX
fa6e9318
AM
32/* Indented so that pre-ansi C compilers will ignore it, rather than
33 choke on it. Some versions of AIX require this to be the first
34 thing in the file. */
252b5132
RH
35 #pragma alloca
36# else
37# ifndef alloca /* predefined by HP cc +Olibcalls */
38# if !defined (__STDC__) && !defined (__hpux)
39extern char *alloca ();
40# else
41extern void *alloca ();
42# endif /* __STDC__, __hpux */
43# endif /* alloca */
44# endif /* _AIX */
45# endif /* HAVE_ALLOCA_H */
fa6e9318 46#endif /* __GNUC__ */
252b5132
RH
47
48#include <stdio.h>
49#ifdef HAVE_STRING_H
50#include <string.h>
51#else
52#include <strings.h>
53#endif
252b5132
RH
54#ifdef HAVE_STDLIB_H
55#include <stdlib.h>
56#endif
89658e52 57#include "as.h"
252b5132 58#include "libiberty.h"
3882b010 59#include "safe-ctype.h"
252b5132
RH
60#include "sb.h"
61#include "hash.h"
62#include "macro.h"
63
64#include "asintl.h"
65
66/* The routines in this file handle macro definition and expansion.
fea17916 67 They are called by gas. */
252b5132 68
252b5132
RH
69/* Internal functions. */
70
254d758c
KH
71static int get_token (int, sb *, sb *);
72static int getstring (int, sb *, sb *);
be03cc84 73static int get_any_string (int, sb *, sb *);
6eaeac8a
JB
74static formal_entry *new_formal (void);
75static void del_formal (formal_entry *);
254d758c
KH
76static int do_formals (macro_entry *, int, sb *);
77static int get_apost_token (int, sb *, sb *, int);
78static int sub_actual (int, sb *, sb *, struct hash_control *, int, sb *, int);
252b5132 79static const char *macro_expand_body
02ddf156 80 (sb *, sb *, formal_entry *, struct hash_control *, const macro_entry *);
254d758c 81static const char *macro_expand (int, sb *, macro_entry *, sb *);
e6ca91be 82static void free_macro(macro_entry *);
252b5132
RH
83
84#define ISWHITE(x) ((x) == ' ' || (x) == '\t')
85
86#define ISSEP(x) \
87 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
88 || (x) == ')' || (x) == '(' \
89 || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
90
91#define ISBASE(x) \
92 ((x) == 'b' || (x) == 'B' \
93 || (x) == 'q' || (x) == 'Q' \
94 || (x) == 'h' || (x) == 'H' \
95 || (x) == 'd' || (x) == 'D')
96
97/* The macro hash table. */
98
c1d05a60 99struct hash_control *macro_hash;
252b5132
RH
100
101/* Whether any macros have been defined. */
102
103int macro_defined;
104
fea17916 105/* Whether we are in alternate syntax mode. */
252b5132
RH
106
107static int macro_alternate;
108
109/* Whether we are in MRI mode. */
110
111static int macro_mri;
112
113/* Whether we should strip '@' characters. */
114
115static int macro_strip_at;
116
117/* Function to use to parse an expression. */
118
254d758c 119static int (*macro_expr) (const char *, int, sb *, int *);
252b5132
RH
120
121/* Number of macro expansions that have been done. */
122
123static int macro_number;
124
125/* Initialize macro processing. */
126
127void
254d758c
KH
128macro_init (int alternate, int mri, int strip_at,
129 int (*expr) (const char *, int, sb *, int *))
252b5132
RH
130{
131 macro_hash = hash_new ();
132 macro_defined = 0;
133 macro_alternate = alternate;
134 macro_mri = mri;
135 macro_strip_at = strip_at;
136 macro_expr = expr;
137}
138
caa32fe5
NC
139/* Switch in and out of alternate mode on the fly. */
140
141void
2766e5e4 142macro_set_alternate (int alternate)
caa32fe5
NC
143{
144 macro_alternate = alternate;
145}
146
252b5132
RH
147/* Switch in and out of MRI mode on the fly. */
148
149void
254d758c 150macro_mri_mode (int mri)
252b5132
RH
151{
152 macro_mri = mri;
153}
154
155/* Read input lines till we get to a TO string.
156 Increase nesting depth if we get a FROM string.
157 Put the results into sb at PTR.
ca3bc58f 158 FROM may be NULL (or will be ignored) if TO is "ENDR".
252b5132
RH
159 Add a new input line to an sb using GET_LINE.
160 Return 1 on success, 0 on unexpected EOF. */
161
162int
254d758c
KH
163buffer_and_nest (const char *from, const char *to, sb *ptr,
164 int (*get_line) (sb *))
252b5132 165{
ca3bc58f 166 int from_len;
252b5132
RH
167 int to_len = strlen (to);
168 int depth = 1;
169 int line_start = ptr->len;
170
171 int more = get_line (ptr);
172
ca3bc58f
JB
173 if (to_len == 4 && strcasecmp(to, "ENDR") == 0)
174 {
175 from = NULL;
176 from_len = 0;
177 }
178 else
179 from_len = strlen (from);
180
252b5132
RH
181 while (more)
182 {
29f8404c 183 /* Try and find the first pseudo op on the line. */
252b5132
RH
184 int i = line_start;
185
ca3bc58f 186 if (! NO_PSEUDO_DOT && ! flag_m68k_mri)
252b5132
RH
187 {
188 /* With normal syntax we can suck what we want till we get
189 to the dot. With the alternate, labels have to start in
ca3bc58f 190 the first column, since we can't tell what's a label and
29f8404c 191 whats a pseudoop. */
252b5132 192
5e75c3ab
JB
193 if (! LABELS_WITHOUT_COLONS)
194 {
195 /* Skip leading whitespace. */
196 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
197 i++;
198 }
252b5132 199
5e75c3ab
JB
200 for (;;)
201 {
202 /* Skip over a label, if any. */
203 if (i >= ptr->len || ! is_name_beginner (ptr->ptr[i]))
204 break;
205 i++;
206 while (i < ptr->len && is_part_of_name (ptr->ptr[i]))
207 i++;
208 if (i < ptr->len && is_name_ender (ptr->ptr[i]))
209 i++;
210 if (LABELS_WITHOUT_COLONS)
211 break;
212 /* Skip whitespace. */
213 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
214 i++;
215 /* Check for the colon. */
216 if (i >= ptr->len || ptr->ptr[i] != ':')
217 {
218 i = line_start;
219 break;
220 }
221 i++;
222 line_start = i;
223 }
252b5132
RH
224
225 }
29f8404c 226 /* Skip trailing whitespace. */
252b5132
RH
227 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
228 i++;
229
230 if (i < ptr->len && (ptr->ptr[i] == '.'
ca3bc58f 231 || NO_PSEUDO_DOT
252b5132
RH
232 || macro_mri))
233 {
ca3bc58f 234 if (! flag_m68k_mri && ptr->ptr[i] == '.')
29f8404c 235 i++;
ca3bc58f
JB
236 if (from == NULL
237 && strncasecmp (ptr->ptr + i, "IRPC", from_len = 4) != 0
238 && strncasecmp (ptr->ptr + i, "IRP", from_len = 3) != 0
239 && strncasecmp (ptr->ptr + i, "IREPC", from_len = 5) != 0
240 && strncasecmp (ptr->ptr + i, "IREP", from_len = 4) != 0
241 && strncasecmp (ptr->ptr + i, "REPT", from_len = 4) != 0
242 && strncasecmp (ptr->ptr + i, "REP", from_len = 3) != 0)
243 from_len = 0;
244 if ((from != NULL
245 ? strncasecmp (ptr->ptr + i, from, from_len) == 0
246 : from_len > 0)
29f8404c 247 && (ptr->len == (i + from_len)
5e75c3ab
JB
248 || ! (is_part_of_name (ptr->ptr[i + from_len])
249 || is_name_ender (ptr->ptr[i + from_len]))))
252b5132 250 depth++;
c1eae114 251 if (strncasecmp (ptr->ptr + i, to, to_len) == 0
29f8404c 252 && (ptr->len == (i + to_len)
5e75c3ab
JB
253 || ! (is_part_of_name (ptr->ptr[i + to_len])
254 || is_name_ender (ptr->ptr[i + to_len]))))
252b5132
RH
255 {
256 depth--;
257 if (depth == 0)
258 {
29f8404c 259 /* Reset the string to not include the ending rune. */
252b5132
RH
260 ptr->len = line_start;
261 break;
262 }
263 }
264 }
265
0822d075
NC
266 /* Add the original end-of-line char to the end and keep running. */
267 sb_add_char (ptr, more);
252b5132
RH
268 line_start = ptr->len;
269 more = get_line (ptr);
270 }
271
272 /* Return 1 on success, 0 on unexpected EOF. */
273 return depth == 0;
274}
275
276/* Pick up a token. */
277
278static int
254d758c 279get_token (int idx, sb *in, sb *name)
252b5132
RH
280{
281 if (idx < in->len
5e75c3ab 282 && is_name_beginner (in->ptr[idx]))
252b5132
RH
283 {
284 sb_add_char (name, in->ptr[idx++]);
285 while (idx < in->len
5e75c3ab
JB
286 && is_part_of_name (in->ptr[idx]))
287 {
288 sb_add_char (name, in->ptr[idx++]);
289 }
290 if (idx < in->len
291 && is_name_ender (in->ptr[idx]))
252b5132
RH
292 {
293 sb_add_char (name, in->ptr[idx++]);
294 }
295 }
29f8404c 296 /* Ignore trailing &. */
252b5132
RH
297 if (macro_alternate && idx < in->len && in->ptr[idx] == '&')
298 idx++;
299 return idx;
300}
301
302/* Pick up a string. */
303
304static int
254d758c 305getstring (int idx, sb *in, sb *acc)
252b5132 306{
252b5132 307 while (idx < in->len
29f8404c 308 && (in->ptr[idx] == '"'
df40eaf9 309 || in->ptr[idx] == '('
252b5132
RH
310 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
311 || (in->ptr[idx] == '\'' && macro_alternate)))
312 {
df40eaf9
NC
313 if (in->ptr[idx] == '<'
314 || in->ptr[idx] == '(')
252b5132
RH
315 {
316 int nest = 0;
df40eaf9
NC
317 char start_char = in->ptr[idx];
318 char end_char = in->ptr[idx] == '<' ? '>' : ')';
319
252b5132 320 idx++;
df40eaf9 321 while ((in->ptr[idx] != end_char || nest)
252b5132
RH
322 && idx < in->len)
323 {
324 if (in->ptr[idx] == '!')
325 {
29f8404c 326 idx++;
252b5132
RH
327 sb_add_char (acc, in->ptr[idx++]);
328 }
329 else
330 {
df40eaf9 331 if (in->ptr[idx] == end_char)
252b5132 332 nest--;
df40eaf9 333 if (in->ptr[idx] == start_char)
252b5132
RH
334 nest++;
335 sb_add_char (acc, in->ptr[idx++]);
336 }
337 }
338 idx++;
339 }
340 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
341 {
342 char tchar = in->ptr[idx];
c06ae4f2 343 int escaped = 0;
29f8404c 344
252b5132 345 idx++;
29f8404c 346
252b5132
RH
347 while (idx < in->len)
348 {
29f8404c 349 if (in->ptr[idx - 1] == '\\')
c06ae4f2
UC
350 escaped ^= 1;
351 else
352 escaped = 0;
353
252b5132
RH
354 if (macro_alternate && in->ptr[idx] == '!')
355 {
e972090a 356 idx ++;
29f8404c 357
1994a7c7
NC
358 sb_add_char (acc, in->ptr[idx]);
359
e972090a 360 idx ++;
252b5132 361 }
c06ae4f2
UC
362 else if (escaped && in->ptr[idx] == tchar)
363 {
364 sb_add_char (acc, tchar);
e972090a 365 idx ++;
c06ae4f2 366 }
252b5132
RH
367 else
368 {
369 if (in->ptr[idx] == tchar)
370 {
e972090a 371 idx ++;
29f8404c 372
252b5132
RH
373 if (idx >= in->len || in->ptr[idx] != tchar)
374 break;
375 }
29f8404c 376
252b5132 377 sb_add_char (acc, in->ptr[idx]);
e972090a 378 idx ++;
252b5132
RH
379 }
380 }
381 }
382 }
29f8404c 383
252b5132
RH
384 return idx;
385}
386
387/* Fetch string from the input stream,
388 rules:
389 'Bxyx<whitespace> -> return 'Bxyza
df40eaf9
NC
390 %<expr> -> return string of decimal value of <expr>
391 "string" -> return string
392 (string) -> return string
393 xyx<whitespace> -> return xyz. */
252b5132
RH
394
395static int
be03cc84 396get_any_string (int idx, sb *in, sb *out)
252b5132
RH
397{
398 sb_reset (out);
399 idx = sb_skip_white (idx, in);
400
401 if (idx < in->len)
402 {
9df59bba 403 if (in->len > idx + 2 && in->ptr[idx + 1] == '\'' && ISBASE (in->ptr[idx]))
252b5132
RH
404 {
405 while (!ISSEP (in->ptr[idx]))
406 sb_add_char (out, in->ptr[idx++]);
407 }
be03cc84 408 else if (in->ptr[idx] == '%' && macro_alternate)
252b5132
RH
409 {
410 int val;
411 char buf[20];
df40eaf9 412
29f8404c 413 /* Turns the next expression into a string. */
06f030db 414 /* xgettext: no-c-format */
252b5132
RH
415 idx = (*macro_expr) (_("% operator needs absolute expression"),
416 idx + 1,
417 in,
418 &val);
d1a6c242 419 sprintf (buf, "%d", val);
252b5132
RH
420 sb_add_string (out, buf);
421 }
422 else if (in->ptr[idx] == '"'
df40eaf9 423 || in->ptr[idx] == '('
252b5132
RH
424 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
425 || (macro_alternate && in->ptr[idx] == '\''))
426 {
be03cc84 427 if (macro_alternate && ! macro_strip_at)
252b5132 428 {
29f8404c
KH
429 /* Keep the quotes. */
430 sb_add_char (out, '\"');
252b5132
RH
431
432 idx = getstring (idx, in, out);
29f8404c 433 sb_add_char (out, '\"');
252b5132
RH
434 }
435 else
436 {
437 idx = getstring (idx, in, out);
438 }
439 }
29f8404c 440 else
252b5132 441 {
29f8404c 442 while (idx < in->len
be03cc84
JB
443 && in->ptr[idx] != ' '
444 && in->ptr[idx] != '\t'
445 && in->ptr[idx] != ','
446 && (in->ptr[idx] != '<'
447 || (! macro_alternate && ! macro_mri)))
252b5132 448 {
29f8404c 449 if (in->ptr[idx] == '"'
252b5132
RH
450 || in->ptr[idx] == '\'')
451 {
452 char tchar = in->ptr[idx];
df40eaf9 453
252b5132
RH
454 sb_add_char (out, in->ptr[idx++]);
455 while (idx < in->len
456 && in->ptr[idx] != tchar)
29f8404c 457 sb_add_char (out, in->ptr[idx++]);
252b5132 458 if (idx == in->len)
29f8404c 459 return idx;
252b5132
RH
460 }
461 sb_add_char (out, in->ptr[idx++]);
462 }
463 }
464 }
465
466 return idx;
467}
468
6eaeac8a
JB
469/* Allocate a new formal. */
470
471static formal_entry *
472new_formal (void)
473{
474 formal_entry *formal;
475
476 formal = xmalloc (sizeof (formal_entry));
477
478 sb_new (&formal->name);
479 sb_new (&formal->def);
480 sb_new (&formal->actual);
481 formal->next = NULL;
482 formal->type = FORMAL_OPTIONAL;
483 return formal;
484}
485
486/* Free a formal. */
487
488static void
489del_formal (formal_entry *formal)
490{
491 sb_kill (&formal->actual);
492 sb_kill (&formal->def);
493 sb_kill (&formal->name);
494 free (formal);
495}
496
252b5132
RH
497/* Pick up the formal parameters of a macro definition. */
498
499static int
254d758c 500do_formals (macro_entry *macro, int idx, sb *in)
252b5132
RH
501{
502 formal_entry **p = &macro->formals;
02ddf156 503 const char *name;
252b5132 504
057f53c1 505 idx = sb_skip_white (idx, in);
252b5132
RH
506 while (idx < in->len)
507 {
6eaeac8a 508 formal_entry *formal = new_formal ();
057f53c1 509 int cidx;
252b5132 510
252b5132
RH
511 idx = get_token (idx, in, &formal->name);
512 if (formal->name.len == 0)
057f53c1
JB
513 {
514 if (macro->formal_count)
515 --idx;
516 break;
517 }
252b5132 518 idx = sb_skip_white (idx, in);
057f53c1 519 /* This is a formal. */
6eaeac8a
JB
520 name = sb_terminate (&formal->name);
521 if (! macro_mri
522 && idx < in->len
523 && in->ptr[idx] == ':'
524 && (! is_name_beginner (':')
525 || idx + 1 >= in->len
526 || ! is_part_of_name (in->ptr[idx + 1])))
527 {
528 /* Got a qualifier. */
529 sb qual;
530
531 sb_new (&qual);
532 idx = get_token (sb_skip_white (idx + 1, in), in, &qual);
533 sb_terminate (&qual);
534 if (qual.len == 0)
535 as_bad_where (macro->file,
536 macro->line,
537 _("Missing parameter qualifier for `%s' in macro `%s'"),
538 name,
539 macro->name);
540 else if (strcmp (qual.ptr, "req") == 0)
541 formal->type = FORMAL_REQUIRED;
542 else if (strcmp (qual.ptr, "vararg") == 0)
543 formal->type = FORMAL_VARARG;
544 else
545 as_bad_where (macro->file,
546 macro->line,
547 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
548 qual.ptr,
549 name,
550 macro->name);
551 sb_kill (&qual);
552 idx = sb_skip_white (idx, in);
553 }
057f53c1 554 if (idx < in->len && in->ptr[idx] == '=')
252b5132 555 {
057f53c1 556 /* Got a default. */
be03cc84 557 idx = get_any_string (idx + 1, in, &formal->def);
057f53c1 558 idx = sb_skip_white (idx, in);
6eaeac8a
JB
559 if (formal->type == FORMAL_REQUIRED)
560 {
561 sb_reset (&formal->def);
562 as_warn_where (macro->file,
563 macro->line,
564 _("Pointless default value for required parameter `%s' in macro `%s'"),
565 name,
566 macro->name);
567 }
252b5132
RH
568 }
569
29f8404c 570 /* Add to macro's hash table. */
02ddf156
JB
571 if (! hash_find (macro->formal_hash, name))
572 hash_jam (macro->formal_hash, name, formal);
573 else
574 as_bad_where (macro->file,
575 macro->line,
576 _("A parameter named `%s' already exists for macro `%s'"),
577 name,
578 macro->name);
252b5132 579
057f53c1 580 formal->index = macro->formal_count++;
6eaeac8a
JB
581 *p = formal;
582 p = &formal->next;
583 if (formal->type == FORMAL_VARARG)
584 break;
057f53c1 585 cidx = idx;
252b5132 586 idx = sb_skip_comma (idx, in);
057f53c1
JB
587 if (idx != cidx && idx >= in->len)
588 {
589 idx = cidx;
590 break;
591 }
252b5132
RH
592 }
593
594 if (macro_mri)
595 {
6eaeac8a 596 formal_entry *formal = new_formal ();
252b5132
RH
597
598 /* Add a special NARG formal, which macro_expand will set to the
599 number of arguments. */
252b5132
RH
600 /* The same MRI assemblers which treat '@' characters also use
601 the name $NARG. At least until we find an exception. */
602 if (macro_strip_at)
603 name = "$NARG";
604 else
605 name = "NARG";
606
607 sb_add_string (&formal->name, name);
608
29f8404c 609 /* Add to macro's hash table. */
02ddf156
JB
610 if (hash_find (macro->formal_hash, name))
611 as_bad_where (macro->file,
612 macro->line,
613 _("Reserved word `%s' used as parameter in macro `%s'"),
614 name,
615 macro->name);
252b5132
RH
616 hash_jam (macro->formal_hash, name, formal);
617
618 formal->index = NARG_INDEX;
619 *p = formal;
252b5132
RH
620 }
621
622 return idx;
623}
624
625/* Define a new macro. Returns NULL on success, otherwise returns an
626 error message. If NAMEP is not NULL, *NAMEP is set to the name of
627 the macro which was defined. */
628
629const char *
254d758c 630define_macro (int idx, sb *in, sb *label,
02ddf156
JB
631 int (*get_line) (sb *),
632 char *file, unsigned int line,
633 const char **namep)
252b5132
RH
634{
635 macro_entry *macro;
636 sb name;
02ddf156 637 const char *error = NULL;
252b5132
RH
638
639 macro = (macro_entry *) xmalloc (sizeof (macro_entry));
640 sb_new (&macro->sub);
641 sb_new (&name);
02ddf156
JB
642 macro->file = file;
643 macro->line = line;
252b5132
RH
644
645 macro->formal_count = 0;
646 macro->formals = 0;
e6ca91be 647 macro->formal_hash = hash_new ();
252b5132
RH
648
649 idx = sb_skip_white (idx, in);
650 if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
02ddf156 651 error = _("unexpected end of file in macro `%s' definition");
252b5132
RH
652 if (label != NULL && label->len != 0)
653 {
654 sb_add_sb (&name, label);
02ddf156 655 macro->name = sb_terminate (&name);
252b5132
RH
656 if (idx < in->len && in->ptr[idx] == '(')
657 {
29f8404c 658 /* It's the label: MACRO (formals,...) sort */
252b5132 659 idx = do_formals (macro, idx + 1, in);
02ddf156
JB
660 if (idx < in->len && in->ptr[idx] == ')')
661 idx = sb_skip_white (idx + 1, in);
662 else if (!error)
663 error = _("missing `)' after formals in macro definition `%s'");
252b5132
RH
664 }
665 else
666 {
29f8404c 667 /* It's the label: MACRO formals,... sort */
252b5132
RH
668 idx = do_formals (macro, idx, in);
669 }
670 }
671 else
672 {
057f53c1
JB
673 int cidx;
674
252b5132 675 idx = get_token (idx, in, &name);
02ddf156 676 macro->name = sb_terminate (&name);
057f53c1 677 if (name.len == 0)
02ddf156 678 error = _("Missing macro name");
057f53c1
JB
679 cidx = sb_skip_white (idx, in);
680 idx = sb_skip_comma (cidx, in);
681 if (idx == cidx || idx < in->len)
682 idx = do_formals (macro, idx, in);
683 else
684 idx = cidx;
252b5132 685 }
02ddf156
JB
686 if (!error && idx < in->len)
687 error = _("Bad parameter list for macro `%s'");
252b5132 688
29f8404c 689 /* And stick it in the macro hash table. */
252b5132 690 for (idx = 0; idx < name.len; idx++)
3882b010 691 name.ptr[idx] = TOLOWER (name.ptr[idx]);
02ddf156
JB
692 if (hash_find (macro_hash, macro->name))
693 error = _("Macro `%s' was already defined");
694 if (!error)
695 error = hash_jam (macro_hash, macro->name, (PTR) macro);
252b5132
RH
696
697 if (namep != NULL)
02ddf156
JB
698 *namep = macro->name;
699
700 if (!error)
701 macro_defined = 1;
702 else
703 free_macro (macro);
252b5132 704
02ddf156 705 return error;
252b5132
RH
706}
707
708/* Scan a token, and then skip KIND. */
709
710static int
254d758c 711get_apost_token (int idx, sb *in, sb *name, int kind)
252b5132
RH
712{
713 idx = get_token (idx, in, name);
714 if (idx < in->len
715 && in->ptr[idx] == kind
716 && (! macro_mri || macro_strip_at)
717 && (! macro_strip_at || kind == '@'))
718 idx++;
719 return idx;
720}
721
722/* Substitute the actual value for a formal parameter. */
723
724static int
254d758c
KH
725sub_actual (int start, sb *in, sb *t, struct hash_control *formal_hash,
726 int kind, sb *out, int copyifnotthere)
252b5132
RH
727{
728 int src;
729 formal_entry *ptr;
730
731 src = get_apost_token (start, in, t, kind);
732 /* See if it's in the macro's hash table, unless this is
733 macro_strip_at and kind is '@' and the token did not end in '@'. */
734 if (macro_strip_at
735 && kind == '@'
736 && (src == start || in->ptr[src - 1] != '@'))
737 ptr = NULL;
738 else
739 ptr = (formal_entry *) hash_find (formal_hash, sb_terminate (t));
740 if (ptr)
741 {
742 if (ptr->actual.len)
743 {
744 sb_add_sb (out, &ptr->actual);
745 }
746 else
747 {
748 sb_add_sb (out, &ptr->def);
749 }
750 }
751 else if (kind == '&')
752 {
753 /* Doing this permits people to use & in macro bodies. */
754 sb_add_char (out, '&');
c1ed1235 755 sb_add_sb (out, t);
252b5132
RH
756 }
757 else if (copyifnotthere)
758 {
759 sb_add_sb (out, t);
760 }
29f8404c 761 else
252b5132
RH
762 {
763 sb_add_char (out, '\\');
764 sb_add_sb (out, t);
765 }
766 return src;
767}
768
769/* Expand the body of a macro. */
770
771static const char *
254d758c 772macro_expand_body (sb *in, sb *out, formal_entry *formals,
02ddf156 773 struct hash_control *formal_hash, const macro_entry *macro)
252b5132
RH
774{
775 sb t;
02ddf156 776 int src = 0, inquote = 0, macro_line = 0;
252b5132 777 formal_entry *loclist = NULL;
02ddf156 778 const char *err = NULL;
252b5132
RH
779
780 sb_new (&t);
781
02ddf156 782 while (src < in->len && !err)
252b5132
RH
783 {
784 if (in->ptr[src] == '&')
785 {
786 sb_reset (&t);
787 if (macro_mri)
788 {
789 if (src + 1 < in->len && in->ptr[src + 1] == '&')
790 src = sub_actual (src + 2, in, &t, formal_hash, '\'', out, 1);
791 else
792 sb_add_char (out, in->ptr[src++]);
793 }
794 else
795 {
796 /* FIXME: Why do we do this? */
02ddf156
JB
797 /* At least in alternate mode this seems correct; without this
798 one can't append a literal to a parameter. */
252b5132
RH
799 src = sub_actual (src + 1, in, &t, formal_hash, '&', out, 0);
800 }
801 }
802 else if (in->ptr[src] == '\\')
803 {
804 src++;
5e75c3ab 805 if (src < in->len && in->ptr[src] == '(')
252b5132 806 {
29f8404c 807 /* Sub in till the next ')' literally. */
252b5132
RH
808 src++;
809 while (src < in->len && in->ptr[src] != ')')
810 {
811 sb_add_char (out, in->ptr[src++]);
812 }
02ddf156 813 if (src < in->len)
252b5132 814 src++;
02ddf156
JB
815 else if (!macro)
816 err = _("missing `)'");
252b5132 817 else
02ddf156 818 as_bad_where (macro->file, macro->line + macro_line, _("missing `)'"));
252b5132 819 }
5e75c3ab 820 else if (src < in->len && in->ptr[src] == '@')
252b5132 821 {
29f8404c 822 /* Sub in the macro invocation number. */
252b5132
RH
823
824 char buffer[10];
825 src++;
a2984248 826 sprintf (buffer, "%d", macro_number);
252b5132
RH
827 sb_add_string (out, buffer);
828 }
5e75c3ab 829 else if (src < in->len && in->ptr[src] == '&')
252b5132
RH
830 {
831 /* This is a preprocessor variable name, we don't do them
29f8404c 832 here. */
252b5132
RH
833 sb_add_char (out, '\\');
834 sb_add_char (out, '&');
835 src++;
836 }
5e75c3ab 837 else if (macro_mri && src < in->len && ISALNUM (in->ptr[src]))
252b5132
RH
838 {
839 int ind;
840 formal_entry *f;
841
3882b010 842 if (ISDIGIT (in->ptr[src]))
252b5132 843 ind = in->ptr[src] - '0';
3882b010 844 else if (ISUPPER (in->ptr[src]))
252b5132
RH
845 ind = in->ptr[src] - 'A' + 10;
846 else
847 ind = in->ptr[src] - 'a' + 10;
848 ++src;
849 for (f = formals; f != NULL; f = f->next)
850 {
851 if (f->index == ind - 1)
852 {
853 if (f->actual.len != 0)
854 sb_add_sb (out, &f->actual);
855 else
856 sb_add_sb (out, &f->def);
857 break;
858 }
859 }
860 }
861 else
862 {
863 sb_reset (&t);
864 src = sub_actual (src, in, &t, formal_hash, '\'', out, 0);
865 }
866 }
867 else if ((macro_alternate || macro_mri)
5e75c3ab 868 && is_name_beginner (in->ptr[src])
252b5132
RH
869 && (! inquote
870 || ! macro_strip_at
871 || (src > 0 && in->ptr[src - 1] == '@')))
872 {
02ddf156 873 if (! macro
252b5132
RH
874 || src + 5 >= in->len
875 || strncasecmp (in->ptr + src, "LOCAL", 5) != 0
876 || ! ISWHITE (in->ptr[src + 5]))
877 {
878 sb_reset (&t);
879 src = sub_actual (src, in, &t, formal_hash,
880 (macro_strip_at && inquote) ? '@' : '\'',
881 out, 1);
882 }
883 else
884 {
252b5132 885 src = sb_skip_white (src + 5, in);
fea17916 886 while (in->ptr[src] != '\n')
252b5132 887 {
02ddf156 888 const char *name;
6eaeac8a 889 formal_entry *f = new_formal ();
252b5132 890
252b5132 891 src = get_token (src, in, &f->name);
02ddf156
JB
892 name = sb_terminate (&f->name);
893 if (! hash_find (formal_hash, name))
894 {
895 static int loccnt;
896 char buf[20];
252b5132 897
02ddf156
JB
898 f->index = LOCAL_INDEX;
899 f->next = loclist;
900 loclist = f;
901
902 sprintf (buf, IS_ELF ? ".LL%04x" : "LL%04x", ++loccnt);
903 sb_add_string (&f->actual, buf);
904
905 err = hash_jam (formal_hash, name, f);
906 if (err != NULL)
907 break;
908 }
909 else
910 {
911 as_bad_where (macro->file,
912 macro->line + macro_line,
913 _("`%s' was already used as parameter (or another local) name"),
914 name);
6eaeac8a 915 del_formal (f);
02ddf156 916 }
252b5132
RH
917
918 src = sb_skip_comma (src, in);
919 }
920 }
921 }
252b5132
RH
922 else if (in->ptr[src] == '"'
923 || (macro_mri && in->ptr[src] == '\''))
924 {
925 inquote = !inquote;
926 sb_add_char (out, in->ptr[src++]);
927 }
928 else if (in->ptr[src] == '@' && macro_strip_at)
929 {
930 ++src;
931 if (src < in->len
932 && in->ptr[src] == '@')
933 {
934 sb_add_char (out, '@');
935 ++src;
936 }
937 }
938 else if (macro_mri
939 && in->ptr[src] == '='
940 && src + 1 < in->len
941 && in->ptr[src + 1] == '=')
942 {
943 formal_entry *ptr;
944
945 sb_reset (&t);
946 src = get_token (src + 2, in, &t);
947 ptr = (formal_entry *) hash_find (formal_hash, sb_terminate (&t));
948 if (ptr == NULL)
949 {
950 /* FIXME: We should really return a warning string here,
951 but we can't, because the == might be in the MRI
952 comment field, and, since the nature of the MRI
953 comment field depends upon the exact instruction
954 being used, we don't have enough information here to
955 figure out whether it is or not. Instead, we leave
956 the == in place, which should cause a syntax error if
957 it is not in a comment. */
958 sb_add_char (out, '=');
959 sb_add_char (out, '=');
960 sb_add_sb (out, &t);
961 }
962 else
963 {
964 if (ptr->actual.len)
965 {
966 sb_add_string (out, "-1");
967 }
968 else
969 {
970 sb_add_char (out, '0');
971 }
972 }
973 }
974 else
975 {
02ddf156
JB
976 if (in->ptr[src] == '\n')
977 ++macro_line;
252b5132
RH
978 sb_add_char (out, in->ptr[src++]);
979 }
980 }
981
982 sb_kill (&t);
983
984 while (loclist != NULL)
985 {
986 formal_entry *f;
987
988 f = loclist->next;
1af6dcd2
ILT
989 /* Setting the value to NULL effectively deletes the entry. We
990 avoid calling hash_delete because it doesn't reclaim memory. */
991 hash_jam (formal_hash, sb_terminate (&loclist->name), NULL);
6eaeac8a 992 del_formal (loclist);
252b5132
RH
993 loclist = f;
994 }
995
02ddf156 996 return err;
252b5132
RH
997}
998
999/* Assign values to the formal parameters of a macro, and expand the
1000 body. */
1001
1002static const char *
254d758c 1003macro_expand (int idx, sb *in, macro_entry *m, sb *out)
252b5132
RH
1004{
1005 sb t;
1006 formal_entry *ptr;
1007 formal_entry *f;
1008 int is_positional = 0;
1009 int is_keyword = 0;
1010 int narg = 0;
6eaeac8a 1011 const char *err = NULL;
252b5132
RH
1012
1013 sb_new (&t);
29f8404c
KH
1014
1015 /* Reset any old value the actuals may have. */
252b5132 1016 for (f = m->formals; f; f = f->next)
29f8404c 1017 sb_reset (&f->actual);
252b5132
RH
1018 f = m->formals;
1019 while (f != NULL && f->index < 0)
1020 f = f->next;
1021
1022 if (macro_mri)
1023 {
1024 /* The macro may be called with an optional qualifier, which may
1025 be referred to in the macro body as \0. */
1026 if (idx < in->len && in->ptr[idx] == '.')
d1a6c242
KH
1027 {
1028 /* The Microtec assembler ignores this if followed by a white space.
1029 (Macro invocation with empty extension) */
1030 idx++;
1031 if ( idx < in->len
1032 && in->ptr[idx] != ' '
1033 && in->ptr[idx] != '\t')
1034 {
6eaeac8a 1035 formal_entry *n = new_formal ();
d1a6c242 1036
d1a6c242
KH
1037 n->index = QUAL_INDEX;
1038
1039 n->next = m->formals;
1040 m->formals = n;
1041
be03cc84 1042 idx = get_any_string (idx, in, &n->actual);
d1a6c242
KH
1043 }
1044 }
1045 }
252b5132 1046
29f8404c 1047 /* Peel off the actuals and store them away in the hash tables' actuals. */
252b5132 1048 idx = sb_skip_white (idx, in);
fea17916 1049 while (idx < in->len)
252b5132
RH
1050 {
1051 int scan;
1052
29f8404c 1053 /* Look and see if it's a positional or keyword arg. */
252b5132
RH
1054 scan = idx;
1055 while (scan < in->len
1056 && !ISSEP (in->ptr[scan])
1057 && !(macro_mri && in->ptr[scan] == '\'')
1058 && (!macro_alternate && in->ptr[scan] != '='))
1059 scan++;
1060 if (scan < in->len && !macro_alternate && in->ptr[scan] == '=')
1061 {
1062 is_keyword = 1;
1063
1064 /* It's OK to go from positional to keyword. */
1065
1066 /* This is a keyword arg, fetch the formal name and
29f8404c 1067 then the actual stuff. */
252b5132
RH
1068 sb_reset (&t);
1069 idx = get_token (idx, in, &t);
1070 if (in->ptr[idx] != '=')
6eaeac8a
JB
1071 {
1072 err = _("confusion in formal parameters");
1073 break;
1074 }
252b5132 1075
29f8404c 1076 /* Lookup the formal in the macro's list. */
252b5132
RH
1077 ptr = (formal_entry *) hash_find (m->formal_hash, sb_terminate (&t));
1078 if (!ptr)
6eaeac8a
JB
1079 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1080 t.ptr,
1081 m->name);
252b5132
RH
1082 else
1083 {
29f8404c 1084 /* Insert this value into the right place. */
6eaeac8a
JB
1085 if (ptr->actual.len)
1086 {
1087 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1088 ptr->name.ptr,
1089 m->name);
1090 sb_reset (&ptr->actual);
1091 }
be03cc84 1092 idx = get_any_string (idx + 1, in, &ptr->actual);
252b5132
RH
1093 if (ptr->actual.len > 0)
1094 ++narg;
1095 }
1096 }
1097 else
1098 {
29f8404c 1099 /* This is a positional arg. */
252b5132
RH
1100 is_positional = 1;
1101 if (is_keyword)
6eaeac8a
JB
1102 {
1103 err = _("can't mix positional and keyword arguments");
1104 break;
1105 }
252b5132
RH
1106
1107 if (!f)
1108 {
1109 formal_entry **pf;
1110 int c;
1111
1112 if (!macro_mri)
6eaeac8a
JB
1113 {
1114 err = _("too many positional arguments");
1115 break;
1116 }
252b5132 1117
6eaeac8a 1118 f = new_formal ();
252b5132
RH
1119
1120 c = -1;
1121 for (pf = &m->formals; *pf != NULL; pf = &(*pf)->next)
1122 if ((*pf)->index >= c)
1123 c = (*pf)->index + 1;
1124 if (c == -1)
1125 c = 0;
1126 *pf = f;
1127 f->index = c;
1128 }
1129
6eaeac8a 1130 if (f->type != FORMAL_VARARG)
be03cc84 1131 idx = get_any_string (idx, in, &f->actual);
6eaeac8a
JB
1132 else
1133 {
1134 sb_add_buffer (&f->actual, in->ptr + idx, in->len - idx);
1135 idx = in->len;
1136 }
252b5132
RH
1137 if (f->actual.len > 0)
1138 ++narg;
1139 do
1140 {
1141 f = f->next;
1142 }
1143 while (f != NULL && f->index < 0);
1144 }
1145
1146 if (! macro_mri)
1147 idx = sb_skip_comma (idx, in);
1148 else
1149 {
1150 if (in->ptr[idx] == ',')
1151 ++idx;
1152 if (ISWHITE (in->ptr[idx]))
1153 break;
1154 }
1155 }
1156
6eaeac8a 1157 if (! err)
252b5132 1158 {
6eaeac8a
JB
1159 for (ptr = m->formals; ptr; ptr = ptr->next)
1160 {
1161 if (ptr->type == FORMAL_REQUIRED && ptr->actual.len == 0)
1162 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1163 ptr->name.ptr,
1164 m->name);
1165 }
252b5132 1166
6eaeac8a
JB
1167 if (macro_mri)
1168 {
1169 char buffer[20];
1170
1171 sb_reset (&t);
1172 sb_add_string (&t, macro_strip_at ? "$NARG" : "NARG");
1173 ptr = (formal_entry *) hash_find (m->formal_hash, sb_terminate (&t));
1174 sprintf (buffer, "%d", narg);
1175 sb_add_string (&ptr->actual, buffer);
1176 }
1177
1178 err = macro_expand_body (&m->sub, out, m->formals, m->formal_hash, m);
1179 }
252b5132
RH
1180
1181 /* Discard any unnamed formal arguments. */
1182 if (macro_mri)
1183 {
1184 formal_entry **pf;
1185
1186 pf = &m->formals;
1187 while (*pf != NULL)
1188 {
1189 if ((*pf)->name.len != 0)
1190 pf = &(*pf)->next;
1191 else
1192 {
252b5132 1193 f = (*pf)->next;
6eaeac8a 1194 del_formal (*pf);
252b5132
RH
1195 *pf = f;
1196 }
1197 }
1198 }
1199
1200 sb_kill (&t);
02ddf156
JB
1201 if (!err)
1202 macro_number++;
252b5132 1203
02ddf156 1204 return err;
252b5132
RH
1205}
1206
1207/* Check for a macro. If one is found, put the expansion into
fea17916 1208 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
252b5132
RH
1209
1210int
254d758c
KH
1211check_macro (const char *line, sb *expand,
1212 const char **error, macro_entry **info)
252b5132
RH
1213{
1214 const char *s;
1215 char *copy, *cs;
1216 macro_entry *macro;
1217 sb line_sb;
1218
5e75c3ab 1219 if (! is_name_beginner (*line)
252b5132
RH
1220 && (! macro_mri || *line != '.'))
1221 return 0;
1222
1223 s = line + 1;
5e75c3ab
JB
1224 while (is_part_of_name (*s))
1225 ++s;
1226 if (is_name_ender (*s))
252b5132
RH
1227 ++s;
1228
1229 copy = (char *) alloca (s - line + 1);
1230 memcpy (copy, line, s - line);
1231 copy[s - line] = '\0';
1232 for (cs = copy; *cs != '\0'; cs++)
3882b010 1233 *cs = TOLOWER (*cs);
252b5132
RH
1234
1235 macro = (macro_entry *) hash_find (macro_hash, copy);
1236
1237 if (macro == NULL)
1238 return 0;
1239
1240 /* Wrap the line up in an sb. */
1241 sb_new (&line_sb);
1242 while (*s != '\0' && *s != '\n' && *s != '\r')
1243 sb_add_char (&line_sb, *s++);
1244
1245 sb_new (expand);
fea17916 1246 *error = macro_expand (0, &line_sb, macro, expand);
252b5132
RH
1247
1248 sb_kill (&line_sb);
1249
29f8404c 1250 /* Export the macro information if requested. */
9f10757c
TW
1251 if (info)
1252 *info = macro;
1253
252b5132
RH
1254 return 1;
1255}
1256
e6ca91be
JB
1257/* Free the memory allocated to a macro. */
1258
1259static void
1260free_macro(macro_entry *macro)
1261{
1262 formal_entry *formal;
1263
1264 for (formal = macro->formals; formal; )
1265 {
6eaeac8a 1266 formal_entry *f;
e6ca91be 1267
6eaeac8a 1268 f = formal;
e6ca91be 1269 formal = formal->next;
6eaeac8a 1270 del_formal (f);
e6ca91be
JB
1271 }
1272 hash_die (macro->formal_hash);
1273 sb_kill (&macro->sub);
1274 free (macro);
1275}
1276
252b5132
RH
1277/* Delete a macro. */
1278
1279void
254d758c 1280delete_macro (const char *name)
252b5132 1281{
e6ca91be
JB
1282 char *copy;
1283 size_t i, len;
1284 macro_entry *macro;
1285
1286 len = strlen (name);
1287 copy = (char *) alloca (len + 1);
1288 for (i = 0; i < len; ++i)
1289 copy[i] = TOLOWER (name[i]);
1290 copy[i] = '\0';
1291
1292 /* Since hash_delete doesn't free memory, just clear out the entry. */
1293 if ((macro = hash_find (macro_hash, copy)) != NULL)
1294 {
1295 hash_jam (macro_hash, copy, NULL);
1296 free_macro (macro);
1297 }
1298 else
1299 as_warn (_("Attempt to purge non-existant macro `%s'"), copy);
252b5132
RH
1300}
1301
1302/* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1303 combined macro definition and execution. This returns NULL on
1304 success, or an error message otherwise. */
1305
1306const char *
254d758c 1307expand_irp (int irpc, int idx, sb *in, sb *out, int (*get_line) (sb *))
252b5132 1308{
252b5132
RH
1309 sb sub;
1310 formal_entry f;
1311 struct hash_control *h;
1312 const char *err;
1313
252b5132
RH
1314 idx = sb_skip_white (idx, in);
1315
1316 sb_new (&sub);
ca3bc58f 1317 if (! buffer_and_nest (NULL, "ENDR", &sub, get_line))
252b5132 1318 return _("unexpected end of file in irp or irpc");
29f8404c 1319
252b5132
RH
1320 sb_new (&f.name);
1321 sb_new (&f.def);
1322 sb_new (&f.actual);
1323
1324 idx = get_token (idx, in, &f.name);
1325 if (f.name.len == 0)
1326 return _("missing model parameter");
1327
1328 h = hash_new ();
1329 err = hash_jam (h, sb_terminate (&f.name), &f);
1330 if (err != NULL)
1331 return err;
1332
1333 f.index = 1;
1334 f.next = NULL;
6eaeac8a 1335 f.type = FORMAL_OPTIONAL;
252b5132
RH
1336
1337 sb_reset (out);
1338
1339 idx = sb_skip_comma (idx, in);
fea17916 1340 if (idx >= in->len)
252b5132
RH
1341 {
1342 /* Expand once with a null string. */
fea17916 1343 err = macro_expand_body (&sub, out, &f, h, 0);
252b5132
RH
1344 }
1345 else
1346 {
1347 if (irpc && in->ptr[idx] == '"')
1348 ++idx;
fea17916 1349 while (idx < in->len)
252b5132
RH
1350 {
1351 if (!irpc)
be03cc84 1352 idx = get_any_string (idx, in, &f.actual);
252b5132
RH
1353 else
1354 {
1355 if (in->ptr[idx] == '"')
1356 {
1357 int nxt;
1358
1359 nxt = sb_skip_white (idx + 1, in);
fea17916 1360 if (nxt >= in->len)
252b5132
RH
1361 {
1362 idx = nxt;
1363 break;
1364 }
1365 }
1366 sb_reset (&f.actual);
1367 sb_add_char (&f.actual, in->ptr[idx]);
1368 ++idx;
1369 }
fea17916 1370 err = macro_expand_body (&sub, out, &f, h, 0);
252b5132 1371 if (err != NULL)
02ddf156 1372 break;
252b5132
RH
1373 if (!irpc)
1374 idx = sb_skip_comma (idx, in);
1375 else
1376 idx = sb_skip_white (idx, in);
1377 }
1378 }
1379
1380 hash_die (h);
6eaeac8a
JB
1381 sb_kill (&f.actual);
1382 sb_kill (&f.def);
1383 sb_kill (&f.name);
252b5132
RH
1384 sb_kill (&sub);
1385
02ddf156 1386 return err;
252b5132 1387}
This page took 0.378755 seconds and 4 git commands to generate.