Blame view

scripts/kconfig/expr.c 30 KB
0c8741001   Masahiro Yamada   kconfig: convert ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
  /*
   * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
   */
558e78e3c   Masahiro Yamada   kconfig: split so...
5
6
  #include <ctype.h>
  #include <errno.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
  #include "lkc.h"
  
  #define DEBUG_EXPR	0
ad8d40cda   Michal Marek   kconfig: Remove u...
13
  static struct expr *expr_eliminate_yn(struct expr *e);
ad8d40cda   Michal Marek   kconfig: Remove u...
14

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
  struct expr *expr_alloc_symbol(struct symbol *sym)
  {
177acf784   Alan Cox   kconfig: Fix mall...
17
  	struct expr *e = xcalloc(1, sizeof(*e));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
21
22
23
24
  	e->type = E_SYMBOL;
  	e->left.sym = sym;
  	return e;
  }
  
  struct expr *expr_alloc_one(enum expr_type type, struct expr *ce)
  {
177acf784   Alan Cox   kconfig: Fix mall...
25
  	struct expr *e = xcalloc(1, sizeof(*e));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
30
31
32
  	e->type = type;
  	e->left.expr = ce;
  	return e;
  }
  
  struct expr *expr_alloc_two(enum expr_type type, struct expr *e1, struct expr *e2)
  {
177acf784   Alan Cox   kconfig: Fix mall...
33
  	struct expr *e = xcalloc(1, sizeof(*e));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
36
37
38
39
40
41
  	e->type = type;
  	e->left.expr = e1;
  	e->right.expr = e2;
  	return e;
  }
  
  struct expr *expr_alloc_comp(enum expr_type type, struct symbol *s1, struct symbol *s2)
  {
177acf784   Alan Cox   kconfig: Fix mall...
42
  	struct expr *e = xcalloc(1, sizeof(*e));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  	e->type = type;
  	e->left.sym = s1;
  	e->right.sym = s2;
  	return e;
  }
  
  struct expr *expr_alloc_and(struct expr *e1, struct expr *e2)
  {
  	if (!e1)
  		return e2;
  	return e2 ? expr_alloc_two(E_AND, e1, e2) : e1;
  }
  
  struct expr *expr_alloc_or(struct expr *e1, struct expr *e2)
  {
  	if (!e1)
  		return e2;
  	return e2 ? expr_alloc_two(E_OR, e1, e2) : e1;
  }
17742dc74   Michal Marek   kconfig: Make exp...
62
  struct expr *expr_copy(const struct expr *org)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
