Skip to contentSkip to conversation

kconfig: Add support for conflict resolution

PATCH Superseded Ole Schuerks 3 participants 25 messages Jul 10, 2024 – Sep 20, 2024 Page 2 of 2

Subsystems (2)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#16 lore raw

Re: [PATCH v4 07/12] kconfig: Add files for handling expressions

In reply to Ole Schuerks
Quoted 61 lines · Ole Schuerks

To translate the Kconfig-model into propositional logic and resolve conflicts, we need to handle propostional formulas. These files contain many functions and macros to deal with propositional formulas.

---
 scripts/kconfig/cf_expr.c | 2594 +++++++++++++++++++++++++++++++++++++
 scripts/kconfig/cf_expr.h |  296 +++++
 2 files changed, 2890 insertions(+)
 create mode 100644 scripts/kconfig/cf_expr.c
 create mode 100644 scripts/kconfig/cf_expr.h
scripts/kconfig/cf_expr.c new file +32 -0
new file mode 100644
index 000000000000..f015f91ec8c6
@@ -0,0 +1,2594 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023 Patrick Franz <[email protected]>
+ */
+
+#define _GNU_SOURCE
+#include <assert.h>
+#include <locale.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "cf_expr.h"
+#include "cf_defs.h"
+#include "cf_utils.h"
+
+static void create_fexpr_bool(struct symbol *sym, struct cfdata *data);
+static void create_fexpr_nonbool(struct symbol *sym, struct cfdata *data);
+static void create_fexpr_unknown(struct symbol *sym, struct cfdata *data);
+static void create_fexpr_choice(struct symbol *sym, struct cfdata *data);
+
+static void pexpr_print_util(struct pexpr *e, int prevtoken);
+static void pexpr_shallow_copy(struct pexpr *dest, struct pexpr *org, unsigned int ref_count);
+
+static struct pexpr *pexpr_move_wrapper(
+       struct pexpr *a, struct pexpr *b, struct cfdata *data,
+       enum pexpr_move move,
+       struct pexpr *(*func)(struct pexpr *, struct pexpr *, struct cfdata *));

scripts/kconfig/cf_expr.c line 42

Quoted 10 lines
scripts/kconfig/cf_expr.c new file +10 -0
+
+static int trans_count;
+
+
+/*
+ *  create a fexpr
+ */
+struct fexpr *fexpr_create(int satval, enum fexpr_type type, char *name)
+{
+       struct fexpr *e = xcalloc(1, sizeof(*e));

Why xcalloc() instead of xmalloc()?

(same for all other places)

Quoted 1,217 lines
scripts/kconfig/cf_expr.c new file +1217 -0
+
+       e->satval = satval;
+       e->type = type;
+       e->name = str_new();
+       e->assumption = false;
+       str_append(&e->name, name);
+
+       return e;
+}
+
+/*
+ * create the fexpr for a symbol
+ */
+void sym_create_fexpr(struct symbol *sym, struct cfdata *data)
+{
+       if (sym_is_choice(sym))
+               create_fexpr_choice(sym, data);
+       else if (sym_is_boolean(sym))
+               create_fexpr_bool(sym, data);
+       else if (sym_is_nonboolean(sym))
+               create_fexpr_nonbool(sym, data);
+       else
+               create_fexpr_unknown(sym, data);
+}
+
+/*
+ * create the fexpr for symbols with reverse dependencies
+ */
+static void create_fexpr_selected(struct symbol *sym, struct cfdata *data)
+{
+       struct fexpr *fexpr_sel_y;
+       struct fexpr *fexpr_sel_m;
+
+       /* fexpr_sel_y */
+       fexpr_sel_y = fexpr_create(data->sat_variable_nr++, FE_SELECT, sym->name);
+       str_append(&fexpr_sel_y->name, "_sel_y");
+       fexpr_sel_y->sym = sym;
+       fexpr_add_to_satmap(fexpr_sel_y, data);
+
+       sym->fexpr_sel_y = fexpr_sel_y;
+
+       /* fexpr_sel_m */
+       if (sym->type == S_BOOLEAN)
+               return;
+
+       fexpr_sel_m = fexpr_create(data->sat_variable_nr++, FE_SELECT, sym->name);
+       str_append(&fexpr_sel_m->name, "_sel_m");
+       fexpr_sel_m->sym = sym;
+       fexpr_add_to_satmap(fexpr_sel_m, data);
+
+       sym->fexpr_sel_m = fexpr_sel_m;
+}
+
+/*
+ * create the fexpr for a boolean/tristate symbol
+ */
+static void create_fexpr_bool(struct symbol *sym, struct cfdata *data)
+{
+       struct fexpr *fexpr_y;
+       struct fexpr *fexpr_m;
+
+       fexpr_y = fexpr_create(data->sat_variable_nr++, FE_SYMBOL, sym->name);
+       fexpr_y->sym = sym;
+       fexpr_y->tri = yes;
+       fexpr_add_to_satmap(fexpr_y, data);
+
+       sym->fexpr_y = fexpr_y;
+
+
+       if (sym->type == S_TRISTATE) {
+               fexpr_m = fexpr_create(data->sat_variable_nr++, FE_SYMBOL, sym->name);
+               str_append(&fexpr_m->name, "_MODULE");
+               fexpr_m->sym = sym;
+               fexpr_m->tri = mod;
+               fexpr_add_to_satmap(fexpr_m, data);
+       } else {
+               fexpr_m = data->constants->const_false;
+       }
+
+       sym->fexpr_m = fexpr_m;
+
+       if (sym->rev_dep.expr)
+               create_fexpr_selected(sym, data);
+}
+
+/*
+ * create the fexpr for a non-boolean symbol
+ */
+static void create_fexpr_nonbool(struct symbol *sym, struct cfdata *data)
+{
+       /* default values */
+       char int_values[][2] = {"n", "0", "1"};
+       char hex_values[][4] = {"n", "0x0", "0x1"};
+       char string_values[][9] = {"n", "", "nonempty"};
+
+       sym->fexpr_y = data->constants->const_false;
+       sym->fexpr_m = data->constants->const_false;
+       sym->nb_vals = fexpr_list_init();
+
+       for (int i = 0; i < 3; i++) {
+               struct fexpr *e = fexpr_create(data->sat_variable_nr++, FE_NONBOOL, sym->name);
+
+               e->sym = sym;
+               str_append(&e->name, "=");
+               e->nb_val = str_new();
+
+               switch (sym->type) {
+               case S_INT:
+                       str_append(&e->name, int_values[i]);
+                       str_append(&e->nb_val, int_values[i]);
+                       break;
+               case S_HEX:
+                       str_append(&e->name, hex_values[i]);
+                       str_append(&e->nb_val, hex_values[i]);
+                       break;
+               case S_STRING:
+                       str_append(&e->name, string_values[i]);
+                       str_append(&e->nb_val, string_values[i]);
+                       break;
+               default:
+                       break;
+               }
+
+               fexpr_list_add(sym->nb_vals, e);
+               fexpr_add_to_satmap(e, data);
+       }
+}
+
+/*
+ * set fexpr_y and fexpr_m simply to False
+ */
+static void create_fexpr_unknown(struct symbol *sym, struct cfdata *data)
+{
+       sym->fexpr_y = data->constants->const_false;
+       sym->fexpr_m = data->constants->const_false;
+}
+
+/*
+ * create the fexpr for a choice symbol
+ */
+static void create_fexpr_choice(struct symbol *sym, struct cfdata *data)
+{
+       struct property *prompt;
+       char *name, *write, *read;
+       struct fexpr *fexpr_y;
+       struct fexpr *fexpr_m;
+
+       if (!sym_is_boolean(sym))
+               return;
+
+       prompt = sym_get_prompt(sym);
+       if (prompt == NULL) {
+               perror("Choice symbol should have a prompt.");
+               return;
+       }
+
+       name = strdup(prompt->text);
+
+       /* remove spaces */
+       write = name;
+       read = name;
+       do {
+               if (*read != ' ')
+                       *write++ = *read;
+       } while (*read++);
+
+       fexpr_y = fexpr_create(data->sat_variable_nr++, FE_CHOICE, "Choice_");
+       str_append(&fexpr_y->name, name);
+       fexpr_y->sym = sym;
+       fexpr_y->tri = yes;
+       fexpr_add_to_satmap(fexpr_y, data);
+
+       sym->fexpr_y = fexpr_y;
+
+       if (sym->type == S_TRISTATE) {
+               fexpr_m = fexpr_create(data->sat_variable_nr++, FE_CHOICE, "Choice_");
+               str_append(&fexpr_m->name, name);
+               str_append(&fexpr_m->name, "_MODULE");
+               fexpr_m->sym = sym;
+               fexpr_m->tri = mod;
+               fexpr_add_to_satmap(fexpr_m, data);
+       } else {
+               fexpr_m = data->constants->const_false;
+       }
+       sym->fexpr_m = fexpr_m;
+       free(name);
+}
+
+/*
+ * evaluate an unequality between a non-Boolean symbol and a constant
+ */
+static struct pexpr *expr_eval_unequal_nonbool_const(struct symbol *sym, struct symbol *compval,
+                                                    enum expr_type type, struct cfdata *data)
+{
+       int base;
+       struct pexpr *c;
+       long val;
+       struct fexpr_node *node;
+       struct fexpr *fe;
+
+       if (!sym || !compval)
+               return pexf(data->constants->const_false);
+
+       base = 0;
+       switch (sym->type) {
+       case S_INT:
+               base = 10;
+               break;
+       case S_HEX:
+               base = 16;
+               break;
+       default:
+               break;
+       }
+
+       c = pexf(data->constants->const_false);
+       val = strtol(compval->name, NULL, base);
+       for (node = sym->nb_vals->head->next; node != NULL; node = node->next) {
+               long symval;
+
+               fe = node->elem;
+               symval = strtol(str_get(&fe->nb_val), NULL, base);
+
+               switch (type) {
+               case E_LTH:
+                       if (symval < val)
+                               c = pexpr_or(c, pexf(fe), data, PEXPR_ARGX);
+                       break;
+               case E_LEQ:
+                       if (symval <= val)
+                               c = pexpr_or(c, pexf(fe), data, PEXPR_ARGX);
+                       break;
+               case E_GTH:
+                       if (symval > val)
+                               c = pexpr_or(c, pexf(fe), data, PEXPR_ARGX);
+                       break;
+               case E_GEQ:
+                       if (symval >= val)
+                               c = pexpr_or(c, pexf(fe), data, PEXPR_ARGX);
+                       break;
+               default:
+                       perror("Illegal unequal.");
+               }
+       }
+
+       return c;
+}
+
+/*
+ * evaluate an unequality between 2 Boolean symbols
+ */
+static struct pexpr *expr_eval_unequal_bool(struct symbol *left, struct symbol *right,
+                                           enum expr_type type, struct cfdata *data)
+{
+       struct pexpr *c;
+
+       if (!left || !right)
+               return pexf(data->constants->const_false);
+
+       if (!sym_is_boolean(left) || !sym_is_boolean(right)) {
+               perror("Comparing 2 symbols that should be boolean.");
+               return pexf(data->constants->const_false);
+       }
+
+       switch (type) {
+       case E_LTH:
+               c = pexpr_and(pexpr_not(sym_get_fexpr_both(left, data), data),
+                             sym_get_fexpr_both(right, data), data,
+                             PEXPR_ARGX);
+               if (left->type == S_TRISTATE)
+                       c = pexpr_or(c,
+                                    pexpr_and(pexf(left->fexpr_m),
+                                              pexf(right->fexpr_y), data,
+                                              PEXPR_ARGX),
+                                    data, PEXPR_ARGX);
+               break;
+       case E_LEQ:
+               c = pexpr_and(pexf(left->fexpr_y), pexf(right->fexpr_y), data,
+                             PEXPR_ARGX);
+               if (left->type == S_TRISTATE)
+                       c = pexpr_or(c,
+                                    pexpr_and(pexf(left->fexpr_m),
+                                              sym_get_fexpr_both(right, data),
+                                              data, PEXPR_ARGX),
+                                    data, PEXPR_ARGX);
+               c = pexpr_or(c, pexpr_not(sym_get_fexpr_both(left, data), data),
+                            data, PEXPR_ARGX);
+               break;
+       case E_GTH:
+               c = pexpr_and(sym_get_fexpr_both(left, data),
+                             pexpr_not(sym_get_fexpr_both(right, data), data),
+                             data, PEXPR_ARGX);
+               if (right->type == S_TRISTATE)
+                       c = pexpr_or(c,
+                                    pexpr_and(pexf(left->fexpr_y),
+                                              pexf(right->fexpr_m), data,
+                                              PEXPR_ARGX),
+                                    data, PEXPR_ARGX);
+               break;
+       case E_GEQ:
+               c = pexpr_and(pexf(left->fexpr_y), pexf(right->fexpr_y), data,
+                             PEXPR_ARGX);
+               if (right->type == S_TRISTATE)
+                       c = pexpr_or(c,
+                                    pexpr_and(sym_get_fexpr_both(left, data),
+                                              pexf(right->fexpr_m), data,
+                                              PEXPR_ARGX),
+                                    data, PEXPR_ARGX);
+               c = pexpr_or(c,
+                            pexpr_not(sym_get_fexpr_both(right, data), data),
+                            data, PEXPR_ARGX);
+               break;
+       default:
+               fprintf(stderr, "Wrong type - %s", __func__);
+               c = pexf(data->constants->const_false);
+       }
+
+       return c;
+}
+/*
+ * calculate, when expr will evaluate to yes or mod
+ */
+struct pexpr *expr_calculate_pexpr_both(struct expr *e, struct cfdata *data)
+{
+       if (!e)
+               return pexf(data->constants->const_false);
+
+       if (!expr_can_evaluate_to_mod(e))
+               return expr_calculate_pexpr_y(e, data);
+
+       switch (e->type) {
+       case E_SYMBOL:
+               return pexpr_or(expr_calculate_pexpr_m(e, data), expr_calculate_pexpr_y(e, data),
+                               data, PEXPR_ARGX);
+       case E_AND:
+               return expr_calculate_pexpr_both_and(e->left.expr, e->right.expr, data);
+       case E_OR:
+               return expr_calculate_pexpr_both_or(e->left.expr, e->right.expr, data);
+       case E_NOT:
+               return pexpr_or(expr_calculate_pexpr_m(e, data), expr_calculate_pexpr_y(e, data),
+                               data, PEXPR_ARGX);
+       case E_EQUAL:
+               return expr_calculate_pexpr_y_equals(e, data);
+       case E_UNEQUAL:
+               return expr_calculate_pexpr_y_unequals(e, data);
+       case E_LTH:
+       case E_LEQ:
+       case E_GTH:
+       case E_GEQ:
+               return expr_calculate_pexpr_y_comp(e, data);
+       default:
+               // TODO
+               fprintf(stderr, "Unhandled type - %s", __func__);
+               return NULL;
+       }
+}
+
+/*
+ * calculate, when expr will evaluate to yes
+ */
+struct pexpr *expr_calculate_pexpr_y(struct expr *e, struct cfdata *data)
+{
+       if (!e)
+               return NULL;
+
+       switch (e->type) {
+       case E_SYMBOL:
+               return pexf(e->left.sym->fexpr_y);
+       case E_AND:
+               return expr_calculate_pexpr_y_and(e->left.expr, e->right.expr, data);
+       case E_OR:
+               return expr_calculate_pexpr_y_or(e->left.expr, e->right.expr, data);
+       case E_NOT:
+               return expr_calculate_pexpr_y_not(e->left.expr, data);
+       case E_EQUAL:
+               return expr_calculate_pexpr_y_equals(e, data);
+       case E_UNEQUAL:
+               return expr_calculate_pexpr_y_unequals(e, data);
+       case E_LTH:
+       case E_LEQ:
+       case E_GTH:
+       case E_GEQ:
+               return expr_calculate_pexpr_y_comp(e, data);
+       default:
+               fprintf(stderr, "Unhandled type - %s", __func__);
+               return NULL;
+       }
+}
+
+/*
+ * calculate, when expr will evaluate to mod
+ */
+struct pexpr *expr_calculate_pexpr_m(struct expr *e, struct cfdata *data)
+{
+       if (!e)
+               return NULL;
+
+       if (!expr_can_evaluate_to_mod(e))
+               return pexf(data->constants->const_false);
+
+       switch (e->type) {
+       case E_SYMBOL:
+               return pexf(e->left.sym->fexpr_m);
+       case E_AND:
+               return expr_calculate_pexpr_m_and(e->left.expr, e->right.expr, data);
+       case E_OR:
+               return expr_calculate_pexpr_m_or(e->left.expr, e->right.expr, data);
+       case E_NOT:
+               return expr_calculate_pexpr_m_not(e->left.expr, data);
+       default:
+               perror("Trying to evaluate to mod.");
+               return NULL;
+       }
+}
+
+/*
+ * calculate, when expr of type AND will evaluate to yes
+ * A && B
+ */
+struct pexpr *expr_calculate_pexpr_y_and(struct expr *a, struct expr *b, struct cfdata *data)
+{
+       return pexpr_and(expr_calculate_pexpr_y(a, data),
+                             expr_calculate_pexpr_y(b, data), data,
+                             PEXPR_ARGX);
+}
+
+/*
+ * calculate, when expr of type AND will evaluate to mod
+ * (A || A_m) && (B || B_m) && !(A && B)
+ */
+struct pexpr *expr_calculate_pexpr_m_and(struct expr *a, struct expr *b,
+                                        struct cfdata *data)
+{
+       struct pexpr *topright =
+               pexpr_not(pexpr_and(expr_calculate_pexpr_y(a, data),
+                                             expr_calculate_pexpr_y(b, data),
+                                             data, PEXPR_ARGX),
+                              data);
+       struct pexpr *ll_left = pexpr_or(expr_calculate_pexpr_y(a, data),
+                                        expr_calculate_pexpr_m(a, data), data,
+                                        PEXPR_ARGX);
+       struct pexpr *ll_right = pexpr_or(expr_calculate_pexpr_y(b, data),
+                                         expr_calculate_pexpr_m(b, data), data,
+                                         PEXPR_ARGX);
+       struct pexpr *topleft = pexpr_and(ll_left, ll_right, data, PEXPR_ARGX);
+
+       return pexpr_and(topleft, topright, data, PEXPR_ARGX);
+}
+
+/*
+ * calculate, when expr of type AND will evaluate to mod or yes
+ * (A || A_m) && (B || B_m)
+ */
+struct pexpr *expr_calculate_pexpr_both_and(struct expr *a, struct expr *b,
+                                           struct cfdata *data)
+{
+       struct pexpr *left = pexpr_or(expr_calculate_pexpr_y(a, data),
+                                     expr_calculate_pexpr_m(a, data), data,
+                                     PEXPR_ARGX);
+       struct pexpr *right = pexpr_or(expr_calculate_pexpr_y(b, data),
+                                      expr_calculate_pexpr_m(b, data), data,
+                                      PEXPR_ARGX);
+
+       return pexpr_and(left, right, data, PEXPR_ARGX);
+}
+
+/*
+ * calculate, when expr of type OR will evaluate to yes
+ * A || B
+ */
+struct pexpr *expr_calculate_pexpr_y_or(struct expr *a, struct expr *b,
+                                       struct cfdata *data)
+{
+       return pexpr_or(expr_calculate_pexpr_y(a, data),
+                            expr_calculate_pexpr_y(b, data), data, PEXPR_ARGX);
+}
+
+/*
+ * calculate, when expr of type OR will evaluate to mod
+ * (A_m || B_m) && !A && !B
+ */
+struct pexpr *expr_calculate_pexpr_m_or(struct expr *a, struct expr *b,
+                                       struct cfdata *data)
+{
+       struct pexpr *topright =
+               pexpr_not(expr_calculate_pexpr_y(b, data), data);
+       struct pexpr *lowerleft = pexpr_or(expr_calculate_pexpr_m(a, data),
+                                               expr_calculate_pexpr_m(b, data),
+                                               data, PEXPR_ARGX);
+       struct pexpr *topleft = pexpr_and(
+               lowerleft,
+               pexpr_not(expr_calculate_pexpr_y(a, data), data), data,
+               PEXPR_ARGX);
+
+       return pexpr_and(topleft, topright, data, PEXPR_ARGX);
+}
+
+/*
+ * calculate, when expr of type OR will evaluate to mod or yes
+ * (A_m || A || B_m || B)
+ */
+struct pexpr *expr_calculate_pexpr_both_or(struct expr *a, struct expr *b,
+                                          struct cfdata *data)
+{
+       struct pexpr *left = pexpr_or(expr_calculate_pexpr_y(a, data),
+                                     expr_calculate_pexpr_m(a, data), data,
+                                     PEXPR_ARGX);
+       struct pexpr *right = pexpr_or(expr_calculate_pexpr_y(b, data),
+                                      expr_calculate_pexpr_m(b, data), data,
+                                      PEXPR_ARGX);
+
+       return pexpr_or(left, right, data, PEXPR_ARGX);
+}
+
+/*
+ * calculate, when expr of type NOT will evaluate to yes
+ * !(A || A_m)
+ */
+struct pexpr *expr_calculate_pexpr_y_not(struct expr *e, struct cfdata *data)
+{
+       return pexpr_not(pexpr_or(expr_calculate_pexpr_y(e, data),
+                                           expr_calculate_pexpr_m(e, data),
+                                           data, PEXPR_ARGX),
+                             data);
+}
+
+/*
+ * calculate, when expr of type NOT will evaluate to mod
+ * A_m
+ */
+struct pexpr *expr_calculate_pexpr_m_not(struct expr *e, struct cfdata *data)
+{
+       return expr_calculate_pexpr_m(e, data);
+}
+
+static struct pexpr *equiv_pexpr_share(struct pexpr *a, struct pexpr *b,
+                                      struct cfdata *data)
+{
+       struct pexpr *yes = pexpr_and_share(a, b, data);
+       struct pexpr *not = pexpr_and(pexpr_not_share(a, data),
+                                          pexpr_not_share(b, data), data,
+                                          PEXPR_ARGX);
+
+       return pexpr_or(yes, not, data, PEXPR_ARGX);
+}
+
+static struct pexpr *equiv_pexpr_move(struct pexpr *a, struct pexpr *b,
+                                      struct cfdata *data,
+                                      enum pexpr_move move)
+{
+       return pexpr_move_wrapper(a, b, data, move, equiv_pexpr_share);
+}
+
+/*
+ * create the fexpr of a non-boolean symbol for a specific value
+ */
+struct fexpr *sym_create_nonbool_fexpr(struct symbol *sym, char *value,
+                                      struct cfdata *data)
+{
+       struct fexpr *e;
+       char *s;
+
+       if (!strcmp(value, "")) {
+               if (sym->type == S_STRING)
+                       return sym->nb_vals->head->next->elem;
+               else
+                       return sym->nb_vals->head->elem;
+       }
+
+       e = sym_get_nonbool_fexpr(sym, value);
+
+       /* fexpr already exists */
+       if (e != NULL)
+               return e;
+
+       s = value;
+       if (sym->type == S_INT && !string_is_number(value)) {
+               struct symbol *tmp = sym_find(value);
+
+               if (tmp != NULL)
+                       s = (char *) tmp->curr.val;
+       } else if (sym->type == S_HEX && !string_is_hex(value)) {
+               struct symbol *tmp = sym_find(value);
+
+               if (tmp != NULL)
+                       s = (char *) tmp->curr.val;
+       } else if (sym->type == S_STRING) {
+               struct symbol *tmp = sym_find(value);
+
+               if (tmp != NULL)
+                       s = (char *) tmp->curr.val;
+       }
+
+       if (!strcmp(s, "")) {
+               if (sym->type == S_STRING)
+                       return sym->nb_vals->head->next->elem;
+               else
+                       return sym->nb_vals->head->elem;
+       }
+
+       e = sym_get_nonbool_fexpr(sym, s);
+       if (e != NULL)
+               return e;
+
+       e = fexpr_create(data->sat_variable_nr++, FE_NONBOOL, sym->name);
+       e->sym = sym;
+       str_append(&e->name, "=");
+       str_append(&e->name, s);
+       e->nb_val = str_new();
+       str_append(&e->nb_val, s);
+
+       fexpr_list_add(sym->nb_vals, e);
+       fexpr_add_to_satmap(e, data);
+
+       return e;
+}
+
+/*
+ * return the fexpr of a non-boolean symbol for a specific value, NULL if
+ * non-existent
+ */
+struct fexpr *sym_get_nonbool_fexpr(struct symbol *sym, char *value)
+{
+       struct fexpr_node *e;
+
+       fexpr_list_for_each(e, sym->nb_vals) {
+               if (strcmp(str_get(&e->elem->nb_val), value) == 0)
+                       return e->elem;
+       }
+
+       return NULL;
+}
+
+/*
+ * return the fexpr of a non-boolean symbol for a specific value, if it exists
+ * otherwise create it
+ */
+struct fexpr *sym_get_or_create_nonbool_fexpr(struct symbol *sym, char *value,
+                                             struct cfdata *data)
+{
+       struct fexpr *e = sym_get_nonbool_fexpr(sym, value);
+
+       if (e != NULL)
+               return e;
+       else
+               return sym_create_nonbool_fexpr(sym, value, data);
+}
+
+/*
+ * calculate, when expr of type EQUAL will evaluate to yes
+ * Side effect: May create certain values in e->{left,right}.sym.nb_vals
+ */
+struct pexpr *expr_calculate_pexpr_y_equals(struct expr *e, struct cfdata *data)
+{
+       /* comparing 2 tristate constants */
+       if (sym_is_tristate_constant(e->left.sym) &&
+           sym_is_tristate_constant(e->right.sym))
+               return e->left.sym == e->right.sym ?
+                              pexf(data->constants->const_true) :
+                              pexf(data->constants->const_false);
+
+       /* comparing 2 nonboolean constants */
+       if (sym_is_nonbool_constant(e->left.sym) &&
+           sym_is_nonbool_constant(e->right.sym))
+               return strcmp(e->left.sym->name, e->right.sym->name) == 0 ?
+                              pexf(data->constants->const_true) :
+                              pexf(data->constants->const_false);
+
+       /* comparing 2 boolean/tristate incl. yes/mod/no constants */
+       if (sym_is_bool_or_triconst(e->left.sym) &&
+           sym_is_bool_or_triconst(e->right.sym)) {
+               struct pexpr *yes = equiv_pexpr_move(
+                       pexf(e->left.sym->fexpr_y), pexf(e->right.sym->fexpr_y),
+                       data, PEXPR_ARGX);
+               struct pexpr *mod = equiv_pexpr_move(
+                       pexf(e->left.sym->fexpr_m), pexf(e->right.sym->fexpr_m),
+                       data, PEXPR_ARGX);
+
+               return pexpr_and(yes, mod, data, PEXPR_ARGX);
+       }
+
+       /* comparing nonboolean with a constant */
+       if (sym_is_nonboolean(e->left.sym) &&
+           sym_is_nonbool_constant(e->right.sym))
+               return pexf(sym_get_or_create_nonbool_fexpr(
+                       e->left.sym, e->right.sym->name, data));
+
+       if (sym_is_nonbool_constant(e->left.sym) &&
+           sym_is_nonboolean(e->right.sym))
+               return pexf(sym_get_or_create_nonbool_fexpr(
+                       e->right.sym, e->left.sym->name, data));
+
+       /* comparing nonboolean with tristate constant, will never be true */
+       if (sym_is_nonboolean(e->left.sym) &&
+           sym_is_tristate_constant(e->right.sym))
+               return pexf(data->constants->const_false);
+       if (sym_is_tristate_constant(e->left.sym) &&
+           sym_is_nonboolean(e->right.sym))
+               return pexf(data->constants->const_false);
+
+       /* comparing 2 nonboolean symbols */
+       if (sym_is_nonboolean(e->left.sym) && sym_is_nonboolean(e->right.sym)) {
+               struct pexpr *c = pexf(data->constants->const_false);
+               struct fexpr *e1, *e2;
+
+               for (struct fexpr_node *node1 =
+                            e->left.sym->nb_vals->head->next;
+                    node1 != NULL; node1 = node1->next) {
+                       e1 = node1->elem;
+                       for (struct fexpr_node *node2 =
+                                    e->right.sym->nb_vals->head->next;
+                            node2 != NULL; node2 = node2->next) {
+                               e2 = node2->elem;
+                               if (!strcmp(str_get(&e1->nb_val),
+                                           str_get(&e2->nb_val))) {
+                                       c = pexpr_or(
+                                               c,
+                                               pexpr_and(pexf(e1),
+                                                              pexf(e2), data,
+                                                              PEXPR_ARGX),
+                                               data, PEXPR_ARGX);
+                                       break;
+                               }
+                       }
+               }
+               return c;
+       }
+
+       /* comparing boolean item with nonboolean constant, will never be true */
+       if (sym_is_tristate_constant(e->left.sym) &&
+           sym_is_nonbool_constant(e->right.sym))
+               return pexf(data->constants->const_false);
+       if (sym_is_nonbool_constant(e->left.sym) &&
+           sym_is_tristate_constant(e->right.sym))
+               return pexf(data->constants->const_false);
+
+       /* comparing symbol of type unknown with tristate constant */
+       if (e->left.sym->type == S_UNKNOWN &&
+           sym_is_tristate_constant(e->right.sym))
+               return pexf(data->constants->const_false);
+       if (sym_is_tristate_constant(e->left.sym) &&
+           e->right.sym->type == S_UNKNOWN)
+               return pexf(data->constants->const_false);
+
+       /* any other comparison is not supported and should not be executed */
+       fprintf(stderr, "Unsupported equality.");
+       print_expr(":", e, 0);
+
+       return pexf(data->constants->const_false);
+}
+
+/*
+ * transform an UNEQUAL into a Not(EQUAL)
+ */
+struct pexpr *expr_calculate_pexpr_y_unequals(struct expr *e, struct cfdata *data)
+{
+       return pexpr_not(expr_calculate_pexpr_y_equals(e, data), data);
+}
+
+struct pexpr *expr_calculate_pexpr_y_comp(struct expr *e, struct cfdata *data)
+{
+       if (!e)
+               return NULL;
+
+       switch (e->type) {
+       case E_LTH:
+       case E_LEQ:
+       case E_GTH:
+       case E_GEQ:
+               /* compare non-Boolean symbol with constant */
+               if (sym_is_nonboolean(e->left.sym) &&
+                       e->right.sym->type == S_UNKNOWN &&
+                       string_is_number(e->right.sym->name)
+               ) {
+                       return expr_eval_unequal_nonbool_const(e->left.sym, e->right.sym, e->type,
+                                                              data);
+               }
+               if (sym_is_nonboolean(e->right.sym) &&
+                       e->left.sym->type == S_UNKNOWN &&
+                       string_is_number(e->left.sym->name)
+               ) {
+                       return expr_eval_unequal_nonbool_const(e->right.sym, e->left.sym, e->type,
+                                                              data);
+               }
+
+               /* compare 2 Boolean symbols */
+               if (sym_is_boolean(e->left.sym) && sym_is_boolean(e->right.sym))
+                       return expr_eval_unequal_bool(e->left.sym, e->right.sym, e->type, data);
+
+               return pexf(data->constants->const_false);
+       default:
+               fprintf(stderr, "Unhandled type - %s", __func__);
+               return NULL;
+       }
+}
+
+static struct pexpr *pexpr_move_wrapper(
+       struct pexpr *a, struct pexpr *b, struct cfdata *data,
+       enum pexpr_move move,
+       struct pexpr *(*func)(struct pexpr *, struct pexpr *, struct cfdata *))
+{
+       struct pexpr *retval = func(a, b, data);
+
+       switch (move) {
+       case PEXPR_ARG1:
+               pexpr_put(a);
+               break;
+       case PEXPR_ARG2:
+               pexpr_put(b);
+               break;
+       case PEXPR_ARGX:
+               pexpr_put(a);
+               pexpr_put(b);
+               break;
+       default:
+               fprintf(stderr, "%s: invalid value for @move - %d\n", __func__,
+                       move);
+       }
+       return retval;
+}
+
+struct pexpr *pexpr_and(struct pexpr *a, struct pexpr *b, struct cfdata *data, enum pexpr_move move)
+{
+       return pexpr_move_wrapper(a, b, data, move, pexpr_and_share);
+}
+
+/*
+ * macro to create a pexpr of type AND
+ */
+struct pexpr *pexpr_and_share(struct pexpr *a, struct pexpr *b, struct cfdata *data)
+{
+       struct pexpr *e;
+
+       /* A && A -> A */
+       if (a == b || pexpr_eq(a, b, data)) {
+               pexpr_get(a);
+               return a;
+       }
+
+       /* simplifications:
+        * expr && False -> False
+        * expr && True  -> expr
+        */
+       if ((a->type == PE_SYMBOL &&
+            a->left.fexpr == data->constants->const_false) ||
+           (b->type == PE_SYMBOL &&
+            b->left.fexpr == data->constants->const_true)) {
+               pexpr_get(a);
+               return a;
+       }
+
+       if ((b->type == PE_SYMBOL &&
+            b->left.fexpr == data->constants->const_false) ||
+           (a->type == PE_SYMBOL &&
+            a->left.fexpr == data->constants->const_true)) {
+               pexpr_get(b);
+               return b;
+       }
+
+       /* (A && B) && C -> A && B if B == C */
+       if (a->type == PE_AND && pexpr_eq(a->right.pexpr, b, data)) {
+               pexpr_get(a);
+               return a;
+       }
+
+       /* A && (B && C) -> B && C if A == B */
+       if (b->type == PE_AND && pexpr_eq(a, b->left.pexpr, data)) {
+               pexpr_get(b);
+               return b;
+       }
+
+       if (a->type == PE_OR && b->type == PE_OR) {
+               e = NULL;
+               /* (A || B) && (C || D) -> A || (B && D) if A == C */
+               if (pexpr_eq(a->left.pexpr, b->left.pexpr, data)) {
+                       e = pexpr_or(a->left.pexpr,
+                                         pexpr_and_share(a->right.pexpr,
+                                                         b->right.pexpr, data),
+                                         data, PEXPR_ARG2);
+               }
+               /* (A || B) && (C || D) -> B || (A && C) if B == D */
+               else if (pexpr_eq(a->right.pexpr, b->right.pexpr, data)) {
+                       e = pexpr_or(a->right.pexpr,
+                                         pexpr_and_share(a->left.pexpr,
+                                                         b->left.pexpr, data),
+                                         data, PEXPR_ARG2);
+               }
+               /* (A || B) && (C || D) -> A || (B && C) if A == D */
+               else if (pexpr_eq(a->left.pexpr, b->right.pexpr, data)) {
+                       e = pexpr_or(a->left.pexpr,
+                                         pexpr_and_share(a->right.pexpr,
+                                                         b->left.pexpr, data),
+                                         data, PEXPR_ARG2);
+               }
+               /* (A || B) && (C || D) -> B || (A && D) if B == C */
+               else if (pexpr_eq(a->right.pexpr, b->left.pexpr, data)) {
+                       e = pexpr_or(a->right.pexpr,
+                                         pexpr_and_share(a->left.pexpr,
+                                                         b->right.pexpr, data),
+                                         data, PEXPR_ARG2);
+               }
+               if (e)
+                       return e;
+       }
+
+       /* general case */
+       e = xcalloc(1, sizeof(*e));
+       pexpr_get(a);
+       pexpr_get(b);
+       pexpr_construct_and(e, a, b, 1);
+       return e;
+}
+
+struct pexpr *pexpr_or(struct pexpr *a, struct pexpr *b, struct cfdata *data, enum pexpr_move move)
+{
+       return pexpr_move_wrapper(a, b, data, move, pexpr_or_share);
+}
+
+/*
+ * macro to create a pexpr of type OR
+ */
+struct pexpr *pexpr_or_share(struct pexpr *a, struct pexpr *b, struct cfdata *data)
+{
+       struct pexpr *e;
+       bool cond1, cond2;
+
+       /* A || A  -> A */
+       if (a == b || pexpr_eq(a, b, data)) {
+               pexpr_get(a);
+               return a;
+       }
+
+       /* simplifications:
+        * A || False -> A
+        * A || True  -> True
+        */
+       cond1 = a->type == PE_SYMBOL && a->left.fexpr == data->constants->const_false;
+       cond2 = b->type == PE_SYMBOL && b->left.fexpr == data->constants->const_true;
+       if (cond1 || cond2) {
+               pexpr_get(b);
+               return b;
+       }
+       cond1 = b->type == PE_SYMBOL && b->left.fexpr == data->constants->const_false;
+       cond2 = a->type == PE_SYMBOL && a->left.fexpr == data->constants->const_true;
+       if (cond1 || cond2) {
+               pexpr_get(a);
+               return a;
+       }
+
+       /* A || (B && C) -> A if (A == B || A == C) */
+       if (b->type == PE_AND && (
+               pexpr_eq(a, b->left.pexpr, data) || pexpr_eq(a, b->right.pexpr, data)
+       )) {
+               pexpr_get(a);
+               return a;
+       }
+       /* (A && B) || C -> C if (A == C || B == C) */
+       if (a->type == PE_AND && (
+               pexpr_eq(a->left.pexpr, b, data) || pexpr_eq(a->right.pexpr, b, data)
+       )) {
+               pexpr_get(b);
+               return b;
+       }
+
+       /* -A || B -> True if A == B
+        * A || -B -> True if A == B
+        */
+       cond1 = a->type == PE_NOT && pexpr_eq(a->left.pexpr, b, data);
+       cond2 = b->type == PE_NOT && pexpr_eq(a, b->left.pexpr, data);
+       if (cond1 || cond2)
+               return pexf(data->constants->const_true);
+
+       if (a->type == PE_AND && b->type == PE_AND) {
+               e = NULL;
+               /* (A && B) || (C && D) -> A && (B || D) if (A == C) */
+               if (pexpr_eq(a->left.pexpr, b->left.pexpr, data)) {
+                       e = pexpr_and(a->left.pexpr,
+                                     pexpr_or_share(a->right.pexpr, b->right.pexpr, data), data,
+                                     PEXPR_ARG2);
+               }
+               /* (A && B) || (C && D) -> B && (A || C) if (B == D) */
+               if (pexpr_eq(a->right.pexpr, b->right.pexpr, data)) {
+                       e = pexpr_and(a->right.pexpr,
+                                     pexpr_or_share(a->left.pexpr, b->left.pexpr, data), data,
+                                     PEXPR_ARG2);
+               }
+               /* (A && B) || (C && D) -> A && (B || C) if (A == D) */
+               if (pexpr_eq(a->left.pexpr, b->right.pexpr, data)) {
+                       e = pexpr_and(a->left.pexpr,
+                                     pexpr_or_share(a->right.pexpr, b->left.pexpr, data), data,
+                                     PEXPR_ARG2);
+               }
+               /* (A && B) || (C && D) -> B && (A || D) if (B == C) */
+               if (pexpr_eq(a->right.pexpr, b->left.pexpr, data)) {
+                       e = pexpr_and(a->right.pexpr,
+                                     pexpr_or_share(a->left.pexpr, b->right.pexpr, data), data,
+                                     PEXPR_ARG2);
+               }
+               if (e)
+                       return e;
+       }
+
+       /* (A && B) || (C || D) -> C || D if
+        * A == C || A == D || B == C || B == D
+        */
+       if (a->type == PE_AND && b->type == PE_OR && (
+               pexpr_eq(a->left.pexpr, b->left.pexpr, data) ||
+               pexpr_eq(a->left.pexpr, b->right.pexpr, data) ||
+               pexpr_eq(a->right.pexpr, b->left.pexpr, data) ||
+               pexpr_eq(a->right.pexpr, b->right.pexpr, data)
+       )) {
+               pexpr_get(b);
+               return b;
+       }
+       /* (C || D) || (A && B) -> C || D if
+        * A == C || A == D || B == C || B == D
+        */
+       if (a->type == PE_OR && b->type == PE_AND && (
+               pexpr_eq(a->left.pexpr, b->left.pexpr, data) ||
+               pexpr_eq(a->left.pexpr, b->right.pexpr, data) ||
+               pexpr_eq(a->right.pexpr, b->left.pexpr, data) ||
+               pexpr_eq(a->right.pexpr, b->right.pexpr, data)
+       )) {
+               pexpr_get(a);
+               return a;
+       }
+
+       /* general case */
+       e = xcalloc(1, sizeof(*e));
+       pexpr_get(a);
+       pexpr_get(b);
+       pexpr_construct_or(e, a, b, 1);
+
+       return e;
+}
+
+struct pexpr *pexpr_not(struct pexpr *a, struct cfdata *data)
+{
+       struct pexpr *retval = pexpr_not_share(a, data);
+
+       pexpr_put(a);
+       return retval;
+}
+
+/*
+ * Builds NOT(@a)
+ */
+struct pexpr *pexpr_not_share(struct pexpr *a, struct cfdata *data)
+{
+       struct pexpr *ret_val;
+
+       if (a->type == PE_SYMBOL &&
+           a->left.fexpr == data->constants->const_false)
+               ret_val = pexf(data->constants->const_true);
+       else if (a->type == PE_SYMBOL &&
+           a->left.fexpr == data->constants->const_true)
+               ret_val = pexf(data->constants->const_false);
+       /* eliminate double negation */
+       else if (a->type == PE_NOT) {
+               ret_val = a->left.pexpr;
+               pexpr_get(ret_val);
+       }
+       /* De Morgan */
+       else if (a->type == PE_AND) {
+               ret_val = xmalloc(sizeof(*ret_val));
+               pexpr_construct_or(ret_val,
+                                  pexpr_not_share(a->left.pexpr, data),
+                                  pexpr_not_share(a->right.pexpr, data), 1);
+       } else if (a->type == PE_OR) {
+               ret_val = xmalloc(sizeof(*ret_val));
+               pexpr_construct_and(ret_val,
+                                   pexpr_not_share(a->left.pexpr, data),
+                                   pexpr_not_share(a->right.pexpr, data), 1);
+       } else {
+               ret_val = xmalloc(sizeof(*ret_val));
+               pexpr_get(a);
+               pexpr_construct_not(ret_val, a, 1);
+       }
+
+       return ret_val;
+}
+
+struct pexpr *pexpr_implies(struct pexpr *a, struct pexpr *b, struct cfdata *data,
+                           enum pexpr_move move)
+{
+       return pexpr_move_wrapper(a, b, data, move, pexpr_implies_share);
+}
+
+/*
+ * macro to construct a pexpr for "A implies B"
+ */
+struct pexpr *pexpr_implies_share(struct pexpr *a, struct pexpr *b, struct cfdata *data)
+{
+       /* A => B -> True if A == B */
+       if (a == b || pexpr_eq(a, b, data))
+               return pexf(data->constants->const_true);
+
+       /* (A => B && C) -> (A => C) if A == B */
+       if (b->type == PE_AND && pexpr_eq(a, b->left.pexpr, data))
+               return pexpr_implies_share(a, b->right.pexpr, data);
+       /* (A => B && C) -> (A => B) if A == C */
+       if (b->type == PE_AND && pexpr_eq(a, b->right.pexpr, data))
+               return pexpr_implies_share(a, b->left.pexpr, data);
+
+       /* (A => B || C) -> True if (A == B || A == C) */
+       if (b->type == PE_OR && (
+               pexpr_eq(a, b->left.pexpr, data) || pexpr_eq(a, b->right.pexpr, data)
+       ))
+               return pexf(data->constants->const_true);
+
+       /* (A && B => C) -> True if (A == C || B == C) */
+       if (a->type == PE_AND && (
+               pexpr_eq(a->left.pexpr, b, data) || pexpr_eq(a->right.pexpr, b, data)
+       ))
+               return pexf(data->constants->const_true);
+
+       return pexpr_or(pexpr_not_share(a, data), b, data, PEXPR_ARG1);
+}
+
+/*
+ * check whether a pexpr is in CNF
+ */
+bool pexpr_is_cnf(struct pexpr *e)
+{
+       if (!e)
+               return false;
+
+       switch (e->type) {
+       case PE_SYMBOL:
+               return true;
+       case PE_AND:
+               return false;
+       case PE_OR:
+               return pexpr_is_cnf(e->left.pexpr) &&
+                      pexpr_is_cnf(e->right.pexpr);
+       case PE_NOT:
+               return e->left.pexpr->type == PE_SYMBOL;
+       }
+
+       return false;
+}
+
+/*
+ * check whether a pexpr is in NNF
+ */
+bool pexpr_is_nnf(struct pexpr *e)
+{
+       if (!e)
+               return false;
+
+       switch (e->type) {
+       case PE_SYMBOL:
+               return true;
+       case PE_AND:
+       case PE_OR:
+               return pexpr_is_nnf(e->left.pexpr) && pexpr_is_nnf(e->right.pexpr);
+       case PE_NOT:
+               return e->left.pexpr->type == PE_SYMBOL;
+       }
+
+       return false;
+}
+
+/*
+ * return fexpr_both for a symbol
+ */
+struct pexpr *sym_get_fexpr_both(struct symbol *sym, struct cfdata *data)
+{
+       return sym->type == S_TRISTATE ?
+                      pexpr_or(pexf(sym->fexpr_m), pexf(sym->fexpr_y),
+                                    data, PEXPR_ARGX) :
+                      pexf(sym->fexpr_y);
+}
+
+/*
+ * return fexpr_sel_both for a symbol
+ */
+struct pexpr *sym_get_fexpr_sel_both(struct symbol *sym, struct cfdata *data)
+{
+       if (!sym->rev_dep.expr)
+               return pexf(data->constants->const_false);
+
+       return sym->type == S_TRISTATE ?
+                      pexpr_or(pexf(sym->fexpr_sel_m),
+                                    pexf(sym->fexpr_sel_y), data, PEXPR_ARGX) :
+                      pexf(sym->fexpr_sel_y);
+}
+
+/*
+ * check, if the fexpr is a symbol, a True/False-constant, a literal symbolizing a non-boolean or
+ * a choice symbol
+ */
+bool fexpr_is_symbol(struct fexpr *e)
+{
+       return e->type == FE_SYMBOL || e->type == FE_FALSE || e->type == FE_TRUE ||
+              e->type == FE_NONBOOL || e->type == FE_CHOICE || e->type == FE_SELECT ||
+              e->type == FE_NPC;
+}
+
+/*
+ * check whether a pexpr is a symbol or a negated symbol
+ */
+bool pexpr_is_symbol(struct pexpr *e)
+{
+       return e->type == PE_SYMBOL || (e->type == PE_NOT && e->left.pexpr->type == PE_SYMBOL);
+}
+
+/*
+ * check whether the fexpr is a constant (true/false)
+ */
+bool fexpr_is_constant(struct fexpr *e, struct cfdata *data)
+{
+       return e == data->constants->const_true || e == data->constants->const_false;
+}
+
+/*
+ * add a fexpr to the satmap

scripts/kconfig/cf_expr.c line 1269

Quoted 10 lines
scripts/kconfig/cf_expr.c new file +10 -0
+ */
+void fexpr_add_to_satmap(struct fexpr *e, struct cfdata *data)
+{
+       if (e->satval >= data->satmap_size) {
+               data->satmap =
+                       xrealloc(data->satmap, data->satmap_size * 2 * sizeof(**data->satmap));
+               data->satmap_size *= 2;
+       }
+
+       data->satmap[e->satval] = e;

I see a bug here.

Size mismatch for memory allocation.
(much bigger than used)

Quoted 263 lines
scripts/kconfig/cf_expr.c new file +263 -0
+}
+
+/*
+ * print a fexpr
+ */
+void fexpr_print(char *tag, struct fexpr *e)
+{
+       if (!e)
+               return;
+
+       printf("%s: %s\n", tag, str_get(&e->name));
+}
+
+/*
+ * write an fexpr into a string (format needed for testing)
+ */
+void fexpr_as_char(struct fexpr *e, struct gstr *s)
+{
+       if (!e)
+               return;
+
+       switch (e->type) {
+       case FE_SYMBOL:
+       case FE_CHOICE:
+       case FE_SELECT:
+       case FE_NPC:
+       case FE_NONBOOL:
+               str_append(s, "definedEx(");
+               str_append(s, str_get(&e->name));
+               str_append(s, ")");
+               return;
+       case FE_FALSE:
+               str_append(s, "0");
+               return;
+       case FE_TRUE:
+               str_append(s, "1");
+               return;
+       default:
+               return;
+       }
+}
+
+/*
+ * write a pexpr into a string
+ */
+void pexpr_as_char(struct pexpr *e, struct gstr *s, int parent, struct cfdata *data)
+{
+       if (!e)
+               return;
+
+       switch (e->type) {
+       case PE_SYMBOL:
+               if (e->left.fexpr == data->constants->const_false) {
+                       str_append(s, "0");
+                       return;
+               }
+               if (e->left.fexpr == data->constants->const_true) {
+                       str_append(s, "1");
+                       return;
+               }
+               str_append(s, "definedEx(");
+               str_append(s, str_get(&e->left.fexpr->name));
+               str_append(s, ")");
+               return;
+       case PE_AND:
+               if (parent != PE_AND)
+                       str_append(s, "(");
+               pexpr_as_char(e->left.pexpr, s, PE_AND, data);
+               str_append(s, " && ");
+               pexpr_as_char(e->right.pexpr, s, PE_AND, data);
+               if (parent != PE_AND)
+                       str_append(s, ")");
+               return;
+       case PE_OR:
+               if (parent != PE_OR)
+                       str_append(s, "(");
+               pexpr_as_char(e->left.pexpr, s, PE_OR, data);
+               str_append(s, " || ");
+               pexpr_as_char(e->right.pexpr, s, PE_OR, data);
+               if (parent != PE_OR)
+                       str_append(s, ")");
+               return;
+       case PE_NOT:
+               str_append(s, "!");
+               pexpr_as_char(e->left.pexpr, s, PE_NOT, data);
+               return;
+       }
+}
+
+/*
+ * write a pexpr into a string
+ */
+void pexpr_as_char_short(struct pexpr *e, struct gstr *s, int parent)
+{
+       if (!e)
+               return;
+
+       switch (e->type) {
+       case PE_SYMBOL:
+               str_append(s, str_get(&e->left.fexpr->name));
+               return;
+       case PE_AND:
+               if (parent != PE_AND)
+                       str_append(s, "(");
+               pexpr_as_char_short(e->left.pexpr, s, PE_AND);
+               str_append(s, " && ");
+               pexpr_as_char_short(e->right.pexpr, s, PE_AND);
+               if (parent != PE_AND)
+                       str_append(s, ")");
+               return;
+       case PE_OR:
+               if (parent != PE_OR)
+                       str_append(s, "(");
+               pexpr_as_char_short(e->left.pexpr, s, PE_OR);
+               str_append(s, " || ");
+               pexpr_as_char_short(e->right.pexpr, s, PE_OR);
+               if (parent != PE_OR)
+                       str_append(s, ")");
+               return;
+       case PE_NOT:
+               str_append(s, "!");
+               pexpr_as_char_short(e->left.pexpr, s, PE_NOT);
+               return;
+       }
+}
+
+/*
+ * check whether a pexpr contains a specific fexpr
+ */
+bool pexpr_contains_fexpr(struct pexpr *e, struct fexpr *fe)
+{
+       if (!e)
+               return false;
+
+       switch (e->type) {
+       case PE_SYMBOL:
+               return e->left.fexpr->satval == fe->satval;
+       case PE_AND:
+       case PE_OR:
+               return pexpr_contains_fexpr(e->left.pexpr, fe) ||
+                       pexpr_contains_fexpr(e->right.pexpr, fe);
+       case PE_NOT:
+               return e->left.pexpr->left.fexpr->satval == fe->satval;
+       }
+
+       return false;
+}
+
+/*
+ * init list of fexpr
+ */
+struct fexpr_list *fexpr_list_init(void)
+{
+       struct fexpr_list *list = xcalloc(1, sizeof(*list));
+
+       list->head = NULL;
+       list->tail = NULL;
+       list->size = 0;
+
+       return list;
+}
+
+/*
+ * init list of fexpr_list
+ */
+struct fexl_list *fexl_list_init(void)
+{
+       struct fexl_list *list = xcalloc(1, sizeof(*list));
+
+       list->head = NULL;
+       list->tail = NULL;
+       list->size = 0;
+
+       return list;
+}
+
+/*
+ * init list of pexpr
+ */
+struct pexpr_list *pexpr_list_init(void)
+{
+       struct pexpr_list *list = xcalloc(1, sizeof(*list));
+
+       list->head = NULL;
+       list->tail = NULL;
+       list->size = 0;
+
+       return list;
+}
+
+/*
+ * init list of symbol_fix
+ */
+struct sfix_list *sfix_list_init(void)
+{
+       struct sfix_list *list = xcalloc(1, sizeof(*list));
+
+       list->head = NULL;
+       list->tail = NULL;
+       list->size = 0;
+
+       return list;
+}
+
+/*
+ * init list of symbol_fix
+ */
+struct sfl_list *sfl_list_init(void)
+{
+       struct sfl_list *list = xcalloc(1, sizeof(*list));
+
+       list->head = NULL;
+       list->tail = NULL;
+       list->size = 0;
+
+       return list;
+}
+
+/*
+ * init list of symbol_dvalue
+ */
+struct sdv_list *sdv_list_init(void)
+{
+       struct sdv_list *list = xcalloc(1, sizeof(*list));
+
+       list->head = NULL;
+       list->tail = NULL;
+       list->size = 0;
+
+       return list;
+}
+
+/*
+ * init list of symbols
+ */
+struct sym_list *sym_list_init(void)
+{
+       struct sym_list *list = xcalloc(1, sizeof(*list));
+
+       list->head = NULL;
+       list->tail = NULL;
+       list->size = 0;
+
+       return list;
+}
+
+/*
+ * init list of default_maps
+ */
+struct defm_list *defm_list_init(void)
+{
+       struct defm_list *list = xcalloc(1, sizeof(*list));
+
+       list->head = NULL;
+       list->tail = NULL;
+       list->size = 0;
+
+       return list;
+}
+
+/*
+ * init list of properties
+ */

