aboutsummaryrefslogtreecommitdiff
path: root/scripts/parse_kconfig/cnfbuild.c
blob: 398a9345b7d2fbb05d4b814c53993f23a8600f11 (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
#include "cnfbuild.h"

struct stck {
    struct boolexpr **stack;
    size_t size, pos;
};

struct stck *stack_create();
void stack_push(struct stck *s, struct boolexpr *b);
struct boolexpr *stack_pop(struct stck *s);

void cnf_and(struct symlist *sl, struct boolexpr *bl);
void cnf_or(struct symlist *sl, struct boolexpr *bl);
void cnf_not(struct symlist *sl, struct boolexpr *bl);

void cnf_boolexpr(struct symlist *sl, struct boolexpr *bl) {
    if (bl->type == BT_TRUE) {
        // Term is always true. No write required.
        return;
    } else if (bl->type == BT_FALSE) {
        fprintf(stderr,
                "E: Trying to write false term. This shouldn't happen.\n");
        exit(6);
    }

    struct stck *stack = stack_create();
    while (bl != NULL) {
        switch (bl->type) {
        case BT_NOT:
            if (bl->left->id != 0) {
                cnf_not(sl, bl);
                bl = stack_pop(stack);
            } else {
                stack_push(stack, bl);
                bl = bl->left;
            }
            break;
        case BT_AND:
        case BT_OR:
            if (bl->left->id != 0) {
                if (bl->right->id != 0) {
                    if (bl->type == BT_OR) {
                        cnf_or(sl, bl);
                        bl = stack_pop(stack);
                    } else {    /* BT_AND */
                        cnf_and(sl, bl);
                        bl = stack_pop(stack);
                    }
                } else {
                    stack_push(stack, bl);
                    bl = bl->right;
                }
            } else {
                stack_push(stack, bl);
                bl = bl->left;
            }
            break;
        default:
            bl = stack_pop(stack);
            break;
        }
    }
}

struct stck *stack_create() {
    struct stck *rtn;
    rtn = malloc(sizeof(struct stck));
    rtn->size = 2;
    rtn->pos = 0;
    rtn->stack = malloc(rtn->size * sizeof(struct boolexpr *));
    return rtn;
}

void stack_push(struct stck *s, struct boolexpr *b) {
    if (++(s->pos) >= s->size) {
        s->size *= 2;
        s->stack = realloc(s->stack, s->size * sizeof(struct boolexpr *));
    }
    s->stack[s->pos - 1] = b;
}

inline struct boolexpr *stack_pop(struct stck *s) {
    if (s->pos > 0)
        return s->stack[--(s->pos)];
    else
        return NULL;
}

void cnf_and(struct symlist *sl, struct boolexpr *bl) {
    if (bl->id != 0)
        return;
    bl->id = symlist_adddummy(sl);
    // bl->id <-> (bl->left->id && bl->right->id)
    //
    // (bl->id || !bl->left->id || !bl->right->id) &&
    // (!bl->id || bl->left->id) &&
    // (!bl->id || bl->right->id)
    output_rules_symbol((int) bl->id);
    output_rules_symbol(-(int) bl->left->id);
    output_rules_symbol(-(int) bl->right->id);
    output_rules_endterm();
    output_rules_symbol(-(int) bl->id);
    output_rules_symbol((int) bl->left->id);
    output_rules_endterm();
    output_rules_symbol(-(int) bl->id);
    output_rules_symbol((int) bl->right->id);
    output_rules_endterm();
}

void cnf_or(struct symlist *sl, struct boolexpr *bl) {
    if (bl->id != 0)
        return;
    bl->id = symlist_adddummy(sl);
    // bl->id <-> (bl->left->id || bl->right->id)
    //
    // (!bl->id || bl->left->id || bl->right->id) &&
    // (bl->id || !bl->left->id) &&
    // (bl->id || !bl->right->id)
    output_rules_symbol(-(int) bl->id);
    output_rules_symbol((int) bl->left->id);
    output_rules_symbol((int) bl->right->id);
    output_rules_endterm();
    output_rules_symbol((int) bl->id);
    output_rules_symbol(-(int) bl->left->id);
    output_rules_endterm();
    output_rules_symbol((int) bl->id);
    output_rules_symbol(-(int) bl->right->id);
    output_rules_endterm();
}

void cnf_not(struct symlist *sl, struct boolexpr *bl) {
    if (bl->id != 0)
        return;
    bl->id = symlist_adddummy(sl);
    // bl->id <-> !bl->left->id
    // (bl->id || bl->left->id) && (!bl->id || !bl->left->id)
    output_rules_symbol((int) bl->id);
    output_rules_symbol((int) bl->left->id);
    output_rules_endterm();
    output_rules_symbol(-(int) bl->id);
    output_rules_symbol(-(int) bl->left->id);
    output_rules_endterm();
}