1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
16 static int expr_eq(struct expr
*e1
, struct expr
*e2
);
17 static struct expr
*expr_eliminate_yn(struct expr
*e
);
19 struct expr
*expr_alloc_symbol(struct symbol
*sym
)
21 struct expr
*e
= xcalloc(1, sizeof(*e
));
27 struct expr
*expr_alloc_one(enum expr_type type
, struct expr
*ce
)
29 struct expr
*e
= xcalloc(1, sizeof(*e
));
35 struct expr
*expr_alloc_two(enum expr_type type
, struct expr
*e1
, struct expr
*e2
)
37 struct expr
*e
= xcalloc(1, sizeof(*e
));
44 struct expr
*expr_alloc_comp(enum expr_type type
, struct symbol
*s1
, struct symbol
*s2
)
46 struct expr
*e
= xcalloc(1, sizeof(*e
));
53 struct expr
*expr_alloc_and(struct expr
*e1
, struct expr
*e2
)
57 return e2
? expr_alloc_two(E_AND
, e1
, e2
) : e1
;
60 struct expr
*expr_alloc_or(struct expr
*e1
, struct expr
*e2
)
64 return e2
? expr_alloc_two(E_OR
, e1
, e2
) : e1
;
67 struct expr
*expr_copy(const struct expr
*org
)
74 e
= xmalloc(sizeof(*org
));
75 memcpy(e
, org
, sizeof(*org
));
81 e
->left
.expr
= expr_copy(org
->left
.expr
);
89 e
->left
.sym
= org
->left
.sym
;
90 e
->right
.sym
= org
->right
.sym
;
95 e
->left
.expr
= expr_copy(org
->left
.expr
);
96 e
->right
.expr
= expr_copy(org
->right
.expr
);
99 fprintf(stderr
, "can't copy type %d\n", e
->type
);
108 void expr_free(struct expr
*e
)
117 expr_free(e
->left
.expr
);
128 expr_free(e
->left
.expr
);
129 expr_free(e
->right
.expr
);
132 fprintf(stderr
, "how to free type %d?\n", e
->type
);
138 static int trans_count
;
144 * expr_eliminate_eq() helper.
146 * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
147 * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
148 * against all other leaves. Two equal leaves are both replaced with either 'y'
149 * or 'n' as appropriate for 'type', to be eliminated later.
151 static void __expr_eliminate_eq(enum expr_type type
, struct expr
**ep1
, struct expr
**ep2
)
153 /* Recurse down to leaves */
155 if (e1
->type
== type
) {
156 __expr_eliminate_eq(type
, &e1
->left
.expr
, &e2
);
157 __expr_eliminate_eq(type
, &e1
->right
.expr
, &e2
);
160 if (e2
->type
== type
) {
161 __expr_eliminate_eq(type
, &e1
, &e2
->left
.expr
);
162 __expr_eliminate_eq(type
, &e1
, &e2
->right
.expr
);
166 /* e1 and e2 are leaves. Compare them. */
168 if (e1
->type
== E_SYMBOL
&& e2
->type
== E_SYMBOL
&&
169 e1
->left
.sym
== e2
->left
.sym
&&
170 (e1
->left
.sym
== &symbol_yes
|| e1
->left
.sym
== &symbol_no
))
172 if (!expr_eq(e1
, e2
))
175 /* e1 and e2 are equal leaves. Prepare them for elimination. */
178 expr_free(e1
); expr_free(e2
);
181 e1
= expr_alloc_symbol(&symbol_no
);
182 e2
= expr_alloc_symbol(&symbol_no
);
185 e1
= expr_alloc_symbol(&symbol_yes
);
186 e2
= expr_alloc_symbol(&symbol_yes
);
194 * Rewrites the expressions 'ep1' and 'ep2' to remove operands common to both.
195 * Example reductions:
197 * ep1: A && B -> ep1: y
198 * ep2: A && B && C -> ep2: C
200 * ep1: A || B -> ep1: n
201 * ep2: A || B || C -> ep2: C
203 * ep1: A && (B && FOO) -> ep1: FOO
204 * ep2: (BAR && B) && A -> ep2: BAR
206 * ep1: A && (B || C) -> ep1: y
207 * ep2: (C || B) && A -> ep2: y
209 * Comparisons are done between all operands at the same "level" of && or ||.
210 * For example, in the expression 'e1 && (e2 || e3) && (e4 || e5)', the
211 * following operands will be compared:
213 * - 'e1', 'e2 || e3', and 'e4 || e5', against each other
217 * Parentheses are irrelevant within a single level. 'e1 && (e2 && e3)' and
218 * '(e1 && e2) && e3' are both a single level.
220 * See __expr_eliminate_eq() as well.
222 void expr_eliminate_eq(struct expr
**ep1
, struct expr
**ep2
)
229 __expr_eliminate_eq(e1
->type
, ep1
, ep2
);
233 if (e1
->type
!= e2
->type
) switch (e2
->type
) {
236 __expr_eliminate_eq(e2
->type
, ep1
, ep2
);
240 e1
= expr_eliminate_yn(e1
);
241 e2
= expr_eliminate_yn(e2
);
248 * Returns true if 'e1' and 'e2' are equal, after minor simplification. Two
249 * &&/|| expressions are considered equal if every operand in one expression
250 * equals some operand in the other (operands do not need to appear in the same
251 * order), recursively.
253 static int expr_eq(struct expr
*e1
, struct expr
*e2
)
258 * A NULL expr is taken to be yes, but there's also a different way to
259 * represent yes. expr_is_yes() checks for either representation.
262 return expr_is_yes(e1
) && expr_is_yes(e2
);
264 if (e1
->type
!= e2
->type
)
273 return e1
->left
.sym
== e2
->left
.sym
&& e1
->right
.sym
== e2
->right
.sym
;
275 return e1
->left
.sym
== e2
->left
.sym
;
277 return expr_eq(e1
->left
.expr
, e2
->left
.expr
);
282 old_count
= trans_count
;
283 expr_eliminate_eq(&e1
, &e2
);
284 res
= (e1
->type
== E_SYMBOL
&& e2
->type
== E_SYMBOL
&&
285 e1
->left
.sym
== e2
->left
.sym
);
288 trans_count
= old_count
;
297 expr_fprint(e1
, stdout
);
299 expr_fprint(e2
, stdout
);
307 * Recursively performs the following simplifications in-place (as well as the
308 * corresponding simplifications with swapped operands):
315 * Returns the optimized expression.
317 static struct expr
*expr_eliminate_yn(struct expr
*e
)
321 if (e
) switch (e
->type
) {
323 e
->left
.expr
= expr_eliminate_yn(e
->left
.expr
);
324 e
->right
.expr
= expr_eliminate_yn(e
->right
.expr
);
325 if (e
->left
.expr
->type
== E_SYMBOL
) {
326 if (e
->left
.expr
->left
.sym
== &symbol_no
) {
327 expr_free(e
->left
.expr
);
328 expr_free(e
->right
.expr
);
330 e
->left
.sym
= &symbol_no
;
331 e
->right
.expr
= NULL
;
333 } else if (e
->left
.expr
->left
.sym
== &symbol_yes
) {
336 *e
= *(e
->right
.expr
);
341 if (e
->right
.expr
->type
== E_SYMBOL
) {
342 if (e
->right
.expr
->left
.sym
== &symbol_no
) {
343 expr_free(e
->left
.expr
);
344 expr_free(e
->right
.expr
);
346 e
->left
.sym
= &symbol_no
;
347 e
->right
.expr
= NULL
;
349 } else if (e
->right
.expr
->left
.sym
== &symbol_yes
) {
352 *e
= *(e
->left
.expr
);
359 e
->left
.expr
= expr_eliminate_yn(e
->left
.expr
);
360 e
->right
.expr
= expr_eliminate_yn(e
->right
.expr
);
361 if (e
->left
.expr
->type
== E_SYMBOL
) {
362 if (e
->left
.expr
->left
.sym
== &symbol_no
) {
365 *e
= *(e
->right
.expr
);
368 } else if (e
->left
.expr
->left
.sym
== &symbol_yes
) {
369 expr_free(e
->left
.expr
);
370 expr_free(e
->right
.expr
);
372 e
->left
.sym
= &symbol_yes
;
373 e
->right
.expr
= NULL
;
377 if (e
->right
.expr
->type
== E_SYMBOL
) {
378 if (e
->right
.expr
->left
.sym
== &symbol_no
) {
381 *e
= *(e
->left
.expr
);
384 } else if (e
->right
.expr
->left
.sym
== &symbol_yes
) {
385 expr_free(e
->left
.expr
);
386 expr_free(e
->right
.expr
);
388 e
->left
.sym
= &symbol_yes
;
389 e
->right
.expr
= NULL
;
403 struct expr
*expr_trans_bool(struct expr
*e
)
411 e
->left
.expr
= expr_trans_bool(e
->left
.expr
);
412 e
->right
.expr
= expr_trans_bool(e
->right
.expr
);
416 if (e
->left
.sym
->type
== S_TRISTATE
) {
417 if (e
->right
.sym
== &symbol_no
) {
432 static struct expr
*expr_join_or(struct expr
*e1
, struct expr
*e2
)
435 struct symbol
*sym1
, *sym2
;
438 return expr_copy(e1
);
439 if (e1
->type
!= E_EQUAL
&& e1
->type
!= E_UNEQUAL
&& e1
->type
!= E_SYMBOL
&& e1
->type
!= E_NOT
)
441 if (e2
->type
!= E_EQUAL
&& e2
->type
!= E_UNEQUAL
&& e2
->type
!= E_SYMBOL
&& e2
->type
!= E_NOT
)
443 if (e1
->type
== E_NOT
) {
445 if (tmp
->type
!= E_EQUAL
&& tmp
->type
!= E_UNEQUAL
&& tmp
->type
!= E_SYMBOL
)
447 sym1
= tmp
->left
.sym
;
450 if (e2
->type
== E_NOT
) {
451 if (e2
->left
.expr
->type
!= E_SYMBOL
)
453 sym2
= e2
->left
.expr
->left
.sym
;
458 if (sym1
->type
!= S_BOOLEAN
&& sym1
->type
!= S_TRISTATE
)
460 if (sym1
->type
== S_TRISTATE
) {
461 if (e1
->type
== E_EQUAL
&& e2
->type
== E_EQUAL
&&
462 ((e1
->right
.sym
== &symbol_yes
&& e2
->right
.sym
== &symbol_mod
) ||
463 (e1
->right
.sym
== &symbol_mod
&& e2
->right
.sym
== &symbol_yes
))) {
464 // (a='y') || (a='m') -> (a!='n')
465 return expr_alloc_comp(E_UNEQUAL
, sym1
, &symbol_no
);
467 if (e1
->type
== E_EQUAL
&& e2
->type
== E_EQUAL
&&
468 ((e1
->right
.sym
== &symbol_yes
&& e2
->right
.sym
== &symbol_no
) ||
469 (e1
->right
.sym
== &symbol_no
&& e2
->right
.sym
== &symbol_yes
))) {
470 // (a='y') || (a='n') -> (a!='m')
471 return expr_alloc_comp(E_UNEQUAL
, sym1
, &symbol_mod
);
473 if (e1
->type
== E_EQUAL
&& e2
->type
== E_EQUAL
&&
474 ((e1
->right
.sym
== &symbol_mod
&& e2
->right
.sym
== &symbol_no
) ||
475 (e1
->right
.sym
== &symbol_no
&& e2
->right
.sym
== &symbol_mod
))) {
476 // (a='m') || (a='n') -> (a!='y')
477 return expr_alloc_comp(E_UNEQUAL
, sym1
, &symbol_yes
);
480 if (sym1
->type
== S_BOOLEAN
&& sym1
== sym2
) {
481 if ((e1
->type
== E_NOT
&& e1
->left
.expr
->type
== E_SYMBOL
&& e2
->type
== E_SYMBOL
) ||
482 (e2
->type
== E_NOT
&& e2
->left
.expr
->type
== E_SYMBOL
&& e1
->type
== E_SYMBOL
))
483 return expr_alloc_symbol(&symbol_yes
);
487 printf("optimize (");
488 expr_fprint(e1
, stdout
);
490 expr_fprint(e2
, stdout
);
496 static struct expr
*expr_join_and(struct expr
*e1
, struct expr
*e2
)
499 struct symbol
*sym1
, *sym2
;
502 return expr_copy(e1
);
503 if (e1
->type
!= E_EQUAL
&& e1
->type
!= E_UNEQUAL
&& e1
->type
!= E_SYMBOL
&& e1
->type
!= E_NOT
)
505 if (e2
->type
!= E_EQUAL
&& e2
->type
!= E_UNEQUAL
&& e2
->type
!= E_SYMBOL
&& e2
->type
!= E_NOT
)
507 if (e1
->type
== E_NOT
) {
509 if (tmp
->type
!= E_EQUAL
&& tmp
->type
!= E_UNEQUAL
&& tmp
->type
!= E_SYMBOL
)
511 sym1
= tmp
->left
.sym
;
514 if (e2
->type
== E_NOT
) {
515 if (e2
->left
.expr
->type
!= E_SYMBOL
)
517 sym2
= e2
->left
.expr
->left
.sym
;
522 if (sym1
->type
!= S_BOOLEAN
&& sym1
->type
!= S_TRISTATE
)
525 if ((e1
->type
== E_SYMBOL
&& e2
->type
== E_EQUAL
&& e2
->right
.sym
== &symbol_yes
) ||
526 (e2
->type
== E_SYMBOL
&& e1
->type
== E_EQUAL
&& e1
->right
.sym
== &symbol_yes
))
527 // (a) && (a='y') -> (a='y')
528 return expr_alloc_comp(E_EQUAL
, sym1
, &symbol_yes
);
530 if ((e1
->type
== E_SYMBOL
&& e2
->type
== E_UNEQUAL
&& e2
->right
.sym
== &symbol_no
) ||
531 (e2
->type
== E_SYMBOL
&& e1
->type
== E_UNEQUAL
&& e1
->right
.sym
== &symbol_no
))
532 // (a) && (a!='n') -> (a)
533 return expr_alloc_symbol(sym1
);
535 if ((e1
->type
== E_SYMBOL
&& e2
->type
== E_UNEQUAL
&& e2
->right
.sym
== &symbol_mod
) ||
536 (e2
->type
== E_SYMBOL
&& e1
->type
== E_UNEQUAL
&& e1
->right
.sym
== &symbol_mod
))
537 // (a) && (a!='m') -> (a='y')
538 return expr_alloc_comp(E_EQUAL
, sym1
, &symbol_yes
);
540 if (sym1
->type
== S_TRISTATE
) {
541 if (e1
->type
== E_EQUAL
&& e2
->type
== E_UNEQUAL
) {
542 // (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
543 sym2
= e1
->right
.sym
;
544 if ((e2
->right
.sym
->flags
& SYMBOL_CONST
) && (sym2
->flags
& SYMBOL_CONST
))
545 return sym2
!= e2
->right
.sym
? expr_alloc_comp(E_EQUAL
, sym1
, sym2
)
546 : expr_alloc_symbol(&symbol_no
);
548 if (e1
->type
== E_UNEQUAL
&& e2
->type
== E_EQUAL
) {
549 // (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
550 sym2
= e2
->right
.sym
;
551 if ((e1
->right
.sym
->flags
& SYMBOL_CONST
) && (sym2
->flags
& SYMBOL_CONST
))
552 return sym2
!= e1
->right
.sym
? expr_alloc_comp(E_EQUAL
, sym1
, sym2
)
553 : expr_alloc_symbol(&symbol_no
);
555 if (e1
->type
== E_UNEQUAL
&& e2
->type
== E_UNEQUAL
&&
556 ((e1
->right
.sym
== &symbol_yes
&& e2
->right
.sym
== &symbol_no
) ||
557 (e1
->right
.sym
== &symbol_no
&& e2
->right
.sym
== &symbol_yes
)))
558 // (a!='y') && (a!='n') -> (a='m')
559 return expr_alloc_comp(E_EQUAL
, sym1
, &symbol_mod
);
561 if (e1
->type
== E_UNEQUAL
&& e2
->type
== E_UNEQUAL
&&
562 ((e1
->right
.sym
== &symbol_yes
&& e2
->right
.sym
== &symbol_mod
) ||
563 (e1
->right
.sym
== &symbol_mod
&& e2
->right
.sym
== &symbol_yes
)))
564 // (a!='y') && (a!='m') -> (a='n')
565 return expr_alloc_comp(E_EQUAL
, sym1
, &symbol_no
);
567 if (e1
->type
== E_UNEQUAL
&& e2
->type
== E_UNEQUAL
&&
568 ((e1
->right
.sym
== &symbol_mod
&& e2
->right
.sym
== &symbol_no
) ||
569 (e1
->right
.sym
== &symbol_no
&& e2
->right
.sym
== &symbol_mod
)))
570 // (a!='m') && (a!='n') -> (a='m')
571 return expr_alloc_comp(E_EQUAL
, sym1
, &symbol_yes
);
573 if ((e1
->type
== E_SYMBOL
&& e2
->type
== E_EQUAL
&& e2
->right
.sym
== &symbol_mod
) ||
574 (e2
->type
== E_SYMBOL
&& e1
->type
== E_EQUAL
&& e1
->right
.sym
== &symbol_mod
) ||
575 (e1
->type
== E_SYMBOL
&& e2
->type
== E_UNEQUAL
&& e2
->right
.sym
== &symbol_yes
) ||
576 (e2
->type
== E_SYMBOL
&& e1
->type
== E_UNEQUAL
&& e1
->right
.sym
== &symbol_yes
))
581 printf("optimize (");
582 expr_fprint(e1
, stdout
);
584 expr_fprint(e2
, stdout
);
591 * expr_eliminate_dups() helper.
593 * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
594 * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
595 * against all other leaves to look for simplifications.
597 static void expr_eliminate_dups1(enum expr_type type
, struct expr
**ep1
, struct expr
**ep2
)
603 /* Recurse down to leaves */
605 if (e1
->type
== type
) {
606 expr_eliminate_dups1(type
, &e1
->left
.expr
, &e2
);
607 expr_eliminate_dups1(type
, &e1
->right
.expr
, &e2
);
610 if (e2
->type
== type
) {
611 expr_eliminate_dups1(type
, &e1
, &e2
->left
.expr
);
612 expr_eliminate_dups1(type
, &e1
, &e2
->right
.expr
);
616 /* e1 and e2 are leaves. Compare and process them. */
622 case E_OR
: case E_AND
:
623 expr_eliminate_dups1(e1
->type
, &e1
, &e1
);
630 tmp
= expr_join_or(e1
, e2
);
632 expr_free(e1
); expr_free(e2
);
633 e1
= expr_alloc_symbol(&symbol_no
);
639 tmp
= expr_join_and(e1
, e2
);
641 expr_free(e1
); expr_free(e2
);
642 e1
= expr_alloc_symbol(&symbol_yes
);
655 * Rewrites 'e' in-place to remove ("join") duplicate and other redundant
658 * Example simplifications:
660 * A || B || A -> A || B
661 * A && B && A=y -> A=y && B
663 * Returns the deduplicated expression.
665 struct expr
*expr_eliminate_dups(struct expr
*e
)
671 oldcount
= trans_count
;
675 case E_OR
: case E_AND
:
676 expr_eliminate_dups1(e
->type
, &e
, &e
);
681 /* No simplifications done in this pass. We're done */
683 e
= expr_eliminate_yn(e
);
685 trans_count
= oldcount
;
690 * Performs various simplifications involving logical operators and
693 * Allocates and returns a new expression.
695 struct expr
*expr_transform(struct expr
*e
)
712 e
->left
.expr
= expr_transform(e
->left
.expr
);
713 e
->right
.expr
= expr_transform(e
->right
.expr
);
718 if (e
->left
.sym
->type
!= S_BOOLEAN
)
720 if (e
->right
.sym
== &symbol_no
) {
722 e
->left
.expr
= expr_alloc_symbol(e
->left
.sym
);
726 if (e
->right
.sym
== &symbol_mod
) {
727 printf("boolean symbol %s tested for 'm'? test forced to 'n'\n", e
->left
.sym
->name
);
729 e
->left
.sym
= &symbol_no
;
733 if (e
->right
.sym
== &symbol_yes
) {
740 if (e
->left
.sym
->type
!= S_BOOLEAN
)
742 if (e
->right
.sym
== &symbol_no
) {
747 if (e
->right
.sym
== &symbol_mod
) {
748 printf("boolean symbol %s tested for 'm'? test forced to 'y'\n", e
->left
.sym
->name
);
750 e
->left
.sym
= &symbol_yes
;
754 if (e
->right
.sym
== &symbol_yes
) {
756 e
->left
.expr
= expr_alloc_symbol(e
->left
.sym
);
762 switch (e
->left
.expr
->type
) {
765 tmp
= e
->left
.expr
->left
.expr
;
769 e
= expr_transform(e
);
777 e
->type
= e
->type
== E_EQUAL
? E_UNEQUAL
: E_EQUAL
;
785 e
->type
= e
->type
== E_LEQ
? E_GTH
: E_LTH
;
793 e
->type
= e
->type
== E_LTH
? E_GEQ
: E_LEQ
;
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 // !(a && b) -> !a || !b
808 e
->right
.expr
= expr_alloc_one(E_NOT
, tmp
->right
.expr
);
810 tmp
->right
.expr
= NULL
;
811 e
= expr_transform(e
);
814 if (e
->left
.expr
->left
.sym
== &symbol_yes
) {
820 e
->left
.sym
= &symbol_no
;
823 if (e
->left
.expr
->left
.sym
== &symbol_mod
) {
829 e
->left
.sym
= &symbol_mod
;
832 if (e
->left
.expr
->left
.sym
== &symbol_no
) {
838 e
->left
.sym
= &symbol_yes
;
852 int expr_contains_symbol(struct expr
*dep
, struct symbol
*sym
)
860 return expr_contains_symbol(dep
->left
.expr
, sym
) ||
861 expr_contains_symbol(dep
->right
.expr
, sym
);
863 return dep
->left
.sym
== sym
;
870 return dep
->left
.sym
== sym
||
871 dep
->right
.sym
== sym
;
873 return expr_contains_symbol(dep
->left
.expr
, sym
);
880 bool expr_depends_symbol(struct expr
*dep
, struct symbol
*sym
)
887 return expr_depends_symbol(dep
->left
.expr
, sym
) ||
888 expr_depends_symbol(dep
->right
.expr
, sym
);
890 return dep
->left
.sym
== sym
;
892 if (dep
->left
.sym
== sym
) {
893 if (dep
->right
.sym
== &symbol_yes
|| dep
->right
.sym
== &symbol_mod
)
898 if (dep
->left
.sym
== sym
) {
899 if (dep
->right
.sym
== &symbol_no
)
910 * Inserts explicit comparisons of type 'type' to symbol 'sym' into the
913 * Examples transformations for type == E_UNEQUAL, sym == &symbol_no:
917 * A && B -> !(A=n || B=n)
918 * A || B -> !(A=n && B=n)
919 * A && (B || C) -> !(A=n || (B=n && C=n))
921 * Allocates and returns a new expression.
923 struct expr
*expr_trans_compare(struct expr
*e
, enum expr_type type
, struct symbol
*sym
)
925 struct expr
*e1
, *e2
;
928 e
= expr_alloc_symbol(sym
);
929 if (type
== E_UNEQUAL
)
930 e
= expr_alloc_one(E_NOT
, e
);
935 e1
= expr_trans_compare(e
->left
.expr
, E_EQUAL
, sym
);
936 e2
= expr_trans_compare(e
->right
.expr
, E_EQUAL
, sym
);
937 if (sym
== &symbol_yes
)
938 e
= expr_alloc_two(E_AND
, e1
, e2
);
939 if (sym
== &symbol_no
)
940 e
= expr_alloc_two(E_OR
, e1
, e2
);
941 if (type
== E_UNEQUAL
)
942 e
= expr_alloc_one(E_NOT
, e
);
945 e1
= expr_trans_compare(e
->left
.expr
, E_EQUAL
, sym
);
946 e2
= expr_trans_compare(e
->right
.expr
, E_EQUAL
, sym
);
947 if (sym
== &symbol_yes
)
948 e
= expr_alloc_two(E_OR
, e1
, e2
);
949 if (sym
== &symbol_no
)
950 e
= expr_alloc_two(E_AND
, e1
, e2
);
951 if (type
== E_UNEQUAL
)
952 e
= expr_alloc_one(E_NOT
, e
);
955 return expr_trans_compare(e
->left
.expr
, type
== E_EQUAL
? E_UNEQUAL
: E_EQUAL
, sym
);
962 if (type
== E_EQUAL
) {
963 if (sym
== &symbol_yes
)
965 if (sym
== &symbol_mod
)
966 return expr_alloc_symbol(&symbol_no
);
967 if (sym
== &symbol_no
)
968 return expr_alloc_one(E_NOT
, expr_copy(e
));
970 if (sym
== &symbol_yes
)
971 return expr_alloc_one(E_NOT
, expr_copy(e
));
972 if (sym
== &symbol_mod
)
973 return expr_alloc_symbol(&symbol_yes
);
974 if (sym
== &symbol_no
)
979 return expr_alloc_comp(type
, e
->left
.sym
, sym
);
988 enum string_value_kind
{
995 unsigned long long u
;
999 static enum string_value_kind
expr_parse_string(const char *str
,
1000 enum symbol_type type
,
1001 union string_value
*val
)
1004 enum string_value_kind kind
;
1010 val
->s
= !strcmp(str
, "n") ? 0 :
1011 !strcmp(str
, "m") ? 1 :
1012 !strcmp(str
, "y") ? 2 : -1;
1015 val
->s
= strtoll(str
, &tail
, 10);
1019 val
->u
= strtoull(str
, &tail
, 16);
1023 val
->s
= strtoll(str
, &tail
, 0);
1027 return !errno
&& !*tail
&& tail
> str
&& isxdigit(tail
[-1])
1031 tristate
expr_calc_value(struct expr
*e
)
1033 tristate val1
, val2
;
1034 const char *str1
, *str2
;
1035 enum string_value_kind k1
= k_string
, k2
= k_string
;
1036 union string_value lval
= {}, rval
= {};
1044 sym_calc_value(e
->left
.sym
);
1045 return e
->left
.sym
->curr
.tri
;
1047 val1
= expr_calc_value(e
->left
.expr
);
1048 val2
= expr_calc_value(e
->right
.expr
);
1049 return EXPR_AND(val1
, val2
);
1051 val1
= expr_calc_value(e
->left
.expr
);
1052 val2
= expr_calc_value(e
->right
.expr
);
1053 return EXPR_OR(val1
, val2
);
1055 val1
= expr_calc_value(e
->left
.expr
);
1056 return EXPR_NOT(val1
);
1065 printf("expr_calc_value: %d?\n", e
->type
);
1069 sym_calc_value(e
->left
.sym
);
1070 sym_calc_value(e
->right
.sym
);
1071 str1
= sym_get_string_value(e
->left
.sym
);
1072 str2
= sym_get_string_value(e
->right
.sym
);
1074 if (e
->left
.sym
->type
!= S_STRING
|| e
->right
.sym
->type
!= S_STRING
) {
1075 k1
= expr_parse_string(str1
, e
->left
.sym
->type
, &lval
);
1076 k2
= expr_parse_string(str2
, e
->right
.sym
->type
, &rval
);
1079 if (k1
== k_string
|| k2
== k_string
)
1080 res
= strcmp(str1
, str2
);
1081 else if (k1
== k_unsigned
|| k2
== k_unsigned
)
1082 res
= (lval
.u
> rval
.u
) - (lval
.u
< rval
.u
);
1083 else /* if (k1 == k_signed && k2 == k_signed) */
1084 res
= (lval
.s
> rval
.s
) - (lval
.s
< rval
.s
);
1088 return res
? no
: yes
;
1090 return res
>= 0 ? yes
: no
;
1092 return res
> 0 ? yes
: no
;
1094 return res
<= 0 ? yes
: no
;
1096 return res
< 0 ? yes
: no
;
1098 return res
? yes
: no
;
1100 printf("expr_calc_value: relation %d?\n", e
->type
);
1105 static int expr_compare_type(enum expr_type t1
, enum expr_type t2
)
1114 if (t2
== E_EQUAL
|| t2
== E_UNEQUAL
)
1135 printf("[%dgt%d?]", t1
, t2
);
1139 void expr_print(struct expr
*e
,
1140 void (*fn
)(void *, struct symbol
*, const char *),
1141 void *data
, int prevtoken
)
1144 fn(data
, NULL
, "y");
1148 if (expr_compare_type(prevtoken
, e
->type
) > 0)
1149 fn(data
, NULL
, "(");
1152 if (e
->left
.sym
->name
)
1153 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1155 fn(data
, NULL
, "<choice>");
1158 fn(data
, NULL
, "!");
1159 expr_print(e
->left
.expr
, fn
, data
, E_NOT
);
1162 if (e
->left
.sym
->name
)
1163 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1165 fn(data
, NULL
, "<choice>");
1166 fn(data
, NULL
, "=");
1167 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1171 if (e
->left
.sym
->name
)
1172 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1174 fn(data
, NULL
, "<choice>");
1175 fn(data
, NULL
, e
->type
== E_LEQ
? "<=" : "<");
1176 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1180 if (e
->left
.sym
->name
)
1181 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1183 fn(data
, NULL
, "<choice>");
1184 fn(data
, NULL
, e
->type
== E_GEQ
? ">=" : ">");
1185 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1188 if (e
->left
.sym
->name
)
1189 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1191 fn(data
, NULL
, "<choice>");
1192 fn(data
, NULL
, "!=");
1193 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1196 expr_print(e
->left
.expr
, fn
, data
, E_OR
);
1197 fn(data
, NULL
, " || ");
1198 expr_print(e
->right
.expr
, fn
, data
, E_OR
);
1201 expr_print(e
->left
.expr
, fn
, data
, E_AND
);
1202 fn(data
, NULL
, " && ");
1203 expr_print(e
->right
.expr
, fn
, data
, E_AND
);
1206 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1208 fn(data
, NULL
, " ^ ");
1209 expr_print(e
->left
.expr
, fn
, data
, E_LIST
);
1213 fn(data
, NULL
, "[");
1214 fn(data
, e
->left
.sym
, e
->left
.sym
->name
);
1215 fn(data
, NULL
, " ");
1216 fn(data
, e
->right
.sym
, e
->right
.sym
->name
);
1217 fn(data
, NULL
, "]");
1222 sprintf(buf
, "<unknown type %d>", e
->type
);
1223 fn(data
, NULL
, buf
);
1227 if (expr_compare_type(prevtoken
, e
->type
) > 0)
1228 fn(data
, NULL
, ")");
1231 static void expr_print_file_helper(void *data
, struct symbol
*sym
, const char *str
)
1233 xfwrite(str
, strlen(str
), 1, data
);
1236 void expr_fprint(struct expr
*e
, FILE *out
)
1238 expr_print(e
, expr_print_file_helper
, out
, E_NONE
);
1241 static void expr_print_gstr_helper(void *data
, struct symbol
*sym
, const char *str
)
1243 struct gstr
*gs
= (struct gstr
*)data
;
1244 const char *sym_str
= NULL
;
1247 sym_str
= sym_get_string_value(sym
);
1249 if (gs
->max_width
) {
1250 unsigned extra_length
= strlen(str
);
1251 const char *last_cr
= strrchr(gs
->s
, '\n');
1252 unsigned last_line_length
;
1255 extra_length
+= 4 + strlen(sym_str
);
1260 last_line_length
= strlen(gs
->s
) - (last_cr
- gs
->s
);
1262 if ((last_line_length
+ extra_length
) > gs
->max_width
)
1263 str_append(gs
, "\\\n");
1266 str_append(gs
, str
);
1267 if (sym
&& sym
->type
!= S_UNKNOWN
)
1268 str_printf(gs
, " [=%s]", sym_str
);
1271 void expr_gstr_print(struct expr
*e
, struct gstr
*gs
)
1273 expr_print(e
, expr_print_gstr_helper
, gs
, E_NONE
);
1277 * Transform the top level "||" tokens into newlines and prepend each
1278 * line with a minus. This makes expressions much easier to read.
1279 * Suitable for reverse dependency expressions.
1281 static void expr_print_revdep(struct expr
*e
,
1282 void (*fn
)(void *, struct symbol
*, const char *),
1283 void *data
, tristate pr_type
, const char **title
)
1285 if (e
->type
== E_OR
) {
1286 expr_print_revdep(e
->left
.expr
, fn
, data
, pr_type
, title
);
1287 expr_print_revdep(e
->right
.expr
, fn
, data
, pr_type
, title
);
1288 } else if (expr_calc_value(e
) == pr_type
) {
1290 fn(data
, NULL
, *title
);
1294 fn(data
, NULL
, " - ");
1295 expr_print(e
, fn
, data
, E_NONE
);
1296 fn(data
, NULL
, "\n");
1300 void expr_gstr_print_revdep(struct expr
*e
, struct gstr
*gs
,
1301 tristate pr_type
, const char *title
)
1303 expr_print_revdep(e
, expr_print_gstr_helper
, gs
, pr_type
, &title
);