1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2018 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_input_line_pointer
= input_line_pointer
;
227 char saved_eol_char
= ptr
->ptr
[ptr
->len
];
229 ptr
->ptr
[ptr
->len
] = '\0';
230 input_line_pointer
= ptr
->ptr
+ i
+ 8;
232 ptr
->ptr
[ptr
->len
] = saved_eol_char
;
233 input_line_pointer
= saved_input_line_pointer
;
234 ptr
->len
= line_start
;
238 /* Add the original end-of-line char to the end and keep running. */
239 sb_add_char (ptr
, more
);
240 line_start
= ptr
->len
;
241 more
= get_line (ptr
);
244 /* Return 1 on success, 0 on unexpected EOF. */
248 /* Pick up a token. */
251 get_token (size_t idx
, sb
*in
, sb
*name
)
254 && is_name_beginner (in
->ptr
[idx
]))
256 sb_add_char (name
, in
->ptr
[idx
++]);
258 && is_part_of_name (in
->ptr
[idx
]))
260 sb_add_char (name
, in
->ptr
[idx
++]);
263 && is_name_ender (in
->ptr
[idx
]))
265 sb_add_char (name
, in
->ptr
[idx
++]);
268 /* Ignore trailing &. */
269 if (macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
274 /* Pick up a string. */
277 getstring (size_t idx
, sb
*in
, sb
*acc
)
280 && (in
->ptr
[idx
] == '"'
281 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
282 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
284 if (in
->ptr
[idx
] == '<')
288 while ((in
->ptr
[idx
] != '>' || nest
)
291 if (in
->ptr
[idx
] == '!')
294 sb_add_char (acc
, in
->ptr
[idx
++]);
298 if (in
->ptr
[idx
] == '>')
300 if (in
->ptr
[idx
] == '<')
302 sb_add_char (acc
, in
->ptr
[idx
++]);
307 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
309 char tchar
= in
->ptr
[idx
];
314 while (idx
< in
->len
)
316 if (in
->ptr
[idx
- 1] == '\\')
321 if (macro_alternate
&& in
->ptr
[idx
] == '!')
325 sb_add_char (acc
, in
->ptr
[idx
]);
329 else if (escaped
&& in
->ptr
[idx
] == tchar
)
331 sb_add_char (acc
, tchar
);
336 if (in
->ptr
[idx
] == tchar
)
340 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
344 sb_add_char (acc
, in
->ptr
[idx
]);
354 /* Fetch string from the input stream,
356 'Bxyx<whitespace> -> return 'Bxyza
357 %<expr> -> return string of decimal value of <expr>
358 "string" -> return string
359 (string) -> return (string-including-whitespaces)
360 xyx<whitespace> -> return xyz. */
363 get_any_string (size_t idx
, sb
*in
, sb
*out
)
366 idx
= sb_skip_white (idx
, in
);
370 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
372 while (!ISSEP (in
->ptr
[idx
]))
373 sb_add_char (out
, in
->ptr
[idx
++]);
375 else if (in
->ptr
[idx
] == '%' && macro_alternate
)
380 /* Turns the next expression into a string. */
381 /* xgettext: no-c-format */
382 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
386 sprintf (buf
, "%" BFD_VMA_FMT
"d", val
);
387 sb_add_string (out
, buf
);
389 else if (in
->ptr
[idx
] == '"'
390 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
391 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
393 if (macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
395 /* Keep the quotes. */
396 sb_add_char (out
, '"');
397 idx
= getstring (idx
, in
, out
);
398 sb_add_char (out
, '"');
402 idx
= getstring (idx
, in
, out
);
407 char *br_buf
= XNEWVEC (char, 1);
408 char *in_br
= br_buf
;
413 || (in
->ptr
[idx
] != ' '
414 && in
->ptr
[idx
] != '\t'))
415 && in
->ptr
[idx
] != ','
416 && (in
->ptr
[idx
] != '<'
417 || (! macro_alternate
&& ! macro_mri
)))
419 char tchar
= in
->ptr
[idx
];
425 sb_add_char (out
, in
->ptr
[idx
++]);
427 && in
->ptr
[idx
] != tchar
)
428 sb_add_char (out
, in
->ptr
[idx
++]);
441 br_buf
= XNEWVEC (char, strlen (in_br
) + 2);
442 strcpy (br_buf
+ 1, in_br
);
457 sb_add_char (out
, tchar
);
467 /* Allocate a new formal. */
469 static formal_entry
*
472 formal_entry
*formal
;
474 formal
= XNEW (formal_entry
);
476 sb_new (&formal
->name
);
477 sb_new (&formal
->def
);
478 sb_new (&formal
->actual
);
480 formal
->type
= FORMAL_OPTIONAL
;
487 del_formal (formal_entry
*formal
)
489 sb_kill (&formal
->actual
);
490 sb_kill (&formal
->def
);
491 sb_kill (&formal
->name
);
495 /* Pick up the formal parameters of a macro definition. */
498 do_formals (macro_entry
*macro
, size_t idx
, sb
*in
)
500 formal_entry
**p
= ¯o
->formals
;
503 idx
= sb_skip_white (idx
, in
);
504 while (idx
< in
->len
)
506 formal_entry
*formal
= new_formal ();
509 idx
= get_token (idx
, in
, &formal
->name
);
510 if (formal
->name
.len
== 0)
512 if (macro
->formal_count
)
514 del_formal (formal
); /* 'formal' goes out of scope. */
517 idx
= sb_skip_white (idx
, in
);
518 /* This is a formal. */
519 name
= sb_terminate (&formal
->name
);
522 && in
->ptr
[idx
] == ':'
523 && (! is_name_beginner (':')
524 || idx
+ 1 >= in
->len
525 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
527 /* Got a qualifier. */
531 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
532 sb_terminate (&qual
);
534 as_bad_where (macro
->file
,
536 _("Missing parameter qualifier for `%s' in macro `%s'"),
539 else if (strcmp (qual
.ptr
, "req") == 0)
540 formal
->type
= FORMAL_REQUIRED
;
541 else if (strcmp (qual
.ptr
, "vararg") == 0)
542 formal
->type
= FORMAL_VARARG
;
544 as_bad_where (macro
->file
,
546 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
551 idx
= sb_skip_white (idx
, in
);
553 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
556 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
557 idx
= sb_skip_white (idx
, in
);
558 if (formal
->type
== FORMAL_REQUIRED
)
560 sb_reset (&formal
->def
);
561 as_warn_where (macro
->file
,
563 _("Pointless default value for required parameter `%s' in macro `%s'"),
569 /* Add to macro's hash table. */
570 if (! hash_find (macro
->formal_hash
, name
))
571 hash_jam (macro
->formal_hash
, name
, formal
);
573 as_bad_where (macro
->file
,
575 _("A parameter named `%s' already exists for macro `%s'"),
579 formal
->index
= macro
->formal_count
++;
582 if (formal
->type
== FORMAL_VARARG
)
585 idx
= sb_skip_comma (idx
, in
);
586 if (idx
!= cidx
&& idx
>= in
->len
)
595 formal_entry
*formal
= new_formal ();
597 /* Add a special NARG formal, which macro_expand will set to the
598 number of arguments. */
599 /* The same MRI assemblers which treat '@' characters also use
600 the name $NARG. At least until we find an exception. */
606 sb_add_string (&formal
->name
, name
);
608 /* Add to macro's hash table. */
609 if (hash_find (macro
->formal_hash
, name
))
610 as_bad_where (macro
->file
,
612 _("Reserved word `%s' used as parameter in macro `%s'"),
615 hash_jam (macro
->formal_hash
, name
, formal
);
617 formal
->index
= NARG_INDEX
;
624 /* Free the memory allocated to a macro. */
627 free_macro (macro_entry
*macro
)
629 formal_entry
*formal
;
631 for (formal
= macro
->formals
; formal
; )
636 formal
= formal
->next
;
639 hash_die (macro
->formal_hash
);
640 sb_kill (¯o
->sub
);
644 /* Define a new macro. Returns NULL on success, otherwise returns an
645 error message. If NAMEP is not NULL, *NAMEP is set to the name of
646 the macro which was defined. */
649 define_macro (size_t idx
, sb
*in
, sb
*label
,
650 size_t (*get_line
) (sb
*),
651 const char *file
, unsigned int line
,
656 const char *error
= NULL
;
658 macro
= XNEW (macro_entry
);
659 sb_new (¯o
->sub
);
664 macro
->formal_count
= 0;
666 macro
->formal_hash
= hash_new_sized (7);
668 idx
= sb_skip_white (idx
, in
);
669 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
670 error
= _("unexpected end of file in macro `%s' definition");
671 if (label
!= NULL
&& label
->len
!= 0)
673 sb_add_sb (&name
, label
);
674 macro
->name
= sb_terminate (&name
);
675 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
677 /* It's the label: MACRO (formals,...) sort */
678 idx
= do_formals (macro
, idx
+ 1, in
);
679 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
680 idx
= sb_skip_white (idx
+ 1, in
);
682 error
= _("missing `)' after formals in macro definition `%s'");
686 /* It's the label: MACRO formals,... sort */
687 idx
= do_formals (macro
, idx
, in
);
694 idx
= get_token (idx
, in
, &name
);
695 macro
->name
= sb_terminate (&name
);
697 error
= _("Missing macro name");
698 cidx
= sb_skip_white (idx
, in
);
699 idx
= sb_skip_comma (cidx
, in
);
700 if (idx
== cidx
|| idx
< in
->len
)
701 idx
= do_formals (macro
, idx
, in
);
705 if (!error
&& idx
< in
->len
)
706 error
= _("Bad parameter list for macro `%s'");
708 /* And stick it in the macro hash table. */
709 for (idx
= 0; idx
< name
.len
; idx
++)
710 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
711 if (hash_find (macro_hash
, macro
->name
))
712 error
= _("Macro `%s' was already defined");
714 error
= hash_jam (macro_hash
, macro
->name
, (void *) macro
);
717 *namep
= macro
->name
;
727 /* Scan a token, and then skip KIND. */
730 get_apost_token (size_t idx
, sb
*in
, sb
*name
, int kind
)
732 idx
= get_token (idx
, in
, name
);
734 && in
->ptr
[idx
] == kind
735 && (! macro_mri
|| macro_strip_at
)
736 && (! macro_strip_at
|| kind
== '@'))
741 /* Substitute the actual value for a formal parameter. */
744 sub_actual (size_t start
, sb
*in
, sb
*t
, struct hash_control
*formal_hash
,
745 int kind
, sb
*out
, int copyifnotthere
)
750 src
= get_apost_token (start
, in
, t
, kind
);
751 /* See if it's in the macro's hash table, unless this is
752 macro_strip_at and kind is '@' and the token did not end in '@'. */
755 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
758 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (t
));
763 sb_add_sb (out
, &ptr
->actual
);
767 sb_add_sb (out
, &ptr
->def
);
770 else if (kind
== '&')
772 /* Doing this permits people to use & in macro bodies. */
773 sb_add_char (out
, '&');
775 if (src
!= start
&& in
->ptr
[src
- 1] == '&')
776 sb_add_char (out
, '&');
778 else if (copyifnotthere
)
784 sb_add_char (out
, '\\');
790 /* Expand the body of a macro. */
793 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
794 struct hash_control
*formal_hash
, const macro_entry
*macro
)
798 int inquote
= 0, macro_line
= 0;
799 formal_entry
*loclist
= NULL
;
800 const char *err
= NULL
;
804 while (src
< in
->len
&& !err
)
806 if (in
->ptr
[src
] == '&')
811 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
812 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
814 sb_add_char (out
, in
->ptr
[src
++]);
818 /* Permit macro parameter substitution delineated with
819 an '&' prefix and optional '&' suffix. */
820 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
823 else if (in
->ptr
[src
] == '\\')
826 if (src
< in
->len
&& in
->ptr
[src
] == '(')
828 /* Sub in till the next ')' literally. */
830 while (src
< in
->len
&& in
->ptr
[src
] != ')')
832 sb_add_char (out
, in
->ptr
[src
++]);
837 err
= _("missing `)'");
839 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
841 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
843 /* Sub in the macro invocation number. */
847 sprintf (buffer
, "%d", macro_number
);
848 sb_add_string (out
, buffer
);
850 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
852 /* This is a preprocessor variable name, we don't do them
854 sb_add_char (out
, '\\');
855 sb_add_char (out
, '&');
858 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
863 if (ISDIGIT (in
->ptr
[src
]))
864 ind
= in
->ptr
[src
] - '0';
865 else if (ISUPPER (in
->ptr
[src
]))
866 ind
= in
->ptr
[src
] - 'A' + 10;
868 ind
= in
->ptr
[src
] - 'a' + 10;
870 for (f
= formals
; f
!= NULL
; f
= f
->next
)
872 if (f
->index
== ind
- 1)
874 if (f
->actual
.len
!= 0)
875 sb_add_sb (out
, &f
->actual
);
877 sb_add_sb (out
, &f
->def
);
885 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
888 else if ((macro_alternate
|| macro_mri
)
889 && is_name_beginner (in
->ptr
[src
])
892 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
895 || src
+ 5 >= in
->len
896 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
897 || ! ISWHITE (in
->ptr
[src
+ 5])
898 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
902 src
= sub_actual (src
, in
, &t
, formal_hash
,
903 (macro_strip_at
&& inquote
) ? '@' : '\'',
908 src
= sb_skip_white (src
+ 5, in
);
909 while (in
->ptr
[src
] != '\n')
912 formal_entry
*f
= new_formal ();
914 src
= get_token (src
, in
, &f
->name
);
915 name
= sb_terminate (&f
->name
);
916 if (! hash_find (formal_hash
, name
))
921 f
->index
= LOCAL_INDEX
;
925 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
926 sb_add_string (&f
->actual
, buf
);
928 err
= hash_jam (formal_hash
, name
, f
);
934 as_bad_where (macro
->file
,
935 macro
->line
+ macro_line
,
936 _("`%s' was already used as parameter (or another local) name"),
941 src
= sb_skip_comma (src
, in
);
945 else if (in
->ptr
[src
] == '"'
946 || (macro_mri
&& in
->ptr
[src
] == '\''))
949 sb_add_char (out
, in
->ptr
[src
++]);
951 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
955 && in
->ptr
[src
] == '@')
957 sb_add_char (out
, '@');
962 && in
->ptr
[src
] == '='
964 && in
->ptr
[src
+ 1] == '=')
969 src
= get_token (src
+ 2, in
, &t
);
970 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (&t
));
973 /* FIXME: We should really return a warning string here,
974 but we can't, because the == might be in the MRI
975 comment field, and, since the nature of the MRI
976 comment field depends upon the exact instruction
977 being used, we don't have enough information here to
978 figure out whether it is or not. Instead, we leave
979 the == in place, which should cause a syntax error if
980 it is not in a comment. */
981 sb_add_char (out
, '=');
982 sb_add_char (out
, '=');
989 sb_add_string (out
, "-1");
993 sb_add_char (out
, '0');
999 if (in
->ptr
[src
] == '\n')
1001 sb_add_char (out
, in
->ptr
[src
++]);
1007 while (loclist
!= NULL
)
1013 name
= sb_terminate (&loclist
->name
);
1014 hash_delete (formal_hash
, name
, f
== NULL
);
1015 del_formal (loclist
);
1022 /* Assign values to the formal parameters of a macro, and expand the
1026 macro_expand (size_t idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1033 const char *err
= NULL
;
1037 /* Reset any old value the actuals may have. */
1038 for (f
= m
->formals
; f
; f
= f
->next
)
1039 sb_reset (&f
->actual
);
1041 while (f
!= NULL
&& f
->index
< 0)
1046 /* The macro may be called with an optional qualifier, which may
1047 be referred to in the macro body as \0. */
1048 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1050 /* The Microtec assembler ignores this if followed by a white space.
1051 (Macro invocation with empty extension) */
1054 && in
->ptr
[idx
] != ' '
1055 && in
->ptr
[idx
] != '\t')
1057 formal_entry
*n
= new_formal ();
1059 n
->index
= QUAL_INDEX
;
1061 n
->next
= m
->formals
;
1064 idx
= get_any_string (idx
, in
, &n
->actual
);
1069 /* Peel off the actuals and store them away in the hash tables' actuals. */
1070 idx
= sb_skip_white (idx
, in
);
1071 while (idx
< in
->len
)
1075 /* Look and see if it's a positional or keyword arg. */
1077 while (scan
< in
->len
1078 && !ISSEP (in
->ptr
[scan
])
1079 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1080 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1082 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1086 /* It's OK to go from positional to keyword. */
1088 /* This is a keyword arg, fetch the formal name and
1089 then the actual stuff. */
1091 idx
= get_token (idx
, in
, &t
);
1092 if (in
->ptr
[idx
] != '=')
1094 err
= _("confusion in formal parameters");
1098 /* Lookup the formal in the macro's list. */
1099 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1102 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1106 idx
= get_any_string (idx
+ 1, in
, &t
);
1110 /* Insert this value into the right place. */
1111 if (ptr
->actual
.len
)
1113 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1116 sb_reset (&ptr
->actual
);
1118 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1119 if (ptr
->actual
.len
> 0)
1127 err
= _("can't mix positional and keyword arguments");
1138 err
= _("too many positional arguments");
1145 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1146 if ((*pf
)->index
>= c
)
1147 c
= (*pf
)->index
+ 1;
1154 if (f
->type
!= FORMAL_VARARG
)
1155 idx
= get_any_string (idx
, in
, &f
->actual
);
1158 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1161 if (f
->actual
.len
> 0)
1167 while (f
!= NULL
&& f
->index
< 0);
1171 idx
= sb_skip_comma (idx
, in
);
1174 if (in
->ptr
[idx
] == ',')
1176 if (ISWHITE (in
->ptr
[idx
]))
1183 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1185 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1186 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1196 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1197 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1198 sprintf (buffer
, "%d", narg
);
1199 sb_add_string (&ptr
->actual
, buffer
);
1202 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1205 /* Discard any unnamed formal arguments. */
1213 if ((*pf
)->name
.len
!= 0)
1231 /* Check for a macro. If one is found, put the expansion into
1232 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1235 check_macro (const char *line
, sb
*expand
,
1236 const char **error
, macro_entry
**info
)
1243 if (! is_name_beginner (*line
)
1244 && (! macro_mri
|| *line
!= '.'))
1248 while (is_part_of_name (*s
))
1250 if (is_name_ender (*s
))
1253 copy
= xmemdup0 (line
, s
- line
);
1254 for (cls
= copy
; *cls
!= '\0'; cls
++)
1255 *cls
= TOLOWER (*cls
);
1257 macro
= (macro_entry
*) hash_find (macro_hash
, copy
);
1263 /* Wrap the line up in an sb. */
1265 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1266 sb_add_char (&line_sb
, *s
++);
1269 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1273 /* Export the macro information if requested. */
1280 /* Delete a macro. */
1283 delete_macro (const char *name
)
1289 len
= strlen (name
);
1290 copy
= XNEWVEC (char, len
+ 1);
1291 for (i
= 0; i
< len
; ++i
)
1292 copy
[i
] = TOLOWER (name
[i
]);
1295 /* We can only ask hash_delete to free memory if we are deleting
1296 macros in reverse order to their definition.
1297 So just clear out the entry. */
1298 if ((macro
= (macro_entry
*) hash_find (macro_hash
, copy
)) != NULL
)
1300 hash_jam (macro_hash
, copy
, NULL
);
1304 as_warn (_("Attempt to purge non-existing macro `%s'"), copy
);
1308 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1309 combined macro definition and execution. This returns NULL on
1310 success, or an error message otherwise. */
1313 expand_irp (int irpc
, size_t idx
, sb
*in
, sb
*out
, size_t (*get_line
) (sb
*))
1317 struct hash_control
*h
;
1320 idx
= sb_skip_white (idx
, in
);
1323 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1324 return _("unexpected end of file in irp or irpc");
1330 idx
= get_token (idx
, in
, &f
.name
);
1331 if (f
.name
.len
== 0)
1332 return _("missing model parameter");
1335 err
= hash_jam (h
, sb_terminate (&f
.name
), &f
);
1341 f
.type
= FORMAL_OPTIONAL
;
1345 idx
= sb_skip_comma (idx
, in
);
1348 /* Expand once with a null string. */
1349 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1353 bfd_boolean in_quotes
= FALSE
;
1355 if (irpc
&& in
->ptr
[idx
] == '"')
1361 while (idx
< in
->len
)
1364 idx
= get_any_string (idx
, in
, &f
.actual
);
1367 if (in
->ptr
[idx
] == '"')
1372 in_quotes
= ! in_quotes
;
1374 nxt
= sb_skip_white (idx
+ 1, in
);
1381 sb_reset (&f
.actual
);
1382 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1386 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1390 idx
= sb_skip_comma (idx
, in
);
1391 else if (! in_quotes
)
1392 idx
= sb_skip_white (idx
, in
);
1397 sb_kill (&f
.actual
);
This page took 0.089651 seconds and 4 git commands to generate.