Merge remote-tracking branch 'origin/topic/vern/cpp-add-option2'

* origin/topic/vern/cpp-add-option2:
  reintroduction of "-O add-C++" option
This commit is contained in:
Tim Wojtulewicz 2021-12-10 13:13:13 -07:00
commit aa91f72b34
14 changed files with 79 additions and 489 deletions

View file

@ -8,69 +8,6 @@ namespace zeek::detail
using namespace std;
bool CPPCompile::CheckForCollisions()
{
for ( auto& g : pfs.AllGlobals() )
{
auto gn = string(g->Name());
if ( hm.HasGlobal(gn) )
{
// Make sure the previous compilation used the
// same type and initialization value for the global.
auto ht_orig = hm.GlobalTypeHash(gn);
auto hv_orig = hm.GlobalValHash(gn);
auto ht = pfs.HashType(g->GetType());
p_hash_type hv = 0;
if ( g->GetVal() )
hv = p_hash(g->GetVal());
if ( ht != ht_orig || hv != hv_orig )
{
fprintf(stderr, "%s: hash clash for global %s (%llu/%llu vs. %llu/%llu)\n",
working_dir.c_str(), gn.c_str(), ht, hv, ht_orig, hv_orig);
fprintf(stderr, "val: %s\n",
g->GetVal() ? obj_desc(g->GetVal().get()).c_str() : "<none>");
return true;
}
}
}
for ( auto& t : pfs.RepTypes() )
{
auto tag = t->Tag();
if ( tag != TYPE_ENUM && tag != TYPE_RECORD )
// Other types, if inconsistent, will just not reuse
// the previously compiled version of the type.
continue;
// We identify enum's and record's by name. Make sure that
// the name either (1) wasn't previously used, or (2) if it
// was, it was likewise for an enum or a record.
const auto& tn = t->GetName();
if ( tn.empty() || ! hm.HasGlobal(tn) )
// No concern of collision since the type name
// wasn't previously compiled.
continue;
if ( tag == TYPE_ENUM && hm.HasEnumTypeGlobal(tn) )
// No inconsistency.
continue;
if ( tag == TYPE_RECORD && hm.HasRecordTypeGlobal(tn) )
// No inconsistency.
continue;
fprintf(stderr, "%s: type \"%s\" collides with compiled global\n", working_dir.c_str(),
tn.c_str());
return true;
}
return false;
}
void CPPCompile::CreateGlobal(const ID* g)
{
auto gn = string(g->Name());
@ -82,7 +19,7 @@ void CPPCompile::CreateGlobal(const ID* g)
// then we'll call it directly.
if ( compilable_funcs.count(gn) > 0 )
{
AddGlobal(gn, "zf", true);
AddGlobal(gn, "zf");
return;
}
@ -93,7 +30,7 @@ void CPPCompile::CreateGlobal(const ID* g)
}
}
if ( AddGlobal(gn, "gl", true) )
if ( AddGlobal(gn, "gl") )
{ // We'll be creating this global.
Emit("IDPtr %s;", globals[gn]);
@ -142,30 +79,20 @@ void CPPCompile::AddBiF(const ID* b, bool is_var)
if ( is_var )
n = n + "_"; // make the name distinct
if ( AddGlobal(n, "bif", true) )
if ( AddGlobal(n, "bif") )
Emit("Func* %s;", globals[n]);
ASSERT(BiFs.count(globals[n]) == 0);
BiFs[globals[n]] = bn;
}
bool CPPCompile::AddGlobal(const string& g, const char* suffix, bool track)
bool CPPCompile::AddGlobal(const string& g, const char* suffix)
{
bool new_var = false;
if ( globals.count(g) > 0 )
return false;
if ( globals.count(g) == 0 )
{
auto gn = GlobalName(g, suffix);
if ( hm.HasGlobalVar(gn) )
gn = scope_prefix(hm.GlobalVarScope(gn)) + gn;
else
new_var = true;
globals.emplace(g, gn);
}
return new_var;
globals.emplace(g, GlobalName(g, suffix));
return true;
}
void CPPCompile::RegisterEvent(string ev_name)