1 /* macro.c - macro support for gas
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 Written by Steve and Judy Chamberlain of Cygnus Support,
8 This file is part of GAS, the GNU Assembler.
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
26 #include "safe-ctype.h"
30 /* The routines in this file handle macro definition and expansion.
31 They are called by gas. */
33 #define ISWHITE(x) ((x) == ' ' || (x) == '\t')
36 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
37 || (x) == ')' || (x) == '(' \
38 || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
41 ((x) == 'b' || (x) == 'B' \
42 || (x) == 'q' || (x) == 'Q' \
43 || (x) == 'h' || (x) == 'H' \
44 || (x) == 'd' || (x) == 'D')
46 /* The macro hash table. */
48 struct hash_control
*macro_hash
;
50 /* Whether any macros have been defined. */
54 /* Whether we are in alternate syntax mode. */
56 static int macro_alternate
;
58 /* Whether we are in MRI mode. */
62 /* Whether we should strip '@' characters. */
64 static int macro_strip_at
;
66 /* Function to use to parse an expression. */
68 static int (*macro_expr
) (const char *, int, sb
*, int *);
70 /* Number of macro expansions that have been done. */
72 static int macro_number
;
74 /* Initialize macro processing. */
77 macro_init (int alternate
, int mri
, int strip_at
,
78 int (*exp
) (const char *, int, sb
*, int *))
80 macro_hash
= hash_new ();
82 macro_alternate
= alternate
;
84 macro_strip_at
= strip_at
;
88 /* Switch in and out of alternate mode on the fly. */
91 macro_set_alternate (int alternate
)
93 macro_alternate
= alternate
;
96 /* Switch in and out of MRI mode on the fly. */
99 macro_mri_mode (int 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 int (*get_line
) (sb
*))
116 int to_len
= strlen (to
);
118 int line_start
= ptr
->len
;
120 int more
= get_line (ptr
);
122 if (to_len
== 4 && strcasecmp(to
, "ENDR") == 0)
128 from_len
= strlen (from
);
132 /* Try to find the first pseudo op on the line. */
134 bfd_boolean had_colon
= FALSE
;
136 /* With normal syntax we can suck what we want till we get
137 to the dot. With the alternate, labels have to start in
138 the first column, since we can't tell what's a label and
139 what's a pseudoop. */
141 if (! LABELS_WITHOUT_COLONS
)
143 /* Skip leading whitespace. */
144 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
150 /* Skip over a label, if any. */
151 if (i
>= ptr
->len
|| ! is_name_beginner (ptr
->ptr
[i
]))
154 while (i
< ptr
->len
&& is_part_of_name (ptr
->ptr
[i
]))
156 if (i
< ptr
->len
&& is_name_ender (ptr
->ptr
[i
]))
158 /* Skip whitespace. */
159 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
161 /* Check for the colon. */
162 if (i
>= ptr
->len
|| ptr
->ptr
[i
] != ':')
164 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
165 colon after a label. If we do have a colon on the
166 first label then handle more than one label on the
167 line, assuming that each label has a colon. */
168 if (LABELS_WITHOUT_COLONS
&& !had_colon
)
178 /* Skip trailing whitespace. */
179 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
182 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
186 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
189 && strncasecmp (ptr
->ptr
+ i
, "IRPC", from_len
= 4) != 0
190 && strncasecmp (ptr
->ptr
+ i
, "IRP", from_len
= 3) != 0
191 && strncasecmp (ptr
->ptr
+ i
, "IREPC", from_len
= 5) != 0
192 && strncasecmp (ptr
->ptr
+ i
, "IREP", from_len
= 4) != 0
193 && strncasecmp (ptr
->ptr
+ i
, "REPT", from_len
= 4) != 0
194 && strncasecmp (ptr
->ptr
+ i
, "REP", from_len
= 3) != 0)
197 ? strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0
199 && (ptr
->len
== (i
+ from_len
)
200 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
201 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
203 if (strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
204 && (ptr
->len
== (i
+ to_len
)
205 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
206 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
211 /* Reset the string to not include the ending rune. */
212 ptr
->len
= line_start
;
218 /* Add the original end-of-line char to the end and keep running. */
219 sb_add_char (ptr
, more
);
220 line_start
= ptr
->len
;
221 more
= get_line (ptr
);
224 /* Return 1 on success, 0 on unexpected EOF. */
228 /* Pick up a token. */
231 get_token (int idx
, sb
*in
, sb
*name
)
234 && is_name_beginner (in
->ptr
[idx
]))
236 sb_add_char (name
, in
->ptr
[idx
++]);
238 && is_part_of_name (in
->ptr
[idx
]))
240 sb_add_char (name
, in
->ptr
[idx
++]);
243 && is_name_ender (in
->ptr
[idx
]))
245 sb_add_char (name
, in
->ptr
[idx
++]);
248 /* Ignore trailing &. */
249 if (macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
254 /* Pick up a string. */
257 getstring (int idx
, sb
*in
, sb
*acc
)
260 && (in
->ptr
[idx
] == '"'
261 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
262 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
264 if (in
->ptr
[idx
] == '<')
268 while ((in
->ptr
[idx
] != '>' || nest
)
271 if (in
->ptr
[idx
] == '!')
274 sb_add_char (acc
, in
->ptr
[idx
++]);
278 if (in
->ptr
[idx
] == '>')
280 if (in
->ptr
[idx
] == '<')
282 sb_add_char (acc
, in
->ptr
[idx
++]);
287 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
289 char tchar
= in
->ptr
[idx
];
294 while (idx
< in
->len
)
296 if (in
->ptr
[idx
- 1] == '\\')
301 if (macro_alternate
&& in
->ptr
[idx
] == '!')
305 sb_add_char (acc
, in
->ptr
[idx
]);
309 else if (escaped
&& in
->ptr
[idx
] == tchar
)
311 sb_add_char (acc
, tchar
);
316 if (in
->ptr
[idx
] == tchar
)
320 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
324 sb_add_char (acc
, in
->ptr
[idx
]);
334 /* Fetch string from the input stream,
336 'Bxyx<whitespace> -> return 'Bxyza
337 %<expr> -> return string of decimal value of <expr>
338 "string" -> return string
339 (string) -> return (string-including-whitespaces)
340 xyx<whitespace> -> return xyz. */
343 get_any_string (int idx
, sb
*in
, sb
*out
)
346 idx
= sb_skip_white (idx
, in
);
350 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
352 while (!ISSEP (in
->ptr
[idx
]))
353 sb_add_char (out
, in
->ptr
[idx
++]);
355 else if (in
->ptr
[idx
] == '%' && macro_alternate
)
360 /* Turns the next expression into a string. */
361 /* xgettext: no-c-format */
362 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
366 sprintf (buf
, "%d", val
);
367 sb_add_string (out
, buf
);
369 else if (in
->ptr
[idx
] == '"'
370 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
371 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
373 if (macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
375 /* Keep the quotes. */
376 sb_add_char (out
, '"');
377 idx
= getstring (idx
, in
, out
);
378 sb_add_char (out
, '"');
382 idx
= getstring (idx
, in
, out
);
387 char *br_buf
= (char *) xmalloc(1);
388 char *in_br
= br_buf
;
393 || (in
->ptr
[idx
] != ' '
394 && in
->ptr
[idx
] != '\t'))
395 && in
->ptr
[idx
] != ','
396 && (in
->ptr
[idx
] != '<'
397 || (! macro_alternate
&& ! macro_mri
)))
399 char tchar
= in
->ptr
[idx
];
405 sb_add_char (out
, in
->ptr
[idx
++]);
407 && in
->ptr
[idx
] != tchar
)
408 sb_add_char (out
, in
->ptr
[idx
++]);
418 br_buf
= (char *) xmalloc(strlen(in_br
) + 2);
419 strcpy(br_buf
+ 1, in_br
);
434 sb_add_char (out
, tchar
);
444 /* Allocate a new formal. */
446 static formal_entry
*
449 formal_entry
*formal
;
451 formal
= (formal_entry
*) xmalloc (sizeof (formal_entry
));
453 sb_new (&formal
->name
);
454 sb_new (&formal
->def
);
455 sb_new (&formal
->actual
);
457 formal
->type
= FORMAL_OPTIONAL
;
464 del_formal (formal_entry
*formal
)
466 sb_kill (&formal
->actual
);
467 sb_kill (&formal
->def
);
468 sb_kill (&formal
->name
);
472 /* Pick up the formal parameters of a macro definition. */
475 do_formals (macro_entry
*macro
, int idx
, sb
*in
)
477 formal_entry
**p
= ¯o
->formals
;
480 idx
= sb_skip_white (idx
, in
);
481 while (idx
< in
->len
)
483 formal_entry
*formal
= new_formal ();
486 idx
= get_token (idx
, in
, &formal
->name
);
487 if (formal
->name
.len
== 0)
489 if (macro
->formal_count
)
493 idx
= sb_skip_white (idx
, in
);
494 /* This is a formal. */
495 name
= sb_terminate (&formal
->name
);
498 && in
->ptr
[idx
] == ':'
499 && (! is_name_beginner (':')
500 || idx
+ 1 >= in
->len
501 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
503 /* Got a qualifier. */
507 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
508 sb_terminate (&qual
);
510 as_bad_where (macro
->file
,
512 _("Missing parameter qualifier for `%s' in macro `%s'"),
515 else if (strcmp (qual
.ptr
, "req") == 0)
516 formal
->type
= FORMAL_REQUIRED
;
517 else if (strcmp (qual
.ptr
, "vararg") == 0)
518 formal
->type
= FORMAL_VARARG
;
520 as_bad_where (macro
->file
,
522 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
527 idx
= sb_skip_white (idx
, in
);
529 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
532 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
533 idx
= sb_skip_white (idx
, in
);
534 if (formal
->type
== FORMAL_REQUIRED
)
536 sb_reset (&formal
->def
);
537 as_warn_where (macro
->file
,
539 _("Pointless default value for required parameter `%s' in macro `%s'"),
545 /* Add to macro's hash table. */
546 if (! hash_find (macro
->formal_hash
, name
))
547 hash_jam (macro
->formal_hash
, name
, formal
);
549 as_bad_where (macro
->file
,
551 _("A parameter named `%s' already exists for macro `%s'"),
555 formal
->index
= macro
->formal_count
++;
558 if (formal
->type
== FORMAL_VARARG
)
561 idx
= sb_skip_comma (idx
, in
);
562 if (idx
!= cidx
&& idx
>= in
->len
)
571 formal_entry
*formal
= new_formal ();
573 /* Add a special NARG formal, which macro_expand will set to the
574 number of arguments. */
575 /* The same MRI assemblers which treat '@' characters also use
576 the name $NARG. At least until we find an exception. */
582 sb_add_string (&formal
->name
, name
);
584 /* Add to macro's hash table. */
585 if (hash_find (macro
->formal_hash
, name
))
586 as_bad_where (macro
->file
,
588 _("Reserved word `%s' used as parameter in macro `%s'"),
591 hash_jam (macro
->formal_hash
, name
, formal
);
593 formal
->index
= NARG_INDEX
;
600 /* Free the memory allocated to a macro. */
603 free_macro (macro_entry
*macro
)
605 formal_entry
*formal
;
607 for (formal
= macro
->formals
; formal
; )
612 formal
= formal
->next
;
615 hash_die (macro
->formal_hash
);
616 sb_kill (¯o
->sub
);
620 /* Define a new macro. Returns NULL on success, otherwise returns an
621 error message. If NAMEP is not NULL, *NAMEP is set to the name of
622 the macro which was defined. */
625 define_macro (int idx
, sb
*in
, sb
*label
,
626 int (*get_line
) (sb
*),
627 char *file
, unsigned int line
,
632 const char *error
= NULL
;
634 macro
= (macro_entry
*) xmalloc (sizeof (macro_entry
));
635 sb_new (¯o
->sub
);
640 macro
->formal_count
= 0;
642 macro
->formal_hash
= hash_new ();
644 idx
= sb_skip_white (idx
, in
);
645 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
646 error
= _("unexpected end of file in macro `%s' definition");
647 if (label
!= NULL
&& label
->len
!= 0)
649 sb_add_sb (&name
, label
);
650 macro
->name
= sb_terminate (&name
);
651 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
653 /* It's the label: MACRO (formals,...) sort */
654 idx
= do_formals (macro
, idx
+ 1, in
);
655 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
656 idx
= sb_skip_white (idx
+ 1, in
);
658 error
= _("missing `)' after formals in macro definition `%s'");
662 /* It's the label: MACRO formals,... sort */
663 idx
= do_formals (macro
, idx
, in
);
670 idx
= get_token (idx
, in
, &name
);
671 macro
->name
= sb_terminate (&name
);
673 error
= _("Missing macro name");
674 cidx
= sb_skip_white (idx
, in
);
675 idx
= sb_skip_comma (cidx
, in
);
676 if (idx
== cidx
|| idx
< in
->len
)
677 idx
= do_formals (macro
, idx
, in
);
681 if (!error
&& idx
< in
->len
)
682 error
= _("Bad parameter list for macro `%s'");
684 /* And stick it in the macro hash table. */
685 for (idx
= 0; idx
< name
.len
; idx
++)
686 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
687 if (hash_find (macro_hash
, macro
->name
))
688 error
= _("Macro `%s' was already defined");
690 error
= hash_jam (macro_hash
, macro
->name
, (void *) macro
);
693 *namep
= macro
->name
;
703 /* Scan a token, and then skip KIND. */
706 get_apost_token (int idx
, sb
*in
, sb
*name
, int kind
)
708 idx
= get_token (idx
, in
, name
);
710 && in
->ptr
[idx
] == kind
711 && (! macro_mri
|| macro_strip_at
)
712 && (! macro_strip_at
|| kind
== '@'))
717 /* Substitute the actual value for a formal parameter. */
720 sub_actual (int start
, sb
*in
, sb
*t
, struct hash_control
*formal_hash
,
721 int kind
, sb
*out
, int copyifnotthere
)
726 src
= get_apost_token (start
, in
, t
, kind
);
727 /* See if it's in the macro's hash table, unless this is
728 macro_strip_at and kind is '@' and the token did not end in '@'. */
731 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
734 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (t
));
739 sb_add_sb (out
, &ptr
->actual
);
743 sb_add_sb (out
, &ptr
->def
);
746 else if (kind
== '&')
748 /* Doing this permits people to use & in macro bodies. */
749 sb_add_char (out
, '&');
752 else if (copyifnotthere
)
758 sb_add_char (out
, '\\');
764 /* Expand the body of a macro. */
767 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
768 struct hash_control
*formal_hash
, const macro_entry
*macro
)
771 int src
= 0, inquote
= 0, macro_line
= 0;
772 formal_entry
*loclist
= NULL
;
773 const char *err
= NULL
;
777 while (src
< in
->len
&& !err
)
779 if (in
->ptr
[src
] == '&')
784 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
785 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
787 sb_add_char (out
, in
->ptr
[src
++]);
791 /* FIXME: Why do we do this? */
792 /* At least in alternate mode this seems correct; without this
793 one can't append a literal to a parameter. */
794 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
797 else if (in
->ptr
[src
] == '\\')
800 if (src
< in
->len
&& in
->ptr
[src
] == '(')
802 /* Sub in till the next ')' literally. */
804 while (src
< in
->len
&& in
->ptr
[src
] != ')')
806 sb_add_char (out
, in
->ptr
[src
++]);
811 err
= _("missing `)'");
813 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
815 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
817 /* Sub in the macro invocation number. */
821 sprintf (buffer
, "%d", macro_number
);
822 sb_add_string (out
, buffer
);
824 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
826 /* This is a preprocessor variable name, we don't do them
828 sb_add_char (out
, '\\');
829 sb_add_char (out
, '&');
832 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
837 if (ISDIGIT (in
->ptr
[src
]))
838 ind
= in
->ptr
[src
] - '0';
839 else if (ISUPPER (in
->ptr
[src
]))
840 ind
= in
->ptr
[src
] - 'A' + 10;
842 ind
= in
->ptr
[src
] - 'a' + 10;
844 for (f
= formals
; f
!= NULL
; f
= f
->next
)
846 if (f
->index
== ind
- 1)
848 if (f
->actual
.len
!= 0)
849 sb_add_sb (out
, &f
->actual
);
851 sb_add_sb (out
, &f
->def
);
859 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
862 else if ((macro_alternate
|| macro_mri
)
863 && is_name_beginner (in
->ptr
[src
])
866 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
869 || src
+ 5 >= in
->len
870 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
871 || ! ISWHITE (in
->ptr
[src
+ 5])
872 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
876 src
= sub_actual (src
, in
, &t
, formal_hash
,
877 (macro_strip_at
&& inquote
) ? '@' : '\'',
882 src
= sb_skip_white (src
+ 5, in
);
883 while (in
->ptr
[src
] != '\n')
886 formal_entry
*f
= new_formal ();
888 src
= get_token (src
, in
, &f
->name
);
889 name
= sb_terminate (&f
->name
);
890 if (! hash_find (formal_hash
, name
))
895 f
->index
= LOCAL_INDEX
;
899 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
900 sb_add_string (&f
->actual
, buf
);
902 err
= hash_jam (formal_hash
, name
, f
);
908 as_bad_where (macro
->file
,
909 macro
->line
+ macro_line
,
910 _("`%s' was already used as parameter (or another local) name"),
915 src
= sb_skip_comma (src
, in
);
919 else if (in
->ptr
[src
] == '"'
920 || (macro_mri
&& in
->ptr
[src
] == '\''))
923 sb_add_char (out
, in
->ptr
[src
++]);
925 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
929 && in
->ptr
[src
] == '@')
931 sb_add_char (out
, '@');
936 && in
->ptr
[src
] == '='
938 && in
->ptr
[src
+ 1] == '=')
943 src
= get_token (src
+ 2, in
, &t
);
944 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (&t
));
947 /* FIXME: We should really return a warning string here,
948 but we can't, because the == might be in the MRI
949 comment field, and, since the nature of the MRI
950 comment field depends upon the exact instruction
951 being used, we don't have enough information here to
952 figure out whether it is or not. Instead, we leave
953 the == in place, which should cause a syntax error if
954 it is not in a comment. */
955 sb_add_char (out
, '=');
956 sb_add_char (out
, '=');
963 sb_add_string (out
, "-1");
967 sb_add_char (out
, '0');
973 if (in
->ptr
[src
] == '\n')
975 sb_add_char (out
, in
->ptr
[src
++]);
981 while (loclist
!= NULL
)
987 name
= sb_terminate (&loclist
->name
);
988 hash_delete (formal_hash
, name
, f
== NULL
);
989 del_formal (loclist
);
996 /* Assign values to the formal parameters of a macro, and expand the
1000 macro_expand (int idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1007 const char *err
= NULL
;
1011 /* Reset any old value the actuals may have. */
1012 for (f
= m
->formals
; f
; f
= f
->next
)
1013 sb_reset (&f
->actual
);
1015 while (f
!= NULL
&& f
->index
< 0)
1020 /* The macro may be called with an optional qualifier, which may
1021 be referred to in the macro body as \0. */
1022 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1024 /* The Microtec assembler ignores this if followed by a white space.
1025 (Macro invocation with empty extension) */
1028 && in
->ptr
[idx
] != ' '
1029 && in
->ptr
[idx
] != '\t')
1031 formal_entry
*n
= new_formal ();
1033 n
->index
= QUAL_INDEX
;
1035 n
->next
= m
->formals
;
1038 idx
= get_any_string (idx
, in
, &n
->actual
);
1043 /* Peel off the actuals and store them away in the hash tables' actuals. */
1044 idx
= sb_skip_white (idx
, in
);
1045 while (idx
< in
->len
)
1049 /* Look and see if it's a positional or keyword arg. */
1051 while (scan
< in
->len
1052 && !ISSEP (in
->ptr
[scan
])
1053 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1054 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1056 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1060 /* It's OK to go from positional to keyword. */
1062 /* This is a keyword arg, fetch the formal name and
1063 then the actual stuff. */
1065 idx
= get_token (idx
, in
, &t
);
1066 if (in
->ptr
[idx
] != '=')
1068 err
= _("confusion in formal parameters");
1072 /* Lookup the formal in the macro's list. */
1073 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1075 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1080 /* Insert this value into the right place. */
1081 if (ptr
->actual
.len
)
1083 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1086 sb_reset (&ptr
->actual
);
1088 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1089 if (ptr
->actual
.len
> 0)
1097 err
= _("can't mix positional and keyword arguments");
1108 err
= _("too many positional arguments");
1115 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1116 if ((*pf
)->index
>= c
)
1117 c
= (*pf
)->index
+ 1;
1124 if (f
->type
!= FORMAL_VARARG
)
1125 idx
= get_any_string (idx
, in
, &f
->actual
);
1128 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1131 if (f
->actual
.len
> 0)
1137 while (f
!= NULL
&& f
->index
< 0);
1141 idx
= sb_skip_comma (idx
, in
);
1144 if (in
->ptr
[idx
] == ',')
1146 if (ISWHITE (in
->ptr
[idx
]))
1153 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1155 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1156 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1166 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1167 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1168 sprintf (buffer
, "%d", narg
);
1169 sb_add_string (&ptr
->actual
, buffer
);
1172 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1175 /* Discard any unnamed formal arguments. */
1183 if ((*pf
)->name
.len
!= 0)
1201 /* Check for a macro. If one is found, put the expansion into
1202 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1205 check_macro (const char *line
, sb
*expand
,
1206 const char **error
, macro_entry
**info
)
1213 if (! is_name_beginner (*line
)
1214 && (! macro_mri
|| *line
!= '.'))
1218 while (is_part_of_name (*s
))
1220 if (is_name_ender (*s
))
1223 copy
= (char *) alloca (s
- line
+ 1);
1224 memcpy (copy
, line
, s
- line
);
1225 copy
[s
- line
] = '\0';
1226 for (cls
= copy
; *cls
!= '\0'; cls
++)
1227 *cls
= TOLOWER (*cls
);
1229 macro
= (macro_entry
*) hash_find (macro_hash
, copy
);
1234 /* Wrap the line up in an sb. */
1236 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1237 sb_add_char (&line_sb
, *s
++);
1240 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1244 /* Export the macro information if requested. */
1251 /* Delete a macro. */
1254 delete_macro (const char *name
)
1260 len
= strlen (name
);
1261 copy
= (char *) alloca (len
+ 1);
1262 for (i
= 0; i
< len
; ++i
)
1263 copy
[i
] = TOLOWER (name
[i
]);
1266 /* We can only ask hash_delete to free memory if we are deleting
1267 macros in reverse order to their definition.
1268 So just clear out the entry. */
1269 if ((macro
= (macro_entry
*) hash_find (macro_hash
, copy
)) != NULL
)
1271 hash_jam (macro_hash
, copy
, NULL
);
1275 as_warn (_("Attempt to purge non-existant macro `%s'"), copy
);
1278 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1279 combined macro definition and execution. This returns NULL on
1280 success, or an error message otherwise. */
1283 expand_irp (int irpc
, int idx
, sb
*in
, sb
*out
, int (*get_line
) (sb
*))
1287 struct hash_control
*h
;
1290 idx
= sb_skip_white (idx
, in
);
1293 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1294 return _("unexpected end of file in irp or irpc");
1300 idx
= get_token (idx
, in
, &f
.name
);
1301 if (f
.name
.len
== 0)
1302 return _("missing model parameter");
1305 err
= hash_jam (h
, sb_terminate (&f
.name
), &f
);
1311 f
.type
= FORMAL_OPTIONAL
;
1315 idx
= sb_skip_comma (idx
, in
);
1318 /* Expand once with a null string. */
1319 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1323 bfd_boolean in_quotes
= FALSE
;
1325 if (irpc
&& in
->ptr
[idx
] == '"')
1331 while (idx
< in
->len
)
1334 idx
= get_any_string (idx
, in
, &f
.actual
);
1337 if (in
->ptr
[idx
] == '"')
1342 in_quotes
= ! in_quotes
;
1344 nxt
= sb_skip_white (idx
+ 1, in
);
1351 sb_reset (&f
.actual
);
1352 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1356 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1360 idx
= sb_skip_comma (idx
, in
);
1361 else if (! in_quotes
)
1362 idx
= sb_skip_white (idx
, in
);
1367 sb_kill (&f
.actual
);