expr.c 27 KB

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