expr.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. /*
  2. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  3. * Released under the terms of the GNU GPL v2.0.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "lkc.h"
  9. #define DEBUG_EXPR 0
  10. struct expr *expr_alloc_symbol(struct symbol *sym)
  11. {
  12. struct expr *e = calloc(1, sizeof(*e));
  13. e->type = E_SYMBOL;
  14. e->left.sym = sym;
  15. return e;
  16. }
  17. struct expr *expr_alloc_one(enum expr_type type, struct expr *ce)
  18. {
  19. struct expr *e = calloc(1, sizeof(*e));
  20. e->type = type;
  21. e->left.expr = ce;
  22. return e;
  23. }
  24. struct expr *expr_alloc_two(enum expr_type type, struct expr *e1, struct expr *e2)
  25. {
  26. struct expr *e = calloc(1, sizeof(*e));
  27. e->type = type;
  28. e->left.expr = e1;
  29. e->right.expr = e2;
  30. return e;
  31. }
  32. struct expr *expr_alloc_comp(enum expr_type type, struct symbol *s1, struct symbol *s2)
  33. {
  34. struct expr *e = calloc(1, sizeof(*e));
  35. e->type = type;
  36. e->left.sym = s1;
  37. e->right.sym = s2;
  38. return e;
  39. }
  40. struct expr *expr_alloc_and(struct expr *e1, struct expr *e2)
  41. {
  42. if (!e1)
  43. return e2;
  44. return e2 ? expr_alloc_two(E_AND, e1, e2) : e1;
  45. }
  46. struct expr *expr_alloc_or(struct expr *e1, struct expr *e2)
  47. {
  48. if (!e1)
  49. return e2;
  50. return e2 ? expr_alloc_two(E_OR, e1, e2) : e1;
  51. }
  52. struct expr *expr_copy(const struct expr *org)
  53. {
  54. struct expr *e;
  55. if (!org)
  56. return NULL;
  57. e = malloc(sizeof(*org));
  58. memcpy(e, org, sizeof(*org));
  59. switch (org->type) {
  60. case E_SYMBOL:
  61. e->left = org->left;
  62. break;
  63. case E_NOT:
  64. e->left.expr = expr_copy(org->left.expr);
  65. break;
  66. case E_EQUAL:
  67. case E_UNEQUAL:
  68. e->left.sym = org->left.sym;
  69. e->right.sym = org->right.sym;
  70. break;
  71. case E_AND:
  72. case E_OR:
  73. case E_LIST:
  74. e->left.expr = expr_copy(org->left.expr);
  75. e->right.expr = expr_copy(org->right.expr);
  76. break;
  77. default:
  78. printf("can't copy type %d\n", e->type);
  79. free(e);
  80. e = NULL;
  81. break;
  82. }
  83. return e;
  84. }
  85. void expr_free(struct expr *e)
  86. {
  87. if (!e)
  88. return;
  89. switch (e->type) {
  90. case E_SYMBOL:
  91. break;
  92. case E_NOT:
  93. expr_free(e->left.expr);
  94. return;
  95. case E_EQUAL:
  96. case E_UNEQUAL:
  97. break;
  98. case E_OR:
  99. case E_AND:
  100. expr_free(e->left.expr);
  101. expr_free(e->right.expr);
  102. break;
  103. default:
  104. printf("how to free type %d?\n", e->type);
  105. break;
  106. }
  107. free(e);
  108. }
  109. static int trans_count;
  110. #define e1 (*ep1)
  111. #define e2 (*ep2)
  112. static void __expr_eliminate_eq(enum expr_type type, struct expr **ep1, struct expr **ep2)
  113. {
  114. if (e1->type == type) {
  115. __expr_eliminate_eq(type, &e1->left.expr, &e2);
  116. __expr_eliminate_eq(type, &e1->right.expr, &e2);
  117. return;
  118. }
  119. if (e2->type == type) {
  120. __expr_eliminate_eq(type, &e1, &e2->left.expr);
  121. __expr_eliminate_eq(type, &e1, &e2->right.expr);
  122. return;
  123. }
  124. if (e1->type == E_SYMBOL && e2->type == E_SYMBOL &&
  125. e1->left.sym == e2->left.sym &&
  126. (e1->left.sym == &symbol_yes || e1->left.sym == &symbol_no))
  127. return;
  128. if (!expr_eq(e1, e2))
  129. return;
  130. trans_count++;
  131. expr_free(e1); expr_free(e2);
  132. switch (type) {
  133. case E_OR:
  134. e1 = expr_alloc_symbol(&symbol_no);
  135. e2 = expr_alloc_symbol(&symbol_no);
  136. break;
  137. case E_AND:
  138. e1 = expr_alloc_symbol(&symbol_yes);
  139. e2 = expr_alloc_symbol(&symbol_yes);
  140. break;
  141. default:
  142. ;
  143. }
  144. }
  145. void expr_eliminate_eq(struct expr **ep1, struct expr **ep2)
  146. {
  147. if (!e1 || !e2)
  148. return;
  149. switch (e1->type) {
  150. case E_OR:
  151. case E_AND:
  152. __expr_eliminate_eq(e1->type, ep1, ep2);
  153. default:
  154. ;
  155. }
  156. if (e1->type != e2->type) switch (e2->type) {
  157. case E_OR:
  158. case E_AND:
  159. __expr_eliminate_eq(e2->type, ep1, ep2);
  160. default:
  161. ;
  162. }
  163. e1 = expr_eliminate_yn(e1);
  164. e2 = expr_eliminate_yn(e2);
  165. }
  166. #undef e1
  167. #undef e2
  168. int expr_eq(struct expr *e1, struct expr *e2)
  169. {
  170. int res, old_count;
  171. /*
  172. * A NULL expr is taken to be yes, but there's also a different way to
  173. * represent yes. expr_is_yes() checks for either representation.
  174. */
  175. if (!e1 || !e2)
  176. return expr_is_yes(e1) && expr_is_yes(e2);
  177. if (e1->type != e2->type)
  178. return 0;
  179. switch (e1->type) {
  180. case E_EQUAL:
  181. case E_UNEQUAL:
  182. return e1->left.sym == e2->left.sym && e1->right.sym == e2->right.sym;
  183. case E_SYMBOL:
  184. return e1->left.sym == e2->left.sym;
  185. case E_NOT:
  186. return expr_eq(e1->left.expr, e2->left.expr);
  187. case E_AND:
  188. case E_OR:
  189. e1 = expr_copy(e1);
  190. e2 = expr_copy(e2);
  191. old_count = trans_count;
  192. expr_eliminate_eq(&e1, &e2);
  193. res = (e1->type == E_SYMBOL && e2->type == E_SYMBOL &&
  194. e1->left.sym == e2->left.sym);
  195. expr_free(e1);
  196. expr_free(e2);
  197. trans_count = old_count;
  198. return res;
  199. case E_LIST:
  200. case E_RANGE:
  201. case E_NONE:
  202. /* panic */;
  203. }
  204. if (DEBUG_EXPR) {
  205. expr_fprint(e1, stdout);
  206. printf(" = ");
  207. expr_fprint(e2, stdout);
  208. printf(" ?\n");
  209. }
  210. return 0;
  211. }
  212. struct expr *expr_eliminate_yn(struct expr *e)
  213. {
  214. struct expr *tmp;
  215. if (e) switch (e->type) {
  216. case E_AND:
  217. e->left.expr = expr_eliminate_yn(e->left.expr);
  218. e->right.expr = expr_eliminate_yn(e->right.expr);
  219. if (e->left.expr->type == E_SYMBOL) {
  220. if (e->left.expr->left.sym == &symbol_no) {
  221. expr_free(e->left.expr);
  222. expr_free(e->right.expr);
  223. e->type = E_SYMBOL;
  224. e->left.sym = &symbol_no;
  225. e->right.expr = NULL;
  226. return e;
  227. } else if (e->left.expr->left.sym == &symbol_yes) {
  228. free(e->left.expr);
  229. tmp = e->right.expr;
  230. *e = *(e->right.expr);
  231. free(tmp);
  232. return e;
  233. }
  234. }
  235. if (e->right.expr->type == E_SYMBOL) {
  236. if (e->right.expr->left.sym == &symbol_no) {
  237. expr_free(e->left.expr);
  238. expr_free(e->right.expr);
  239. e->type = E_SYMBOL;
  240. e->left.sym = &symbol_no;
  241. e->right.expr = NULL;
  242. return e;
  243. } else if (e->right.expr->left.sym == &symbol_yes) {
  244. free(e->right.expr);
  245. tmp = e->left.expr;
  246. *e = *(e->left.expr);
  247. free(tmp);
  248. return e;
  249. }
  250. }
  251. break;
  252. case E_OR:
  253. e->left.expr = expr_eliminate_yn(e->left.expr);
  254. e->right.expr = expr_eliminate_yn(e->right.expr);
  255. if (e->left.expr->type == E_SYMBOL) {
  256. if (e->left.expr->left.sym == &symbol_no) {
  257. free(e->left.expr);
  258. tmp = e->right.expr;
  259. *e = *(e->right.expr);
  260. free(tmp);
  261. return e;
  262. } else if (e->left.expr->left.sym == &symbol_yes) {
  263. expr_free(e->left.expr);
  264. expr_free(e->right.expr);
  265. e->type = E_SYMBOL;
  266. e->left.sym = &symbol_yes;
  267. e->right.expr = NULL;
  268. return e;
  269. }
  270. }
  271. if (e->right.expr->type == E_SYMBOL) {
  272. if (e->right.expr->left.sym == &symbol_no) {
  273. free(e->right.expr);
  274. tmp = e->left.expr;
  275. *e = *(e->left.expr);
  276. free(tmp);
  277. return e;
  278. } else if (e->right.expr->left.sym == &symbol_yes) {
  279. expr_free(e->left.expr);
  280. expr_free(e->right.expr);
  281. e->type = E_SYMBOL;
  282. e->left.sym = &symbol_yes;
  283. e->right.expr = NULL;
  284. return e;
  285. }
  286. }
  287. break;
  288. default:
  289. ;
  290. }
  291. return e;
  292. }
  293. /*
  294. * bool FOO!=n => FOO
  295. */
  296. struct expr *expr_trans_bool(struct expr *e)
  297. {
  298. if (!e)
  299. return NULL;
  300. switch (e->type) {
  301. case E_AND:
  302. case E_OR:
  303. case E_NOT:
  304. e->left.expr = expr_trans_bool(e->left.expr);
  305. e->right.expr = expr_trans_bool(e->right.expr);
  306. break;
  307. case E_UNEQUAL:
  308. // FOO!=n -> FOO
  309. if (e->left.sym->type == S_TRISTATE) {
  310. if (e->right.sym == &symbol_no) {
  311. e->type = E_SYMBOL;
  312. e->right.sym = NULL;
  313. }
  314. }
  315. break;
  316. default:
  317. ;
  318. }
  319. return e;
  320. }
  321. /*
  322. * e1 || e2 -> ?
  323. */
  324. static struct expr *expr_join_or(struct expr *e1, struct expr *e2)
  325. {
  326. struct expr *tmp;
  327. struct symbol *sym1, *sym2;
  328. if (expr_eq(e1, e2))
  329. return expr_copy(e1);
  330. if (e1->type != E_EQUAL && e1->type != E_UNEQUAL && e1->type != E_SYMBOL && e1->type != E_NOT)
  331. return NULL;
  332. if (e2->type != E_EQUAL && e2->type != E_UNEQUAL && e2->type != E_SYMBOL && e2->type != E_NOT)
  333. return NULL;
  334. if (e1->type == E_NOT) {
  335. tmp = e1->left.expr;
  336. if (tmp->type != E_EQUAL && tmp->type != E_UNEQUAL && tmp->type != E_SYMBOL)
  337. return NULL;
  338. sym1 = tmp->left.sym;
  339. } else
  340. sym1 = e1->left.sym;
  341. if (e2->type == E_NOT) {
  342. if (e2->left.expr->type != E_SYMBOL)
  343. return NULL;
  344. sym2 = e2->left.expr->left.sym;
  345. } else
  346. sym2 = e2->left.sym;
  347. if (sym1 != sym2)
  348. return NULL;
  349. if (sym1->type != S_BOOLEAN && sym1->type != S_TRISTATE)
  350. return NULL;
  351. if (sym1->type == S_TRISTATE) {
  352. if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
  353. ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_mod) ||
  354. (e1->right.sym == &symbol_mod && e2->right.sym == &symbol_yes))) {
  355. // (a='y') || (a='m') -> (a!='n')
  356. return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_no);
  357. }
  358. if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
  359. ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_no) ||
  360. (e1->right.sym == &symbol_no && e2->right.sym == &symbol_yes))) {
  361. // (a='y') || (a='n') -> (a!='m')
  362. return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_mod);
  363. }
  364. if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
  365. ((e1->right.sym == &symbol_mod && e2->right.sym == &symbol_no) ||
  366. (e1->right.sym == &symbol_no && e2->right.sym == &symbol_mod))) {
  367. // (a='m') || (a='n') -> (a!='y')
  368. return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_yes);
  369. }
  370. }
  371. if (sym1->type == S_BOOLEAN && sym1 == sym2) {
  372. if ((e1->type == E_NOT && e1->left.expr->type == E_SYMBOL && e2->type == E_SYMBOL) ||
  373. (e2->type == E_NOT && e2->left.expr->type == E_SYMBOL && e1->type == E_SYMBOL))
  374. return expr_alloc_symbol(&symbol_yes);
  375. }
  376. if (DEBUG_EXPR) {
  377. printf("optimize (");
  378. expr_fprint(e1, stdout);
  379. printf(") || (");
  380. expr_fprint(e2, stdout);
  381. printf(")?\n");
  382. }
  383. return NULL;
  384. }
  385. static struct expr *expr_join_and(struct expr *e1, struct expr *e2)
  386. {
  387. struct expr *tmp;
  388. struct symbol *sym1, *sym2;
  389. if (expr_eq(e1, e2))
  390. return expr_copy(e1);
  391. if (e1->type != E_EQUAL && e1->type != E_UNEQUAL && e1->type != E_SYMBOL && e1->type != E_NOT)
  392. return NULL;
  393. if (e2->type != E_EQUAL && e2->type != E_UNEQUAL && e2->type != E_SYMBOL && e2->type != E_NOT)
  394. return NULL;
  395. if (e1->type == E_NOT) {
  396. tmp = e1->left.expr;
  397. if (tmp->type != E_EQUAL && tmp->type != E_UNEQUAL && tmp->type != E_SYMBOL)
  398. return NULL;
  399. sym1 = tmp->left.sym;
  400. } else
  401. sym1 = e1->left.sym;
  402. if (e2->type == E_NOT) {
  403. if (e2->left.expr->type != E_SYMBOL)
  404. return NULL;
  405. sym2 = e2->left.expr->left.sym;
  406. } else
  407. sym2 = e2->left.sym;
  408. if (sym1 != sym2)
  409. return NULL;
  410. if (sym1->type != S_BOOLEAN && sym1->type != S_TRISTATE)
  411. return NULL;
  412. if ((e1->type == E_SYMBOL && e2->type == E_EQUAL && e2->right.sym == &symbol_yes) ||
  413. (e2->type == E_SYMBOL && e1->type == E_EQUAL && e1->right.sym == &symbol_yes))
  414. // (a) && (a='y') -> (a='y')
  415. return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
  416. if ((e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_no) ||
  417. (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_no))
  418. // (a) && (a!='n') -> (a)
  419. return expr_alloc_symbol(sym1);
  420. if ((e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_mod) ||
  421. (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_mod))
  422. // (a) && (a!='m') -> (a='y')
  423. return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
  424. if (sym1->type == S_TRISTATE) {
  425. if (e1->type == E_EQUAL && e2->type == E_UNEQUAL) {
  426. // (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
  427. sym2 = e1->right.sym;
  428. if ((e2->right.sym->flags & SYMBOL_CONST) && (sym2->flags & SYMBOL_CONST))
  429. return sym2 != e2->right.sym ? expr_alloc_comp(E_EQUAL, sym1, sym2)
  430. : expr_alloc_symbol(&symbol_no);
  431. }
  432. if (e1->type == E_UNEQUAL && e2->type == E_EQUAL) {
  433. // (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
  434. sym2 = e2->right.sym;
  435. if ((e1->right.sym->flags & SYMBOL_CONST) && (sym2->flags & SYMBOL_CONST))
  436. return sym2 != e1->right.sym ? expr_alloc_comp(E_EQUAL, sym1, sym2)
  437. : expr_alloc_symbol(&symbol_no);
  438. }
  439. if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
  440. ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_no) ||
  441. (e1->right.sym == &symbol_no && e2->right.sym == &symbol_yes)))
  442. // (a!='y') && (a!='n') -> (a='m')
  443. return expr_alloc_comp(E_EQUAL, sym1, &symbol_mod);
  444. if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
  445. ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_mod) ||
  446. (e1->right.sym == &symbol_mod && e2->right.sym == &symbol_yes)))
  447. // (a!='y') && (a!='m') -> (a='n')
  448. return expr_alloc_comp(E_EQUAL, sym1, &symbol_no);
  449. if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
  450. ((e1->right.sym == &symbol_mod && e2->right.sym == &symbol_no) ||
  451. (e1->right.sym == &symbol_no && e2->right.sym == &symbol_mod)))
  452. // (a!='m') && (a!='n') -> (a='m')
  453. return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
  454. if ((e1->type == E_SYMBOL && e2->type == E_EQUAL && e2->right.sym == &symbol_mod) ||
  455. (e2->type == E_SYMBOL && e1->type == E_EQUAL && e1->right.sym == &symbol_mod) ||
  456. (e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_yes) ||
  457. (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_yes))
  458. return NULL;
  459. }
  460. if (DEBUG_EXPR) {
  461. printf("optimize (");
  462. expr_fprint(e1, stdout);
  463. printf(") && (");
  464. expr_fprint(e2, stdout);
  465. printf(")?\n");
  466. }
  467. return NULL;
  468. }
  469. static void expr_eliminate_dups1(enum expr_type type, struct expr **ep1, struct expr **ep2)
  470. {
  471. #define e1 (*ep1)
  472. #define e2 (*ep2)
  473. struct expr *tmp;
  474. if (e1->type == type) {
  475. expr_eliminate_dups1(type, &e1->left.expr, &e2);
  476. expr_eliminate_dups1(type, &e1->right.expr, &e2);
  477. return;
  478. }
  479. if (e2->type == type) {
  480. expr_eliminate_dups1(type, &e1, &e2->left.expr);
  481. expr_eliminate_dups1(type, &e1, &e2->right.expr);
  482. return;
  483. }
  484. if (e1 == e2)
  485. return;
  486. switch (e1->type) {
  487. case E_OR: case E_AND:
  488. expr_eliminate_dups1(e1->type, &e1, &e1);
  489. default:
  490. ;
  491. }
  492. switch (type) {
  493. case E_OR:
  494. tmp = expr_join_or(e1, e2);
  495. if (tmp) {
  496. expr_free(e1); expr_free(e2);
  497. e1 = expr_alloc_symbol(&symbol_no);
  498. e2 = tmp;
  499. trans_count++;
  500. }
  501. break;
  502. case E_AND:
  503. tmp = expr_join_and(e1, e2);
  504. if (tmp) {
  505. expr_free(e1); expr_free(e2);
  506. e1 = expr_alloc_symbol(&symbol_yes);
  507. e2 = tmp;
  508. trans_count++;
  509. }
  510. break;
  511. default:
  512. ;
  513. }
  514. #undef e1
  515. #undef e2
  516. }
  517. static void expr_eliminate_dups2(enum expr_type type, struct expr **ep1, struct expr **ep2)
  518. {
  519. #define e1 (*ep1)
  520. #define e2 (*ep2)
  521. struct expr *tmp, *tmp1, *tmp2;
  522. if (e1->type == type) {
  523. expr_eliminate_dups2(type, &e1->left.expr, &e2);
  524. expr_eliminate_dups2(type, &e1->right.expr, &e2);
  525. return;
  526. }
  527. if (e2->type == type) {
  528. expr_eliminate_dups2(type, &e1, &e2->left.expr);
  529. expr_eliminate_dups2(type, &e1, &e2->right.expr);
  530. }
  531. if (e1 == e2)
  532. return;
  533. switch (e1->type) {
  534. case E_OR:
  535. expr_eliminate_dups2(e1->type, &e1, &e1);
  536. // (FOO || BAR) && (!FOO && !BAR) -> n
  537. tmp1 = expr_transform(expr_alloc_one(E_NOT, expr_copy(e1)));
  538. tmp2 = expr_copy(e2);
  539. tmp = expr_extract_eq_and(&tmp1, &tmp2);
  540. if (expr_is_yes(tmp1)) {
  541. expr_free(e1);
  542. e1 = expr_alloc_symbol(&symbol_no);
  543. trans_count++;
  544. }
  545. expr_free(tmp2);
  546. expr_free(tmp1);
  547. expr_free(tmp);
  548. break;
  549. case E_AND:
  550. expr_eliminate_dups2(e1->type, &e1, &e1);
  551. // (FOO && BAR) || (!FOO || !BAR) -> y
  552. tmp1 = expr_transform(expr_alloc_one(E_NOT, expr_copy(e1)));
  553. tmp2 = expr_copy(e2);
  554. tmp = expr_extract_eq_or(&tmp1, &tmp2);
  555. if (expr_is_no(tmp1)) {
  556. expr_free(e1);
  557. e1 = expr_alloc_symbol(&symbol_yes);
  558. trans_count++;
  559. }
  560. expr_free(tmp2);
  561. expr_free(tmp1);
  562. expr_free(tmp);
  563. break;
  564. default:
  565. ;
  566. }
  567. #undef e1
  568. #undef e2
  569. }
  570. struct expr *expr_eliminate_dups(struct expr *e)
  571. {
  572. int oldcount;
  573. if (!e)
  574. return e;
  575. oldcount = trans_count;
  576. while (1) {
  577. trans_count = 0;
  578. switch (e->type) {
  579. case E_OR: case E_AND:
  580. expr_eliminate_dups1(e->type, &e, &e);
  581. expr_eliminate_dups2(e->type, &e, &e);
  582. default:
  583. ;
  584. }
  585. if (!trans_count)
  586. break;
  587. e = expr_eliminate_yn(e);
  588. }
  589. trans_count = oldcount;
  590. return e;
  591. }
  592. struct expr *expr_transform(struct expr *e)
  593. {
  594. struct expr *tmp;
  595. if (!e)
  596. return NULL;
  597. switch (e->type) {
  598. case E_EQUAL:
  599. case E_UNEQUAL:
  600. case E_SYMBOL:
  601. case E_LIST:
  602. break;
  603. default:
  604. e->left.expr = expr_transform(e->left.expr);
  605. e->right.expr = expr_transform(e->right.expr);
  606. }
  607. switch (e->type) {
  608. case E_EQUAL:
  609. if (e->left.sym->type != S_BOOLEAN)
  610. break;
  611. if (e->right.sym == &symbol_no) {
  612. e->type = E_NOT;
  613. e->left.expr = expr_alloc_symbol(e->left.sym);
  614. e->right.sym = NULL;
  615. break;
  616. }
  617. if (e->right.sym == &symbol_mod) {
  618. printf("boolean symbol %s tested for 'm'? test forced to 'n'\n", e->left.sym->name);
  619. e->type = E_SYMBOL;
  620. e->left.sym = &symbol_no;
  621. e->right.sym = NULL;
  622. break;
  623. }
  624. if (e->right.sym == &symbol_yes) {
  625. e->type = E_SYMBOL;
  626. e->right.sym = NULL;
  627. break;
  628. }
  629. break;
  630. case E_UNEQUAL:
  631. if (e->left.sym->type != S_BOOLEAN)
  632. break;
  633. if (e->right.sym == &symbol_no) {
  634. e->type = E_SYMBOL;
  635. e->right.sym = NULL;
  636. break;
  637. }
  638. if (e->right.sym == &symbol_mod) {
  639. printf("boolean symbol %s tested for 'm'? test forced to 'y'\n", e->left.sym->name);
  640. e->type = E_SYMBOL;
  641. e->left.sym = &symbol_yes;
  642. e->right.sym = NULL;
  643. break;
  644. }
  645. if (e->right.sym == &symbol_yes) {
  646. e->type = E_NOT;
  647. e->left.expr = expr_alloc_symbol(e->left.sym);
  648. e->right.sym = NULL;
  649. break;
  650. }
  651. break;
  652. case E_NOT:
  653. switch (e->left.expr->type) {
  654. case E_NOT:
  655. // !!a -> a
  656. tmp = e->left.expr->left.expr;
  657. free(e->left.expr);
  658. free(e);
  659. e = tmp;
  660. e = expr_transform(e);
  661. break;
  662. case E_EQUAL:
  663. case E_UNEQUAL:
  664. // !a='x' -> a!='x'
  665. tmp = e->left.expr;
  666. free(e);
  667. e = tmp;
  668. e->type = e->type == E_EQUAL ? E_UNEQUAL : E_EQUAL;
  669. break;
  670. case E_OR:
  671. // !(a || b) -> !a && !b
  672. tmp = e->left.expr;
  673. e->type = E_AND;
  674. e->right.expr = expr_alloc_one(E_NOT, tmp->right.expr);
  675. tmp->type = E_NOT;
  676. tmp->right.expr = NULL;
  677. e = expr_transform(e);
  678. break;
  679. case E_AND:
  680. // !(a && b) -> !a || !b
  681. tmp = e->left.expr;
  682. e->type = E_OR;
  683. e->right.expr = expr_alloc_one(E_NOT, tmp->right.expr);
  684. tmp->type = E_NOT;
  685. tmp->right.expr = NULL;
  686. e = expr_transform(e);
  687. break;
  688. case E_SYMBOL:
  689. if (e->left.expr->left.sym == &symbol_yes) {
  690. // !'y' -> 'n'
  691. tmp = e->left.expr;
  692. free(e);
  693. e = tmp;
  694. e->type = E_SYMBOL;
  695. e->left.sym = &symbol_no;
  696. break;
  697. }
  698. if (e->left.expr->left.sym == &symbol_mod) {
  699. // !'m' -> 'm'
  700. tmp = e->left.expr;
  701. free(e);
  702. e = tmp;
  703. e->type = E_SYMBOL;
  704. e->left.sym = &symbol_mod;
  705. break;
  706. }
  707. if (e->left.expr->left.sym == &symbol_no) {
  708. // !'n' -> 'y'
  709. tmp = e->left.expr;
  710. free(e);
  711. e = tmp;
  712. e->type = E_SYMBOL;
  713. e->left.sym = &symbol_yes;
  714. break;
  715. }
  716. break;
  717. default:
  718. ;
  719. }
  720. break;
  721. default:
  722. ;
  723. }
  724. return e;
  725. }
  726. int expr_contains_symbol(struct expr *dep, struct symbol *sym)
  727. {
  728. if (!dep)
  729. return 0;
  730. switch (dep->type) {
  731. case E_AND:
  732. case E_OR:
  733. return expr_contains_symbol(dep->left.expr, sym) ||
  734. expr_contains_symbol(dep->right.expr, sym);
  735. case E_SYMBOL:
  736. return dep->left.sym == sym;
  737. case E_EQUAL:
  738. case E_UNEQUAL:
  739. return dep->left.sym == sym ||
  740. dep->right.sym == sym;
  741. case E_NOT:
  742. return expr_contains_symbol(dep->left.expr, sym);
  743. default:
  744. ;
  745. }
  746. return 0;
  747. }
  748. bool expr_depends_symbol(struct expr *dep, struct symbol *sym)
  749. {
  750. if (!dep)
  751. return false;
  752. switch (dep->type) {
  753. case E_AND:
  754. return expr_depends_symbol(dep->left.expr, sym) ||
  755. expr_depends_symbol(dep->right.expr, sym);
  756. case E_SYMBOL:
  757. return dep->left.sym == sym;
  758. case E_EQUAL:
  759. if (dep->left.sym == sym) {
  760. if (dep->right.sym == &symbol_yes || dep->right.sym == &symbol_mod)
  761. return true;
  762. }
  763. break;
  764. case E_UNEQUAL:
  765. if (dep->left.sym == sym) {
  766. if (dep->right.sym == &symbol_no)
  767. return true;
  768. }
  769. break;
  770. default:
  771. ;
  772. }
  773. return false;
  774. }
  775. struct expr *expr_extract_eq_and(struct expr **ep1, struct expr **ep2)
  776. {
  777. struct expr *tmp = NULL;
  778. expr_extract_eq(E_AND, &tmp, ep1, ep2);
  779. if (tmp) {
  780. *ep1 = expr_eliminate_yn(*ep1);
  781. *ep2 = expr_eliminate_yn(*ep2);
  782. }
  783. return tmp;
  784. }
  785. struct expr *expr_extract_eq_or(struct expr **ep1, struct expr **ep2)
  786. {
  787. struct expr *tmp = NULL;
  788. expr_extract_eq(E_OR, &tmp, ep1, ep2);
  789. if (tmp) {
  790. *ep1 = expr_eliminate_yn(*ep1);
  791. *ep2 = expr_eliminate_yn(*ep2);
  792. }
  793. return tmp;
  794. }
  795. void expr_extract_eq(enum expr_type type, struct expr **ep, struct expr **ep1, struct expr **ep2)
  796. {
  797. #define e1 (*ep1)
  798. #define e2 (*ep2)
  799. if (e1->type == type) {
  800. expr_extract_eq(type, ep, &e1->left.expr, &e2);
  801. expr_extract_eq(type, ep, &e1->right.expr, &e2);
  802. return;
  803. }
  804. if (e2->type == type) {
  805. expr_extract_eq(type, ep, ep1, &e2->left.expr);
  806. expr_extract_eq(type, ep, ep1, &e2->right.expr);
  807. return;
  808. }
  809. if (expr_eq(e1, e2)) {
  810. *ep = *ep ? expr_alloc_two(type, *ep, e1) : e1;
  811. expr_free(e2);
  812. if (type == E_AND) {
  813. e1 = expr_alloc_symbol(&symbol_yes);
  814. e2 = expr_alloc_symbol(&symbol_yes);
  815. } else if (type == E_OR) {
  816. e1 = expr_alloc_symbol(&symbol_no);
  817. e2 = expr_alloc_symbol(&symbol_no);
  818. }
  819. }
  820. #undef e1
  821. #undef e2
  822. }
  823. struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym)
  824. {
  825. struct expr *e1, *e2;
  826. if (!e) {
  827. e = expr_alloc_symbol(sym);
  828. if (type == E_UNEQUAL)
  829. e = expr_alloc_one(E_NOT, e);
  830. return e;
  831. }
  832. switch (e->type) {
  833. case E_AND:
  834. e1 = expr_trans_compare(e->left.expr, E_EQUAL, sym);
  835. e2 = expr_trans_compare(e->right.expr, E_EQUAL, sym);
  836. if (sym == &symbol_yes)
  837. e = expr_alloc_two(E_AND, e1, e2);
  838. if (sym == &symbol_no)
  839. e = expr_alloc_two(E_OR, e1, e2);
  840. if (type == E_UNEQUAL)
  841. e = expr_alloc_one(E_NOT, e);
  842. return e;
  843. case E_OR:
  844. e1 = expr_trans_compare(e->left.expr, E_EQUAL, sym);
  845. e2 = expr_trans_compare(e->right.expr, E_EQUAL, sym);
  846. if (sym == &symbol_yes)
  847. e = expr_alloc_two(E_OR, e1, e2);
  848. if (sym == &symbol_no)
  849. e = expr_alloc_two(E_AND, e1, e2);
  850. if (type == E_UNEQUAL)
  851. e = expr_alloc_one(E_NOT, e);
  852. return e;
  853. case E_NOT:
  854. return expr_trans_compare(e->left.expr, type == E_EQUAL ? E_UNEQUAL : E_EQUAL, sym);
  855. case E_UNEQUAL:
  856. case E_EQUAL:
  857. if (type == E_EQUAL) {
  858. if (sym == &symbol_yes)
  859. return expr_copy(e);
  860. if (sym == &symbol_mod)
  861. return expr_alloc_symbol(&symbol_no);
  862. if (sym == &symbol_no)
  863. return expr_alloc_one(E_NOT, expr_copy(e));
  864. } else {
  865. if (sym == &symbol_yes)
  866. return expr_alloc_one(E_NOT, expr_copy(e));
  867. if (sym == &symbol_mod)
  868. return expr_alloc_symbol(&symbol_yes);
  869. if (sym == &symbol_no)
  870. return expr_copy(e);
  871. }
  872. break;
  873. case E_SYMBOL:
  874. return expr_alloc_comp(type, e->left.sym, sym);
  875. case E_LIST:
  876. case E_RANGE:
  877. case E_NONE:
  878. /* panic */;
  879. }
  880. return NULL;
  881. }
  882. tristate expr_calc_value(struct expr *e)
  883. {
  884. tristate val1, val2;
  885. const char *str1, *str2;
  886. if (!e)
  887. return yes;
  888. switch (e->type) {
  889. case E_SYMBOL:
  890. sym_calc_value(e->left.sym);
  891. return e->left.sym->curr.tri;
  892. case E_AND:
  893. val1 = expr_calc_value(e->left.expr);
  894. val2 = expr_calc_value(e->right.expr);
  895. return EXPR_AND(val1, val2);
  896. case E_OR:
  897. val1 = expr_calc_value(e->left.expr);
  898. val2 = expr_calc_value(e->right.expr);
  899. return EXPR_OR(val1, val2);
  900. case E_NOT:
  901. val1 = expr_calc_value(e->left.expr);
  902. return EXPR_NOT(val1);
  903. case E_EQUAL:
  904. sym_calc_value(e->left.sym);
  905. sym_calc_value(e->right.sym);
  906. str1 = sym_get_string_value(e->left.sym);
  907. str2 = sym_get_string_value(e->right.sym);
  908. return !strcmp(str1, str2) ? yes : no;
  909. case E_UNEQUAL:
  910. sym_calc_value(e->left.sym);
  911. sym_calc_value(e->right.sym);
  912. str1 = sym_get_string_value(e->left.sym);
  913. str2 = sym_get_string_value(e->right.sym);
  914. return !strcmp(str1, str2) ? no : yes;
  915. default:
  916. printf("expr_calc_value: %d?\n", e->type);
  917. return no;
  918. }
  919. }
  920. int expr_compare_type(enum expr_type t1, enum expr_type t2)
  921. {
  922. #if 0
  923. return 1;
  924. #else
  925. if (t1 == t2)
  926. return 0;
  927. switch (t1) {
  928. case E_EQUAL:
  929. case E_UNEQUAL:
  930. if (t2 == E_NOT)
  931. return 1;
  932. case E_NOT:
  933. if (t2 == E_AND)
  934. return 1;
  935. case E_AND:
  936. if (t2 == E_OR)
  937. return 1;
  938. case E_OR:
  939. if (t2 == E_LIST)
  940. return 1;
  941. case E_LIST:
  942. if (t2 == 0)
  943. return 1;
  944. default:
  945. return -1;
  946. }
  947. printf("[%dgt%d?]", t1, t2);
  948. return 0;
  949. #endif
  950. }
  951. static inline struct expr *
  952. expr_get_leftmost_symbol(const struct expr *e)
  953. {
  954. if (e == NULL)
  955. return NULL;
  956. while (e->type != E_SYMBOL)
  957. e = e->left.expr;
  958. return expr_copy(e);
  959. }
  960. /*
  961. * Given expression `e1' and `e2', returns the leaf of the longest
  962. * sub-expression of `e1' not containing 'e2.
  963. */
  964. struct expr *expr_simplify_unmet_dep(struct expr *e1, struct expr *e2)
  965. {
  966. struct expr *ret;
  967. switch (e1->type) {
  968. case E_OR:
  969. return expr_alloc_and(
  970. expr_simplify_unmet_dep(e1->left.expr, e2),
  971. expr_simplify_unmet_dep(e1->right.expr, e2));
  972. case E_AND: {
  973. struct expr *e;
  974. e = expr_alloc_and(expr_copy(e1), expr_copy(e2));
  975. e = expr_eliminate_dups(e);
  976. ret = (!expr_eq(e, e1)) ? e1 : NULL;
  977. expr_free(e);
  978. break;
  979. }
  980. default:
  981. ret = e1;
  982. break;
  983. }
  984. return expr_get_leftmost_symbol(ret);
  985. }
  986. void expr_print(struct expr *e, void (*fn)(void *, struct symbol *, const char *), void *data, int prevtoken)
  987. {
  988. if (!e) {
  989. fn(data, NULL, "y");
  990. return;
  991. }
  992. if (expr_compare_type(prevtoken, e->type) > 0)
  993. fn(data, NULL, "(");
  994. switch (e->type) {
  995. case E_SYMBOL:
  996. if (e->left.sym->name)
  997. fn(data, e->left.sym, e->left.sym->name);
  998. else
  999. fn(data, NULL, "<choice>");
  1000. break;
  1001. case E_NOT:
  1002. fn(data, NULL, "!");
  1003. expr_print(e->left.expr, fn, data, E_NOT);
  1004. break;
  1005. case E_EQUAL:
  1006. if (e->left.sym->name)
  1007. fn(data, e->left.sym, e->left.sym->name);
  1008. else
  1009. fn(data, NULL, "<choice>");
  1010. fn(data, NULL, "=");
  1011. fn(data, e->right.sym, e->right.sym->name);
  1012. break;
  1013. case E_UNEQUAL:
  1014. if (e->left.sym->name)
  1015. fn(data, e->left.sym, e->left.sym->name);
  1016. else
  1017. fn(data, NULL, "<choice>");
  1018. fn(data, NULL, "!=");
  1019. fn(data, e->right.sym, e->right.sym->name);
  1020. break;
  1021. case E_OR:
  1022. expr_print(e->left.expr, fn, data, E_OR);
  1023. fn(data, NULL, " || ");
  1024. expr_print(e->right.expr, fn, data, E_OR);
  1025. break;
  1026. case E_AND:
  1027. expr_print(e->left.expr, fn, data, E_AND);
  1028. fn(data, NULL, " && ");
  1029. expr_print(e->right.expr, fn, data, E_AND);
  1030. break;
  1031. case E_LIST:
  1032. fn(data, e->right.sym, e->right.sym->name);
  1033. if (e->left.expr) {
  1034. fn(data, NULL, " ^ ");
  1035. expr_print(e->left.expr, fn, data, E_LIST);
  1036. }
  1037. break;
  1038. case E_RANGE:
  1039. fn(data, NULL, "[");
  1040. fn(data, e->left.sym, e->left.sym->name);
  1041. fn(data, NULL, " ");
  1042. fn(data, e->right.sym, e->right.sym->name);
  1043. fn(data, NULL, "]");
  1044. break;
  1045. default:
  1046. {
  1047. char buf[32];
  1048. sprintf(buf, "<unknown type %d>", e->type);
  1049. fn(data, NULL, buf);
  1050. break;
  1051. }
  1052. }
  1053. if (expr_compare_type(prevtoken, e->type) > 0)
  1054. fn(data, NULL, ")");
  1055. }
  1056. static void expr_print_file_helper(void *data, struct symbol *sym, const char *str)
  1057. {
  1058. xfwrite(str, strlen(str), 1, data);
  1059. }
  1060. void expr_fprint(struct expr *e, FILE *out)
  1061. {
  1062. expr_print(e, expr_print_file_helper, out, E_NONE);
  1063. }
  1064. static void expr_print_gstr_helper(void *data, struct symbol *sym, const char *str)
  1065. {
  1066. struct gstr *gs = (struct gstr*)data;
  1067. const char *sym_str = NULL;
  1068. if (sym)
  1069. sym_str = sym_get_string_value(sym);
  1070. if (gs->max_width) {
  1071. unsigned extra_length = strlen(str);
  1072. const char *last_cr = strrchr(gs->s, '\n');
  1073. unsigned last_line_length;
  1074. if (sym_str)
  1075. extra_length += 4 + strlen(sym_str);
  1076. if (!last_cr)
  1077. last_cr = gs->s;
  1078. last_line_length = strlen(gs->s) - (last_cr - gs->s);
  1079. if ((last_line_length + extra_length) > gs->max_width)
  1080. str_append(gs, "\\\n");
  1081. }
  1082. str_append(gs, str);
  1083. if (sym && sym->type != S_UNKNOWN)
  1084. str_printf(gs, " [=%s]", sym_str);
  1085. }
  1086. void expr_gstr_print(struct expr *e, struct gstr *gs)
  1087. {
  1088. expr_print(e, expr_print_gstr_helper, gs, E_NONE);
  1089. }