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