2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
14 static int expr_eq(struct expr
*e1
, struct expr
*e2
);
15 static struct expr
*expr_eliminate_yn(struct expr
*e
);
17 struct expr
*expr_alloc_symbol(struct symbol
*sym
)
19 struct expr
*e
= xcalloc(1, sizeof(*e
));
25 struct expr
*expr_alloc_one(enum expr_type type
, struct expr
*ce
)
27 struct expr
*e
= xcalloc(1, sizeof(*e
));
33 struct expr
*expr_alloc_two(enum expr_type type
, struct expr
*e1
, struct expr
*e2
)
35 struct expr
*e
= xcalloc(1, sizeof(*e
));
42 struct expr
*expr_alloc_comp(enum expr_type type
, struct symbol
*s1
, struct symbol
*s2
)
44 struct expr
*e
= xcalloc(1, sizeof(*e
));
51 struct expr
*expr_alloc_and(struct expr
*e1
, struct expr
*e2
)
55 return e2
? expr_alloc_two(E_AND
, e1
, e2
) : e1
;
58 struct expr
*expr_alloc_or(struct expr
*e1
, struct expr
*e2
)
62 return e2
? expr_alloc_two(E_OR
, e1
, e2
) : e1
;
65 struct expr
*expr_copy(const struct expr
*org
)
72 e
= xmalloc(sizeof(*org
));
73 memcpy(e
, org
, sizeof(*org
));
79 e
->left
.expr
= expr_copy(org
->left
.expr
);
87 e
->left
.sym
= org
->left
.sym
;
88 e
->right
.sym
= org
->right
.sym
;
93 e
->left
.expr
= expr_copy(org
->left
.expr
);
94 e
->right
.expr
= expr_copy(org
->right
.expr
);
97 fprintf(stderr
, "can't copy type %d\n", e
->type
);
106 void expr_free(struct expr
*e
)
115 expr_free(e
->left
.expr
);
126 expr_free(e
->left
.expr
);
127 expr_free(e
->right
.expr
);
130 fprintf(stderr
, "how to free type %d?\n", e
->type
);
136 static int trans_count
;
142 * expr_eliminate_eq() helper.
144 * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
145 * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
146 * against all other leaves. Two equal leaves are both replaced with either 'y'
147 * or 'n' as appropriate for 'type', to be eliminated later.
149 static void __expr_eliminate_eq(enum expr_type type
, struct expr
**ep1
, struct expr
**ep2
)
151 /* Recurse down to leaves */
153 if (e1
->type
== type
) {
154 __expr_eliminate_eq(type
, &e1
->left
.expr
, &e2
);
155 __expr_eliminate_eq(type
, &e1
->right
.expr
, &e2
);
158 if (e2
->type
== type
) {
159 __expr_eliminate_eq(type
, &e1
, &e2
->left
.expr
);
160 __expr_eliminate_eq(type
, &e1
, &e2
->right
.expr
);
164 /* e1 and e2 are leaves. Compare them. */
166 if (e1
->type
== E_SYMBOL
&& e2
->type
== E_SYMBOL
&&
167 e1
->left
.sym
== e2
->left
.sym
&&
168 (e1
->left
.sym
== &symbol_yes
|| e1
->left
.sym
== &symbol_no
))
170 if (!expr_eq(e1
, e2
))
173 /* e1 and e2 are equal leaves. Prepare them for elimination. */
176 expr_free(e1
); expr_free(e2
);
179 e1
= expr_alloc_symbol(&symbol_no
);
180 e2
= expr_alloc_symbol(&symbol_no
);
183 e1
= expr_alloc_symbol(&symbol_yes
);
184 e2
= expr_alloc_symbol(&symbol_yes
);
192 * Rewrites the expressions 'ep1' and 'ep2' to remove operands common to both.
193 * Example reductions:
195 * ep1: A && B -> ep1: y
196 * ep2: A && B && C -> ep2: C
198 * ep1: A || B -> ep1: n
199 * ep2: A || B || C -> ep2: C
201 * ep1: A && (B && FOO) -> ep1: FOO
202 * ep2: (BAR && B) && A -> ep2: BAR
204 * ep1: A && (B || C) -> ep1: y
205 * ep2: (C || B) && A -> ep2: y
207 * Comparisons are done between all operands at the same "level" of && or ||.
208 * For example, in the expression 'e1 && (e2 || e3) && (e4 || e5)', the
209 * following operands will be compared:
211 * - 'e1', 'e2 || e3', and 'e4 || e5', against each other
215 * Parentheses are irrelevant within a single level. 'e1 && (e2 && e3)' and
216 * '(e1 && e2) && e3' are both a single level.
218 * See __expr_eliminate_eq() as well.
220 void expr_eliminate_eq(struct expr
**ep1
, struct expr
**ep2
)
227 __expr_eliminate_eq(e1
->type
, ep1
, ep2
);
231 if (e1
->type
!= e2
->type
) switch (e2
->type
) {
234 __expr_eliminate_eq(e2
->type
, ep1
, ep2
);
238 e1
= expr_eliminate_yn(e1
);
239 e2
= expr_eliminate_yn(e2
);
246 * Returns true if 'e1' and 'e2' are equal, after minor simplification. Two
247 * &&/|| expressions are considered equal if every operand in one expression
248 * equals some operand in the other (operands do not need to appear in the same
249 * order), recursively.
251 static int expr_eq(struct expr
*e1
, struct expr
*e2
)
255 if (e1
->type
!= e2
->type
)
264 return e1
->left
.sym
== e2
->left
.sym
&& e1
->right
.sym
== e2
->right
.sym
;
266 return e1
->left
.sym
== e2
->left
.sym
;
268 return expr_eq(e1
->left
.expr
, e2
->left
.expr
);
273 old_count
= trans_count
;
274 expr_eliminate_eq(&e1
, &e2
);
275 res
= (e1
->type
== E_SYMBOL
&& e2
->type
== E_SYMBOL
&&
276 e1
->left
.sym
== e2
->left
.sym
);
279 trans_count
= old_count
;
288 expr_fprint(e1
, stdout
);
290 expr_fprint(e2
, stdout
);
298 * Recursively performs the following simplifications in-place (as well as the
299 * corresponding simplifications with swapped operands):
306 * Returns the optimized expression.
308 static struct expr
*expr_eliminate_yn(struct expr
*e
)
312 if (e
) switch (e
->type
) {
314 e
->left
.expr
= expr_eliminate_yn(e
->left
.expr
);
315 e
->right
.expr
= expr_eliminate_yn(e
->right
.expr
);
316 if (e
->left
.expr
->type
== E_SYMBOL
) {
317 if (e
->left
.expr
->left
.sym
== &symbol_no
) {
318 expr_free(e
->left
.expr
);
319 expr_free(e
->right
.expr
);
321 e
->left
.sym
= &symbol_no
;
322 e
->right
.expr
= NULL
;
324 } else if (e
->left
.expr
->left
.sym
== &symbol_yes
) {
327 *e
= *(e
->right
.expr
);
332 if (e
->right
.expr
->type
== E_SYMBOL
) {
333 if (e
->right
.expr
->left
.sym
== &symbol_no
) {
334 expr_free(e
->left
.expr
);
335 expr_free(e
->right
.expr
);
337 e
->left
.sym
= &symbol_no
;
338 e
->right
.expr
= NULL
;
340 } else if (e
->right
.expr
->left
.sym
== &symbol_yes
) {
343 *e
= *(e
->left
.expr
);
350 e
->left
.expr
= expr_eliminate_yn(e
->left
.expr
);
351 e
->right
.expr
= expr_eliminate_yn(e
->right
.expr
);
352 if (e
->left
.expr
->type
== E_SYMBOL
) {
353 if (e
->left
.expr
->left
.sym
== &symbol_no
) {
356 *e
= *(e
->right
.expr
);
359 } else if (e
->left
.expr
->left
.sym
== &symbol_yes
) {
360 expr_free(e
->left
.expr
);
361 expr_free(e
->right
.expr
);
363 e
->left
.sym
= &symbol_yes
;
364 e
->right
.expr
= NULL
;
368 if (e
->right
.expr
->type
== E_SYMBOL
) {
369 if (e
->right
.expr
->left
.sym
== &symbol_no
) {
372 *e
= *(e
->left
.expr
);
375 } else if (e
->right
.expr
->left
.sym
== &symbol_yes
) {
376 expr_free(e
->left
.expr
);
377 expr_free(e
->right
.expr
);
379 e
->left
.sym
= &symbol_yes
;
380 e
->right
.expr
= NULL
;
394 struct expr
*expr_trans_bool(struct expr
*e
)
402 e
->left
.expr
= expr_trans_bool(e
->left
.expr
);
403 e
->right
.expr
= expr_trans_bool(e
->right
.expr
);
407 if (e
->left
.sym
->type
== S_TRISTATE
) {
408 if (e
->right
.sym
== &symbol_no
) {
423 static struct expr
*expr_join_or(struct expr
*e1
, struct expr
*e2
)
426 struct symbol
*sym1
, *sym2
;
429 return expr_copy(e1
);
430 if (e1
->type
!= E_EQUAL
&& e1
->type
!= E_UNEQUAL
&& e1
->type
!= E_SYMBOL
&& e1
->type
!= E_NOT
)
432 if (e2
->type
!= E_EQUAL
&& e2
->type
!= E_UNEQUAL
&& e2
->type
!= E_SYMBOL
&& e2
->type
!= E_NOT
)
434 if (e1
->type
== E_NOT
) {
436 if (tmp
->type
!= E_EQUAL
&& tmp
->type
!= E_UNEQUAL
&& tmp
->type
!= E_SYMBOL
)
438 sym1
= tmp
->left
.sym
;
441 if (e2
->type
== E_NOT
) {
442 if (e2
->left
.expr
->type
!= E_SYMBOL
)
444 sym2
= e2
->left
.expr
->left
.sym
;
449 if (sym1
->type
!= S_BOOLEAN
&& sym1
->type
!= S_TRISTATE
)
451 if (sym1
->type
== S_TRISTATE
) {
452 if (e1
->type
== E_EQUAL
&& e2
->type
== E_EQUAL
&&
453 ((e1
->right
.sym
== &symbol_yes
&& e2
->right
.sym
== &symbol_mod
) ||
454 (e1
->right
.sym
== &symbol_mod
&& e2
->right
.sym
== &symbol_yes
))) {
455 // (a='y') || (a='m') -> (a!='n')
456 return expr_alloc_comp(E_UNEQUAL
, sym1
, &symbol_no
);
458 if (e1
->type
== E_EQUAL
&& e2
->type
== E_EQUAL
&&
459 ((e1
->right
.sym
== &symbol_yes
&& e2
->right
.sym
== &symbol_no
) ||
460 (e1
->right
.sym
== &symbol_no
&& e2
->right
.sym
== &symbol_yes
))) {
461 // (a='y') || (a='n') -> (a!='m')
462 return expr_alloc_comp(E_UNEQUAL
, sym1
, &symbol_mod
);
464 if (e1
->type
== E_EQUAL
&& e2
->type
== E_EQUAL
&&
465 ((e1
->right
.sym
== &symbol_mod
&& e2
->right
.sym
== &symbol_no
) ||
466 (e1
->right
.sym
== &symbol_no
&& e2
->right
.sym
== &symbol_mod
))) {
467 // (a='m') || (a='n') -> (a!='y')
468 return expr_alloc_comp(E_UNEQUAL
, sym1
, &symbol_yes
);
471 if (sym1
->type
== S_BOOLEAN
&& sym1
== sym2
) {
472 if ((e1
->type
== E_NOT
&& e1
->left
.expr
->type
== E_SYMBOL
&& e2
->type
== E_SYMBOL
) ||
473 (e2
->type
== E_NOT
&& e2
->left
.expr
->type
== E_SYMBOL
&& e1
->type
== E_SYMBOL
))
474 return expr_alloc_symbol(&symbol_yes
);
478 printf("optimize (");
479 expr_fprint(e1
, stdout
);
481 expr_fprint(e2
, stdout
);
487 static struct expr
*expr_join_and(struct expr
*e1
, struct expr
*e2
)
490 struct symbol
*sym1
, *sym2
;
493 return expr_copy(e1
);
494 if (e1
->type
!= E_EQUAL
&& e1
->type
!= E_UNEQUAL
&& e1
->type
!= E_SYMBOL
&& e1
->type
!= E_NOT
)
496 if (e2
->type
!= E_EQUAL
&& e2
->type
!= E_UNEQUAL
&& e2
->type
!= E_SYMBOL
&& e2
->type
!= E_NOT
)
498 if (e1
->type
== E_NOT
) {
500 if (tmp
->type
!= E_EQUAL
&& tmp
->type
!= E_UNEQUAL
&& tmp
->type
!= E_SYMBOL
)
502 sym1
= tmp
->left
.sym
;
505 if (e2
->type
== E_NOT
) {
506 if (e2
->left
.expr
->type
!= E_SYMBOL
)
508 sym2
= e2
->left
.expr
->left
.sym
;
513 if (sym1
->type
!= S_BOOLEAN
&& sym1
->type
!= S_TRISTATE
)
516 if ((e1
->type
== E_SYMBOL
&& e2
->type
== E_EQUAL
&& e2
->right
.sym
== &symbol_yes
) ||
517 (e2
->type
== E_SYMBOL
&& e1
->type
== E_EQUAL
&& e1
->right
.sym
== &symbol_yes
))
518 // (a) && (a='y') -> (a='y')
519 return expr_alloc_comp(E_EQUAL
, sym1
, &symbol_yes
);
521 if ((e1
->type
== E_SYMBOL
&& e2
->type
== E_UNEQUAL
&& e2
->right
.sym
== &symbol_no
) ||
522 (e2
->type
== E_SYMBOL
&& e1
->type
== E_UNEQUAL
&& e1
->right
.sym
== &symbol_no
))
523 // (a) && (a!='n') -> (a)
524 return expr_alloc_symbol(sym1
);
526 if ((e1
->type
== E_SYMBOL
&& e2
->type
== E_UNEQUAL
&& e2
->right
.sym
== &symbol_mod
) ||
527 (e2
->type
== E_SYMBOL
&& e1
->type
== E_UNEQUAL
&& e1
->right
.sym
== &symbol_mod
))
528 // (a) && (a!='m') -> (a='y')
529 return expr_alloc_comp(E_EQUAL
, sym1
, &symbol_yes
);
531 if (sym1
->type
== S_TRISTATE
) {
532 if (e1
->type
== E_EQUAL
&& e2
->type
== E_UNEQUAL
) {
533 // (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
534 sym2
= e1
->right
.sym
;
535 if ((e2
->right
.sym
->flags
& SYMBOL_CONST
) && (sym2
->flags
& SYMBOL_CONST
))
536 return sym2
!= e2
->right
.sym
? expr_alloc_comp(E_EQUAL
, sym1
, sym2
)
537 : expr_alloc_symbol(&symbol_no
);
539 if (e1
->type
== E_UNEQUAL
&& e2
->type
== E_EQUAL
) {
540 // (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
541 sym2
= e2
->right
.sym
;
542 if ((e1
->right
.sym
->flags
& SYMBOL_CONST
) && (sym2
->flags
& SYMBOL_CONST
))
543 return sym2
!= e1
->right
.sym
? expr_alloc_comp(E_EQUAL
, sym1
, sym2
)
544 : expr_alloc_symbol(&symbol_no
);
546 if (e1
->type
== E_UNEQUAL
&& e2
->type
== E_UNEQUAL
&&
547 ((e1
->right
.sym
== &symbol_yes
&& e2
->right
.sym
== &symbol_no
) ||
548 (e1
->right
.sym
== &symbol_no
&& e2
->right
.sym
== &symbol_yes
)))
549 // (a!='y') && (a!='n') -> (a='m')
550 return expr_alloc_comp(E_EQUAL
, sym1
, &symbol_mod
);
552 if (e1
->type
== E_UNEQUAL
&& e2
->type
== E_UNEQUAL
&&
553 ((e1
->right
.sym
== &symbol_yes
&& e2
->right
.sym
== &symbol_mod
) ||
554 (e1
->right
.sym
== &symbol_mod
&& e2
->right
.sym
== &symbol_yes
)))
555 // (a!='y') && (a!='m') -> (a='n')
556 return expr_alloc_comp(E_EQUAL
, sym1
, &symbol_no
);
558 if (e1
->type
== E_UNEQUAL
&& e2
->type
== E_UNEQUAL
&&
559 ((e1
->right
.sym
== &symbol_mod
&& e2
->right
.sym
== &symbol_no
) ||
560 (e1
->right
.sym
== &symbol_no
&& e2
->right
.sym
== &symbol_mod
)))
561 // (a!='m') && (a!='n') -> (a='m')
562 return expr_alloc_comp(E_EQUAL
, sym1
, &symbol_yes
);
564 if ((e1
->type
== E_SYMBOL
&& e2
->type
== E_EQUAL
&& e2
->right
.sym
== &symbol_mod
) ||
565 (e2
->type
== E_SYMBOL
&& e1
->type
== E_EQUAL
&& e1
->right
.sym
== &symbol_mod
) ||
566 (e1
->type
== E_SYMBOL
&& e2
->type
== E_UNEQUAL
&& e2
->right
.sym
== &symbol_yes
) ||
567 (e2
->type
== E_SYMBOL
&& e1
->type
== E_UNEQUAL
&& e1
->right
.sym
== &symbol_yes
))
572 printf("optimize (");
573 expr_fprint(e1
, stdout
);
575 expr_fprint(e2
, stdout
);
582 * expr_eliminate_dups() helper.
584 * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
585 * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
586 * against all other leaves to look for simplifications.
588 static void expr_eliminate_dups1(enum expr_type type
, struct expr
**ep1
, struct expr
**ep2
)
594 /* Recurse down to leaves */
596 if (e1
->type
== type
) {
597 expr_eliminate_dups1(type
, &e1
->left
.expr
, &e2
);
598 expr_eliminate_dups1(type
, &e1
->right
.expr
, &e2
);
601 if (e2
->type
== type
) {
602 expr_eliminate_dups1(type
, &e1
, &e2
->left
.expr
);
603 expr_eliminate_dups1(type
, &e1
, &e2
->right
.expr
);
607 /* e1 and e2 are leaves. Compare and process them. */
613 case E_OR
: case E_AND
:
614 expr_eliminate_dups1(e1
->type
, &e1
, &e1
);
621 tmp
= expr_join_or(e1
, e2
);
623 expr_free(e1
); expr_free(e2
);
624 e1
= expr_alloc_symbol(&symbol_no
);
630 tmp
= expr_join_and(e1
, e2
);
632 expr_free(e1
); expr_free(e2
);
633 e1
= expr_alloc_symbol(&symbol_yes
);
646 * Rewrites 'e' in-place to remove ("join") duplicate and other redundant
649 * Example simplifications:
651 * A || B || A -> A || B
652 * A && B && A=y -> A=y && B
654 * Returns the deduplicated expression.
656 struct expr
*expr_eliminate_dups(struct expr
*e
)
662 oldcount
= trans_count
;
666 case E_OR
: case E_AND
:
667 expr_eliminate_dups1(e
->type
, &e
, &e
);
672 /* No simplifications done in this pass. We're done */
674 e
= expr_eliminate_yn(e
);
676 trans_count
= oldcount
;
681 * Performs various simplifications involving logical operators and
684 * Allocates and returns a new expression.
686 struct expr
*expr_transform(struct expr
*e
)
703 e
->left
.expr
= expr_transform(e
->left
.expr
);
704 e
->right
.expr
= expr_transform(e
->right
.expr
);
709 if (e
->left
.sym
->type
!= S_BOOLEAN
)
711 if (e
->right
.sym
== &symbol_no
) {
713 e
->left
.expr
= expr_alloc_symbol(e
->left
.sym
);
717 if (e
->right
.sym
== &symbol_mod
) {
718 printf("boolean symbol %s tested for 'm'? test forced to 'n'\n", e
->left
.sym
->name
);
720 e
->left
.sym
= &symbol_no
;
724 if (e
->right
.sym
== &symbol_yes
) {
731 if (e
->left
.sym
->type
!= S_BOOLEAN
)
733 if (e
->right
.sym
== &symbol_no
) {
738 if (e
->right
.sym
== &symbol_mod
) {
739 printf("boolean symbol %s tested for 'm'? test forced to 'y'\n", e
->left
.sym
->name
);
741 e
->left
.sym
= &symbol_yes
;
745 if (e
->right
.sym
== &symbol_yes
) {
747 e
->left
.expr
= expr_alloc_symbol(e
->left
.sym
);
753 switch (e
->left
.expr
->type
) {
756 tmp
= e
->left
.expr
->left
.expr
;
760 e
= expr_transform(e
);
768 e
->type
= e
->type
== E_EQUAL
? E_UNEQUAL
: E_EQUAL
;
776 e
->type
= e
->type
== E_LEQ
? E_GTH
: E_LTH
;
784 e
->type
= e
->type
== E_LTH
? E_GEQ
: E_LEQ
;
787 // !(a || b) -> !a && !b
790 e
->right
.expr
= expr_alloc_one(E_NOT
, tmp
->right
.expr
);
792 tmp
->right
.expr
= NULL
;
793 e
= expr_transform(e
);
796 // !(a && b) -> !a || !b
799 e
->right
.expr
= expr_alloc_one(E_NOT
, tmp
->right
.expr
);
801 tmp
->right
.expr
= NULL
;
802 e
= expr_transform(e
);
805 if (e
->left
.expr
->left
.sym
== &symbol_yes
) {
811 e
->left
.sym
= &symbol_no
;
814 if (e
->left
.expr
->left
.sym
== &symbol_mod
) {
820 e
->left
.sym
= &symbol_mod
;
823 if (e
->left
.expr
->left
.sym
== &symbol_no
) {
829 e
->left
.sym
= &symbol_yes
;
843 int expr_contains_symbol(struct expr
*dep
, struct symbol
*sym
)
851 return expr_contains_symbol(dep
->left
.expr
, sym
) ||
852 expr_contains_symbol(dep
->right
.expr
, sym
);
854 return dep
->left
.sym
== sym
;
861 return dep
->left
.sym
== sym
||
862 dep
->right
.sym
== sym
;
864 return expr_contains_symbol(dep
->left
.expr
, sym
);
871 bool expr_depends_symbol(struct expr
*dep
, struct symbol
*sym
)
878 return expr_depends_symbol(dep
->left
.expr
, sym
) ||
879 expr_depends_symbol(dep
->right
.expr
, sym
);
881 return dep
->left
.sym
== sym
;
883 if (dep
->left
.sym
== sym
) {
884 if (dep
->right
.sym
== &symbol_yes
|| dep
->right
.sym
== &symbol_mod
)
889 if (dep
->left
.sym
== sym
) {
890 if (dep
->right
.sym
== &symbol_no
)
901 * Inserts explicit comparisons of type 'type' to symbol 'sym' into the
904 * Examples transformations for type == E_UNEQUAL, sym == &symbol_no:
908 * A && B -> !(A=n || B=n)
909 * A || B -> !(A=n && B=n)
910 * A && (B || C) -> !(A=n || (B=n && C=n))
912 * Allocates and returns a new expression.
914 struct expr
*expr_trans_compare(struct expr
*e
, enum expr_type type
, struct symbol
*sym
)
916 struct expr
*e1
, *e2
;
919 e
= expr_alloc_symbol(sym
);
920 if (type
== E_UNEQUAL
)
921 e
= expr_alloc_one(E_NOT
, e
);
926 e1
= expr_trans_compare(e
->left
.expr
, E_EQUAL
, sym
);
927 e2
= expr_trans_compare(e
->right
.expr
, E_EQUAL
, sym
);
928 if (sym
== &symbol_yes
)
929 e
= expr_alloc_two(E_AND
, e1
, e2
);
930 if (sym
== &symbol_no
)
931 e
= expr_alloc_two(E_OR
, e1
, e2
);
932 if (type
== E_UNEQUAL
)
933 e
= expr_alloc_one(E_NOT
, e
);
936 e1
= expr_trans_compare(e
->left
.expr
, E_EQUAL
, sym
);
937 e2
= expr_trans_compare(e
->right
.expr
, E_EQUAL
, sym
);
938 if (sym
== &symbol_yes
)
939 e
= expr_alloc_two(E_OR
, e1
, e2
);
940 if (sym
== &symbol_no
)
941 e
= expr_alloc_two(E_AND
, e1
, e2
);
942 if (type
== E_UNEQUAL
)
943 e
= expr_alloc_one(E_NOT
, e
);
946 return expr_trans_compare(e
->left
.expr
, type
== E_EQUAL
? E_UNEQUAL
: E_EQUAL
, sym
);
953 if (type
== E_EQUAL
) {
954 if (sym
== &symbol_yes
)
956 if (sym
== &symbol_mod
)
957 return expr_alloc_symbol(&symbol_no
);
958 if (sym
== &symbol_no
)
959 return expr_alloc_one(E_NOT
, expr_copy(e
));
961 if (sym
== &symbol_yes
)
962 return expr_alloc_one(E_NOT
, expr_copy(e
));
963 if (sym
== &symbol_mod
)
964 return expr_alloc_symbol(&symbol_yes
);
965 if (sym
== &symbol_no
)
970 return expr_alloc_comp(type
, e
->left
.sym
, sym
);
979 enum string_value_kind
{
987 unsigned long long u
;
991 static enum string_value_kind
expr_parse_string(const char *str
,
992 enum symbol_type type
,
993 union string_value
*val
)
996 enum string_value_kind kind
;
1002 val
->s
= !strcmp(str
, "n") ? 0 :
1003 !strcmp(str
, "m") ? 1 :
1004 !strcmp(str
, "y") ? 2 : -1;
1007 val
->s
= strtoll(str
, &tail
, 10);
1011 val
->u
= strtoull(str
, &tail
, 16);
1016 val
->s
= strtoll(str
, &tail
, 0);
1022 return !errno
&& !*tail
&& tail
> str
&& isxdigit(tail
[-1])
1026 tristate
expr_calc_value(struct expr
*e
)
1028 tristate val1
, val2
;
1029 const char *str1
, *str2
;
1030 enum string_value_kind k1
= k_string
, k2
= k_string
;
1031 union string_value lval
= {}, rval
= {};
1039 sym_calc_value(e
->left
.sym
);
1040 return e
->left
.sym
->curr
.tri
;
1042 val1
= expr_calc_value(e
->left
.expr
);
1043 val2
= expr_calc_value(e
->right
.expr
);
1044 return EXPR_AND(val1
, val2
);
1046 val1
= expr_calc_value(e
->left
.expr
);
1047 val2
= expr_calc_value(e
->right
.expr
);
1048 return EXPR_OR(val1
, val2
);
1050 val1
= expr_calc_value(e
->left
.expr
);
1051 return EXPR_NOT(val1
);
1060 printf("expr_calc_value: %d?\n", e
->type
);
1064 sym_calc_value(e
->left
.sym
);
1065 sym_calc_value(e
->right
.sym
);
1066 str1
= sym_get_string_value(e
->left
.sym
);
1067 str2
= sym_get_string_value(e
->right
.sym
);
1069 if (e
->left
.sym
->type
!= S_STRING
|| e
->right
.sym
->type
!= S_STRING
) {
1070 k1
= expr_parse_string(str1
, e
->left
.sym
->type
, &lval
);
1071 k2
= expr_parse_string(str2
, e
->right
.sym
->type
, &rval
);
1074 if (k1
== k_string
|| k2
== k_string
)
1075 res
= strcmp(str1
, str2
);
1076 else if (k1
== k_invalid
|| k2
== k_invalid
) {
1077 if (e
->type
!= E_EQUAL
&& e
->type
!= E_UNEQUAL
) {
1078 printf("Cannot compare \"%s\" and \"%s\"\n", str1
, str2
);
1081 res
= strcmp(str1
, str2
);
1082 } else if (k1
== k_unsigned
|| k2
== k_unsigned
)
1083 res
= (lval
.u
> rval
.u
) - (lval
.u
< rval
.u
);
1084 else /* if (k1 == k_signed && k2 == k_signed) */
1085 res
= (lval
.s
> rval
.s
) - (lval
.s
< rval
.s
);
1089 return res
? no
: yes
;
1091 return res
>= 0 ? yes
: no
;
1093 return res
> 0 ? yes
: no
;
1095 return res
<= 0 ? yes
: no
;
1097 return res
< 0 ? yes
: no
;
1099 return res
? yes
: no
;
1101 printf("expr_calc_value: relation %d?\n", e
->type
);
1106 static int expr_compare_type(enum expr_type t1
, enum expr_type t2
)
1115 if (t2
== E_EQUAL
|| t2
== E_UNEQUAL
)
1136 printf("[%dgt%d?]", t1
, t2
);
1140 static inline struct expr
*
1141 expr_get_leftmost_symbol(const struct expr
*e
)
1147 while (e
->type
!= E_SYMBOL
)
1150 return expr_copy(e
);
1154 * Given expression `e1' and `e2', returns the leaf of the longest
1155 * sub-expression of `e1' not containing 'e2.
1157 struct expr
*expr_simplify_unmet_dep(struct expr
*e1
, struct expr
*e2
)
1163 return expr_alloc_and(
1164 expr_simplify_unmet_dep(e1
->left
.expr
, e2
),
1165 expr_simplify_unmet_dep(e1
->right
.expr
, e2
));
1168 e
= expr_alloc_and(expr_copy(e1
), expr_copy(e2
));
1169 e
= expr_eliminate_dups(e
);
1170 ret
= (!expr_eq(e
, e1
)) ? e1
: NULL
;
1179 return expr_get_leftmost_symbol(ret
);
1182 static void __expr_print(struct expr
*e
, void (*fn
)(void *, struct symbol
*, const char *), void *data
, int prevtoken
, bool revdep
)
1185 fn(data
, NULL
, "y");
1189 if (expr_compare_type(prevtoken
, e
->type
) > 0)
1190 fn(data
, NULL
, "(");
1193 if (e
->left
.sym
->name
)
1194 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1196 fn(data
, NULL
, "<choice>");
1199 fn(data
, NULL
, "!");
1200 expr_print(e
->left
.expr
, fn
, data
, E_NOT
);
1203 if (e
->left
.sym
->name
)
1204 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1206 fn(data
, NULL
, "<choice>");
1207 fn(data
, NULL
, "=");
1208 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1212 if (e
->left
.sym
->name
)
1213 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1215 fn(data
, NULL
, "<choice>");
1216 fn(data
, NULL
, e
->type
== E_LEQ
? "<=" : "<");
1217 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1221 if (e
->left
.sym
->name
)
1222 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1224 fn(data
, NULL
, "<choice>");
1225 fn(data
, NULL
, e
->type
== E_GEQ
? ">=" : ">");
1226 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1229 if (e
->left
.sym
->name
)
1230 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1232 fn(data
, NULL
, "<choice>");
1233 fn(data
, NULL
, "!=");
1234 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1237 if (revdep
&& e
->left
.expr
->type
!= E_OR
)
1238 fn(data
, NULL
, "\n - ");
1239 __expr_print(e
->left
.expr
, fn
, data
, E_OR
, revdep
);
1241 fn(data
, NULL
, "\n - ");
1243 fn(data
, NULL
, " || ");
1244 __expr_print(e
->right
.expr
, fn
, data
, E_OR
, revdep
);
1247 expr_print(e
->left
.expr
, fn
, data
, E_AND
);
1248 fn(data
, NULL
, " && ");
1249 expr_print(e
->right
.expr
, fn
, data
, E_AND
);
1252 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1254 fn(data
, NULL
, " ^ ");
1255 expr_print(e
->left
.expr
, fn
, data
, E_LIST
);
1259 fn(data
, NULL
, "[");
1260 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1261 fn(data
, NULL
, " ");
1262 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1263 fn(data
, NULL
, "]");
1268 sprintf(buf
, "<unknown type %d>", e
->type
);
1269 fn(data
, NULL
, buf
);
1273 if (expr_compare_type(prevtoken
, e
->type
) > 0)
1274 fn(data
, NULL
, ")");
1277 void expr_print(struct expr
*e
, void (*fn
)(void *, struct symbol
*, const char *), void *data
, int prevtoken
)
1279 __expr_print(e
, fn
, data
, prevtoken
, false);
1282 static void expr_print_file_helper(void *data
, struct symbol
*sym
, const char *str
)
1284 xfwrite(str
, strlen(str
), 1, data
);
1287 void expr_fprint(struct expr
*e
, FILE *out
)
1289 expr_print(e
, expr_print_file_helper
, out
, E_NONE
);
1292 static void expr_print_gstr_helper(void *data
, struct symbol
*sym
, const char *str
)
1294 struct gstr
*gs
= (struct gstr
*)data
;
1295 const char *sym_str
= NULL
;
1298 sym_str
= sym_get_string_value(sym
);
1300 if (gs
->max_width
) {
1301 unsigned extra_length
= strlen(str
);
1302 const char *last_cr
= strrchr(gs
->s
, '\n');
1303 unsigned last_line_length
;
1306 extra_length
+= 4 + strlen(sym_str
);
1311 last_line_length
= strlen(gs
->s
) - (last_cr
- gs
->s
);
1313 if ((last_line_length
+ extra_length
) > gs
->max_width
)
1314 str_append(gs
, "\\\n");
1317 str_append(gs
, str
);
1318 if (sym
&& sym
->type
!= S_UNKNOWN
)
1319 str_printf(gs
, " [=%s]", sym_str
);
1322 void expr_gstr_print(struct expr
*e
, struct gstr
*gs
)
1324 expr_print(e
, expr_print_gstr_helper
, gs
, E_NONE
);
1328 * Transform the top level "||" tokens into newlines and prepend each
1329 * line with a minus. This makes expressions much easier to read.
1330 * Suitable for reverse dependency expressions.
1332 void expr_gstr_print_revdep(struct expr
*e
, struct gstr
*gs
)
1334 __expr_print(e
, expr_print_gstr_helper
, gs
, E_NONE
, true);