]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/macro.c
gas/
[thirdparty/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 *);
73static int get_any_string (int, sb *, sb *, int, int);
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
RH
306{
307 idx = sb_skip_white (idx, in);
308
309 while (idx < in->len
29f8404c 310 && (in->ptr[idx] == '"'
252b5132
RH
311 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
312 || (in->ptr[idx] == '\'' && macro_alternate)))
313 {
314 if (in->ptr[idx] == '<')
315 {
316 int nest = 0;
317 idx++;
318 while ((in->ptr[idx] != '>' || nest)
319 && idx < in->len)
320 {
321 if (in->ptr[idx] == '!')
322 {
29f8404c 323 idx++;
252b5132
RH
324 sb_add_char (acc, in->ptr[idx++]);
325 }
326 else
327 {
328 if (in->ptr[idx] == '>')
329 nest--;
330 if (in->ptr[idx] == '<')
331 nest++;
332 sb_add_char (acc, in->ptr[idx++]);
333 }
334 }
335 idx++;
336 }
337 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
338 {
339 char tchar = in->ptr[idx];
c06ae4f2 340 int escaped = 0;
29f8404c 341
252b5132 342 idx++;
29f8404c 343
252b5132
RH
344 while (idx < in->len)
345 {
29f8404c 346 if (in->ptr[idx - 1] == '\\')
c06ae4f2
UC
347 escaped ^= 1;
348 else
349 escaped = 0;
350
252b5132
RH
351 if (macro_alternate && in->ptr[idx] == '!')
352 {
e972090a 353 idx ++;
29f8404c 354
1994a7c7
NC
355 sb_add_char (acc, in->ptr[idx]);
356
e972090a 357 idx ++;
252b5132 358 }
c06ae4f2
UC
359 else if (escaped && in->ptr[idx] == tchar)
360 {
361 sb_add_char (acc, tchar);
e972090a 362 idx ++;
c06ae4f2 363 }
252b5132
RH
364 else
365 {
366 if (in->ptr[idx] == tchar)
367 {
e972090a 368 idx ++;
29f8404c 369
252b5132
RH
370 if (idx >= in->len || in->ptr[idx] != tchar)
371 break;
372 }
29f8404c 373
252b5132 374 sb_add_char (acc, in->ptr[idx]);
e972090a 375 idx ++;
252b5132
RH
376 }
377 }
378 }
379 }
29f8404c 380
252b5132
RH
381 return idx;
382}
383
384/* Fetch string from the input stream,
385 rules:
386 'Bxyx<whitespace> -> return 'Bxyza
387 %<char> -> return string of decimal value of x
388 "<string>" -> return string
389 xyx<whitespace> -> return xyz
390*/
391
392static int
254d758c 393get_any_string (int idx, sb *in, sb *out, int expand, int pretend_quoted)
252b5132
RH
394{
395 sb_reset (out);
396 idx = sb_skip_white (idx, in);
397
398 if (idx < in->len)
399 {
9df59bba 400 if (in->len > idx + 2 && in->ptr[idx + 1] == '\'' && ISBASE (in->ptr[idx]))
252b5132
RH
401 {
402 while (!ISSEP (in->ptr[idx]))
403 sb_add_char (out, in->ptr[idx++]);
404 }
405 else if (in->ptr[idx] == '%'
406 && macro_alternate
407 && expand)
408 {
409 int val;
410 char buf[20];
29f8404c 411 /* Turns the next expression into a string. */
06f030db 412 /* xgettext: no-c-format */
252b5132
RH
413 idx = (*macro_expr) (_("% operator needs absolute expression"),
414 idx + 1,
415 in,
416 &val);
d1a6c242 417 sprintf (buf, "%d", val);
252b5132
RH
418 sb_add_string (out, buf);
419 }
420 else if (in->ptr[idx] == '"'
421 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
422 || (macro_alternate && in->ptr[idx] == '\''))
423 {
424 if (macro_alternate
425 && ! macro_strip_at
426 && expand)
427 {
29f8404c
KH
428 /* Keep the quotes. */
429 sb_add_char (out, '\"');
252b5132
RH
430
431 idx = getstring (idx, in, out);
29f8404c 432 sb_add_char (out, '\"');
252b5132
RH
433 }
434 else
435 {
436 idx = getstring (idx, in, out);
437 }
438 }
29f8404c 439 else
252b5132 440 {
29f8404c 441 while (idx < in->len
252b5132
RH
442 && (in->ptr[idx] == '"'
443 || in->ptr[idx] == '\''
29f8404c 444 || pretend_quoted
252b5132
RH
445 || (in->ptr[idx] != ' '
446 && in->ptr[idx] != '\t'
447 && in->ptr[idx] != ','
448 && (in->ptr[idx] != '<'
449 || (! macro_alternate && ! macro_mri)))))
450 {
29f8404c 451 if (in->ptr[idx] == '"'
252b5132
RH
452 || in->ptr[idx] == '\'')
453 {
454 char tchar = in->ptr[idx];
455 sb_add_char (out, in->ptr[idx++]);
456 while (idx < in->len
457 && in->ptr[idx] != tchar)
29f8404c 458 sb_add_char (out, in->ptr[idx++]);
252b5132 459 if (idx == in->len)
29f8404c 460 return idx;
252b5132
RH
461 }
462 sb_add_char (out, in->ptr[idx++]);
463 }
464 }
465 }
466
467 return idx;
468}
469
6eaeac8a
JB
470/* Allocate a new formal. */
471
472static formal_entry *
473new_formal (void)
474{
475 formal_entry *formal;
476
477 formal = xmalloc (sizeof (formal_entry));
478
479 sb_new (&formal->name);
480 sb_new (&formal->def);
481 sb_new (&formal->actual);
482 formal->next = NULL;
483 formal->type = FORMAL_OPTIONAL;
484 return formal;
485}
486
487/* Free a formal. */
488
489static void
490del_formal (formal_entry *formal)
491{
492 sb_kill (&formal->actual);
493 sb_kill (&formal->def);
494 sb_kill (&formal->name);
495 free (formal);
496}
497
252b5132
RH
498/* Pick up the formal parameters of a macro definition. */
499
500static int
254d758c 501do_formals (macro_entry *macro, int idx, sb *in)
252b5132
RH
502{
503 formal_entry **p = &macro->formals;
02ddf156 504 const char *name;
252b5132 505
057f53c1 506 idx = sb_skip_white (idx, in);
252b5132
RH
507 while (idx < in->len)
508 {
6eaeac8a 509 formal_entry *formal = new_formal ();
057f53c1 510 int cidx;
252b5132 511
252b5132
RH
512 idx = get_token (idx, in, &formal->name);
513 if (formal->name.len == 0)
057f53c1
JB
514 {
515 if (macro->formal_count)
516 --idx;
517 break;
518 }
252b5132 519 idx = sb_skip_white (idx, in);
057f53c1 520 /* This is a formal. */
6eaeac8a
JB
521 name = sb_terminate (&formal->name);
522 if (! macro_mri
523 && idx < in->len
524 && in->ptr[idx] == ':'
525 && (! is_name_beginner (':')
526 || idx + 1 >= in->len
527 || ! is_part_of_name (in->ptr[idx + 1])))
528 {
529 /* Got a qualifier. */
530 sb qual;
531
532 sb_new (&qual);
533 idx = get_token (sb_skip_white (idx + 1, in), in, &qual);
534 sb_terminate (&qual);
535 if (qual.len == 0)
536 as_bad_where (macro->file,
537 macro->line,
538 _("Missing parameter qualifier for `%s' in macro `%s'"),
539 name,
540 macro->name);
541 else if (strcmp (qual.ptr, "req") == 0)
542 formal->type = FORMAL_REQUIRED;
543 else if (strcmp (qual.ptr, "vararg") == 0)
544 formal->type = FORMAL_VARARG;
545 else
546 as_bad_where (macro->file,
547 macro->line,
548 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
549 qual.ptr,
550 name,
551 macro->name);
552 sb_kill (&qual);
553 idx = sb_skip_white (idx, in);
554 }
057f53c1 555 if (idx < in->len && in->ptr[idx] == '=')
252b5132 556 {
057f53c1
JB
557 /* Got a default. */
558 idx = get_any_string (idx + 1, in, &formal->def, 1, 0);
559 idx = sb_skip_white (idx, in);
6eaeac8a
JB
560 if (formal->type == FORMAL_REQUIRED)
561 {
562 sb_reset (&formal->def);
563 as_warn_where (macro->file,
564 macro->line,
565 _("Pointless default value for required parameter `%s' in macro `%s'"),
566 name,
567 macro->name);
568 }
252b5132
RH
569 }
570
29f8404c 571 /* Add to macro's hash table. */
02ddf156
JB
572 if (! hash_find (macro->formal_hash, name))
573 hash_jam (macro->formal_hash, name, formal);
574 else
575 as_bad_where (macro->file,
576 macro->line,
577 _("A parameter named `%s' already exists for macro `%s'"),
578 name,
579 macro->name);
252b5132 580
057f53c1 581 formal->index = macro->formal_count++;
6eaeac8a
JB
582 *p = formal;
583 p = &formal->next;
584 if (formal->type == FORMAL_VARARG)
585 break;
057f53c1 586 cidx = idx;
252b5132 587 idx = sb_skip_comma (idx, in);
057f53c1
JB
588 if (idx != cidx && idx >= in->len)
589 {
590 idx = cidx;
591 break;
592 }
252b5132
RH
593 }
594
595 if (macro_mri)
596 {
6eaeac8a 597 formal_entry *formal = new_formal ();
252b5132
RH
598
599 /* Add a special NARG formal, which macro_expand will set to the
600 number of arguments. */
252b5132
RH
601 /* The same MRI assemblers which treat '@' characters also use
602 the name $NARG. At least until we find an exception. */
603 if (macro_strip_at)
604 name = "$NARG";
605 else
606 name = "NARG";
607
608 sb_add_string (&formal->name, name);
609
29f8404c 610 /* Add to macro's hash table. */
02ddf156
JB
611 if (hash_find (macro->formal_hash, name))
612 as_bad_where (macro->file,
613 macro->line,
614 _("Reserved word `%s' used as parameter in macro `%s'"),
615 name,
616 macro->name);
252b5132
RH
617 hash_jam (macro->formal_hash, name, formal);
618
619 formal->index = NARG_INDEX;
620 *p = formal;
252b5132
RH
621 }
622
623 return idx;
624}
625
626/* Define a new macro. Returns NULL on success, otherwise returns an
627 error message. If NAMEP is not NULL, *NAMEP is set to the name of
628 the macro which was defined. */
629
630const char *
254d758c 631define_macro (int idx, sb *in, sb *label,
02ddf156
JB
632 int (*get_line) (sb *),
633 char *file, unsigned int line,
634 const char **namep)
252b5132
RH
635{
636 macro_entry *macro;
637 sb name;
02ddf156 638 const char *error = NULL;
252b5132
RH
639
640 macro = (macro_entry *) xmalloc (sizeof (macro_entry));
641 sb_new (&macro->sub);
642 sb_new (&name);
02ddf156
JB
643 macro->file = file;
644 macro->line = line;
252b5132
RH
645
646 macro->formal_count = 0;
647 macro->formals = 0;
e6ca91be 648 macro->formal_hash = hash_new ();
252b5132
RH
649
650 idx = sb_skip_white (idx, in);
651 if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
02ddf156 652 error = _("unexpected end of file in macro `%s' definition");
252b5132
RH
653 if (label != NULL && label->len != 0)
654 {
655 sb_add_sb (&name, label);
02ddf156 656 macro->name = sb_terminate (&name);
252b5132
RH
657 if (idx < in->len && in->ptr[idx] == '(')
658 {
29f8404c 659 /* It's the label: MACRO (formals,...) sort */
252b5132 660 idx = do_formals (macro, idx + 1, in);
02ddf156
JB
661 if (idx < in->len && in->ptr[idx] == ')')
662 idx = sb_skip_white (idx + 1, in);
663 else if (!error)
664 error = _("missing `)' after formals in macro definition `%s'");
252b5132
RH
665 }
666 else
667 {
29f8404c 668 /* It's the label: MACRO formals,... sort */
252b5132
RH
669 idx = do_formals (macro, idx, in);
670 }
671 }
672 else
673 {
057f53c1
JB
674 int cidx;
675
252b5132 676 idx = get_token (idx, in, &name);
02ddf156 677 macro->name = sb_terminate (&name);
057f53c1 678 if (name.len == 0)
02ddf156 679 error = _("Missing macro name");
057f53c1
JB
680 cidx = sb_skip_white (idx, in);
681 idx = sb_skip_comma (cidx, in);
682 if (idx == cidx || idx < in->len)
683 idx = do_formals (macro, idx, in);
684 else
685 idx = cidx;
252b5132 686 }
02ddf156
JB
687 if (!error && idx < in->len)
688 error = _("Bad parameter list for macro `%s'");
252b5132 689
29f8404c 690 /* And stick it in the macro hash table. */
252b5132 691 for (idx = 0; idx < name.len; idx++)
3882b010 692 name.ptr[idx] = TOLOWER (name.ptr[idx]);
02ddf156
JB
693 if (hash_find (macro_hash, macro->name))
694 error = _("Macro `%s' was already defined");
695 if (!error)
696 error = hash_jam (macro_hash, macro->name, (PTR) macro);
252b5132
RH
697
698 if (namep != NULL)
02ddf156
JB
699 *namep = macro->name;
700
701 if (!error)
702 macro_defined = 1;
703 else
704 free_macro (macro);
252b5132 705
02ddf156 706 return error;
252b5132
RH
707}
708
709/* Scan a token, and then skip KIND. */
710
711static int
254d758c 712get_apost_token (int idx, sb *in, sb *name, int kind)
252b5132
RH
713{
714 idx = get_token (idx, in, name);
715 if (idx < in->len
716 && in->ptr[idx] == kind
717 && (! macro_mri || macro_strip_at)
718 && (! macro_strip_at || kind == '@'))
719 idx++;
720 return idx;
721}
722
723/* Substitute the actual value for a formal parameter. */
724
725static int
254d758c
KH
726sub_actual (int start, sb *in, sb *t, struct hash_control *formal_hash,
727 int kind, sb *out, int copyifnotthere)
252b5132
RH
728{
729 int src;
730 formal_entry *ptr;
731
732 src = get_apost_token (start, in, t, kind);
733 /* See if it's in the macro's hash table, unless this is
734 macro_strip_at and kind is '@' and the token did not end in '@'. */
735 if (macro_strip_at
736 && kind == '@'
737 && (src == start || in->ptr[src - 1] != '@'))
738 ptr = NULL;
739 else
740 ptr = (formal_entry *) hash_find (formal_hash, sb_terminate (t));
741 if (ptr)
742 {
743 if (ptr->actual.len)
744 {
745 sb_add_sb (out, &ptr->actual);
746 }
747 else
748 {
749 sb_add_sb (out, &ptr->def);
750 }
751 }
752 else if (kind == '&')
753 {
754 /* Doing this permits people to use & in macro bodies. */
755 sb_add_char (out, '&');
c1ed1235 756 sb_add_sb (out, t);
252b5132
RH
757 }
758 else if (copyifnotthere)
759 {
760 sb_add_sb (out, t);
761 }
29f8404c 762 else
252b5132
RH
763 {
764 sb_add_char (out, '\\');
765 sb_add_sb (out, t);
766 }
767 return src;
768}
769
770/* Expand the body of a macro. */
771
772static const char *
254d758c 773macro_expand_body (sb *in, sb *out, formal_entry *formals,
02ddf156 774 struct hash_control *formal_hash, const macro_entry *macro)
252b5132
RH
775{
776 sb t;
02ddf156 777 int src = 0, inquote = 0, macro_line = 0;
252b5132 778 formal_entry *loclist = NULL;
02ddf156 779 const char *err = NULL;
252b5132
RH
780
781 sb_new (&t);
782
02ddf156 783 while (src < in->len && !err)
252b5132
RH
784 {
785 if (in->ptr[src] == '&')
786 {
787 sb_reset (&t);
788 if (macro_mri)
789 {
790 if (src + 1 < in->len && in->ptr[src + 1] == '&')
791 src = sub_actual (src + 2, in, &t, formal_hash, '\'', out, 1);
792 else
793 sb_add_char (out, in->ptr[src++]);
794 }
795 else
796 {
797 /* FIXME: Why do we do this? */
02ddf156
JB
798 /* At least in alternate mode this seems correct; without this
799 one can't append a literal to a parameter. */
252b5132
RH
800 src = sub_actual (src + 1, in, &t, formal_hash, '&', out, 0);
801 }
802 }
803 else if (in->ptr[src] == '\\')
804 {
805 src++;
5e75c3ab 806 if (src < in->len && in->ptr[src] == '(')
252b5132 807 {
29f8404c 808 /* Sub in till the next ')' literally. */
252b5132
RH
809 src++;
810 while (src < in->len && in->ptr[src] != ')')
811 {
812 sb_add_char (out, in->ptr[src++]);
813 }
02ddf156 814 if (src < in->len)
252b5132 815 src++;
02ddf156
JB
816 else if (!macro)
817 err = _("missing `)'");
252b5132 818 else
02ddf156 819 as_bad_where (macro->file, macro->line + macro_line, _("missing `)'"));
252b5132 820 }
5e75c3ab 821 else if (src < in->len && in->ptr[src] == '@')
252b5132 822 {
29f8404c 823 /* Sub in the macro invocation number. */
252b5132
RH
824
825 char buffer[10];
826 src++;
a2984248 827 sprintf (buffer, "%d", macro_number);
252b5132
RH
828 sb_add_string (out, buffer);
829 }
5e75c3ab 830 else if (src < in->len && in->ptr[src] == '&')
252b5132
RH
831 {
832 /* This is a preprocessor variable name, we don't do them
29f8404c 833 here. */
252b5132
RH
834 sb_add_char (out, '\\');
835 sb_add_char (out, '&');
836 src++;
837 }
5e75c3ab 838 else if (macro_mri && src < in->len && ISALNUM (in->ptr[src]))
252b5132
RH
839 {
840 int ind;
841 formal_entry *f;
842
3882b010 843 if (ISDIGIT (in->ptr[src]))
252b5132 844 ind = in->ptr[src] - '0';
3882b010 845 else if (ISUPPER (in->ptr[src]))
252b5132
RH
846 ind = in->ptr[src] - 'A' + 10;
847 else
848 ind = in->ptr[src] - 'a' + 10;
849 ++src;
850 for (f = formals; f != NULL; f = f->next)
851 {
852 if (f->index == ind - 1)
853 {
854 if (f->actual.len != 0)
855 sb_add_sb (out, &f->actual);
856 else
857 sb_add_sb (out, &f->def);
858 break;
859 }
860 }
861 }
862 else
863 {
864 sb_reset (&t);
865 src = sub_actual (src, in, &t, formal_hash, '\'', out, 0);
866 }
867 }
868 else if ((macro_alternate || macro_mri)
5e75c3ab 869 && is_name_beginner (in->ptr[src])
252b5132
RH
870 && (! inquote
871 || ! macro_strip_at
872 || (src > 0 && in->ptr[src - 1] == '@')))
873 {
02ddf156 874 if (! macro
252b5132
RH
875 || src + 5 >= in->len
876 || strncasecmp (in->ptr + src, "LOCAL", 5) != 0
877 || ! ISWHITE (in->ptr[src + 5]))
878 {
879 sb_reset (&t);
880 src = sub_actual (src, in, &t, formal_hash,
881 (macro_strip_at && inquote) ? '@' : '\'',
882 out, 1);
883 }
884 else
885 {
252b5132 886 src = sb_skip_white (src + 5, in);
fea17916 887 while (in->ptr[src] != '\n')
252b5132 888 {
02ddf156 889 const char *name;
6eaeac8a 890 formal_entry *f = new_formal ();
252b5132 891
252b5132 892 src = get_token (src, in, &f->name);
02ddf156
JB
893 name = sb_terminate (&f->name);
894 if (! hash_find (formal_hash, name))
895 {
896 static int loccnt;
897 char buf[20];
252b5132 898
02ddf156
JB
899 f->index = LOCAL_INDEX;
900 f->next = loclist;
901 loclist = f;
902
903 sprintf (buf, IS_ELF ? ".LL%04x" : "LL%04x", ++loccnt);
904 sb_add_string (&f->actual, buf);
905
906 err = hash_jam (formal_hash, name, f);
907 if (err != NULL)
908 break;
909 }
910 else
911 {
912 as_bad_where (macro->file,
913 macro->line + macro_line,
914 _("`%s' was already used as parameter (or another local) name"),
915 name);
6eaeac8a 916 del_formal (f);
02ddf156 917 }
252b5132
RH
918
919 src = sb_skip_comma (src, in);
920 }
921 }
922 }
252b5132
RH
923 else if (in->ptr[src] == '"'
924 || (macro_mri && in->ptr[src] == '\''))
925 {
926 inquote = !inquote;
927 sb_add_char (out, in->ptr[src++]);
928 }
929 else if (in->ptr[src] == '@' && macro_strip_at)
930 {
931 ++src;
932 if (src < in->len
933 && in->ptr[src] == '@')
934 {
935 sb_add_char (out, '@');
936 ++src;
937 }
938 }
939 else if (macro_mri
940 && in->ptr[src] == '='
941 && src + 1 < in->len
942 && in->ptr[src + 1] == '=')
943 {
944 formal_entry *ptr;
945
946 sb_reset (&t);
947 src = get_token (src + 2, in, &t);
948 ptr = (formal_entry *) hash_find (formal_hash, sb_terminate (&t));
949 if (ptr == NULL)
950 {
951 /* FIXME: We should really return a warning string here,
952 but we can't, because the == might be in the MRI
953 comment field, and, since the nature of the MRI
954 comment field depends upon the exact instruction
955 being used, we don't have enough information here to
956 figure out whether it is or not. Instead, we leave
957 the == in place, which should cause a syntax error if
958 it is not in a comment. */
959 sb_add_char (out, '=');
960 sb_add_char (out, '=');
961 sb_add_sb (out, &t);
962 }
963 else
964 {
965 if (ptr->actual.len)
966 {
967 sb_add_string (out, "-1");
968 }
969 else
970 {
971 sb_add_char (out, '0');
972 }
973 }
974 }
975 else
976 {
02ddf156
JB
977 if (in->ptr[src] == '\n')
978 ++macro_line;
252b5132
RH
979 sb_add_char (out, in->ptr[src++]);
980 }
981 }
982
983 sb_kill (&t);
984
985 while (loclist != NULL)
986 {
987 formal_entry *f;
988
989 f = loclist->next;
1af6dcd2
ILT
990 /* Setting the value to NULL effectively deletes the entry. We
991 avoid calling hash_delete because it doesn't reclaim memory. */
992 hash_jam (formal_hash, sb_terminate (&loclist->name), NULL);
6eaeac8a 993 del_formal (loclist);
252b5132
RH
994 loclist = f;
995 }
996
02ddf156 997 return err;
252b5132
RH
998}
999
1000/* Assign values to the formal parameters of a macro, and expand the
1001 body. */
1002
1003static const char *
254d758c 1004macro_expand (int idx, sb *in, macro_entry *m, sb *out)
252b5132
RH
1005{
1006 sb t;
1007 formal_entry *ptr;
1008 formal_entry *f;
1009 int is_positional = 0;
1010 int is_keyword = 0;
1011 int narg = 0;
6eaeac8a 1012 const char *err = NULL;
252b5132
RH
1013
1014 sb_new (&t);
29f8404c
KH
1015
1016 /* Reset any old value the actuals may have. */
252b5132 1017 for (f = m->formals; f; f = f->next)
29f8404c 1018 sb_reset (&f->actual);
252b5132
RH
1019 f = m->formals;
1020 while (f != NULL && f->index < 0)
1021 f = f->next;
1022
1023 if (macro_mri)
1024 {
1025 /* The macro may be called with an optional qualifier, which may
1026 be referred to in the macro body as \0. */
1027 if (idx < in->len && in->ptr[idx] == '.')
d1a6c242
KH
1028 {
1029 /* The Microtec assembler ignores this if followed by a white space.
1030 (Macro invocation with empty extension) */
1031 idx++;
1032 if ( idx < in->len
1033 && in->ptr[idx] != ' '
1034 && in->ptr[idx] != '\t')
1035 {
6eaeac8a 1036 formal_entry *n = new_formal ();
d1a6c242 1037
d1a6c242
KH
1038 n->index = QUAL_INDEX;
1039
1040 n->next = m->formals;
1041 m->formals = n;
1042
1043 idx = get_any_string (idx, in, &n->actual, 1, 0);
1044 }
1045 }
1046 }
252b5132 1047
29f8404c 1048 /* Peel off the actuals and store them away in the hash tables' actuals. */
252b5132 1049 idx = sb_skip_white (idx, in);
fea17916 1050 while (idx < in->len)
252b5132
RH
1051 {
1052 int scan;
1053
29f8404c 1054 /* Look and see if it's a positional or keyword arg. */
252b5132
RH
1055 scan = idx;
1056 while (scan < in->len
1057 && !ISSEP (in->ptr[scan])
1058 && !(macro_mri && in->ptr[scan] == '\'')
1059 && (!macro_alternate && in->ptr[scan] != '='))
1060 scan++;
1061 if (scan < in->len && !macro_alternate && in->ptr[scan] == '=')
1062 {
1063 is_keyword = 1;
1064
1065 /* It's OK to go from positional to keyword. */
1066
1067 /* This is a keyword arg, fetch the formal name and
29f8404c 1068 then the actual stuff. */
252b5132
RH
1069 sb_reset (&t);
1070 idx = get_token (idx, in, &t);
1071 if (in->ptr[idx] != '=')
6eaeac8a
JB
1072 {
1073 err = _("confusion in formal parameters");
1074 break;
1075 }
252b5132 1076
29f8404c 1077 /* Lookup the formal in the macro's list. */
252b5132
RH
1078 ptr = (formal_entry *) hash_find (m->formal_hash, sb_terminate (&t));
1079 if (!ptr)
6eaeac8a
JB
1080 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1081 t.ptr,
1082 m->name);
252b5132
RH
1083 else
1084 {
29f8404c 1085 /* Insert this value into the right place. */
6eaeac8a
JB
1086 if (ptr->actual.len)
1087 {
1088 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1089 ptr->name.ptr,
1090 m->name);
1091 sb_reset (&ptr->actual);
1092 }
252b5132
RH
1093 idx = get_any_string (idx + 1, in, &ptr->actual, 0, 0);
1094 if (ptr->actual.len > 0)
1095 ++narg;
1096 }
1097 }
1098 else
1099 {
29f8404c 1100 /* This is a positional arg. */
252b5132
RH
1101 is_positional = 1;
1102 if (is_keyword)
6eaeac8a
JB
1103 {
1104 err = _("can't mix positional and keyword arguments");
1105 break;
1106 }
252b5132
RH
1107
1108 if (!f)
1109 {
1110 formal_entry **pf;
1111 int c;
1112
1113 if (!macro_mri)
6eaeac8a
JB
1114 {
1115 err = _("too many positional arguments");
1116 break;
1117 }
252b5132 1118
6eaeac8a 1119 f = new_formal ();
252b5132
RH
1120
1121 c = -1;
1122 for (pf = &m->formals; *pf != NULL; pf = &(*pf)->next)
1123 if ((*pf)->index >= c)
1124 c = (*pf)->index + 1;
1125 if (c == -1)
1126 c = 0;
1127 *pf = f;
1128 f->index = c;
1129 }
1130
6eaeac8a
JB
1131 if (f->type != FORMAL_VARARG)
1132 idx = get_any_string (idx, in, &f->actual, 1, 0);
1133 else
1134 {
1135 sb_add_buffer (&f->actual, in->ptr + idx, in->len - idx);
1136 idx = in->len;
1137 }
252b5132
RH
1138 if (f->actual.len > 0)
1139 ++narg;
1140 do
1141 {
1142 f = f->next;
1143 }
1144 while (f != NULL && f->index < 0);
1145 }
1146
1147 if (! macro_mri)
1148 idx = sb_skip_comma (idx, in);
1149 else
1150 {
1151 if (in->ptr[idx] == ',')
1152 ++idx;
1153 if (ISWHITE (in->ptr[idx]))
1154 break;
1155 }
1156 }
1157
6eaeac8a 1158 if (! err)
252b5132 1159 {
6eaeac8a
JB
1160 for (ptr = m->formals; ptr; ptr = ptr->next)
1161 {
1162 if (ptr->type == FORMAL_REQUIRED && ptr->actual.len == 0)
1163 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1164 ptr->name.ptr,
1165 m->name);
1166 }
252b5132 1167
6eaeac8a
JB
1168 if (macro_mri)
1169 {
1170 char buffer[20];
1171
1172 sb_reset (&t);
1173 sb_add_string (&t, macro_strip_at ? "$NARG" : "NARG");
1174 ptr = (formal_entry *) hash_find (m->formal_hash, sb_terminate (&t));
1175 sprintf (buffer, "%d", narg);
1176 sb_add_string (&ptr->actual, buffer);
1177 }
1178
1179 err = macro_expand_body (&m->sub, out, m->formals, m->formal_hash, m);
1180 }
252b5132
RH
1181
1182 /* Discard any unnamed formal arguments. */
1183 if (macro_mri)
1184 {
1185 formal_entry **pf;
1186
1187 pf = &m->formals;
1188 while (*pf != NULL)
1189 {
1190 if ((*pf)->name.len != 0)
1191 pf = &(*pf)->next;
1192 else
1193 {
252b5132 1194 f = (*pf)->next;
6eaeac8a 1195 del_formal (*pf);
252b5132
RH
1196 *pf = f;
1197 }
1198 }
1199 }
1200
1201 sb_kill (&t);
02ddf156
JB
1202 if (!err)
1203 macro_number++;
252b5132 1204
02ddf156 1205 return err;
252b5132
RH
1206}
1207
1208/* Check for a macro. If one is found, put the expansion into
fea17916 1209 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
252b5132
RH
1210
1211int
254d758c
KH
1212check_macro (const char *line, sb *expand,
1213 const char **error, macro_entry **info)
252b5132
RH
1214{
1215 const char *s;
1216 char *copy, *cs;
1217 macro_entry *macro;
1218 sb line_sb;
1219
5e75c3ab 1220 if (! is_name_beginner (*line)
252b5132
RH
1221 && (! macro_mri || *line != '.'))
1222 return 0;
1223
1224 s = line + 1;
5e75c3ab
JB
1225 while (is_part_of_name (*s))
1226 ++s;
1227 if (is_name_ender (*s))
252b5132
RH
1228 ++s;
1229
1230 copy = (char *) alloca (s - line + 1);
1231 memcpy (copy, line, s - line);
1232 copy[s - line] = '\0';
1233 for (cs = copy; *cs != '\0'; cs++)
3882b010 1234 *cs = TOLOWER (*cs);
252b5132
RH
1235
1236 macro = (macro_entry *) hash_find (macro_hash, copy);
1237
1238 if (macro == NULL)
1239 return 0;
1240
1241 /* Wrap the line up in an sb. */
1242 sb_new (&line_sb);
1243 while (*s != '\0' && *s != '\n' && *s != '\r')
1244 sb_add_char (&line_sb, *s++);
1245
1246 sb_new (expand);
fea17916 1247 *error = macro_expand (0, &line_sb, macro, expand);
252b5132
RH
1248
1249 sb_kill (&line_sb);
1250
29f8404c 1251 /* Export the macro information if requested. */
9f10757c
TW
1252 if (info)
1253 *info = macro;
1254
252b5132
RH
1255 return 1;
1256}
1257
e6ca91be
JB
1258/* Free the memory allocated to a macro. */
1259
1260static void
1261free_macro(macro_entry *macro)
1262{
1263 formal_entry *formal;
1264
1265 for (formal = macro->formals; formal; )
1266 {
6eaeac8a 1267 formal_entry *f;
e6ca91be 1268
6eaeac8a 1269 f = formal;
e6ca91be 1270 formal = formal->next;
6eaeac8a 1271 del_formal (f);
e6ca91be
JB
1272 }
1273 hash_die (macro->formal_hash);
1274 sb_kill (&macro->sub);
1275 free (macro);
1276}
1277
252b5132
RH
1278/* Delete a macro. */
1279
1280void
254d758c 1281delete_macro (const char *name)
252b5132 1282{
e6ca91be
JB
1283 char *copy;
1284 size_t i, len;
1285 macro_entry *macro;
1286
1287 len = strlen (name);
1288 copy = (char *) alloca (len + 1);
1289 for (i = 0; i < len; ++i)
1290 copy[i] = TOLOWER (name[i]);
1291 copy[i] = '\0';
1292
1293 /* Since hash_delete doesn't free memory, just clear out the entry. */
1294 if ((macro = hash_find (macro_hash, copy)) != NULL)
1295 {
1296 hash_jam (macro_hash, copy, NULL);
1297 free_macro (macro);
1298 }
1299 else
1300 as_warn (_("Attempt to purge non-existant macro `%s'"), copy);
252b5132
RH
1301}
1302
1303/* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1304 combined macro definition and execution. This returns NULL on
1305 success, or an error message otherwise. */
1306
1307const char *
254d758c 1308expand_irp (int irpc, int idx, sb *in, sb *out, int (*get_line) (sb *))
252b5132 1309{
252b5132
RH
1310 sb sub;
1311 formal_entry f;
1312 struct hash_control *h;
1313 const char *err;
1314
252b5132
RH
1315 idx = sb_skip_white (idx, in);
1316
1317 sb_new (&sub);
ca3bc58f 1318 if (! buffer_and_nest (NULL, "ENDR", &sub, get_line))
252b5132 1319 return _("unexpected end of file in irp or irpc");
29f8404c 1320
252b5132
RH
1321 sb_new (&f.name);
1322 sb_new (&f.def);
1323 sb_new (&f.actual);
1324
1325 idx = get_token (idx, in, &f.name);
1326 if (f.name.len == 0)
1327 return _("missing model parameter");
1328
1329 h = hash_new ();
1330 err = hash_jam (h, sb_terminate (&f.name), &f);
1331 if (err != NULL)
1332 return err;
1333
1334 f.index = 1;
1335 f.next = NULL;
6eaeac8a 1336 f.type = FORMAL_OPTIONAL;
252b5132
RH
1337
1338 sb_reset (out);
1339
1340 idx = sb_skip_comma (idx, in);
fea17916 1341 if (idx >= in->len)
252b5132
RH
1342 {
1343 /* Expand once with a null string. */
fea17916 1344 err = macro_expand_body (&sub, out, &f, h, 0);
252b5132
RH
1345 }
1346 else
1347 {
1348 if (irpc && in->ptr[idx] == '"')
1349 ++idx;
fea17916 1350 while (idx < in->len)
252b5132
RH
1351 {
1352 if (!irpc)
1353 idx = get_any_string (idx, in, &f.actual, 1, 0);
1354 else
1355 {
1356 if (in->ptr[idx] == '"')
1357 {
1358 int nxt;
1359
1360 nxt = sb_skip_white (idx + 1, in);
fea17916 1361 if (nxt >= in->len)
252b5132
RH
1362 {
1363 idx = nxt;
1364 break;
1365 }
1366 }
1367 sb_reset (&f.actual);
1368 sb_add_char (&f.actual, in->ptr[idx]);
1369 ++idx;
1370 }
fea17916 1371 err = macro_expand_body (&sub, out, &f, h, 0);
252b5132 1372 if (err != NULL)
02ddf156 1373 break;
252b5132
RH
1374 if (!irpc)
1375 idx = sb_skip_comma (idx, in);
1376 else
1377 idx = sb_skip_white (idx, in);
1378 }
1379 }
1380
1381 hash_die (h);
6eaeac8a
JB
1382 sb_kill (&f.actual);
1383 sb_kill (&f.def);
1384 sb_kill (&f.name);
252b5132
RH
1385 sb_kill (&sub);
1386
02ddf156 1387 return err;
252b5132 1388}