File: iflib_act.c

package info (click to toggle)
tcng 10b-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,644 kB
  • sloc: ansic: 19,040; pascal: 4,640; yacc: 2,619; sh: 1,914; perl: 1,546; lex: 772; makefile: 751
file content (292 lines) | stat: -rw-r--r-- 7,599 bytes parent folder | download | duplicates (5)
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
/*
 * iflib_act.c - Re-arrange expressions containing policing and other actions
 *
 * Written 2001 by Werner Almesberger
 * Copyright 2001 EPFL-ICA, Network Robots
 */

/*
 * The canonical form for actions (i.e. policing and decisions) is to be at
 * the end of && chains, after all static matches. iflib_actions bubbles all
 * non-actions up the tree.
 */

/*
 * By generously combining matches from all over the place, we may generate
 * redundant or conflicting rules here. This is left to the user (i.e. if_ext)
 * to sort out.
 */


#include <stdlib.h>
#include <assert.h>

#include "config.h"
#include "location.h"
#include "error.h"
#include "data.h"
#include "op.h"
#include "iflib.h"


/*
 * Tricky case:
 *
 * (expr1 || decision) && expr2
 *
 * Here, (expr1 || decision) is _not_ self-contained, because we take expr2 if
 * expr1 is false.
 */


SELF_CONTAINED self_contained(DATA d)
{
    if (!d.op) {
	if (d.type == dt_unum) return d.u.unum ? sc_no_true : sc_no_false;
	return d.type == dt_decision ? sc_yes : sc_no;
    }
    if (d.op->dsc == &op_conform) return sc_no;
    if (d.op->dsc == &op_count) return sc_no_true;
    if (d.op->dsc == &op_logical_not)
	switch (self_contained(d.op->a)) {
	    case sc_no:
		return sc_no;
	    case sc_no_true:
		return sc_no_false;
	    case sc_no_false:
		return sc_no_true;
	    case sc_yes:
		return sc_yes;
	    default:
		abort();
	}
    if (d.op->dsc == &op_logical_and) {
	/*
	 * A B  res
	 * - -  ---
	 * Y -  Y	Y = self-contained
	 * F -  F	F = not self-contained, value is "false"
	 * T x  x	T = not self-contained, value is "true"
	 * N Y  F	N = not self-contained, value is unknown
	 * N T  N
	 * N F  F
	 * N N  N
	 */

	SELF_CONTAINED sc;

	sc = self_contained(d.op->a);
	if (sc == sc_yes || sc == sc_no_false) return sc;
	if (sc == sc_no_true) return self_contained(d.op->b);
	sc = self_contained(d.op->b);
	return sc == sc_yes || sc == sc_no_false ? sc_no_false : sc_no;
    }
    if (d.op->dsc == &op_logical_or) {
	/*
	 * A B  res
	 * - -  ---
	 * Y -  Y	Y = self-contained
	 * T -  T	F = not self-contained, value is "false"
	 * F x  x	T = not self-contained, value is "true"
	 * N Y  T	N = not self-contained, value is unknown
	 * N T  T
	 * N F  N
	 * N N  N
	 */

	SELF_CONTAINED sc;

	sc = self_contained(d.op->a);
	if (sc == sc_yes || sc == sc_no_true) return sc;
	if (sc == sc_no_false) return self_contained(d.op->b);
	sc = self_contained(d.op->b);
	return sc == sc_yes || sc == sc_no_true ? sc_no_true : sc_no;
    }
    return sc_no;
}


int action_tree(DATA d)
{
    if (!d.op) return d.type == dt_decision;
    if (d.op->dsc == &op_conform || d.op->dsc == &op_count) return 1;
    if (d.op->dsc == &op_logical_and || d.op->dsc == &op_logical_or)
	return action_tree(d.op->a) &&
	  (self_contained(d.op->a) == sc_yes || action_tree(d.op->b));
    return 0;
}


int side_effect(DATA d)
{
    if (!d.op) return d.type == dt_decision;
    if (d.op->dsc == &op_count) return 1;
    return side_effect(d.op->a) || side_effect(d.op->b) || side_effect(d.op->c);
}


/*
 * Transforms  A && M  ->  M && A
 */

/*
 * This transformation is wrong if A or M have side-effects, e.g.
 * count && match is very different from match && count.
 *
 * Unfortunately, this seems to be very hard to fix, so perhaps it's better to
 * switch over to the new algorithm instead, which doesn't have such issues.
 */

static void bubble_and(DATA *d)
{
    DATA tmp;

    assert(d->op && d->op->dsc == &op_logical_and);
    if (!action_tree(d->op->a) || action_tree(d->op->b)) return;
	/* already in canonical form */
    tmp = d->op->a;
    if (self_contained(tmp) == sc_yes) {
	data_destroy(d->op->b);
	data_destroy_1(*d);
	*d = tmp;
    }
    if (side_effect(tmp) || side_effect(d->op->b))
	dump_failed(*d,"too many side effects");
    d->op->a = d->op->b;
    d->op->b = tmp;
}


