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