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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
m4_divert(-1)m4_dnl
#
# BIRD -- Construction of per-instruction structures
#
# (c) 2018 Maria Matejka <mq@jmq.cz>
#
# Can be freely distributed and used under the terms of the GNU GPL.
#
#
# Global Diversions:
# 4 enum fi_code
# 5 enum fi_code to string
# 6 dump line item
# 7 dump line item callers
# 8 linearize
# 9 same (filter comparator)
# 1 union in struct f_inst
# 3 constructors
# 10 interpreter
#
# Per-inst Diversions:
# 101 content of per-inst struct
# 102 constructor arguments
# 103 constructor body
# 104 dump line item content
# 105 linearize body
# 106 comparator body
# 107 struct f_line_item content
# 108 interpreter body
#
# Diversions for TARGET=I: 7xx
# Diversions for TARGET=C: 8xx
# Diversions for TARGET=H: 9xx
# Flush the completed instruction
m4_define(FID_END, `m4_divert(-1)')
m4_dnl m4_debugmode(aceflqtx)
m4_define(FID_ZONE, `m4_divert($1) /* $2 for INST_NAME() */')
m4_define(FID_INST, `FID_ZONE(1, Instruction structure for config)')
m4_define(FID_LINE, `FID_ZONE(2, Instruction structure for interpreter)')
m4_define(FID_NEW, `FID_ZONE(3, Constructor)')
m4_define(FID_ENUM, `FID_ZONE(4, Code enum)')
m4_define(FID_ENUM_STR, `FID_ZONE(5, Code enum to string)')
m4_define(FID_DUMP, `FID_ZONE(6, Dump line)')
m4_define(FID_DUMP_CALLER, `FID_ZONE(7, Dump line caller)')
m4_define(FID_LINEARIZE, `FID_ZONE(8, Linearize)')
m4_define(FID_SAME, `FID_ZONE(9, Comparison)')
m4_define(FID_INTERPRET, `FID_ZONE(10, Interpret)')
m4_define(FID_STRUCT_IN, `m4_divert(101)')
m4_define(FID_NEW_ARGS, `m4_divert(102)')
m4_define(FID_NEW_BODY, `m4_divert(103)')
m4_define(FID_DUMP_BODY, `m4_divert(104)m4_define([[FID_DUMP_BODY_EXISTS]])')
m4_define(FID_LINEARIZE_BODY, `m4_divert(105)m4_define([[FID_LINEARIZE_BODY_EXISTS]])')
m4_define(FID_SAME_BODY, `m4_divert(106)')
m4_define(FID_LINE_IN, `m4_divert(107)')
m4_define(FID_INTERPRET_BODY, `m4_divert(108)')
m4_define(FID_ALL, `FID_INTERPRET_BODY');
m4_define(FID_ALL_TARGETS, `m4_ifdef([[FID_CURDIV]], [[m4_divert(FID_CURDIV)m4_undefine([[FID_CURDIV]])]])')
m4_define(FID_C, `m4_ifelse(TARGET, [[C]], FID_ALL_TARGETS, [[m4_define(FID_CURDIV, m4_divnum)m4_divert(-1)]])')
m4_define(FID_I, `m4_ifelse(TARGET, [[I]], FID_ALL_TARGETS, [[m4_define(FID_CURDIV, m4_divnum)m4_divert(-1)]])')
m4_define(FID_H, `m4_ifelse(TARGET, [[H]], FID_ALL_TARGETS, [[m4_define(FID_CURDIV, m4_divnum)m4_divert(-1)]])')
m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[
FID_ENUM
INST_NAME(),
FID_ENUM_STR
[INST_NAME()] = "INST_NAME()",
FID_INST
struct {
m4_undivert(101)
} i_[[]]INST_NAME();
FID_LINE
struct {
m4_undivert(107)
} i_[[]]INST_NAME();
FID_NEW
struct f_inst *f_new_inst_]]INST_NAME()[[(enum f_instruction_code fi_code
m4_undivert(102)
)
FID_H
;
FID_C
{
struct f_inst *what_ = cfg_allocz(sizeof(struct f_inst));
what_->fi_code = fi_code;
what_->lineno = ifs->lino;
what_->size = 1;
#define what (&(what_->i_]]INST_NAME()[[))
m4_undivert(103)
#undef what
return what_;
}
FID_DUMP_CALLER
case INST_NAME(): f_dump_line_item_]]INST_NAME()[[(item, indent + 1); break;
FID_DUMP
m4_ifdef([[FID_DUMP_BODY_EXISTS]],
[[static inline void f_dump_line_item_]]INST_NAME()[[(const struct f_line_item *item_, const int indent)]],
[[static inline void f_dump_line_item_]]INST_NAME()[[(const struct f_line_item *item UNUSED, const int indent UNUSED)]])
m4_undefine([[FID_DUMP_BODY_EXISTS]])
{
#define item (&(item_->i_]]INST_NAME()[[))
m4_undivert(104)
#undef item
}
FID_ALL_TARGETS
FID_LINEARIZE
case INST_NAME(): {
#define what (&(what_->i_]]INST_NAME()[[))
#define item (&(dest->items[pos].i_]]INST_NAME()[[))
m4_undivert(105)
#undef what
#undef item
break;
}
m4_undefine([[FID_LINEARIZE_BODY_EXISTS]])
FID_SAME
case INST_NAME():
#define f1 (&(f1_->i_]]INST_NAME()[[))
#define f2 (&(f2_->i_]]INST_NAME()[[))
m4_undivert(106)
#undef f1
#undef f2
break;
FID_INTERPRET
case INST_NAME():
#define whati (&(what->i_]]INST_NAME()[[))
m4_ifelse(m4_eval(INST_INVAL() > 0), 1, [[if (fstk->vcnt < INST_INVAL()) runtime("Stack underflow"); fstk->vcnt -= INST_INVAL(); ]])
m4_undivert(108)
#undef whati
break;
FID_END
]])')
m4_define(INST, `m4_dnl
INST_FLUSH()m4_dnl
m4_define([[INST_NAME]], [[$1]])m4_dnl
m4_define([[INST_INVAL]], [[$2]])m4_dnl
FID_ALL() m4_dnl
')
m4_dnl FID_MEMBER call:
m4_dnl type
m4_dnl name in f_inst
m4_dnl name in f_line_item
m4_dnl comparator for same
m4_dnl dump format string
m4_dnl dump format args
m4_dnl interpreter body
m4_define(FID_MEMBER, `m4_dnl
FID_LINE_IN
$1 $2;
FID_STRUCT_IN
$1 $2;
FID_NEW_ARGS
, $1 $2
FID_NEW_BODY
what->$2 = $2;
m4_ifelse($3,,,[[
FID_LINEARIZE_BODY
item->$3 = what->$2;
]])
m4_ifelse($4,,,[[
FID_SAME_BODY
if ($4) return 0;
]])
m4_ifelse($5,,,[[
FID_DUMP_BODY
debug("%s$5\n", INDENT, $6);
]])
m4_ifelse($7,,,[[
FID_INTERPRET_BODY
$7
]])
FID_ALL')
m4_define(ARG_ANY, `
FID_STRUCT_IN
const struct f_inst * f$1;
FID_NEW_ARGS
, const struct f_inst * f$1
FID_NEW_BODY
what->f$1 = f$1;
for (const struct f_inst *child = f$1; child; child = child->next) what_->size += child->size;
FID_LINEARIZE_BODY
pos = linearize(dest, what->f$1, pos);m4_dnl
FID_ALL()')
m4_define(ARG, `ARG_ANY($1)
FID_INTERPRET_BODY
if (v$1.type != $2) runtime("Argument $1 of instruction %s must be of type $2, got 0x%02x", f_instruction_name(what->fi_code), v$1.type)m4_dnl
FID_ALL()')
m4_define(LINEX, `FID_INTERPRET_BODY
do {
fstk->estk[fstk->ecnt].pos = 0;
fstk->estk[fstk->ecnt].line = $1;
fstk->estk[fstk->ecnt].ventry = fstk->vcnt;
fstk->estk[fstk->ecnt].vbase = fstk->estk[fstk->ecnt-1].vbase;
fstk->estk[fstk->ecnt].emask = 0;
fstk->ecnt++;
} while (0)m4_dnl
FID_ALL()')
m4_define(LINE, `
FID_LINE_IN
const struct f_line * fl$1;
FID_STRUCT_IN
const struct f_inst * f$1;
FID_NEW_ARGS
, const struct f_inst * f$1
FID_NEW_BODY
what->f$1 = f$1;
FID_DUMP_BODY
f_dump_line(item->fl$1, indent + 1);
FID_LINEARIZE_BODY
item->fl$1 = f_linearize(what->f$1);
FID_SAME_BODY
if (!f_same(f1->fl$1, f2->fl$1)) return 0;
FID_INTERPRET_BODY
do { if (whati->fl$1) {
LINEX(whati->fl$1);
} } while(0)m4_dnl
FID_ALL()')
m4_define(RESULT_PTR, `
FID_INTERPRET_BODY
do {
enum filter_return fret = f_lval_set(fs, &(what->result), $1);
if (fret != F_NOP) return fret;
} while (0)m4_dnl
FID_ALL()')
m4_define(RESULT, `
FID_INTERPRET_BODY
do {
struct f_val res_ = { .type = $1, .val.$2 = $3 };
RESULT_PTR(&res_);
} while (0)m4_dnl
FID_ALL()')
m4_define(RESULT_VOID, `
FID_INTERPRET_BODY
do {
struct f_val res_ = { .type = T_VOID };
RESULT_PTR(&res_);
} while (0)m4_dnl
FID_ALL()')
m4_define(SYMBOL, `FID_MEMBER(const struct symbol *, sym, sym,
[[strcmp(f1->sym->name, f2->sym->name) || (f1->sym->class != f2->sym->class)]], symbol %s, item->sym->name, const struct symbol *sym = whati->sym)')
m4_define(VAL, `FID_MEMBER(struct f_val $1, val, val m4_ifelse($1,,,[0]), [[!val_same(&f1->val, &f2->val)]], value %s, val_dump(&item->val),)')
m4_define(FRET, `FID_MEMBER(enum filter_return, fret, fret, f1->fret != f2->fret, %s, filter_return_str(item->fret), enum filter_return fret = whati->fret)')
m4_define(ECS, `FID_MEMBER(enum ec_subtype, ecs, ecs, f1->ecs != f2->ecs, ec subtype %s, ec_subtype_str(item->ecs), enum ec_subtype ecs = whati->ecs)')
m4_define(RTC, `FID_MEMBER(const struct rtable_config *, rtc, rtc, [[strcmp(f1->rtc->name, f2->rtc->name)]], route table %s, item->rtc->name, struct rtable *table = whati->rtc->table)')
m4_define(STATIC_ATTR, `FID_MEMBER(struct f_static_attr, sa, sa, f1->sa.sa_code != f2->sa.sa_code,,, struct f_static_attr sa = whati->sa)')
m4_define(DYNAMIC_ATTR, `FID_MEMBER(struct f_dynamic_attr, da, da, f1->da.ea_code != f2->da.ea_code,,, struct f_dynamic_attr da = whati->da)')
m4_define(COUNT, `FID_MEMBER(uint, count, count, f1->count != f2->count, number %u, item->count)')
m4_define(TREE, `FID_MEMBER(const struct f_tree *, tree, tree, [[!same_tree(f1->tree, f2->tree)]], tree %p, item->tree, const struct f_tree *tree = whati->tree)')
m4_define(STRING, `FID_MEMBER(const char *, s, s, [[strcmp(f1->s, f2->s)]], string \"%s\", item->s)')
m4_define(FID_WR_PUT_LIST)
m4_define(FID_WR_DROP_LIST)
m4_define(FID_WR_IPUT, `m4_define([[FID_WR_CUR_DIRECT]], m4_eval(FID_WR_CUR_DIRECT + 1))m4_define([[FID_WR_PUT_LIST]], FID_WR_PUT_LIST[[]]FID_WR_DPUT($1)FID_WR_DPUT(FID_WR_CUR_DIRECT))m4_divert(FID_WR_CUR_DIRECT)')
m4_define(FID_WR_IDROP, `m4_define([[FID_WR_CUR_DIRECT]], m4_eval(FID_WR_CUR_DIRECT + 1))m4_define([[FID_WR_DROP_LIST]], FID_WR_DROP_LIST[[]]FID_WR_DPUT($1)FID_WR_DPUT(FID_WR_CUR_DIRECT))m4_divert(FID_WR_CUR_DIRECT)')
m4_define(FID_WR_DIRECT, `m4_define([[FID_WR_CUR_DIRECT]],$1)m4_ifelse(TARGET,[[$2]],[[m4_define([[FID_WR_PUT]], [[FID_WR_IPUT($]][[@)]])m4_define([[FID_WR_PUT_LIST]],FID_WR_PUT_LIST[[]]FID_WR_DPUT($1))]],[[m4_define([[FID_WR_PUT]], [[FID_WR_IDROP($]][[@)]])m4_define([[FID_WR_DROP_LIST]],FID_WR_DROP_LIST[[]]FID_WR_DPUT($1))]])m4_divert($1)')
m4_dnl m4_define(FID_WR_CUR_DIRECT,m4_ifelse(TARGET,`C',800,TARGET,`H',900,m4_errprint(`Bad TARGET: 'TARGET)m4_m4exit(1)))
m4_changequote([[,]])
FID_WR_DIRECT(700,I)
FID_WR_PUT(10)
FID_WR_DIRECT(800,C)
#include "nest/bird.h"
#include "filter/filter.h"
#include "filter/f-inst.h"
/* Instruction codes to string */
static const char * const f_instruction_name_str[] = {
FID_WR_PUT(5)
};
const char *
f_instruction_name(enum f_instruction_code fi)
{
if (fi < (sizeof(f_instruction_name_str) / sizeof(f_instruction_name_str[0])))
return f_instruction_name_str[fi];
else
bug("Got unknown instruction code: %d", fi);
}
/* Instruction constructors */
FID_WR_PUT(3)
/* Line dumpers */
#define INDENT (((const char *) f_dump_line_indent_str) + sizeof(f_dump_line_indent_str) - (indent) - 1)
static const char f_dump_line_indent_str[] = " ";
FID_WR_PUT(6)
void f_dump_line(const struct f_line *dest, uint indent)
{
if (!dest) {
debug("%sNo filter line (NULL)\n", INDENT);
return;
}
debug("%sFilter line %p (len=%u)\n", INDENT, dest, dest->len);
for (uint i=0; i<dest->len; i++) {
const struct f_line_item *item = &dest->items[i];
debug("%sInstruction %s at line %u\n", INDENT, f_instruction_name(item->fi_code), item->lineno);
switch (item->result.type) {
case F_LVAL_STACK: debug("%son stack\n", INDENT); break;
case F_LVAL_EXCEPTION: debug("%s=>exception 0x%x\n", INDENT, item->result.exception); break;
case F_LVAL_VARIABLE: debug("%s=>%s\n", INDENT, item->result.sym->name); break;
case F_LVAL_PREFERENCE: debug("%s=>preference\n", INDENT); break;
case F_LVAL_SA: debug("%s=>sa\n", INDENT); break;
case F_LVAL_EA: debug("%s=>ea\n", INDENT); break;
}
switch (item->fi_code) {
FID_WR_PUT(7)
default: bug("Unknown instruction %x in f_dump_line", item->fi_code);
}
}
debug("%sFilter line %p dump done\n", INDENT, dest);
}
/* Linearize */
static uint
linearize(struct f_line *dest, const struct f_inst *what_, uint pos)
{
for ( ; what_; what_ = what_->next) {
switch (what_->fi_code) {
FID_WR_PUT(8)
}
dest->items[pos].fi_code = what_->fi_code;
dest->items[pos].lineno = what_->lineno;
dest->items[pos].result = what_->result;
pos++;
}
return pos;
}
struct f_line *
f_linearize_concat(const struct f_inst * const inst[], uint count)
{
uint len = 0;
for (uint i=0; i<count; i++)
for (const struct f_inst *what = inst[i]; what; what = what->next)
len += what->size;
struct f_line *out = cfg_allocz(sizeof(struct f_line) + sizeof(struct f_line_item)*len);
for (uint i=0; i<count; i++)
out->len = linearize(out, inst[i], out->len);
#if DEBUGGING
f_dump_line(out, 0);
#endif
return out;
}
/* Filter line comparison */
int
f_same(const struct f_line *fl1, const struct f_line *fl2)
{
if ((!fl1) && (!fl2))
return 1;
if ((!fl1) || (!fl2))
return 0;
if (fl1->len != fl2->len)
return 0;
for (uint i=0; i<fl1->len; i++) {
#define f1_ (&(fl1->items[i]))
#define f2_ (&(fl2->items[i]))
if (f1_->fi_code != f2_->fi_code)
return 0;
if (f1_->flags != f2_->flags)
return 0;
if (f1_->result.type != f2_->result.type) return 0;
switch (f1_->result.type) {
case F_LVAL_STACK:
break;
case F_LVAL_EXCEPTION:
if (f1_->result.exception != f2_->result.exception)
return 0;
break;
case F_LVAL_VARIABLE:
if (strcmp(f1_->result.sym->name, f2_->result.sym->name))
return 0;
if (f1_->result.sym->class != f2_->result.sym->class)
return 0;
break;
case F_LVAL_PREFERENCE:
break;
case F_LVAL_SA:
if (f1_->result.sa.sa_code != f2_->result.sa.sa_code)
return 0;
break;
case F_LVAL_EA:
if (f1_->result.da.ea_code != f2_->result.da.ea_code)
return 0;
break;
}
switch(f1_->fi_code) {
FID_WR_PUT(9)
}
}
#undef f1_
#undef f2_
return 1;
}
FID_WR_DIRECT(900,H)
/* Filter instruction codes */
enum f_instruction_code {
FID_WR_PUT(4)
} PACKED;
/* Filter instruction structure for config */
struct f_inst {
struct f_inst *next; /* Next instruction */
enum f_instruction_code fi_code; /* Instruction code */
int size; /* How many instructions are underneath */
int lineno; /* Line number */
struct f_lval result; /* Destination */
union {
FID_WR_PUT(1)
};
};
/* Filter line item */
struct f_line_item {
enum f_instruction_code fi_code; /* What to do */
enum f_instruction_flags flags; /* Flags, instruction-specific */
uint lineno; /* Where */
struct f_lval result; /* Destination */
union {
FID_WR_PUT(2)
};
};
/* Instruction constructors */
FID_WR_PUT(3)
m4_divert(-1)
m4_changequote(`,')
m4_m4wrap(`INST_FLUSH()m4_define(FID_WR_DPUT, [[m4_undivert($1)]])m4_divert(0)FID_WR_PUT_LIST[[]]m4_divert(-1)FID_WR_DROP_LIST[[]]')
m4_changequote([[,]])
|