low-level cleanups found by code review

This commit is contained in:
Vern Paxson 2021-09-02 12:55:25 -07:00
parent 8bc04d3593
commit 38578a2ea3
13 changed files with 56 additions and 71 deletions

View file

@ -145,7 +145,7 @@ void CPPCompile::ExpandListTypeVar(const TypePtr& t, string& tn)
const auto& tl = t->AsTypeList()->GetTypes(); const auto& tl = t->AsTypeList()->GetTypes();
auto t_name = tn + "->AsTypeList()"; auto t_name = tn + "->AsTypeList()";
for ( auto& tl_i : tl ) for ( const auto& tl_i : tl )
AddInit(t, t_name + "->Append(" + GenTypeName(tl_i) + ");"); AddInit(t, t_name + "->Append(" + GenTypeName(tl_i) + ");");
} }

View file

@ -753,12 +753,9 @@ void ZAMCompiler::SetLifetimeStart(int slot, const ZInstI* inst)
denizen_beginning[slot] = inst; denizen_beginning[slot] = inst;
if ( inst_beginnings.count(inst) == 0 ) if ( inst_beginnings.count(inst) == 0 )
{
// Need to create a set to track the denizens // Need to create a set to track the denizens
// beginning at the instruction. // beginning at the instruction.
std::unordered_set<ID*> denizens; inst_beginnings[inst] = {};
inst_beginnings[inst] = denizens;
}
inst_beginnings[inst].insert(frame_denizens[slot]); inst_beginnings[inst].insert(frame_denizens[slot]);
} }
@ -826,10 +823,7 @@ void ZAMCompiler::ExtendLifetime(int slot, const ZInstI* inst)
inst_endings[old_inst].erase(frame_denizens[slot]); inst_endings[old_inst].erase(frame_denizens[slot]);
if ( inst_endings.count(inst) == 0 ) if ( inst_endings.count(inst) == 0 )
{ inst_endings[inst] = {};
std::unordered_set<ID*> denizens;
inst_endings[inst] = denizens;
}
inst_endings[inst].insert(frame_denizens[slot]); inst_endings[inst].insert(frame_denizens[slot]);
denizen_ending.at(slot) = inst; denizen_ending.at(slot) = inst;

View file

@ -25,7 +25,7 @@ class CatchReturnStmt;
class ProfileFunc; class ProfileFunc;
typedef ZInstI* InstLabel; using InstLabel = ZInstI*;
// Class representing a single compiled statement. (This is different from, // Class representing a single compiled statement. (This is different from,
// but related to, the ZAM instruction(s) generated for that compilation.) // but related to, the ZAM instruction(s) generated for that compilation.)
@ -296,8 +296,8 @@ private:
} }
typedef std::vector<ZAMStmt> GoToSet; using GoToSet = std::vector<ZAMStmt>;
typedef std::vector<GoToSet> GoToSets; using GoToSets = std::vector<GoToSet>;
void PushGoTos(GoToSets& gotos); void PushGoTos(GoToSets& gotos);
void ResolveGoTos(GoToSets& gotos, const InstLabel l); void ResolveGoTos(GoToSets& gotos, const InstLabel l);
@ -569,8 +569,8 @@ private:
// A type for mapping an instruction to a set of locals associated // A type for mapping an instruction to a set of locals associated
// with it. // with it.
typedef std::unordered_map<const ZInstI*, std::unordered_set<ID*>> using AssociatedLocals =
AssociatedLocals; std::unordered_map<const ZInstI*, std::unordered_set<ID*>>;
// Maps (live) instructions to which frame denizens begin their // Maps (live) instructions to which frame denizens begin their
// lifetime via an initialization at that instruction, if any ... // lifetime via an initialization at that instruction, if any ...
@ -584,7 +584,7 @@ private:
AssociatedLocals inst_endings; AssociatedLocals inst_endings;
// A type for inverse mappings. // A type for inverse mappings.
typedef std::unordered_map<int, const ZInstI*> AssociatedInsts; using AssociatedInsts = std::unordered_map<int, const ZInstI*>;
// Inverse mappings: for a given frame denizen's slot, where its // Inverse mappings: for a given frame denizen's slot, where its
// lifetime begins and ends. // lifetime begins and ends.

View file

