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