]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/decl.m4
Filter: Don't fail badly when trying to access non-existent route in config time
[thirdparty/bird.git] / filter / decl.m4
1 m4_divert(-1)m4_dnl
2 #
3 # BIRD -- Construction of per-instruction structures
4 #
5 # (c) 2018 Maria Matejka <mq@jmq.cz>
6 #
7 # Can be freely distributed and used under the terms of the GNU GPL.
8 #
9 # THIS IS A M4 MACRO FILE GENERATING 3 FILES ALTOGETHER.
10 # KEEP YOUR HANDS OFF UNLESS YOU KNOW WHAT YOU'RE DOING.
11 # EDITING AND DEBUGGING THIS FILE MAY DAMAGE YOUR BRAIN SERIOUSLY.
12 #
13 # But you're welcome to read and edit and debug if you aren't scared.
14 #
15 # Uncomment the following line to get exhaustive debug output.
16 # m4_debugmode(aceflqtx)
17 #
18 # How it works:
19 # 1) Instruction to code conversion (uses diversions 100..199)
20 # 2) Code wrapping (uses diversions 1..99)
21 # 3) Final preparation (uses diversions 200..299)
22 # 4) Shipout
23 #
24 # See below for detailed description.
25 #
26 #
27 # 1) Instruction to code conversion
28 # The code provided in f-inst.c between consecutive INST() calls
29 # is interleaved for many different places. It is here processed
30 # and split into separate instances where split-by-instruction
31 # happens. These parts are stored in temporary diversions listed:
32 #
33 # 101 content of per-inst struct
34 # 102 constructor arguments
35 # 103 constructor body
36 # 104 dump line item content
37 # (there may be nothing in dump-line content and
38 # it must be handled specially in phase 2)
39 # 105 linearize body
40 # 106 comparator body
41 # 107 struct f_line_item content
42 # 108 interpreter body
43 #
44 # Here are macros to allow you to _divert to the right directions.
45 m4_define(FID_STRUCT_IN, `m4_divert(101)')
46 m4_define(FID_NEW_ARGS, `m4_divert(102)')
47 m4_define(FID_NEW_BODY, `m4_divert(103)')
48 m4_define(FID_DUMP_BODY, `m4_divert(104)m4_define([[FID_DUMP_BODY_EXISTS]])')
49 m4_define(FID_LINEARIZE_BODY, `m4_divert(105)')
50 m4_define(FID_SAME_BODY, `m4_divert(106)')
51 m4_define(FID_LINE_IN, `m4_divert(107)')
52 m4_define(FID_INTERPRET_BODY, `m4_divert(108)')
53
54 # Sometimes you want slightly different code versions in different
55 # outputs.
56 # Use FID_HIC(code for inst-gen.h, code for inst-gen.c, code for inst-interpret.c)
57 # and put it into [[ ]] quotes if it shall contain commas.
58 m4_define(FID_HIC, `m4_ifelse(TARGET, [[H]], [[$1]], TARGET, [[I]], [[$2]], TARGET, [[C]], [[$3]])')
59
60 # In interpreter code, this is quite common.
61 m4_define(FID_INTERPRET_EXEC, `FID_HIC(,[[FID_INTERPRET_BODY()]],[[m4_divert(-1)]])')
62 m4_define(FID_INTERPRET_NEW, `FID_HIC(,[[m4_divert(-1)]],[[FID_INTERPRET_BODY()]])')
63
64 # If the instruction is never converted to constant, the interpret
65 # code is not produced at all for constructor
66 m4_define(NEVER_CONSTANT, `m4_define([[INST_NEVER_CONSTANT]])')
67 m4_define(FID_IFCONST, `m4_ifdef([[INST_NEVER_CONSTANT]],[[$2]],[[$1]])')
68
69 # If the instruction has some attributes (here called members),
70 # these are typically carried with the instruction from constructor
71 # to interpreter. This yields a line of code everywhere on the path.
72 # FID_MEMBER is a macro to help with this task.
73 m4_define(FID_MEMBER, `m4_dnl
74 FID_LINE_IN()m4_dnl
75 $1 $2;
76 FID_STRUCT_IN()m4_dnl
77 $1 $2;
78 FID_NEW_ARGS()m4_dnl
79 , $1 $2
80 FID_NEW_BODY()m4_dnl
81 whati->$2 = $2;
82 FID_LINEARIZE_BODY()m4_dnl
83 item->$2 = whati->$2;
84 m4_ifelse($3,,,[[
85 FID_SAME_BODY()m4_dnl
86 if ($3) return 0;
87 ]])
88 m4_ifelse($4,,,[[
89 FID_DUMP_BODY()m4_dnl
90 debug("%s$4\n", INDENT, $5);
91 ]])
92 FID_INTERPRET_EXEC()m4_dnl
93 const $1 $2 = whati->$2
94 FID_INTERPRET_BODY')
95
96 m4_define(FID_MEMBER_IN, `m4_dnl
97 FID_LINE_IN()m4_dnl
98 $1 $2;
99 FID_STRUCT_IN()m4_dnl
100 $1 $2;
101 FID_LINEARIZE_BODY()m4_dnl
102 item->$2 = whati->$2;
103 m4_ifelse($3,,,[[
104 FID_SAME_BODY()m4_dnl
105 if ($3) return 0;
106 ]])
107 m4_ifelse($4,,,[[
108 FID_DUMP_BODY()m4_dnl
109 debug("%s$4\n", INDENT, $5);
110 ]])
111 FID_INTERPRET_EXEC()m4_dnl
112 const $1 $2 = whati->$2
113 FID_INTERPRET_BODY')
114
115 # Instruction arguments are needed only until linearization is done.
116 # This puts the arguments into the filter line to be executed before
117 # the instruction itself.
118 #
119 # To achieve this, ARG_ANY must be called before anything writes into
120 # the instruction line as it moves the instruction pointer forward.
121 m4_define(ARG_ANY, `
122 FID_STRUCT_IN()m4_dnl
123 struct f_inst * f$1;
124 FID_NEW_ARGS()m4_dnl
125 , struct f_inst * f$1
126 FID_NEW_BODY
127 whati->f$1 = f$1;
128 for (const struct f_inst *child = f$1; child; child = child->next) {
129 what->size += child->size;
130 FID_IFCONST([[
131 if (child->fi_code != FI_CONSTANT)
132 constargs = 0;
133 ]])
134 }
135 FID_LINEARIZE_BODY
136 pos = linearize(dest, whati->f$1, pos);
137 FID_INTERPRET_BODY()')
138
139 # Some arguments need to check their type. After that, ARG_ANY is called.
140 m4_define(ARG, `ARG_ANY($1)
141 FID_INTERPRET_EXEC()m4_dnl
142 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
143 FID_INTERPRET_BODY()')
144
145 # Executing another filter line. This replaces the recursion
146 # that was needed in the former implementation.
147 m4_define(LINEX, `FID_INTERPRET_EXEC()LINEX_($1)FID_INTERPRET_NEW()return $1 FID_INTERPRET_BODY()')
148 m4_define(LINEX_, `do {
149 fstk->estk[fstk->ecnt].pos = 0;
150 fstk->estk[fstk->ecnt].line = $1;
151 fstk->estk[fstk->ecnt].ventry = fstk->vcnt;
152 fstk->estk[fstk->ecnt].vbase = fstk->estk[fstk->ecnt-1].vbase;
153 fstk->estk[fstk->ecnt].emask = 0;
154 fstk->ecnt++;
155 } while (0)')
156
157 m4_define(LINE, `
158 FID_LINE_IN()m4_dnl
159 const struct f_line * fl$1;
160 FID_STRUCT_IN()m4_dnl
161 struct f_inst * f$1;
162 FID_NEW_ARGS()m4_dnl
163 , struct f_inst * f$1
164 FID_NEW_BODY()m4_dnl
165 whati->f$1 = f$1;
166 FID_DUMP_BODY()m4_dnl
167 f_dump_line(item->fl$1, indent + 1);
168 FID_LINEARIZE_BODY()m4_dnl
169 item->fl$1 = f_linearize(whati->f$1);
170 FID_SAME_BODY()m4_dnl
171 if (!f_same(f1->fl$1, f2->fl$1)) return 0;
172 FID_INTERPRET_EXEC()m4_dnl
173 do { if (whati->fl$1) {
174 LINEX_(whati->fl$1);
175 } } while(0)
176 FID_INTERPRET_NEW()m4_dnl
177 return whati->f$1
178 FID_INTERPRET_BODY()')
179
180 # Some of the instructions have a result. These constructions
181 # state the result and put it to the right place.
182 m4_define(RESULT, `RESULT_VAL([[ (struct f_val) { .type = $1, .val.$2 = $3 } ]])')
183 m4_define(RESULT_VAL, `FID_HIC(, [[do { res = $1; fstk->vcnt++; } while (0)]],
184 [[return fi_constant(what, $1)]])')
185 m4_define(RESULT_VOID, `RESULT_VAL([[ (struct f_val) { .type = T_VOID } ]])')
186
187 # Some common filter instruction members
188 m4_define(SYMBOL, `FID_MEMBER(struct symbol *, sym, [[strcmp(f1->sym->name, f2->sym->name) || (f1->sym->class != f2->sym->class)]], symbol %s, item->sym->name)')
189 m4_define(RTC, `FID_MEMBER(struct rtable_config *, rtc, [[strcmp(f1->rtc->name, f2->rtc->name)]], route table %s, item->rtc->name)')
190 m4_define(STATIC_ATTR, `FID_MEMBER(struct f_static_attr, sa, f1->sa.sa_code != f2->sa.sa_code,,)')
191 m4_define(DYNAMIC_ATTR, `FID_MEMBER(struct f_dynamic_attr, da, f1->da.ea_code != f2->da.ea_code,,)')
192 m4_define(ACCESS_RTE, `FID_HIC(,[[do { if (!fs->rte) runtime("No route to access"); } while (0)]],NEVER_CONSTANT())')
193
194 # 2) Code wrapping
195 # The code produced in 1xx temporary diversions is a raw code without
196 # any auxiliary commands and syntactical structures around. When the
197 # instruction is done, INST_FLUSH is called. More precisely, it is called
198 # at the beginning of INST() call and at the end of file.
199 #
200 # INST_FLUSH picks all the temporary diversions, wraps their content
201 # into appropriate headers and structures and saves them into global
202 # diversions listed:
203 #
204 # 4 enum fi_code
205 # 5 enum fi_code to string
206 # 6 dump line item
207 # 7 dump line item callers
208 # 8 linearize
209 # 9 same (filter comparator)
210 # 1 union in struct f_inst
211 # 3 constructors + interpreter
212 #
213 # These global diversions contain blocks of code that can be directly
214 # put into the final file, yet it still can't be written out now as
215 # every instruction writes to all of these diversions.
216
217 # Code wrapping diversion names. Here we want an explicit newline
218 # after the C comment.
219 m4_define(FID_ZONE, `m4_divert($1) /* $2 for INST_NAME() */
220 ')
221 m4_define(FID_INST, `FID_ZONE(1, Instruction structure for config)')
222 m4_define(FID_LINE, `FID_ZONE(2, Instruction structure for interpreter)')
223 m4_define(FID_NEW, `FID_ZONE(3, Constructor)')
224 m4_define(FID_ENUM, `FID_ZONE(4, Code enum)')
225 m4_define(FID_ENUM_STR, `FID_ZONE(5, Code enum to string)')
226 m4_define(FID_DUMP, `FID_ZONE(6, Dump line)')
227 m4_define(FID_DUMP_CALLER, `FID_ZONE(7, Dump line caller)')
228 m4_define(FID_LINEARIZE, `FID_ZONE(8, Linearize)')
229 m4_define(FID_SAME, `FID_ZONE(9, Comparison)')
230
231 # This macro does all the code wrapping. See inline comments.
232 m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[
233 FID_ENUM()m4_dnl Contents of enum fi_code { ... }
234 INST_NAME(),
235 FID_ENUM_STR()m4_dnl Contents of const char * indexed by enum fi_code
236 [INST_NAME()] = "INST_NAME()",
237 FID_INST()m4_dnl Anonymous structure inside struct f_inst
238 struct {
239 m4_undivert(101)m4_dnl
240 } i_[[]]INST_NAME();
241 FID_LINE()m4_dnl Anonymous structure inside struct f_line_item
242 struct {
243 m4_undivert(107)m4_dnl
244 } i_[[]]INST_NAME();
245 FID_NEW()m4_dnl Constructor and interpreter code together
246 FID_HIC(
247 [[m4_dnl Public declaration of constructor in H file
248 struct f_inst *f_new_inst_]]INST_NAME()[[(enum f_instruction_code fi_code
249 m4_undivert(102)m4_dnl
250 );]],
251 [[m4_dnl The one case in The Big Switch inside interpreter
252 case INST_NAME():
253 #define whati (&(what->i_]]INST_NAME()[[))
254 m4_ifelse(m4_eval(INST_INVAL() > 0), 1, [[if (fstk->vcnt < INST_INVAL()) runtime("Stack underflow"); fstk->vcnt -= INST_INVAL(); ]])
255 m4_undivert(108)m4_dnl
256 #undef whati
257 break;
258 ]],
259 [[m4_dnl Constructor itself
260 struct f_inst *f_new_inst_]]INST_NAME()[[(enum f_instruction_code fi_code
261 m4_undivert(102)m4_dnl
262 )
263 {
264 /* Allocate the structure */
265 struct f_inst *what = fi_new(fi_code);
266 FID_IFCONST([[uint constargs = 1;]])
267
268 /* Initialize all the members */
269 #define whati (&(what->i_]]INST_NAME()[[))
270 m4_undivert(103)m4_dnl
271
272 /* If not constant, return the instruction itself */
273 FID_IFCONST([[if (!constargs)]])
274 return what;
275
276 /* Try to pre-calculate the result */
277 FID_IFCONST([[m4_undivert(108)]])m4_dnl
278 #undef whati
279 }
280 ]])
281
282 FID_DUMP_CALLER()m4_dnl Case in another big switch used in instruction dumping (debug)
283 case INST_NAME(): f_dump_line_item_]]INST_NAME()[[(item, indent + 1); break;
284
285 FID_DUMP()m4_dnl The dumper itself
286 m4_ifdef([[FID_DUMP_BODY_EXISTS]],
287 [[static inline void f_dump_line_item_]]INST_NAME()[[(const struct f_line_item *item_, const int indent)]],
288 [[static inline void f_dump_line_item_]]INST_NAME()[[(const struct f_line_item *item UNUSED, const int indent UNUSED)]])
289 m4_undefine([[FID_DUMP_BODY_EXISTS]])
290 {
291 #define item (&(item_->i_]]INST_NAME()[[))
292 m4_undivert(104)m4_dnl
293 #undef item
294 }
295
296 FID_LINEARIZE()m4_dnl The linearizer
297 case INST_NAME(): {
298 #define whati (&(what->i_]]INST_NAME()[[))
299 #define item (&(dest->items[pos].i_]]INST_NAME()[[))
300 m4_undivert(105)m4_dnl
301 #undef whati
302 #undef item
303 dest->items[pos].fi_code = what->fi_code;
304 dest->items[pos].lineno = what->lineno;
305 break;
306 }
307
308 FID_SAME()m4_dnl This code compares two f_line"s while reconfiguring
309 case INST_NAME():
310 #define f1 (&(f1_->i_]]INST_NAME()[[))
311 #define f2 (&(f2_->i_]]INST_NAME()[[))
312 m4_undivert(106)m4_dnl
313 #undef f1
314 #undef f2
315 break;
316
317 m4_divert(-1)FID_FLUSH(101,200)m4_dnl And finally this flushes all the unused diversions
318 ]])')
319
320 m4_define(INST, `m4_dnl This macro is called on beginning of each instruction.
321 INST_FLUSH()m4_dnl First, old data is flushed
322 m4_define([[INST_NAME]], [[$1]])m4_dnl Then we store instruction name,
323 m4_define([[INST_INVAL]], [[$2]])m4_dnl instruction input value count
324 m4_undefine([[INST_NEVER_CONSTANT]])m4_dnl and reset NEVER_CONSTANT trigger.
325 FID_INTERPRET_BODY()m4_dnl By default, every code is interpreter code.
326 ')
327
328 # 3) Final preparation
329 #
330 # Now we prepare all the code around the global diversions.
331 # It must be here, not in m4wrap, as we want M4 to mark the code
332 # by #line directives correctly, not to claim that every single line
333 # is at the beginning of the m4wrap directive.
334 #
335 # This part is split by the final file.
336 # H for inst-gen.h
337 # I for inst-interpret.c
338 # C for inst-gen.c
339 #
340 # So we in cycle:
341 # A. open a diversion
342 # B. send there some code
343 # C. close that diversion
344 # D. flush a global diversion
345 # E. open another diversion and goto B.
346 #
347 # Final diversions
348 # 200+ completed text before it is flushed to output
349
350 # This is a list of output diversions
351 m4_define(FID_WR_PUT_LIST)
352
353 # This macro does the steps C to E, see before.
354 m4_define(FID_WR_PUT_ALSO, `m4_define([[FID_WR_PUT_LIST]],FID_WR_PUT_LIST()[[FID_WR_DPUT(]]FID_WR_DIDX[[)FID_WR_DPUT(]]$1[[)]])m4_define([[FID_WR_DIDX]],m4_eval(FID_WR_DIDX+1))m4_divert(FID_WR_DIDX)')
355
356 # These macros do the splitting between H/I/C
357 m4_define(FID_WR_DIRECT, `m4_ifelse(TARGET,[[$1]],[[FID_WR_INIT()]],[[FID_WR_STOP()]])')
358 m4_define(FID_WR_INIT, `m4_define([[FID_WR_DIDX]],200)m4_define([[FID_WR_PUT]],[[FID_WR_PUT_ALSO($]][[@)]])m4_divert(200)')
359 m4_define(FID_WR_STOP, `m4_define([[FID_WR_PUT]])m4_divert(-1)')
360
361 # Here is the direct code to be put into the output files
362 # together with the undiversions, being hidden under FID_WR_PUT()
363
364 m4_changequote([[,]])
365 FID_WR_DIRECT(I)
366 FID_WR_PUT(3)
367 FID_WR_DIRECT(C)
368
369 #if defined(__GNUC__) && __GNUC__ >= 6
370 #pragma GCC diagnostic push
371 #pragma GCC diagnostic ignored "-Wmisleading-indentation"
372 #endif
373
374 #include "nest/bird.h"
375 #include "filter/filter.h"
376 #include "filter/f-inst.h"
377
378 /* Instruction codes to string */
379 static const char * const f_instruction_name_str[] = {
380 FID_WR_PUT(5)
381 };
382
383 const char *
384 f_instruction_name(enum f_instruction_code fi)
385 {
386 if (fi < (sizeof(f_instruction_name_str) / sizeof(f_instruction_name_str[0])))
387 return f_instruction_name_str[fi];
388 else
389 bug("Got unknown instruction code: %d", fi);
390 }
391
392 static inline struct f_inst *
393 fi_new(enum f_instruction_code fi_code)
394 {
395 struct f_inst *what = cfg_allocz(sizeof(struct f_inst));
396 what->lineno = ifs->lino;
397 what->size = 1;
398 what->fi_code = fi_code;
399 return what;
400 }
401
402 static inline struct f_inst *
403 fi_constant(struct f_inst *what, struct f_val val)
404 {
405 what->fi_code = FI_CONSTANT;
406 what->i_FI_CONSTANT.val = val;
407 return what;
408 }
409
410 #define v1 whati->f1->i_FI_CONSTANT.val
411 #define v2 whati->f2->i_FI_CONSTANT.val
412 #define v3 whati->f3->i_FI_CONSTANT.val
413 #define runtime(fmt, ...) cf_error("filter preevaluation, line %d: " fmt, ifs->lino, ##__VA_ARGS__)
414 #define fpool cfg_mem
415 #define falloc(size) cfg_alloc(size)
416 /* Instruction constructors */
417 FID_WR_PUT(3)
418 #undef v1
419 #undef v2
420 #undef v3
421
422 /* Line dumpers */
423 #define INDENT (((const char *) f_dump_line_indent_str) + sizeof(f_dump_line_indent_str) - (indent) - 1)
424 static const char f_dump_line_indent_str[] = " ";
425
426 FID_WR_PUT(6)
427
428 void f_dump_line(const struct f_line *dest, uint indent)
429 {
430 if (!dest) {
431 debug("%sNo filter line (NULL)\n", INDENT);
432 return;
433 }
434 debug("%sFilter line %p (len=%u)\n", INDENT, dest, dest->len);
435 for (uint i=0; i<dest->len; i++) {
436 const struct f_line_item *item = &dest->items[i];
437 debug("%sInstruction %s at line %u\n", INDENT, f_instruction_name(item->fi_code), item->lineno);
438 switch (item->fi_code) {
439 FID_WR_PUT(7)
440 default: bug("Unknown instruction %x in f_dump_line", item->fi_code);
441 }
442 }
443 debug("%sFilter line %p dump done\n", INDENT, dest);
444 }
445
446 /* Linearize */
447 static uint
448 linearize(struct f_line *dest, const struct f_inst *what, uint pos)
449 {
450 for ( ; what; what = what->next) {
451 switch (what->fi_code) {
452 FID_WR_PUT(8)
453 }
454 pos++;
455 }
456 return pos;
457 }
458
459 struct f_line *
460 f_linearize_concat(const struct f_inst * const inst[], uint count)
461 {
462 uint len = 0;
463 for (uint i=0; i<count; i++)
464 for (const struct f_inst *what = inst[i]; what; what = what->next)
465 len += what->size;
466
467 struct f_line *out = cfg_allocz(sizeof(struct f_line) + sizeof(struct f_line_item)*len);
468
469 for (uint i=0; i<count; i++)
470 out->len = linearize(out, inst[i], out->len);
471
472 #if DEBUGGING
473 f_dump_line(out, 0);
474 #endif
475 return out;
476 }
477
478 /* Filter line comparison */
479 int
480 f_same(const struct f_line *fl1, const struct f_line *fl2)
481 {
482 if ((!fl1) && (!fl2))
483 return 1;
484 if ((!fl1) || (!fl2))
485 return 0;
486 if (fl1->len != fl2->len)
487 return 0;
488 for (uint i=0; i<fl1->len; i++) {
489 #define f1_ (&(fl1->items[i]))
490 #define f2_ (&(fl2->items[i]))
491 if (f1_->fi_code != f2_->fi_code)
492 return 0;
493 if (f1_->flags != f2_->flags)
494 return 0;
495
496 switch(f1_->fi_code) {
497 FID_WR_PUT(9)
498 }
499 }
500 #undef f1_
501 #undef f2_
502 return 1;
503 }
504
505 #if defined(__GNUC__) && __GNUC__ >= 6
506 #pragma GCC diagnostic pop
507 #endif
508
509 FID_WR_DIRECT(H)
510 /* Filter instruction codes */
511 enum f_instruction_code {
512 FID_WR_PUT(4)m4_dnl
513 } PACKED;
514
515 /* Filter instruction structure for config */
516 struct f_inst {
517 struct f_inst *next; /* Next instruction */
518 enum f_instruction_code fi_code; /* Instruction code */
519 int size; /* How many instructions are underneath */
520 int lineno; /* Line number */
521 union {
522 FID_WR_PUT(1)m4_dnl
523 };
524 };
525
526 /* Filter line item */
527 struct f_line_item {
528 enum f_instruction_code fi_code; /* What to do */
529 enum f_instruction_flags flags; /* Flags, instruction-specific */
530 uint lineno; /* Where */
531 union {
532 FID_WR_PUT(2)m4_dnl
533 };
534 };
535
536 /* Instruction constructors */
537 FID_WR_PUT(3)
538 m4_divert(-1)
539
540 # 4) Shipout
541 #
542 # Everything is prepared in FID_WR_PUT_LIST now. Let's go!
543
544 m4_changequote(`,')
545
546 # Flusher auxiliary macro
547 m4_define(FID_FLUSH, `m4_ifelse($1,$2,,[[m4_undivert($1)FID_FLUSH(m4_eval($1+1),$2)]])')
548
549 # Defining the macro used in FID_WR_PUT_LIST
550 m4_define(FID_WR_DPUT, `m4_undivert($1)')
551
552 # After the code is read and parsed, we:
553 m4_m4wrap(`INST_FLUSH()m4_divert(0)FID_WR_PUT_LIST()m4_divert(-1)FID_FLUSH(1,200)')
554
555 m4_changequote([[,]])
556 # And now M4 is going to parse f-inst.c, fill the diversions
557 # and after the file is done, the content of m4_m4wrap (see before)
558 # is executed.