1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2019 Free Software Foundation, Inc.
4 Written by Steve and Judy Chamberlain of Cygnus Support,
7 This file is part of GAS, the GNU Assembler.
9 GAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GAS; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "safe-ctype.h"
29 /* The routines in this file handle macro definition and expansion.
30 They are called by gas. */
32 #define ISWHITE(x) ((x) == ' ' || (x) == '\t')
35 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
36 || (x) == ')' || (x) == '(' \
37 || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
40 ((x) == 'b' || (x) == 'B' \
41 || (x) == 'q' || (x) == 'Q' \
42 || (x) == 'h' || (x) == 'H' \
43 || (x) == 'd' || (x) == 'D')
45 /* The macro hash table. */
47 struct hash_control
*macro_hash
;
49 /* Whether any macros have been defined. */
53 /* Whether we are in alternate syntax mode. */
55 static int macro_alternate
;
57 /* Whether we are in MRI mode. */
61 /* Whether we should strip '@' characters. */
63 static int macro_strip_at
;
65 /* Function to use to parse an expression. */
67 static size_t (*macro_expr
) (const char *, size_t, sb
*, offsetT
*);
69 /* Number of macro expansions that have been done. */
71 static int macro_number
;
73 /* Initialize macro processing. */
76 macro_init (int alternate
, int mri
, int strip_at
,
77 size_t (*exp
) (const char *, size_t, sb
*, offsetT
*))
79 macro_hash
= hash_new ();
81 macro_alternate
= alternate
;
83 macro_strip_at
= strip_at
;
87 /* Switch in and out of alternate mode on the fly. */
90 macro_set_alternate (int alternate
)
92 macro_alternate
= alternate
;
95 /* Switch in and out of MRI mode on the fly. */
98 macro_mri_mode (int mri
)
103 /* Read input lines till we get to a TO string.
104 Increase nesting depth if we get a FROM string.
105 Put the results into sb at PTR.
106 FROM may be NULL (or will be ignored) if TO is "ENDR".
107 Add a new input line to an sb using GET_LINE.
108 Return 1 on success, 0 on unexpected EOF. */
111 buffer_and_nest (const char *from
, const char *to
, sb
*ptr
,
112 size_t (*get_line
) (sb
*))
115 size_t to_len
= strlen (to
);
117 size_t line_start
= ptr
->len
;
118 size_t more
= get_line (ptr
);
120 if (to_len
== 4 && strcasecmp (to
, "ENDR") == 0)
126 from_len
= strlen (from
);
130 /* Try to find the first pseudo op on the line. */
131 size_t i
= line_start
;
132 bfd_boolean 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
] == '.')
187 && strncasecmp (ptr
->ptr
+ i
, "IRPC", from_len
= 4) != 0
188 && strncasecmp (ptr
->ptr
+ i
, "IRP", from_len
= 3) != 0
189 && strncasecmp (ptr
->ptr
+ i
, "IREPC", from_len
= 5) != 0
190 && strncasecmp (ptr
->ptr
+ i
, "IREP", from_len
= 4) != 0
191 && strncasecmp (ptr
->ptr
+ i
, "REPT", from_len
= 4) != 0
192 && strncasecmp (ptr
->ptr
+ i
, "REP", from_len
= 3) != 0)
195 ? strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0
197 && (ptr
->len
== (i
+ from_len
)
198 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
199 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
201 if (strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
202 && (ptr
->len
== (i
+ to_len
)
203 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
204 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
209 /* Reset the string to not include the ending rune. */
210 ptr
->len
= line_start
;
216 Apply and discard .linefile directives that appear within
217 the macro. For long macros, one might want to report the
218 line number information associated with the lines within
219 the macro definition, but we would need more infrastructure
220 to make that happen correctly (e.g. resetting the line
221 number when expanding the macro), and since for short
222 macros we clearly prefer reporting the point of expansion
223 anyway, there's not an obviously better fix here. */
224 if (strncasecmp (ptr
->ptr
+ i
, "linefile", 8) == 0)
226 char saved_eol_char
= ptr
->ptr
[ptr
->len
];
228 ptr
->ptr
[ptr
->len
] = '\0';
229 temp_ilp (ptr
->ptr
+ i
+ 8);
232 ptr
->ptr
[ptr
->len
] = saved_eol_char
;
233 ptr
->len
= line_start
;
237 /* Add the original end-of-line char to the end and keep running. */
238 sb_add_char (ptr
, more
);
239 line_start
= ptr
->len
;
240 more
= get_line (ptr
);
243 /* Return 1 on success, 0 on unexpected EOF. */
247 /* Pick up a token. */
250 get_token (size_t idx
, sb
*in
, sb
*name
)
253 && is_name_beginner (in
->ptr
[idx
]))
255 sb_add_char (name
, in
->ptr
[idx
++]);
257 && is_part_of_name (in
->ptr
[idx
]))
259 sb_add_char (name
, in
->ptr
[idx
++]);
262 && is_name_ender (in
->ptr
[idx
]))
264 sb_add_char (name
, in
->ptr
[idx
++]);
267 /* Ignore trailing &. */
268 if (macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
273 /* Pick up a string. */
276 getstring (size_t idx
, sb
*in
, sb
*acc
)
279 && (in
->ptr
[idx
] == '"'
280 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
281 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
283 if (in
->ptr
[idx
] == '<')
288 && (in
->ptr
[idx
] != '>' || nest
))
290 if (in
->ptr
[idx
] == '!')
293 sb_add_char (acc
, in
->ptr
[idx
++]);
297 if (in
->ptr
[idx
] == '>')
299 if (in
->ptr
[idx
] == '<')
301 sb_add_char (acc
, in
->ptr
[idx
++]);
306 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
308 char tchar
= in
->ptr
[idx
];
313 while (idx
< in
->len
)
315 if (in
->ptr
[idx
- 1] == '\\')
320 if (macro_alternate
&& in
->ptr
[idx
] == '!')
324 sb_add_char (acc
, in
->ptr
[idx
]);
328 else if (escaped
&& in
->ptr
[idx
] == tchar
)
330 sb_add_char (acc
, tchar
);
335 if (in
->ptr
[idx
] == tchar
)
339 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
343 sb_add_char (acc
, in
->ptr
[idx
]);
353 /* Fetch string from the input stream,
355 'Bxyx<whitespace> -> return 'Bxyza
356 %<expr> -> return string of decimal value of <expr>
357 "string" -> return string
358 (string) -> return (string-including-whitespaces)
359 xyx<whitespace> -> return xyz. */
362 get_any_string (size_t idx
, sb
*in
, sb
*out
)
365 idx
= sb_skip_white (idx
, in
);
369 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
371 while (idx
< in
->len
&& !ISSEP (in
->ptr
[idx
]))
372 sb_add_char (out
, in
->ptr
[idx
++]);
374 else if (in
->ptr
[idx
] == '%' && macro_alternate
)
379 /* Turns the next expression into a string. */
380 /* xgettext: no-c-format */
381 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
385 sprintf (buf
, "%" BFD_VMA_FMT
"d", val
);
386 sb_add_string (out
, buf
);
388 else if (in
->ptr
[idx
] == '"'
389 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
390 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
392 if (macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
394 /* Keep the quotes. */
395 sb_add_char (out
, '"');
396 idx
= getstring (idx
, in
, out
);
397 sb_add_char (out
, '"');
401 idx
= getstring (idx
, in
, out
);
406 char *br_buf
= XNEWVEC (char, 1);
407 char *in_br
= br_buf
;
412 || (in
->ptr
[idx
] != ' '
413 && in
->ptr
[idx
] != '\t'))
414 && in
->ptr
[idx
] != ','
415 && (in
->ptr
[idx
] != '<'
416 || (! macro_alternate
&& ! macro_mri
)))
418 char tchar
= in
->ptr
[idx
];
424 sb_add_char (out
, in
->ptr
[idx
++]);
426 && in
->ptr
[idx
] != tchar
)
427 sb_add_char (out
, in
->ptr
[idx
++]);
440 br_buf
= XNEWVEC (char, strlen (in_br
) + 2);
441 strcpy (br_buf
+ 1, in_br
);
456 sb_add_char (out
, tchar
);
466 /* Allocate a new formal. */
468 static formal_entry
*
471 formal_entry
*formal
;
473 formal
= XNEW (formal_entry
);
475 sb_new (&formal
->name
);
476 sb_new (&formal
->def
);
477 sb_new (&formal
->actual
);
479 formal
->type
= FORMAL_OPTIONAL
;
486 del_formal (formal_entry
*formal
)
488 sb_kill (&formal
->actual
);
489 sb_kill (&formal
->def
);
490 sb_kill (&formal
->name
);
494 /* Pick up the formal parameters of a macro definition. */
497 do_formals (macro_entry
*macro
, size_t idx
, sb
*in
)
499 formal_entry
**p
= ¯o
->formals
;
502 idx
= sb_skip_white (idx
, in
);
503 while (idx
< in
->len
)
505 formal_entry
*formal
= new_formal ();
508 idx
= get_token (idx
, in
, &formal
->name
);
509 if (formal
->name
.len
== 0)
511 if (macro
->formal_count
)
513 del_formal (formal
); /* 'formal' goes out of scope. */
516 idx
= sb_skip_white (idx
, in
);
517 /* This is a formal. */
518 name
= sb_terminate (&formal
->name
);
521 && in
->ptr
[idx
] == ':'
522 && (! is_name_beginner (':')
523 || idx
+ 1 >= in
->len
524 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
526 /* Got a qualifier. */
530 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
531 sb_terminate (&qual
);
533 as_bad_where (macro
->file
,
535 _("Missing parameter qualifier for `%s' in macro `%s'"),
538 else if (strcmp (qual
.ptr
, "req") == 0)
539 formal
->type
= FORMAL_REQUIRED
;
540 else if (strcmp (qual
.ptr
, "vararg") == 0)
541 formal
->type
= FORMAL_VARARG
;
543 as_bad_where (macro
->file
,
545 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
550 idx
= sb_skip_white (idx
, in
);
552 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
555 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
556 idx
= sb_skip_white (idx
, in
);
557 if (formal
->type
== FORMAL_REQUIRED
)
559 sb_reset (&formal
->def
);
560 as_warn_where (macro
->file
,
562 _("Pointless default value for required parameter `%s' in macro `%s'"),
568 /* Add to macro's hash table. */
569 if (! hash_find (macro
->formal_hash
, name
))
570 hash_jam (macro
->formal_hash
, name
, formal
);
572 as_bad_where (macro
->file
,
574 _("A parameter named `%s' already exists for macro `%s'"),
578 formal
->index
= macro
->formal_count
++;
581 if (formal
->type
== FORMAL_VARARG
)
584 idx
= sb_skip_comma (idx
, in
);
585 if (idx
!= cidx
&& idx
>= in
->len
)
594 formal_entry
*formal
= new_formal ();
596 /* Add a special NARG formal, which macro_expand will set to the
597 number of arguments. */
598 /* The same MRI assemblers which treat '@' characters also use
599 the name $NARG. At least until we find an exception. */
605 sb_add_string (&formal
->name
, name
);
607 /* Add to macro's hash table. */
608 if (hash_find (macro
->formal_hash
, name
))
609 as_bad_where (macro
->file
,
611 _("Reserved word `%s' used as parameter in macro `%s'"),
614 hash_jam (macro
->formal_hash
, name
, formal
);
616 formal
->index
= NARG_INDEX
;
623 /* Free the memory allocated to a macro. */
626 free_macro (macro_entry
*macro
)
628 formal_entry
*formal
;
630 for (formal
= macro
->formals
; formal
; )
635 formal
= formal
->next
;
638 hash_die (macro
->formal_hash
);
639 sb_kill (¯o
->sub
);
643 /* Define a new macro. Returns NULL on success, otherwise returns an
644 error message. If NAMEP is not NULL, *NAMEP is set to the name of
645 the macro which was defined. */
648 define_macro (size_t idx
, sb
*in
, sb
*label
,
649 size_t (*get_line
) (sb
*),
650 const char *file
, unsigned int line
,
655 const char *error
= NULL
;
657 macro
= XNEW (macro_entry
);
658 sb_new (¯o
->sub
);
663 macro
->formal_count
= 0;
665 macro
->formal_hash
= hash_new_sized (7);
667 idx
= sb_skip_white (idx
, in
);
668 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
669 error
= _("unexpected end of file in macro `%s' definition");
670 if (label
!= NULL
&& label
->len
!= 0)
672 sb_add_sb (&name
, label
);
673 macro
->name
= sb_terminate (&name
);
674 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
676 /* It's the label: MACRO (formals,...) sort */
677 idx
= do_formals (macro
, idx
+ 1, in
);
678 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
679 idx
= sb_skip_white (idx
+ 1, in
);
681 error
= _("missing `)' after formals in macro definition `%s'");
685 /* It's the label: MACRO formals,... sort */
686 idx
= do_formals (macro
, idx
, in
);
693 idx
= get_token (idx
, in
, &name
);
694 macro
->name
= sb_terminate (&name
);
696 error
= _("Missing macro name");
697 cidx
= sb_skip_white (idx
, in
);
698 idx
= sb_skip_comma (cidx
, in
);
699 if (idx
== cidx
|| idx
< in
->len
)
700 idx
= do_formals (macro
, idx
, in
);
704 if (!error
&& idx
< in
->len
)
705 error
= _("Bad parameter list for macro `%s'");
707 /* And stick it in the macro hash table. */
708 for (idx
= 0; idx
< name
.len
; idx
++)
709 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
710 if (hash_find (macro_hash
, macro
->name
))
711 error
= _("Macro `%s' was already defined");
713 error
= hash_jam (macro_hash
, macro
->name
, (void *) macro
);
716 *namep
= macro
->name
;
726 /* Scan a token, and then skip KIND. */
729 get_apost_token (size_t idx
, sb
*in
, sb
*name
, int kind
)
731 idx
= get_token (idx
, in
, name
);
733 && in
->ptr
[idx
] == kind
734 && (! macro_mri
|| macro_strip_at
)
735 && (! macro_strip_at
|| kind
== '@'))
740 /* Substitute the actual value for a formal parameter. */
743 sub_actual (size_t start
, sb
*in
, sb
*t
, struct hash_control
*formal_hash
,
744 int kind
, sb
*out
, int copyifnotthere
)
749 src
= get_apost_token (start
, in
, t
, kind
);
750 /* See if it's in the macro's hash table, unless this is
751 macro_strip_at and kind is '@' and the token did not end in '@'. */
754 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
757 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (t
));
762 sb_add_sb (out
, &ptr
->actual
);
766 sb_add_sb (out
, &ptr
->def
);
769 else if (kind
== '&')
771 /* Doing this permits people to use & in macro bodies. */
772 sb_add_char (out
, '&');
774 if (src
!= start
&& in
->ptr
[src
- 1] == '&')
775 sb_add_char (out
, '&');
777 else if (copyifnotthere
)
783 sb_add_char (out
, '\\');
789 /* Expand the body of a macro. */
792 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
793 struct hash_control
*formal_hash
, const macro_entry
*macro
)
797 int inquote
= 0, macro_line
= 0;
798 formal_entry
*loclist
= NULL
;
799 const char *err
= NULL
;
803 while (src
< in
->len
&& !err
)
805 if (in
->ptr
[src
] == '&')
810 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
811 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
813 sb_add_char (out
, in
->ptr
[src
++]);
817 /* Permit macro parameter substitution delineated with
818 an '&' prefix and optional '&' suffix. */
819 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
822 else if (in
->ptr
[src
] == '\\')
825 if (src
< in
->len
&& in
->ptr
[src
] == '(')
827 /* Sub in till the next ')' literally. */
829 while (src
< in
->len
&& in
->ptr
[src
] != ')')
831 sb_add_char (out
, in
->ptr
[src
++]);
836 err
= _("missing `)'");
838 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
840 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
842 /* Sub in the macro invocation number. */
846 sprintf (buffer
, "%d", macro_number
);
847 sb_add_string (out
, buffer
);
849 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
851 /* This is a preprocessor variable name, we don't do them
853 sb_add_char (out
, '\\');
854 sb_add_char (out
, '&');
857 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
862 if (ISDIGIT (in
->ptr
[src
]))
863 ind
= in
->ptr
[src
] - '0';
864 else if (ISUPPER (in
->ptr
[src
]))
865 ind
= in
->ptr
[src
] - 'A' + 10;
867 ind
= in
->ptr
[src
] - 'a' + 10;
869 for (f
= formals
; f
!= NULL
; f
= f
->next
)
871 if (f
->index
== ind
- 1)
873 if (f
->actual
.len
!= 0)
874 sb_add_sb (out
, &f
->actual
);
876 sb_add_sb (out
, &f
->def
);
884 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
887 else if ((macro_alternate
|| macro_mri
)
888 && is_name_beginner (in
->ptr
[src
])
891 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
894 || src
+ 5 >= in
->len
895 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
896 || ! ISWHITE (in
->ptr
[src
+ 5])
897 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
901 src
= sub_actual (src
, in
, &t
, formal_hash
,
902 (macro_strip_at
&& inquote
) ? '@' : '\'',
907 src
= sb_skip_white (src
+ 5, in
);
908 while (in
->ptr
[src
] != '\n')
911 formal_entry
*f
= new_formal ();
913 src
= get_token (src
, in
, &f
->name
);
914 name
= sb_terminate (&f
->name
);
915 if (! hash_find (formal_hash
, name
))
920 f
->index
= LOCAL_INDEX
;
924 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
925 sb_add_string (&f
->actual
, buf
);
927 err
= hash_jam (formal_hash
, name
, f
);
933 as_bad_where (macro
->file
,
934 macro
->line
+ macro_line
,
935 _("`%s' was already used as parameter (or another local) name"),
940 src
= sb_skip_comma (src
, in
);
944 else if (in
->ptr
[src
] == '"'
945 || (macro_mri
&& in
->ptr
[src
] == '\''))
948 sb_add_char (out
, in
->ptr
[src
++]);
950 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
954 && in
->ptr
[src
] == '@')
956 sb_add_char (out
, '@');
961 && in
->ptr
[src
] == '='
963 && in
->ptr
[src
+ 1] == '=')
968 src
= get_token (src
+ 2, in
, &t
);
969 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (&t
));
972 /* FIXME: We should really return a warning string here,
973 but we can't, because the == might be in the MRI
974 comment field, and, since the nature of the MRI
975 comment field depends upon the exact instruction
976 being used, we don't have enough information here to
977 figure out whether it is or not. Instead, we leave
978 the == in place, which should cause a syntax error if
979 it is not in a comment. */
980 sb_add_char (out
, '=');
981 sb_add_char (out
, '=');
988 sb_add_string (out
, "-1");
992 sb_add_char (out
, '0');
998 if (in
->ptr
[src
] == '\n')
1000 sb_add_char (out
, in
->ptr
[src
++]);
1006 while (loclist
!= NULL
)
1012 name
= sb_terminate (&loclist
->name
);
1013 hash_delete (formal_hash
, name
, f
== NULL
);
1014 del_formal (loclist
);
1021 /* Assign values to the formal parameters of a macro, and expand the
1025 macro_expand (size_t idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1032 const char *err
= NULL
;
1036 /* Reset any old value the actuals may have. */
1037 for (f
= m
->formals
; f
; f
= f
->next
)
1038 sb_reset (&f
->actual
);
1040 while (f
!= NULL
&& f
->index
< 0)
1045 /* The macro may be called with an optional qualifier, which may
1046 be referred to in the macro body as \0. */
1047 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1049 /* The Microtec assembler ignores this if followed by a white space.
1050 (Macro invocation with empty extension) */
1053 && in
->ptr
[idx
] != ' '
1054 && in
->ptr
[idx
] != '\t')
1056 formal_entry
*n
= new_formal ();
1058 n
->index
= QUAL_INDEX
;
1060 n
->next
= m
->formals
;
1063 idx
= get_any_string (idx
, in
, &n
->actual
);
1068 /* Peel off the actuals and store them away in the hash tables' actuals. */
1069 idx
= sb_skip_white (idx
, in
);
1070 while (idx
< in
->len
)
1074 /* Look and see if it's a positional or keyword arg. */
1076 while (scan
< in
->len
1077 && !ISSEP (in
->ptr
[scan
])
1078 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1079 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1081 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1085 /* It's OK to go from positional to keyword. */
1087 /* This is a keyword arg, fetch the formal name and
1088 then the actual stuff. */
1090 idx
= get_token (idx
, in
, &t
);
1091 if (in
->ptr
[idx
] != '=')
1093 err
= _("confusion in formal parameters");
1097 /* Lookup the formal in the macro's list. */
1098 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1101 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1105 idx
= get_any_string (idx
+ 1, in
, &t
);
1109 /* Insert this value into the right place. */
1110 if (ptr
->actual
.len
)
1112 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1115 sb_reset (&ptr
->actual
);
1117 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1118 if (ptr
->actual
.len
> 0)
1126 err
= _("can't mix positional and keyword arguments");
1137 err
= _("too many positional arguments");
1144 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1145 if ((*pf
)->index
>= c
)
1146 c
= (*pf
)->index
+ 1;
1153 if (f
->type
!= FORMAL_VARARG
)
1154 idx
= get_any_string (idx
, in
, &f
->actual
);
1157 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1160 if (f
->actual
.len
> 0)
1166 while (f
!= NULL
&& f
->index
< 0);
1170 idx
= sb_skip_comma (idx
, in
);
1173 if (in
->ptr
[idx
] == ',')
1175 if (ISWHITE (in
->ptr
[idx
]))
1182 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1184 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1185 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1195 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1196 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1197 sprintf (buffer
, "%d", narg
);
1198 sb_add_string (&ptr
->actual
, buffer
);
1201 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1204 /* Discard any unnamed formal arguments. */
1212 if ((*pf
)->name
.len
!= 0)
1230 /* Check for a macro. If one is found, put the expansion into
1231 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1234 check_macro (const char *line
, sb
*expand
,
1235 const char **error
, macro_entry
**info
)
1242 if (! is_name_beginner (*line
)
1243 && (! macro_mri
|| *line
!= '.'))
1247 while (is_part_of_name (*s
))
1249 if (is_name_ender (*s
))
1252 copy
= xmemdup0 (line
, s
- line
);
1253 for (cls
= copy
; *cls
!= '\0'; cls
++)
1254 *cls
= TOLOWER (*cls
);
1256 macro
= (macro_entry
*) hash_find (macro_hash
, copy
);
1262 /* Wrap the line up in an sb. */
1264 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1265 sb_add_char (&line_sb
, *s
++);
1268 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1272 /* Export the macro information if requested. */
1279 /* Delete a macro. */
1282 delete_macro (const char *name
)
1288 len
= strlen (name
);
1289 copy
= XNEWVEC (char, len
+ 1);
1290 for (i
= 0; i
< len
; ++i
)
1291 copy
[i
] = TOLOWER (name
[i
]);
1294 /* We can only ask hash_delete to free memory if we are deleting
1295 macros in reverse order to their definition.
1296 So just clear out the entry. */
1297 if ((macro
= (macro_entry
*) hash_find (macro_hash
, copy
)) != NULL
)
1299 hash_jam (macro_hash
, copy
, NULL
);
1303 as_warn (_("Attempt to purge non-existing macro `%s'"), copy
);
1307 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1308 combined macro definition and execution. This returns NULL on
1309 success, or an error message otherwise. */
1312 expand_irp (int irpc
, size_t idx
, sb
*in
, sb
*out
, size_t (*get_line
) (sb
*))
1316 struct hash_control
*h
;
1319 idx
= sb_skip_white (idx
, in
);
1322 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1323 return _("unexpected end of file in irp or irpc");
1329 idx
= get_token (idx
, in
, &f
.name
);
1330 if (f
.name
.len
== 0)
1331 return _("missing model parameter");
1334 err
= hash_jam (h
, sb_terminate (&f
.name
), &f
);
1340 f
.type
= FORMAL_OPTIONAL
;
1344 idx
= sb_skip_comma (idx
, in
);
1347 /* Expand once with a null string. */
1348 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1352 bfd_boolean in_quotes
= FALSE
;
1354 if (irpc
&& in
->ptr
[idx
] == '"')
1360 while (idx
< in
->len
)
1363 idx
= get_any_string (idx
, in
, &f
.actual
);
1366 if (in
->ptr
[idx
] == '"')
1371 in_quotes
= ! in_quotes
;
1373 nxt
= sb_skip_white (idx
+ 1, in
);
1380 sb_reset (&f
.actual
);
1381 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1385 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1389 idx
= sb_skip_comma (idx
, in
);
1390 else if (! in_quotes
)
1391 idx
= sb_skip_white (idx
, in
);
1396 sb_kill (&f
.actual
);