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