fbe266612c30036434e397a36d636bbbca3ce22a
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 and find the first pseudo op on the line. */
186 if (! NO_PSEUDO_DOT
&& ! flag_m68k_mri
)
188 /* With normal syntax we can suck what we want till we get
189 to the dot. With the alternate, labels have to start in
190 the first column, since we can't tell what's a label and
193 if (! LABELS_WITHOUT_COLONS
)
195 /* Skip leading whitespace. */
196 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
202 /* Skip over a label, if any. */
203 if (i
>= ptr
->len
|| ! is_name_beginner (ptr
->ptr
[i
]))
206 while (i
< ptr
->len
&& is_part_of_name (ptr
->ptr
[i
]))
208 if (i
< ptr
->len
&& is_name_ender (ptr
->ptr
[i
]))
210 if (LABELS_WITHOUT_COLONS
)
212 /* Skip whitespace. */
213 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
215 /* Check for the colon. */
216 if (i
>= ptr
->len
|| ptr
->ptr
[i
] != ':')
226 /* Skip trailing whitespace. */
227 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
230 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
234 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
237 && strncasecmp (ptr
->ptr
+ i
, "IRPC", from_len
= 4) != 0
238 && strncasecmp (ptr
->ptr
+ i
, "IRP", from_len
= 3) != 0
239 && strncasecmp (ptr
->ptr
+ i
, "IREPC", from_len
= 5) != 0
240 && strncasecmp (ptr
->ptr
+ i
, "IREP", from_len
= 4) != 0
241 && strncasecmp (ptr
->ptr
+ i
, "REPT", from_len
= 4) != 0
242 && strncasecmp (ptr
->ptr
+ i
, "REP", from_len
= 3) != 0)
245 ? strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0
247 && (ptr
->len
== (i
+ from_len
)
248 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
249 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
251 if (strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
252 && (ptr
->len
== (i
+ to_len
)
253 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
254 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
259 /* Reset the string to not include the ending rune. */
260 ptr
->len
= line_start
;
266 /* Add the original end-of-line char to the end and keep running. */
267 sb_add_char (ptr
, more
);
268 line_start
= ptr
->len
;
269 more
= get_line (ptr
);
272 /* Return 1 on success, 0 on unexpected EOF. */
276 /* Pick up a token. */
279 get_token (int idx
, sb
*in
, sb
*name
)
282 && is_name_beginner (in
->ptr
[idx
]))
284 sb_add_char (name
, in
->ptr
[idx
++]);
286 && is_part_of_name (in
->ptr
[idx
]))
288 sb_add_char (name
, in
->ptr
[idx
++]);
291 && is_name_ender (in
->ptr
[idx
]))
293 sb_add_char (name
, in
->ptr
[idx
++]);
296 /* Ignore trailing &. */
297 if (macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
302 /* Pick up a string. */
305 getstring (int idx
, sb
*in
, sb
*acc
)
308 && (in
->ptr
[idx
] == '"'
309 || in
->ptr
[idx
] == '('
310 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
311 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
313 if (in
->ptr
[idx
] == '<'
314 || in
->ptr
[idx
] == '(')
317 char start_char
= in
->ptr
[idx
];
318 char end_char
= in
->ptr
[idx
] == '<' ? '>' : ')';
321 while ((in
->ptr
[idx
] != end_char
|| nest
)
324 if (in
->ptr
[idx
] == '!')
327 sb_add_char (acc
, in
->ptr
[idx
++]);
331 if (in
->ptr
[idx
] == end_char
)
333 if (in
->ptr
[idx
] == start_char
)
335 sb_add_char (acc
, in
->ptr
[idx
++]);
340 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
342 char tchar
= in
->ptr
[idx
];
347 while (idx
< in
->len
)
349 if (in
->ptr
[idx
- 1] == '\\')
354 if (macro_alternate
&& in
->ptr
[idx
] == '!')
358 sb_add_char (acc
, in
->ptr
[idx
]);
362 else if (escaped
&& in
->ptr
[idx
] == tchar
)
364 sb_add_char (acc
, tchar
);
369 if (in
->ptr
[idx
] == tchar
)
373 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
377 sb_add_char (acc
, in
->ptr
[idx
]);
387 /* Fetch string from the input stream,
389 'Bxyx<whitespace> -> return 'Bxyza
390 %<expr> -> return string of decimal value of <expr>
391 "string" -> return string
392 (string) -> return string
393 xyx<whitespace> -> return xyz. */
396 get_any_string (int idx
, sb
*in
, sb
*out
)
399 idx
= sb_skip_white (idx
, in
);
403 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
405 while (!ISSEP (in
->ptr
[idx
]))
406 sb_add_char (out
, in
->ptr
[idx
++]);
408 else if (in
->ptr
[idx
] == '%' && macro_alternate
)
413 /* Turns the next expression into a string. */
414 /* xgettext: no-c-format */
415 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
419 sprintf (buf
, "%d", val
);
420 sb_add_string (out
, buf
);
422 else if (in
->ptr
[idx
] == '"'
423 || in
->ptr
[idx
] == '('
424 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
425 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
427 if (macro_alternate
&& ! macro_strip_at
)
429 /* Keep the quotes. */
430 sb_add_char (out
, '\"');
432 idx
= getstring (idx
, in
, out
);
433 sb_add_char (out
, '\"');
437 idx
= getstring (idx
, in
, out
);
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
];
454 sb_add_char (out
, in
->ptr
[idx
++]);
456 && in
->ptr
[idx
] != tchar
)
457 sb_add_char (out
, in
->ptr
[idx
++]);
461 sb_add_char (out
, in
->ptr
[idx
++]);
469 /* Allocate a new formal. */
471 static formal_entry
*
474 formal_entry
*formal
;
476 formal
= xmalloc (sizeof (formal_entry
));
478 sb_new (&formal
->name
);
479 sb_new (&formal
->def
);
480 sb_new (&formal
->actual
);
482 formal
->type
= FORMAL_OPTIONAL
;
489 del_formal (formal_entry
*formal
)
491 sb_kill (&formal
->actual
);
492 sb_kill (&formal
->def
);
493 sb_kill (&formal
->name
);
497 /* Pick up the formal parameters of a macro definition. */
500 do_formals (macro_entry
*macro
, int idx
, sb
*in
)
502 formal_entry
**p
= ¯o
->formals
;
505 idx
= sb_skip_white (idx
, in
);
506 while (idx
< in
->len
)
508 formal_entry
*formal
= new_formal ();
511 idx
= get_token (idx
, in
, &formal
->name
);
512 if (formal
->name
.len
== 0)
514 if (macro
->formal_count
)
518 idx
= sb_skip_white (idx
, in
);
519 /* This is a formal. */
520 name
= sb_terminate (&formal
->name
);
523 && in
->ptr
[idx
] == ':'
524 && (! is_name_beginner (':')
525 || idx
+ 1 >= in
->len
526 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
528 /* Got a qualifier. */
532 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
533 sb_terminate (&qual
);
535 as_bad_where (macro
->file
,
537 _("Missing parameter qualifier for `%s' in macro `%s'"),
540 else if (strcmp (qual
.ptr
, "req") == 0)
541 formal
->type
= FORMAL_REQUIRED
;
542 else if (strcmp (qual
.ptr
, "vararg") == 0)
543 formal
->type
= FORMAL_VARARG
;
545 as_bad_where (macro
->file
,
547 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
552 idx
= sb_skip_white (idx
, in
);
554 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
557 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
558 idx
= sb_skip_white (idx
, in
);
559 if (formal
->type
== FORMAL_REQUIRED
)
561 sb_reset (&formal
->def
);
562 as_warn_where (macro
->file
,
564 _("Pointless default value for required parameter `%s' in macro `%s'"),
570 /* Add to macro's hash table. */
571 if (! hash_find (macro
->formal_hash
, name
))
572 hash_jam (macro
->formal_hash
, name
, formal
);
574 as_bad_where (macro
->file
,
576 _("A parameter named `%s' already exists for macro `%s'"),
580 formal
->index
= macro
->formal_count
++;
583 if (formal
->type
== FORMAL_VARARG
)
586 idx
= sb_skip_comma (idx
, in
);
587 if (idx
!= cidx
&& idx
>= in
->len
)
596 formal_entry
*formal
= new_formal ();
598 /* Add a special NARG formal, which macro_expand will set to the
599 number of arguments. */
600 /* The same MRI assemblers which treat '@' characters also use
601 the name $NARG. At least until we find an exception. */
607 sb_add_string (&formal
->name
, name
);
609 /* Add to macro's hash table. */
610 if (hash_find (macro
->formal_hash
, name
))
611 as_bad_where (macro
->file
,
613 _("Reserved word `%s' used as parameter in macro `%s'"),
616 hash_jam (macro
->formal_hash
, name
, formal
);
618 formal
->index
= NARG_INDEX
;
625 /* Define a new macro. Returns NULL on success, otherwise returns an
626 error message. If NAMEP is not NULL, *NAMEP is set to the name of
627 the macro which was defined. */
630 define_macro (int idx
, sb
*in
, sb
*label
,
631 int (*get_line
) (sb
*),
632 char *file
, unsigned int line
,
637 const char *error
= NULL
;
639 macro
= (macro_entry
*) xmalloc (sizeof (macro_entry
));
640 sb_new (¯o
->sub
);
645 macro
->formal_count
= 0;
647 macro
->formal_hash
= hash_new ();
649 idx
= sb_skip_white (idx
, in
);
650 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
651 error
= _("unexpected end of file in macro `%s' definition");
652 if (label
!= NULL
&& label
->len
!= 0)
654 sb_add_sb (&name
, label
);
655 macro
->name
= sb_terminate (&name
);
656 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
658 /* It's the label: MACRO (formals,...) sort */
659 idx
= do_formals (macro
, idx
+ 1, in
);
660 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
661 idx
= sb_skip_white (idx
+ 1, in
);
663 error
= _("missing `)' after formals in macro definition `%s'");
667 /* It's the label: MACRO formals,... sort */
668 idx
= do_formals (macro
, idx
, in
);
675 idx
= get_token (idx
, in
, &name
);
676 macro
->name
= sb_terminate (&name
);
678 error
= _("Missing macro name");
679 cidx
= sb_skip_white (idx
, in
);
680 idx
= sb_skip_comma (cidx
, in
);
681 if (idx
== cidx
|| idx
< in
->len
)
682 idx
= do_formals (macro
, idx
, in
);
686 if (!error
&& idx
< in
->len
)
687 error
= _("Bad parameter list for macro `%s'");
689 /* And stick it in the macro hash table. */
690 for (idx
= 0; idx
< name
.len
; idx
++)
691 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
692 if (hash_find (macro_hash
, macro
->name
))
693 error
= _("Macro `%s' was already defined");
695 error
= hash_jam (macro_hash
, macro
->name
, (PTR
) macro
);
698 *namep
= macro
->name
;
708 /* Scan a token, and then skip KIND. */
711 get_apost_token (int idx
, sb
*in
, sb
*name
, int kind
)
713 idx
= get_token (idx
, in
, name
);
715 && in
->ptr
[idx
] == kind
716 && (! macro_mri
|| macro_strip_at
)
717 && (! macro_strip_at
|| kind
== '@'))
722 /* Substitute the actual value for a formal parameter. */
725 sub_actual (int start
, sb
*in
, sb
*t
, struct hash_control
*formal_hash
,
726 int kind
, sb
*out
, int copyifnotthere
)
731 src
= get_apost_token (start
, in
, t
, kind
);
732 /* See if it's in the macro's hash table, unless this is
733 macro_strip_at and kind is '@' and the token did not end in '@'. */
736 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
739 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (t
));
744 sb_add_sb (out
, &ptr
->actual
);
748 sb_add_sb (out
, &ptr
->def
);
751 else if (kind
== '&')
753 /* Doing this permits people to use & in macro bodies. */
754 sb_add_char (out
, '&');
757 else if (copyifnotthere
)
763 sb_add_char (out
, '\\');
769 /* Expand the body of a macro. */
772 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
773 struct hash_control
*formal_hash
, const macro_entry
*macro
)
776 int src
= 0, inquote
= 0, macro_line
= 0;
777 formal_entry
*loclist
= NULL
;
778 const char *err
= NULL
;
782 while (src
< in
->len
&& !err
)
784 if (in
->ptr
[src
] == '&')
789 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
790 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
792 sb_add_char (out
, in
->ptr
[src
++]);
796 /* FIXME: Why do we do this? */
797 /* At least in alternate mode this seems correct; without this
798 one can't append a literal to a parameter. */
799 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
802 else if (in
->ptr
[src
] == '\\')
805 if (src
< in
->len
&& in
->ptr
[src
] == '(')
807 /* Sub in till the next ')' literally. */
809 while (src
< in
->len
&& in
->ptr
[src
] != ')')
811 sb_add_char (out
, in
->ptr
[src
++]);
816 err
= _("missing `)'");
818 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
820 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
822 /* Sub in the macro invocation number. */
826 sprintf (buffer
, "%d", macro_number
);
827 sb_add_string (out
, buffer
);
829 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
831 /* This is a preprocessor variable name, we don't do them
833 sb_add_char (out
, '\\');
834 sb_add_char (out
, '&');
837 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
842 if (ISDIGIT (in
->ptr
[src
]))
843 ind
= in
->ptr
[src
] - '0';
844 else if (ISUPPER (in
->ptr
[src
]))
845 ind
= in
->ptr
[src
] - 'A' + 10;
847 ind
= in
->ptr
[src
] - 'a' + 10;
849 for (f
= formals
; f
!= NULL
; f
= f
->next
)
851 if (f
->index
== ind
- 1)
853 if (f
->actual
.len
!= 0)
854 sb_add_sb (out
, &f
->actual
);
856 sb_add_sb (out
, &f
->def
);
864 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
867 else if ((macro_alternate
|| macro_mri
)
868 && is_name_beginner (in
->ptr
[src
])
871 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
874 || src
+ 5 >= in
->len
875 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
876 || ! ISWHITE (in
->ptr
[src
+ 5]))
879 src
= sub_actual (src
, in
, &t
, formal_hash
,
880 (macro_strip_at
&& inquote
) ? '@' : '\'',
885 src
= sb_skip_white (src
+ 5, in
);
886 while (in
->ptr
[src
] != '\n')
889 formal_entry
*f
= new_formal ();
891 src
= get_token (src
, in
, &f
->name
);
892 name
= sb_terminate (&f
->name
);
893 if (! hash_find (formal_hash
, name
))
898 f
->index
= LOCAL_INDEX
;
902 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
903 sb_add_string (&f
->actual
, buf
);
905 err
= hash_jam (formal_hash
, name
, f
);
911 as_bad_where (macro
->file
,
912 macro
->line
+ macro_line
,
913 _("`%s' was already used as parameter (or another local) name"),
918 src
= sb_skip_comma (src
, in
);
922 else if (in
->ptr
[src
] == '"'
923 || (macro_mri
&& in
->ptr
[src
] == '\''))
926 sb_add_char (out
, in
->ptr
[src
++]);
928 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
932 && in
->ptr
[src
] == '@')
934 sb_add_char (out
, '@');
939 && in
->ptr
[src
] == '='
941 && in
->ptr
[src
+ 1] == '=')
946 src
= get_token (src
+ 2, in
, &t
);
947 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (&t
));
950 /* FIXME: We should really return a warning string here,
951 but we can't, because the == might be in the MRI
952 comment field, and, since the nature of the MRI
953 comment field depends upon the exact instruction
954 being used, we don't have enough information here to
955 figure out whether it is or not. Instead, we leave
956 the == in place, which should cause a syntax error if
957 it is not in a comment. */
958 sb_add_char (out
, '=');
959 sb_add_char (out
, '=');
966 sb_add_string (out
, "-1");
970 sb_add_char (out
, '0');
976 if (in
->ptr
[src
] == '\n')
978 sb_add_char (out
, in
->ptr
[src
++]);
984 while (loclist
!= NULL
)
989 /* Setting the value to NULL effectively deletes the entry. We
990 avoid calling hash_delete because it doesn't reclaim memory. */
991 hash_jam (formal_hash
, sb_terminate (&loclist
->name
), NULL
);
992 del_formal (loclist
);
999 /* Assign values to the formal parameters of a macro, and expand the
1003 macro_expand (int idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1008 int is_positional
= 0;
1011 const char *err
= NULL
;
1015 /* Reset any old value the actuals may have. */
1016 for (f
= m
->formals
; f
; f
= f
->next
)
1017 sb_reset (&f
->actual
);
1019 while (f
!= NULL
&& f
->index
< 0)
1024 /* The macro may be called with an optional qualifier, which may
1025 be referred to in the macro body as \0. */
1026 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1028 /* The Microtec assembler ignores this if followed by a white space.
1029 (Macro invocation with empty extension) */
1032 && in
->ptr
[idx
] != ' '
1033 && in
->ptr
[idx
] != '\t')
1035 formal_entry
*n
= new_formal ();
1037 n
->index
= QUAL_INDEX
;
1039 n
->next
= m
->formals
;
1042 idx
= get_any_string (idx
, in
, &n
->actual
);
1047 /* Peel off the actuals and store them away in the hash tables' actuals. */
1048 idx
= sb_skip_white (idx
, in
);
1049 while (idx
< in
->len
)
1053 /* Look and see if it's a positional or keyword arg. */
1055 while (scan
< in
->len
1056 && !ISSEP (in
->ptr
[scan
])
1057 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1058 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1060 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1064 /* It's OK to go from positional to keyword. */
1066 /* This is a keyword arg, fetch the formal name and
1067 then the actual stuff. */
1069 idx
= get_token (idx
, in
, &t
);
1070 if (in
->ptr
[idx
] != '=')
1072 err
= _("confusion in formal parameters");
1076 /* Lookup the formal in the macro's list. */
1077 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1079 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1084 /* Insert this value into the right place. */
1085 if (ptr
->actual
.len
)
1087 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1090 sb_reset (&ptr
->actual
);
1092 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1093 if (ptr
->actual
.len
> 0)
1099 /* This is a positional arg. */
1103 err
= _("can't mix positional and keyword arguments");
1114 err
= _("too many positional arguments");
1121 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1122 if ((*pf
)->index
>= c
)
1123 c
= (*pf
)->index
+ 1;
1130 if (f
->type
!= FORMAL_VARARG
)
1131 idx
= get_any_string (idx
, in
, &f
->actual
);
1134 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1137 if (f
->actual
.len
> 0)
1143 while (f
!= NULL
&& f
->index
< 0);
1147 idx
= sb_skip_comma (idx
, in
);
1150 if (in
->ptr
[idx
] == ',')
1152 if (ISWHITE (in
->ptr
[idx
]))
1159 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1161 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1162 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1172 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1173 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1174 sprintf (buffer
, "%d", narg
);
1175 sb_add_string (&ptr
->actual
, buffer
);
1178 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1181 /* Discard any unnamed formal arguments. */
1189 if ((*pf
)->name
.len
!= 0)
1207 /* Check for a macro. If one is found, put the expansion into
1208 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1211 check_macro (const char *line
, sb
*expand
,
1212 const char **error
, macro_entry
**info
)
1219 if (! is_name_beginner (*line
)
1220 && (! macro_mri
|| *line
!= '.'))
1224 while (is_part_of_name (*s
))
1226 if (is_name_ender (*s
))
1229 copy
= (char *) alloca (s
- line
+ 1);
1230 memcpy (copy
, line
, s
- line
);
1231 copy
[s
- line
] = '\0';
1232 for (cs
= copy
; *cs
!= '\0'; cs
++)
1233 *cs
= TOLOWER (*cs
);
1235 macro
= (macro_entry
*) hash_find (macro_hash
, copy
);
1240 /* Wrap the line up in an sb. */
1242 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1243 sb_add_char (&line_sb
, *s
++);
1246 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1250 /* Export the macro information if requested. */
1257 /* Free the memory allocated to a macro. */
1260 free_macro(macro_entry
*macro
)
1262 formal_entry
*formal
;
1264 for (formal
= macro
->formals
; formal
; )
1269 formal
= formal
->next
;
1272 hash_die (macro
->formal_hash
);
1273 sb_kill (¯o
->sub
);
1277 /* Delete a macro. */
1280 delete_macro (const char *name
)
1286 len
= strlen (name
);
1287 copy
= (char *) alloca (len
+ 1);
1288 for (i
= 0; i
< len
; ++i
)
1289 copy
[i
] = TOLOWER (name
[i
]);
1292 /* Since hash_delete doesn't free memory, just clear out the entry. */
1293 if ((macro
= hash_find (macro_hash
, copy
)) != NULL
)
1295 hash_jam (macro_hash
, copy
, NULL
);
1299 as_warn (_("Attempt to purge non-existant macro `%s'"), copy
);
1302 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1303 combined macro definition and execution. This returns NULL on
1304 success, or an error message otherwise. */
1307 expand_irp (int irpc
, int idx
, sb
*in
, sb
*out
, int (*get_line
) (sb
*))
1311 struct hash_control
*h
;
1314 idx
= sb_skip_white (idx
, in
);
1317 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1318 return _("unexpected end of file in irp or irpc");
1324 idx
= get_token (idx
, in
, &f
.name
);
1325 if (f
.name
.len
== 0)
1326 return _("missing model parameter");
1329 err
= hash_jam (h
, sb_terminate (&f
.name
), &f
);
1335 f
.type
= FORMAL_OPTIONAL
;
1339 idx
= sb_skip_comma (idx
, in
);
1342 /* Expand once with a null string. */
1343 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1347 if (irpc
&& in
->ptr
[idx
] == '"')
1349 while (idx
< in
->len
)
1352 idx
= get_any_string (idx
, in
, &f
.actual
);
1355 if (in
->ptr
[idx
] == '"')
1359 nxt
= sb_skip_white (idx
+ 1, in
);
1366 sb_reset (&f
.actual
);
1367 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1370 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1374 idx
= sb_skip_comma (idx
, in
);
1376 idx
= sb_skip_white (idx
, in
);
1381 sb_kill (&f
.actual
);
This page took 0.096398 seconds and 4 git commands to generate.