improved checking for liveness when reading and writing memory (#448)

* improved checking for liveness when reading and writing memory

* moving some functions to be methods of State

* finished moving functions into State

* Update executable_semantics/interpreter/interpreter.h

Co-authored-by: Geoff Romer <gromer@google.com>

* moved some comments, other minor edits

Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Jeremy G. Siek
2021-04-21 14:10:42 -04:00
committed by GitHub
co-authored by Geoff Romer
parent 7e81fb9c62
commit 27fd9de5bf
8 changed files with 148 additions and 86 deletions
+12 -16
View File
@@ -213,7 +213,14 @@ auto MakeChoiceTypeVal(std::string name,
return v;
}
void PrintValue(const Value* val, std::ostream& out) {
auto State::PrintAddress(Address a, std::ostream& out) -> void {
if (!this->alive[a]) {
out << "!!";
}
PrintValue(this->heap[a], out);
}
auto PrintValue(const Value* val, std::ostream& out) -> void {
switch (val->tag) {
case ValKind::AltConsV: {
out << *val->u.alt_cons.choice_name << "." << *val->u.alt_cons.alt_name;
@@ -227,7 +234,7 @@ void PrintValue(const Value* val, std::ostream& out) {
case ValKind::AltV: {
out << "alt " << *val->u.alt.choice_name << "." << *val->u.alt.alt_name
<< " ";
PrintValue(state->heap[val->u.alt.argument], out);
state->PrintAddress(val->u.alt.argument, out);
break;
}
case ValKind::StructV: {
@@ -246,7 +253,7 @@ void PrintValue(const Value* val, std::ostream& out) {
}
out << elt.first << " = ";
PrintValue(state->heap[elt.second], out);
state->PrintAddress(elt.second, out);
out << "@" << elt.second;
}
out << ")";
@@ -336,8 +343,8 @@ auto TypeEqual(const Value* t1, const Value* t2) -> bool {
if (t2_field == std::nullopt) {
return false;
}
if (!TypeEqual(state->heap[(*t1->u.tuple.elts)[i].second],
state->heap[*t2_field])) {
if (!TypeEqual(state->ReadFromMemory((*t1->u.tuple.elts)[i].second, 0),
state->ReadFromMemory(*t2_field, 0))) {
return false;
}
}
@@ -363,8 +370,6 @@ auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool {
case ValKind::BoolV:
return v1->u.boolean == v2->u.boolean;
case ValKind::PtrV:
CheckAlive(v1->u.ptr, line_num);
CheckAlive(v2->u.ptr, line_num);
return v1->u.ptr == v2->u.ptr;
case ValKind::FunV:
return v1->u.fun.body == v2->u.fun.body;
@@ -402,13 +407,4 @@ auto ToInteger(const Value* v) -> int {
}
}
void CheckAlive(Address address, int line_num) {
if (!state->alive[address]) {
std::cerr << line_num << ": undefined behavior: access to dead value ";
PrintValue(state->heap[address], std::cerr);
std::cerr << std::endl;
exit(-1);
}
}
} // namespace Carbon