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