1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2023 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
, more
;
125 if (to_len
== 4 && strcasecmp (to
, "ENDR") == 0)
131 from_len
= strlen (from
);
133 /* Record the present source position, such that diagnostics and debug info
134 can be properly associated with the respective original lines, rather
135 than with the line of the ending directive (TO). */
140 as_where_top (&line
);
142 linefile
= xasprintf ("\t.linefile %u .", line
+ 1);
144 linefile
= xasprintf ("\tlinefile %u .", line
+ 1);
145 sb_add_string (ptr
, linefile
);
149 line_start
= ptr
->len
;
150 more
= get_line (ptr
);
153 /* Try to find the first pseudo op on the line. */
154 size_t i
= line_start
;
155 bool had_colon
= false;
157 /* With normal syntax we can suck what we want till we get
158 to the dot. With the alternate, labels have to start in
159 the first column, since we can't tell what's a label and
160 what's a pseudoop. */
162 if (! LABELS_WITHOUT_COLONS
)
164 /* Skip leading whitespace. */
165 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
171 /* Skip over a label, if any. */
172 if (i
>= ptr
->len
|| ! is_name_beginner (ptr
->ptr
[i
]))
175 while (i
< ptr
->len
&& is_part_of_name (ptr
->ptr
[i
]))
177 if (i
< ptr
->len
&& is_name_ender (ptr
->ptr
[i
]))
179 /* Skip whitespace. */
180 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
182 /* Check for the colon. */
183 if (i
>= ptr
->len
|| ptr
->ptr
[i
] != ':')
185 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
186 colon after a label. If we do have a colon on the
187 first label then handle more than one label on the
188 line, assuming that each label has a colon. */
189 if (LABELS_WITHOUT_COLONS
&& !had_colon
)
199 /* Skip trailing whitespace. */
200 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
203 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
207 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
209 size_t len
= ptr
->len
- i
;
212 if (len
>= 5 && strncasecmp (ptr
->ptr
+ i
, "IREPC", 5) == 0)
214 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "IREP", 4) == 0)
216 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "IRPC", 4) == 0)
218 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "REPT", 4) == 0)
220 else if (len
>= 3 && strncasecmp (ptr
->ptr
+ i
, "IRP", 3) == 0)
222 else if (len
>= 3 && strncasecmp (ptr
->ptr
+ i
, "REP", 3) == 0)
229 && strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0)
232 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
233 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
236 && strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
238 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
239 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
244 /* Reset the string to not include the ending rune. */
245 ptr
->len
= line_start
;
251 Apply .linefile directives that appear within the macro, alongside
252 keeping them for later expansion of the macro. */
253 if (from
!= NULL
&& strcasecmp (from
, "MACRO") == 0
254 && len
>= 8 && strncasecmp (ptr
->ptr
+ i
, "linefile", 8) == 0)
256 char saved_eol_char
= ptr
->ptr
[ptr
->len
];
258 ptr
->ptr
[ptr
->len
] = '\0';
259 temp_ilp (ptr
->ptr
+ i
+ 8);
262 ptr
->ptr
[ptr
->len
] = saved_eol_char
;
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 (size_t 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 (size_t idx
, sb
*in
, sb
*acc
)
308 && (in
->ptr
[idx
] == '"'
309 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
310 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
312 if (in
->ptr
[idx
] == '<')
317 && (in
->ptr
[idx
] != '>' || nest
))
319 if (in
->ptr
[idx
] == '!')
322 sb_add_char (acc
, in
->ptr
[idx
++]);
326 if (in
->ptr
[idx
] == '>')
328 if (in
->ptr
[idx
] == '<')
330 sb_add_char (acc
, in
->ptr
[idx
++]);
335 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
337 char tchar
= in
->ptr
[idx
];
342 while (idx
< in
->len
)
344 if (in
->ptr
[idx
- 1] == '\\')
349 if (macro_alternate
&& in
->ptr
[idx
] == '!')
353 sb_add_char (acc
, in
->ptr
[idx
]);
357 else if (escaped
&& in
->ptr
[idx
] == tchar
)
359 sb_add_char (acc
, tchar
);
364 if (in
->ptr
[idx
] == tchar
)
368 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
372 sb_add_char (acc
, in
->ptr
[idx
]);
382 /* Fetch string from the input stream,
384 'Bxyx<whitespace> -> return 'Bxyza
385 %<expr> -> return string of decimal value of <expr>
386 "string" -> return string
387 (string) -> return (string-including-whitespaces)
388 xyx<whitespace> -> return xyz. */
391 get_any_string (size_t idx
, sb
*in
, sb
*out
)
394 idx
= sb_skip_white (idx
, in
);
398 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
400 while (idx
< in
->len
&& !ISSEP (in
->ptr
[idx
]))
401 sb_add_char (out
, in
->ptr
[idx
++]);
403 else if (in
->ptr
[idx
] == '%' && macro_alternate
)
408 /* Turns the next expression into a string. */
409 /* xgettext: no-c-format */
410 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
414 sprintf (buf
, "%" PRId64
, (int64_t) val
);
415 sb_add_string (out
, buf
);
417 else if (in
->ptr
[idx
] == '"'
418 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
419 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
421 if (macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
423 /* Keep the quotes. */
424 sb_add_char (out
, '"');
425 idx
= getstring (idx
, in
, out
);
426 sb_add_char (out
, '"');
430 idx
= getstring (idx
, in
, out
);
435 char *br_buf
= XNEWVEC (char, 1);
436 char *in_br
= br_buf
;
441 || (in
->ptr
[idx
] != ' '
442 && in
->ptr
[idx
] != '\t'))
443 && in
->ptr
[idx
] != ','
444 && (in
->ptr
[idx
] != '<'
445 || (! macro_alternate
&& ! macro_mri
)))
447 char tchar
= in
->ptr
[idx
];
453 sb_add_char (out
, in
->ptr
[idx
++]);
455 && in
->ptr
[idx
] != tchar
)
456 sb_add_char (out
, in
->ptr
[idx
++]);
469 br_buf
= XNEWVEC (char, strlen (in_br
) + 2);
470 strcpy (br_buf
+ 1, in_br
);
485 sb_add_char (out
, tchar
);
495 /* Allocate a new formal. */
497 static formal_entry
*
500 formal_entry
*formal
;
502 formal
= XNEW (formal_entry
);
504 sb_new (&formal
->name
);
505 sb_new (&formal
->def
);
506 sb_new (&formal
->actual
);
508 formal
->type
= FORMAL_OPTIONAL
;
515 del_formal (formal_entry
*formal
)
517 sb_kill (&formal
->actual
);
518 sb_kill (&formal
->def
);
519 sb_kill (&formal
->name
);
523 /* Pick up the formal parameters of a macro definition. */
526 do_formals (macro_entry
*macro
, size_t idx
, sb
*in
)
528 formal_entry
**p
= ¯o
->formals
;
531 idx
= sb_skip_white (idx
, in
);
532 while (idx
< in
->len
)
534 formal_entry
*formal
= new_formal ();
537 idx
= get_token (idx
, in
, &formal
->name
);
538 if (formal
->name
.len
== 0)
540 if (macro
->formal_count
)
542 del_formal (formal
); /* 'formal' goes out of scope. */
545 idx
= sb_skip_white (idx
, in
);
546 /* This is a formal. */
547 name
= sb_terminate (&formal
->name
);
550 && in
->ptr
[idx
] == ':'
551 && (! is_name_beginner (':')
552 || idx
+ 1 >= in
->len
553 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
555 /* Got a qualifier. */
559 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
560 sb_terminate (&qual
);
562 as_bad_where (macro
->file
,
564 _("Missing parameter qualifier for `%s' in macro `%s'"),
567 else if (strcmp (qual
.ptr
, "req") == 0)
568 formal
->type
= FORMAL_REQUIRED
;
569 else if (strcmp (qual
.ptr
, "vararg") == 0)
570 formal
->type
= FORMAL_VARARG
;
572 as_bad_where (macro
->file
,
574 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
579 idx
= sb_skip_white (idx
, in
);
581 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
584 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
585 idx
= sb_skip_white (idx
, in
);
586 if (formal
->type
== FORMAL_REQUIRED
)
588 sb_reset (&formal
->def
);
589 as_warn_where (macro
->file
,
591 _("Pointless default value for required parameter `%s' in macro `%s'"),
597 /* Add to macro's hash table. */
598 if (str_hash_insert (macro
->formal_hash
, name
, formal
, 0) != NULL
)
600 as_bad_where (macro
->file
, macro
->line
,
601 _("A parameter named `%s' "
602 "already exists for macro `%s'"),
606 formal
->index
= macro
->formal_count
++;
609 if (formal
->type
== FORMAL_VARARG
)
612 idx
= sb_skip_comma (idx
, in
);
613 if (idx
!= cidx
&& idx
>= in
->len
)
622 formal_entry
*formal
= new_formal ();
624 /* Add a special NARG formal, which macro_expand will set to the
625 number of arguments. */
626 /* The same MRI assemblers which treat '@' characters also use
627 the name $NARG. At least until we find an exception. */
633 sb_add_string (&formal
->name
, name
);
635 /* Add to macro's hash table. */
636 if (str_hash_insert (macro
->formal_hash
, name
, formal
, 0) != NULL
)
638 as_bad_where (macro
->file
, macro
->line
,
639 _("Reserved word `%s' used as parameter in macro `%s'"),
643 formal
->index
= NARG_INDEX
;
650 /* Free the memory allocated to a macro. */
653 free_macro (macro_entry
*macro
)
655 formal_entry
*formal
;
657 for (formal
= macro
->formals
; formal
; )
662 formal
= formal
->next
;
665 htab_delete (macro
->formal_hash
);
666 sb_kill (¯o
->sub
);
670 /* Define a new macro. Returns NULL on success, otherwise returns an
671 error message. If NAMEP is not NULL, *NAMEP is set to the name of
672 the macro which was defined. */
675 define_macro (size_t idx
, sb
*in
, sb
*label
,
676 size_t (*get_line
) (sb
*),
677 const char *file
, unsigned int line
,
682 const char *error
= NULL
;
684 macro
= XNEW (macro_entry
);
685 sb_new (¯o
->sub
);
690 macro
->formal_count
= 0;
692 macro
->formal_hash
= str_htab_create ();
694 idx
= sb_skip_white (idx
, in
);
695 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
696 error
= _("unexpected end of file in macro `%s' definition");
697 if (label
!= NULL
&& label
->len
!= 0)
699 sb_add_sb (&name
, label
);
700 macro
->name
= sb_terminate (&name
);
701 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
703 /* It's the label: MACRO (formals,...) sort */
704 idx
= do_formals (macro
, idx
+ 1, in
);
705 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
706 idx
= sb_skip_white (idx
+ 1, in
);
708 error
= _("missing `)' after formals in macro definition `%s'");
712 /* It's the label: MACRO formals,... sort */
713 idx
= do_formals (macro
, idx
, in
);
720 idx
= get_token (idx
, in
, &name
);
721 macro
->name
= sb_terminate (&name
);
723 error
= _("Missing macro name");
724 cidx
= sb_skip_white (idx
, in
);
725 idx
= sb_skip_comma (cidx
, in
);
726 if (idx
== cidx
|| idx
< in
->len
)
727 idx
= do_formals (macro
, idx
, in
);
731 if (!error
&& idx
< in
->len
)
732 error
= _("Bad parameter list for macro `%s'");
734 /* And stick it in the macro hash table. */
735 for (idx
= 0; idx
< name
.len
; idx
++)
736 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
739 if (str_hash_insert (macro_hash
, macro
->name
, macro
, 0) != NULL
)
740 error
= _("Macro `%s' was already defined");
744 *namep
= macro
->name
;
754 /* Scan a token, and then skip KIND. */
757 get_apost_token (size_t idx
, sb
*in
, sb
*name
, int kind
)
759 idx
= get_token (idx
, in
, name
);
761 && in
->ptr
[idx
] == kind
762 && (! macro_mri
|| macro_strip_at
)
763 && (! macro_strip_at
|| kind
== '@'))
768 /* Substitute the actual value for a formal parameter. */
771 sub_actual (size_t start
, sb
*in
, sb
*t
, struct htab
*formal_hash
,
772 int kind
, sb
*out
, int copyifnotthere
)
777 src
= get_apost_token (start
, in
, t
, kind
);
778 /* See if it's in the macro's hash table, unless this is
779 macro_strip_at and kind is '@' and the token did not end in '@'. */
782 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
785 ptr
= str_hash_find (formal_hash
, sb_terminate (t
));
790 sb_add_sb (out
, &ptr
->actual
);
794 sb_add_sb (out
, &ptr
->def
);
797 else if (kind
== '&')
799 /* Doing this permits people to use & in macro bodies. */
800 sb_add_char (out
, '&');
802 if (src
!= start
&& in
->ptr
[src
- 1] == '&')
803 sb_add_char (out
, '&');
805 else if (copyifnotthere
)
811 sb_add_char (out
, '\\');
817 /* Expand the body of a macro. */
820 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
821 struct htab
*formal_hash
, const macro_entry
*macro
)
825 int inquote
= 0, macro_line
= 0;
826 formal_entry
*loclist
= NULL
;
827 const char *err
= NULL
;
831 while (src
< in
->len
&& !err
)
833 if (in
->ptr
[src
] == '&')
838 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
839 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
841 sb_add_char (out
, in
->ptr
[src
++]);
845 /* Permit macro parameter substitution delineated with
846 an '&' prefix and optional '&' suffix. */
847 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
850 else if (in
->ptr
[src
] == '\\')
853 if (src
< in
->len
&& in
->ptr
[src
] == '(')
855 /* Sub in till the next ')' literally. */
857 while (src
< in
->len
&& in
->ptr
[src
] != ')')
859 sb_add_char (out
, in
->ptr
[src
++]);
864 err
= _("missing `)'");
866 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
868 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
870 /* Sub in the macro invocation number. */
874 sprintf (buffer
, "%d", macro_number
);
875 sb_add_string (out
, buffer
);
877 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
879 /* This is a preprocessor variable name, we don't do them
881 sb_add_char (out
, '\\');
882 sb_add_char (out
, '&');
885 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
890 if (ISDIGIT (in
->ptr
[src
]))
891 ind
= in
->ptr
[src
] - '0';
892 else if (ISUPPER (in
->ptr
[src
]))
893 ind
= in
->ptr
[src
] - 'A' + 10;
895 ind
= in
->ptr
[src
] - 'a' + 10;
897 for (f
= formals
; f
!= NULL
; f
= f
->next
)
899 if (f
->index
== ind
- 1)
901 if (f
->actual
.len
!= 0)
902 sb_add_sb (out
, &f
->actual
);
904 sb_add_sb (out
, &f
->def
);
912 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
915 else if ((macro_alternate
|| macro_mri
)
916 && is_name_beginner (in
->ptr
[src
])
919 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
922 || src
+ 5 >= in
->len
923 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
924 || ! ISWHITE (in
->ptr
[src
+ 5])
925 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
929 src
= sub_actual (src
, in
, &t
, formal_hash
,
930 (macro_strip_at
&& inquote
) ? '@' : '\'',
935 src
= sb_skip_white (src
+ 5, in
);
936 while (in
->ptr
[src
] != '\n')
939 formal_entry
*f
= new_formal ();
941 src
= get_token (src
, in
, &f
->name
);
942 name
= sb_terminate (&f
->name
);
943 if (str_hash_insert (formal_hash
, name
, f
, 0) != NULL
)
945 as_bad_where (macro
->file
, macro
->line
+ macro_line
,
946 _("`%s' was already used as parameter "
947 "(or another local) name"), name
);
955 f
->index
= LOCAL_INDEX
;
959 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
960 sb_add_string (&f
->actual
, buf
);
963 src
= sb_skip_comma (src
, in
);
967 else if (in
->ptr
[src
] == '"'
968 || (macro_mri
&& in
->ptr
[src
] == '\''))
971 sb_add_char (out
, in
->ptr
[src
++]);
973 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
977 && in
->ptr
[src
] == '@')
979 sb_add_char (out
, '@');
984 && in
->ptr
[src
] == '='
986 && in
->ptr
[src
+ 1] == '=')
991 src
= get_token (src
+ 2, in
, &t
);
992 ptr
= str_hash_find (formal_hash
, sb_terminate (&t
));
995 /* FIXME: We should really return a warning string here,
996 but we can't, because the == might be in the MRI
997 comment field, and, since the nature of the MRI
998 comment field depends upon the exact instruction
999 being used, we don't have enough information here to
1000 figure out whether it is or not. Instead, we leave
1001 the == in place, which should cause a syntax error if
1002 it is not in a comment. */
1003 sb_add_char (out
, '=');
1004 sb_add_char (out
, '=');
1005 sb_add_sb (out
, &t
);
1009 if (ptr
->actual
.len
)
1011 sb_add_string (out
, "-1");
1015 sb_add_char (out
, '0');
1021 if (in
->ptr
[src
] == '\n')
1023 sb_add_char (out
, in
->ptr
[src
++]);
1029 while (loclist
!= NULL
)
1035 name
= sb_terminate (&loclist
->name
);
1036 str_hash_delete (formal_hash
, name
);
1037 del_formal (loclist
);
1041 if (!err
&& (out
->len
== 0 || out
->ptr
[out
->len
- 1] != '\n'))
1042 sb_add_char (out
, '\n');
1046 /* Assign values to the formal parameters of a macro, and expand the
1050 macro_expand (size_t idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1057 const char *err
= NULL
;
1061 /* Reset any old value the actuals may have. */
1062 for (f
= m
->formals
; f
; f
= f
->next
)
1063 sb_reset (&f
->actual
);
1065 while (f
!= NULL
&& f
->index
< 0)
1070 /* The macro may be called with an optional qualifier, which may
1071 be referred to in the macro body as \0. */
1072 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1074 /* The Microtec assembler ignores this if followed by a white space.
1075 (Macro invocation with empty extension) */
1078 && in
->ptr
[idx
] != ' '
1079 && in
->ptr
[idx
] != '\t')
1081 formal_entry
*n
= new_formal ();
1083 n
->index
= QUAL_INDEX
;
1085 n
->next
= m
->formals
;
1088 idx
= get_any_string (idx
, in
, &n
->actual
);
1093 /* Peel off the actuals and store them away in the hash tables' actuals. */
1094 idx
= sb_skip_white (idx
, in
);
1095 while (idx
< in
->len
)
1099 /* Look and see if it's a positional or keyword arg. */
1101 while (scan
< in
->len
1102 && !ISSEP (in
->ptr
[scan
])
1103 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1104 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1106 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1110 /* It's OK to go from positional to keyword. */
1112 /* This is a keyword arg, fetch the formal name and
1113 then the actual stuff. */
1115 idx
= get_token (idx
, in
, &t
);
1116 if (in
->ptr
[idx
] != '=')
1118 err
= _("confusion in formal parameters");
1122 /* Lookup the formal in the macro's list. */
1123 ptr
= str_hash_find (m
->formal_hash
, sb_terminate (&t
));
1126 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1130 idx
= get_any_string (idx
+ 1, in
, &t
);
1134 /* Insert this value into the right place. */
1135 if (ptr
->actual
.len
)
1137 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1140 sb_reset (&ptr
->actual
);
1142 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1143 if (ptr
->actual
.len
> 0)
1151 err
= _("can't mix positional and keyword arguments");
1162 err
= _("too many positional arguments");
1169 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1170 if ((*pf
)->index
>= c
)
1171 c
= (*pf
)->index
+ 1;
1178 if (f
->type
!= FORMAL_VARARG
)
1179 idx
= get_any_string (idx
, in
, &f
->actual
);
1182 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1185 if (f
->actual
.len
> 0)
1191 while (f
!= NULL
&& f
->index
< 0);
1195 idx
= sb_skip_comma (idx
, in
);
1198 if (in
->ptr
[idx
] == ',')
1200 if (ISWHITE (in
->ptr
[idx
]))
1207 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1209 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1210 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1220 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1221 ptr
= str_hash_find (m
->formal_hash
, sb_terminate (&t
));
1222 sprintf (buffer
, "%d", narg
);
1223 sb_add_string (&ptr
->actual
, buffer
);
1226 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1229 /* Discard any unnamed formal arguments. */
1237 if ((*pf
)->name
.len
!= 0)
1255 /* Check for a macro. If one is found, put the expansion into
1256 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1259 check_macro (const char *line
, sb
*expand
,
1260 const char **error
, macro_entry
**info
)
1267 if (! is_name_beginner (*line
)
1268 && (! macro_mri
|| *line
!= '.'))
1272 while (is_part_of_name (*s
))
1274 if (is_name_ender (*s
))
1277 copy
= xmemdup0 (line
, s
- line
);
1278 for (cls
= copy
; *cls
!= '\0'; cls
++)
1279 *cls
= TOLOWER (*cls
);
1281 macro
= str_hash_find (macro_hash
, copy
);
1287 /* Wrap the line up in an sb. */
1289 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1290 sb_add_char (&line_sb
, *s
++);
1293 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1297 /* Export the macro information if requested. */
1304 /* Delete a macro. */
1307 delete_macro (const char *name
)
1313 len
= strlen (name
);
1314 copy
= XNEWVEC (char, len
+ 1);
1315 for (i
= 0; i
< len
; ++i
)
1316 copy
[i
] = TOLOWER (name
[i
]);
1319 macro
= str_hash_find (macro_hash
, copy
);
1323 str_hash_delete (macro_hash
, copy
);
1326 as_warn (_("Attempt to purge non-existing macro `%s'"), copy
);
1330 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1331 combined macro definition and execution. This returns NULL on
1332 success, or an error message otherwise. */
1335 expand_irp (int irpc
, size_t idx
, sb
*in
, sb
*out
, size_t (*get_line
) (sb
*))
1340 const char *err
= NULL
;
1342 idx
= sb_skip_white (idx
, in
);
1345 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1346 return _("unexpected end of file in irp or irpc");
1352 idx
= get_token (idx
, in
, &f
.name
);
1353 if (f
.name
.len
== 0)
1354 return _("missing model parameter");
1356 h
= str_htab_create ();
1358 str_hash_insert (h
, sb_terminate (&f
.name
), &f
, 0);
1362 f
.type
= FORMAL_OPTIONAL
;
1366 idx
= sb_skip_comma (idx
, in
);
1369 /* Expand once with a null string. */
1370 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1374 bool in_quotes
= false;
1376 if (irpc
&& in
->ptr
[idx
] == '"')
1382 while (idx
< in
->len
)
1385 idx
= get_any_string (idx
, in
, &f
.actual
);
1388 if (in
->ptr
[idx
] == '"')
1393 in_quotes
= ! in_quotes
;
1395 nxt
= sb_skip_white (idx
+ 1, in
);
1402 sb_reset (&f
.actual
);
1403 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1407 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1411 idx
= sb_skip_comma (idx
, in
);
1412 else if (! in_quotes
)
1413 idx
= sb_skip_white (idx
, in
);
1418 sb_kill (&f
.actual
);