mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 11:21:05 +01:00
Separate alive flag from the Value class (#408)
* separate the alive flag from the Value class * restored KillValue, added KillAddress * added comments * Update executable_semantics/interpreter/interpreter.cpp Co-authored-by: Dave Abrahams <dabrahams@google.com> * Update executable_semantics/interpreter/interpreter.cpp Co-authored-by: Dave Abrahams <dabrahams@google.com> * Update executable_semantics/interpreter/interpreter.cpp Co-authored-by: Dave Abrahams <dabrahams@google.com> * finish edits from Dave Co-authored-by: Dave Abrahams <dabrahams@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Dave Abrahams
parent
b8f6602857
commit
1da97fc9cf
@@ -49,23 +49,27 @@ auto AllocateValue(Value* v) -> Address {
|
||||
// or to leave it up to the caller.
|
||||
Address a = state->heap.size();
|
||||
state->heap.push_back(v);
|
||||
state->alive.push_back(true);
|
||||
return a;
|
||||
}
|
||||
|
||||
auto CopyVal(Value* val, int line_num) -> Value* {
|
||||
CheckAlive(val, line_num);
|
||||
switch (val->tag) {
|
||||
case ValKind::TupleV: {
|
||||
auto elts = new std::vector<std::pair<std::string, Address>>();
|
||||
for (auto& i : *val->u.tuple.elts) {
|
||||
CheckAlive(i.second, line_num);
|
||||
Value* elt = CopyVal(state->heap[i.second], line_num);
|
||||
elts->push_back(make_pair(i.first, AllocateValue(elt)));
|
||||
Address new_address = AllocateValue(elt);
|
||||
elts->push_back(make_pair(i.first, new_address));
|
||||
}
|
||||
return MakeTupleVal(elts);
|
||||
}
|
||||
case ValKind::AltV: {
|
||||
Value* arg = CopyVal(val->u.alt.arg, line_num);
|
||||
return MakeAltVal(*val->u.alt.alt_name, *val->u.alt.choice_name, arg);
|
||||
Value* arg = CopyVal(state->heap[val->u.alt.argument], line_num);
|
||||
Address argument_address = AllocateValue(arg);
|
||||
return MakeAltVal(*val->u.alt.alt_name, *val->u.alt.choice_name,
|
||||
argument_address);
|
||||
}
|
||||
case ValKind::StructV: {
|
||||
Value* inits = CopyVal(val->u.struct_val.inits, line_num);
|
||||
@@ -112,24 +116,20 @@ auto CopyVal(Value* val, int line_num) -> Value* {
|
||||
}
|
||||
}
|
||||
|
||||
void KillValue(Value* val) {
|
||||
val->alive = false;
|
||||
void KillObject(Address address);
|
||||
|
||||
// Marks all of the sub-objects of this value as dead.
|
||||
void KillSubObjects(Value* val) {
|
||||
switch (val->tag) {
|
||||
case ValKind::AltV:
|
||||
KillValue(val->u.alt.arg);
|
||||
KillObject(val->u.alt.argument);
|
||||
break;
|
||||
case ValKind::StructV:
|
||||
KillValue(val->u.struct_val.inits);
|
||||
KillSubObjects(val->u.struct_val.inits);
|
||||
break;
|
||||
case ValKind::TupleV:
|
||||
for (auto& elt : *val->u.tuple.elts) {
|
||||
if (state->heap[elt.second]->alive) {
|
||||
KillValue(state->heap[elt.second]);
|
||||
} else {
|
||||
std::cerr << "runtime error, killing an already dead value"
|
||||
<< std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
KillObject(elt.second);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -137,6 +137,17 @@ void KillValue(Value* val) {
|
||||
}
|
||||
}
|
||||
|
||||
// Marks the object at this address, and all of its sub-objects, as dead.
|
||||
void KillObject(Address address) {
|
||||
if (state->alive[address]) {
|
||||
state->alive[address] = false;
|
||||
KillSubObjects(state->heap[address]);
|
||||
} else {
|
||||
std::cerr << "runtime error, killing an already dead value" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
void PrintEnv(Env env, std::ostream& out) {
|
||||
for (const auto& [name, value] : env) {
|
||||
out << name << ": ";
|
||||
@@ -198,7 +209,6 @@ void PrintState(std::ostream& out) {
|
||||
//
|
||||
|
||||
auto ValToInt(Value* v, int line_num) -> int {
|
||||
CheckAlive(v, line_num);
|
||||
switch (v->tag) {
|
||||
case ValKind::IntV:
|
||||
return v->u.integer;
|
||||
@@ -210,7 +220,6 @@ auto ValToInt(Value* v, int line_num) -> int {
|
||||
}
|
||||
|
||||
auto ValToBool(Value* v, int line_num) -> int {
|
||||
CheckAlive(v, line_num);
|
||||
switch (v->tag) {
|
||||
case ValKind::BoolV:
|
||||
return v->u.boolean;
|
||||
@@ -221,7 +230,7 @@ auto ValToBool(Value* v, int line_num) -> int {
|
||||
}
|
||||
|
||||
auto ValToPtr(Value* v, int line_num) -> Address {
|
||||
CheckAlive(v, line_num);
|
||||
CheckAlive(v->u.ptr, line_num);
|
||||
switch (v->tag) {
|
||||
case ValKind::PtrV:
|
||||
return v->u.ptr;
|
||||
@@ -317,7 +326,6 @@ auto VariableDeclaration::InitGlobals(Env& globals) const -> void {
|
||||
// E is the environment (functions + parameters + locals)
|
||||
// F is the function
|
||||
void CallFunction(int line_num, std::vector<Value*> operas, State* state) {
|
||||
CheckAlive(operas[0], line_num);
|
||||
switch (operas[0]->tag) {
|
||||
case ValKind::FunV: {
|
||||
// Bind arguments to parameters
|
||||
@@ -345,8 +353,9 @@ void CallFunction(int line_num, std::vector<Value*> operas, State* state) {
|
||||
}
|
||||
case ValKind::AltConsV: {
|
||||
Value* arg = CopyVal(operas[1], line_num);
|
||||
Value* av = MakeAltVal(*operas[0]->u.alt_cons.alt_name,
|
||||
*operas[0]->u.alt_cons.choice_name, arg);
|
||||
Value* av =
|
||||
MakeAltVal(*operas[0]->u.alt_cons.alt_name,
|
||||
*operas[0]->u.alt_cons.choice_name, AllocateValue(arg));
|
||||
Frame* frame = state->stack.Top();
|
||||
frame->todo.Push(MakeValAct(av));
|
||||
break;
|
||||
@@ -366,7 +375,7 @@ void KillScope(int line_num, Scope* scope) {
|
||||
std::cerr << "internal error in KillScope" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
KillValue(state->heap[*a]);
|
||||
KillObject(*a);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -444,7 +453,8 @@ auto PatternMatch(Value* p, Value* v, Env env, std::list<std::string>* vars,
|
||||
return std::nullopt;
|
||||
}
|
||||
std::optional<Env> env_with_matches =
|
||||
PatternMatch(p->u.alt.arg, v->u.alt.arg, env, vars, line_num);
|
||||
PatternMatch(state->heap[p->u.alt.argument],
|
||||
state->heap[v->u.alt.argument], env, vars, line_num);
|
||||
if (!env_with_matches) {
|
||||
return std::nullopt;
|
||||
}
|
||||
@@ -525,7 +535,8 @@ void PatternAssignment(Value* pat, Value* val, int line_num) {
|
||||
std::cerr << "internal error in pattern assignment" << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
PatternAssignment(pat->u.alt.arg, val->u.alt.arg, line_num);
|
||||
PatternAssignment(state->heap[pat->u.alt.argument],
|
||||
state->heap[val->u.alt.argument], line_num);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -569,7 +580,7 @@ void StepLvalue() {
|
||||
exit(-1);
|
||||
}
|
||||
Value* v = MakePtrVal(*pointer);
|
||||
CheckAlive(v, exp->line_num);
|
||||
CheckAlive(*pointer, exp->line_num);
|
||||
frame->todo.Pop();
|
||||
frame->todo.Push(MakeValAct(v));
|
||||
break;
|
||||
@@ -950,7 +961,7 @@ void HandleValue() {
|
||||
}
|
||||
switch (act->tag) {
|
||||
case ActionKind::DeleteTmpAction: {
|
||||
KillValue(state->heap[act->u.delete_tmp]);
|
||||
KillObject(act->u.delete_tmp);
|
||||
frame->todo.Pop(2);
|
||||
frame->todo.Push(val_act);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user