64
65
66
67
  {
  	struct expr *e;
  
  	if (!org)
  		return NULL;
177acf784   Alan Cox   kconfig: Fix mall...
68
  	e = xmalloc(sizeof(*org));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
71
72
73
74
75
76
77
  	memcpy(e, org, sizeof(*org));
  	switch (org->type) {
  	case E_SYMBOL:
  		e->left = org->left;
  		break;
  	case E_NOT:
  		e->left.expr = expr_copy(org->left.expr);
  		break;
  	case E_EQUAL:
31847b67b   Jan Beulich   kconfig: allow us...
78
79
80
81
  	case E_GEQ:
  	case E_GTH:
  	case E_LEQ:
  	case E_LTH:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
85
86
87
  	case E_UNEQUAL:
  		e->left.sym = org->left.sym;
  		e->right.sym = org->right.sym;
  		break;
  	case E_AND:
  	case E_OR:
7a9629233   Roman Zippel   kconfig: explicit...
88
  	case E_LIST:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
91
92
  		e->left.expr = expr_copy(org->left.expr);
  		e->right.expr = expr_copy(org->right.expr);
  		break;
  	default:
9e3e10c72   Masahiro Yamada   kconfig: send err...
93
94
  		fprintf(stderr, "can't copy type %d
  ", e->type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  		free(e);
  		e = NULL;
  		break;
  	}
  
  	return e;
  }
  
  void expr_free(struct expr *e)
  {
  	if (!e)
  		return;
  
  	switch (e->type) {
  	case E_SYMBOL:
  		break;
  	case E_NOT:
  		expr_free(e->left.expr);
5b1374b3b   Ulf Magnusson   kconfig: Fix expr...
113
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
  	case E_EQUAL:
31847b67b   Jan Beulich   kconfig: allow us...
115
116
117
118
  	case E_GEQ:
  	case E_GTH:
  	case E_LEQ:
  	case E_LTH:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
121
122
123
124
125
126
  	case E_UNEQUAL:
  		break;
  	case E_OR:
  	case E_AND:
  		expr_free(e->left.expr);
  		expr_free(e->right.expr);
  		break;
  	default:
9e3e10c72   Masahiro Yamada   kconfig: send err...
127
128
  		fprintf(stderr, "how to free type %d?
  ", e->type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
133
134
135
136
137
  		break;
  	}
  	free(e);
  }
  
  static int trans_count;
  
  #define e1 (*ep1)
  #define e2 (*ep2)
0735f7e5d   Ulf Magnusson   kconfig: Document...
138
139
140
141
142
143
144
145
  /*
   * expr_eliminate_eq() helper.
   *
   * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
   * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
   * against all other leaves. Two equal leaves are both replaced with either 'y'
   * or 'n' as appropriate for 'type', to be eliminated later.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
  static void __expr_eliminate_eq(enum expr_type type, struct expr **ep1, struct expr **ep2)
  {
0735f7e5d   Ulf Magnusson   kconfig: Document...
148
  	/* Recurse down to leaves */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
150
151
152
153
154
155
156
157
158
  	if (e1->type == type) {
  		__expr_eliminate_eq(type, &e1->left.expr, &e2);
  		__expr_eliminate_eq(type, &e1->right.expr, &e2);
  		return;
  	}
  	if (e2->type == type) {
  		__expr_eliminate_eq(type, &e1, &e2->left.expr);
  		__expr_eliminate_eq(type, &e1, &e2->right.expr);
  		return;
  	}
0735f7e5d   Ulf Magnusson   kconfig: Document...
159
160
  
  	/* e1 and e2 are leaves. Compare them. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
  	if (e1->type == E_SYMBOL && e2->type == E_SYMBOL &&
c0e150acd   Roman Zippel   kconfig: remove S...
162
163
  	    e1->left.sym == e2->left.sym &&
  	    (e1->left.sym == &symbol_yes || e1->left.sym == &symbol_no))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
165
166
  		return;
  	if (!expr_eq(e1, e2))
  		return;
0735f7e5d   Ulf Magnusson   kconfig: Document...
167
168
  
  	/* e1 and e2 are equal leaves. Prepare them for elimination. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  	trans_count++;
  	expr_free(e1); expr_free(e2);
  	switch (type) {
  	case E_OR:
  		e1 = expr_alloc_symbol(&symbol_no);
  		e2 = expr_alloc_symbol(&symbol_no);
  		break;
  	case E_AND:
  		e1 = expr_alloc_symbol(&symbol_yes);
  		e2 = expr_alloc_symbol(&symbol_yes);
  		break;
  	default:
  		;
  	}
  }
0735f7e5d   Ulf Magnusson   kconfig: Document...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  /*
   * Rewrites the expressions 'ep1' and 'ep2' to remove operands common to both.
   * Example reductions:
   *
   *	ep1: A && B           ->  ep1: y
   *	ep2: A && B && C      ->  ep2: C
   *
   *	ep1: A || B           ->  ep1: n
   *	ep2: A || B || C      ->  ep2: C
   *
   *	ep1: A && (B && FOO)  ->  ep1: FOO
   *	ep2: (BAR && B) && A  ->  ep2: BAR
   *
   *	ep1: A && (B || C)    ->  ep1: y
   *	ep2: (C || B) && A    ->  ep2: y
   *
   * Comparisons are done between all operands at the same "level" of && or ||.
   * For example, in the expression 'e1 && (e2 || e3) && (e4 || e5)', the
   * following operands will be compared:
   *
   *	- 'e1', 'e2 || e3', and 'e4 || e5', against each other
   *	- e2 against e3
   *	- e4 against e5
   *
   * Parentheses are irrelevant within a single level. 'e1 && (e2 && e3)' and
   * '(e1 && e2) && e3' are both a single level.
   *
   * See __expr_eliminate_eq() as well.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  void expr_eliminate_eq(struct expr **ep1, struct expr **ep2)
  {
  	if (!e1 || !e2)
  		return;
  	switch (e1->type) {
  	case E_OR:
  	case E_AND:
  		__expr_eliminate_eq(e1->type, ep1, ep2);
  	default:
  		;
  	}
  	if (e1->type != e2->type) switch (e2->type) {
  	case E_OR:
  	case E_AND:
  		__expr_eliminate_eq(e2->type, ep1, ep2);
  	default:
  		;
  	}
  	e1 = expr_eliminate_yn(e1);
  	e2 = expr_eliminate_yn(e2);
  }
  
  #undef e1
  #undef e2
0735f7e5d   Ulf Magnusson   kconfig: Document...
237
238
239
240
241
242
  /*
   * Returns true if 'e1' and 'e2' are equal, after minor simplification. Two
   * &&/|| expressions are considered equal if every operand in one expression
   * equals some operand in the other (operands do not need to appear in the same
   * order), recursively.
   */
3460d0bc2   Thomas Hebb   kconfig: distingu...
243
  int expr_eq(struct expr *e1, struct expr *e2)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
  {
  	int res, old_count;
272a72103   Thomas Hebb   kconfig: don't cr...
246
247
248
249
250
251
  	/*
  	 * A NULL expr is taken to be yes, but there's also a different way to
  	 * represent yes. expr_is_yes() checks for either representation.
  	 */
  	if (!e1 || !e2)
  		return expr_is_yes(e1) && expr_is_yes(e2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
253
254
255
  	if (e1->type != e2->type)
  		return 0;
  	switch (e1->type) {
  	case E_EQUAL:
31847b67b   Jan Beulich   kconfig: allow us...
256
257
258
259
  	case E_GEQ:
  	case E_GTH:
  	case E_LEQ:
  	case E_LTH:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
  	case E_UNEQUAL:
  		return e1->left.sym == e2->left.sym && e1->right.sym == e2->right.sym;
  	case E_SYMBOL:
  		return e1->left.sym == e2->left.sym;
  	case E_NOT:
  		return expr_eq(e1->left.expr, e2->left.expr);
  	case E_AND:
  	case E_OR:
  		e1 = expr_copy(e1);
  		e2 = expr_copy(e2);
  		old_count = trans_count;
  		expr_eliminate_eq(&e1, &e2);
  		res = (e1->type == E_SYMBOL && e2->type == E_SYMBOL &&
  		       e1->left.sym == e2->left.sym);
  		expr_free(e1);
  		expr_free(e2);
  		trans_count = old_count;
  		return res;
7a9629233   Roman Zippel   kconfig: explicit...
278
  	case E_LIST:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
  	case E_RANGE:
  	case E_NONE:
  		/* panic */;
  	}
  
  	if (DEBUG_EXPR) {
  		expr_fprint(e1, stdout);
  		printf(" = ");
  		expr_fprint(e2, stdout);
  		printf(" ?
  ");
  	}
  
  	return 0;
  }
0735f7e5d   Ulf Magnusson   kconfig: Document...
294
295
296
297
298
299
300
301
302
303
304
  /*
   * Recursively performs the following simplifications in-place (as well as the
   * corresponding simplifications with swapped operands):
   *
   *	expr && n  ->  n
   *	expr && y  ->  expr
   *	expr || n  ->  expr
   *	expr || y  ->  y
   *
   * Returns the optimized expression.
   */
ad8d40cda   Michal Marek   kconfig: Remove u...
305
  static struct expr *expr_eliminate_yn(struct expr *e)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
  {
  	struct expr *tmp;
  
  	if (e) switch (e->type) {
  	case E_AND:
  		e->left.expr = expr_eliminate_yn(e->left.expr);
  		e->right.expr = expr_eliminate_yn(e->right.expr);
  		if (e->left.expr->type == E_SYMBOL) {
  			if (e->left.expr->left.sym == &symbol_no) {
  				expr_free(e->left.expr);
  				expr_free(e->right.expr);
  				e->type = E_SYMBOL;
  				e->left.sym = &symbol_no;
  				e->right.expr = NULL;
  				return e;
  			} else if (e->left.expr->left.sym == &symbol_yes) {
  				free(e->left.expr);
  				tmp = e->right.expr;
  				*e = *(e->right.expr);
  				free(tmp);
  				return e;
  			}
  		}
  		if (e->right.expr->type == E_SYMBOL) {
  			if (e->right.expr->left.sym == &symbol_no) {
  				expr_free(e->left.expr);
  				expr_free(e->right.expr);
  				e->type = E_SYMBOL;
  				e->left.sym = &symbol_no;
  				e->right.expr = NULL;
  				return e;
  			} else if (e->right.expr->left.sym == &symbol_yes) {
  				free(e->right.expr);
  				tmp = e->left.expr;
  				*e = *(e->left.expr);
  				free(tmp);
  				return e;
  			}
  		}
  		break;
  	case E_OR:
  		e->left.expr = expr_eliminate_yn(e->left.expr);
  		e->right.expr = expr_eliminate_yn(e->right.expr);
  		if (e->left.expr->type == E_SYMBOL) {
  			if (e->left.expr->left.sym == &symbol_no) {
  				free(e->left.expr);
  				tmp = e->right.expr;
  				*e = *(e->right.expr);
  				free(tmp);
  				return e;
  			} else if (e->left.expr->left.sym == &symbol_yes) {
  				expr_free(e->left.expr);
  				expr_free(e->right.expr);
  				e->type = E_SYMBOL;
  				e->left.sym = &symbol_yes;
  				e->right.expr = NULL;
  				return e;
  			}
  		}
  		if (e->right.expr->type == E_SYMBOL) {
  			if (e->right.expr->left.sym == &symbol_no) {
  				free(e->right.expr);
  				tmp = e->left.expr;
  				*e = *(e->left.expr);
  				free(tmp);
  				return e;
  			} else if (e->right.expr->left.sym == &symbol_yes) {
  				expr_free(e->left.expr);
  				expr_free(e->right.expr);
  				e->type = E_SYMBOL;
  				e->left.sym = &symbol_yes;
  				e->right.expr = NULL;
  				return e;
  			}
  		}
  		break;
  	default:
  		;
  	}
  	return e;
  }
  
  /*
   * bool FOO!=n => FOO
   */
  struct expr *expr_trans_bool(struct expr *e)
  {
  	if (!e)
  		return NULL;
  	switch (e->type) {
  	case E_AND:
  	case E_OR:
  	case E_NOT:
  		e->left.expr = expr_trans_bool(e->left.expr);
  		e->right.expr = expr_trans_bool(e->right.expr);
  		break;
  	case E_UNEQUAL:
  		// FOO!=n -> FOO
  		if (e->left.sym->type == S_TRISTATE) {
  			if (e->right.sym == &symbol_no) {
  				e->type = E_SYMBOL;
  				e->right.sym = NULL;
  			}
  		}
  		break;
  	default:
  		;
  	}
  	return e;
  }
  
  /*
   * e1 || e2 -> ?
   */
4356f4890   Trevor Keith   kbuild: add stati...
420
  static struct expr *expr_join_or(struct expr *e1, struct expr *e2)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
  {
  	struct expr *tmp;
  	struct symbol *sym1, *sym2;
  
  	if (expr_eq(e1, e2))
  		return expr_copy(e1);
  	if (e1->type != E_EQUAL && e1->type != E_UNEQUAL && e1->type != E_SYMBOL && e1->type != E_NOT)
  		return NULL;
  	if (e2->type != E_EQUAL && e2->type != E_UNEQUAL && e2->type != E_SYMBOL && e2->type != E_NOT)
  		return NULL;
  	if (e1->type == E_NOT) {
  		tmp = e1->left.expr;
  		if (tmp->type != E_EQUAL && tmp->type != E_UNEQUAL && tmp->type != E_SYMBOL)
  			return NULL;
  		sym1 = tmp->left.sym;
  	} else
  		sym1 = e1->left.sym;
  	if (e2->type == E_NOT) {
  		if (e2->left.expr->type != E_SYMBOL)
  			return NULL;
  		sym2 = e2->left.expr->left.sym;
  	} else
  		sym2 = e2->left.sym;
  	if (sym1 != sym2)
  		return NULL;
  	if (sym1->type != S_BOOLEAN && sym1->type != S_TRISTATE)
  		return NULL;
  	if (sym1->type == S_TRISTATE) {
  		if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
  		    ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_mod) ||
  		     (e1->right.sym == &symbol_mod && e2->right.sym == &symbol_yes))) {
  			// (a='y') || (a='m') -> (a!='n')
  			return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_no);
  		}
  		if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
  		    ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_no) ||
  		     (e1->right.sym == &symbol_no && e2->right.sym == &symbol_yes))) {
  			// (a='y') || (a='n') -> (a!='m')
  			return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_mod);
  		}
  		if (e1->type == E_EQUAL && e2->type == E_EQUAL &&
  		    ((e1->right.sym == &symbol_mod && e2->right.sym == &symbol_no) ||
  		     (e1->right.sym == &symbol_no && e2->right.sym == &symbol_mod))) {
  			// (a='m') || (a='n') -> (a!='y')
  			return expr_alloc_comp(E_UNEQUAL, sym1, &symbol_yes);
  		}
  	}
  	if (sym1->type == S_BOOLEAN && sym1 == sym2) {
  		if ((e1->type == E_NOT && e1->left.expr->type == E_SYMBOL && e2->type == E_SYMBOL) ||
  		    (e2->type == E_NOT && e2->left.expr->type == E_SYMBOL && e1->type == E_SYMBOL))
  			return expr_alloc_symbol(&symbol_yes);
  	}
  
  	if (DEBUG_EXPR) {
  		printf("optimize (");
  		expr_fprint(e1, stdout);
  		printf(") || (");
  		expr_fprint(e2, stdout);
  		printf(")?
  ");
  	}
  	return NULL;
  }
