gold: don't invoke IA32 syscall in x86_64 assembly testcase
[binutils-gdb.git] / gas / macro.c
blobc8ebcab0f07022d9d57fc90172eaf6db832c09d9
1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2022 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 || ((macro_alternate || macro_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 struct htab *macro_hash;
49 /* Whether any macros have been defined. */
51 int macro_defined;
53 /* Whether we are in alternate syntax mode. */
55 static int macro_alternate;
57 /* Whether we are in MRI mode. */
59 static int macro_mri;
61 /* Whether we should strip '@' characters. */
63 static int macro_strip_at;
65 /* Function to use to parse an expression. */
67 static size_t (*macro_expr) (const char *, size_t, sb *, offsetT *);
69 /* Number of macro expansions that have been done. */
71 static int macro_number;
73 /* Initialize macro processing. */
75 void
76 macro_init (int alternate, int mri, int strip_at,
77 size_t (*exp) (const char *, size_t, sb *, offsetT *))
79 macro_hash = htab_create_alloc (16, hash_macro_entry, eq_macro_entry,
80 NULL, xcalloc, free);
81 macro_defined = 0;
82 macro_alternate = alternate;
83 macro_mri = mri;
84 macro_strip_at = strip_at;
85 macro_expr = exp;
88 /* Switch in and out of alternate mode on the fly. */
90 void
91 macro_set_alternate (int alternate)
93 macro_alternate = alternate;
96 /* Switch in and out of MRI mode on the fly. */
98 void
99 macro_mri_mode (int mri)
101 macro_mri = mri;
104 /* Read input lines till we get to a TO string.
105 Increase nesting depth if we get a FROM string.
106 Put the results into sb at PTR.
107 FROM may be NULL (or will be ignored) if TO is "ENDR".
108 Add a new input line to an sb using GET_LINE.
109 Return 1 on success, 0 on unexpected EOF. */
112 buffer_and_nest (const char *from, const char *to, sb *ptr,
113 size_t (*get_line) (sb *))
115 size_t from_len;
116 size_t to_len = strlen (to);
117 int depth = 1;
118 size_t line_start = ptr->len;
119 size_t more = get_line (ptr);
121 if (to_len == 4 && strcasecmp (to, "ENDR") == 0)
123 from = NULL;
124 from_len = 0;
126 else
127 from_len = strlen (from);
129 /* Except for macros record the present source position, such that
130 diagnostics and debug info will be properly associated with the
131 respective original lines, rather than with the line of the ending
132 directive (TO). */
133 if (from == NULL || strcasecmp (from, "MACRO") != 0)
135 unsigned int line;
136 char *linefile;
138 as_where (&line);
139 linefile = xasprintf ("\t.linefile %u .\n", line);
140 sb_add_buffer (ptr, linefile, strlen (linefile));
141 xfree (linefile);
144 while (more)
146 /* Try to find the first pseudo op on the line. */
147 size_t i = line_start;
148 bool had_colon = false;
150 /* With normal syntax we can suck what we want till we get
151 to the dot. With the alternate, labels have to start in
152 the first column, since we can't tell what's a label and
153 what's a pseudoop. */
155 if (! LABELS_WITHOUT_COLONS)
157 /* Skip leading whitespace. */
158 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
159 i++;
162 for (;;)
164 /* Skip over a label, if any. */
165 if (i >= ptr->len || ! is_name_beginner (ptr->ptr[i]))
166 break;
167 i++;
168 while (i < ptr->len && is_part_of_name (ptr->ptr[i]))
169 i++;
170 if (i < ptr->len && is_name_ender (ptr->ptr[i]))
171 i++;
172 /* Skip whitespace. */
173 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
174 i++;
175 /* Check for the colon. */
176 if (i >= ptr->len || ptr->ptr[i] != ':')
178 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
179 colon after a label. If we do have a colon on the
180 first label then handle more than one label on the
181 line, assuming that each label has a colon. */
182 if (LABELS_WITHOUT_COLONS && !had_colon)
183 break;
184 i = line_start;
185 break;
187 i++;
188 line_start = i;
189 had_colon = true;
192 /* Skip trailing whitespace. */
193 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
194 i++;
196 if (i < ptr->len && (ptr->ptr[i] == '.'
197 || NO_PSEUDO_DOT
198 || macro_mri))
200 if (! flag_m68k_mri && ptr->ptr[i] == '.')
201 i++;
202 size_t len = ptr->len - i;
203 if (from == NULL)
205 if (len >= 5 && strncasecmp (ptr->ptr + i, "IREPC", 5) == 0)
206 from_len = 5;
207 else if (len >= 4 && strncasecmp (ptr->ptr + i, "IREP", 4) == 0)
208 from_len = 4;
209 else if (len >= 4 && strncasecmp (ptr->ptr + i, "IRPC", 4) == 0)
210 from_len = 4;
211 else if (len >= 4 && strncasecmp (ptr->ptr + i, "REPT", 4) == 0)
212 from_len = 4;
213 else if (len >= 3 && strncasecmp (ptr->ptr + i, "IRP", 3) == 0)
214 from_len = 3;
215 else if (len >= 3 && strncasecmp (ptr->ptr + i, "REP", 3) == 0)
216 from_len = 3;
217 else
218 from_len = 0;
220 if ((from != NULL
221 ? (len >= from_len
222 && strncasecmp (ptr->ptr + i, from, from_len) == 0)
223 : from_len > 0)
224 && (len == from_len
225 || ! (is_part_of_name (ptr->ptr[i + from_len])
226 || is_name_ender (ptr->ptr[i + from_len]))))
227 depth++;
228 if (len >= to_len
229 && strncasecmp (ptr->ptr + i, to, to_len) == 0
230 && (len == to_len
231 || ! (is_part_of_name (ptr->ptr[i + to_len])
232 || is_name_ender (ptr->ptr[i + to_len]))))
234 depth--;
235 if (depth == 0)
237 /* Reset the string to not include the ending rune. */
238 ptr->len = line_start;
239 break;
243 /* PR gas/16908
244 Apply and discard .linefile directives that appear within
245 the macro. For long macros, one might want to report the
246 line number information associated with the lines within
247 the macro definition, but we would need more infrastructure
248 to make that happen correctly (e.g. resetting the line
249 number when expanding the macro), and since for short
250 macros we clearly prefer reporting the point of expansion
251 anyway, there's not an obviously better fix here. */
252 if (from != NULL && strcasecmp (from, "MACRO") == 0
253 && len >= 8 && strncasecmp (ptr->ptr + i, "linefile", 8) == 0)
255 char saved_eol_char = ptr->ptr[ptr->len];
257 ptr->ptr[ptr->len] = '\0';
258 temp_ilp (ptr->ptr + i + 8);
259 s_linefile (0);
260 restore_ilp ();
261 ptr->ptr[ptr->len] = saved_eol_char;
262 ptr->len = line_start;
266 /* Add the original end-of-line char to the end and keep running. */
267 sb_add_char (ptr, more);
268 line_start = ptr->len;
269 more = get_line (ptr);
272 /* Return 1 on success, 0 on unexpected EOF. */
273 return depth == 0;
276 /* Pick up a token. */
278 static size_t
279 get_token (size_t idx, sb *in, sb *name)
281 if (idx < in->len
282 && is_name_beginner (in->ptr[idx]))
284 sb_add_char (name, in->ptr[idx++]);
285 while (idx < in->len
286 && is_part_of_name (in->ptr[idx]))
288 sb_add_char (name, in->ptr[idx++]);
290 if (idx < in->len
291 && is_name_ender (in->ptr[idx]))
293 sb_add_char (name, in->ptr[idx++]);
296 /* Ignore trailing &. */
297 if (macro_alternate && idx < in->len && in->ptr[idx] == '&')
298 idx++;
299 return idx;
302 /* Pick up a string. */
304 static size_t
305 getstring (size_t idx, sb *in, sb *acc)
307 while (idx < in->len
308 && (in->ptr[idx] == '"'
309 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
310 || (in->ptr[idx] == '\'' && macro_alternate)))
312 if (in->ptr[idx] == '<')
314 int nest = 0;
315 idx++;
316 while (idx < in->len
317 && (in->ptr[idx] != '>' || nest))
319 if (in->ptr[idx] == '!')
321 idx++;
322 sb_add_char (acc, in->ptr[idx++]);
324 else
326 if (in->ptr[idx] == '>')
327 nest--;
328 if (in->ptr[idx] == '<')
329 nest++;
330 sb_add_char (acc, in->ptr[idx++]);
333 idx++;
335 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
337 char tchar = in->ptr[idx];
338 int escaped = 0;
340 idx++;
342 while (idx < in->len)
344 if (in->ptr[idx - 1] == '\\')
345 escaped ^= 1;
346 else
347 escaped = 0;
349 if (macro_alternate && in->ptr[idx] == '!')
351 idx ++;
353 sb_add_char (acc, in->ptr[idx]);
355 idx ++;
357 else if (escaped && in->ptr[idx] == tchar)
359 sb_add_char (acc, tchar);
360 idx ++;
362 else
364 if (in->ptr[idx] == tchar)
366 idx ++;
368 if (idx >= in->len || in->ptr[idx] != tchar)
369 break;
372 sb_add_char (acc, in->ptr[idx]);
373 idx ++;
379 return idx;
382 /* Fetch string from the input stream,
383 rules:
384 'Bxyx<whitespace> -> return 'Bxyza
385 %<expr> -> return string of decimal value of <expr>
386 "string" -> return string
387 (string) -> return (string-including-whitespaces)
388 xyx<whitespace> -> return xyz. */
390 static size_t
391 get_any_string (size_t idx, sb *in, sb *out)
393 sb_reset (out);
394 idx = sb_skip_white (idx, in);
396 if (idx < in->len)
398 if (in->len > idx + 2 && in->ptr[idx + 1] == '\'' && ISBASE (in->ptr[idx]))
400 while (idx < in->len && !ISSEP (in->ptr[idx]))
401 sb_add_char (out, in->ptr[idx++]);
403 else if (in->ptr[idx] == '%' && macro_alternate)
405 offsetT val;
406 char buf[64];
408 /* Turns the next expression into a string. */
409 /* xgettext: no-c-format */
410 idx = (*macro_expr) (_("% operator needs absolute expression"),
411 idx + 1,
413 &val);
414 sprintf (buf, "%" BFD_VMA_FMT "d", val);
415 sb_add_string (out, buf);
417 else if (in->ptr[idx] == '"'
418 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
419 || (macro_alternate && in->ptr[idx] == '\''))
421 if (macro_alternate && ! macro_strip_at && in->ptr[idx] != '<')
423 /* Keep the quotes. */
424 sb_add_char (out, '"');
425 idx = getstring (idx, in, out);
426 sb_add_char (out, '"');
428 else
430 idx = getstring (idx, in, out);
433 else
435 char *br_buf = XNEWVEC (char, 1);
436 char *in_br = br_buf;
438 *in_br = '\0';
439 while (idx < in->len
440 && (*in_br
441 || (in->ptr[idx] != ' '
442 && in->ptr[idx] != '\t'))
443 && in->ptr[idx] != ','
444 && (in->ptr[idx] != '<'
445 || (! macro_alternate && ! macro_mri)))
447 char tchar = in->ptr[idx];
449 switch (tchar)
451 case '"':
452 case '\'':
453 sb_add_char (out, in->ptr[idx++]);
454 while (idx < in->len
455 && in->ptr[idx] != tchar)
456 sb_add_char (out, in->ptr[idx++]);
457 if (idx == in->len)
459 free (br_buf);
460 return idx;
462 break;
463 case '(':
464 case '[':
465 if (in_br > br_buf)
466 --in_br;
467 else
469 br_buf = XNEWVEC (char, strlen (in_br) + 2);
470 strcpy (br_buf + 1, in_br);
471 free (in_br);
472 in_br = br_buf;
474 *in_br = tchar;
475 break;
476 case ')':
477 if (*in_br == '(')
478 ++in_br;
479 break;
480 case ']':
481 if (*in_br == '[')
482 ++in_br;
483 break;
485 sb_add_char (out, tchar);
486 ++idx;
488 free (br_buf);
492 return idx;
495 /* Allocate a new formal. */
497 static formal_entry *
498 new_formal (void)
500 formal_entry *formal;
502 formal = XNEW (formal_entry);
504 sb_new (&formal->name);
505 sb_new (&formal->def);
506 sb_new (&formal->actual);
507 formal->next = NULL;
508 formal->type = FORMAL_OPTIONAL;
509 return formal;
512 /* Free a formal. */
514 static void
515 del_formal (formal_entry *formal)
517 sb_kill (&formal->actual);
518 sb_kill (&formal->def);
519 sb_kill (&formal->name);
520 free (formal);
523 /* Pick up the formal parameters of a macro definition. */
525 static size_t
526 do_formals (macro_entry *macro, size_t idx, sb *in)
528 formal_entry **p = &macro->formals;
529 const char *name;
531 idx = sb_skip_white (idx, in);
532 while (idx < in->len)
534 formal_entry *formal = new_formal ();
535 size_t cidx;
536 formal_hash_entry_t *elt;
538 idx = get_token (idx, in, &formal->name);
539 if (formal->name.len == 0)
541 if (macro->formal_count)
542 --idx;
543 del_formal (formal); /* 'formal' goes out of scope. */
544 break;
546 idx = sb_skip_white (idx, in);
547 /* This is a formal. */
548 name = sb_terminate (&formal->name);
549 if (! macro_mri
550 && idx < in->len
551 && in->ptr[idx] == ':'
552 && (! is_name_beginner (':')
553 || idx + 1 >= in->len
554 || ! is_part_of_name (in->ptr[idx + 1])))
556 /* Got a qualifier. */
557 sb qual;
559 sb_new (&qual);
560 idx = get_token (sb_skip_white (idx + 1, in), in, &qual);
561 sb_terminate (&qual);
562 if (qual.len == 0)
563 as_bad_where (macro->file,
564 macro->line,
565 _("Missing parameter qualifier for `%s' in macro `%s'"),
566 name,
567 macro->name);
568 else if (strcmp (qual.ptr, "req") == 0)
569 formal->type = FORMAL_REQUIRED;
570 else if (strcmp (qual.ptr, "vararg") == 0)
571 formal->type = FORMAL_VARARG;
572 else
573 as_bad_where (macro->file,
574 macro->line,
575 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
576 qual.ptr,
577 name,
578 macro->name);
579 sb_kill (&qual);
580 idx = sb_skip_white (idx, in);
582 if (idx < in->len && in->ptr[idx] == '=')
584 /* Got a default. */
585 idx = get_any_string (idx + 1, in, &formal->def);
586 idx = sb_skip_white (idx, in);
587 if (formal->type == FORMAL_REQUIRED)
589 sb_reset (&formal->def);
590 as_warn_where (macro->file,
591 macro->line,
592 _("Pointless default value for required parameter `%s' in macro `%s'"),
593 name,
594 macro->name);
598 /* Add to macro's hash table. */
599 elt = formal_entry_alloc (name, formal);
600 if (htab_insert (macro->formal_hash, elt, 0) != NULL)
602 free (elt);
603 as_bad_where (macro->file, macro->line,
604 _("A parameter named `%s' "
605 "already exists for macro `%s'"),
606 name, macro->name);
609 formal->index = macro->formal_count++;
610 *p = formal;
611 p = &formal->next;
612 if (formal->type == FORMAL_VARARG)
613 break;
614 cidx = idx;
615 idx = sb_skip_comma (idx, in);
616 if (idx != cidx && idx >= in->len)
618 idx = cidx;
619 break;
623 if (macro_mri)
625 formal_entry *formal = new_formal ();
626 formal_hash_entry_t *elt;
628 /* Add a special NARG formal, which macro_expand will set to the
629 number of arguments. */
630 /* The same MRI assemblers which treat '@' characters also use
631 the name $NARG. At least until we find an exception. */
632 if (macro_strip_at)
633 name = "$NARG";
634 else
635 name = "NARG";
637 sb_add_string (&formal->name, name);
639 /* Add to macro's hash table. */
640 elt = formal_entry_alloc (name, formal);
641 if (htab_insert (macro->formal_hash, elt, 0) != NULL)
643 free (elt);
644 as_bad_where (macro->file, macro->line,
645 _("Reserved word `%s' used as parameter in macro `%s'"),
646 name, macro->name);
649 formal->index = NARG_INDEX;
650 *p = formal;
653 return idx;
656 /* Free the memory allocated to a macro. */
658 static void
659 free_macro (macro_entry *macro)
661 formal_entry *formal;
663 for (formal = macro->formals; formal; )
665 formal_entry *f;
667 f = formal;
668 formal = formal->next;
669 del_formal (f);
671 htab_delete (macro->formal_hash);
672 sb_kill (&macro->sub);
673 free (macro);
676 /* Define a new macro. Returns NULL on success, otherwise returns an
677 error message. If NAMEP is not NULL, *NAMEP is set to the name of
678 the macro which was defined. */
680 const char *
681 define_macro (size_t idx, sb *in, sb *label,
682 size_t (*get_line) (sb *),
683 const char *file, unsigned int line,
684 const char **namep)
686 macro_entry *macro;
687 sb name;
688 const char *error = NULL;
690 macro = XNEW (macro_entry);
691 sb_new (&macro->sub);
692 sb_new (&name);
693 macro->file = file;
694 macro->line = line;
696 macro->formal_count = 0;
697 macro->formals = 0;
698 macro->formal_hash = htab_create_alloc (7, hash_formal_entry, eq_formal_entry,
699 NULL, xcalloc, free);
701 idx = sb_skip_white (idx, in);
702 if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
703 error = _("unexpected end of file in macro `%s' definition");
704 if (label != NULL && label->len != 0)
706 sb_add_sb (&name, label);
707 macro->name = sb_terminate (&name);
708 if (idx < in->len && in->ptr[idx] == '(')
710 /* It's the label: MACRO (formals,...) sort */
711 idx = do_formals (macro, idx + 1, in);
712 if (idx < in->len && in->ptr[idx] == ')')
713 idx = sb_skip_white (idx + 1, in);
714 else if (!error)
715 error = _("missing `)' after formals in macro definition `%s'");
717 else
719 /* It's the label: MACRO formals,... sort */
720 idx = do_formals (macro, idx, in);
723 else
725 size_t cidx;
727 idx = get_token (idx, in, &name);
728 macro->name = sb_terminate (&name);
729 if (name.len == 0)
730 error = _("Missing macro name");
731 cidx = sb_skip_white (idx, in);
732 idx = sb_skip_comma (cidx, in);
733 if (idx == cidx || idx < in->len)
734 idx = do_formals (macro, idx, in);
735 else
736 idx = cidx;
738 if (!error && idx < in->len)
739 error = _("Bad parameter list for macro `%s'");
741 /* And stick it in the macro hash table. */
742 for (idx = 0; idx < name.len; idx++)
743 name.ptr[idx] = TOLOWER (name.ptr[idx]);
744 if (!error)
746 macro_hash_entry_t *elt = macro_entry_alloc (macro->name, macro);
747 if (htab_insert (macro_hash, elt, 0) != NULL)
749 free (elt);
750 error = _("Macro `%s' was already defined");
754 if (namep != NULL)
755 *namep = macro->name;
757 if (!error)
758 macro_defined = 1;
759 else
760 free_macro (macro);
762 return error;
765 /* Scan a token, and then skip KIND. */
767 static size_t
768 get_apost_token (size_t idx, sb *in, sb *name, int kind)
770 idx = get_token (idx, in, name);
771 if (idx < in->len
772 && in->ptr[idx] == kind
773 && (! macro_mri || macro_strip_at)
774 && (! macro_strip_at || kind == '@'))
775 idx++;
776 return idx;
779 /* Substitute the actual value for a formal parameter. */
781 static size_t
782 sub_actual (size_t start, sb *in, sb *t, struct htab *formal_hash,
783 int kind, sb *out, int copyifnotthere)
785 size_t src;
786 formal_entry *ptr;
788 src = get_apost_token (start, in, t, kind);
789 /* See if it's in the macro's hash table, unless this is
790 macro_strip_at and kind is '@' and the token did not end in '@'. */
791 if (macro_strip_at
792 && kind == '@'
793 && (src == start || in->ptr[src - 1] != '@'))
794 ptr = NULL;
795 else
796 ptr = formal_entry_find (formal_hash, sb_terminate (t));
797 if (ptr)
799 if (ptr->actual.len)
801 sb_add_sb (out, &ptr->actual);
803 else
805 sb_add_sb (out, &ptr->def);
808 else if (kind == '&')
810 /* Doing this permits people to use & in macro bodies. */
811 sb_add_char (out, '&');
812 sb_add_sb (out, t);
813 if (src != start && in->ptr[src - 1] == '&')
814 sb_add_char (out, '&');
816 else if (copyifnotthere)
818 sb_add_sb (out, t);
820 else
822 sb_add_char (out, '\\');
823 sb_add_sb (out, t);
825 return src;
828 /* Expand the body of a macro. */
830 static const char *
831 macro_expand_body (sb *in, sb *out, formal_entry *formals,
832 struct htab *formal_hash, const macro_entry *macro)
834 sb t;
835 size_t src = 0;
836 int inquote = 0, macro_line = 0;
837 formal_entry *loclist = NULL;
838 const char *err = NULL;
840 sb_new (&t);
842 while (src < in->len && !err)
844 if (in->ptr[src] == '&')
846 sb_reset (&t);
847 if (macro_mri)
849 if (src + 1 < in->len && in->ptr[src + 1] == '&')
850 src = sub_actual (src + 2, in, &t, formal_hash, '\'', out, 1);
851 else
852 sb_add_char (out, in->ptr[src++]);
854 else
856 /* Permit macro parameter substitution delineated with
857 an '&' prefix and optional '&' suffix. */
858 src = sub_actual (src + 1, in, &t, formal_hash, '&', out, 0);
861 else if (in->ptr[src] == '\\')
863 src++;
864 if (src < in->len && in->ptr[src] == '(')
866 /* Sub in till the next ')' literally. */
867 src++;
868 while (src < in->len && in->ptr[src] != ')')
870 sb_add_char (out, in->ptr[src++]);
872 if (src < in->len)
873 src++;
874 else if (!macro)
875 err = _("missing `)'");
876 else
877 as_bad_where (macro->file, macro->line + macro_line, _("missing `)'"));
879 else if (src < in->len && in->ptr[src] == '@')
881 /* Sub in the macro invocation number. */
883 char buffer[12];
884 src++;
885 sprintf (buffer, "%d", macro_number);
886 sb_add_string (out, buffer);
888 else if (src < in->len && in->ptr[src] == '&')
890 /* This is a preprocessor variable name, we don't do them
891 here. */
892 sb_add_char (out, '\\');
893 sb_add_char (out, '&');
894 src++;
896 else if (macro_mri && src < in->len && ISALNUM (in->ptr[src]))
898 int ind;
899 formal_entry *f;
901 if (ISDIGIT (in->ptr[src]))
902 ind = in->ptr[src] - '0';
903 else if (ISUPPER (in->ptr[src]))
904 ind = in->ptr[src] - 'A' + 10;
905 else
906 ind = in->ptr[src] - 'a' + 10;
907 ++src;
908 for (f = formals; f != NULL; f = f->next)
910 if (f->index == ind - 1)
912 if (f->actual.len != 0)
913 sb_add_sb (out, &f->actual);
914 else
915 sb_add_sb (out, &f->def);
916 break;
920 else
922 sb_reset (&t);
923 src = sub_actual (src, in, &t, formal_hash, '\'', out, 0);
926 else if ((macro_alternate || macro_mri)
927 && is_name_beginner (in->ptr[src])
928 && (! inquote
929 || ! macro_strip_at
930 || (src > 0 && in->ptr[src - 1] == '@')))
932 if (! macro
933 || src + 5 >= in->len
934 || strncasecmp (in->ptr + src, "LOCAL", 5) != 0
935 || ! ISWHITE (in->ptr[src + 5])
936 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
937 || inquote)
939 sb_reset (&t);
940 src = sub_actual (src, in, &t, formal_hash,
941 (macro_strip_at && inquote) ? '@' : '\'',
942 out, 1);
944 else
946 src = sb_skip_white (src + 5, in);
947 while (in->ptr[src] != '\n')
949 const char *name;
950 formal_entry *f = new_formal ();
951 formal_hash_entry_t *elt;
953 src = get_token (src, in, &f->name);
954 name = sb_terminate (&f->name);
955 elt = formal_entry_alloc (name, f);
956 if (htab_insert (formal_hash, elt, 0) != NULL)
958 free (elt);
959 as_bad_where (macro->file, macro->line + macro_line,
960 _("`%s' was already used as parameter "
961 "(or another local) name"), name);
962 del_formal (f);
964 else
966 static int loccnt;
967 char buf[20];
969 f->index = LOCAL_INDEX;
970 f->next = loclist;
971 loclist = f;
973 sprintf (buf, IS_ELF ? ".LL%04x" : "LL%04x", ++loccnt);
974 sb_add_string (&f->actual, buf);
977 src = sb_skip_comma (src, in);
981 else if (in->ptr[src] == '"'
982 || (macro_mri && in->ptr[src] == '\''))
984 inquote = !inquote;
985 sb_add_char (out, in->ptr[src++]);
987 else if (in->ptr[src] == '@' && macro_strip_at)
989 ++src;
990 if (src < in->len
991 && in->ptr[src] == '@')
993 sb_add_char (out, '@');
994 ++src;
997 else if (macro_mri
998 && in->ptr[src] == '='
999 && src + 1 < in->len
1000 && in->ptr[src + 1] == '=')
1002 formal_entry *ptr;
1004 sb_reset (&t);
1005 src = get_token (src + 2, in, &t);
1006 ptr = formal_entry_find (formal_hash, sb_terminate (&t));
1007 if (ptr == NULL)
1009 /* FIXME: We should really return a warning string here,
1010 but we can't, because the == might be in the MRI
1011 comment field, and, since the nature of the MRI
1012 comment field depends upon the exact instruction
1013 being used, we don't have enough information here to
1014 figure out whether it is or not. Instead, we leave
1015 the == in place, which should cause a syntax error if
1016 it is not in a comment. */
1017 sb_add_char (out, '=');
1018 sb_add_char (out, '=');
1019 sb_add_sb (out, &t);
1021 else
1023 if (ptr->actual.len)
1025 sb_add_string (out, "-1");
1027 else
1029 sb_add_char (out, '0');
1033 else
1035 if (in->ptr[src] == '\n')
1036 ++macro_line;
1037 sb_add_char (out, in->ptr[src++]);
1041 sb_kill (&t);
1043 while (loclist != NULL)
1045 formal_entry *f;
1046 const char *name;
1048 f = loclist->next;
1049 name = sb_terminate (&loclist->name);
1050 formal_hash_entry_t needle = { name, NULL };
1051 htab_remove_elt (formal_hash, &needle);
1052 del_formal (loclist);
1053 loclist = f;
1056 return err;
1059 /* Assign values to the formal parameters of a macro, and expand the
1060 body. */
1062 static const char *
1063 macro_expand (size_t idx, sb *in, macro_entry *m, sb *out)
1065 sb t;
1066 formal_entry *ptr;
1067 formal_entry *f;
1068 int is_keyword = 0;
1069 int narg = 0;
1070 const char *err = NULL;
1072 sb_new (&t);
1074 /* Reset any old value the actuals may have. */
1075 for (f = m->formals; f; f = f->next)
1076 sb_reset (&f->actual);
1077 f = m->formals;
1078 while (f != NULL && f->index < 0)
1079 f = f->next;
1081 if (macro_mri)
1083 /* The macro may be called with an optional qualifier, which may
1084 be referred to in the macro body as \0. */
1085 if (idx < in->len && in->ptr[idx] == '.')
1087 /* The Microtec assembler ignores this if followed by a white space.
1088 (Macro invocation with empty extension) */
1089 idx++;
1090 if ( idx < in->len
1091 && in->ptr[idx] != ' '
1092 && in->ptr[idx] != '\t')
1094 formal_entry *n = new_formal ();
1096 n->index = QUAL_INDEX;
1098 n->next = m->formals;
1099 m->formals = n;
1101 idx = get_any_string (idx, in, &n->actual);
1106 /* Peel off the actuals and store them away in the hash tables' actuals. */
1107 idx = sb_skip_white (idx, in);
1108 while (idx < in->len)
1110 size_t scan;
1112 /* Look and see if it's a positional or keyword arg. */
1113 scan = idx;
1114 while (scan < in->len
1115 && !ISSEP (in->ptr[scan])
1116 && !(macro_mri && in->ptr[scan] == '\'')
1117 && (!macro_alternate && in->ptr[scan] != '='))
1118 scan++;
1119 if (scan < in->len && !macro_alternate && in->ptr[scan] == '=')
1121 is_keyword = 1;
1123 /* It's OK to go from positional to keyword. */
1125 /* This is a keyword arg, fetch the formal name and
1126 then the actual stuff. */
1127 sb_reset (&t);
1128 idx = get_token (idx, in, &t);
1129 if (in->ptr[idx] != '=')
1131 err = _("confusion in formal parameters");
1132 break;
1135 /* Lookup the formal in the macro's list. */
1136 ptr = formal_entry_find (m->formal_hash, sb_terminate (&t));
1137 if (!ptr)
1139 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1140 t.ptr,
1141 m->name);
1142 sb_reset (&t);
1143 idx = get_any_string (idx + 1, in, &t);
1145 else
1147 /* Insert this value into the right place. */
1148 if (ptr->actual.len)
1150 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1151 ptr->name.ptr,
1152 m->name);
1153 sb_reset (&ptr->actual);
1155 idx = get_any_string (idx + 1, in, &ptr->actual);
1156 if (ptr->actual.len > 0)
1157 ++narg;
1160 else
1162 if (is_keyword)
1164 err = _("can't mix positional and keyword arguments");
1165 break;
1168 if (!f)
1170 formal_entry **pf;
1171 int c;
1173 if (!macro_mri)
1175 err = _("too many positional arguments");
1176 break;
1179 f = new_formal ();
1181 c = -1;
1182 for (pf = &m->formals; *pf != NULL; pf = &(*pf)->next)
1183 if ((*pf)->index >= c)
1184 c = (*pf)->index + 1;
1185 if (c == -1)
1186 c = 0;
1187 *pf = f;
1188 f->index = c;
1191 if (f->type != FORMAL_VARARG)
1192 idx = get_any_string (idx, in, &f->actual);
1193 else
1195 sb_add_buffer (&f->actual, in->ptr + idx, in->len - idx);
1196 idx = in->len;
1198 if (f->actual.len > 0)
1199 ++narg;
1202 f = f->next;
1204 while (f != NULL && f->index < 0);
1207 if (! macro_mri)
1208 idx = sb_skip_comma (idx, in);
1209 else
1211 if (in->ptr[idx] == ',')
1212 ++idx;
1213 if (ISWHITE (in->ptr[idx]))
1214 break;
1218 if (! err)
1220 for (ptr = m->formals; ptr; ptr = ptr->next)
1222 if (ptr->type == FORMAL_REQUIRED && ptr->actual.len == 0)
1223 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1224 ptr->name.ptr,
1225 m->name);
1228 if (macro_mri)
1230 char buffer[20];
1232 sb_reset (&t);
1233 sb_add_string (&t, macro_strip_at ? "$NARG" : "NARG");
1234 ptr = formal_entry_find (m->formal_hash, sb_terminate (&t));
1235 sprintf (buffer, "%d", narg);
1236 sb_add_string (&ptr->actual, buffer);
1239 err = macro_expand_body (&m->sub, out, m->formals, m->formal_hash, m);
1242 /* Discard any unnamed formal arguments. */
1243 if (macro_mri)
1245 formal_entry **pf;
1247 pf = &m->formals;
1248 while (*pf != NULL)
1250 if ((*pf)->name.len != 0)
1251 pf = &(*pf)->next;
1252 else
1254 f = (*pf)->next;
1255 del_formal (*pf);
1256 *pf = f;
1261 sb_kill (&t);
1262 if (!err)
1263 macro_number++;
1265 return err;
1268 /* Check for a macro. If one is found, put the expansion into
1269 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1272 check_macro (const char *line, sb *expand,
1273 const char **error, macro_entry **info)
1275 const char *s;
1276 char *copy, *cls;
1277 macro_entry *macro;
1278 sb line_sb;
1280 if (! is_name_beginner (*line)
1281 && (! macro_mri || *line != '.'))
1282 return 0;
1284 s = line + 1;
1285 while (is_part_of_name (*s))
1286 ++s;
1287 if (is_name_ender (*s))
1288 ++s;
1290 copy = xmemdup0 (line, s - line);
1291 for (cls = copy; *cls != '\0'; cls ++)
1292 *cls = TOLOWER (*cls);
1294 macro = macro_entry_find (macro_hash, copy);
1295 free (copy);
1297 if (macro == NULL)
1298 return 0;
1300 /* Wrap the line up in an sb. */
1301 sb_new (&line_sb);
1302 while (*s != '\0' && *s != '\n' && *s != '\r')
1303 sb_add_char (&line_sb, *s++);
1305 sb_new (expand);
1306 *error = macro_expand (0, &line_sb, macro, expand);
1308 sb_kill (&line_sb);
1310 /* Export the macro information if requested. */
1311 if (info)
1312 *info = macro;
1314 return 1;
1317 /* Delete a macro. */
1319 void
1320 delete_macro (const char *name)
1322 char *copy;
1323 size_t i, len;
1324 void **slot;
1325 macro_hash_entry_t needle;
1327 len = strlen (name);
1328 copy = XNEWVEC (char, len + 1);
1329 for (i = 0; i < len; ++i)
1330 copy[i] = TOLOWER (name[i]);
1331 copy[i] = '\0';
1333 needle.name = copy;
1334 needle.macro = NULL;
1335 slot = htab_find_slot (macro_hash, &needle, NO_INSERT);
1336 if (slot)
1338 free_macro (((macro_hash_entry_t *) *slot)->macro);
1339 htab_clear_slot (macro_hash, slot);
1341 else
1342 as_warn (_("Attempt to purge non-existing macro `%s'"), copy);
1343 free (copy);
1346 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1347 combined macro definition and execution. This returns NULL on
1348 success, or an error message otherwise. */
1350 const char *
1351 expand_irp (int irpc, size_t idx, sb *in, sb *out, size_t (*get_line) (sb *))
1353 sb sub;
1354 formal_entry f;
1355 struct htab *h;
1356 const char *err = NULL;
1358 idx = sb_skip_white (idx, in);
1360 sb_new (&sub);
1361 if (! buffer_and_nest (NULL, "ENDR", &sub, get_line))
1362 return _("unexpected end of file in irp or irpc");
1364 sb_new (&f.name);
1365 sb_new (&f.def);
1366 sb_new (&f.actual);
1368 idx = get_token (idx, in, &f.name);
1369 if (f.name.len == 0)
1370 return _("missing model parameter");
1372 h = htab_create_alloc (16, hash_formal_entry, eq_formal_entry,
1373 NULL, xcalloc, free);
1375 htab_insert (h, formal_entry_alloc (sb_terminate (&f.name), &f), 0);
1377 f.index = 1;
1378 f.next = NULL;
1379 f.type = FORMAL_OPTIONAL;
1381 sb_reset (out);
1383 idx = sb_skip_comma (idx, in);
1384 if (idx >= in->len)
1386 /* Expand once with a null string. */
1387 err = macro_expand_body (&sub, out, &f, h, 0);
1389 else
1391 bool in_quotes = false;
1393 if (irpc && in->ptr[idx] == '"')
1395 in_quotes = true;
1396 ++idx;
1399 while (idx < in->len)
1401 if (!irpc)
1402 idx = get_any_string (idx, in, &f.actual);
1403 else
1405 if (in->ptr[idx] == '"')
1407 size_t nxt;
1409 if (irpc)
1410 in_quotes = ! in_quotes;
1412 nxt = sb_skip_white (idx + 1, in);
1413 if (nxt >= in->len)
1415 idx = nxt;
1416 break;
1419 sb_reset (&f.actual);
1420 sb_add_char (&f.actual, in->ptr[idx]);
1421 ++idx;
1424 err = macro_expand_body (&sub, out, &f, h, 0);
1425 if (err != NULL)
1426 break;
1427 if (!irpc)
1428 idx = sb_skip_comma (idx, in);
1429 else if (! in_quotes)
1430 idx = sb_skip_white (idx, in);
1434 htab_delete (h);
1435 sb_kill (&f.actual);
1436 sb_kill (&f.def);
1437 sb_kill (&f.name);
1438 sb_kill (&sub);
1440 return err;