From a725cfa9e3a2c7248d0dcb5eddb1e32da8bde0c9 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Fri, 16 Sep 2022 16:45:09 -0700 Subject: [PATCH] C++ compilation support for 2-valued vector "for" loops --- src/script_opt/CPP/Compile.h | 2 +- src/script_opt/CPP/Stmts.cc | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/script_opt/CPP/Compile.h b/src/script_opt/CPP/Compile.h index 2862bb0eff..bb75a5fba3 100644 --- a/src/script_opt/CPP/Compile.h +++ b/src/script_opt/CPP/Compile.h @@ -727,7 +727,7 @@ private: void GenWhenStmt(const WhenStmt* w); void GenForStmt(const ForStmt* f); void GenForOverTable(const ExprPtr& tbl, const IDPtr& value_var, const IDPList* loop_vars); - void GenForOverVector(const ExprPtr& tbl, const IDPList* loop_vars); + void GenForOverVector(const ExprPtr& tbl, const IDPtr& value_var, const IDPList* loop_vars); void GenForOverString(const ExprPtr& str, const IDPList* loop_vars); // Nested level of loops/switches for which "break"'s should be diff --git a/src/script_opt/CPP/Stmts.cc b/src/script_opt/CPP/Stmts.cc index edef5b0b3a..840b6c79f8 100644 --- a/src/script_opt/CPP/Stmts.cc +++ b/src/script_opt/CPP/Stmts.cc @@ -458,12 +458,13 @@ void CPPCompile::GenForStmt(const ForStmt* f) auto v = f->StmtExprPtr(); auto t = v->GetType()->Tag(); auto loop_vars = f->LoopVars(); + auto value_var = f->ValueVar(); if ( t == TYPE_TABLE ) - GenForOverTable(v, f->ValueVar(), loop_vars); + GenForOverTable(v, value_var, loop_vars); else if ( t == TYPE_VECTOR ) - GenForOverVector(v, loop_vars); + GenForOverVector(v, value_var, loop_vars); else if ( t == TYPE_STRING ) GenForOverString(v, loop_vars); @@ -515,7 +516,8 @@ void CPPCompile::GenForOverTable(const ExprPtr& tbl, const IDPtr& value_var, } } -void CPPCompile::GenForOverVector(const ExprPtr& vec, const IDPList* loop_vars) +void CPPCompile::GenForOverVector(const ExprPtr& vec, const IDPtr& value_var, + const IDPList* loop_vars) { Emit("auto vv__CPP = %s;", GenExpr(vec, GEN_DONT_CARE)); @@ -523,7 +525,16 @@ void CPPCompile::GenForOverVector(const ExprPtr& vec, const IDPList* loop_vars) StartBlock(); Emit("if ( ! vv__CPP->Has(i__CPP) ) continue;"); + Emit("%s = i__CPP;", IDName((*loop_vars)[0])); + + if ( value_var ) + { + auto vv = IDName(value_var); + auto access = "vv__CPP->ValAt(i__CPP)"; + auto native = GenericValPtrToGT(access, value_var->GetType(), GEN_NATIVE); + Emit("%s = %s;", IDName(value_var), native); + } } void CPPCompile::GenForOverString(const ExprPtr& str, const IDPList* loop_vars)