Automatic date update in version.in
[binutils-gdb.git] / gas / macro.c
blob3036e6a58669dadf9c149cbb4c2e5023d129daa9
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;
223 break;
227 /* PR gas/16908
228 Apply .linefile directives that appear within the macro, alongside
229 keeping them for later expansion of the macro. */
230 if (from != NULL && strcasecmp (from, "MACRO") == 0
231 && len >= 8 && strncasecmp (ptr->ptr + i, "linefile", 8) == 0)
233 sb_add_char (ptr, more);
234 temp_ilp (sb_terminate (ptr) + i + 8);
235 s_linefile (0);
236 restore_ilp ();
237 line_start = ptr->len;
238 more = get_line (ptr);
239 continue;
243 /* Add the original end-of-line char to the end and keep running. */
244 sb_add_char (ptr, more);
245 line_start = ptr->len;
246 more = get_line (ptr);
249 /* Return 1 on success, 0 on unexpected EOF. */
250 return depth == 0;
253 /* Pick up a token. */
255 static size_t
256 get_token (size_t idx, sb *in, sb *name)
258 if (idx < in->len
259 && is_name_beginner (in->ptr[idx]))
261 sb_add_char (name, in->ptr[idx++]);
262 while (idx < in->len
263 && is_part_of_name (in->ptr[idx]))
265 sb_add_char (name, in->ptr[idx++]);
267 if (idx < in->len
268 && is_name_ender (in->ptr[idx]))
270 sb_add_char (name, in->ptr[idx++]);
273 /* Ignore trailing &. */
274 if (flag_macro_alternate && idx < in->len && in->ptr[idx] == '&')
275 idx++;
276 return idx;
279 /* Pick up a string. */
281 static size_t
282 getstring (size_t idx, sb *in, sb *acc)
284 while (idx < in->len
285 && (in->ptr[idx] == '"'
286 || (in->ptr[idx] == '<' && (flag_macro_alternate || flag_mri))
287 || (in->ptr[idx] == '\'' && flag_macro_alternate)))
289 if (in->ptr[idx] == '<')
291 int nest = 0;
292 idx++;
293 while (idx < in->len
294 && (in->ptr[idx] != '>' || nest))
296 if (in->ptr[idx] == '!')
298 idx++;
299 sb_add_char (acc, in->ptr[idx++]);
301 else
303 if (in->ptr[idx] == '>')
304 nest--;
305 if (in->ptr[idx] == '<')
306 nest++;
307 sb_add_char (acc, in->ptr[idx++]);
310 idx++;
312 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
314 char tchar = in->ptr[idx];
315 int escaped = 0;
317 idx++;
319 while (idx < in->len)
321 if (in->ptr[idx - 1] == '\\')
322 escaped ^= 1;
323 else
324 escaped = 0;
326 if (flag_macro_alternate && in->ptr[idx] == '!')
328 idx ++;
330 sb_add_char (acc, in->ptr[idx]);
332 idx ++;
334 else if (escaped && in->ptr[idx] == tchar)
336 sb_add_char (acc, tchar);
337 idx ++;
339 else
341 if (in->ptr[idx] == tchar)
343 idx ++;
345 if (idx >= in->len || in->ptr[idx] != tchar)
346 break;
349 sb_add_char (acc, in->ptr[idx]);
350 idx ++;
356 return idx;
359 /* Fetch string from the input stream,
360 rules:
361 'Bxyx<whitespace> -> return 'Bxyza
362 %<expr> -> return string of decimal value of <expr>
363 "string" -> return string
364 (string) -> return (string-including-whitespaces)
365 xyx<whitespace> -> return xyz. */
367 static size_t
368 get_any_string (size_t idx, sb *in, sb *out)
370 sb_reset (out);
371 idx = sb_skip_white (idx, in);
373 if (idx < in->len)
375 if (in->len > idx + 2 && in->ptr[idx + 1] == '\'' && ISBASE (in->ptr[idx]))
377 while (idx < in->len && !ISSEP (in->ptr[idx]))
378 sb_add_char (out, in->ptr[idx++]);
380 else if (in->ptr[idx] == '%' && flag_macro_alternate)
382 /* Turn the following expression into a string. */
383 expressionS ex;
384 char buf[64];
386 sb_terminate (in);
388 temp_ilp (in->ptr + idx + 1);
389 expression_and_evaluate (&ex);
390 idx = input_line_pointer - in->ptr;
391 restore_ilp ();
393 if (ex.X_op != O_constant)
394 as_bad (_("%% operator needs absolute expression"));
396 sprintf (buf, "%" PRId64, (int64_t) ex.X_add_number);
397 sb_add_string (out, buf);
399 else if (in->ptr[idx] == '"'
400 || (in->ptr[idx] == '<' && (flag_macro_alternate || flag_mri))
401 || (flag_macro_alternate && in->ptr[idx] == '\''))
403 if (flag_macro_alternate && ! macro_strip_at && in->ptr[idx] != '<')
405 /* Keep the quotes. */
406 sb_add_char (out, '"');
407 idx = getstring (idx, in, out);
408 sb_add_char (out, '"');
410 else
412 idx = getstring (idx, in, out);
415 else
417 char *br_buf = XNEWVEC (char, 1);
418 char *in_br = br_buf;
420 *in_br = '\0';
421 while (idx < in->len
422 && (*in_br
423 || (in->ptr[idx] != ' '
424 && in->ptr[idx] != '\t'))
425 && in->ptr[idx] != ','
426 && (in->ptr[idx] != '<'
427 || (! flag_macro_alternate && ! flag_mri)))
429 char tchar = in->ptr[idx];
431 switch (tchar)
433 case '"':
434 case '\'':
435 sb_add_char (out, in->ptr[idx++]);
436 while (idx < in->len
437 && in->ptr[idx] != tchar)
438 sb_add_char (out, in->ptr[idx++]);
439 if (idx == in->len)
441 free (br_buf);
442 return idx;
444 break;
445 case '(':
446 case '[':
447 if (in_br > br_buf)
448 --in_br;
449 else
451 br_buf = XNEWVEC (char, strlen (in_br) + 2);
452 strcpy (br_buf + 1, in_br);
453 free (in_br);
454 in_br = br_buf;
456 *in_br = tchar;
457 break;
458 case ')':
459 if (*in_br == '(')
460 ++in_br;
461 break;
462 case ']':
463 if (*in_br == '[')
464 ++in_br;
465 break;
467 sb_add_char (out, tchar);
468 ++idx;
470 free (br_buf);
474 return idx;
477 /* Allocate a new formal. */
479 static formal_entry *
480 new_formal (void)
482 formal_entry *formal;
484 formal = XNEW (formal_entry);
486 sb_new (&formal->name);
487 sb_new (&formal->def);
488 sb_new (&formal->actual);
489 formal->next = NULL;
490 formal->type = FORMAL_OPTIONAL;
491 return formal;
494 /* Free a formal. */
496 static void
497 del_formal (formal_entry *formal)
499 sb_kill (&formal->actual);
500 sb_kill (&formal->def);
501 sb_kill (&formal->name);
502 free (formal);
505 /* Pick up the formal parameters of a macro definition. */
507 static size_t
508 do_formals (macro_entry *macro, size_t idx, sb *in)
510 formal_entry **p = &macro->formals;
511 const char *name;
513 idx = sb_skip_white (idx, in);
514 while (idx < in->len)
516 formal_entry *formal = new_formal ();
517 size_t cidx;
519 idx = get_token (idx, in, &formal->name);
520 if (formal->name.len == 0)
522 if (macro->formal_count)
523 --idx;
524 del_formal (formal); /* 'formal' goes out of scope. */
525 break;
527 idx = sb_skip_white (idx, in);
528 /* This is a formal. */
529 name = sb_terminate (&formal->name);
530 if (! flag_mri
531 && idx < in->len
532 && in->ptr[idx] == ':'
533 && (! is_name_beginner (':')
534 || idx + 1 >= in->len
535 || ! is_part_of_name (in->ptr[idx + 1])))
537 /* Got a qualifier. */
538 sb qual;
540 sb_new (&qual);
541 idx = get_token (sb_skip_white (idx + 1, in), in, &qual);
542 sb_terminate (&qual);
543 if (qual.len == 0)
544 as_bad_where (macro->file,
545 macro->line,
546 _("Missing parameter qualifier for `%s' in macro `%s'"),
547 name,
548 macro->name);
549 else if (strcmp (qual.ptr, "req") == 0)
550 formal->type = FORMAL_REQUIRED;
551 else if (strcmp (qual.ptr, "vararg") == 0)
552 formal->type = FORMAL_VARARG;
553 else
554 as_bad_where (macro->file,
555 macro->line,
556 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
557 qual.ptr,
558 name,
559 macro->name);
560 sb_kill (&qual);
561 idx = sb_skip_white (idx, in);
563 if (idx < in->len && in->ptr[idx] == '=')
565 /* Got a default. */
566 idx = get_any_string (idx + 1, in, &formal->def);
567 idx = sb_skip_white (idx, in);
568 if (formal->type == FORMAL_REQUIRED)
570 sb_reset (&formal->def);
571 as_warn_where (macro->file,
572 macro->line,
573 _("Pointless default value for required parameter `%s' in macro `%s'"),
574 name,
575 macro->name);
579 /* Add to macro's hash table. */
580 if (str_hash_insert (macro->formal_hash, name, formal, 0) != NULL)
582 as_bad_where (macro->file, macro->line,
583 _("A parameter named `%s' "
584 "already exists for macro `%s'"),
585 name, macro->name);
588 formal->index = macro->formal_count++;
589 *p = formal;
590 p = &formal->next;
591 if (formal->type == FORMAL_VARARG)
592 break;
593 cidx = idx;
594 idx = sb_skip_comma (idx, in);
595 if (idx != cidx && idx >= in->len)
597 idx = cidx;
598 break;
602 if (flag_mri)
604 formal_entry *formal = new_formal ();
606 /* Add a special NARG formal, which macro_expand will set to the
607 number of arguments. */
608 /* The same MRI assemblers which treat '@' characters also use
609 the name $NARG. At least until we find an exception. */
610 if (macro_strip_at)
611 name = "$NARG";
612 else
613 name = "NARG";
615 sb_add_string (&formal->name, name);
617 /* Add to macro's hash table. */
618 if (str_hash_insert (macro->formal_hash, name, formal, 0) != NULL)
620 as_bad_where (macro->file, macro->line,
621 _("Reserved word `%s' used as parameter in macro `%s'"),
622 name, macro->name);
625 formal->index = NARG_INDEX;
626 *p = formal;
629 return idx;
632 /* Free the memory allocated to a macro. */
634 static void
635 free_macro (macro_entry *macro)
637 formal_entry *formal;
639 for (formal = macro->formals; formal; )
641 formal_entry *f;
643 f = formal;
644 formal = formal->next;
645 del_formal (f);
647 htab_delete (macro->formal_hash);
648 sb_kill (&macro->sub);
649 free ((char *) macro->name);
650 free (macro);
653 /* Define a new macro. */
655 macro_entry *
656 define_macro (sb *in, sb *label, size_t (*get_line) (sb *))
658 macro_entry *macro;
659 sb name;
660 size_t idx;
661 const char *error = NULL;
663 macro = XNEW (macro_entry);
664 sb_new (&macro->sub);
665 sb_new (&name);
666 macro->file = as_where (&macro->line);
668 macro->formal_count = 0;
669 macro->formals = 0;
670 macro->formal_hash = str_htab_create ();
671 macro->count = 0;
673 idx = sb_skip_white (0, in);
674 if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
675 error = _("unexpected end of file in macro `%s' definition");
676 if (label != NULL && label->len != 0)
678 sb_add_sb (&name, label);
679 macro->name = sb_terminate (&name);
680 if (idx < in->len && in->ptr[idx] == '(')
682 /* It's the label: MACRO (formals,...) sort */
683 idx = do_formals (macro, idx + 1, in);
684 if (idx < in->len && in->ptr[idx] == ')')
685 idx = sb_skip_white (idx + 1, in);
686 else if (!error)
687 error = _("missing `)' after formals in macro definition `%s'");
689 else
691 /* It's the label: MACRO formals,... sort */
692 idx = do_formals (macro, idx, in);
695 else
697 size_t cidx;
699 idx = get_token (idx, in, &name);
700 macro->name = sb_terminate (&name);
701 if (name.len == 0)
702 error = _("Missing macro name");
703 cidx = sb_skip_white (idx, in);
704 idx = sb_skip_comma (cidx, in);
705 if (idx == cidx || idx < in->len)
706 idx = do_formals (macro, idx, in);
707 else
708 idx = cidx;
710 if (!error && idx < in->len)
711 error = _("Bad parameter list for macro `%s'");
713 /* And stick it in the macro hash table. */
714 for (idx = 0; idx < name.len; idx++)
715 name.ptr[idx] = TOLOWER (name.ptr[idx]);
716 if (!error)
718 if (str_hash_insert (macro_hash, macro->name, macro, 0) != NULL)
719 error = _("Macro `%s' was already defined");
722 if (!error)
723 macro_defined = 1;
724 else
726 as_bad_where (macro->file, macro->line, error, macro->name);
727 free_macro (macro);
728 macro = NULL;
731 return macro;
734 /* Scan a token, and then skip KIND. */
736 static size_t
737 get_apost_token (size_t idx, sb *in, sb *name, int kind)
739 idx = get_token (idx, in, name);
740 if (idx < in->len
741 && in->ptr[idx] == kind
742 && (! flag_mri || macro_strip_at)
743 && (! macro_strip_at || kind == '@'))
744 idx++;
745 return idx;
748 /* Substitute the actual value for a formal parameter. */
750 static size_t
751 sub_actual (size_t start, sb *in, sb *t, struct htab *formal_hash,
752 int kind, sb *out, int copyifnotthere)
754 size_t src;
755 formal_entry *ptr;
757 src = get_apost_token (start, in, t, kind);
758 /* See if it's in the macro's hash table, unless this is
759 macro_strip_at and kind is '@' and the token did not end in '@'. */
760 if (macro_strip_at
761 && kind == '@'
762 && (src == start || in->ptr[src - 1] != '@'))
763 ptr = NULL;
764 else
765 ptr = str_hash_find (formal_hash, sb_terminate (t));
766 if (ptr)
768 if (ptr->actual.len)
770 sb_add_sb (out, &ptr->actual);
772 else
774 sb_add_sb (out, &ptr->def);
777 else if (kind == '&')
779 /* Doing this permits people to use & in macro bodies. */
780 sb_add_char (out, '&');
781 sb_add_sb (out, t);
782 if (src != start && in->ptr[src - 1] == '&')
783 sb_add_char (out, '&');
785 else if (copyifnotthere)
787 sb_add_sb (out, t);
789 else
791 sb_add_char (out, '\\');
792 sb_add_sb (out, t);
794 return src;
797 /* Expand the body of a macro. */
799 static const char *
800 macro_expand_body (sb *in, sb *out, formal_entry *formals,
801 struct htab *formal_hash, const macro_entry *macro)
803 sb t;
804 size_t src = 0;
805 int inquote = 0, macro_line = 0;
806 formal_entry *loclist = NULL;
807 const char *err = NULL;
809 sb_new (&t);
811 while (src < in->len && !err)
813 if (in->ptr[src] == '&')
815 sb_reset (&t);
816 if (flag_mri)
818 if (src + 1 < in->len && in->ptr[src + 1] == '&')
819 src = sub_actual (src + 2, in, &t, formal_hash, '\'', out, 1);
820 else
821 sb_add_char (out, in->ptr[src++]);
823 else
825 /* Permit macro parameter substitution delineated with
826 an '&' prefix and optional '&' suffix. */
827 src = sub_actual (src + 1, in, &t, formal_hash, '&', out, 0);
830 else if (in->ptr[src] == '\\')
832 src++;
833 if (src < in->len && in->ptr[src] == '(')
835 /* Sub in till the next ')' literally. */
836 src++;
837 while (src < in->len && in->ptr[src] != ')')
839 sb_add_char (out, in->ptr[src++]);
841 if (src < in->len)
842 src++;
843 else if (!macro)
844 err = _("missing `)'");
845 else
846 as_bad_where (macro->file, macro->line + macro_line, _("missing `)'"));
848 else if (src < in->len && in->ptr[src] == '@')
850 /* Sub in the total macro invocation number. */
852 char buffer[12];
853 src++;
854 sprintf (buffer, "%u", macro_number);
855 sb_add_string (out, buffer);
857 else if (macro && src < in->len && in->ptr[src] == '+')
859 /* Sub in the current macro invocation number. */
861 char buffer[12];
862 src++;
863 sprintf (buffer, "%d", macro->count);
864 sb_add_string (out, buffer);
866 else if (src < in->len && in->ptr[src] == '&')
868 /* This is a preprocessor variable name, we don't do them
869 here. */
870 sb_add_char (out, '\\');
871 sb_add_char (out, '&');
872 src++;
874 else if (flag_mri && src < in->len && ISALNUM (in->ptr[src]))
876 int ind;
877 formal_entry *f;
879 if (ISDIGIT (in->ptr[src]))
880 ind = in->ptr[src] - '0';
881 else if (ISUPPER (in->ptr[src]))
882 ind = in->ptr[src] - 'A' + 10;
883 else
884 ind = in->ptr[src] - 'a' + 10;
885 ++src;
886 for (f = formals; f != NULL; f = f->next)
888 if (f->index == ind - 1)
890 if (f->actual.len != 0)
891 sb_add_sb (out, &f->actual);
892 else
893 sb_add_sb (out, &f->def);
894 break;
898 else
900 sb_reset (&t);
901 src = sub_actual (src, in, &t, formal_hash, '\'', out, 0);
904 else if ((flag_macro_alternate || flag_mri)
905 && is_name_beginner (in->ptr[src])
906 && (! inquote
907 || ! macro_strip_at
908 || (src > 0 && in->ptr[src - 1] == '@')))
910 if (! macro
911 || src + 5 >= in->len
912 || strncasecmp (in->ptr + src, "LOCAL", 5) != 0
913 || ! ISWHITE (in->ptr[src + 5])
914 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
915 || inquote)
917 sb_reset (&t);
918 src = sub_actual (src, in, &t, formal_hash,
919 (macro_strip_at && inquote) ? '@' : '\'',
920 out, 1);
922 else
924 src = sb_skip_white (src + 5, in);
925 while (in->ptr[src] != '\n')
927 const char *name;
928 formal_entry *f = new_formal ();
930 src = get_token (src, in, &f->name);
931 name = sb_terminate (&f->name);
932 if (str_hash_insert (formal_hash, name, f, 0) != NULL)
934 as_bad_where (macro->file, macro->line + macro_line,
935 _("`%s' was already used as parameter "
936 "(or another local) name"), name);
937 del_formal (f);
939 else
941 static int loccnt;
942 char buf[20];
944 f->index = LOCAL_INDEX;
945 f->next = loclist;
946 loclist = f;
948 sprintf (buf, IS_ELF ? ".LL%04x" : "LL%04x", ++loccnt);
949 sb_add_string (&f->actual, buf);
952 src = sb_skip_comma (src, in);
956 else if (in->ptr[src] == '"'
957 || (flag_mri && in->ptr[src] == '\''))
959 inquote = !inquote;
960 sb_add_char (out, in->ptr[src++]);
962 else if (in->ptr[src] == '@' && macro_strip_at)
964 ++src;
965 if (src < in->len
966 && in->ptr[src] == '@')
968 sb_add_char (out, '@');
969 ++src;
972 else if (flag_mri
973 && in->ptr[src] == '='
974 && src + 1 < in->len
975 && in->ptr[src + 1] == '=')
977 formal_entry *ptr;
979 sb_reset (&t);
980 src = get_token (src + 2, in, &t);
981 ptr = str_hash_find (formal_hash, sb_terminate (&t));
982 if (ptr == NULL)
984 /* FIXME: We should really return a warning string here,
985 but we can't, because the == might be in the MRI
986 comment field, and, since the nature of the MRI
987 comment field depends upon the exact instruction
988 being used, we don't have enough information here to
989 figure out whether it is or not. Instead, we leave
990 the == in place, which should cause a syntax error if
991 it is not in a comment. */
992 sb_add_char (out, '=');
993 sb_add_char (out, '=');
994 sb_add_sb (out, &t);
996 else
998 if (ptr->actual.len)
1000 sb_add_string (out, "-1");
1002 else
1004 sb_add_char (out, '0');
1008 else
1010 if (in->ptr[src] == '\n')
1011 ++macro_line;
1012 sb_add_char (out, in->ptr[src++]);
1016 sb_kill (&t);
1018 while (loclist != NULL)
1020 formal_entry *f;
1021 const char *name;
1023 f = loclist->next;
1024 name = sb_terminate (&loclist->name);
1025 str_hash_delete (formal_hash, name);
1026 del_formal (loclist);
1027 loclist = f;
1030 if (!err && (out->len == 0 || out->ptr[out->len - 1] != '\n'))
1031 sb_add_char (out, '\n');
1032 return err;
1035 /* Assign values to the formal parameters of a macro, and expand the
1036 body. */
1038 static const char *
1039 macro_expand (size_t idx, sb *in, macro_entry *m, sb *out)
1041 sb t;
1042 formal_entry *ptr;
1043 formal_entry *f;
1044 int is_keyword = 0;
1045 int narg = 0;
1046 const char *err = NULL;
1048 sb_new (&t);
1050 /* Reset any old value the actuals may have. */
1051 for (f = m->formals; f; f = f->next)
1052 sb_reset (&f->actual);
1053 f = m->formals;
1054 while (f != NULL && f->index < 0)
1055 f = f->next;
1057 if (flag_mri)
1059 /* The macro may be called with an optional qualifier, which may
1060 be referred to in the macro body as \0. */
1061 if (idx < in->len && in->ptr[idx] == '.')
1063 /* The Microtec assembler ignores this if followed by a white space.
1064 (Macro invocation with empty extension) */
1065 idx++;
1066 if ( idx < in->len
1067 && in->ptr[idx] != ' '
1068 && in->ptr[idx] != '\t')
1070 formal_entry *n = new_formal ();
1072 n->index = QUAL_INDEX;
1074 n->next = m->formals;
1075 m->formals = n;
1077 idx = get_any_string (idx, in, &n->actual);
1082 /* Peel off the actuals and store them away in the hash tables' actuals. */
1083 idx = sb_skip_white (idx, in);
1084 while (idx < in->len)
1086 size_t scan;
1088 /* Look and see if it's a positional or keyword arg. */
1089 scan = idx;
1090 while (scan < in->len
1091 && !ISSEP (in->ptr[scan])
1092 && !(flag_mri && in->ptr[scan] == '\'')
1093 && (!flag_macro_alternate && in->ptr[scan] != '='))
1094 scan++;
1095 if (scan < in->len && !flag_macro_alternate && in->ptr[scan] == '=')
1097 is_keyword = 1;
1099 /* It's OK to go from positional to keyword. */
1101 /* This is a keyword arg, fetch the formal name and
1102 then the actual stuff. */
1103 sb_reset (&t);
1104 idx = get_token (idx, in, &t);
1105 if (idx >= in->len || in->ptr[idx] != '=')
1107 err = _("confusion in formal parameters");
1108 break;
1111 /* Lookup the formal in the macro's list. */
1112 ptr = str_hash_find (m->formal_hash, sb_terminate (&t));
1113 if (!ptr)
1115 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1116 t.ptr,
1117 m->name);
1118 sb_reset (&t);
1119 idx = get_any_string (idx + 1, in, &t);
1121 else
1123 /* Insert this value into the right place. */
1124 if (ptr->actual.len)
1126 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1127 ptr->name.ptr,
1128 m->name);
1129 sb_reset (&ptr->actual);
1131 idx = get_any_string (idx + 1, in, &ptr->actual);
1132 if (ptr->actual.len > 0)
1133 ++narg;
1136 else
1138 if (is_keyword)
1140 err = _("can't mix positional and keyword arguments");
1141 break;
1144 if (!f)
1146 formal_entry **pf;
1147 int c;
1149 if (!flag_mri)
1151 err = _("too many positional arguments");
1152 break;
1155 f = new_formal ();
1157 c = -1;
1158 for (pf = &m->formals; *pf != NULL; pf = &(*pf)->next)
1159 if ((*pf)->index >= c)
1160 c = (*pf)->index + 1;
1161 if (c == -1)
1162 c = 0;
1163 *pf = f;
1164 f->index = c;
1167 if (f->type != FORMAL_VARARG)
1168 idx = get_any_string (idx, in, &f->actual);
1169 else if (idx < in->len)
1171 sb_add_buffer (&f->actual, in->ptr + idx, in->len - idx);
1172 idx = in->len;
1174 if (f->actual.len > 0)
1175 ++narg;
1178 f = f->next;
1180 while (f != NULL && f->index < 0);
1183 if (! flag_mri)
1184 idx = sb_skip_comma (idx, in);
1185 else
1187 if (idx < in->len && in->ptr[idx] == ',')
1188 ++idx;
1189 if (idx < in->len && ISWHITE (in->ptr[idx]))
1190 break;
1194 if (! err)
1196 for (ptr = m->formals; ptr; ptr = ptr->next)
1198 if (ptr->type == FORMAL_REQUIRED && ptr->actual.len == 0)
1199 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1200 ptr->name.ptr,
1201 m->name);
1204 if (flag_mri)
1206 ptr = str_hash_find (m->formal_hash,
1207 macro_strip_at ? "$NARG" : "NARG");
1208 if (ptr)
1210 char buffer[20];
1211 sprintf (buffer, "%d", narg);
1212 sb_add_string (&ptr->actual, buffer);
1216 err = macro_expand_body (&m->sub, out, m->formals, m->formal_hash, m);
1219 /* Discard any unnamed formal arguments. */
1220 if (flag_mri)
1222 formal_entry **pf;
1224 pf = &m->formals;
1225 while (*pf != NULL)
1227 if ((*pf)->name.len != 0)
1228 pf = &(*pf)->next;
1229 else
1231 f = (*pf)->next;
1232 del_formal (*pf);
1233 *pf = f;
1238 sb_kill (&t);
1239 if (!err)
1241 macro_number++;
1242 m->count++;
1245 return err;
1248 /* Check for a macro. If one is found, put the expansion into
1249 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1252 check_macro (const char *line, sb *expand,
1253 const char **error, macro_entry **info)
1255 const char *s;
1256 char *copy, *cls;
1257 macro_entry *macro;
1258 sb line_sb;
1260 if (! is_name_beginner (*line)
1261 && (! flag_mri || *line != '.'))
1262 return 0;
1264 s = line + 1;
1265 while (is_part_of_name (*s))
1266 ++s;
1267 if (is_name_ender (*s))
1268 ++s;
1270 copy = xmemdup0 (line, s - line);
1271 for (cls = copy; *cls != '\0'; cls ++)
1272 *cls = TOLOWER (*cls);
1274 macro = str_hash_find (macro_hash, copy);
1275 free (copy);
1277 if (macro == NULL)
1278 return 0;
1280 /* Wrap the line up in an sb. */
1281 sb_new (&line_sb);
1282 while (*s != '\0' && *s != '\n' && *s != '\r')
1283 sb_add_char (&line_sb, *s++);
1285 sb_new (expand);
1286 *error = macro_expand (0, &line_sb, macro, expand);
1288 sb_kill (&line_sb);
1290 /* Export the macro information if requested. */
1291 if (info)
1292 *info = macro;
1294 return 1;
1297 /* Delete a macro. */
1299 void
1300 delete_macro (const char *name)
1302 char *copy;
1303 size_t i, len;
1304 macro_entry *macro;
1306 len = strlen (name);
1307 copy = XNEWVEC (char, len + 1);
1308 for (i = 0; i < len; ++i)
1309 copy[i] = TOLOWER (name[i]);
1310 copy[i] = '\0';
1312 macro = str_hash_find (macro_hash, copy);
1313 if (macro != NULL)
1314 str_hash_delete (macro_hash, copy);
1315 else
1316 as_warn (_("Attempt to purge non-existing macro `%s'"), copy);
1317 free (copy);
1320 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1321 combined macro definition and execution. This returns NULL on
1322 success, or an error message otherwise. */
1324 const char *
1325 expand_irp (int irpc, size_t idx, sb *in, sb *out, size_t (*get_line) (sb *))
1327 sb sub;
1328 formal_entry f;
1329 struct htab *h;
1330 const char *err = NULL;
1332 idx = sb_skip_white (idx, in);
1334 sb_new (&sub);
1335 if (! buffer_and_nest (NULL, "ENDR", &sub, get_line))
1337 err = _("unexpected end of file in irp or irpc");
1338 goto out2;
1341 sb_new (&f.name);
1342 sb_new (&f.def);
1343 sb_new (&f.actual);
1345 idx = get_token (idx, in, &f.name);
1346 if (f.name.len == 0)
1348 err = _("missing model parameter");
1349 goto out1;
1352 h = str_htab_create ();
1354 str_hash_insert (h, sb_terminate (&f.name), &f, 0);
1356 f.index = 1;
1357 f.next = NULL;
1358 f.type = FORMAL_OPTIONAL;
1360 sb_reset (out);
1362 idx = sb_skip_comma (idx, in);
1363 if (idx >= in->len)
1365 /* Expand once with a null string. */
1366 err = macro_expand_body (&sub, out, &f, h, 0);
1368 else
1370 bool in_quotes = false;
1372 if (irpc && in->ptr[idx] == '"')
1374 in_quotes = true;
1375 ++idx;
1378 while (idx < in->len)
1380 if (!irpc)
1381 idx = get_any_string (idx, in, &f.actual);
1382 else
1384 if (in->ptr[idx] == '"')
1386 size_t nxt;
1388 if (irpc)
1389 in_quotes = ! in_quotes;
1391 nxt = sb_skip_white (idx + 1, in);
1392 if (nxt >= in->len)
1394 idx = nxt;
1395 break;
1398 sb_reset (&f.actual);
1399 sb_add_char (&f.actual, in->ptr[idx]);
1400 ++idx;
1403 err = macro_expand_body (&sub, out, &f, h, 0);
1404 if (err != NULL)
1405 break;
1406 if (!irpc)
1407 idx = sb_skip_comma (idx, in);
1408 else if (! in_quotes)
1409 idx = sb_skip_white (idx, in);
1413 htab_delete (h);
1414 out1:
1415 sb_kill (&f.actual);
1416 sb_kill (&f.def);
1417 sb_kill (&f.name);
1418 out2:
1419 sb_kill (&sub);
1421 return err;