1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2024 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 || ((flag_macro_alternate || flag_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 should strip '@' characters. */
55 #define macro_strip_at false
57 /* Number of macro expansions that have been done. */
59 static unsigned int macro_number
;
61 static void free_macro (macro_entry
*);
64 macro_del_f (void *ent
)
66 string_tuple_t
*tuple
= ent
;
67 free_macro ((macro_entry
*) tuple
->value
);
70 /* Initialize macro processing. */
75 macro_hash
= htab_create_alloc (16, hash_string_tuple
, eq_string_tuple
,
76 macro_del_f
, notes_calloc
, NULL
);
83 htab_delete (macro_hash
);
86 /* Read input lines till we get to a TO string.
87 Increase nesting depth if we get a FROM string.
88 Put the results into sb at PTR.
89 FROM may be NULL (or will be ignored) if TO is "ENDR".
90 Add a new input line to an sb using GET_LINE.
91 Return 1 on success, 0 on unexpected EOF. */
94 buffer_and_nest (const char *from
, const char *to
, sb
*ptr
,
95 size_t (*get_line
) (sb
*))
98 size_t to_len
= strlen (to
);
100 size_t line_start
, more
;
102 if (to_len
== 4 && strcasecmp (to
, "ENDR") == 0)
108 from_len
= strlen (from
);
110 /* Record the present source position, such that diagnostics and debug info
111 can be properly associated with the respective original lines, rather
112 than with the line of the ending directive (TO). */
117 as_where_top (&line
);
119 linefile
= xasprintf ("\t.linefile %u .", line
+ 1);
121 linefile
= xasprintf ("\tlinefile %u .", line
+ 1);
122 sb_add_string (ptr
, linefile
);
126 line_start
= ptr
->len
;
127 more
= get_line (ptr
);
130 /* Try to find the first pseudo op on the line. */
131 size_t i
= line_start
;
132 bool had_colon
= false;
134 /* With normal syntax we can suck what we want till we get
135 to the dot. With the alternate, labels have to start in
136 the first column, since we can't tell what's a label and
137 what's a pseudoop. */
139 if (! LABELS_WITHOUT_COLONS
)
141 /* Skip leading whitespace. */
142 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
148 /* Skip over a label, if any. */
149 if (i
>= ptr
->len
|| ! is_name_beginner (ptr
->ptr
[i
]))
152 while (i
< ptr
->len
&& is_part_of_name (ptr
->ptr
[i
]))
154 if (i
< ptr
->len
&& is_name_ender (ptr
->ptr
[i
]))
156 /* Skip whitespace. */
157 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
159 /* Check for the colon. */
160 if (i
>= ptr
->len
|| ptr
->ptr
[i
] != ':')
162 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
163 colon after a label. If we do have a colon on the
164 first label then handle more than one label on the
165 line, assuming that each label has a colon. */
166 if (LABELS_WITHOUT_COLONS
&& !had_colon
)
176 /* Skip trailing whitespace. */
177 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
180 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
184 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
186 size_t len
= ptr
->len
- i
;
189 if (len
>= 5 && strncasecmp (ptr
->ptr
+ i
, "IREPC", 5) == 0)
191 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "IREP", 4) == 0)
193 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "IRPC", 4) == 0)
195 else if (len
>= 4 && strncasecmp (ptr
->ptr
+ i
, "REPT", 4) == 0)
197 else if (len
>= 3 && strncasecmp (ptr
->ptr
+ i
, "IRP", 3) == 0)
199 else if (len
>= 3 && strncasecmp (ptr
->ptr
+ i
, "REP", 3) == 0)
206 && strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0)
209 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
210 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
213 && strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
215 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
216 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
221 /* Reset the string to not include the ending rune. */
222 ptr
->len
= line_start
;
224 /* With the ending directive consumed here, announce the
225 line for macro-expanded listings. */
226 if (listing
& LISTING_MACEXP
)
227 listing_newline (NULL
);
233 Apply .linefile directives that appear within the macro, alongside
234 keeping them for later expansion of the macro. */
235 if (from
!= NULL
&& strcasecmp (from
, "MACRO") == 0
236 && len
>= 8 && strncasecmp (ptr
->ptr
+ i
, "linefile", 8) == 0)
238 sb_add_char (ptr
, more
);
239 temp_ilp (sb_terminate (ptr
) + i
+ 8);
242 line_start
= ptr
->len
;
243 more
= get_line (ptr
);
248 /* Add the original end-of-line char to the end and keep running. */
249 sb_add_char (ptr
, more
);
250 line_start
= ptr
->len
;
251 more
= get_line (ptr
);
254 /* Return 1 on success, 0 on unexpected EOF. */
258 /* Pick up a token. */
261 get_token (size_t idx
, sb
*in
, sb
*name
)
264 && is_name_beginner (in
->ptr
[idx
]))
266 sb_add_char (name
, in
->ptr
[idx
++]);
268 && is_part_of_name (in
->ptr
[idx
]))
270 sb_add_char (name
, in
->ptr
[idx
++]);
273 && is_name_ender (in
->ptr
[idx
]))
275 sb_add_char (name
, in
->ptr
[idx
++]);
278 /* Ignore trailing &. */
279 if (flag_macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
284 /* Pick up a string. */
287 getstring (size_t idx
, sb
*in
, sb
*acc
)
290 && (in
->ptr
[idx
] == '"'
291 || (in
->ptr
[idx
] == '<' && (flag_macro_alternate
|| flag_mri
))
292 || (in
->ptr
[idx
] == '\'' && flag_macro_alternate
)))
294 if (in
->ptr
[idx
] == '<')
299 && (in
->ptr
[idx
] != '>' || nest
))
301 if (in
->ptr
[idx
] == '!')
304 sb_add_char (acc
, in
->ptr
[idx
++]);
308 if (in
->ptr
[idx
] == '>')
310 if (in
->ptr
[idx
] == '<')
312 sb_add_char (acc
, in
->ptr
[idx
++]);
317 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
319 char tchar
= in
->ptr
[idx
];
324 while (idx
< in
->len
)
326 if (in
->ptr
[idx
- 1] == '\\')
331 if (flag_macro_alternate
&& in
->ptr
[idx
] == '!')
335 sb_add_char (acc
, in
->ptr
[idx
]);
339 else if (escaped
&& in
->ptr
[idx
] == tchar
)
341 sb_add_char (acc
, tchar
);
346 if (in
->ptr
[idx
] == tchar
)
350 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
354 sb_add_char (acc
, in
->ptr
[idx
]);
364 /* Fetch string from the input stream,
366 'Bxyx<whitespace> -> return 'Bxyza
367 %<expr> -> return string of decimal value of <expr>
368 "string" -> return string
369 (string) -> return (string-including-whitespaces)
370 xyx<whitespace> -> return xyz. */
373 get_any_string (size_t idx
, sb
*in
, sb
*out
)
376 idx
= sb_skip_white (idx
, in
);
380 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
382 while (idx
< in
->len
&& !ISSEP (in
->ptr
[idx
]))
383 sb_add_char (out
, in
->ptr
[idx
++]);
385 else if (in
->ptr
[idx
] == '%' && flag_macro_alternate
)
387 /* Turn the following expression into a string. */
393 temp_ilp (in
->ptr
+ idx
+ 1);
394 expression_and_evaluate (&ex
);
395 idx
= input_line_pointer
- in
->ptr
;
398 if (ex
.X_op
!= O_constant
)
399 as_bad (_("%% operator needs absolute expression"));
401 sprintf (buf
, "%" PRId64
, (int64_t) ex
.X_add_number
);
402 sb_add_string (out
, buf
);
404 else if (in
->ptr
[idx
] == '"'
405 || (in
->ptr
[idx
] == '<' && (flag_macro_alternate
|| flag_mri
))
406 || (flag_macro_alternate
&& in
->ptr
[idx
] == '\''))
408 if (flag_macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
410 /* Keep the quotes. */
411 sb_add_char (out
, '"');
412 idx
= getstring (idx
, in
, out
);
413 sb_add_char (out
, '"');
417 idx
= getstring (idx
, in
, out
);
422 char *br_buf
= XNEWVEC (char, 1);
423 char *in_br
= br_buf
;
428 || (in
->ptr
[idx
] != ' '
429 && in
->ptr
[idx
] != '\t'))
430 && in
->ptr
[idx
] != ','
431 && (in
->ptr
[idx
] != '<'
432 || (! flag_macro_alternate
&& ! flag_mri
)))
434 char tchar
= in
->ptr
[idx
];
440 sb_add_char (out
, in
->ptr
[idx
++]);
442 && in
->ptr
[idx
] != tchar
)
443 sb_add_char (out
, in
->ptr
[idx
++]);
456 br_buf
= XNEWVEC (char, strlen (in_br
) + 2);
457 strcpy (br_buf
+ 1, in_br
);
472 sb_add_char (out
, tchar
);
482 /* Allocate a new formal. */
484 static formal_entry
*
487 formal_entry
*formal
;
489 formal
= XNEW (formal_entry
);
491 sb_new (&formal
->name
);
492 sb_new (&formal
->def
);
493 sb_new (&formal
->actual
);
495 formal
->type
= FORMAL_OPTIONAL
;
502 del_formal (formal_entry
*formal
)
504 sb_kill (&formal
->actual
);
505 sb_kill (&formal
->def
);
506 sb_kill (&formal
->name
);
510 /* Pick up the formal parameters of a macro definition. */
513 do_formals (macro_entry
*macro
, size_t idx
, sb
*in
)
515 formal_entry
**p
= ¯o
->formals
;
518 idx
= sb_skip_white (idx
, in
);
519 while (idx
< in
->len
)
521 formal_entry
*formal
= new_formal ();
524 idx
= get_token (idx
, in
, &formal
->name
);
525 if (formal
->name
.len
== 0)
527 if (macro
->formal_count
)
529 del_formal (formal
); /* 'formal' goes out of scope. */
532 idx
= sb_skip_white (idx
, in
);
533 /* This is a formal. */
534 name
= sb_terminate (&formal
->name
);
537 && in
->ptr
[idx
] == ':'
538 && (! is_name_beginner (':')
539 || idx
+ 1 >= in
->len
540 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
542 /* Got a qualifier. */
546 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
547 sb_terminate (&qual
);
549 as_bad_where (macro
->file
,
551 _("Missing parameter qualifier for `%s' in macro `%s'"),
554 else if (strcmp (qual
.ptr
, "req") == 0)
555 formal
->type
= FORMAL_REQUIRED
;
556 else if (strcmp (qual
.ptr
, "vararg") == 0)
557 formal
->type
= FORMAL_VARARG
;
559 as_bad_where (macro
->file
,
561 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
566 idx
= sb_skip_white (idx
, in
);
568 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
571 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
572 idx
= sb_skip_white (idx
, in
);
573 if (formal
->type
== FORMAL_REQUIRED
)
575 sb_reset (&formal
->def
);
576 as_warn_where (macro
->file
,
578 _("Pointless default value for required parameter `%s' in macro `%s'"),
584 /* Add to macro's hash table. */
585 if (str_hash_insert (macro
->formal_hash
, name
, formal
, 0) != NULL
)
587 as_bad_where (macro
->file
, macro
->line
,
588 _("A parameter named `%s' "
589 "already exists for macro `%s'"),
593 formal
->index
= macro
->formal_count
++;
596 if (formal
->type
== FORMAL_VARARG
)
599 idx
= sb_skip_comma (idx
, in
);
600 if (idx
!= cidx
&& idx
>= in
->len
)
609 formal_entry
*formal
= new_formal ();
611 /* Add a special NARG formal, which macro_expand will set to the
612 number of arguments. */
613 /* The same MRI assemblers which treat '@' characters also use
614 the name $NARG. At least until we find an exception. */
620 sb_add_string (&formal
->name
, name
);
622 /* Add to macro's hash table. */
623 if (str_hash_insert (macro
->formal_hash
, name
, formal
, 0) != NULL
)
625 as_bad_where (macro
->file
, macro
->line
,
626 _("Reserved word `%s' used as parameter in macro `%s'"),
630 formal
->index
= NARG_INDEX
;
637 /* Free the memory allocated to a macro. */
640 free_macro (macro_entry
*macro
)
642 formal_entry
*formal
;
644 for (formal
= macro
->formals
; formal
; )
649 formal
= formal
->next
;
652 htab_delete (macro
->formal_hash
);
653 sb_kill (¯o
->sub
);
654 free ((char *) macro
->name
);
658 /* Define a new macro. */
661 define_macro (sb
*in
, sb
*label
, size_t (*get_line
) (sb
*))
666 const char *error
= NULL
;
668 macro
= XNEW (macro_entry
);
669 sb_new (¯o
->sub
);
671 macro
->file
= as_where (¯o
->line
);
673 macro
->formal_count
= 0;
675 macro
->formal_hash
= str_htab_create ();
678 idx
= sb_skip_white (0, in
);
679 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
680 error
= _("unexpected end of file in macro `%s' definition");
681 if (label
!= NULL
&& label
->len
!= 0)
683 sb_add_sb (&name
, label
);
684 macro
->name
= sb_terminate (&name
);
685 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
687 /* It's the label: MACRO (formals,...) sort */
688 idx
= do_formals (macro
, idx
+ 1, in
);
689 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
690 idx
= sb_skip_white (idx
+ 1, in
);
692 error
= _("missing `)' after formals in macro definition `%s'");
696 /* It's the label: MACRO formals,... sort */
697 idx
= do_formals (macro
, idx
, in
);
704 idx
= get_token (idx
, in
, &name
);
705 macro
->name
= sb_terminate (&name
);
707 error
= _("Missing macro name");
708 cidx
= sb_skip_white (idx
, in
);
709 idx
= sb_skip_comma (cidx
, in
);
710 if (idx
== cidx
|| idx
< in
->len
)
711 idx
= do_formals (macro
, idx
, in
);
715 if (!error
&& idx
< in
->len
)
716 error
= _("Bad parameter list for macro `%s'");
718 /* And stick it in the macro hash table. */
719 for (idx
= 0; idx
< name
.len
; idx
++)
720 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
723 if (str_hash_insert (macro_hash
, macro
->name
, macro
, 0) != NULL
)
724 error
= _("Macro `%s' was already defined");
731 as_bad_where (macro
->file
, macro
->line
, error
, macro
->name
);
739 /* Scan a token, and then skip KIND. */
742 get_apost_token (size_t idx
, sb
*in
, sb
*name
, int kind
)
744 idx
= get_token (idx
, in
, name
);
746 && in
->ptr
[idx
] == kind
747 && (! flag_mri
|| macro_strip_at
)
748 && (! macro_strip_at
|| kind
== '@'))
753 /* Substitute the actual value for a formal parameter. */
756 sub_actual (size_t start
, sb
*in
, sb
*t
, struct htab
*formal_hash
,
757 int kind
, sb
*out
, int copyifnotthere
)
762 src
= get_apost_token (start
, in
, t
, kind
);
763 /* See if it's in the macro's hash table, unless this is
764 macro_strip_at and kind is '@' and the token did not end in '@'. */
767 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
770 ptr
= str_hash_find (formal_hash
, sb_terminate (t
));
775 sb_add_sb (out
, &ptr
->actual
);
779 sb_add_sb (out
, &ptr
->def
);
782 else if (kind
== '&')
784 /* Doing this permits people to use & in macro bodies. */
785 sb_add_char (out
, '&');
787 if (src
!= start
&& in
->ptr
[src
- 1] == '&')
788 sb_add_char (out
, '&');
790 else if (copyifnotthere
)
796 sb_add_char (out
, '\\');
802 /* Expand the body of a macro. */
805 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
806 struct htab
*formal_hash
, const macro_entry
*macro
,
807 unsigned int instance
)
811 int inquote
= 0, macro_line
= 0;
812 formal_entry
*loclist
= NULL
;
813 const char *err
= NULL
;
817 while (src
< in
->len
&& !err
)
819 if (in
->ptr
[src
] == '&')
824 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
825 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
827 sb_add_char (out
, in
->ptr
[src
++]);
831 /* Permit macro parameter substitution delineated with
832 an '&' prefix and optional '&' suffix. */
833 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
836 else if (in
->ptr
[src
] == '\\')
839 if (src
< in
->len
&& in
->ptr
[src
] == '(')
841 /* Sub in till the next ')' literally. */
843 while (src
< in
->len
&& in
->ptr
[src
] != ')')
845 sb_add_char (out
, in
->ptr
[src
++]);
850 err
= _("missing `)'");
852 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
854 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
856 /* Sub in the total macro invocation number. */
860 sprintf (buffer
, "%u", macro_number
);
861 sb_add_string (out
, buffer
);
863 else if (src
< in
->len
&& in
->ptr
[src
] == '+')
865 /* Sub in the current macro invocation number. */
869 sprintf (buffer
, "%d", instance
);
870 sb_add_string (out
, buffer
);
872 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
874 /* This is a preprocessor variable name, we don't do them
876 sb_add_char (out
, '\\');
877 sb_add_char (out
, '&');
880 else if (flag_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
885 if (ISDIGIT (in
->ptr
[src
]))
886 ind
= in
->ptr
[src
] - '0';
887 else if (ISUPPER (in
->ptr
[src
]))
888 ind
= in
->ptr
[src
] - 'A' + 10;
890 ind
= in
->ptr
[src
] - 'a' + 10;
892 for (f
= formals
; f
!= NULL
; f
= f
->next
)
894 if (f
->index
== ind
- 1)
896 if (f
->actual
.len
!= 0)
897 sb_add_sb (out
, &f
->actual
);
899 sb_add_sb (out
, &f
->def
);
907 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
910 else if ((flag_macro_alternate
|| flag_mri
)
911 && is_name_beginner (in
->ptr
[src
])
914 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
917 || src
+ 5 >= in
->len
918 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
919 || ! ISWHITE (in
->ptr
[src
+ 5])
920 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
924 src
= sub_actual (src
, in
, &t
, formal_hash
,
925 (macro_strip_at
&& inquote
) ? '@' : '\'',
930 src
= sb_skip_white (src
+ 5, in
);
931 while (in
->ptr
[src
] != '\n')
934 formal_entry
*f
= new_formal ();
936 src
= get_token (src
, in
, &f
->name
);
937 name
= sb_terminate (&f
->name
);
938 if (str_hash_insert (formal_hash
, name
, f
, 0) != NULL
)
940 as_bad_where (macro
->file
, macro
->line
+ macro_line
,
941 _("`%s' was already used as parameter "
942 "(or another local) name"), name
);
950 f
->index
= LOCAL_INDEX
;
954 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
955 sb_add_string (&f
->actual
, buf
);
958 src
= sb_skip_comma (src
, in
);
962 else if (in
->ptr
[src
] == '"'
963 || (flag_mri
&& in
->ptr
[src
] == '\''))
966 sb_add_char (out
, in
->ptr
[src
++]);
968 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
972 && in
->ptr
[src
] == '@')
974 sb_add_char (out
, '@');
979 && in
->ptr
[src
] == '='
981 && in
->ptr
[src
+ 1] == '=')
986 src
= get_token (src
+ 2, in
, &t
);
987 ptr
= str_hash_find (formal_hash
, sb_terminate (&t
));
990 /* FIXME: We should really return a warning string here,
991 but we can't, because the == might be in the MRI
992 comment field, and, since the nature of the MRI
993 comment field depends upon the exact instruction
994 being used, we don't have enough information here to
995 figure out whether it is or not. Instead, we leave
996 the == in place, which should cause a syntax error if
997 it is not in a comment. */
998 sb_add_char (out
, '=');
999 sb_add_char (out
, '=');
1000 sb_add_sb (out
, &t
);
1004 if (ptr
->actual
.len
)
1006 sb_add_string (out
, "-1");
1010 sb_add_char (out
, '0');
1016 if (in
->ptr
[src
] == '\n')
1018 sb_add_char (out
, in
->ptr
[src
++]);
1024 while (loclist
!= NULL
)
1030 name
= sb_terminate (&loclist
->name
);
1031 str_hash_delete (formal_hash
, name
);
1032 del_formal (loclist
);
1036 if (!err
&& (out
->len
== 0 || out
->ptr
[out
->len
- 1] != '\n'))
1037 sb_add_char (out
, '\n');
1041 /* Assign values to the formal parameters of a macro, and expand the
1045 macro_expand (size_t idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1052 const char *err
= NULL
;
1056 /* Reset any old value the actuals may have. */
1057 for (f
= m
->formals
; f
; f
= f
->next
)
1058 sb_reset (&f
->actual
);
1060 while (f
!= NULL
&& f
->index
< 0)
1065 /* The macro may be called with an optional qualifier, which may
1066 be referred to in the macro body as \0. */
1067 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1069 /* The Microtec assembler ignores this if followed by a white space.
1070 (Macro invocation with empty extension) */
1073 && in
->ptr
[idx
] != ' '
1074 && in
->ptr
[idx
] != '\t')
1076 formal_entry
*n
= new_formal ();
1078 n
->index
= QUAL_INDEX
;
1080 n
->next
= m
->formals
;
1083 idx
= get_any_string (idx
, in
, &n
->actual
);
1088 /* Peel off the actuals and store them away in the hash tables' actuals. */
1089 idx
= sb_skip_white (idx
, in
);
1090 while (idx
< in
->len
)
1094 /* Look and see if it's a positional or keyword arg. */
1096 while (scan
< in
->len
1097 && !ISSEP (in
->ptr
[scan
])
1098 && !(flag_mri
&& in
->ptr
[scan
] == '\'')
1099 && (!flag_macro_alternate
&& in
->ptr
[scan
] != '='))
1101 if (scan
< in
->len
&& !flag_macro_alternate
&& in
->ptr
[scan
] == '=')
1105 /* It's OK to go from positional to keyword. */
1107 /* This is a keyword arg, fetch the formal name and
1108 then the actual stuff. */
1110 idx
= get_token (idx
, in
, &t
);
1111 if (idx
>= in
->len
|| in
->ptr
[idx
] != '=')
1113 err
= _("confusion in formal parameters");
1117 /* Lookup the formal in the macro's list. */
1118 ptr
= str_hash_find (m
->formal_hash
, sb_terminate (&t
));
1121 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1125 idx
= get_any_string (idx
+ 1, in
, &t
);
1129 /* Insert this value into the right place. */
1130 if (ptr
->actual
.len
)
1132 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1135 sb_reset (&ptr
->actual
);
1137 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1138 if (ptr
->actual
.len
> 0)
1146 err
= _("can't mix positional and keyword arguments");
1157 err
= _("too many positional arguments");
1164 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1165 if ((*pf
)->index
>= c
)
1166 c
= (*pf
)->index
+ 1;
1173 if (f
->type
!= FORMAL_VARARG
)
1174 idx
= get_any_string (idx
, in
, &f
->actual
);
1175 else if (idx
< in
->len
)
1177 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1180 if (f
->actual
.len
> 0)
1186 while (f
!= NULL
&& f
->index
< 0);
1190 idx
= sb_skip_comma (idx
, in
);
1193 if (idx
< in
->len
&& in
->ptr
[idx
] == ',')
1195 if (idx
< in
->len
&& ISWHITE (in
->ptr
[idx
]))
1202 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1204 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1205 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1212 ptr
= str_hash_find (m
->formal_hash
,
1213 macro_strip_at
? "$NARG" : "NARG");
1217 sprintf (buffer
, "%d", narg
);
1218 sb_add_string (&ptr
->actual
, buffer
);
1222 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
,
1226 /* Discard any unnamed formal arguments. */
1234 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 && (! flag_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
);
1321 str_hash_delete (macro_hash
, copy
);
1323 as_warn (_("Attempt to purge non-existing macro `%s'"), copy
);
1327 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1328 combined macro definition and execution. This returns NULL on
1329 success, or an error message otherwise. */
1332 expand_irp (int irpc
, size_t idx
, sb
*in
, sb
*out
, size_t (*get_line
) (sb
*))
1337 const char *err
= NULL
;
1339 idx
= sb_skip_white (idx
, in
);
1342 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1344 err
= _("unexpected end of file in irp or irpc");
1352 idx
= get_token (idx
, in
, &f
.name
);
1353 if (f
.name
.len
== 0)
1355 err
= _("missing model parameter");
1359 h
= str_htab_create ();
1361 str_hash_insert (h
, sb_terminate (&f
.name
), &f
, 0);
1365 f
.type
= FORMAL_OPTIONAL
;
1369 idx
= sb_skip_comma (idx
, in
);
1372 /* Expand once with a null string. */
1373 err
= macro_expand_body (&sub
, out
, &f
, h
, NULL
, 0);
1377 bool in_quotes
= false;
1378 unsigned int instance
= 0;
1380 while (idx
< in
->len
)
1383 idx
= get_any_string (idx
, in
, &f
.actual
);
1386 if (in
->ptr
[idx
] == '"')
1388 in_quotes
= ! in_quotes
;
1393 idx
= sb_skip_white (idx
, in
);
1399 sb_reset (&f
.actual
);
1400 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1404 err
= macro_expand_body (&sub
, out
, &f
, h
, NULL
, instance
);
1409 idx
= sb_skip_comma (idx
, in
);
1410 else if (! in_quotes
)
1411 idx
= sb_skip_white (idx
, in
);
1417 sb_kill (&f
.actual
);