scripts/kconfig/cf_expr.c line 1542

Quoted 10 lines
scripts/kconfig/cf_expr.c new file +10 -0
+struct prop_list *prop_list_init(void)
+{
+       struct prop_list *list = xcalloc(1, sizeof(*list));
+
+       list->head = NULL;
+       list->tail = NULL;
+       list->size = 0;
+
+       return list;
+}

A bunch of similar *_list_init() functions. Do not do this.

Kconfig uses the similar liss.h to the kernel space.

Remove all the duplications.

Quoted 178 lines
scripts/kconfig/cf_expr.c new file +178 -0
+/*
+ * add element to tail of a fexpr_list
+ */
+void fexpr_list_add(struct fexpr_list *list, struct fexpr *fe)
+{
+       struct fexpr_node *node = xcalloc(1, sizeof(*node));
+
+       node->elem = fe;
+
+       if (list->size == 0) {
+               list->head = node;
+               list->tail = node;
+       } else {
+               node->prev = list->tail;
+               list->tail = node;
+               node->prev->next = node;
+       }
+
+       list->size++;
+}
+
+/*
+ * add element to tail of a fexl_list
+ */
+void fexl_list_add(struct fexl_list *list, struct fexpr_list *elem)
+{
+       struct fexl_node *node = xcalloc(1, sizeof(*node));
+
+       node->elem = elem;
+
+       if (list->size == 0) {
+               list->head = node;
+               list->tail = node;
+       } else {
+               node->prev = list->tail;
+               list->tail = node;
+               node->prev->next = node;
+       }
+
+       list->size++;
+}
+
+/*
+ * add element to tail of a pexpr_list
+ */
+void pexpr_list_add(struct pexpr_list *list, struct pexpr *e)
+{
+       struct pexpr_node *node = xcalloc(1, sizeof(*node));
+
+       node->elem = e;
+
+       if (list->size == 0) {
+               list->head = node;
+               list->tail = node;
+       } else {
+               node->prev = list->tail;
+               list->tail = node;
+               node->prev->next = node;
+       }
+
+       list->size++;
+}
+
+/*
+ * add element to tail of a sfix_list
+ */
+void sfix_list_add(struct sfix_list *list, struct symbol_fix *fix)
+{
+       struct sfix_node *node = xcalloc(1, sizeof(*node));
+
+       node->elem = fix;
+
+       if (list->size == 0) {
+               list->head = node;
+               list->tail = node;
+       } else {
+               node->prev = list->tail;
+               list->tail = node;
+               node->prev->next = node;
+       }
+
+       list->size++;
+}
+
+/*
+ * add element to tail of a sfl_list
+ */
+void sfl_list_add(struct sfl_list *list, struct sfix_list *elem)
+{
+       struct sfl_node *node = xcalloc(1, sizeof(*node));
+
+       node->elem = elem;
+
+       if (list->size == 0) {
+               list->head = node;
+               list->tail = node;
+       } else {
+               node->prev = list->tail;
+               list->tail = node;
+               node->prev->next = node;
+       }
+
+       list->size++;
+}
+
+/*
+ * add element to tail of a sdv_list
+ */
+void sdv_list_add(struct sdv_list *list, struct symbol_dvalue *sdv)
+{
+       struct sdv_node *node = xcalloc(1, sizeof(*node));
+
+       node->elem = sdv;
+
+       if (list->size == 0) {
+               list->head = node;
+               list->tail = node;
+       } else {
+               node->prev = list->tail;
+               list->tail = node;
+               node->prev->next = node;
+       }
+
+       list->size++;
+}
+
+/*
+ * add element to tail of a sym_list
+ */
+void sym_list_add(struct sym_list *list, struct symbol *sym)
+{
+       struct sym_node *node = xcalloc(1, sizeof(*node));
+
+       node->elem = sym;
+
+       if (list->size == 0) {
+               list->head = node;
+               list->tail = node;
+       } else {
+               node->prev = list->tail;
+               list->tail = node;
+               node->prev->next = node;
+       }
+
+       list->size++;
+}
+
+/*
+ * add element to tail of a defm_list
+ */
+void defm_list_add(struct defm_list *list, struct default_map *map)
+{
+       struct defm_node *node = xcalloc(1, sizeof(*node));
+
+       node->elem = map;
+
+       if (list->size == 0) {
+               list->head = node;
+               list->tail = node;
+       } else {
+               node->prev = list->tail;
+               list->tail = node;
+               node->prev->next = node;
+       }
+
+       list->size++;
+}
+
+/*
+ * add element to tail of a prop_list
+ */
+void prop_list_add(struct prop_list *list, struct property *prop)
+{
+       struct prop_node *node = xcalloc(1, sizeof(*node));
+
+       node->elem = prop;
+
+       if (list->size == 0) {

scripts/kconfig/cf_expr.c line 1731

Quoted 10 lines
scripts/kconfig/cf_expr.c new file +10 -0
+               list->head = node;
+               list->tail = node;
+       } else {
+               node->prev = list->tail;
+               list->tail = node;
+               node->prev->next = node;
+       }
+
+       list->size++;
+}

Again. A bunch of *_list_add().

