Fixed handling of invalid array access in mem2reg code

This commit is contained in:
Clifford Wolf 2014-10-16 00:44:23 +02:00
parent 82ed814fa1
commit 6b05a9e807
2 changed files with 15 additions and 3 deletions

View File

@ -209,6 +209,7 @@ namespace AST
void mem2reg_as_needed_pass1(std::map<AstNode*, std::set<std::string>> &mem2reg_places, void mem2reg_as_needed_pass1(std::map<AstNode*, std::set<std::string>> &mem2reg_places,
std::map<AstNode*, uint32_t> &mem2reg_flags, std::map<AstNode*, uint32_t> &proc_flags, uint32_t &status_flags); std::map<AstNode*, uint32_t> &mem2reg_flags, std::map<AstNode*, uint32_t> &proc_flags, uint32_t &status_flags);
void mem2reg_as_needed_pass2(std::set<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block); void mem2reg_as_needed_pass2(std::set<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block);
bool mem2reg_check(std::set<AstNode*> &mem2reg_set);
void meminfo(int &mem_width, int &mem_size, int &addr_bits); void meminfo(int &mem_width, int &mem_size, int &addr_bits);
// additional functionality for evaluating constant functions // additional functionality for evaluating constant functions

View File

@ -2179,14 +2179,25 @@ void AstNode::mem2reg_as_needed_pass1(std::map<AstNode*, std::set<std::string>>
} }
} }
bool AstNode::mem2reg_check(std::set<AstNode*> &mem2reg_set)
{
if (type != AST_IDENTIFIER || !id2ast || !mem2reg_set.count(id2ast))
return false;
if (children.empty() || children[0]->type != AST_RANGE || GetSize(children[0]->children) != 1)
log_error("Invalid array access at %s:%d.\n", filename.c_str(), linenum);
return true;
}
// actually replace memories with registers // actually replace memories with registers
void AstNode::mem2reg_as_needed_pass2(std::set<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block) void AstNode::mem2reg_as_needed_pass2(std::set<AstNode*> &mem2reg_set, AstNode *mod, AstNode *block)
{ {
if (type == AST_BLOCK) if (type == AST_BLOCK)
block = this; block = this;
if ((type == AST_ASSIGN_LE || type == AST_ASSIGN_EQ) && block != NULL && children[0]->id2ast && if ((type == AST_ASSIGN_LE || type == AST_ASSIGN_EQ) && block != NULL &&
mem2reg_set.count(children[0]->id2ast) > 0 && children[0]->children[0]->children[0]->type != AST_CONSTANT) children[0]->mem2reg_check(mem2reg_set) && children[0]->children[0]->children[0]->type != AST_CONSTANT)
{ {
std::stringstream sstr; std::stringstream sstr;
sstr << "$mem2reg_wr$" << children[0]->str << "$" << filename << ":" << linenum << "$" << (autoidx++); sstr << "$mem2reg_wr$" << children[0]->str << "$" << filename << ":" << linenum << "$" << (autoidx++);
@ -2242,7 +2253,7 @@ void AstNode::mem2reg_as_needed_pass2(std::set<AstNode*> &mem2reg_set, AstNode *
type = AST_ASSIGN_EQ; type = AST_ASSIGN_EQ;
} }
if (type == AST_IDENTIFIER && id2ast && mem2reg_set.count(id2ast) > 0) if (mem2reg_check(mem2reg_set))
{ {
AstNode *bit_part_sel = NULL; AstNode *bit_part_sel = NULL;
if (children.size() == 2) if (children.size() == 2)