PageRenderTime 66ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/freebsd/contrib/binutils/gas/macro.c

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