1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4 Written by Steve and Judy Chamberlain of Cygnus Support,
7 This file is part of GAS, the GNU Assembler.
9 GAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GAS; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "safe-ctype.h"
29 /* The routines in this file handle macro definition and expansion.
30 They are called by gas. */
32 #define ISWHITE(x) ((x) == ' ' || (x) == '\t')
35 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
36 || (x) == ')' || (x) == '(' \
37 || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
40 ((x) == 'b' || (x) == 'B' \
41 || (x) == 'q' || (x) == 'Q' \
42 || (x) == 'h' || (x) == 'H' \
43 || (x) == 'd' || (x) == 'D')
45 /* The macro hash table. */
47 struct htab
*macro_hash
;
49 /* Whether any macros have been defined. */
53 /* Whether we are in alternate syntax mode. */
55 static int macro_alternate
;
57 /* Whether we are in MRI mode. */
61 /* Whether we should strip '@' characters. */
63 static int macro_strip_at
;
65 /* Function to use to parse an expression. */
67 static size_t (*macro_expr
) (const char *, size_t, sb
*, offsetT
*);
69 /* Number of macro expansions that have been done. */
71 static int macro_number
;
73 /* Initialize macro processing. */
76 macro_init (int alternate
, int mri
, int strip_at
,
77 size_t (*exp
) (const char *, size_t, sb
*, offsetT
*))
79 macro_hash
= htab_create_alloc (16, hash_macro_entry
, eq_macro_entry
,
82 macro_alternate
= alternate
;
84 macro_strip_at
= strip_at
;
88 /* Switch in and out of alternate mode on the fly. */
91 macro_set_alternate (int alternate
)
93 macro_alternate
= alternate
;
96 /* Switch in and out of MRI mode on the fly. */
99 macro_mri_mode (int mri
)
104 /* Read input lines till we get to a TO string.
105 Increase nesting depth if we get a FROM string.
106 Put the results into sb at PTR.
107 FROM may be NULL (or will be ignored) if TO is "ENDR".
108 Add a new input line to an sb using GET_LINE.
109 Return 1 on success, 0 on unexpected EOF. */
112 buffer_and_nest (const char *from
, const char *to
, sb
*ptr
,
113 size_t (*get_line
) (sb
*))
116 size_t to_len
= strlen (to
);
118 size_t line_start
= ptr
->len
;
119 size_t more
= get_line (ptr
);
121 if (to_len
== 4 && strcasecmp (to
, "ENDR") == 0)
127 from_len
= strlen (from
);
131 /* Try to find the first pseudo op on the line. */
132 size_t i
= line_start
;
133 bfd_boolean had_colon
= FALSE
;
135 /* With normal syntax we can suck what we want till we get
136 to the dot. With the alternate, labels have to start in
137 the first column, since we can't tell what's a label and
138 what's a pseudoop. */
140 if (! LABELS_WITHOUT_COLONS
)
142 /* Skip leading whitespace. */
143 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
149 /* Skip over a label, if any. */
150 if (i
>= ptr
->len
|| ! is_name_beginner (ptr
->ptr
[i
]))
153 while (i
< ptr
->len
&& is_part_of_name (ptr
->ptr
[i
]))
155 if (i
< ptr
->len
&& is_name_ender (ptr
->ptr
[i
]))
157 /* Skip whitespace. */
158 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
160 /* Check for the colon. */
161 if (i
>= ptr
->len
|| ptr
->ptr
[i
] != ':')
163 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
164 colon after a label. If we do have a colon on the
165 first label then handle more than one label on the
166 line, assuming that each label has a colon. */
167 if (LABELS_WITHOUT_COLONS
&& !had_colon
)
177 /* Skip trailing whitespace. */
178 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
181 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
185 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
188 && strncasecmp (ptr
->ptr
+ i
, "IRPC", from_len
= 4) != 0
189 && strncasecmp (ptr
->ptr
+ i
, "IRP", from_len
= 3) != 0
190 && strncasecmp (ptr
->ptr
+ i
, "IREPC", from_len
= 5) != 0
191 && strncasecmp (ptr
->ptr
+ i
, "IREP", from_len
= 4) != 0
192 && strncasecmp (ptr
->ptr
+ i
, "REPT", from_len
= 4) != 0
193 && strncasecmp (ptr
->ptr
+ i
, "REP", from_len
= 3) != 0)
196 ? strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0
198 && (ptr
->len
== (i
+ from_len
)
199 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
200 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
202 if (strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
203 && (ptr
->len
== (i
+ to_len
)
204 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
205 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
210 /* Reset the string to not include the ending rune. */
211 ptr
->len
= line_start
;
217 Apply and discard .linefile directives that appear within
218 the macro. For long macros, one might want to report the
219 line number information associated with the lines within
220 the macro definition, but we would need more infrastructure
221 to make that happen correctly (e.g. resetting the line
222 number when expanding the macro), and since for short
223 macros we clearly prefer reporting the point of expansion
224 anyway, there's not an obviously better fix here. */
225 if (strncasecmp (ptr
->ptr
+ i
, "linefile", 8) == 0)
227 char saved_eol_char
= ptr
->ptr
[ptr
->len
];
229 ptr
->ptr
[ptr
->len
] = '\0';
230 temp_ilp (ptr
->ptr
+ i
+ 8);
233 ptr
->ptr
[ptr
->len
] = saved_eol_char
;
234 ptr
->len
= line_start
;
238 /* Add the original end-of-line char to the end and keep running. */
239 sb_add_char (ptr
, more
);
240 line_start
= ptr
->len
;
241 more
= get_line (ptr
);
244 /* Return 1 on success, 0 on unexpected EOF. */
248 /* Pick up a token. */
251 get_token (size_t idx
, sb
*in
, sb
*name
)
254 && is_name_beginner (in
->ptr
[idx
]))
256 sb_add_char (name
, in
->ptr
[idx
++]);
258 && is_part_of_name (in
->ptr
[idx
]))
260 sb_add_char (name
, in
->ptr
[idx
++]);
263 && is_name_ender (in
->ptr
[idx
]))
265 sb_add_char (name
, in
->ptr
[idx
++]);
268 /* Ignore trailing &. */
269 if (macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
274 /* Pick up a string. */
277 getstring (size_t idx
, sb
*in
, sb
*acc
)
280 && (in
->ptr
[idx
] == '"'
281 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
282 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
284 if (in
->ptr
[idx
] == '<')
289 && (in
->ptr
[idx
] != '>' || nest
))
291 if (in
->ptr
[idx
] == '!')
294 sb_add_char (acc
, in
->ptr
[idx
++]);
298 if (in
->ptr
[idx
] == '>')
300 if (in
->ptr
[idx
] == '<')
302 sb_add_char (acc
, in
->ptr
[idx
++]);
307 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
309 char tchar
= in
->ptr
[idx
];
314 while (idx
< in
->len
)
316 if (in
->ptr
[idx
- 1] == '\\')
321 if (macro_alternate
&& in
->ptr
[idx
] == '!')
325 sb_add_char (acc
, in
->ptr
[idx
]);
329 else if (escaped
&& in
->ptr
[idx
] == tchar
)
331 sb_add_char (acc
, tchar
);
336 if (in
->ptr
[idx
] == tchar
)
340 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
344 sb_add_char (acc
, in
->ptr
[idx
]);
354 /* Fetch string from the input stream,
356 'Bxyx<whitespace> -> return 'Bxyza
357 %<expr> -> return string of decimal value of <expr>
358 "string" -> return string
359 (string) -> return (string-including-whitespaces)
360 xyx<whitespace> -> return xyz. */
363 get_any_string (size_t idx
, sb
*in
, sb
*out
)
366 idx
= sb_skip_white (idx
, in
);
370 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
372 while (idx
< in
->len
&& !ISSEP (in
->ptr
[idx
]))
373 sb_add_char (out
, in
->ptr
[idx
++]);
375 else if (in
->ptr
[idx
] == '%' && macro_alternate
)
380 /* Turns the next expression into a string. */
381 /* xgettext: no-c-format */
382 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
386 sprintf (buf
, "%" BFD_VMA_FMT
"d", val
);
387 sb_add_string (out
, buf
);
389 else if (in
->ptr
[idx
] == '"'
390 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
391 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
393 if (macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
395 /* Keep the quotes. */
396 sb_add_char (out
, '"');
397 idx
= getstring (idx
, in
, out
);
398 sb_add_char (out
, '"');
402 idx
= getstring (idx
, in
, out
);
407 char *br_buf
= XNEWVEC (char, 1);
408 char *in_br
= br_buf
;
413 || (in
->ptr
[idx
] != ' '
414 && in
->ptr
[idx
] != '\t'))
415 && in
->ptr
[idx
] != ','
416 && (in
->ptr
[idx
] != '<'
417 || (! macro_alternate
&& ! macro_mri
)))
419 char tchar
= in
->ptr
[idx
];
425 sb_add_char (out
, in
->ptr
[idx
++]);
427 && in
->ptr
[idx
] != tchar
)
428 sb_add_char (out
, in
->ptr
[idx
++]);
441 br_buf
= XNEWVEC (char, strlen (in_br
) + 2);
442 strcpy (br_buf
+ 1, in_br
);
457 sb_add_char (out
, tchar
);
467 /* Allocate a new formal. */
469 static formal_entry
*
472 formal_entry
*formal
;
474 formal
= XNEW (formal_entry
);
476 sb_new (&formal
->name
);
477 sb_new (&formal
->def
);
478 sb_new (&formal
->actual
);
480 formal
->type
= FORMAL_OPTIONAL
;
487 del_formal (formal_entry
*formal
)
489 sb_kill (&formal
->actual
);
490 sb_kill (&formal
->def
);
491 sb_kill (&formal
->name
);
495 /* Pick up the formal parameters of a macro definition. */
498 do_formals (macro_entry
*macro
, size_t idx
, sb
*in
)
500 formal_entry
**p
= ¯o
->formals
;
503 idx
= sb_skip_white (idx
, in
);
504 while (idx
< in
->len
)
506 formal_entry
*formal
= new_formal ();
508 formal_hash_entry_t
*elt
;
510 idx
= get_token (idx
, in
, &formal
->name
);
511 if (formal
->name
.len
== 0)
513 if (macro
->formal_count
)
515 del_formal (formal
); /* 'formal' goes out of scope. */
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 elt
= formal_entry_alloc (name
, formal
);
572 if (htab_insert (macro
->formal_hash
, elt
, 0) != NULL
)
575 as_bad_where (macro
->file
, macro
->line
,
576 _("A parameter named `%s' "
577 "already exists for macro `%s'"),
581 formal
->index
= macro
->formal_count
++;
584 if (formal
->type
== FORMAL_VARARG
)
587 idx
= sb_skip_comma (idx
, in
);
588 if (idx
!= cidx
&& idx
>= in
->len
)
597 formal_entry
*formal
= new_formal ();
598 formal_hash_entry_t
*elt
;
600 /* Add a special NARG formal, which macro_expand will set to the
601 number of arguments. */
602 /* The same MRI assemblers which treat '@' characters also use
603 the name $NARG. At least until we find an exception. */
609 sb_add_string (&formal
->name
, name
);
611 /* Add to macro's hash table. */
612 elt
= formal_entry_alloc (name
, formal
);
613 if (htab_insert (macro
->formal_hash
, elt
, 0) != NULL
)
616 as_bad_where (macro
->file
, macro
->line
,
617 _("Reserved word `%s' used as parameter in macro `%s'"),
621 formal
->index
= NARG_INDEX
;
628 /* Free the memory allocated to a macro. */
631 free_macro (macro_entry
*macro
)
633 formal_entry
*formal
;
635 for (formal
= macro
->formals
; formal
; )
640 formal
= formal
->next
;
643 htab_delete (macro
->formal_hash
);
644 sb_kill (¯o
->sub
);
648 /* Define a new macro. Returns NULL on success, otherwise returns an
649 error message. If NAMEP is not NULL, *NAMEP is set to the name of
650 the macro which was defined. */
653 define_macro (size_t idx
, sb
*in
, sb
*label
,
654 size_t (*get_line
) (sb
*),
655 const char *file
, unsigned int line
,
660 const char *error
= NULL
;
662 macro
= XNEW (macro_entry
);
663 sb_new (¯o
->sub
);
668 macro
->formal_count
= 0;
670 macro
->formal_hash
= htab_create_alloc (7, hash_formal_entry
, eq_formal_entry
,
671 NULL
, xcalloc
, free
);
673 idx
= sb_skip_white (idx
, in
);
674 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
675 error
= _("unexpected end of file in macro `%s' definition");
676 if (label
!= NULL
&& label
->len
!= 0)
678 sb_add_sb (&name
, label
);
679 macro
->name
= sb_terminate (&name
);
680 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
682 /* It's the label: MACRO (formals,...) sort */
683 idx
= do_formals (macro
, idx
+ 1, in
);
684 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
685 idx
= sb_skip_white (idx
+ 1, in
);
687 error
= _("missing `)' after formals in macro definition `%s'");
691 /* It's the label: MACRO formals,... sort */
692 idx
= do_formals (macro
, idx
, in
);
699 idx
= get_token (idx
, in
, &name
);
700 macro
->name
= sb_terminate (&name
);
702 error
= _("Missing macro name");
703 cidx
= sb_skip_white (idx
, in
);
704 idx
= sb_skip_comma (cidx
, in
);
705 if (idx
== cidx
|| idx
< in
->len
)
706 idx
= do_formals (macro
, idx
, in
);
710 if (!error
&& idx
< in
->len
)
711 error
= _("Bad parameter list for macro `%s'");
713 /* And stick it in the macro hash table. */
714 for (idx
= 0; idx
< name
.len
; idx
++)
715 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
718 macro_hash_entry_t
*elt
= macro_entry_alloc (macro
->name
, macro
);
719 if (htab_insert (macro_hash
, elt
, 0) != NULL
)
722 error
= _("Macro `%s' was already defined");
727 *namep
= macro
->name
;
737 /* Scan a token, and then skip KIND. */
740 get_apost_token (size_t idx
, sb
*in
, sb
*name
, int kind
)
742 idx
= get_token (idx
, in
, name
);
744 && in
->ptr
[idx
] == kind
745 && (! macro_mri
|| macro_strip_at
)
746 && (! macro_strip_at
|| kind
== '@'))
751 /* Substitute the actual value for a formal parameter. */
754 sub_actual (size_t start
, sb
*in
, sb
*t
, struct htab
*formal_hash
,
755 int kind
, sb
*out
, int copyifnotthere
)
760 src
= get_apost_token (start
, in
, t
, kind
);
761 /* See if it's in the macro's hash table, unless this is
762 macro_strip_at and kind is '@' and the token did not end in '@'. */
765 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
768 ptr
= formal_entry_find (formal_hash
, sb_terminate (t
));
773 sb_add_sb (out
, &ptr
->actual
);
777 sb_add_sb (out
, &ptr
->def
);
780 else if (kind
== '&')
782 /* Doing this permits people to use & in macro bodies. */
783 sb_add_char (out
, '&');
785 if (src
!= start
&& in
->ptr
[src
- 1] == '&')
786 sb_add_char (out
, '&');
788 else if (copyifnotthere
)
794 sb_add_char (out
, '\\');
800 /* Expand the body of a macro. */
803 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
804 struct htab
*formal_hash
, const macro_entry
*macro
)
808 int inquote
= 0, macro_line
= 0;
809 formal_entry
*loclist
= NULL
;
810 const char *err
= NULL
;
814 while (src
< in
->len
&& !err
)
816 if (in
->ptr
[src
] == '&')
821 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
822 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
824 sb_add_char (out
, in
->ptr
[src
++]);
828 /* Permit macro parameter substitution delineated with
829 an '&' prefix and optional '&' suffix. */
830 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
833 else if (in
->ptr
[src
] == '\\')
836 if (src
< in
->len
&& in
->ptr
[src
] == '(')
838 /* Sub in till the next ')' literally. */
840 while (src
< in
->len
&& in
->ptr
[src
] != ')')
842 sb_add_char (out
, in
->ptr
[src
++]);
847 err
= _("missing `)'");
849 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
851 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
853 /* Sub in the macro invocation number. */
857 sprintf (buffer
, "%d", macro_number
);
858 sb_add_string (out
, buffer
);
860 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
862 /* This is a preprocessor variable name, we don't do them
864 sb_add_char (out
, '\\');
865 sb_add_char (out
, '&');
868 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
873 if (ISDIGIT (in
->ptr
[src
]))
874 ind
= in
->ptr
[src
] - '0';
875 else if (ISUPPER (in
->ptr
[src
]))
876 ind
= in
->ptr
[src
] - 'A' + 10;
878 ind
= in
->ptr
[src
] - 'a' + 10;
880 for (f
= formals
; f
!= NULL
; f
= f
->next
)
882 if (f
->index
== ind
- 1)
884 if (f
->actual
.len
!= 0)
885 sb_add_sb (out
, &f
->actual
);
887 sb_add_sb (out
, &f
->def
);
895 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
898 else if ((macro_alternate
|| macro_mri
)
899 && is_name_beginner (in
->ptr
[src
])
902 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
905 || src
+ 5 >= in
->len
906 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
907 || ! ISWHITE (in
->ptr
[src
+ 5])
908 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
912 src
= sub_actual (src
, in
, &t
, formal_hash
,
913 (macro_strip_at
&& inquote
) ? '@' : '\'',
918 src
= sb_skip_white (src
+ 5, in
);
919 while (in
->ptr
[src
] != '\n')
922 formal_entry
*f
= new_formal ();
923 formal_hash_entry_t
*elt
;
925 src
= get_token (src
, in
, &f
->name
);
926 name
= sb_terminate (&f
->name
);
927 elt
= formal_entry_alloc (name
, f
);
928 if (htab_insert (formal_hash
, elt
, 0) != NULL
)
931 as_bad_where (macro
->file
, macro
->line
+ macro_line
,
932 _("`%s' was already used as parameter "
933 "(or another local) name"), name
);
941 f
->index
= LOCAL_INDEX
;
945 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
946 sb_add_string (&f
->actual
, buf
);
949 src
= sb_skip_comma (src
, in
);
953 else if (in
->ptr
[src
] == '"'
954 || (macro_mri
&& in
->ptr
[src
] == '\''))
957 sb_add_char (out
, in
->ptr
[src
++]);
959 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
963 && in
->ptr
[src
] == '@')
965 sb_add_char (out
, '@');
970 && in
->ptr
[src
] == '='
972 && in
->ptr
[src
+ 1] == '=')
977 src
= get_token (src
+ 2, in
, &t
);
978 ptr
= formal_entry_find (formal_hash
, sb_terminate (&t
));
981 /* FIXME: We should really return a warning string here,
982 but we can't, because the == might be in the MRI
983 comment field, and, since the nature of the MRI
984 comment field depends upon the exact instruction
985 being used, we don't have enough information here to
986 figure out whether it is or not. Instead, we leave
987 the == in place, which should cause a syntax error if
988 it is not in a comment. */
989 sb_add_char (out
, '=');
990 sb_add_char (out
, '=');
997 sb_add_string (out
, "-1");
1001 sb_add_char (out
, '0');
1007 if (in
->ptr
[src
] == '\n')
1009 sb_add_char (out
, in
->ptr
[src
++]);
1015 while (loclist
!= NULL
)
1021 name
= sb_terminate (&loclist
->name
);
1022 formal_hash_entry_t needle
= { name
, NULL
};
1023 htab_remove_elt (formal_hash
, &needle
);
1024 del_formal (loclist
);
1031 /* Assign values to the formal parameters of a macro, and expand the
1035 macro_expand (size_t idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1042 const char *err
= NULL
;
1046 /* Reset any old value the actuals may have. */
1047 for (f
= m
->formals
; f
; f
= f
->next
)
1048 sb_reset (&f
->actual
);
1050 while (f
!= NULL
&& f
->index
< 0)
1055 /* The macro may be called with an optional qualifier, which may
1056 be referred to in the macro body as \0. */
1057 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1059 /* The Microtec assembler ignores this if followed by a white space.
1060 (Macro invocation with empty extension) */
1063 && in
->ptr
[idx
] != ' '
1064 && in
->ptr
[idx
] != '\t')
1066 formal_entry
*n
= new_formal ();
1068 n
->index
= QUAL_INDEX
;
1070 n
->next
= m
->formals
;
1073 idx
= get_any_string (idx
, in
, &n
->actual
);
1078 /* Peel off the actuals and store them away in the hash tables' actuals. */
1079 idx
= sb_skip_white (idx
, in
);
1080 while (idx
< in
->len
)
1084 /* Look and see if it's a positional or keyword arg. */
1086 while (scan
< in
->len
1087 && !ISSEP (in
->ptr
[scan
])
1088 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1089 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1091 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1095 /* It's OK to go from positional to keyword. */
1097 /* This is a keyword arg, fetch the formal name and
1098 then the actual stuff. */
1100 idx
= get_token (idx
, in
, &t
);
1101 if (in
->ptr
[idx
] != '=')
1103 err
= _("confusion in formal parameters");
1107 /* Lookup the formal in the macro's list. */
1108 ptr
= formal_entry_find (m
->formal_hash
, sb_terminate (&t
));
1111 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1115 idx
= get_any_string (idx
+ 1, in
, &t
);
1119 /* Insert this value into the right place. */
1120 if (ptr
->actual
.len
)
1122 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1125 sb_reset (&ptr
->actual
);
1127 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1128 if (ptr
->actual
.len
> 0)
1136 err
= _("can't mix positional and keyword arguments");
1147 err
= _("too many positional arguments");
1154 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1155 if ((*pf
)->index
>= c
)
1156 c
= (*pf
)->index
+ 1;
1163 if (f
->type
!= FORMAL_VARARG
)
1164 idx
= get_any_string (idx
, in
, &f
->actual
);
1167 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1170 if (f
->actual
.len
> 0)
1176 while (f
!= NULL
&& f
->index
< 0);
1180 idx
= sb_skip_comma (idx
, in
);
1183 if (in
->ptr
[idx
] == ',')
1185 if (ISWHITE (in
->ptr
[idx
]))
1192 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1194 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1195 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1205 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1206 ptr
= formal_entry_find (m
->formal_hash
, sb_terminate (&t
));
1207 sprintf (buffer
, "%d", narg
);
1208 sb_add_string (&ptr
->actual
, buffer
);
1211 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1214 /* Discard any unnamed formal arguments. */
1222 if ((*pf
)->name
.len
!= 0)
1240 /* Check for a macro. If one is found, put the expansion into
1241 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1244 check_macro (const char *line
, sb
*expand
,
1245 const char **error
, macro_entry
**info
)
1252 if (! is_name_beginner (*line
)
1253 && (! macro_mri
|| *line
!= '.'))
1257 while (is_part_of_name (*s
))
1259 if (is_name_ender (*s
))
1262 copy
= xmemdup0 (line
, s
- line
);
1263 for (cls
= copy
; *cls
!= '\0'; cls
++)
1264 *cls
= TOLOWER (*cls
);
1266 macro
= macro_entry_find (macro_hash
, copy
);
1272 /* Wrap the line up in an sb. */
1274 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1275 sb_add_char (&line_sb
, *s
++);
1278 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1282 /* Export the macro information if requested. */
1289 /* Delete a macro. */
1292 delete_macro (const char *name
)
1297 macro_hash_entry_t needle
;
1299 len
= strlen (name
);
1300 copy
= XNEWVEC (char, len
+ 1);
1301 for (i
= 0; i
< len
; ++i
)
1302 copy
[i
] = TOLOWER (name
[i
]);
1306 needle
.macro
= NULL
;
1307 slot
= htab_find_slot (macro_hash
, &needle
, NO_INSERT
);
1310 free_macro (((macro_hash_entry_t
*) *slot
)->macro
);
1311 htab_clear_slot (macro_hash
, slot
);
1314 as_warn (_("Attempt to purge non-existing macro `%s'"), copy
);
1318 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1319 combined macro definition and execution. This returns NULL on
1320 success, or an error message otherwise. */
1323 expand_irp (int irpc
, size_t idx
, sb
*in
, sb
*out
, size_t (*get_line
) (sb
*))
1328 const char *err
= NULL
;
1330 idx
= sb_skip_white (idx
, in
);
1333 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1334 return _("unexpected end of file in irp or irpc");
1340 idx
= get_token (idx
, in
, &f
.name
);
1341 if (f
.name
.len
== 0)
1342 return _("missing model parameter");
1344 h
= htab_create_alloc (16, hash_formal_entry
, eq_formal_entry
,
1345 NULL
, xcalloc
, free
);
1347 htab_insert (h
, formal_entry_alloc (sb_terminate (&f
.name
), &f
), 0);
1351 f
.type
= FORMAL_OPTIONAL
;
1355 idx
= sb_skip_comma (idx
, in
);
1358 /* Expand once with a null string. */
1359 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1363 bfd_boolean in_quotes
= FALSE
;
1365 if (irpc
&& in
->ptr
[idx
] == '"')
1371 while (idx
< in
->len
)
1374 idx
= get_any_string (idx
, in
, &f
.actual
);
1377 if (in
->ptr
[idx
] == '"')
1382 in_quotes
= ! in_quotes
;
1384 nxt
= sb_skip_white (idx
+ 1, in
);
1391 sb_reset (&f
.actual
);
1392 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1396 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1400 idx
= sb_skip_comma (idx
, in
);
1401 else if (! in_quotes
)
1402 idx
= sb_skip_white (idx
, in
);
1407 sb_kill (&f
.actual
);