1 /* macro.c - macro support for gas
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006 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 2, 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 /* Internal functions. */
35 static int get_token (int, sb
*, sb
*);
36 static int getstring (int, sb
*, sb
*);
37 static int get_any_string (int, sb
*, sb
*);
38 static formal_entry
*new_formal (void);
39 static void del_formal (formal_entry
*);
40 static int do_formals (macro_entry
*, int, sb
*);
41 static int get_apost_token (int, sb
*, sb
*, int);
42 static int sub_actual (int, sb
*, sb
*, struct hash_control
*, int, sb
*, int);
43 static const char *macro_expand_body
44 (sb
*, sb
*, formal_entry
*, struct hash_control
*, const macro_entry
*);
45 static const char *macro_expand (int, sb
*, macro_entry
*, sb
*);
46 static void free_macro(macro_entry
*);
48 #define ISWHITE(x) ((x) == ' ' || (x) == '\t')
51 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
52 || (x) == ')' || (x) == '(' \
53 || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
56 ((x) == 'b' || (x) == 'B' \
57 || (x) == 'q' || (x) == 'Q' \
58 || (x) == 'h' || (x) == 'H' \
59 || (x) == 'd' || (x) == 'D')
61 /* The macro hash table. */
63 struct hash_control
*macro_hash
;
65 /* Whether any macros have been defined. */
69 /* Whether we are in alternate syntax mode. */
71 static int macro_alternate
;
73 /* Whether we are in MRI mode. */
77 /* Whether we should strip '@' characters. */
79 static int macro_strip_at
;
81 /* Function to use to parse an expression. */
83 static int (*macro_expr
) (const char *, int, sb
*, int *);
85 /* Number of macro expansions that have been done. */
87 static int macro_number
;
89 /* Initialize macro processing. */
92 macro_init (int alternate
, int mri
, int strip_at
,
93 int (*expr
) (const char *, int, sb
*, int *))
95 macro_hash
= hash_new ();
97 macro_alternate
= alternate
;
99 macro_strip_at
= strip_at
;
103 /* Switch in and out of alternate mode on the fly. */
106 macro_set_alternate (int alternate
)
108 macro_alternate
= alternate
;
111 /* Switch in and out of MRI mode on the fly. */
114 macro_mri_mode (int mri
)
119 /* Read input lines till we get to a TO string.
120 Increase nesting depth if we get a FROM string.
121 Put the results into sb at PTR.
122 FROM may be NULL (or will be ignored) if TO is "ENDR".
123 Add a new input line to an sb using GET_LINE.
124 Return 1 on success, 0 on unexpected EOF. */
127 buffer_and_nest (const char *from
, const char *to
, sb
*ptr
,
128 int (*get_line
) (sb
*))
131 int to_len
= strlen (to
);
133 int line_start
= ptr
->len
;
135 int more
= get_line (ptr
);
137 if (to_len
== 4 && strcasecmp(to
, "ENDR") == 0)
143 from_len
= strlen (from
);
147 /* Try to find the first pseudo op on the line. */
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
]))
164 /* Skip over a label, if any. */
165 if (i
>= ptr
->len
|| ! is_name_beginner (ptr
->ptr
[i
]))
168 while (i
< ptr
->len
&& is_part_of_name (ptr
->ptr
[i
]))
170 if (i
< ptr
->len
&& is_name_ender (ptr
->ptr
[i
]))
172 if (LABELS_WITHOUT_COLONS
)
174 /* Skip whitespace. */
175 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
177 /* Check for the colon. */
178 if (i
>= ptr
->len
|| ptr
->ptr
[i
] != ':')
187 /* Skip trailing whitespace. */
188 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
191 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
195 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
198 && strncasecmp (ptr
->ptr
+ i
, "IRPC", from_len
= 4) != 0
199 && strncasecmp (ptr
->ptr
+ i
, "IRP", from_len
= 3) != 0
200 && strncasecmp (ptr
->ptr
+ i
, "IREPC", from_len
= 5) != 0
201 && strncasecmp (ptr
->ptr
+ i
, "IREP", from_len
= 4) != 0
202 && strncasecmp (ptr
->ptr
+ i
, "REPT", from_len
= 4) != 0
203 && strncasecmp (ptr
->ptr
+ i
, "REP", from_len
= 3) != 0)
206 ? strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0
208 && (ptr
->len
== (i
+ from_len
)
209 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
210 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
212 if (strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
213 && (ptr
->len
== (i
+ to_len
)
214 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
215 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
220 /* Reset the string to not include the ending rune. */
221 ptr
->len
= line_start
;
227 /* Add the original end-of-line char to the end and keep running. */
228 sb_add_char (ptr
, more
);
229 line_start
= ptr
->len
;
230 more
= get_line (ptr
);
233 /* Return 1 on success, 0 on unexpected EOF. */
237 /* Pick up a token. */
240 get_token (int idx
, sb
*in
, sb
*name
)
243 && is_name_beginner (in
->ptr
[idx
]))
245 sb_add_char (name
, in
->ptr
[idx
++]);
247 && is_part_of_name (in
->ptr
[idx
]))
249 sb_add_char (name
, in
->ptr
[idx
++]);
252 && is_name_ender (in
->ptr
[idx
]))
254 sb_add_char (name
, in
->ptr
[idx
++]);
257 /* Ignore trailing &. */
258 if (macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
263 /* Pick up a string. */
266 getstring (int idx
, sb
*in
, sb
*acc
)
269 && (in
->ptr
[idx
] == '"'
270 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
271 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
273 if (in
->ptr
[idx
] == '<')
277 while ((in
->ptr
[idx
] != '>' || nest
)
280 if (in
->ptr
[idx
] == '!')
283 sb_add_char (acc
, in
->ptr
[idx
++]);
287 if (in
->ptr
[idx
] == '>')
289 if (in
->ptr
[idx
] == '<')
291 sb_add_char (acc
, in
->ptr
[idx
++]);
296 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
298 char tchar
= in
->ptr
[idx
];
303 while (idx
< in
->len
)
305 if (in
->ptr
[idx
- 1] == '\\')
310 if (macro_alternate
&& in
->ptr
[idx
] == '!')
314 sb_add_char (acc
, in
->ptr
[idx
]);
318 else if (escaped
&& in
->ptr
[idx
] == tchar
)
320 sb_add_char (acc
, tchar
);
325 if (in
->ptr
[idx
] == tchar
)
329 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
333 sb_add_char (acc
, in
->ptr
[idx
]);
343 /* Fetch string from the input stream,
345 'Bxyx<whitespace> -> return 'Bxyza
346 %<expr> -> return string of decimal value of <expr>
347 "string" -> return string
348 (string) -> return (string-including-whitespaces)
349 xyx<whitespace> -> return xyz. */
352 get_any_string (int idx
, sb
*in
, sb
*out
)
355 idx
= sb_skip_white (idx
, in
);
359 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
361 while (!ISSEP (in
->ptr
[idx
]))
362 sb_add_char (out
, in
->ptr
[idx
++]);
364 else if (in
->ptr
[idx
] == '%' && macro_alternate
)
369 /* Turns the next expression into a string. */
370 /* xgettext: no-c-format */
371 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
375 sprintf (buf
, "%d", val
);
376 sb_add_string (out
, buf
);
378 else if (in
->ptr
[idx
] == '"'
379 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
380 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
382 if (macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
384 /* Keep the quotes. */
385 sb_add_char (out
, '"');
386 idx
= getstring (idx
, in
, out
);
387 sb_add_char (out
, '"');
391 idx
= getstring (idx
, in
, out
);
396 char *br_buf
= xmalloc(1);
397 char *in_br
= br_buf
;
402 || (in
->ptr
[idx
] != ' '
403 && in
->ptr
[idx
] != '\t'))
404 && in
->ptr
[idx
] != ','
405 && (in
->ptr
[idx
] != '<'
406 || (! macro_alternate
&& ! macro_mri
)))
408 char tchar
= in
->ptr
[idx
];
414 sb_add_char (out
, in
->ptr
[idx
++]);
416 && in
->ptr
[idx
] != tchar
)
417 sb_add_char (out
, in
->ptr
[idx
++]);
427 br_buf
= xmalloc(strlen(in_br
) + 2);
428 strcpy(br_buf
+ 1, in_br
);
443 sb_add_char (out
, tchar
);
453 /* Allocate a new formal. */
455 static formal_entry
*
458 formal_entry
*formal
;
460 formal
= xmalloc (sizeof (formal_entry
));
462 sb_new (&formal
->name
);
463 sb_new (&formal
->def
);
464 sb_new (&formal
->actual
);
466 formal
->type
= FORMAL_OPTIONAL
;
473 del_formal (formal_entry
*formal
)
475 sb_kill (&formal
->actual
);
476 sb_kill (&formal
->def
);
477 sb_kill (&formal
->name
);
481 /* Pick up the formal parameters of a macro definition. */
484 do_formals (macro_entry
*macro
, int idx
, sb
*in
)
486 formal_entry
**p
= ¯o
->formals
;
489 idx
= sb_skip_white (idx
, in
);
490 while (idx
< in
->len
)
492 formal_entry
*formal
= new_formal ();
495 idx
= get_token (idx
, in
, &formal
->name
);
496 if (formal
->name
.len
== 0)
498 if (macro
->formal_count
)
502 idx
= sb_skip_white (idx
, in
);
503 /* This is a formal. */
504 name
= sb_terminate (&formal
->name
);
507 && in
->ptr
[idx
] == ':'
508 && (! is_name_beginner (':')
509 || idx
+ 1 >= in
->len
510 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
512 /* Got a qualifier. */
516 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
517 sb_terminate (&qual
);
519 as_bad_where (macro
->file
,
521 _("Missing parameter qualifier for `%s' in macro `%s'"),
524 else if (strcmp (qual
.ptr
, "req") == 0)
525 formal
->type
= FORMAL_REQUIRED
;
526 else if (strcmp (qual
.ptr
, "vararg") == 0)
527 formal
->type
= FORMAL_VARARG
;
529 as_bad_where (macro
->file
,
531 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
536 idx
= sb_skip_white (idx
, in
);
538 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
541 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
542 idx
= sb_skip_white (idx
, in
);
543 if (formal
->type
== FORMAL_REQUIRED
)
545 sb_reset (&formal
->def
);
546 as_warn_where (macro
->file
,
548 _("Pointless default value for required parameter `%s' in macro `%s'"),
554 /* Add to macro's hash table. */
555 if (! hash_find (macro
->formal_hash
, name
))
556 hash_jam (macro
->formal_hash
, name
, formal
);
558 as_bad_where (macro
->file
,
560 _("A parameter named `%s' already exists for macro `%s'"),
564 formal
->index
= macro
->formal_count
++;
567 if (formal
->type
== FORMAL_VARARG
)
570 idx
= sb_skip_comma (idx
, in
);
571 if (idx
!= cidx
&& idx
>= in
->len
)
580 formal_entry
*formal
= new_formal ();
582 /* Add a special NARG formal, which macro_expand will set to the
583 number of arguments. */
584 /* The same MRI assemblers which treat '@' characters also use
585 the name $NARG. At least until we find an exception. */
591 sb_add_string (&formal
->name
, name
);
593 /* Add to macro's hash table. */
594 if (hash_find (macro
->formal_hash
, name
))
595 as_bad_where (macro
->file
,
597 _("Reserved word `%s' used as parameter in macro `%s'"),
600 hash_jam (macro
->formal_hash
, name
, formal
);
602 formal
->index
= NARG_INDEX
;
609 /* Define a new macro. Returns NULL on success, otherwise returns an
610 error message. If NAMEP is not NULL, *NAMEP is set to the name of
611 the macro which was defined. */
614 define_macro (int idx
, sb
*in
, sb
*label
,
615 int (*get_line
) (sb
*),
616 char *file
, unsigned int line
,
621 const char *error
= NULL
;
623 macro
= (macro_entry
*) xmalloc (sizeof (macro_entry
));
624 sb_new (¯o
->sub
);
629 macro
->formal_count
= 0;
631 macro
->formal_hash
= hash_new ();
633 idx
= sb_skip_white (idx
, in
);
634 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
635 error
= _("unexpected end of file in macro `%s' definition");
636 if (label
!= NULL
&& label
->len
!= 0)
638 sb_add_sb (&name
, label
);
639 macro
->name
= sb_terminate (&name
);
640 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
642 /* It's the label: MACRO (formals,...) sort */
643 idx
= do_formals (macro
, idx
+ 1, in
);
644 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
645 idx
= sb_skip_white (idx
+ 1, in
);
647 error
= _("missing `)' after formals in macro definition `%s'");
651 /* It's the label: MACRO formals,... sort */
652 idx
= do_formals (macro
, idx
, in
);
659 idx
= get_token (idx
, in
, &name
);
660 macro
->name
= sb_terminate (&name
);
662 error
= _("Missing macro name");
663 cidx
= sb_skip_white (idx
, in
);
664 idx
= sb_skip_comma (cidx
, in
);
665 if (idx
== cidx
|| idx
< in
->len
)
666 idx
= do_formals (macro
, idx
, in
);
670 if (!error
&& idx
< in
->len
)
671 error
= _("Bad parameter list for macro `%s'");
673 /* And stick it in the macro hash table. */
674 for (idx
= 0; idx
< name
.len
; idx
++)
675 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
676 if (hash_find (macro_hash
, macro
->name
))
677 error
= _("Macro `%s' was already defined");
679 error
= hash_jam (macro_hash
, macro
->name
, (PTR
) macro
);
682 *namep
= macro
->name
;
692 /* Scan a token, and then skip KIND. */
695 get_apost_token (int idx
, sb
*in
, sb
*name
, int kind
)
697 idx
= get_token (idx
, in
, name
);
699 && in
->ptr
[idx
] == kind
700 && (! macro_mri
|| macro_strip_at
)
701 && (! macro_strip_at
|| kind
== '@'))
706 /* Substitute the actual value for a formal parameter. */
709 sub_actual (int start
, sb
*in
, sb
*t
, struct hash_control
*formal_hash
,
710 int kind
, sb
*out
, int copyifnotthere
)
715 src
= get_apost_token (start
, in
, t
, kind
);
716 /* See if it's in the macro's hash table, unless this is
717 macro_strip_at and kind is '@' and the token did not end in '@'. */
720 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
723 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (t
));
728 sb_add_sb (out
, &ptr
->actual
);
732 sb_add_sb (out
, &ptr
->def
);
735 else if (kind
== '&')
737 /* Doing this permits people to use & in macro bodies. */
738 sb_add_char (out
, '&');
741 else if (copyifnotthere
)
747 sb_add_char (out
, '\\');
753 /* Expand the body of a macro. */
756 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
757 struct hash_control
*formal_hash
, const macro_entry
*macro
)
760 int src
= 0, inquote
= 0, macro_line
= 0;
761 formal_entry
*loclist
= NULL
;
762 const char *err
= NULL
;
766 while (src
< in
->len
&& !err
)
768 if (in
->ptr
[src
] == '&')
773 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
774 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
776 sb_add_char (out
, in
->ptr
[src
++]);
780 /* FIXME: Why do we do this? */
781 /* At least in alternate mode this seems correct; without this
782 one can't append a literal to a parameter. */
783 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
786 else if (in
->ptr
[src
] == '\\')
789 if (src
< in
->len
&& in
->ptr
[src
] == '(')
791 /* Sub in till the next ')' literally. */
793 while (src
< in
->len
&& in
->ptr
[src
] != ')')
795 sb_add_char (out
, in
->ptr
[src
++]);
800 err
= _("missing `)'");
802 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
804 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
806 /* Sub in the macro invocation number. */
810 sprintf (buffer
, "%d", macro_number
);
811 sb_add_string (out
, buffer
);
813 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
815 /* This is a preprocessor variable name, we don't do them
817 sb_add_char (out
, '\\');
818 sb_add_char (out
, '&');
821 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
826 if (ISDIGIT (in
->ptr
[src
]))
827 ind
= in
->ptr
[src
] - '0';
828 else if (ISUPPER (in
->ptr
[src
]))
829 ind
= in
->ptr
[src
] - 'A' + 10;
831 ind
= in
->ptr
[src
] - 'a' + 10;
833 for (f
= formals
; f
!= NULL
; f
= f
->next
)
835 if (f
->index
== ind
- 1)
837 if (f
->actual
.len
!= 0)
838 sb_add_sb (out
, &f
->actual
);
840 sb_add_sb (out
, &f
->def
);
848 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
851 else if ((macro_alternate
|| macro_mri
)
852 && is_name_beginner (in
->ptr
[src
])
855 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
858 || src
+ 5 >= in
->len
859 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
860 || ! ISWHITE (in
->ptr
[src
+ 5]))
863 src
= sub_actual (src
, in
, &t
, formal_hash
,
864 (macro_strip_at
&& inquote
) ? '@' : '\'',
869 src
= sb_skip_white (src
+ 5, in
);
870 while (in
->ptr
[src
] != '\n')
873 formal_entry
*f
= new_formal ();
875 src
= get_token (src
, in
, &f
->name
);
876 name
= sb_terminate (&f
->name
);
877 if (! hash_find (formal_hash
, name
))
882 f
->index
= LOCAL_INDEX
;
886 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
887 sb_add_string (&f
->actual
, buf
);
889 err
= hash_jam (formal_hash
, name
, f
);
895 as_bad_where (macro
->file
,
896 macro
->line
+ macro_line
,
897 _("`%s' was already used as parameter (or another local) name"),
902 src
= sb_skip_comma (src
, in
);
906 else if (in
->ptr
[src
] == '"'
907 || (macro_mri
&& in
->ptr
[src
] == '\''))
910 sb_add_char (out
, in
->ptr
[src
++]);
912 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
916 && in
->ptr
[src
] == '@')
918 sb_add_char (out
, '@');
923 && in
->ptr
[src
] == '='
925 && in
->ptr
[src
+ 1] == '=')
930 src
= get_token (src
+ 2, in
, &t
);
931 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (&t
));
934 /* FIXME: We should really return a warning string here,
935 but we can't, because the == might be in the MRI
936 comment field, and, since the nature of the MRI
937 comment field depends upon the exact instruction
938 being used, we don't have enough information here to
939 figure out whether it is or not. Instead, we leave
940 the == in place, which should cause a syntax error if
941 it is not in a comment. */
942 sb_add_char (out
, '=');
943 sb_add_char (out
, '=');
950 sb_add_string (out
, "-1");
954 sb_add_char (out
, '0');
960 if (in
->ptr
[src
] == '\n')
962 sb_add_char (out
, in
->ptr
[src
++]);
968 while (loclist
!= NULL
)
973 /* Setting the value to NULL effectively deletes the entry. We
974 avoid calling hash_delete because it doesn't reclaim memory. */
975 hash_jam (formal_hash
, sb_terminate (&loclist
->name
), NULL
);
976 del_formal (loclist
);
983 /* Assign values to the formal parameters of a macro, and expand the
987 macro_expand (int idx
, sb
*in
, macro_entry
*m
, sb
*out
)
994 const char *err
= NULL
;
998 /* Reset any old value the actuals may have. */
999 for (f
= m
->formals
; f
; f
= f
->next
)
1000 sb_reset (&f
->actual
);
1002 while (f
!= NULL
&& f
->index
< 0)
1007 /* The macro may be called with an optional qualifier, which may
1008 be referred to in the macro body as \0. */
1009 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1011 /* The Microtec assembler ignores this if followed by a white space.
1012 (Macro invocation with empty extension) */
1015 && in
->ptr
[idx
] != ' '
1016 && in
->ptr
[idx
] != '\t')
1018 formal_entry
*n
= new_formal ();
1020 n
->index
= QUAL_INDEX
;
1022 n
->next
= m
->formals
;
1025 idx
= get_any_string (idx
, in
, &n
->actual
);
1030 /* Peel off the actuals and store them away in the hash tables' actuals. */
1031 idx
= sb_skip_white (idx
, in
);
1032 while (idx
< in
->len
)
1036 /* Look and see if it's a positional or keyword arg. */
1038 while (scan
< in
->len
1039 && !ISSEP (in
->ptr
[scan
])
1040 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1041 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1043 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1047 /* It's OK to go from positional to keyword. */
1049 /* This is a keyword arg, fetch the formal name and
1050 then the actual stuff. */
1052 idx
= get_token (idx
, in
, &t
);
1053 if (in
->ptr
[idx
] != '=')
1055 err
= _("confusion in formal parameters");
1059 /* Lookup the formal in the macro's list. */
1060 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1062 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1067 /* Insert this value into the right place. */
1068 if (ptr
->actual
.len
)
1070 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1073 sb_reset (&ptr
->actual
);
1075 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1076 if (ptr
->actual
.len
> 0)
1084 err
= _("can't mix positional and keyword arguments");
1095 err
= _("too many positional arguments");
1102 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1103 if ((*pf
)->index
>= c
)
1104 c
= (*pf
)->index
+ 1;
1111 if (f
->type
!= FORMAL_VARARG
)
1112 idx
= get_any_string (idx
, in
, &f
->actual
);
1115 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1118 if (f
->actual
.len
> 0)
1124 while (f
!= NULL
&& f
->index
< 0);
1128 idx
= sb_skip_comma (idx
, in
);
1131 if (in
->ptr
[idx
] == ',')
1133 if (ISWHITE (in
->ptr
[idx
]))
1140 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1142 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1143 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1153 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1154 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1155 sprintf (buffer
, "%d", narg
);
1156 sb_add_string (&ptr
->actual
, buffer
);
1159 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1162 /* Discard any unnamed formal arguments. */
1170 if ((*pf
)->name
.len
!= 0)
1188 /* Check for a macro. If one is found, put the expansion into
1189 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1192 check_macro (const char *line
, sb
*expand
,
1193 const char **error
, macro_entry
**info
)
1200 if (! is_name_beginner (*line
)
1201 && (! macro_mri
|| *line
!= '.'))
1205 while (is_part_of_name (*s
))
1207 if (is_name_ender (*s
))
1210 copy
= (char *) alloca (s
- line
+ 1);
1211 memcpy (copy
, line
, s
- line
);
1212 copy
[s
- line
] = '\0';
1213 for (cs
= copy
; *cs
!= '\0'; cs
++)
1214 *cs
= TOLOWER (*cs
);
1216 macro
= (macro_entry
*) hash_find (macro_hash
, copy
);
1221 /* Wrap the line up in an sb. */
1223 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1224 sb_add_char (&line_sb
, *s
++);
1227 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1231 /* Export the macro information if requested. */
1238 /* Free the memory allocated to a macro. */
1241 free_macro(macro_entry
*macro
)
1243 formal_entry
*formal
;
1245 for (formal
= macro
->formals
; formal
; )
1250 formal
= formal
->next
;
1253 hash_die (macro
->formal_hash
);
1254 sb_kill (¯o
->sub
);
1258 /* Delete a macro. */
1261 delete_macro (const char *name
)
1267 len
= strlen (name
);
1268 copy
= (char *) alloca (len
+ 1);
1269 for (i
= 0; i
< len
; ++i
)
1270 copy
[i
] = TOLOWER (name
[i
]);
1273 /* Since hash_delete doesn't free memory, just clear out the entry. */
1274 if ((macro
= hash_find (macro_hash
, copy
)) != NULL
)
1276 hash_jam (macro_hash
, copy
, NULL
);
1280 as_warn (_("Attempt to purge non-existant macro `%s'"), copy
);
1283 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1284 combined macro definition and execution. This returns NULL on
1285 success, or an error message otherwise. */
1288 expand_irp (int irpc
, int idx
, sb
*in
, sb
*out
, int (*get_line
) (sb
*))
1292 struct hash_control
*h
;
1295 idx
= sb_skip_white (idx
, in
);
1298 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1299 return _("unexpected end of file in irp or irpc");
1305 idx
= get_token (idx
, in
, &f
.name
);
1306 if (f
.name
.len
== 0)
1307 return _("missing model parameter");
1310 err
= hash_jam (h
, sb_terminate (&f
.name
), &f
);
1316 f
.type
= FORMAL_OPTIONAL
;
1320 idx
= sb_skip_comma (idx
, in
);
1323 /* Expand once with a null string. */
1324 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1328 if (irpc
&& in
->ptr
[idx
] == '"')
1330 while (idx
< in
->len
)
1333 idx
= get_any_string (idx
, in
, &f
.actual
);
1336 if (in
->ptr
[idx
] == '"')
1340 nxt
= sb_skip_white (idx
+ 1, in
);
1347 sb_reset (&f
.actual
);
1348 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1351 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1355 idx
= sb_skip_comma (idx
, in
);
1357 idx
= sb_skip_white (idx
, in
);
1362 sb_kill (&f
.actual
);