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