macro.c 32 KB

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