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