mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
use dynamic rather than static initialization of globals for scripts compiled to C++
This commit is contained in:
parent
6ad28b37e6
commit
0687959f1c
5 changed files with 59 additions and 14 deletions
|
@ -1015,6 +1015,10 @@ private:
|
|||
// Generate code to initialize indirect references to constants.
|
||||
void InitializeConsts();
|
||||
|
||||
// Generate code to initialize globals (using dynamic statements
|
||||
// rather than constants).
|
||||
void InitializeGlobals();
|
||||
|
||||
// Generate the initialization hook for this set of compiled code.
|
||||
void GenInitHook();
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <cerrno>
|
||||
|
||||
#include "zeek/script_opt/CPP/Compile.h"
|
||||
#include "zeek/script_opt/IDOptInfo.h"
|
||||
|
||||
extern std::unordered_set<std::string> files_with_conditionals;
|
||||
|
||||
|
@ -310,6 +311,9 @@ void CPPCompile::RegisterCompiledBody(const string& f)
|
|||
|
||||
void CPPCompile::GenEpilog()
|
||||
{
|
||||
NL();
|
||||
InitializeGlobals();
|
||||
|
||||
NL();
|
||||
for ( const auto& ii : init_infos )
|
||||
GenInitExpr(ii.second);
|
||||
|
@ -468,6 +472,9 @@ void CPPCompile::GenFinishInit()
|
|||
NL();
|
||||
Emit("load_BiFs__CPP();");
|
||||
|
||||
NL();
|
||||
Emit("init_globals__CPP();");
|
||||
|
||||
EndBlock();
|
||||
}
|
||||
|
||||
|
|
|
@ -188,6 +188,50 @@ void CPPCompile::InitializeConsts()
|
|||
EndBlock(true);
|
||||
}
|
||||
|
||||
void CPPCompile::InitializeGlobals()
|
||||
{
|
||||
Emit("static void init_globals__CPP()");
|
||||
StartBlock();
|
||||
|
||||
Emit("Frame* f__CPP = nullptr;");
|
||||
NL();
|
||||
|
||||
for ( auto ginit : IDOptInfo::GetGlobalInitExprs() )
|
||||
{
|
||||
auto g = ginit.Id();
|
||||
if ( pfs.Globals().count(g) == 0 )
|
||||
continue;
|
||||
|
||||
auto ic = ginit.IC();
|
||||
auto& init = ginit.Init();
|
||||
|
||||
if ( ic == INIT_NONE )
|
||||
{
|
||||
IDPtr gid = {NewRef{}, const_cast<ID*>(g)};
|
||||
auto gn = make_intrusive<RefExpr>(make_intrusive<NameExpr>(gid));
|
||||
auto ae = make_intrusive<AssignExpr>(gn, init, true);
|
||||
Emit(GenExpr(ae.get(), GEN_NATIVE, true) + ";");
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
// This branch occurs for += or -= initializations that
|
||||
// use associated functions.
|
||||
string ics;
|
||||
if ( ic == INIT_EXTRA )
|
||||
ics = "INIT_EXTRA";
|
||||
else if ( ic == INIT_REMOVE )
|
||||
ics = "INIT_REMOVE";
|
||||
else
|
||||
reporter->FatalError("bad initialization class in CPPCompile::InitializeGlobals()");
|
||||
|
||||
Emit("%s->SetValue(%s, %s);", globals[g->Name()], GenExpr(init, GEN_NATIVE, true), ics);
|
||||
}
|
||||
}
|
||||
|
||||
EndBlock();
|
||||
}
|
||||
|
||||
void CPPCompile::GenInitHook()
|
||||
{
|
||||
NL();
|
||||
|
|
|
@ -339,16 +339,7 @@ GlobalInitInfo::GlobalInitInfo(CPPCompile* c, const ID* g, string _CPP_name)
|
|||
attrs = -1;
|
||||
|
||||
exported = g->IsExport();
|
||||
|
||||
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);
|
||||
val = ValElem(c, nullptr); // empty because we initialize dynamically
|
||||
}
|
||||
|
||||
void GlobalInitInfo::InitializerVals(std::vector<std::string>& ivs) const
|
||||
|
|
|
@ -485,11 +485,10 @@ void CPP_GlobalInit::Generate(InitsManager* im, std::vector<void*>& /* inits_vec
|
|||
global = lookup_global__CPP(name, im->Types(type), exported);
|
||||
|
||||
if ( ! global->HasVal() && val >= 0 )
|
||||
{
|
||||
global->SetVal(im->ConstVals(val));
|
||||
if ( attrs >= 0 )
|
||||
global->SetAttrs(im->Attributes(attrs));
|
||||
}
|
||||
|
||||
if ( attrs >= 0 )
|
||||
global->SetAttrs(im->Attributes(attrs));
|
||||
}
|
||||
|
||||
void generate_indices_set(int* inits, std::vector<std::vector<int>>& indices_set)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue