extensive rewrite of generation & execution of run-time initialization

This commit is contained in:
Vern Paxson 2021-11-07 17:00:19 -08:00
parent bc3bf4ea6c
commit e1a760e674
26 changed files with 3459 additions and 1580 deletions

View file

@ -83,7 +83,7 @@ void CPPCompile::CreateGlobal(const ID* g)
if ( pfs.Globals().count(g) == 0 )
{
// Only used in the context of calls. If it's compilable,
// the we'll call it directly.
// then we'll call it directly.
if ( compilable_funcs.count(gn) > 0 )
{
AddGlobal(gn, "zf", true);
@ -102,18 +102,12 @@ void CPPCompile::CreateGlobal(const ID* g)
Emit("IDPtr %s;", globals[gn]);
if ( pfs.Events().count(gn) > 0 )
// This is an event that's also used as
// a variable.
// This is an event that's also used as a variable.
Emit("EventHandlerPtr %s_ev;", globals[gn]);
const auto& t = g->GetType();
NoteInitDependency(g, TypeRep(t));
auto exported = g->IsExport() ? "true" : "false";
AddInit(g, globals[gn],
string("lookup_global__CPP(\"") + gn + "\", " + GenTypeName(t) + ", " + exported +
")");
auto gi = make_shared<GlobalInitInfo>(this, g, globals[gn]);
global_id_info->AddInstance(gi);
global_gis[g] = gi;
}
if ( is_bif )
@ -124,40 +118,22 @@ void CPPCompile::CreateGlobal(const ID* g)
global_vars.emplace(g);
}
void CPPCompile::UpdateGlobalHashes()
std::shared_ptr<CPP_InitInfo> CPPCompile::RegisterGlobal(const ID* g)
{
for ( auto& g : pfs.AllGlobals() )
if ( global_gis.count(g) == 0 )
{
auto gn = g->Name();
auto gn = string(g->Name());
if ( hm.HasGlobal(gn) )
// Not new to this compilation run.
continue;
if ( globals.count(gn) == 0 )
// Create a name for it.
(void)IDNameStr(g);
auto ht = pfs.HashType(g->GetType());
p_hash_type hv = 0;
if ( g->GetVal() )
hv = p_hash(g->GetVal());
fprintf(hm.HashFile(), "global\n%s\n", gn);
fprintf(hm.HashFile(), "%llu %llu\n", ht, hv);
// Record location information in the hash file for
// diagnostic purposes.
auto loc = g->GetLocationInfo();
fprintf(hm.HashFile(), "%s %d\n", loc->filename, loc->first_line);
// Flag any named record/enum types.
if ( g->IsType() )
{
const auto& t = g->GetType();
if ( t->Tag() == TYPE_RECORD )
fprintf(hm.HashFile(), "record\n%s\n", gn);
else if ( t->Tag() == TYPE_ENUM )
fprintf(hm.HashFile(), "enum\n%s\n", gn);
}
auto gi = make_shared<GlobalInitInfo>(this, g, globals[gn]);
global_id_info->AddInstance(gi);
global_gis[g] = gi;
}
return global_gis[g];
}
void CPPCompile::AddBiF(const ID* b, bool is_var)
@ -170,12 +146,8 @@ void CPPCompile::AddBiF(const ID* b, bool is_var)
if ( AddGlobal(n, "bif", true) )
Emit("Func* %s;", globals[n]);
auto lookup = string("lookup_bif__CPP(\"") + bn + "\")";
if ( standalone )
AddActivation(globals[n] + " = " + lookup + ";");
else
AddInit(b, globals[n], lookup);
ASSERT(BiFs.count(globals[n]) == 0);
BiFs[globals[n]] = bn;
}
bool CPPCompile::AddGlobal(const string& g, const char* suffix, bool track)
@ -189,13 +161,8 @@ bool CPPCompile::AddGlobal(const string& g, const char* suffix, bool track)
if ( hm.HasGlobalVar(gn) )
gn = scope_prefix(hm.GlobalVarScope(gn)) + gn;
else
{
new_var = true;
if ( track && update )
fprintf(hm.HashFile(), "global-var\n%s\n%d\n", gn.c_str(), addl_tag);
}
globals.emplace(g, gn);
}
@ -207,18 +174,19 @@ void CPPCompile::RegisterEvent(string ev_name)
body_events[body_name].emplace_back(move(ev_name));
}
const string& CPPCompile::IDNameStr(const ID* id) const
const string& CPPCompile::IDNameStr(const ID* id)
{
if ( id->IsGlobal() )
{
auto g = string(id->Name());
ASSERT(globals.count(g) > 0);
return ((CPPCompile*)(this))->globals[g];
if ( globals.count(g) == 0 )
CreateGlobal(id);
return globals[g];
}
ASSERT(locals.count(id) > 0);
return ((CPPCompile*)(this))->locals[id];
return locals[id];
}
string CPPCompile::LocalName(const ID* l) const