cc_macro.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /* Copyright (C) 2021 Sanne Wouda
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "cc.h"
  18. #include "gcc_req.h"
  19. void require(int bool, char* error);
  20. int numerate_string(char* a);
  21. void line_error_token(struct token_list* list);
  22. struct token_list* eat_token(struct token_list* head);
  23. struct conditional_inclusion
  24. {
  25. struct conditional_inclusion* prev;
  26. int include; /* 1 == include, 0 == skip */
  27. int previous_condition_matched; /* 1 == all subsequent conditions treated as FALSE */
  28. };
  29. struct macro_list
  30. {
  31. struct macro_list* next;
  32. char* symbol;
  33. struct token_list* expansion;
  34. };
  35. struct macro_list* macro_env;
  36. struct conditional_inclusion* conditional_inclusion_top;
  37. /* point where we are currently modifying the global_token list */
  38. struct token_list* macro_token;
  39. void eat_current_token()
  40. {
  41. int update_global_token = FALSE;
  42. if (macro_token == global_token)
  43. update_global_token = TRUE;
  44. macro_token = eat_token(macro_token);
  45. if(update_global_token)
  46. global_token = macro_token;
  47. }
  48. void eat_newline_tokens()
  49. {
  50. macro_token = global_token;
  51. while(TRUE)
  52. {
  53. if(NULL == macro_token) return;
  54. if(match("\n", macro_token->s))
  55. {
  56. eat_current_token();
  57. }
  58. else
  59. {
  60. macro_token = macro_token->next;
  61. }
  62. }
  63. }
  64. /* returns the first token inserted; inserts *before* point */
  65. struct token_list* insert_tokens(struct token_list* point, struct token_list* token)
  66. {
  67. struct token_list* copy;
  68. struct token_list* first = NULL;
  69. while (NULL != token)
  70. {
  71. copy = calloc(1, sizeof(struct token_list));
  72. copy->s = token->s;
  73. copy->filename = token->filename;
  74. copy->linenumber = token->linenumber;
  75. if(NULL == first)
  76. {
  77. first = copy;
  78. }
  79. copy->next = point;
  80. if (NULL != point)
  81. {
  82. copy->prev = point->prev;
  83. if(NULL != point->prev)
  84. {
  85. point->prev->next = copy;
  86. }
  87. point->prev = copy;
  88. }
  89. token = token->next;
  90. }
  91. return first;
  92. }
  93. struct macro_list* lookup_macro(struct token_list* token)
  94. {
  95. struct macro_list* hold = macro_env;
  96. while (NULL != hold)
  97. {
  98. if (match(token->s, hold->symbol))
  99. {
  100. /* found! */
  101. return hold;
  102. }
  103. hold = hold->next;
  104. }
  105. /* not found! */
  106. return NULL;
  107. }
  108. int macro_expression();
  109. int macro_variable()
  110. {
  111. eat_current_token();
  112. return 0;
  113. }
  114. int macro_number()
  115. {
  116. int result = numerate_string(macro_token->s);
  117. eat_current_token();
  118. return result;
  119. }
  120. int macro_primary_expr()
  121. {
  122. int defined_has_paren = FALSE;
  123. int hold;
  124. require(NULL != macro_token, "got an EOF terminated macro primary expression\n");
  125. if('-' == macro_token->s[0])
  126. {
  127. eat_current_token();
  128. return -macro_primary_expr();
  129. }
  130. else if('!' == macro_token->s[0])
  131. {
  132. eat_current_token();
  133. return !macro_primary_expr();
  134. }
  135. else if('(' == macro_token->s[0])
  136. {
  137. eat_current_token();
  138. return macro_expression();
  139. }
  140. else if(match("defined", macro_token->s))
  141. {
  142. eat_current_token();
  143. require(NULL != macro_token, "got an EOF terminated macro defined expression\n");
  144. if('(' == macro_token->s[0])
  145. {
  146. defined_has_paren = TRUE;
  147. eat_current_token();
  148. }
  149. if (NULL != lookup_macro(macro_token))
  150. {
  151. hold = TRUE;
  152. }
  153. else
  154. {
  155. hold = FALSE;
  156. }
  157. eat_current_token();
  158. if(TRUE == defined_has_paren)
  159. {
  160. require(')' == macro_token->s[0], "missing close parenthesis for defined()\n");
  161. eat_current_token();
  162. }
  163. return hold;
  164. }
  165. else if(in_set(macro_token->s[0], "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"))
  166. {
  167. return macro_variable();
  168. }
  169. else if(in_set(macro_token->s[0], "0123456789"))
  170. {
  171. return macro_number();
  172. }
  173. else
  174. {
  175. return 0; /* FIXME: error handling */
  176. }
  177. }
  178. int macro_additive_expr()
  179. {
  180. int lhs = macro_primary_expr();
  181. int hold;
  182. require(NULL != macro_token, "got an EOF terminated macro additive expression\n");
  183. if(match("+", macro_token->s))
  184. {
  185. eat_current_token();
  186. return lhs + macro_additive_expr();
  187. }
  188. else if(match("-", macro_token->s))
  189. {
  190. eat_current_token();
  191. return lhs - macro_additive_expr();
  192. }
  193. else if(match("*", macro_token->s))
  194. {
  195. eat_current_token();
  196. return lhs * macro_additive_expr();
  197. }
  198. else if(match("/", macro_token->s))
  199. {
  200. eat_current_token();
  201. hold = macro_additive_expr();
  202. require(0 != hold, "divide by zero not valid even in C macros\n");
  203. return lhs / hold;
  204. }
  205. else if(match("%", macro_token->s))
  206. {
  207. eat_current_token();
  208. hold = macro_additive_expr();
  209. require(0 != hold, "modulus by zero not valid even in C macros\n");
  210. return lhs % hold;
  211. }
  212. else if(match(">>", macro_token->s))
  213. {
  214. eat_current_token();
  215. return lhs >> macro_additive_expr();
  216. }
  217. else if(match("<<", macro_token->s))
  218. {
  219. eat_current_token();
  220. return lhs << macro_additive_expr();
  221. }
  222. else
  223. {
  224. return lhs;
  225. }
  226. }
  227. int macro_relational_expr()
  228. {
  229. int lhs = macro_additive_expr();
  230. if(match("<", macro_token->s))
  231. {
  232. eat_current_token();
  233. return lhs < macro_relational_expr();
  234. }
  235. else if(match("<=", macro_token->s))
  236. {
  237. eat_current_token();
  238. return lhs <= macro_relational_expr();
  239. }
  240. else if(match(">=", macro_token->s))
  241. {
  242. eat_current_token();
  243. return lhs >= macro_relational_expr();
  244. }
  245. else if(match(">", macro_token->s))
  246. {
  247. eat_current_token();
  248. return lhs > macro_relational_expr();
  249. }
  250. else if(match("==", macro_token->s))
  251. {
  252. eat_current_token();
  253. return lhs == macro_relational_expr();
  254. }
  255. else if(match("!=", macro_token->s))
  256. {
  257. eat_current_token();
  258. return lhs != macro_relational_expr();
  259. }
  260. else
  261. {
  262. return lhs;
  263. }
  264. }
  265. int macro_bitwise_expr()
  266. {
  267. int rhs;
  268. int lhs = macro_relational_expr();
  269. if(match("&", macro_token->s))
  270. {
  271. eat_current_token();
  272. return lhs & macro_bitwise_expr();
  273. }
  274. else if(match("&&", macro_token->s))
  275. {
  276. eat_current_token();
  277. rhs = macro_bitwise_expr();
  278. return lhs && rhs;
  279. }
  280. else if(match("|", macro_token->s))
  281. {
  282. eat_current_token();
  283. rhs = macro_bitwise_expr();
  284. return lhs | rhs;
  285. }
  286. else if(match("||", macro_token->s))
  287. {
  288. eat_current_token();
  289. rhs = macro_bitwise_expr();
  290. return lhs || rhs;
  291. }
  292. else if(match("^", macro_token->s))
  293. {
  294. eat_current_token();
  295. rhs = macro_bitwise_expr();
  296. return lhs ^ rhs;
  297. }
  298. else
  299. {
  300. return lhs;
  301. }
  302. }
  303. int macro_expression()
  304. {
  305. return macro_bitwise_expr();
  306. }
  307. void handle_define()
  308. {
  309. struct macro_list* hold;
  310. struct token_list* expansion_end;
  311. eat_current_token();
  312. require(NULL != macro_token, "got an EOF terminated #define\n");
  313. require('\n' != macro_token->s[0], "unexpected newline after #define\n");
  314. /* insert new macro */
  315. hold = calloc(1, sizeof(struct macro_list));
  316. hold->symbol = macro_token->s;
  317. hold->next = macro_env;
  318. macro_env = hold;
  319. /* discard the macro name */
  320. eat_current_token();
  321. while (TRUE)
  322. {
  323. require(NULL != macro_token, "got an EOF terminated #define\n");
  324. if ('\n' == macro_token->s[0])
  325. {
  326. expansion_end->next = NULL;
  327. return;
  328. }
  329. expansion_end = macro_token;
  330. /* in the first iteration, we set the first token of the expansion, if
  331. it exists */
  332. if (NULL == hold->expansion)
  333. {
  334. hold->expansion = macro_token;
  335. }
  336. eat_current_token();
  337. }
  338. }
  339. void macro_directive()
  340. {
  341. struct conditional_inclusion *t;
  342. int result;
  343. /* FIXME: whitespace is allowed between "#"" and "if" */
  344. if(match("#if", macro_token->s))
  345. {
  346. eat_current_token();
  347. /* evaluate constant integer expression */
  348. result = macro_expression();
  349. /* push conditional inclusion */
  350. t = calloc(1, sizeof(struct conditional_inclusion));
  351. t->prev = conditional_inclusion_top;
  352. conditional_inclusion_top = t;
  353. t->include = TRUE;
  354. if(FALSE == result)
  355. {
  356. t->include = FALSE;
  357. }
  358. t->previous_condition_matched = t->include;
  359. }
  360. else if(match("#elif", macro_token->s))
  361. {
  362. eat_current_token();
  363. result = macro_expression();
  364. require(NULL != conditional_inclusion_top, "#elif without leading #if\n");
  365. conditional_inclusion_top->include = result && !conditional_inclusion_top->previous_condition_matched;
  366. conditional_inclusion_top->previous_condition_matched =
  367. conditional_inclusion_top->previous_condition_matched || conditional_inclusion_top->include;
  368. }
  369. else if(match("#else", macro_token->s))
  370. {
  371. eat_current_token();
  372. require(NULL != conditional_inclusion_top, "#else without leading #if\n");
  373. conditional_inclusion_top->include = !conditional_inclusion_top->previous_condition_matched;
  374. }
  375. else if(match("#endif", macro_token->s))
  376. {
  377. if(NULL == conditional_inclusion_top)
  378. {
  379. line_error_token(macro_token);
  380. file_print("unexpected #endif\n", stderr);
  381. exit(EXIT_FAILURE);
  382. }
  383. eat_current_token();
  384. /* pop conditional inclusion */
  385. t = conditional_inclusion_top;
  386. conditional_inclusion_top = conditional_inclusion_top->prev;
  387. free(t);
  388. }
  389. else if(match("#define", macro_token->s))
  390. {
  391. handle_define();
  392. }
  393. else
  394. {
  395. /* unhandled macro directive; let's eat until a newline; om nom nom */
  396. while(TRUE)
  397. {
  398. if(NULL == macro_token)
  399. {
  400. return;
  401. }
  402. if('\n' == macro_token->s[0])
  403. {
  404. return;
  405. }
  406. eat_current_token();
  407. }
  408. }
  409. }
  410. struct token_list* maybe_expand(struct token_list* token)
  411. {
  412. struct macro_list* hold = lookup_macro(token);
  413. struct token_list* hold2;
  414. if (NULL == hold)
  415. {
  416. return token->next;
  417. }
  418. token = eat_token(token);
  419. hold2 = insert_tokens(token, hold->expansion);
  420. return hold2->next;
  421. }
  422. void preprocess()
  423. {
  424. int start_of_line = TRUE;
  425. macro_token = global_token;
  426. while(NULL != macro_token)
  427. {
  428. if(start_of_line && '#' == macro_token->s[0])
  429. {
  430. macro_directive();
  431. if(macro_token)
  432. {
  433. if('\n' != macro_token->s[0])
  434. {
  435. line_error_token(macro_token);
  436. file_print("newline expected at end of macro directive\n", stderr);
  437. file_print("found: '", stderr);
  438. file_print(macro_token->s, stderr);
  439. file_print("'\n", stderr);
  440. exit(EXIT_FAILURE);
  441. }
  442. }
  443. }
  444. else if('\n' == macro_token->s[0])
  445. {
  446. start_of_line = TRUE;
  447. macro_token = macro_token->next;
  448. }
  449. else
  450. {
  451. start_of_line = FALSE;
  452. if(NULL == conditional_inclusion_top)
  453. {
  454. macro_token = maybe_expand(macro_token);
  455. }
  456. else if(!conditional_inclusion_top->include)
  457. {
  458. /* rewrite the token stream to exclude the current token */
  459. eat_current_token();
  460. }
  461. else
  462. {
  463. macro_token = maybe_expand(macro_token);
  464. }
  465. }
  466. }
  467. }