aboutsummaryrefslogtreecommitdiff
path: root/scripts/parse_kconfig/boolexpr.c
blob: 4d5865482309d1116a1fdeb4c69b762b94609cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "boolexpr.h"

struct boolexpr *boolexpr_eql(struct symlist *sl, struct symbol *sym1,
                              struct symbol *sym2);

struct boolexpr *boolexpr_kconfig(struct symlist *sl, struct expr *expr,
                                  bool modulesym) {
    struct stck {
        struct expr *expr;
        struct boolexpr *bl;
    };
    struct expr **back;
    int back_size = 2, back_pos = -1;
    back = malloc((unsigned) back_size * sizeof(struct expr *));
    struct stck *stack;
    unsigned stack_size = 2;
    int stack_pos = -1;
    stack = malloc((unsigned) stack_size * sizeof(struct stck));
    struct boolexpr *rtn = NULL;

    while (expr != NULL) {
        if ((back_pos >= 0 && back[back_pos] != expr) || back_pos < 0) {
            if (++back_pos >= back_size) {
                back_size *= 2;
                back =
                    realloc(back,
                            (unsigned) back_size * sizeof(struct expr *));
            }
            back[back_pos] = expr;
        }
        switch (expr->type) {
        case E_SYMBOL:
            rtn = boolexpr_sym(sl, expr->left.sym, modulesym);
            goto go_up;
        case E_NOT:
            if (rtn == NULL)
                expr = expr->left.expr;
            else {
                rtn = boolexpr_not(rtn);
                goto go_up;
            }
            break;
        case E_OR:
        case E_AND:
            if (stack_pos < 0 || stack[stack_pos].expr != expr) {
                if (rtn == NULL)
                    expr = expr->left.expr;
                else {
                    if (++stack_pos >= stack_size) {
                        stack_size *= 2;
                        stack =
                            realloc(stack,
                                    stack_size * sizeof(struct stck));
                    }
                    stack[stack_pos].expr = expr;
                    stack[stack_pos].bl = rtn;
                    expr = expr->right.expr;
                    rtn = NULL;
                }
            } else {
                if (expr->type == E_OR)
                    rtn = boolexpr_or(stack[stack_pos].bl, rtn);
                else
                    rtn = boolexpr_and(stack[stack_pos].bl, rtn);
                stack_pos--;
                goto go_up;
            }
            break;
        case E_EQUAL:
            rtn = boolexpr_eql(sl, expr->left.sym, expr->right.sym);
            goto go_up;
        case E_UNEQUAL:
            rtn =
                boolexpr_not(boolexpr_eql
                             (sl, expr->left.sym, expr->right.sym));
            goto go_up;
        default:
            fprintf(stderr, "ERROR: Unknown expression type.\n");
        }
        continue;

      go_up:
        if (--back_pos >= 0)
            expr = back[back_pos];
        else
            expr = NULL;
    }

    free(back);
    free(stack);

    return rtn;
}

struct boolexpr *boolexpr_true() {
    struct boolexpr *rtn;
    rtn = malloc(sizeof(struct boolexpr));
    rtn->type = BT_TRUE;
    rtn->overusage = 0;
    rtn->id = 0;
    return rtn;
}

struct boolexpr *boolexpr_false() {
    struct boolexpr *rtn;
    rtn = malloc(sizeof(struct boolexpr));
    rtn->type = BT_FALSE;
    rtn->overusage = 0;
    rtn->id = 0;
    return rtn;
}

struct boolexpr *boolexpr_sym(struct symlist *sl, struct symbol *sym,
                              bool modulesym) {
    struct boolexpr *rtn;
    rtn = malloc(sizeof(struct boolexpr));
    rtn->overusage = 0;
    if (!strcmp(sym->name, "m")) {
        if (modulesym)
            rtn->type = BT_TRUE;
        else
            rtn->type = BT_FALSE;
    } else if (!strcmp(sym->name, "n")) {
        rtn->type = BT_FALSE;
    } else if (!strcmp(sym->name, "y")) {
        rtn->type = BT_TRUE;
    } else {
        rtn->id = symlist_id(sl, sym->name);
        if (rtn->id != 0)
            rtn->type = BT_SYM;
        else
            rtn->type = BT_FALSE;
    }
    return rtn;
}

