]> 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 2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4c665b71 3 2004, 2005, 2006, 2007, 2008, 2011, 2012, 2013 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
ec2655a6 12 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
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 24
89658e52 25#include "as.h"
3882b010 26#include "safe-ctype.h"
252b5132 27#include "sb.h"
252b5132
RH
28#include "macro.h"
29
252b5132 30/* The routines in this file handle macro definition and expansion.
fea17916 31 They are called by gas. */
252b5132 32
252b5132
RH
33#define ISWHITE(x) ((x) == ' ' || (x) == '\t')
34
35#define ISSEP(x) \
36 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
37 || (x) == ')' || (x) == '(' \
38 || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
39
40#define ISBASE(x) \
41 ((x) == 'b' || (x) == 'B' \
42 || (x) == 'q' || (x) == 'Q' \
43 || (x) == 'h' || (x) == 'H' \
44 || (x) == 'd' || (x) == 'D')
45
46/* The macro hash table. */
47
c1d05a60 48struct hash_control *macro_hash;
252b5132
RH
49
50/* Whether any macros have been defined. */
51
52int macro_defined;
53
fea17916 54/* Whether we are in alternate syntax mode. */
252b5132
RH
55
56static int macro_alternate;
57
58/* Whether we are in MRI mode. */
59
60static int macro_mri;
61
62/* Whether we should strip '@' characters. */
63
64static int macro_strip_at;
65
66/* Function to use to parse an expression. */
67
39a45edc 68static size_t (*macro_expr) (const char *, size_t, sb *, offsetT *);
252b5132
RH
69
70/* Number of macro expansions that have been done. */
71
72static int macro_number;
73
74/* Initialize macro processing. */
75
76void
254d758c 77macro_init (int alternate, int mri, int strip_at,
39a45edc 78 size_t (*exp) (const char *, size_t, sb *, offsetT *))
252b5132
RH
79{
80 macro_hash = hash_new ();
81 macro_defined = 0;
82 macro_alternate = alternate;
83 macro_mri = mri;
84 macro_strip_at = strip_at;
91d6fa6a 85 macro_expr = exp;
252b5132
RH
86}
87
caa32fe5
NC
88/* Switch in and out of alternate mode on the fly. */
89
90void
2766e5e4 91macro_set_alternate (int alternate)
caa32fe5
NC
92{
93 macro_alternate = alternate;
94}
95
252b5132
RH
96/* Switch in and out of MRI mode on the fly. */
97
98void
254d758c 99macro_mri_mode (int mri)
252b5132
RH
100{
101 macro_mri = mri;
102}
103
104/* Read input lines till we get to a TO string.
105 Increase nesting depth if we get a FROM string.
106 Put the results into sb at PTR.
ca3bc58f 107 FROM may be NULL (or will be ignored) if TO is "ENDR".
252b5132
RH
108 Add a new input line to an sb using GET_LINE.
109 Return 1 on success, 0 on unexpected EOF. */
110
111int
254d758c 112buffer_and_nest (const char *from, const char *to, sb *ptr,
39a45edc 113 size_t (*get_line) (sb *))
252b5132 114{
39a45edc
AM
115 size_t from_len;
116 size_t to_len = strlen (to);
252b5132 117 int depth = 1;
39a45edc
AM
118 size_t line_start = ptr->len;
119 size_t more = get_line (ptr);
252b5132 120
4ac14836 121 if (to_len == 4 && strcasecmp (to, "ENDR") == 0)
ca3bc58f
JB
122 {
123 from = NULL;
124 from_len = 0;
125 }
126 else
127 from_len = strlen (from);
128
252b5132
RH
129 while (more)
130 {
0e470c55 131 /* Try to find the first pseudo op on the line. */
39a45edc 132 size_t i = line_start;
f19df8f7 133 bfd_boolean had_colon = FALSE;
252b5132 134
0e470c55
AM
135 /* With normal syntax we can suck what we want till we get
136 to the dot. With the alternate, labels have to start in
137 the first column, since we can't tell what's a label and
138 what's a pseudoop. */
252b5132 139
0e470c55
AM
140 if (! LABELS_WITHOUT_COLONS)
141 {
142 /* Skip leading whitespace. */
143 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
144 i++;
145 }
252b5132 146
0e470c55
AM
147 for (;;)
148 {
149 /* Skip over a label, if any. */
150 if (i >= ptr->len || ! is_name_beginner (ptr->ptr[i]))
151 break;
152 i++;
153 while (i < ptr->len && is_part_of_name (ptr->ptr[i]))
154 i++;
155 if (i < ptr->len && is_name_ender (ptr->ptr[i]))
156 i++;
0e470c55
AM
157 /* Skip whitespace. */
158 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
159 i++;
160 /* Check for the colon. */
161 if (i >= ptr->len || ptr->ptr[i] != ':')
5e75c3ab 162 {
f19df8f7
AM
163 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
164 colon after a label. If we do have a colon on the
165 first label then handle more than one label on the
166 line, assuming that each label has a colon. */
167 if (LABELS_WITHOUT_COLONS && !had_colon)
168 break;
0e470c55
AM
169 i = line_start;
170 break;
5e75c3ab 171 }
0e470c55
AM
172 i++;
173 line_start = i;
f19df8f7 174 had_colon = TRUE;
252b5132 175 }
0e470c55 176
29f8404c 177 /* Skip trailing whitespace. */
252b5132
RH
178 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
179 i++;
180
181 if (i < ptr->len && (ptr->ptr[i] == '.'
ca3bc58f 182 || NO_PSEUDO_DOT
252b5132
RH
183 || macro_mri))
184 {
ca3bc58f 185 if (! flag_m68k_mri && ptr->ptr[i] == '.')
29f8404c 186 i++;
ca3bc58f
JB
187 if (from == NULL
188 && strncasecmp (ptr->ptr + i, "IRPC", from_len = 4) != 0
189 && strncasecmp (ptr->ptr + i, "IRP", from_len = 3) != 0
190 && strncasecmp (ptr->ptr + i, "IREPC", from_len = 5) != 0
191 && strncasecmp (ptr->ptr + i, "IREP", from_len = 4) != 0
192 && strncasecmp (ptr->ptr + i, "REPT", from_len = 4) != 0
193 && strncasecmp (ptr->ptr + i, "REP", from_len = 3) != 0)
194 from_len = 0;
195 if ((from != NULL
196 ? strncasecmp (ptr->ptr + i, from, from_len) == 0
197 : from_len > 0)
29f8404c 198 && (ptr->len == (i + from_len)
5e75c3ab
JB
199 || ! (is_part_of_name (ptr->ptr[i + from_len])
200 || is_name_ender (ptr->ptr[i + from_len]))))
252b5132 201 depth++;
c1eae114 202 if (strncasecmp (ptr->ptr + i, to, to_len) == 0
29f8404c 203 && (ptr->len == (i + to_len)
5e75c3ab
JB
204 || ! (is_part_of_name (ptr->ptr[i + to_len])
205 || is_name_ender (ptr->ptr[i + to_len]))))
252b5132
RH
206 {
207 depth--;
208 if (depth == 0)
209 {
29f8404c 210 /* Reset the string to not include the ending rune. */
252b5132
RH
211 ptr->len = line_start;
212 break;
213 }
214 }
215 }
216
0822d075
NC
217 /* Add the original end-of-line char to the end and keep running. */
218 sb_add_char (ptr, more);
252b5132
RH
219 line_start = ptr->len;
220 more = get_line (ptr);
221 }
222
223 /* Return 1 on success, 0 on unexpected EOF. */
224 return depth == 0;
225}
226
227/* Pick up a token. */
228
39a45edc
AM
229static size_t
230get_token (size_t idx, sb *in, sb *name)
252b5132
RH
231{
232 if (idx < in->len
5e75c3ab 233 && is_name_beginner (in->ptr[idx]))
252b5132
RH
234 {
235 sb_add_char (name, in->ptr[idx++]);
236 while (idx < in->len
5e75c3ab
JB
237 && is_part_of_name (in->ptr[idx]))
238 {
239 sb_add_char (name, in->ptr[idx++]);
240 }
241 if (idx < in->len
242 && is_name_ender (in->ptr[idx]))
252b5132
RH
243 {
244 sb_add_char (name, in->ptr[idx++]);
245 }
246 }
29f8404c 247 /* Ignore trailing &. */
252b5132
RH
248 if (macro_alternate && idx < in->len && in->ptr[idx] == '&')
249 idx++;
250 return idx;
251}
252
253/* Pick up a string. */
254
39a45edc
AM
255static size_t
256getstring (size_t idx, sb *in, sb *acc)
252b5132 257{
252b5132 258 while (idx < in->len
29f8404c 259 && (in->ptr[idx] == '"'
252b5132
RH
260 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
261 || (in->ptr[idx] == '\'' && macro_alternate)))
262 {
2f6178c1 263 if (in->ptr[idx] == '<')
252b5132
RH
264 {
265 int nest = 0;
266 idx++;
0e31b3e1 267 while ((in->ptr[idx] != '>' || nest)
252b5132
RH
268 && idx < in->len)
269 {
270 if (in->ptr[idx] == '!')
271 {
29f8404c 272 idx++;
252b5132
RH
273 sb_add_char (acc, in->ptr[idx++]);
274 }
275 else
276 {
0e31b3e1 277 if (in->ptr[idx] == '>')
252b5132 278 nest--;
0e31b3e1 279 if (in->ptr[idx] == '<')
252b5132
RH
280 nest++;
281 sb_add_char (acc, in->ptr[idx++]);
282 }
283 }
284 idx++;
285 }
286 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
287 {
288 char tchar = in->ptr[idx];
c06ae4f2 289 int escaped = 0;
29f8404c 290
252b5132 291 idx++;
29f8404c 292
252b5132
RH
293 while (idx < in->len)
294 {
29f8404c 295 if (in->ptr[idx - 1] == '\\')
c06ae4f2
UC
296 escaped ^= 1;
297 else
298 escaped = 0;
299
252b5132
RH
300 if (macro_alternate && in->ptr[idx] == '!')
301 {
e972090a 302 idx ++;
29f8404c 303
1994a7c7
NC
304 sb_add_char (acc, in->ptr[idx]);
305
e972090a 306 idx ++;
252b5132 307 }
c06ae4f2
UC
308 else if (escaped && in->ptr[idx] == tchar)
309 {
310 sb_add_char (acc, tchar);
e972090a 311 idx ++;
c06ae4f2 312 }
252b5132
RH
313 else
314 {
315 if (in->ptr[idx] == tchar)
316 {
e972090a 317 idx ++;
29f8404c 318
252b5132
RH
319 if (idx >= in->len || in->ptr[idx] != tchar)
320 break;
321 }
29f8404c 322
252b5132 323 sb_add_char (acc, in->ptr[idx]);
e972090a 324 idx ++;
252b5132
RH
325 }
326 }
327 }
328 }
29f8404c 329
252b5132
RH
330 return idx;
331}
332
333/* Fetch string from the input stream,
334 rules:
335 'Bxyx<whitespace> -> return 'Bxyza
df40eaf9
NC
336 %<expr> -> return string of decimal value of <expr>
337 "string" -> return string
2f6178c1 338 (string) -> return (string-including-whitespaces)
df40eaf9 339 xyx<whitespace> -> return xyz. */
252b5132 340
39a45edc
AM
341static size_t
342get_any_string (size_t idx, sb *in, sb *out)
252b5132
RH
343{
344 sb_reset (out);
345 idx = sb_skip_white (idx, in);
346
347 if (idx < in->len)
348 {
9df59bba 349 if (in->len > idx + 2 && in->ptr[idx + 1] == '\'' && ISBASE (in->ptr[idx]))
252b5132
RH
350 {
351 while (!ISSEP (in->ptr[idx]))
352 sb_add_char (out, in->ptr[idx++]);
353 }
be03cc84 354 else if (in->ptr[idx] == '%' && macro_alternate)
252b5132 355 {
39a45edc 356 offsetT val;
252b5132 357 char buf[20];
df40eaf9 358
29f8404c 359 /* Turns the next expression into a string. */
06f030db 360 /* xgettext: no-c-format */
252b5132
RH
361 idx = (*macro_expr) (_("% operator needs absolute expression"),
362 idx + 1,
363 in,
364 &val);
39a45edc 365 sprintf (buf, "%" BFD_VMA_FMT "d", val);
252b5132
RH
366 sb_add_string (out, buf);
367 }
368 else if (in->ptr[idx] == '"'
369 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
370 || (macro_alternate && in->ptr[idx] == '\''))
371 {
9f6f925e 372 if (macro_alternate && ! macro_strip_at && in->ptr[idx] != '<')
252b5132 373 {
29f8404c 374 /* Keep the quotes. */
9f6f925e 375 sb_add_char (out, '"');
252b5132 376 idx = getstring (idx, in, out);
9f6f925e 377 sb_add_char (out, '"');
252b5132
RH
378 }
379 else
380 {
381 idx = getstring (idx, in, out);
382 }
383 }
29f8404c 384 else
252b5132 385 {
4ac14836 386 char *br_buf = (char *) xmalloc (1);
0e31b3e1
JB
387 char *in_br = br_buf;
388
389 *in_br = '\0';
29f8404c 390 while (idx < in->len
0e31b3e1
JB
391 && (*in_br
392 || (in->ptr[idx] != ' '
393 && in->ptr[idx] != '\t'))
be03cc84
JB
394 && in->ptr[idx] != ','
395 && (in->ptr[idx] != '<'
396 || (! macro_alternate && ! macro_mri)))
252b5132 397 {
0e31b3e1 398 char tchar = in->ptr[idx];
df40eaf9 399
0e31b3e1
JB
400 switch (tchar)
401 {
402 case '"':
403 case '\'':
252b5132
RH
404 sb_add_char (out, in->ptr[idx++]);
405 while (idx < in->len
406 && in->ptr[idx] != tchar)
29f8404c 407 sb_add_char (out, in->ptr[idx++]);
252b5132 408 if (idx == in->len)
4ac14836
NC
409 {
410 free (br_buf);
411 return idx;
412 }
0e31b3e1
JB
413 break;
414 case '(':
415 case '[':
416 if (in_br > br_buf)
417 --in_br;
418 else
419 {
4ac14836
NC
420 br_buf = (char *) xmalloc (strlen (in_br) + 2);
421 strcpy (br_buf + 1, in_br);
422 free (in_br);
0e31b3e1
JB
423 in_br = br_buf;
424 }
425 *in_br = tchar;
426 break;
427 case ')':
428 if (*in_br == '(')
429 ++in_br;
430 break;
431 case ']':
432 if (*in_br == '[')
433 ++in_br;
434 break;
252b5132 435 }
0e31b3e1
JB
436 sb_add_char (out, tchar);
437 ++idx;
252b5132 438 }
4ac14836 439 free (br_buf);
252b5132
RH
440 }
441 }
442
443 return idx;
444}
445
6eaeac8a
JB
446/* Allocate a new formal. */
447
448static formal_entry *
449new_formal (void)
450{
451 formal_entry *formal;
452
1e9cc1c2 453 formal = (formal_entry *) xmalloc (sizeof (formal_entry));
6eaeac8a
JB
454
455 sb_new (&formal->name);
456 sb_new (&formal->def);
457 sb_new (&formal->actual);
458 formal->next = NULL;
459 formal->type = FORMAL_OPTIONAL;
460 return formal;
461}
462
463/* Free a formal. */
464
465static void
466del_formal (formal_entry *formal)
467{
468 sb_kill (&formal->actual);
469 sb_kill (&formal->def);
470 sb_kill (&formal->name);
471 free (formal);
472}
473
252b5132
RH
474/* Pick up the formal parameters of a macro definition. */
475
39a45edc
AM
476static size_t
477do_formals (macro_entry *macro, size_t idx, sb *in)
252b5132
RH
478{
479 formal_entry **p = &macro->formals;
02ddf156 480 const char *name;
252b5132 481
057f53c1 482 idx = sb_skip_white (idx, in);
252b5132
RH
483 while (idx < in->len)
484 {
6eaeac8a 485 formal_entry *formal = new_formal ();
39a45edc 486 size_t cidx;
252b5132 487
252b5132
RH
488 idx = get_token (idx, in, &formal->name);
489 if (formal->name.len == 0)
057f53c1
JB
490 {
491 if (macro->formal_count)
492 --idx;
4ac14836 493 del_formal (formal); /* 'formal' goes out of scope. */
057f53c1
JB
494 break;
495 }
252b5132 496 idx = sb_skip_white (idx, in);
057f53c1 497 /* This is a formal. */
6eaeac8a
JB
498 name = sb_terminate (&formal->name);
499 if (! macro_mri
500 && idx < in->len
501 && in->ptr[idx] == ':'
502 && (! is_name_beginner (':')
503 || idx + 1 >= in->len
504 || ! is_part_of_name (in->ptr[idx + 1])))
505 {
506 /* Got a qualifier. */
507 sb qual;
508
509 sb_new (&qual);
510 idx = get_token (sb_skip_white (idx + 1, in), in, &qual);
511 sb_terminate (&qual);
512 if (qual.len == 0)
513 as_bad_where (macro->file,
514 macro->line,
515 _("Missing parameter qualifier for `%s' in macro `%s'"),
516 name,
517 macro->name);
518 else if (strcmp (qual.ptr, "req") == 0)
519 formal->type = FORMAL_REQUIRED;
520 else if (strcmp (qual.ptr, "vararg") == 0)
521 formal->type = FORMAL_VARARG;
522 else
523 as_bad_where (macro->file,
524 macro->line,
525 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
526 qual.ptr,
527 name,
528 macro->name);
529 sb_kill (&qual);
530 idx = sb_skip_white (idx, in);
531 }
057f53c1 532 if (idx < in->len && in->ptr[idx] == '=')
252b5132 533 {
057f53c1 534 /* Got a default. */
be03cc84 535 idx = get_any_string (idx + 1, in, &formal->def);
057f53c1 536 idx = sb_skip_white (idx, in);
6eaeac8a
JB
537 if (formal->type == FORMAL_REQUIRED)
538 {
539 sb_reset (&formal->def);
540 as_warn_where (macro->file,
541 macro->line,
542 _("Pointless default value for required parameter `%s' in macro `%s'"),
543 name,
544 macro->name);
545 }
252b5132
RH
546 }
547
29f8404c 548 /* Add to macro's hash table. */
02ddf156
JB
549 if (! hash_find (macro->formal_hash, name))
550 hash_jam (macro->formal_hash, name, formal);
551 else
552 as_bad_where (macro->file,
553 macro->line,
554 _("A parameter named `%s' already exists for macro `%s'"),
555 name,
556 macro->name);
252b5132 557
057f53c1 558 formal->index = macro->formal_count++;
6eaeac8a
JB
559 *p = formal;
560 p = &formal->next;
561 if (formal->type == FORMAL_VARARG)
562 break;
057f53c1 563 cidx = idx;
252b5132 564 idx = sb_skip_comma (idx, in);
057f53c1
JB
565 if (idx != cidx && idx >= in->len)
566 {
567 idx = cidx;
568 break;
569 }
252b5132
RH
570 }
571
572 if (macro_mri)
573 {
6eaeac8a 574 formal_entry *formal = new_formal ();
252b5132
RH
575
576 /* Add a special NARG formal, which macro_expand will set to the
4c665b71 577 number of arguments. */
252b5132 578 /* The same MRI assemblers which treat '@' characters also use
4c665b71 579 the name $NARG. At least until we find an exception. */
252b5132
RH
580 if (macro_strip_at)
581 name = "$NARG";
582 else
583 name = "NARG";
584
585 sb_add_string (&formal->name, name);
586
29f8404c 587 /* Add to macro's hash table. */
02ddf156
JB
588 if (hash_find (macro->formal_hash, name))
589 as_bad_where (macro->file,
590 macro->line,
591 _("Reserved word `%s' used as parameter in macro `%s'"),
592 name,
593 macro->name);
252b5132
RH
594 hash_jam (macro->formal_hash, name, formal);
595
596 formal->index = NARG_INDEX;
597 *p = formal;
252b5132
RH
598 }
599
600 return idx;
601}
602
f19df8f7
AM
603/* Free the memory allocated to a macro. */
604
605static void
606free_macro (macro_entry *macro)
607{
608 formal_entry *formal;
609
610 for (formal = macro->formals; formal; )
611 {
612 formal_entry *f;
613
614 f = formal;
615 formal = formal->next;
616 del_formal (f);
617 }
618 hash_die (macro->formal_hash);
619 sb_kill (&macro->sub);
620 free (macro);
621}
622
252b5132
RH
623/* Define a new macro. Returns NULL on success, otherwise returns an
624 error message. If NAMEP is not NULL, *NAMEP is set to the name of
625 the macro which was defined. */
626
627const char *
39a45edc
AM
628define_macro (size_t idx, sb *in, sb *label,
629 size_t (*get_line) (sb *),
02ddf156
JB
630 char *file, unsigned int line,
631 const char **namep)
252b5132
RH
632{
633 macro_entry *macro;
634 sb name;
02ddf156 635 const char *error = NULL;
252b5132
RH
636
637 macro = (macro_entry *) xmalloc (sizeof (macro_entry));
638 sb_new (&macro->sub);
639 sb_new (&name);
02ddf156
JB
640 macro->file = file;
641 macro->line = line;
252b5132
RH
642
643 macro->formal_count = 0;
644 macro->formals = 0;
4c665b71 645 macro->formal_hash = hash_new_sized (7);
252b5132
RH
646
647 idx = sb_skip_white (idx, in);
648 if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
02ddf156 649 error = _("unexpected end of file in macro `%s' definition");
252b5132
RH
650 if (label != NULL && label->len != 0)
651 {
652 sb_add_sb (&name, label);
02ddf156 653 macro->name = sb_terminate (&name);
252b5132
RH
654 if (idx < in->len && in->ptr[idx] == '(')
655 {
29f8404c 656 /* It's the label: MACRO (formals,...) sort */
252b5132 657 idx = do_formals (macro, idx + 1, in);
02ddf156
JB
658 if (idx < in->len && in->ptr[idx] == ')')
659 idx = sb_skip_white (idx + 1, in);
660 else if (!error)
661 error = _("missing `)' after formals in macro definition `%s'");
252b5132
RH
662 }
663 else
664 {
29f8404c 665 /* It's the label: MACRO formals,... sort */
252b5132
RH
666 idx = do_formals (macro, idx, in);
667 }
668 }
669 else
670 {
39a45edc 671 size_t cidx;
057f53c1 672
252b5132 673 idx = get_token (idx, in, &name);
02ddf156 674 macro->name = sb_terminate (&name);
057f53c1 675 if (name.len == 0)
02ddf156 676 error = _("Missing macro name");
057f53c1
JB
677 cidx = sb_skip_white (idx, in);
678 idx = sb_skip_comma (cidx, in);
679 if (idx == cidx || idx < in->len)
680 idx = do_formals (macro, idx, in);
681 else
682 idx = cidx;
252b5132 683 }
02ddf156
JB
684 if (!error && idx < in->len)
685 error = _("Bad parameter list for macro `%s'");
252b5132 686
29f8404c 687 /* And stick it in the macro hash table. */
252b5132 688 for (idx = 0; idx < name.len; idx++)
3882b010 689 name.ptr[idx] = TOLOWER (name.ptr[idx]);
02ddf156
JB
690 if (hash_find (macro_hash, macro->name))
691 error = _("Macro `%s' was already defined");
692 if (!error)
5a49b8ac 693 error = hash_jam (macro_hash, macro->name, (void *) macro);
252b5132
RH
694
695 if (namep != NULL)
02ddf156
JB
696 *namep = macro->name;
697
698 if (!error)
699 macro_defined = 1;
700 else
701 free_macro (macro);
252b5132 702
02ddf156 703 return error;
252b5132
RH
704}
705
706/* Scan a token, and then skip KIND. */
707
39a45edc
AM
708static size_t
709get_apost_token (size_t idx, sb *in, sb *name, int kind)
252b5132
RH
710{
711 idx = get_token (idx, in, name);
712 if (idx < in->len
713 && in->ptr[idx] == kind
714 && (! macro_mri || macro_strip_at)
715 && (! macro_strip_at || kind == '@'))
716 idx++;
717 return idx;
718}
719
720/* Substitute the actual value for a formal parameter. */
721
39a45edc
AM
722static size_t
723sub_actual (size_t start, sb *in, sb *t, struct hash_control *formal_hash,
254d758c 724 int kind, sb *out, int copyifnotthere)
252b5132 725{
39a45edc 726 size_t src;
252b5132
RH
727 formal_entry *ptr;
728
729 src = get_apost_token (start, in, t, kind);
730 /* See if it's in the macro's hash table, unless this is
731 macro_strip_at and kind is '@' and the token did not end in '@'. */
732 if (macro_strip_at
733 && kind == '@'
734 && (src == start || in->ptr[src - 1] != '@'))
735 ptr = NULL;
736 else
737 ptr = (formal_entry *) hash_find (formal_hash, sb_terminate (t));
738 if (ptr)
739 {
740 if (ptr->actual.len)
741 {
742 sb_add_sb (out, &ptr->actual);
743 }
744 else
745 {
746 sb_add_sb (out, &ptr->def);
747 }
748 }
749 else if (kind == '&')
750 {
751 /* Doing this permits people to use & in macro bodies. */
752 sb_add_char (out, '&');
c1ed1235 753 sb_add_sb (out, t);
d1f52f54
AM
754 if (src != start && in->ptr[src - 1] == '&')
755 sb_add_char (out, '&');
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;
39a45edc
AM
776 size_t src = 0;
777 int 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 {
d1f52f54
AM
797 /* Permit macro parameter substition delineated with
798 an '&' prefix and optional '&' suffix. */
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
aa27de95
NC
876 || ! ISWHITE (in->ptr[src + 5])
877 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
878 || inquote)
252b5132
RH
879 {
880 sb_reset (&t);
881 src = sub_actual (src, in, &t, formal_hash,
882 (macro_strip_at && inquote) ? '@' : '\'',
883 out, 1);
884 }
885 else
886 {
252b5132 887 src = sb_skip_white (src + 5, in);
fea17916 888 while (in->ptr[src] != '\n')
252b5132 889 {
02ddf156 890 const char *name;
6eaeac8a 891 formal_entry *f = new_formal ();
252b5132 892
252b5132 893 src = get_token (src, in, &f->name);
02ddf156
JB
894 name = sb_terminate (&f->name);
895 if (! hash_find (formal_hash, name))
896 {
897 static int loccnt;
898 char buf[20];
252b5132 899
02ddf156
JB
900 f->index = LOCAL_INDEX;
901 f->next = loclist;
902 loclist = f;
903
904 sprintf (buf, IS_ELF ? ".LL%04x" : "LL%04x", ++loccnt);
905 sb_add_string (&f->actual, buf);
906
907 err = hash_jam (formal_hash, name, f);
908 if (err != NULL)
909 break;
910 }
911 else
912 {
913 as_bad_where (macro->file,
914 macro->line + macro_line,
915 _("`%s' was already used as parameter (or another local) name"),
916 name);
6eaeac8a 917 del_formal (f);
02ddf156 918 }
252b5132
RH
919
920 src = sb_skip_comma (src, in);
921 }
922 }
923 }
252b5132
RH
924 else if (in->ptr[src] == '"'
925 || (macro_mri && in->ptr[src] == '\''))
926 {
927 inquote = !inquote;
928 sb_add_char (out, in->ptr[src++]);
929 }
930 else if (in->ptr[src] == '@' && macro_strip_at)
931 {
932 ++src;
933 if (src < in->len
934 && in->ptr[src] == '@')
935 {
936 sb_add_char (out, '@');
937 ++src;
938 }
939 }
940 else if (macro_mri
941 && in->ptr[src] == '='
942 && src + 1 < in->len
943 && in->ptr[src + 1] == '=')
944 {
945 formal_entry *ptr;
946
947 sb_reset (&t);
948 src = get_token (src + 2, in, &t);
949 ptr = (formal_entry *) hash_find (formal_hash, sb_terminate (&t));
950 if (ptr == NULL)
951 {
952 /* FIXME: We should really return a warning string here,
4c665b71
RM
953 but we can't, because the == might be in the MRI
954 comment field, and, since the nature of the MRI
955 comment field depends upon the exact instruction
956 being used, we don't have enough information here to
957 figure out whether it is or not. Instead, we leave
958 the == in place, which should cause a syntax error if
959 it is not in a comment. */
252b5132
RH
960 sb_add_char (out, '=');
961 sb_add_char (out, '=');
962 sb_add_sb (out, &t);
963 }
964 else
965 {
966 if (ptr->actual.len)
967 {
968 sb_add_string (out, "-1");
969 }
970 else
971 {
972 sb_add_char (out, '0');
973 }
974 }
975 }
976 else
977 {
02ddf156
JB
978 if (in->ptr[src] == '\n')
979 ++macro_line;
252b5132
RH
980 sb_add_char (out, in->ptr[src++]);
981 }
982 }
983
984 sb_kill (&t);
985
986 while (loclist != NULL)
987 {
988 formal_entry *f;
818236e5 989 const char *name;
252b5132
RH
990
991 f = loclist->next;
818236e5
AM
992 name = sb_terminate (&loclist->name);
993 hash_delete (formal_hash, name, f == NULL);
6eaeac8a 994 del_formal (loclist);
252b5132
RH
995 loclist = f;
996 }
997
02ddf156 998 return err;
252b5132
RH
999}
1000
1001/* Assign values to the formal parameters of a macro, and expand the
1002 body. */
1003
1004static const char *
39a45edc 1005macro_expand (size_t idx, sb *in, macro_entry *m, sb *out)
252b5132
RH
1006{
1007 sb t;
1008 formal_entry *ptr;
1009 formal_entry *f;
252b5132
RH
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
4c665b71 1026 be referred to in the macro body as \0. */
252b5132 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
be03cc84 1043 idx = get_any_string (idx, in, &n->actual);
d1a6c242
KH
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 1051 {
39a45edc 1052 size_t scan;
252b5132 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)
c0ba1095
AM
1080 {
1081 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1082 t.ptr,
1083 m->name);
1084 sb_reset (&t);
1085 idx = get_any_string (idx + 1, in, &t);
1086 }
252b5132
RH
1087 else
1088 {
29f8404c 1089 /* Insert this value into the right place. */
6eaeac8a
JB
1090 if (ptr->actual.len)
1091 {
1092 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1093 ptr->name.ptr,
1094 m->name);
1095 sb_reset (&ptr->actual);
1096 }
be03cc84 1097 idx = get_any_string (idx + 1, in, &ptr->actual);
252b5132
RH
1098 if (ptr->actual.len > 0)
1099 ++narg;
1100 }
1101 }
1102 else
1103 {
252b5132 1104 if (is_keyword)
6eaeac8a
JB
1105 {
1106 err = _("can't mix positional and keyword arguments");
1107 break;
1108 }
252b5132
RH
1109
1110 if (!f)
1111 {
1112 formal_entry **pf;
1113 int c;
1114
1115 if (!macro_mri)
6eaeac8a
JB
1116 {
1117 err = _("too many positional arguments");
1118 break;
1119 }
252b5132 1120
6eaeac8a 1121 f = new_formal ();
252b5132
RH
1122
1123 c = -1;
1124 for (pf = &m->formals; *pf != NULL; pf = &(*pf)->next)
1125 if ((*pf)->index >= c)
1126 c = (*pf)->index + 1;
1127 if (c == -1)
1128 c = 0;
1129 *pf = f;
1130 f->index = c;
1131 }
1132
6eaeac8a 1133 if (f->type != FORMAL_VARARG)
be03cc84 1134 idx = get_any_string (idx, in, &f->actual);
6eaeac8a
JB
1135 else
1136 {
1137 sb_add_buffer (&f->actual, in->ptr + idx, in->len - idx);
1138 idx = in->len;
1139 }
252b5132
RH
1140 if (f->actual.len > 0)
1141 ++narg;
1142 do
1143 {
1144 f = f->next;
1145 }
1146 while (f != NULL && f->index < 0);
1147 }
1148
1149 if (! macro_mri)
1150 idx = sb_skip_comma (idx, in);
1151 else
1152 {
1153 if (in->ptr[idx] == ',')
1154 ++idx;
1155 if (ISWHITE (in->ptr[idx]))
1156 break;
1157 }
1158 }
1159
6eaeac8a 1160 if (! err)
252b5132 1161 {
6eaeac8a
JB
1162 for (ptr = m->formals; ptr; ptr = ptr->next)
1163 {
1164 if (ptr->type == FORMAL_REQUIRED && ptr->actual.len == 0)
1165 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1166 ptr->name.ptr,
1167 m->name);
1168 }
252b5132 1169
6eaeac8a
JB
1170 if (macro_mri)
1171 {
1172 char buffer[20];
1173
1174 sb_reset (&t);
1175 sb_add_string (&t, macro_strip_at ? "$NARG" : "NARG");
1176 ptr = (formal_entry *) hash_find (m->formal_hash, sb_terminate (&t));
1177 sprintf (buffer, "%d", narg);
1178 sb_add_string (&ptr->actual, buffer);
1179 }
1180
1181 err = macro_expand_body (&m->sub, out, m->formals, m->formal_hash, m);
1182 }
252b5132
RH
1183
1184 /* Discard any unnamed formal arguments. */
1185 if (macro_mri)
1186 {
1187 formal_entry **pf;
1188
1189 pf = &m->formals;
1190 while (*pf != NULL)
1191 {
1192 if ((*pf)->name.len != 0)
1193 pf = &(*pf)->next;
1194 else
1195 {
252b5132 1196 f = (*pf)->next;
6eaeac8a 1197 del_formal (*pf);
252b5132
RH
1198 *pf = f;
1199 }
1200 }
1201 }
1202
1203 sb_kill (&t);
02ddf156
JB
1204 if (!err)
1205 macro_number++;
252b5132 1206
02ddf156 1207 return err;
252b5132
RH
1208}
1209
1210/* Check for a macro. If one is found, put the expansion into
fea17916 1211 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
252b5132
RH
1212
1213int
254d758c
KH
1214check_macro (const char *line, sb *expand,
1215 const char **error, macro_entry **info)
252b5132
RH
1216{
1217 const char *s;
91d6fa6a 1218 char *copy, *cls;
252b5132
RH
1219 macro_entry *macro;
1220 sb line_sb;
1221
5e75c3ab 1222 if (! is_name_beginner (*line)
252b5132
RH
1223 && (! macro_mri || *line != '.'))
1224 return 0;
1225
1226 s = line + 1;
5e75c3ab
JB
1227 while (is_part_of_name (*s))
1228 ++s;
1229 if (is_name_ender (*s))
252b5132
RH
1230 ++s;
1231
1232 copy = (char *) alloca (s - line + 1);
1233 memcpy (copy, line, s - line);
1234 copy[s - line] = '\0';
91d6fa6a
NC
1235 for (cls = copy; *cls != '\0'; cls ++)
1236 *cls = TOLOWER (*cls);
252b5132
RH
1237
1238 macro = (macro_entry *) hash_find (macro_hash, copy);
1239
1240 if (macro == NULL)
1241 return 0;
1242
1243 /* Wrap the line up in an sb. */
1244 sb_new (&line_sb);
1245 while (*s != '\0' && *s != '\n' && *s != '\r')
1246 sb_add_char (&line_sb, *s++);
1247
1248 sb_new (expand);
fea17916 1249 *error = macro_expand (0, &line_sb, macro, expand);
252b5132
RH
1250
1251 sb_kill (&line_sb);
1252
29f8404c 1253 /* Export the macro information if requested. */
9f10757c
TW
1254 if (info)
1255 *info = macro;
1256
252b5132
RH
1257 return 1;
1258}
1259
1260/* Delete a macro. */
1261
1262void
254d758c 1263delete_macro (const char *name)
252b5132 1264{
e6ca91be
JB
1265 char *copy;
1266 size_t i, len;
1267 macro_entry *macro;
1268
1269 len = strlen (name);
1270 copy = (char *) alloca (len + 1);
1271 for (i = 0; i < len; ++i)
1272 copy[i] = TOLOWER (name[i]);
1273 copy[i] = '\0';
1274
818236e5
AM
1275 /* We can only ask hash_delete to free memory if we are deleting
1276 macros in reverse order to their definition.
1277 So just clear out the entry. */
1e9cc1c2 1278 if ((macro = (macro_entry *) hash_find (macro_hash, copy)) != NULL)
e6ca91be
JB
1279 {
1280 hash_jam (macro_hash, copy, NULL);
1281 free_macro (macro);
1282 }
1283 else
1284 as_warn (_("Attempt to purge non-existant macro `%s'"), copy);
252b5132
RH
1285}
1286
1287/* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1288 combined macro definition and execution. This returns NULL on
1289 success, or an error message otherwise. */
1290
1291const char *
39a45edc 1292expand_irp (int irpc, size_t idx, sb *in, sb *out, size_t (*get_line) (sb *))
252b5132 1293{
252b5132
RH
1294 sb sub;
1295 formal_entry f;
1296 struct hash_control *h;
1297 const char *err;
1298
252b5132
RH
1299 idx = sb_skip_white (idx, in);
1300
1301 sb_new (&sub);
ca3bc58f 1302 if (! buffer_and_nest (NULL, "ENDR", &sub, get_line))
252b5132 1303 return _("unexpected end of file in irp or irpc");
29f8404c 1304
252b5132
RH
1305 sb_new (&f.name);
1306 sb_new (&f.def);
1307 sb_new (&f.actual);
1308
1309 idx = get_token (idx, in, &f.name);
1310 if (f.name.len == 0)
1311 return _("missing model parameter");
1312
1313 h = hash_new ();
1314 err = hash_jam (h, sb_terminate (&f.name), &f);
1315 if (err != NULL)
1316 return err;
1317
1318 f.index = 1;
1319 f.next = NULL;
6eaeac8a 1320 f.type = FORMAL_OPTIONAL;
252b5132
RH
1321
1322 sb_reset (out);
1323
1324 idx = sb_skip_comma (idx, in);
fea17916 1325 if (idx >= in->len)
252b5132
RH
1326 {
1327 /* Expand once with a null string. */
fea17916 1328 err = macro_expand_body (&sub, out, &f, h, 0);
252b5132
RH
1329 }
1330 else
1331 {
465e5617
NC
1332 bfd_boolean in_quotes = FALSE;
1333
252b5132 1334 if (irpc && in->ptr[idx] == '"')
465e5617
NC
1335 {
1336 in_quotes = TRUE;
1337 ++idx;
1338 }
1339
fea17916 1340 while (idx < in->len)
252b5132
RH
1341 {
1342 if (!irpc)
be03cc84 1343 idx = get_any_string (idx, in, &f.actual);
252b5132
RH
1344 else
1345 {
1346 if (in->ptr[idx] == '"')
1347 {
39a45edc 1348 size_t nxt;
252b5132 1349
465e5617
NC
1350 if (irpc)
1351 in_quotes = ! in_quotes;
4c665b71 1352
252b5132 1353 nxt = sb_skip_white (idx + 1, in);
fea17916 1354 if (nxt >= in->len)
252b5132
RH
1355 {
1356 idx = nxt;
1357 break;
1358 }
1359 }
1360 sb_reset (&f.actual);
1361 sb_add_char (&f.actual, in->ptr[idx]);
1362 ++idx;
1363 }
465e5617 1364
fea17916 1365 err = macro_expand_body (&sub, out, &f, h, 0);
252b5132 1366 if (err != NULL)
02ddf156 1367 break;
252b5132
RH
1368 if (!irpc)
1369 idx = sb_skip_comma (idx, in);
465e5617 1370 else if (! in_quotes)
252b5132
RH
1371 idx = sb_skip_white (idx, in);
1372 }
1373 }
1374
1375 hash_die (h);
6eaeac8a
JB
1376 sb_kill (&f.actual);
1377 sb_kill (&f.def);
1378 sb_kill (&f.name);
252b5132
RH
1379 sb_kill (&sub);
1380
02ddf156 1381 return err;
252b5132 1382}