4356f4890   Trevor Keith   kbuild: add stati...
484
  static struct expr *expr_join_and(struct expr *e1, struct expr *e2)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  {
  	struct expr *tmp;
  	struct symbol *sym1, *sym2;
  
  	if (expr_eq(e1, e2))
  		return expr_copy(e1);
  	if (e1->type != E_EQUAL && e1->type != E_UNEQUAL && e1->type != E_SYMBOL && e1->type != E_NOT)
  		return NULL;
  	if (e2->type != E_EQUAL && e2->type != E_UNEQUAL && e2->type != E_SYMBOL && e2->type != E_NOT)
  		return NULL;
  	if (e1->type == E_NOT) {
  		tmp = e1->left.expr;
  		if (tmp->type != E_EQUAL && tmp->type != E_UNEQUAL && tmp->type != E_SYMBOL)
  			return NULL;
  		sym1 = tmp->left.sym;
  	} else
  		sym1 = e1->left.sym;
  	if (e2->type == E_NOT) {
  		if (e2->left.expr->type != E_SYMBOL)
  			return NULL;
  		sym2 = e2->left.expr->left.sym;
  	} else
  		sym2 = e2->left.sym;
  	if (sym1 != sym2)
  		return NULL;
  	if (sym1->type != S_BOOLEAN && sym1->type != S_TRISTATE)
  		return NULL;
  
  	if ((e1->type == E_SYMBOL && e2->type == E_EQUAL && e2->right.sym == &symbol_yes) ||
  	    (e2->type == E_SYMBOL && e1->type == E_EQUAL && e1->right.sym == &symbol_yes))
  		// (a) && (a='y') -> (a='y')
  		return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
  
  	if ((e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_no) ||
  	    (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_no))
  		// (a) && (a!='n') -> (a)
  		return expr_alloc_symbol(sym1);
  
  	if ((e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_mod) ||
  	    (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_mod))
  		// (a) && (a!='m') -> (a='y')
  		return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
  
  	if (sym1->type == S_TRISTATE) {
  		if (e1->type == E_EQUAL && e2->type == E_UNEQUAL) {
  			// (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
  			sym2 = e1->right.sym;
  			if ((e2->right.sym->flags & SYMBOL_CONST) && (sym2->flags & SYMBOL_CONST))
  				return sym2 != e2->right.sym ? expr_alloc_comp(E_EQUAL, sym1, sym2)
  							     : expr_alloc_symbol(&symbol_no);
  		}
  		if (e1->type == E_UNEQUAL && e2->type == E_EQUAL) {
  			// (a='b') && (a!='c') -> 'b'='c' ? 'n' : a='b'
  			sym2 = e2->right.sym;
  			if ((e1->right.sym->flags & SYMBOL_CONST) && (sym2->flags & SYMBOL_CONST))
  				return sym2 != e1->right.sym ? expr_alloc_comp(E_EQUAL, sym1, sym2)
  							     : expr_alloc_symbol(&symbol_no);
  		}
  		if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
  			   ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_no) ||
  			    (e1->right.sym == &symbol_no && e2->right.sym == &symbol_yes)))
  			// (a!='y') && (a!='n') -> (a='m')
  			return expr_alloc_comp(E_EQUAL, sym1, &symbol_mod);
  
  		if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
  			   ((e1->right.sym == &symbol_yes && e2->right.sym == &symbol_mod) ||
  			    (e1->right.sym == &symbol_mod && e2->right.sym == &symbol_yes)))
  			// (a!='y') && (a!='m') -> (a='n')
  			return expr_alloc_comp(E_EQUAL, sym1, &symbol_no);
  
  		if (e1->type == E_UNEQUAL && e2->type == E_UNEQUAL &&
  			   ((e1->right.sym == &symbol_mod && e2->right.sym == &symbol_no) ||
  			    (e1->right.sym == &symbol_no && e2->right.sym == &symbol_mod)))
  			// (a!='m') && (a!='n') -> (a='m')
  			return expr_alloc_comp(E_EQUAL, sym1, &symbol_yes);
  
  		if ((e1->type == E_SYMBOL && e2->type == E_EQUAL && e2->right.sym == &symbol_mod) ||
  		    (e2->type == E_SYMBOL && e1->type == E_EQUAL && e1->right.sym == &symbol_mod) ||
  		    (e1->type == E_SYMBOL && e2->type == E_UNEQUAL && e2->right.sym == &symbol_yes) ||
  		    (e2->type == E_SYMBOL && e1->type == E_UNEQUAL && e1->right.sym == &symbol_yes))
  			return NULL;
  	}
  
  	if (DEBUG_EXPR) {
  		printf("optimize (");
  		expr_fprint(e1, stdout);
  		printf(") && (");
  		expr_fprint(e2, stdout);
  		printf(")?
  ");
  	}
  	return NULL;
  }
