script optimization support for "add" and "delete" being expressions

This commit is contained in:
Vern Paxson 2024-05-16 14:37:30 -07:00 committed by Tim Wojtulewicz
parent 0e5bece385
commit 37c1f6641c
11 changed files with 132 additions and 144 deletions

View file

@ -688,8 +688,6 @@ private:
void GenIfStmt(const IfStmt* i);
void GenWhileStmt(const WhileStmt* w);
void GenReturnStmt(const ReturnStmt* r);
void GenAddStmt(const ExprStmt* es);
void GenDeleteStmt(const ExprStmt* es);
void GenEventStmt(const EventStmt* ev);
void GenSwitchStmt(const SwitchStmt* sw);
@ -757,6 +755,8 @@ private:
std::string GenNameExpr(const NameExpr* ne, GenType gt);
std::string GenConstExpr(const ConstExpr* c, GenType gt);
std::string GenAggrAdd(const Expr* e);
std::string GenAggrDel(const Expr* e);
std::string GenIncrExpr(const Expr* e, GenType gt, bool is_incr, bool top_level);
std::string GenCondExpr(const Expr* e, GenType gt);
std::string GenCallExpr(const CallExpr* c, GenType gt, bool top_level);

View file

@ -52,6 +52,9 @@ string CPPCompile::GenExpr(const Expr* e, GenType gt, bool top_level) {
gen = GenExpr(e->GetOp1(), GEN_VAL_PTR) + "->Clone()";
return GenericValPtrToGT(gen, e->GetType(), gt);
case EXPR_AGGR_ADD: return GenAggrAdd(e);
case EXPR_AGGR_DEL: return GenAggrDel(e);
case EXPR_INCR:
case EXPR_DECR: return GenIncrExpr(e, gt, e->Tag() == EXPR_INCR, top_level);
@ -183,6 +186,39 @@ string CPPCompile::GenConstExpr(const ConstExpr* c, GenType gt) {
return NativeToGT(GenVal(c->ValuePtr()), t, gt);
}
string CPPCompile::GenAggrAdd(const Expr* e) {
auto op = e->GetOp1();
auto aggr = GenExpr(op->GetOp1(), GEN_DONT_CARE);
auto indices = GenExpr(op->GetOp2(), GEN_VAL_PTR);
return "add_element__CPP(" + aggr + ", index_val__CPP({" + indices + "}))";
}
string CPPCompile::GenAggrDel(const Expr* e) {
auto op = e->GetOp1();
if ( op->Tag() == EXPR_NAME ) {
auto aggr_gen = GenExpr(op, GEN_VAL_PTR);
if ( op->GetType()->Tag() == TYPE_TABLE )
return aggr_gen + "->RemoveAll()";
else
return aggr_gen + "->Resize(0)";
}
auto aggr = op->GetOp1();
auto aggr_gen = GenExpr(aggr, GEN_VAL_PTR);
if ( op->Tag() == EXPR_INDEX ) {
auto indices = GenExpr(op->GetOp2(), GEN_VAL_PTR);
return "remove_element__CPP(" + aggr_gen + ", index_val__CPP({" + indices + "}))";
}
ASSERT(op->Tag() == EXPR_FIELD);
auto field = GenField(aggr, op->AsFieldExpr()->Field());
return aggr_gen + "->Remove(" + field + ")";
}
string CPPCompile::GenIncrExpr(const Expr* e, GenType gt, bool is_incr, bool top_level) {
// For compound operands (table indexing, record fields),
// Zeek's interpreter will actually evaluate the operand

View file

@ -39,10 +39,6 @@ void CPPCompile::GenStmt(const Stmt* s) {
case STMT_RETURN: GenReturnStmt(s->AsReturnStmt()); break;
case STMT_ADD: GenAddStmt(static_cast<const ExprStmt*>(s)); break;
case STMT_DELETE: GenDeleteStmt(static_cast<const ExprStmt*>(s)); break;
case STMT_EVENT: GenEventStmt(static_cast<const EventStmt*>(s)); break;
case STMT_SWITCH: GenSwitchStmt(static_cast<const SwitchStmt*>(s)); break;
@ -149,41 +145,6 @@ void CPPCompile::GenReturnStmt(const ReturnStmt* r) {
}
}
void CPPCompile::GenAddStmt(const ExprStmt* es) {
auto op = es->StmtExpr();
auto aggr = GenExpr(op->GetOp1(), GEN_DONT_CARE);
auto indices = op->GetOp2();
Emit("add_element__CPP(%s, index_val__CPP({%s}));", aggr, GenExpr(indices, GEN_VAL_PTR));
}
void CPPCompile::GenDeleteStmt(const ExprStmt* es) {
auto op = es->StmtExpr();
if ( op->Tag() == EXPR_NAME ) {
if ( op->GetType()->Tag() == TYPE_TABLE )
Emit("%s->RemoveAll();", GenExpr(op, GEN_VAL_PTR));
else
Emit("%s->Resize(0);", GenExpr(op, GEN_VAL_PTR));
return;
}
auto aggr = op->GetOp1();
auto aggr_gen = GenExpr(aggr, GEN_VAL_PTR);
if ( op->Tag() == EXPR_INDEX ) {
auto indices = op->GetOp2();
Emit("remove_element__CPP(%s, index_val__CPP({%s}));", aggr_gen, GenExpr(indices, GEN_VAL_PTR));
}
else {
ASSERT(op->Tag() == EXPR_FIELD);
auto field = GenField(aggr, op->AsFieldExpr()->Field());
Emit("%s->Remove(%s);", aggr_gen, field);
}
}
void CPPCompile::GenEventStmt(const EventStmt* ev) {
auto ev_s = ev->StmtExprPtr();
auto ev_e = cast_intrusive<EventExpr>(ev_s);