]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/decl.m4
Merge remote-tracking branch 'origin/master' into haugesund
[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 # 110 constructor attributes
36 # 103 constructor body
37 # 104 dump line item content
38 # (there may be nothing in dump-line content and
39 # it must be handled specially in phase 2)
40 # 105 linearize body
41 # 106 comparator body
42 # 107 struct f_line_item content
43 # 108 interpreter body
44 # 109 iterator body
45 #
46 # Here are macros to allow you to _divert to the right directions.
47 m4_define(FID_STRUCT_IN, `m4_divert(101)')
48 m4_define(FID_NEW_ARGS, `m4_divert(102)')
49 m4_define(FID_NEW_ATTRIBUTES, `m4_divert(110)')
50 m4_define(FID_NEW_BODY, `m4_divert(103)')
51 m4_define(FID_DUMP_BODY, `m4_divert(104)m4_define([[FID_DUMP_BODY_EXISTS]])')
52 m4_define(FID_LINEARIZE_BODY, `m4_divert(105)')
53 m4_define(FID_SAME_BODY, `m4_divert(106)')
54 m4_define(FID_LINE_IN, `m4_divert(107)')
55 m4_define(FID_INTERPRET_BODY, `m4_divert(108)')
56 m4_define(FID_ITERATE_BODY, `m4_divert(109)')
57
58 # Sometimes you want slightly different code versions in different
59 # outputs.
60 # Use FID_HIC(code for inst-gen.h, code for inst-gen.c, code for inst-interpret.c)
61 # and put it into [[ ]] quotes if it shall contain commas.
62 m4_define(FID_HIC, `m4_ifelse(TARGET, [[H]], [[$1]], TARGET, [[I]], [[$2]], TARGET, [[C]], [[$3]])')
63
64 # In interpreter code, this is quite common.
65 m4_define(FID_INTERPRET_EXEC, `FID_HIC(,[[FID_INTERPRET_BODY()]],[[m4_divert(-1)]])')
66 m4_define(FID_INTERPRET_NEW, `FID_HIC(,[[m4_divert(-1)]],[[FID_INTERPRET_BODY()]])')
67
68 # If the instruction is never converted to constant, the interpret
69 # code is not produced at all for constructor
70 m4_define(NEVER_CONSTANT, `m4_define([[INST_NEVER_CONSTANT]])')
71 m4_define(FID_IFCONST, `m4_ifdef([[INST_NEVER_CONSTANT]],[[$2]],[[$1]])')
72
73 # If the instruction has some attributes (here called members),
74 # these are typically carried with the instruction from constructor
75 # to interpreter. This yields a line of code everywhere on the path.
76 # FID_MEMBER is a macro to help with this task.
77 m4_define(FID_MEMBER, `m4_dnl
78 FID_LINE_IN()m4_dnl
79 $1 $2;
80 FID_STRUCT_IN()m4_dnl
81 $1 $2;
82 FID_NEW_ARGS()m4_dnl
83 , $1 $2
84 FID_NEW_BODY()m4_dnl
85 whati->$2 = $2;
86 FID_LINEARIZE_BODY()m4_dnl
87 item->$2 = whati->$2;
88 m4_ifelse($3,,,[[
89 FID_SAME_BODY()m4_dnl
90 if ($3) return 0;
91 ]])
92 m4_ifelse($4,,,[[
93 FID_DUMP_BODY()m4_dnl
94 debug("%s" $4 "\n", INDENT, $5);
95 ]])
96 FID_INTERPRET_EXEC()m4_dnl
97 const $1 $2 = whati->$2
98 FID_INTERPRET_BODY')
99
100 # Instruction arguments are needed only until linearization is done.
101 # This puts the arguments into the filter line to be executed before
102 # the instruction itself.
103 #
104 # To achieve this, ARG_ANY must be called before anything writes into
105 # the instruction line as it moves the instruction pointer forward.
106 m4_define(ARG_ANY, `
107 FID_STRUCT_IN()m4_dnl
108 struct f_inst * f$1;
109 FID_NEW_ARGS()m4_dnl
110 , struct f_inst * f$1
111 FID_NEW_ATTRIBUTES()m4_dnl
112 NONNULL(m4_eval($1+1))
113 FID_NEW_BODY()m4_dnl
114 whati->f$1 = f$1;
115 const struct f_inst *child$1 = f$1;
116 do {
117 what->size += child$1->size;
118 FID_IFCONST([[
119 if (child$1->fi_code != FI_CONSTANT)
120 constargs = 0;
121 ]])
122 } while (child$1 = child$1->next);
123 FID_LINEARIZE_BODY
124 pos = linearize(dest, whati->f$1, pos);
125 FID_INTERPRET_BODY()')
126
127 # Some instructions accept variable number of arguments.
128 m4_define(VARARG, `
129 FID_NEW_ARGS()m4_dnl
130 , struct f_inst * fvar
131 FID_STRUCT_IN()m4_dnl
132 struct f_inst * fvar;
133 uint varcount;
134 FID_LINE_IN()m4_dnl
135 uint varcount;
136 FID_NEW_BODY()m4_dnl
137 whati->varcount = 0;
138 whati->fvar = fvar;
139 for (const struct f_inst *child = fvar; child; child = child->next, whati->varcount++) {
140 what->size += child->size;
141 FID_IFCONST([[
142 if (child->fi_code != FI_CONSTANT)
143 constargs = 0;
144 ]])
145 }
146 FID_IFCONST([[
147 const struct f_inst **items = NULL;
148 if (constargs && whati->varcount) {
149 items = alloca(whati->varcount * sizeof(struct f_inst *));
150 const struct f_inst *child = fvar;
151 for (uint i=0; child; i++)
152 child = (items[i] = child)->next;
153 }
154 ]])
155 FID_LINEARIZE_BODY()m4_dnl
156 pos = linearize(dest, whati->fvar, pos);
157 item->varcount = whati->varcount;
158 FID_DUMP_BODY()m4_dnl
159 debug("%snumber of varargs %u\n", INDENT, item->varcount);
160 FID_SAME_BODY()m4_dnl
161 if (f1->varcount != f2->varcount) return 0;
162 FID_INTERPRET_BODY()
163 FID_HIC(,[[
164 if (fstk->vcnt < whati->varcount) runtime("Stack underflow");
165 fstk->vcnt -= whati->varcount;
166 ]],)
167 ')
168
169 # Some arguments need to check their type. After that, ARG_ANY is called.
170 m4_define(ARG, `ARG_ANY($1) ARG_TYPE($1,$2)')
171 m4_define(ARG_TYPE, `ARG_TYPE_STATIC($1,$2) ARG_TYPE_DYNAMIC($1,$2)')
172
173 m4_define(ARG_TYPE_STATIC, `
174 FID_NEW_BODY()m4_dnl
175 if (f$1->type && (f$1->type != ($2)) && !f_const_promotion(f$1, ($2)))
176 cf_error("Argument $1 of %s must be of type %s, got type %s",
177 f_instruction_name(what->fi_code), f_type_name($2), f_type_name(f$1->type));
178 FID_INTERPRET_BODY()')
179
180 m4_define(ARG_TYPE_DYNAMIC, `
181 FID_INTERPRET_EXEC()m4_dnl
182 if (v$1.type != ($2))
183 runtime("Argument $1 of %s must be of type %s, got type %s",
184 f_instruction_name(what->fi_code), f_type_name($2), f_type_name(v$1.type));
185 FID_INTERPRET_BODY()')
186
187 m4_define(ARG_SAME_TYPE, `
188 FID_NEW_BODY()m4_dnl
189 if (f$1->type && f$2->type && (f$1->type != f$2->type) &&
190 !f_const_promotion(f$2, f$1->type) && !f_const_promotion(f$1, f$2->type))
191 cf_error("Arguments $1 and $2 of %s must be of the same type", f_instruction_name(what->fi_code));
192 FID_INTERPRET_BODY()')
193
194 # Executing another filter line. This replaces the recursion
195 # that was needed in the former implementation.
196 m4_define(LINEX, `FID_INTERPRET_EXEC()LINEX_($1)FID_INTERPRET_NEW()return $1 FID_INTERPRET_BODY()')
197 m4_define(LINEX_, `do {
198 if (fstk->ecnt + 1 >= fstk->elen) runtime("Filter execution stack overflow");
199 fstk->estk[fstk->ecnt].pos = 0;
200 fstk->estk[fstk->ecnt].line = $1;
201 fstk->estk[fstk->ecnt].ventry = fstk->vcnt;
202 fstk->estk[fstk->ecnt].vbase = fstk->estk[fstk->ecnt-1].vbase;
203 fstk->estk[fstk->ecnt].emask = 0;
204 fstk->ecnt++;
205 } while (0)')
206
207 m4_define(LINE, `
208 FID_LINE_IN()m4_dnl
209 const struct f_line * fl$1;
210 FID_STRUCT_IN()m4_dnl
211 struct f_inst * f$1;
212 FID_NEW_ARGS()m4_dnl
213 , struct f_inst * f$1
214 FID_NEW_BODY()m4_dnl
215 whati->f$1 = f$1;
216 FID_DUMP_BODY()m4_dnl
217 f_dump_line(item->fl$1, indent + 1);
218 FID_LINEARIZE_BODY()m4_dnl
219 item->fl$1 = f_linearize(whati->f$1);
220 FID_SAME_BODY()m4_dnl
221 if (!f_same(f1->fl$1, f2->fl$1)) return 0;
222 FID_ITERATE_BODY()m4_dnl
223 if (whati->fl$1) BUFFER_PUSH(fit->lines) = whati->fl$1;
224 FID_INTERPRET_EXEC()m4_dnl
225 do { if (whati->fl$1) {
226 LINEX_(whati->fl$1);
227 } } while(0)
228 FID_INTERPRET_NEW()m4_dnl
229 return whati->f$1
230 FID_INTERPRET_BODY()')
231
232 # Some of the instructions have a result. These constructions
233 # state the result and put it to the right place.
234 m4_define(RESULT, `RESULT_TYPE([[$1]]) RESULT_([[$1]],[[$2]],[[$3]])')
235 m4_define(RESULT_, `RESULT_VAL([[ (struct f_val) { .type = $1, .val.$2 = $3 } ]])')
236 m4_define(RESULT_VAL, `FID_HIC(, [[do { res = $1; f_vcnt_check_overflow(1); fstk->vcnt++; } while (0)]],
237 [[return fi_constant(what, $1)]])')
238 m4_define(RESULT_VOID, `RESULT_VAL([[ (struct f_val) { .type = T_VOID } ]])')
239
240 m4_define(ERROR,
241 `m4_errprint(m4___file__:m4___line__: $*
242 )m4_m4exit(1)')
243
244 # This macro specifies result type and makes there are no conflicting definitions
245 m4_define(RESULT_TYPE,
246 `m4_ifdef([[INST_RESULT_TYPE]],
247 [[m4_ifelse(INST_RESULT_TYPE,$1,,[[ERROR([[Multiple type definitons]])]])]],
248 [[m4_define(INST_RESULT_TYPE,$1) RESULT_TYPE_($1)]])')
249
250 m4_define(RESULT_TYPE_, `
251 FID_NEW_BODY()m4_dnl
252 what->type = $1;
253 FID_INTERPRET_BODY()')
254
255 # Some common filter instruction members
256 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)')
257 m4_define(RTC, `FID_MEMBER(struct rtable_config *, rtc, [[strcmp(f1->rtc->name, f2->rtc->name)]], "route table %s", item->rtc->name)')
258 m4_define(STATIC_ATTR, `FID_MEMBER(struct f_static_attr, sa, f1->sa.sa_code != f2->sa.sa_code,,)')
259 m4_define(DYNAMIC_ATTR, `FID_MEMBER(struct f_dynamic_attr, da, f1->da.ea_code != f2->da.ea_code,,)')
260 m4_define(ACCESS_RTE, `FID_HIC(,[[do { if (!fs->rte) runtime("No route to access"); } while (0)]],NEVER_CONSTANT())')
261
262 # 2) Code wrapping
263 # The code produced in 1xx temporary diversions is a raw code without
264 # any auxiliary commands and syntactical structures around. When the
265 # instruction is done, INST_FLUSH is called. More precisely, it is called
266 # at the beginning of INST() call and at the end of file.
267 #
268 # INST_FLUSH picks all the temporary diversions, wraps their content
269 # into appropriate headers and structures and saves them into global
270 # diversions listed:
271 #
272 # 4 enum fi_code
273 # 5 enum fi_code to string
274 # 6 dump line item
275 # 7 dump line item callers
276 # 8 linearize
277 # 9 same (filter comparator)
278 # 10 iterate
279 # 1 union in struct f_inst
280 # 3 constructors + interpreter
281 #
282 # These global diversions contain blocks of code that can be directly
283 # put into the final file, yet it still can't be written out now as
284 # every instruction writes to all of these diversions.
285
286 # Code wrapping diversion names. Here we want an explicit newline
287 # after the C comment.
288 m4_define(FID_ZONE, `m4_divert($1) /* $2 for INST_NAME() */
289 ')
290 m4_define(FID_INST, `FID_ZONE(1, Instruction structure for config)')
291 m4_define(FID_LINE, `FID_ZONE(2, Instruction structure for interpreter)')
292 m4_define(FID_NEW, `FID_ZONE(3, Constructor)')
293 m4_define(FID_ENUM, `FID_ZONE(4, Code enum)')
294 m4_define(FID_ENUM_STR, `FID_ZONE(5, Code enum to string)')
295 m4_define(FID_DUMP, `FID_ZONE(6, Dump line)')
296 m4_define(FID_DUMP_CALLER, `FID_ZONE(7, Dump line caller)')
297 m4_define(FID_LINEARIZE, `FID_ZONE(8, Linearize)')
298 m4_define(FID_SAME, `FID_ZONE(9, Comparison)')
299 m4_define(FID_ITERATE, `FID_ZONE(10, Iteration)')
300
301 # This macro does all the code wrapping. See inline comments.
302 m4_define(INST_FLUSH, `m4_ifdef([[INST_NAME]], [[
303 FID_ENUM()m4_dnl Contents of enum fi_code { ... }
304 INST_NAME(),
305 FID_ENUM_STR()m4_dnl Contents of const char * indexed by enum fi_code
306 [INST_NAME()] = "INST_NAME()",
307 FID_INST()m4_dnl Anonymous structure inside struct f_inst
308 struct {
309 m4_undivert(101)m4_dnl
310 } i_[[]]INST_NAME();
311 FID_LINE()m4_dnl Anonymous structure inside struct f_line_item
312 struct {
313 m4_undivert(107)m4_dnl
314 } i_[[]]INST_NAME();
315 FID_NEW()m4_dnl Constructor and interpreter code together
316 FID_HIC(
317 [[m4_dnl Public declaration of constructor in H file
318 struct f_inst *
319 m4_undivert(110)m4_dnl
320 f_new_inst_]]INST_NAME()[[(enum f_instruction_code fi_code
321 m4_undivert(102)m4_dnl
322 );]],
323 [[m4_dnl The one case in The Big Switch inside interpreter
324 case INST_NAME():
325 #define whati (&(what->i_]]INST_NAME()[[))
326 m4_ifelse(m4_eval(INST_INVAL() > 0), 1, [[if (fstk->vcnt < INST_INVAL()) runtime("Stack underflow"); fstk->vcnt -= INST_INVAL(); ]])
327 m4_undivert(108)m4_dnl
328 #undef whati
329 break;
330 ]],
331 [[m4_dnl Constructor itself
332 struct f_inst *
333 m4_undivert(110)m4_dnl
334 f_new_inst_]]INST_NAME()[[(enum f_instruction_code fi_code
335 m4_undivert(102)m4_dnl
336 )
337 {
338 /* Allocate the structure */
339 struct f_inst *what = fi_new(fi_code);
340 FID_IFCONST([[uint constargs = 1;]])
341
342 /* Initialize all the members */
343 #define whati (&(what->i_]]INST_NAME()[[))
344 m4_undivert(103)m4_dnl
345
346 /* If not constant, return the instruction itself */
347 FID_IFCONST([[if (!constargs)]])
348 return what;
349
350 /* Try to pre-calculate the result */
351 FID_IFCONST([[m4_undivert(108)]])m4_dnl
352 #undef whati
353 }
354 ]])
355
356 FID_DUMP_CALLER()m4_dnl Case in another big switch used in instruction dumping (debug)
357 case INST_NAME(): f_dump_line_item_]]INST_NAME()[[(item, indent + 1); break;
358
359 FID_DUMP()m4_dnl The dumper itself
360 m4_ifdef([[FID_DUMP_BODY_EXISTS]],
361 [[static inline void f_dump_line_item_]]INST_NAME()[[(const struct f_line_item *item_, const int indent)]],
362 [[static inline void f_dump_line_item_]]INST_NAME()[[(const struct f_line_item *item UNUSED, const int indent UNUSED)]])
363 m4_undefine([[FID_DUMP_BODY_EXISTS]])
364 {
365 #define item (&(item_->i_]]INST_NAME()[[))
366 m4_undivert(104)m4_dnl
367 #undef item
368 }
369
370 FID_LINEARIZE()m4_dnl The linearizer
371 case INST_NAME(): {
372 #define whati (&(what->i_]]INST_NAME()[[))
373 #define item (&(dest->items[pos].i_]]INST_NAME()[[))
374 m4_undivert(105)m4_dnl
375 #undef whati
376 #undef item
377 dest->items[pos].fi_code = what->fi_code;
378 dest->items[pos].lineno = what->lineno;
379 break;
380 }
381
382 FID_SAME()m4_dnl This code compares two f_line"s while reconfiguring
383 case INST_NAME():
384 #define f1 (&(f1_->i_]]INST_NAME()[[))
385 #define f2 (&(f2_->i_]]INST_NAME()[[))
386 m4_undivert(106)m4_dnl
387 #undef f1
388 #undef f2
389 break;
390
391 FID_ITERATE()m4_dnl The iterator
392 case INST_NAME():
393 #define whati (&(what->i_]]INST_NAME()[[))
394 m4_undivert(109)m4_dnl
395 #undef whati
396 break;
397
398 m4_divert(-1)FID_FLUSH(101,200)m4_dnl And finally this flushes all the unused diversions
399 ]])')
400
401 m4_define(INST, `m4_dnl This macro is called on beginning of each instruction.
402 INST_FLUSH()m4_dnl First, old data is flushed
403 m4_define([[INST_NAME]], [[$1]])m4_dnl Then we store instruction name,
404 m4_define([[INST_INVAL]], [[$2]])m4_dnl instruction input value count,
405 m4_undefine([[INST_NEVER_CONSTANT]])m4_dnl reset NEVER_CONSTANT trigger,
406 m4_undefine([[INST_RESULT_TYPE]])m4_dnl and reset RESULT_TYPE value.
407 FID_INTERPRET_BODY()m4_dnl By default, every code is interpreter code.
408 ')
409
410 # 3) Final preparation
411 #
412 # Now we prepare all the code around the global diversions.
413 # It must be here, not in m4wrap, as we want M4 to mark the code
414 # by #line directives correctly, not to claim that every single line
415 # is at the beginning of the m4wrap directive.
416 #
417 # This part is split by the final file.
418 # H for inst-gen.h
419 # I for inst-interpret.c
420 # C for inst-gen.c
421 #
422 # So we in cycle:
423 # A. open a diversion
424 # B. send there some code
425 # C. close that diversion
426 # D. flush a global diversion
427 # E. open another diversion and goto B.
428 #
429 # Final diversions
430 # 200+ completed text before it is flushed to output
431
432 # This is a list of output diversions
433 m4_define(FID_WR_PUT_LIST)
434
435 # This macro does the steps C to E, see before.
436 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)')
437
438 # These macros do the splitting between H/I/C
439 m4_define(FID_WR_DIRECT, `m4_ifelse(TARGET,[[$1]],[[FID_WR_INIT()]],[[FID_WR_STOP()]])')
440 m4_define(FID_WR_INIT, `m4_define([[FID_WR_DIDX]],200)m4_define([[FID_WR_PUT]],[[FID_WR_PUT_ALSO($]][[@)]])m4_divert(200)')
441 m4_define(FID_WR_STOP, `m4_define([[FID_WR_PUT]])m4_divert(-1)')
442
443 # Here is the direct code to be put into the output files
444 # together with the undiversions, being hidden under FID_WR_PUT()
445
446 m4_changequote([[,]])
447 FID_WR_DIRECT(I)
448 FID_WR_PUT(3)
449 FID_WR_DIRECT(C)
450
451 #if defined(__GNUC__) && __GNUC__ >= 6
452 #pragma GCC diagnostic push
453 #pragma GCC diagnostic ignored "-Wmisleading-indentation"
454 #endif
455
456 #include "nest/bird.h"
457 #include "filter/filter.h"
458 #include "filter/f-inst.h"
459
460 /* Instruction codes to string */
461 static const char * const f_instruction_name_str[] = {
462 FID_WR_PUT(5)
463 };
464
465 const char *
466 f_instruction_name_(enum f_instruction_code fi)
467 {
468 if (fi < (sizeof(f_instruction_name_str) / sizeof(f_instruction_name_str[0])))
469 return f_instruction_name_str[fi];
470 else
471 bug("Got unknown instruction code: %d", fi);
472 }
473
474 static inline struct f_inst *
475 fi_new(enum f_instruction_code fi_code)
476 {
477 struct f_inst *what = cfg_allocz(sizeof(struct f_inst));
478 what->lineno = ifs->lino;
479 what->size = 1;
480 what->fi_code = fi_code;
481 return what;
482 }
483
484 static inline struct f_inst *
485 fi_constant(struct f_inst *what, struct f_val val)
486 {
487 what->fi_code = FI_CONSTANT;
488 what->i_FI_CONSTANT.val = val;
489 return what;
490 }
491
492 static int
493 f_const_promotion(struct f_inst *arg, enum f_type want)
494 {
495 if (arg->fi_code != FI_CONSTANT)
496 return 0;
497
498 struct f_val *c = &arg->i_FI_CONSTANT.val;
499
500 if ((c->type == T_IP) && ipa_is_ip4(c->val.ip) && (want == T_QUAD)) {
501 *c = (struct f_val) {
502 .type = T_QUAD,
503 .val.i = ipa_to_u32(c->val.ip),
504 };
505 return 1;
506 }
507
508 return 0;
509 }
510
511 #define v1 whati->f1->i_FI_CONSTANT.val
512 #define v2 whati->f2->i_FI_CONSTANT.val
513 #define v3 whati->f3->i_FI_CONSTANT.val
514 #define vv(i) items[i]->i_FI_CONSTANT.val
515 #define runtime(fmt, ...) cf_error("filter preevaluation, line %d: " fmt, ifs->lino, ##__VA_ARGS__)
516 #define fpool cfg_mem
517 #define falloc(size) cfg_alloc(size)
518 /* Instruction constructors */
519 FID_WR_PUT(3)
520 #undef v1
521 #undef v2
522 #undef v3
523 #undef vv
524
525 /* Line dumpers */
526 #define INDENT (((const char *) f_dump_line_indent_str) + sizeof(f_dump_line_indent_str) - (indent) - 1)
527 static const char f_dump_line_indent_str[] = " ";
528
529 FID_WR_PUT(6)
530
531 void f_dump_line(const struct f_line *dest, uint indent)
532 {
533 if (!dest) {
534 debug("%sNo filter line (NULL)\n", INDENT);
535 return;
536 }
537 debug("%sFilter line %p (len=%u)\n", INDENT, dest, dest->len);
538 for (uint i=0; i<dest->len; i++) {
539 const struct f_line_item *item = &dest->items[i];
540 debug("%sInstruction %s at line %u\n", INDENT, f_instruction_name_(item->fi_code), item->lineno);
541 switch (item->fi_code) {
542 FID_WR_PUT(7)
543 default: bug("Unknown instruction %x in f_dump_line", item->fi_code);
544 }
545 }
546 debug("%sFilter line %p dump done\n", INDENT, dest);
547 }
548
549 /* Linearize */
550 static uint
551 linearize(struct f_line *dest, const struct f_inst *what, uint pos)
552 {
553 for ( ; what; what = what->next) {
554 switch (what->fi_code) {
555 FID_WR_PUT(8)
556 }
557 pos++;
558 }
559 return pos;
560 }
561
562 struct f_line *
563 f_linearize_concat(const struct f_inst * const inst[], uint count)
564 {
565 uint len = 0;
566 for (uint i=0; i<count; i++)
567 for (const struct f_inst *what = inst[i]; what; what = what->next)
568 len += what->size;
569
570 struct f_line *out = cfg_allocz(sizeof(struct f_line) + sizeof(struct f_line_item)*len);
571
572 for (uint i=0; i<count; i++)
573 out->len = linearize(out, inst[i], out->len);
574
575 #ifdef LOCAL_DEBUG
576 f_dump_line(out, 0);
577 #endif
578 return out;
579 }
580
581 /* Filter line comparison */
582 int
583 f_same(const struct f_line *fl1, const struct f_line *fl2)
584 {
585 if ((!fl1) && (!fl2))
586 return 1;
587 if ((!fl1) || (!fl2))
588 return 0;
589 if (fl1->len != fl2->len)
590 return 0;
591 for (uint i=0; i<fl1->len; i++) {
592 #define f1_ (&(fl1->items[i]))
593 #define f2_ (&(fl2->items[i]))
594 if (f1_->fi_code != f2_->fi_code)
595 return 0;
596 if (f1_->flags != f2_->flags)
597 return 0;
598
599 switch(f1_->fi_code) {
600 FID_WR_PUT(9)
601 }
602 }
603 #undef f1_
604 #undef f2_
605 return 1;
606 }
607
608
609 /* Part of FI_SWITCH filter iterator */
610 static void
611 f_add_tree_lines(const struct f_tree *t, void *fit_)
612 {
613 struct filter_iterator * fit = fit_;
614
615 if (t->data)
616 BUFFER_PUSH(fit->lines) = t->data;
617 }
618
619 /* Filter line iterator */
620 void
621 f_add_lines(const struct f_line_item *what, struct filter_iterator *fit)
622 {
623 switch(what->fi_code) {
624 FID_WR_PUT(10)
625 }
626 }
627
628
629 #if defined(__GNUC__) && __GNUC__ >= 6
630 #pragma GCC diagnostic pop
631 #endif
632
633 FID_WR_DIRECT(H)
634 /* Filter instruction codes */
635 enum f_instruction_code {
636 FID_WR_PUT(4)m4_dnl
637 } PACKED;
638
639 /* Filter instruction structure for config */
640 struct f_inst {
641 struct f_inst *next; /* Next instruction */
642 enum f_instruction_code fi_code; /* Instruction code */
643 enum f_type type; /* Type of returned value, if known */
644 int size; /* How many instructions are underneath */
645 int lineno; /* Line number */
646 union {
647 FID_WR_PUT(1)m4_dnl
648 };
649 };
650
651 /* Filter line item */
652 struct f_line_item {
653 enum f_instruction_code fi_code; /* What to do */
654 enum f_instruction_flags flags; /* Flags, instruction-specific */
655 uint lineno; /* Where */
656 union {
657 FID_WR_PUT(2)m4_dnl
658 };
659 };
660
661 /* Instruction constructors */
662 FID_WR_PUT(3)
663 m4_divert(-1)
664
665 # 4) Shipout
666 #
667 # Everything is prepared in FID_WR_PUT_LIST now. Let's go!
668
669 m4_changequote(`,')
670
671 # Flusher auxiliary macro
672 m4_define(FID_FLUSH, `m4_ifelse($1,$2,,[[m4_undivert($1)FID_FLUSH(m4_eval($1+1),$2)]])')
673
674 # Defining the macro used in FID_WR_PUT_LIST
675 m4_define(FID_WR_DPUT, `m4_undivert($1)')
676
677 # After the code is read and parsed, we:
678 m4_m4wrap(`INST_FLUSH()m4_divert(0)FID_WR_PUT_LIST()m4_divert(-1)FID_FLUSH(1,200)')
679
680 m4_changequote([[,]])
681 # And now M4 is going to parse f-inst.c, fill the diversions
682 # and after the file is done, the content of m4_m4wrap (see before)
683 # is executed.