arm: Support pac_key_* register operand for MRS/MSR in Armv8.1-M Mainline
[binutils-gdb.git] / gas / macro.c
blob8b376f7f490ba6923f99c8b77875b0946b937989
1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2024 Free Software Foundation, Inc.
4 Written by Steve and Judy Chamberlain of Cygnus Support,
5 sac@cygnus.com
7 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
24 #include "as.h"
25 #include "safe-ctype.h"
26 #include "sb.h"
27 #include "macro.h"
29 /* The routines in this file handle macro definition and expansion.
30 They are called by gas. */
32 #define ISWHITE(x) ((x) == ' ' || (x) == '\t')
34 #define ISSEP(x) \
35 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
36 || (x) == ')' || (x) == '(' \
37 || ((flag_macro_alternate || flag_mri) && ((x) == '<' || (x) == '>')))
39 #define ISBASE(x) \
40 ((x) == 'b' || (x) == 'B' \
41 || (x) == 'q' || (x) == 'Q' \
42 || (x) == 'h' || (x) == 'H' \
43 || (x) == 'd' || (x) == 'D')
45 /* The macro hash table. */
47 htab_t macro_hash;
49 /* Whether any macros have been defined. */
51 int macro_defined;
53 /* Whether we should strip '@' characters. */
55 #define macro_strip_at false
57 /* Number of macro expansions that have been done. */
59 static unsigned int macro_number;
61 static void free_macro (macro_entry *);
63 static void
64 macro_del_f (void *ent)
66 string_tuple_t *tuple = ent;
67 free_macro ((macro_entry *) tuple->value);
70 /* Initialize macro processing. */
72 void
73 macro_init (void)
75 macro_hash = htab_create_alloc (16, hash_string_tuple, eq_string_tuple,
76 macro_del_f, notes_calloc, NULL);
77 macro_defined = 0;
80 void
81 macro_end (void)
83 htab_delete (macro_hash);
86 /* Read input lines till we get to a TO string.
87 Increase nesting depth if we get a FROM string.
88 Put the results into sb at PTR.
89 FROM may be NULL (or will be ignored) if TO is "ENDR".
90 Add a new input line to an sb using GET_LINE.
91 Return 1 on success, 0 on unexpected EOF. */
93 int
94 buffer_and_nest (const char *from, const char *to, sb *ptr,
95 size_t (*get_line) (sb *))
97 size_t from_len;
98 size_t to_len = strlen (to);
99 int depth = 1;
100 size_t line_start, more;
102 if (to_len == 4 && strcasecmp (to, "ENDR") == 0)
104 from = NULL;
105 from_len = 0;
107 else
108 from_len = strlen (from);
110 /* Record the present source position, such that diagnostics and debug info
111 can be properly associated with the respective original lines, rather
112 than with the line of the ending directive (TO). */
114 unsigned int line;
115 char *linefile;
117 as_where_top (&line);
118 if (!flag_m68k_mri)
119 linefile = xasprintf ("\t.linefile %u .", line + 1);
120 else
121 linefile = xasprintf ("\tlinefile %u .", line + 1);
122 sb_add_string (ptr, linefile);
123 xfree (linefile);
126 line_start = ptr->len;
127 more = get_line (ptr);
128 while (more)
130 /* Try to find the first pseudo op on the line. */
131 size_t i = line_start;
132 bool had_colon = false;
134 /* With normal syntax we can suck what we want till we get
135 to the dot. With the alternate, labels have to start in
136 the first column, since we can't tell what's a label and
137 what's a pseudoop. */
139 if (! LABELS_WITHOUT_COLONS)
141 /* Skip leading whitespace. */
142 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
143 i++;
146 for (;;)
148 /* Skip over a label, if any. */
149 if (i >= ptr->len || ! is_name_beginner (ptr->ptr[i]))
150 break;
151 i++;
152 while (i < ptr->len && is_part_of_name (ptr->ptr[i]))
153 i++;
154 if (i < ptr->len && is_name_ender (ptr->ptr[i]))
155 i++;
156 /* Skip whitespace. */
157 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
158 i++;
159 /* Check for the colon. */
160 if (i >= ptr->len || ptr->ptr[i] != ':')
162 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
163 colon after a label. If we do have a colon on the
164 first label then handle more than one label on the
165 line, assuming that each label has a colon. */
166 if (LABELS_WITHOUT_COLONS && !had_colon)
167 break;
168 i = line_start;
169 break;
171 i++;
172 line_start = i;
173 had_colon = true;
176 /* Skip trailing whitespace. */
177 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
178 i++;
180 if (i < ptr->len && (ptr->ptr[i] == '.'
181 || NO_PSEUDO_DOT
182 || flag_mri))
184 if (! flag_m68k_mri && ptr->ptr[i] == '.')
185 i++;
186 size_t len = ptr->len - i;
187 if (from == NULL)
189 if (len >= 5 && strncasecmp (ptr->ptr + i, "IREPC", 5) == 0)
190 from_len = 5;
191 else if (len >= 4 && strncasecmp (ptr->ptr + i, "IREP", 4) == 0)
192 from_len = 4;
193 else if (len >= 4 && strncasecmp (ptr->ptr + i, "IRPC", 4) == 0)
194 from_len = 4;
195 else if (len >= 4 && strncasecmp (ptr->ptr + i, "REPT", 4) == 0)
196 from_len = 4;
197 else if (len >= 3 && strncasecmp (ptr->ptr + i, "IRP", 3) == 0)
198 from_len = 3;
199 else if (len >= 3 && strncasecmp (ptr->ptr + i, "REP", 3) == 0)
200 from_len = 3;
201 else
202 from_len = 0;
204 if ((from != NULL
205 ? (len >= from_len
206 && strncasecmp (ptr->ptr + i, from, from_len) == 0)
207 : from_len > 0)
208 && (len == from_len
209 || ! (is_part_of_name (ptr->ptr[i + from_len])
210 || is_name_ender (ptr->ptr[i + from_len]))))
211 depth++;
212 if (len >= to_len
213 && strncasecmp (ptr->ptr + i, to, to_len) == 0
214 && (len == to_len
215 || ! (is_part_of_name (ptr->ptr[i + to_len])
216 || is_name_ender (ptr->ptr[i + to_len]))))
218 depth--;
219 if (depth == 0)
221 /* Reset the string to not include the ending rune. */
222 ptr->len = line_start;
224 /* With the ending directive consumed here, announce the
225 line for macro-expanded listings. */
226 if (listing & LISTING_MACEXP)
227 listing_newline (NULL);
228 break;
232 /* PR gas/16908
233 Apply .linefile directives that appear within the macro, alongside
234 keeping them for later expansion of the macro. */
235 if (from != NULL && strcasecmp (from, "MACRO") == 0
236 && len >= 8 && strncasecmp (ptr->ptr + i, "linefile", 8) == 0)
238 sb_add_char (ptr, more);
239 temp_ilp (sb_terminate (ptr) + i + 8);
240 s_linefile (0);
241 restore_ilp ();
242 line_start = ptr->len;
243 more = get_line (ptr);
244 continue;
248 /* Add the original end-of-line char to the end and keep running. */
249 sb_add_char (ptr, more);
250 line_start = ptr->len;
251 more = get_line (ptr);
254 /* Return 1 on success, 0 on unexpected EOF. */
255 return depth == 0;
258 /* Pick up a token. */
260 static size_t
261 get_token (size_t idx, sb *in, sb *name)
263 if (idx < in->len
264 && is_name_beginner (in->ptr[idx]))
266 sb_add_char (name, in->ptr[idx++]);
267 while (idx < in->len
268 && is_part_of_name (in->ptr[idx]))
270 sb_add_char (name, in->ptr[idx++]);
272 if (idx < in->len
273 && is_name_ender (in->ptr[idx]))
275 sb_add_char (name, in->ptr[idx++]);
278 /* Ignore trailing &. */
279 if (flag_macro_alternate && idx < in->len && in->ptr[idx] == '&')
280 idx++;
281 return idx;
284 /* Pick up a string. */
286 static size_t
287 getstring (size_t idx, sb *in, sb *acc)
289 while (idx < in->len
290 && (in->ptr[idx] == '"'
291 || (in->ptr[idx] == '<' && (flag_macro_alternate || flag_mri))
292 || (in->ptr[idx] == '\'' && flag_macro_alternate)))
294 if (in->ptr[idx] == '<')
296 int nest = 0;
297 idx++;
298 while (idx < in->len
299 && (in->ptr[idx] != '>' || nest))
301 if (in->ptr[idx] == '!')
303 idx++;
304 sb_add_char (acc, in->ptr[idx++]);
306 else
308 if (in->ptr[idx] == '>')
309 nest--;
310 if (in->ptr[idx] == '<')
311 nest++;
312 sb_add_char (acc, in->ptr[idx++]);
315 idx++;
317 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
319 char tchar = in->ptr[idx];
320 int escaped = 0;
322 idx++;
324 while (idx < in->len)
326 if (in->ptr[idx - 1] == '\\')
327 escaped ^= 1;
328 else
329 escaped = 0;
331 if (flag_macro_alternate && in->ptr[idx] == '!')
333 idx ++;
335 sb_add_char (acc, in->ptr[idx]);
337 idx ++;
339 else if (escaped && in->ptr[idx] == tchar)
341 sb_add_char (acc, tchar);
342 idx ++;
344 else
346 if (in->ptr[idx] == tchar)
348 idx ++;
350 if (idx >= in->len || in->ptr[idx] != tchar)
351 break;
354 sb_add_char (acc, in->ptr[idx]);
355 idx ++;
361 return idx;
364 /* Fetch string from the input stream,
365 rules:
366 'Bxyx<whitespace> -> return 'Bxyza
367 %<expr> -> return string of decimal value of <expr>
368 "string" -> return string
369 (string) -> return (string-including-whitespaces)
370 xyx<whitespace> -> return xyz. */
372 static size_t
373 get_any_string (size_t idx, sb *in, sb *out)
375 sb_reset (out);
376 idx = sb_skip_white (idx, in);
378 if (idx < in->len)
380 if (in->len > idx + 2 && in->ptr[idx + 1] == '\'' && ISBASE (in->ptr[idx]))
382 while (idx < in->len && !ISSEP (in->ptr[idx]))
383 sb_add_char (out, in->ptr[idx++]);
385 else if (in->ptr[idx] == '%' && flag_macro_alternate)
387 /* Turn the following expression into a string. */
388 expressionS ex;
389 char buf[64];
391 sb_terminate (in);
393 temp_ilp (in->ptr + idx + 1);
394 expression_and_evaluate (&ex);
395 idx = input_line_pointer - in->ptr;
396 restore_ilp ();
398 if (ex.X_op != O_constant)
399 as_bad (_("%% operator needs absolute expression"));
401 sprintf (buf, "%" PRId64, (int64_t) ex.X_add_number);
402 sb_add_string (out, buf);
404 else if (in->ptr[idx] == '"'
405 || (in->ptr[idx] == '<' && (flag_macro_alternate || flag_mri))
406 || (flag_macro_alternate && in->ptr[idx] == '\''))
408 if (flag_macro_alternate && ! macro_strip_at && in->ptr[idx] != '<')
410 /* Keep the quotes. */
411 sb_add_char (out, '"');
412 idx = getstring (idx, in, out);
413 sb_add_char (out, '"');
415 else
417 idx = getstring (idx, in, out);
420 else
422 char *br_buf = XNEWVEC (char, 1);
423 char *in_br = br_buf;
425 *in_br = '\0';
426 while (idx < in->len
427 && (*in_br
428 || (in->ptr[idx] != ' '
429 && in->ptr[idx] != '\t'))
430 && in->ptr[idx] != ','
431 && (in->ptr[idx] != '<'
432 || (! flag_macro_alternate && ! flag_mri)))
434 char tchar = in->ptr[idx];
436 switch (tchar)
438 case '"':
439 case '\'':
440 sb_add_char (out, in->ptr[idx++]);
441 while (idx < in->len
442 && in->ptr[idx] != tchar)
443 sb_add_char (out, in->ptr[idx++]);
444 if (idx == in->len)
446 free (br_buf);
447 return idx;
449 break;
450 case '(':
451 case '[':
452 if (in_br > br_buf)
453 --in_br;
454 else
456 br_buf = XNEWVEC (char, strlen (in_br) + 2);
457 strcpy (br_buf + 1, in_br);
458 free (in_br);
459 in_br = br_buf;
461 *in_br = tchar;
462 break;
463 case ')':
464 if (*in_br == '(')
465 ++in_br;
466 break;
467 case ']':
468 if (*in_br == '[')
469 ++in_br;
470 break;
472 sb_add_char (out, tchar);
473 ++idx;
475 free (br_buf);
479 return idx;
482 /* Allocate a new formal. */
484 static formal_entry *
485 new_formal (void)
487 formal_entry *formal;
489 formal = XNEW (formal_entry);
491 sb_new (&formal->name);
492 sb_new (&formal->def);
493 sb_new (&formal->actual);
494 formal->next = NULL;
495 formal->type = FORMAL_OPTIONAL;
496 return formal;
499 /* Free a formal. */
501 static void
502 del_formal (formal_entry *formal)
504 sb_kill (&formal->actual);
505 sb_kill (&formal->def);
506 sb_kill (&formal->name);
507 free (formal);
510 /* Pick up the formal parameters of a macro definition. */
512 static size_t
513 do_formals (macro_entry *macro, size_t idx, sb *in)
515 formal_entry **p = &macro->formals;
516 const char *name;
518 idx = sb_skip_white (idx, in);
519 while (idx < in->len)
521 formal_entry *formal = new_formal ();
522 size_t cidx;
524 idx = get_token (idx, in, &formal->name);
525 if (formal->name.len == 0)
527 if (macro->formal_count)
528 --idx;
529 del_formal (formal); /* 'formal' goes out of scope. */
530 break;
532 idx = sb_skip_white (idx, in);
533 /* This is a formal. */
534 name = sb_terminate (&formal->name);
535 if (! flag_mri
536 && idx < in->len
537 && in->ptr[idx] == ':'
538 && (! is_name_beginner (':')
539 || idx + 1 >= in->len
540 || ! is_part_of_name (in->ptr[idx + 1])))
542 /* Got a qualifier. */
543 sb qual;
545 sb_new (&qual);
546 idx = get_token (sb_skip_white (idx + 1, in), in, &qual);
547 sb_terminate (&qual);
548 if (qual.len == 0)
549 as_bad_where (macro->file,
550 macro->line,
551 _("Missing parameter qualifier for `%s' in macro `%s'"),
552 name,
553 macro->name);
554 else if (strcmp (qual.ptr, "req") == 0)
555 formal->type = FORMAL_REQUIRED;
556 else if (strcmp (qual.ptr, "vararg") == 0)
557 formal->type = FORMAL_VARARG;
558 else
559 as_bad_where (macro->file,
560 macro->line,
561 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
562 qual.ptr,
563 name,
564 macro->name);
565 sb_kill (&qual);
566 idx = sb_skip_white (idx, in);
568 if (idx < in->len && in->ptr[idx] == '=')
570 /* Got a default. */
571 idx = get_any_string (idx + 1, in, &formal->def);
572 idx = sb_skip_white (idx, in);
573 if (formal->type == FORMAL_REQUIRED)
575 sb_reset (&formal->def);
576 as_warn_where (macro->file,
577 macro->line,
578 _("Pointless default value for required parameter `%s' in macro `%s'"),
579 name,
580 macro->name);
584 /* Add to macro's hash table. */
585 if (str_hash_insert (macro->formal_hash, name, formal, 0) != NULL)
587 as_bad_where (macro->file, macro->line,
588 _("A parameter named `%s' "
589 "already exists for macro `%s'"),
590 name, macro->name);
593 formal->index = macro->formal_count++;
594 *p = formal;
595 p = &formal->next;
596 if (formal->type == FORMAL_VARARG)
597 break;
598 cidx = idx;
599 idx = sb_skip_comma (idx, in);
600 if (idx != cidx && idx >= in->len)
602 idx = cidx;
603 break;
607 if (flag_mri)
609 formal_entry *formal = new_formal ();
611 /* Add a special NARG formal, which macro_expand will set to the
612 number of arguments. */
613 /* The same MRI assemblers which treat '@' characters also use
614 the name $NARG. At least until we find an exception. */
615 if (macro_strip_at)
616 name = "$NARG";
617 else
618 name = "NARG";
620 sb_add_string (&formal->name, name);
622 /* Add to macro's hash table. */
623 if (str_hash_insert (macro->formal_hash, name, formal, 0) != NULL)
625 as_bad_where (macro->file, macro->line,
626 _("Reserved word `%s' used as parameter in macro `%s'"),
627 name, macro->name);
630 formal->index = NARG_INDEX;
631 *p = formal;
634 return idx;
637 /* Free the memory allocated to a macro. */
639 static void
640 free_macro (macro_entry *macro)
642 formal_entry *formal;
644 for (formal = macro->formals; formal; )
646 formal_entry *f;
648 f = formal;
649 formal = formal->next;
650 del_formal (f);
652 htab_delete (macro->formal_hash);
653 sb_kill (&macro->sub);
654 free ((char *) macro->name);
655 free (macro);
658 /* Define a new macro. */
660 macro_entry *
661 define_macro (sb *in, sb *label, size_t (*get_line) (sb *))
663 macro_entry *macro;
664 sb name;
665 size_t idx;
666 const char *error = NULL;
668 macro = XNEW (macro_entry);
669 sb_new (&macro->sub);
670 sb_new (&name);
671 macro->file = as_where (&macro->line);
673 macro->formal_count = 0;
674 macro->formals = 0;
675 macro->formal_hash = str_htab_create ();
676 macro->count = 0;
678 idx = sb_skip_white (0, in);
679 if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
680 error = _("unexpected end of file in macro `%s' definition");
681 if (label != NULL && label->len != 0)
683 sb_add_sb (&name, label);
684 macro->name = sb_terminate (&name);
685 if (idx < in->len && in->ptr[idx] == '(')
687 /* It's the label: MACRO (formals,...) sort */
688 idx = do_formals (macro, idx + 1, in);
689 if (idx < in->len && in->ptr[idx] == ')')
690 idx = sb_skip_white (idx + 1, in);
691 else if (!error)
692 error = _("missing `)' after formals in macro definition `%s'");
694 else
696 /* It's the label: MACRO formals,... sort */
697 idx = do_formals (macro, idx, in);
700 else
702 size_t cidx;
704 idx = get_token (idx, in, &name);
705 macro->name = sb_terminate (&name);
706 if (name.len == 0)
707 error = _("Missing macro name");
708 cidx = sb_skip_white (idx, in);
709 idx = sb_skip_comma (cidx, in);
710 if (idx == cidx || idx < in->len)
711 idx = do_formals (macro, idx, in);
712 else
713 idx = cidx;
715 if (!error && idx < in->len)
716 error = _("Bad parameter list for macro `%s'");
718 /* And stick it in the macro hash table. */
719 for (idx = 0; idx < name.len; idx++)
720 name.ptr[idx] = TOLOWER (name.ptr[idx]);
721 if (!error)
723 if (str_hash_insert (macro_hash, macro->name, macro, 0) != NULL)
724 error = _("Macro `%s' was already defined");
727 if (!error)
728 macro_defined = 1;
729 else
731 as_bad_where (macro->file, macro->line, error, macro->name);
732 free_macro (macro);
733 macro = NULL;
736 return macro;
739 /* Scan a token, and then skip KIND. */
741 static size_t
742 get_apost_token (size_t idx, sb *in, sb *name, int kind)
744 idx = get_token (idx, in, name);
745 if (idx < in->len
746 && in->ptr[idx] == kind
747 && (! flag_mri || macro_strip_at)
748 && (! macro_strip_at || kind == '@'))
749 idx++;
750 return idx;
753 /* Substitute the actual value for a formal parameter. */
755 static size_t
756 sub_actual (size_t start, sb *in, sb *t, struct htab *formal_hash,
757 int kind, sb *out, int copyifnotthere)
759 size_t src;
760 formal_entry *ptr;
762 src = get_apost_token (start, in, t, kind);
763 /* See if it's in the macro's hash table, unless this is
764 macro_strip_at and kind is '@' and the token did not end in '@'. */
765 if (macro_strip_at
766 && kind == '@'
767 && (src == start || in->ptr[src - 1] != '@'))
768 ptr = NULL;
769 else
770 ptr = str_hash_find (formal_hash, sb_terminate (t));
771 if (ptr)
773 if (ptr->actual.len)
775 sb_add_sb (out, &ptr->actual);
777 else
779 sb_add_sb (out, &ptr->def);
782 else if (kind == '&')
784 /* Doing this permits people to use & in macro bodies. */
785 sb_add_char (out, '&');
786 sb_add_sb (out, t);
787 if (src != start && in->ptr[src - 1] == '&')
788 sb_add_char (out, '&');
790 else if (copyifnotthere)
792 sb_add_sb (out, t);
794 else
796 sb_add_char (out, '\\');
797 sb_add_sb (out, t);
799 return src;
802 /* Expand the body of a macro. */
804 static const char *
805 macro_expand_body (sb *in, sb *out, formal_entry *formals,
806 struct htab *formal_hash, const macro_entry *macro,
807 unsigned int instance)
809 sb t;
810 size_t src = 0;
811 int inquote = 0, macro_line = 0;
812 formal_entry *loclist = NULL;
813 const char *err = NULL;
815 sb_new (&t);
817 while (src < in->len && !err)
819 if (in->ptr[src] == '&')
821 sb_reset (&t);
822 if (flag_mri)
824 if (src + 1 < in->len && in->ptr[src + 1] == '&')
825 src = sub_actual (src + 2, in, &t, formal_hash, '\'', out, 1);
826 else
827 sb_add_char (out, in->ptr[src++]);
829 else
831 /* Permit macro parameter substitution delineated with
832 an '&' prefix and optional '&' suffix. */
833 src = sub_actual (src + 1, in, &t, formal_hash, '&', out, 0);
836 else if (in->ptr[src] == '\\')
838 src++;
839 if (src < in->len && in->ptr[src] == '(')
841 /* Sub in till the next ')' literally. */
842 src++;
843 while (src < in->len && in->ptr[src] != ')')
845 sb_add_char (out, in->ptr[src++]);
847 if (src < in->len)
848 src++;
849 else if (!macro)
850 err = _("missing `)'");
851 else
852 as_bad_where (macro->file, macro->line + macro_line, _("missing `)'"));
854 else if (src < in->len && in->ptr[src] == '@')
856 /* Sub in the total macro invocation number. */
858 char buffer[12];
859 src++;
860 sprintf (buffer, "%u", macro_number);
861 sb_add_string (out, buffer);
863 else if (src < in->len && in->ptr[src] == '+')
865 /* Sub in the current macro invocation number. */
867 char buffer[12];
868 src++;
869 sprintf (buffer, "%d", instance);
870 sb_add_string (out, buffer);
872 else if (src < in->len && in->ptr[src] == '&')
874 /* This is a preprocessor variable name, we don't do them
875 here. */
876 sb_add_char (out, '\\');
877 sb_add_char (out, '&');
878 src++;
880 else if (flag_mri && src < in->len && ISALNUM (in->ptr[src]))
882 int ind;
883 formal_entry *f;
885 if (ISDIGIT (in->ptr[src]))
886 ind = in->ptr[src] - '0';
887 else if (ISUPPER (in->ptr[src]))
888 ind = in->ptr[src] - 'A' + 10;
889 else
890 ind = in->ptr[src] - 'a' + 10;
891 ++src;
892 for (f = formals; f != NULL; f = f->next)
894 if (f->index == ind - 1)
896 if (f->actual.len != 0)
897 sb_add_sb (out, &f->actual);
898 else
899 sb_add_sb (out, &f->def);
900 break;
904 else
906 sb_reset (&t);
907 src = sub_actual (src, in, &t, formal_hash, '\'', out, 0);
910 else if ((flag_macro_alternate || flag_mri)
911 && is_name_beginner (in->ptr[src])
912 && (! inquote
913 || ! macro_strip_at
914 || (src > 0 && in->ptr[src - 1] == '@')))
916 if (! macro
917 || src + 5 >= in->len
918 || strncasecmp (in->ptr + src, "LOCAL", 5) != 0
919 || ! ISWHITE (in->ptr[src + 5])
920 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
921 || inquote)
923 sb_reset (&t);
924 src = sub_actual (src, in, &t, formal_hash,
925 (macro_strip_at && inquote) ? '@' : '\'',
926 out, 1);
928 else
930 src = sb_skip_white (src + 5, in);
931 while (in->ptr[src] != '\n')
933 const char *name;
934 formal_entry *f = new_formal ();
936 src = get_token (src, in, &f->name);
937 name = sb_terminate (&f->name);
938 if (str_hash_insert (formal_hash, name, f, 0) != NULL)
940 as_bad_where (macro->file, macro->line + macro_line,
941 _("`%s' was already used as parameter "
942 "(or another local) name"), name);
943 del_formal (f);
945 else
947 static int loccnt;
948 char buf[20];
950 f->index = LOCAL_INDEX;
951 f->next = loclist;
952 loclist = f;
954 sprintf (buf, IS_ELF ? ".LL%04x" : "LL%04x", ++loccnt);
955 sb_add_string (&f->actual, buf);
958 src = sb_skip_comma (src, in);
962 else if (in->ptr[src] == '"'
963 || (flag_mri && in->ptr[src] == '\''))
965 inquote = !inquote;
966 sb_add_char (out, in->ptr[src++]);
968 else if (in->ptr[src] == '@' && macro_strip_at)
970 ++src;
971 if (src < in->len
972 && in->ptr[src] == '@')
974 sb_add_char (out, '@');
975 ++src;
978 else if (flag_mri
979 && in->ptr[src] == '='
980 && src + 1 < in->len
981 && in->ptr[src + 1] == '=')
983 formal_entry *ptr;
985 sb_reset (&t);
986 src = get_token (src + 2, in, &t);
987 ptr = str_hash_find (formal_hash, sb_terminate (&t));
988 if (ptr == NULL)
990 /* FIXME: We should really return a warning string here,
991 but we can't, because the == might be in the MRI
992 comment field, and, since the nature of the MRI
993 comment field depends upon the exact instruction
994 being used, we don't have enough information here to
995 figure out whether it is or not. Instead, we leave
996 the == in place, which should cause a syntax error if
997 it is not in a comment. */
998 sb_add_char (out, '=');
999 sb_add_char (out, '=');
1000 sb_add_sb (out, &t);
1002 else
1004 if (ptr->actual.len)
1006 sb_add_string (out, "-1");
1008 else
1010 sb_add_char (out, '0');
1014 else
1016 if (in->ptr[src] == '\n')
1017 ++macro_line;
1018 sb_add_char (out, in->ptr[src++]);
1022 sb_kill (&t);
1024 while (loclist != NULL)
1026 formal_entry *f;
1027 const char *name;
1029 f = loclist->next;
1030 name = sb_terminate (&loclist->name);
1031 str_hash_delete (formal_hash, name);
1032 del_formal (loclist);
1033 loclist = f;
1036 if (!err && (out->len == 0 || out->ptr[out->len - 1] != '\n'))
1037 sb_add_char (out, '\n');
1038 return err;
1041 /* Assign values to the formal parameters of a macro, and expand the
1042 body. */
1044 static const char *
1045 macro_expand (size_t idx, sb *in, macro_entry *m, sb *out)
1047 sb t;
1048 formal_entry *ptr;
1049 formal_entry *f;
1050 int is_keyword = 0;
1051 int narg = 0;
1052 const char *err = NULL;
1054 sb_new (&t);
1056 /* Reset any old value the actuals may have. */
1057 for (f = m->formals; f; f = f->next)
1058 sb_reset (&f->actual);
1059 f = m->formals;
1060 while (f != NULL && f->index < 0)
1061 f = f->next;
1063 if (flag_mri)
1065 /* The macro may be called with an optional qualifier, which may
1066 be referred to in the macro body as \0. */
1067 if (idx < in->len && in->ptr[idx] == '.')
1069 /* The Microtec assembler ignores this if followed by a white space.
1070 (Macro invocation with empty extension) */
1071 idx++;
1072 if ( idx < in->len
1073 && in->ptr[idx] != ' '
1074 && in->ptr[idx] != '\t')
1076 formal_entry *n = new_formal ();
1078 n->index = QUAL_INDEX;
1080 n->next = m->formals;
1081 m->formals = n;
1083 idx = get_any_string (idx, in, &n->actual);
1088 /* Peel off the actuals and store them away in the hash tables' actuals. */
1089 idx = sb_skip_white (idx, in);
1090 while (idx < in->len)
1092 size_t scan;
1094 /* Look and see if it's a positional or keyword arg. */
1095 scan = idx;
1096 while (scan < in->len
1097 && !ISSEP (in->ptr[scan])
1098 && !(flag_mri && in->ptr[scan] == '\'')
1099 && (!flag_macro_alternate && in->ptr[scan] != '='))
1100 scan++;
1101 if (scan < in->len && !flag_macro_alternate && in->ptr[scan] == '=')
1103 is_keyword = 1;
1105 /* It's OK to go from positional to keyword. */
1107 /* This is a keyword arg, fetch the formal name and
1108 then the actual stuff. */
1109 sb_reset (&t);
1110 idx = get_token (idx, in, &t);
1111 if (idx >= in->len || in->ptr[idx] != '=')
1113 err = _("confusion in formal parameters");
1114 break;
1117 /* Lookup the formal in the macro's list. */
1118 ptr = str_hash_find (m->formal_hash, sb_terminate (&t));
1119 if (!ptr)
1121 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1122 t.ptr,
1123 m->name);
1124 sb_reset (&t);
1125 idx = get_any_string (idx + 1, in, &t);
1127 else
1129 /* Insert this value into the right place. */
1130 if (ptr->actual.len)
1132 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1133 ptr->name.ptr,
1134 m->name);
1135 sb_reset (&ptr->actual);
1137 idx = get_any_string (idx + 1, in, &ptr->actual);
1138 if (ptr->actual.len > 0)
1139 ++narg;
1142 else
1144 if (is_keyword)
1146 err = _("can't mix positional and keyword arguments");
1147 break;
1150 if (!f)
1152 formal_entry **pf;
1153 int c;
1155 if (!flag_mri)
1157 err = _("too many positional arguments");
1158 break;
1161 f = new_formal ();
1163 c = -1;
1164 for (pf = &m->formals; *pf != NULL; pf = &(*pf)->next)
1165 if ((*pf)->index >= c)
1166 c = (*pf)->index + 1;
1167 if (c == -1)
1168 c = 0;
1169 *pf = f;
1170 f->index = c;
1173 if (f->type != FORMAL_VARARG)
1174 idx = get_any_string (idx, in, &f->actual);
1175 else if (idx < in->len)
1177 sb_add_buffer (&f->actual, in->ptr + idx, in->len - idx);
1178 idx = in->len;
1180 if (f->actual.len > 0)
1181 ++narg;
1184 f = f->next;
1186 while (f != NULL && f->index < 0);
1189 if (! flag_mri)
1190 idx = sb_skip_comma (idx, in);
1191 else
1193 if (idx < in->len && in->ptr[idx] == ',')
1194 ++idx;
1195 if (idx < in->len && ISWHITE (in->ptr[idx]))
1196 break;
1200 if (! err)
1202 for (ptr = m->formals; ptr; ptr = ptr->next)
1204 if (ptr->type == FORMAL_REQUIRED && ptr->actual.len == 0)
1205 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1206 ptr->name.ptr,
1207 m->name);
1210 if (flag_mri)
1212 ptr = str_hash_find (m->formal_hash,
1213 macro_strip_at ? "$NARG" : "NARG");
1214 if (ptr)
1216 char buffer[20];
1217 sprintf (buffer, "%d", narg);
1218 sb_add_string (&ptr->actual, buffer);
1222 err = macro_expand_body (&m->sub, out, m->formals, m->formal_hash, m,
1223 m->count);
1226 /* Discard any unnamed formal arguments. */
1227 if (flag_mri)
1229 formal_entry **pf;
1231 pf = &m->formals;
1232 while (*pf != NULL)
1234 if ((*pf)->name.len != 0)
1235 pf = &(*pf)->next;
1236 else
1238 f = (*pf)->next;
1239 del_formal (*pf);
1240 *pf = f;
1245 sb_kill (&t);
1246 if (!err)
1248 macro_number++;
1249 m->count++;
1252 return err;
1255 /* Check for a macro. If one is found, put the expansion into
1256 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1259 check_macro (const char *line, sb *expand,
1260 const char **error, macro_entry **info)
1262 const char *s;
1263 char *copy, *cls;
1264 macro_entry *macro;
1265 sb line_sb;
1267 if (! is_name_beginner (*line)
1268 && (! flag_mri || *line != '.'))
1269 return 0;
1271 s = line + 1;
1272 while (is_part_of_name (*s))
1273 ++s;
1274 if (is_name_ender (*s))
1275 ++s;
1277 copy = xmemdup0 (line, s - line);
1278 for (cls = copy; *cls != '\0'; cls ++)
1279 *cls = TOLOWER (*cls);
1281 macro = str_hash_find (macro_hash, copy);
1282 free (copy);
1284 if (macro == NULL)
1285 return 0;
1287 /* Wrap the line up in an sb. */
1288 sb_new (&line_sb);
1289 while (*s != '\0' && *s != '\n' && *s != '\r')
1290 sb_add_char (&line_sb, *s++);
1292 sb_new (expand);
1293 *error = macro_expand (0, &line_sb, macro, expand);
1295 sb_kill (&line_sb);
1297 /* Export the macro information if requested. */
1298 if (info)
1299 *info = macro;
1301 return 1;
1304 /* Delete a macro. */
1306 void
1307 delete_macro (const char *name)
1309 char *copy;
1310 size_t i, len;
1311 macro_entry *macro;
1313 len = strlen (name);
1314 copy = XNEWVEC (char, len + 1);
1315 for (i = 0; i < len; ++i)
1316 copy[i] = TOLOWER (name[i]);
1317 copy[i] = '\0';
1319 macro = str_hash_find (macro_hash, copy);
1320 if (macro != NULL)
1321 str_hash_delete (macro_hash, copy);
1322 else
1323 as_warn (_("Attempt to purge non-existing macro `%s'"), copy);
1324 free (copy);
1327 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1328 combined macro definition and execution. This returns NULL on
1329 success, or an error message otherwise. */
1331 const char *
1332 expand_irp (int irpc, size_t idx, sb *in, sb *out, size_t (*get_line) (sb *))
1334 sb sub;
1335 formal_entry f;
1336 struct htab *h;
1337 const char *err = NULL;
1339 idx = sb_skip_white (idx, in);
1341 sb_new (&sub);
1342 if (! buffer_and_nest (NULL, "ENDR", &sub, get_line))
1344 err = _("unexpected end of file in irp or irpc");
1345 goto out2;
1348 sb_new (&f.name);
1349 sb_new (&f.def);
1350 sb_new (&f.actual);
1352 idx = get_token (idx, in, &f.name);
1353 if (f.name.len == 0)
1355 err = _("missing model parameter");
1356 goto out1;
1359 h = str_htab_create ();
1361 str_hash_insert (h, sb_terminate (&f.name), &f, 0);
1363 f.index = 1;
1364 f.next = NULL;
1365 f.type = FORMAL_OPTIONAL;
1367 sb_reset (out);
1369 idx = sb_skip_comma (idx, in);
1370 if (idx >= in->len)
1372 /* Expand once with a null string. */
1373 err = macro_expand_body (&sub, out, &f, h, NULL, 0);
1375 else
1377 bool in_quotes = false;
1378 unsigned int instance = 0;
1380 while (idx < in->len)
1382 if (!irpc)
1383 idx = get_any_string (idx, in, &f.actual);
1384 else
1386 if (in->ptr[idx] == '"')
1388 in_quotes = ! in_quotes;
1389 ++idx;
1391 if (! in_quotes)
1393 idx = sb_skip_white (idx, in);
1394 if (idx >= in->len)
1395 break;
1397 continue;
1399 sb_reset (&f.actual);
1400 sb_add_char (&f.actual, in->ptr[idx]);
1401 ++idx;
1404 err = macro_expand_body (&sub, out, &f, h, NULL, instance);
1405 ++instance;
1406 if (err != NULL)
1407 break;
1408 if (!irpc)
1409 idx = sb_skip_comma (idx, in);
1410 else if (! in_quotes)
1411 idx = sb_skip_white (idx, in);
1415 htab_delete (h);
1416 out1:
1417 sb_kill (&f.actual);
1418 sb_kill (&f.def);
1419 sb_kill (&f.name);
1420 out2:
1421 sb_kill (&sub);
1423 return err;