expr.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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. fprintf(stderr, "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. fprintf(stderr, "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. /*
  123. * expr_eliminate_eq() helper.
  124. *
  125. * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
  126. * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
  127. * against all other leaves. Two equal leaves are both replaced with either 'y'
  128. * or 'n' as appropriate for 'type', to be eliminated later.
  129. */
  130. static void __expr_eliminate_eq(enum expr_type type, struct expr **ep1, struct expr **ep2)
  131. {
  132. /* Recurse down to leaves */
  133. if (e1->type == type) {
  134. __expr_eliminate_eq(type, &e1->left.expr, &e2);
  135. __expr_eliminate_eq(type, &e1->right.expr, &e2);
  136. return;
  137. }
  138. if (e2->type == type) {
  139. __expr_eliminate_eq(type, &e1, &e2->left.expr);
  140. __expr_eliminate_eq(type, &e1, &e2->right.expr);
  141. return;
  142. }
  143. /* e1 and e2 are leaves. Compare them. */
  144. if (e1->type == E_SYMBOL && e2->type == E_SYMBOL &&
  145. e1->left.sym == e2->left.sym &&
  146. (e1->left.sym == &symbol_yes || e1->left.sym == &symbol_no))
  147. return;
  148. if (!expr_eq(e1, e2))
  149. return;
  150. /* e1 and e2 are equal leaves. Prepare them for elimination. */
  151. trans_count++;
  152. expr_free(e1); expr_free(e2);
  153. switch (type) {
  154. case E_OR:
  155. e1 = expr_alloc_symbol(&symbol_no);
  156. e2 = expr_alloc_symbol(&symbol_no);
  157. break;
  158. case E_AND:
  159. e1 = expr_alloc_symbol(&symbol_yes);
  160. e2 = expr_alloc_symbol(&symbol_yes);
  161. break;
  162. default:
  163. ;
  164. }
  165. }
  166. /*
  167. * Rewrites the expressions 'ep1' and 'ep2' to remove operands common to both.
  168. * Example reductions:
  169. *
  170. * ep1: A && B -> ep1: y
  171. * ep2: A && B && C -> ep2: C
  172. *
  173. * ep1: A || B -> ep1: n
  174. * ep2: A || B || C -> ep2: C
  175. *
  176. * ep1: A && (B && FOO) -> ep1: FOO
  177. * ep2: (BAR && B) && A -> ep2: BAR
  178. *
  179. * ep1: A && (B || C) -> ep1: y
  180. * ep2: (C || B) && A -> ep2: y
  181. *
  182. * Comparisons are done between all operands at the same "level" of && or ||.
  183. * For example, in the expression 'e1 && (e2 || e3) && (e4 || e5)', the
  184. * following operands will be compared:
  185. *
  186. * - 'e1', 'e2 || e3', and 'e4 || e5', against each other
  187. * - e2 against e3
  188. * - e4 against e5
  189. *
  190. * Parentheses are irrelevant within a single level. 'e1 && (e2 && e3)' and
  191. * '(e1 && e2) && e3' are both a single level.
  192. *
  193. * See __expr_eliminate_eq() as well.
  194. */
  195. void expr_eliminate_eq(struct expr **ep1, struct expr **ep2)
  196. {
  197. if (!e1 || !e2)
  198. return;
  199. switch (e1->type) {
  200. case E_OR:
  201. case E_AND:
  202. __expr_eliminate_eq(e1->type, ep1, ep2);
  203. default:
  204. ;
  205. }
  206. if (e1->type != e2->type) switch (e2->type) {
  207. case E_OR:
  208. case E_AND:
  209. __expr_eliminate_eq(e2->type, ep1, ep2);
  210. default:
  211. ;
  212. }
  213. e1 = expr_eliminate_yn(e1);
  214. e2 = expr_eliminate_yn(e2);
  215. }
  216. #undef e1
  217. #undef e2
  218. /*
  219. * Returns true if 'e1' and 'e2' are equal, after minor simplification. Two
  220. * &&/|| expressions are considered equal if every operand in one expression
  221. * equals some operand in the other (operands do not need to appear in the same
  222. * order), recursively.
  223. */
  224. static int expr_eq(struct expr *e1, struct expr *e2)
  225. {
  226. int res, old_count;
  227. /*
  228. * A NULL expr is taken to be yes, but there's also a different way to
  229. * represent yes. expr_is_yes() checks for either representation.
  230. */
  231. if (!e1 || !e2)
  232. return expr_is_yes(e1) && expr_is_yes(e2);
  233. if (e1->type != e2->type)
  234. return 0;
  235. switch (e1->type) {
  236. case E_EQUAL:
  237. case E_GEQ:
  238. case E_GTH:
  239. case E_LEQ:
  240. case E_LTH:
  241. case E_UNEQUAL:
  242. return e1->left.sym == e2->left.sym && e1->right.sym == e2->right.sym;
  243. case E_SYMBOL:
  244. return e1->left.sym == e2->left.sym;
  245. case E_NOT:
  246. return expr_eq(e1->left.expr, e2->left.expr);
  247. case E_AND:
  248. case E_OR:
  249. e1 = expr_copy(e1);
  250. e2 = expr_copy(e2);
  251. old_count = trans_count;
  252. expr_eliminate_eq(&e1, &e2);
  253. res = (e1->type == E_SYMBOL && e2->type == E_SYMBOL &&
  254. e1->left.sym == e2->left.sym);
  255. expr_free(e1);
  256. expr_free(e2);
  257. trans_count = old_count;
  258. return res;
  259. case E_LIST:
  260. case E_RANGE:
  261. case E_NONE:
  262. /* panic */;
  263. }
  264. if (DEBUG_EXPR) {
  265. expr_fprint(e1, stdout);
  266. printf(" = ");
  267. expr_fprint(e2, stdout);
  268. printf(" ?\n");
  269. }
  270. return 0;
  271. }
  272. /*
  273. * Recursively performs the following simplifications in-place (as well as the
  274. * corresponding simplifications with swapped operands):
  275. *
  276. * expr && n -> n
  277. * expr && y -> expr
  278. * expr || n -> expr
  279. * expr || y -> y
  280. *
  281. * Returns the optimized expression.
  282. */
  283. static struct expr *expr_eliminate_yn(struct expr *e)
  284. {
  285. struct expr *tmp;
  286. if (e) switch (e->type) {
  287. case E_AND:
  288. e->left.expr = expr_eliminate_yn(e->left.expr);
  289. e->right.expr = expr_eliminate_yn(e->right.expr);
  290. if (e->left.expr->type == E_SYMBOL) {
  291. if (e->left.expr->left.sym == &symbol_no) {
  292. expr_free(e->left.expr);
  293. expr_free(e->right.expr);
  294. e->type = E_SYMBOL;
  295. e->left.sym = &symbol_no;
  296. e->right.expr = NULL;
  297. return e;
  298. } else if (e->left.expr->left.sym == &symbol_yes) {
  299. free(e->left.expr);
  300. tmp = e->right.expr;
  301. *e = *(e->right.expr);
  302. free(tmp);
  303. return e;
  304. }
  305. }
  306. if (e->right.expr->type == E_SYMBOL) {
  307. if (e->right.expr->left.sym == &symbol_no) {
  308. expr_free(e->left.expr);
  309. expr_free(e->right.expr);
  310. e->type = E_SYMBOL;
  311. e->left.sym = &symbol_no;
  312. e->right.expr = NULL;
  313. return e;
  314. } else if (e->right.expr->left.sym == &symbol_yes) {
  315. free(e->right.expr);
  316. tmp = e->left.expr;
  317. *e = *(e->left.expr);
  318. free(tmp);
  319. return e;
  320. }
  321. }
  322. break;
  323. case E_OR:
  324. e->left.expr = expr_eliminate_yn(e->left.expr);
  325. e->right.expr = expr_eliminate_yn(e->right.expr);
  326. if (e->left.expr->type == E_SYMBOL) {
  327. if (e->left.expr->left.sym == &symbol_no) {
  328. free(e->left.expr);
  329. tmp = e->right.expr;
  330. *e = *(e->right.expr);
  331. free(tmp);
  332. return e;
  333. } else if (e->left.expr->left.sym == &symbol_yes) {
  334. expr_free(e->left.expr);
  335. expr_free(e->right.expr);
  336. e->type = E_SYMBOL;
  337. e->left.sym = &symbol_yes;
  338. e->right.expr = NULL;
  339. return e;
  340. }
  341. }
  342. if (e->right.expr->type == E_SYMBOL) {
  343. if (e->right.expr->left.sym == &symbol_no) {
  344. free(e->right.expr);
  345. tmp = e->left.expr;
  346. *e = *(e->left.expr);
  347. free(tmp);
  348. return e;
  349. } else if (e->right.expr->left.sym == &symbol_yes) {
  350. expr_free(e->left.expr);
  351. expr_free(e->right.expr);
  352. e->type = E_SYMBOL;
  353. e->left.sym = &symbol_yes;
  354. e->right.expr = NULL;
  355. return e;
  356. }
  357. }
  358. break;
  359. default:
  360. ;
  361. }
  362. return e;
  363. }
  364. /*
  365. * bool FOO!=n => FOO
  366. */
  367. struct expr *expr_trans_bool(struct expr *e)
  368. {
  369. if (!e)
  370. return NULL;
  371. switch (e->type) {
  372. case E_AND:
  373. case E_OR:
  374. case E_NOT:
  375. e->left.expr = expr_trans_bool(e->left.expr);
  376. e->right.expr = expr_trans_bool(e->right.expr);
  377. break;
  378. case E_UNEQUAL:
  379. // FOO!=n -> FOO
  380. if (e->left.sym->type == S_TRISTATE) {
  381. if (e->right.sym == &symbol_no) {
  382. e->type = E_SYMBOL;
  383. e->right.sym = NULL;
  384. }
  385. }
  386. break;
  387. default:
  388. ;
  389. }
  390. return e;
  391. }
  392. /*
  393. * e1 || e2 -> ?
  394. */
  395. static struct expr *expr_join_or(struct expr *e1, struct expr *e2)
  396. {
  397. struct expr *tmp;
  398. struct symbol *sym1, *sym2;
  399. if (expr_eq(e1, e2))
  400. return expr_copy(e1);
  401. if (e1->type != E_EQUAL && e1->type != E_UNEQUAL && e1->type != E_SYMBOL && e1->type != E_NOT)
  402. return NULL;
  403. if (e2->type != E_EQUAL && e2->type != E_UNEQUAL && e2->type != E_SYMBOL && e2->type != E_NOT)
  404. return NULL;
  405. if (e1->type == E_NOT) {
  406. tmp = e1->left.expr;
  407. if (tmp->type != E_EQUAL && tmp->type != E_UNEQUAL && tmp->type != E_SYMBOL)
  408. return NULL;
  409. sym1 = tmp->left.sym;
  410. } else
  411. sym1 = e1->left.sym;
  412. if (e2->type == E_NOT) {
  413. if (e2->left.expr->type != E_SYMBOL)
  414. return NULL;
  415. sym2 = e2->left.expr->left.sym;
  416. } else
  417. sym2 = e2->left.sym;
  418. if (sym1 != sym2)
  419. return NULL;
  420. if (sym1->type != S_BOOLEAN && sym1->type != S_TRISTATE)
  421. return NULL;
  422. if (sym1->type == S_TRISTATE) {
  423. if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
  424. ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_mod) ||
  425. (e1->right.sym == &symbol_mod && e2->right.sym == &symbol_yes))) {
  426. // (a='y') || (a='m') -> (a!='n')
  427. return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_no);
  428. }
  429. if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
  430. ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_no) ||
  431. (e1->right.sym == &symbol_no && e2->right.sym == &symbol_yes))) {
  432. // (a='y') || (a='n') -> (a!='m')
  433. return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_mod);
  434. }
  435. if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
  436. ((e1->right.sym == &symbol_mod && e2->right.sym == &symbol_no) ||
  437. (e1->right.sym == &symbol_no && e2->right.sym == &symbol_mod))) {
  438. // (a='m') || (a='n') -> (a!='y')
  439. return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_yes);
  440. }
  441. }
  442. if (sym1->type == S_BOOLEAN && sym1 == sym2) {
  443. if ((e1->type == E_NOT && e1->left.expr->type == E_SYMBOL && e2->type == E_SYMBOL) ||
  444. (e2->type == E_NOT && e2->left.expr->type == E_SYMBOL && e1->type == E_SYMBOL))
  445. return expr_alloc_symbol(&symbol_yes);
  446. }
  447. if (DEBUG_EXPR) {
  448. printf("optimize (");
  449. expr_fprint(e1, stdout);
  450. printf(") || (");
  451. expr_fprint(e2, stdout);
  452. printf(")?\n");
  453. }
  454. return NULL;
  455. }
  456. static struct expr *expr_join_and(struct expr *e1, struct expr *e2)
  457. {
  458. struct expr *tmp;
  459. struct symbol *sym1, *sym2;
  460. if (expr_eq(e1, e2))
  461. return expr_copy(e1);
  462. if (e1->type != E_EQUAL && e1->type != E_UNEQUAL && e1->type != E_SYMBOL && e1->type != E_NOT)
  463. return NULL;
  464. if (e2->type != E_EQUAL && e2->type != E_UNEQUAL && e2->type != E_SYMBOL && e2->type != E_NOT)
  465. return NULL;
  466. if (e1->type == E_NOT) {
  467. tmp = e1->left.expr;
  468. if (tmp->type != E_EQUAL && tmp->type != E_UNEQUAL && tmp->type != E_SYMBOL)
  469. return NULL;
  470. sym1 = tmp->left.sym;
  471. } else
  472. sym1 = e1->left.sym;
  473. if (e2->type == E_NOT) {
  474. if (e2->left.expr->type != E_SYMBOL)
  475. return NULL;
  476. sym2 = e2->left.expr->left.sym;
  477. } else
  478. sym2 = e2->left.sym;
  479. if (sym1 != sym2)
  480. return NULL;
  481. if (sym1->type != S_BOOLEAN && sym1->type != S_TRISTATE)
  482. return NULL;
  483. if ((e1->type == E_SYMBOL && e2->type == E_EQUAL && e2->right.sym == &symbol_yes) ||
  484. (e2->type == E_SYMBOL && e1->type == E_EQUAL && e1->right.sym == &symbol_yes))
  485. // (a) && (a='y') -> (a='y')
  486. return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
  487. if ((e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_no) ||
  488. (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_no))
  489. // (a) && (a!='n') -> (a)
  490. return expr_alloc_symbol(sym1);
  491. if ((e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_mod) ||
  492. (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_mod))
  493. // (a) && (a!='m') -> (a='y')
  494. return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
  495. if (sym1->type == S_TRISTATE) {
  496. if (e1->type == E_EQUAL && e2->type == E_UNEQUAL) {
  497. // (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
  498. sym2 = e1->right.sym;
  499. if ((e2->right.sym->flags & SYMBOL_CONST) && (sym2->flags & SYMBOL_CONST))
  500. return sym2 != e2->right.sym ? expr_alloc_comp(E_EQUAL, sym1, sym2)
  501. : expr_alloc_symbol(&symbol_no);
  502. }
  503. if (e1->type == E_UNEQUAL && e2->type == E_EQUAL) {
  504. // (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
  505. sym2 = e2->right.sym;
  506. if ((e1->right.sym->flags & SYMBOL_CONST) && (sym2->flags & SYMBOL_CONST))
  507. return sym2 != e1->right.sym ? expr_alloc_comp(E_EQUAL, sym1, sym2)
  508. : expr_alloc_symbol(&symbol_no);
  509. }
  510. if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
  511. ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_no) ||
  512. (e1->right.sym == &symbol_no && e2->right.sym == &symbol_yes)))
  513. // (a!='y') && (a!='n') -> (a='m')
  514. return expr_alloc_comp(E_EQUAL, sym1, &symbol_mod);
  515. if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
  516. ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_mod) ||
  517. (e1->right.sym == &symbol_mod && e2->right.sym == &symbol_yes)))
  518. // (a!='y') && (a!='m') -> (a='n')
  519. return expr_alloc_comp(E_EQUAL, sym1, &symbol_no);
  520. if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
  521. ((e1->right.sym == &symbol_mod && e2->right.sym == &symbol_no) ||
  522. (e1->right.sym == &symbol_no && e2->right.sym == &symbol_mod)))
  523. // (a!='m') && (a!='n') -> (a='m')
  524. return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
  525. if ((e1->type == E_SYMBOL && e2->type == E_EQUAL && e2->right.sym == &symbol_mod) ||
  526. (e2->type == E_SYMBOL && e1->type == E_EQUAL && e1->right.sym == &symbol_mod) ||
  527. (e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_yes) ||
  528. (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_yes))
  529. return NULL;
  530. }
  531. if (DEBUG_EXPR) {
  532. printf("optimize (");
  533. expr_fprint(e1, stdout);
  534. printf(") && (");
  535. expr_fprint(e2, stdout);
  536. printf(")?\n");
  537. }
  538. return NULL;
  539. }
  540. /*
  541. * expr_eliminate_dups() helper.
  542. *
  543. * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
  544. * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
  545. * against all other leaves to look for simplifications.
  546. */
  547. static void expr_eliminate_dups1(enum expr_type type, struct expr **ep1, struct expr **ep2)
  548. {
  549. #define e1 (*ep1)
  550. #define e2 (*ep2)
  551. struct expr *tmp;
  552. /* Recurse down to leaves */
  553. if (e1->type == type) {
  554. expr_eliminate_dups1(type, &e1->left.expr, &e2);
  555. expr_eliminate_dups1(type, &e1->right.expr, &e2);
  556. return;
  557. }
  558. if (e2->type == type) {
  559. expr_eliminate_dups1(type, &e1, &e2->left.expr);
  560. expr_eliminate_dups1(type, &e1, &e2->right.expr);
  561. return;
  562. }
  563. /* e1 and e2 are leaves. Compare and process them. */
  564. if (e1 == e2)
  565. return;
  566. switch (e1->type) {
  567. case E_OR: case E_AND:
  568. expr_eliminate_dups1(e1->type, &e1, &e1);
  569. default:
  570. ;
  571. }
  572. switch (type) {
  573. case E_OR:
  574. tmp = expr_join_or(e1, e2);
  575. if (tmp) {
  576. expr_free(e1); expr_free(e2);
  577. e1 = expr_alloc_symbol(&symbol_no);
  578. e2 = tmp;
  579. trans_count++;
  580. }
  581. break;
  582. case E_AND:
  583. tmp = expr_join_and(e1, e2);
  584. if (tmp) {
  585. expr_free(e1); expr_free(e2);
  586. e1 = expr_alloc_symbol(&symbol_yes);
  587. e2 = tmp;
  588. trans_count++;
  589. }
  590. break;
  591. default:
  592. ;
  593. }
  594. #undef e1
  595. #undef e2
  596. }
  597. /*
  598. * Rewrites 'e' in-place to remove ("join") duplicate and other redundant
  599. * operands.
  600. *
  601. * Example simplifications:
  602. *
  603. * A || B || A -> A || B
  604. * A && B && A=y -> A=y && B
  605. *
  606. * Returns the deduplicated expression.
  607. */
  608. struct expr *expr_eliminate_dups(struct expr *e)
  609. {
  610. int oldcount;
  611. if (!e)
  612. return e;
  613. oldcount = trans_count;
  614. while (1) {
  615. trans_count = 0;
  616. switch (e->type) {
  617. case E_OR: case E_AND:
  618. expr_eliminate_dups1(e->type, &e, &e);
  619. default:
  620. ;
  621. }
  622. if (!trans_count)
  623. /* No simplifications done in this pass. We're done */
  624. break;
  625. e = expr_eliminate_yn(e);
  626. }
  627. trans_count = oldcount;
  628. return e;
  629. }
  630. /*
  631. * Performs various simplifications involving logical operators and
  632. * comparisons.
  633. *
  634. * Allocates and returns a new expression.
  635. */
  636. struct expr *expr_transform(struct expr *e)
  637. {
  638. struct expr *tmp;
  639. if (!e)
  640. return NULL;
  641. switch (e->type) {
  642. case E_EQUAL:
  643. case E_GEQ:
  644. case E_GTH:
  645. case E_LEQ:
  646. case E_LTH:
  647. case E_UNEQUAL:
  648. case E_SYMBOL:
  649. case E_LIST:
  650. break;
  651. default:
  652. e->left.expr = expr_transform(e->left.expr);
  653. e->right.expr = expr_transform(e->right.expr);
  654. }
  655. switch (e->type) {
  656. case E_EQUAL:
  657. if (e->left.sym->type != S_BOOLEAN)
  658. break;
  659. if (e->right.sym == &symbol_no) {
  660. e->type = E_NOT;
  661. e->left.expr = expr_alloc_symbol(e->left.sym);
  662. e->right.sym = NULL;
  663. break;
  664. }
  665. if (e->right.sym == &symbol_mod) {
  666. printf("boolean symbol %s tested for 'm'? test forced to 'n'\n", e->left.sym->name);
  667. e->type = E_SYMBOL;
  668. e->left.sym = &symbol_no;
  669. e->right.sym = NULL;
  670. break;
  671. }
  672. if (e->right.sym == &symbol_yes) {
  673. e->type = E_SYMBOL;
  674. e->right.sym = NULL;
  675. break;
  676. }
  677. break;
  678. case E_UNEQUAL:
  679. if (e->left.sym->type != S_BOOLEAN)
  680. break;
  681. if (e->right.sym == &symbol_no) {
  682. e->type = E_SYMBOL;
  683. e->right.sym = NULL;
  684. break;
  685. }
  686. if (e->right.sym == &symbol_mod) {
  687. printf("boolean symbol %s tested for 'm'? test forced to 'y'\n", e->left.sym->name);
  688. e->type = E_SYMBOL;
  689. e->left.sym = &symbol_yes;
  690. e->right.sym = NULL;
  691. break;
  692. }
  693. if (e->right.sym == &symbol_yes) {
  694. e->type = E_NOT;
  695. e->left.expr = expr_alloc_symbol(e->left.sym);
  696. e->right.sym = NULL;
  697. break;
  698. }
  699. break;
  700. case E_NOT:
  701. switch (e->left.expr->type) {
  702. case E_NOT:
  703. // !!a -> a
  704. tmp = e->left.expr->left.expr;
  705. free(e->left.expr);
  706. free(e);
  707. e = tmp;
  708. e = expr_transform(e);
  709. break;
  710. case E_EQUAL:
  711. case E_UNEQUAL:
  712. // !a='x' -> a!='x'
  713. tmp = e->left.expr;
  714. free(e);
  715. e = tmp;
  716. e->type = e->type == E_EQUAL ? E_UNEQUAL : E_EQUAL;
  717. break;
  718. case E_LEQ:
  719. case E_GEQ:
  720. // !a<='x' -> a>'x'
  721. tmp = e->left.expr;
  722. free(e);
  723. e = tmp;
  724. e->type = e->type == E_LEQ ? E_GTH : E_LTH;
  725. break;
  726. case E_LTH:
  727. case E_GTH:
  728. // !a<'x' -> a>='x'
  729. tmp = e->left.expr;
  730. free(e);
  731. e = tmp;
  732. e->type = e->type == E_LTH ? E_GEQ : E_LEQ;
  733. break;
  734. case E_OR:
  735. // !(a || b) -> !a && !b
  736. tmp = e->left.expr;
  737. e->type = E_AND;
  738. e->right.expr = expr_alloc_one(E_NOT, tmp->right.expr);
  739. tmp->type = E_NOT;
  740. tmp->right.expr = NULL;
  741. e = expr_transform(e);
  742. break;
  743. case E_AND:
  744. // !(a && b) -> !a || !b
  745. tmp = e->left.expr;
  746. e->type = E_OR;
  747. e->right.expr = expr_alloc_one(E_NOT, tmp->right.expr);
  748. tmp->type = E_NOT;
  749. tmp->right.expr = NULL;
  750. e = expr_transform(e);
  751. break;
  752. case E_SYMBOL:
  753. if (e->left.expr->left.sym == &symbol_yes) {
  754. // !'y' -> 'n'
  755. tmp = e->left.expr;
  756. free(e);
  757. e = tmp;
  758. e->type = E_SYMBOL;
  759. e->left.sym = &symbol_no;
  760. break;
  761. }
  762. if (e->left.expr->left.sym == &symbol_mod) {
  763. // !'m' -> 'm'
  764. tmp = e->left.expr;
  765. free(e);
  766. e = tmp;
  767. e->type = E_SYMBOL;
  768. e->left.sym = &symbol_mod;
  769. break;
  770. }
  771. if (e->left.expr->left.sym == &symbol_no) {
  772. // !'n' -> 'y'
  773. tmp = e->left.expr;
  774. free(e);
  775. e = tmp;
  776. e->type = E_SYMBOL;
  777. e->left.sym = &symbol_yes;
  778. break;
  779. }
  780. break;
  781. default:
  782. ;
  783. }
  784. break;
  785. default:
  786. ;
  787. }
  788. return e;
  789. }
  790. int expr_contains_symbol(struct expr *dep, struct symbol *sym)
  791. {
  792. if (!dep)
  793. return 0;
  794. switch (dep->type) {
  795. case E_AND:
  796. case E_OR:
  797. return expr_contains_symbol(dep->left.expr, sym) ||
  798. expr_contains_symbol(dep->right.expr, sym);
  799. case E_SYMBOL:
  800. return dep->left.sym == sym;
  801. case E_EQUAL:
  802. case E_GEQ:
  803. case E_GTH:
  804. case E_LEQ:
  805. case E_LTH:
  806. case E_UNEQUAL:
  807. return dep->left.sym == sym ||
  808. dep->right.sym == sym;
  809. case E_NOT:
  810. return expr_contains_symbol(dep->left.expr, sym);
  811. default:
  812. ;
  813. }
  814. return 0;
  815. }
  816. bool expr_depends_symbol(struct expr *dep, struct symbol *sym)
  817. {
  818. if (!dep)
  819. return false;
  820. switch (dep->type) {
  821. case E_AND:
  822. return expr_depends_symbol(dep->left.expr, sym) ||
  823. expr_depends_symbol(dep->right.expr, sym);
  824. case E_SYMBOL:
  825. return dep->left.sym == sym;
  826. case E_EQUAL:
  827. if (dep->left.sym == sym) {
  828. if (dep->right.sym == &symbol_yes || dep->right.sym == &symbol_mod)
  829. return true;
  830. }
  831. break;
  832. case E_UNEQUAL:
  833. if (dep->left.sym == sym) {
  834. if (dep->right.sym == &symbol_no)
  835. return true;
  836. }
  837. break;
  838. default:
  839. ;
  840. }
  841. return false;
  842. }
  843. /*
  844. * Inserts explicit comparisons of type 'type' to symbol 'sym' into the
  845. * expression 'e'.
  846. *
  847. * Examples transformations for type == E_UNEQUAL, sym == &symbol_no:
  848. *
  849. * A -> A!=n
  850. * !A -> A=n
  851. * A && B -> !(A=n || B=n)
  852. * A || B -> !(A=n && B=n)
  853. * A && (B || C) -> !(A=n || (B=n && C=n))
  854. *
  855. * Allocates and returns a new expression.
  856. */
  857. struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym)
  858. {
  859. struct expr *e1, *e2;
  860. if (!e) {
  861. e = expr_alloc_symbol(sym);
  862. if (type == E_UNEQUAL)
  863. e = expr_alloc_one(E_NOT, e);
  864. return e;
  865. }
  866. switch (e->type) {
  867. case E_AND:
  868. e1 = expr_trans_compare(e->left.expr, E_EQUAL, sym);
  869. e2 = expr_trans_compare(e->right.expr, E_EQUAL, sym);
  870. if (sym == &symbol_yes)
  871. e = expr_alloc_two(E_AND, e1, e2);
  872. if (sym == &symbol_no)
  873. e = expr_alloc_two(E_OR, e1, e2);
  874. if (type == E_UNEQUAL)
  875. e = expr_alloc_one(E_NOT, e);
  876. return e;
  877. case E_OR:
  878. e1 = expr_trans_compare(e->left.expr, E_EQUAL, sym);
  879. e2 = expr_trans_compare(e->right.expr, E_EQUAL, sym);
  880. if (sym == &symbol_yes)
  881. e = expr_alloc_two(E_OR, e1, e2);
  882. if (sym == &symbol_no)
  883. e = expr_alloc_two(E_AND, e1, e2);
  884. if (type == E_UNEQUAL)
  885. e = expr_alloc_one(E_NOT, e);
  886. return e;
  887. case E_NOT:
  888. return expr_trans_compare(e->left.expr, type == E_EQUAL ? E_UNEQUAL : E_EQUAL, sym);
  889. case E_UNEQUAL:
  890. case E_LTH:
  891. case E_LEQ:
  892. case E_GTH:
  893. case E_GEQ:
  894. case E_EQUAL:
  895. if (type == E_EQUAL) {
  896. if (sym == &symbol_yes)
  897. return expr_copy(e);
  898. if (sym == &symbol_mod)
  899. return expr_alloc_symbol(&symbol_no);
  900. if (sym == &symbol_no)
  901. return expr_alloc_one(E_NOT, expr_copy(e));
  902. } else {
  903. if (sym == &symbol_yes)
  904. return expr_alloc_one(E_NOT, expr_copy(e));
  905. if (sym == &symbol_mod)
  906. return expr_alloc_symbol(&symbol_yes);
  907. if (sym == &symbol_no)
  908. return expr_copy(e);
  909. }
  910. break;
  911. case E_SYMBOL:
  912. return expr_alloc_comp(type, e->left.sym, sym);
  913. case E_LIST:
  914. case E_RANGE:
  915. case E_NONE:
  916. /* panic */;
  917. }
  918. return NULL;
  919. }
  920. enum string_value_kind {
  921. k_string,
  922. k_signed,
  923. k_unsigned,
  924. k_invalid
  925. };
  926. union string_value {
  927. unsigned long long u;
  928. signed long long s;
  929. };
  930. static enum string_value_kind expr_parse_string(const char *str,
  931. enum symbol_type type,
  932. union string_value *val)
  933. {
  934. char *tail;
  935. enum string_value_kind kind;
  936. errno = 0;
  937. switch (type) {
  938. case S_BOOLEAN:
  939. case S_TRISTATE:
  940. val->s = !strcmp(str, "n") ? 0 :
  941. !strcmp(str, "m") ? 1 :
  942. !strcmp(str, "y") ? 2 : -1;
  943. return k_signed;
  944. case S_INT:
  945. val->s = strtoll(str, &tail, 10);
  946. kind = k_signed;
  947. break;
  948. case S_HEX:
  949. val->u = strtoull(str, &tail, 16);
  950. kind = k_unsigned;
  951. break;
  952. case S_STRING:
  953. case S_UNKNOWN:
  954. val->s = strtoll(str, &tail, 0);
  955. kind = k_signed;
  956. break;
  957. default:
  958. return k_invalid;
  959. }
  960. return !errno && !*tail && tail > str && isxdigit(tail[-1])
  961. ? kind : k_string;
  962. }
  963. tristate expr_calc_value(struct expr *e)
  964. {
  965. tristate val1, val2;
  966. const char *str1, *str2;
  967. enum string_value_kind k1 = k_string, k2 = k_string;
  968. union string_value lval = {}, rval = {};
  969. int res;
  970. if (!e)
  971. return yes;
  972. switch (e->type) {
  973. case E_SYMBOL:
  974. sym_calc_value(e->left.sym);
  975. return e->left.sym->curr.tri;
  976. case E_AND:
  977. val1 = expr_calc_value(e->left.expr);
  978. val2 = expr_calc_value(e->right.expr);
  979. return EXPR_AND(val1, val2);
  980. case E_OR:
  981. val1 = expr_calc_value(e->left.expr);
  982. val2 = expr_calc_value(e->right.expr);
  983. return EXPR_OR(val1, val2);
  984. case E_NOT:
  985. val1 = expr_calc_value(e->left.expr);
  986. return EXPR_NOT(val1);
  987. case E_EQUAL:
  988. case E_GEQ:
  989. case E_GTH:
  990. case E_LEQ:
  991. case E_LTH:
  992. case E_UNEQUAL:
  993. break;
  994. default:
  995. printf("expr_calc_value: %d?\n", e->type);
  996. return no;
  997. }
  998. sym_calc_value(e->left.sym);
  999. sym_calc_value(e->right.sym);
  1000. str1 = sym_get_string_value(e->left.sym);
  1001. str2 = sym_get_string_value(e->right.sym);
  1002. if (e->left.sym->type != S_STRING || e->right.sym->type != S_STRING) {
  1003. k1 = expr_parse_string(str1, e->left.sym->type, &lval);
  1004. k2 = expr_parse_string(str2, e->right.sym->type, &rval);
  1005. }
  1006. if (k1 == k_string || k2 == k_string)
  1007. res = strcmp(str1, str2);
  1008. else if (k1 == k_invalid || k2 == k_invalid) {
  1009. if (e->type != E_EQUAL && e->type != E_UNEQUAL) {
  1010. printf("Cannot compare \"%s\" and \"%s\"\n", str1, str2);
  1011. return no;
  1012. }
  1013. res = strcmp(str1, str2);
  1014. } else if (k1 == k_unsigned || k2 == k_unsigned)
  1015. res = (lval.u > rval.u) - (lval.u < rval.u);
  1016. else /* if (k1 == k_signed && k2 == k_signed) */
  1017. res = (lval.s > rval.s) - (lval.s < rval.s);
  1018. switch(e->type) {
  1019. case E_EQUAL:
  1020. return res ? no : yes;
  1021. case E_GEQ:
  1022. return res >= 0 ? yes : no;
  1023. case E_GTH:
  1024. return res > 0 ? yes : no;
  1025. case E_LEQ:
  1026. return res <= 0 ? yes : no;
  1027. case E_LTH:
  1028. return res < 0 ? yes : no;
  1029. case E_UNEQUAL:
  1030. return res ? yes : no;
  1031. default:
  1032. printf("expr_calc_value: relation %d?\n", e->type);
  1033. return no;
  1034. }
  1035. }
  1036. static int expr_compare_type(enum expr_type t1, enum expr_type t2)
  1037. {
  1038. if (t1 == t2)
  1039. return 0;
  1040. switch (t1) {
  1041. case E_LEQ:
  1042. case E_LTH:
  1043. case E_GEQ:
  1044. case E_GTH:
  1045. if (t2 == E_EQUAL || t2 == E_UNEQUAL)
  1046. return 1;
  1047. case E_EQUAL:
  1048. case E_UNEQUAL:
  1049. if (t2 == E_NOT)
  1050. return 1;
  1051. case E_NOT:
  1052. if (t2 == E_AND)
  1053. return 1;
  1054. case E_AND:
  1055. if (t2 == E_OR)
  1056. return 1;
  1057. case E_OR:
  1058. if (t2 == E_LIST)
  1059. return 1;
  1060. case E_LIST:
  1061. if (t2 == 0)
  1062. return 1;
  1063. default:
  1064. return -1;
  1065. }
  1066. printf("[%dgt%d?]", t1, t2);
  1067. return 0;
  1068. }
  1069. void expr_print(struct expr *e,
  1070. void (*fn)(void *, struct symbol *, const char *),
  1071. void *data, int prevtoken)
  1072. {
  1073. if (!e) {
  1074. fn(data, NULL, "y");
  1075. return;
  1076. }
  1077. if (expr_compare_type(prevtoken, e->type) > 0)
  1078. fn(data, NULL, "(");
  1079. switch (e->type) {
  1080. case E_SYMBOL:
  1081. if (e->left.sym->name)
  1082. fn(data, e->left.sym, e->left.sym->name);
  1083. else
  1084. fn(data, NULL, "<choice>");
  1085. break;
  1086. case E_NOT:
  1087. fn(data, NULL, "!");
  1088. expr_print(e->left.expr, fn, data, E_NOT);
  1089. break;
  1090. case E_EQUAL:
  1091. if (e->left.sym->name)
  1092. fn(data, e->left.sym, e->left.sym->name);
  1093. else
  1094. fn(data, NULL, "<choice>");
  1095. fn(data, NULL, "=");
  1096. fn(data, e->right.sym, e->right.sym->name);
  1097. break;
  1098. case E_LEQ:
  1099. case E_LTH:
  1100. if (e->left.sym->name)
  1101. fn(data, e->left.sym, e->left.sym->name);
  1102. else
  1103. fn(data, NULL, "<choice>");
  1104. fn(data, NULL, e->type == E_LEQ ? "<=" : "<");
  1105. fn(data, e->right.sym, e->right.sym->name);
  1106. break;
  1107. case E_GEQ:
  1108. case E_GTH:
  1109. if (e->left.sym->name)
  1110. fn(data, e->left.sym, e->left.sym->name);
  1111. else
  1112. fn(data, NULL, "<choice>");
  1113. fn(data, NULL, e->type == E_GEQ ? ">=" : ">");
  1114. fn(data, e->right.sym, e->right.sym->name);
  1115. break;
  1116. case E_UNEQUAL:
  1117. if (e->left.sym->name)
  1118. fn(data, e->left.sym, e->left.sym->name);
  1119. else
  1120. fn(data, NULL, "<choice>");
  1121. fn(data, NULL, "!=");
  1122. fn(data, e->right.sym, e->right.sym->name);
  1123. break;
  1124. case E_OR:
  1125. expr_print(e->left.expr, fn, data, E_OR);
  1126. fn(data, NULL, " || ");
  1127. expr_print(e->right.expr, fn, data, E_OR);
  1128. break;
  1129. case E_AND:
  1130. expr_print(e->left.expr, fn, data, E_AND);
  1131. fn(data, NULL, " && ");
  1132. expr_print(e->right.expr, fn, data, E_AND);
  1133. break;
  1134. case E_LIST:
  1135. fn(data, e->right.sym, e->right.sym->name);
  1136. if (e->left.expr) {
  1137. fn(data, NULL, " ^ ");
  1138. expr_print(e->left.expr, fn, data, E_LIST);
  1139. }
  1140. break;
  1141. case E_RANGE:
  1142. fn(data, NULL, "[");
  1143. fn(data, e->left.sym, e->left.sym->name);
  1144. fn(data, NULL, " ");
  1145. fn(data, e->right.sym, e->right.sym->name);
  1146. fn(data, NULL, "]");
  1147. break;
  1148. default:
  1149. {
  1150. char buf[32];
  1151. sprintf(buf, "<unknown type %d>", e->type);
  1152. fn(data, NULL, buf);
  1153. break;
  1154. }
  1155. }
  1156. if (expr_compare_type(prevtoken, e->type) > 0)
  1157. fn(data, NULL, ")");
  1158. }
  1159. static void expr_print_file_helper(void *data, struct symbol *sym, const char *str)
  1160. {
  1161. xfwrite(str, strlen(str), 1, data);
  1162. }
  1163. void expr_fprint(struct expr *e, FILE *out)
  1164. {
  1165. expr_print(e, expr_print_file_helper, out, E_NONE);
  1166. }
  1167. static void expr_print_gstr_helper(void *data, struct symbol *sym, const char *str)
  1168. {
  1169. struct gstr *gs = (struct gstr*)data;
  1170. const char *sym_str = NULL;
  1171. if (sym)
  1172. sym_str = sym_get_string_value(sym);
  1173. if (gs->max_width) {
  1174. unsigned extra_length = strlen(str);
  1175. const char *last_cr = strrchr(gs->s, '\n');
  1176. unsigned last_line_length;
  1177. if (sym_str)
  1178. extra_length += 4 + strlen(sym_str);
  1179. if (!last_cr)
  1180. last_cr = gs->s;
  1181. last_line_length = strlen(gs->s) - (last_cr - gs->s);
  1182. if ((last_line_length + extra_length) > gs->max_width)
  1183. str_append(gs, "\\\n");
  1184. }
  1185. str_append(gs, str);
  1186. if (sym && sym->type != S_UNKNOWN)
  1187. str_printf(gs, " [=%s]", sym_str);
  1188. }
  1189. void expr_gstr_print(struct expr *e, struct gstr *gs)
  1190. {
  1191. expr_print(e, expr_print_gstr_helper, gs, E_NONE);
  1192. }
  1193. /*
  1194. * Transform the top level "||" tokens into newlines and prepend each
  1195. * line with a minus. This makes expressions much easier to read.
  1196. * Suitable for reverse dependency expressions.
  1197. */
  1198. static void expr_print_revdep(struct expr *e,
  1199. void (*fn)(void *, struct symbol *, const char *),
  1200. void *data, tristate pr_type, const char **title)
  1201. {
  1202. if (e->type == E_OR) {
  1203. expr_print_revdep(e->left.expr, fn, data, pr_type, title);
  1204. expr_print_revdep(e->right.expr, fn, data, pr_type, title);
  1205. } else if (expr_calc_value(e) == pr_type) {
  1206. if (*title) {
  1207. fn(data, NULL, *title);
  1208. *title = NULL;
  1209. }
  1210. fn(data, NULL, " - ");
  1211. expr_print(e, fn, data, E_NONE);
  1212. fn(data, NULL, "\n");
  1213. }
  1214. }
  1215. void expr_gstr_print_revdep(struct expr *e, struct gstr *gs,
  1216. tristate pr_type, const char *title)
  1217. {
  1218. expr_print_revdep(e, expr_print_gstr_helper, gs, pr_type, &title);
  1219. }