Filter refactoring: Some instructions eat up excessively much space.

This commit is contained in:
Maria Matejka 2018-12-20 14:29:47 +01:00
parent c577493908
commit 8e8b1fe48c
2 changed files with 31 additions and 42 deletions

View file

@ -11,47 +11,35 @@
/* Binary operators */ /* Binary operators */
case FI_ADD: case FI_ADD:
ARG(1,T_INT); ARG_T(1,0,T_INT);
ARG(2,T_INT); ARG_T(2,1,T_INT);
res.type = T_INT; res.val.i += v1.val.i;
res.val.i = v1.val.i + v2.val.i;
break; break;
case FI_SUBTRACT: case FI_SUBTRACT:
ARG(1,T_INT); ARG_T(1,0,T_INT);
ARG(2,T_INT); ARG_T(2,1,T_INT);
res.type = T_INT; res.val.i -= v1.val.i;
res.val.i = v1.val.i - v2.val.i;
break; break;
case FI_MULTIPLY: case FI_MULTIPLY:
ARG(1,T_INT); ARG_T(1,0,T_INT);
ARG(2,T_INT); ARG_T(2,1,T_INT);
res.type = T_INT; res.val.i *= v1.val.i;
res.val.i = v1.val.i * v2.val.i;
break; break;
case FI_DIVIDE: case FI_DIVIDE:
ARG(1,T_INT); ARG_T(1,0,T_INT);
ARG(2,T_INT); ARG_T(2,1,T_INT);
res.type = T_INT; if (v1.val.i == 0) runtime( "Mother told me not to divide by 0" );
if (v2.val.i == 0) runtime( "Mother told me not to divide by 0" ); res.val.i /= v1.val.i;
res.val.i = v1.val.i / v2.val.i;
break; break;
case FI_AND: case FI_AND:
ARG(1,T_BOOL); ARG_T(1,0,T_BOOL);
if (!v1.val.i) { if (res.val.i)
res = v1; ARG_T(2,0,T_BOOL);
} else {
ARG(2,T_BOOL);
res = v2;
}
break; break;
case FI_OR: case FI_OR:
ARG(1,T_BOOL); ARG_T(1,0,T_BOOL);
if (v1.val.i) { if (!res.val.i)
res = v1; ARG_T(2,0,T_BOOL);
} else {
ARG(2,T_BOOL);
res = v2;
}
break; break;
case FI_PAIR_CONSTRUCT: case FI_PAIR_CONSTRUCT:
ARG(1,T_INT); ARG(1,T_INT);
@ -184,8 +172,7 @@
break; break;
case FI_NOT: case FI_NOT:
ARG(1,T_BOOL); ARG_T(1,0,T_BOOL);
res = v1;
res.val.i = !res.val.i; res.val.i = !res.val.i;
break; break;
@ -642,7 +629,6 @@
break; break;
case FI_ROUTE_DISTINGUISHER: case FI_ROUTE_DISTINGUISHER:
ARG(1, T_NET); ARG(1, T_NET);
res.type = T_IP;
if (!net_is_vpn(v1.val.net)) if (!net_is_vpn(v1.val.net))
runtime( "VPN address expected" ); runtime( "VPN address expected" );
res.type = T_RD; res.type = T_RD;
@ -671,11 +657,10 @@
res.val.i = as_path_get_last_nonaggregated(v1.val.ad); res.val.i = as_path_get_last_nonaggregated(v1.val.ad);
break; break;
case FI_RETURN: case FI_RETURN:
ARG_ANY(1); ARG_ANY_T(1,0);
res = v1;
return F_RETURN; return F_RETURN;
case FI_CALL: case FI_CALL:
ARG_ANY(1); ARG_ANY_T(1,0);
fret = interpret(fs, what->a2.p); fret = interpret(fs, what->a2.p);
if (fret > F_RETURN) if (fret > F_RETURN)
return fret; return fret;

View file

@ -634,6 +634,7 @@ interpret(struct filter_state *fs, struct f_inst *what)
u32 as; u32 as;
#define res fs->stack[fs->stack_ptr].val #define res fs->stack[fs->stack_ptr].val
#define v0 res
#define v1 fs->stack[fs->stack_ptr + 1].val #define v1 fs->stack[fs->stack_ptr + 1].val
#define v2 fs->stack[fs->stack_ptr + 2].val #define v2 fs->stack[fs->stack_ptr + 2].val
#define v3 fs->stack[fs->stack_ptr + 3].val #define v3 fs->stack[fs->stack_ptr + 3].val
@ -650,15 +651,18 @@ interpret(struct filter_state *fs, struct f_inst *what)
return F_ERROR; \ return F_ERROR; \
} while(0) } while(0)
#define ARG_ANY(n) INTERPRET(what->a##n.p, n) #define ARG_ANY_T(n, tt) INTERPRET(what->a##n.p, tt)
#define ARG_ANY(n) ARG_ANY_T(n, n)
#define ARG(n,t) do { \ #define ARG_T(n,tt,t) do { \
ARG_ANY(n); \ ARG_ANY_T(n,tt); \
if (v##n.type != t) \ if (v##tt.type != t) \
runtime("Argument %d of instruction %s must be of type %02x, got %02x", \ runtime("Argument %d of instruction %s must be of type %02x, got %02x", \
n, f_instruction_name(what->fi_code), t, v##n.type); \ n, f_instruction_name(what->fi_code), t, v##tt.type); \
} while (0) } while (0)
#define ARG(n,t) ARG_T(n,n,t)
#define INTERPRET(what_, n) do { \ #define INTERPRET(what_, n) do { \
fs->stack_ptr += n; \ fs->stack_ptr += n; \
fret = interpret(fs, what_); \ fret = interpret(fs, what_); \