0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-09 20:58:44 +00:00

Filter: Use explicit function structure

This commit is contained in:
Ondrej Zajicek (work) 2019-10-21 16:28:15 +02:00
parent b3fdba0bf8
commit 3d7939561b
5 changed files with 21 additions and 13 deletions

View File

@ -116,7 +116,7 @@ struct symbol {
union { union {
struct proto_config *proto; /* For SYM_PROTO and SYM_TEMPLATE */ struct proto_config *proto; /* For SYM_PROTO and SYM_TEMPLATE */
const struct f_line *function; /* For SYM_FUNCTION */ const struct function *function; /* For SYM_FUNCTION */
const struct filter *filter; /* For SYM_FILTER */ const struct filter *filter; /* For SYM_FILTER */
struct rtable_config *table; /* For SYM_TABLE */ struct rtable_config *table; /* For SYM_TABLE */
struct f_dynamic_attr *attribute; /* For SYM_ATTRIBUTE */ struct f_dynamic_attr *attribute; /* For SYM_ATTRIBUTE */

View File

@ -492,7 +492,7 @@ bt_test_suite:
BT_TEST_SUITE '(' CF_SYM_KNOWN ',' text ')' { BT_TEST_SUITE '(' CF_SYM_KNOWN ',' text ')' {
cf_assert_symbol($3, SYM_FUNCTION); cf_assert_symbol($3, SYM_FUNCTION);
struct f_bt_test_suite *t = cfg_allocz(sizeof(struct f_bt_test_suite)); struct f_bt_test_suite *t = cfg_allocz(sizeof(struct f_bt_test_suite));
t->fn = $3->function; t->fn = $3->function->body;
t->fn_name = $3->name; t->fn_name = $3->name;
t->dsc = $5; t->dsc = $5;
@ -506,8 +506,8 @@ bt_test_same:
cf_assert_symbol($3, SYM_FUNCTION); cf_assert_symbol($3, SYM_FUNCTION);
cf_assert_symbol($5, SYM_FUNCTION); cf_assert_symbol($5, SYM_FUNCTION);
struct f_bt_test_suite *t = cfg_allocz(sizeof(struct f_bt_test_suite)); struct f_bt_test_suite *t = cfg_allocz(sizeof(struct f_bt_test_suite));
t->fn = $3->function; t->fn = $3->function->body;
t->cmp = $5->function; t->cmp = $5->function->body;
t->result = $7; t->result = $7;
t->fn_name = $3->name; t->fn_name = $3->name;
t->dsc = $5->name; t->dsc = $5->name;
@ -612,8 +612,10 @@ function_def:
cf_push_scope($2); cf_push_scope($2);
} function_args function_body { } function_args function_body {
DBG("Definition of function %s with %u args and %u local vars.\n", $2->name, $4, $5->vars); DBG("Definition of function %s with %u args and %u local vars.\n", $2->name, $4, $5->vars);
struct function *fn = cfg_alloc(sizeof(struct function));
*fn = (struct function) { .sym = $2, .body = $5 };
$2->function = fn;
$5->args = $4; $5->args = $4;
$2->function = $5;
cf_pop_scope(); cf_pop_scope();
} }
; ;

View File

@ -899,9 +899,9 @@
SYMBOL; SYMBOL;
FID_NEW_BODY() FID_NEW_BODY()
if (whati->varcount != sym->function->args) if (whati->varcount != sym->function->body->args)
cf_error("Function call '%s' got %u arguments, needs %u arguments", cf_error("Function call '%s' got %u arguments, needs %u arguments",
sym->name, whati->varcount, sym->function->args); sym->name, whati->varcount, sym->function->body->args);
/* Add void slot for return value (requires [[NEVER_CONSTANT]]) */ /* Add void slot for return value (requires [[NEVER_CONSTANT]]) */
struct f_inst *rv = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID }); struct f_inst *rv = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID });
@ -916,7 +916,7 @@
FID_INTERPRET_BODY() FID_INTERPRET_BODY()
/* Push the body on stack */ /* Push the body on stack */
LINEX(sym->function); LINEX(sym->function->body);
curline.emask |= FE_RETURN; curline.emask |= FE_RETURN;
/* Set new base for local variables */ /* Set new base for local variables */
@ -926,8 +926,9 @@
fstk->vcnt += whati->varcount; fstk->vcnt += whati->varcount;
/* Storage for local variables */ /* Storage for local variables */
memset(&(fstk->vstk[fstk->vcnt]), 0, sizeof(struct f_val) * sym->function->vars); uint vars = sym->function->body->vars;
fstk->vcnt += sym->function->vars; memset(&(fstk->vstk[fstk->vcnt]), 0, sizeof(struct f_val) * vars);
fstk->vcnt += vars;
} }
INST(FI_DROP_RESULT, 1, 0) { INST(FI_DROP_RESULT, 1, 0) {

View File

@ -457,7 +457,7 @@ filter_commit(struct config *new, struct config *old)
case SYM_FUNCTION: case SYM_FUNCTION:
if ((osym = cf_find_symbol(old, sym->name)) && if ((osym = cf_find_symbol(old, sym->name)) &&
(osym->class == SYM_FUNCTION) && (osym->class == SYM_FUNCTION) &&
f_same(sym->function, osym->function)) f_same(sym->function->body, osym->function->body))
sym->flags |= SYM_FLAG_SAME; sym->flags |= SYM_FLAG_SAME;
else else
sym->flags &= ~SYM_FLAG_SAME; sym->flags &= ~SYM_FLAG_SAME;
@ -485,7 +485,7 @@ void filters_dump_all(void)
break; break;
case SYM_FUNCTION: case SYM_FUNCTION:
debug("Function %s:\n", sym->name); debug("Function %s:\n", sym->name);
f_dump_line(sym->function, 1); f_dump_line(sym->function->body, 1);
break; break;
case SYM_PROTO: case SYM_PROTO:
{ {

View File

@ -43,14 +43,19 @@ static inline const char *filter_return_str(const enum filter_return fret) {
} }
struct f_val; struct f_val;
struct f_line;
/* The filter encapsulating structure to be pointed-to from outside */ /* The filter encapsulating structure to be pointed-to from outside */
struct f_line;
struct filter { struct filter {
struct symbol *sym; struct symbol *sym;
const struct f_line *root; const struct f_line *root;
}; };
struct function {
struct symbol *sym;
const struct f_line *body;
};
struct rte; struct rte;
enum filter_return f_run(const struct filter *filter, struct rte **rte, struct linpool *tmp_pool, int flags); enum filter_return f_run(const struct filter *filter, struct rte **rte, struct linpool *tmp_pool, int flags);