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