/*
 * Transforms an expression of the type  (M && P) || X  where M are static
 * matches, and P are non-self-contained policing actions, by attaching a
 * copy of X at the place of P, and replacing at each transition Mi->Ai
 * from matches to an action tree (i.e. policing or decisions), Ai with
 * (P || Ai)
 *
 * E.g.  (M1 && P1) || (M2 && P2) || D  would become
 * (M1 && ((M2 && (P1 || P2)) || (P1 || D)) || (M2 && P2) || D
 *
 * Now, || and && still remain lists, but this transformation might disturb
 * the "|| over &&" structure for matches, which would throw off later
 * stages. Therefore, we merge the || chain of X (after inserting P) back on
 * top by extracting each element E and changing it to (M && E).
 */

static void attach_prev(DATA *d,DATA attach)
{
    if (action_tree(*d)) {
	if (self_contained(attach) != sc_yes)
	    *d = op_binary(&op_logical_or,data_clone(attach),*d);
	else {
	    data_destroy(*d);
	    *d = data_clone(attach);
	}
	return;
    }
    if (!d->op) return;
    attach_prev(&d->op->a,attach);
    attach_prev(&d->op->b,attach);
    attach_prev(&d->op->c,attach);
}


static void add_precondition(DATA *d,const DATA *cond,DATA *cut)
{
    *cut = *d;
    *d = data_clone(*cond);
}


static void precondition_or(DATA *d,const DATA *cond,DATA *cut)
{
    while (d->op && d->op->dsc == &op_logical_or && !action_tree(*d)) {
	add_precondition(&d->op->a,cond,cut);
	d = &d->op->b;
    }
    add_precondition(d,cond,cut);
}


static void bubble_or(DATA *d)
{
    DATA *cut,p,x,x_cond;

    assert(d->op && d->op->dsc == &op_logical_or);
    for (cut = &d->op->a; cut->op && cut->op->dsc == &op_logical_and;
      cut = &cut->op->b)
	if (action_tree(*cut)) break;
    if (self_contained(*cut) == sc_yes) return;
	/* already in canonical form */
    p = *cut;
    x = data_clone(d->op->b);
    x_cond = data_clone(x);
    if (debug > 1) {
	debug_expr("x_cond BEFORE precondition_or",x_cond);
	debug_expr("... with precondition",d->op->a);
	debug_expr("... and cut at",*cut);
    }
    precondition_or(&x_cond,&d->op->a,cut);
    if (debug > 1) debug_expr("x_cond AFTER precondition_or",x_cond);
    attach_prev(&x_cond,p);
    if (debug > 1) {
	debug_expr("x_cond AFTER attach_prev",x_cond);
	debug_expr("... with prev being",p);
    }
    data_destroy(*d);
    *d = x_cond;
    if (self_contained(*d) == sc_yes) data_destroy(x);
    else {
	DATA *end;

	for (end = d; end->op && end->op->dsc == &op_logical_or;
	  end = &end->op->b);
	*end = op_binary(&op_logical_or,*end,x);
    }
    if (debug > 1) debug_expr("AFTER adding default",*d);
}


static void push_action(DATA *d)
{
    if (!d->op) return;
    push_action(&d->op->a);
    push_action(&d->op->b);
    push_action(&d->op->c);
    if (d->op->dsc == &op_logical_and) bubble_and(d);
    else if (d->op->dsc == &op_logical_or) bubble_or(d);
}


static void trim_self_contained(DATA *d)
{
    if (!d->op) return;
    trim_self_contained(&d->op->a);
    trim_self_contained(&d->op->b);
    trim_self_contained(&d->op->c);
    if ((d->op->dsc == &op_logical_and || d->op->dsc == &op_logical_or) &&
      self_contained(d->op->a) == sc_yes) {
      /* @@@ also use sc_no_* ? */
	DATA next = d->op->a;

	data_destroy(d->op->b);
	data_destroy_1(*d);
	*d = next;
    }
}


void iflib_actions(LOCATION loc,DATA *d)
{
    trim_self_contained(d);
    debug_expr("AFTER trim_self_contained",*d);
    iflib_cheap_actions(loc,*d);
    push_action(d);
    debug_expr("AFTER push_action",*d);
    iflib_reduce(d); /* eliminate redundancy uncovered by push_action */
    debug_expr("AFTER prune_shortcuts (3)",*d);
    iflib_normalize(d);
    debug_expr("AFTER iflib_normalize (again)",*d);
    push_action(d); /* better safe than sorry */
    debug_expr("AFTER push_action (2)",*d);
}