1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2014 Free Software Foundation, Inc.
4 Written by Steve and Judy Chamberlain of Cygnus Support,
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)
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
25 #include "safe-ctype.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')
35 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
36 || (x) == ')' || (x) == '(' \
37 || ((macro_alternate || macro_mri) && ((x) == '<' || (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 hash_control *macro_hash;
49 /* Whether any macros have been defined. */
53 /* Whether we are in alternate syntax mode. */
55 static int macro_alternate;
57 /* Whether we are in MRI mode. */
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. */
76 macro_init (int alternate, int mri, int strip_at,
77 size_t (*exp) (const char *, size_t, sb *, offsetT *))
79 macro_hash = hash_new ();
81 macro_alternate = alternate;
83 macro_strip_at = strip_at;
87 /* Switch in and out of alternate mode on the fly. */
90 macro_set_alternate (int alternate)
92 macro_alternate = alternate;
95 /* Switch in and out of MRI mode on the fly. */
98 macro_mri_mode (int mri)
103 /* Read input lines till we get to a TO string.
104 Increase nesting depth if we get a FROM string.
105 Put the results into sb at PTR.
106 FROM may be NULL (or will be ignored) if TO is "ENDR".
107 Add a new input line to an sb using GET_LINE.
108 Return 1 on success, 0 on unexpected EOF. */
111 buffer_and_nest (const char *from, const char *to, sb *ptr,
112 size_t (*get_line) (sb *))
115 size_t to_len = strlen (to);
117 size_t line_start = ptr->len;
118 size_t more = get_line (ptr);
120 if (to_len == 4 && strcasecmp (to, "ENDR") == 0)
126 from_len = strlen (from);
130 /* Try to find the first pseudo op on the line. */
131 size_t i = line_start;
132 bfd_boolean 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]))
148 /* Skip over a label, if any. */
149 if (i >= ptr->len || ! is_name_beginner (ptr->ptr[i]))
152 while (i < ptr->len && is_part_of_name (ptr->ptr[i]))
154 if (i < ptr->len && is_name_ender (ptr->ptr[i]))
156 /* Skip whitespace. */
157 while (i < ptr->len && ISWHITE (ptr->ptr[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)
176 /* Skip trailing whitespace. */
177 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
180 if (i < ptr->len && (ptr->ptr[i] == '.'
184 if (! flag_m68k_mri && ptr->ptr[i] == '.')
187 && strncasecmp (ptr->ptr + i, "IRPC", from_len = 4) != 0
188 && strncasecmp (ptr->ptr + i, "IRP", from_len = 3) != 0
189 && strncasecmp (ptr->ptr + i, "IREPC", from_len = 5) != 0
190 && strncasecmp (ptr->ptr + i, "IREP", from_len = 4) != 0
191 && strncasecmp (ptr->ptr + i, "REPT", from_len = 4) != 0
192 && strncasecmp (ptr->ptr + i, "REP", from_len = 3) != 0)
195 ? strncasecmp (ptr->ptr + i, from, from_len) == 0
197 && (ptr->len == (i + from_len)
198 || ! (is_part_of_name (ptr->ptr[i + from_len])
199 || is_name_ender (ptr->ptr[i + from_len]))))
201 if (strncasecmp (ptr->ptr + i, to, to_len) == 0
202 && (ptr->len == (i + to_len)
203 || ! (is_part_of_name (ptr->ptr[i + to_len])
204 || is_name_ender (ptr->ptr[i + to_len]))))
209 /* Reset the string to not include the ending rune. */
210 ptr->len = line_start;
216 /* Add the original end-of-line char to the end and keep running. */
217 sb_add_char (ptr, more);
218 line_start = ptr->len;
219 more = get_line (ptr);
222 /* Return 1 on success, 0 on unexpected EOF. */
226 /* Pick up a token. */
229 get_token (size_t idx, sb *in, sb *name)
232 && is_name_beginner (in->ptr[idx]))
234 sb_add_char (name, in->ptr[idx++]);
236 && is_part_of_name (in->ptr[idx]))
238 sb_add_char (name, in->ptr[idx++]);
241 && is_name_ender (in->ptr[idx]))
243 sb_add_char (name, in->ptr[idx++]);
246 /* Ignore trailing &. */
247 if (macro_alternate && idx < in->len && in->ptr[idx] == '&')
252 /* Pick up a string. */
255 getstring (size_t idx, sb *in, sb *acc)
258 && (in->ptr[idx] == '"'
259 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
260 || (in->ptr[idx] == '\'' && macro_alternate)))
262 if (in->ptr[idx] == '<')
266 while ((in->ptr[idx] != '>' || nest)
269 if (in->ptr[idx] == '!')
272 sb_add_char (acc, in->ptr[idx++]);
276 if (in->ptr[idx] == '>')
278 if (in->ptr[idx] == '<')
280 sb_add_char (acc, in->ptr[idx++]);
285 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
287 char tchar = in->ptr[idx];
292 while (idx < in->len)
294 if (in->ptr[idx - 1] == '\\')
299 if (macro_alternate && in->ptr[idx] == '!')
303 sb_add_char (acc, in->ptr[idx]);
307 else if (escaped && in->ptr[idx] == tchar)
309 sb_add_char (acc, tchar);
314 if (in->ptr[idx] == tchar)
318 if (idx >= in->len || in->ptr[idx] != tchar)
322 sb_add_char (acc, in->ptr[idx]);
332 /* Fetch string from the input stream,
334 'Bxyx<whitespace> -> return 'Bxyza
335 %<expr> -> return string of decimal value of <expr>
336 "string" -> return string
337 (string) -> return (string-including-whitespaces)
338 xyx<whitespace> -> return xyz. */
341 get_any_string (size_t idx, sb *in, sb *out)
344 idx = sb_skip_white (idx, in);
348 if (in->len > idx + 2 && in->ptr[idx + 1] == '\'' && ISBASE (in->ptr[idx]))
350 while (!ISSEP (in->ptr[idx]))
351 sb_add_char (out, in->ptr[idx++]);
353 else if (in->ptr[idx] == '%' && macro_alternate)
358 /* Turns the next expression into a string. */
359 /* xgettext: no-c-format */
360 idx = (*macro_expr) (_("% operator needs absolute expression"),
364 sprintf (buf, "%" BFD_VMA_FMT "d", val);
365 sb_add_string (out, buf);
367 else if (in->ptr[idx] == '"'
368 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
369 || (macro_alternate && in->ptr[idx] == '\''))
371 if (macro_alternate && ! macro_strip_at && in->ptr[idx] != '<')
373 /* Keep the quotes. */
374 sb_add_char (out, '"');
375 idx = getstring (idx, in, out);
376 sb_add_char (out, '"');
380 idx = getstring (idx, in, out);
385 char *br_buf = (char *) xmalloc (1);
386 char *in_br = br_buf;
391 || (in->ptr[idx] != ' '
392 && in->ptr[idx] != '\t'))
393 && in->ptr[idx] != ','
394 && (in->ptr[idx] != '<'
395 || (! macro_alternate && ! macro_mri)))
397 char tchar = in->ptr[idx];
403 sb_add_char (out, in->ptr[idx++]);
405 && in->ptr[idx] != tchar)
406 sb_add_char (out, in->ptr[idx++]);
419 br_buf = (char *) xmalloc (strlen (in_br) + 2);
420 strcpy (br_buf + 1, in_br);
435 sb_add_char (out, tchar);
445 /* Allocate a new formal. */
447 static formal_entry *
450 formal_entry *formal;
452 formal = (formal_entry *) xmalloc (sizeof (formal_entry));
454 sb_new (&formal->name);
455 sb_new (&formal->def);
456 sb_new (&formal->actual);
458 formal->type = FORMAL_OPTIONAL;
465 del_formal (formal_entry *formal)
467 sb_kill (&formal->actual);
468 sb_kill (&formal->def);
469 sb_kill (&formal->name);
473 /* Pick up the formal parameters of a macro definition. */
476 do_formals (macro_entry *macro, size_t idx, sb *in)
478 formal_entry **p = ¯o->formals;
481 idx = sb_skip_white (idx, in);
482 while (idx < in->len)
484 formal_entry *formal = new_formal ();
487 idx = get_token (idx, in, &formal->name);
488 if (formal->name.len == 0)
490 if (macro->formal_count)
492 del_formal (formal); /* 'formal' goes out of scope. */
495 idx = sb_skip_white (idx, in);
496 /* This is a formal. */
497 name = sb_terminate (&formal->name);
500 && in->ptr[idx] == ':'
501 && (! is_name_beginner (':')
502 || idx + 1 >= in->len
503 || ! is_part_of_name (in->ptr[idx + 1])))
505 /* Got a qualifier. */
509 idx = get_token (sb_skip_white (idx + 1, in), in, &qual);
510 sb_terminate (&qual);
512 as_bad_where (macro->file,
514 _("Missing parameter qualifier for `%s' in macro `%s'"),
517 else if (strcmp (qual.ptr, "req") == 0)
518 formal->type = FORMAL_REQUIRED;
519 else if (strcmp (qual.ptr, "vararg") == 0)
520 formal->type = FORMAL_VARARG;
522 as_bad_where (macro->file,
524 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
529 idx = sb_skip_white (idx, in);
531 if (idx < in->len && in->ptr[idx] == '=')
534 idx = get_any_string (idx + 1, in, &formal->def);
535 idx = sb_skip_white (idx, in);
536 if (formal->type == FORMAL_REQUIRED)
538 sb_reset (&formal->def);
539 as_warn_where (macro->file,
541 _("Pointless default value for required parameter `%s' in macro `%s'"),
547 /* Add to macro's hash table. */
548 if (! hash_find (macro->formal_hash, name))
549 hash_jam (macro->formal_hash, name, formal);
551 as_bad_where (macro->file,
553 _("A parameter named `%s' already exists for macro `%s'"),
557 formal->index = macro->formal_count++;
560 if (formal->type == FORMAL_VARARG)
563 idx = sb_skip_comma (idx, in);
564 if (idx != cidx && idx >= in->len)
573 formal_entry *formal = new_formal ();
575 /* Add a special NARG formal, which macro_expand will set to the
576 number of arguments. */
577 /* The same MRI assemblers which treat '@' characters also use
578 the name $NARG. At least until we find an exception. */
584 sb_add_string (&formal->name, name);
586 /* Add to macro's hash table. */
587 if (hash_find (macro->formal_hash, name))
588 as_bad_where (macro->file,
590 _("Reserved word `%s' used as parameter in macro `%s'"),
593 hash_jam (macro->formal_hash, name, formal);
595 formal->index = NARG_INDEX;
602 /* Free the memory allocated to a macro. */
605 free_macro (macro_entry *macro)
607 formal_entry *formal;
609 for (formal = macro->formals; formal; )
614 formal = formal->next;
617 hash_die (macro->formal_hash);
618 sb_kill (¯o->sub);
622 /* Define a new macro. Returns NULL on success, otherwise returns an
623 error message. If NAMEP is not NULL, *NAMEP is set to the name of
624 the macro which was defined. */
627 define_macro (size_t idx, sb *in, sb *label,
628 size_t (*get_line) (sb *),
629 char *file, unsigned int line,
634 const char *error = NULL;
636 macro = (macro_entry *) xmalloc (sizeof (macro_entry));
637 sb_new (¯o->sub);
642 macro->formal_count = 0;
644 macro->formal_hash = hash_new_sized (7);
646 idx = sb_skip_white (idx, in);
647 if (! buffer_and_nest ("MACRO", "ENDM", ¯o->sub, get_line))
648 error = _("unexpected end of file in macro `%s' definition");
649 if (label != NULL && label->len != 0)
651 sb_add_sb (&name, label);
652 macro->name = sb_terminate (&name);
653 if (idx < in->len && in->ptr[idx] == '(')
655 /* It's the label: MACRO (formals,...) sort */
656 idx = do_formals (macro, idx + 1, in);
657 if (idx < in->len && in->ptr[idx] == ')')
658 idx = sb_skip_white (idx + 1, in);
660 error = _("missing `)' after formals in macro definition `%s'");
664 /* It's the label: MACRO formals,... sort */
665 idx = do_formals (macro, idx, in);
672 idx = get_token (idx, in, &name);
673 macro->name = sb_terminate (&name);
675 error = _("Missing macro name");
676 cidx = sb_skip_white (idx, in);
677 idx = sb_skip_comma (cidx, in);
678 if (idx == cidx || idx < in->len)
679 idx = do_formals (macro, idx, in);
683 if (!error && idx < in->len)
684 error = _("Bad parameter list for macro `%s'");
686 /* And stick it in the macro hash table. */
687 for (idx = 0; idx < name.len; idx++)
688 name.ptr[idx] = TOLOWER (name.ptr[idx]);
689 if (hash_find (macro_hash, macro->name))
690 error = _("Macro `%s' was already defined");
692 error = hash_jam (macro_hash, macro->name, (void *) macro);
695 *namep = macro->name;
705 /* Scan a token, and then skip KIND. */
708 get_apost_token (size_t idx, sb *in, sb *name, int kind)
710 idx = get_token (idx, in, name);
712 && in->ptr[idx] == kind
713 && (! macro_mri || macro_strip_at)
714 && (! macro_strip_at || kind == '@'))
719 /* Substitute the actual value for a formal parameter. */
722 sub_actual (size_t start, sb *in, sb *t, struct hash_control *formal_hash,
723 int kind, sb *out, int copyifnotthere)
728 src = get_apost_token (start, in, t, kind);
729 /* See if it's in the macro's hash table, unless this is
730 macro_strip_at and kind is '@' and the token did not end in '@'. */
733 && (src == start || in->ptr[src - 1] != '@'))
736 ptr = (formal_entry *) hash_find (formal_hash, sb_terminate (t));
741 sb_add_sb (out, &ptr->actual);
745 sb_add_sb (out, &ptr->def);
748 else if (kind == '&')
750 /* Doing this permits people to use & in macro bodies. */
751 sb_add_char (out, '&');
753 if (src != start && in->ptr[src - 1] == '&')
754 sb_add_char (out, '&');
756 else if (copyifnotthere)
762 sb_add_char (out, '\\');
768 /* Expand the body of a macro. */
771 macro_expand_body (sb *in, sb *out, formal_entry *formals,
772 struct hash_control *formal_hash, const macro_entry *macro)
776 int inquote = 0, macro_line = 0;
777 formal_entry *loclist = NULL;
778 const char *err = NULL;
782 while (src < in->len && !err)
784 if (in->ptr[src] == '&')
789 if (src + 1 < in->len && in->ptr[src + 1] == '&')
790 src = sub_actual (src + 2, in, &t, formal_hash, '\'', out, 1);
792 sb_add_char (out, in->ptr[src++]);
796 /* Permit macro parameter substition delineated with
797 an '&' prefix and optional '&' suffix. */
798 src = sub_actual (src + 1, in, &t, formal_hash, '&', out, 0);
801 else if (in->ptr[src] == '\\')
804 if (src < in->len && in->ptr[src] == '(')
806 /* Sub in till the next ')' literally. */
808 while (src < in->len && in->ptr[src] != ')')
810 sb_add_char (out, in->ptr[src++]);
815 err = _("missing `)'");
817 as_bad_where (macro->file, macro->line + macro_line, _("missing `)'"));
819 else if (src < in->len && in->ptr[src] == '@')
821 /* Sub in the macro invocation number. */
825 sprintf (buffer, "%d", macro_number);
826 sb_add_string (out, buffer);
828 else if (src < in->len && in->ptr[src] == '&')
830 /* This is a preprocessor variable name, we don't do them
832 sb_add_char (out, '\\');
833 sb_add_char (out, '&');
836 else if (macro_mri && src < in->len && ISALNUM (in->ptr[src]))
841 if (ISDIGIT (in->ptr[src]))
842 ind = in->ptr[src] - '0';
843 else if (ISUPPER (in->ptr[src]))
844 ind = in->ptr[src] - 'A' + 10;
846 ind = in->ptr[src] - 'a' + 10;
848 for (f = formals; f != NULL; f = f->next)
850 if (f->index == ind - 1)
852 if (f->actual.len != 0)
853 sb_add_sb (out, &f->actual);
855 sb_add_sb (out, &f->def);
863 src = sub_actual (src, in, &t, formal_hash, '\'', out, 0);
866 else if ((macro_alternate || macro_mri)
867 && is_name_beginner (in->ptr[src])
870 || (src > 0 && in->ptr[src - 1] == '@')))
873 || src + 5 >= in->len
874 || strncasecmp (in->ptr + src, "LOCAL", 5) != 0
875 || ! ISWHITE (in->ptr[src + 5])
876 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
880 src = sub_actual (src, in, &t, formal_hash,
881 (macro_strip_at && inquote) ? '@' : '\'',
886 src = sb_skip_white (src + 5, in);
887 while (in->ptr[src] != '\n')
890 formal_entry *f = new_formal ();
892 src = get_token (src, in, &f->name);
893 name = sb_terminate (&f->name);
894 if (! hash_find (formal_hash, name))
899 f->index = LOCAL_INDEX;
903 sprintf (buf, IS_ELF ? ".LL%04x" : "LL%04x", ++loccnt);
904 sb_add_string (&f->actual, buf);
906 err = hash_jam (formal_hash, name, f);
912 as_bad_where (macro->file,
913 macro->line + macro_line,
914 _("`%s' was already used as parameter (or another local) name"),
919 src = sb_skip_comma (src, in);
923 else if (in->ptr[src] == '"'
924 || (macro_mri && in->ptr[src] == '\''))
927 sb_add_char (out, in->ptr[src++]);
929 else if (in->ptr[src] == '@' && macro_strip_at)
933 && in->ptr[src] == '@')
935 sb_add_char (out, '@');
940 && in->ptr[src] == '='
942 && in->ptr[src + 1] == '=')
947 src = get_token (src + 2, in, &t);
948 ptr = (formal_entry *) hash_find (formal_hash, sb_terminate (&t));
951 /* FIXME: We should really return a warning string here,
952 but we can't, because the == might be in the MRI
953 comment field, and, since the nature of the MRI
954 comment field depends upon the exact instruction
955 being used, we don't have enough information here to
956 figure out whether it is or not. Instead, we leave
957 the == in place, which should cause a syntax error if
958 it is not in a comment. */
959 sb_add_char (out, '=');
960 sb_add_char (out, '=');
967 sb_add_string (out, "-1");
971 sb_add_char (out, '0');
977 if (in->ptr[src] == '\n')
979 sb_add_char (out, in->ptr[src++]);
985 while (loclist != NULL)
991 name = sb_terminate (&loclist->name);
992 hash_delete (formal_hash, name, f == NULL);
993 del_formal (loclist);
1000 /* Assign values to the formal parameters of a macro, and expand the
1004 macro_expand (size_t idx, sb *in, macro_entry *m, sb *out)
1011 const char *err = NULL;
1015 /* Reset any old value the actuals may have. */
1016 for (f = m->formals; f; f = f->next)
1017 sb_reset (&f->actual);
1019 while (f != NULL && f->index < 0)
1024 /* The macro may be called with an optional qualifier, which may
1025 be referred to in the macro body as \0. */
1026 if (idx < in->len && in->ptr[idx] == '.')
1028 /* The Microtec assembler ignores this if followed by a white space.
1029 (Macro invocation with empty extension) */
1032 && in->ptr[idx] != ' '
1033 && in->ptr[idx] != '\t')
1035 formal_entry *n = new_formal ();
1037 n->index = QUAL_INDEX;
1039 n->next = m->formals;
1042 idx = get_any_string (idx, in, &n->actual);
1047 /* Peel off the actuals and store them away in the hash tables' actuals. */
1048 idx = sb_skip_white (idx, in);
1049 while (idx < in->len)
1053 /* Look and see if it's a positional or keyword arg. */
1055 while (scan < in->len
1056 && !ISSEP (in->ptr[scan])
1057 && !(macro_mri && in->ptr[scan] == '\'')
1058 && (!macro_alternate && in->ptr[scan] != '='))
1060 if (scan < in->len && !macro_alternate && in->ptr[scan] == '=')
1064 /* It's OK to go from positional to keyword. */
1066 /* This is a keyword arg, fetch the formal name and
1067 then the actual stuff. */
1069 idx = get_token (idx, in, &t);
1070 if (in->ptr[idx] != '=')
1072 err = _("confusion in formal parameters");
1076 /* Lookup the formal in the macro's list. */
1077 ptr = (formal_entry *) hash_find (m->formal_hash, sb_terminate (&t));
1080 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1084 idx = get_any_string (idx + 1, in, &t);
1088 /* Insert this value into the right place. */
1089 if (ptr->actual.len)
1091 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1094 sb_reset (&ptr->actual);
1096 idx = get_any_string (idx + 1, in, &ptr->actual);
1097 if (ptr->actual.len > 0)
1105 err = _("can't mix positional and keyword arguments");
1116 err = _("too many positional arguments");
1123 for (pf = &m->formals; *pf != NULL; pf = &(*pf)->next)
1124 if ((*pf)->index >= c)
1125 c = (*pf)->index + 1;
1132 if (f->type != FORMAL_VARARG)
1133 idx = get_any_string (idx, in, &f->actual);
1136 sb_add_buffer (&f->actual, in->ptr + idx, in->len - idx);
1139 if (f->actual.len > 0)
1145 while (f != NULL && f->index < 0);
1149 idx = sb_skip_comma (idx, in);
1152 if (in->ptr[idx] == ',')
1154 if (ISWHITE (in->ptr[idx]))
1161 for (ptr = m->formals; ptr; ptr = ptr->next)
1163 if (ptr->type == FORMAL_REQUIRED && ptr->actual.len == 0)
1164 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1174 sb_add_string (&t, macro_strip_at ? "$NARG" : "NARG");
1175 ptr = (formal_entry *) hash_find (m->formal_hash, sb_terminate (&t));
1176 sprintf (buffer, "%d", narg);
1177 sb_add_string (&ptr->actual, buffer);
1180 err = macro_expand_body (&m->sub, out, m->formals, m->formal_hash, m);
1183 /* Discard any unnamed formal arguments. */
1191 if ((*pf)->name.len != 0)
1209 /* Check for a macro. If one is found, put the expansion into
1210 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1213 check_macro (const char *line, sb *expand,
1214 const char **error, macro_entry **info)
1221 if (! is_name_beginner (*line)
1222 && (! macro_mri || *line != '.'))
1226 while (is_part_of_name (*s))
1228 if (is_name_ender (*s))
1231 copy = (char *) alloca (s - line + 1);
1232 memcpy (copy, line, s - line);
1233 copy[s - line] = '\0';
1234 for (cls = copy; *cls != '\0'; cls ++)
1235 *cls = TOLOWER (*cls);
1237 macro = (macro_entry *) hash_find (macro_hash, copy);
1242 /* Wrap the line up in an sb. */
1244 while (*s != '\0' && *s != '\n' && *s != '\r')
1245 sb_add_char (&line_sb, *s++);
1248 *error = macro_expand (0, &line_sb, macro, expand);
1252 /* Export the macro information if requested. */
1259 /* Delete a macro. */
1262 delete_macro (const char *name)
1268 len = strlen (name);
1269 copy = (char *) alloca (len + 1);
1270 for (i = 0; i < len; ++i)
1271 copy[i] = TOLOWER (name[i]);
1274 /* We can only ask hash_delete to free memory if we are deleting
1275 macros in reverse order to their definition.
1276 So just clear out the entry. */
1277 if ((macro = (macro_entry *) hash_find (macro_hash, copy)) != NULL)
1279 hash_jam (macro_hash, copy, NULL);
1283 as_warn (_("Attempt to purge non-existant macro `%s'"), copy);
1286 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1287 combined macro definition and execution. This returns NULL on
1288 success, or an error message otherwise. */
1291 expand_irp (int irpc, size_t idx, sb *in, sb *out, size_t (*get_line) (sb *))
1295 struct hash_control *h;
1298 idx = sb_skip_white (idx, in);
1301 if (! buffer_and_nest (NULL, "ENDR", &sub, get_line))
1302 return _("unexpected end of file in irp or irpc");
1308 idx = get_token (idx, in, &f.name);
1309 if (f.name.len == 0)
1310 return _("missing model parameter");
1313 err = hash_jam (h, sb_terminate (&f.name), &f);
1319 f.type = FORMAL_OPTIONAL;
1323 idx = sb_skip_comma (idx, in);
1326 /* Expand once with a null string. */
1327 err = macro_expand_body (&sub, out, &f, h, 0);
1331 bfd_boolean in_quotes = FALSE;
1333 if (irpc && in->ptr[idx] == '"')
1339 while (idx < in->len)
1342 idx = get_any_string (idx, in, &f.actual);
1345 if (in->ptr[idx] == '"')
1350 in_quotes = ! in_quotes;
1352 nxt = sb_skip_white (idx + 1, in);
1359 sb_reset (&f.actual);
1360 sb_add_char (&f.actual, in->ptr[idx]);
1364 err = macro_expand_body (&sub, out, &f, h, 0);
1368 idx = sb_skip_comma (idx, in);
1369 else if (! in_quotes)
1370 idx = sb_skip_white (idx, in);
1375 sb_kill (&f.actual);