tweaks to address concerns flagged by Coverity

This commit is contained in:
Vern Paxson 2021-09-13 12:57:15 -07:00
parent 8414d13030
commit 7f3993ca0e
8 changed files with 45 additions and 10 deletions

View file

@ -1007,6 +1007,8 @@ void ZAMCompiler::KillInst(int i)
if ( inst->IsUnconditionalBranch() ) if ( inst->IsUnconditionalBranch() )
{ {
ASSERT(t);
// No direct flow after this point ... unless we're // No direct flow after this point ... unless we're
// branching to the next immediate live instruction. // branching to the next immediate live instruction.
auto after_inst = NextLiveInst(inst, true); auto after_inst = NextLiveInst(inst, true);

View file

@ -371,21 +371,25 @@ bool ZAMCompiler::BuiltIn_sub_bytes(const NameExpr* n, const ExprPList& args)
break; break;
case 0x4: // first argument a constant case 0x4: // first argument a constant
ASSERT(c);
z = ZInstI(OP_SUB_BYTES_VVVC, nslot, v3, v4, c); z = ZInstI(OP_SUB_BYTES_VVVC, nslot, v3, v4, c);
z.op_type = OP_VVVC; z.op_type = OP_VVVC;
break; break;
case 0x5: // first and third constant case 0x5: // first and third constant
ASSERT(c);
z = ZInstI(OP_SUB_BYTES_VViC, nslot, v3, v4, c); z = ZInstI(OP_SUB_BYTES_VViC, nslot, v3, v4, c);
z.op_type = OP_VVVC_I3; z.op_type = OP_VVVC_I3;
break; break;
case 0x6: // first and second constant - flip! case 0x6: // first and second constant - flip!
ASSERT(c);
z = ZInstI(OP_SUB_BYTES_ViVC, nslot, v4, v3, c); z = ZInstI(OP_SUB_BYTES_ViVC, nslot, v4, v3, c);
z.op_type = OP_VVVC_I3; z.op_type = OP_VVVC_I3;
break; break;
case 0x7: // whole shebang case 0x7: // whole shebang
ASSERT(c);
z = ZInstI(OP_SUB_BYTES_ViiC, nslot, v3, v4, c); z = ZInstI(OP_SUB_BYTES_ViiC, nslot, v3, v4, c);
z.op_type = OP_VVVC_I2_I3; z.op_type = OP_VVVC_I2_I3;
break; break;

View file

@ -183,7 +183,7 @@ private:
const ZAMStmt LoopOverVector(const ForStmt* f, const NameExpr* val); const ZAMStmt LoopOverVector(const ForStmt* f, const NameExpr* val);
const ZAMStmt LoopOverString(const ForStmt* f, const Expr* e); const ZAMStmt LoopOverString(const ForStmt* f, const Expr* e);
const ZAMStmt FinishLoop(const ZAMStmt iter_head, ZInstI iter_stmt, const ZAMStmt FinishLoop(const ZAMStmt iter_head, ZInstI& iter_stmt,
const Stmt* body, int iter_slot, const Stmt* body, int iter_slot,
bool is_table); bool is_table);

View file

@ -105,6 +105,7 @@ const ZAMStmt ZAMCompiler::AppendToField(const NameExpr* n1, const NameExpr* n2,
} }
else else
{ {
ASSERT(c);
z = ZInstI(OP_APPENDTOFIELD_VCi, FrameSlot(n1), offset, c); z = ZInstI(OP_APPENDTOFIELD_VCi, FrameSlot(n1), offset, c);
z.op_type = OP_VVC_I2; z.op_type = OP_VVC_I2;
} }
@ -547,6 +548,8 @@ const ZAMStmt ZAMCompiler::CompileInExpr(const NameExpr* n1, const ListExpr* l,
else if ( l_e0_n ) else if ( l_e0_n )
{ {
ASSERT(l_e1_c);
z = GenInst(OP_VAL2_IS_IN_TABLE_VVVC, z = GenInst(OP_VAL2_IS_IN_TABLE_VVVC,
n1, l_e0_n, n2, l_e1_c); n1, l_e0_n, n2, l_e1_c);
z.t2 = l_e0_n->GetType(); z.t2 = l_e0_n->GetType();
@ -554,6 +557,8 @@ const ZAMStmt ZAMCompiler::CompileInExpr(const NameExpr* n1, const ListExpr* l,
else if ( l_e1_n ) else if ( l_e1_n )
{ {
ASSERT(l_e0_c);
z = GenInst(OP_VAL2_IS_IN_TABLE_VVCV, z = GenInst(OP_VAL2_IS_IN_TABLE_VVCV,
n1, l_e1_n, n2, l_e0_c); n1, l_e1_n, n2, l_e0_c);
z.t2 = l_e1_n->GetType(); z.t2 = l_e1_n->GetType();
@ -563,6 +568,9 @@ const ZAMStmt ZAMCompiler::CompileInExpr(const NameExpr* n1, const ListExpr* l,
{ {
// Ugh, both are constants. Assign first to // Ugh, both are constants. Assign first to
// a temporary. // a temporary.
ASSERT(l_e0_c);
ASSERT(l_e1_c);
auto slot = TempForConst(l_e0_c); auto slot = TempForConst(l_e0_c);
z = ZInstI(OP_VAL2_IS_IN_TABLE_VVVC, FrameSlot(n1), z = ZInstI(OP_VAL2_IS_IN_TABLE_VVVC, FrameSlot(n1),
slot, FrameSlot(n2), l_e1_c); slot, FrameSlot(n2), l_e1_c);
@ -687,6 +695,8 @@ const ZAMStmt ZAMCompiler::CompileIndex(const NameExpr* n1, int n2_slot,
else else
{ {
ASSERT(c3);
auto zop = AssignmentFlavor(OP_TABLE_INDEX1_VVC, auto zop = AssignmentFlavor(OP_TABLE_INDEX1_VVC,
n1->GetType()->Tag()); n1->GetType()->Tag());
z = ZInstI(zop, Frame1Slot(n1, zop), z = ZInstI(zop, Frame1Slot(n1, zop),
@ -911,16 +921,22 @@ const ZAMStmt ZAMCompiler::DoCall(const CallExpr* c, const NameExpr* n)
z = ZInstI(AssignmentFlavor(OP_CALL1_VV, nt), z = ZInstI(AssignmentFlavor(OP_CALL1_VV, nt),
n_slot, FrameSlot(n0)); n_slot, FrameSlot(n0));
else else
{
ASSERT(c0);
z = ZInstI(AssignmentFlavor(OP_CALL1_VC, nt), z = ZInstI(AssignmentFlavor(OP_CALL1_VC, nt),
n_slot, c0); n_slot, c0);
} }
}
else else
{ {
if ( n0 ) if ( n0 )
z = ZInstI(OP_CALL1_V, FrameSlot(n0)); z = ZInstI(OP_CALL1_V, FrameSlot(n0));
else else
{
ASSERT(c0);
z = ZInstI(OP_CALL1_C, c0); z = ZInstI(OP_CALL1_C, c0);
} }
}
z.t = arg0->GetType(); z.t = arg0->GetType();
} }

View file

@ -2272,6 +2272,15 @@ bool ZAMGen::ParseTemplate()
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
ZAMGen(argc, argv); try
{
ZAMGen zg(argc, argv);
exit(0); exit(0);
} }
catch ( const std::regex_error& e )
{
fprintf(stderr, "%s: regular expression error - %s\n",
argv[0], e.what());
exit(1);
}
}

View file

@ -90,7 +90,7 @@ private:
const TableVal* tv = nullptr; const TableVal* tv = nullptr;
// Associated auxiliary information. // Associated auxiliary information.
ZInstAux* aux; ZInstAux* aux = nullptr;
std::optional<DictIterator> tbl_iter; std::optional<DictIterator> tbl_iter;
std::optional<DictIterator> tbl_end; std::optional<DictIterator> tbl_end;

View file

@ -530,6 +530,7 @@ const ZAMStmt ZAMCompiler::ValueSwitch(const SwitchStmt* sw, const NameExpr* v,
auto sv = cv->AsString()->Render(); auto sv = cv->AsString()->Render();
std::string s(sv); std::string s(sv);
new_str_cases[s] = case_body_start; new_str_cases[s] = case_body_start;
delete[] sv;
break; break;
} }
@ -897,6 +898,7 @@ const ZAMStmt ZAMCompiler::LoopOverString(const ForStmt* f, const Expr* e)
} }
else else
{ {
ASSERT(c);
z = ZInstI(OP_INIT_STRING_LOOP_VC, iter_slot, c); z = ZInstI(OP_INIT_STRING_LOOP_VC, iter_slot, c);
z.op_type = OP_VC_I1; z.op_type = OP_VC_I1;
} }
@ -926,9 +928,9 @@ const ZAMStmt ZAMCompiler::Loop(const Stmt* body)
return tail; return tail;
} }
const ZAMStmt ZAMCompiler::FinishLoop(const ZAMStmt iter_head, ZInstI iter_stmt, const ZAMStmt ZAMCompiler::FinishLoop(const ZAMStmt iter_head,
const Stmt* body, int iter_slot, ZInstI& iter_stmt, const Stmt* body,
bool is_table) int iter_slot, bool is_table)
{ {
auto loop_iter = AddInst(iter_stmt); auto loop_iter = AddInst(iter_stmt);
auto body_end = CompileStmt(body); auto body_end = CompileStmt(body);

View file

@ -39,10 +39,10 @@ public:
// The current end of the frame slot's scope. Gets updated as // The current end of the frame slot's scope. Gets updated as
// new IDs are added to share the slot. // new IDs are added to share the slot.
int scope_end; int scope_end = -1;
// Whether this is a managed slot. // Whether this is a managed slot.
bool is_managed; bool is_managed = false;
}; };
using FrameReMap = std::vector<FrameSharingInfo>; using FrameReMap = std::vector<FrameSharingInfo>;
@ -98,7 +98,9 @@ public:
// When an instruction has both frame slots and integer constants, // When an instruction has both frame slots and integer constants,
// the former always come first, even if conceptually in the operation // the former always come first, even if conceptually in the operation
// the constant is an "earlier" operand. // the constant is an "earlier" operand.
int v1, v2, v3, v4; //
// Initialized here to keep Coverity happy.
int v1 = -1, v2 = -1, v3 = -1, v4 = -1;
ZVal c; // constant associated with instruction, if any ZVal c; // constant associated with instruction, if any