gas-preproc.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. /*
  2. * GAS preprocessor (emulates GNU Assembler's preprocessor)
  3. *
  4. * Copyright (C) 2009 Alexei Svitkine
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <util.h>
  28. #include <ctype.h>
  29. #include <libyasm.h>
  30. #include "modules/preprocs/gas/gas-eval.h"
  31. #define FALSE 0
  32. #define TRUE 1
  33. #define BSIZE 512
  34. #ifndef MAXPATHLEN
  35. #define MAXPATHLEN 1024
  36. #endif
  37. typedef struct buffered_line {
  38. char *line;
  39. int line_number;
  40. SLIST_ENTRY(buffered_line) next;
  41. } buffered_line;
  42. typedef struct included_file {
  43. char *filename;
  44. int lines_remaining;
  45. SLIST_ENTRY(included_file) next;
  46. } included_file;
  47. typedef struct macro_entry {
  48. char *name;
  49. int num_params;
  50. char **params;
  51. int num_lines;
  52. char **lines;
  53. STAILQ_ENTRY(macro_entry) next;
  54. } macro_entry;
  55. typedef struct deferred_define {
  56. char *name;
  57. char *value;
  58. SLIST_ENTRY(deferred_define) next;
  59. } deferred_define;
  60. typedef struct expr_state {
  61. const char *string;
  62. char *symbol;
  63. int string_cursor;
  64. } expr_state;
  65. typedef struct yasm_preproc_gas {
  66. yasm_preproc_base preproc; /* base structure */
  67. FILE *in;
  68. char *in_filename;
  69. yasm_symtab *defines;
  70. SLIST_HEAD(deferred_defines_head, deferred_define) deferred_defines;
  71. int depth;
  72. int skip_depth;
  73. int in_comment;
  74. expr_state expr;
  75. SLIST_HEAD(buffered_lines_head, buffered_line) buffered_lines;
  76. SLIST_HEAD(included_files_head, included_file) included_files;
  77. STAILQ_HEAD(macros_head, macro_entry) macros;
  78. int in_line_number;
  79. int next_line_number;
  80. int current_line_number; /* virtual (output) line number */
  81. yasm_linemap *cur_lm;
  82. yasm_errwarns *errwarns;
  83. int fatal_error;
  84. int detect_errors_only;
  85. } yasm_preproc_gas;
  86. yasm_preproc_module yasm_gas_LTX_preproc;
  87. /* Forward declarations. */
  88. static int substitute_values(yasm_preproc_gas *pp, char **line_ptr);
  89. /* String helpers. */
  90. static const char *starts_with(const char *big, const char *little)
  91. {
  92. while (*little) {
  93. if (*little++ != *big++) {
  94. return NULL;
  95. }
  96. }
  97. return big;
  98. }
  99. static void skip_whitespace(const char **line)
  100. {
  101. while (isspace(**line)) {
  102. (*line)++;
  103. }
  104. }
  105. static void skip_whitespace2(char **line)
  106. {
  107. while (isspace(**line)) {
  108. (*line)++;
  109. }
  110. }
  111. static const char *matches(const char *line, const char *directive)
  112. {
  113. skip_whitespace(&line);
  114. if (*line == '.') {
  115. line = starts_with(line + 1, directive);
  116. if (line && (!*line || isspace(*line))) {
  117. skip_whitespace(&line);
  118. return line;
  119. }
  120. }
  121. return NULL;
  122. }
  123. static int unquote(const char *arg, char *to, size_t to_size, char q, char expected, const char **remainder)
  124. {
  125. const char *quote;
  126. const char *end;
  127. size_t len;
  128. skip_whitespace(&arg);
  129. if (*arg != q) {
  130. return -1;
  131. }
  132. arg++;
  133. end = arg;
  134. do {
  135. quote = strchr(end, q);
  136. if (!quote) {
  137. return -2;
  138. }
  139. end = quote + 1;
  140. } while (*(quote - 1) == '\\');
  141. skip_whitespace(&end);
  142. if (*end != expected) {
  143. return -3;
  144. }
  145. if (remainder) {
  146. *remainder = end + 1;
  147. }
  148. len = (size_t) (quote - arg);
  149. if (len >= to_size) {
  150. return -4;
  151. }
  152. strncpy(to, arg, len);
  153. to[len] = '\0';
  154. return (int) len;
  155. }
  156. /* Line-reading. */
  157. static char *read_line_from_file(yasm_preproc_gas *pp, FILE *file)
  158. {
  159. int bufsize = BSIZE;
  160. char *buf;
  161. char *p;
  162. buf = yasm_xmalloc((size_t)bufsize);
  163. /* Loop to ensure entire line is read (don't want to limit line length). */
  164. p = buf;
  165. for (;;) {
  166. if (!fgets(p, bufsize - (p - buf), file)) {
  167. if (ferror(file)) {
  168. yasm_error_set(YASM_ERROR_IO, N_("error when reading from file"));
  169. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  170. }
  171. break;
  172. }
  173. p += strlen(p);
  174. if (p > buf && p[-1] == '\n') {
  175. break;
  176. }
  177. if ((p - buf) + 1 >= bufsize) {
  178. /* Increase size of buffer */
  179. char *oldbuf = buf;
  180. bufsize *= 2;
  181. buf = yasm_xrealloc(buf, (size_t) bufsize);
  182. p = buf + (p - oldbuf);
  183. }
  184. }
  185. if (p == buf) {
  186. /* No data; must be at EOF */
  187. yasm_xfree(buf);
  188. return NULL;
  189. }
  190. /* Strip the line ending */
  191. buf[strcspn(buf, "\r\n")] = '\0';
  192. return buf;
  193. }
  194. static char *read_line(yasm_preproc_gas *pp)
  195. {
  196. char *line;
  197. if (!SLIST_EMPTY(&pp->included_files)) {
  198. included_file *inc_file = SLIST_FIRST(&pp->included_files);
  199. if (inc_file->lines_remaining <= 0) {
  200. SLIST_REMOVE_HEAD(&pp->included_files, next);
  201. yasm_xfree(inc_file->filename);
  202. yasm_xfree(inc_file);
  203. }
  204. }
  205. if (!SLIST_EMPTY(&pp->buffered_lines)) {
  206. buffered_line *bline = SLIST_FIRST(&pp->buffered_lines);
  207. SLIST_REMOVE_HEAD(&pp->buffered_lines, next);
  208. line = bline->line;
  209. if (bline->line_number != -1) {
  210. pp->next_line_number = bline->line_number;
  211. }
  212. yasm_xfree(bline);
  213. if (!SLIST_EMPTY(&pp->included_files)) {
  214. SLIST_FIRST(&pp->included_files)->lines_remaining--;
  215. }
  216. return line;
  217. }
  218. line = read_line_from_file(pp, pp->in);
  219. if (line) {
  220. pp->in_line_number++;
  221. pp->next_line_number = pp->in_line_number;
  222. }
  223. return line;
  224. }
  225. static const char *get_arg(yasm_preproc_gas *pp, const char *src, char *dest, size_t dest_size)
  226. {
  227. const char *comma = strchr(src, ',');
  228. if (comma) {
  229. size_t len = (size_t) (comma - src);
  230. if (len >= dest_size) {
  231. len = dest_size - 1;
  232. }
  233. strncpy(dest, src, len);
  234. dest[len] = '\0';
  235. comma++;
  236. skip_whitespace(&comma);
  237. } else {
  238. yasm_error_set(YASM_ERROR_SYNTAX, N_("expected comma"));
  239. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  240. }
  241. return comma;
  242. }
  243. /* GAS expression evaluation. */
  244. static char get_char(yasm_preproc_gas *pp)
  245. {
  246. return pp->expr.string[pp->expr.string_cursor];
  247. }
  248. static const char *get_str(yasm_preproc_gas *pp)
  249. {
  250. return pp->expr.string + pp->expr.string_cursor;
  251. }
  252. static void next_char(yasm_preproc_gas *pp)
  253. {
  254. pp->expr.string_cursor++;
  255. }
  256. static int ishex(char c)
  257. {
  258. c = tolower(c);
  259. return isdigit(c) || (c >= 'a' && c <= 'f');
  260. }
  261. static void gas_scan_init(yasm_preproc_gas *pp, struct tokenval *tokval, const char *arg1)
  262. {
  263. pp->expr.symbol = NULL;
  264. pp->expr.string = arg1;
  265. pp->expr.string_cursor = 0;
  266. memset(tokval, 0, sizeof(struct tokenval));
  267. tokval->t_type = TOKEN_INVALID;
  268. }
  269. static void gas_scan_cleanup(yasm_preproc_gas *pp, struct tokenval *tokval)
  270. {
  271. if (tokval->t_integer) {
  272. yasm_intnum_destroy(tokval->t_integer);
  273. tokval->t_integer = NULL;
  274. }
  275. if (pp->expr.symbol) {
  276. yasm_xfree(pp->expr.symbol);
  277. pp->expr.symbol = NULL;
  278. }
  279. }
  280. static int gas_scan(void *preproc, struct tokenval *tokval)
  281. {
  282. yasm_preproc_gas *pp = (yasm_preproc_gas *) preproc;
  283. char c = get_char(pp);
  284. const char *str;
  285. tokval->t_charptr = NULL;
  286. if (c == '\0') {
  287. return tokval->t_type = TOKEN_EOS;
  288. }
  289. if (isspace(c)) {
  290. do {
  291. next_char(pp);
  292. c = get_char(pp);
  293. } while (isspace(c));
  294. }
  295. if (isdigit(c)) {
  296. int char_index = 0;
  297. int value = 0;
  298. do {
  299. value = value*10 + (c - '0');
  300. char_index++;
  301. next_char(pp);
  302. c = get_char(pp);
  303. if (char_index == 1 && c == 'x' && value == 0) {
  304. next_char(pp);
  305. c = get_char(pp);
  306. /* Hex notation. */
  307. while (ishex(c)) {
  308. if (isdigit(c)) {
  309. value = (value << 4) | (c - '0');
  310. } else {
  311. value = (value << 4) | (tolower(c) - 'a' + 0xa);
  312. }
  313. next_char(pp);
  314. c = get_char(pp);
  315. }
  316. break;
  317. }
  318. } while (isdigit(c));
  319. if (tokval->t_integer) {
  320. yasm_intnum_destroy(tokval->t_integer);
  321. }
  322. tokval->t_integer = yasm_intnum_create_int(value);
  323. return tokval->t_type = TOKEN_NUM;
  324. }
  325. tokval->t_type = TOKEN_INVALID;
  326. str = get_str(pp);
  327. {
  328. /* It should be tested whether GAS supports all of these or if there are missing ones. */
  329. unsigned i;
  330. struct {
  331. const char *op;
  332. int token;
  333. } ops[] = {
  334. { "<<", TOKEN_SHL },
  335. { ">>", TOKEN_SHR },
  336. { "//", TOKEN_SDIV },
  337. { "%%", TOKEN_SMOD },
  338. { "==", TOKEN_EQ },
  339. { "!=", TOKEN_NE },
  340. { "<>", TOKEN_NE },
  341. { "<>", TOKEN_NE },
  342. { "<=", TOKEN_LE },
  343. { ">=", TOKEN_GE },
  344. { "&&", TOKEN_DBL_AND },
  345. { "^^", TOKEN_DBL_XOR },
  346. { "||", TOKEN_DBL_OR }
  347. };
  348. for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
  349. if (!strcmp(str, ops[i].op)) {
  350. tokval->t_type = ops[i].token;
  351. break;
  352. }
  353. }
  354. }
  355. if (tokval->t_type != TOKEN_INVALID) {
  356. next_char(pp);
  357. next_char(pp);
  358. } else {
  359. str = get_str(pp);
  360. next_char(pp);
  361. tokval->t_type = c;
  362. /* Is it a symbol? If so we need to make it a TOKEN_ID. */
  363. if (isalpha(c) || c == '_' || c == '.') {
  364. int symbol_length = 1;
  365. c = get_char(pp);
  366. while (isalnum(c) || c == '$' || c == '_') {
  367. symbol_length++;
  368. next_char(pp);
  369. c = get_char(pp);
  370. }
  371. pp->expr.symbol = yasm_xrealloc(pp->expr.symbol, symbol_length + 1);
  372. memcpy(pp->expr.symbol, str, symbol_length);
  373. pp->expr.symbol[symbol_length] = '\0';
  374. tokval->t_type = TOKEN_ID;
  375. tokval->t_charptr = pp->expr.symbol;
  376. }
  377. }
  378. return tokval->t_type;
  379. }
  380. static void gas_err(void *private_data, int severity, const char *fmt, ...)
  381. {
  382. va_list args;
  383. yasm_preproc_gas *pp = private_data;
  384. if (!pp->detect_errors_only) {
  385. va_start(args, fmt);
  386. yasm_error_set_va(YASM_ERROR_SYNTAX, N_(fmt), args);
  387. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  388. va_end(args);
  389. }
  390. pp->fatal_error = 1;
  391. }
  392. static long eval_expr(yasm_preproc_gas *pp, const char *arg1)
  393. {
  394. struct tokenval tv;
  395. yasm_expr *expr;
  396. yasm_intnum *intn;
  397. long value;
  398. expr_state prev_state;
  399. if (!*arg1) {
  400. return 0;
  401. }
  402. prev_state = pp->expr;
  403. gas_scan_init(pp, &tv, arg1);
  404. expr = evaluate(gas_scan, pp, &tv, pp, CRITICAL, gas_err, pp->defines);
  405. intn = yasm_expr_get_intnum(&expr, 0);
  406. value = yasm_intnum_get_int(intn);
  407. yasm_expr_destroy(expr);
  408. gas_scan_cleanup(pp, &tv);
  409. pp->expr = prev_state;
  410. return value;
  411. }
  412. /* If-directive helpers. */
  413. static int handle_if(yasm_preproc_gas *pp, int is_true)
  414. {
  415. assert(pp->depth >= 0);
  416. assert(pp->skip_depth == 0);
  417. if (is_true) {
  418. pp->depth++;
  419. } else {
  420. pp->skip_depth = 1;
  421. }
  422. return 1;
  423. }
  424. static int handle_endif(yasm_preproc_gas *pp)
  425. {
  426. if (pp->depth) {
  427. pp->depth--;
  428. } else {
  429. yasm_error_set(YASM_ERROR_SYNTAX, N_("\".endif\" without \".if\""));
  430. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  431. return 0;
  432. }
  433. return 1;
  434. }
  435. static int handle_else(yasm_preproc_gas *pp, int is_elseif)
  436. {
  437. if (!pp->depth) {
  438. yasm_error_set(YASM_ERROR_SYNTAX, N_("\".%s\" without \".if\""), is_elseif ? "elseif" : "else");
  439. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  440. return 0;
  441. } else {
  442. pp->skip_depth = 1;
  443. }
  444. return 1;
  445. }
  446. /* Directive-handling functions. */
  447. static int eval_if(yasm_preproc_gas *pp, int negate, const char *arg1)
  448. {
  449. long value;
  450. if (!*arg1) {
  451. yasm_error_set(YASM_ERROR_SYNTAX, N_("expression is required in \".if\" statement"));
  452. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  453. return 0;
  454. }
  455. value = eval_expr(pp, arg1);
  456. handle_if(pp, (negate ? !value : !!value));
  457. return 1;
  458. }
  459. static int eval_else(yasm_preproc_gas *pp, int unused)
  460. {
  461. return handle_else(pp, 0);
  462. }
  463. static int eval_endif(yasm_preproc_gas *pp, int unused)
  464. {
  465. return handle_endif(pp);
  466. }
  467. static int eval_elseif(yasm_preproc_gas *pp, int unused, const char *arg1)
  468. {
  469. if (!*arg1) {
  470. yasm_error_set(YASM_ERROR_SYNTAX, N_("expression is required in \".elseif\" statement"));
  471. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  472. return 0;
  473. }
  474. if (!handle_else(pp, 1)) {
  475. return 0;
  476. }
  477. return eval_if(pp, 0, arg1);
  478. }
  479. static int eval_ifb(yasm_preproc_gas *pp, int negate, const char *arg1)
  480. {
  481. int is_blank = !*arg1;
  482. return handle_if(pp, (negate ? !is_blank : is_blank));
  483. }
  484. static int eval_ifc(yasm_preproc_gas *pp, int negate, const char *args)
  485. {
  486. char arg1[512], arg2[512];
  487. const char *remainder;
  488. int len = unquote(args, arg1, sizeof(arg1), '\'', ',', &remainder);
  489. if (len >= 0) {
  490. len = unquote(remainder, arg2, sizeof(arg2), '\'', '\0', NULL);
  491. if (len >= 0) {
  492. int result = !strcmp(arg1, arg2);
  493. return handle_if(pp, (negate ? !result : result));
  494. }
  495. } else {
  496. /* first argument was not single-quoted, assume non-quoted mode */
  497. remainder = get_arg(pp, args, arg1, sizeof(arg1));
  498. if (remainder) {
  499. int result = !strcmp(arg1, remainder);
  500. return handle_if(pp, (negate ? !result : result));
  501. }
  502. }
  503. yasm_error_set(YASM_ERROR_SYNTAX, N_("\"%s\" expects two single-quoted or unquoted arguments"), negate ? ".ifnc" : ".ifc");
  504. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  505. return 0;
  506. }
  507. static int eval_ifeqs(yasm_preproc_gas *pp, int negate, const char *args)
  508. {
  509. char arg1[512], arg2[512];
  510. const char *remainder;
  511. int len = unquote(args, arg1, sizeof(arg1), '"', ',', &remainder);
  512. if (len >= 0) {
  513. len = unquote(remainder, arg2, sizeof(arg2), '"', '\0', NULL);
  514. if (len >= 0) {
  515. int result = !strcmp(arg1, arg2);
  516. return handle_if(pp, (negate ? !result : result));
  517. }
  518. }
  519. yasm_error_set(YASM_ERROR_SYNTAX, N_("\"%s\" expects two double-quoted arguments"), negate ? ".ifnes" : ".ifeqs");
  520. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  521. return 1;
  522. }
  523. static int eval_ifdef(yasm_preproc_gas *pp, int negate, const char *name)
  524. {
  525. yasm_symrec *rec = yasm_symtab_get(pp->defines, name);
  526. int result = (rec != NULL);
  527. return handle_if(pp, (negate ? !result : result));
  528. }
  529. static int eval_ifge(yasm_preproc_gas *pp, int negate, const char *arg1)
  530. {
  531. long value = eval_expr(pp, arg1);
  532. int result = (value >= 0);
  533. return handle_if(pp, (negate ? !result : result));
  534. }
  535. static int eval_ifgt(yasm_preproc_gas *pp, int negate, const char *arg1)
  536. {
  537. long value = eval_expr(pp, arg1);
  538. int result = (value > 0);
  539. return handle_if(pp, (negate ? !result : result));
  540. }
  541. static int eval_include(yasm_preproc_gas *pp, int unused, const char *arg1)
  542. {
  543. char *current_filename;
  544. char filename[MAXPATHLEN];
  545. char *line;
  546. int num_lines;
  547. FILE *file;
  548. buffered_line *prev_bline;
  549. included_file *inc_file;
  550. if (unquote(arg1, filename, sizeof(filename), '"', '\0', NULL) < 0) {
  551. yasm_error_set(YASM_ERROR_SYNTAX, N_("string expected"));
  552. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  553. return 0;
  554. }
  555. if (SLIST_EMPTY(&pp->included_files)) {
  556. current_filename = pp->in_filename;
  557. } else {
  558. current_filename = SLIST_FIRST(&pp->included_files)->filename;
  559. }
  560. file = yasm_fopen_include(filename, current_filename, "r", NULL);
  561. if (!file) {
  562. yasm_error_set(YASM_ERROR_SYNTAX, N_("unable to open included file \"%s\""), filename);
  563. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  564. return 0;
  565. }
  566. num_lines = 0;
  567. prev_bline = NULL;
  568. line = read_line_from_file(pp, file);
  569. while (line) {
  570. buffered_line *bline = yasm_xmalloc(sizeof(buffered_line));
  571. bline->line = line;
  572. bline->line_number = -1;
  573. if (prev_bline) {
  574. SLIST_INSERT_AFTER(prev_bline, bline, next);
  575. } else {
  576. SLIST_INSERT_HEAD(&pp->buffered_lines, bline, next);
  577. }
  578. prev_bline = bline;
  579. line = read_line_from_file(pp, file);
  580. num_lines++;
  581. }
  582. inc_file = yasm_xmalloc(sizeof(included_file));
  583. inc_file->filename = yasm__xstrdup(filename);
  584. inc_file->lines_remaining = num_lines;
  585. SLIST_INSERT_HEAD(&pp->included_files, inc_file, next);
  586. return 1;
  587. }
  588. static int try_eval_expr(yasm_preproc_gas *pp, const char *value, long *result)
  589. {
  590. int success;
  591. pp->detect_errors_only = 1;
  592. *result = eval_expr(pp, value);
  593. success = !pp->fatal_error;
  594. pp->fatal_error = 0;
  595. pp->detect_errors_only = 0;
  596. return success;
  597. }
  598. static int remove_define(yasm_preproc_gas *pp, const char *name, int allow_redefine)
  599. {
  600. yasm_symrec *rec = yasm_symtab_get(pp->defines, name);
  601. if (rec) {
  602. const yasm_symtab_iter *entry;
  603. yasm_symtab *new_defines;
  604. if (!allow_redefine) {
  605. yasm_error_set(YASM_ERROR_SYNTAX, N_("symbol \"%s\" is already defined"), name);
  606. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  607. return 0;
  608. }
  609. new_defines = yasm_symtab_create();
  610. for (entry = yasm_symtab_first(pp->defines); entry; entry = yasm_symtab_next(entry)) {
  611. yasm_symrec *entry_rec = yasm_symtab_iter_value(entry);
  612. const char *rec_name = yasm_symrec_get_name(entry_rec);
  613. if (strcmp(rec_name, name)) {
  614. yasm_intnum *num = yasm_intnum_create_int(eval_expr(pp, rec_name));
  615. yasm_expr *expr = yasm_expr_create_ident(yasm_expr_int(num), 0);
  616. yasm_symtab_define_equ(new_defines, rec_name, expr, 0);
  617. }
  618. }
  619. yasm_symtab_destroy(pp->defines);
  620. pp->defines = new_defines;
  621. }
  622. return (rec != NULL);
  623. }
  624. static void add_define(yasm_preproc_gas *pp, const char *name, long value, int allow_redefine, int substitute)
  625. {
  626. deferred_define *def, *prev_def, *temp_def;
  627. yasm_intnum *num;
  628. yasm_expr *expr;
  629. remove_define(pp, name, allow_redefine);
  630. /* Add the new define. */
  631. num = yasm_intnum_create_int(value);
  632. expr = yasm_expr_create_ident(yasm_expr_int(num), 0);
  633. yasm_symtab_define_equ(pp->defines, name, expr, 0);
  634. /* Perform substitution on any deferred defines. */
  635. if (substitute) {
  636. prev_def = NULL;
  637. temp_def = NULL;
  638. SLIST_FOREACH_SAFE(def, &pp->deferred_defines, next, temp_def) {
  639. if (substitute_values(pp, &def->value)) {
  640. /* Value was updated - check if it can be added to the symtab. */
  641. if (try_eval_expr(pp, def->value, &value)) {
  642. add_define(pp, def->name, value, FALSE, FALSE);
  643. if (prev_def) {
  644. SLIST_NEXT(prev_def, next) = SLIST_NEXT(def, next);
  645. } else {
  646. SLIST_FIRST(&pp->deferred_defines) = SLIST_NEXT(def, next);
  647. }
  648. yasm_xfree(def->name);
  649. yasm_xfree(def->value);
  650. yasm_xfree(def);
  651. continue;
  652. }
  653. }
  654. prev_def = def;
  655. }
  656. }
  657. }
  658. static int eval_set(yasm_preproc_gas *pp, int allow_redefine, const char *name, const char *value)
  659. {
  660. if (!pp->skip_depth) {
  661. long result;
  662. if (!try_eval_expr(pp, value, &result)) {
  663. deferred_define *def;
  664. remove_define(pp, name, allow_redefine);
  665. def = yasm_xmalloc(sizeof(deferred_define));
  666. def->name = yasm__xstrdup(name);
  667. def->value = yasm__xstrdup(value);
  668. substitute_values(pp, &def->value);
  669. SLIST_INSERT_HEAD(&pp->deferred_defines, def, next);
  670. } else {
  671. add_define(pp, name, result, allow_redefine, TRUE);
  672. }
  673. }
  674. return 1;
  675. }
  676. static int eval_macro(yasm_preproc_gas *pp, int unused, char *args)
  677. {
  678. char *end;
  679. char *line;
  680. long nesting = 1;
  681. macro_entry *macro = yasm_xmalloc(sizeof(macro_entry));
  682. memset(macro, 0, sizeof(macro_entry));
  683. end = args;
  684. while (*end && !isspace(*end)) {
  685. end++;
  686. }
  687. macro->name = yasm_xmalloc(end - args + 1);
  688. memcpy(macro->name, args, end - args);
  689. macro->name[end - args] = '\0';
  690. skip_whitespace2(&end);
  691. while (*end) {
  692. args = end;
  693. while (*end && !isspace(*end) && *end != ',') {
  694. end++;
  695. }
  696. macro->num_params++;
  697. macro->params = yasm_xrealloc(macro->params, macro->num_params*sizeof(char *));
  698. macro->params[macro->num_params - 1] = yasm_xmalloc(end - args + 1);
  699. memcpy(macro->params[macro->num_params - 1], args, end - args);
  700. macro->params[macro->num_params - 1][end - args] = '\0';
  701. skip_whitespace2(&end);
  702. if (*end == ',') {
  703. end++;
  704. skip_whitespace2(&end);
  705. }
  706. }
  707. STAILQ_INSERT_TAIL(&pp->macros, macro, next);
  708. line = read_line(pp);
  709. while (line) {
  710. char *line2 = line;
  711. skip_whitespace2(&line2);
  712. if (starts_with(line2, ".macro")) {
  713. nesting++;
  714. } else if (starts_with(line, ".endm") && --nesting == 0) {
  715. return 1;
  716. }
  717. macro->num_lines++;
  718. macro->lines = yasm_xrealloc(macro->lines, macro->num_lines*sizeof(char *));
  719. macro->lines[macro->num_lines - 1] = line;
  720. line = read_line(pp);
  721. }
  722. yasm_error_set(YASM_ERROR_SYNTAX, N_("unexpected EOF in \".macro\" block"));
  723. yasm_errwarn_propagate(pp->errwarns, yasm_linemap_get_current(pp->cur_lm));
  724. return 0;
  725. }
  726. static int eval_endm(yasm_preproc_gas *pp, int unused)
  727. {
  728. yasm_error_set(YASM_ERROR_SYNTAX, N_("\".endm\" without \".macro\""));
  729. yasm_errwarn_propagate(pp->errwarns, yasm_linemap_get_current(pp->cur_lm));
  730. return 0;
  731. }
  732. static void get_param_value(macro_entry *macro, int param_index, const char *args, const char **value, int *length)
  733. {
  734. int arg_index = 0;
  735. const char *default_value = NULL;
  736. const char *end, *eq = strstr(macro->params[param_index], "=");
  737. if (eq) {
  738. default_value = eq + 1;
  739. }
  740. skip_whitespace(&args);
  741. end = args;
  742. while (*end) {
  743. args = end;
  744. while (*end && !isspace(*end) && *end != ',') {
  745. end++;
  746. }
  747. if (arg_index == param_index) {
  748. if (end == args && default_value) {
  749. *value = default_value;
  750. *length = strlen(default_value);
  751. } else {
  752. *value = args;
  753. *length = end - args;
  754. }
  755. return;
  756. }
  757. arg_index++;
  758. skip_whitespace(&end);
  759. if (*end == ',') {
  760. end++;
  761. skip_whitespace(&end);
  762. }
  763. }
  764. *value = default_value;
  765. *length = (default_value ? strlen(default_value) : 0);
  766. }
  767. static void expand_macro(yasm_preproc_gas *pp, macro_entry *macro, const char *args)
  768. {
  769. int i, j;
  770. buffered_line *prev_bline = NULL;
  771. for (i = 0; i < macro->num_lines; i++) {
  772. buffered_line *bline = yasm_xmalloc(sizeof(buffered_line));
  773. struct tokenval tokval;
  774. int prev_was_backslash = FALSE;
  775. int line_length = strlen(macro->lines[i]);
  776. char *work = yasm__xstrdup(macro->lines[i]);
  777. expr_state prev_state = pp->expr;
  778. gas_scan_init(pp, &tokval, work);
  779. while (gas_scan(pp, &tokval) != TOKEN_EOS) {
  780. if (prev_was_backslash) {
  781. if (tokval.t_type == TOKEN_ID) {
  782. for (j = 0; j < macro->num_params; j++) {
  783. char *end = strstr(macro->params[j], "=");
  784. int len = (end ? (size_t)(end - macro->params[j])
  785. : strlen(macro->params[j]));
  786. if (!strncmp(tokval.t_charptr, macro->params[j], len)
  787. && tokval.t_charptr[len] == '\0') {
  788. /* now, find matching argument. */
  789. const char *value;
  790. char *line = work + (pp->expr.string - work);
  791. int cursor = pp->expr.string_cursor;
  792. int value_length, delta;
  793. get_param_value(macro, j, args, &value, &value_length);
  794. len++; /* leading slash */
  795. delta = value_length - len;
  796. line_length += delta;
  797. if (delta > 0) {
  798. line = yasm_xrealloc(line, line_length + 1);
  799. }
  800. memmove(line + cursor - len + value_length, line + cursor, strlen(line + cursor) + 1);
  801. memcpy(line + cursor - len, value, value_length);
  802. pp->expr.string = work = line;
  803. pp->expr.string_cursor += delta;
  804. if (pp->expr.symbol) {
  805. yasm_xfree(pp->expr.symbol);
  806. pp->expr.symbol = NULL;
  807. }
  808. }
  809. }
  810. }
  811. prev_was_backslash = FALSE;
  812. } else if (tokval.t_type == '\\') {
  813. prev_was_backslash = TRUE;
  814. }
  815. }
  816. gas_scan_cleanup(pp, &tokval);
  817. bline->line = work + (pp->expr.string - work);
  818. bline->line_number = -1;
  819. pp->expr = prev_state;
  820. if (prev_bline) {
  821. SLIST_INSERT_AFTER(prev_bline, bline, next);
  822. } else {
  823. SLIST_INSERT_HEAD(&pp->buffered_lines, bline, next);
  824. }
  825. prev_bline = bline;
  826. }
  827. }
  828. static int eval_rept(yasm_preproc_gas *pp, int unused, const char *arg1)
  829. {
  830. long i, n = eval_expr(pp, arg1);
  831. long num_lines = 0;
  832. long nesting = 1;
  833. char *line = read_line(pp);
  834. buffered_line *prev_bline = NULL;
  835. SLIST_HEAD(buffered_lines_head, buffered_line) lines;
  836. int rept_start_file_line_number = pp->next_line_number - 1;
  837. int rept_start_output_line_number = pp->current_line_number;
  838. SLIST_INIT(&lines);
  839. while (line) {
  840. skip_whitespace2(&line);
  841. if (starts_with(line, ".rept")) {
  842. nesting++;
  843. } else if (starts_with(line, ".endr") && --nesting == 0) {
  844. for (i = 0; i < n; i++) {
  845. buffered_line *current_line;
  846. prev_bline = NULL;
  847. SLIST_FOREACH(current_line, &lines, next) {
  848. buffered_line *bline = yasm_xmalloc(sizeof(buffered_line));
  849. bline->line = yasm__xstrdup(current_line->line);
  850. bline->line_number = current_line->line_number;
  851. if (prev_bline) {
  852. SLIST_INSERT_AFTER(prev_bline, bline, next);
  853. } else {
  854. SLIST_INSERT_HEAD(&pp->buffered_lines, bline, next);
  855. }
  856. prev_bline = bline;
  857. }
  858. }
  859. if (!SLIST_EMPTY(&pp->included_files)) {
  860. included_file *inc_file = SLIST_FIRST(&pp->included_files);
  861. inc_file->lines_remaining += n * num_lines;
  862. }
  863. while (!SLIST_EMPTY(&lines)) {
  864. buffered_line *bline = SLIST_FIRST(&lines);
  865. SLIST_REMOVE_HEAD(&lines, next);
  866. yasm_xfree(bline->line);
  867. yasm_xfree(bline);
  868. }
  869. yasm_xfree(line);
  870. return 1;
  871. }
  872. if (n > 0) {
  873. buffered_line *bline = yasm_xmalloc(sizeof(buffered_line));
  874. bline->line = line;
  875. bline->line_number = pp->next_line_number;
  876. if (prev_bline) {
  877. SLIST_INSERT_AFTER(prev_bline, bline, next);
  878. } else {
  879. SLIST_INSERT_HEAD(&lines, bline, next);
  880. }
  881. prev_bline = bline;
  882. } else {
  883. yasm_xfree(line);
  884. }
  885. line = read_line(pp);
  886. num_lines++;
  887. }
  888. yasm_linemap_set(pp->cur_lm, pp->in_filename, rept_start_output_line_number, rept_start_file_line_number, 0);
  889. yasm_error_set(YASM_ERROR_SYNTAX, N_("rept without matching endr"));
  890. yasm_errwarn_propagate(pp->errwarns, rept_start_output_line_number);
  891. return 0;
  892. }
  893. static int eval_endr(yasm_preproc_gas *pp, int unused)
  894. {
  895. yasm_error_set(YASM_ERROR_SYNTAX, N_("\".endr\" without \".rept\""));
  896. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  897. return 0;
  898. }
  899. /* Top-level line processing. */
  900. typedef int (*pp_fn0_t)(yasm_preproc_gas *pp, int param);
  901. typedef int (*pp_fn1_t)(yasm_preproc_gas *pp, int param, const char *arg1);
  902. typedef int (*pp_fn2_t)(yasm_preproc_gas *pp, int param, const char *arg1, const char *arg2);
  903. #define FN(f) ((pp_fn0_t) &(f))
  904. static void kill_comments(yasm_preproc_gas *pp, char *line)
  905. {
  906. int next = 2;
  907. char *cstart;
  908. skip_whitespace2(&line);
  909. if (*line == '#' || !strncmp(line, "//", 2)) {
  910. *line = '\0';
  911. return;
  912. }
  913. if (pp->in_comment) {
  914. cstart = line;
  915. next = 0;
  916. } else {
  917. cstart = strstr(line, "/*");
  918. next = 2;
  919. }
  920. while (cstart) {
  921. char *cend = strstr(cstart + next, "*/");
  922. if (!cend) {
  923. *cstart = '\0';
  924. pp->in_comment = TRUE;
  925. return;
  926. }
  927. memmove(cstart, cend + 2, strlen(cend + 2) + 1);
  928. pp->in_comment = FALSE;
  929. cstart = strstr(cstart, "/*");
  930. next = 2;
  931. }
  932. }
  933. static int substitute_values(yasm_preproc_gas *pp, char **line_ptr)
  934. {
  935. int changed = 0;
  936. char *line = *line_ptr;
  937. int line_length = strlen(line);
  938. struct tokenval tokval;
  939. expr_state prev_state = pp->expr;
  940. gas_scan_init(pp, &tokval, line);
  941. while (gas_scan(pp, &tokval) != TOKEN_EOS) {
  942. if (tokval.t_type == TOKEN_ID) {
  943. yasm_symrec *rec = yasm_symtab_get(pp->defines, tokval.t_charptr);
  944. if (rec) {
  945. int cursor = pp->expr.string_cursor;
  946. int len = strlen(tokval.t_charptr);
  947. char value[64];
  948. int value_length = sprintf(value, "%ld", eval_expr(pp, tokval.t_charptr));
  949. int delta = value_length - len;
  950. line_length += delta;
  951. if (delta > 0) {
  952. line = yasm_xrealloc(line, line_length + 1);
  953. }
  954. memmove(line + cursor - len + value_length, line + cursor, strlen(line + cursor) + 1);
  955. memcpy(line + cursor - len, value, value_length);
  956. pp->expr.string = line;
  957. pp->expr.string_cursor = cursor + delta;
  958. changed = 1;
  959. }
  960. yasm_xfree(pp->expr.symbol);
  961. pp->expr.symbol = NULL;
  962. }
  963. }
  964. gas_scan_cleanup(pp, &tokval);
  965. pp->expr = prev_state;
  966. if (changed) {
  967. *line_ptr = line;
  968. }
  969. return changed;
  970. }
  971. static int process_line(yasm_preproc_gas *pp, char **line_ptr)
  972. {
  973. macro_entry *macro;
  974. size_t i;
  975. char *line = *line_ptr;
  976. struct {
  977. const char *name;
  978. int nargs;
  979. pp_fn0_t fn;
  980. int param;
  981. } directives[] = {
  982. {"else", 0, FN(eval_else), 0},
  983. {"elseif", 1, FN(eval_elseif), 0},
  984. {"endif", 0, FN(eval_endif), 0},
  985. {"if", 1, FN(eval_if), 0},
  986. {"ifb", 1, FN(eval_ifb), 0},
  987. {"ifc", 1, FN(eval_ifc), 0},
  988. {"ifdef", 1, FN(eval_ifdef), 0},
  989. {"ifeq", 1, FN(eval_if), 1},
  990. {"ifeqs", 1, FN(eval_ifeqs), 0},
  991. {"ifge", 1, FN(eval_ifge), 0},
  992. {"ifgt", 1, FN(eval_ifgt), 0},
  993. {"ifle", 1, FN(eval_ifgt), 1},
  994. {"iflt", 1, FN(eval_ifge), 1},
  995. {"ifnb", 1, FN(eval_ifb), 1},
  996. {"ifnc", 1, FN(eval_ifc), 1},
  997. {"ifndef", 1, FN(eval_ifdef), 1},
  998. {"ifnotdef", 1, FN(eval_ifdef), 1},
  999. {"ifne", 1, FN(eval_if), 0},
  1000. {"ifnes", 1, FN(eval_ifeqs), 1},
  1001. {"include", 1, FN(eval_include), 0},
  1002. {"set", 2, FN(eval_set), 1},
  1003. {"equ", 2, FN(eval_set), 1},
  1004. {"equiv", 2, FN(eval_set), 0},
  1005. {"macro", 1, FN(eval_macro), 0},
  1006. {"endm", 0, FN(eval_endm), 0},
  1007. {"rept", 1, FN(eval_rept), 0},
  1008. {"endr", 1, FN(eval_endr), 0},
  1009. };
  1010. kill_comments(pp, line);
  1011. skip_whitespace2(&line);
  1012. if (*line == '\0') {
  1013. return FALSE;
  1014. }
  1015. /* See if this is a macro call. */
  1016. STAILQ_FOREACH(macro, &pp->macros, next) {
  1017. const char *remainder = starts_with(line, macro->name);
  1018. if (remainder && (!*remainder || isspace(*remainder))) {
  1019. skip_whitespace2(&line);
  1020. expand_macro(pp, macro, remainder);
  1021. return FALSE;
  1022. }
  1023. }
  1024. for (i = 0; i < sizeof(directives)/sizeof(directives[0]); i++) {
  1025. char buf1[1024];
  1026. const char *remainder = matches(line, directives[i].name);
  1027. if (remainder) {
  1028. if (pp->skip_depth) {
  1029. if (!strncmp("if", directives[i].name, 2)) {
  1030. pp->skip_depth++;
  1031. } else if (!strcmp("endif", directives[i].name)) {
  1032. pp->skip_depth--;
  1033. } else if (!strcmp("else", directives[i].name)) {
  1034. if (pp->skip_depth == 1) {
  1035. pp->skip_depth = 0;
  1036. pp->depth++;
  1037. }
  1038. }
  1039. return FALSE;
  1040. } else if (directives[i].nargs == 0) {
  1041. pp_fn0_t fn = (pp_fn0_t) directives[i].fn;
  1042. pp->fatal_error = !fn(pp, directives[i].param);
  1043. return FALSE;
  1044. } else if (directives[i].nargs == 1) {
  1045. pp_fn1_t fn = (pp_fn1_t) directives[i].fn;
  1046. skip_whitespace(&remainder);
  1047. pp->fatal_error = !fn(pp, directives[i].param, remainder);
  1048. return FALSE;
  1049. } else if (directives[i].nargs == 2) {
  1050. remainder = get_arg(pp, remainder, buf1, sizeof(buf1));
  1051. if (!remainder || !*remainder || !*buf1) {
  1052. yasm_error_set(YASM_ERROR_SYNTAX, N_("\".%s\" expects two arguments"), directives[i].name);
  1053. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  1054. pp->fatal_error = 1;
  1055. } else {
  1056. pp_fn2_t fn = (pp_fn2_t) directives[i].fn;
  1057. pp->fatal_error = !fn(pp, directives[i].param, buf1, remainder);
  1058. }
  1059. return FALSE;
  1060. }
  1061. }
  1062. }
  1063. if (pp->skip_depth == 0) {
  1064. substitute_values(pp, line_ptr);
  1065. return TRUE;
  1066. }
  1067. return FALSE;
  1068. }
  1069. /* Functions exported by the preprocessor. */
  1070. static yasm_preproc *
  1071. gas_preproc_create(const char *in_filename, yasm_symtab *symtab,
  1072. yasm_linemap *lm, yasm_errwarns *errwarns)
  1073. {
  1074. FILE *f;
  1075. yasm_preproc_gas *pp = yasm_xmalloc(sizeof(yasm_preproc_gas));
  1076. if (strcmp(in_filename, "-") != 0) {
  1077. f = fopen(in_filename, "r");
  1078. if (!f) {
  1079. yasm__fatal_missing_input_file(N_("Could not open input file"), in_filename);
  1080. }
  1081. } else {
  1082. f = stdin;
  1083. }
  1084. pp->preproc.module = &yasm_gas_LTX_preproc;
  1085. pp->in = f;
  1086. pp->in_filename = yasm__xstrdup(in_filename);
  1087. pp->defines = yasm_symtab_create();
  1088. SLIST_INIT(&pp->deferred_defines);
  1089. yasm_symtab_set_case_sensitive(pp->defines, 1);
  1090. pp->depth = 0;
  1091. pp->skip_depth = 0;
  1092. pp->in_comment = FALSE;
  1093. SLIST_INIT(&pp->buffered_lines);
  1094. SLIST_INIT(&pp->included_files);
  1095. STAILQ_INIT(&pp->macros);
  1096. pp->in_line_number = 0;
  1097. pp->next_line_number = 0;
  1098. pp->current_line_number = 0;
  1099. pp->cur_lm = lm;
  1100. pp->errwarns = errwarns;
  1101. pp->fatal_error = 0;
  1102. pp->detect_errors_only = 0;
  1103. return (yasm_preproc *) pp;
  1104. }
  1105. static void
  1106. gas_preproc_destroy(yasm_preproc *preproc)
  1107. {
  1108. yasm_preproc_gas *pp = (yasm_preproc_gas *) preproc;
  1109. yasm_xfree(pp->in_filename);
  1110. yasm_symtab_destroy(pp->defines);
  1111. while (!SLIST_EMPTY(&pp->deferred_defines)) {
  1112. deferred_define *def = SLIST_FIRST(&pp->deferred_defines);
  1113. SLIST_REMOVE_HEAD(&pp->deferred_defines, next);
  1114. yasm_xfree(def->name);
  1115. yasm_xfree(def->value);
  1116. yasm_xfree(def);
  1117. }
  1118. while (!SLIST_EMPTY(&pp->buffered_lines)) {
  1119. buffered_line *bline = SLIST_FIRST(&pp->buffered_lines);
  1120. SLIST_REMOVE_HEAD(&pp->buffered_lines, next);
  1121. yasm_xfree(bline->line);
  1122. yasm_xfree(bline);
  1123. }
  1124. while (!SLIST_EMPTY(&pp->included_files)) {
  1125. included_file *inc_file = SLIST_FIRST(&pp->included_files);
  1126. SLIST_REMOVE_HEAD(&pp->included_files, next);
  1127. yasm_xfree(inc_file->filename);
  1128. yasm_xfree(inc_file);
  1129. }
  1130. while (!STAILQ_EMPTY(&pp->macros)) {
  1131. int i;
  1132. macro_entry *macro = STAILQ_FIRST(&pp->macros);
  1133. STAILQ_REMOVE_HEAD(&pp->macros, next);
  1134. yasm_xfree(macro->name);
  1135. for (i = 0; i < macro->num_params; i++)
  1136. yasm_xfree(macro->params[i]);
  1137. yasm_xfree(macro->params);
  1138. for (i = 0; i < macro->num_lines; i++)
  1139. yasm_xfree(macro->lines[i]);
  1140. yasm_xfree(macro->lines);
  1141. yasm_xfree(macro);
  1142. }
  1143. yasm_xfree(preproc);
  1144. }
  1145. static char *
  1146. gas_preproc_get_line(yasm_preproc *preproc)
  1147. {
  1148. yasm_preproc_gas *pp = (yasm_preproc_gas *)preproc;
  1149. int done = FALSE;
  1150. char *line = NULL;
  1151. pp->current_line_number++;
  1152. do {
  1153. if (line != NULL) {
  1154. yasm_xfree(line);
  1155. }
  1156. if (pp->fatal_error) {
  1157. return NULL;
  1158. }
  1159. line = read_line(pp);
  1160. if (line == NULL) {
  1161. if (pp->in_comment) {
  1162. yasm_linemap_set(pp->cur_lm, pp->in_filename, pp->current_line_number, pp->next_line_number, 0);
  1163. yasm_warn_set(YASM_WARN_GENERAL, N_("end of file in comment"));
  1164. yasm_errwarn_propagate(pp->errwarns, pp->current_line_number);
  1165. pp->in_comment = FALSE;
  1166. }
  1167. return NULL;
  1168. }
  1169. done = process_line(pp, &line);
  1170. } while (!done);
  1171. yasm_linemap_set(pp->cur_lm, pp->in_filename, pp->current_line_number, pp->next_line_number, 0);
  1172. return line;
  1173. }
  1174. static size_t
  1175. gas_preproc_get_included_file(yasm_preproc *preproc, char *buf,
  1176. size_t max_size)
  1177. {
  1178. /* TODO */
  1179. return 0;
  1180. }
  1181. static void
  1182. gas_preproc_add_include_file(yasm_preproc *preproc, const char *filename)
  1183. {
  1184. yasm_preproc_gas *pp = (yasm_preproc_gas *) preproc;
  1185. eval_include(pp, 0, filename);
  1186. }
  1187. static void
  1188. gas_preproc_predefine_macro(yasm_preproc *preproc, const char *macronameval)
  1189. {
  1190. yasm_preproc_gas *pp = (yasm_preproc_gas *) preproc;
  1191. const char *eq = strstr(macronameval, "=");
  1192. char *name, *value;
  1193. if (eq) {
  1194. value = yasm__xstrdup(eq + 1);
  1195. name = yasm_xmalloc(eq - macronameval + 1);
  1196. memcpy(name, macronameval, eq - macronameval);
  1197. name[eq - macronameval] = '\0';
  1198. } else {
  1199. name = yasm__xstrdup(macronameval);
  1200. value = yasm__xstrdup("");
  1201. }
  1202. eval_set(pp, 1, name, value);
  1203. yasm_xfree(name);
  1204. yasm_xfree(value);
  1205. }
  1206. static void
  1207. gas_preproc_undefine_macro(yasm_preproc *preproc, const char *macroname)
  1208. {
  1209. /* TODO */
  1210. }
  1211. static void
  1212. gas_preproc_define_builtin(yasm_preproc *preproc, const char *macronameval)
  1213. {
  1214. /* TODO */
  1215. }
  1216. static void
  1217. gas_preproc_add_standard(yasm_preproc *preproc, const char **macros)
  1218. {
  1219. /* TODO */
  1220. }
  1221. /* Define preproc structure -- see preproc.h for details */
  1222. yasm_preproc_module yasm_gas_LTX_preproc = {
  1223. "GNU AS (GAS)-compatible preprocessor",
  1224. "gas",
  1225. gas_preproc_create,
  1226. gas_preproc_destroy,
  1227. gas_preproc_get_line,
  1228. gas_preproc_get_included_file,
  1229. gas_preproc_add_include_file,
  1230. gas_preproc_predefine_macro,
  1231. gas_preproc_undefine_macro,
  1232. gas_preproc_define_builtin,
  1233. gas_preproc_add_standard
  1234. };