expr.c 29 KB

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