Coverage Report

Created: 2026-07-25 10:20

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