expr.c 24 KB

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