struct boolexpr *boolexpr_eql(struct symlist *sl,
                              struct symbol *sym1, struct symbol *sym2) {
    if (!strcmp(sym2->name, "m")) {
        struct boolexpr *rtn = malloc(sizeof(struct boolexpr));
        rtn->overusage = 0;
        rtn->id = 0;
        rtn->type = BT_FALSE;
        return rtn;
    }
    if (!strcmp(sym2->name, "n"))
        return boolexpr_not(boolexpr_sym(sl, sym1, false));
    if (!strcmp(sym2->name, "y"))
        return boolexpr_sym(sl, sym1, false);
    // sym1 <-> sym2
    // (!sym1 || sym2) && (sym1 || !sym2)
    return
        boolexpr_and(boolexpr_or
                     (boolexpr_not(boolexpr_sym(sl, sym1, false)),
                      boolexpr_sym(sl, sym2, false)),
                     boolexpr_or(boolexpr_sym(sl, sym1, false),
                                 boolexpr_not(boolexpr_sym
                                              (sl, sym2, false))));
}

struct boolexpr *boolexpr_or(struct boolexpr *e1, struct boolexpr *e2) {
    switch (e1->type) {
    case BT_TRUE:
        boolexpr_free(e2);
        return e1;
    case BT_FALSE:
        boolexpr_free(e1);
        return e2;
    default:
        switch (e2->type) {
        case BT_TRUE:
            boolexpr_free(e1);
            return e2;
        case BT_FALSE:
            boolexpr_free(e2);
            return e1;
        default:
            break;
        }
        break;
    }

    struct boolexpr *rtn;
    rtn = malloc(sizeof(struct boolexpr));
    rtn->type = BT_OR;
    rtn->overusage = 0;
    rtn->id = 0;
    rtn->left = e1;
    rtn->right = e2;
    return rtn;
}

struct boolexpr *boolexpr_and(struct boolexpr *e1, struct boolexpr *e2) {
    switch (e1->type) {
    case BT_FALSE:
        boolexpr_free(e2);
        return e1;
    case BT_TRUE:
        boolexpr_free(e1);
        return e2;
    default:
        switch (e2->type) {
        case BT_FALSE:
            boolexpr_free(e1);
            return e2;
        case BT_TRUE:
            boolexpr_free(e2);
            return e1;
        default:
            break;
        }
        break;
    }

    struct boolexpr *rtn;
    rtn = malloc(sizeof(struct boolexpr));
    rtn->type = BT_AND;
    rtn->overusage = 0;
    rtn->id = 0;
    rtn->left = e1;
    rtn->right = e2;
    return rtn;
}

struct boolexpr *boolexpr_not(struct boolexpr *e) {
    struct boolexpr *rtn;
    rtn = malloc(sizeof(struct boolexpr));
    rtn->overusage = 0;
    rtn->id = 0;
    switch (e->type) {
    case BT_FALSE:
        rtn->type = BT_TRUE;
        boolexpr_free(e);
        break;
    case BT_TRUE:
        rtn->type = BT_FALSE;
        boolexpr_free(e);
        break;
    default:
        rtn->type = BT_NOT;
        rtn->left = e;
        break;
    }
    return rtn;
}

void boolexpr_free(struct boolexpr *e) {
    struct boolexpr **stack;
    size_t stack_size = 2, stack_pos = 0;
    stack = malloc(stack_size * sizeof(struct boolexpr *));
    struct boolexpr *m;
    while (e != NULL) {
        m = e;
        switch (e->type) {
        case BT_OR:
        case BT_AND:
            if (++stack_pos >= stack_size) {
                stack_size *= 2;
                stack =
                    realloc(stack, stack_size * sizeof(struct boolexpr *));
            }
            stack[stack_pos - 1] = e->right;
        case BT_NOT:
            e = e->left;
            break;
        default:
            if (stack_pos > 0)
                e = stack[--stack_pos];
            else
                e = NULL;
        }
        if (m->overusage > 0)
            m->overusage--;
        else
            free(m);
    }
    free(stack);
}

struct boolexpr *boolexpr_copy(struct boolexpr *e) {
    struct boolexpr **stack;
    size_t stack_size = 2, stack_pos = 0;
    stack = malloc(stack_size * sizeof(struct boolexpr *));
    while (e != NULL) {
        e->overusage++;
        switch (e->type) {
        case BT_OR:
        case BT_AND:
            if (++stack_pos >= stack_size) {
                stack_size *= 2;
                stack =
                    realloc(stack, stack_size * sizeof(struct boolexpr *));
            }
            stack[stack_pos - 1] = e->right;
        case BT_NOT:
            e = e->left;
            break;
        default:
            if (stack_pos > 0)
                e = stack[--stack_pos];
            else
                e = NULL;
        }
    }
    free(stack);
    return e;
}