diff --git a/src/script_opt/CPP/InitsInfo.cc b/src/script_opt/CPP/InitsInfo.cc index b4c03bf541..24eb5986fd 100644 --- a/src/script_opt/CPP/InitsInfo.cc +++ b/src/script_opt/CPP/InitsInfo.cc @@ -38,6 +38,13 @@ void CPP_InitsInfo::GenerateInitializers(CPPCompile* c) { c->Emit("%s %s = %s(%s, %s,", gt, InitializersName(), gt, base_name, Fmt(offset_set)); c->IndentUp(); + GenerateCohorts(c); + c->IndentDown(); + + c->Emit(");"); +} + +void CPP_InitsInfo::GenerateCohorts(CPPCompile* c) { c->Emit("{"); int n = 0; @@ -47,7 +54,7 @@ void CPP_InitsInfo::GenerateInitializers(CPPCompile* c) { if ( ++n > 1 ) c->Emit(""); - if ( cohort.size() == 1 && ! IsCompound() ) + if ( cohort.size() == 1 && ! UsesCompoundVectors() ) BuildCohort(c, cohort); else { c->Emit("{"); @@ -57,8 +64,6 @@ void CPP_InitsInfo::GenerateInitializers(CPPCompile* c) { } c->Emit("}"); - c->IndentDown(); - c->Emit(");"); } void CPP_InitsInfo::BuildOffsetSet(CPPCompile* c) { @@ -117,12 +122,36 @@ void CPP_InitsInfo::BuildCohortElement(CPPCompile* c, string init_type, vectorEmit("std::make_shared<%s>(%s),", init_type, full_init); } +void CPP_CompoundInitsInfo::GenerateInitializers(CPPCompile* c) { + c->Emit("static int %s_init[] = {", tag); + int n = 0; + + c->IndentUp(); + + for ( auto& cohort : instances ) { + if ( ++n > 1 ) + c->Emit(""); + + c->Emit(Fmt(int(cohort.size())) + ","); + BuildCohort(c, cohort); + c->Emit("-1,"); + } + + c->Emit("-2,"); + c->IndentDown(); + c->Emit("};"); + + CPP_InitsInfo::GenerateInitializers(c); +} + +void CPP_CompoundInitsInfo::GenerateCohorts(CPPCompile* c) { c->Emit("%s_init", tag); } + void CPP_CompoundInitsInfo::BuildCohortElement(CPPCompile* c, string init_type, vector& ivs) { string init_line; for ( auto& iv : ivs ) - init_line += iv + ", "; + init_line += iv; - c->Emit("{ %s},", init_line); + c->Emit("%s,", init_line); } void CPP_BasicConstInitsInfo::BuildCohortElement(CPPCompile* c, string init_type, vector& ivs) { diff --git a/src/script_opt/CPP/InitsInfo.h b/src/script_opt/CPP/InitsInfo.h index c2bba9fbd6..d2b097a304 100644 --- a/src/script_opt/CPP/InitsInfo.h +++ b/src/script_opt/CPP/InitsInfo.h @@ -133,10 +133,10 @@ public: // Sets the associated C++ type. virtual void SetCPPType(std::string ct) { CPP_type = std::move(ct); } - // Whether this initializer is in terms of compound objects. Used + // Whether this initializer is in terms of compound vectors. Used // for avoiding compiler warnings about singleton initializations in // braces. - virtual bool IsCompound() const { return false; } + virtual bool UsesCompoundVectors() const { return false; } // Returns the type associated with the table used for initialization // (i.e., this is the type of the global returned by InitializersName()). @@ -146,9 +146,11 @@ public: void AddInstance(std::shared_ptr g); // Emit code to populate the table used to initialize this collection. - void GenerateInitializers(CPPCompile* c); + virtual void GenerateInitializers(CPPCompile* c); protected: + virtual void GenerateCohorts(CPPCompile* c); + // Computes offset_set - see below. void BuildOffsetSet(CPPCompile* c); @@ -214,7 +216,7 @@ public: BuildInitType(); } - bool IsCompound() const override { return true; } + bool UsesCompoundVectors() const override { return true; } private: void BuildInitType() { inits_type = std::string("CPP_CustomInits<") + CPPType() + ">"; } @@ -236,7 +238,7 @@ public: inits_type = std::string("CPP_BasicConsts<") + CPP_type + ", " + c_type + ", " + tag + "Val>"; } - bool IsCompound() const override { return false; } + bool UsesCompoundVectors() const override { return false; } void BuildCohortElement(CPPCompile* c, std::string init_type, std::vector& ivs) override; }; @@ -254,7 +256,12 @@ public: inits_type = std::string("CPP_IndexedInits<") + CPPType() + ">"; } - bool IsCompound() const override { return true; } + // This isn't true (anymore) because we separately build up the compound + // vectors needed for the initialization. + bool UsesCompoundVectors() const override { return false; } + + void GenerateInitializers(CPPCompile* c) override; + void GenerateCohorts(CPPCompile* c) override; void BuildCohortElement(CPPCompile* c, std::string init_type, std::vector& ivs) override; }; diff --git a/src/script_opt/CPP/RuntimeInits.cc b/src/script_opt/CPP/RuntimeInits.cc index 80c2337f63..93105eccb7 100644 --- a/src/script_opt/CPP/RuntimeInits.cc +++ b/src/script_opt/CPP/RuntimeInits.cc @@ -465,7 +465,7 @@ void CPP_GlobalInit::Generate(InitsManager* im, std::vector& /* inits_vec global->SetAttrs(im->Attributes(attrs)); } -void generate_indices_set(int* inits, std::vector>& indices_set) { +size_t generate_indices_set(int* inits, std::vector>& indices_set) { // First figure out how many groups of indices there are, so we // can pre-allocate the outer vector. auto i_ptr = inits; @@ -490,6 +490,26 @@ void generate_indices_set(int* inits, std::vector>& indices_set indices_set.emplace_back(std::move(indices)); } + + return i_ptr - inits + 1; +} + +std::vector>> generate_indices_set(int* inits) { + // Figure out how many vector-of-vectors there are. + int num_vv = 0; + for ( int i = 0; inits[i] >= -1; ++i ) + if ( inits[i] == -1 ) + ++num_vv; + + std::vector>> indices_set(num_vv); + + auto i_ptr = inits; + for ( int i = 0; i < num_vv; ++i ) + i_ptr += generate_indices_set(i_ptr, indices_set[i]); + + ASSERT(*i_ptr == -2); + + return indices_set; } } // namespace zeek::detail diff --git a/src/script_opt/CPP/RuntimeInits.h b/src/script_opt/CPP/RuntimeInits.h index e8b35dd4f8..8e153e1472 100644 --- a/src/script_opt/CPP/RuntimeInits.h +++ b/src/script_opt/CPP/RuntimeInits.h @@ -19,6 +19,23 @@ using FuncValPtr = IntrusivePtr; class InitsManager; +// Helper function that takes a (large) array of int's and from them +// constructs the corresponding vector-of-vector-of-indices. Each +// vector-of-indices is represented first by an int specifying its +// size, and then that many int's for its values. We recognize the +// end of the array upon encountering a "size" entry of -1. +// +// Returns how many elements were processed out of "inits", including its +// terminator. +extern size_t generate_indices_set(int* inits, std::vector>& indices_set); + +// The same but for one more level of vector construction. The source array +// has sub-arrays terminated with -1 per the above, and the whole shebang +// is terminated with -2. +// +// Returns the vector construction. +extern std::vector>> generate_indices_set(int* inits); + // An abstract helper class used to access elements of an initialization vector. // We need the abstraction because InitsManager below needs to be able to refer // to any of a range of templated classes. @@ -223,16 +240,16 @@ using ValElemVecVec = std::vector; template class CPP_IndexedInits : public CPP_AbstractInits { public: - CPP_IndexedInits(std::vector& _inits_vec, int _offsets_set, std::vector _inits) - : CPP_AbstractInits(_inits_vec, _offsets_set, std::move(_inits)) {} + CPP_IndexedInits(std::vector& _inits_vec, int _offsets_set, int* raw_inits) + : CPP_AbstractInits(_inits_vec, _offsets_set, generate_indices_set(raw_inits)) {} protected: void InitializeCohortWithOffsets(InitsManager* im, int cohort, const std::vector& cohort_offsets) override; - // Note, in the following we pass in the inits_vec, even though - // the method will have direct access to it, because we want to - // use overloading to dispatch to custom generation for different - // types of values. + // Note, in the following we pass in the inits_vec ("ivec", even though + // the method will have direct access to it, because we want to use + // overloading to dispatch to custom generation for different types of + // values. void Generate(InitsManager* im, std::vector& ivec, int offset, ValElemVec& init_vals); void Generate(InitsManager* im, std::vector& ivec, int offset, ValElemVec& init_vals); void Generate(InitsManager* im, std::vector& ivec, int offset, ValElemVec& init_vals); @@ -256,8 +273,8 @@ protected: // on subclasses of TypePtr. class CPP_TypeInits : public CPP_IndexedInits { public: - CPP_TypeInits(std::vector& _inits_vec, int _offsets_set, std::vector _inits) - : CPP_IndexedInits(_inits_vec, _offsets_set, _inits) {} + CPP_TypeInits(std::vector& _inits_vec, int _offsets_set, int* raw_inits) + : CPP_IndexedInits(_inits_vec, _offsets_set, raw_inits) {} protected: void DoPreInits(InitsManager* im, const std::vector& offsets_vec) override; @@ -506,11 +523,4 @@ struct CPP_RegisterBody { std::vector events; }; -// Helper function that takes a (large) array of int's and from them -// constructs the corresponding vector-of-vector-of-indices. Each -// vector-of-indices is represented first by an int specifying its -// size, and then that many int's for its values. We recognize the -// end of the array upon encountering a "size" entry of -1. -extern void generate_indices_set(int* inits, std::vector>& indices_set); - } // namespace zeek::detail