File: field.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 (311 lines) | stat: -rw-r--r-- 7,011 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * field.c - Packet field definition
 *
 * Written 2001 by Werner Almesberger
 * Copyright 2001 EPFL-ICA
 * Copyright 2001,2002 Network Robots
 */


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

#include "util.h"
#include "error.h"
#include "data.h"
#include "op.h"
#include "field.h"


static FIELD_ROOT *field_roots = NULL;
static FIELD *fields = NULL;
static int level = 0;


/* ----- Field roots ------------------------------------------------------- */


FIELD_ROOT *field_root(int offset_group)
{
    FIELD_ROOT *root;

    for (root = field_roots; root; root = root->next)
	if (root->offset_group == offset_group) return root;
    root = alloc_t(FIELD_ROOT);
    root->offset_group = offset_group;
    root->next = field_roots;
    field_roots = root;
    return root;
}


int offset_group_taken(int offset_group)
{
    const FIELD_ROOT *root;

    for (root = field_roots; root; root = root->next)
	if (root->offset_group == offset_group) return 1;
    return 0;
}


/* ----- Field definition -------------------------------------------------- */


void field_begin_scope(void)
{
    level++;
}


void field_end_scope(void)
{
    FIELD **field,**next;

    for (field = &fields; *field; field = next) {
	next = &(*field)->next;
	if ((*field)->level == level) {
	    data_destroy((*field)->condition);
	    data_destroy((*field)->access);
	    if ((*field)->outer) {
		FIELD *outer = (*field)->outer;

		(*field)->condition = outer->condition;
		(*field)->access = outer->access;
		(*field)->level = outer->level;
		(*field)->outer = outer->outer;
		free(outer);
	    }
	    else {
		FIELD *this = *field;

		*field = this->next;
		next = field;
		free((void *) this->name);
		free(this);
	    }
	}
    }
    level--;
}


FIELD *field_find(const char *name)
{
    FIELD *field;

    for (field = fields; field; field = field->next)
	if (!strcmp(field->name,name)) break;
    return field;
}


void field_set(const char *name,DATA access,DATA condition)
{
    FIELD *field;

    if (field_find(name)) yyerrorf("field \"%s\" already exists",name);
	/* the parser actually already disallows this */
    field = alloc_t(FIELD);
    field->name = stralloc(name);
    field->condition = condition;
    field->access = access;
    field->level = level;
    field->outer = NULL;
    field->next = fields;
    fields = field;
}


static void is_known(DATA d,const FIELD *horizon)
{
    if (d.type == dt_field) {
	const FIELD *scan;

	for (scan = horizon; scan; scan = scan->next)
	    if (scan == d.u.field) return;
	yyerrorf("field \"%s\" defined after redefined field",d.u.field->name);
    }
    if (!d.op) return;
    is_known(d.op->a,horizon);
    is_known(d.op->b,horizon);
    is_known(d.op->c,horizon);
}


void field_redefine(FIELD *field,DATA access,DATA condition)
{
    is_known(access,field->next);
    is_known(condition,field->next);
    if (field->level == level) {
	data_destroy(field->access);
	data_destroy(field->condition);
    }
    else {
	FIELD *outer;

	outer = alloc_t(FIELD);
	outer->name = NULL;
	outer->access = field->access;
	outer->condition = field->condition;
	outer->level = field->level;
	outer->next = NULL;
	outer->outer = field->outer;
	field->outer = outer;
	field->level = level;
    }
    field->access = access;
    field->condition = condition;
}


/* ----- Field expansion --------------------------------------------------- */


static void do_field_expand(DATA *d)
{
    if (d->type == dt_none) return;
    while (d->op && d->op->dsc == &op_access && d->op->a.type == dt_field) {
	FIELD *field = d->op->a.u.field;
	DATA access = data_clone(field->access);

	data_destroy_1(d->op->a);
	/*
	 * disallow things like
	 *   field foo = bar+5
	 *   field baz = foo[0]
	 */
	if ((d->op->b.type != dt_none || d->op->c.u.unum) &&
	  field->access.type != dt_none &&
	  (!field->access.op || field->access.op->dsc != &op_access))
	    yyerrorf("access not allowed for field \"%s\"",field->name);
	if (field->condition.type == dt_none) d->op->a = access;
	else {
	    DATA condition;

	    condition = data_clone(field->condition);
	    do_field_expand(&condition);
	    d->op->a = op_binary(&op_precond2,access,condition);
	}
    }
    if (!d->op) return;
    do_field_expand(&d->op->a);
    do_field_expand(&d->op->b);
    do_field_expand(&d->op->c);
}


