]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/macro.c
20309d4d68f0958839f07a85b83f47d5624750ef
1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2025 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. */
33 (is_whitespace (x) || (x) == ',' || (x) == '"' || (x) == ';' \
34 || (x) == ')' || (x) == '(' \
35 || ((flag_macro_alternate || flag_mri) && ((x) == '<' || (x) == '>')))
38 ((x) == 'b' || (x) == 'B' \
39 || (x) == 'q' || (x) == 'Q' \
40 || (x) == 'h' || (x) == 'H' \
41 || (x) == 'd' || (x) == 'D')
43 /* The macro hash table. */
47 /* Whether any macros have been defined. */
51 /* Whether we should strip '@' characters. */
53 #define macro_strip_at false
55 /* Number of macro expansions that have been done. */
57 static unsigned int macro_number
;
59 static void free_macro (macro_entry
*);
62 macro_del_f (void *ent
)
64 string_tuple_t
*tuple
= ent
;
65 free_macro ((macro_entry
*) tuple
->value
);
68 /* Initialize macro processing. */
73 macro_hash
= htab_create_alloc (16, hash_string_tuple
, eq_string_tuple
,
74 macro_del_f
, notes_calloc
, NULL
);
81 htab_delete (macro_hash
);
84 /* Read input lines till we get to a TO string.
85 Increase nesting depth if we get a FROM string.
86 Put the results into sb at PTR.
87 FROM may be NULL (or will be ignored) if TO is "ENDR".
88 Add a new input line to an sb using GET_LINE.
89 Return 1 on success, 0 on unexpected EOF. */
92 buffer_and_nest (const char *from
, const char *to
, sb
*ptr
,
93 size_t (*get_line
) (sb
*))
96 size_t to_len
= strlen (to
);
98 size_t line_start
, more
;
100 if (to_len
== 4 && strcasecmp (to
, "ENDR") == 0)
106 from_len
= strlen (from
);
108 /* Record the present source position, such that diagnostics and debug info
109 can be properly associated with the respective original lines, rather
110 than with the line of the ending directive (TO). */
115 as_where_top (&line
);
117 linefile
= xasprintf ("\t.linefile %u .", line
+ 1);
119 linefile
= xasprintf ("\tlinefile %u .", line
+ 1);
120 sb_add_string (ptr
, linefile
);
124 line_start
= ptr
->len
;
125 more
= get_line (ptr
);
128 /* Try to find the first pseudo op on the line. */
129 size_t i
= line_start
;
130 bool had_colon
= false;
132 /* With normal syntax we can suck what we want till we get
133 to the dot. With the alternate, labels have to start in
134 the first column, since we can't tell what's a label and
135 what's a pseudoop. */
137 if (! LABELS_WITHOUT_COLONS
)
139 /* Skip leading whitespace. */
140 i
= sb_skip_white (i
, ptr
);
145 /* Skip over a label, if any. */
146 if (i
>= ptr
->len
|| ! is_name_beginner (ptr
->ptr
[i
]))
149 while (i
< ptr
->len
&& is_part_of_name (ptr
->ptr
[i
]))
151 if (i
< ptr
->len
&& is_name_ender (ptr
->ptr
[i
]))
153 /* Skip whitespace. */
154 i
= sb_skip_white (i
, ptr
);
155 /* Check for the colon. */
156 if (i
>= ptr
->len
|| ptr
->ptr
[i
] != ':')
158 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
159 colon after a label. If we do have a colon on the
160 first label then handle more than one label on the
161 line, assuming that each label has a colon. */
162 if (LABELS_WITHOUT_COLONS
&& !had_colon
)
172 /* Skip trailing whitespace. */
173 i
= sb_skip_white (i
, ptr
);
175 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
179 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
181 size_t len
= ptr
->len
- i
;
184 if (len
>= 5 && strncasecmp (ptr
->ptr
+ i
, "IREPC", 5) == 0)
186 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "IREP", 4) == 0)
188 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "IRPC", 4) == 0)
190 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "REPT", 4) == 0)
192 else if (len
>= 3 && strncasecmp (ptr
->ptr
+ i
, "IRP", 3) == 0)
194 else if (len
>= 3 && strncasecmp (ptr
->ptr
+ i
, "REP", 3) == 0)
201 && strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0)
204 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
205 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
208 && strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
210 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
211 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
216 /* Reset the string to not include the ending rune. */
217 ptr
->len
= line_start
;
219 /* With the ending directive consumed here, announce the
220 line for macro-expanded listings. */
221 if (listing
& LISTING_MACEXP
)
222 listing_newline (NULL
);
228 Apply .linefile directives that appear within the macro, alongside
229 keeping them for later expansion of the macro. */
230 if (from
!= NULL
&& strcasecmp (from
, "MACRO") == 0
231 && len
>= 8 && strncasecmp (ptr
->ptr
+ i
, "linefile", 8) == 0)
233 sb_add_char (ptr
, more
);
234 temp_ilp (sb_terminate (ptr
) + i
+ 8);
237 line_start
= ptr
->len
;
238 more
= get_line (ptr
);
243 /* Add the original end-of-line char to the end and keep running. */
244 sb_add_char (ptr
, more
);
245 line_start
= ptr
->len
;
246 more
= get_line (ptr
);
249 /* Return 1 on success, 0 on unexpected EOF. */
253 /* Pick up a token. */
256 get_token (size_t idx
, sb
*in
, sb
*name
)
259 && is_name_beginner (in
->ptr
[idx
]))
261 sb_add_char (name
, in
->ptr
[idx
++]);
263 && is_part_of_name (in
->ptr
[idx
]))
265 sb_add_char (name
, in
->ptr
[idx
++]);
268 && is_name_ender (in
->ptr
[idx
]))
270 sb_add_char (name
, in
->ptr
[idx
++]);
273 /* Ignore trailing &. */
274 if (flag_macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
279 /* Pick up a string. */
282 getstring (size_t idx
, sb
*in
, sb
*acc
)
285 && (in
->ptr
[idx
] == '"'
286 || (in
->ptr
[idx
] == '<' && (flag_macro_alternate
|| flag_mri
))
287 || (in
->ptr
[idx
] == '\'' && flag_macro_alternate
)))
289 if (in
->ptr
[idx
] == '<')
294 && (in
->ptr
[idx
] != '>' || nest
))
296 if (in
->ptr
[idx
] == '!')
299 sb_add_char (acc
, in
->ptr
[idx
++]);
303 if (in
->ptr
[idx
] == '>')
305 if (in
->ptr
[idx
] == '<')
307 sb_add_char (acc
, in
->ptr
[idx
++]);
312 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
314 char tchar
= in
->ptr
[idx
];
319 while (idx
< in
->len
)
321 if (in
->ptr
[idx
- 1] == '\\')
326 if (flag_macro_alternate
&& in
->ptr
[idx
] == '!')
330 sb_add_char (acc
, in
->ptr
[idx
]);
334 else if (escaped
&& in
->ptr
[idx
] == tchar
)
336 sb_add_char (acc
, tchar
);
341 if (in
->ptr
[idx
] == tchar
)
345 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
349 sb_add_char (acc
, in
->ptr
[idx
]);
359 /* Fetch string from the input stream,
361 'Bxyx<whitespace> -> return 'Bxyza
362 %<expr> -> return string of decimal value of <expr>
363 "string" -> return string
364 (string) -> return (string-including-whitespaces)
365 xyx<whitespace> -> return xyz. */
368 get_any_string (size_t idx
, sb
*in
, sb
*out
)
371 idx
= sb_skip_white (idx
, in
);
375 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
377 while (idx
< in
->len
&& !ISSEP (in
->ptr
[idx
]))
378 sb_add_char (out
, in
->ptr
[idx
++]);
380 else if (in
->ptr
[idx
] == '%' && flag_macro_alternate
)
382 /* Turn the following expression into a string. */
388 temp_ilp (in
->ptr
+ idx
+ 1);
389 expression_and_evaluate (&ex
);
390 idx
= input_line_pointer
- in
->ptr
;
393 if (ex
.X_op
!= O_constant
)
394 as_bad (_("%% operator needs absolute expression"));
396 sprintf (buf
, "%" PRId64
, (int64_t) ex
.X_add_number
);
397 sb_add_string (out
, buf
);
399 else if (in
->ptr
[idx
] == '"'
400 || (in
->ptr
[idx
] == '<' && (flag_macro_alternate
|| flag_mri
))
401 || (flag_macro_alternate
&& in
->ptr
[idx
] == '\''))
403 if (flag_macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
405 /* Keep the quotes. */
406 sb_add_char (out
, '"');
407 idx
= getstring (idx
, in
, out
);
408 sb_add_char (out
, '"');
412 idx
= getstring (idx
, in
, out
);
417 char *br_buf
= XNEWVEC (char, 1);
418 char *in_br
= br_buf
;
422 && (*in_br
|| !is_whitespace (in
->ptr
[idx
]))
423 && in
->ptr
[idx
] != ','
424 && (in
->ptr
[idx
] != '<'
425 || (! flag_macro_alternate
&& ! flag_mri
)))
427 char tchar
= in
->ptr
[idx
];
433 sb_add_char (out
, in
->ptr
[idx
++]);
435 && in
->ptr
[idx
] != tchar
)
436 sb_add_char (out
, in
->ptr
[idx
++]);
449 br_buf
= XNEWVEC (char, strlen (in_br
) + 2);
450 strcpy (br_buf
+ 1, in_br
);
465 sb_add_char (out
, tchar
);
475 /* Allocate a new formal. */
477 static formal_entry
*
480 formal_entry
*formal
;
482 formal
= XNEW (formal_entry
);
484 sb_new (&formal
->name
);
485 sb_new (&formal
->def
);
486 sb_new (&formal
->actual
);
488 formal
->type
= FORMAL_OPTIONAL
;
495 del_formal (formal_entry
*formal
)
497 sb_kill (&formal
->actual
);
498 sb_kill (&formal
->def
);
499 sb_kill (&formal
->name
);
503 /* Pick up the formal parameters of a macro definition. */
506 do_formals (macro_entry
*macro
, size_t idx
, sb
*in
)
508 formal_entry
**p
= ¯o
->formals
;
511 idx
= sb_skip_white (idx
, in
);
512 while (idx
< in
->len
)
514 formal_entry
*formal
= new_formal ();
517 idx
= get_token (idx
, in
, &formal
->name
);
518 if (formal
->name
.len
== 0)
520 if (macro
->formal_count
)
522 del_formal (formal
); /* 'formal' goes out of scope. */
525 idx
= sb_skip_white (idx
, in
);
526 /* This is a formal. */
527 name
= sb_terminate (&formal
->name
);
530 && in
->ptr
[idx
] == ':'
531 && (! is_name_beginner (':')
532 || idx
+ 1 >= in
->len
533 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
535 /* Got a qualifier. */
539 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
540 sb_terminate (&qual
);
542 as_bad_where (macro
->file
,
544 _("Missing parameter qualifier for `%s' in macro `%s'"),
547 else if (strcmp (qual
.ptr
, "req") == 0)
548 formal
->type
= FORMAL_REQUIRED
;
549 else if (strcmp (qual
.ptr
, "vararg") == 0)
550 formal
->type
= FORMAL_VARARG
;
552 as_bad_where (macro
->file
,
554 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
559 idx
= sb_skip_white (idx
, in
);
561 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
564 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
565 idx
= sb_skip_white (idx
, in
);
566 if (formal
->type
== FORMAL_REQUIRED
)
568 sb_reset (&formal
->def
);
569 as_warn_where (macro
->file
,
571 _("Pointless default value for required parameter `%s' in macro `%s'"),
577 /* Add to macro's hash table. */
578 if (str_hash_insert (macro
->formal_hash
, name
, formal
, 0) != NULL
)
580 as_bad_where (macro
->file
, macro
->line
,
581 _("A parameter named `%s' "
582 "already exists for macro `%s'"),
586 formal
->index
= macro
->formal_count
++;
589 if (formal
->type
== FORMAL_VARARG
)
592 idx
= sb_skip_comma (idx
, in
);
593 if (idx
!= cidx
&& idx
>= in
->len
)
602 formal_entry
*formal
= new_formal ();
604 /* Add a special NARG formal, which macro_expand will set to the
605 number of arguments. */
606 /* The same MRI assemblers which treat '@' characters also use
607 the name $NARG. At least until we find an exception. */
613 sb_add_string (&formal
->name
, name
);
615 /* Add to macro's hash table. */
616 if (str_hash_insert (macro
->formal_hash
, name
, formal
, 0) != NULL
)
618 as_bad_where (macro
->file
, macro
->line
,
619 _("Reserved word `%s' used as parameter in macro `%s'"),
623 formal
->index
= NARG_INDEX
;
630 /* Free the memory allocated to a macro. */
633 free_macro (macro_entry
*macro
)
635 formal_entry
*formal
;
637 for (formal
= macro
->formals
; formal
; )
642 formal
= formal
->next
;
645 htab_delete (macro
->formal_hash
);
646 sb_kill (¯o
->sub
);
647 free ((char *) macro
->name
);
651 /* Define a new macro. */
654 define_macro (sb
*in
, sb
*label
, size_t (*get_line
) (sb
*))
659 const char *error
= NULL
;
661 macro
= XNEW (macro_entry
);
662 sb_new (¯o
->sub
);
664 macro
->file
= as_where (¯o
->line
);
666 macro
->formal_count
= 0;
668 macro
->formal_hash
= str_htab_create ();
671 idx
= sb_skip_white (0, in
);
672 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
673 error
= _("unexpected end of file in macro `%s' definition");
674 if (label
!= NULL
&& label
->len
!= 0)
676 sb_add_sb (&name
, label
);
677 macro
->name
= sb_terminate (&name
);
678 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
680 /* It's the label: MACRO (formals,...) sort */
681 idx
= do_formals (macro
, idx
+ 1, in
);
682 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
683 idx
= sb_skip_white (idx
+ 1, in
);
685 error
= _("missing `)' after formals in macro definition `%s'");
689 /* It's the label: MACRO formals,... sort */
690 idx
= do_formals (macro
, idx
, in
);
697 idx
= get_token (idx
, in
, &name
);
698 macro
->name
= sb_terminate (&name
);
700 error
= _("Missing macro name");
701 cidx
= sb_skip_white (idx
, in
);
702 idx
= sb_skip_comma (cidx
, in
);
703 if (idx
== cidx
|| idx
< in
->len
)
704 idx
= do_formals (macro
, idx
, in
);
708 if (!error
&& idx
< in
->len
)
709 error
= _("Bad parameter list for macro `%s'");
711 /* And stick it in the macro hash table. */
712 for (idx
= 0; idx
< name
.len
; idx
++)
713 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
716 if (str_hash_insert (macro_hash
, macro
->name
, macro
, 0) != NULL
)
717 error
= _("Macro `%s' was already defined");
724 as_bad_where (macro
->file
, macro
->line
, error
, macro
->name
);
732 /* Scan a token, and then skip KIND. */
735 get_apost_token (size_t idx
, sb
*in
, sb
*name
, int kind
)
737 idx
= get_token (idx
, in
, name
);
739 && in
->ptr
[idx
] == kind
740 && (! flag_mri
|| macro_strip_at
)
741 && (! macro_strip_at
|| kind
== '@'))
746 /* Substitute the actual value for a formal parameter. */
749 sub_actual (size_t start
, sb
*in
, sb
*t
, struct htab
*formal_hash
,
750 int kind
, sb
*out
, int copyifnotthere
)
755 src
= get_apost_token (start
, in
, t
, kind
);
756 /* See if it's in the macro's hash table, unless this is
757 macro_strip_at and kind is '@' and the token did not end in '@'. */
760 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
763 ptr
= str_hash_find (formal_hash
, sb_terminate (t
));
768 sb_add_sb (out
, &ptr
->actual
);
772 sb_add_sb (out
, &ptr
->def
);
775 else if (kind
== '&')
777 /* Doing this permits people to use & in macro bodies. */
778 sb_add_char (out
, '&');
780 if (src
!= start
&& in
->ptr
[src
- 1] == '&')
781 sb_add_char (out
, '&');
783 else if (copyifnotthere
)
789 sb_add_char (out
, '\\');
795 /* Expand the body of a macro. */
798 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
799 struct htab
*formal_hash
, const macro_entry
*macro
,
800 unsigned int instance
)
804 int inquote
= 0, macro_line
= 0;
805 formal_entry
*loclist
= NULL
;
806 const char *err
= NULL
;
810 while (src
< in
->len
&& !err
)
812 if (in
->ptr
[src
] == '&')
817 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
818 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
820 sb_add_char (out
, in
->ptr
[src
++]);
824 /* Permit macro parameter substitution delineated with
825 an '&' prefix and optional '&' suffix. */
826 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
829 else if (in
->ptr
[src
] == '\\')
832 if (src
< in
->len
&& in
->ptr
[src
] == '(')
834 /* Sub in till the next ')' literally. */
836 while (src
< in
->len
&& in
->ptr
[src
] != ')')
838 sb_add_char (out
, in
->ptr
[src
++]);
843 err
= _("missing `)'");
845 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
847 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
849 /* Sub in the total macro invocation number. */
853 sprintf (buffer
, "%u", macro_number
);
854 sb_add_string (out
, buffer
);
856 else if (src
< in
->len
&& in
->ptr
[src
] == '+')
858 /* Sub in the current macro invocation number. */
862 sprintf (buffer
, "%d", instance
);
863 sb_add_string (out
, buffer
);
865 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
867 /* This is a preprocessor variable name, we don't do them
869 sb_add_char (out
, '\\');
870 sb_add_char (out
, '&');
873 else if (flag_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
878 if (ISDIGIT (in
->ptr
[src
]))
879 ind
= in
->ptr
[src
] - '0';
880 else if (ISUPPER (in
->ptr
[src
]))
881 ind
= in
->ptr
[src
] - 'A' + 10;
883 ind
= in
->ptr
[src
] - 'a' + 10;
885 for (f
= formals
; f
!= NULL
; f
= f
->next
)
887 if (f
->index
== ind
- 1)
889 if (f
->actual
.len
!= 0)
890 sb_add_sb (out
, &f
->actual
);
892 sb_add_sb (out
, &f
->def
);
900 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
903 else if ((flag_macro_alternate
|| flag_mri
)
904 && is_name_beginner (in
->ptr
[src
])
907 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
910 || src
+ 5 >= in
->len
911 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
912 || ! is_whitespace (in
->ptr
[src
+ 5])
913 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
917 src
= sub_actual (src
, in
, &t
, formal_hash
,
918 (macro_strip_at
&& inquote
) ? '@' : '\'',
923 src
= sb_skip_white (src
+ 5, in
);
924 while (in
->ptr
[src
] != '\n')
927 formal_entry
*f
= new_formal ();
929 src
= get_token (src
, in
, &f
->name
);
930 name
= sb_terminate (&f
->name
);
931 if (str_hash_insert (formal_hash
, name
, f
, 0) != NULL
)
933 as_bad_where (macro
->file
, macro
->line
+ macro_line
,
934 _("`%s' was already used as parameter "
935 "(or another local) name"), name
);
943 f
->index
= LOCAL_INDEX
;
947 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
948 sb_add_string (&f
->actual
, buf
);
951 src
= sb_skip_comma (src
, in
);
955 else if (in
->ptr
[src
] == '"'
956 || (flag_mri
&& in
->ptr
[src
] == '\''))
959 sb_add_char (out
, in
->ptr
[src
++]);
961 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
965 && in
->ptr
[src
] == '@')
967 sb_add_char (out
, '@');
972 && in
->ptr
[src
] == '='
974 && in
->ptr
[src
+ 1] == '=')
979 src
= get_token (src
+ 2, in
, &t
);
980 ptr
= str_hash_find (formal_hash
, sb_terminate (&t
));
983 /* FIXME: We should really return a warning string here,
984 but we can't, because the == might be in the MRI
985 comment field, and, since the nature of the MRI
986 comment field depends upon the exact instruction
987 being used, we don't have enough information here to
988 figure out whether it is or not. Instead, we leave
989 the == in place, which should cause a syntax error if
990 it is not in a comment. */
991 sb_add_char (out
, '=');
992 sb_add_char (out
, '=');
999 sb_add_string (out
, "-1");
1003 sb_add_char (out
, '0');
1009 if (in
->ptr
[src
] == '\n')
1011 sb_add_char (out
, in
->ptr
[src
++]);
1017 while (loclist
!= NULL
)
1023 name
= sb_terminate (&loclist
->name
);
1024 str_hash_delete (formal_hash
, name
);
1025 del_formal (loclist
);
1029 if (!err
&& (out
->len
== 0 || out
->ptr
[out
->len
- 1] != '\n'))
1030 sb_add_char (out
, '\n');
1034 /* Assign values to the formal parameters of a macro, and expand the
1038 macro_expand (size_t idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1045 const char *err
= NULL
;
1049 /* Reset any old value the actuals may have. */
1050 for (f
= m
->formals
; f
; f
= f
->next
)
1051 sb_reset (&f
->actual
);
1053 while (f
!= NULL
&& f
->index
< 0)
1058 /* The macro may be called with an optional qualifier, which may
1059 be referred to in the macro body as \0. */
1060 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1062 /* The Microtec assembler ignores this if followed by a white space.
1063 (Macro invocation with empty extension) */
1065 if (idx
< in
->len
&& !is_whitespace (in
->ptr
[idx
]))
1067 formal_entry
*n
= new_formal ();
1069 n
->index
= QUAL_INDEX
;
1071 n
->next
= m
->formals
;
1074 idx
= get_any_string (idx
, in
, &n
->actual
);
1079 /* Peel off the actuals and store them away in the hash tables' actuals. */
1080 idx
= sb_skip_white (idx
, in
);
1081 while (idx
< in
->len
)
1085 /* Look and see if it's a positional or keyword arg. */
1087 while (scan
< in
->len
1088 && !ISSEP (in
->ptr
[scan
])
1089 && !(flag_mri
&& in
->ptr
[scan
] == '\'')
1090 && (!flag_macro_alternate
&& in
->ptr
[scan
] != '='))
1092 if (scan
< in
->len
&& !flag_macro_alternate
&& in
->ptr
[scan
] == '=')
1096 /* It's OK to go from positional to keyword. */
1098 /* This is a keyword arg, fetch the formal name and
1099 then the actual stuff. */
1101 idx
= get_token (idx
, in
, &t
);
1102 if (idx
>= in
->len
|| in
->ptr
[idx
] != '=')
1104 err
= _("confusion in formal parameters");
1108 /* Lookup the formal in the macro's list. */
1109 ptr
= str_hash_find (m
->formal_hash
, sb_terminate (&t
));
1112 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1116 idx
= get_any_string (idx
+ 1, in
, &t
);
1120 /* Insert this value into the right place. */
1121 if (ptr
->actual
.len
)
1123 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1126 sb_reset (&ptr
->actual
);
1128 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1129 if (ptr
->actual
.len
> 0)
1137 err
= _("can't mix positional and keyword arguments");
1148 err
= _("too many positional arguments");
1155 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1156 if ((*pf
)->index
>= c
)
1157 c
= (*pf
)->index
+ 1;
1164 if (f
->type
!= FORMAL_VARARG
)
1165 idx
= get_any_string (idx
, in
, &f
->actual
);
1166 else if (idx
< in
->len
)
1168 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1171 if (f
->actual
.len
> 0)
1177 while (f
!= NULL
&& f
->index
< 0);
1181 idx
= sb_skip_comma (idx
, in
);
1184 if (idx
< in
->len
&& in
->ptr
[idx
] == ',')
1186 if (idx
< in
->len
&& is_whitespace (in
->ptr
[idx
]))
1193 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1195 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1196 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1203 ptr
= str_hash_find (m
->formal_hash
,
1204 macro_strip_at
? "$NARG" : "NARG");
1208 sprintf (buffer
, "%d", narg
);
1209 sb_add_string (&ptr
->actual
, buffer
);
1213 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
,
1217 /* Discard any unnamed formal arguments. */
1225 if ((*pf
)->name
.len
!= 0)
1246 /* Check for a macro. If one is found, put the expansion into
1247 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1250 check_macro (const char *line
, sb
*expand
,
1251 const char **error
, macro_entry
**info
)
1258 if (! is_name_beginner (*line
)
1259 && (! flag_mri
|| *line
!= '.'))
1263 while (is_part_of_name (*s
))
1265 if (is_name_ender (*s
))
1268 copy
= xmemdup0 (line
, s
- line
);
1269 for (cls
= copy
; *cls
!= '\0'; cls
++)
1270 *cls
= TOLOWER (*cls
);
1272 macro
= str_hash_find (macro_hash
, copy
);
1278 /* Wrap the line up in an sb. */
1280 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1281 sb_add_char (&line_sb
, *s
++);
1284 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1288 /* Export the macro information if requested. */
1295 /* Delete a macro. */
1298 delete_macro (const char *name
)
1304 len
= strlen (name
);
1305 copy
= XNEWVEC (char, len
+ 1);
1306 for (i
= 0; i
< len
; ++i
)
1307 copy
[i
] = TOLOWER (name
[i
]);
1310 macro
= str_hash_find (macro_hash
, copy
);
1312 str_hash_delete (macro_hash
, copy
);
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
))
1335 err
= _("unexpected end of file in irp or irpc");
1343 idx
= get_token (idx
, in
, &f
.name
);
1344 if (f
.name
.len
== 0)
1346 err
= _("missing model parameter");
1350 h
= str_htab_create ();
1352 str_hash_insert (h
, sb_terminate (&f
.name
), &f
, 0);
1356 f
.type
= FORMAL_OPTIONAL
;
1360 idx
= sb_skip_comma (idx
, in
);
1363 /* Expand once with a null string. */
1364 err
= macro_expand_body (&sub
, out
, &f
, h
, NULL
, 0);
1368 bool in_quotes
= false;
1369 unsigned int instance
= 0;
1371 while (idx
< in
->len
)
1374 idx
= get_any_string (idx
, in
, &f
.actual
);
1377 if (in
->ptr
[idx
] == '"')
1379 in_quotes
= ! in_quotes
;
1384 idx
= sb_skip_white (idx
, in
);
1390 sb_reset (&f
.actual
);
1391 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1395 err
= macro_expand_body (&sub
, out
, &f
, h
, NULL
, instance
);
1400 idx
= sb_skip_comma (idx
, in
);
1401 else if (! in_quotes
)
1402 idx
= sb_skip_white (idx
, in
);
1408 sb_kill (&f
.actual
);