script optimization support for clearing tables/vectors using "delete"

This commit is contained in:
Vern Paxson 2024-03-02 14:21:22 -08:00 committed by Arne Welzel
parent ce6d77e2ce
commit 74d36eb759
6 changed files with 37 additions and 8 deletions

View file

@ -159,6 +159,15 @@ void CPPCompile::GenAddStmt(const ExprStmt* es) {
void CPPCompile::GenDeleteStmt(const ExprStmt* es) { void CPPCompile::GenDeleteStmt(const ExprStmt* es) {
auto op = es->StmtExpr(); 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 = op->GetOp1();
auto aggr_gen = GenExpr(aggr, GEN_VAL_PTR); auto aggr_gen = GenExpr(aggr, GEN_VAL_PTR);

View file

@ -161,8 +161,11 @@ TraversalCode ProfileFunc::PreStmt(const Stmt* s) {
case STMT_DELETE: { case STMT_DELETE: {
auto ad_stmt = static_cast<const AddDelStmt*>(s); auto ad_stmt = static_cast<const AddDelStmt*>(s);
auto ad_e = ad_stmt->StmtExpr(); auto ad_e = ad_stmt->StmtExpr();
auto& lhs_t = ad_e->GetOp1()->GetType(); auto lhs = ad_e->GetOp1();
aggr_mods.insert(lhs_t.get()); if ( lhs )
aggr_mods.insert(lhs->GetType().get());
else
aggr_mods.insert(ad_e->GetType().get());
} break; } break;
default: break; default: break;

View file

@ -255,7 +255,7 @@ StmtPtr IfStmt::DoReduce(Reducer* c) {
sl = make_intrusive<StmtList>(red_e_stmt, ThisPtr()); sl = make_intrusive<StmtList>(red_e_stmt, ThisPtr());
if ( sl ) if ( sl )
return TransformMe(sl, c); return TransformMe(std::move(sl), c);
return ThisPtr(); return ThisPtr();
} }
@ -418,14 +418,10 @@ StmtPtr AddDelStmt::DoReduce(Reducer* c) {
return ThisPtr(); return ThisPtr();
} }
if ( e->Tag() != EXPR_INDEX && e->Tag() != EXPR_FIELD )
Internal("bad \"add\"/\"delete\"");
auto red_e_stmt = e->ReduceToSingletons(c); auto red_e_stmt = e->ReduceToSingletons(c);
if ( red_e_stmt ) if ( red_e_stmt )
return TransformMe(make_intrusive<StmtList>(red_e_stmt, ThisPtr()), c); return TransformMe(make_intrusive<StmtList>(red_e_stmt, ThisPtr()), c);
else else
return ThisPtr(); return ThisPtr();
} }

View file

@ -874,7 +874,7 @@ const ZAMStmt ZAMCompiler::Call(const ExprStmt* e) {
if ( IsZAM_BuiltIn(c.get()) ) { if ( IsZAM_BuiltIn(c.get()) ) {
auto ret = LastInst(); auto ret = LastInst();
insts1.back()->call_expr = c; insts1.back()->call_expr = std::move(c);
return ret; return ret;
} }

View file

@ -2087,6 +2087,17 @@ type VC
eval EvalAddStmt(z.c.ToVal(z.t)) eval EvalAddStmt(z.c.ToVal(z.t))
op ClearTable
op1-read
type V
eval frame[z.v1].table_val->RemoveAll();
op ClearVector
op1-read
type V
eval frame[z.v1].vector_val->Resize(0);
op DelTable op DelTable
op1-read op1-read
type VO type VO

View file

@ -591,6 +591,16 @@ const ZAMStmt ZAMCompiler::CompileAdd(const AddStmt* as) {
const ZAMStmt ZAMCompiler::CompileDel(const DelStmt* ds) { const ZAMStmt ZAMCompiler::CompileDel(const DelStmt* ds) {
auto e = ds->StmtExprPtr(); auto e = ds->StmtExprPtr();
if ( e->Tag() == EXPR_NAME ) {
auto n = e->AsNameExpr();
if ( n->GetType()->Tag() == TYPE_TABLE )
return ClearTableV(n);
else
return ClearVectorV(n);
}
auto aggr = e->GetOp1()->AsNameExpr(); auto aggr = e->GetOp1()->AsNameExpr();
if ( e->Tag() == EXPR_FIELD ) { if ( e->Tag() == EXPR_FIELD ) {