From 9d884c02856f2f0f3227020d639d8ac7a728535f Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Fri, 10 Dec 2021 09:27:07 -0800 Subject: [PATCH 1/5] remove unnecessary includes --- src/script_opt/CPP/DeclFunc.cc | 4 ---- src/script_opt/CPP/Emit.cc | 4 ---- src/script_opt/CPP/Exprs.cc | 4 ---- src/script_opt/CPP/GenFunc.cc | 4 ---- src/script_opt/CPP/Inits.cc | 4 ---- src/script_opt/CPP/Vars.cc | 4 ---- 6 files changed, 24 deletions(-) diff --git a/src/script_opt/CPP/DeclFunc.cc b/src/script_opt/CPP/DeclFunc.cc index 0d9117d2d8..c96530a16f 100644 --- a/src/script_opt/CPP/DeclFunc.cc +++ b/src/script_opt/CPP/DeclFunc.cc @@ -1,9 +1,5 @@ // See the file "COPYING" in the main distribution directory for copyright. -#include -#include -#include - #include "zeek/script_opt/CPP/Compile.h" namespace zeek::detail diff --git a/src/script_opt/CPP/Emit.cc b/src/script_opt/CPP/Emit.cc index 84e122f9c0..0357815e54 100644 --- a/src/script_opt/CPP/Emit.cc +++ b/src/script_opt/CPP/Emit.cc @@ -1,9 +1,5 @@ // See the file "COPYING" in the main distribution directory for copyright. -#include -#include -#include - #include "zeek/script_opt/CPP/Compile.h" namespace zeek::detail diff --git a/src/script_opt/CPP/Exprs.cc b/src/script_opt/CPP/Exprs.cc index dd3baa995c..fcb0d1bd74 100644 --- a/src/script_opt/CPP/Exprs.cc +++ b/src/script_opt/CPP/Exprs.cc @@ -1,9 +1,5 @@ // See the file "COPYING" in the main distribution directory for copyright. -#include -#include -#include - #include "zeek/RE.h" #include "zeek/script_opt/CPP/Compile.h" #include "zeek/script_opt/ProfileFunc.h" diff --git a/src/script_opt/CPP/GenFunc.cc b/src/script_opt/CPP/GenFunc.cc index 47a9ec33bc..966e4212b2 100644 --- a/src/script_opt/CPP/GenFunc.cc +++ b/src/script_opt/CPP/GenFunc.cc @@ -1,9 +1,5 @@ // See the file "COPYING" in the main distribution directory for copyright. -#include -#include -#include - #include "zeek/script_opt/CPP/Compile.h" namespace zeek::detail diff --git a/src/script_opt/CPP/Inits.cc b/src/script_opt/CPP/Inits.cc index d462f19a82..f1d7a52338 100644 --- a/src/script_opt/CPP/Inits.cc +++ b/src/script_opt/CPP/Inits.cc @@ -1,9 +1,5 @@ // See the file "COPYING" in the main distribution directory for copyright. -#include -#include -#include - #include "zeek/module_util.h" #include "zeek/script_opt/CPP/Compile.h" #include "zeek/script_opt/IDOptInfo.h" diff --git a/src/script_opt/CPP/Vars.cc b/src/script_opt/CPP/Vars.cc index c62aae0869..232fb7e4b2 100644 --- a/src/script_opt/CPP/Vars.cc +++ b/src/script_opt/CPP/Vars.cc @@ -1,9 +1,5 @@ // See the file "COPYING" in the main distribution directory for copyright. -#include -#include -#include - #include "zeek/script_opt/CPP/Compile.h" #include "zeek/script_opt/ProfileFunc.h" From 474a2edc98c4c1e3b30863c4936e660b9a7d0ff9 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Fri, 10 Dec 2021 09:27:21 -0800 Subject: [PATCH 2/5] fix for translating filenames beginning with numbers to C++ variable names --- src/script_opt/CPP/GenFunc.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/script_opt/CPP/GenFunc.cc b/src/script_opt/CPP/GenFunc.cc index 966e4212b2..673e6def4a 100644 --- a/src/script_opt/CPP/GenFunc.cc +++ b/src/script_opt/CPP/GenFunc.cc @@ -206,6 +206,10 @@ string CPPCompile::BodyName(const FuncInfo& func) string fns = fn; transform(fns.begin(), fns.end(), fns.begin(), canonicalize); + if ( ! isalpha(fns[0]) ) + // This can happen for filenames beginning with numbers. + fns = "_" + fns; + fname = fns + "__" + fname; } From 53342fdd29e45f614af432551764745f97a7f878 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Fri, 10 Dec 2021 09:27:58 -0800 Subject: [PATCH 3/5] skip type signatures for lambdas --- src/script_opt/CPP/DeclFunc.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/script_opt/CPP/DeclFunc.cc b/src/script_opt/CPP/DeclFunc.cc index c96530a16f..cdc95b5f2f 100644 --- a/src/script_opt/CPP/DeclFunc.cc +++ b/src/script_opt/CPP/DeclFunc.cc @@ -69,7 +69,7 @@ void CPPCompile::CreateFunction(const FuncTypePtr& ft, const ProfileFunc* pf, co func_index[fname] = cast; - if ( casting_index.count(cast) == 0 ) + if ( ! l && casting_index.count(cast) == 0 ) { casting_index[cast] = func_casting_glue.size(); From 6788be94c984579750cced4b92dbed22128ac555 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Fri, 10 Dec 2021 09:28:25 -0800 Subject: [PATCH 4/5] flag globals initialized to opaque values as non-compilable --- src/script_opt/CPP/InitsInfo.cc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/script_opt/CPP/InitsInfo.cc b/src/script_opt/CPP/InitsInfo.cc index c83b78f5ed..2f2ab1ed8c 100644 --- a/src/script_opt/CPP/InitsInfo.cc +++ b/src/script_opt/CPP/InitsInfo.cc @@ -323,7 +323,8 @@ GlobalInitInfo::GlobalInitInfo(CPPCompile* c, const ID* g, string _CPP_name) { Zeek_name = g->Name(); - auto gi = c->RegisterType(g->GetType()); + auto& gt = g->GetType(); + auto gi = c->RegisterType(gt); init_cohort = max(init_cohort, gi->InitCohort() + 1); type = gi->Offset(); @@ -338,7 +339,15 @@ GlobalInitInfo::GlobalInitInfo(CPPCompile* c, const ID* g, string _CPP_name) exported = g->IsExport(); - val = ValElem(c, g->GetVal()); + auto v = g->GetVal(); + if ( v && gt->Tag() == TYPE_OPAQUE ) + { + reporter->Error("cannot compile to C++ global \"%s\" initialized to opaque value", + g->Name()); + v = nullptr; + } + + val = ValElem(c, v); } void GlobalInitInfo::InitializerVals(std::vector& ivs) const From ac74943f2687a2c4ad7f0bf1250598b85330f60f Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Fri, 10 Dec 2021 09:32:42 -0800 Subject: [PATCH 5/5] fixes for vector operations --- src/script_opt/CPP/Exprs.cc | 18 +++++++++++++++--- src/script_opt/CPP/RuntimeVec.h | 5 +++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/script_opt/CPP/Exprs.cc b/src/script_opt/CPP/Exprs.cc index fcb0d1bd74..ae7e7a6489 100644 --- a/src/script_opt/CPP/Exprs.cc +++ b/src/script_opt/CPP/Exprs.cc @@ -603,7 +603,7 @@ string CPPCompile::GenArithCoerceExpr(const Expr* e, GenType gt) } if ( is_vec ) - return string("vec_coerce_") + cast_name + "__CPP(" + GenExpr(op, GEN_NATIVE) + ", " + + return string("vec_coerce_to_") + cast_name + "__CPP(" + GenExpr(op, GEN_NATIVE) + ", " + GenTypeName(t) + ")"; return NativeToGT(cast_name + "(" + GenExpr(op, GEN_NATIVE) + ")", t, gt); @@ -784,7 +784,7 @@ string CPPCompile::GenBinary(const Expr* e, GenType gt, const char* op, const ch if ( t->Tag() == TYPE_VECTOR && t->Yield()->Tag() == TYPE_STRING && op2->GetType()->Tag() == TYPE_VECTOR ) - return string("vec_str_op_") + vec_op + "__CPP(" + gen1 + ", " + gen2 + ")"; + return string("str_vec_op_") + vec_op + "__CPP(" + gen1 + ", " + gen2 + ")"; return GenVectorOp(e, gen1, gen2, vec_op); } @@ -1103,9 +1103,21 @@ string CPPCompile::GenVectorOp(const Expr* e, string op, const char* vec_op) string CPPCompile::GenVectorOp(const Expr* e, string op1, string op2, const char* vec_op) { + auto& op1_t = e->GetOp1()->GetType(); + auto& op2_t = e->GetOp2()->GetType(); + + if ( op1_t->Tag() != TYPE_VECTOR || op2_t->Tag() != TYPE_VECTOR ) + { + // This is a deprecated mixed-scalar-and-vector operation. + // We don't support these. Arrange for linking errors. + reporter->Error( + "C++ generation does not support deprecated scalar-mixed-with-vector operations"); + return "vec_scalar_mixed_with_vector()"; + } + auto invoke = string(vec_op) + "__CPP(" + op1 + ", " + op2 + ")"; - if ( e->GetOp1()->GetType()->Yield()->Tag() == TYPE_STRING ) + if ( op2_t->Yield()->Tag() == TYPE_STRING ) return string("str_vec_op_") + invoke; auto gen = string("vec_op_") + invoke; diff --git a/src/script_opt/CPP/RuntimeVec.h b/src/script_opt/CPP/RuntimeVec.h index 918bee2c49..96cc1cfc67 100644 --- a/src/script_opt/CPP/RuntimeVec.h +++ b/src/script_opt/CPP/RuntimeVec.h @@ -80,4 +80,9 @@ extern VectorValPtr vec_coerce_to_bro_int_t__CPP(const VectorValPtr& v, TypePtr extern VectorValPtr vec_coerce_to_bro_uint_t__CPP(const VectorValPtr& v, TypePtr targ); extern VectorValPtr vec_coerce_to_double__CPP(const VectorValPtr& v, TypePtr targ); +// A dummy function used during code generation for unsupported operations +// that mix vector and scalar arguments. We don't define it in RuntimeVec.cc +// so that it'll generate a linking error. +extern VectorValPtr vec_scalar_mixed_with_vector(); + } // namespace zeek::detail