expr.c 24 KB

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