Quoted 700 lines
scripts/kconfig/cf_expr.c new file +700 -0
+/*
+ * delete an element from a fexpr_list
+ */
+void fexpr_list_delete(struct fexpr_list *list, struct fexpr_node *node)
+{
+       if (list->size == 0 || node == NULL)
+               return;
+
+       if (node == list->head)
+               list->head = node->next;
+       else
+               node->prev->next = node->next;
+
+       if (node == list->tail)
+               list->tail = node->prev;
+       else
+               node->next->prev = node->prev;
+
+       list->size--;
+       free(node);
+}
+
+/*
+ * delete an element from a fexpr_list
+ */
+void sfix_list_delete(struct sfix_list *list, struct sfix_node *node)
+{
+       if (list->size == 0 || node == NULL)
+               return;
+
+       if (node == list->head)
+               list->head = node->next;
+       else
+               node->prev->next = node->next;
+
+       if (node == list->tail)
+               list->tail = node->prev;
+       else
+               node->next->prev = node->prev;
+
+       list->size--;
+       free(node);
+}
+
+/*
+ * delete an element from a fexpr_list
+ */
+void pexpr_list_delete(struct pexpr_list *list, struct pexpr_node *node)
+{
+       if (list->size == 0 || node == NULL)
+               return;
+
+       if (node == list->head)
+               list->head = node->next;
+       else
+               node->prev->next = node->next;
+
+       if (node == list->tail)
+               list->tail = node->prev;
+       else
+               node->next->prev = node->prev;
+
+       list->size--;
+       free(node);
+}
+
+/*
+ * delete an element from a fexl_list
+ */
+void fexl_list_delete(struct fexl_list *list, struct fexl_node *node)
+{
+       if (list->size == 0 || node == NULL)
+               return;
+
+       if (node == list->head)
+               list->head = node->next;
+       else
+               node->prev->next = node->next;
+
+       if (node == list->tail)
+               list->tail = node->prev;
+       else
+               node->next->prev = node->prev;
+
+       list->size--;
+       free(node);
+}
+
+/*
+ * delete the first occurrence of elem in an fexl_list
+ */
+void fexl_list_delete_elem(struct fexl_list *list, struct fexpr_list *elem)
+{
+       struct fexl_node *node, *to_delete = NULL;
+
+       fexl_list_for_each(node, list) {
+               if (node->elem == elem) {
+                       to_delete = node;
+                       break;
+               }
+       }
+
+       if (to_delete != NULL)
+               fexl_list_delete(list, to_delete);
+}
+
+/*
+ * make a shallow copy of a fexpr_list
+ */
+struct fexpr_list *fexpr_list_copy(struct fexpr_list *list)
+{
+       struct fexpr_list *ret = fexpr_list_init();
+       struct fexpr_node *node;
+
+       fexpr_list_for_each(node, list)
+               fexpr_list_add(ret, node->elem);
+
+       return ret;
+}
+
+/*
+ * make a shallow copy of a fexl_list
+ */
+struct fexl_list *fexl_list_copy(struct fexl_list *list)
+{
+       struct fexl_list *ret = fexl_list_init();
+       struct fexl_node *node;
+
+       fexl_list_for_each(node, list)
+               fexl_list_add(ret, node->elem);
+
+       return ret;
+}
+
+/*
+ * make a shallow copy of a sdv_list
+ */
+struct sdv_list *sdv_list_copy(struct sdv_list *list)
+{
+       struct sdv_list *ret = sdv_list_init();
+       struct sdv_node *node;
+
+       sdv_list_for_each(node, list)
+               sdv_list_add(ret, node->elem);
+
+
+       return ret;
+}
+
+/*
+ * make a shallow copy of a sfix_list
+ */
+struct sfix_list *sfix_list_copy(struct sfix_list *list)
+{
+       struct sfix_list *ret = sfix_list_init();
+       struct sfix_node *node;
+
+       sfix_list_for_each(node, list)
+               sfix_list_add(ret, node->elem);
+
+       return ret;
+}
+
+/*
+ * print a fexpr_list
+ */
+void fexpr_list_print(char *title, struct fexpr_list *list)
+{
+       struct fexpr_node *node;
+
+       printf("%s: [", title);
+
+       fexpr_list_for_each(node, list) {
+               printf("%s", str_get(&node->elem->name));
+               if (node->next != NULL)
+                       printf(", ");
+       }
+
+       printf("]\n");
+}
+
+/*
+ * print a fexl_list
+ */
+void fexl_list_print(char *title, struct fexl_list *list)
+{
+       struct fexl_node *node;
+
+       printf("%s:\n", title);
+
+       fexl_list_for_each(node, list)
+               fexpr_list_print(":", node->elem);
+}
+
+/*
+ * print a pexpr_list
+ */
+void pexpr_list_print(char *title, struct pexpr_list *list)
+{
+       struct pexpr_node *node;
+
+       printf("%s: [", title);
+
+       pexpr_list_for_each(node, list) {
+               pexpr_print_util(node->elem, -1);
+               if (node->next != NULL)
+                       printf(", ");
+       }
+
+       printf("]\n");
+}
+
+/*
+ * free an fexpr_list
+ */
+void fexpr_list_free(struct fexpr_list *list)
+{
+       struct fexpr_node *node = list->head, *tmp;
+
+       while (node != NULL) {
+               tmp = node->next;
+               free(node);
+               node = tmp;
+       }
+
+       free(list);
+}
+
+/*
+ * free an defm_list (and pexpr_put the conditions of the maps and free the
+ * node->element's)
+ */
+void defm_list_destruct(struct defm_list *list)
+{
+       struct defm_node *node = list->head, *tmp;
+
+       while (node != NULL) {
+               tmp = node->next;
+               pexpr_put(node->elem->e);
+               free(node->elem);
+               free(node);
+               node = tmp;
+       }
+
+       free(list);
+}
+
+/*
+ * free an fexl_list
+ */
+void fexl_list_free(struct fexl_list *list)
+{
+       struct fexl_node *node = list->head, *tmp;
+
+       while (node != NULL) {
+               tmp = node->next;
+               free(node);
+               node = tmp;
+       }
+
+       free(list);
+}
+
+/*
+ * free a sdv_list
+ */
+void sdv_list_free(struct sdv_list *list)
+{
+       struct sdv_node *node = list->head, *tmp;
+
+       while (node != NULL) {
+               tmp = node->next;
+               free(node);
+               node = tmp;
+       }
+
+       free(list);
+}
+
+/*
+ * free a pexpr_list (and pexpr_put the elements)
+ */
+void pexpr_list_free_put(struct pexpr_list *list)
+{
+       struct pexpr_node *node = list->head, *tmp;
+
+       while (node != NULL) {
+               tmp = node->next;
+               pexpr_put(node->elem);
+               free(node);
+               node = tmp;
+       }
+
+       free(list);
+}
+
+/*
+ * free a prop_list
+ */
+void prop_list_free(struct prop_list *list)
+{
+       struct prop_node *node = list->head, *tmp;
+
+       while (node != NULL) {
+               tmp = node->next;
+               free(node);
+               node = tmp;
+       }
+
+       free(list);
+}
+
+/*
+ * free a sym_list
+ */
+void sym_list_free(struct sym_list *list)
+{
+       struct sym_node *node = list->head, *tmp;
+
+       while (node != NULL) {
+               tmp = node->next;
+               free(node);
+               node = tmp;
+       }
+
+       free(list);
+}
+
+/*
+ * simplify a pexpr in-place
+ *     pexpr && False -> False
+ *     pexpr && True  -> pexpr
+ *     pexpr || False -> pexpr
+ *     pexpr || True  -> True
+ */
+static void pexpr_eliminate_yn(struct pexpr *e, struct cfdata *data)
+{
+       struct pexpr *tmp;
+       unsigned int ref_count;
+
+       if (!e)
+               return;
+
+       switch (e->type) {
+       case PE_AND:
+               pexpr_eliminate_yn(e->left.pexpr, data);
+               pexpr_eliminate_yn(e->right.pexpr, data);
+               if (e->left.pexpr->type == PE_SYMBOL) {
+                       if (e->left.pexpr->left.fexpr == data->constants->const_false) {
+                               pexpr_put(e->left.pexpr);
+                               pexpr_put(e->right.pexpr);
+                               ref_count = e->ref_count;
+                               pexpr_construct_sym(
+                                       e, data->constants->const_false,
+                                       ref_count);
+                               return;
+                       } else if (e->left.pexpr->left.fexpr == data->constants->const_true) {
+                               pexpr_put(e->left.pexpr);
+                               tmp = e->right.pexpr;
+                               ref_count = e->ref_count;
+                               pexpr_shallow_copy(e, tmp, ref_count);
+                               pexpr_put(tmp);
+                               return;
+                       }
+               }
+               if (e->right.pexpr->type == PE_SYMBOL) {
+                       if (e->right.pexpr->left.fexpr == data->constants->const_false) {
+                               pexpr_put(e->left.pexpr);
+                               pexpr_put(e->right.pexpr);
+                               ref_count = e->ref_count;
+                               pexpr_construct_sym(
+                                       e, data->constants->const_false,
+                                       ref_count);
+                               return;
+                       } else if (e->right.pexpr->left.fexpr == data->constants->const_true) {
+                               pexpr_put(e->right.pexpr);
+                               tmp = e->left.pexpr;
+                               ref_count = e->ref_count;
+                               pexpr_shallow_copy(e, tmp, ref_count);
+                               pexpr_put(tmp);
+                               return;
+                       }
+               }
+               break;
+       case PE_OR:
+               pexpr_eliminate_yn(e->left.pexpr, data);
+               pexpr_eliminate_yn(e->right.pexpr, data);
+               if (e->left.pexpr->type == PE_SYMBOL) {
+                       if (e->left.pexpr->left.fexpr == data->constants->const_false) {
+                               pexpr_put(e->left.pexpr);
+                               tmp = e->right.pexpr;
+                               ref_count = e->ref_count;
+                               pexpr_shallow_copy(e, tmp, ref_count);
+                               pexpr_put(tmp);
+                               return;
+                       } else if (e->left.pexpr->left.fexpr == data->constants->const_true) {
+                               pexpr_put(e->left.pexpr);
+                               pexpr_put(e->right.pexpr);
+                               ref_count = e->ref_count;
+                               pexpr_construct_sym(
+                                       e, data->constants->const_true,
+                                       ref_count);
+                               return;
+                       }
+               }
+               if (e->right.pexpr->type == PE_SYMBOL) {
+                       if (e->right.pexpr->left.fexpr == data->constants->const_false) {
+                               pexpr_put(e->right.pexpr);
+                               tmp = e->left.pexpr;
+                               ref_count = e->ref_count;
+                               pexpr_shallow_copy(e, tmp, ref_count);
+                               pexpr_put(tmp);
+                               return;
+                       } else if (e->right.pexpr->left.fexpr == data->constants->const_true) {
+                               pexpr_put(e->left.pexpr);
+                               pexpr_put(e->right.pexpr);
+                               ref_count = e->ref_count;
+                               pexpr_construct_sym(e,
+                                                   data->constants->const_true,
+                                                   ref_count);
+                               return;
+                       }
+               }
+       default:
+               break;
+       }
+}
+
+static void pexpr_shallow_copy(struct pexpr *dest, struct pexpr *org, unsigned int ref_count)
+{
+       struct pexpr inter;
+
+       inter.type = org->type;
+       inter.left = org->left;
+       inter.right = org->right;
+       if (org->type == PE_OR || org->type == PE_AND) {
+               pexpr_get(org->left.pexpr);
+               pexpr_get(org->right.pexpr);
+       } else if (org->type == PE_NOT) {
+               pexpr_get(org->left.pexpr);
+       }
+       inter.ref_count = ref_count;
+       *dest = inter;
+}
+
+/*
+ * copy a pexpr
+ */
+struct pexpr *pexpr_deep_copy(const struct pexpr *org)
+{
+       struct pexpr *e;
+
+       if (!org)
+               return NULL;
+
+       e = xmalloc(sizeof(*org));
+       memcpy(e, org, sizeof(*org));
+       e->ref_count = 1;
+       switch (org->type) {
+       case PE_SYMBOL:
+               e->left = org->left;
+               break;
+       case PE_AND:
+       case PE_OR:
+               e->left.pexpr = pexpr_deep_copy(org->left.pexpr);
+               e->right.pexpr = pexpr_deep_copy(org->right.pexpr);
+               break;
+       case PE_NOT:
+               e->left.pexpr = pexpr_deep_copy(org->left.pexpr);
+               break;
+       }
+
+       return e;
+}
+
+/*
+ * free a pexpr
+ */
+void pexpr_free_depr(struct pexpr *e)
+{
+       if (!e)
+               return;
+
+       switch (e->type) {
+       case PE_SYMBOL:
+               break;
+       case PE_AND:
+       case PE_OR:
+               pexpr_free_depr(e->left.pexpr);
+               pexpr_free_depr(e->right.pexpr);
+               break;
+       case PE_NOT:
+               pexpr_free_depr(e->left.pexpr);
+               break;
+       }
+
+       free(e);
+}
+
+/*
+ * Increments ref_count and returns @e
+ */
+struct pexpr *pexpr_get(struct pexpr *e)
+{
+       ++e->ref_count;
+       return e;
+}
+
+/*
+ * Decrements ref_count and if it becomes 0, it recursively puts the references
+ * to its children and calls ``free(e)``. If @e == NULL, it does nothing.
+ */
+void pexpr_put(struct pexpr *e)
+{
+       if (!e)
+               return;
+
+       if (e->ref_count == 0) {
+               printd("Invalid call to %s - ref_count is zero\n", __func__);
+               return;
+       }
+
+       --e->ref_count;
+       if (e->ref_count > 0)
+               return;
+
+       switch (e->type) {
+       case PE_SYMBOL:
+               break;
+       case PE_AND:
+       case PE_OR:
+               pexpr_put(e->left.pexpr);
+               pexpr_put(e->right.pexpr);
+               break;
+       case PE_NOT:
+               pexpr_put(e->left.pexpr);
+               break;
+       }
+
+       free(e);
+}
+
+/*
+ * calls pexpr_put for a NULL-terminated array of struct pexpr *
+ */
+void _pexpr_put_list(struct pexpr **es)
+{
+       for (; *es != NULL; es++)
+               pexpr_put(*es);
+}
+
+#define e1 (*ep1)
+#define e2 (*ep2)
+/*
+ * pexpr_eliminate_eq() helper
+ */
+static void __pexpr_eliminate_eq(enum pexpr_type type, struct pexpr **ep1, struct pexpr **ep2,
+                                struct cfdata *data)
+{
+       /* recurse down to the leaves */
+       if (e1->type == type) {
+               __pexpr_eliminate_eq(type, &e1->left.pexpr, &e2, data);
+               __pexpr_eliminate_eq(type, &e1->right.pexpr, &e2, data);
+               return;
+       }
+       if (e2->type == type) {
+               __pexpr_eliminate_eq(type, &e1, &e2->left.pexpr, data);
+               __pexpr_eliminate_eq(type, &e1, &e2->right.pexpr, data);
+               return;
+       }
+
+       /* e1 and e2 are leaves. Compare them. */
+       if (e1->type == PE_SYMBOL && e2->type == PE_SYMBOL &&
+           e1->left.fexpr->satval == e2->left.fexpr->satval &&
+           (e1->left.fexpr == data->constants->const_true ||
+            e2->left.fexpr == data->constants->const_false))
+               return;
+       if (!pexpr_eq(e1, e2, data))
+               return;
+
+       /* e1 and e2 are equal leaves. Prepare them for elimination. */
+       trans_count++;
+       pexpr_put(e1);
+       pexpr_put(e2);
+       switch (type) {
+       case PE_AND:
+               e1 = pexf(data->constants->const_true);
+               e2 = pexf(data->constants->const_true);
+               break;
+       case PE_OR:
+               e1 = pexf(data->constants->const_false);
+               e2 = pexf(data->constants->const_false);
+               break;
+       default:
+               break;
+       }
+}
+
+/*
+ * rewrite pexpr ep1 and ep2 to remove operands common to both
+ */
+static void pexpr_eliminate_eq(struct pexpr **ep1, struct pexpr **ep2, struct cfdata *data)
+{
+       if (!e1 || !e2)
+               return;
+
+       switch (e1->type) {
+       case PE_AND:
+       case PE_OR:
+               __pexpr_eliminate_eq(e1->type, ep1, ep2, data);
+       default:
+               break;
+       }
+       if (e1->type != e2->type)
+               switch (e2->type) {
+               case PE_AND:
+               case PE_OR:
+                       __pexpr_eliminate_eq(e2->type, ep1, ep2, data);
+               default:
+                       break;
+               }
+       pexpr_eliminate_yn(e1, data);
+       pexpr_eliminate_yn(e2, data);
+}
+#undef e1
+#undef e2
+
+/*
+ * check whether 2 pexpr are equal
+ */
+bool pexpr_eq(struct pexpr *e1, struct pexpr *e2, struct cfdata *data)
+{
+       bool res;
+       int old_count;
+
+       if (!e1 || !e2)
+               return false;
+
+       if (e1->type != e2->type)
+               return false;
+
+       switch (e1->type) {
+       case PE_SYMBOL:
+               return e1->left.fexpr->satval == e2->left.fexpr->satval;
+       case PE_AND:
+       case PE_OR:
+               e1 = pexpr_deep_copy(e1);
+               e2 = pexpr_deep_copy(e2);
+               old_count = trans_count;
+               pexpr_eliminate_eq(&e1, &e2, data);
+               res = (e1->type == PE_SYMBOL && e2->type == PE_SYMBOL &&
+                       e1->left.fexpr->satval == e2->left.fexpr->satval);
+               pexpr_put(e1);
+               pexpr_put(e2);
+               trans_count = old_count;
+               return res;
+       case PE_NOT:
+               return pexpr_eq(e1->left.pexpr, e2->left.pexpr, data);
+       }
+
+       return false;
+}
+
+/*
+ * print a pexpr
+ */
+static void pexpr_print_util(struct pexpr *e, int prevtoken)
+{
+       if (!e)
+               return;
+
+       switch (e->type) {
+       case PE_SYMBOL:
+               printf("%s", str_get(&e->left.fexpr->name));
+               break;
+       case PE_AND:
+               if (prevtoken != PE_AND && prevtoken != -1)
+                       printf("(");
+               pexpr_print_util(e->left.pexpr, PE_AND);
+               printf(" && ");
+               pexpr_print_util(e->right.pexpr, PE_AND);
+               if (prevtoken != PE_AND && prevtoken != -1)
+                       printf(")");
+               break;
+       case PE_OR:
+               if (prevtoken != PE_OR && prevtoken != -1)
+                       printf("(");
+               pexpr_print_util(e->left.pexpr, PE_OR);
+               printf(" || ");
+               pexpr_print_util(e->right.pexpr, PE_OR);
+               if (prevtoken != PE_OR && prevtoken != -1)
+                       printf(")");
+               break;
+       case PE_NOT:
+               printf("!");
+               pexpr_print_util(e->left.pexpr, PE_NOT);
+               break;
+       }
+}
+void pexpr_print(char *tag, struct pexpr *e, int prevtoken)

scripts/kconfig/cf_expr.c line 2442

Quoted 10 lines
scripts/kconfig/cf_expr.c new file +10 -0
+{
+       printf("%s: ", tag);
+       pexpr_print_util(e, prevtoken);
+       printf("\n");
+}
+
+/*
+ * convert a fexpr to a pexpr
+ */
+struct pexpr *pexf(struct fexpr *fe)

Not only this one, but more descriptive function name please.

"pexf", so what?
I do not understand what it is doing from the name.

Quoted 142 lines
scripts/kconfig/cf_expr.c new file +142 -0
+{
+       struct pexpr *pe = xcalloc(1, sizeof(*pe));
+
+       pexpr_construct_sym(pe, fe, 1);
+       return pe;
+}
+
+void pexpr_construct_or(struct pexpr *e, struct pexpr *left,
+                            struct pexpr *right, unsigned int ref_count)
+{
+       e->type = PE_OR;
+       e->left.pexpr = left;
+       e->right.pexpr = right;
+       e->ref_count = ref_count;
+}
+
+void pexpr_construct_and(struct pexpr *e, struct pexpr *left,
+                            struct pexpr *right, unsigned int ref_count)
+{
+       e->type = PE_AND;
+       e->left.pexpr = left;
+       e->right.pexpr = right;
+       e->ref_count = ref_count;
+}
+
+void pexpr_construct_not(struct pexpr *e, struct pexpr *left,
+                         unsigned int ref_count)
+{
+       e->type = PE_NOT;
+       e->left.pexpr = left;
+       e->right.pexpr = NULL;
+       e->ref_count = ref_count;
+}
+
+void pexpr_construct_sym(struct pexpr *e, struct fexpr *left,
+                         unsigned int ref_count)
+{
+       e->type = PE_SYMBOL;
+       e->left.fexpr = left;
+       e->right.pexpr = NULL;
+       e->ref_count = ref_count;
+}
+
+static struct pexpr *pexpr_join_or(struct pexpr *e1, struct pexpr *e2, struct cfdata *data)
+{
+       if (pexpr_eq(e1, e2, data))
+               return pexpr_deep_copy(e1);
+       else
+               return NULL;
+}
+
+static struct pexpr *pexpr_join_and(struct pexpr *e1, struct pexpr *e2, struct cfdata *data)
+{
+       if (pexpr_eq(e1, e2, data))
+               return pexpr_deep_copy(e1);
+       else
+               return NULL;
+}
+
+/*
+ * pexpr_eliminate_dups() helper.
+ */
+static void pexpr_eliminate_dups1(enum pexpr_type type, struct pexpr **ep1, struct pexpr **ep2,
+                                 struct cfdata *data)
+{
+#define e1 (*ep1)
+#define e2 (*ep2)
+
+       struct pexpr *tmp;
+
+       /* recurse down to leaves */
+       if (e1->type == type) {
+               pexpr_eliminate_dups1(type, &e1->left.pexpr, &e2, data);
+               pexpr_eliminate_dups1(type, &e1->right.pexpr, &e2, data);
+               return;
+       }
+       if (e2->type == type) {
+               pexpr_eliminate_dups1(type, &e1, &e2->left.pexpr, data);
+               pexpr_eliminate_dups1(type, &e1, &e2->right.pexpr, data);
+               return;
+       }
+
+       /* e1 and e2 are leaves. Compare them. */
+
+       if (e1 == e2)
+               return;
+
+       switch (e1->type) {
+       case PE_AND:
+       case PE_OR:
+               pexpr_eliminate_dups1(e1->type, &e1, &e1, data);
+       default:
+               break;
+       }
+
+       switch (type) {
+       case PE_AND:
+               tmp = pexpr_join_and(e1, e2, data);
+               if (tmp) {
+                       pexpr_put(e1);
+                       pexpr_put(e2);
+                       e1 = pexf(data->constants->const_true);
+                       e2 = tmp;
+                       trans_count++;
+               }
+               break;
+       case PE_OR:
+               tmp = pexpr_join_or(e1, e2, data);
+               if (tmp) {
+                       pexpr_put(e1);
+                       pexpr_put(e2);
+                       e1 = pexf(data->constants->const_false);
+                       e2 = tmp;
+                       trans_count++;
+               }
+               break;
+       default:
+               break;
+       }
+
+#undef e1
+#undef e2
+}
+
+/*
+ * eliminate duplicate and redundant operands
+ */
+struct pexpr *pexpr_eliminate_dups(struct pexpr *e, struct cfdata *data)
+{
+       int oldcount;
+
+       if (!e)
+               return e;
+
+       oldcount = trans_count;
+       while (true) {
+               trans_count = 0;
+               switch (e->type) {
+               case PE_AND:
+               case PE_OR:
+                       pexpr_eliminate_dups1(e->type, &e, &e, data);
+               default:

scripts/kconfig/cf_expr.c line 2594

Quoted 10 lines
scripts/kconfig/cf_expr.c new file +10 -0
+                       break;
+               }
+               if (!trans_count)
+                       /* no simplification done in this pass. We're done. */
+                       break;
+               pexpr_eliminate_yn(e, data);
+       }
+       trans_count = oldcount;
+       return e;
+}

This is a full copy-paste of scripts/kconfig/expr.c

If you need all of these, please referector to avoid code duplication.

Quoted 34 lines
scripts/kconfig/cf_expr.h new file +28 -0
new file mode 100644
index 000000000000..07435ae381e6
@@ -0,0 +1,296 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023 Patrick Franz <[email protected]>
+ */
+
+#ifndef CF_EXPR_H
+#define CF_EXPR_H
+
+#include <stdbool.h>
+
+#include "cf_defs.h"(
+
+#define fexpr_list_for_each(node, list) \
+       for (node = list->head; node != NULL; node = node->next)
+
+#define fexl_list_for_each(node, list) \
+       fexpr_list_for_each(node, list)
+
+#define pexpr_list_for_each(node, list) \
+       fexpr_list_for_each(node, list)
+
+#define sdv_list_for_each(node, list) \
+       fexpr_list_for_each(node, list)
+
+#define sfix_list_for_each(node, list) \
+       fexpr_list_for_each(node, list)
+
+#define sfl_list_for_each(node, list) \

scripts/kconfig/cf_expr.h line 38

Quoted 10 lines
scripts/kconfig/cf_expr.h new file +10 -0
+       fexpr_list_for_each(node, list)
+
+#define sym_list_for_each(node, list) \
+       fexpr_list_for_each(node, list)
+
+#define defm_list_for_each(node, list) \
+       fexpr_list_for_each(node, list)
+
+#define prop_list_for_each(node, list) \
+       fexpr_list_for_each(node, list)

All of these are the same as fexpr_list_for_each(), which is the same as list_for_each().

kconfig already use list.h

Please do not proliferate similar code.

