1 /* macro.c - macro support for gas
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2011, 2012, 2013 Free Software Foundation, Inc.
5 Written by Steve and Judy Chamberlain of Cygnus Support,
8 This file is part of GAS, the GNU Assembler.
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
26 #include "safe-ctype.h"
30 /* The routines in this file handle macro definition and expansion.
31 They are called by gas. */
33 #define ISWHITE(x) ((x) == ' ' || (x) == '\t')
36 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
37 || (x) == ')' || (x) == '(' \
38 || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
41 ((x) == 'b' || (x) == 'B' \
42 || (x) == 'q' || (x) == 'Q' \
43 || (x) == 'h' || (x) == 'H' \
44 || (x) == 'd' || (x) == 'D')
46 /* The macro hash table. */
48 struct hash_control
*macro_hash
;
50 /* Whether any macros have been defined. */
54 /* Whether we are in alternate syntax mode. */
56 static int macro_alternate
;
58 /* Whether we are in MRI mode. */
62 /* Whether we should strip '@' characters. */
64 static int macro_strip_at
;
66 /* Function to use to parse an expression. */
68 static size_t (*macro_expr
) (const char *, size_t, sb
*, offsetT
*);
70 /* Number of macro expansions that have been done. */
72 static int macro_number
;
74 /* Initialize macro processing. */
77 macro_init (int alternate
, int mri
, int strip_at
,
78 size_t (*exp
) (const char *, size_t, sb
*, offsetT
*))
80 macro_hash
= hash_new ();
82 macro_alternate
= alternate
;
84 macro_strip_at
= strip_at
;
88 /* Switch in and out of alternate mode on the fly. */
91 macro_set_alternate (int alternate
)
93 macro_alternate
= alternate
;
96 /* Switch in and out of MRI mode on the fly. */
99 macro_mri_mode (int mri
)
104 /* Read input lines till we get to a TO string.
105 Increase nesting depth if we get a FROM string.
106 Put the results into sb at PTR.
107 FROM may be NULL (or will be ignored) if TO is "ENDR".
108 Add a new input line to an sb using GET_LINE.
109 Return 1 on success, 0 on unexpected EOF. */
112 buffer_and_nest (const char *from
, const char *to
, sb
*ptr
,
113 size_t (*get_line
) (sb
*))
116 size_t to_len
= strlen (to
);
118 size_t line_start
= ptr
->len
;
119 size_t more
= get_line (ptr
);
121 if (to_len
== 4 && strcasecmp (to
, "ENDR") == 0)
127 from_len
= strlen (from
);
131 /* Try to find the first pseudo op on the line. */
132 size_t i
= line_start
;
133 bfd_boolean had_colon
= FALSE
;
135 /* With normal syntax we can suck what we want till we get
136 to the dot. With the alternate, labels have to start in
137 the first column, since we can't tell what's a label and
138 what's a pseudoop. */
140 if (! LABELS_WITHOUT_COLONS
)
142 /* Skip leading whitespace. */
143 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
149 /* Skip over a label, if any. */
150 if (i
>= ptr
->len
|| ! is_name_beginner (ptr
->ptr
[i
]))
153 while (i
< ptr
->len
&& is_part_of_name (ptr
->ptr
[i
]))
155 if (i
< ptr
->len
&& is_name_ender (ptr
->ptr
[i
]))
157 /* Skip whitespace. */
158 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
160 /* Check for the colon. */
161 if (i
>= ptr
->len
|| ptr
->ptr
[i
] != ':')
163 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
164 colon after a label. If we do have a colon on the
165 first label then handle more than one label on the
166 line, assuming that each label has a colon. */
167 if (LABELS_WITHOUT_COLONS
&& !had_colon
)
177 /* Skip trailing whitespace. */
178 while (i
< ptr
->len
&& ISWHITE (ptr
->ptr
[i
]))
181 if (i
< ptr
->len
&& (ptr
->ptr
[i
] == '.'
185 if (! flag_m68k_mri
&& ptr
->ptr
[i
] == '.')
188 && strncasecmp (ptr
->ptr
+ i
, "IRPC", from_len
= 4) != 0
189 && strncasecmp (ptr
->ptr
+ i
, "IRP", from_len
= 3) != 0
190 && strncasecmp (ptr
->ptr
+ i
, "IREPC", from_len
= 5) != 0
191 && strncasecmp (ptr
->ptr
+ i
, "IREP", from_len
= 4) != 0
192 && strncasecmp (ptr
->ptr
+ i
, "REPT", from_len
= 4) != 0
193 && strncasecmp (ptr
->ptr
+ i
, "REP", from_len
= 3) != 0)
196 ? strncasecmp (ptr
->ptr
+ i
, from
, from_len
) == 0
198 && (ptr
->len
== (i
+ from_len
)
199 || ! (is_part_of_name (ptr
->ptr
[i
+ from_len
])
200 || is_name_ender (ptr
->ptr
[i
+ from_len
]))))
202 if (strncasecmp (ptr
->ptr
+ i
, to
, to_len
) == 0
203 && (ptr
->len
== (i
+ to_len
)
204 || ! (is_part_of_name (ptr
->ptr
[i
+ to_len
])
205 || is_name_ender (ptr
->ptr
[i
+ to_len
]))))
210 /* Reset the string to not include the ending rune. */
211 ptr
->len
= line_start
;
217 /* Add the original end-of-line char to the end and keep running. */
218 sb_add_char (ptr
, more
);
219 line_start
= ptr
->len
;
220 more
= get_line (ptr
);
223 /* Return 1 on success, 0 on unexpected EOF. */
227 /* Pick up a token. */
230 get_token (size_t idx
, sb
*in
, sb
*name
)
233 && is_name_beginner (in
->ptr
[idx
]))
235 sb_add_char (name
, in
->ptr
[idx
++]);
237 && is_part_of_name (in
->ptr
[idx
]))
239 sb_add_char (name
, in
->ptr
[idx
++]);
242 && is_name_ender (in
->ptr
[idx
]))
244 sb_add_char (name
, in
->ptr
[idx
++]);
247 /* Ignore trailing &. */
248 if (macro_alternate
&& idx
< in
->len
&& in
->ptr
[idx
] == '&')
253 /* Pick up a string. */
256 getstring (size_t idx
, sb
*in
, sb
*acc
)
259 && (in
->ptr
[idx
] == '"'
260 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
261 || (in
->ptr
[idx
] == '\'' && macro_alternate
)))
263 if (in
->ptr
[idx
] == '<')
267 while ((in
->ptr
[idx
] != '>' || nest
)
270 if (in
->ptr
[idx
] == '!')
273 sb_add_char (acc
, in
->ptr
[idx
++]);
277 if (in
->ptr
[idx
] == '>')
279 if (in
->ptr
[idx
] == '<')
281 sb_add_char (acc
, in
->ptr
[idx
++]);
286 else if (in
->ptr
[idx
] == '"' || in
->ptr
[idx
] == '\'')
288 char tchar
= in
->ptr
[idx
];
293 while (idx
< in
->len
)
295 if (in
->ptr
[idx
- 1] == '\\')
300 if (macro_alternate
&& in
->ptr
[idx
] == '!')
304 sb_add_char (acc
, in
->ptr
[idx
]);
308 else if (escaped
&& in
->ptr
[idx
] == tchar
)
310 sb_add_char (acc
, tchar
);
315 if (in
->ptr
[idx
] == tchar
)
319 if (idx
>= in
->len
|| in
->ptr
[idx
] != tchar
)
323 sb_add_char (acc
, in
->ptr
[idx
]);
333 /* Fetch string from the input stream,
335 'Bxyx<whitespace> -> return 'Bxyza
336 %<expr> -> return string of decimal value of <expr>
337 "string" -> return string
338 (string) -> return (string-including-whitespaces)
339 xyx<whitespace> -> return xyz. */
342 get_any_string (size_t idx
, sb
*in
, sb
*out
)
345 idx
= sb_skip_white (idx
, in
);
349 if (in
->len
> idx
+ 2 && in
->ptr
[idx
+ 1] == '\'' && ISBASE (in
->ptr
[idx
]))
351 while (!ISSEP (in
->ptr
[idx
]))
352 sb_add_char (out
, in
->ptr
[idx
++]);
354 else if (in
->ptr
[idx
] == '%' && macro_alternate
)
359 /* Turns the next expression into a string. */
360 /* xgettext: no-c-format */
361 idx
= (*macro_expr
) (_("% operator needs absolute expression"),
365 sprintf (buf
, "%" BFD_VMA_FMT
"d", val
);
366 sb_add_string (out
, buf
);
368 else if (in
->ptr
[idx
] == '"'
369 || (in
->ptr
[idx
] == '<' && (macro_alternate
|| macro_mri
))
370 || (macro_alternate
&& in
->ptr
[idx
] == '\''))
372 if (macro_alternate
&& ! macro_strip_at
&& in
->ptr
[idx
] != '<')
374 /* Keep the quotes. */
375 sb_add_char (out
, '"');
376 idx
= getstring (idx
, in
, out
);
377 sb_add_char (out
, '"');
381 idx
= getstring (idx
, in
, out
);
386 char *br_buf
= (char *) xmalloc (1);
387 char *in_br
= br_buf
;
392 || (in
->ptr
[idx
] != ' '
393 && in
->ptr
[idx
] != '\t'))
394 && in
->ptr
[idx
] != ','
395 && (in
->ptr
[idx
] != '<'
396 || (! macro_alternate
&& ! macro_mri
)))
398 char tchar
= in
->ptr
[idx
];
404 sb_add_char (out
, in
->ptr
[idx
++]);
406 && in
->ptr
[idx
] != tchar
)
407 sb_add_char (out
, in
->ptr
[idx
++]);
420 br_buf
= (char *) xmalloc (strlen (in_br
) + 2);
421 strcpy (br_buf
+ 1, in_br
);
436 sb_add_char (out
, tchar
);
446 /* Allocate a new formal. */
448 static formal_entry
*
451 formal_entry
*formal
;
453 formal
= (formal_entry
*) xmalloc (sizeof (formal_entry
));
455 sb_new (&formal
->name
);
456 sb_new (&formal
->def
);
457 sb_new (&formal
->actual
);
459 formal
->type
= FORMAL_OPTIONAL
;
466 del_formal (formal_entry
*formal
)
468 sb_kill (&formal
->actual
);
469 sb_kill (&formal
->def
);
470 sb_kill (&formal
->name
);
474 /* Pick up the formal parameters of a macro definition. */
477 do_formals (macro_entry
*macro
, size_t idx
, sb
*in
)
479 formal_entry
**p
= ¯o
->formals
;
482 idx
= sb_skip_white (idx
, in
);
483 while (idx
< in
->len
)
485 formal_entry
*formal
= new_formal ();
488 idx
= get_token (idx
, in
, &formal
->name
);
489 if (formal
->name
.len
== 0)
491 if (macro
->formal_count
)
493 del_formal (formal
); /* 'formal' goes out of scope. */
496 idx
= sb_skip_white (idx
, in
);
497 /* This is a formal. */
498 name
= sb_terminate (&formal
->name
);
501 && in
->ptr
[idx
] == ':'
502 && (! is_name_beginner (':')
503 || idx
+ 1 >= in
->len
504 || ! is_part_of_name (in
->ptr
[idx
+ 1])))
506 /* Got a qualifier. */
510 idx
= get_token (sb_skip_white (idx
+ 1, in
), in
, &qual
);
511 sb_terminate (&qual
);
513 as_bad_where (macro
->file
,
515 _("Missing parameter qualifier for `%s' in macro `%s'"),
518 else if (strcmp (qual
.ptr
, "req") == 0)
519 formal
->type
= FORMAL_REQUIRED
;
520 else if (strcmp (qual
.ptr
, "vararg") == 0)
521 formal
->type
= FORMAL_VARARG
;
523 as_bad_where (macro
->file
,
525 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
530 idx
= sb_skip_white (idx
, in
);
532 if (idx
< in
->len
&& in
->ptr
[idx
] == '=')
535 idx
= get_any_string (idx
+ 1, in
, &formal
->def
);
536 idx
= sb_skip_white (idx
, in
);
537 if (formal
->type
== FORMAL_REQUIRED
)
539 sb_reset (&formal
->def
);
540 as_warn_where (macro
->file
,
542 _("Pointless default value for required parameter `%s' in macro `%s'"),
548 /* Add to macro's hash table. */
549 if (! hash_find (macro
->formal_hash
, name
))
550 hash_jam (macro
->formal_hash
, name
, formal
);
552 as_bad_where (macro
->file
,
554 _("A parameter named `%s' already exists for macro `%s'"),
558 formal
->index
= macro
->formal_count
++;
561 if (formal
->type
== FORMAL_VARARG
)
564 idx
= sb_skip_comma (idx
, in
);
565 if (idx
!= cidx
&& idx
>= in
->len
)
574 formal_entry
*formal
= new_formal ();
576 /* Add a special NARG formal, which macro_expand will set to the
577 number of arguments. */
578 /* The same MRI assemblers which treat '@' characters also use
579 the name $NARG. At least until we find an exception. */
585 sb_add_string (&formal
->name
, name
);
587 /* Add to macro's hash table. */
588 if (hash_find (macro
->formal_hash
, name
))
589 as_bad_where (macro
->file
,
591 _("Reserved word `%s' used as parameter in macro `%s'"),
594 hash_jam (macro
->formal_hash
, name
, formal
);
596 formal
->index
= NARG_INDEX
;
603 /* Free the memory allocated to a macro. */
606 free_macro (macro_entry
*macro
)
608 formal_entry
*formal
;
610 for (formal
= macro
->formals
; formal
; )
615 formal
= formal
->next
;
618 hash_die (macro
->formal_hash
);
619 sb_kill (¯o
->sub
);
623 /* Define a new macro. Returns NULL on success, otherwise returns an
624 error message. If NAMEP is not NULL, *NAMEP is set to the name of
625 the macro which was defined. */
628 define_macro (size_t idx
, sb
*in
, sb
*label
,
629 size_t (*get_line
) (sb
*),
630 char *file
, unsigned int line
,
635 const char *error
= NULL
;
637 macro
= (macro_entry
*) xmalloc (sizeof (macro_entry
));
638 sb_new (¯o
->sub
);
643 macro
->formal_count
= 0;
645 macro
->formal_hash
= hash_new_sized (7);
647 idx
= sb_skip_white (idx
, in
);
648 if (! buffer_and_nest ("MACRO", "ENDM", ¯o
->sub
, get_line
))
649 error
= _("unexpected end of file in macro `%s' definition");
650 if (label
!= NULL
&& label
->len
!= 0)
652 sb_add_sb (&name
, label
);
653 macro
->name
= sb_terminate (&name
);
654 if (idx
< in
->len
&& in
->ptr
[idx
] == '(')
656 /* It's the label: MACRO (formals,...) sort */
657 idx
= do_formals (macro
, idx
+ 1, in
);
658 if (idx
< in
->len
&& in
->ptr
[idx
] == ')')
659 idx
= sb_skip_white (idx
+ 1, in
);
661 error
= _("missing `)' after formals in macro definition `%s'");
665 /* It's the label: MACRO formals,... sort */
666 idx
= do_formals (macro
, idx
, in
);
673 idx
= get_token (idx
, in
, &name
);
674 macro
->name
= sb_terminate (&name
);
676 error
= _("Missing macro name");
677 cidx
= sb_skip_white (idx
, in
);
678 idx
= sb_skip_comma (cidx
, in
);
679 if (idx
== cidx
|| idx
< in
->len
)
680 idx
= do_formals (macro
, idx
, in
);
684 if (!error
&& idx
< in
->len
)
685 error
= _("Bad parameter list for macro `%s'");
687 /* And stick it in the macro hash table. */
688 for (idx
= 0; idx
< name
.len
; idx
++)
689 name
.ptr
[idx
] = TOLOWER (name
.ptr
[idx
]);
690 if (hash_find (macro_hash
, macro
->name
))
691 error
= _("Macro `%s' was already defined");
693 error
= hash_jam (macro_hash
, macro
->name
, (void *) macro
);
696 *namep
= macro
->name
;
706 /* Scan a token, and then skip KIND. */
709 get_apost_token (size_t idx
, sb
*in
, sb
*name
, int kind
)
711 idx
= get_token (idx
, in
, name
);
713 && in
->ptr
[idx
] == kind
714 && (! macro_mri
|| macro_strip_at
)
715 && (! macro_strip_at
|| kind
== '@'))
720 /* Substitute the actual value for a formal parameter. */
723 sub_actual (size_t start
, sb
*in
, sb
*t
, struct hash_control
*formal_hash
,
724 int kind
, sb
*out
, int copyifnotthere
)
729 src
= get_apost_token (start
, in
, t
, kind
);
730 /* See if it's in the macro's hash table, unless this is
731 macro_strip_at and kind is '@' and the token did not end in '@'. */
734 && (src
== start
|| in
->ptr
[src
- 1] != '@'))
737 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (t
));
742 sb_add_sb (out
, &ptr
->actual
);
746 sb_add_sb (out
, &ptr
->def
);
749 else if (kind
== '&')
751 /* Doing this permits people to use & in macro bodies. */
752 sb_add_char (out
, '&');
754 if (src
!= start
&& in
->ptr
[src
- 1] == '&')
755 sb_add_char (out
, '&');
757 else if (copyifnotthere
)
763 sb_add_char (out
, '\\');
769 /* Expand the body of a macro. */
772 macro_expand_body (sb
*in
, sb
*out
, formal_entry
*formals
,
773 struct hash_control
*formal_hash
, const macro_entry
*macro
)
777 int inquote
= 0, macro_line
= 0;
778 formal_entry
*loclist
= NULL
;
779 const char *err
= NULL
;
783 while (src
< in
->len
&& !err
)
785 if (in
->ptr
[src
] == '&')
790 if (src
+ 1 < in
->len
&& in
->ptr
[src
+ 1] == '&')
791 src
= sub_actual (src
+ 2, in
, &t
, formal_hash
, '\'', out
, 1);
793 sb_add_char (out
, in
->ptr
[src
++]);
797 /* Permit macro parameter substition delineated with
798 an '&' prefix and optional '&' suffix. */
799 src
= sub_actual (src
+ 1, in
, &t
, formal_hash
, '&', out
, 0);
802 else if (in
->ptr
[src
] == '\\')
805 if (src
< in
->len
&& in
->ptr
[src
] == '(')
807 /* Sub in till the next ')' literally. */
809 while (src
< in
->len
&& in
->ptr
[src
] != ')')
811 sb_add_char (out
, in
->ptr
[src
++]);
816 err
= _("missing `)'");
818 as_bad_where (macro
->file
, macro
->line
+ macro_line
, _("missing `)'"));
820 else if (src
< in
->len
&& in
->ptr
[src
] == '@')
822 /* Sub in the macro invocation number. */
826 sprintf (buffer
, "%d", macro_number
);
827 sb_add_string (out
, buffer
);
829 else if (src
< in
->len
&& in
->ptr
[src
] == '&')
831 /* This is a preprocessor variable name, we don't do them
833 sb_add_char (out
, '\\');
834 sb_add_char (out
, '&');
837 else if (macro_mri
&& src
< in
->len
&& ISALNUM (in
->ptr
[src
]))
842 if (ISDIGIT (in
->ptr
[src
]))
843 ind
= in
->ptr
[src
] - '0';
844 else if (ISUPPER (in
->ptr
[src
]))
845 ind
= in
->ptr
[src
] - 'A' + 10;
847 ind
= in
->ptr
[src
] - 'a' + 10;
849 for (f
= formals
; f
!= NULL
; f
= f
->next
)
851 if (f
->index
== ind
- 1)
853 if (f
->actual
.len
!= 0)
854 sb_add_sb (out
, &f
->actual
);
856 sb_add_sb (out
, &f
->def
);
864 src
= sub_actual (src
, in
, &t
, formal_hash
, '\'', out
, 0);
867 else if ((macro_alternate
|| macro_mri
)
868 && is_name_beginner (in
->ptr
[src
])
871 || (src
> 0 && in
->ptr
[src
- 1] == '@')))
874 || src
+ 5 >= in
->len
875 || strncasecmp (in
->ptr
+ src
, "LOCAL", 5) != 0
876 || ! ISWHITE (in
->ptr
[src
+ 5])
877 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
881 src
= sub_actual (src
, in
, &t
, formal_hash
,
882 (macro_strip_at
&& inquote
) ? '@' : '\'',
887 src
= sb_skip_white (src
+ 5, in
);
888 while (in
->ptr
[src
] != '\n')
891 formal_entry
*f
= new_formal ();
893 src
= get_token (src
, in
, &f
->name
);
894 name
= sb_terminate (&f
->name
);
895 if (! hash_find (formal_hash
, name
))
900 f
->index
= LOCAL_INDEX
;
904 sprintf (buf
, IS_ELF
? ".LL%04x" : "LL%04x", ++loccnt
);
905 sb_add_string (&f
->actual
, buf
);
907 err
= hash_jam (formal_hash
, name
, f
);
913 as_bad_where (macro
->file
,
914 macro
->line
+ macro_line
,
915 _("`%s' was already used as parameter (or another local) name"),
920 src
= sb_skip_comma (src
, in
);
924 else if (in
->ptr
[src
] == '"'
925 || (macro_mri
&& in
->ptr
[src
] == '\''))
928 sb_add_char (out
, in
->ptr
[src
++]);
930 else if (in
->ptr
[src
] == '@' && macro_strip_at
)
934 && in
->ptr
[src
] == '@')
936 sb_add_char (out
, '@');
941 && in
->ptr
[src
] == '='
943 && in
->ptr
[src
+ 1] == '=')
948 src
= get_token (src
+ 2, in
, &t
);
949 ptr
= (formal_entry
*) hash_find (formal_hash
, sb_terminate (&t
));
952 /* FIXME: We should really return a warning string here,
953 but we can't, because the == might be in the MRI
954 comment field, and, since the nature of the MRI
955 comment field depends upon the exact instruction
956 being used, we don't have enough information here to
957 figure out whether it is or not. Instead, we leave
958 the == in place, which should cause a syntax error if
959 it is not in a comment. */
960 sb_add_char (out
, '=');
961 sb_add_char (out
, '=');
968 sb_add_string (out
, "-1");
972 sb_add_char (out
, '0');
978 if (in
->ptr
[src
] == '\n')
980 sb_add_char (out
, in
->ptr
[src
++]);
986 while (loclist
!= NULL
)
992 name
= sb_terminate (&loclist
->name
);
993 hash_delete (formal_hash
, name
, f
== NULL
);
994 del_formal (loclist
);
1001 /* Assign values to the formal parameters of a macro, and expand the
1005 macro_expand (size_t idx
, sb
*in
, macro_entry
*m
, sb
*out
)
1012 const char *err
= NULL
;
1016 /* Reset any old value the actuals may have. */
1017 for (f
= m
->formals
; f
; f
= f
->next
)
1018 sb_reset (&f
->actual
);
1020 while (f
!= NULL
&& f
->index
< 0)
1025 /* The macro may be called with an optional qualifier, which may
1026 be referred to in the macro body as \0. */
1027 if (idx
< in
->len
&& in
->ptr
[idx
] == '.')
1029 /* The Microtec assembler ignores this if followed by a white space.
1030 (Macro invocation with empty extension) */
1033 && in
->ptr
[idx
] != ' '
1034 && in
->ptr
[idx
] != '\t')
1036 formal_entry
*n
= new_formal ();
1038 n
->index
= QUAL_INDEX
;
1040 n
->next
= m
->formals
;
1043 idx
= get_any_string (idx
, in
, &n
->actual
);
1048 /* Peel off the actuals and store them away in the hash tables' actuals. */
1049 idx
= sb_skip_white (idx
, in
);
1050 while (idx
< in
->len
)
1054 /* Look and see if it's a positional or keyword arg. */
1056 while (scan
< in
->len
1057 && !ISSEP (in
->ptr
[scan
])
1058 && !(macro_mri
&& in
->ptr
[scan
] == '\'')
1059 && (!macro_alternate
&& in
->ptr
[scan
] != '='))
1061 if (scan
< in
->len
&& !macro_alternate
&& in
->ptr
[scan
] == '=')
1065 /* It's OK to go from positional to keyword. */
1067 /* This is a keyword arg, fetch the formal name and
1068 then the actual stuff. */
1070 idx
= get_token (idx
, in
, &t
);
1071 if (in
->ptr
[idx
] != '=')
1073 err
= _("confusion in formal parameters");
1077 /* Lookup the formal in the macro's list. */
1078 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1081 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1085 idx
= get_any_string (idx
+ 1, in
, &t
);
1089 /* Insert this value into the right place. */
1090 if (ptr
->actual
.len
)
1092 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1095 sb_reset (&ptr
->actual
);
1097 idx
= get_any_string (idx
+ 1, in
, &ptr
->actual
);
1098 if (ptr
->actual
.len
> 0)
1106 err
= _("can't mix positional and keyword arguments");
1117 err
= _("too many positional arguments");
1124 for (pf
= &m
->formals
; *pf
!= NULL
; pf
= &(*pf
)->next
)
1125 if ((*pf
)->index
>= c
)
1126 c
= (*pf
)->index
+ 1;
1133 if (f
->type
!= FORMAL_VARARG
)
1134 idx
= get_any_string (idx
, in
, &f
->actual
);
1137 sb_add_buffer (&f
->actual
, in
->ptr
+ idx
, in
->len
- idx
);
1140 if (f
->actual
.len
> 0)
1146 while (f
!= NULL
&& f
->index
< 0);
1150 idx
= sb_skip_comma (idx
, in
);
1153 if (in
->ptr
[idx
] == ',')
1155 if (ISWHITE (in
->ptr
[idx
]))
1162 for (ptr
= m
->formals
; ptr
; ptr
= ptr
->next
)
1164 if (ptr
->type
== FORMAL_REQUIRED
&& ptr
->actual
.len
== 0)
1165 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1175 sb_add_string (&t
, macro_strip_at
? "$NARG" : "NARG");
1176 ptr
= (formal_entry
*) hash_find (m
->formal_hash
, sb_terminate (&t
));
1177 sprintf (buffer
, "%d", narg
);
1178 sb_add_string (&ptr
->actual
, buffer
);
1181 err
= macro_expand_body (&m
->sub
, out
, m
->formals
, m
->formal_hash
, m
);
1184 /* Discard any unnamed formal arguments. */
1192 if ((*pf
)->name
.len
!= 0)
1210 /* Check for a macro. If one is found, put the expansion into
1211 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1214 check_macro (const char *line
, sb
*expand
,
1215 const char **error
, macro_entry
**info
)
1222 if (! is_name_beginner (*line
)
1223 && (! macro_mri
|| *line
!= '.'))
1227 while (is_part_of_name (*s
))
1229 if (is_name_ender (*s
))
1232 copy
= (char *) alloca (s
- line
+ 1);
1233 memcpy (copy
, line
, s
- line
);
1234 copy
[s
- line
] = '\0';
1235 for (cls
= copy
; *cls
!= '\0'; cls
++)
1236 *cls
= TOLOWER (*cls
);
1238 macro
= (macro_entry
*) hash_find (macro_hash
, copy
);
1243 /* Wrap the line up in an sb. */
1245 while (*s
!= '\0' && *s
!= '\n' && *s
!= '\r')
1246 sb_add_char (&line_sb
, *s
++);
1249 *error
= macro_expand (0, &line_sb
, macro
, expand
);
1253 /* Export the macro information if requested. */
1260 /* Delete a macro. */
1263 delete_macro (const char *name
)
1269 len
= strlen (name
);
1270 copy
= (char *) alloca (len
+ 1);
1271 for (i
= 0; i
< len
; ++i
)
1272 copy
[i
] = TOLOWER (name
[i
]);
1275 /* We can only ask hash_delete to free memory if we are deleting
1276 macros in reverse order to their definition.
1277 So just clear out the entry. */
1278 if ((macro
= (macro_entry
*) hash_find (macro_hash
, copy
)) != NULL
)
1280 hash_jam (macro_hash
, copy
, NULL
);
1284 as_warn (_("Attempt to purge non-existant macro `%s'"), copy
);
1287 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1288 combined macro definition and execution. This returns NULL on
1289 success, or an error message otherwise. */
1292 expand_irp (int irpc
, size_t idx
, sb
*in
, sb
*out
, size_t (*get_line
) (sb
*))
1296 struct hash_control
*h
;
1299 idx
= sb_skip_white (idx
, in
);
1302 if (! buffer_and_nest (NULL
, "ENDR", &sub
, get_line
))
1303 return _("unexpected end of file in irp or irpc");
1309 idx
= get_token (idx
, in
, &f
.name
);
1310 if (f
.name
.len
== 0)
1311 return _("missing model parameter");
1314 err
= hash_jam (h
, sb_terminate (&f
.name
), &f
);
1320 f
.type
= FORMAL_OPTIONAL
;
1324 idx
= sb_skip_comma (idx
, in
);
1327 /* Expand once with a null string. */
1328 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1332 bfd_boolean in_quotes
= FALSE
;
1334 if (irpc
&& in
->ptr
[idx
] == '"')
1340 while (idx
< in
->len
)
1343 idx
= get_any_string (idx
, in
, &f
.actual
);
1346 if (in
->ptr
[idx
] == '"')
1351 in_quotes
= ! in_quotes
;
1353 nxt
= sb_skip_white (idx
+ 1, in
);
1360 sb_reset (&f
.actual
);
1361 sb_add_char (&f
.actual
, in
->ptr
[idx
]);
1365 err
= macro_expand_body (&sub
, out
, &f
, h
, 0);
1369 idx
= sb_skip_comma (idx
, in
);
1370 else if (! in_quotes
)
1371 idx
= sb_skip_white (idx
, in
);
1376 sb_kill (&f
.actual
);
This page took 0.05964 seconds and 4 git commands to generate.