@ -102,9 +102,11 @@ void ZAMCompiler::InitLocals()
for ( auto l : pf->Locals() ) for ( auto l : pf->Locals() )
{ {
auto non_const_l = const_cast<ID*>(l); auto non_const_l = const_cast<ID*>(l);
// ### should check for unused variables.
// Don't add locals that were already added because they're // Don't add locals that were already added because they're
// parameters. // parameters.
//
// Don't worry about unused variables, those will get
// removed during low-level ZAM optimization.
if ( ! HasFrameSlot(non_const_l) ) if ( ! HasFrameSlot(non_const_l) )
(void) AddToFrame(non_const_l); (void) AddToFrame(non_const_l);
} }
@ -289,11 +291,7 @@ void ZAMCompiler::AdjustBranches()
if ( ! inst->live ) if ( ! inst->live )
continue; continue;
auto t = inst->target; if ( auto t = inst->target )
if ( ! t )
continue;
inst->target = FindLiveTarget(t); inst->target = FindLiveTarget(t);
} }
} }
@ -301,13 +299,9 @@ void ZAMCompiler::AdjustBranches()
void ZAMCompiler::RetargetBranches() void ZAMCompiler::RetargetBranches()
{ {
for ( auto& inst : insts2 ) for ( auto& inst : insts2 )
{ if ( inst->target )
if ( ! inst->target )
continue;
ConcretizeBranch(inst, inst->target, inst->target_slot); ConcretizeBranch(inst, inst->target, inst->target_slot);
} }
}
void ZAMCompiler::RemapFrameDenizens(const std::vector<int>& inst1_to_inst2) void ZAMCompiler::RemapFrameDenizens(const std::vector<int>& inst1_to_inst2)
{ {

View file

@ -58,8 +58,6 @@ const ZAMStmt ZAMCompiler::CompileIncrExpr(const IncrExpr* e)
{ {
auto target = e->Op()->AsRefExpr()->GetOp1()->AsNameExpr(); auto target = e->Op()->AsRefExpr()->GetOp1()->AsNameExpr();
auto s = EmptyStmt();
if ( target->GetType()->Tag() == TYPE_INT ) if ( target->GetType()->Tag() == TYPE_INT )
{ {
if ( e->Tag() == EXPR_INCR ) if ( e->Tag() == EXPR_INCR )

View file

@ -8,12 +8,12 @@
using namespace std; using namespace std;
// Helper functions to convert dashes to underscores or vice versa. // Helper functions to convert dashes to underscores or vice versa.
char dash_to_under(char c) static char dash_to_under(char c)
{ {
return c == '-' ? '_' : c; return c == '-' ? '_' : c;
} }
char under_to_dash(char c) static char under_to_dash(char c)
{ {
return c == '_' ? '-' : c; return c == '_' ? '-' : c;
} }
@ -1944,7 +1944,7 @@ string TemplateInput::SkipWords(const string& line, int n) const
return string(s); return string(s);
} }
void TemplateInput::Gripe(const char* msg, const string& input) void TemplateInput::Gripe(const char* msg, const string& input) const
{ {
auto input_s = input.c_str(); auto input_s = input.c_str();
int n = strlen(input_s); int n = strlen(input_s);
@ -1957,7 +1957,7 @@ void TemplateInput::Gripe(const char* msg, const string& input)
exit(1); exit(1);
} }
void TemplateInput::Gripe(const char* msg, const InputLoc& l) void TemplateInput::Gripe(const char* msg, const InputLoc& l) const
{ {
fprintf(stderr, "%s, line %d: %s\n", l.file_name, l.line_num, msg); fprintf(stderr, "%s, line %d: %s\n", l.file_name, l.line_num, msg);
exit(1); exit(1);

View file

@ -183,15 +183,15 @@ public:
// Returns a string defining the parameters for a declaration; // Returns a string defining the parameters for a declaration;
// these have full C++ type information along with the parameter // these have full C++ type information along with the parameter
// name. // name.
string Decls() { return full_decl; } string Decls() const { return full_decl; }
// Returns a string for passing the parameters in a function // Returns a string for passing the parameters in a function
// call. This is a comma-separated list of the parameter names, // call. This is a comma-separated list of the parameter names,
// with no associated C++ types. // with no associated C++ types.
string Params() { return full_params; } string Params() const { return full_params; }
// Returns the name of the given parameter, indexed starting with 0. // Returns the name of the given parameter, indexed starting with 0.
const string& NthParam(int n) { return params[n]; } const string& NthParam(int n) const { return params[n]; }
private: private:
// Makes sure that each parameter has a unique name. For any // Makes sure that each parameter has a unique name. For any
@ -894,8 +894,8 @@ public:
void PutBack(const string& line) { put_back = line; } void PutBack(const string& line) { put_back = line; }
// Report an error and exit. // Report an error and exit.
void Gripe(const char* msg, const string& input); [[noreturn]] void Gripe(const char* msg, const string& input) const;
void Gripe(const char* msg, const InputLoc& loc); [[noreturn]] void Gripe(const char* msg, const InputLoc& loc) const;
private: private:
string put_back; // if non-empty, use this for the next ScanLine string put_back; // if non-empty, use this for the next ScanLine
@ -943,9 +943,9 @@ public:
void IndentDown() { --indent_level; } void IndentDown() { --indent_level; }
void SetNoNL(bool _no_NL) { no_NL = _no_NL; } void SetNoNL(bool _no_NL) { no_NL = _no_NL; }
void Gripe(const char* msg, const string& input) [[noreturn]] void Gripe(const char* msg, const string& input) const
{ ti->Gripe(msg, input); } { ti->Gripe(msg, input); }
void Gripe(const char* msg, const InputLoc& loc) [[noreturn]] void Gripe(const char* msg, const InputLoc& loc) const
{ ti->Gripe(msg, loc); } { ti->Gripe(msg, loc); }
private: private:

View file

@ -1463,7 +1463,7 @@ macro EvalScheduleArgs(time, is_delta, build_args)
if ( is_delta ) if ( is_delta )
dt += run_state::network_time; dt += run_state::network_time;
auto handler = EventHandlerPtr(z.event_handler); auto handler = EventHandlerPtr(z.event_handler);
val_vec args; ValVec args;
build_args build_args
auto timer = new ScheduleTimer(handler, std::move(args), dt); auto timer = new ScheduleTimer(handler, std::move(args), dt);
timer_mgr->Add(timer); timer_mgr->Add(timer);
@ -1498,26 +1498,26 @@ op Event
type HL type HL
op1-read op1-read
custom-method return CompileEvent(h, l); custom-method return CompileEvent(h, l);
eval val_vec args; eval ValVec args;
z.aux->FillValVec(args, frame); z.aux->FillValVec(args, frame);
event_mgr.Enqueue(z.event_handler, std::move(args)); event_mgr.Enqueue(z.event_handler, std::move(args));
internal-op Event0 internal-op Event0
type X type X
eval val_vec args(0); eval ValVec args(0);
event_mgr.Enqueue(z.event_handler, std::move(args)); event_mgr.Enqueue(z.event_handler, std::move(args));
internal-op Event1 internal-op Event1
type V type V
op1-read op1-read
eval val_vec args(1); eval ValVec args(1);
args[0] = frame[z.v1].ToVal(z.t); args[0] = frame[z.v1].ToVal(z.t);
event_mgr.Enqueue(z.event_handler, std::move(args)); event_mgr.Enqueue(z.event_handler, std::move(args));
internal-op Event2 internal-op Event2
type VV type VV
op1-read op1-read
eval val_vec args(2); eval ValVec args(2);
args[0] = frame[z.v1].ToVal(z.t); args[0] = frame[z.v1].ToVal(z.t);
args[1] = frame[z.v2].ToVal(z.t2); args[1] = frame[z.v2].ToVal(z.t2);
event_mgr.Enqueue(z.event_handler, std::move(args)); event_mgr.Enqueue(z.event_handler, std::move(args));
@ -1525,7 +1525,7 @@ eval val_vec args(2);
internal-op Event3 internal-op Event3
type VVV type VVV
op1-read op1-read
eval val_vec args(3); eval ValVec args(3);
args[0] = frame[z.v1].ToVal(z.t); args[0] = frame[z.v1].ToVal(z.t);
args[1] = frame[z.v2].ToVal(z.t2); args[1] = frame[z.v2].ToVal(z.t2);
auto types = z.aux->types; auto types = z.aux->types;
@ -1535,7 +1535,7 @@ eval val_vec args(3);
internal-op Event4 internal-op Event4
type VVVV type VVVV
op1-read op1-read
eval val_vec args(4); eval ValVec args(4);
args[0] = frame[z.v1].ToVal(z.t); args[0] = frame[z.v1].ToVal(z.t);
args[1] = frame[z.v2].ToVal(z.t2); args[1] = frame[z.v2].ToVal(z.t2);
auto types = z.aux->types; auto types = z.aux->types;

View file

@ -9,7 +9,7 @@
namespace zeek::detail { namespace zeek::detail {
typedef std::vector<ValPtr> val_vec; using ValVec = std::vector<ValPtr>;
// The (reduced) statement currently being compiled. Used for both // The (reduced) statement currently being compiled. Used for both
// tracking "use" and "reaching" definitions, and for error messages. // tracking "use" and "reaching" definitions, and for error messages.

View file

@ -98,6 +98,7 @@ static void vec_exec(ZOp op, TypePtr t, VectorVal*& v1, VectorVal* v2,
auto res_zv = new VectorVal(yt); \ auto res_zv = new VectorVal(yt); \
auto n = v.size(); \ auto n = v.size(); \
res_zv->Resize(n); \ res_zv->Resize(n); \
ASSERT(0); \
auto& res = *res_zv->RawVec(); \ auto& res = *res_zv->RawVec(); \
for ( auto i = 0U; i < n; ++i ) \ for ( auto i = 0U; i < n; ++i ) \
if ( v[i] ) \ if ( v[i] ) \
@ -284,7 +285,8 @@ ValPtr ZBody::DoExec(Frame* f, int start_pc, StmtFlowType& flow)
flow = FLOW_RETURN; // can be over-written by a Hook-Break flow = FLOW_RETURN; // can be over-written by a Hook-Break
while ( pc < end_pc && ! ZAM_error ) { while ( pc < end_pc && ! ZAM_error )
{
auto& z = insts[pc]; auto& z = insts[pc];
#ifdef DEBUG #ifdef DEBUG

View file

@ -121,11 +121,8 @@ private:
class ZAMResumption : public Stmt { class ZAMResumption : public Stmt {
public: public:
ZAMResumption(ZBody* _am, int _xfer_pc) ZAMResumption(ZBody* _am, int _xfer_pc)
: Stmt(STMT_ZAM_RESUMPTION) : Stmt(STMT_ZAM_RESUMPTION), am(_am), xfer_pc(_xfer_pc)
{ { }
am = _am;
xfer_pc = _xfer_pc;
}
ValPtr Exec(Frame* f, StmtFlowType& flow) override; ValPtr Exec(Frame* f, StmtFlowType& flow) override;

View file

@ -17,7 +17,7 @@ class Stmt;
using AttributesPtr = IntrusivePtr<Attributes>; using AttributesPtr = IntrusivePtr<Attributes>;
// Maps ZAM frame slots to associated identifiers. // Maps ZAM frame slots to associated identifiers.
typedef std::vector<ID*> FrameMap; using FrameMap = std::vector<ID*>;
// Maps ZAM frame slots to information for sharing the slot across // Maps ZAM frame slots to information for sharing the slot across
// multiple script variables. // multiple script variables.
@ -45,7 +45,7 @@ public:
bool is_managed; bool is_managed;
}; };
typedef std::vector<FrameSharingInfo> FrameReMap; using FrameReMap = std::vector<FrameSharingInfo>;
class ZInstAux; class ZInstAux;
@ -358,7 +358,7 @@ public:
} }
// Returns the parallel arrays converted to a vector of ValPtr's. // Returns the parallel arrays converted to a vector of ValPtr's.
const val_vec& ToValVec(const ZVal* frame) const ValVec& ToValVec(const ZVal* frame)
{ {
vv.clear(); vv.clear();
FillValVec(vv, frame); FillValVec(vv, frame);
@ -367,7 +367,7 @@ public:
// Populates the given vector of ValPtr's with the conversion // Populates the given vector of ValPtr's with the conversion
// of the parallel arrays. // of the parallel arrays.
void FillValVec(val_vec& vec, const ZVal* frame) const void FillValVec(ValVec& vec, const ZVal* frame) const
{ {
for ( auto i = 0; i < n; ++i ) for ( auto i = 0; i < n; ++i )
vec.push_back(ToVal(frame, i)); vec.push_back(ToVal(frame, i));
@ -439,7 +439,7 @@ public:
// //
// If we cared about memory penny-pinching, we could make this // If we cared about memory penny-pinching, we could make this
// a pointer and only instantiate as needed. // a pointer and only instantiate as needed.
val_vec vv; ValVec vv;
}; };
// Returns a human-readable version of the given ZAM op-code. // Returns a human-readable version of the given ZAM op-code.