PR29991, MicroMIPS flag erased after align directives
[binutils-gdb.git] / gas / macro.c
1 /* macro.c - macro support for gas
2 Copyright (C) 1994-2023 Free Software Foundation, Inc.
3
4 Written by Steve and Judy Chamberlain of Cygnus Support,
5 sac@cygnus.com
6
7 This file is part of GAS, the GNU Assembler.
8
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)
12 any later version.
13
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.
18
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
22 02110-1301, USA. */
23
24 #include "as.h"
25 #include "safe-ctype.h"
26 #include "sb.h"
27 #include "macro.h"
28
29 /* The routines in this file handle macro definition and expansion.
30 They are called by gas. */
31
32 #define ISWHITE(x) ((x) == ' ' || (x) == '\t')
33
34 #define ISSEP(x) \
35 ((x) == ' ' || (x) == '\t' || (x) == ',' || (x) == '"' || (x) == ';' \
36 || (x) == ')' || (x) == '(' \
37 || ((macro_alternate || macro_mri) && ((x) == '<' || (x) == '>')))
38
39 #define ISBASE(x) \
40 ((x) == 'b' || (x) == 'B' \
41 || (x) == 'q' || (x) == 'Q' \
42 || (x) == 'h' || (x) == 'H' \
43 || (x) == 'd' || (x) == 'D')
44
45 /* The macro hash table. */
46
47 htab_t macro_hash;
48
49 /* Whether any macros have been defined. */
50
51 int macro_defined;
52
53 /* Whether we are in alternate syntax mode. */
54
55 static int macro_alternate;
56
57 /* Whether we are in MRI mode. */
58
59 static int macro_mri;
60
61 /* Whether we should strip '@' characters. */
62
63 static int macro_strip_at;
64
65 /* Function to use to parse an expression. */
66
67 static size_t (*macro_expr) (const char *, size_t, sb *, offsetT *);
68
69 /* Number of macro expansions that have been done. */
70
71 static int macro_number;
72
73 /* Initialize macro processing. */
74
75 void
76 macro_init (int alternate, int mri, int strip_at,
77 size_t (*exp) (const char *, size_t, sb *, offsetT *))
78 {
79 macro_hash = str_htab_create ();
80 macro_defined = 0;
81 macro_alternate = alternate;
82 macro_mri = mri;
83 macro_strip_at = strip_at;
84 macro_expr = exp;
85 }
86
87 void
88 macro_end (void)
89 {
90 htab_delete (macro_hash);
91 }
92
93 /* Switch in and out of alternate mode on the fly. */
94
95 void
96 macro_set_alternate (int alternate)
97 {
98 macro_alternate = alternate;
99 }
100
101 /* Switch in and out of MRI mode on the fly. */
102
103 void
104 macro_mri_mode (int mri)
105 {
106 macro_mri = mri;
107 }
108
109 /* Read input lines till we get to a TO string.
110 Increase nesting depth if we get a FROM string.
111 Put the results into sb at PTR.
112 FROM may be NULL (or will be ignored) if TO is "ENDR".
113 Add a new input line to an sb using GET_LINE.
114 Return 1 on success, 0 on unexpected EOF. */
115
116 int
117 buffer_and_nest (const char *from, const char *to, sb *ptr,
118 size_t (*get_line) (sb *))
119 {
120 size_t from_len;
121 size_t to_len = strlen (to);
122 int depth = 1;
123 size_t line_start, more;
124
125 if (to_len == 4 && strcasecmp (to, "ENDR") == 0)
126 {
127 from = NULL;
128 from_len = 0;
129 }
130 else
131 from_len = strlen (from);
132
133 /* Record the present source position, such that diagnostics and debug info
134 can be properly associated with the respective original lines, rather
135 than with the line of the ending directive (TO). */
136 {
137 unsigned int line;
138 char *linefile;
139
140 as_where_top (&line);
141 if (!flag_m68k_mri)
142 linefile = xasprintf ("\t.linefile %u .", line + 1);
143 else
144 linefile = xasprintf ("\tlinefile %u .", line + 1);
145 sb_add_string (ptr, linefile);
146 xfree (linefile);
147 }
148
149 line_start = ptr->len;
150 more = get_line (ptr);
151 while (more)
152 {
153 /* Try to find the first pseudo op on the line. */
154 size_t i = line_start;
155 bool had_colon = false;
156
157 /* With normal syntax we can suck what we want till we get
158 to the dot. With the alternate, labels have to start in
159 the first column, since we can't tell what's a label and
160 what's a pseudoop. */
161
162 if (! LABELS_WITHOUT_COLONS)
163 {
164 /* Skip leading whitespace. */
165 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
166 i++;
167 }
168
169 for (;;)
170 {
171 /* Skip over a label, if any. */
172 if (i >= ptr->len || ! is_name_beginner (ptr->ptr[i]))
173 break;
174 i++;
175 while (i < ptr->len && is_part_of_name (ptr->ptr[i]))
176 i++;
177 if (i < ptr->len && is_name_ender (ptr->ptr[i]))
178 i++;
179 /* Skip whitespace. */
180 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
181 i++;
182 /* Check for the colon. */
183 if (i >= ptr->len || ptr->ptr[i] != ':')
184 {
185 /* LABELS_WITHOUT_COLONS doesn't mean we cannot have a
186 colon after a label. If we do have a colon on the
187 first label then handle more than one label on the
188 line, assuming that each label has a colon. */
189 if (LABELS_WITHOUT_COLONS && !had_colon)
190 break;
191 i = line_start;
192 break;
193 }
194 i++;
195 line_start = i;
196 had_colon = true;
197 }
198
199 /* Skip trailing whitespace. */
200 while (i < ptr->len && ISWHITE (ptr->ptr[i]))
201 i++;
202
203 if (i < ptr->len && (ptr->ptr[i] == '.'
204 || NO_PSEUDO_DOT
205 || macro_mri))
206 {
207 if (! flag_m68k_mri && ptr->ptr[i] == '.')
208 i++;
209 size_t len = ptr->len - i;
210 if (from == NULL)
211 {
212 if (len >= 5 && strncasecmp (ptr->ptr + i, "IREPC", 5) == 0)
213 from_len = 5;
214 else if (len >= 4 && strncasecmp (ptr->ptr + i, "IREP", 4) == 0)
215 from_len = 4;
216 else if (len >= 4 && strncasecmp (ptr->ptr + i, "IRPC", 4) == 0)
217 from_len = 4;
218 else if (len >= 4 && strncasecmp (ptr->ptr + i, "REPT", 4) == 0)
219 from_len = 4;
220 else if (len >= 3 && strncasecmp (ptr->ptr + i, "IRP", 3) == 0)
221 from_len = 3;
222 else if (len >= 3 && strncasecmp (ptr->ptr + i, "REP", 3) == 0)
223 from_len = 3;
224 else
225 from_len = 0;
226 }
227 if ((from != NULL
228 ? (len >= from_len
229 && strncasecmp (ptr->ptr + i, from, from_len) == 0)
230 : from_len > 0)
231 && (len == from_len
232 || ! (is_part_of_name (ptr->ptr[i + from_len])
233 || is_name_ender (ptr->ptr[i + from_len]))))
234 depth++;
235 if (len >= to_len
236 && strncasecmp (ptr->ptr + i, to, to_len) == 0
237 && (len == to_len
238 || ! (is_part_of_name (ptr->ptr[i + to_len])
239 || is_name_ender (ptr->ptr[i + to_len]))))
240 {
241 depth--;
242 if (depth == 0)
243 {
244 /* Reset the string to not include the ending rune. */
245 ptr->len = line_start;
246 break;
247 }
248 }
249
250 /* PR gas/16908
251 Apply .linefile directives that appear within the macro, alongside
252 keeping them for later expansion of the macro. */
253 if (from != NULL && strcasecmp (from, "MACRO") == 0
254 && len >= 8 && strncasecmp (ptr->ptr + i, "linefile", 8) == 0)
255 {
256 char saved_eol_char = ptr->ptr[ptr->len];
257
258 ptr->ptr[ptr->len] = '\0';
259 temp_ilp (ptr->ptr + i + 8);
260 s_linefile (0);
261 restore_ilp ();
262 ptr->ptr[ptr->len] = saved_eol_char;
263 }
264 }
265
266 /* Add the original end-of-line char to the end and keep running. */
267 sb_add_char (ptr, more);
268 line_start = ptr->len;
269 more = get_line (ptr);
270 }
271
272 /* Return 1 on success, 0 on unexpected EOF. */
273 return depth == 0;
274 }
275
276 /* Pick up a token. */
277
278 static size_t
279 get_token (size_t idx, sb *in, sb *name)
280 {
281 if (idx < in->len
282 && is_name_beginner (in->ptr[idx]))
283 {
284 sb_add_char (name, in->ptr[idx++]);
285 while (idx < in->len
286 && is_part_of_name (in->ptr[idx]))
287 {
288 sb_add_char (name, in->ptr[idx++]);
289 }
290 if (idx < in->len
291 && is_name_ender (in->ptr[idx]))
292 {
293 sb_add_char (name, in->ptr[idx++]);
294 }
295 }
296 /* Ignore trailing &. */
297 if (macro_alternate && idx < in->len && in->ptr[idx] == '&')
298 idx++;
299 return idx;
300 }
301
302 /* Pick up a string. */
303
304 static size_t
305 getstring (size_t idx, sb *in, sb *acc)
306 {
307 while (idx < in->len
308 && (in->ptr[idx] == '"'
309 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
310 || (in->ptr[idx] == '\'' && macro_alternate)))
311 {
312 if (in->ptr[idx] == '<')
313 {
314 int nest = 0;
315 idx++;
316 while (idx < in->len
317 && (in->ptr[idx] != '>' || nest))
318 {
319 if (in->ptr[idx] == '!')
320 {
321 idx++;
322 sb_add_char (acc, in->ptr[idx++]);
323 }
324 else
325 {
326 if (in->ptr[idx] == '>')
327 nest--;
328 if (in->ptr[idx] == '<')
329 nest++;
330 sb_add_char (acc, in->ptr[idx++]);
331 }
332 }
333 idx++;
334 }
335 else if (in->ptr[idx] == '"' || in->ptr[idx] == '\'')
336 {
337 char tchar = in->ptr[idx];
338 int escaped = 0;
339
340 idx++;
341
342 while (idx < in->len)
343 {
344 if (in->ptr[idx - 1] == '\\')
345 escaped ^= 1;
346 else
347 escaped = 0;
348
349 if (macro_alternate && in->ptr[idx] == '!')
350 {
351 idx ++;
352
353 sb_add_char (acc, in->ptr[idx]);
354
355 idx ++;
356 }
357 else if (escaped && in->ptr[idx] == tchar)
358 {
359 sb_add_char (acc, tchar);
360 idx ++;
361 }
362 else
363 {
364 if (in->ptr[idx] == tchar)
365 {
366 idx ++;
367
368 if (idx >= in->len || in->ptr[idx] != tchar)
369 break;
370 }
371
372 sb_add_char (acc, in->ptr[idx]);
373 idx ++;
374 }
375 }
376 }
377 }
378
379 return idx;
380 }
381
382 /* Fetch string from the input stream,
383 rules:
384 'Bxyx<whitespace> -> return 'Bxyza
385 %<expr> -> return string of decimal value of <expr>
386 "string" -> return string
387 (string) -> return (string-including-whitespaces)
388 xyx<whitespace> -> return xyz. */
389
390 static size_t
391 get_any_string (size_t idx, sb *in, sb *out)
392 {
393 sb_reset (out);
394 idx = sb_skip_white (idx, in);
395
396 if (idx < in->len)
397 {
398 if (in->len > idx + 2 && in->ptr[idx + 1] == '\'' && ISBASE (in->ptr[idx]))
399 {
400 while (idx < in->len && !ISSEP (in->ptr[idx]))
401 sb_add_char (out, in->ptr[idx++]);
402 }
403 else if (in->ptr[idx] == '%' && macro_alternate)
404 {
405 offsetT val;
406 char buf[64];
407
408 /* Turns the next expression into a string. */
409 /* xgettext: no-c-format */
410 idx = (*macro_expr) (_("% operator needs absolute expression"),
411 idx + 1,
412 in,
413 &val);
414 sprintf (buf, "%" PRId64, (int64_t) val);
415 sb_add_string (out, buf);
416 }
417 else if (in->ptr[idx] == '"'
418 || (in->ptr[idx] == '<' && (macro_alternate || macro_mri))
419 || (macro_alternate && in->ptr[idx] == '\''))
420 {
421 if (macro_alternate && ! macro_strip_at && in->ptr[idx] != '<')
422 {
423 /* Keep the quotes. */
424 sb_add_char (out, '"');
425 idx = getstring (idx, in, out);
426 sb_add_char (out, '"');
427 }
428 else
429 {
430 idx = getstring (idx, in, out);
431 }
432 }
433 else
434 {
435 char *br_buf = XNEWVEC (char, 1);
436 char *in_br = br_buf;
437
438 *in_br = '\0';
439 while (idx < in->len
440 && (*in_br
441 || (in->ptr[idx] != ' '
442 && in->ptr[idx] != '\t'))
443 && in->ptr[idx] != ','
444 && (in->ptr[idx] != '<'
445 || (! macro_alternate && ! macro_mri)))
446 {
447 char tchar = in->ptr[idx];
448
449 switch (tchar)
450 {
451 case '"':
452 case '\'':
453 sb_add_char (out, in->ptr[idx++]);
454 while (idx < in->len
455 && in->ptr[idx] != tchar)
456 sb_add_char (out, in->ptr[idx++]);
457 if (idx == in->len)
458 {
459 free (br_buf);
460 return idx;
461 }
462 break;
463 case '(':
464 case '[':
465 if (in_br > br_buf)
466 --in_br;
467 else
468 {
469 br_buf = XNEWVEC (char, strlen (in_br) + 2);
470 strcpy (br_buf + 1, in_br);
471 free (in_br);
472 in_br = br_buf;
473 }
474 *in_br = tchar;
475 break;
476 case ')':
477 if (*in_br == '(')
478 ++in_br;
479 break;
480 case ']':
481 if (*in_br == '[')
482 ++in_br;
483 break;
484 }
485 sb_add_char (out, tchar);
486 ++idx;
487 }
488 free (br_buf);
489 }
490 }
491
492 return idx;
493 }
494
495 /* Allocate a new formal. */
496
497 static formal_entry *
498 new_formal (void)
499 {
500 formal_entry *formal;
501
502 formal = XNEW (formal_entry);
503
504 sb_new (&formal->name);
505 sb_new (&formal->def);
506 sb_new (&formal->actual);
507 formal->next = NULL;
508 formal->type = FORMAL_OPTIONAL;
509 return formal;
510 }
511
512 /* Free a formal. */
513
514 static void
515 del_formal (formal_entry *formal)
516 {
517 sb_kill (&formal->actual);
518 sb_kill (&formal->def);
519 sb_kill (&formal->name);
520 free (formal);
521 }
522
523 /* Pick up the formal parameters of a macro definition. */
524
525 static size_t
526 do_formals (macro_entry *macro, size_t idx, sb *in)
527 {
528 formal_entry **p = &macro->formals;
529 const char *name;
530
531 idx = sb_skip_white (idx, in);
532 while (idx < in->len)
533 {
534 formal_entry *formal = new_formal ();
535 size_t cidx;
536
537 idx = get_token (idx, in, &formal->name);
538 if (formal->name.len == 0)
539 {
540 if (macro->formal_count)
541 --idx;
542 del_formal (formal); /* 'formal' goes out of scope. */
543 break;
544 }
545 idx = sb_skip_white (idx, in);
546 /* This is a formal. */
547 name = sb_terminate (&formal->name);
548 if (! macro_mri
549 && idx < in->len
550 && in->ptr[idx] == ':'
551 && (! is_name_beginner (':')
552 || idx + 1 >= in->len
553 || ! is_part_of_name (in->ptr[idx + 1])))
554 {
555 /* Got a qualifier. */
556 sb qual;
557
558 sb_new (&qual);
559 idx = get_token (sb_skip_white (idx + 1, in), in, &qual);
560 sb_terminate (&qual);
561 if (qual.len == 0)
562 as_bad_where (macro->file,
563 macro->line,
564 _("Missing parameter qualifier for `%s' in macro `%s'"),
565 name,
566 macro->name);
567 else if (strcmp (qual.ptr, "req") == 0)
568 formal->type = FORMAL_REQUIRED;
569 else if (strcmp (qual.ptr, "vararg") == 0)
570 formal->type = FORMAL_VARARG;
571 else
572 as_bad_where (macro->file,
573 macro->line,
574 _("`%s' is not a valid parameter qualifier for `%s' in macro `%s'"),
575 qual.ptr,
576 name,
577 macro->name);
578 sb_kill (&qual);
579 idx = sb_skip_white (idx, in);
580 }
581 if (idx < in->len && in->ptr[idx] == '=')
582 {
583 /* Got a default. */
584 idx = get_any_string (idx + 1, in, &formal->def);
585 idx = sb_skip_white (idx, in);
586 if (formal->type == FORMAL_REQUIRED)
587 {
588 sb_reset (&formal->def);
589 as_warn_where (macro->file,
590 macro->line,
591 _("Pointless default value for required parameter `%s' in macro `%s'"),
592 name,
593 macro->name);
594 }
595 }
596
597 /* Add to macro's hash table. */
598 if (str_hash_insert (macro->formal_hash, name, formal, 0) != NULL)
599 {
600 as_bad_where (macro->file, macro->line,
601 _("A parameter named `%s' "
602 "already exists for macro `%s'"),
603 name, macro->name);
604 }
605
606 formal->index = macro->formal_count++;
607 *p = formal;
608 p = &formal->next;
609 if (formal->type == FORMAL_VARARG)
610 break;
611 cidx = idx;
612 idx = sb_skip_comma (idx, in);
613 if (idx != cidx && idx >= in->len)
614 {
615 idx = cidx;
616 break;
617 }
618 }
619
620 if (macro_mri)
621 {
622 formal_entry *formal = new_formal ();
623
624 /* Add a special NARG formal, which macro_expand will set to the
625 number of arguments. */
626 /* The same MRI assemblers which treat '@' characters also use
627 the name $NARG. At least until we find an exception. */
628 if (macro_strip_at)
629 name = "$NARG";
630 else
631 name = "NARG";
632
633 sb_add_string (&formal->name, name);
634
635 /* Add to macro's hash table. */
636 if (str_hash_insert (macro->formal_hash, name, formal, 0) != NULL)
637 {
638 as_bad_where (macro->file, macro->line,
639 _("Reserved word `%s' used as parameter in macro `%s'"),
640 name, macro->name);
641 }
642
643 formal->index = NARG_INDEX;
644 *p = formal;
645 }
646
647 return idx;
648 }
649
650 /* Free the memory allocated to a macro. */
651
652 static void
653 free_macro (macro_entry *macro)
654 {
655 formal_entry *formal;
656
657 for (formal = macro->formals; formal; )
658 {
659 formal_entry *f;
660
661 f = formal;
662 formal = formal->next;
663 del_formal (f);
664 }
665 htab_delete (macro->formal_hash);
666 sb_kill (&macro->sub);
667 free (macro);
668 }
669
670 /* Define a new macro. Returns NULL on success, otherwise returns an
671 error message. If NAMEP is not NULL, *NAMEP is set to the name of
672 the macro which was defined. */
673
674 const char *
675 define_macro (size_t idx, sb *in, sb *label,
676 size_t (*get_line) (sb *),
677 const char *file, unsigned int line,
678 const char **namep)
679 {
680 macro_entry *macro;
681 sb name;
682 const char *error = NULL;
683
684 macro = XNEW (macro_entry);
685 sb_new (&macro->sub);
686 sb_new (&name);
687 macro->file = file;
688 macro->line = line;
689
690 macro->formal_count = 0;
691 macro->formals = 0;
692 macro->formal_hash = str_htab_create ();
693
694 idx = sb_skip_white (idx, in);
695 if (! buffer_and_nest ("MACRO", "ENDM", &macro->sub, get_line))
696 error = _("unexpected end of file in macro `%s' definition");
697 if (label != NULL && label->len != 0)
698 {
699 sb_add_sb (&name, label);
700 macro->name = sb_terminate (&name);
701 if (idx < in->len && in->ptr[idx] == '(')
702 {
703 /* It's the label: MACRO (formals,...) sort */
704 idx = do_formals (macro, idx + 1, in);
705 if (idx < in->len && in->ptr[idx] == ')')
706 idx = sb_skip_white (idx + 1, in);
707 else if (!error)
708 error = _("missing `)' after formals in macro definition `%s'");
709 }
710 else
711 {
712 /* It's the label: MACRO formals,... sort */
713 idx = do_formals (macro, idx, in);
714 }
715 }
716 else
717 {
718 size_t cidx;
719
720 idx = get_token (idx, in, &name);
721 macro->name = sb_terminate (&name);
722 if (name.len == 0)
723 error = _("Missing macro name");
724 cidx = sb_skip_white (idx, in);
725 idx = sb_skip_comma (cidx, in);
726 if (idx == cidx || idx < in->len)
727 idx = do_formals (macro, idx, in);
728 else
729 idx = cidx;
730 }
731 if (!error && idx < in->len)
732 error = _("Bad parameter list for macro `%s'");
733
734 /* And stick it in the macro hash table. */
735 for (idx = 0; idx < name.len; idx++)
736 name.ptr[idx] = TOLOWER (name.ptr[idx]);
737 if (!error)
738 {
739 if (str_hash_insert (macro_hash, macro->name, macro, 0) != NULL)
740 error = _("Macro `%s' was already defined");
741 }
742
743 if (namep != NULL)
744 *namep = macro->name;
745
746 if (!error)
747 macro_defined = 1;
748 else
749 free_macro (macro);
750
751 return error;
752 }
753
754 /* Scan a token, and then skip KIND. */
755
756 static size_t
757 get_apost_token (size_t idx, sb *in, sb *name, int kind)
758 {
759 idx = get_token (idx, in, name);
760 if (idx < in->len
761 && in->ptr[idx] == kind
762 && (! macro_mri || macro_strip_at)
763 && (! macro_strip_at || kind == '@'))
764 idx++;
765 return idx;
766 }
767
768 /* Substitute the actual value for a formal parameter. */
769
770 static size_t
771 sub_actual (size_t start, sb *in, sb *t, struct htab *formal_hash,
772 int kind, sb *out, int copyifnotthere)
773 {
774 size_t src;
775 formal_entry *ptr;
776
777 src = get_apost_token (start, in, t, kind);
778 /* See if it's in the macro's hash table, unless this is
779 macro_strip_at and kind is '@' and the token did not end in '@'. */
780 if (macro_strip_at
781 && kind == '@'
782 && (src == start || in->ptr[src - 1] != '@'))
783 ptr = NULL;
784 else
785 ptr = str_hash_find (formal_hash, sb_terminate (t));
786 if (ptr)
787 {
788 if (ptr->actual.len)
789 {
790 sb_add_sb (out, &ptr->actual);
791 }
792 else
793 {
794 sb_add_sb (out, &ptr->def);
795 }
796 }
797 else if (kind == '&')
798 {
799 /* Doing this permits people to use & in macro bodies. */
800 sb_add_char (out, '&');
801 sb_add_sb (out, t);
802 if (src != start && in->ptr[src - 1] == '&')
803 sb_add_char (out, '&');
804 }
805 else if (copyifnotthere)
806 {
807 sb_add_sb (out, t);
808 }
809 else
810 {
811 sb_add_char (out, '\\');
812 sb_add_sb (out, t);
813 }
814 return src;
815 }
816
817 /* Expand the body of a macro. */
818
819 static const char *
820 macro_expand_body (sb *in, sb *out, formal_entry *formals,
821 struct htab *formal_hash, const macro_entry *macro)
822 {
823 sb t;
824 size_t src = 0;
825 int inquote = 0, macro_line = 0;
826 formal_entry *loclist = NULL;
827 const char *err = NULL;
828
829 sb_new (&t);
830
831 while (src < in->len && !err)
832 {
833 if (in->ptr[src] == '&')
834 {
835 sb_reset (&t);
836 if (macro_mri)
837 {
838 if (src + 1 < in->len && in->ptr[src + 1] == '&')
839 src = sub_actual (src + 2, in, &t, formal_hash, '\'', out, 1);
840 else
841 sb_add_char (out, in->ptr[src++]);
842 }
843 else
844 {
845 /* Permit macro parameter substitution delineated with
846 an '&' prefix and optional '&' suffix. */
847 src = sub_actual (src + 1, in, &t, formal_hash, '&', out, 0);
848 }
849 }
850 else if (in->ptr[src] == '\\')
851 {
852 src++;
853 if (src < in->len && in->ptr[src] == '(')
854 {
855 /* Sub in till the next ')' literally. */
856 src++;
857 while (src < in->len && in->ptr[src] != ')')
858 {
859 sb_add_char (out, in->ptr[src++]);
860 }
861 if (src < in->len)
862 src++;
863 else if (!macro)
864 err = _("missing `)'");
865 else
866 as_bad_where (macro->file, macro->line + macro_line, _("missing `)'"));
867 }
868 else if (src < in->len && in->ptr[src] == '@')
869 {
870 /* Sub in the macro invocation number. */
871
872 char buffer[12];
873 src++;
874 sprintf (buffer, "%d", macro_number);
875 sb_add_string (out, buffer);
876 }
877 else if (src < in->len && in->ptr[src] == '&')
878 {
879 /* This is a preprocessor variable name, we don't do them
880 here. */
881 sb_add_char (out, '\\');
882 sb_add_char (out, '&');
883 src++;
884 }
885 else if (macro_mri && src < in->len && ISALNUM (in->ptr[src]))
886 {
887 int ind;
888 formal_entry *f;
889
890 if (ISDIGIT (in->ptr[src]))
891 ind = in->ptr[src] - '0';
892 else if (ISUPPER (in->ptr[src]))
893 ind = in->ptr[src] - 'A' + 10;
894 else
895 ind = in->ptr[src] - 'a' + 10;
896 ++src;
897 for (f = formals; f != NULL; f = f->next)
898 {
899 if (f->index == ind - 1)
900 {
901 if (f->actual.len != 0)
902 sb_add_sb (out, &f->actual);
903 else
904 sb_add_sb (out, &f->def);
905 break;
906 }
907 }
908 }
909 else
910 {
911 sb_reset (&t);
912 src = sub_actual (src, in, &t, formal_hash, '\'', out, 0);
913 }
914 }
915 else if ((macro_alternate || macro_mri)
916 && is_name_beginner (in->ptr[src])
917 && (! inquote
918 || ! macro_strip_at
919 || (src > 0 && in->ptr[src - 1] == '@')))
920 {
921 if (! macro
922 || src + 5 >= in->len
923 || strncasecmp (in->ptr + src, "LOCAL", 5) != 0
924 || ! ISWHITE (in->ptr[src + 5])
925 /* PR 11507: Skip keyword LOCAL if it is found inside a quoted string. */
926 || inquote)
927 {
928 sb_reset (&t);
929 src = sub_actual (src, in, &t, formal_hash,
930 (macro_strip_at && inquote) ? '@' : '\'',
931 out, 1);
932 }
933 else
934 {
935 src = sb_skip_white (src + 5, in);
936 while (in->ptr[src] != '\n')
937 {
938 const char *name;
939 formal_entry *f = new_formal ();
940
941 src = get_token (src, in, &f->name);
942 name = sb_terminate (&f->name);
943 if (str_hash_insert (formal_hash, name, f, 0) != NULL)
944 {
945 as_bad_where (macro->file, macro->line + macro_line,
946 _("`%s' was already used as parameter "
947 "(or another local) name"), name);
948 del_formal (f);
949 }
950 else
951 {
952 static int loccnt;
953 char buf[20];
954
955 f->index = LOCAL_INDEX;
956 f->next = loclist;
957 loclist = f;
958
959 sprintf (buf, IS_ELF ? ".LL%04x" : "LL%04x", ++loccnt);
960 sb_add_string (&f->actual, buf);
961 }
962
963 src = sb_skip_comma (src, in);
964 }
965 }
966 }
967 else if (in->ptr[src] == '"'
968 || (macro_mri && in->ptr[src] == '\''))
969 {
970 inquote = !inquote;
971 sb_add_char (out, in->ptr[src++]);
972 }
973 else if (in->ptr[src] == '@' && macro_strip_at)
974 {
975 ++src;
976 if (src < in->len
977 && in->ptr[src] == '@')
978 {
979 sb_add_char (out, '@');
980 ++src;
981 }
982 }
983 else if (macro_mri
984 && in->ptr[src] == '='
985 && src + 1 < in->len
986 && in->ptr[src + 1] == '=')
987 {
988 formal_entry *ptr;
989
990 sb_reset (&t);
991 src = get_token (src + 2, in, &t);
992 ptr = str_hash_find (formal_hash, sb_terminate (&t));
993 if (ptr == NULL)
994 {
995 /* FIXME: We should really return a warning string here,
996 but we can't, because the == might be in the MRI
997 comment field, and, since the nature of the MRI
998 comment field depends upon the exact instruction
999 being used, we don't have enough information here to
1000 figure out whether it is or not. Instead, we leave
1001 the == in place, which should cause a syntax error if
1002 it is not in a comment. */
1003 sb_add_char (out, '=');
1004 sb_add_char (out, '=');
1005 sb_add_sb (out, &t);
1006 }
1007 else
1008 {
1009 if (ptr->actual.len)
1010 {
1011 sb_add_string (out, "-1");
1012 }
1013 else
1014 {
1015 sb_add_char (out, '0');
1016 }
1017 }
1018 }
1019 else
1020 {
1021 if (in->ptr[src] == '\n')
1022 ++macro_line;
1023 sb_add_char (out, in->ptr[src++]);
1024 }
1025 }
1026
1027 sb_kill (&t);
1028
1029 while (loclist != NULL)
1030 {
1031 formal_entry *f;
1032 const char *name;
1033
1034 f = loclist->next;
1035 name = sb_terminate (&loclist->name);
1036 str_hash_delete (formal_hash, name);
1037 del_formal (loclist);
1038 loclist = f;
1039 }
1040
1041 if (!err && (out->len == 0 || out->ptr[out->len - 1] != '\n'))
1042 sb_add_char (out, '\n');
1043 return err;
1044 }
1045
1046 /* Assign values to the formal parameters of a macro, and expand the
1047 body. */
1048
1049 static const char *
1050 macro_expand (size_t idx, sb *in, macro_entry *m, sb *out)
1051 {
1052 sb t;
1053 formal_entry *ptr;
1054 formal_entry *f;
1055 int is_keyword = 0;
1056 int narg = 0;
1057 const char *err = NULL;
1058
1059 sb_new (&t);
1060
1061 /* Reset any old value the actuals may have. */
1062 for (f = m->formals; f; f = f->next)
1063 sb_reset (&f->actual);
1064 f = m->formals;
1065 while (f != NULL && f->index < 0)
1066 f = f->next;
1067
1068 if (macro_mri)
1069 {
1070 /* The macro may be called with an optional qualifier, which may
1071 be referred to in the macro body as \0. */
1072 if (idx < in->len && in->ptr[idx] == '.')
1073 {
1074 /* The Microtec assembler ignores this if followed by a white space.
1075 (Macro invocation with empty extension) */
1076 idx++;
1077 if ( idx < in->len
1078 && in->ptr[idx] != ' '
1079 && in->ptr[idx] != '\t')
1080 {
1081 formal_entry *n = new_formal ();
1082
1083 n->index = QUAL_INDEX;
1084
1085 n->next = m->formals;
1086 m->formals = n;
1087
1088 idx = get_any_string (idx, in, &n->actual);
1089 }
1090 }
1091 }
1092
1093 /* Peel off the actuals and store them away in the hash tables' actuals. */
1094 idx = sb_skip_white (idx, in);
1095 while (idx < in->len)
1096 {
1097 size_t scan;
1098
1099 /* Look and see if it's a positional or keyword arg. */
1100 scan = idx;
1101 while (scan < in->len
1102 && !ISSEP (in->ptr[scan])
1103 && !(macro_mri && in->ptr[scan] == '\'')
1104 && (!macro_alternate && in->ptr[scan] != '='))
1105 scan++;
1106 if (scan < in->len && !macro_alternate && in->ptr[scan] == '=')
1107 {
1108 is_keyword = 1;
1109
1110 /* It's OK to go from positional to keyword. */
1111
1112 /* This is a keyword arg, fetch the formal name and
1113 then the actual stuff. */
1114 sb_reset (&t);
1115 idx = get_token (idx, in, &t);
1116 if (in->ptr[idx] != '=')
1117 {
1118 err = _("confusion in formal parameters");
1119 break;
1120 }
1121
1122 /* Lookup the formal in the macro's list. */
1123 ptr = str_hash_find (m->formal_hash, sb_terminate (&t));
1124 if (!ptr)
1125 {
1126 as_bad (_("Parameter named `%s' does not exist for macro `%s'"),
1127 t.ptr,
1128 m->name);
1129 sb_reset (&t);
1130 idx = get_any_string (idx + 1, in, &t);
1131 }
1132 else
1133 {
1134 /* Insert this value into the right place. */
1135 if (ptr->actual.len)
1136 {
1137 as_warn (_("Value for parameter `%s' of macro `%s' was already specified"),
1138 ptr->name.ptr,
1139 m->name);
1140 sb_reset (&ptr->actual);
1141 }
1142 idx = get_any_string (idx + 1, in, &ptr->actual);
1143 if (ptr->actual.len > 0)
1144 ++narg;
1145 }
1146 }
1147 else
1148 {
1149 if (is_keyword)
1150 {
1151 err = _("can't mix positional and keyword arguments");
1152 break;
1153 }
1154
1155 if (!f)
1156 {
1157 formal_entry **pf;
1158 int c;
1159
1160 if (!macro_mri)
1161 {
1162 err = _("too many positional arguments");
1163 break;
1164 }
1165
1166 f = new_formal ();
1167
1168 c = -1;
1169 for (pf = &m->formals; *pf != NULL; pf = &(*pf)->next)
1170 if ((*pf)->index >= c)
1171 c = (*pf)->index + 1;
1172 if (c == -1)
1173 c = 0;
1174 *pf = f;
1175 f->index = c;
1176 }
1177
1178 if (f->type != FORMAL_VARARG)
1179 idx = get_any_string (idx, in, &f->actual);
1180 else
1181 {
1182 sb_add_buffer (&f->actual, in->ptr + idx, in->len - idx);
1183 idx = in->len;
1184 }
1185 if (f->actual.len > 0)
1186 ++narg;
1187 do
1188 {
1189 f = f->next;
1190 }
1191 while (f != NULL && f->index < 0);
1192 }
1193
1194 if (! macro_mri)
1195 idx = sb_skip_comma (idx, in);
1196 else
1197 {
1198 if (in->ptr[idx] == ',')
1199 ++idx;
1200 if (ISWHITE (in->ptr[idx]))
1201 break;
1202 }
1203 }
1204
1205 if (! err)
1206 {
1207 for (ptr = m->formals; ptr; ptr = ptr->next)
1208 {
1209 if (ptr->type == FORMAL_REQUIRED && ptr->actual.len == 0)
1210 as_bad (_("Missing value for required parameter `%s' of macro `%s'"),
1211 ptr->name.ptr,
1212 m->name);
1213 }
1214
1215 if (macro_mri)
1216 {
1217 char buffer[20];
1218
1219 sb_reset (&t);
1220 sb_add_string (&t, macro_strip_at ? "$NARG" : "NARG");
1221 ptr = str_hash_find (m->formal_hash, sb_terminate (&t));
1222 sprintf (buffer, "%d", narg);
1223 sb_add_string (&ptr->actual, buffer);
1224 }
1225
1226 err = macro_expand_body (&m->sub, out, m->formals, m->formal_hash, m);
1227 }
1228
1229 /* Discard any unnamed formal arguments. */
1230 if (macro_mri)
1231 {
1232 formal_entry **pf;
1233
1234 pf = &m->formals;
1235 while (*pf != NULL)
1236 {
1237 if ((*pf)->name.len != 0)
1238 pf = &(*pf)->next;
1239 else
1240 {
1241 f = (*pf)->next;
1242 del_formal (*pf);
1243 *pf = f;
1244 }
1245 }
1246 }
1247
1248 sb_kill (&t);
1249 if (!err)
1250 macro_number++;
1251
1252 return err;
1253 }
1254
1255 /* Check for a macro. If one is found, put the expansion into
1256 *EXPAND. Return 1 if a macro is found, 0 otherwise. */
1257
1258 int
1259 check_macro (const char *line, sb *expand,
1260 const char **error, macro_entry **info)
1261 {
1262 const char *s;
1263 char *copy, *cls;
1264 macro_entry *macro;
1265 sb line_sb;
1266
1267 if (! is_name_beginner (*line)
1268 && (! macro_mri || *line != '.'))
1269 return 0;
1270
1271 s = line + 1;
1272 while (is_part_of_name (*s))
1273 ++s;
1274 if (is_name_ender (*s))
1275 ++s;
1276
1277 copy = xmemdup0 (line, s - line);
1278 for (cls = copy; *cls != '\0'; cls ++)
1279 *cls = TOLOWER (*cls);
1280
1281 macro = str_hash_find (macro_hash, copy);
1282 free (copy);
1283
1284 if (macro == NULL)
1285 return 0;
1286
1287 /* Wrap the line up in an sb. */
1288 sb_new (&line_sb);
1289 while (*s != '\0' && *s != '\n' && *s != '\r')
1290 sb_add_char (&line_sb, *s++);
1291
1292 sb_new (expand);
1293 *error = macro_expand (0, &line_sb, macro, expand);
1294
1295 sb_kill (&line_sb);
1296
1297 /* Export the macro information if requested. */
1298 if (info)
1299 *info = macro;
1300
1301 return 1;
1302 }
1303
1304 /* Delete a macro. */
1305
1306 void
1307 delete_macro (const char *name)
1308 {
1309 char *copy;
1310 size_t i, len;
1311 macro_entry *macro;
1312
1313 len = strlen (name);
1314 copy = XNEWVEC (char, len + 1);
1315 for (i = 0; i < len; ++i)
1316 copy[i] = TOLOWER (name[i]);
1317 copy[i] = '\0';
1318
1319 macro = str_hash_find (macro_hash, copy);
1320 if (macro != NULL)
1321 {
1322 free_macro (macro);
1323 str_hash_delete (macro_hash, copy);
1324 }
1325 else
1326 as_warn (_("Attempt to purge non-existing macro `%s'"), copy);
1327 free (copy);
1328 }
1329
1330 /* Handle the MRI IRP and IRPC pseudo-ops. These are handled as a
1331 combined macro definition and execution. This returns NULL on
1332 success, or an error message otherwise. */
1333
1334 const char *
1335 expand_irp (int irpc, size_t idx, sb *in, sb *out, size_t (*get_line) (sb *))
1336 {
1337 sb sub;
1338 formal_entry f;
1339 struct htab *h;
1340 const char *err = NULL;
1341
1342 idx = sb_skip_white (idx, in);
1343
1344 sb_new (&sub);
1345 if (! buffer_and_nest (NULL, "ENDR", &sub, get_line))
1346 return _("unexpected end of file in irp or irpc");
1347
1348 sb_new (&f.name);
1349 sb_new (&f.def);
1350 sb_new (&f.actual);
1351
1352 idx = get_token (idx, in, &f.name);
1353 if (f.name.len == 0)
1354 return _("missing model parameter");
1355
1356 h = str_htab_create ();
1357
1358 str_hash_insert (h, sb_terminate (&f.name), &f, 0);
1359
1360 f.index = 1;
1361 f.next = NULL;
1362 f.type = FORMAL_OPTIONAL;
1363
1364 sb_reset (out);
1365
1366 idx = sb_skip_comma (idx, in);
1367 if (idx >= in->len)
1368 {
1369 /* Expand once with a null string. */
1370 err = macro_expand_body (&sub, out, &f, h, 0);
1371 }
1372 else
1373 {
1374 bool in_quotes = false;
1375
1376 if (irpc && in->ptr[idx] == '"')
1377 {
1378 in_quotes = true;
1379 ++idx;
1380 }
1381
1382 while (idx < in->len)
1383 {
1384 if (!irpc)
1385 idx = get_any_string (idx, in, &f.actual);
1386 else
1387 {
1388 if (in->ptr[idx] == '"')
1389 {
1390 size_t nxt;
1391
1392 if (irpc)
1393 in_quotes = ! in_quotes;
1394
1395 nxt = sb_skip_white (idx + 1, in);
1396 if (nxt >= in->len)
1397 {
1398 idx = nxt;
1399 break;
1400 }
1401 }
1402 sb_reset (&f.actual);
1403 sb_add_char (&f.actual, in->ptr[idx]);
1404 ++idx;
1405 }
1406
1407 err = macro_expand_body (&sub, out, &f, h, 0);
1408 if (err != NULL)
1409 break;
1410 if (!irpc)
1411 idx = sb_skip_comma (idx, in);
1412 else if (! in_quotes)
1413 idx = sb_skip_white (idx, in);
1414 }
1415 }
1416
1417 htab_delete (h);
1418 sb_kill (&f.actual);
1419 sb_kill (&f.def);
1420 sb_kill (&f.name);
1421 sb_kill (&sub);
1422
1423 return err;
1424 }