0735f7e5d   Ulf Magnusson   kconfig: Document...
578
579
580
581
582
583
584
  /*
   * expr_eliminate_dups() helper.
   *
   * Walks the two expression trees given in 'ep1' and 'ep2'. Any node that does
   * not have type 'type' (E_OR/E_AND) is considered a leaf, and is compared
   * against all other leaves to look for simplifications.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
585
586
587
588
589
  static void expr_eliminate_dups1(enum expr_type type, struct expr **ep1, struct expr **ep2)
  {
  #define e1 (*ep1)
  #define e2 (*ep2)
  	struct expr *tmp;
0735f7e5d   Ulf Magnusson   kconfig: Document...
590
  	/* Recurse down to leaves */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
591
592
593
594
595
596
597
598
599
600
  	if (e1->type == type) {
  		expr_eliminate_dups1(type, &e1->left.expr, &e2);
  		expr_eliminate_dups1(type, &e1->right.expr, &e2);
  		return;
  	}
  	if (e2->type == type) {
  		expr_eliminate_dups1(type, &e1, &e2->left.expr);
  		expr_eliminate_dups1(type, &e1, &e2->right.expr);
  		return;
  	}
0735f7e5d   Ulf Magnusson   kconfig: Document...
601
602
  
  	/* e1 and e2 are leaves. Compare and process them. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
  	if (e1 == e2)
  		return;
  
  	switch (e1->type) {
  	case E_OR: case E_AND:
  		expr_eliminate_dups1(e1->type, &e1, &e1);
  	default:
  		;
  	}
  
  	switch (type) {
  	case E_OR:
  		tmp = expr_join_or(e1, e2);
  		if (tmp) {
  			expr_free(e1); expr_free(e2);
  			e1 = expr_alloc_symbol(&symbol_no);
  			e2 = tmp;
  			trans_count++;
  		}
  		break;
  	case E_AND:
  		tmp = expr_join_and(e1, e2);
  		if (tmp) {
  			expr_free(e1); expr_free(e2);
  			e1 = expr_alloc_symbol(&symbol_yes);
  			e2 = tmp;
  			trans_count++;
  		}
  		break;
  	default:
  		;
  	}
  #undef e1
  #undef e2
  }
0735f7e5d   Ulf Magnusson   kconfig: Document...
638
639
640
641
642
643
644
645
646
647
648
  /*
   * Rewrites 'e' in-place to remove ("join") duplicate and other redundant
   * operands.
   *
   * Example simplifications:
   *
   *	A || B || A    ->  A || B
   *	A && B && A=y  ->  A=y && B
   *
   * Returns the deduplicated expression.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
649
650
651
652
653
654
655
656
657
658
659
660
  struct expr *expr_eliminate_dups(struct expr *e)
  {
  	int oldcount;
  	if (!e)
  		return e;
  
  	oldcount = trans_count;
  	while (1) {
  		trans_count = 0;
  		switch (e->type) {
  		case E_OR: case E_AND:
  			expr_eliminate_dups1(e->type, &e, &e);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
661
662
663
664
  		default:
  			;
  		}
  		if (!trans_count)
0735f7e5d   Ulf Magnusson   kconfig: Document...
665
  			/* No simplifications done in this pass. We're done */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666