static void bubble_precond(DATA *d,DATA *root,int adjust_root)
{
    DATA old = *d;

    if (!d->op) return;
    if (d->op->dsc == &op_precond) {
	DATA new = data_unum(1);

	bubble_precond(&d->op->a,&new,0);
	new = data_clone(new);
	data_destroy(*d);
	*d = new;
	return;
    }
    if (d->op->dsc == &op_precond2) {
	*root = op_binary(&op_logical_and,d->op->b,*root);
	bubble_precond(&root->op->a,root,1); /* was d->op->b */
	/* resolve pointer alias */
	if (root == d) d = &root->op->b;
	bubble_precond(&d->op->a,root,adjust_root);
	*d = d->op->a;
	data_destroy_1(old);
	return;
    }
    if (adjust_root &&
      (d->op->dsc == &op_logical_or || d->op->dsc == &op_logical_not)) {
	bubble_precond(&old.op->a,&old.op->a,1);
	bubble_precond(&old.op->b,&old.op->b,1);
    }
    else {
	/*
	 * d->op->b before d->op->a to put preconditions of a || b in the right
         * order if adjust_root is not set (i.e. when processing precond() )
	 */
	bubble_precond(&old.op->b,root,adjust_root);
	bubble_precond(&old.op->a,root,adjust_root);
	bubble_precond(&old.op->c,root,adjust_root);
    }
}


static void reduce_access(DATA *d)
{
    while (d->op && d->op->dsc == &op_access) {
	DATA old;

	/*
	 * Change access(root,-,0) to access(root,-,8) and stop reducing in all
	 * cases of access(root,-,*)
	 */
	if (d->op->a.type == dt_field_root && d->op->b.type == dt_none) {
	    if (!d->op->c.u.unum) d->op->c.u.unum = 8;
	    break;
	}

	/*
	 * Reduce access(X,-,0) to X unless X is dt_field_root
	 */
	if (d->op->b.type == dt_none && !d->op->c.u.unum) {
	    old = *d;

	    *d = d->op->a;
	    data_destroy_1(old);
	    continue;
	}

	/*
	 * Reduce
	 *   access(access(D,O2,S2),O1,S1)
	 * to
	 *   access(D,O1+O2,S1 ? S1 : S2)
	 */
	if (!d->op->a.op || d->op->a.op->dsc != &op_access) {
	    if (!d->op->c.u.unum) d->op->c.u.unum = 8;
	    break;
	}
	old = d->op->a;
	if (d->op->b.type == dt_none)
	    d->op->b = data_clone(d->op->a.op->b);
	else if (d->op->a.op->b.type != dt_none)
	    d->op->b = op_binary(&op_plus,d->op->b,d->op->a.op->b);
	if (!d->op->c.u.unum) d->op->c.u.unum = d->op->a.op->c.u.unum;
	if (!d->op->c.u.unum) d->op->c.u.unum = 8;
	d->op->a = d->op->a.op->a;
	data_destroy_1(old);
    }

    /* make life easier for later processing stages */
    if (d->op && d->op->dsc == &op_access && d->op->b.type == dt_none)
	d->op->b = data_unum(0);
    if (d->op) {
	reduce_access(&d->op->a);
	reduce_access(&d->op->b);
	reduce_access(&d->op->c);
    }
}


DATA field_expand(DATA d)
{
    do_field_expand(&d);
    debug_expr("BEFORE bubble_precond",d);
    bubble_precond(&d,&d,1);
    reduce_access(&d);
    return d;
}


DATA field_snapshot(DATA d)
{
    do_field_expand(&d);
    return d;
}