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, 59 Temple Place - Suite 330, 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
*, int, int);
74 static int do_formals (macro_entry
*, int, sb
*);
75 static int get_apost_token (int, sb
*, sb
*, int);
76 static int sub_actual (int, sb
*, sb
*, struct hash_control
*, int, sb
*, int);
77 static const char *macro_expand_body
78 (sb
*, sb
*, formal_entry
*, struct hash_control
*, const macro_entry
*);
79 static const char *macro_expand (int, sb
*, macro_entry
*, sb
*);
80 static void free_macro(macro_entry
*);
82 #define ISWHITE(x) ((x) == ' ' || (x) == '\t')
85 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
86 || (x) == ')' || (x) == '(' \
87 || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
90 ((x) == 'b' || (x) == 'B' \
91 || (x) == 'q' || (x) == 'Q' \
92 || (x) == 'h' || (x) == 'H' \
93 || (x) == 'd' || (x) == 'D')
95 /* The macro hash table. */
97 static struct hash_control
*macro_hash
;
99 /* Whether any macros have been defined. */
103 /* Whether we are in alternate syntax mode. */
105 static int macro_alternate
;
107 /* Whether we are in MRI mode. */
109 static int macro_mri
;
111 /* Whether we should strip '@' characters. */
113 static int macro_strip_at
;
115 /* Function to use to parse an expression. */
117 static int (*macro_expr
) (const char *, int, sb
*, int *);
119 /* Number of macro expansions that have been done. */
121 static int macro_number
;
123 /* Initialize macro processing. */
126 macro_init (int alternate
, int mri
, int strip_at
,
127 int (*expr
) (const char *, int, sb
*, int *))
129 macro_hash
= hash_new ();
131 macro_alternate
= alternate
;
133 macro_strip_at
= strip_at
;
137 /* Switch in and out of alternate mode on the fly. */
140 macro_set_alternate (int alternate
)
142 macro_alternate
= alternate
;
145 /* Switch in and out of MRI mode on the fly. */
148 macro_mri_mode (int mri
)
153 /* Read input lines till we get to a TO string.
154 Increase nesting depth if we get a FROM string.
155 Put the results into sb at PTR.
156 FROM may be NULL (or will be ignored) if TO is "ENDR".
157 Add a new input line to an sb using GET_LINE.
158 Return 1 on success, 0 on unexpected EOF. */
161 buffer_and_nest (const char *from
, const char *to
, sb
*ptr
,
162 int (*get_line
) (sb
*))
165 int to_len
= strlen (to
);
167 int line_start
= ptr
->len
;
169 int more
= get_line (ptr
);
171 if (to_len
== 4 && strcasecmp(to
, "ENDR") == 0)
177 from_len
= strlen (from
);
181 /* Try and find the first pseudo op on the line. */
184 if (! NO_PSEUDO_DOT
&& ! flag_m68k_mri
)
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
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
] != ':')
224 /* Skip trailing whitespace. */
225 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
228 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
232 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
235 && strncasecmp (ptr
->ptr
+ i
, "IRPC", from_len
= 4) != 0
236 && strncasecmp (ptr
->ptr
+ i
, "IRP", from_len
= 3) != 0
237 && strncasecmp (ptr
->ptr
+ i
, "IREPC", from_len
= 5) != 0
238 && strncasecmp (ptr
->ptr
+ i
, "IREP", from_len
= 4) != 0
239 && strncasecmp (ptr
->ptr
+ i
, "REPT", from_len
= 4) != 0
240 && strncasecmp (ptr
->ptr
+ i
, "REP", from_len
= 3) != 0)
243 ? strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0
245 && (ptr
->len
== (i
+ from_len
)
246 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
247 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
249 if (strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
250 && (ptr
->len
== (i
+ to_len
)
251 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
252 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
257 /* Reset the string to not include the ending rune. */
258 ptr
->len
= line_start
;
264 /* Add the original end-of-line char to the end and keep running. */
265 sb_add_char (ptr
, more
);
266 line_start
= ptr
->len
;
267 more
= get_line (ptr
);
270 /* Return 1 on success, 0 on unexpected EOF. */
274 /* Pick up a token. */
277 get_token (int idx
, sb
*in
, sb
*name
)
280 && is_name_beginner (in
->ptr
[idx
]))
282 sb_add_char (name
, in
->ptr
[idx
++]);
284 && is_part_of_name (in
->ptr
[idx
]))
286 sb_add_char (name
, in
->ptr
[idx
++]);
289 && is_name_ender (in
->ptr
[idx
]))
291 sb_add_char (name
, in
->ptr
[idx
++]);
294 /* Ignore trailing &. */
295 if (macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
300 /* Pick up a string. */
303 getstring (int idx
, sb
*in
, sb
*acc
)
305 idx
= sb_skip_white (idx
, in
);
308 && (in
->ptr
[idx
] == '"'
309 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
310 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
312 if (in
->ptr
[idx
] == '<')
316 while ((in
->ptr
[idx
] != '>' || nest
)
319 if (in
->ptr
[idx
] == '!')
322 sb_add_char (acc
, in
->ptr
[idx
++]);
326 if (in
->ptr
[idx
] == '>')
328 if (in
->ptr
[idx
] == '<')
330 sb_add_char (acc
, in
->ptr
[idx
++]);
335 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
337 char tchar
= in
->ptr
[idx
];
342 while (idx
< in
->len
)
344 if (in
->ptr
[idx
- 1] == '\\')
349 if (macro_alternate
&& in
->ptr
[idx
] == '!')
353 sb_add_char (acc
, in
->ptr
[idx
]);
357 else if (escaped
&& in
->ptr
[idx
] == tchar
)
359 sb_add_char (acc
, tchar
);
364 if (in
->ptr
[idx
] == tchar
)
368 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
372 sb_add_char (acc
, in
->ptr
[idx
]);
382 /* Fetch string from the input stream,
384 'Bxyx<whitespace> -> return 'Bxyza
385 %<char> -> return string of decimal value of x
386 "<string>" -> return string
387 xyx<whitespace> -> return xyz
391 get_any_string (int idx
, sb
*in
, sb
*out
, int expand
, int pretend_quoted
)
394 idx
= sb_skip_white (idx
, in
);
398 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
400 while (!ISSEP (in
->ptr
[idx
]))
401 sb_add_char (out
, in
->ptr
[idx
++]);
403 else if (in
->ptr
[idx
] == '%'
409 /* Turns the next expression into a string. */
410 /* xgettext: no-c-format */
411 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
415 sprintf (buf
, "%d", val
);
416 sb_add_string (out
, buf
);
418 else if (in
->ptr
[idx
] == '"'
419 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
420 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
426 /* Keep the quotes. */
427 sb_add_char (out
, '\"');
429 idx
= getstring (idx
, in
, out
);
430 sb_add_char (out
, '\"');
434 idx
= getstring (idx
, in
, out
);
440 && (in
->ptr
[idx
] == '"'
441 || in
->ptr
[idx
] == '\''
443 || (in
->ptr
[idx
] != ' '
444 && in
->ptr
[idx
] != '\t'
445 && in
->ptr
[idx
] != ','
446 && (in
->ptr
[idx
] != '<'
447 || (! macro_alternate
&& ! macro_mri
)))))
449 if (in
->ptr
[idx
] == '"'
450 || in
->ptr
[idx
] == '\'')
452 char tchar
= in
->ptr
[idx
];
453 sb_add_char (out
, in
->ptr
[idx
++]);
455 && in
->ptr
[idx
] != tchar
)
456 sb_add_char (out
, in
->ptr
[idx
++]);
460 sb_add_char (out
, in
->ptr
[idx
++]);
468 /* Pick up the formal parameters of a macro definition. */
471 do_formals (macro_entry
*macro
, int idx
, sb
*in
)
473 formal_entry
**p
= ¯o
->formals
;
476 idx
= sb_skip_white (idx
, in
);
477 while (idx
< in
->len
)
479 formal_entry
*formal
;
482 formal
= (formal_entry
*) xmalloc (sizeof (formal_entry
));
484 sb_new (&formal
->name
);
485 sb_new (&formal
->def
);
486 sb_new (&formal
->actual
);
488 idx
= get_token (idx
, in
, &formal
->name
);
489 if (formal
->name
.len
== 0)
491 if (macro
->formal_count
)
495 idx
= sb_skip_white (idx
, in
);
496 /* This is a formal. */
497 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
500 idx
= get_any_string (idx
+ 1, in
, &formal
->def
, 1, 0);
501 idx
= sb_skip_white (idx
, in
);
504 /* Add to macro's hash table. */
505 name
= sb_terminate (&formal
->name
);
506 if (! hash_find (macro
->formal_hash
, name
))
507 hash_jam (macro
->formal_hash
, name
, formal
);
509 as_bad_where (macro
->file
,
511 _("A parameter named `%s' already exists for macro `%s'"),
515 formal
->index
= macro
->formal_count
++;
517 idx
= sb_skip_comma (idx
, in
);
518 if (idx
!= cidx
&& idx
>= in
->len
)
530 formal_entry
*formal
;
532 /* Add a special NARG formal, which macro_expand will set to the
533 number of arguments. */
534 formal
= (formal_entry
*) xmalloc (sizeof (formal_entry
));
536 sb_new (&formal
->name
);
537 sb_new (&formal
->def
);
538 sb_new (&formal
->actual
);
540 /* The same MRI assemblers which treat '@' characters also use
541 the name $NARG. At least until we find an exception. */
547 sb_add_string (&formal
->name
, name
);
549 /* Add to macro's hash table. */
550 if (hash_find (macro
->formal_hash
, name
))
551 as_bad_where (macro
->file
,
553 _("Reserved word `%s' used as parameter in macro `%s'"),
556 hash_jam (macro
->formal_hash
, name
, formal
);
558 formal
->index
= NARG_INDEX
;
566 /* Define a new macro. Returns NULL on success, otherwise returns an
567 error message. If NAMEP is not NULL, *NAMEP is set to the name of
568 the macro which was defined. */
571 define_macro (int idx
, sb
*in
, sb
*label
,
572 int (*get_line
) (sb
*),
573 char *file
, unsigned int line
,
578 const char *error
= NULL
;
580 macro
= (macro_entry
*) xmalloc (sizeof (macro_entry
));
581 sb_new (¯o
->sub
);
586 macro
->formal_count
= 0;
588 macro
->formal_hash
= hash_new ();
590 idx
= sb_skip_white (idx
, in
);
591 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
592 error
= _("unexpected end of file in macro `%s' definition");
593 if (label
!= NULL
&& label
->len
!= 0)
595 sb_add_sb (&name
, label
);
596 macro
->name
= sb_terminate (&name
);
597 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
599 /* It's the label: MACRO (formals,...) sort */
600 idx
= do_formals (macro
, idx
+ 1, in
);
601 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
602 idx
= sb_skip_white (idx
+ 1, in
);
604 error
= _("missing `)' after formals in macro definition `%s'");
608 /* It's the label: MACRO formals,... sort */
609 idx
= do_formals (macro
, idx
, in
);
616 idx
= get_token (idx
, in
, &name
);
617 macro
->name
= sb_terminate (&name
);
619 error
= _("Missing macro name");
620 cidx
= sb_skip_white (idx
, in
);
621 idx
= sb_skip_comma (cidx
, in
);
622 if (idx
== cidx
|| idx
< in
->len
)
623 idx
= do_formals (macro
, idx
, in
);
627 if (!error
&& idx
< in
->len
)
628 error
= _("Bad parameter list for macro `%s'");
630 /* And stick it in the macro hash table. */
631 for (idx
= 0; idx
< name
.len
; idx
++)
632 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
633 if (hash_find (macro_hash
, macro
->name
))
634 error
= _("Macro `%s' was already defined");
636 error
= hash_jam (macro_hash
, macro
->name
, (PTR
) macro
);
639 *namep
= macro
->name
;
649 /* Scan a token, and then skip KIND. */
652 get_apost_token (int idx
, sb
*in
, sb
*name
, int kind
)
654 idx
= get_token (idx
, in
, name
);
656 && in
->ptr
[idx
] == kind
657 && (! macro_mri
|| macro_strip_at
)
658 && (! macro_strip_at
|| kind
== '@'))
663 /* Substitute the actual value for a formal parameter. */
666 sub_actual (int start
, sb
*in
, sb
*t
, struct hash_control
*formal_hash
,
667 int kind
, sb
*out
, int copyifnotthere
)
672 src
= get_apost_token (start
, in
, t
, kind
);
673 /* See if it's in the macro's hash table, unless this is
674 macro_strip_at and kind is '@' and the token did not end in '@'. */
677 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
680 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (t
));
685 sb_add_sb (out
, &ptr
->actual
);
689 sb_add_sb (out
, &ptr
->def
);
692 else if (kind
== '&')
694 /* Doing this permits people to use & in macro bodies. */
695 sb_add_char (out
, '&');
698 else if (copyifnotthere
)
704 sb_add_char (out
, '\\');
710 /* Expand the body of a macro. */
713 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
714 struct hash_control
*formal_hash
, const macro_entry
*macro
)
717 int src
= 0, inquote
= 0, macro_line
= 0;
718 formal_entry
*loclist
= NULL
;
719 const char *err
= NULL
;
723 while (src
< in
->len
&& !err
)
725 if (in
->ptr
[src
] == '&')
730 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
731 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
733 sb_add_char (out
, in
->ptr
[src
++]);
737 /* FIXME: Why do we do this? */
738 /* At least in alternate mode this seems correct; without this
739 one can't append a literal to a parameter. */
740 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
743 else if (in
->ptr
[src
] == '\\')
746 if (src
< in
->len
&& in
->ptr
[src
] == '(')
748 /* Sub in till the next ')' literally. */
750 while (src
< in
->len
&& in
->ptr
[src
] != ')')
752 sb_add_char (out
, in
->ptr
[src
++]);
757 err
= _("missing `)'");
759 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
761 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
763 /* Sub in the macro invocation number. */
767 sprintf (buffer
, "%d", macro_number
);
768 sb_add_string (out
, buffer
);
770 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
772 /* This is a preprocessor variable name, we don't do them
774 sb_add_char (out
, '\\');
775 sb_add_char (out
, '&');
778 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
783 if (ISDIGIT (in
->ptr
[src
]))
784 ind
= in
->ptr
[src
] - '0';
785 else if (ISUPPER (in
->ptr
[src
]))
786 ind
= in
->ptr
[src
] - 'A' + 10;
788 ind
= in
->ptr
[src
] - 'a' + 10;
790 for (f
= formals
; f
!= NULL
; f
= f
->next
)
792 if (f
->index
== ind
- 1)
794 if (f
->actual
.len
!= 0)
795 sb_add_sb (out
, &f
->actual
);
797 sb_add_sb (out
, &f
->def
);
805 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
808 else if ((macro_alternate
|| macro_mri
)
809 && is_name_beginner (in
->ptr
[src
])
812 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
815 || src
+ 5 >= in
->len
816 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
817 || ! ISWHITE (in
->ptr
[src
+ 5]))
820 src
= sub_actual (src
, in
, &t
, formal_hash
,
821 (macro_strip_at
&& inquote
) ? '@' : '\'',
826 src
= sb_skip_white (src
+ 5, in
);
827 while (in
->ptr
[src
] != '\n')
832 f
= (formal_entry
*) xmalloc (sizeof (formal_entry
));
834 src
= get_token (src
, in
, &f
->name
);
835 name
= sb_terminate (&f
->name
);
836 if (! hash_find (formal_hash
, name
))
843 f
->index
= LOCAL_INDEX
;
847 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
848 sb_add_string (&f
->actual
, buf
);
850 err
= hash_jam (formal_hash
, name
, f
);
856 as_bad_where (macro
->file
,
857 macro
->line
+ macro_line
,
858 _("`%s' was already used as parameter (or another local) name"),
864 src
= sb_skip_comma (src
, in
);
868 else if (in
->ptr
[src
] == '"'
869 || (macro_mri
&& in
->ptr
[src
] == '\''))
872 sb_add_char (out
, in
->ptr
[src
++]);
874 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
878 && in
->ptr
[src
] == '@')
880 sb_add_char (out
, '@');
885 && in
->ptr
[src
] == '='
887 && in
->ptr
[src
+ 1] == '=')
892 src
= get_token (src
+ 2, in
, &t
);
893 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (&t
));
896 /* FIXME: We should really return a warning string here,
897 but we can't, because the == might be in the MRI
898 comment field, and, since the nature of the MRI
899 comment field depends upon the exact instruction
900 being used, we don't have enough information here to
901 figure out whether it is or not. Instead, we leave
902 the == in place, which should cause a syntax error if
903 it is not in a comment. */
904 sb_add_char (out
, '=');
905 sb_add_char (out
, '=');
912 sb_add_string (out
, "-1");
916 sb_add_char (out
, '0');
922 if (in
->ptr
[src
] == '\n')
924 sb_add_char (out
, in
->ptr
[src
++]);
930 while (loclist
!= NULL
)
935 /* Setting the value to NULL effectively deletes the entry. We
936 avoid calling hash_delete because it doesn't reclaim memory. */
937 hash_jam (formal_hash
, sb_terminate (&loclist
->name
), NULL
);
938 sb_kill (&loclist
->name
);
939 sb_kill (&loclist
->def
);
940 sb_kill (&loclist
->actual
);
948 /* Assign values to the formal parameters of a macro, and expand the
952 macro_expand (int idx
, sb
*in
, macro_entry
*m
, sb
*out
)
957 int is_positional
= 0;
964 /* Reset any old value the actuals may have. */
965 for (f
= m
->formals
; f
; f
= f
->next
)
966 sb_reset (&f
->actual
);
968 while (f
!= NULL
&& f
->index
< 0)
973 /* The macro may be called with an optional qualifier, which may
974 be referred to in the macro body as \0. */
975 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
977 /* The Microtec assembler ignores this if followed by a white space.
978 (Macro invocation with empty extension) */
981 && in
->ptr
[idx
] != ' '
982 && in
->ptr
[idx
] != '\t')
986 n
= (formal_entry
*) xmalloc (sizeof (formal_entry
));
990 n
->index
= QUAL_INDEX
;
992 n
->next
= m
->formals
;
995 idx
= get_any_string (idx
, in
, &n
->actual
, 1, 0);
1000 /* Peel off the actuals and store them away in the hash tables' actuals. */
1001 idx
= sb_skip_white (idx
, in
);
1002 while (idx
< in
->len
)
1006 /* Look and see if it's a positional or keyword arg. */
1008 while (scan
< in
->len
1009 && !ISSEP (in
->ptr
[scan
])
1010 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1011 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1013 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1017 /* It's OK to go from positional to keyword. */
1019 /* This is a keyword arg, fetch the formal name and
1020 then the actual stuff. */
1022 idx
= get_token (idx
, in
, &t
);
1023 if (in
->ptr
[idx
] != '=')
1024 return _("confusion in formal parameters");
1026 /* Lookup the formal in the macro's list. */
1027 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1029 return _("macro formal argument does not exist");
1032 /* Insert this value into the right place. */
1033 sb_reset (&ptr
->actual
);
1034 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
, 0, 0);
1035 if (ptr
->actual
.len
> 0)
1041 /* This is a positional arg. */
1044 return _("can't mix positional and keyword arguments");
1052 return _("too many positional arguments");
1054 f
= (formal_entry
*) xmalloc (sizeof (formal_entry
));
1057 sb_new (&f
->actual
);
1061 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1062 if ((*pf
)->index
>= c
)
1063 c
= (*pf
)->index
+ 1;
1070 sb_reset (&f
->actual
);
1071 idx
= get_any_string (idx
, in
, &f
->actual
, 1, 0);
1072 if (f
->actual
.len
> 0)
1078 while (f
!= NULL
&& f
->index
< 0);
1082 idx
= sb_skip_comma (idx
, in
);
1085 if (in
->ptr
[idx
] == ',')
1087 if (ISWHITE (in
->ptr
[idx
]))
1097 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1098 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1099 sb_reset (&ptr
->actual
);
1100 sprintf (buffer
, "%d", narg
);
1101 sb_add_string (&ptr
->actual
, buffer
);
1104 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1106 /* Discard any unnamed formal arguments. */
1114 if ((*pf
)->name
.len
!= 0)
1118 sb_kill (&(*pf
)->name
);
1119 sb_kill (&(*pf
)->def
);
1120 sb_kill (&(*pf
)->actual
);
1135 /* Check for a macro. If one is found, put the expansion into
1136 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1139 check_macro (const char *line
, sb
*expand
,
1140 const char **error
, macro_entry
**info
)
1147 if (! is_name_beginner (*line
)
1148 && (! macro_mri
|| *line
!= '.'))
1152 while (is_part_of_name (*s
))
1154 if (is_name_ender (*s
))
1157 copy
= (char *) alloca (s
- line
+ 1);
1158 memcpy (copy
, line
, s
- line
);
1159 copy
[s
- line
] = '\0';
1160 for (cs
= copy
; *cs
!= '\0'; cs
++)
1161 *cs
= TOLOWER (*cs
);
1163 macro
= (macro_entry
*) hash_find (macro_hash
, copy
);
1168 /* Wrap the line up in an sb. */
1170 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1171 sb_add_char (&line_sb
, *s
++);
1174 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1178 /* Export the macro information if requested. */
1185 /* Free the memory allocated to a macro. */
1188 free_macro(macro_entry
*macro
)
1190 formal_entry
*formal
;
1192 for (formal
= macro
->formals
; formal
; )
1196 sb_kill (&formal
->name
);
1197 sb_kill (&formal
->def
);
1198 sb_kill (&formal
->actual
);
1200 formal
= formal
->next
;
1203 hash_die (macro
->formal_hash
);
1204 sb_kill (¯o
->sub
);
1208 /* Delete a macro. */
1211 delete_macro (const char *name
)
1217 len
= strlen (name
);
1218 copy
= (char *) alloca (len
+ 1);
1219 for (i
= 0; i
< len
; ++i
)
1220 copy
[i
] = TOLOWER (name
[i
]);
1223 /* Since hash_delete doesn't free memory, just clear out the entry. */
1224 if ((macro
= hash_find (macro_hash
, copy
)) != NULL
)
1226 hash_jam (macro_hash
, copy
, NULL
);
1230 as_warn (_("Attempt to purge non-existant macro `%s'"), copy
);
1233 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1234 combined macro definition and execution. This returns NULL on
1235 success, or an error message otherwise. */
1238 expand_irp (int irpc
, int idx
, sb
*in
, sb
*out
, int (*get_line
) (sb
*))
1242 struct hash_control
*h
;
1245 idx
= sb_skip_white (idx
, in
);
1248 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1249 return _("unexpected end of file in irp or irpc");
1255 idx
= get_token (idx
, in
, &f
.name
);
1256 if (f
.name
.len
== 0)
1257 return _("missing model parameter");
1260 err
= hash_jam (h
, sb_terminate (&f
.name
), &f
);
1269 idx
= sb_skip_comma (idx
, in
);
1272 /* Expand once with a null string. */
1273 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1277 if (irpc
&& in
->ptr
[idx
] == '"')
1279 while (idx
< in
->len
)
1282 idx
= get_any_string (idx
, in
, &f
.actual
, 1, 0);
1285 if (in
->ptr
[idx
] == '"')
1289 nxt
= sb_skip_white (idx
+ 1, in
);
1296 sb_reset (&f
.actual
);
1297 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1300 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1304 idx
= sb_skip_comma (idx
, in
);
1306 idx
= sb_skip_white (idx
, in
);