1 /* macro.c - macro support for gas
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 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
32 /* Indented so that pre-ansi C compilers will ignore it, rather than
33 choke on it. Some versions of AIX require this to be the first
37 # ifndef alloca /* predefined by HP cc +Olibcalls */
38 # if !defined (__STDC__) && !defined (__hpux)
39 extern char *alloca ();
41 extern void *alloca ();
42 # endif /* __STDC__, __hpux */
45 # endif /* HAVE_ALLOCA_H */
58 #include "libiberty.h"
59 #include "safe-ctype.h"
66 /* The routines in this file handle macro definition and expansion.
67 They are called by gas. */
69 /* Internal functions. */
71 static int get_token (int, sb
*, sb
*);
72 static int getstring (int, sb
*, sb
*);
73 static int get_any_string (int, sb
*, sb
*);
74 static formal_entry
*new_formal (void);
75 static void del_formal (formal_entry
*);
76 static int do_formals (macro_entry
*, int, sb
*);
77 static int get_apost_token (int, sb
*, sb
*, int);
78 static int sub_actual (int, sb
*, sb
*, struct hash_control
*, int, sb
*, int);
79 static const char *macro_expand_body
80 (sb
*, sb
*, formal_entry
*, struct hash_control
*, const macro_entry
*);
81 static const char *macro_expand (int, sb
*, macro_entry
*, sb
*);
82 static void free_macro(macro_entry
*);
84 #define ISWHITE(x) ((x) == ' ' || (x) == '\t')
87 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
88 || (x) == ')' || (x) == '(' \
89 || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
92 ((x) == 'b' || (x) == 'B' \
93 || (x) == 'q' || (x) == 'Q' \
94 || (x) == 'h' || (x) == 'H' \
95 || (x) == 'd' || (x) == 'D')
97 /* The macro hash table. */
99 struct hash_control
*macro_hash
;
101 /* Whether any macros have been defined. */
105 /* Whether we are in alternate syntax mode. */
107 static int macro_alternate
;
109 /* Whether we are in MRI mode. */
111 static int macro_mri
;
113 /* Whether we should strip '@' characters. */
115 static int macro_strip_at
;
117 /* Function to use to parse an expression. */
119 static int (*macro_expr
) (const char *, int, sb
*, int *);
121 /* Number of macro expansions that have been done. */
123 static int macro_number
;
125 /* Initialize macro processing. */
128 macro_init (int alternate
, int mri
, int strip_at
,
129 int (*expr
) (const char *, int, sb
*, int *))
131 macro_hash
= hash_new ();
133 macro_alternate
= alternate
;
135 macro_strip_at
= strip_at
;
139 /* Switch in and out of alternate mode on the fly. */
142 macro_set_alternate (int alternate
)
144 macro_alternate
= alternate
;
147 /* Switch in and out of MRI mode on the fly. */
150 macro_mri_mode (int mri
)
155 /* Read input lines till we get to a TO string.
156 Increase nesting depth if we get a FROM string.
157 Put the results into sb at PTR.
158 FROM may be NULL (or will be ignored) if TO is "ENDR".
159 Add a new input line to an sb using GET_LINE.
160 Return 1 on success, 0 on unexpected EOF. */
163 buffer_and_nest (const char *from
, const char *to
, sb
*ptr
,
164 int (*get_line
) (sb
*))
167 int to_len
= strlen (to
);
169 int line_start
= ptr
->len
;
171 int more
= get_line (ptr
);
173 if (to_len
== 4 && strcasecmp(to
, "ENDR") == 0)
179 from_len
= strlen (from
);
183 /* Try to find the first pseudo op on the line. */
186 /* With normal syntax we can suck what we want till we get
187 to the dot. With the alternate, labels have to start in
188 the first column, since we can't tell what's a label and
189 what's a pseudoop. */
191 if (! LABELS_WITHOUT_COLONS
)
193 /* Skip leading whitespace. */
194 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
200 /* Skip over a label, if any. */
201 if (i
>= ptr
->len
|| ! is_name_beginner (ptr
->ptr
[i
]))
204 while (i
< ptr
->len
&& is_part_of_name (ptr
->ptr
[i
]))
206 if (i
< ptr
->len
&& is_name_ender (ptr
->ptr
[i
]))
208 if (LABELS_WITHOUT_COLONS
)
210 /* Skip whitespace. */
211 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
213 /* Check for the colon. */
214 if (i
>= ptr
->len
|| ptr
->ptr
[i
] != ':')
223 /* Skip trailing whitespace. */
224 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
227 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
231 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
234 && strncasecmp (ptr
->ptr
+ i
, "IRPC", from_len
= 4) != 0
235 && strncasecmp (ptr
->ptr
+ i
, "IRP", from_len
= 3) != 0
236 && strncasecmp (ptr
->ptr
+ i
, "IREPC", from_len
= 5) != 0
237 && strncasecmp (ptr
->ptr
+ i
, "IREP", from_len
= 4) != 0
238 && strncasecmp (ptr
->ptr
+ i
, "REPT", from_len
= 4) != 0
239 && strncasecmp (ptr
->ptr
+ i
, "REP", from_len
= 3) != 0)
242 ? strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0
244 && (ptr
->len
== (i
+ from_len
)
245 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
246 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
248 if (strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
249 && (ptr
->len
== (i
+ to_len
)
250 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
251 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
256 /* Reset the string to not include the ending rune. */
257 ptr
->len
= line_start
;
263 /* Add the original end-of-line char to the end and keep running. */
264 sb_add_char (ptr
, more
);
265 line_start
= ptr
->len
;
266 more
= get_line (ptr
);
269 /* Return 1 on success, 0 on unexpected EOF. */
273 /* Pick up a token. */
276 get_token (int idx
, sb
*in
, sb
*name
)
279 && is_name_beginner (in
->ptr
[idx
]))
281 sb_add_char (name
, in
->ptr
[idx
++]);
283 && is_part_of_name (in
->ptr
[idx
]))
285 sb_add_char (name
, in
->ptr
[idx
++]);
288 && is_name_ender (in
->ptr
[idx
]))
290 sb_add_char (name
, in
->ptr
[idx
++]);
293 /* Ignore trailing &. */
294 if (macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
299 /* Pick up a string. */
302 getstring (int idx
, sb
*in
, sb
*acc
)
305 && (in
->ptr
[idx
] == '"'
306 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
307 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
309 if (in
->ptr
[idx
] == '<')
313 while ((in
->ptr
[idx
] != '>' || nest
)
316 if (in
->ptr
[idx
] == '!')
319 sb_add_char (acc
, in
->ptr
[idx
++]);
323 if (in
->ptr
[idx
] == '>')
325 if (in
->ptr
[idx
] == '<')
327 sb_add_char (acc
, in
->ptr
[idx
++]);
332 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
334 char tchar
= in
->ptr
[idx
];
339 while (idx
< in
->len
)
341 if (in
->ptr
[idx
- 1] == '\\')
346 if (macro_alternate
&& in
->ptr
[idx
] == '!')
350 sb_add_char (acc
, in
->ptr
[idx
]);
354 else if (escaped
&& in
->ptr
[idx
] == tchar
)
356 sb_add_char (acc
, tchar
);
361 if (in
->ptr
[idx
] == tchar
)
365 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
369 sb_add_char (acc
, in
->ptr
[idx
]);
379 /* Fetch string from the input stream,
381 'Bxyx<whitespace> -> return 'Bxyza
382 %<expr> -> return string of decimal value of <expr>
383 "string" -> return string
384 (string) -> return (string-including-whitespaces)
385 xyx<whitespace> -> return xyz. */
388 get_any_string (int idx
, sb
*in
, sb
*out
)
391 idx
= sb_skip_white (idx
, in
);
395 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
397 while (!ISSEP (in
->ptr
[idx
]))
398 sb_add_char (out
, in
->ptr
[idx
++]);
400 else if (in
->ptr
[idx
] == '%' && macro_alternate
)
405 /* Turns the next expression into a string. */
406 /* xgettext: no-c-format */
407 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
411 sprintf (buf
, "%d", val
);
412 sb_add_string (out
, buf
);
414 else if (in
->ptr
[idx
] == '"'
415 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
416 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
418 if (macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
420 /* Keep the quotes. */
421 sb_add_char (out
, '"');
422 idx
= getstring (idx
, in
, out
);
423 sb_add_char (out
, '"');
427 idx
= getstring (idx
, in
, out
);
432 char *br_buf
= xmalloc(1);
433 char *in_br
= br_buf
;
438 || (in
->ptr
[idx
] != ' '
439 && in
->ptr
[idx
] != '\t'))
440 && in
->ptr
[idx
] != ','
441 && (in
->ptr
[idx
] != '<'
442 || (! macro_alternate
&& ! macro_mri
)))
444 char tchar
= in
->ptr
[idx
];
450 sb_add_char (out
, in
->ptr
[idx
++]);
452 && in
->ptr
[idx
] != tchar
)
453 sb_add_char (out
, in
->ptr
[idx
++]);
463 br_buf
= xmalloc(strlen(in_br
) + 2);
464 strcpy(br_buf
+ 1, in_br
);
479 sb_add_char (out
, tchar
);
489 /* Allocate a new formal. */
491 static formal_entry
*
494 formal_entry
*formal
;
496 formal
= xmalloc (sizeof (formal_entry
));
498 sb_new (&formal
->name
);
499 sb_new (&formal
->def
);
500 sb_new (&formal
->actual
);
502 formal
->type
= FORMAL_OPTIONAL
;
509 del_formal (formal_entry
*formal
)
511 sb_kill (&formal
->actual
);
512 sb_kill (&formal
->def
);
513 sb_kill (&formal
->name
);
517 /* Pick up the formal parameters of a macro definition. */
520 do_formals (macro_entry
*macro
, int idx
, sb
*in
)
522 formal_entry
**p
= ¯o
->formals
;
525 idx
= sb_skip_white (idx
, in
);
526 while (idx
< in
->len
)
528 formal_entry
*formal
= new_formal ();
531 idx
= get_token (idx
, in
, &formal
->name
);
532 if (formal
->name
.len
== 0)
534 if (macro
->formal_count
)
538 idx
= sb_skip_white (idx
, in
);
539 /* This is a formal. */
540 name
= sb_terminate (&formal
->name
);
543 && in
->ptr
[idx
] == ':'
544 && (! is_name_beginner (':')
545 || idx
+ 1 >= in
->len
546 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
548 /* Got a qualifier. */
552 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
553 sb_terminate (&qual
);
555 as_bad_where (macro
->file
,
557 _("Missing parameter qualifier for `%s' in macro `%s'"),
560 else if (strcmp (qual
.ptr
, "req") == 0)
561 formal
->type
= FORMAL_REQUIRED
;
562 else if (strcmp (qual
.ptr
, "vararg") == 0)
563 formal
->type
= FORMAL_VARARG
;
565 as_bad_where (macro
->file
,
567 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
572 idx
= sb_skip_white (idx
, in
);
574 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
577 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
578 idx
= sb_skip_white (idx
, in
);
579 if (formal
->type
== FORMAL_REQUIRED
)
581 sb_reset (&formal
->def
);
582 as_warn_where (macro
->file
,
584 _("Pointless default value for required parameter `%s' in macro `%s'"),
590 /* Add to macro's hash table. */
591 if (! hash_find (macro
->formal_hash
, name
))
592 hash_jam (macro
->formal_hash
, name
, formal
);
594 as_bad_where (macro
->file
,
596 _("A parameter named `%s' already exists for macro `%s'"),
600 formal
->index
= macro
->formal_count
++;
603 if (formal
->type
== FORMAL_VARARG
)
606 idx
= sb_skip_comma (idx
, in
);
607 if (idx
!= cidx
&& idx
>= in
->len
)
616 formal_entry
*formal
= new_formal ();
618 /* Add a special NARG formal, which macro_expand will set to the
619 number of arguments. */
620 /* The same MRI assemblers which treat '@' characters also use
621 the name $NARG. At least until we find an exception. */
627 sb_add_string (&formal
->name
, name
);
629 /* Add to macro's hash table. */
630 if (hash_find (macro
->formal_hash
, name
))
631 as_bad_where (macro
->file
,
633 _("Reserved word `%s' used as parameter in macro `%s'"),
636 hash_jam (macro
->formal_hash
, name
, formal
);
638 formal
->index
= NARG_INDEX
;
645 /* Define a new macro. Returns NULL on success, otherwise returns an
646 error message. If NAMEP is not NULL, *NAMEP is set to the name of
647 the macro which was defined. */
650 define_macro (int idx
, sb
*in
, sb
*label
,
651 int (*get_line
) (sb
*),
652 char *file
, unsigned int line
,
657 const char *error
= NULL
;
659 macro
= (macro_entry
*) xmalloc (sizeof (macro_entry
));
660 sb_new (¯o
->sub
);
665 macro
->formal_count
= 0;
667 macro
->formal_hash
= hash_new ();
669 idx
= sb_skip_white (idx
, in
);
670 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
671 error
= _("unexpected end of file in macro `%s' definition");
672 if (label
!= NULL
&& label
->len
!= 0)
674 sb_add_sb (&name
, label
);
675 macro
->name
= sb_terminate (&name
);
676 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
678 /* It's the label: MACRO (formals,...) sort */
679 idx
= do_formals (macro
, idx
+ 1, in
);
680 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
681 idx
= sb_skip_white (idx
+ 1, in
);
683 error
= _("missing `)' after formals in macro definition `%s'");
687 /* It's the label: MACRO formals,... sort */
688 idx
= do_formals (macro
, idx
, in
);
695 idx
= get_token (idx
, in
, &name
);
696 macro
->name
= sb_terminate (&name
);
698 error
= _("Missing macro name");
699 cidx
= sb_skip_white (idx
, in
);
700 idx
= sb_skip_comma (cidx
, in
);
701 if (idx
== cidx
|| idx
< in
->len
)
702 idx
= do_formals (macro
, idx
, in
);
706 if (!error
&& idx
< in
->len
)
707 error
= _("Bad parameter list for macro `%s'");
709 /* And stick it in the macro hash table. */
710 for (idx
= 0; idx
< name
.len
; idx
++)
711 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
712 if (hash_find (macro_hash
, macro
->name
))
713 error
= _("Macro `%s' was already defined");
715 error
= hash_jam (macro_hash
, macro
->name
, (PTR
) macro
);
718 *namep
= macro
->name
;
728 /* Scan a token, and then skip KIND. */
731 get_apost_token (int idx
, sb
*in
, sb
*name
, int kind
)
733 idx
= get_token (idx
, in
, name
);
735 && in
->ptr
[idx
] == kind
736 && (! macro_mri
|| macro_strip_at
)
737 && (! macro_strip_at
|| kind
== '@'))
742 /* Substitute the actual value for a formal parameter. */
745 sub_actual (int start
, sb
*in
, sb
*t
, struct hash_control
*formal_hash
,
746 int kind
, sb
*out
, int copyifnotthere
)
751 src
= get_apost_token (start
, in
, t
, kind
);
752 /* See if it's in the macro's hash table, unless this is
753 macro_strip_at and kind is '@' and the token did not end in '@'. */
756 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
759 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (t
));
764 sb_add_sb (out
, &ptr
->actual
);
768 sb_add_sb (out
, &ptr
->def
);
771 else if (kind
== '&')
773 /* Doing this permits people to use & in macro bodies. */
774 sb_add_char (out
, '&');
777 else if (copyifnotthere
)
783 sb_add_char (out
, '\\');
789 /* Expand the body of a macro. */
792 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
793 struct hash_control
*formal_hash
, const macro_entry
*macro
)
796 int src
= 0, inquote
= 0, macro_line
= 0;
797 formal_entry
*loclist
= NULL
;
798 const char *err
= NULL
;
802 while (src
< in
->len
&& !err
)
804 if (in
->ptr
[src
] == '&')
809 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
810 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
812 sb_add_char (out
, in
->ptr
[src
++]);
816 /* FIXME: Why do we do this? */
817 /* At least in alternate mode this seems correct; without this
818 one can't append a literal to a parameter. */
819 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
822 else if (in
->ptr
[src
] == '\\')
825 if (src
< in
->len
&& in
->ptr
[src
] == '(')
827 /* Sub in till the next ')' literally. */
829 while (src
< in
->len
&& in
->ptr
[src
] != ')')
831 sb_add_char (out
, in
->ptr
[src
++]);
836 err
= _("missing `)'");
838 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
840 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
842 /* Sub in the macro invocation number. */
846 sprintf (buffer
, "%d", macro_number
);
847 sb_add_string (out
, buffer
);
849 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
851 /* This is a preprocessor variable name, we don't do them
853 sb_add_char (out
, '\\');
854 sb_add_char (out
, '&');
857 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
862 if (ISDIGIT (in
->ptr
[src
]))
863 ind
= in
->ptr
[src
] - '0';
864 else if (ISUPPER (in
->ptr
[src
]))
865 ind
= in
->ptr
[src
] - 'A' + 10;
867 ind
= in
->ptr
[src
] - 'a' + 10;
869 for (f
= formals
; f
!= NULL
; f
= f
->next
)
871 if (f
->index
== ind
- 1)
873 if (f
->actual
.len
!= 0)
874 sb_add_sb (out
, &f
->actual
);
876 sb_add_sb (out
, &f
->def
);
884 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
887 else if ((macro_alternate
|| macro_mri
)
888 && is_name_beginner (in
->ptr
[src
])
891 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
894 || src
+ 5 >= in
->len
895 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
896 || ! ISWHITE (in
->ptr
[src
+ 5]))
899 src
= sub_actual (src
, in
, &t
, formal_hash
,
900 (macro_strip_at
&& inquote
) ? '@' : '\'',
905 src
= sb_skip_white (src
+ 5, in
);
906 while (in
->ptr
[src
] != '\n')
909 formal_entry
*f
= new_formal ();
911 src
= get_token (src
, in
, &f
->name
);
912 name
= sb_terminate (&f
->name
);
913 if (! hash_find (formal_hash
, name
))
918 f
->index
= LOCAL_INDEX
;
922 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
923 sb_add_string (&f
->actual
, buf
);
925 err
= hash_jam (formal_hash
, name
, f
);
931 as_bad_where (macro
->file
,
932 macro
->line
+ macro_line
,
933 _("`%s' was already used as parameter (or another local) name"),
938 src
= sb_skip_comma (src
, in
);
942 else if (in
->ptr
[src
] == '"'
943 || (macro_mri
&& in
->ptr
[src
] == '\''))
946 sb_add_char (out
, in
->ptr
[src
++]);
948 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
952 && in
->ptr
[src
] == '@')
954 sb_add_char (out
, '@');
959 && in
->ptr
[src
] == '='
961 && in
->ptr
[src
+ 1] == '=')
966 src
= get_token (src
+ 2, in
, &t
);
967 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (&t
));
970 /* FIXME: We should really return a warning string here,
971 but we can't, because the == might be in the MRI
972 comment field, and, since the nature of the MRI
973 comment field depends upon the exact instruction
974 being used, we don't have enough information here to
975 figure out whether it is or not. Instead, we leave
976 the == in place, which should cause a syntax error if
977 it is not in a comment. */
978 sb_add_char (out
, '=');
979 sb_add_char (out
, '=');
986 sb_add_string (out
, "-1");
990 sb_add_char (out
, '0');
996 if (in
->ptr
[src
] == '\n')
998 sb_add_char (out
, in
->ptr
[src
++]);
1004 while (loclist
!= NULL
)
1009 /* Setting the value to NULL effectively deletes the entry. We
1010 avoid calling hash_delete because it doesn't reclaim memory. */
1011 hash_jam (formal_hash
, sb_terminate (&loclist
->name
), NULL
);
1012 del_formal (loclist
);
1019 /* Assign values to the formal parameters of a macro, and expand the
1023 macro_expand (int idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1028 int is_positional
= 0;
1031 const char *err
= NULL
;
1035 /* Reset any old value the actuals may have. */
1036 for (f
= m
->formals
; f
; f
= f
->next
)
1037 sb_reset (&f
->actual
);
1039 while (f
!= NULL
&& f
->index
< 0)
1044 /* The macro may be called with an optional qualifier, which may
1045 be referred to in the macro body as \0. */
1046 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1048 /* The Microtec assembler ignores this if followed by a white space.
1049 (Macro invocation with empty extension) */
1052 && in
->ptr
[idx
] != ' '
1053 && in
->ptr
[idx
] != '\t')
1055 formal_entry
*n
= new_formal ();
1057 n
->index
= QUAL_INDEX
;
1059 n
->next
= m
->formals
;
1062 idx
= get_any_string (idx
, in
, &n
->actual
);
1067 /* Peel off the actuals and store them away in the hash tables' actuals. */
1068 idx
= sb_skip_white (idx
, in
);
1069 while (idx
< in
->len
)
1073 /* Look and see if it's a positional or keyword arg. */
1075 while (scan
< in
->len
1076 && !ISSEP (in
->ptr
[scan
])
1077 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1078 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1080 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1084 /* It's OK to go from positional to keyword. */
1086 /* This is a keyword arg, fetch the formal name and
1087 then the actual stuff. */
1089 idx
= get_token (idx
, in
, &t
);
1090 if (in
->ptr
[idx
] != '=')
1092 err
= _("confusion in formal parameters");
1096 /* Lookup the formal in the macro's list. */
1097 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1099 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1104 /* Insert this value into the right place. */
1105 if (ptr
->actual
.len
)
1107 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1110 sb_reset (&ptr
->actual
);
1112 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1113 if (ptr
->actual
.len
> 0)
1119 /* This is a positional arg. */
1123 err
= _("can't mix positional and keyword arguments");
1134 err
= _("too many positional arguments");
1141 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1142 if ((*pf
)->index
>= c
)
1143 c
= (*pf
)->index
+ 1;
1150 if (f
->type
!= FORMAL_VARARG
)
1151 idx
= get_any_string (idx
, in
, &f
->actual
);
1154 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1157 if (f
->actual
.len
> 0)
1163 while (f
!= NULL
&& f
->index
< 0);
1167 idx
= sb_skip_comma (idx
, in
);
1170 if (in
->ptr
[idx
] == ',')
1172 if (ISWHITE (in
->ptr
[idx
]))
1179 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1181 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1182 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1192 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1193 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1194 sprintf (buffer
, "%d", narg
);
1195 sb_add_string (&ptr
->actual
, buffer
);
1198 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1201 /* Discard any unnamed formal arguments. */
1209 if ((*pf
)->name
.len
!= 0)
1227 /* Check for a macro. If one is found, put the expansion into
1228 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1231 check_macro (const char *line
, sb
*expand
,
1232 const char **error
, macro_entry
**info
)
1239 if (! is_name_beginner (*line
)
1240 && (! macro_mri
|| *line
!= '.'))
1244 while (is_part_of_name (*s
))
1246 if (is_name_ender (*s
))
1249 copy
= (char *) alloca (s
- line
+ 1);
1250 memcpy (copy
, line
, s
- line
);
1251 copy
[s
- line
] = '\0';
1252 for (cs
= copy
; *cs
!= '\0'; cs
++)
1253 *cs
= TOLOWER (*cs
);
1255 macro
= (macro_entry
*) hash_find (macro_hash
, copy
);
1260 /* Wrap the line up in an sb. */
1262 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1263 sb_add_char (&line_sb
, *s
++);
1266 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1270 /* Export the macro information if requested. */
1277 /* Free the memory allocated to a macro. */
1280 free_macro(macro_entry
*macro
)
1282 formal_entry
*formal
;
1284 for (formal
= macro
->formals
; formal
; )
1289 formal
= formal
->next
;
1292 hash_die (macro
->formal_hash
);
1293 sb_kill (¯o
->sub
);
1297 /* Delete a macro. */
1300 delete_macro (const char *name
)
1306 len
= strlen (name
);
1307 copy
= (char *) alloca (len
+ 1);
1308 for (i
= 0; i
< len
; ++i
)
1309 copy
[i
] = TOLOWER (name
[i
]);
1312 /* Since hash_delete doesn't free memory, just clear out the entry. */
1313 if ((macro
= hash_find (macro_hash
, copy
)) != NULL
)
1315 hash_jam (macro_hash
, copy
, NULL
);
1319 as_warn (_("Attempt to purge non-existant macro `%s'"), copy
);
1322 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1323 combined macro definition and execution. This returns NULL on
1324 success, or an error message otherwise. */
1327 expand_irp (int irpc
, int idx
, sb
*in
, sb
*out
, int (*get_line
) (sb
*))
1331 struct hash_control
*h
;
1334 idx
= sb_skip_white (idx
, in
);
1337 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1338 return _("unexpected end of file in irp or irpc");
1344 idx
= get_token (idx
, in
, &f
.name
);
1345 if (f
.name
.len
== 0)
1346 return _("missing model parameter");
1349 err
= hash_jam (h
, sb_terminate (&f
.name
), &f
);
1355 f
.type
= FORMAL_OPTIONAL
;
1359 idx
= sb_skip_comma (idx
, in
);
1362 /* Expand once with a null string. */
1363 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1367 if (irpc
&& in
->ptr
[idx
] == '"')
1369 while (idx
< in
->len
)
1372 idx
= get_any_string (idx
, in
, &f
.actual
);
1375 if (in
->ptr
[idx
] == '"')
1379 nxt
= sb_skip_white (idx
+ 1, in
);
1386 sb_reset (&f
.actual
);
1387 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1390 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1394 idx
= sb_skip_comma (idx
, in
);
1396 idx
= sb_skip_white (idx
, in
);
1401 sb_kill (&f
.actual
);