667
668
669
670
671
  			break;
  		e = expr_eliminate_yn(e);
  	}
  	trans_count = oldcount;
  	return e;
  }
0735f7e5d   Ulf Magnusson   kconfig: Document...
672
673
674
675
676
677
  /*
   * Performs various simplifications involving logical operators and
   * comparisons.
   *
   * Allocates and returns a new expression.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
678
679
680
681
682
683
684
685
  struct expr *expr_transform(struct expr *e)
  {
  	struct expr *tmp;
  
  	if (!e)
  		return NULL;
  	switch (e->type) {
  	case E_EQUAL:
31847b67b   Jan Beulich   kconfig: allow us...
686
687
688
689
  	case E_GEQ:
  	case E_GTH:
  	case E_LEQ:
  	case E_LTH:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
690
691
  	case E_UNEQUAL:
  	case E_SYMBOL:
7a9629233   Roman Zippel   kconfig: explicit...
692
  	case E_LIST:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
  		break;
  	default:
  		e->left.expr = expr_transform(e->left.expr);
  		e->right.expr = expr_transform(e->right.expr);
  	}
  
  	switch (e->type) {
  	case E_EQUAL:
  		if (e->left.sym->type != S_BOOLEAN)
  			break;
  		if (e->right.sym == &symbol_no) {
  			e->type = E_NOT;
  			e->left.expr = expr_alloc_symbol(e->left.sym);
  			e->right.sym = NULL;
  			break;
  		}
  		if (e->right.sym == &symbol_mod) {
  			printf("boolean symbol %s tested for 'm'? test forced to 'n'
  ", e->left.sym->name);
  			e->type = E_SYMBOL;
  			e->left.sym = &symbol_no;
  			e->right.sym = NULL;
  			break;
  		}
  		if (e->right.sym == &symbol_yes) {
  			e->type = E_SYMBOL;
  			e->right.sym = NULL;
  			break;
  		}
  		break;
  	case E_UNEQUAL:
  		if (e->left.sym->type != S_BOOLEAN)
  			break;
  		if (e->right.sym == &symbol_no) {
  			e->type = E_SYMBOL;
  			e->right.sym = NULL;
  			break;
  		}
  		if (e->right.sym == &symbol_mod) {
  			printf("boolean symbol %s tested for 'm'? test forced to 'y'
  ", e->left.sym->name);
  			e->type = E_SYMBOL;
  			e->left.sym = &symbol_yes;
  			e->right.sym = NULL;
  			break;
  		}
  		if (e->right.sym == &symbol_yes) {
  			e->type = E_NOT;
  			e->left.expr = expr_alloc_symbol(e->left.sym);
  			e->right.sym = NULL;
  			break;
  		}
  		break;
  	case E_NOT:
  		switch (e->left.expr->type) {
  		case E_NOT:
  			// !!a -> a
  			tmp = e->left.expr->left.expr;
  			free(e->left.expr);
  			free(e);
  			e = tmp;
  			e = expr_transform(e);
  			break;
  		case E_EQUAL:
  		case E_UNEQUAL:
  			// !a='x' -> a!='x'
  			tmp = e->left.expr;
  			free(e);
  			e = tmp;
  			e->type = e->type == E_EQUAL ? E_UNEQUAL : E_EQUAL;
  			break;
31847b67b   Jan Beulich   kconfig: allow us...
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
  		case E_LEQ:
  		case E_GEQ:
  			// !a<='x' -> a>'x'
  			tmp = e->left.expr;
  			free(e);
  			e = tmp;
  			e->type = e->type == E_LEQ ? E_GTH : E_LTH;
  			break;
  		case E_LTH:
  		case E_GTH:
  			// !a<'x' -> a>='x'
  			tmp = e->left.expr;
  			free(e);
  			e = tmp;
  			e->type = e->type == E_LTH ? E_GEQ : E_LEQ;
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
  		case E_OR:
  			// !(a || b) -> !a && !b
  			tmp = e->left.expr;
  			e->type = E_AND;
  			e->right.expr = expr_alloc_one(E_NOT, tmp->right.expr);
  			tmp->type = E_NOT;
  			tmp->right.expr = NULL;
  			e = expr_transform(e);
  			break;
  		case E_AND:
  			// !(a && b) -> !a || !b
  			tmp = e->left.expr;
  			e->type = E_OR;
  			e->right.expr = expr_alloc_one(E_NOT, tmp->right.expr);
  			tmp->type = E_NOT;
  			tmp->right.expr = NULL;
  			e = expr_transform(e);
  			break;
  		case E_SYMBOL:
  			if (e->left.expr->left.sym == &symbol_yes) {
  				// !'y' -> 'n'
  				tmp = e->left.expr;
  				free(e);
  				e = tmp;
  				e->type = E_SYMBOL;
  				e->left.sym = &symbol_no;
  				break;
  			}
  			if (e->left.expr->left.sym == &symbol_mod) {
  				// !'m' -> 'm'
  				tmp = e->left.expr;
  				free(e);
  				e = tmp;
  				e->type = E_SYMBOL;
  				e->left.sym = &symbol_mod;
  				break;
  			}
  			if (e->left.expr->left.sym == &symbol_no) {
  				// !'n' -> 'y'
  				tmp = e->left.expr;
  				free(e);
  				e = tmp;
  				e->type = E_SYMBOL;
  				e->left.sym = &symbol_yes;
  				break;
  			}
  			break;
  		default:
  			;
  		}
  		break;
  	default:
  		;
  	}
  	return e;
  }
  
  int expr_contains_symbol(struct expr *dep, struct symbol *sym)
  {
  	if (!dep)
  		return 0;
  
  	switch (dep->type) {
  	case E_AND:
  	case E_OR:
  		return expr_contains_symbol(dep->left.expr, sym) ||
  		       expr_contains_symbol(dep->right.expr, sym);
  	case E_SYMBOL:
  		return dep->left.sym == sym;
  	case E_EQUAL:
31847b67b   Jan Beulich   kconfig: allow us...
850
851
852
853
  	case E_GEQ:
  	case E_GTH:
  	case E_LEQ:
  	case E_LTH:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
  	case E_UNEQUAL:
  		return dep->left.sym == sym ||
  		       dep->right.sym == sym;
  	case E_NOT:
  		return expr_contains_symbol(dep->left.expr, sym);
  	default:
  		;
  	}
  	return 0;
  }
  
  bool expr_depends_symbol(struct expr *dep, struct symbol *sym)
  {
  	if (!dep)
  		return false;
  
  	switch (dep->type) {
  	case E_AND:
  		return expr_depends_symbol(dep->left.expr, sym) ||
  		       expr_depends_symbol(dep->right.expr, sym);
  	case E_SYMBOL:
  		return dep->left.sym == sym;
  	case E_EQUAL:
  		if (dep->left.sym == sym) {
  			if (dep->right.sym == &symbol_yes || dep->right.sym == &symbol_mod)
  				return true;
  		}
  		break;
  	case E_UNEQUAL:
  		if (dep->left.sym == sym) {
  			if (dep->right.sym == &symbol_no)
  				return true;
  		}
  		break;
  	default:
  		;
  	}
   	return false;
  }
0735f7e5d   Ulf Magnusson   kconfig: Document...
893
894
895
896
897
898
899
900
901
902
903
904
905
906
  /*
   * Inserts explicit comparisons of type 'type' to symbol 'sym' into the
   * expression 'e'.
   *
   * Examples transformations for type == E_UNEQUAL, sym == &symbol_no:
   *
   *	A              ->  A!=n
   *	!A             ->  A=n
   *	A && B         ->  !(A=n || B=n)
   *	A || B         ->  !(A=n && B=n)
   *	A && (B || C)  ->  !(A=n || (B=n && C=n))
   *
   * Allocates and returns a new expression.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
  struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym)
  {
  	struct expr *e1, *e2;
  
  	if (!e) {
  		e = expr_alloc_symbol(sym);
  		if (type == E_UNEQUAL)
  			e = expr_alloc_one(E_NOT, e);
  		return e;
  	}
  	switch (e->type) {
  	case E_AND:
  		e1 = expr_trans_compare(e->left.expr, E_EQUAL, sym);
  		e2 = expr_trans_compare(e->right.expr, E_EQUAL, sym);
  		if (sym == &symbol_yes)
  			e = expr_alloc_two(E_AND, e1, e2);
  		if (sym == &symbol_no)
  			e = expr_alloc_two(E_OR, e1, e2);
  		if (type == E_UNEQUAL)
  			e = expr_alloc_one(E_NOT, e);
  		return e;
  	case E_OR:
  		e1 = expr_trans_compare(e->left.expr, E_EQUAL, sym);
  		e2 = expr_trans_compare(e->right.expr, E_EQUAL, sym);
  		if (sym == &symbol_yes)
  			e = expr_alloc_two(E_OR, e1, e2);
  		if (sym == &symbol_no)
  			e = expr_alloc_two(E_AND, e1, e2);
  		if (type == E_UNEQUAL)
  			e = expr_alloc_one(E_NOT, e);
  		return e;
  	case E_NOT:
  		return expr_trans_compare(e->left.expr, type == E_EQUAL ? E_UNEQUAL : E_EQUAL, sym);
  	case E_UNEQUAL:
31847b67b   Jan Beulich   kconfig: allow us...
941
942
943
944
  	case E_LTH:
  	case E_LEQ:
  	case E_GTH:
  	case E_GEQ:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
  	case E_EQUAL:
  		if (type == E_EQUAL) {
  			if (sym == &symbol_yes)
  				return expr_copy(e);
  			if (sym == &symbol_mod)
  				return expr_alloc_symbol(&symbol_no);
  			if (sym == &symbol_no)
  				return expr_alloc_one(E_NOT, expr_copy(e));
  		} else {
  			if (sym == &symbol_yes)
  				return expr_alloc_one(E_NOT, expr_copy(e));
  			if (sym == &symbol_mod)
  				return expr_alloc_symbol(&symbol_yes);
  			if (sym == &symbol_no)
  				return expr_copy(e);
  		}
  		break;
  	case E_SYMBOL:
  		return expr_alloc_comp(type, e->left.sym, sym);
7a9629233   Roman Zippel   kconfig: explicit...
964
  	case E_LIST:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
965
966
967
968
969
970
  	case E_RANGE:
  	case E_NONE:
  		/* panic */;
  	}
  	return NULL;
  }
