expr.c 27 KB

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