c2a47684b15d74d1d807a6b167d6535652f20306
1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2022 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. */
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
= str_htab_create ();
81 macro_alternate
= alternate
;
83 macro_strip_at
= strip_at
;
90 htab_delete (macro_hash
);
93 /* Switch in and out of alternate mode on the fly. */
96 macro_set_alternate (int alternate
)
98 macro_alternate
= alternate
;
101 /* Switch in and out of MRI mode on the fly. */
104 macro_mri_mode (int mri
)
109 /* Read input lines till we get to a TO string.
110 Increase nesting depth if we get a FROM string.
111 Put the results into sb at PTR.
112 FROM may be NULL (or will be ignored) if TO is "ENDR".
113 Add a new input line to an sb using GET_LINE.
114 Return 1 on success, 0 on unexpected EOF. */
117 buffer_and_nest (const char *from
, const char *to
, sb
*ptr
,
118 size_t (*get_line
) (sb
*))
121 size_t to_len
= strlen (to
);
123 size_t line_start
= ptr
->len
;
124 size_t more
= get_line (ptr
);
126 if (to_len
== 4 && strcasecmp (to
, "ENDR") == 0)
132 from_len
= strlen (from
);
134 /* Except for macros record the present source position, such that
135 diagnostics and debug info will be properly associated with the
136 respective original lines, rather than with the line of the ending
138 if (from
== NULL
|| strcasecmp (from
, "MACRO") != 0)
145 linefile
= xasprintf ("\t.linefile %u .\n", line
);
147 linefile
= xasprintf ("\tlinefile %u .\n", line
);
148 sb_add_buffer (ptr
, linefile
, strlen (linefile
));
154 /* Try to find the first pseudo op on the line. */
155 size_t i
= line_start
;
156 bool had_colon
= false;
158 /* With normal syntax we can suck what we want till we get
159 to the dot. With the alternate, labels have to start in
160 the first column, since we can't tell what's a label and
161 what's a pseudoop. */
163 if (! LABELS_WITHOUT_COLONS
)
165 /* Skip leading whitespace. */
166 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
172 /* Skip over a label, if any. */
173 if (i
>= ptr
->len
|| ! is_name_beginner (ptr
->ptr
[i
]))
176 while (i
< ptr
->len
&& is_part_of_name (ptr
->ptr
[i
]))
178 if (i
< ptr
->len
&& is_name_ender (ptr
->ptr
[i
]))
180 /* Skip whitespace. */
181 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
183 /* Check for the colon. */
184 if (i
>= ptr
->len
|| ptr
->ptr
[i
] != ':')
186 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
187 colon after a label. If we do have a colon on the
188 first label then handle more than one label on the
189 line, assuming that each label has a colon. */
190 if (LABELS_WITHOUT_COLONS
&& !had_colon
)
200 /* Skip trailing whitespace. */
201 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
204 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
208 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
210 size_t len
= ptr
->len
- i
;
213 if (len
>= 5 && strncasecmp (ptr
->ptr
+ i
, "IREPC", 5) == 0)
215 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "IREP", 4) == 0)
217 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "IRPC", 4) == 0)
219 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "REPT", 4) == 0)
221 else if (len
>= 3 && strncasecmp (ptr
->ptr
+ i
, "IRP", 3) == 0)
223 else if (len
>= 3 && strncasecmp (ptr
->ptr
+ i
, "REP", 3) == 0)
230 && strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0)
233 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
234 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
237 && strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
239 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
240 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
245 /* Reset the string to not include the ending rune. */
246 ptr
->len
= line_start
;
252 Apply and discard .linefile directives that appear within
253 the macro. For long macros, one might want to report the
254 line number information associated with the lines within
255 the macro definition, but we would need more infrastructure
256 to make that happen correctly (e.g. resetting the line
257 number when expanding the macro), and since for short
258 macros we clearly prefer reporting the point of expansion
259 anyway, there's not an obviously better fix here. */
260 if (from
!= NULL
&& strcasecmp (from
, "MACRO") == 0
261 && len
>= 8 && strncasecmp (ptr
->ptr
+ i
, "linefile", 8) == 0)
263 char saved_eol_char
= ptr
->ptr
[ptr
->len
];
265 ptr
->ptr
[ptr
->len
] = '\0';
266 temp_ilp (ptr
->ptr
+ i
+ 8);
269 ptr
->ptr
[ptr
->len
] = saved_eol_char
;
270 ptr
->len
= line_start
;
274 /* Add the original end-of-line char to the end and keep running. */
275 sb_add_char (ptr
, more
);
276 line_start
= ptr
->len
;
277 more
= get_line (ptr
);
280 /* Return 1 on success, 0 on unexpected EOF. */
284 /* Pick up a token. */
287 get_token (size_t idx
, sb
*in
, sb
*name
)
290 && is_name_beginner (in
->ptr
[idx
]))
292 sb_add_char (name
, in
->ptr
[idx
++]);
294 && is_part_of_name (in
->ptr
[idx
]))
296 sb_add_char (name
, in
->ptr
[idx
++]);
299 && is_name_ender (in
->ptr
[idx
]))
301 sb_add_char (name
, in
->ptr
[idx
++]);
304 /* Ignore trailing &. */
305 if (macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
310 /* Pick up a string. */
313 getstring (size_t idx
, sb
*in
, sb
*acc
)
316 && (in
->ptr
[idx
] == '"'
317 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
318 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
320 if (in
->ptr
[idx
] == '<')
325 && (in
->ptr
[idx
] != '>' || nest
))
327 if (in
->ptr
[idx
] == '!')
330 sb_add_char (acc
, in
->ptr
[idx
++]);
334 if (in
->ptr
[idx
] == '>')
336 if (in
->ptr
[idx
] == '<')
338 sb_add_char (acc
, in
->ptr
[idx
++]);
343 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
345 char tchar
= in
->ptr
[idx
];
350 while (idx
< in
->len
)
352 if (in
->ptr
[idx
- 1] == '\\')
357 if (macro_alternate
&& in
->ptr
[idx
] == '!')
361 sb_add_char (acc
, in
->ptr
[idx
]);
365 else if (escaped
&& in
->ptr
[idx
] == tchar
)
367 sb_add_char (acc
, tchar
);
372 if (in
->ptr
[idx
] == tchar
)
376 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
380 sb_add_char (acc
, in
->ptr
[idx
]);
390 /* Fetch string from the input stream,
392 'Bxyx<whitespace> -> return 'Bxyza
393 %<expr> -> return string of decimal value of <expr>
394 "string" -> return string
395 (string) -> return (string-including-whitespaces)
396 xyx<whitespace> -> return xyz. */
399 get_any_string (size_t idx
, sb
*in
, sb
*out
)
402 idx
= sb_skip_white (idx
, in
);
406 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
408 while (idx
< in
->len
&& !ISSEP (in
->ptr
[idx
]))
409 sb_add_char (out
, in
->ptr
[idx
++]);
411 else if (in
->ptr
[idx
] == '%' && macro_alternate
)
416 /* Turns the next expression into a string. */
417 /* xgettext: no-c-format */
418 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
422 sprintf (buf
, "%" PRId64
, (int64_t) val
);
423 sb_add_string (out
, buf
);
425 else if (in
->ptr
[idx
] == '"'
426 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
427 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
429 if (macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
431 /* Keep the quotes. */
432 sb_add_char (out
, '"');
433 idx
= getstring (idx
, in
, out
);
434 sb_add_char (out
, '"');
438 idx
= getstring (idx
, in
, out
);
443 char *br_buf
= XNEWVEC (char, 1);
444 char *in_br
= br_buf
;
449 || (in
->ptr
[idx
] != ' '
450 && in
->ptr
[idx
] != '\t'))
451 && in
->ptr
[idx
] != ','
452 && (in
->ptr
[idx
] != '<'
453 || (! macro_alternate
&& ! macro_mri
)))
455 char tchar
= in
->ptr
[idx
];
461 sb_add_char (out
, in
->ptr
[idx
++]);
463 && in
->ptr
[idx
] != tchar
)
464 sb_add_char (out
, in
->ptr
[idx
++]);
477 br_buf
= XNEWVEC (char, strlen (in_br
) + 2);
478 strcpy (br_buf
+ 1, in_br
);
493 sb_add_char (out
, tchar
);
503 /* Allocate a new formal. */
505 static formal_entry
*
508 formal_entry
*formal
;
510 formal
= XNEW (formal_entry
);
512 sb_new (&formal
->name
);
513 sb_new (&formal
->def
);
514 sb_new (&formal
->actual
);
516 formal
->type
= FORMAL_OPTIONAL
;
523 del_formal (formal_entry
*formal
)
525 sb_kill (&formal
->actual
);
526 sb_kill (&formal
->def
);
527 sb_kill (&formal
->name
);
531 /* Pick up the formal parameters of a macro definition. */
534 do_formals (macro_entry
*macro
, size_t idx
, sb
*in
)
536 formal_entry
**p
= ¯o
->formals
;
539 idx
= sb_skip_white (idx
, in
);
540 while (idx
< in
->len
)
542 formal_entry
*formal
= new_formal ();
545 idx
= get_token (idx
, in
, &formal
->name
);
546 if (formal
->name
.len
== 0)
548 if (macro
->formal_count
)
550 del_formal (formal
); /* 'formal' goes out of scope. */
553 idx
= sb_skip_white (idx
, in
);
554 /* This is a formal. */
555 name
= sb_terminate (&formal
->name
);
558 && in
->ptr
[idx
] == ':'
559 && (! is_name_beginner (':')
560 || idx
+ 1 >= in
->len
561 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
563 /* Got a qualifier. */
567 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
568 sb_terminate (&qual
);
570 as_bad_where (macro
->file
,
572 _("Missing parameter qualifier for `%s' in macro `%s'"),
575 else if (strcmp (qual
.ptr
, "req") == 0)
576 formal
->type
= FORMAL_REQUIRED
;
577 else if (strcmp (qual
.ptr
, "vararg") == 0)
578 formal
->type
= FORMAL_VARARG
;
580 as_bad_where (macro
->file
,
582 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
587 idx
= sb_skip_white (idx
, in
);
589 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
592 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
593 idx
= sb_skip_white (idx
, in
);
594 if (formal
->type
== FORMAL_REQUIRED
)
596 sb_reset (&formal
->def
);
597 as_warn_where (macro
->file
,
599 _("Pointless default value for required parameter `%s' in macro `%s'"),
605 /* Add to macro's hash table. */
606 if (str_hash_insert (macro
->formal_hash
, name
, formal
, 0) != NULL
)
608 as_bad_where (macro
->file
, macro
->line
,
609 _("A parameter named `%s' "
610 "already exists for macro `%s'"),
614 formal
->index
= macro
->formal_count
++;
617 if (formal
->type
== FORMAL_VARARG
)
620 idx
= sb_skip_comma (idx
, in
);
621 if (idx
!= cidx
&& idx
>= in
->len
)
630 formal_entry
*formal
= new_formal ();
632 /* Add a special NARG formal, which macro_expand will set to the
633 number of arguments. */
634 /* The same MRI assemblers which treat '@' characters also use
635 the name $NARG. At least until we find an exception. */
641 sb_add_string (&formal
->name
, name
);
643 /* Add to macro's hash table. */
644 if (str_hash_insert (macro
->formal_hash
, name
, formal
, 0) != NULL
)
646 as_bad_where (macro
->file
, macro
->line
,
647 _("Reserved word `%s' used as parameter in macro `%s'"),
651 formal
->index
= NARG_INDEX
;
658 /* Free the memory allocated to a macro. */
661 free_macro (macro_entry
*macro
)
663 formal_entry
*formal
;
665 for (formal
= macro
->formals
; formal
; )
670 formal
= formal
->next
;
673 htab_delete (macro
->formal_hash
);
674 sb_kill (¯o
->sub
);
678 /* Define a new macro. Returns NULL on success, otherwise returns an
679 error message. If NAMEP is not NULL, *NAMEP is set to the name of
680 the macro which was defined. */
683 define_macro (size_t idx
, sb
*in
, sb
*label
,
684 size_t (*get_line
) (sb
*),
685 const char *file
, unsigned int line
,
690 const char *error
= NULL
;
692 macro
= XNEW (macro_entry
);
693 sb_new (¯o
->sub
);
698 macro
->formal_count
= 0;
700 macro
->formal_hash
= str_htab_create ();
702 idx
= sb_skip_white (idx
, in
);
703 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
704 error
= _("unexpected end of file in macro `%s' definition");
705 if (label
!= NULL
&& label
->len
!= 0)
707 sb_add_sb (&name
, label
);
708 macro
->name
= sb_terminate (&name
);
709 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
711 /* It's the label: MACRO (formals,...) sort */
712 idx
= do_formals (macro
, idx
+ 1, in
);
713 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
714 idx
= sb_skip_white (idx
+ 1, in
);
716 error
= _("missing `)' after formals in macro definition `%s'");
720 /* It's the label: MACRO formals,... sort */
721 idx
= do_formals (macro
, idx
, in
);
728 idx
= get_token (idx
, in
, &name
);
729 macro
->name
= sb_terminate (&name
);
731 error
= _("Missing macro name");
732 cidx
= sb_skip_white (idx
, in
);
733 idx
= sb_skip_comma (cidx
, in
);
734 if (idx
== cidx
|| idx
< in
->len
)
735 idx
= do_formals (macro
, idx
, in
);
739 if (!error
&& idx
< in
->len
)
740 error
= _("Bad parameter list for macro `%s'");
742 /* And stick it in the macro hash table. */
743 for (idx
= 0; idx
< name
.len
; idx
++)
744 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
747 if (str_hash_insert (macro_hash
, macro
->name
, macro
, 0) != NULL
)
748 error
= _("Macro `%s' was already defined");
752 *namep
= macro
->name
;
762 /* Scan a token, and then skip KIND. */
765 get_apost_token (size_t idx
, sb
*in
, sb
*name
, int kind
)
767 idx
= get_token (idx
, in
, name
);
769 && in
->ptr
[idx
] == kind
770 && (! macro_mri
|| macro_strip_at
)
771 && (! macro_strip_at
|| kind
== '@'))
776 /* Substitute the actual value for a formal parameter. */
779 sub_actual (size_t start
, sb
*in
, sb
*t
, struct htab
*formal_hash
,
780 int kind
, sb
*out
, int copyifnotthere
)
785 src
= get_apost_token (start
, in
, t
, kind
);
786 /* See if it's in the macro's hash table, unless this is
787 macro_strip_at and kind is '@' and the token did not end in '@'. */
790 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
793 ptr
= str_hash_find (formal_hash
, sb_terminate (t
));
798 sb_add_sb (out
, &ptr
->actual
);
802 sb_add_sb (out
, &ptr
->def
);
805 else if (kind
== '&')
807 /* Doing this permits people to use & in macro bodies. */
808 sb_add_char (out
, '&');
810 if (src
!= start
&& in
->ptr
[src
- 1] == '&')
811 sb_add_char (out
, '&');
813 else if (copyifnotthere
)
819 sb_add_char (out
, '\\');
825 /* Expand the body of a macro. */
828 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
829 struct htab
*formal_hash
, const macro_entry
*macro
)
833 int inquote
= 0, macro_line
= 0;
834 formal_entry
*loclist
= NULL
;
835 const char *err
= NULL
;
839 while (src
< in
->len
&& !err
)
841 if (in
->ptr
[src
] == '&')
846 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
847 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
849 sb_add_char (out
, in
->ptr
[src
++]);
853 /* Permit macro parameter substitution delineated with
854 an '&' prefix and optional '&' suffix. */
855 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
858 else if (in
->ptr
[src
] == '\\')
861 if (src
< in
->len
&& in
->ptr
[src
] == '(')
863 /* Sub in till the next ')' literally. */
865 while (src
< in
->len
&& in
->ptr
[src
] != ')')
867 sb_add_char (out
, in
->ptr
[src
++]);
872 err
= _("missing `)'");
874 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
876 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
878 /* Sub in the macro invocation number. */
882 sprintf (buffer
, "%d", macro_number
);
883 sb_add_string (out
, buffer
);
885 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
887 /* This is a preprocessor variable name, we don't do them
889 sb_add_char (out
, '\\');
890 sb_add_char (out
, '&');
893 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
898 if (ISDIGIT (in
->ptr
[src
]))
899 ind
= in
->ptr
[src
] - '0';
900 else if (ISUPPER (in
->ptr
[src
]))
901 ind
= in
->ptr
[src
] - 'A' + 10;
903 ind
= in
->ptr
[src
] - 'a' + 10;
905 for (f
= formals
; f
!= NULL
; f
= f
->next
)
907 if (f
->index
== ind
- 1)
909 if (f
->actual
.len
!= 0)
910 sb_add_sb (out
, &f
->actual
);
912 sb_add_sb (out
, &f
->def
);
920 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
923 else if ((macro_alternate
|| macro_mri
)
924 && is_name_beginner (in
->ptr
[src
])
927 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
930 || src
+ 5 >= in
->len
931 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
932 || ! ISWHITE (in
->ptr
[src
+ 5])
933 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
937 src
= sub_actual (src
, in
, &t
, formal_hash
,
938 (macro_strip_at
&& inquote
) ? '@' : '\'',
943 src
= sb_skip_white (src
+ 5, in
);
944 while (in
->ptr
[src
] != '\n')
947 formal_entry
*f
= new_formal ();
949 src
= get_token (src
, in
, &f
->name
);
950 name
= sb_terminate (&f
->name
);
951 if (str_hash_insert (formal_hash
, name
, f
, 0) != NULL
)
953 as_bad_where (macro
->file
, macro
->line
+ macro_line
,
954 _("`%s' was already used as parameter "
955 "(or another local) name"), name
);
963 f
->index
= LOCAL_INDEX
;
967 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
968 sb_add_string (&f
->actual
, buf
);
971 src
= sb_skip_comma (src
, in
);
975 else if (in
->ptr
[src
] == '"'
976 || (macro_mri
&& in
->ptr
[src
] == '\''))
979 sb_add_char (out
, in
->ptr
[src
++]);
981 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
985 && in
->ptr
[src
] == '@')
987 sb_add_char (out
, '@');
992 && in
->ptr
[src
] == '='
994 && in
->ptr
[src
+ 1] == '=')
999 src
= get_token (src
+ 2, in
, &t
);
1000 ptr
= str_hash_find (formal_hash
, sb_terminate (&t
));
1003 /* FIXME: We should really return a warning string here,
1004 but we can't, because the == might be in the MRI
1005 comment field, and, since the nature of the MRI
1006 comment field depends upon the exact instruction
1007 being used, we don't have enough information here to
1008 figure out whether it is or not. Instead, we leave
1009 the == in place, which should cause a syntax error if
1010 it is not in a comment. */
1011 sb_add_char (out
, '=');
1012 sb_add_char (out
, '=');
1013 sb_add_sb (out
, &t
);
1017 if (ptr
->actual
.len
)
1019 sb_add_string (out
, "-1");
1023 sb_add_char (out
, '0');
1029 if (in
->ptr
[src
] == '\n')
1031 sb_add_char (out
, in
->ptr
[src
++]);
1037 while (loclist
!= NULL
)
1043 name
= sb_terminate (&loclist
->name
);
1044 str_hash_delete (formal_hash
, name
);
1045 del_formal (loclist
);
1049 if (!err
&& (out
->len
== 0 || out
->ptr
[out
->len
- 1] != '\n'))
1050 sb_add_char (out
, '\n');
1054 /* Assign values to the formal parameters of a macro, and expand the
1058 macro_expand (size_t idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1065 const char *err
= NULL
;
1069 /* Reset any old value the actuals may have. */
1070 for (f
= m
->formals
; f
; f
= f
->next
)
1071 sb_reset (&f
->actual
);
1073 while (f
!= NULL
&& f
->index
< 0)
1078 /* The macro may be called with an optional qualifier, which may
1079 be referred to in the macro body as \0. */
1080 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1082 /* The Microtec assembler ignores this if followed by a white space.
1083 (Macro invocation with empty extension) */
1086 && in
->ptr
[idx
] != ' '
1087 && in
->ptr
[idx
] != '\t')
1089 formal_entry
*n
= new_formal ();
1091 n
->index
= QUAL_INDEX
;
1093 n
->next
= m
->formals
;
1096 idx
= get_any_string (idx
, in
, &n
->actual
);
1101 /* Peel off the actuals and store them away in the hash tables' actuals. */
1102 idx
= sb_skip_white (idx
, in
);
1103 while (idx
< in
->len
)
1107 /* Look and see if it's a positional or keyword arg. */
1109 while (scan
< in
->len
1110 && !ISSEP (in
->ptr
[scan
])
1111 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1112 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1114 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1118 /* It's OK to go from positional to keyword. */
1120 /* This is a keyword arg, fetch the formal name and
1121 then the actual stuff. */
1123 idx
= get_token (idx
, in
, &t
);
1124 if (in
->ptr
[idx
] != '=')
1126 err
= _("confusion in formal parameters");
1130 /* Lookup the formal in the macro's list. */
1131 ptr
= str_hash_find (m
->formal_hash
, sb_terminate (&t
));
1134 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1138 idx
= get_any_string (idx
+ 1, in
, &t
);
1142 /* Insert this value into the right place. */
1143 if (ptr
->actual
.len
)
1145 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1148 sb_reset (&ptr
->actual
);
1150 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1151 if (ptr
->actual
.len
> 0)
1159 err
= _("can't mix positional and keyword arguments");
1170 err
= _("too many positional arguments");
1177 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1178 if ((*pf
)->index
>= c
)
1179 c
= (*pf
)->index
+ 1;
1186 if (f
->type
!= FORMAL_VARARG
)
1187 idx
= get_any_string (idx
, in
, &f
->actual
);
1190 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1193 if (f
->actual
.len
> 0)
1199 while (f
!= NULL
&& f
->index
< 0);
1203 idx
= sb_skip_comma (idx
, in
);
1206 if (in
->ptr
[idx
] == ',')
1208 if (ISWHITE (in
->ptr
[idx
]))
1215 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1217 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1218 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1228 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1229 ptr
= str_hash_find (m
->formal_hash
, sb_terminate (&t
));
1230 sprintf (buffer
, "%d", narg
);
1231 sb_add_string (&ptr
->actual
, buffer
);
1234 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1237 /* Discard any unnamed formal arguments. */
1245 if ((*pf
)->name
.len
!= 0)
1263 /* Check for a macro. If one is found, put the expansion into
1264 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1267 check_macro (const char *line
, sb
*expand
,
1268 const char **error
, macro_entry
**info
)
1275 if (! is_name_beginner (*line
)
1276 && (! macro_mri
|| *line
!= '.'))
1280 while (is_part_of_name (*s
))
1282 if (is_name_ender (*s
))
1285 copy
= xmemdup0 (line
, s
- line
);
1286 for (cls
= copy
; *cls
!= '\0'; cls
++)
1287 *cls
= TOLOWER (*cls
);
1289 macro
= str_hash_find (macro_hash
, copy
);
1295 /* Wrap the line up in an sb. */
1297 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1298 sb_add_char (&line_sb
, *s
++);
1301 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1305 /* Export the macro information if requested. */
1312 /* Delete a macro. */
1315 delete_macro (const char *name
)
1321 len
= strlen (name
);
1322 copy
= XNEWVEC (char, len
+ 1);
1323 for (i
= 0; i
< len
; ++i
)
1324 copy
[i
] = TOLOWER (name
[i
]);
1327 macro
= str_hash_find (macro_hash
, copy
);
1331 str_hash_delete (macro_hash
, copy
);
1334 as_warn (_("Attempt to purge non-existing macro `%s'"), copy
);
1338 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1339 combined macro definition and execution. This returns NULL on
1340 success, or an error message otherwise. */
1343 expand_irp (int irpc
, size_t idx
, sb
*in
, sb
*out
, size_t (*get_line
) (sb
*))
1348 const char *err
= NULL
;
1350 idx
= sb_skip_white (idx
, in
);
1353 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1354 return _("unexpected end of file in irp or irpc");
1360 idx
= get_token (idx
, in
, &f
.name
);
1361 if (f
.name
.len
== 0)
1362 return _("missing model parameter");
1364 h
= str_htab_create ();
1366 str_hash_insert (h
, sb_terminate (&f
.name
), &f
, 0);
1370 f
.type
= FORMAL_OPTIONAL
;
1374 idx
= sb_skip_comma (idx
, in
);
1377 /* Expand once with a null string. */
1378 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1382 bool in_quotes
= false;
1384 if (irpc
&& in
->ptr
[idx
] == '"')
1390 while (idx
< in
->len
)
1393 idx
= get_any_string (idx
, in
, &f
.actual
);
1396 if (in
->ptr
[idx
] == '"')
1401 in_quotes
= ! in_quotes
;
1403 nxt
= sb_skip_white (idx
+ 1, in
);
1410 sb_reset (&f
.actual
);
1411 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1415 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1419 idx
= sb_skip_comma (idx
, in
);
1420 else if (! in_quotes
)
1421 idx
= sb_skip_white (idx
, in
);
1426 sb_kill (&f
.actual
);