31847b67b   Jan Beulich   kconfig: allow us...
971
972
973
974
  enum string_value_kind {
  	k_string,
  	k_signed,
  	k_unsigned,
31847b67b   Jan Beulich   kconfig: allow us...
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
  };
  
  union string_value {
  	unsigned long long u;
  	signed long long s;
  };
  
  static enum string_value_kind expr_parse_string(const char *str,
  						enum symbol_type type,
  						union string_value *val)
  {
  	char *tail;
  	enum string_value_kind kind;
  
  	errno = 0;
  	switch (type) {
  	case S_BOOLEAN:
  	case S_TRISTATE:
9059a3493   Nicolas Pitre   kconfig: fix rela...
993
994
995
996
  		val->s = !strcmp(str, "n") ? 0 :
  			 !strcmp(str, "m") ? 1 :
  			 !strcmp(str, "y") ? 2 : -1;
  		return k_signed;
31847b67b   Jan Beulich   kconfig: allow us...
997
998
999
1000
1001
1002
1003
1004
  	case S_INT:
  		val->s = strtoll(str, &tail, 10);
  		kind = k_signed;
  		break;
  	case S_HEX:
  		val->u = strtoull(str, &tail, 16);
  		kind = k_unsigned;
  		break;
0cbe3ac43   Masahiro Yamada   kconfig: remove k...
1005
  	default:
31847b67b   Jan Beulich   kconfig: allow us...
1006
1007
1008
  		val->s = strtoll(str, &tail, 0);
  		kind = k_signed;
  		break;
31847b67b   Jan Beulich   kconfig: allow us...
1009
1010
1011
1012
  	}
  	return !errno && !*tail && tail > str && isxdigit(tail[-1])
  	       ? kind : k_string;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1013
1014
1015
1016
  tristate expr_calc_value(struct expr *e)
  {
  	tristate val1, val2;
  	const char *str1, *str2;
31847b67b   Jan Beulich   kconfig: allow us...
1017
1018
1019
  	enum string_value_kind k1 = k_string, k2 = k_string;
  	union string_value lval = {}, rval = {};
  	int res;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
  
  	if (!e)
  		return yes;
  
  	switch (e->type) {
  	case E_SYMBOL:
  		sym_calc_value(e->left.sym);
  		return e->left.sym->curr.tri;
  	case E_AND:
  		val1 = expr_calc_value(e->left.expr);
  		val2 = expr_calc_value(e->right.expr);
d6ee35764   Sam Ravnborg   kconfig: rename E...
1031
  		return EXPR_AND(val1, val2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1032
1033
1034
  	case E_OR:
  		val1 = expr_calc_value(e->left.expr);
  		val2 = expr_calc_value(e->right.expr);
d6ee35764   Sam Ravnborg   kconfig: rename E...
1035
  		return EXPR_OR(val1, val2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1036
1037
  	case E_NOT:
  		val1 = expr_calc_value(e->left.expr);
d6ee35764   Sam Ravnborg   kconfig: rename E...
1038
  		return EXPR_NOT(val1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1039
  	case E_EQUAL:
31847b67b   Jan Beulich   kconfig: allow us...
1040
1041
1042
1043
  	case E_GEQ:
  	case E_GTH:
  	case E_LEQ:
  	case E_LTH:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1044
  	case E_UNEQUAL:
31847b67b   Jan Beulich   kconfig: allow us...
1045
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1046
1047
1048
1049
1050
  	default:
  		printf("expr_calc_value: %d?
  ", e->type);
  		return no;
  	}
31847b67b   Jan Beulich   kconfig: allow us...
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
  
  	sym_calc_value(e->left.sym);
  	sym_calc_value(e->right.sym);
  	str1 = sym_get_string_value(e->left.sym);
  	str2 = sym_get_string_value(e->right.sym);
  
  	if (e->left.sym->type != S_STRING || e->right.sym->type != S_STRING) {
  		k1 = expr_parse_string(str1, e->left.sym->type, &lval);
  		k2 = expr_parse_string(str2, e->right.sym->type, &rval);
  	}
  
  	if (k1 == k_string || k2 == k_string)
  		res = strcmp(str1, str2);
0cbe3ac43   Masahiro Yamada   kconfig: remove k...
1064
  	else if (k1 == k_unsigned || k2 == k_unsigned)
31847b67b   Jan Beulich   kconfig: allow us...
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
  		res = (lval.u > rval.u) - (lval.u < rval.u);
  	else /* if (k1 == k_signed && k2 == k_signed) */
  		res = (lval.s > rval.s) - (lval.s < rval.s);
  
  	switch(e->type) {
  	case E_EQUAL:
  		return res ? no : yes;
  	case E_GEQ:
  		return res >= 0 ? yes : no;
  	case E_GTH:
  		return res > 0 ? yes : no;
  	case E_LEQ:
  		return res <= 0 ? yes : no;
  	case E_LTH:
  		return res < 0 ? yes : no;
  	case E_UNEQUAL:
  		return res ? yes : no;
  	default:
  		printf("expr_calc_value: relation %d?
  ", e->type);
  		return no;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1087
  }
ad8d40cda   Michal Marek   kconfig: Remove u...
1088
  static int expr_compare_type(enum expr_type t1, enum expr_type t2)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1089
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1090
1091
1092
  	if (t1 == t2)
  		return 0;
  	switch (t1) {
31847b67b   Jan Beulich   kconfig: allow us...
1093
1094
1095
1096
1097
1098
  	case E_LEQ:
  	case E_LTH:
  	case E_GEQ:
  	case E_GTH:
  		if (t2 == E_EQUAL || t2 == E_UNEQUAL)
  			return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
  	case E_EQUAL:
  	case E_UNEQUAL:
  		if (t2 == E_NOT)
  			return 1;
  	case E_NOT:
  		if (t2 == E_AND)
  			return 1;
  	case E_AND:
  		if (t2 == E_OR)
  			return 1;
  	case E_OR:
7a9629233   Roman Zippel   kconfig: explicit...
1110
  		if (t2 == E_LIST)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1111
  			return 1;
7a9629233   Roman Zippel   kconfig: explicit...
1112
  	case E_LIST:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1113
1114
1115
1116
1117
1118
1119
  		if (t2 == 0)
  			return 1;
  	default:
  		return -1;
  	}
  	printf("[%dgt%d?]", t1, t2);
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1120
  }
9a47ceec5   Masahiro Yamada   kconfig: clean-up...
1121
1122
1123
  void expr_print(struct expr *e,
  		void (*fn)(void *, struct symbol *, const char *),
  		void *data, int prevtoken)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1124
1125
  {
  	if (!e) {
ab45d190f   Roman Zippel   kconfig: create l...
1126
  		fn(data, NULL, "y");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1127
1128
1129
1130
  		return;
  	}
  
  	if (expr_compare_type(prevtoken, e->type) > 0)
ab45d190f   Roman Zippel   kconfig: create l...
1131
  		fn(data, NULL, "(");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1132
1133
1134
  	switch (e->type) {
  	case E_SYMBOL:
  		if (e->left.sym->name)
ab45d190f   Roman Zippel   kconfig: create l...
1135
  			fn(data, e->left.sym, e->left.sym->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1136
  		else
ab45d190f   Roman Zippel   kconfig: create l...
1137
  			fn(data, NULL, "<choice>");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1138
1139
  		break;
  	case E_NOT:
ab45d190f   Roman Zippel   kconfig: create l...
1140
  		fn(data, NULL, "!");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1141
1142
1143
  		expr_print(e->left.expr, fn, data, E_NOT);
  		break;
  	case E_EQUAL:
f5eaa323e   Jan Beulich   kconfig: tristate...
1144
1145
1146
1147
  		if (e->left.sym->name)
  			fn(data, e->left.sym, e->left.sym->name);
  		else
  			fn(data, NULL, "<choice>");
ab45d190f   Roman Zippel   kconfig: create l...
1148
1149
  		fn(data, NULL, "=");
  		fn(data, e->right.sym, e->right.sym->name);
31847b67b   Jan Beulich   kconfig: allow us...
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
  		break;
  	case E_LEQ:
  	case E_LTH:
  		if (e->left.sym->name)
  			fn(data, e->left.sym, e->left.sym->name);
  		else
  			fn(data, NULL, "<choice>");
  		fn(data, NULL, e->type == E_LEQ ? "<=" : "<");
  		fn(data, e->right.sym, e->right.sym->name);
  		break;
  	case E_GEQ:
  	case E_GTH:
  		if (e->left.sym->name)
  			fn(data, e->left.sym, e->left.sym->name);
  		else
  			fn(data, NULL, "<choice>");
f6aad2615   Michal Sojka   kconfig: Fix copy...
1166
  		fn(data, NULL, e->type == E_GEQ ? ">=" : ">");
31847b67b   Jan Beulich   kconfig: allow us...
1167
  		fn(data, e->right.sym, e->right.sym->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1168
1169
  		break;
  	case E_UNEQUAL:
f5eaa323e   Jan Beulich   kconfig: tristate...
1170
1171
1172
1173
  		if (e->left.sym->name)
  			fn(data, e->left.sym, e->left.sym->name);
  		else
  			fn(data, NULL, "<choice>");
ab45d190f   Roman Zippel   kconfig: create l...
1174
1175
  		fn(data, NULL, "!=");
  		fn(data, e->right.sym, e->right.sym->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1176
1177
  		break;
  	case E_OR:
9a47ceec5   Masahiro Yamada   kconfig: clean-up...
1178
1179
1180
  		expr_print(e->left.expr, fn, data, E_OR);
  		fn(data, NULL, " || ");
  		expr_print(e->right.expr, fn, data, E_OR);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1181
1182
1183
  		break;
  	case E_AND:
  		expr_print(e->left.expr, fn, data, E_AND);
ab45d190f   Roman Zippel   kconfig: create l...
1184
  		fn(data, NULL, " && ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1185
1186
  		expr_print(e->right.expr, fn, data, E_AND);
  		break;
7a9629233   Roman Zippel   kconfig: explicit...
1187
  	case E_LIST:
ab45d190f   Roman Zippel   kconfig: create l...
1188
  		fn(data, e->right.sym, e->right.sym->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1189
  		if (e->left.expr) {
ab45d190f   Roman Zippel   kconfig: create l...
1190
  			fn(data, NULL, " ^ ");
7a9629233   Roman Zippel   kconfig: explicit...
1191
  			expr_print(e->left.expr, fn, data, E_LIST);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1192
1193
1194
  		}
  		break;
  	case E_RANGE:
ab45d190f   Roman Zippel   kconfig: create l...
1195
1196
1197
1198
1199
  		fn(data, NULL, "[");
  		fn(data, e->left.sym, e->left.sym->name);
  		fn(data, NULL, " ");
  		fn(data, e->right.sym, e->right.sym->name);
  		fn(data, NULL, "]");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1200
1201
1202
1203
1204
  		break;
  	default:
  	  {
  		char buf[32];
  		sprintf(buf, "<unknown type %d>", e->type);
ab45d190f   Roman Zippel   kconfig: create l...
1205
  		fn(data, NULL, buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1206
1207
1208
1209
  		break;
  	  }
  	}
  	if (expr_compare_type(prevtoken, e->type) > 0)
ab45d190f   Roman Zippel   kconfig: create l...
1210
  		fn(data, NULL, ")");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1211
  }
ab45d190f   Roman Zippel   kconfig: create l...
1212
  static void expr_print_file_helper(void *data, struct symbol *sym, const char *str)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1213
  {
bf5e327a3   Jean Sacren   kconfig: Fix warn...
1214
  	xfwrite(str, strlen(str), 1, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1215
1216
1217
1218
1219
1220
  }
  
  void expr_fprint(struct expr *e, FILE *out)
  {
  	expr_print(e, expr_print_file_helper, out, E_NONE);
  }
ab45d190f   Roman Zippel   kconfig: create l...
1221
  static void expr_print_gstr_helper(void *data, struct symbol *sym, const char *str)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1222
  {
da60fbbcb   Vadim Bendebury (вб)   menuconfig: wrap ...
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
  	struct gstr *gs = (struct gstr*)data;
  	const char *sym_str = NULL;
  
  	if (sym)
  		sym_str = sym_get_string_value(sym);
  
  	if (gs->max_width) {
  		unsigned extra_length = strlen(str);
  		const char *last_cr = strrchr(gs->s, '
  ');
  		unsigned last_line_length;
  
  		if (sym_str)
  			extra_length += 4 + strlen(sym_str);
  
  		if (!last_cr)
  			last_cr = gs->s;
  
  		last_line_length = strlen(gs->s) - (last_cr - gs->s);
  
  		if ((last_line_length + extra_length) > gs->max_width)
  			str_append(gs, "\\
  ");
  	}
  
  	str_append(gs, str);
70ed07471   Li Zefan   kconfig: print th...
1249
  	if (sym && sym->type != S_UNKNOWN)
da60fbbcb   Vadim Bendebury (вб)   menuconfig: wrap ...
1250
  		str_printf(gs, " [=%s]", sym_str);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1251
1252
1253
1254
1255
1256
  }
  
  void expr_gstr_print(struct expr *e, struct gstr *gs)
  {
  	expr_print(e, expr_print_gstr_helper, gs, E_NONE);
  }
1ccb27143   Petr Vorel   kconfig: make "Se...
1257
1258
1259
1260
1261
1262
  
  /*
   * Transform the top level "||" tokens into newlines and prepend each
   * line with a minus. This makes expressions much easier to read.
   * Suitable for reverse dependency expressions.
   */
9a47ceec5   Masahiro Yamada   kconfig: clean-up...
1263
1264
  static void expr_print_revdep(struct expr *e,
  			      void (*fn)(void *, struct symbol *, const char *),
d9119b592   Eugeniu Rosca   kconfig: Print re...
1265
  			      void *data, tristate pr_type, const char **title)
9a47ceec5   Masahiro Yamada   kconfig: clean-up...
1266
1267
  {
  	if (e->type == E_OR) {
d9119b592   Eugeniu Rosca   kconfig: Print re...
1268
1269
1270
1271
1272
1273
1274
  		expr_print_revdep(e->left.expr, fn, data, pr_type, title);
  		expr_print_revdep(e->right.expr, fn, data, pr_type, title);
  	} else if (expr_calc_value(e) == pr_type) {
  		if (*title) {
  			fn(data, NULL, *title);
  			*title = NULL;
  		}
9a47ceec5   Masahiro Yamada   kconfig: clean-up...
1275
1276
1277
1278
1279
1280
  		fn(data, NULL, "  - ");
  		expr_print(e, fn, data, E_NONE);
  		fn(data, NULL, "
  ");
  	}
  }
d9119b592   Eugeniu Rosca   kconfig: Print re...
1281
1282
  void expr_gstr_print_revdep(struct expr *e, struct gstr *gs,
  			    tristate pr_type, const char *title)
1ccb27143   Petr Vorel   kconfig: make "Se...
1283
  {
d9119b592   Eugeniu Rosca   kconfig: Print re...
1284
  	expr_print_revdep(e, expr_print_gstr_helper, gs, pr_type, &title);
1ccb27143   Petr Vorel   kconfig: make "Se...
1285
  }