Quoted 260 lines
scripts/kconfig/cf_expr.h new file +258 -1
+
+/* call pexpr_put for a list of pexpr's */
+#define PEXPR_PUT(...) _pexpr_put_list((struct pexpr *[]){ __VA_ARGS__, NULL })
+
+/* create a fexpr */
+struct fexpr *fexpr_create(int satval, enum fexpr_type type, char *name);
+
+/* create the fexpr for a symbol */
+void sym_create_fexpr(struct symbol *sym, struct cfdata *data);
+
+struct pexpr *expr_calculate_pexpr_both(struct expr *e, struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_y(struct expr *e, struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_m(struct expr *e, struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_y_and(struct expr *a, struct expr *b,
+                                        struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_m_and(struct expr *a, struct expr *b,
+                                        struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_both_and(struct expr *a, struct expr *b,
+                                           struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_y_or(struct expr *a, struct expr *b,
+                                       struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_m_or(struct expr *a, struct expr *b,
+                                       struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_both_or(struct expr *a, struct expr *b,
+                                          struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_y_not(struct expr *e, struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_m_not(struct expr *e, struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_y_equals(struct expr *e,
+                                           struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_y_unequals(struct expr *e,
+                                             struct cfdata *data);
+struct pexpr *expr_calculate_pexpr_y_comp(struct expr *e, struct cfdata *data);
+
+/* macro to create a pexpr of type AND */
+struct pexpr *pexpr_and_share(struct pexpr *a, struct pexpr *b,
+                             struct cfdata *data);
+struct pexpr *pexpr_and(struct pexpr *a, struct pexpr *b, struct cfdata *data,
+                       enum pexpr_move move);
+
+/* macro to create a pexpr of type OR */
+struct pexpr *pexpr_or_share(struct pexpr *a, struct pexpr *b,
+                            struct cfdata *data);
+struct pexpr *pexpr_or(struct pexpr *a, struct pexpr *b, struct cfdata *data,
+                      enum pexpr_move move);
+
+/* macro to create a pexpr of type NOT */
+struct pexpr *pexpr_not_share(struct pexpr *a, struct cfdata *data);
+struct pexpr *pexpr_not(struct pexpr *a, struct cfdata *data);
+
+/* check whether a pexpr is in CNF */
+bool pexpr_is_cnf(struct pexpr *e);
+
+/* check whether a pexpr is in NNF */
+bool pexpr_is_nnf(struct pexpr *e);
+
+/* return fexpr_both for a symbol */
+struct pexpr *sym_get_fexpr_both(struct symbol *sym, struct cfdata *data);
+
+/* return fexpr_sel_both for a symbol */
+struct pexpr *sym_get_fexpr_sel_both(struct symbol *sym, struct cfdata *data);
+
+/* create the fexpr of a non-boolean symbol for a specific value */
+struct fexpr *sym_create_nonbool_fexpr(struct symbol *sym, char *value,
+                                      struct cfdata *data);
+
+/*
+ * return the fexpr of a non-boolean symbol for a specific value, NULL if
+ * non-existent
+ */
+struct fexpr *sym_get_nonbool_fexpr(struct symbol *sym, char *value);
+
+/*
+ * return the fexpr of a non-boolean symbol for a specific value, if it exists
+ * otherwise create it
+ */
+struct fexpr *sym_get_or_create_nonbool_fexpr(struct symbol *sym, char *value, struct cfdata *data);
+
+/* macro to construct a pexpr for "A implies B" */
+struct pexpr *pexpr_implies_share(struct pexpr *a, struct pexpr *b, struct cfdata *data);
+struct pexpr *pexpr_implies(struct pexpr *a, struct pexpr *b, struct cfdata *data,
+                           enum pexpr_move move);
+
+/* check, if the fexpr is a symbol, a True/False-constant, a literal symbolising a non-boolean or
+ * a choice symbol
+ */
+bool fexpr_is_symbol(struct fexpr *e);
+
+/* check whether a pexpr is a symbol or a negated symbol */
+bool pexpr_is_symbol(struct pexpr *e);
+
+/* check whether the fexpr is a constant (true/false) */
+bool fexpr_is_constant(struct fexpr *e, struct cfdata *data);
+
+/* add a fexpr to the satmap */
+void fexpr_add_to_satmap(struct fexpr *e, struct cfdata *data);
+
+/* print an fexpr */
+void fexpr_print(char *tag, struct fexpr *e);
+
+/* write an fexpr into a string (format needed for testing) */
+void fexpr_as_char(struct fexpr *e, struct gstr *s);
+
+/* write pn pexpr into a string */
+void pexpr_as_char_short(struct pexpr *e, struct gstr *s, int parent);
+
+/* write an fexpr into a string (format needed for testing) */
+void pexpr_as_char(struct pexpr *e, struct gstr *s, int parent, struct cfdata *data);
+
+/* check whether a pexpr contains a specific fexpr */
+bool pexpr_contains_fexpr(struct pexpr *e, struct fexpr *fe);
+
+/* init list of fexpr */
+struct fexpr_list *fexpr_list_init(void);
+
+/* init list of fexpr_list */
+struct fexl_list *fexl_list_init(void);
+
+/* init list of pexpr */
+struct pexpr_list *pexpr_list_init(void);
+
+/* init list of symbol_fix */
+struct sfix_list *sfix_list_init(void);
+
+/* init list of sfix_list */
+struct sfl_list *sfl_list_init(void);
+
+/* init list of symbol_dvalue */
+struct sdv_list *sdv_list_init(void);
+
+/* init list of symbols */
+struct sym_list *sym_list_init(void);
+
+/* init list of default_maps */
+struct defm_list *defm_list_init(void);
+
+/* init list of properties */
+struct prop_list *prop_list_init(void);
+
+/* add element to tail of a fexpr_list */
+void fexpr_list_add(struct fexpr_list *list, struct fexpr *fe);
+
+/* add element to tail of a fexl_list */
+void fexl_list_add(struct fexl_list *list, struct fexpr_list *elem);
+
+/* add element to tail of a pexpr_list */
+void pexpr_list_add(struct pexpr_list *list, struct pexpr *e);
+
+/* add element to tail of a sfix_list */
+void sfix_list_add(struct sfix_list *list, struct symbol_fix *fix);
+
+/* add element to tail of a sfl_list */
+void sfl_list_add(struct sfl_list *list, struct sfix_list *elem);
+
+/* add element to tail of a sdv_list */
+void sdv_list_add(struct sdv_list *list, struct symbol_dvalue *sdv);
+
+/* add element to tail of a sym_list */
+void sym_list_add(struct sym_list *list, struct symbol *sym);
+
+/* add element to tail of a defm_list */
+void defm_list_add(struct defm_list *list, struct default_map *map);
+
+/* add element to tail of a prop_list */
+void prop_list_add(struct prop_list *list, struct property *prop);
+
+/* delete an element from a fexpr_list */
+void fexpr_list_delete(struct fexpr_list *list, struct fexpr_node *node);
+
+/* delete an element from a fexpr_list */
+void fexl_list_delete(struct fexl_list *list, struct fexl_node *node);
+
+/* delete the first occurrence of elem in an fexl_list */
+void fexl_list_delete_elem(struct fexl_list *list, struct fexpr_list *elem);
+
+/* delete an element from a pexpr_list */
+void pexpr_list_delete(struct pexpr_list *list, struct pexpr_node *node);
+
+/* delete an element from a sfix_list */
+void sfix_list_delete(struct sfix_list *list, struct sfix_node *node);
+
+/* make a shallow copy of a fexpr_list */
+struct fexpr_list *fexpr_list_copy(struct fexpr_list *list);
+
+/* make a shallow copy of a fexpr_list */
+struct fexl_list *fexl_list_copy(struct fexl_list *list);
+
+/* make a shallow copy of a sdv_list */
+struct sdv_list *sdv_list_copy(struct sdv_list *list);
+
+/* make a shallow copy of a sfix_list */
+struct sfix_list *sfix_list_copy(struct sfix_list *list);
+
+/* print a fexpr_list */
+void fexpr_list_print(char *title, struct fexpr_list *list);
+
+/* print a fexl_list */
+void fexl_list_print(char *title, struct fexl_list *list);
+
+/* print a pexpr_list */
+void pexpr_list_print(char *title, struct pexpr_list *list);
+
+/* free an fexpr_list */
+void fexpr_list_free(struct fexpr_list *list);
+
+/* free an pexpr_list (and pexpr_put the elements) */
+void pexpr_list_free_put(struct pexpr_list *list);
+
+/* free an fexl_list */
+void fexl_list_free(struct fexl_list *list);
+
+/* free a sdv_list */
+void sdv_list_free(struct sdv_list *list);
+
+/* free a prop_list */
+void prop_list_free(struct prop_list *list);
+
+/* free a defm_list (and pexpr_put the conditions of the maps) */
+void defm_list_destruct(struct defm_list *list);
+
+/* free a sym_list */
+void sym_list_free(struct sym_list *list);
+
+/* check whether 2 pexpr are equal */
+bool pexpr_eq(struct pexpr *e1, struct pexpr *e2, struct cfdata *data);
+
+/* copy a pexpr */
+struct pexpr *pexpr_deep_copy(const struct pexpr *org);
+
+void pexpr_construct_sym(struct pexpr *e, struct fexpr *left,
+                        unsigned int ref_count);
+void pexpr_construct_not(struct pexpr *e, struct pexpr *left,
+                        unsigned int ref_count);
+void pexpr_construct_and(struct pexpr *e, struct pexpr *left,
+                        struct pexpr *right, unsigned int ref_count);
+void pexpr_construct_or(struct pexpr *e, struct pexpr *left,
+                       struct pexpr *right, unsigned int ref_count);
+
+/* free a pexpr */
+void pexpr_free_depr(struct pexpr *e);
+
+/* give up a reference to e. Also see struct pexpr. */
+void pexpr_put(struct pexpr *e);
+/* Used by PEXPR_PUT(). Not to be used directly. */
+void _pexpr_put_list(struct pexpr **es);
+
+/* acquire a reference to e. Also see struct pexpr. */
+struct pexpr *pexpr_get(struct pexpr *e);
+
+/* print a pexpr  */
+void pexpr_print(char *tag, struct pexpr *e, int prevtoken);
+
+/* convert a fexpr to a pexpr */
+struct pexpr *pexf(struct fexpr *fe);
+
+/* eliminate duplicate and redundant operands */
+struct pexpr *pexpr_eliminate_dups(struct pexpr *e, struct cfdata *data);
+
+#endif
 
--
Quoted 2 lines

2.39.2

--
Best Regards

Masahiro Yamada

#17 lore raw

Re: [PATCH v4 07/12] kconfig: Add files for handling expressions

Quoted 34 lines · Masahiro Yamada
Quoted 23 lines · Ole Schuerks

To translate the Kconfig-model into propositional logic and resolve conflicts, we need to handle propostional formulas. These files contain many functions and macros to deal with propositional formulas.

---
 scripts/kconfig/cf_expr.c | 2594 +++++++++++++++++++++++++++++++++++++
 scripts/kconfig/cf_expr.h |  296 +++++
 2 files changed, 2890 insertions(+)
 create mode 100644 scripts/kconfig/cf_expr.c
 create mode 100644 scripts/kconfig/cf_expr.h

scripts/kconfig/cf_expr.c line 2442

Quoted 4 lines
scripts/kconfig/cf_expr.c new file +4 -0
+/*
+ * convert a fexpr to a pexpr
+ */
+struct pexpr *pexf(struct fexpr *fe)

Not only this one, but more descriptive function name please.

Quoted 4 lines

"pexf", so what?
I do not understand what it is doing from the name.

Could you give us one or two examples of what other functions have bad names? I didn't find anything as bad as pexf() so I'm not sure what the expectations are. Thank you, that would be helpful.

#18 lore raw

PATCH v4 8/12 kconfig: Add files for RangeFix

In reply to Ole Schuerks

The algorithm RangeFix is used to resolve the conflicts. This is the implementation of the algorithm.

---
 scripts/kconfig/cf_rangefix.c | 1066 +++++++++++++++++++++++++++++++++
 scripts/kconfig/cf_rangefix.h |   21 +
 2 files changed, 1087 insertions(+)
 create mode 100644 scripts/kconfig/cf_rangefix.c
 create mode 100644 scripts/kconfig/cf_rangefix.h
scripts/kconfig/cf_rangefix.c new file +1066 -0
new file mode 100644
index 000000000000..bd5229b1d656
@@ -0,0 +1,1066 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023 Patrick Franz <[email protected]>
+ */
+
+#include "cf_defs.h"
+#include "cf_expr.h"
+#define _GNU_SOURCE
+#include <assert.h>
+#include <locale.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "cf_rangefix.h"
+#include "internal.h"
+#include "cf_utils.h"
+
+#define MAX_DIAGNOSES 3
+#define MAX_SECONDS 120
+#define PRINT_UNSAT_CORE true
+#define PRINT_DIAGNOSES false
+#define PRINT_DIAGNOSIS_FOUND true
+#define MINIMISE_DIAGNOSES false
+#define MINIMISE_UNSAT_CORE true
+
+static struct sfl_list *diagnoses_symbol;
+
+static struct fexl_list *generate_diagnoses(PicoSAT *pico, struct cfdata *data);
+
+static void add_fexpr_to_constraint_set(struct fexpr_list *C, struct cfdata *data);
+static void set_assumptions(PicoSAT *pico, struct fexpr_list *c, struct cfdata *data);
+static void fexpr_add_assumption(PicoSAT *pico, struct fexpr *e, int satval);
+static struct fexpr_list *get_unsat_core_soft(PicoSAT *pico, struct cfdata *data);
+static void minimise_unsat_core(PicoSAT *pico, struct fexpr_list *C, struct cfdata *data);
+
+
+static struct fexpr_list *get_difference(struct fexpr_list *C, struct fexpr_list *E0);
+static bool has_intersection(struct fexpr_list *e, struct fexpr_list *X);
+static struct fexpr_list *fexpr_list_union(struct fexpr_list *A, struct fexpr_list *B);
+static struct fexl_list *fexl_list_union(struct fexl_list *A, struct fexl_list *B);
+static bool is_subset_of(struct fexpr_list *A, struct fexpr_list *B);
+static void print_unsat_core(struct fexpr_list *list);
+static bool diagnosis_contains_fexpr(struct fexpr_list *diagnosis, struct fexpr *e);
+static bool diagnosis_contains_symbol(struct sfix_list *diagnosis, struct symbol *sym);
+
+static void print_diagnoses(struct fexl_list *diag);
+static void print_diagnoses_symbol(struct sfl_list *diag_sym);
+
+static struct sfl_list *convert_diagnoses(struct fexl_list *diagnoses, struct cfdata *data);
+static struct sfix_list *convert_diagnosis(struct fexpr_list *diagnosis, struct cfdata *data);
+static struct symbol_fix *symbol_fix_create(struct fexpr *e, enum symbolfix_type type,
+					    struct fexpr_list *diagnosis);
+static struct sfl_list *minimise_diagnoses(PicoSAT *pico, struct fexl_list *diagnoses,
+					   struct cfdata *data);
+
+static tristate calculate_new_tri_val(struct fexpr *e, struct fexpr_list *diagnosis);
+static const char *calculate_new_string_value(struct fexpr *e, struct fexpr_list *diagnosis);
+
+/* count assumptions, only used for debugging */
+static unsigned int nr_of_assumptions = 0, nr_of_assumptions_true;
+
+/* -------------------------------------- */
+
+struct sfl_list *rangefix_run(PicoSAT *pico, struct cfdata *data)
+{
+	clock_t start, end;
+	double time;
+	struct fexl_list *diagnoses;
+	struct fexl_node *node;
+
+	printd("Starting RangeFix...\n");
+	printd("Generating diagnoses...");
+
+	/* generate the diagnoses */
+	start = clock();
+	diagnoses = generate_diagnoses(pico, data);
+	end = clock();
+
+	time = ((double) (end - start)) / CLOCKS_PER_SEC;
+	printd("Generating diagnoses...done. (%.6f secs.)\n", time);
+
+	if (PRINT_DIAGNOSES) {
+		printd("Diagnoses (only for debugging):\n");
+		print_diagnoses(diagnoses);
+		printd("\n");
+	}
+
+	/* convert diagnoses of fexpr to diagnoses of symbols */
+	if (MINIMISE_DIAGNOSES)
+		diagnoses_symbol = minimise_diagnoses(pico, diagnoses, data);
+	else
+		diagnoses_symbol = convert_diagnoses(diagnoses, data);
+
+	printd("\n");
+
+	fexpr_list_for_each(node, diagnoses)
+		fexpr_list_free(node->elem);
+	fexl_list_free(diagnoses);
+
+	return diagnoses_symbol;
+}
+
+/*
+ * generate the diagnoses
+ */
+static struct fexl_list *generate_diagnoses(PicoSAT *pico, struct cfdata *data)
+{
+	struct fexpr_list *C = fexpr_list_init();
+	struct fexl_list *E = fexl_list_init();
+	struct fexl_list *R = fexl_list_init();
+	struct fexpr_list *X, *e, *x_set, *E1, *E2;
+	struct fexl_list *E_R_Union;
+	struct fexpr_list *empty_diagnosis;
+	clock_t start_t, end_t;
+	double time_t;
+
+	/* create constraint set C */
+	add_fexpr_to_constraint_set(C, data);
+
+	if (PRINT_UNSAT_CORE)
+		printd("\n");
+
+	/* init E with an empty diagnosis */
+	empty_diagnosis = fexpr_list_init();
+	fexl_list_add(E, empty_diagnosis);
+
+	/* start the clock */
+	start_t = clock();
+
+	while (E->size > 0) {
+		/* get random diagnosis */
+		struct fexpr_list *E0 = E->head->elem;
+
+		/* calculate C\E0 */
+		struct fexpr_list *c = get_difference(C, E0);
+
+		struct fexl_node *node, *tmp;
+		int res;
+
+		/* set assumptions */
+		nr_of_assumptions = 0;
+		nr_of_assumptions_true = 0;
+		set_assumptions(pico, c, data);
+		fexpr_list_free(c);
+
+		res = picosat_sat(pico, -1);
+
+		if (res == PICOSAT_SATISFIABLE) {
+			if (PRINT_DIAGNOSIS_FOUND && CFDEBUG)
+				fexpr_list_print("DIAGNOSIS FOUND", E0);
+
+			fexl_list_delete(E, E->head);
+			if (E0->size > 0)
+				fexl_list_add(R, E0);
+			else
+				fexpr_list_free(E0);
+
+			if (R->size >= MAX_DIAGNOSES)
+				goto DIAGNOSES_FOUND;
+
+			continue;
+
+		} else if (res == PICOSAT_UNSATISFIABLE) {
+
+		} else if (res == PICOSAT_UNKNOWN) {
+			printd("UNKNOWN\n");
+		} else {
+			perror("Doh.");
+		}
+
+		/* check elapsed time */
+		end_t = clock();
+		time_t = ((double) (end_t - start_t)) / CLOCKS_PER_SEC;
+		if (time_t > (double) MAX_SECONDS)
+			goto DIAGNOSES_FOUND;
+
+		/* abort and return results if cancelled by user */
+		if (stop_rangefix) {
+			stop_rangefix = false;
+			goto DIAGNOSES_FOUND;
+		}
+
+		/* get unsat core from SAT solver */
+		X = get_unsat_core_soft(pico, data);
+
+		/* minimise the unsat core */
+		if (MINIMISE_UNSAT_CORE)
+			minimise_unsat_core(pico, X, data);
+
+		if (PRINT_UNSAT_CORE)
+			print_unsat_core(X);
+
+		for (node = E->head; node != NULL;) {
+			struct fexpr_node *fnode;
+
+			/* get partial diagnosis */
+			e = node->elem;
+
+			/* check, if there is an intersection between e and X
+			 * if there is, go to the next partial diagnosis
+			 */
+			if (has_intersection(e, X)) {
+				node = node->next;
+				continue;
+			}
+
+			/* for each fexpr in the core */
+			fexpr_list_for_each(fnode, X) {
+				struct fexpr *x = fnode->elem;
+				bool E2_subset_of_E1;
+				struct fexl_node *lnode;
+				struct fexl_list *E_without_e;
+
+				/* create {x} */
+				x_set = fexpr_list_init();
+				fexpr_list_add(x_set, x);
+
+				/* create E' = e U {x} */
+				E1 = fexpr_list_union(e, x_set);
+
+				/* create (E\e) U R */
+				E_without_e = fexl_list_copy(E);
+				fexl_list_delete_elem(E_without_e, e);
+				E_R_Union = fexl_list_union(E_without_e, R);
+
+				E2_subset_of_E1 = false;
+
+				/* E" in (E\e) U R */
+				fexl_list_for_each(lnode, E_R_Union) {
+					E2 = lnode->elem;
+
+					/* E" subset of E' ? */
+					if (is_subset_of(E2, E1)) {
+						E2_subset_of_E1 = true;
+						break;
+					}
+				}
+
+				fexl_list_free(E_without_e);
+				fexl_list_free(E_R_Union);
+				fexpr_list_free(x_set);
+
+				/* there exists no E" that is a subset of E' */
+				if (!E2_subset_of_E1)
+					fexl_list_add(E, E1);
+				else
+					fexpr_list_free(E1);
+			}
+
+			fexpr_list_free(e);
+
+			tmp = node->next;
+			fexl_list_delete(E, node);
+			node = tmp;
+		}
+		fexpr_list_free(X);
+	}
+
+	struct fexl_node *node;
+DIAGNOSES_FOUND:
+	fexpr_list_free(C);
+	fexl_list_for_each(node, E)
+		fexpr_list_free(node->elem);
+	fexl_list_free(E);
+
+	return R;
+}
+
+/*
+ * add the fexpr to the constraint set C
+ */
+static void add_fexpr_to_constraint_set(struct fexpr_list *C, struct cfdata *data)
+{
+	struct symbol *sym;
+
+	for_all_symbols(sym) {
+		/* must be a proper symbol */
+		if (sym->type == S_UNKNOWN)
+			continue;
+
+		/* don't need the conflict symbols they are handled separately */
+		if (sym_is_sdv(data->sdv_symbols, sym))
+			continue;
+
+		/* must have a prompt and a name */
+		if (!sym->name || !sym_has_prompt(sym))
+			continue;
+
+		if (sym->type == S_BOOLEAN)
+			fexpr_list_add(C, sym->fexpr_y);
+		else if (sym->type == S_TRISTATE) {
+			fexpr_list_add(C, sym->fexpr_y);
+			fexpr_list_add(C, sym->fexpr_m);
+		} else if (sym->type == S_INT || sym->type == S_HEX || sym->type == S_STRING) {
+			struct fexpr_node *node;
+
+			fexpr_list_for_each(node, sym->nb_vals)
+				fexpr_list_add(C, node->elem);
+		} else {
+			perror("Error adding variables to constraint set C.");
+		}
+	}
+}
+
+/*
+ * check whether the fexpr symbolises the no-value-set fexpr for a non-boolean symbol
+ */
+static bool fexpr_is_novalue(struct fexpr *e)
+{
+	if (!sym_is_nonboolean(e->sym))
+		return false;
+
+	return e == e->sym->nb_vals->head->elem;
+}
+
+static void set_assumptions_sdv(PicoSAT *pico, struct sdv_list *arr)
+{
+	struct symbol_dvalue *sdv;
+	struct sdv_node *node;
+	struct symbol *sym;
+
+	sdv_list_for_each(node, arr) {
+		int lit_y;
+
+		sdv = node->elem;
+		sym = sdv->sym;
+
+		lit_y = sym->fexpr_y->satval;
+
+		if (sym->type == S_BOOLEAN) {
+			switch (sdv->tri) {
+			case yes:
+				picosat_assume(pico, lit_y);
+				sym->fexpr_y->assumption = true;
+				nr_of_assumptions_true++;
+				break;
+			case no:
+				picosat_assume(pico, -lit_y);
+				sym->fexpr_y->assumption = false;
+				break;
+			case mod:
+				perror("Should not happen.\n");
+			}
+			nr_of_assumptions++;
+		} else if (sym->type == S_TRISTATE) {
+			int lit_m = sym->fexpr_m->satval;
+
+			switch (sdv->tri) {
+			case yes:
+				picosat_assume(pico, lit_y);
+				sym->fexpr_y->assumption = true;
+				picosat_assume(pico, -lit_m);
+				sym->fexpr_m->assumption = false;
+				nr_of_assumptions_true++;
+				break;
+			case mod:
+				picosat_assume(pico, -lit_y);
+				sym->fexpr_y->assumption = false;
+				picosat_assume(pico, lit_m);
+				sym->fexpr_m->assumption = true;
+				nr_of_assumptions_true++;
+				break;
+			case no:
+				picosat_assume(pico, -lit_y);
+				sym->fexpr_y->assumption = false;
+				picosat_assume(pico, -lit_m);
+				sym->fexpr_y->assumption = false;
+			}
+			nr_of_assumptions += 2;
+		}
+	}
+}
+
+/*
+ * set the assumptions for the next run of Picosat
+ */
+static void set_assumptions(PicoSAT *pico, struct fexpr_list *c, struct cfdata *data)
+{
+	struct fexpr_node *node;
+
+	fexpr_list_for_each(node, c)
+		fexpr_add_assumption(pico, node->elem, node->elem->satval);
+
+	/* set assumptions for the conflict-symbols */
+	set_assumptions_sdv(pico, data->sdv_symbols);
+}
+
+/*
+ * set the assumtption for a fexpr for the next run of Picosat
+ */
+static void fexpr_add_assumption(PicoSAT *pico, struct fexpr *e, int satval)
+{
+	struct symbol *sym = e->sym;
+
+	if (sym->type == S_BOOLEAN) {
+		int tri_val = sym_get_tristate_value(sym);
+
+		if (tri_val == yes) {
+			picosat_assume(pico, satval);
+			e->assumption = true;
+			nr_of_assumptions_true++;
+		} else {
+			picosat_assume(pico, -satval);
+			e->assumption = false;
+		}
+		nr_of_assumptions++;
+	}
+
+	if (sym->type == S_TRISTATE) {
+		int tri_val = sym_get_tristate_value(sym);
+
+		if (e->tri == yes) {
+			if (tri_val == yes) {
+				picosat_assume(pico, satval);
+				e->assumption = true;
+				nr_of_assumptions_true++;
+			} else {
+				picosat_assume(pico, -satval);
+				e->assumption = false;
+			}
+		} else if (e->tri == mod) {
+			if (tri_val == mod) {
+				picosat_assume(pico, satval);
+				e->assumption = true;
+				nr_of_assumptions_true++;
+			} else {
+				picosat_assume(pico, -satval);
+				e->assumption = false;
+			}
+		}
+		nr_of_assumptions++;
+	}
+
+	if (sym->type == S_INT || sym->type == S_HEX || sym->type == S_STRING) {
+
+		char *string_val = (char *) sym_get_string_value(sym);
+
+		if (sym->type == S_STRING && !strcmp(string_val, ""))
+			return;
+
+		/* check, if e symbolises the no-value-set fexpr */
+		if (fexpr_is_novalue(e)) {
+			if (!sym_nonbool_has_value_set(sym)) {
+				picosat_assume(pico, satval);
+				e->assumption = true;
+				nr_of_assumptions_true++;
+			} else {
+				picosat_assume(pico, -satval);
+				e->assumption = false;
+			}
+		}
+		/* check whena string-symbol has value "" */
+		else if (sym->type == S_STRING && !strcmp(string_val, "")) {
+			if (sym_nonbool_has_value_set(sym)) {
+				picosat_assume(pico, satval);
+				e->assumption = true;
+				nr_of_assumptions_true++;
+			} else {
+				picosat_assume(pico, -satval);
+				e->assumption = false;
+			}
+		} else {
+			if (!strcmp(str_get(&e->nb_val), string_val) &&
+					sym_nonbool_has_value_set(sym)) {
+				picosat_assume(pico, satval);
+				e->assumption = true;
+				nr_of_assumptions_true++;
+			} else {
+				picosat_assume(pico, -satval);
+				e->assumption = false;
+			}
+		}
+		nr_of_assumptions++;
+	}
+}
+
+/*
+ * get the unsatisfiable soft constraints from the last run of Picosat
+ */
+static struct fexpr_list *get_unsat_core_soft(PicoSAT *pico, struct cfdata *data)
+{
+	struct fexpr_list *ret = fexpr_list_init();
+	struct fexpr *e;
+
+	int lit;
+	const int *i = picosat_failed_assumptions(pico);
+
+	lit = abs(*i++);
+
+	while (lit != 0) {
+		e = data->satmap[lit];
+
+		if (!sym_is_sdv(data->sdv_symbols, e->sym))
+			fexpr_list_add(ret, e);
+
+		lit = abs(*i++);
+	}
+
+	return ret;
+}
+
+/*
+ * minimise the unsat core C
+ */
+static void minimise_unsat_core(PicoSAT *pico, struct fexpr_list *C, struct cfdata *data)
+{
+	struct fexpr_list *c_set;
+	struct fexpr_node *node, *tmp;
+
+	/* no need to check further */
+	if (C->size == 1)
+		return;
+
+	for (node = C->head; node != NULL;) {
+		struct fexpr_list *t;
+		int res;
+
+		if (C->size == 1)
+			return;
+
+		/* create C\c */
+		c_set = fexpr_list_init();
+		fexpr_list_add(c_set, node->elem);
+		t = get_difference(C, c_set);
+
+		/* invoke PicoSAT */
+		set_assumptions(pico, t, data);
+
+		res = picosat_sat(pico, -1);
+
+		tmp = node->next;
+
+		if (res == PICOSAT_UNSATISFIABLE)
+			fexpr_list_delete(C, node);
+
+		node = tmp;
+
+		fexpr_list_free(c_set);
+		fexpr_list_free(t);
+	}
+}
+
+
+/*
+ * Calculate C\E0
+ */
+static struct fexpr_list *get_difference(struct fexpr_list *C, struct fexpr_list *E0)
+{
+	struct fexpr_list *ret = fexpr_list_init();
+	struct fexpr_node *node1, *node2;
+	bool found;
+
+	fexpr_list_for_each(node1, C) {
+		found = false;
+		fexpr_list_for_each(node2, E0) {
+			if (node1->elem->satval == node2->elem->satval) {
+				found = true;
+				break;
+			}
+		}
+		if (!found)
+			fexpr_list_add(ret, node1->elem);
+	}
+
+	return ret;
+}
+
+/*
+ * check, if there is an intersection between e and X
+ */
+static bool has_intersection(struct fexpr_list *e, struct fexpr_list *X)
+{
+	struct fexpr_node *node1, *node2;
+
+	fexpr_list_for_each(node1, e)
+		fexpr_list_for_each(node2, X)
+			if (node1->elem->satval == node2->elem->satval)
+				return true;
+
+	return false;
+}
+
+/*
+ * get the union of 2 fexpr_list
+ */
+static struct fexpr_list *fexpr_list_union(struct fexpr_list *A, struct fexpr_list *B)
+{
+	struct fexpr_list *ret = fexpr_list_copy(A);
+	struct fexpr_node *node1, *node2;
+	bool found;
+
+	fexpr_list_for_each(node2, B) {
+		found = false;
+		fexpr_list_for_each(node1, A) {
+			if (node2->elem->satval == node1->elem->satval) {
+				found = true;
+				break;
+			}
+		}
+		if (!found)
+			fexpr_list_add(ret, node2->elem);
+	}
+
+	return ret;
+}
+
+/*
+ * get the union of 2 fexl_list
+ */
+static struct fexl_list *fexl_list_union(struct fexl_list *A, struct fexl_list *B)
+{
+	struct fexl_list *ret = fexl_list_copy(A);
+	struct fexl_node *node1, *node2;
+	bool found;
+
+	fexl_list_for_each(node2, B) {
+		found = false;
+		fexl_list_for_each(node1, A) {
+			if (node2->elem == node1->elem) {
+				found = true;
+				break;
+			}
+		}
+		if (!found)
+			fexl_list_add(ret, node2->elem);
+	}
+
+	return ret;
+}
+
+/*
+ * check, whether A is a subset of B
+ */
+static bool is_subset_of(struct fexpr_list *A, struct fexpr_list *B)
+{
+	struct fexpr_node *node1, *node2;
+	bool found;
+
+	fexpr_list_for_each(node1, A) {
+		found = false;
+		fexpr_list_for_each(node2, B) {
+			if (node1->elem->satval == node2->elem->satval) {
+				found = true;
+				break;
+			}
+		}
+		if (!found)
+			return false;
+	}
+
+	return true;
+}
+
+/*
+ * print an unsat core
+ */
+static void print_unsat_core(struct fexpr_list *list)
+{
+	struct fexpr_node *node;
+
+	printd("Unsat core: [");
+
+	fexpr_list_for_each(node, list) {
+		printd("%s", str_get(&node->elem->name));
+		printd(" <%s>", node->elem->assumption == true ? "T" : "F");
+		if (node->next != NULL)
+			printd(", ");
+	}
+
+	printd("]\n");
+}
+
+
+/*
+ * check if a diagnosis contains a fexpr
+ */
+static bool diagnosis_contains_fexpr(struct fexpr_list *diagnosis, struct fexpr *e)
+{
+	struct fexpr_node *node;
+
+	fexpr_list_for_each(node, diagnosis)
+		if (node->elem->satval == e->satval)
+			return true;
+
+	return false;
+}
+
+/*
+ * check if a diagnosis contains a symbol
+ */
+static bool diagnosis_contains_symbol(struct sfix_list *diagnosis, struct symbol *sym)
+{
+	struct sfix_node *node;
+
+	sfix_list_for_each(node, diagnosis)
+		if (sym == node->elem->sym)
+			return true;
+
+	return false;
+}
+
+/*
+ * print the diagnoses of type fexpr_list
+ */
+static void print_diagnoses(struct fexl_list *diag)
+{
+	struct fexl_node *lnode;
+	unsigned int i = 1;
+
+	fexl_list_for_each(lnode, diag) {
+		struct fexpr_node *node;
+
+		printd("%d: [", i++);
+		fexpr_list_for_each(node, lnode->elem) {
+			char *new_val = node->elem->assumption ? "false" : "true";
+
+			printd("%s => %s", str_get(&node->elem->name), new_val);
+			if (node->next != NULL)
+				printd(", ");
+		}
+		printd("]\n");
+	}
+}
+
+/*
+ * print a single diagnosis of type symbol_fix
+ */
+void print_diagnosis_symbol(struct sfix_list *diag_sym)
+{
+	struct symbol_fix *fix;
+	struct sfix_node *node;
+
+	printd("[");
+
+	sfix_list_for_each(node, diag_sym) {
+		fix = node->elem;
+
+		if (fix->type == SF_BOOLEAN)
+			printd("%s => %s", fix->sym->name, tristate_get_char(fix->tri));
+		else if (fix->type == SF_NONBOOLEAN)
+			printd("%s => %s", fix->sym->name, str_get(&fix->nb_val));
+		else
+			perror("NB not yet implemented.");
+
+		if (node->next != NULL)
+			printd(", ");
+	}
+	printd("]\n");
+}
+
+/*
+ * print the diagnoses of type symbol_fix
+ */
+static void print_diagnoses_symbol(struct sfl_list *diag_sym)
+{
+	struct sfl_node *arr;
+	unsigned int i = 1;
+
+	sfl_list_for_each(arr, diag_sym) {
+		printd("%d: ", i++);
+		print_diagnosis_symbol(arr->elem);
+	}
+}
+
+/*
+ * convert a single diagnosis of fexpr into a diagnosis of symbols
+ */
+static struct sfix_list *convert_diagnosis(struct fexpr_list *diagnosis, struct cfdata *data)
+{
+	struct sfix_list *diagnosis_symbol = sfix_list_init();
+	struct fexpr *e;
+	struct symbol_fix *fix;
+	struct symbol_dvalue *sdv;
+	struct sdv_node *snode;
+	struct fexpr_node *fnode;
+
+	/* set the values for the conflict symbols */
+	sdv_list_for_each(snode, data->sdv_symbols) {
+		sdv = snode->elem;
+		fix = xcalloc(1, sizeof(*fix));
+		fix->sym = sdv->sym;
+		fix->type = SF_BOOLEAN;
+		fix->tri = sdv->tri;
+		sfix_list_add(diagnosis_symbol, fix);
+	}
+
+	fexpr_list_for_each(fnode, diagnosis) {
+		enum symbolfix_type type;
+
+		e = fnode->elem;
+
+		/* diagnosis already contains symbol, so continue */
+		if (diagnosis_contains_symbol(diagnosis_symbol, e->sym))
+			continue;
+
+		if (sym_is_boolean(e->sym))
+			type = SF_BOOLEAN;
+		else if (sym_is_nonboolean(e->sym))
+			type = SF_NONBOOLEAN;
+		else
+			type = SF_DISALLOWED;
+		fix = symbol_fix_create(e, type, diagnosis);
+
+		sfix_list_add(diagnosis_symbol, fix);
+	}
+
+	return diagnosis_symbol;
+}
+
+/*
+ * convert the diagnoses of fexpr into diagnoses of symbols
+ * it is easier to handle symbols when applying fixes
+ */
+static struct sfl_list *convert_diagnoses(struct fexl_list *diag_arr, struct cfdata *data)
+{
+	struct fexl_node *lnode;
+
+	diagnoses_symbol = sfl_list_init();
+
+	fexl_list_for_each(lnode, diag_arr) {
+		struct sfix_list *fix = convert_diagnosis(lnode->elem, data);
+
+		sfl_list_add(diagnoses_symbol, fix);
+	}
+
+	return diagnoses_symbol;
+}
+
+/*
+ * create a symbol_fix given a fexpr
+ */
+static struct symbol_fix *symbol_fix_create(struct fexpr *e, enum symbolfix_type type,
+					    struct fexpr_list *diagnosis)
+{
+	struct symbol_fix *fix = malloc(sizeof(struct symbol_fix));
+
+	fix->sym = e->sym;
+	fix->type = type;
+
+	switch (type) {
+	case SF_BOOLEAN:
+		fix->tri = calculate_new_tri_val(e, diagnosis);
+		break;
+	case SF_NONBOOLEAN:
+		fix->nb_val = str_new();
+		str_append(&fix->nb_val, calculate_new_string_value(e, diagnosis));
+		break;
+	default:
+		perror("Illegal symbolfix_type.\n");
+	}
+
+	return fix;
+}
+
+/*
+ * remove symbols from the diagnosis, which will be set automatically:
+ * 1. symbol gets selected
+ * 2. choice symbol gets enabled/disabled automatically
+ * 3. symbol uses a default value
+ */
+static struct sfl_list *minimise_diagnoses(PicoSAT *pico, struct fexl_list *diagnoses,
+					   struct cfdata *data)
+{
+	clock_t start, end;
+	double time;
+	struct fexpr_list *d;
+	struct sfix_list *diagnosis_symbol;
+	struct sfl_list *diagnoses_symbol = sfl_list_init();
+	struct fexpr *e;
+	int satval, deref = 0;
+	struct symbol_fix *fix;
+	struct fexl_node *flnode;
+	struct fexpr_list *C;
+
+	printd("Minimising diagnoses...");
+
+	start = clock();
+
+	/* create soft constraint set C */
+	C = fexpr_list_init();
+	add_fexpr_to_constraint_set(C, data);
+
+	fexl_list_for_each(flnode, diagnoses) {
+		struct fexpr_node *fnode;
+		struct sfix_node *snode;
+		struct fexpr_list *C_without_d;
+		int res;
+
+		d = flnode->elem;
+
+		/* set assumptions for those symbols that don't need to be changed */
+		C_without_d = get_difference(C, d);
+		set_assumptions(pico, C_without_d, data);
+		fexpr_list_free(C_without_d);
+		fexpr_list_free(C);
+
+
+		/* flip the assumptions from the diagnosis */
+		fexpr_list_for_each(fnode, d) {
+			e = fnode->elem;
+			satval = e->assumption ? -(e->satval) : e->satval;
+			picosat_assume(pico, satval);
+		}
+
+		res = picosat_sat(pico, -1);
+		if (res != PICOSAT_SATISFIABLE)
+			perror("Diagnosis not satisfiable (minimise).");
+
+		diagnosis_symbol = convert_diagnosis(d, data);
+
+		/* check if symbol gets selected */
+		for (snode = diagnosis_symbol->head; snode != NULL;) {
+			fix = snode->elem;
+
+			/* symbol is never selected, continue */
+			if (!fix->sym->fexpr_sel_y) {
+				snode = snode->next;
+				continue;
+			}
+
+			/* check, whether the symbol was selected anyway */
+			if (fix->sym->type == S_BOOLEAN && fix->tri == yes)
+				deref = picosat_deref(pico, fix->sym->fexpr_sel_y->satval);
+			else if (fix->sym->type == S_TRISTATE && fix->tri == yes)
+				deref = picosat_deref(pico, fix->sym->fexpr_sel_y->satval);
+			else if (fix->sym->type == S_TRISTATE && fix->tri == mod)
+				deref = picosat_deref(pico, fix->sym->fexpr_sel_m->satval);
+
+			if (deref == 1) {
+				struct sfix_node *tmp = snode->next;
+
+				sfix_list_delete(diagnosis_symbol, snode);
+				snode = tmp;
+			} else {
+				deref = 0;
+				snode = snode->next;
+			}
+		}
+		sfl_list_add(diagnoses_symbol, diagnosis_symbol);
+	}
+
+	end = clock();
+	time = ((double) (end - start)) / CLOCKS_PER_SEC;
+
+	printd("done. (%.6f secs.)\n", time);
+
+	return diagnoses_symbol;
+}
+
+/*
+ * list the diagnoses and let user choose a diagnosis to be applied
+ */
+struct sfix_list *choose_fix(struct sfl_list *diag)
+{
+	int choice;
+	struct sfl_node *node;
+
+	printd("=== GENERATED DIAGNOSES ===\n");
+	printd("0: No changes wanted\n");
+	print_diagnoses_symbol(diag);
+
+	printd("\n> Choose option: ");
+	scanf("%d", &choice);
+
+	/* no changes wanted */
+	if (choice == 0)
+		return NULL;
+
+	/* invalid choice */
+	if (choice > diag->size)
+		return NULL;
+
+	node = diag->head;
+	for (int counter = 1; counter < choice; counter++)
+		node = node->next;
+
+	return node->elem;
+}
+
+
+/*
+ * calculate the new value for a boolean symbol given a diagnosis and an fexpr
+ */
+static tristate calculate_new_tri_val(struct fexpr *e, struct fexpr_list *diagnosis)
+{
+	assert(sym_is_boolean(e->sym));
+
+	/* return the opposite of the last assumption for booleans */
+	if (e->sym->type == S_BOOLEAN)
+		return e->assumption ? no : yes;
+
+	/* new values for tristate must be deduced from the diagnosis */
+	if (e->sym->type == S_TRISTATE) {
+		/* fexpr_y */
+		if (e->tri == yes) {
+			if (e->assumption == true)
+				/*
+				 * if diagnosis contains fexpr_m, fexpr_m was false
+				 * => new value is mod
+				 */
+				return diagnosis_contains_fexpr(
+						diagnosis, e->sym->fexpr_m) ? mod : no;
+			else if (e->assumption == false)
+				/*
+				 * if fexpr_y is set to true, the new value must be yes
+				 */
+				return yes;
+		}
+		/* fexpr_m */
+		if (e->tri == mod) {
+			if (e->assumption == true)
+				/*
+				 * if diagnosis contains fexpr_y, fexpr_y was false
+				 * => new value is yes
+				 */
+				return diagnosis_contains_fexpr(
+						diagnosis, e->sym->fexpr_m) ? yes : no;
+			else if (e->assumption == false)
+				/* if diagnosis contains fexpr_m, the new value must be mod */
+				return mod;
+		}
+		perror("Should not get here.\n");
+	}
+
+	perror("Error calculating new tristate value.\n");
+	return no;
+}
+
+/*
+ * calculate the new value for a non-boolean symbol given a diagnosis and an fexpr
+ */
+static const char *calculate_new_string_value(struct fexpr *e, struct fexpr_list *diagnosis)
+{
+	struct fexpr_node *node;
+	struct fexpr *e2;
+
+	assert(sym_is_nonboolean(e->sym));
+
+	/* if assumption was false before, this is the new value because only 1
+	 * variable can be true
+	 */
+	if (e->assumption == false)
+		return str_get(&e->nb_val);
+
+	/* a diagnosis always contains 2 variables for the same non-boolean symbol
+	 * one is set to true, the other to false
+	 * otherwise you'd set 2 variables to true, which is not allowed
+	 */
+	fexpr_list_for_each(node, diagnosis) {
+		e2 = node->elem;
+
+		/* not interested in other symbols or the same fexpr */
+		if (e->sym != e2->sym || e->satval == e2->satval)
+			continue;
+
+		return str_get(&e2->nb_val);
+	}
+
+	perror("Error calculating new string value.\n");
+	return "";
+}
scripts/kconfig/cf_rangefix.h new file +21 -0
new file mode 100644
index 000000000000..3de0b7811487
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023 Patrick Franz <[email protected]>
+ */
+
+#ifndef CF_RANGEFIX_H
+#define CF_RANGEFIX_H
+
+#include "picosat.h"
+#include "cf_defs.h"
+
+/* initialize RangeFix and return the diagnoses */
+struct sfl_list *rangefix_run(PicoSAT *pico, struct cfdata *data);
+
+/* ask user which fix to apply */
+struct sfix_list *choose_fix(struct sfl_list *diag);
+
+/* print a single diagnosis of type symbol_fix */
+void print_diagnosis_symbol(struct sfix_list *diag_sym);
+
+#endif
Signature · 1 line
2.39.2
#19 lore raw

PATCH v4 9/12 kconfig: Add files with utility functions

In reply to Ole Schuerks

This commit contains various helper functions used in the project.

---
 scripts/kconfig/cf_utils.c | 1031 ++++++++++++++++++++++++++++++++++++
 scripts/kconfig/cf_utils.h |  115 ++++
 2 files changed, 1146 insertions(+)
 create mode 100644 scripts/kconfig/cf_utils.c
 create mode 100644 scripts/kconfig/cf_utils.h
scripts/kconfig/cf_utils.c new file +1031 -0
new file mode 100644
index 000000000000..bcffd0a4fc1b
@@ -0,0 +1,1031 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023 Patrick Franz <[email protected]>
+ */
+
+#define _GNU_SOURCE
+#include <assert.h>
+#include <locale.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <ctype.h>
+
+#include "configfix.h"
+#include "internal.h"
+
+#define SATMAP_INIT_SIZE 2
+
+static PicoSAT *pico;
+
+static void unfold_cnf_clause(struct pexpr *e);
+static void build_cnf_tseytin(struct pexpr *e, struct cfdata *data);
+
+static void build_cnf_tseytin_top_and(struct pexpr *e, struct cfdata *data);
+static void build_cnf_tseytin_top_or(struct pexpr *e, struct cfdata *data);
+
+static void build_cnf_tseytin_tmp(struct pexpr *e, struct fexpr *t, struct cfdata *data);
+static void build_cnf_tseytin_and(struct pexpr *e, struct fexpr *t, struct cfdata *data);
+static void build_cnf_tseytin_or(struct pexpr *e, struct fexpr *t, struct cfdata *data);
+static int pexpr_satval(struct pexpr *e);
+
+/*
+ * parse Kconfig-file and read .config
+ */
+void init_config(const char *Kconfig_file)
+{
+	conf_parse(Kconfig_file);
+	conf_read(NULL);
+}
+
+/*
+ * initialize satmap
+ */
+void init_data(struct cfdata *data)
+{
+	/* create hashtable with all fexpr */
+	data->satmap = xcalloc(SATMAP_INIT_SIZE, sizeof(**data->satmap));
+	data->satmap_size = SATMAP_INIT_SIZE;
+
+	printd("done.\n");
+}
+
+/*
+ * create SAT-variables for all fexpr
+ */
+void create_sat_variables(struct cfdata *data)
+{
+	struct symbol *sym;
+
+	printd("Creating SAT-variables...");
+
+	for_all_symbols(sym) {
+		sym->constraints = pexpr_list_init();
+		sym_create_fexpr(sym, data);
+	}
+
+	printd("done.\n");
+}
+
+/*
+ * create various constants
+ */
+void create_constants(struct cfdata *data)
+{
+	printd("Creating constants...");
+
+	/* create TRUE and FALSE constants */
+	data->constants->const_false = fexpr_create(data->sat_variable_nr++, FE_FALSE, "False");
+	// const_false = fexpr_create(sat_variable_nr++, FE_FALSE, "False");
+	fexpr_add_to_satmap(data->constants->const_false, data);
+
+	data->constants->const_true = fexpr_create(data->sat_variable_nr++, FE_TRUE, "True");
+	fexpr_add_to_satmap(data->constants->const_true, data);
+
+	/* add fexpr of constants to tristate constants */
+	symbol_yes.fexpr_y = data->constants->const_true;
+	symbol_yes.fexpr_m = data->constants->const_false;
+
+	symbol_mod.fexpr_y = data->constants->const_false;
+	symbol_mod.fexpr_m = data->constants->const_true;
+
+	symbol_no.fexpr_y = data->constants->const_false;
+	symbol_no.fexpr_m = data->constants->const_false;
+
+	/* create symbols yes/mod/no as fexpr */
+	data->constants->symbol_yes_fexpr = fexpr_create(0, FE_SYMBOL, "y");
+	data->constants->symbol_yes_fexpr->sym = &symbol_yes;
+	data->constants->symbol_yes_fexpr->tri = yes;
+
+	data->constants->symbol_mod_fexpr = fexpr_create(0, FE_SYMBOL, "m");
+	data->constants->symbol_mod_fexpr->sym = &symbol_mod;
+	data->constants->symbol_mod_fexpr->tri = mod;
+
+	data->constants->symbol_no_fexpr = fexpr_create(0, FE_SYMBOL, "n");
+	data->constants->symbol_no_fexpr->sym = &symbol_no;
+	data->constants->symbol_no_fexpr->tri = no;
+
+	printd("done.\n");
+}
+
+/*
+ * create a temporary SAT-variable
+ */
+struct fexpr *create_tmpsatvar(struct cfdata *data)
+{
+	char *name = get_tmp_var_as_char(data->tmp_variable_nr);
+	struct fexpr *t = fexpr_create(data->sat_variable_nr++, FE_TMPSATVAR, name);
+
+	++data->tmp_variable_nr;
+	fexpr_add_to_satmap(t, data);
+
+	free(name);
+	return t;
+}
+
+/*
+ * return a temporary SAT variable as string
+ */
+char *get_tmp_var_as_char(int i)
+{
+	char *val = malloc(sizeof(char) * 18);
+
+	snprintf(val, 18, "T_%d", i);
+	return val;
+}
+
+/*
+ * return a tristate value as a char *
+ */
+char *tristate_get_char(tristate val)
+{
+	switch (val) {
+	case yes:
+		return "yes";
+	case mod:
+		return "mod";
+	case no:
+		return "no";
+	default:
+		return "";
+	}
+}
+
+/*
+ *check whether an expr can evaluate to mod
+ */
+bool expr_can_evaluate_to_mod(struct expr *e)
+{
+	if (!e)
+		return false;
+
+	switch (e->type) {
+	case E_SYMBOL:
+		return e->left.sym == &symbol_mod || e->left.sym->type == S_TRISTATE ? true : false;
+	case E_AND:
+	case E_OR:
+		return expr_can_evaluate_to_mod(e->left.expr) ||
+		       expr_can_evaluate_to_mod(e->right.expr);
+	case E_NOT:
+		return expr_can_evaluate_to_mod(e->left.expr);
+	default:
+		return false;
+	}
+}
+
+/*
+ * check whether an expr is a non-Boolean constant
+ */
+bool expr_is_nonbool_constant(struct expr *e)
+{
+	if (e->type != E_SYMBOL)
+		return false;
+	if (e->left.sym->type != S_UNKNOWN)
+		return false;
+
+	if (e->left.sym->flags & SYMBOL_CONST)
+		return true;
+
+	return string_is_number(e->left.sym->name) || string_is_hex(e->left.sym->name);
+}
+
+/*
+ * check whether a symbol is a non-Boolean constant
+ */
+bool sym_is_nonbool_constant(struct symbol *sym)
+{
+	if (sym->type != S_UNKNOWN)
+		return false;
+
+	if (sym->flags & SYMBOL_CONST)
+		return true;
+
+	return string_is_number(sym->name) || string_is_hex(sym->name);
+}
+
+/*
+ * print an expr
+ */
+static void print_expr_util(struct expr *e, int prevtoken)
+{
+	if (!e)
+		return;
+
+	switch (e->type) {
+	case E_SYMBOL:
+		if (sym_get_name(e->left.sym) != NULL)
+			printf("%s", sym_get_name(e->left.sym));
+		else
+			printf("left was null\n");
+		break;
+	case E_NOT:
+		printf("!");
+		print_expr_util(e->left.expr, E_NOT);
+		break;
+	case E_AND:
+		if (prevtoken != E_AND && prevtoken != 0)
+			printf("(");
+		print_expr_util(e->left.expr, E_AND);
+		printf(" && ");
+		print_expr_util(e->right.expr, E_AND);
+		if (prevtoken != E_AND && prevtoken != 0)
+			printf(")");
+		break;
+	case E_OR:
+		if (prevtoken != E_OR && prevtoken != 0)
+			printf("(");
+		print_expr_util(e->left.expr, E_OR);
+		printf(" || ");
+		print_expr_util(e->right.expr, E_OR);
+		if (prevtoken != E_OR && prevtoken != 0)
+			printf(")");
+		break;
+	case E_EQUAL:
+	case E_UNEQUAL:
+		if (e->left.sym->name)
+			printf("%s", e->left.sym->name);
+		else
+			printf("left was null\n");
+		printf("%s", e->type == E_EQUAL ? "=" : "!=");
+		printf("%s", e->right.sym->name);
+		break;
+	case E_LEQ:
+	case E_LTH:
+		if (e->left.sym->name)
+			printf("%s", e->left.sym->name);
+		else
+			printf("left was null\n");
+		printf("%s", e->type == E_LEQ ? "<=" : "<");
+		printf("%s", e->right.sym->name);
+		break;
+	case E_GEQ:
+	case E_GTH:
+		if (e->left.sym->name)
+			printf("%s", e->left.sym->name);
+		else
+			printf("left was null\n");
+		printf("%s", e->type == E_GEQ ? ">=" : ">");
+		printf("%s", e->right.sym->name);
+		break;
+	case E_RANGE:
+		printf("[");
+		printf("%s", e->left.sym->name);
+		printf(" ");
+		printf("%s", e->right.sym->name);
+		printf("]");
+		break;
+	default:
+		break;
+	}
+}
+void print_expr(char *tag, struct expr *e, int prevtoken)
+{
+	printf("%s ", tag);
+	print_expr_util(e, prevtoken);
+	printf("\n");
+}
+
+/*
+ * check, if the symbol is a tristate-constant
+ */
+bool sym_is_tristate_constant(struct symbol *sym)
+{
+	return sym == &symbol_yes || sym == &symbol_mod || sym == &symbol_no;
+}
+
+/*
+ * check, if a symbol is of type boolean or tristate
+ */
+bool sym_is_boolean(struct symbol *sym)
+{
+	return sym->type == S_BOOLEAN || sym->type == S_TRISTATE;
+}
+
+/*
+ * check, if a symbol is a boolean/tristate or a tristate constant
+ */
+bool sym_is_bool_or_triconst(struct symbol *sym)
+{
+	return sym_is_tristate_constant(sym) || sym_is_boolean(sym);
+}
+
+/*
+ * check, if a symbol is of type int, hex, or string
+ */
+bool sym_is_nonboolean(struct symbol *sym)
+{
+	return sym->type == S_INT || sym->type == S_HEX || sym->type == S_STRING;
+}
+
+/*
+ * check, if a symbol has a prompt
+ */
+bool sym_has_prompt(struct symbol *sym)
+{
+	struct property *prop;
+
+	for_all_prompts(sym, prop)
+		return true;
+
+	return false;
+}
+
+/*
+ * return the prompt of the symbol if there is one, NULL otherwise
+ */
+struct property *sym_get_prompt(struct symbol *sym)
+{
+	struct property *prop;
+
+	for_all_prompts(sym, prop)
+		return prop;
+
+	return NULL;
+}
+
+/*
+ * return the condition for the property, NULL if there is none. To be pexpr_put
+ * by caller.
+ */
+struct pexpr *prop_get_condition(struct property *prop, struct cfdata *data)
+{
+	if (prop == NULL)
+		return NULL;
+
+	/* if there is no condition, return True */
+	if (!prop->visible.expr)
+		return pexf(data->constants->const_true);
+
+	return expr_calculate_pexpr_both(prop->visible.expr, data);
+}
+
+/*
+ * return the default property, NULL if none exists or can be satisfied
+ */
+struct property *sym_get_default_prop(struct symbol *sym)
+{
+	struct property *prop;
+
+	for_all_defaults(sym, prop) {
+		prop->visible.tri = expr_calc_value(prop->visible.expr);
+		if (prop->visible.tri != no)
+			return prop;
+	}
+	return NULL;
+}
+
+/*
+ * check whether a non-boolean symbol has a value set
+ */
+bool sym_nonbool_has_value_set(struct symbol *sym)
+{
+	/*
+	 * The built constraints make the following constraints:
+	 *
+	 * visible -> not 'n'
+	 * sym->dir_dep not fulfilled -> 'n'
+	 * invisible -> (no default's condition is fulfilled <-> 'n')
+	 */
+	struct property *prompt;
+	struct property *p;
+
+	if (!sym_is_nonboolean(sym))
+		return false;
+
+	/* cannot have a value with unmet dependencies */
+	if (sym->dir_dep.expr && sym->dir_dep.tri == no)
+		return false;
+
+	/* visible prompt => value set */
+	prompt = sym_get_prompt(sym);
+	if (prompt != NULL && prompt->visible.tri != no)
+		return true;
+
+	/* invisible prompt => must get value from default value */
+	p = sym_get_default_prop(sym);
+	return p != NULL;
+}
+
+/*
+ * return pointer to the name of the symbol or the current prompt-text, if it
+ * is a choice symbol
+ */
+const char *sym_get_name(struct symbol *sym)
+{
+	if (sym_is_choice(sym)) {
+		struct property *prompt = sym_get_prompt(sym);
+
+		if (prompt == NULL)
+			return "";
+
+		return prompt->text;
+	} else {
+		return sym->name;
+	}
+}
+
+/*
+ * check whether symbol is to be changed
+ */
+bool sym_is_sdv(struct sdv_list *list, struct symbol *sym)
+{
+	struct sdv_node *node;
+
+	sdv_list_for_each(node, list)
+		if (sym == node->elem->sym)
+			return true;
+
+	return false;
+}
+
+/*
+ * print a symbol's name
+ */
+void print_sym_name(struct symbol *sym)
+{
+	printf("Symbol: ");
+	if (sym_is_choice(sym)) {
+		struct property *prompt = sym_get_prompt(sym);
+
+		printf("(Choice) %s", prompt->text);
+	} else  {
+		printf("%s", sym->name);
+	}
+	printf("\n");
+}
+
+/*
+ * print all constraints for a symbol
+ */
+void print_sym_constraint(struct symbol *sym)
+{
+	struct pexpr_node *node;
+
+	pexpr_list_for_each(node, sym->constraints)
+		pexpr_print("::", node->elem, -1);
+}
+
+/*
+ * print a default map
+ */
+void print_default_map(struct defm_list *map)
+{
+	struct default_map *entry;
+	struct defm_node *node;
+
+	defm_list_for_each(node, map) {
+		struct gstr s = str_new();
+
+		entry = node->elem;
+
+		str_append(&s, "\t");
+		str_append(&s, str_get(&entry->val->name));
+		str_append(&s, " ->");
+		pexpr_print(strdup(str_get(&s)), entry->e, -1);
+		str_free(&s);
+	}
+}
+
+/*
+ * check whether a string is a number
+ */
+bool string_is_number(char *s)
+{
+	int len = strlen(s);
+	int i = 0;
+
+	while (i < len) {
+		if (!isdigit(s[i]))
+			return false;
+		i++;
+	}
+
+	return true;
+}
+
+/*
+ * check whether a string is a hexadecimal number
+ */
+bool string_is_hex(char *s)
+{
+	int len = strlen(s);
+	int i = 2;
+
+	if (len >= 3 && s[0] == '0' && s[1] == 'x') {
+		while (i < len) {
+			if (!isxdigit(s[i]))
+				return false;
+			i++;
+		}
+		return true;
+	} else {
+		return false;
+	}
+}
+
+/*
+ * initialize PicoSAT
+ */
+PicoSAT *initialize_picosat(void)
+{
+	PicoSAT *pico;
+
+	printd("\nInitializing PicoSAT...");
+	pico = picosat_init();
+	picosat_enable_trace_generation(pico);
+	printd("done.\n");
+
+	return pico;
+}
+
+/*
+ * construct the CNF-clauses from the constraints
+ */
+void construct_cnf_clauses(PicoSAT *p, struct cfdata *data)
+{
+	struct symbol *sym;
+
+	pico = p;
+
+	/* adding unit-clauses for constants */
+	sat_add_clause(2, pico, -(data->constants->const_false->satval));
+	sat_add_clause(2, pico, data->constants->const_true->satval);
+
+	for_all_symbols(sym) {
+		struct pexpr_node *node;
+
+		if (sym->type == S_UNKNOWN)
+			continue;
+
+		pexpr_list_for_each(node, sym->constraints) {
+			if (pexpr_is_cnf(node->elem)) {
+				unfold_cnf_clause(node->elem);
+				picosat_add(pico, 0);
+			} else {
+				build_cnf_tseytin(node->elem, data);
+			}
+
+		}
+	}
+}
+
+/*
+ * helper function to add an expression to a CNF-clause
+ */
+static void unfold_cnf_clause(struct pexpr *e)
+{
+	switch (e->type) {
+	case PE_SYMBOL:
+		picosat_add(pico, e->left.fexpr->satval);
+		break;
+	case PE_OR:
+		unfold_cnf_clause(e->left.pexpr);
+		unfold_cnf_clause(e->right.pexpr);
+		break;
+	case PE_NOT:
+		picosat_add(pico, -(e->left.pexpr->left.fexpr->satval));
+		break;
+	default:
+		perror("Not in CNF, FE_EQUALS.");
+	}
+}
+
+/*
+ * build CNF-clauses for a pexpr not in CNF
+ */
+static void build_cnf_tseytin(struct pexpr *e, struct cfdata *data)
+{
+	switch (e->type) {
+	case PE_AND:
+		build_cnf_tseytin_top_and(e, data);
+		break;
+	case PE_OR:
+		build_cnf_tseytin_top_or(e, data);
+		break;
+	default:
+		perror("Expression not a propositional logic formula. root.");
+	}
+}
+
+/*
+ * split up a pexpr of type AND as both sides must be satisfied
+ */
+static void build_cnf_tseytin_top_and(struct pexpr *e, struct cfdata *data)
+{
+	if (pexpr_is_cnf(e->left.pexpr))
+		unfold_cnf_clause(e->left.pexpr);
+	else
+		build_cnf_tseytin(e->left.pexpr, data);
+
+	if (pexpr_is_cnf(e->right.pexpr))
+		unfold_cnf_clause(e->right.pexpr);
+	else
+		build_cnf_tseytin(e->right.pexpr, data);
+
+}
+
+static void build_cnf_tseytin_top_or(struct pexpr *e, struct cfdata *data)
+{
+	struct fexpr *t1 = NULL, *t2 = NULL;
+	int a, b;
+
+	/* set left side */
+	if (pexpr_is_symbol(e->left.pexpr)) {
+		a = pexpr_satval(e->left.pexpr);
+	} else {
+		t1 = create_tmpsatvar(data);
+		a = t1->satval;
+	}
+
+	/* set right side */
+	if (pexpr_is_symbol(e->right.pexpr)) {
+		b = pexpr_satval(e->right.pexpr);
+	} else {
+		t2 = create_tmpsatvar(data);
+		b = t2->satval;
+	}
+
+	/* A v B */
+	sat_add_clause(3, pico, a, b);
+
+	/* traverse down the tree to build more constraints if needed */
+	if (!pexpr_is_symbol(e->left.pexpr)) {
+		if (t1 == NULL)
+			perror("t1 is NULL.");
+
+		build_cnf_tseytin_tmp(e->left.pexpr, t1, data);
+	}
+
+	if (!pexpr_is_symbol(e->right.pexpr)) {
+		if (t2 == NULL)
+			perror("t2 is NULL.");
+
+		build_cnf_tseytin_tmp(e->right.pexpr, t2, data);
+	}
+}
+
+/*
+ * build the sub-expressions
+ */
+static void build_cnf_tseytin_tmp(struct pexpr *e, struct fexpr *t, struct cfdata *data)
+{
+	switch (e->type) {
+	case PE_AND:
+		build_cnf_tseytin_and(e, t, data);
+		break;
+	case PE_OR:
+		build_cnf_tseytin_or(e, t, data);
+		break;
+	default:
+		perror("Expression not a propositional logic formula. root.");
+	}
+}
+
+/*
+ * build the Tseytin sub-expressions for a pexpr of type AND
+ */
+static void build_cnf_tseytin_and(struct pexpr *e, struct fexpr *t, struct cfdata *data)
+{
+	struct fexpr *t1 = NULL, *t2 = NULL;
+	int a, b, c;
+
+	assert(t != NULL);
+
+	/* set left side */
+	if (pexpr_is_symbol(e->left.pexpr)) {
+		a = pexpr_satval(e->left.pexpr);
+	} else {
+		t1 = create_tmpsatvar(data);
+		a = t1->satval;
+	}
+
+	/* set right side */
+	if (pexpr_is_symbol(e->right.pexpr)) {
+		b = pexpr_satval(e->right.pexpr);
+	} else {
+		t2 = create_tmpsatvar(data);
+		b = t2->satval;
+	}
+
+	c = t->satval;
+
+	/* -A v -B v C */
+	sat_add_clause(4, pico, -a, -b, c);
+	/* A v -C */
+	sat_add_clause(3, pico, a, -c);
+	/* B v -C */
+	sat_add_clause(3, pico, b, -c);
+
+	/* traverse down the tree to build more constraints if needed */
+	if (!pexpr_is_symbol(e->left.pexpr)) {
+		if (t1 == NULL)
+			perror("t1 is NULL.");
+
+		build_cnf_tseytin_tmp(e->left.pexpr, t1, data);
+	}
+	if (!pexpr_is_symbol(e->right.pexpr)) {
+		if (t2 == NULL)
+			perror("t2 is NULL.");
+
+		build_cnf_tseytin_tmp(e->right.pexpr, t2, data);
+	}
+}
+
+/*
+ * build the Tseytin sub-expressions for a pexpr of type
+ */
+static void build_cnf_tseytin_or(struct pexpr *e, struct fexpr *t, struct cfdata *data)
+{
+	struct fexpr *t1 = NULL, *t2 = NULL;
+	int a, b, c;
+
+	assert(t != NULL);
+
+	/* set left side */
+	if (pexpr_is_symbol(e->left.pexpr)) {
+		a = pexpr_satval(e->left.pexpr);
+	} else {
+		t1 = create_tmpsatvar(data);
+		a = t1->satval;
+	}
+
+	/* set right side */
+	if (pexpr_is_symbol(e->right.pexpr)) {
+		b = pexpr_satval(e->right.pexpr);
+	} else {
+		t2 = create_tmpsatvar(data);
+		b = t2->satval;
+	}
+
+	c = t->satval;
+
+	/* A v B v -C */
+	sat_add_clause(4, pico, a, b, -c);
+	/* -A v C */;
+	sat_add_clause(3, pico, -a, c);
+	/* -B v C */
+	sat_add_clause(3, pico, -b, c);
+
+	/* traverse down the tree to build more constraints if needed */
+	if (!pexpr_is_symbol(e->left.pexpr)) {
+		if (t1 == NULL)
+			perror("t1 is NULL.");
+
+		build_cnf_tseytin_tmp(e->left.pexpr, t1, data);
+	}
+	if (!pexpr_is_symbol(e->right.pexpr)) {
+		if (t2 == NULL)
+			perror("t2 is NULL.");
+
+		build_cnf_tseytin_tmp(e->right.pexpr, t2, data);
+	}
+}
+
+/*
+ * add a clause to PicoSAT
+ * First argument must be the SAT solver
+ */
+void sat_add_clause(int num, ...)
+{
+	va_list valist;
+	int lit;
+	PicoSAT *pico;
+
+	if (num <= 1)
+		return;
+
+	va_start(valist, num);
+
+	pico = va_arg(valist, PicoSAT *);
+
+	/* access all the arguments assigned to valist */
+	for (int i = 1; i < num; i++) {
+		lit = va_arg(valist, int);
+		picosat_add(pico, lit);
+	}
+	picosat_add(pico, 0);
+
+	va_end(valist);
+}
+
+/*
+ * return the SAT-variable for a pexpr that is a symbol
+ */
+static int pexpr_satval(struct pexpr *e)
+{
+	if (!pexpr_is_symbol(e)) {
+		perror("pexpr is not a symbol.");
+		return -1;
+	}
+
+	switch (e->type) {
+	case PE_SYMBOL:
+		return e->left.fexpr->satval;
+	case PE_NOT:
+		return -(e->left.pexpr->left.fexpr->satval);
+	default:
+		perror("Not a symbol.");
+	}
+
+	return -1;
+}
+
+/*
+ * start PicoSAT
+ */
+void picosat_solve(PicoSAT *pico, struct cfdata *data)
+{
+	clock_t start, end;
+	double time;
+	int res;
+
+	printd("Solving SAT-problem...");
+
+	start = clock();
+	res = picosat_sat(pico, -1);
+	end = clock();
+
+	time = ((double) (end - start)) / CLOCKS_PER_SEC;
+	printd("done. (%.6f secs.)\n\n", time);
+
+	if (res == PICOSAT_SATISFIABLE) {
+		printd("===> PROBLEM IS SATISFIABLE <===\n");
+
+	} else if (res == PICOSAT_UNSATISFIABLE) {
+		struct fexpr *e;
+		int lit;
+		const int *i;
+
+		printd("===> PROBLEM IS UNSATISFIABLE <===\n");
+
+		/* print unsat core */
+		printd("\nPrinting unsatisfiable core:\n");
+
+		i = picosat_failed_assumptions(pico);
+		lit = abs(*i++);
+
+		while (lit != 0) {
+			e = data->satmap[lit];
+
+			printd("(%d) %s <%d>\n", lit, str_get(&e->name), e->assumption);
+			lit = abs(*i++);
+		}
+	} else {
+		printd("Unknown if satisfiable.\n");
+	}
+}
+
+/*
+ * add assumption for a symbol to the SAT-solver
+ */
+void sym_add_assumption(PicoSAT *pico, struct symbol *sym)
+{
+	if (sym_is_boolean(sym)) {
+		int tri_val = sym_get_tristate_value(sym);
+
+		sym_add_assumption_tri(pico, sym, tri_val);
+		return;
+	}
+
+	if (sym_is_nonboolean(sym)) {
+		struct fexpr *e = sym->nb_vals->head->elem;
+		struct fexpr_node *node;
+
+		const char *string_val = sym_get_string_value(sym);
+
+		if (sym->type == S_STRING && !strcmp(string_val, ""))
+			return;
+
+		/* symbol does not have a value */
+		if (!sym_nonbool_has_value_set(sym)) {
+			/* set value for sym=n */
+			picosat_assume(pico, e->satval);
+			e->assumption = true;
+
+			for (node = sym->nb_vals->head->next; node != NULL; node = node->next) {
+				picosat_assume(pico, -(node->elem->satval));
+				node->elem->assumption = false;
+			}
+
+			return;
+		}
+
+		/* symbol does have a value set */
+
+		/* set value for sym=n */
+		picosat_assume(pico, -(e->satval));
+		e->assumption = false;
+
+		/* set value for all other fexpr */
+		fexpr_list_for_each(node, sym->nb_vals) {
+			if (node->prev == NULL)
+				continue;
+
+			if (strcmp(str_get(&node->elem->nb_val), string_val) == 0) {
+				picosat_assume(pico, node->elem->satval);
+				node->elem->assumption = true;
+			} else {
+				picosat_assume(pico, -(node->elem->satval));
+				node->elem->assumption = false;
+			}
+		}
+	}
+}
+
+/*
+ * add assumption for a boolean symbol to the SAT-solver
+ */
+void sym_add_assumption_tri(PicoSAT *pico, struct symbol *sym, tristate tri_val)
+{
+	if (sym->type == S_BOOLEAN) {
+		int a = sym->fexpr_y->satval;
+
+		switch (tri_val) {
+		case no:
+			picosat_assume(pico, -a);
+			sym->fexpr_y->assumption = false;
+			break;
+		case mod:
+			perror("Should not happen. Boolean symbol is set to mod.\n");
+			break;
+		case yes:
+
+			picosat_assume(pico, a);
+			sym->fexpr_y->assumption = true;
+			break;
+		}
+	}
+	if (sym->type == S_TRISTATE) {
+		int a = sym->fexpr_y->satval;
+		int a_m = sym->fexpr_m->satval;
+
+		switch (tri_val) {
+		case no:
+			picosat_assume(pico, -a);
+			picosat_assume(pico, -a_m);
+			sym->fexpr_y->assumption = false;
+			sym->fexpr_m->assumption = false;
+			break;
+		case mod:
+			picosat_assume(pico, -a);
+			picosat_assume(pico, a_m);
+			sym->fexpr_y->assumption = false;
+			sym->fexpr_m->assumption = true;
+			break;
+		case yes:
+			picosat_assume(pico, a);
+			picosat_assume(pico, -a_m);
+			sym->fexpr_y->assumption = true;
+			sym->fexpr_m->assumption = false;
+			break;
+		}
+	}
+}
+
+/*
+ * add assumptions for the symbols to be changed to the SAT solver
+ */
+void sym_add_assumption_sdv(PicoSAT *pico, struct sdv_list *list)
+{
+	struct symbol_dvalue *sdv;
+	struct sdv_node *node;
+	int lit_y, lit_m;
+
+	sdv_list_for_each(node, list) {
+		sdv = node->elem;
+		lit_y = sdv->sym->fexpr_y->satval;
+
+		if (sdv->sym->type == S_BOOLEAN) {
+			switch (sdv->tri) {
+			case yes:
+				picosat_assume(pico, lit_y);
+				break;
+			case no:
+				picosat_assume(pico, -lit_y);
+				break;
+			case mod:
+				perror("Should not happen.\n");
+			}
+		} else if (sdv->sym->type == S_TRISTATE) {
+			lit_m = sdv->sym->fexpr_m->satval;
+
+			switch (sdv->tri) {
+			case yes:
+				picosat_assume(pico, lit_y);
+				picosat_assume(pico, -lit_m);
+				break;
+			case mod:
+				picosat_assume(pico, -lit_y);
+				picosat_assume(pico, lit_m);
+				break;
+			case no:
+				picosat_assume(pico, -lit_y);
+				picosat_assume(pico, -lit_m);
+			}
+		}
+	}
+}
scripts/kconfig/cf_utils.h new file +115 -0

115 lines not on this page · raw message

Signature · 1 line
2.39.2
#20 lore raw

Re: [PATCH v4 09/12] kconfig: Add files with utility functions

Quoted 68 lines · Ole Schuerks

This commit contains various helper functions used in the project.

---
 scripts/kconfig/cf_utils.c | 1031 ++++++++++++++++++++++++++++++++++++
 scripts/kconfig/cf_utils.h |  115 ++++
 2 files changed, 1146 insertions(+)
 create mode 100644 scripts/kconfig/cf_utils.c
 create mode 100644 scripts/kconfig/cf_utils.h
scripts/kconfig/cf_utils.c new file +42 -0
new file mode 100644
index 000000000000..bcffd0a4fc1b
@@ -0,0 +1,1031 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023 Patrick Franz <[email protected]>
+ */
+
+#define _GNU_SOURCE
+#include <assert.h>
+#include <locale.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <ctype.h>
+
+#include "configfix.h"
+#include "internal.h"
+
+#define SATMAP_INIT_SIZE 2
+
+static PicoSAT *pico;
+
+static void unfold_cnf_clause(struct pexpr *e);
+static void build_cnf_tseytin(struct pexpr *e, struct cfdata *data);
+
+static void build_cnf_tseytin_top_and(struct pexpr *e, struct cfdata *data);
+static void build_cnf_tseytin_top_or(struct pexpr *e, struct cfdata *data);
+
+static void build_cnf_tseytin_tmp(struct pexpr *e, struct fexpr *t, struct cfdata *data);
+static void build_cnf_tseytin_and(struct pexpr *e, struct fexpr *t, struct cfdata *data);
+static void build_cnf_tseytin_or(struct pexpr *e, struct fexpr *t, struct cfdata *data);
+static int pexpr_satval(struct pexpr *e);
+
+/*
+ * parse Kconfig-file and read .config
+ */
+void init_config(const char *Kconfig_file)
+{
+       conf_parse(Kconfig_file);
+       conf_read(NULL);

scripts/kconfig/cf_utils.c line 52

Quoted 10 lines
scripts/kconfig/cf_utils.c new file +10 -0
+}
+
+/*
+ * initialize satmap
+ */
+void init_data(struct cfdata *data)
+{
+       /* create hashtable with all fexpr */
+       data->satmap = xcalloc(SATMAP_INIT_SIZE, sizeof(**data->satmap));
+       data->satmap_size = SATMAP_INIT_SIZE;

This is a bug.

xcalloc() allocated much bigger memory than used.

Quoted 228 lines
scripts/kconfig/cf_utils.c new file +228 -0
+       printd("done.\n");
+}
+
+/*
+ * create SAT-variables for all fexpr
+ */
+void create_sat_variables(struct cfdata *data)
+{
+       struct symbol *sym;
+
+       printd("Creating SAT-variables...");
+
+       for_all_symbols(sym) {
+               sym->constraints = pexpr_list_init();
+               sym_create_fexpr(sym, data);
+       }
+
+       printd("done.\n");
+}
+
+/*
+ * create various constants
+ */
+void create_constants(struct cfdata *data)
+{
+       printd("Creating constants...");
+
+       /* create TRUE and FALSE constants */
+       data->constants->const_false = fexpr_create(data->sat_variable_nr++, FE_FALSE, "False");
+       // const_false = fexpr_create(sat_variable_nr++, FE_FALSE, "False");
+       fexpr_add_to_satmap(data->constants->const_false, data);
+
+       data->constants->const_true = fexpr_create(data->sat_variable_nr++, FE_TRUE, "True");
+       fexpr_add_to_satmap(data->constants->const_true, data);
+
+       /* add fexpr of constants to tristate constants */
+       symbol_yes.fexpr_y = data->constants->const_true;
+       symbol_yes.fexpr_m = data->constants->const_false;
+
+       symbol_mod.fexpr_y = data->constants->const_false;
+       symbol_mod.fexpr_m = data->constants->const_true;
+
+       symbol_no.fexpr_y = data->constants->const_false;
+       symbol_no.fexpr_m = data->constants->const_false;
+
+       /* create symbols yes/mod/no as fexpr */
+       data->constants->symbol_yes_fexpr = fexpr_create(0, FE_SYMBOL, "y");
+       data->constants->symbol_yes_fexpr->sym = &symbol_yes;
+       data->constants->symbol_yes_fexpr->tri = yes;
+
+       data->constants->symbol_mod_fexpr = fexpr_create(0, FE_SYMBOL, "m");
+       data->constants->symbol_mod_fexpr->sym = &symbol_mod;
+       data->constants->symbol_mod_fexpr->tri = mod;
+
+       data->constants->symbol_no_fexpr = fexpr_create(0, FE_SYMBOL, "n");
+       data->constants->symbol_no_fexpr->sym = &symbol_no;
+       data->constants->symbol_no_fexpr->tri = no;
+
+       printd("done.\n");
+}
+
+/*
+ * create a temporary SAT-variable
+ */
+struct fexpr *create_tmpsatvar(struct cfdata *data)
+{
+       char *name = get_tmp_var_as_char(data->tmp_variable_nr);
+       struct fexpr *t = fexpr_create(data->sat_variable_nr++, FE_TMPSATVAR, name);
+
+       ++data->tmp_variable_nr;
+       fexpr_add_to_satmap(t, data);
+
+       free(name);
+       return t;
+}
+
+/*
+ * return a temporary SAT variable as string
+ */
+char *get_tmp_var_as_char(int i)
+{
+       char *val = malloc(sizeof(char) * 18);
+
+       snprintf(val, 18, "T_%d", i);
+       return val;
+}
+
+/*
+ * return a tristate value as a char *
+ */
+char *tristate_get_char(tristate val)
+{
+       switch (val) {
+       case yes:
+               return "yes";
+       case mod:
+               return "mod";
+       case no:
+               return "no";
+       default:
+               return "";
+       }
+}
+
+/*
+ *check whether an expr can evaluate to mod
+ */
+bool expr_can_evaluate_to_mod(struct expr *e)
+{
+       if (!e)
+               return false;
+
+       switch (e->type) {
+       case E_SYMBOL:
+               return e->left.sym == &symbol_mod || e->left.sym->type == S_TRISTATE ? true : false;
+       case E_AND:
+       case E_OR:
+               return expr_can_evaluate_to_mod(e->left.expr) ||
+                      expr_can_evaluate_to_mod(e->right.expr);
+       case E_NOT:
+               return expr_can_evaluate_to_mod(e->left.expr);
+       default:
+               return false;
+       }
+}
+
+/*
+ * check whether an expr is a non-Boolean constant
+ */
+bool expr_is_nonbool_constant(struct expr *e)
+{
+       if (e->type != E_SYMBOL)
+               return false;
+       if (e->left.sym->type != S_UNKNOWN)
+               return false;
+
+       if (e->left.sym->flags & SYMBOL_CONST)
+               return true;
+
+       return string_is_number(e->left.sym->name) || string_is_hex(e->left.sym->name);
+}
+
+/*
+ * check whether a symbol is a non-Boolean constant
+ */
+bool sym_is_nonbool_constant(struct symbol *sym)
+{
+       if (sym->type != S_UNKNOWN)
+               return false;
+
+       if (sym->flags & SYMBOL_CONST)
+               return true;
+
+       return string_is_number(sym->name) || string_is_hex(sym->name);
+}
+
+/*
+ * print an expr
+ */
+static void print_expr_util(struct expr *e, int prevtoken)
+{
+       if (!e)
+               return;
+
+       switch (e->type) {
+       case E_SYMBOL:
+               if (sym_get_name(e->left.sym) != NULL)
+                       printf("%s", sym_get_name(e->left.sym));
+               else
+                       printf("left was null\n");
+               break;
+       case E_NOT:
+               printf("!");
+               print_expr_util(e->left.expr, E_NOT);
+               break;
+       case E_AND:
+               if (prevtoken != E_AND && prevtoken != 0)
+                       printf("(");
+               print_expr_util(e->left.expr, E_AND);
+               printf(" && ");
+               print_expr_util(e->right.expr, E_AND);
+               if (prevtoken != E_AND && prevtoken != 0)
+                       printf(")");
+               break;
+       case E_OR:
+               if (prevtoken != E_OR && prevtoken != 0)
+                       printf("(");
+               print_expr_util(e->left.expr, E_OR);
+               printf(" || ");
+               print_expr_util(e->right.expr, E_OR);
+               if (prevtoken != E_OR && prevtoken != 0)
+                       printf(")");
+               break;
+       case E_EQUAL:
+       case E_UNEQUAL:
+               if (e->left.sym->name)
+                       printf("%s", e->left.sym->name);
+               else
+                       printf("left was null\n");
+               printf("%s", e->type == E_EQUAL ? "=" : "!=");
+               printf("%s", e->right.sym->name);
+               break;
+       case E_LEQ:
+       case E_LTH:
+               if (e->left.sym->name)
+                       printf("%s", e->left.sym->name);
+               else
+                       printf("left was null\n");
+               printf("%s", e->type == E_LEQ ? "<=" : "<");
+               printf("%s", e->right.sym->name);
+               break;
+       case E_GEQ:
+       case E_GTH:
+               if (e->left.sym->name)
+                       printf("%s", e->left.sym->name);
+               else
+                       printf("left was null\n");
+               printf("%s", e->type == E_GEQ ? ">=" : ">");
+               printf("%s", e->right.sym->name);
+               break;
+       case E_RANGE:
+               printf("[");
+               printf("%s", e->left.sym->name);
+               printf(" ");
+               printf("%s", e->right.sym->name);
+               printf("]");
+               break;
+       default:

scripts/kconfig/cf_utils.c line 291

Quoted 10 lines
scripts/kconfig/cf_utils.c new file +10 -0
+               break;
+       }
+}
+void print_expr(char *tag, struct expr *e, int prevtoken)
+{
+       printf("%s ", tag);
+       print_expr_util(e, prevtoken);
+       printf("\n");
+}
+

This is the same as the existing expr_fprint() except that it prints the 'tag'.

If you need this, presumably you need to do code refactoring.

Quoted 862 lines
scripts/kconfig/cf_utils.c new file +740 -0
+/*
+ * check, if the symbol is a tristate-constant
+ */
+bool sym_is_tristate_constant(struct symbol *sym)
+{
+       return sym == &symbol_yes || sym == &symbol_mod || sym == &symbol_no;
+}
+
+/*
+ * check, if a symbol is of type boolean or tristate
+ */
+bool sym_is_boolean(struct symbol *sym)
+{
+       return sym->type == S_BOOLEAN || sym->type == S_TRISTATE;
+}
+
+/*
+ * check, if a symbol is a boolean/tristate or a tristate constant
+ */
+bool sym_is_bool_or_triconst(struct symbol *sym)
+{
+       return sym_is_tristate_constant(sym) || sym_is_boolean(sym);
+}
+
+/*
+ * check, if a symbol is of type int, hex, or string
+ */
+bool sym_is_nonboolean(struct symbol *sym)
+{
+       return sym->type == S_INT || sym->type == S_HEX || sym->type == S_STRING;
+}
+
+/*
+ * check, if a symbol has a prompt
+ */
+bool sym_has_prompt(struct symbol *sym)
+{
+       struct property *prop;
+
+       for_all_prompts(sym, prop)
+               return true;
+
+       return false;
+}
+
+/*
+ * return the prompt of the symbol if there is one, NULL otherwise
+ */
+struct property *sym_get_prompt(struct symbol *sym)
+{
+       struct property *prop;
+
+       for_all_prompts(sym, prop)
+               return prop;
+
+       return NULL;
+}
+
+/*
+ * return the condition for the property, NULL if there is none. To be pexpr_put
+ * by caller.
+ */
+struct pexpr *prop_get_condition(struct property *prop, struct cfdata *data)
+{
+       if (prop == NULL)
+               return NULL;
+
+       /* if there is no condition, return True */
+       if (!prop->visible.expr)
+               return pexf(data->constants->const_true);
+
+       return expr_calculate_pexpr_both(prop->visible.expr, data);
+}
+
+/*
+ * return the default property, NULL if none exists or can be satisfied
+ */
+struct property *sym_get_default_prop(struct symbol *sym)
+{
+       struct property *prop;
+
+       for_all_defaults(sym, prop) {
+               prop->visible.tri = expr_calc_value(prop->visible.expr);
+               if (prop->visible.tri != no)
+                       return prop;
+       }
+       return NULL;
+}
+
+/*
+ * check whether a non-boolean symbol has a value set
+ */
+bool sym_nonbool_has_value_set(struct symbol *sym)
+{
+       /*
+        * The built constraints make the following constraints:
+        *
+        * visible -> not 'n'
+        * sym->dir_dep not fulfilled -> 'n'
+        * invisible -> (no default's condition is fulfilled <-> 'n')
+        */
+       struct property *prompt;
+       struct property *p;
+
+       if (!sym_is_nonboolean(sym))
+               return false;
+
+       /* cannot have a value with unmet dependencies */
+       if (sym->dir_dep.expr && sym->dir_dep.tri == no)
+               return false;
+
+       /* visible prompt => value set */
+       prompt = sym_get_prompt(sym);
+       if (prompt != NULL && prompt->visible.tri != no)
+               return true;
+
+       /* invisible prompt => must get value from default value */
+       p = sym_get_default_prop(sym);
+       return p != NULL;
+}
+
+/*
+ * return pointer to the name of the symbol or the current prompt-text, if it
+ * is a choice symbol
+ */
+const char *sym_get_name(struct symbol *sym)
+{
+       if (sym_is_choice(sym)) {
+               struct property *prompt = sym_get_prompt(sym);
+
+               if (prompt == NULL)
+                       return "";
+
+               return prompt->text;
+       } else {
+               return sym->name;
+       }
+}
+
+/*
+ * check whether symbol is to be changed
+ */
+bool sym_is_sdv(struct sdv_list *list, struct symbol *sym)
+{
+       struct sdv_node *node;
+
+       sdv_list_for_each(node, list)
+               if (sym == node->elem->sym)
+                       return true;
+
+       return false;
+}
+
+/*
+ * print a symbol's name
+ */
+void print_sym_name(struct symbol *sym)
+{
+       printf("Symbol: ");
+       if (sym_is_choice(sym)) {
+               struct property *prompt = sym_get_prompt(sym);
+
+               printf("(Choice) %s", prompt->text);
+       } else  {
+               printf("%s", sym->name);
+       }
+       printf("\n");
+}
+
+/*
+ * print all constraints for a symbol
+ */
+void print_sym_constraint(struct symbol *sym)
+{
+       struct pexpr_node *node;
+
+       pexpr_list_for_each(node, sym->constraints)
+               pexpr_print("::", node->elem, -1);
+}
+
+/*
+ * print a default map
+ */
+void print_default_map(struct defm_list *map)
+{
+       struct default_map *entry;
+       struct defm_node *node;
+
+       defm_list_for_each(node, map) {
+               struct gstr s = str_new();
+
+               entry = node->elem;
+
+               str_append(&s, "\t");
+               str_append(&s, str_get(&entry->val->name));
+               str_append(&s, " ->");
+               pexpr_print(strdup(str_get(&s)), entry->e, -1);
+               str_free(&s);
+       }
+}
+
+/*
+ * check whether a string is a number
+ */
+bool string_is_number(char *s)
+{
+       int len = strlen(s);
+       int i = 0;
+
+       while (i < len) {
+               if (!isdigit(s[i]))
+                       return false;
+               i++;
+       }
+
+       return true;
+}
+
+/*
+ * check whether a string is a hexadecimal number
+ */
+bool string_is_hex(char *s)
+{
+       int len = strlen(s);
+       int i = 2;
+
+       if (len >= 3 && s[0] == '0' && s[1] == 'x') {
+               while (i < len) {
+                       if (!isxdigit(s[i]))
+                               return false;
+                       i++;
+               }
+               return true;
+       } else {
+               return false;
+       }
+}
+
+/*
+ * initialize PicoSAT
+ */
+PicoSAT *initialize_picosat(void)
+{
+       PicoSAT *pico;
+
+       printd("\nInitializing PicoSAT...");
+       pico = picosat_init();
+       picosat_enable_trace_generation(pico);
+       printd("done.\n");
+
+       return pico;
+}
+
+/*
+ * construct the CNF-clauses from the constraints
+ */
+void construct_cnf_clauses(PicoSAT *p, struct cfdata *data)
+{
+       struct symbol *sym;
+
+       pico = p;
+
+       /* adding unit-clauses for constants */
+       sat_add_clause(2, pico, -(data->constants->const_false->satval));
+       sat_add_clause(2, pico, data->constants->const_true->satval);
+
+       for_all_symbols(sym) {
+               struct pexpr_node *node;
+
+               if (sym->type == S_UNKNOWN)
+                       continue;
+
+               pexpr_list_for_each(node, sym->constraints) {
+                       if (pexpr_is_cnf(node->elem)) {
+                               unfold_cnf_clause(node->elem);
+                               picosat_add(pico, 0);
+                       } else {
+                               build_cnf_tseytin(node->elem, data);
+                       }
+
+               }
+       }
+}
+
+/*
+ * helper function to add an expression to a CNF-clause
+ */
+static void unfold_cnf_clause(struct pexpr *e)
+{
+       switch (e->type) {
+       case PE_SYMBOL:
+               picosat_add(pico, e->left.fexpr->satval);
+               break;
+       case PE_OR:
+               unfold_cnf_clause(e->left.pexpr);
+               unfold_cnf_clause(e->right.pexpr);
+               break;
+       case PE_NOT:
+               picosat_add(pico, -(e->left.pexpr->left.fexpr->satval));
+               break;
+       default:
+               perror("Not in CNF, FE_EQUALS.");
+       }
+}
+
+/*
+ * build CNF-clauses for a pexpr not in CNF
+ */
+static void build_cnf_tseytin(struct pexpr *e, struct cfdata *data)
+{
+       switch (e->type) {
+       case PE_AND:
+               build_cnf_tseytin_top_and(e, data);
+               break;
+       case PE_OR:
+               build_cnf_tseytin_top_or(e, data);
+               break;
+       default:
+               perror("Expression not a propositional logic formula. root.");
+       }
+}
+
+/*
+ * split up a pexpr of type AND as both sides must be satisfied
+ */
+static void build_cnf_tseytin_top_and(struct pexpr *e, struct cfdata *data)
+{
+       if (pexpr_is_cnf(e->left.pexpr))
+               unfold_cnf_clause(e->left.pexpr);
+       else
+               build_cnf_tseytin(e->left.pexpr, data);
+
+       if (pexpr_is_cnf(e->right.pexpr))
+               unfold_cnf_clause(e->right.pexpr);
+       else
+               build_cnf_tseytin(e->right.pexpr, data);
+
+}
+
+static void build_cnf_tseytin_top_or(struct pexpr *e, struct cfdata *data)
+{
+       struct fexpr *t1 = NULL, *t2 = NULL;
+       int a, b;
+
+       /* set left side */
+       if (pexpr_is_symbol(e->left.pexpr)) {
+               a = pexpr_satval(e->left.pexpr);
+       } else {
+               t1 = create_tmpsatvar(data);
+               a = t1->satval;
+       }
+
+       /* set right side */
+       if (pexpr_is_symbol(e->right.pexpr)) {
+               b = pexpr_satval(e->right.pexpr);
+       } else {
+               t2 = create_tmpsatvar(data);
+               b = t2->satval;
+       }
+
+       /* A v B */
+       sat_add_clause(3, pico, a, b);
+
+       /* traverse down the tree to build more constraints if needed */
+       if (!pexpr_is_symbol(e->left.pexpr)) {
+               if (t1 == NULL)
+                       perror("t1 is NULL.");
+
+               build_cnf_tseytin_tmp(e->left.pexpr, t1, data);
+       }
+
+       if (!pexpr_is_symbol(e->right.pexpr)) {
+               if (t2 == NULL)
+                       perror("t2 is NULL.");
+
+               build_cnf_tseytin_tmp(e->right.pexpr, t2, data);
+       }
+}
+
+/*
+ * build the sub-expressions
+ */
+static void build_cnf_tseytin_tmp(struct pexpr *e, struct fexpr *t, struct cfdata *data)
+{
+       switch (e->type) {
+       case PE_AND:
+               build_cnf_tseytin_and(e, t, data);
+               break;
+       case PE_OR:
+               build_cnf_tseytin_or(e, t, data);
+               break;
+       default:
+               perror("Expression not a propositional logic formula. root.");
+       }
+}
+
+/*
+ * build the Tseytin sub-expressions for a pexpr of type AND
+ */
+static void build_cnf_tseytin_and(struct pexpr *e, struct fexpr *t, struct cfdata *data)
+{
+       struct fexpr *t1 = NULL, *t2 = NULL;
+       int a, b, c;
+
+       assert(t != NULL);
+
+       /* set left side */
+       if (pexpr_is_symbol(e->left.pexpr)) {
+               a = pexpr_satval(e->left.pexpr);
+       } else {
+               t1 = create_tmpsatvar(data);
+               a = t1->satval;
+       }
+
+       /* set right side */
+       if (pexpr_is_symbol(e->right.pexpr)) {
+               b = pexpr_satval(e->right.pexpr);
+       } else {
+               t2 = create_tmpsatvar(data);
+               b = t2->satval;
+       }
+
+       c = t->satval;
+
+       /* -A v -B v C */
+       sat_add_clause(4, pico, -a, -b, c);
+       /* A v -C */
+       sat_add_clause(3, pico, a, -c);
+       /* B v -C */
+       sat_add_clause(3, pico, b, -c);
+
+       /* traverse down the tree to build more constraints if needed */
+       if (!pexpr_is_symbol(e->left.pexpr)) {
+               if (t1 == NULL)
+                       perror("t1 is NULL.");
+
+               build_cnf_tseytin_tmp(e->left.pexpr, t1, data);
+       }
+       if (!pexpr_is_symbol(e->right.pexpr)) {
+               if (t2 == NULL)
+                       perror("t2 is NULL.");
+
+               build_cnf_tseytin_tmp(e->right.pexpr, t2, data);
+       }
+}
+
+/*
+ * build the Tseytin sub-expressions for a pexpr of type
+ */
+static void build_cnf_tseytin_or(struct pexpr *e, struct fexpr *t, struct cfdata *data)
+{
+       struct fexpr *t1 = NULL, *t2 = NULL;
+       int a, b, c;
+
+       assert(t != NULL);
+
+       /* set left side */
+       if (pexpr_is_symbol(e->left.pexpr)) {
+               a = pexpr_satval(e->left.pexpr);
+       } else {
+               t1 = create_tmpsatvar(data);
+               a = t1->satval;
+       }
+
+       /* set right side */
+       if (pexpr_is_symbol(e->right.pexpr)) {
+               b = pexpr_satval(e->right.pexpr);
+       } else {
+               t2 = create_tmpsatvar(data);
+               b = t2->satval;
+       }
+
+       c = t->satval;
+
+       /* A v B v -C */
+       sat_add_clause(4, pico, a, b, -c);
+       /* -A v C */;
+       sat_add_clause(3, pico, -a, c);
+       /* -B v C */
+       sat_add_clause(3, pico, -b, c);
+
+       /* traverse down the tree to build more constraints if needed */
+       if (!pexpr_is_symbol(e->left.pexpr)) {
+               if (t1 == NULL)
+                       perror("t1 is NULL.");
+
+               build_cnf_tseytin_tmp(e->left.pexpr, t1, data);
+       }
+       if (!pexpr_is_symbol(e->right.pexpr)) {
+               if (t2 == NULL)
+                       perror("t2 is NULL.");
+
+               build_cnf_tseytin_tmp(e->right.pexpr, t2, data);
+       }
+}
+
+/*
+ * add a clause to PicoSAT
+ * First argument must be the SAT solver
+ */
+void sat_add_clause(int num, ...)
+{
+       va_list valist;
+       int lit;
+       PicoSAT *pico;
+
+       if (num <= 1)
+               return;
+
+       va_start(valist, num);
+
+       pico = va_arg(valist, PicoSAT *);
+
+       /* access all the arguments assigned to valist */
+       for (int i = 1; i < num; i++) {
+               lit = va_arg(valist, int);
+               picosat_add(pico, lit);
+       }
+       picosat_add(pico, 0);
+
+       va_end(valist);
+}
+
+/*
+ * return the SAT-variable for a pexpr that is a symbol
+ */
+static int pexpr_satval(struct pexpr *e)
+{
+       if (!pexpr_is_symbol(e)) {
+               perror("pexpr is not a symbol.");
+               return -1;
+       }
+
+       switch (e->type) {
+       case PE_SYMBOL:
+               return e->left.fexpr->satval;
+       case PE_NOT:
+               return -(e->left.pexpr->left.fexpr->satval);
+       default:
+               perror("Not a symbol.");
+       }
+
+       return -1;
+}
+
+/*
+ * start PicoSAT
+ */
+void picosat_solve(PicoSAT *pico, struct cfdata *data)
+{
+       clock_t start, end;
+       double time;
+       int res;
+
+       printd("Solving SAT-problem...");
+
+       start = clock();
+       res = picosat_sat(pico, -1);
+       end = clock();
+
+       time = ((double) (end - start)) / CLOCKS_PER_SEC;
+       printd("done. (%.6f secs.)\n\n", time);
+
+       if (res == PICOSAT_SATISFIABLE) {
+               printd("===> PROBLEM IS SATISFIABLE <===\n");
+
+       } else if (res == PICOSAT_UNSATISFIABLE) {
+               struct fexpr *e;
+               int lit;
+               const int *i;
+
+               printd("===> PROBLEM IS UNSATISFIABLE <===\n");
+
+               /* print unsat core */
+               printd("\nPrinting unsatisfiable core:\n");
+
+               i = picosat_failed_assumptions(pico);
+               lit = abs(*i++);
+
+               while (lit != 0) {
+                       e = data->satmap[lit];
+
+                       printd("(%d) %s <%d>\n", lit, str_get(&e->name), e->assumption);
+                       lit = abs(*i++);
+               }
+       } else {
+               printd("Unknown if satisfiable.\n");
+       }
+}
+
+/*
+ * add assumption for a symbol to the SAT-solver
+ */
+void sym_add_assumption(PicoSAT *pico, struct symbol *sym)
+{
+       if (sym_is_boolean(sym)) {
+               int tri_val = sym_get_tristate_value(sym);
+
+               sym_add_assumption_tri(pico, sym, tri_val);
+               return;
+       }
+
+       if (sym_is_nonboolean(sym)) {
+               struct fexpr *e = sym->nb_vals->head->elem;
+               struct fexpr_node *node;
+
+               const char *string_val = sym_get_string_value(sym);
+
+               if (sym->type == S_STRING && !strcmp(string_val, ""))
+                       return;
+
+               /* symbol does not have a value */
+               if (!sym_nonbool_has_value_set(sym)) {
+                       /* set value for sym=n */
+                       picosat_assume(pico, e->satval);
+                       e->assumption = true;
+
+                       for (node = sym->nb_vals->head->next; node != NULL; node = node->next) {
+                               picosat_assume(pico, -(node->elem->satval));
+                               node->elem->assumption = false;
+                       }
+
+                       return;
+               }
+
+               /* symbol does have a value set */
+
+               /* set value for sym=n */
+               picosat_assume(pico, -(e->satval));
+               e->assumption = false;
+
+               /* set value for all other fexpr */
+               fexpr_list_for_each(node, sym->nb_vals) {
+                       if (node->prev == NULL)
+                               continue;
+
+                       if (strcmp(str_get(&node->elem->nb_val), string_val) == 0) {
+                               picosat_assume(pico, node->elem->satval);
+                               node->elem->assumption = true;
+                       } else {
+                               picosat_assume(pico, -(node->elem->satval));
+                               node->elem->assumption = false;
+                       }
+               }
+       }
+}
+
+/*
+ * add assumption for a boolean symbol to the SAT-solver
+ */
+void sym_add_assumption_tri(PicoSAT *pico, struct symbol *sym, tristate tri_val)
+{
+       if (sym->type == S_BOOLEAN) {
+               int a = sym->fexpr_y->satval;
+
+               switch (tri_val) {
+               case no:
+                       picosat_assume(pico, -a);
+                       sym->fexpr_y->assumption = false;
+                       break;
+               case mod:
+                       perror("Should not happen. Boolean symbol is set to mod.\n");
+                       break;
+               case yes:
+
+                       picosat_assume(pico, a);
+                       sym->fexpr_y->assumption = true;
+                       break;
+               }
+       }
+       if (sym->type == S_TRISTATE) {
+               int a = sym->fexpr_y->satval;
+               int a_m = sym->fexpr_m->satval;
+
+               switch (tri_val) {
+               case no:
+                       picosat_assume(pico, -a);
+                       picosat_assume(pico, -a_m);
+                       sym->fexpr_y->assumption = false;
+                       sym->fexpr_m->assumption = false;
+                       break;
+               case mod:
+                       picosat_assume(pico, -a);
+                       picosat_assume(pico, a_m);
+                       sym->fexpr_y->assumption = false;
+                       sym->fexpr_m->assumption = true;
+                       break;
+               case yes:
+                       picosat_assume(pico, a);
+                       picosat_assume(pico, -a_m);
+                       sym->fexpr_y->assumption = true;
+                       sym->fexpr_m->assumption = false;
+                       break;
+               }
+       }
+}
+
+/*
+ * add assumptions for the symbols to be changed to the SAT solver
+ */
+void sym_add_assumption_sdv(PicoSAT *pico, struct sdv_list *list)
+{
+       struct symbol_dvalue *sdv;
+       struct sdv_node *node;
+       int lit_y, lit_m;
+
+       sdv_list_for_each(node, list) {
+               sdv = node->elem;
+               lit_y = sdv->sym->fexpr_y->satval;
+
+               if (sdv->sym->type == S_BOOLEAN) {
+                       switch (sdv->tri) {
+                       case yes:
+                               picosat_assume(pico, lit_y);
+                               break;
+                       case no:
+                               picosat_assume(pico, -lit_y);
+                               break;
+                       case mod:
+                               perror("Should not happen.\n");
+                       }
+               } else if (sdv->sym->type == S_TRISTATE) {
+                       lit_m = sdv->sym->fexpr_m->satval;
+
+                       switch (sdv->tri) {
+                       case yes:
+                               picosat_assume(pico, lit_y);
+                               picosat_assume(pico, -lit_m);
+                               break;
+                       case mod:
+                               picosat_assume(pico, -lit_y);
+                               picosat_assume(pico, lit_m);
+                               break;
+                       case no:
+                               picosat_assume(pico, -lit_y);
+                               picosat_assume(pico, -lit_m);
+                       }
+               }
+       }
+}
scripts/kconfig/cf_utils.h new file +115 -1
new file mode 100644
index 000000000000..b71c8731a8ff
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023 Patrick Franz <[email protected]>
+ */
+
+#ifndef CF_UTILS_H
+#define CF_UTILS_H
+
+#include "expr.h"
+#include "cf_defs.h"
+#include "picosat.h"
+
+/* parse Kconfig-file and read .config */
+void init_config(const char *Kconfig_file);
+
+/* initialize satmap and cnf_clauses */
+void init_data(struct cfdata *data);
+
+/* assign SAT-variables to all fexpr and create the sat_map */
+void create_sat_variables(struct cfdata *data);
+
+/* create True/False constants */
+void create_constants(struct cfdata *data);
+
+/* create a temporary SAT-variable */
+struct fexpr *create_tmpsatvar(struct cfdata *data);
+
+/* return a temporary SAT variable as string */
+char *get_tmp_var_as_char(int i);
+
+/* return a tristate value as a char * */
+char *tristate_get_char(tristate val);
+
+/* check whether an expr can evaluate to mod */
+bool expr_can_evaluate_to_mod(struct expr *e);
+
+/* check whether an expr is a non-Boolean constant */
+bool expr_is_nonbool_constant(struct expr *e);
+
+/* check whether a symbol is a non-Boolean constant */
+bool sym_is_nonbool_constant(struct symbol *sym);
+
+/* print an expr */
+void print_expr(char *tag, struct expr *e, int prevtoken);
+
+/* check, if the symbol is a tristate-constant */
+bool sym_is_tristate_constant(struct symbol *sym);
+
+/* check, if a symbol is of type boolean or tristate */
+bool sym_is_boolean(struct symbol *sym);
+
+/* check, if a symbol is a boolean/tristate or a tristate constant */
+bool sym_is_bool_or_triconst(struct symbol *sym);
+
+/* check, if a symbol is of type int, hex, or string */
+bool sym_is_nonboolean(struct symbol *sym);
+
+/* check, if a symbol has a prompt */
+bool sym_has_prompt(struct symbol *sym);
+
+/* return the prompt of the symbol, if there is one */
+struct property *sym_get_prompt(struct symbol *sym);
+
+/* return the condition for the property, True if there is none */
+struct pexpr *prop_get_condition(struct property *prop, struct cfdata *data);
+
+/* return the default property, NULL if none exists or can be satisfied */
+struct property *sym_get_default_prop(struct symbol *sym);
+
+/* check whether a non-boolean symbol has a value set */
+bool sym_nonbool_has_value_set(struct symbol *sym);
+
+/* return the name of the symbol */
+const char *sym_get_name(struct symbol *sym);
+
+/* check whether symbol is to be changed */
+bool sym_is_sdv(struct sdv_list *list, struct symbol *sym);
+
+/* print a symbol's name */
+void print_sym_name(struct symbol *sym);
+
+/* print all constraints for a symbol */
+void print_sym_constraint(struct symbol *sym);
+
+/* print a default map */
+void print_default_map(struct defm_list *map);
+
+/* check whether a string is a number */
+bool string_is_number(char *s);
+
+/* check whether a string is a hexadecimal number */
+bool string_is_hex(char *s);
+
+/* initialize PicoSAT */
+PicoSAT *initialize_picosat(void);
+
+/* construct the CNF-clauses from the constraints */
+void construct_cnf_clauses(PicoSAT *pico, struct cfdata *data);
+
+/* add a clause to PicoSAT */
+void sat_add_clause(int num, ...);
+
+/* start PicoSAT */
+void picosat_solve(PicoSAT *pico, struct cfdata *data);
+
+/* add assumption for a symbol to the SAT-solver */
+void sym_add_assumption(PicoSAT *pico, struct symbol *sym);
+
+/* add assumption for a boolean symbol to the SAT-solver */
+void sym_add_assumption_tri(PicoSAT *pico, struct symbol *sym, tristate tri_val);
+
+/* add assumptions for the symbols to be changed to the SAT solver */
+void sym_add_assumption_sdv(PicoSAT *pico, struct sdv_list *list);
+
+#endif
--
Quoted 2 lines

2.39.2

--
Best Regards

Masahiro Yamada

#21 lore raw

PATCH v4 10/12 kconfig: Add tools

In reply to Ole Schuerks

This commit contains the actual API to be used from a configurator. Furthermore, it contains a tool to print all constraints into a file.

---
 scripts/kconfig/.gitignore    |   1 +
 scripts/kconfig/cfoutconfig.c | 142 ++++++++++++++
 scripts/kconfig/configfix.c   | 337 ++++++++++++++++++++++++++++++++++
 scripts/kconfig/configfix.h   |  40 ++++
 4 files changed, 520 insertions(+)
 create mode 100644 scripts/kconfig/cfoutconfig.c
 create mode 100644 scripts/kconfig/configfix.c
 create mode 100644 scripts/kconfig/configfix.h
scripts/kconfig/.gitignore +1 -0
index 0b2ff775b2e3..23446f70083e 100644
@@ -5,3 +5,4 @@
 /[gmnq]conf-cflags
 /[gmnq]conf-libs
 /qconf-moc.cc
+/cfoutconfig
scripts/kconfig/cfoutconfig.c new file +142 -0

142 lines not on this page · raw message

scripts/kconfig/configfix.c new file +337 -0

337 lines not on this page · raw message

scripts/kconfig/configfix.h new file +40 -0
new file mode 100644
index 000000000000..8ebcc807da9d
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023 Patrick Franz <[email protected]>
+ */
+
+#ifndef CONFIGFIX_H
+#define CONFIGFIX_H
+
+/* make functions accessible from xconfig */
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* include internal definitions */
+#define LKC_DIRECT_LINK
+#include "lkc.h"
+
+/* include own definitions */
+#include "cf_defs.h"
+
+/* include other header files needed */
+#include "picosat.h"
+#include "cf_constraints.h"
+#include "cf_expr.h"
+#include "cf_rangefix.h"
+#include "cf_utils.h"
+
+/* external functions */
+struct sfl_list *run_satconf(struct sdv_list *symbols);
+int apply_fix(struct sfix_list *fix);
+int run_satconf_cli(const char *Kconfig_file);
+void interrupt_rangefix(void);
+struct sfix_list *select_solution(struct sfl_list *solutions, int index);
+struct symbol_fix *select_symbol(struct sfix_list *solution, int index);
+
+/* make functions accessible from xconfig */
+#ifdef __cplusplus
+}
+#endif
+#endif
Signature · 1 line
2.39.2
#22 lore raw

PATCH v4 11/12 kconfig: Add xconfig-modifications

In reply to Ole Schuerks

The tool can be called from any configurator. We chose to modify xconfig for this purpose. These files contain the necessary modifications to xconfig in order to resolve conflicts.

---
 scripts/kconfig/qconf.cc | 515 ++++++++++++++++++++++++++++++++++++++-
 scripts/kconfig/qconf.h  | 103 ++++++++
 2 files changed, 616 insertions(+), 2 deletions(-)
scripts/kconfig/qconf.cc +513 -2
index 7d239c032b3d..20ca9936d592 100644
@@ -3,7 +3,7 @@
  * Copyright (C) 2002 Roman Zippel <[email protected]>
  * Copyright (C) 2015 Boris Barbulovski <[email protected]>
  */
-
+#include "qnamespace.h"
 #include <QAction>
 #include <QActionGroup>
 #include <QApplication>
@@ -19,14 +19,27 @@
 #include <QRegularExpression>
 #include <QScreen>
 #include <QToolBar>
+#include <QListWidget>
+#include <QComboBox>
+#include <QTableWidget>
+#include <QHBoxLayout>
+#include <QMovie>
+#include <QMessageBox>
 
 #include <stdlib.h>
+#include <QAbstractItemView>
+#include <QMimeData>
+#include <QBrush>
+#include <QColor>
 
 #include "lkc.h"
 #include "qconf.h"
+#include "configfix.h"
 
 #include "images.h"
 
+static QString tristate_value_to_string(tristate val);
+static tristate string_value_to_tristate(QString s);
 
 static QApplication *configApp;
 static ConfigSettings *configSettings;
@@ -178,6 +191,7 @@ void ConfigItem::updateMenu(void)
 		prompt += " (NEW)";
 set_prompt:
 	setText(promptColIdx, prompt);
+
 }
 
 void ConfigItem::testUpdateMenu(bool v)
@@ -225,6 +239,7 @@ void ConfigItem::init(void)
 		}
 	}
 	updateMenu();
+
 }
 
 /*
@@ -405,7 +420,7 @@ void ConfigList::updateSelection(void)
 	ConfigItem* item = (ConfigItem*)selectedItems().first();
 	if (!item)
 		return;
-
+	emit selectionChanged(selectedItems());
 	menu = item->menu;
 	emit menuChanged(menu);
 	if (!menu)
@@ -471,6 +486,7 @@ void ConfigList::updateListForAll()
 
 		list->updateList();
 	}
+
 }
 
 void ConfigList::updateListAllForAll()
@@ -539,6 +555,7 @@ void ConfigList::changeValue(ConfigItem* item)
 		}
 		if (oldexpr != newexpr)
 			ConfigList::updateListForAll();
+			emit updateConflictsViewColorization();
 		break;
 	default:
 		break;
@@ -898,6 +915,7 @@ void ConfigList::contextMenuEvent(QContextMenuEvent *e)
 			action, &QAction::setChecked);
 		action->setChecked(showName);
 		headerPopup->addAction(action);
+		headerPopup->addAction(addSymbolFromContextMenu);
 	}
 
 	headerPopup->exec(e->globalPos());
@@ -918,6 +936,7 @@ QList<ConfigList *> ConfigList::allLists;
 QAction *ConfigList::showNormalAction;
 QAction *ConfigList::showAllAction;
 QAction *ConfigList::showPromptAction;
+QAction *ConfigList::addSymbolFromContextMenu;
 
 void ConfigList::setAllOpen(bool open)
 {
@@ -1249,7 +1268,10 @@ ConfigSearchWindow::ConfigSearchWindow(ConfigMainWindow *parent)
 		info, &ConfigInfoView::setInfo);
 	connect(list, &ConfigList::menuChanged,
 		parent, &ConfigMainWindow::setMenuLink);
+	connect(list, &ConfigList::menuChanged,
+		parent, &ConfigMainWindow::conflictSelected);
 
+	connect(list,&ConfigList::updateConflictsViewColorization,this,&ConfigSearchWindow::updateConflictsViewColorizationFowarder);
 	layout1->addWidget(split);
 
 	QVariant x, y;
@@ -1272,6 +1294,10 @@ ConfigSearchWindow::ConfigSearchWindow(ConfigMainWindow *parent)
 		this, &ConfigSearchWindow::saveSettings);
 }
 
+void ConfigSearchWindow::updateConflictsViewColorizationFowarder(void){
+	emit updateConflictsViewColorization();
+}
+
 void ConfigSearchWindow::saveSettings(void)
 {
 	if (!objectName().isEmpty()) {
@@ -1364,6 +1390,17 @@ ConfigMainWindow::ConfigMainWindow(void)
 	split1->addWidget(configList);
 	split1->addWidget(menuList);
 	split2->addWidget(helpText);
+	split3 = new QSplitter(split2);
+	split3->setOrientation(Qt::Vertical);
+	conflictsView = new ConflictsView(split3, "help");
+	/* conflictsSelected signal in conflictsview triggers when a conflict is selected
+		 in the view. this line connects that event to conflictselected event in mainwindow
+		 which updates the selection to match (in the configlist) the symbol that was selected.
+	*/
+	connect(conflictsView,SIGNAL(conflictSelected(struct menu *)),SLOT(conflictSelected(struct menu *)));
+	connect(conflictsView,SIGNAL(refreshMenu()),SLOT(refreshMenu()));
+	connect(menuList,SIGNAL(updateConflictsViewColorization()),conflictsView,SLOT(updateConflictsViewColorization()));
+	connect(configList,SIGNAL(updateConflictsViewColorization()),conflictsView,SLOT(updateConflictsViewColorization()));
 
 	setTabOrder(configList, helpText);
 	configList->setFocus();
@@ -1430,6 +1467,8 @@ ConfigMainWindow::ConfigMainWindow(void)
 	ConfigList::showAllAction->setCheckable(true);
 	ConfigList::showPromptAction = new QAction("Show Prompt Options", optGroup);
 	ConfigList::showPromptAction->setCheckable(true);
+	ConfigList::addSymbolFromContextMenu = new QAction("Add symbol from context menu");
+	connect(ConfigList::addSymbolFromContextMenu, &QAction::triggered, conflictsView, &ConflictsView::addSymbol);
 
 	QAction *showDebugAction = new QAction("Show Debug Info", this);
 	  showDebugAction->setCheckable(true);
@@ -1485,6 +1524,8 @@ ConfigMainWindow::ConfigMainWindow(void)
 
 	connect(configList, &ConfigList::menuChanged,
 		helpText, &ConfigInfoView::setInfo);
+	connect(configList, &ConfigList::menuChanged,
+		conflictsView, &ConflictsView::menuChanged);
 	connect(configList, &ConfigList::menuSelected,
 		this, &ConfigMainWindow::changeMenu);
 	connect(configList, &ConfigList::itemSelected,
@@ -1493,6 +1534,8 @@ ConfigMainWindow::ConfigMainWindow(void)
 		this, &ConfigMainWindow::goBack);
 	connect(menuList, &ConfigList::menuChanged,
 		helpText, &ConfigInfoView::setInfo);
+	connect(menuList, &ConfigList::menuChanged,
+		conflictsView, &ConflictsView::menuChanged);
 	connect(menuList, &ConfigList::menuSelected,
 		this, &ConfigMainWindow::changeMenu);
 
@@ -1712,6 +1755,13 @@ void ConfigMainWindow::showSplitView(void)
 	menuList->setFocus();
 }
 
+void ConfigMainWindow::conflictSelected(struct menu * men)
+{
+	configList->clearSelection();
+	menuList->clearSelection();
+	emit(setMenuLink(men));
+}
+
 void ConfigMainWindow::showFullView(void)
 {
 	singleViewAction->setEnabled(true);
@@ -1847,6 +1897,432 @@ void ConfigMainWindow::conf_changed(bool dirty)
 		saveAction->setEnabled(dirty);
 }
 
+void ConfigMainWindow::refreshMenu(void)
+{
+	configList->updateListAll();
+}
+
+void QTableWidget::dropEvent(QDropEvent *event)
+{
+}
+
+ConflictsView::ConflictsView(QWidget *parent, const char *name)
+	: Parent(parent)
+{
+	currentSelectedMenu = nullptr;
+	setObjectName(name);
+	QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
+	QVBoxLayout *verticalLayout = new QVBoxLayout();
+	verticalLayout->setContentsMargins(0, 0, 0, 0);
+	conflictsToolBar = new QToolBar("ConflictTools", this);
+	// toolbar buttons [n] [m] [y] [calculate fixes] [remove]
+	QAction *addSymbol = new QAction("Add Symbol");
+	QAction *setConfigSymbolAsNo = new QAction("N");
+	QAction *setConfigSymbolAsModule = new QAction("M");
+	QAction *setConfigSymbolAsYes = new QAction("Y");
+	fixConflictsAction_ = new QAction("Calculate Fixes");
+	QAction *removeSymbol = new QAction("Remove Symbol");
+	QMovie *loadingGif = new QMovie("scripts/kconfig/loader.gif");
+	auto loadingLabel = new QLabel;
+
+	if (loadingGif->isValid()) {
+		loadingGif->setScaledSize(loadingGif->scaledSize().scaled(
+			20, 20, Qt::KeepAspectRatioByExpanding));
+		loadingGif->start();
+		loadingLabel->setMovie(loadingGif);
+	} else {
+		loadingLabel->setText("Calculating...");
+	}
+
+	//if you change the order of buttons here, change the code where
+	//module button was disabled if symbol is boolean, selecting module button
+	//depends on a specific index in list of action
+	fixConflictsAction_->setCheckable(false);
+	conflictsToolBar->addAction(addSymbol);
+	conflictsToolBar->addAction(setConfigSymbolAsNo);
+	conflictsToolBar->addAction(setConfigSymbolAsModule);
+	conflictsToolBar->addAction(setConfigSymbolAsYes);
+	conflictsToolBar->addAction(fixConflictsAction_);
+	conflictsToolBar->addAction(removeSymbol);
+	// loadingLabel->setMargin(5);
+	loadingLabel->setContentsMargins(5, 5, 5, 5);
+	loadingAction = conflictsToolBar->addWidget(loadingLabel);
+	loadingAction->setVisible(false);
+
+
+	verticalLayout->addWidget(conflictsToolBar);
+
+	connect(addSymbol, &QAction::triggered, this, &ConflictsView::addSymbol);
+	connect(setConfigSymbolAsNo, &QAction::triggered,this, &ConflictsView::changeToNo);
+	connect(setConfigSymbolAsModule, &QAction::triggered,this, &ConflictsView::changeToModule);
+	connect(setConfigSymbolAsYes, &QAction::triggered,this, &ConflictsView::changeToYes);
+	connect(removeSymbol, &QAction::triggered,this, &ConflictsView::removeSymbol);
+	connect(this, SIGNAL(resultsReady()), SLOT(updateResults()));
+	//connect clicking 'calculate fixes' to 'change all symbol values to fix all conflicts'
+	// no longer used anymore for now.
+	connect(fixConflictsAction_, &QAction::triggered,this, &ConflictsView::calculateFixes);
+
+	conflictsTable = (QTableWidget *) new dropAbleView(this);
+	conflictsTable->setRowCount(0);
+	conflictsTable->setColumnCount(3);
+	conflictsTable->setSelectionBehavior(QAbstractItemView::SelectRows);
+
+	conflictsTable->setHorizontalHeaderLabels(QStringList()  << "Option" << "Wanted value" << "Current value" );
+	verticalLayout->addWidget(conflictsTable);
+
+	conflictsTable->setDragDropMode(QAbstractItemView::DropOnly);
+	setAcceptDrops(true);
+
+	connect(conflictsTable, SIGNAL(cellClicked(int, int)), SLOT(cellClicked(int,int)));
+	horizontalLayout->addLayout(verticalLayout);
+
+	// populate the solution view on the right hand side:
+	QVBoxLayout *solutionLayout = new QVBoxLayout();
+	solutionLayout->setContentsMargins(0, 0, 0, 0);
+	solutionSelector = new QComboBox();
+	connect(solutionSelector, QOverload<int>::of(&QComboBox::currentIndexChanged),
+		[=](int index){ changeSolutionTable(index); });
+	solutionTable = new QTableWidget();
+	solutionTable->setRowCount(0);
+	solutionTable->setColumnCount(2);
+	solutionTable->setHorizontalHeaderLabels(QStringList()  << "Symbol" << "New Value" );
+
+	applyFixButton = new QPushButton("Apply Selected solution");
+	connect(applyFixButton, SIGNAL(clicked(bool)), SLOT(applyFixButtonClick()));
+
+	numSolutionLabel = new QLabel("Solutions:");
+	solutionLayout->addWidget(numSolutionLabel);
+	solutionLayout->addWidget(solutionSelector);
+	solutionLayout->addWidget(solutionTable);
+	solutionLayout->addWidget(applyFixButton);
+
+	horizontalLayout->addLayout(solutionLayout);
+}
+
+
+void ConflictsView::changeToNo(){
+	QItemSelectionModel *select = conflictsTable->selectionModel();
+	if (select->hasSelection()){
+		QModelIndexList rows = select->selectedRows();
+		for (int i = 0;i < rows.count(); i++)
+		{
+			conflictsTable->item(rows[i].row(),1)->setText("NO");
+		}
+	}
+}
+
+void ConflictsView::applyFixButtonClick(){
+	signed int solution_number = solutionSelector->currentIndex();
+
+	if (solution_number == -1 || solution_output == NULL) {
+		return;
+	}
+
+	struct sfix_list * selected_solution = select_solution(solution_output, solution_number);
+	apply_fix(selected_solution);
+
+
+	ConfigList::updateListForAll();
+	for (int i = 0;i < conflictsTable->rowCount(); i++)
+	{
+		conflictsTable->item(i,2)->setText(conflictsTable->item(i,1)->text());
+	}
+	updateConflictsViewColorization();
+	QMessageBox msgBox;
+	msgBox.setText("The solution has been applied.");
+	msgBox.exec();
+}
+
+void ConflictsView::changeToYes(){
+	QItemSelectionModel *select = conflictsTable->selectionModel();
+	if (select->hasSelection()){
+		QModelIndexList rows = select->selectedRows();
+		for (int i = 0;i < rows.count(); i++)
+		{
+			conflictsTable->item(rows[i].row(),1)->setText("YES");
+		}
+	}
+
+}
+
+void ConflictsView::changeToModule() {
+	QItemSelectionModel *select = conflictsTable->selectionModel();
+	if (select->hasSelection()){
+		QModelIndexList rows = select->selectedRows();
+		for (int i = 0;i < rows.count(); i++)
+		{
+			conflictsTable->item(rows[i].row(),1)->setText("MODULE");
+		}
+	}
+
+}
+
+void ConflictsView::menuChanged(struct menu *m)
+{
+	currentSelectedMenu = m;
+}
+
+void ConflictsView::addSymbol()
+{
+	addSymbolFromMenu(currentSelectedMenu);
+}
+
+void ConflictsView::selectionChanged(QList<QTreeWidgetItem *> selection)
+{
+	currentSelection = selection;
+
+}
+
+void ConflictsView::addSymbolFromMenu(struct menu *m)
+{
+	// adds a symbol to the conflict resolver list
+	if (m != nullptr){
+		if (m->sym != nullptr){
+			struct symbol *sym = m->sym;
+			tristate currentval = sym_get_tristate_value(sym);
+			//if symbol is not added yet:
+			QAbstractItemModel *tableModel = conflictsTable->model();
+			QModelIndexList matches = tableModel->match(tableModel->index(0,0), Qt::DisplayRole, QString(sym->name));
+			if (matches.isEmpty()){
+				conflictsTable->insertRow(conflictsTable->rowCount());
+				conflictsTable->setItem(conflictsTable->rowCount()-1,0,new QTableWidgetItem(sym->name));
+				conflictsTable->setItem(conflictsTable->rowCount()-1,1,new QTableWidgetItem(tristate_value_to_string(currentval)));
+				conflictsTable->setItem(conflictsTable->rowCount()-1,2,new QTableWidgetItem(tristate_value_to_string(currentval)));
+			}else{
+				conflictsTable->item(matches[0].row(),2)->setText(tristate_value_to_string(currentval));
+			}
+		}
+	}
+}
+
+void ConflictsView::addSymbolFromContextMenu() {
+	struct menu *menu;
+
+	if (currentSelection.count() < 0){
+		return;
+	}
+	for (auto el: currentSelection){
+		ConfigItem *item = (ConfigItem *)el;
+		if (!item)
+		{
+			continue;
+		}
+		menu = item->menu;
+		addSymbolFromMenu(menu);
+	}
+
+}
+
+void ConflictsView::removeSymbol()
+{
+	QItemSelectionModel *select = conflictsTable->selectionModel();
+	QAbstractItemModel *itemModel = select->model();
+	if (select->hasSelection()){
+		QModelIndexList rows = select->selectedRows();
+		itemModel->removeRows(rows[0].row(),rows.size());
+	}
+}
+
+void ConflictsView::cellClicked(int row, int column)
+{
+	auto itemText = conflictsTable->item(row,0)->text().toUtf8().data();
+	struct property *prop;
+	struct menu *men;
+	struct symbol *sym = sym_find(itemText);
+
+	if (sym == NULL)
+		return;
+	prop = sym->prop;
+	men = prop->menu;
+	// uncommenting following like somehow disables click signal of 'apply selected solution'
+	if (sym->type == symbol_type::S_BOOLEAN) {
+		//disable module button
+		conflictsToolBar->actions()[2]->setDisabled(true);
+	} else {
+		//enable module button
+		conflictsToolBar->actions()[2]->setDisabled(false);
+	}
+	emit(conflictSelected(men));
+}
+
+void ConflictsView::changeSolutionTable(int solution_number){
+	if (solution_output == nullptr || solution_number < 0){
+		return;
+	}
+	struct sfix_list *selected_solution = select_solution(solution_output, solution_number);
+	current_solution_number = solution_number;
+	solutionTable->setRowCount(0);
+	for (unsigned int i = 0; i <selected_solution->size; i++)
+	{
+		solutionTable->insertRow(solutionTable->rowCount());
+		struct symbol_fix *cur_symbol = select_symbol(selected_solution,i);
+
+		QTableWidgetItem *symbol_name = new QTableWidgetItem(cur_symbol->sym->name);
+
+		solutionTable->setItem(solutionTable->rowCount()-1,0,symbol_name);
+
+		if (cur_symbol->type == symbolfix_type::SF_BOOLEAN){
+			QTableWidgetItem *symbol_value = new QTableWidgetItem(tristate_value_to_string(cur_symbol->tri));
+			solutionTable->setItem(solutionTable->rowCount()-1,1,symbol_value);
+		} else if(cur_symbol->type == symbolfix_type::SF_NONBOOLEAN){
+			QTableWidgetItem *symbol_value = new QTableWidgetItem(cur_symbol->nb_val.s);
+			solutionTable->setItem(solutionTable->rowCount()-1,1,symbol_value);
+		} else {
+			QTableWidgetItem *symbol_value = new QTableWidgetItem(cur_symbol->disallowed.s);
+			solutionTable->setItem(solutionTable->rowCount()-1,1,symbol_value);
+		}
+	}
+	updateConflictsViewColorization();
+}
+
+void ConflictsView::updateConflictsViewColorization(void)
+{
+	auto green = QColor(0,170,0);
+	auto red = QColor(255,0,0);
+	auto grey = QColor(180,180,180);
+
+	if (solutionTable->rowCount() == 0 || current_solution_number < 0)
+		return;
+
+	for (int i=0; i< solutionTable->rowCount(); i++) {
+		QTableWidgetItem *symbol =  solutionTable->item(i,0);
+		//symbol from solution list
+		struct sfix_list *selected_solution = select_solution(solution_output, current_solution_number);
+		struct symbol_fix *cur_symbol = select_symbol(selected_solution,i);
+
+		// if symbol is editable but the value is not the target value from solution we got, the color is red
+		// if symbol is editable but the value is the target value from solution we got, the color is green
+		// if symbol is not editable , the value is not the target value, the color is grey
+		// if symbol is not editable , the value is the target value, the color is green
+		auto editable = sym_string_within_range(cur_symbol->sym, tristate_value_to_string(cur_symbol->tri).toStdString().c_str());
+		auto _symbol = solutionTable->item(i,0)->text().toUtf8().data();
+		struct symbol *sym_ = sym_find(_symbol);
+
+		tristate current_value_of_symbol = sym_get_tristate_value(sym_);
+		tristate target_value_of_symbol = string_value_to_tristate(solutionTable->item(i,1)->text());
+		bool symbol_value_same_as_target = current_value_of_symbol == target_value_of_symbol;
+
+		if (editable && !symbol_value_same_as_target){
+			symbol->setForeground(red);
+		} else if (editable && symbol_value_same_as_target){
+			symbol->setForeground(green);
+		} else if (!editable && !symbol_value_same_as_target){
+			symbol->setForeground(grey);
+		} else if (!editable && symbol_value_same_as_target){
+			symbol->setForeground(green);
+		}
+    }
+
+}
+
+void ConflictsView::runSatConfAsync()
+{
+	//loop through the rows in conflicts table adding each row into the array:
+	struct symbol_dvalue *p = nullptr;
+	p = static_cast<struct symbol_dvalue *>(calloc(conflictsTable->rowCount(),sizeof(struct symbol_dvalue)));
+	if (!p)
+	{
+		printf("memory allocation error\n");
+		return;
+	}
+
+	struct sdv_list *wanted_symbols = sdv_list_init();
+
+	for (int i = 0; i < conflictsTable->rowCount(); i++)
+	{
+
+		struct symbol_dvalue *tmp = (p+i);
+		auto _symbol = conflictsTable->item(i,0)->text().toUtf8().data();
+		struct symbol *sym = sym_find(_symbol);
+
+		tmp->sym = sym;
+		tmp->type = static_cast<symboldv_type>(sym->type == symbol_type::S_BOOLEAN?0:1);
+		tmp->tri = string_value_to_tristate(conflictsTable->item(i,1)->text());
+		sdv_list_add(wanted_symbols,tmp);
+	}
+	fixConflictsAction_->setText("Cancel");
+	loadingAction->setVisible(true);
+
+	struct sfl_list *ret = run_satconf(wanted_symbols);
+	solution_output = ret;
+
+	free(p);
+	emit resultsReady();
+	{
+		std::lock_guard<std::mutex> lk{satconf_mutex};
+		satconf_cancelled = true;
+	}
+	satconf_cancellation_cv.notify_one();
+
+}
+
+void ConflictsView::updateResults(void)
+{
+	fixConflictsAction_->setText("Calculate Fixes");
+	loadingAction->setVisible(false);
+	if (!(solution_output == nullptr || solution_output->size == 0))
+	{
+		solutionSelector->clear();
+		for (unsigned int i = 0; i < solution_output->size; i++)
+		{
+			solutionSelector->addItem(QString::number(i+1));
+		}
+		// populate the solution table from the first solution gotten
+		numSolutionLabel->setText(QString("Solutions: (%1) found").arg(solution_output->size));
+		changeSolutionTable(0);
+	}
+	else {
+		QMessageBox msgBox;
+		msgBox.setText("All symbols are already within range.");
+		msgBox.exec();
+	}
+	if (runSatConfAsyncThread->joinable()){
+		runSatConfAsyncThread->join();
+		delete runSatConfAsyncThread;
+		runSatConfAsyncThread  = nullptr;
+	}
+
+}
+
+void ConflictsView::calculateFixes()
+{
+	if(conflictsTable->rowCount() == 0)
+	{
+		printd("table is empty\n");
+		return;
+	}
+
+	if (runSatConfAsyncThread == nullptr)
+	{
+		// fire away asynchronous call
+		std::unique_lock<std::mutex> lk{satconf_mutex};
+
+		numSolutionLabel->setText(QString("Solutions: "));
+		solutionSelector->clear();
+		solutionTable->setRowCount(0);
+		satconf_cancelled = false;
+		runSatConfAsyncThread = new std::thread(&ConflictsView::runSatConfAsync,this);
+	}else{
+		printd("Interrupting rangefix\n");
+		interrupt_rangefix();
+		std::unique_lock<std::mutex> lk{satconf_mutex};
+		satconf_cancellation_cv.wait(lk,[this] {return satconf_cancelled == true;});
+	}
+
+}
+
+void ConflictsView::changeAll(void)
+{
+	// not implemented for now
+	return;
+}
+
+ConflictsView::~ConflictsView(void)
+{
+
+}
+
+
 void fixup_rootmenu(struct menu *menu)
 {
 	struct menu *child;
@@ -1918,3 +2394,38 @@ int main(int ac, char** av)
 
 	return 0;
 }
+
+dropAbleView::dropAbleView(QWidget *parent) :
+    QTableWidget(parent) {}
+
+dropAbleView::~dropAbleView() {}
+void dropAbleView::dropEvent(QDropEvent *event)
+{
+   event->acceptProposedAction();
+}
+
+static QString tristate_value_to_string(tristate val)
+{
+	switch ( val ) {
+	case yes:
+		return QString::fromStdString("YES");
+	case mod:
+		return QString::fromStdString("MODULE");
+	case no:
+		return QString::fromStdString("NO");
+	default:
+		return QString::fromStdString("");
+	}
+
+}
+
+static tristate string_value_to_tristate(QString s){
+	if (s == "YES")
+		return tristate::yes;
+	else if (s == "MODULE")
+		return tristate::mod;
+	else if (s == "NO")
+		return tristate::no;
+	else
+		return tristate::no;
+}
scripts/kconfig/qconf.h +103 -0

146 lines not on this page · raw message

Signature · 1 line
2.39.2
#23 lore raw

Re: [PATCH v4 11/12] kconfig: Add xconfig-modifications

Quoted 62 lines · Ole Schuerks

The tool can be called from any configurator. We chose to modify xconfig for this purpose. These files contain the necessary modifications to xconfig in order to resolve conflicts.

---
 scripts/kconfig/qconf.cc | 515 ++++++++++++++++++++++++++++++++++++++-
 scripts/kconfig/qconf.h  | 103 ++++++++
 2 files changed, 616 insertions(+), 2 deletions(-)
scripts/kconfig/qconf.cc +14 -1
index 7d239c032b3d..20ca9936d592 100644
@@ -3,7 +3,7 @@
  * Copyright (C) 2002 Roman Zippel <[email protected]>
  * Copyright (C) 2015 Boris Barbulovski <[email protected]>
  */
-
+#include "qnamespace.h"
 #include <QAction>
 #include <QActionGroup>
 #include <QApplication>
@@ -19,14 +19,27 @@
 #include <QRegularExpression>
 #include <QScreen>
 #include <QToolBar>
+#include <QListWidget>
+#include <QComboBox>
+#include <QTableWidget>
+#include <QHBoxLayout>
+#include <QMovie>
+#include <QMessageBox>
 
 #include <stdlib.h>
+#include <QAbstractItemView>
+#include <QMimeData>
+#include <QBrush>
+#include <QColor>
 
 #include "lkc.h"
 #include "qconf.h"
+#include "configfix.h"
 
 #include "images.h"
 
+static QString tristate_value_to_string(tristate val);
+static tristate string_value_to_tristate(QString s);
 
 static QApplication *configApp;
 static ConfigSettings *configSettings;

scripts/kconfig/qconf.cc void ConfigItem::updateMenu(void) · line 195

Quoted 5 lines
scripts/kconfig/qconf.cc +1 -0
@@ -178,6 +191,7 @@ void ConfigItem::updateMenu(void)
                prompt += " (NEW)";
 set_prompt:
        setText(promptColIdx, prompt);
+
 }

Unrelated change.
(Same in many places)

Quoted 226 lines
scripts/kconfig/qconf.cc +128 -1
 void ConfigItem::testUpdateMenu(bool v)
@@ -225,6 +239,7 @@ void ConfigItem::init(void)
                }
        }
        updateMenu();
+
 }
 
 /*
@@ -405,7 +420,7 @@ void ConfigList::updateSelection(void)
        ConfigItem* item = (ConfigItem*)selectedItems().first();
        if (!item)
                return;
-
+       emit selectionChanged(selectedItems());
        menu = item->menu;
        emit menuChanged(menu);
        if (!menu)
@@ -471,6 +486,7 @@ void ConfigList::updateListForAll()
 
                list->updateList();
        }
+
 }
 
 void ConfigList::updateListAllForAll()
@@ -539,6 +555,7 @@ void ConfigList::changeValue(ConfigItem* item)
                }
                if (oldexpr != newexpr)
                        ConfigList::updateListForAll();
+                       emit updateConflictsViewColorization();
                break;
        default:
                break;
@@ -898,6 +915,7 @@ void ConfigList::contextMenuEvent(QContextMenuEvent *e)
                        action, &QAction::setChecked);
                action->setChecked(showName);
                headerPopup->addAction(action);
+               headerPopup->addAction(addSymbolFromContextMenu);
        }
 
        headerPopup->exec(e->globalPos());
@@ -918,6 +936,7 @@ QList<ConfigList *> ConfigList::allLists;
 QAction *ConfigList::showNormalAction;
 QAction *ConfigList::showAllAction;
 QAction *ConfigList::showPromptAction;
+QAction *ConfigList::addSymbolFromContextMenu;
 
 void ConfigList::setAllOpen(bool open)
 {
@@ -1249,7 +1268,10 @@ ConfigSearchWindow::ConfigSearchWindow(ConfigMainWindow *parent)
                info, &ConfigInfoView::setInfo);
        connect(list, &ConfigList::menuChanged,
                parent, &ConfigMainWindow::setMenuLink);
+       connect(list, &ConfigList::menuChanged,
+               parent, &ConfigMainWindow::conflictSelected);
 
+       connect(list,&ConfigList::updateConflictsViewColorization,this,&ConfigSearchWindow::updateConflictsViewColorizationFowarder);
        layout1->addWidget(split);
 
        QVariant x, y;
@@ -1272,6 +1294,10 @@ ConfigSearchWindow::ConfigSearchWindow(ConfigMainWindow *parent)
                this, &ConfigSearchWindow::saveSettings);
 }
 
+void ConfigSearchWindow::updateConflictsViewColorizationFowarder(void){
+       emit updateConflictsViewColorization();
+}
+
 void ConfigSearchWindow::saveSettings(void)
 {
        if (!objectName().isEmpty()) {
@@ -1364,6 +1390,17 @@ ConfigMainWindow::ConfigMainWindow(void)
        split1->addWidget(configList);
        split1->addWidget(menuList);
        split2->addWidget(helpText);
+       split3 = new QSplitter(split2);
+       split3->setOrientation(Qt::Vertical);
+       conflictsView = new ConflictsView(split3, "help");
+       /* conflictsSelected signal in conflictsview triggers when a conflict is selected
+                in the view. this line connects that event to conflictselected event in mainwindow
+                which updates the selection to match (in the configlist) the symbol that was selected.
+       */
+       connect(conflictsView,SIGNAL(conflictSelected(struct menu *)),SLOT(conflictSelected(struct menu *)));
+       connect(conflictsView,SIGNAL(refreshMenu()),SLOT(refreshMenu()));
+       connect(menuList,SIGNAL(updateConflictsViewColorization()),conflictsView,SLOT(updateConflictsViewColorization()));
+       connect(configList,SIGNAL(updateConflictsViewColorization()),conflictsView,SLOT(updateConflictsViewColorization()));
 
        setTabOrder(configList, helpText);
        configList->setFocus();
@@ -1430,6 +1467,8 @@ ConfigMainWindow::ConfigMainWindow(void)
        ConfigList::showAllAction->setCheckable(true);
        ConfigList::showPromptAction = new QAction("Show Prompt Options", optGroup);
        ConfigList::showPromptAction->setCheckable(true);
+       ConfigList::addSymbolFromContextMenu = new QAction("Add symbol from context menu");
+       connect(ConfigList::addSymbolFromContextMenu, &QAction::triggered, conflictsView, &ConflictsView::addSymbol);
 
        QAction *showDebugAction = new QAction("Show Debug Info", this);
          showDebugAction->setCheckable(true);
@@ -1485,6 +1524,8 @@ ConfigMainWindow::ConfigMainWindow(void)
 
        connect(configList, &ConfigList::menuChanged,
                helpText, &ConfigInfoView::setInfo);
+       connect(configList, &ConfigList::menuChanged,
+               conflictsView, &ConflictsView::menuChanged);
        connect(configList, &ConfigList::menuSelected,
                this, &ConfigMainWindow::changeMenu);
        connect(configList, &ConfigList::itemSelected,
@@ -1493,6 +1534,8 @@ ConfigMainWindow::ConfigMainWindow(void)
                this, &ConfigMainWindow::goBack);
        connect(menuList, &ConfigList::menuChanged,
                helpText, &ConfigInfoView::setInfo);
+       connect(menuList, &ConfigList::menuChanged,
+               conflictsView, &ConflictsView::menuChanged);
        connect(menuList, &ConfigList::menuSelected,
                this, &ConfigMainWindow::changeMenu);
 
@@ -1712,6 +1755,13 @@ void ConfigMainWindow::showSplitView(void)
        menuList->setFocus();
 }
 
+void ConfigMainWindow::conflictSelected(struct menu * men)
+{
+       configList->clearSelection();
+       menuList->clearSelection();
+       emit(setMenuLink(men));
+}
+
 void ConfigMainWindow::showFullView(void)
 {
        singleViewAction->setEnabled(true);
@@ -1847,6 +1897,432 @@ void ConfigMainWindow::conf_changed(bool dirty)
                saveAction->setEnabled(dirty);
 }
 
+void ConfigMainWindow::refreshMenu(void)
+{
+       configList->updateListAll();
+}
+
+void QTableWidget::dropEvent(QDropEvent *event)
+{
+}
+
+ConflictsView::ConflictsView(QWidget *parent, const char *name)
+       : Parent(parent)
+{
+       currentSelectedMenu = nullptr;
+       setObjectName(name);
+       QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
+       QVBoxLayout *verticalLayout = new QVBoxLayout();
+       verticalLayout->setContentsMargins(0, 0, 0, 0);
+       conflictsToolBar = new QToolBar("ConflictTools", this);
+       // toolbar buttons [n] [m] [y] [calculate fixes] [remove]
+       QAction *addSymbol = new QAction("Add Symbol");
+       QAction *setConfigSymbolAsNo = new QAction("N");
+       QAction *setConfigSymbolAsModule = new QAction("M");
+       QAction *setConfigSymbolAsYes = new QAction("Y");
+       fixConflictsAction_ = new QAction("Calculate Fixes");
+       QAction *removeSymbol = new QAction("Remove Symbol");
+       QMovie *loadingGif = new QMovie("scripts/kconfig/loader.gif");
+       auto loadingLabel = new QLabel;
+
+       if (loadingGif->isValid()) {
+               loadingGif->setScaledSize(loadingGif->scaledSize().scaled(
+                       20, 20, Qt::KeepAspectRatioByExpanding));
+               loadingGif->start();
+               loadingLabel->setMovie(loadingGif);
+       } else {
+               loadingLabel->setText("Calculating...");
+       }
+
+       //if you change the order of buttons here, change the code where
+       //module button was disabled if symbol is boolean, selecting module button
+       //depends on a specific index in list of action
+       fixConflictsAction_->setCheckable(false);
+       conflictsToolBar->addAction(addSymbol);
+       conflictsToolBar->addAction(setConfigSymbolAsNo);
+       conflictsToolBar->addAction(setConfigSymbolAsModule);
+       conflictsToolBar->addAction(setConfigSymbolAsYes);
+       conflictsToolBar->addAction(fixConflictsAction_);
+       conflictsToolBar->addAction(removeSymbol);
+       // loadingLabel->setMargin(5);
+       loadingLabel->setContentsMargins(5, 5, 5, 5);
+       loadingAction = conflictsToolBar->addWidget(loadingLabel);
+       loadingAction->setVisible(false);
+
+
+       verticalLayout->addWidget(conflictsToolBar);
+
+       connect(addSymbol, &QAction::triggered, this, &ConflictsView::addSymbol);
+       connect(setConfigSymbolAsNo, &QAction::triggered,this, &ConflictsView::changeToNo);
+       connect(setConfigSymbolAsModule, &QAction::triggered,this, &ConflictsView::changeToModule);
+       connect(setConfigSymbolAsYes, &QAction::triggered,this, &ConflictsView::changeToYes);
+       connect(removeSymbol, &QAction::triggered,this, &ConflictsView::removeSymbol);
+       connect(this, SIGNAL(resultsReady()), SLOT(updateResults()));
+       //connect clicking 'calculate fixes' to 'change all symbol values to fix all conflicts'
+       // no longer used anymore for now.
+       connect(fixConflictsAction_, &QAction::triggered,this, &ConflictsView::calculateFixes);
+
+       conflictsTable = (QTableWidget *) new dropAbleView(this);
+       conflictsTable->setRowCount(0);
+       conflictsTable->setColumnCount(3);
+       conflictsTable->setSelectionBehavior(QAbstractItemView::SelectRows);
+
+       conflictsTable->setHorizontalHeaderLabels(QStringList()  << "Option" << "Wanted value" << "Current value" );
+       verticalLayout->addWidget(conflictsTable);
+
+       conflictsTable->setDragDropMode(QAbstractItemView::DropOnly);
+       setAcceptDrops(true);
+
+       connect(conflictsTable, SIGNAL(cellClicked(int, int)), SLOT(cellClicked(int,int)));
+       horizontalLayout->addLayout(verticalLayout);
+
+       // populate the solution view on the right hand side:
+       QVBoxLayout *solutionLayout = new QVBoxLayout();
+       solutionLayout->setContentsMargins(0, 0, 0, 0);
+       solutionSelector = new QComboBox();
+       connect(solutionSelector, QOverload<int>::of(&QComboBox::currentIndexChanged),
+               [=](int index){ changeSolutionTable(index); });
+       solutionTable = new QTableWidget();
+       solutionTable->setRowCount(0);
+       solutionTable->setColumnCount(2);
+       solutionTable->setHorizontalHeaderLabels(QStringList()  << "Symbol" << "New Value" );
+
+       applyFixButton = new QPushButton("Apply Selected solution");

scripts/kconfig/qconf.cc void ConfigMainWindow::conf_changed(bool dirty) · line 2000

Quoted 10 lines
scripts/kconfig/qconf.cc +10 -0
+       connect(applyFixButton, SIGNAL(clicked(bool)), SLOT(applyFixButtonClick()));
+
+       numSolutionLabel = new QLabel("Solutions:");
+       solutionLayout->addWidget(numSolutionLabel);
+       solutionLayout->addWidget(solutionSelector);
+       solutionLayout->addWidget(solutionTable);
+       solutionLayout->addWidget(applyFixButton);
+
+       horizontalLayout->addLayout(solutionLayout);
+}

The "Current value" does not reflect the current value.

1. "Add Symbol" button.
2. Edit the "Option" column

-> The "Current value" stay the same
(it shows the value of the previous symbol)

The "Wanted value" should toggle when you click on the cell.

See how the existing window works.
The values should be "Y", "M", "N" instead of "YES", "MODULE", "NO", for consistency.

Quoted 374 lines
scripts/kconfig/qconf.cc +359 -0
+
+void ConflictsView::changeToNo(){
+       QItemSelectionModel *select = conflictsTable->selectionModel();
+       if (select->hasSelection()){
+               QModelIndexList rows = select->selectedRows();
+               for (int i = 0;i < rows.count(); i++)
+               {
+                       conflictsTable->item(rows[i].row(),1)->setText("NO");
+               }
+       }
+}
+
+void ConflictsView::applyFixButtonClick(){
+       signed int solution_number = solutionSelector->currentIndex();
+
+       if (solution_number == -1 || solution_output == NULL) {
+               return;
+       }
+
+       struct sfix_list * selected_solution = select_solution(solution_output, solution_number);
+       apply_fix(selected_solution);
+
+
+       ConfigList::updateListForAll();
+       for (int i = 0;i < conflictsTable->rowCount(); i++)
+       {
+               conflictsTable->item(i,2)->setText(conflictsTable->item(i,1)->text());
+       }
+       updateConflictsViewColorization();
+       QMessageBox msgBox;
+       msgBox.setText("The solution has been applied.");
+       msgBox.exec();
+}
+
+void ConflictsView::changeToYes(){
+       QItemSelectionModel *select = conflictsTable->selectionModel();
+       if (select->hasSelection()){
+               QModelIndexList rows = select->selectedRows();
+               for (int i = 0;i < rows.count(); i++)
+               {
+                       conflictsTable->item(rows[i].row(),1)->setText("YES");
+               }
+       }
+
+}
+
+void ConflictsView::changeToModule() {
+       QItemSelectionModel *select = conflictsTable->selectionModel();
+       if (select->hasSelection()){
+               QModelIndexList rows = select->selectedRows();
+               for (int i = 0;i < rows.count(); i++)
+               {
+                       conflictsTable->item(rows[i].row(),1)->setText("MODULE");
+               }
+       }
+
+}
+
+void ConflictsView::menuChanged(struct menu *m)
+{
+       currentSelectedMenu = m;
+}
+
+void ConflictsView::addSymbol()
+{
+       addSymbolFromMenu(currentSelectedMenu);
+}
+
+void ConflictsView::selectionChanged(QList<QTreeWidgetItem *> selection)
+{
+       currentSelection = selection;
+
+}
+
+void ConflictsView::addSymbolFromMenu(struct menu *m)
+{
+       // adds a symbol to the conflict resolver list
+       if (m != nullptr){
+               if (m->sym != nullptr){
+                       struct symbol *sym = m->sym;
+                       tristate currentval = sym_get_tristate_value(sym);
+                       //if symbol is not added yet:
+                       QAbstractItemModel *tableModel = conflictsTable->model();
+                       QModelIndexList matches = tableModel->match(tableModel->index(0,0), Qt::DisplayRole, QString(sym->name));
+                       if (matches.isEmpty()){
+                               conflictsTable->insertRow(conflictsTable->rowCount());
+                               conflictsTable->setItem(conflictsTable->rowCount()-1,0,new QTableWidgetItem(sym->name));
+                               conflictsTable->setItem(conflictsTable->rowCount()-1,1,new QTableWidgetItem(tristate_value_to_string(currentval)));
+                               conflictsTable->setItem(conflictsTable->rowCount()-1,2,new QTableWidgetItem(tristate_value_to_string(currentval)));
+                       }else{
+                               conflictsTable->item(matches[0].row(),2)->setText(tristate_value_to_string(currentval));
+                       }
+               }
+       }
+}
+
+void ConflictsView::addSymbolFromContextMenu() {
+       struct menu *menu;
+
+       if (currentSelection.count() < 0){
+               return;
+       }
+       for (auto el: currentSelection){
+               ConfigItem *item = (ConfigItem *)el;
+               if (!item)
+               {
+                       continue;
+               }
+               menu = item->menu;
+               addSymbolFromMenu(menu);
+       }
+
+}
+
+void ConflictsView::removeSymbol()
+{
+       QItemSelectionModel *select = conflictsTable->selectionModel();
+       QAbstractItemModel *itemModel = select->model();
+       if (select->hasSelection()){
+               QModelIndexList rows = select->selectedRows();
+               itemModel->removeRows(rows[0].row(),rows.size());
+       }
+}
+
+void ConflictsView::cellClicked(int row, int column)
+{
+       auto itemText = conflictsTable->item(row,0)->text().toUtf8().data();
+       struct property *prop;
+       struct menu *men;
+       struct symbol *sym = sym_find(itemText);
+
+       if (sym == NULL)
+               return;
+       prop = sym->prop;
+       men = prop->menu;
+       // uncommenting following like somehow disables click signal of 'apply selected solution'
+       if (sym->type == symbol_type::S_BOOLEAN) {
+               //disable module button
+               conflictsToolBar->actions()[2]->setDisabled(true);
+       } else {
+               //enable module button
+               conflictsToolBar->actions()[2]->setDisabled(false);
+       }
+       emit(conflictSelected(men));
+}
+
+void ConflictsView::changeSolutionTable(int solution_number){
+       if (solution_output == nullptr || solution_number < 0){
+               return;
+       }
+       struct sfix_list *selected_solution = select_solution(solution_output, solution_number);
+       current_solution_number = solution_number;
+       solutionTable->setRowCount(0);
+       for (unsigned int i = 0; i <selected_solution->size; i++)
+       {
+               solutionTable->insertRow(solutionTable->rowCount());
+               struct symbol_fix *cur_symbol = select_symbol(selected_solution,i);
+
+               QTableWidgetItem *symbol_name = new QTableWidgetItem(cur_symbol->sym->name);
+
+               solutionTable->setItem(solutionTable->rowCount()-1,0,symbol_name);
+
+               if (cur_symbol->type == symbolfix_type::SF_BOOLEAN){
+                       QTableWidgetItem *symbol_value = new QTableWidgetItem(tristate_value_to_string(cur_symbol->tri));
+                       solutionTable->setItem(solutionTable->rowCount()-1,1,symbol_value);
+               } else if(cur_symbol->type == symbolfix_type::SF_NONBOOLEAN){
+                       QTableWidgetItem *symbol_value = new QTableWidgetItem(cur_symbol->nb_val.s);
+                       solutionTable->setItem(solutionTable->rowCount()-1,1,symbol_value);
+               } else {
+                       QTableWidgetItem *symbol_value = new QTableWidgetItem(cur_symbol->disallowed.s);
+                       solutionTable->setItem(solutionTable->rowCount()-1,1,symbol_value);
+               }
+       }
+       updateConflictsViewColorization();
+}
+
+void ConflictsView::updateConflictsViewColorization(void)
+{
+       auto green = QColor(0,170,0);
+       auto red = QColor(255,0,0);
+       auto grey = QColor(180,180,180);
+
+       if (solutionTable->rowCount() == 0 || current_solution_number < 0)
+               return;
+
+       for (int i=0; i< solutionTable->rowCount(); i++) {
+               QTableWidgetItem *symbol =  solutionTable->item(i,0);
+               //symbol from solution list
+               struct sfix_list *selected_solution = select_solution(solution_output, current_solution_number);
+               struct symbol_fix *cur_symbol = select_symbol(selected_solution,i);
+
+               // if symbol is editable but the value is not the target value from solution we got, the color is red
+               // if symbol is editable but the value is the target value from solution we got, the color is green
+               // if symbol is not editable , the value is not the target value, the color is grey
+               // if symbol is not editable , the value is the target value, the color is green
+               auto editable = sym_string_within_range(cur_symbol->sym, tristate_value_to_string(cur_symbol->tri).toStdString().c_str());
+               auto _symbol = solutionTable->item(i,0)->text().toUtf8().data();
+               struct symbol *sym_ = sym_find(_symbol);
+
+               tristate current_value_of_symbol = sym_get_tristate_value(sym_);
+               tristate target_value_of_symbol = string_value_to_tristate(solutionTable->item(i,1)->text());
+               bool symbol_value_same_as_target = current_value_of_symbol == target_value_of_symbol;
+
+               if (editable && !symbol_value_same_as_target){
+                       symbol->setForeground(red);
+               } else if (editable && symbol_value_same_as_target){
+                       symbol->setForeground(green);
+               } else if (!editable && !symbol_value_same_as_target){
+                       symbol->setForeground(grey);
+               } else if (!editable && symbol_value_same_as_target){
+                       symbol->setForeground(green);
+               }
+    }
+
+}
+
+void ConflictsView::runSatConfAsync()
+{
+       //loop through the rows in conflicts table adding each row into the array:
+       struct symbol_dvalue *p = nullptr;
+       p = static_cast<struct symbol_dvalue *>(calloc(conflictsTable->rowCount(),sizeof(struct symbol_dvalue)));
+       if (!p)
+       {
+               printf("memory allocation error\n");
+               return;
+       }
+
+       struct sdv_list *wanted_symbols = sdv_list_init();
+
+       for (int i = 0; i < conflictsTable->rowCount(); i++)
+       {
+
+               struct symbol_dvalue *tmp = (p+i);
+               auto _symbol = conflictsTable->item(i,0)->text().toUtf8().data();
+               struct symbol *sym = sym_find(_symbol);
+
+               tmp->sym = sym;
+               tmp->type = static_cast<symboldv_type>(sym->type == symbol_type::S_BOOLEAN?0:1);
+               tmp->tri = string_value_to_tristate(conflictsTable->item(i,1)->text());
+               sdv_list_add(wanted_symbols,tmp);
+       }
+       fixConflictsAction_->setText("Cancel");
+       loadingAction->setVisible(true);
+
+       struct sfl_list *ret = run_satconf(wanted_symbols);
+       solution_output = ret;
+
+       free(p);
+       emit resultsReady();
+       {
+               std::lock_guard<std::mutex> lk{satconf_mutex};
+               satconf_cancelled = true;
+       }
+       satconf_cancellation_cv.notify_one();
+
+}
+
+void ConflictsView::updateResults(void)
+{
+       fixConflictsAction_->setText("Calculate Fixes");
+       loadingAction->setVisible(false);
+       if (!(solution_output == nullptr || solution_output->size == 0))
+       {
+               solutionSelector->clear();
+               for (unsigned int i = 0; i < solution_output->size; i++)
+               {
+                       solutionSelector->addItem(QString::number(i+1));
+               }
+               // populate the solution table from the first solution gotten
+               numSolutionLabel->setText(QString("Solutions: (%1) found").arg(solution_output->size));
+               changeSolutionTable(0);
+       }
+       else {
+               QMessageBox msgBox;
+               msgBox.setText("All symbols are already within range.");
+               msgBox.exec();
+       }
+       if (runSatConfAsyncThread->joinable()){
+               runSatConfAsyncThread->join();
+               delete runSatConfAsyncThread;
+               runSatConfAsyncThread  = nullptr;
+       }
+
+}
+
+void ConflictsView::calculateFixes()
+{
+       if(conflictsTable->rowCount() == 0)
+       {
+               printd("table is empty\n");
+               return;
+       }
+
+       if (runSatConfAsyncThread == nullptr)
+       {
+               // fire away asynchronous call
+               std::unique_lock<std::mutex> lk{satconf_mutex};
+
+               numSolutionLabel->setText(QString("Solutions: "));
+               solutionSelector->clear();
+               solutionTable->setRowCount(0);
+               satconf_cancelled = false;
+               runSatConfAsyncThread = new std::thread(&ConflictsView::runSatConfAsync,this);
+       }else{
+               printd("Interrupting rangefix\n");
+               interrupt_rangefix();
+               std::unique_lock<std::mutex> lk{satconf_mutex};
+               satconf_cancellation_cv.wait(lk,[this] {return satconf_cancelled == true;});
+       }
+
+}
+
+void ConflictsView::changeAll(void)
+{
+       // not implemented for now
+       return;
+}
+
+ConflictsView::~ConflictsView(void)
+{
+
+}
+
+
 void fixup_rootmenu(struct menu *menu)
 {
        struct menu *child;
@@ -1918,3 +2394,38 @@ int main(int ac, char** av)
 
        return 0;
 }
+
+dropAbleView::dropAbleView(QWidget *parent) :
+    QTableWidget(parent) {}
+
+dropAbleView::~dropAbleView() {}
+void dropAbleView::dropEvent(QDropEvent *event)
+{
+   event->acceptProposedAction();
+}
+
+static QString tristate_value_to_string(tristate val)
+{
+       switch ( val ) {
+       case yes:
+               return QString::fromStdString("YES");
+       case mod:
+               return QString::fromStdString("MODULE");
+       case no:
+               return QString::fromStdString("NO");
+       default:
+               return QString::fromStdString("");
+       }
+
+}
+
+static tristate string_value_to_tristate(QString s){
+       if (s == "YES")
+               return tristate::yes;
+       else if (s == "MODULE")
+               return tristate::mod;
+       else if (s == "NO")
+               return tristate::no;
+       else
+               return tristate::no;
+}
scripts/kconfig/qconf.h +0 -0
index 53373064d90a..1ace9f673fd0 100644
@@ -14,9 +14,16 @@
 #include <QStyledItemDelegate>
 #include <QTextBrowser>
 #include <QTreeWidget>

scripts/kconfig/qconf.h line 26

Quoted 10 lines
scripts/kconfig/qconf.h +7 -0
+#include <QTableWidget>
+#include <QList>
+#include <QComboBox>
+#include <QLabel>
+#include <thread>
+#include <condition_variable>
 
 #include "expr.h"
 
+

Unrelated change.
(Do not add excessive brank line)

Quoted 139 lines
scripts/kconfig/qconf.h +96 -1
 class ConfigList;
 class ConfigItem;
 class ConfigMainWindow;
@@ -80,6 +87,8 @@ public slots:
        void parentSelected(void);
        void gotFocus(struct menu *);
        void showNameChanged(bool on);
+       void selectionChanged(QList<QTreeWidgetItem *> selection);
+       void updateConflictsViewColorization();
 
 public:
        void updateListAll(void)
@@ -111,6 +120,82 @@ public slots:
        static void updateListAllForAll();
 
        static QAction *showNormalAction, *showAllAction, *showPromptAction;
+       static QAction *addSymbolFromContextMenu;
+
+};
+
+class ConflictsView : public QWidget {
+       Q_OBJECT
+       typedef class QWidget Parent;
+private:
+       QAction *loadingAction;
+public:
+       ConflictsView(QWidget *parent, const char *name = 0);
+       ~ConflictsView(void);
+       void addSymbolFromMenu(struct menu *m);
+       int current_solution_number = -1;
+
+public slots:
+       void cellClicked(int, int);
+       void changeAll();
+       // triggered by Qactions on the tool bar that adds/remove symbol
+       void addSymbol();
+       // triggered from config list right click -> add symbols
+       void addSymbolFromContextMenu();
+       void removeSymbol();
+       void menuChanged(struct menu *);
+       void changeToNo();
+       void changeToYes();
+       void changeToModule();
+       void selectionChanged(QList<QTreeWidgetItem *> selection);
+
+
+       void applyFixButtonClick();
+       void updateConflictsViewColorization();
+       void updateResults();
+
+
+
+       // switches the solution table with selected solution index from  solution_output
+       void changeSolutionTable(int solution_number);
+
+       // calls satconfig to solve to get wanted value to current value
+       void calculateFixes();
+signals:
+       void showNameChanged(bool);
+       void showRangeChanged(bool);
+       void showDataChanged(bool);
+       void conflictSelected(struct menu *);
+       void refreshMenu();
+       void resultsReady();
+public:
+       QTableWidget *conflictsTable;
+
+       // the comobox on the right hand side. used to select a solution after
+       // getting solution from satconfig
+       QComboBox *solutionSelector{nullptr};
+
+       // the table which shows the selected solution showing variable = New value changes
+       QTableWidget *solutionTable{nullptr};
+
+       // Apply fixes button on the solution view
+       QPushButton *applyFixButton{nullptr};
+
+       struct sfl_list *solution_output{nullptr};
+
+       QToolBar *conflictsToolBar;
+       struct menu *currentSelectedMenu;
+       QLabel *numSolutionLabel{nullptr};
+       // currently selected config items in configlist.
+       QList<QTreeWidgetItem *> currentSelection;
+       QAction *fixConflictsAction_{nullptr};
+       void runSatConfAsync();
+       std::thread *runSatConfAsyncThread{nullptr};
+
+       std::mutex satconf_mutex;
+       std::condition_variable satconf_cancellation_cv;
+       bool satconf_cancelled{false};
+
 };
 
 class ConfigItem : public QTreeWidgetItem {
@@ -223,6 +308,9 @@ class ConfigSearchWindow : public QDialog {
 public slots:
        void saveSettings(void);
        void search(void);
+       void updateConflictsViewColorizationFowarder();
+signals:
+       void updateConflictsViewColorization();
 
 protected:
        QLineEdit* editField;
@@ -258,6 +346,8 @@ public slots:
        void showIntro(void);
        void showAbout(void);
        void saveSettings(void);
+       void conflictSelected(struct menu *);
+       void refreshMenu();
 
 protected:
        void closeEvent(QCloseEvent *e);
@@ -266,10 +356,23 @@ public slots:
        ConfigList *menuList;
        ConfigList *configList;
        ConfigInfoView *helpText;
+       ConflictsView *conflictsView;
+       QToolBar *conflictsToolBar;
        QAction *backAction;
        QAction *singleViewAction;
        QAction *splitViewAction;
        QAction *fullViewAction;
        QSplitter *split1;
        QSplitter *split2;
+       QSplitter *split3;
+};
+
+class dropAbleView : public QTableWidget
+{
+public:
+       dropAbleView(QWidget *parent = nullptr);
+       ~dropAbleView();
+
+protected:
+       void dropEvent(QDropEvent *event);
 };
--
Quoted 3 lines

2.39.2

--
Best Regards
Masahiro Yamada

#24 lore raw

PATCH v4 12/12 kconfig: Add loader.gif

In reply to Ole Schuerks

Add the GIF used as the loading indicator.

We created the file from scratch using GIMP. It's also available at
https://github.com/delta-one/linux/blob/configfix_20240607-patches-5/scripts/kconfig/loader.gif

---
 scripts/kconfig/loader.gif | Bin 0 -> 4177 bytes
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 scripts/kconfig/loader.gif
scripts/kconfig/loader.gif new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..54c8bc76cc037ab04dc1d37ddc34384f9714284e
GIT binary patch
Signature · 1 line
2.39.2