script optimization fixes for pattern tables

This commit is contained in:
Vern Paxson 2023-12-08 15:03:17 -05:00 committed by Arne Welzel
parent 709d410fcd
commit f573036099
4 changed files with 58 additions and 25 deletions

View file

@ -318,20 +318,25 @@ string CPPCompile::GenInExpr(const Expr* e, GenType gt) {
auto t1 = op1->GetType();
auto t2 = op2->GetType();
auto tag1 = t1->Tag();
auto tag2 = t2->Tag();
string gen;
if ( t1->Tag() == TYPE_PATTERN )
if ( tag1 == TYPE_STRING && tag2 == TYPE_TABLE && t2->AsTableType()->IsPatternIndex() )
gen = GenExpr(op2, GEN_DONT_CARE) + "->MatchPattern(" + GenExpr(op1, GEN_NATIVE) + ")";
else if ( tag1 == TYPE_PATTERN )
gen = string("(") + GenExpr(op1, GEN_DONT_CARE) + ")->MatchAnywhere(" + GenExpr(op2, GEN_DONT_CARE) +
"->AsString())";
else if ( t2->Tag() == TYPE_STRING )
else if ( tag2 == TYPE_STRING )
gen = string("str_in__CPP(") + GenExpr(op1, GEN_DONT_CARE) + "->AsString(), " + GenExpr(op2, GEN_DONT_CARE) +
"->AsString())";
else if ( t1->Tag() == TYPE_ADDR && t2->Tag() == TYPE_SUBNET )
else if ( tag1 == TYPE_ADDR && tag2 == TYPE_SUBNET )
gen = string("(") + GenExpr(op2, GEN_DONT_CARE) + ")->Contains(" + GenExpr(op1, GEN_VAL_PTR) + "->Get())";
else if ( t2->Tag() == TYPE_VECTOR )
else if ( tag2 == TYPE_VECTOR )
gen = GenExpr(op2, GEN_DONT_CARE) + "->Has(" + GenExpr(op1, GEN_NATIVE) + ")";
else

View file

@ -332,6 +332,8 @@ ExprPtr Inliner::CheckForInlining(CallExprPtr c) {
auto scope = func_vf->GetScope();
auto ie = DoInline(func_vf, body, c->ArgsPtr(), scope, ia->second);
printf("inlined %s\n", obj_desc(c.get()).c_str());
if ( ie ) {
ie->SetOriginal(c);
did_inline.insert(func_vf.get());

View file

@ -418,7 +418,16 @@ const ZAMStmt ZAMCompiler::CompileInExpr(const NameExpr* n1, const NameExpr* n2,
ZOp a;
if ( op2->GetType()->Tag() == TYPE_PATTERN )
auto& op2_t = op2->GetType();
auto& op3_t = op3->GetType();
if ( op3_t->Tag() == TYPE_TABLE ) {
if ( op3_t->AsTableType()->IsPatternIndex() && op2_t->Tag() == TYPE_STRING )
a = n2 ? OP_STR_IN_PAT_TBL_VVV : OP_STR_IN_PAT_TBL_VCV;
else
a = n2 ? OP_VAL_IS_IN_TABLE_VVV : OP_CONST_IS_IN_TABLE_VCV;
}
else if ( op2->GetType()->Tag() == TYPE_PATTERN )
a = n2 ? (n3 ? OP_P_IN_S_VVV : OP_P_IN_S_VVC) : OP_P_IN_S_VCV;
else if ( op2->GetType()->Tag() == TYPE_STRING )
@ -427,9 +436,6 @@ const ZAMStmt ZAMCompiler::CompileInExpr(const NameExpr* n1, const NameExpr* n2,
else if ( op2->GetType()->Tag() == TYPE_ADDR && op3->GetType()->Tag() == TYPE_SUBNET )
a = n2 ? (n3 ? OP_A_IN_S_VVV : OP_A_IN_S_VVC) : OP_A_IN_S_VCV;
else if ( op3->GetType()->Tag() == TYPE_TABLE )
a = n2 ? OP_VAL_IS_IN_TABLE_VVV : OP_CONST_IS_IN_TABLE_VCV;
else
reporter->InternalError("bad types when compiling \"in\"");
@ -650,19 +656,25 @@ const ZAMStmt ZAMCompiler::CompileIndex(const NameExpr* n1, int n2_slot, const T
}
if ( n2tag == TYPE_TABLE ) {
if ( is_pat_str_ind ) {
auto n1_slot = Frame1Slot(n1, OP1_WRITE);
if ( n3 ) {
int n3_slot = FrameSlot(n3);
auto op = is_pat_str_ind ? OP_TABLE_PATSTR_INDEX1_VVV : OP_TABLE_INDEX1_VVV;
auto zop = AssignmentFlavor(op, n1->GetType()->Tag());
z = ZInstI(OP_TABLE_PATSTR_INDEX_VVV, n1_slot, n2_slot, n3_slot);
}
else
z = ZInstI(OP_TABLE_PATSTR_INDEX_VVC, n1_slot, n2_slot, c3);
}
else if ( n3 ) {
int n3_slot = FrameSlot(n3);
auto zop = AssignmentFlavor(OP_TABLE_INDEX1_VVV, n1->GetType()->Tag());
z = ZInstI(zop, Frame1Slot(n1, zop), n2_slot, n3_slot);
z.SetType(n3->GetType());
}
else {
ASSERT(c3);
auto op = is_pat_str_ind ? OP_TABLE_PATSTR_INDEX1_VVC : OP_TABLE_INDEX1_VVC;
auto zop = AssignmentFlavor(op, n1->GetType()->Tag());
auto zop = AssignmentFlavor(OP_TABLE_INDEX1_VVC, n1->GetType()->Tag());
z = ZInstI(zop, Frame1Slot(n1, zop), n2_slot, c3);
}

View file

@ -696,6 +696,17 @@ internal-op P-In-S
type VVC
eval EvalPInS(frame[z.v2], z.c)
macro EvalStrInPatTbl(op1, op2)
frame[z.v1].int_val = op2.table_val->MatchPattern({NewRef{}, op1.string_val});
internal-op Str-In-Pat-Tbl
type VVV
eval EvalStrInPatTbl(frame[z.v2], frame[z.v3])
internal-op Str-In-Pat-Tbl
type VCV
eval EvalStrInPatTbl(z.c, frame[z.v2])
internal-binary-op S-In-S
op-accessor string_val
op-type I
@ -1045,6 +1056,20 @@ type VV
eval EvalTableIndex(z.aux->ToListVal(frame))
AssignV1(BuildVal(v, z.t))
macro EvalTablePatStr(index)
auto& lhs = frame[z.v1];
auto vec = ZVal(frame[z.v2].table_val->LookupPattern({NewRef{}, index.string_val}));
ZVal::DeleteManagedType(lhs);
lhs = vec;
internal-op Table-PatStr-Index
type VVV
eval EvalTablePatStr(frame[z.v3])
internal-op Table-PatStr-Index
type VVC
eval EvalTablePatStr(z.c)
internal-op When-Table-Index
type VV
eval EvalTableIndex(z.aux->ToListVal(frame))
@ -1080,17 +1105,6 @@ type VVC
assign-val v
eval EvalTableIndex(z.c.ToVal(z.t))
# Same, but for indexing table[pattern] of X with a string.
internal-assignment-op Table-PatStr-Index1
type VVV
assign-val v
eval auto v = frame[z.v2].table_val->LookupPattern({NewRef{}, frame[z.v3].AsString()});
internal-assignment-op Table-PatStr-Index1
type VVC
assign-val v
eval auto v = frame[z.v2].table_val->LookupPattern({NewRef{}, z.c.AsString()});
# This version is for a variable v3.
internal-op Index-String
type VVV