-O gen-C++ maintenance: fixes for attributes & initializing globals

This commit is contained in:
Vern Paxson 2022-10-14 08:37:45 -05:00
parent 6fcbb55ccd
commit 26dae9a5ae
9 changed files with 54 additions and 12 deletions

View file

@ -48,6 +48,16 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterConstant(const ValPtr& vp, int& con
// render the same. // render the same.
t->Describe(&d); t->Describe(&d);
// Likewise, tables that have attributes.
if ( t->Tag() == TYPE_TABLE )
{
const auto& attrs = v->AsTableVal()->GetAttrs();
if ( attrs )
attrs->Describe(&d);
else
d.Add("<no-attrs>");
}
c_desc = d.Description(); c_desc = d.Description();
} }

View file

@ -310,9 +310,12 @@ void CPPCompile::RegisterCompiledBody(const string& f)
} }
void CPPCompile::GenEpilog() void CPPCompile::GenEpilog()
{
if ( standalone )
{ {
NL(); NL();
InitializeGlobals(); InitializeGlobals();
}
NL(); NL();
for ( const auto& ii : init_infos ) for ( const auto& ii : init_infos )
@ -472,8 +475,11 @@ void CPPCompile::GenFinishInit()
NL(); NL();
Emit("load_BiFs__CPP();"); Emit("load_BiFs__CPP();");
if ( standalone )
{
NL(); NL();
Emit("init_globals__CPP();"); Emit("init_globals__CPP();");
}
EndBlock(); EndBlock();
} }

View file

@ -206,12 +206,7 @@ void CPPCompile::InitializeGlobals()
auto& init = ginit.Init(); auto& init = ginit.Init();
if ( ic == INIT_NONE ) if ( ic == INIT_NONE )
{ Emit(GenExpr(init, GEN_NATIVE, true) + ";");
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 else
{ {
@ -227,6 +222,15 @@ void CPPCompile::InitializeGlobals()
Emit("%s->SetValue(%s, %s);", globals[g->Name()], GenExpr(init, GEN_NATIVE, true), ics); Emit("%s->SetValue(%s, %s);", globals[g->Name()], GenExpr(init, GEN_NATIVE, true), ics);
} }
const auto& attrs = g->GetAttrs();
if ( attrs )
{
string attr_tags;
string attr_vals;
BuildAttrs(attrs, attr_tags, attr_vals);
Emit("assign_attrs__CPP(%s, %s, %s);", globals[g->Name()], attr_tags, attr_vals);
}
} }
EndBlock(); EndBlock();

View file

@ -211,6 +211,16 @@ TableConstInfo::TableConstInfo(CPPCompile* c, ValPtr v) : CompoundItemInfo(c, v)
{ {
auto tv = cast_intrusive<TableVal>(v); auto tv = cast_intrusive<TableVal>(v);
auto gi = c->RegisterAttributes(tv->GetAttrs());
int attrs = -1;
if ( gi )
{
init_cohort = max(init_cohort, gi->InitCohort() + 1);
attrs = gi->Offset();
}
vals.emplace_back(std::to_string(attrs));
for ( auto& tv_i : tv->ToMap() ) for ( auto& tv_i : tv->ToMap() )
{ {
vals.emplace_back(ValElem(c, tv_i.first)); // index vals.emplace_back(ValElem(c, tv_i.first)); // index

View file

@ -114,10 +114,14 @@ void CPP_IndexedInits<T>::Generate(InitsManager* im, std::vector<TableValPtr>& i
auto iv_it = init_vals.begin(); auto iv_it = init_vals.begin();
auto iv_end = init_vals.end(); auto iv_end = init_vals.end();
auto t = *(iv_it++); auto t = *(iv_it++);
auto attrs = *(iv_it++);
auto tt = cast_intrusive<TableType>(im->Types(t)); auto tt = cast_intrusive<TableType>(im->Types(t));
auto tv = make_intrusive<TableVal>(tt); auto tv = make_intrusive<TableVal>(tt);
if ( attrs >= 0 )
tv->SetAttrs(im->Attributes(attrs));
while ( iv_it != iv_end ) while ( iv_it != iv_end )
{ {
auto index = im->ConstVals(*(iv_it++)); auto index = im->ConstVals(*(iv_it++));

View file

@ -259,6 +259,11 @@ TableValPtr table_constructor__CPP(vector<ValPtr> indices, vector<ValPtr> vals,
return aggr; return aggr;
} }
void assign_attrs__CPP(IDPtr id, std::vector<int> attr_tags, std::vector<ValPtr> attr_vals)
{
id->SetAttrs(build_attrs__CPP(move(attr_tags), move(attr_vals)));
}
RecordValPtr record_constructor__CPP(vector<ValPtr> vals, RecordTypePtr t) RecordValPtr record_constructor__CPP(vector<ValPtr> vals, RecordTypePtr t)
{ {
auto rv = make_intrusive<RecordVal>(t); auto rv = make_intrusive<RecordVal>(t);

View file

@ -171,6 +171,9 @@ extern TableValPtr table_constructor__CPP(std::vector<ValPtr> indices, std::vect
TableTypePtr t, std::vector<int> attr_tags, TableTypePtr t, std::vector<int> attr_tags,
std::vector<ValPtr> attr_vals); std::vector<ValPtr> attr_vals);
// Assigns a set of attributes to an identifier.
extern void assign_attrs__CPP(IDPtr id, std::vector<int> attr_tags, std::vector<ValPtr> attr_vals);
// Constructs a record of the given type, whose (ordered) fields are // Constructs a record of the given type, whose (ordered) fields are
// assigned to the corresponding elements of the given vector of values. // assigned to the corresponding elements of the given vector of values.
extern RecordValPtr record_constructor__CPP(std::vector<ValPtr> vals, RecordTypePtr t); extern RecordValPtr record_constructor__CPP(std::vector<ValPtr> vals, RecordTypePtr t);

View file

@ -15,7 +15,7 @@ The maintenance workflow:
to check in updates to the list of how the compiler currently fares to check in updates to the list of how the compiler currently fares
on various btests (see end of this doc): on various btests (see end of this doc):
Thu Sep 29 14:49:49 PDT 2022 Wed Oct 12 11:36:46 PDT 2022
2. Run "find-test-files.sh" to generate a list (to stdout) of all of the 2. Run "find-test-files.sh" to generate a list (to stdout) of all of the
possible Zeek source files found in the test suite. possible Zeek source files found in the test suite.

View file

@ -10,7 +10,7 @@ gen_out=CPP-test/gen.$abbr
echo "fail" echo "fail"
exit 1 exit 1
fi fi
if grep -E -q 'deprecated|skipping|cannot compile|no matching functions' $gen_out; then if grep -E -q '(deprecated.*(when|vector))|skipping|cannot compile|no matching functions' $gen_out; then
echo "fail" echo "fail"
exit 1 exit 1
fi fi