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