1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
16 static struct expr *expr_eliminate_yn(struct expr *e);
18 struct expr *expr_alloc_symbol(struct symbol *sym)
20 struct expr *e = xcalloc(1, sizeof(*e));
26 struct expr *expr_alloc_one(enum expr_type type, struct expr *ce)
28 struct expr *e = xcalloc(1, sizeof(*e));
34 struct expr *expr_alloc_two(enum expr_type type, struct expr *e1, struct expr *e2)
36 struct expr *e = xcalloc(1, sizeof(*e));
43 struct expr *expr_alloc_comp(enum expr_type type, struct symbol *s1, struct symbol *s2)
45 struct expr *e = xcalloc(1, sizeof(*e));
52 struct expr *expr_alloc_and(struct expr *e1, struct expr *e2)
56 return e2 ? expr_alloc_two(E_AND, e1, e2) : e1;
59 struct expr *expr_alloc_or(struct expr *e1, struct expr *e2)
63 return e2 ? expr_alloc_two(E_OR, e1, e2) : e1;
66 struct expr *expr_copy(const struct expr *org)
73 e = xmalloc(sizeof(*org));
74 memcpy(e, org, sizeof(*org));
80 e->left.expr = expr_copy(org->left.expr);
88 e->left.sym = org->left.sym;
89 e->right.sym = org->right.sym;
94 e->left.expr = expr_copy(org->left.expr);
95 e->right.expr = expr_copy(org->right.expr);
98 fprintf(stderr, "can't copy type %d\n", e->type);
107 void expr_free(struct expr *e)
116 expr_free(e->left.expr);
127 expr_free(e->left.expr);
128 expr_free(e->right.expr);
131 fprintf(stderr, "how to free type %d?\n", e->type);
137 static int trans_count;
143 * expr_eliminate_eq() helper.
145 * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
146 * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
147 * against all other leaves. Two equal leaves are both replaced with either 'y'
148 * or 'n' as appropriate for 'type', to be eliminated later.
150 static void __expr_eliminate_eq(enum expr_type type, struct expr **ep1, struct expr **ep2)
152 /* Recurse down to leaves */
154 if (e1->type == type) {
155 __expr_eliminate_eq(type, &e1->left.expr, &e2);
156 __expr_eliminate_eq(type, &e1->right.expr, &e2);
159 if (e2->type == type) {
160 __expr_eliminate_eq(type, &e1, &e2->left.expr);
161 __expr_eliminate_eq(type, &e1, &e2->right.expr);
165 /* e1 and e2 are leaves. Compare them. */
167 if (e1->type == E_SYMBOL && e2->type == E_SYMBOL &&
168 e1->left.sym == e2->left.sym &&
169 (e1->left.sym == &symbol_yes || e1->left.sym == &symbol_no))
171 if (!expr_eq(e1, e2))
174 /* e1 and e2 are equal leaves. Prepare them for elimination. */
177 expr_free(e1); expr_free(e2);
180 e1 = expr_alloc_symbol(&symbol_no);
181 e2 = expr_alloc_symbol(&symbol_no);
184 e1 = expr_alloc_symbol(&symbol_yes);
185 e2 = expr_alloc_symbol(&symbol_yes);
193 * Rewrites the expressions 'ep1' and 'ep2' to remove operands common to both.
194 * Example reductions:
196 * ep1: A && B -> ep1: y
197 * ep2: A && B && C -> ep2: C
199 * ep1: A || B -> ep1: n
200 * ep2: A || B || C -> ep2: C
202 * ep1: A && (B && FOO) -> ep1: FOO
203 * ep2: (BAR && B) && A -> ep2: BAR
205 * ep1: A && (B || C) -> ep1: y
206 * ep2: (C || B) && A -> ep2: y
208 * Comparisons are done between all operands at the same "level" of && or ||.
209 * For example, in the expression 'e1 && (e2 || e3) && (e4 || e5)', the
210 * following operands will be compared:
212 * - 'e1', 'e2 || e3', and 'e4 || e5', against each other
216 * Parentheses are irrelevant within a single level. 'e1 && (e2 && e3)' and
217 * '(e1 && e2) && e3' are both a single level.
219 * See __expr_eliminate_eq() as well.
221 void expr_eliminate_eq(struct expr **ep1, struct expr **ep2)
228 __expr_eliminate_eq(e1->type, ep1, ep2);
232 if (e1->type != e2->type) switch (e2->type) {
235 __expr_eliminate_eq(e2->type, ep1, ep2);
239 e1 = expr_eliminate_yn(e1);
240 e2 = expr_eliminate_yn(e2);
247 * Returns true if 'e1' and 'e2' are equal, after minor simplification. Two
248 * &&/|| expressions are considered equal if every operand in one expression
249 * equals some operand in the other (operands do not need to appear in the same
250 * order), recursively.
252 int expr_eq(struct expr *e1, struct expr *e2)
257 * A NULL expr is taken to be yes, but there's also a different way to
258 * represent yes. expr_is_yes() checks for either representation.
261 return expr_is_yes(e1) && expr_is_yes(e2);
263 if (e1->type != e2->type)
272 return e1->left.sym == e2->left.sym && e1->right.sym == e2->right.sym;
274 return e1->left.sym == e2->left.sym;
276 return expr_eq(e1->left.expr, e2->left.expr);
281 old_count = trans_count;
282 expr_eliminate_eq(&e1, &e2);
283 res = (e1->type == E_SYMBOL && e2->type == E_SYMBOL &&
284 e1->left.sym == e2->left.sym);
287 trans_count = old_count;
296 expr_fprint(e1, stdout);
298 expr_fprint(e2, stdout);
306 * Recursively performs the following simplifications in-place (as well as the
307 * corresponding simplifications with swapped operands):
314 * Returns the optimized expression.
316 static struct expr *expr_eliminate_yn(struct expr *e)
320 if (e) switch (e->type) {
322 e->left.expr = expr_eliminate_yn(e->left.expr);
323 e->right.expr = expr_eliminate_yn(e->right.expr);
324 if (e->left.expr->type == E_SYMBOL) {
325 if (e->left.expr->left.sym == &symbol_no) {
326 expr_free(e->left.expr);
327 expr_free(e->right.expr);
329 e->left.sym = &symbol_no;
330 e->right.expr = NULL;
332 } else if (e->left.expr->left.sym == &symbol_yes) {
335 *e = *(e->right.expr);
340 if (e->right.expr->type == E_SYMBOL) {
341 if (e->right.expr->left.sym == &symbol_no) {
342 expr_free(e->left.expr);
343 expr_free(e->right.expr);
345 e->left.sym = &symbol_no;
346 e->right.expr = NULL;
348 } else if (e->right.expr->left.sym == &symbol_yes) {
351 *e = *(e->left.expr);
358 e->left.expr = expr_eliminate_yn(e->left.expr);
359 e->right.expr = expr_eliminate_yn(e->right.expr);
360 if (e->left.expr->type == E_SYMBOL) {
361 if (e->left.expr->left.sym == &symbol_no) {
364 *e = *(e->right.expr);
367 } else if (e->left.expr->left.sym == &symbol_yes) {
368 expr_free(e->left.expr);
369 expr_free(e->right.expr);
371 e->left.sym = &symbol_yes;
372 e->right.expr = NULL;
376 if (e->right.expr->type == E_SYMBOL) {
377 if (e->right.expr->left.sym == &symbol_no) {
380 *e = *(e->left.expr);
383 } else if (e->right.expr->left.sym == &symbol_yes) {
384 expr_free(e->left.expr);
385 expr_free(e->right.expr);
387 e->left.sym = &symbol_yes;
388 e->right.expr = NULL;
402 struct expr *expr_trans_bool(struct expr *e)
410 e->left.expr = expr_trans_bool(e->left.expr);
411 e->right.expr = expr_trans_bool(e->right.expr);
415 if (e->left.sym->type == S_TRISTATE) {
416 if (e->right.sym == &symbol_no) {
431 static struct expr *expr_join_or(struct expr *e1, struct expr *e2)
434 struct symbol *sym1, *sym2;
437 return expr_copy(e1);
438 if (e1->type != E_EQUAL && e1->type != E_UNEQUAL && e1->type != E_SYMBOL && e1->type != E_NOT)
440 if (e2->type != E_EQUAL && e2->type != E_UNEQUAL && e2->type != E_SYMBOL && e2->type != E_NOT)
442 if (e1->type == E_NOT) {
444 if (tmp->type != E_EQUAL && tmp->type != E_UNEQUAL && tmp->type != E_SYMBOL)
446 sym1 = tmp->left.sym;
449 if (e2->type == E_NOT) {
450 if (e2->left.expr->type != E_SYMBOL)
452 sym2 = e2->left.expr->left.sym;
457 if (sym1->type != S_BOOLEAN && sym1->type != S_TRISTATE)
459 if (sym1->type == S_TRISTATE) {
460 if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
461 ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_mod) ||
462 (e1->right.sym == &symbol_mod && e2->right.sym == &symbol_yes))) {
463 // (a='y') || (a='m') -> (a!='n')
464 return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_no);
466 if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
467 ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_no) ||
468 (e1->right.sym == &symbol_no && e2->right.sym == &symbol_yes))) {
469 // (a='y') || (a='n') -> (a!='m')
470 return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_mod);
472 if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
473 ((e1->right.sym == &symbol_mod && e2->right.sym == &symbol_no) ||
474 (e1->right.sym == &symbol_no && e2->right.sym == &symbol_mod))) {
475 // (a='m') || (a='n') -> (a!='y')
476 return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_yes);
479 if (sym1->type == S_BOOLEAN && sym1 == sym2) {
480 if ((e1->type == E_NOT && e1->left.expr->type == E_SYMBOL && e2->type == E_SYMBOL) ||
481 (e2->type == E_NOT && e2->left.expr->type == E_SYMBOL && e1->type == E_SYMBOL))
482 return expr_alloc_symbol(&symbol_yes);
486 printf("optimize (");
487 expr_fprint(e1, stdout);
489 expr_fprint(e2, stdout);
495 static struct expr *expr_join_and(struct expr *e1, struct expr *e2)
498 struct symbol *sym1, *sym2;
501 return expr_copy(e1);
502 if (e1->type != E_EQUAL && e1->type != E_UNEQUAL && e1->type != E_SYMBOL && e1->type != E_NOT)
504 if (e2->type != E_EQUAL && e2->type != E_UNEQUAL && e2->type != E_SYMBOL && e2->type != E_NOT)
506 if (e1->type == E_NOT) {
508 if (tmp->type != E_EQUAL && tmp->type != E_UNEQUAL && tmp->type != E_SYMBOL)
510 sym1 = tmp->left.sym;
513 if (e2->type == E_NOT) {
514 if (e2->left.expr->type != E_SYMBOL)
516 sym2 = e2->left.expr->left.sym;
521 if (sym1->type != S_BOOLEAN && sym1->type != S_TRISTATE)
524 if ((e1->type == E_SYMBOL && e2->type == E_EQUAL && e2->right.sym == &symbol_yes) ||
525 (e2->type == E_SYMBOL && e1->type == E_EQUAL && e1->right.sym == &symbol_yes))
526 // (a) && (a='y') -> (a='y')
527 return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
529 if ((e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_no) ||
530 (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_no))
531 // (a) && (a!='n') -> (a)
532 return expr_alloc_symbol(sym1);
534 if ((e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_mod) ||
535 (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_mod))
536 // (a) && (a!='m') -> (a='y')
537 return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
539 if (sym1->type == S_TRISTATE) {
540 if (e1->type == E_EQUAL && e2->type == E_UNEQUAL) {
541 // (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
542 sym2 = e1->right.sym;
543 if ((e2->right.sym->flags & SYMBOL_CONST) && (sym2->flags & SYMBOL_CONST))
544 return sym2 != e2->right.sym ? expr_alloc_comp(E_EQUAL, sym1, sym2)
545 : expr_alloc_symbol(&symbol_no);
547 if (e1->type == E_UNEQUAL && e2->type == E_EQUAL) {
548 // (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
549 sym2 = e2->right.sym;
550 if ((e1->right.sym->flags & SYMBOL_CONST) && (sym2->flags & SYMBOL_CONST))
551 return sym2 != e1->right.sym ? expr_alloc_comp(E_EQUAL, sym1, sym2)
552 : expr_alloc_symbol(&symbol_no);
554 if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
555 ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_no) ||
556 (e1->right.sym == &symbol_no && e2->right.sym == &symbol_yes)))
557 // (a!='y') && (a!='n') -> (a='m')
558 return expr_alloc_comp(E_EQUAL, sym1, &symbol_mod);
560 if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
561 ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_mod) ||
562 (e1->right.sym == &symbol_mod && e2->right.sym == &symbol_yes)))
563 // (a!='y') && (a!='m') -> (a='n')
564 return expr_alloc_comp(E_EQUAL, sym1, &symbol_no);
566 if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
567 ((e1->right.sym == &symbol_mod && e2->right.sym == &symbol_no) ||
568 (e1->right.sym == &symbol_no && e2->right.sym == &symbol_mod)))
569 // (a!='m') && (a!='n') -> (a='m')
570 return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
572 if ((e1->type == E_SYMBOL && e2->type == E_EQUAL && e2->right.sym == &symbol_mod) ||
573 (e2->type == E_SYMBOL && e1->type == E_EQUAL && e1->right.sym == &symbol_mod) ||
574 (e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_yes) ||
575 (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_yes))
580 printf("optimize (");
581 expr_fprint(e1, stdout);
583 expr_fprint(e2, stdout);
590 * expr_eliminate_dups() helper.
592 * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
593 * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
594 * against all other leaves to look for simplifications.
596 static void expr_eliminate_dups1(enum expr_type type, struct expr **ep1, struct expr **ep2)
602 /* Recurse down to leaves */
604 if (e1->type == type) {
605 expr_eliminate_dups1(type, &e1->left.expr, &e2);
606 expr_eliminate_dups1(type, &e1->right.expr, &e2);
609 if (e2->type == type) {
610 expr_eliminate_dups1(type, &e1, &e2->left.expr);
611 expr_eliminate_dups1(type, &e1, &e2->right.expr);
615 /* e1 and e2 are leaves. Compare and process them. */
621 case E_OR: case E_AND:
622 expr_eliminate_dups1(e1->type, &e1, &e1);
629 tmp = expr_join_or(e1, e2);
631 expr_free(e1); expr_free(e2);
632 e1 = expr_alloc_symbol(&symbol_no);
638 tmp = expr_join_and(e1, e2);
640 expr_free(e1); expr_free(e2);
641 e1 = expr_alloc_symbol(&symbol_yes);
654 * Rewrites 'e' in-place to remove ("join") duplicate and other redundant
657 * Example simplifications:
659 * A || B || A -> A || B
660 * A && B && A=y -> A=y && B
662 * Returns the deduplicated expression.
664 struct expr *expr_eliminate_dups(struct expr *e)
670 oldcount = trans_count;
674 case E_OR: case E_AND:
675 expr_eliminate_dups1(e->type, &e, &e);
680 /* No simplifications done in this pass. We're done */
682 e = expr_eliminate_yn(e);
684 trans_count = oldcount;
689 * Performs various simplifications involving logical operators and
692 * Allocates and returns a new expression.
694 struct expr *expr_transform(struct expr *e)
711 e->left.expr = expr_transform(e->left.expr);
712 e->right.expr = expr_transform(e->right.expr);
717 if (e->left.sym->type != S_BOOLEAN)
719 if (e->right.sym == &symbol_no) {
721 e->left.expr = expr_alloc_symbol(e->left.sym);
725 if (e->right.sym == &symbol_mod) {
726 printf("boolean symbol %s tested for 'm'? test forced to 'n'\n", e->left.sym->name);
728 e->left.sym = &symbol_no;
732 if (e->right.sym == &symbol_yes) {
739 if (e->left.sym->type != S_BOOLEAN)
741 if (e->right.sym == &symbol_no) {
746 if (e->right.sym == &symbol_mod) {
747 printf("boolean symbol %s tested for 'm'? test forced to 'y'\n", e->left.sym->name);
749 e->left.sym = &symbol_yes;
753 if (e->right.sym == &symbol_yes) {
755 e->left.expr = expr_alloc_symbol(e->left.sym);
761 switch (e->left.expr->type) {
764 tmp = e->left.expr->left.expr;
768 e = expr_transform(e);
776 e->type = e->type == E_EQUAL ? E_UNEQUAL : E_EQUAL;
784 e->type = e->type == E_LEQ ? E_GTH : E_LTH;
792 e->type = e->type == E_LTH ? E_GEQ : E_LEQ;
795 // !(a || b) -> !a && !b
798 e->right.expr = expr_alloc_one(E_NOT, tmp->right.expr);
800 tmp->right.expr = NULL;
801 e = expr_transform(e);
804 // !(a && b) -> !a || !b
807 e->right.expr = expr_alloc_one(E_NOT, tmp->right.expr);
809 tmp->right.expr = NULL;
810 e = expr_transform(e);
813 if (e->left.expr->left.sym == &symbol_yes) {
819 e->left.sym = &symbol_no;
822 if (e->left.expr->left.sym == &symbol_mod) {
828 e->left.sym = &symbol_mod;
831 if (e->left.expr->left.sym == &symbol_no) {
837 e->left.sym = &symbol_yes;
851 int expr_contains_symbol(struct expr *dep, struct symbol *sym)
859 return expr_contains_symbol(dep->left.expr, sym) ||
860 expr_contains_symbol(dep->right.expr, sym);
862 return dep->left.sym == sym;
869 return dep->left.sym == sym ||
870 dep->right.sym == sym;
872 return expr_contains_symbol(dep->left.expr, sym);
879 bool expr_depends_symbol(struct expr *dep, struct symbol *sym)
886 return expr_depends_symbol(dep->left.expr, sym) ||
887 expr_depends_symbol(dep->right.expr, sym);
889 return dep->left.sym == sym;
891 if (dep->left.sym == sym) {
892 if (dep->right.sym == &symbol_yes || dep->right.sym == &symbol_mod)
897 if (dep->left.sym == sym) {
898 if (dep->right.sym == &symbol_no)
909 * Inserts explicit comparisons of type 'type' to symbol 'sym' into the
912 * Examples transformations for type == E_UNEQUAL, sym == &symbol_no:
916 * A && B -> !(A=n || B=n)
917 * A || B -> !(A=n && B=n)
918 * A && (B || C) -> !(A=n || (B=n && C=n))
920 * Allocates and returns a new expression.
922 struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym)
924 struct expr *e1, *e2;
927 e = expr_alloc_symbol(sym);
928 if (type == E_UNEQUAL)
929 e = expr_alloc_one(E_NOT, e);
934 e1 = expr_trans_compare(e->left.expr, E_EQUAL, sym);
935 e2 = expr_trans_compare(e->right.expr, E_EQUAL, sym);
936 if (sym == &symbol_yes)
937 e = expr_alloc_two(E_AND, e1, e2);
938 if (sym == &symbol_no)
939 e = expr_alloc_two(E_OR, e1, e2);
940 if (type == E_UNEQUAL)
941 e = expr_alloc_one(E_NOT, e);
944 e1 = expr_trans_compare(e->left.expr, E_EQUAL, sym);
945 e2 = expr_trans_compare(e->right.expr, E_EQUAL, sym);
946 if (sym == &symbol_yes)
947 e = expr_alloc_two(E_OR, e1, e2);
948 if (sym == &symbol_no)
949 e = expr_alloc_two(E_AND, e1, e2);
950 if (type == E_UNEQUAL)
951 e = expr_alloc_one(E_NOT, e);
954 return expr_trans_compare(e->left.expr, type == E_EQUAL ? E_UNEQUAL : E_EQUAL, sym);
961 if (type == E_EQUAL) {
962 if (sym == &symbol_yes)
964 if (sym == &symbol_mod)
965 return expr_alloc_symbol(&symbol_no);
966 if (sym == &symbol_no)
967 return expr_alloc_one(E_NOT, expr_copy(e));
969 if (sym == &symbol_yes)
970 return expr_alloc_one(E_NOT, expr_copy(e));
971 if (sym == &symbol_mod)
972 return expr_alloc_symbol(&symbol_yes);
973 if (sym == &symbol_no)
978 return expr_alloc_comp(type, e->left.sym, sym);
987 enum string_value_kind {
994 unsigned long long u;
998 static enum string_value_kind expr_parse_string(const char *str,
999 enum symbol_type type,
1000 union string_value *val)
1003 enum string_value_kind kind;
1009 val->s = !strcmp(str, "n") ? 0 :
1010 !strcmp(str, "m") ? 1 :
1011 !strcmp(str, "y") ? 2 : -1;
1014 val->s = strtoll(str, &tail, 10);
1018 val->u = strtoull(str, &tail, 16);
1022 val->s = strtoll(str, &tail, 0);
1026 return !errno && !*tail && tail > str && isxdigit(tail[-1])
1030 tristate expr_calc_value(struct expr *e)
1032 tristate val1, val2;
1033 const char *str1, *str2;
1034 enum string_value_kind k1 = k_string, k2 = k_string;
1035 union string_value lval = {}, rval = {};
1043 sym_calc_value(e->left.sym);
1044 return e->left.sym->curr.tri;
1046 val1 = expr_calc_value(e->left.expr);
1047 val2 = expr_calc_value(e->right.expr);
1048 return EXPR_AND(val1, val2);
1050 val1 = expr_calc_value(e->left.expr);
1051 val2 = expr_calc_value(e->right.expr);
1052 return EXPR_OR(val1, val2);
1054 val1 = expr_calc_value(e->left.expr);
1055 return EXPR_NOT(val1);
1064 printf("expr_calc_value: %d?\n", e->type);
1068 sym_calc_value(e->left.sym);
1069 sym_calc_value(e->right.sym);
1070 str1 = sym_get_string_value(e->left.sym);
1071 str2 = sym_get_string_value(e->right.sym);
1073 if (e->left.sym->type != S_STRING || e->right.sym->type != S_STRING) {
1074 k1 = expr_parse_string(str1, e->left.sym->type, &lval);
1075 k2 = expr_parse_string(str2, e->right.sym->type, &rval);
1078 if (k1 == k_string || k2 == k_string)
1079 res = strcmp(str1, str2);
1080 else if (k1 == k_unsigned || k2 == k_unsigned)
1081 res = (lval.u > rval.u) - (lval.u < rval.u);
1082 else /* if (k1 == k_signed && k2 == k_signed) */
1083 res = (lval.s > rval.s) - (lval.s < rval.s);
1087 return res ? no : yes;
1089 return res >= 0 ? yes : no;
1091 return res > 0 ? yes : no;
1093 return res <= 0 ? yes : no;
1095 return res < 0 ? yes : no;
1097 return res ? yes : no;
1099 printf("expr_calc_value: relation %d?\n", e->type);
1104 static int expr_compare_type(enum expr_type t1, enum expr_type t2)
1113 if (t2 == E_EQUAL || t2 == E_UNEQUAL)
1134 printf("[%dgt%d?]", t1, t2);
1138 void expr_print(struct expr *e,
1139 void (*fn)(void *, struct symbol *, const char *),
1140 void *data, int prevtoken)
1143 fn(data, NULL, "y");
1147 if (expr_compare_type(prevtoken, e->type) > 0)
1148 fn(data, NULL, "(");
1151 if (e->left.sym->name)
1152 fn(data, e->left.sym, e->left.sym->name);
1154 fn(data, NULL, "<choice>");
1157 fn(data, NULL, "!");
1158 expr_print(e->left.expr, fn, data, E_NOT);
1161 if (e->left.sym->name)
1162 fn(data, e->left.sym, e->left.sym->name);
1164 fn(data, NULL, "<choice>");
1165 fn(data, NULL, "=");
1166 fn(data, e->right.sym, e->right.sym->name);
1170 if (e->left.sym->name)
1171 fn(data, e->left.sym, e->left.sym->name);
1173 fn(data, NULL, "<choice>");
1174 fn(data, NULL, e->type == E_LEQ ? "<=" : "<");
1175 fn(data, e->right.sym, e->right.sym->name);
1179 if (e->left.sym->name)
1180 fn(data, e->left.sym, e->left.sym->name);
1182 fn(data, NULL, "<choice>");
1183 fn(data, NULL, e->type == E_GEQ ? ">=" : ">");
1184 fn(data, e->right.sym, e->right.sym->name);
1187 if (e->left.sym->name)
1188 fn(data, e->left.sym, e->left.sym->name);
1190 fn(data, NULL, "<choice>");
1191 fn(data, NULL, "!=");
1192 fn(data, e->right.sym, e->right.sym->name);
1195 expr_print(e->left.expr, fn, data, E_OR);
1196 fn(data, NULL, " || ");
1197 expr_print(e->right.expr, fn, data, E_OR);
1200 expr_print(e->left.expr, fn, data, E_AND);
1201 fn(data, NULL, " && ");
1202 expr_print(e->right.expr, fn, data, E_AND);
1205 fn(data, e->right.sym, e->right.sym->name);
1207 fn(data, NULL, " ^ ");
1208 expr_print(e->left.expr, fn, data, E_LIST);
1212 fn(data, NULL, "[");
1213 fn(data, e->left.sym, e->left.sym->name);
1214 fn(data, NULL, " ");
1215 fn(data, e->right.sym, e->right.sym->name);
1216 fn(data, NULL, "]");
1221 sprintf(buf, "<unknown type %d>", e->type);
1222 fn(data, NULL, buf);
1226 if (expr_compare_type(prevtoken, e->type) > 0)
1227 fn(data, NULL, ")");
1230 static void expr_print_file_helper(void *data, struct symbol *sym, const char *str)
1232 xfwrite(str, strlen(str), 1, data);
1235 void expr_fprint(struct expr *e, FILE *out)
1237 expr_print(e, expr_print_file_helper, out, E_NONE);
1240 static void expr_print_gstr_helper(void *data, struct symbol *sym, const char *str)
1242 struct gstr *gs = (struct gstr*)data;
1243 const char *sym_str = NULL;
1246 sym_str = sym_get_string_value(sym);
1248 if (gs->max_width) {
1249 unsigned extra_length = strlen(str);
1250 const char *last_cr = strrchr(gs->s, '\n');
1251 unsigned last_line_length;
1254 extra_length += 4 + strlen(sym_str);
1259 last_line_length = strlen(gs->s) - (last_cr - gs->s);
1261 if ((last_line_length + extra_length) > gs->max_width)
1262 str_append(gs, "\\\n");
1265 str_append(gs, str);
1266 if (sym && sym->type != S_UNKNOWN)
1267 str_printf(gs, " [=%s]", sym_str);
1270 void expr_gstr_print(struct expr *e, struct gstr *gs)
1272 expr_print(e, expr_print_gstr_helper, gs, E_NONE);
1276 * Transform the top level "||" tokens into newlines and prepend each
1277 * line with a minus. This makes expressions much easier to read.
1278 * Suitable for reverse dependency expressions.
1280 static void expr_print_revdep(struct expr *e,
1281 void (*fn)(void *, struct symbol *, const char *),
1282 void *data, tristate pr_type, const char **title)
1284 if (e->type == E_OR) {
1285 expr_print_revdep(e->left.expr, fn, data, pr_type, title);
1286 expr_print_revdep(e->right.expr, fn, data, pr_type, title);
1287 } else if (expr_calc_value(e) == pr_type) {
1289 fn(data, NULL, *title);
1293 fn(data, NULL, " - ");
1294 expr_print(e, fn, data, E_NONE);
1295 fn(data, NULL, "\n");
1299 void expr_gstr_print_revdep(struct expr *e, struct gstr *gs,
1300 tristate pr_type, const char *title)
1302 expr_print_revdep(e, expr_print_gstr_helper, gs, pr_type, &title);