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