From 47325be112832955808657a38928aadf696dd023 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Thu, 19 Aug 2021 11:34:03 -0700 Subject: [PATCH] Switch interpreter Stacks to Ptr (#763) This also eliminates the ctad wrapper for Stack: I think the leaning is to remove it. It felt worth keeping the constructor because constructing with a single element is a common use-case. Adds a single-argument constructor for Scope because the `std::list()` is common, and eliding it is consistent with what we've done for things like tuples. I was considering a vector constructor due to the double-Push on line 1139, but thought the Push() semantics may mean that it's better not to provide. --- executable_semantics/interpreter/action.cpp | 2 +- executable_semantics/interpreter/action.h | 2 +- executable_semantics/interpreter/frame.h | 24 +- .../interpreter/interpreter.cpp | 250 +++++++++--------- .../interpreter/interpreter.h | 2 +- executable_semantics/interpreter/stack.h | 9 +- executable_semantics/interpreter/value.h | 7 +- 7 files changed, 142 insertions(+), 154 deletions(-) diff --git a/executable_semantics/interpreter/action.cpp b/executable_semantics/interpreter/action.cpp index 52985b97d63c..7f6a30b39075 100644 --- a/executable_semantics/interpreter/action.cpp +++ b/executable_semantics/interpreter/action.cpp @@ -53,7 +53,7 @@ void Action::Print(llvm::raw_ostream& out) const { } } -void Action::PrintList(const Stack& ls, llvm::raw_ostream& out) { +void Action::PrintList(const Stack>& ls, llvm::raw_ostream& out) { llvm::ListSeparator sep(" :: "); for (const auto& action : ls) { out << sep << *action; diff --git a/executable_semantics/interpreter/action.h b/executable_semantics/interpreter/action.h index 3cfb7fe6a072..8c539e7a273e 100644 --- a/executable_semantics/interpreter/action.h +++ b/executable_semantics/interpreter/action.h @@ -54,7 +54,7 @@ class Action { // object. auto Tag() const -> Kind { return tag; } - static void PrintList(const Stack& ls, llvm::raw_ostream& out); + static void PrintList(const Stack>& ls, llvm::raw_ostream& out); void Print(llvm::raw_ostream& out) const; LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } diff --git a/executable_semantics/interpreter/frame.h b/executable_semantics/interpreter/frame.h index 01b49b884952..599527135d38 100644 --- a/executable_semantics/interpreter/frame.h +++ b/executable_semantics/interpreter/frame.h @@ -20,14 +20,25 @@ namespace Carbon { using Env = Dictionary; struct Scope { + explicit Scope(Env values) : Scope(values, std::list()) {} Scope(Env values, std::list l) : values(values), locals(std::move(l)) {} + Env values; std::list locals; }; // A frame represents either a function call or a delimited continuation. struct Frame { + Frame(const Frame&) = delete; + Frame& operator=(const Frame&) = delete; + + Frame(std::string n, Stack> s, Stack> c) + : name(std::move(std::move(n))), scopes(s), todo(c), continuation() {} + + void Print(llvm::raw_ostream& out) const; + LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } + // The name of the function. std::string name; // If the frame represents a function call, the bottom scope @@ -36,23 +47,14 @@ struct Frame { // blocks within the function. The scope at the top of the stack is // the current scope and its environment is the one used for looking // up the value associated with a variable. - Stack scopes; + Stack> scopes; // The actions that need to be executed in the future of the // current function call. The top of the stack is the action // that is executed first. - Stack todo; + Stack> todo; // If this frame is the bottom frame of a continuation, then it stores // the address of the continuation. std::optional
continuation; - - Frame(const Frame&) = delete; - Frame& operator=(const Frame&) = delete; - - Frame(std::string n, Stack s, Stack c) - : name(std::move(std::move(n))), scopes(s), todo(c), continuation() {} - - void Print(llvm::raw_ostream& out) const; - LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); } }; } // namespace Carbon diff --git a/executable_semantics/interpreter/interpreter.cpp b/executable_semantics/interpreter/interpreter.cpp index 87aeac77b22e..b64e0a84494e 100644 --- a/executable_semantics/interpreter/interpreter.cpp +++ b/executable_semantics/interpreter/interpreter.cpp @@ -46,7 +46,7 @@ void PrintEnv(Env values, llvm::raw_ostream& out) { // State Operations // -void PrintStack(const Stack& ls, llvm::raw_ostream& out) { +void PrintStack(const Stack>& ls, llvm::raw_ostream& out) { llvm::ListSeparator sep(" :: "); for (const auto& frame : ls) { out << sep << *frame; @@ -54,7 +54,7 @@ void PrintStack(const Stack& ls, llvm::raw_ostream& out) { } auto CurrentEnv(State* state) -> Env { - Frame* frame = state->stack.Top(); + Ptr frame = state->stack.Top(); return frame->scopes.Top()->values; } @@ -209,18 +209,18 @@ void CallFunction(int line_num, std::vector operas, values.Set(name, value); params.push_back(name); } - auto* scope = global_arena->RawNew(values, params); - auto* frame = global_arena->RawNew( - fn.Name(), Stack(scope), - Stack(global_arena->RawNew(fn.Body()))); + auto scopes = Stack>(global_arena->New(values, params)); + auto todo = + Stack>(global_arena->New(fn.Body())); + auto frame = global_arena->New(fn.Name(), scopes, todo); state->stack.Push(frame); break; } case Value::Kind::StructType: { const Value* arg = CopyVal(operas[1], line_num); const Value* sv = global_arena->RawNew(operas[0], arg); - Frame* frame = state->stack.Top(); - frame->todo.Push(global_arena->RawNew(sv)); + Ptr frame = state->stack.Top(); + frame->todo.Push(global_arena->New(sv)); break; } case Value::Kind::AlternativeConstructorValue: { @@ -228,8 +228,8 @@ void CallFunction(int line_num, std::vector operas, const Value* arg = CopyVal(operas[1], line_num); const Value* av = global_arena->RawNew( alt.AltName(), alt.ChoiceName(), arg); - Frame* frame = state->stack.Top(); - frame->todo.Push(global_arena->RawNew(av)); + Ptr frame = state->stack.Top(); + frame->todo.Push(global_arena->New(av)); break; } default: @@ -238,7 +238,7 @@ void CallFunction(int line_num, std::vector operas, } } -void DeallocateScope(int line_num, Scope* scope) { +void DeallocateScope(int line_num, Ptr scope) { for (const auto& l : scope->locals) { std::optional
a = scope->values.Get(l); CHECK(a); @@ -246,14 +246,14 @@ void DeallocateScope(int line_num, Scope* scope) { } } -void DeallocateLocals(int line_num, Frame* frame) { +void DeallocateLocals(int line_num, Ptr frame) { while (!frame->scopes.IsEmpty()) { DeallocateScope(line_num, frame->scopes.Top()); frame->scopes.Pop(); } } -void CreateTuple(Frame* frame, Action* act, const Expression* exp) { +void CreateTuple(Ptr frame, Ptr act, const Expression* exp) { // { { (v1,...,vn) :: C, E, F} :: S, H} // -> { { `(v1,...,vn) :: C, E, F} :: S, H} const auto& tup_lit = cast(*exp); @@ -266,7 +266,7 @@ void CreateTuple(Frame* frame, Action* act, const Expression* exp) { const Value* tv = global_arena->RawNew(std::move(elements)); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(tv)); + frame->todo.Push(global_arena->New(tv)); } auto PatternMatch(const Value* p, const Value* v, int line_num) @@ -420,8 +420,8 @@ void PatternAssignment(const Value* pat, const Value* val, int line_num) { // State transitions for lvalues. void StepLvalue() { - Frame* frame = state->stack.Top(); - Action* act = frame->todo.Top(); + Ptr frame = state->stack.Top(); + Ptr act = frame->todo.Top(); const Expression* exp = cast(*act).Exp(); if (tracing_output) { llvm::outs() << "--- step lvalue " << *exp << " --->\n"; @@ -434,14 +434,14 @@ void StepLvalue() { cast(*exp).Name()); const Value* v = global_arena->RawNew(pointer); frame->todo.Pop(); - frame->todo.Push(global_arena->RawNew(v)); + frame->todo.Push(global_arena->New(v)); break; } case Expression::Kind::FieldAccessExpression: { if (act->Pos() == 0) { // { {e.f :: C, E, F} :: S, H} // -> { e :: [].f :: C, E, F} :: S, H} - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*exp).Aggregate())); act->IncrementPos(); } else { @@ -451,7 +451,7 @@ void StepLvalue() { Address field = aggregate.SubobjectAddress( cast(*exp).Field()); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( global_arena->RawNew(field))); } break; @@ -460,11 +460,11 @@ void StepLvalue() { if (act->Pos() == 0) { // { {e[i] :: C, E, F} :: S, H} // -> { e :: [][i] :: C, E, F} :: S, H} - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*exp).Aggregate())); act->IncrementPos(); } else if (act->Pos() == 1) { - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*exp).Offset())); act->IncrementPos(); } else if (act->Pos() == 2) { @@ -475,7 +475,7 @@ void StepLvalue() { std::to_string(cast(*act->Results()[1]).Val()); Address field = aggregate.SubobjectAddress(f); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( global_arena->RawNew(field))); } break; @@ -485,7 +485,7 @@ void StepLvalue() { // { {(f1=e1,...) :: C, E, F} :: S, H} // -> { {e1 :: (f1=[],...) :: C, E, F} :: S, H} const Expression* e1 = cast(*exp).Fields()[0].expression; - frame->todo.Push(global_arena->RawNew(e1)); + frame->todo.Push(global_arena->New(e1)); act->IncrementPos(); } else if (act->Pos() != static_cast(cast(*exp).Fields().size())) { @@ -495,7 +495,7 @@ void StepLvalue() { // H} const Expression* elt = cast(*exp).Fields()[act->Pos()].expression; - frame->todo.Push(global_arena->RawNew(elt)); + frame->todo.Push(global_arena->New(elt)); act->IncrementPos(); } else { CreateTuple(frame, act, exp); @@ -522,8 +522,8 @@ void StepLvalue() { // State transitions for expressions. void StepExp() { - Frame* frame = state->stack.Top(); - Action* act = frame->todo.Top(); + Ptr frame = state->stack.Top(); + Ptr act = frame->todo.Top(); const Expression* exp = cast(*act).Exp(); if (tracing_output) { llvm::outs() << "--- step exp " << *exp << " --->\n"; @@ -533,11 +533,11 @@ void StepExp() { if (act->Pos() == 0) { // { { e[i] :: C, E, F} :: S, H} // -> { { e :: [][i] :: C, E, F} :: S, H} - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*exp).Aggregate())); act->IncrementPos(); } else if (act->Pos() == 1) { - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*exp).Offset())); act->IncrementPos(); } else if (act->Pos() == 2) { @@ -554,7 +554,7 @@ void StepExp() { << "field " << f << " not in " << *tuple; } frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(field)); + frame->todo.Push(global_arena->New(field)); break; } default: @@ -571,7 +571,7 @@ void StepExp() { // -> { {e1 :: (f1=[],...) :: C, E, F} :: S, H} const Expression* e1 = cast(*exp).Fields()[0].expression; - frame->todo.Push(global_arena->RawNew(e1)); + frame->todo.Push(global_arena->New(e1)); act->IncrementPos(); } else { CreateTuple(frame, act, exp); @@ -584,7 +584,7 @@ void StepExp() { // H} const Expression* elt = cast(*exp).Fields()[act->Pos()].expression; - frame->todo.Push(global_arena->RawNew(elt)); + frame->todo.Push(global_arena->New(elt)); act->IncrementPos(); } else { CreateTuple(frame, act, exp); @@ -597,7 +597,7 @@ void StepExp() { // { { e.f :: C, E, F} :: S, H} // -> { { e :: [].f :: C, E, F} :: S, H} frame->todo.Push( - global_arena->RawNew(access.Aggregate())); + global_arena->New(access.Aggregate())); act->IncrementPos(); } else { // { { v :: [].f :: C, E, F} :: S, H} @@ -605,7 +605,7 @@ void StepExp() { const Value* element = act->Results()[0]->GetField( FieldPath(access.Field()), exp->LineNumber()); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(element)); + frame->todo.Push(global_arena->New(element)); } break; } @@ -616,21 +616,21 @@ void StepExp() { Address pointer = GetFromEnv(exp->LineNumber(), ident.Name()); const Value* pointee = state->heap.Read(pointer, exp->LineNumber()); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(pointee)); + frame->todo.Push(global_arena->New(pointee)); break; } case Expression::Kind::IntLiteral: CHECK(act->Pos() == 0); // { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H} frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( global_arena->RawNew(cast(*exp).Val()))); break; case Expression::Kind::BoolLiteral: CHECK(act->Pos() == 0); // { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H} frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( global_arena->RawNew(cast(*exp).Val()))); break; case Expression::Kind::PrimitiveOperatorExpression: { @@ -639,14 +639,14 @@ void StepExp() { // { {v :: op(vs,[],e,es) :: C, E, F} :: S, H} // -> { {e :: op(vs,v,[],es) :: C, E, F} :: S, H} const Expression* arg = op.Arguments()[act->Pos()]; - frame->todo.Push(global_arena->RawNew(arg)); + frame->todo.Push(global_arena->New(arg)); act->IncrementPos(); } else { // { {v :: op(vs,[]) :: C, E, F} :: S, H} // -> { {eval_prim(op, (vs,v)) :: C, E, F} :: S, H} const Value* v = EvalPrim(op.Op(), act->Results(), exp->LineNumber()); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(v)); + frame->todo.Push(global_arena->New(v)); } break; } @@ -654,13 +654,13 @@ void StepExp() { if (act->Pos() == 0) { // { {e1(e2) :: C, E, F} :: S, H} // -> { {e1 :: [](e2) :: C, E, F} :: S, H} - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*exp).Function())); act->IncrementPos(); } else if (act->Pos() == 1) { // { { v :: [](e) :: C, E, F} :: S, H} // -> { { e :: v([]) :: C, E, F} :: S, H} - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*exp).Argument())); act->IncrementPos(); } else if (act->Pos() == 2) { @@ -683,8 +683,7 @@ void StepExp() { CHECK(pointee->Tag() == Value::Kind::StringValue); // TODO: This could eventually use something like llvm::formatv. llvm::outs() << cast(*pointee).Val(); - frame->todo.Push( - global_arena->RawNew(&TupleValue::Empty())); + frame->todo.Push(global_arena->New(&TupleValue::Empty())); break; } break; @@ -693,32 +692,32 @@ void StepExp() { CHECK(act->Pos() == 0); const Value* v = global_arena->RawNew(); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(v)); + frame->todo.Push(global_arena->New(v)); break; } case Expression::Kind::BoolTypeLiteral: { CHECK(act->Pos() == 0); const Value* v = global_arena->RawNew(); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(v)); + frame->todo.Push(global_arena->New(v)); break; } case Expression::Kind::TypeTypeLiteral: { CHECK(act->Pos() == 0); const Value* v = global_arena->RawNew(); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(v)); + frame->todo.Push(global_arena->New(v)); break; } case Expression::Kind::FunctionTypeLiteral: { if (act->Pos() == 0) { - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*exp).Parameter())); act->IncrementPos(); } else if (act->Pos() == 1) { // { { pt :: fn [] -> e :: C, E, F} :: S, H} // -> { { e :: fn pt -> []) :: C, E, F} :: S, H} - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*exp).ReturnType())); act->IncrementPos(); } else if (act->Pos() == 2) { @@ -728,7 +727,7 @@ void StepExp() { std::vector(), act->Results()[0], act->Results()[1]); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(v)); + frame->todo.Push(global_arena->New(v)); } break; } @@ -736,29 +735,29 @@ void StepExp() { CHECK(act->Pos() == 0); const Value* v = global_arena->RawNew(); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(v)); + frame->todo.Push(global_arena->New(v)); break; } case Expression::Kind::StringLiteral: CHECK(act->Pos() == 0); // { {n :: C, E, F} :: S, H} -> { {n' :: C, E, F} :: S, H} frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( global_arena->RawNew(cast(*exp).Val()))); break; case Expression::Kind::StringTypeLiteral: { CHECK(act->Pos() == 0); const Value* v = global_arena->RawNew(); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(v)); + frame->todo.Push(global_arena->New(v)); break; } } // switch (exp->Tag) } void StepPattern() { - Frame* frame = state->stack.Top(); - Action* act = frame->todo.Top(); + Ptr frame = state->stack.Top(); + Ptr act = frame->todo.Top(); const Pattern* pattern = cast(*act).Pat(); if (tracing_output) { llvm::outs() << "--- step pattern " << *pattern << " --->\n"; @@ -768,19 +767,19 @@ void StepPattern() { CHECK(act->Pos() == 0); const Value* v = global_arena->RawNew(); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(v)); + frame->todo.Push(global_arena->New(v)); break; } case Pattern::Kind::BindingPattern: { const auto& binding = cast(*pattern); if (act->Pos() == 0) { - frame->todo.Push(global_arena->RawNew(binding.Type())); + frame->todo.Push(global_arena->New(binding.Type())); act->IncrementPos(); } else { auto v = global_arena->RawNew( binding.Name(), act->Results()[0]); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(v)); + frame->todo.Push(global_arena->New(v)); } break; } @@ -789,11 +788,10 @@ void StepPattern() { if (act->Pos() == 0) { if (tuple.Fields().empty()) { frame->todo.Pop(1); - frame->todo.Push( - global_arena->RawNew(&TupleValue::Empty())); + frame->todo.Push(global_arena->New(&TupleValue::Empty())); } else { const Pattern* p1 = tuple.Fields()[0].pattern; - frame->todo.Push(global_arena->RawNew(p1)); + frame->todo.Push(global_arena->New(p1)); act->IncrementPos(); } } else if (act->Pos() != static_cast(tuple.Fields().size())) { @@ -802,7 +800,7 @@ void StepPattern() { // -> { { ek+1 :: (f1=v1,..., fk=vk, fk+1=[],...) :: C, E, F} :: S, // H} const Pattern* elt = tuple.Fields()[act->Pos()].pattern; - frame->todo.Push(global_arena->RawNew(elt)); + frame->todo.Push(global_arena->New(elt)); act->IncrementPos(); } else { std::vector elements; @@ -813,7 +811,7 @@ void StepPattern() { const Value* tuple_value = global_arena->RawNew(std::move(elements)); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew(tuple_value)); + frame->todo.Push(global_arena->New(tuple_value)); } break; } @@ -821,18 +819,18 @@ void StepPattern() { const auto& alternative = cast(*pattern); if (act->Pos() == 0) { frame->todo.Push( - global_arena->RawNew(alternative.ChoiceType())); + global_arena->New(alternative.ChoiceType())); act->IncrementPos(); } else if (act->Pos() == 1) { frame->todo.Push( - global_arena->RawNew(alternative.Arguments())); + global_arena->New(alternative.Arguments())); act->IncrementPos(); } else { CHECK(act->Pos() == 2); const auto& choice_type = cast(*act->Results()[0]); frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew( - global_arena->RawNew( + frame->todo.Push( + global_arena->New(global_arena->RawNew( alternative.AlternativeName(), choice_type.Name(), act->Results()[1]))); } @@ -840,13 +838,13 @@ void StepPattern() { } case Pattern::Kind::ExpressionPattern: frame->todo.Pop(1); - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(pattern)->Expression())); break; } } -auto IsWhileAct(Action* act) -> bool { +auto IsWhileAct(Ptr act) -> bool { switch (act->Tag()) { case Action::Kind::StatementAction: switch (cast(*act).Stmt()->Tag()) { @@ -860,7 +858,7 @@ auto IsWhileAct(Action* act) -> bool { } } -auto IsBlockAct(Action* act) -> bool { +auto IsBlockAct(Ptr act) -> bool { switch (act->Tag()) { case Action::Kind::StatementAction: switch (cast(*act).Stmt()->Tag()) { @@ -877,8 +875,8 @@ auto IsBlockAct(Action* act) -> bool { // State transitions for statements. void StepStmt() { - Frame* frame = state->stack.Top(); - Action* act = frame->todo.Top(); + Ptr frame = state->stack.Top(); + Ptr act = frame->todo.Top(); const Statement* stmt = cast(*act).Stmt(); CHECK(stmt != nullptr) << "null statement!"; if (tracing_output) { @@ -892,7 +890,7 @@ void StepStmt() { // { { (match (e) ...) :: C, E, F} :: S, H} // -> { { e :: (match ([]) ...) :: C, E, F} :: S, H} frame->todo.Push( - global_arena->RawNew(cast(*stmt).Exp())); + global_arena->New(cast(*stmt).Exp())); act->IncrementPos(); } else { // Regarding act->Pos(): @@ -917,7 +915,7 @@ void StepStmt() { // start interpreting the pattern of the clause // { {v :: (match ([]) ...) :: C, E, F} :: S, H} // -> { {pi :: (match ([]) ...) :: C, E, F} :: S, H} - frame->todo.Push(global_arena->RawNew(c->first)); + frame->todo.Push(global_arena->New(c->first)); act->IncrementPos(); } else { // try to match auto v = act->Results()[0]; @@ -930,16 +928,14 @@ void StepStmt() { values.Set(name, value); vars.push_back(name); } - auto* new_scope = global_arena->RawNew(values, vars); - frame->scopes.Push(new_scope); + frame->scopes.Push(global_arena->New(values, vars)); const Statement* body_block = global_arena->RawNew(stmt->LineNumber(), c->second); - Action* body_act = - global_arena->RawNew(body_block); + auto body_act = global_arena->New(body_block); body_act->IncrementPos(); frame->todo.Pop(1); frame->todo.Push(body_act); - frame->todo.Push(global_arena->RawNew(c->second)); + frame->todo.Push(global_arena->New(c->second)); } else { // this case did not match, moving on act->IncrementPos(); @@ -957,14 +953,14 @@ void StepStmt() { // { { (while (e) s) :: C, E, F} :: S, H} // -> { { e :: (while ([]) s) :: C, E, F} :: S, H} frame->todo.Push( - global_arena->RawNew(cast(*stmt).Cond())); + global_arena->New(cast(*stmt).Cond())); act->IncrementPos(); } else if (cast(*act->Results()[0]).Val()) { // { {true :: (while ([]) s) :: C, E, F} :: S, H} // -> { { s :: (while (e) s) :: C, E, F } :: S, H} frame->todo.Top()->Clear(); frame->todo.Push( - global_arena->RawNew(cast(*stmt).Body())); + global_arena->New(cast(*stmt).Body())); } else { // { {false :: (while ([]) s) :: C, E, F} :: S, H} // -> { { C, E, F } :: S, H} @@ -1002,18 +998,16 @@ void StepStmt() { case Statement::Kind::Block: { if (act->Pos() == 0) { if (cast(*stmt).Stmt()) { - auto* scope = global_arena->RawNew(CurrentEnv(state), - std::list()); - frame->scopes.Push(scope); + frame->scopes.Push(global_arena->New(CurrentEnv(state))); frame->todo.Push( - global_arena->RawNew(cast(*stmt).Stmt())); + global_arena->New(cast(*stmt).Stmt())); act->IncrementPos(); act->IncrementPos(); } else { frame->todo.Pop(); } } else { - Scope* scope = frame->scopes.Top(); + Ptr scope = frame->scopes.Top(); DeallocateScope(stmt->LineNumber(), scope); frame->scopes.Pop(1); frame->todo.Pop(1); @@ -1024,11 +1018,11 @@ void StepStmt() { if (act->Pos() == 0) { // { {(var x = e) :: C, E, F} :: S, H} // -> { {e :: (var x = []) :: C, E, F} :: S, H} - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*stmt).Init())); act->IncrementPos(); } else if (act->Pos() == 1) { - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*stmt).Pat())); act->IncrementPos(); } else if (act->Pos() == 2) { @@ -1052,7 +1046,7 @@ void StepStmt() { if (act->Pos() == 0) { // { {e :: C, E, F} :: S, H} // -> { {e :: C, E, F} :: S, H} - frame->todo.Push(global_arena->RawNew( + frame->todo.Push(global_arena->New( cast(*stmt).Exp())); act->IncrementPos(); } else { @@ -1064,13 +1058,13 @@ void StepStmt() { // { {(lv = e) :: C, E, F} :: S, H} // -> { {lv :: ([] = e) :: C, E, F} :: S, H} frame->todo.Push( - global_arena->RawNew(cast(*stmt).Lhs())); + global_arena->New(cast(*stmt).Lhs())); act->IncrementPos(); } else if (act->Pos() == 1) { // { { a :: ([] = e) :: C, E, F} :: S, H} // -> { { e :: (a = []) :: C, E, F} :: S, H} frame->todo.Push( - global_arena->RawNew(cast(*stmt).Rhs())); + global_arena->New(cast(*stmt).Rhs())); act->IncrementPos(); } else if (act->Pos() == 2) { // { { v :: (a = []) :: C, E, F} :: S, H} @@ -1086,7 +1080,7 @@ void StepStmt() { // { {(if (e) then_stmt else else_stmt) :: C, E, F} :: S, H} // -> { { e :: (if ([]) then_stmt else else_stmt) :: C, E, F} :: S, H} frame->todo.Push( - global_arena->RawNew(cast(*stmt).Cond())); + global_arena->New(cast(*stmt).Cond())); act->IncrementPos(); } else if (cast(*act->Results()[0]).Val()) { // { {true :: if ([]) then_stmt else else_stmt :: C, E, F} :: @@ -1094,14 +1088,14 @@ void StepStmt() { // -> { { then_stmt :: C, E, F } :: S, H} frame->todo.Pop(1); frame->todo.Push( - global_arena->RawNew(cast(*stmt).ThenStmt())); + global_arena->New(cast(*stmt).ThenStmt())); } else if (cast(*stmt).ElseStmt()) { // { {false :: if ([]) then_stmt else else_stmt :: C, E, F} :: // S, H} // -> { { else_stmt :: C, E, F } :: S, H} frame->todo.Pop(1); frame->todo.Push( - global_arena->RawNew(cast(*stmt).ElseStmt())); + global_arena->New(cast(*stmt).ElseStmt())); } else { frame->todo.Pop(1); } @@ -1111,7 +1105,7 @@ void StepStmt() { // { {return e :: C, E, F} :: S, H} // -> { {e :: return [] :: C, E, F} :: S, H} frame->todo.Push( - global_arena->RawNew(cast(*stmt).Exp())); + global_arena->New(cast(*stmt).Exp())); act->IncrementPos(); } else { // { {v :: return [] :: C, E, F} :: {C', E', F'} :: S, H} @@ -1120,7 +1114,7 @@ void StepStmt() { DeallocateLocals(stmt->LineNumber(), frame); state->stack.Pop(1); frame = state->stack.Top(); - frame->todo.Push(global_arena->RawNew(ret_val)); + frame->todo.Push(global_arena->New(ret_val)); } break; case Statement::Kind::Sequence: @@ -1129,31 +1123,29 @@ void StepStmt() { // -> { { s1 :: s2 :: C, E, F} :: S, H} frame->todo.Pop(1); if (cast(*stmt).Next()) { - frame->todo.Push(global_arena->RawNew( - cast(*stmt).Next())); + frame->todo.Push( + global_arena->New(cast(*stmt).Next())); } frame->todo.Push( - global_arena->RawNew(cast(*stmt).Stmt())); + global_arena->New(cast(*stmt).Stmt())); break; case Statement::Kind::Continuation: { CHECK(act->Pos() == 0); // Create a continuation object by creating a frame similar the // way one is created in a function call. - Scope* scope = global_arena->RawNew(CurrentEnv(state), - std::list()); - Stack scopes; - scopes.Push(scope); - Stack todo; - todo.Push(global_arena->RawNew( + auto scopes = + Stack>(global_arena->New(CurrentEnv(state))); + Stack> todo; + todo.Push(global_arena->New( global_arena->RawNew(stmt->LineNumber(), nullptr, /*is_omitted_exp=*/true))); - todo.Push(global_arena->RawNew( - cast(*stmt).Body())); - Frame* continuation_frame = - global_arena->RawNew("__continuation", scopes, todo); + todo.Push( + global_arena->New(cast(*stmt).Body())); + auto continuation_frame = + global_arena->New("__continuation", scopes, todo); Address continuation_address = state->heap.AllocateValue(global_arena->RawNew( - std::vector({continuation_frame}))); + std::vector>({continuation_frame}))); // Store the continuation's address in the frame. continuation_frame->continuation = continuation_address; // Bind the continuation object to the continuation variable @@ -1167,20 +1159,20 @@ void StepStmt() { case Statement::Kind::Run: if (act->Pos() == 0) { // Evaluate the argument of the run statement. - frame->todo.Push(global_arena->RawNew( - cast(*stmt).Argument())); + frame->todo.Push( + global_arena->New(cast(*stmt).Argument())); act->IncrementPos(); } else { frame->todo.Pop(1); // Push an expression statement action to ignore the result // value from the continuation. - Action* ignore_result = global_arena->RawNew( + auto ignore_result = global_arena->New( global_arena->RawNew( stmt->LineNumber(), global_arena->RawNew(stmt->LineNumber()))); frame->todo.Push(ignore_result); // Push the continuation onto the current stack. - const std::vector& continuation_vector = + const std::vector>& continuation_vector = cast(*act->Results()[0]).Stack(); for (auto frame_iter = continuation_vector.rbegin(); frame_iter != continuation_vector.rend(); ++frame_iter) { @@ -1192,7 +1184,7 @@ void StepStmt() { CHECK(act->Pos() == 0); // Pause the current continuation frame->todo.Pop(); - std::vector paused; + std::vector> paused; do { paused.push_back(state->stack.Pop()); } while (paused.back()->continuation == std::nullopt); @@ -1206,17 +1198,17 @@ void StepStmt() { // State transition. void Step() { - Frame* frame = state->stack.Top(); + Ptr frame = state->stack.Top(); if (frame->todo.IsEmpty()) { FATAL_RUNTIME_ERROR_NO_LINE() << "fell off end of function " << frame->name << " without `return`"; } - Action* act = frame->todo.Top(); + Ptr act = frame->todo.Top(); switch (act->Tag()) { case Action::Kind::ValAction: { const ValAction& val_act = cast(*frame->todo.Pop()); - Action* act = frame->todo.Top(); + Ptr act = frame->todo.Top(); act->AddResult(val_act.Val()); break; } @@ -1246,10 +1238,11 @@ auto InterpProgram(const std::list>& fs) -> int { const Expression* arg = global_arena->RawNew(0); const Expression* call_main = global_arena->RawNew( 0, global_arena->RawNew(0, "main"), arg); - auto todo = Stack(global_arena->RawNew(call_main)); - auto* scope = global_arena->RawNew(globals, std::list()); - auto* frame = global_arena->RawNew("top", Stack(scope), todo); - state->stack = Stack(frame); + auto todo = + Stack>(global_arena->New(call_main)); + auto scopes = Stack>(global_arena->New(globals)); + state->stack = + Stack>(global_arena->New("top", scopes, todo)); if (tracing_output) { llvm::outs() << "********** calling main function **********\n"; @@ -1269,10 +1262,10 @@ auto InterpProgram(const std::list>& fs) -> int { // Interpret an expression at compile-time. auto InterpExp(Env values, const Expression* e) -> const Value* { - auto todo = Stack(global_arena->RawNew(e)); - auto* scope = global_arena->RawNew(values, std::list()); - auto* frame = global_arena->RawNew("InterpExp", Stack(scope), todo); - state->stack = Stack(frame); + auto todo = Stack>(global_arena->New(e)); + auto scopes = Stack>(global_arena->New(values)); + state->stack = + Stack>(global_arena->New("InterpExp", scopes, todo)); while (state->stack.Count() > 1 || state->stack.Top()->todo.Count() > 1 || state->stack.Top()->todo.Top()->Tag() != Action::Kind::ValAction) { @@ -1283,11 +1276,10 @@ auto InterpExp(Env values, const Expression* e) -> const Value* { // Interpret a pattern at compile-time. auto InterpPattern(Env values, const Pattern* p) -> const Value* { - auto todo = Stack(global_arena->RawNew(p)); - auto* scope = global_arena->RawNew(values, std::list()); - auto* frame = - global_arena->RawNew("InterpPattern", Stack(scope), todo); - state->stack = Stack(frame); + auto todo = Stack>(global_arena->New(p)); + auto scopes = Stack>(global_arena->New(values)); + state->stack = Stack>( + global_arena->New("InterpPattern", scopes, todo)); while (state->stack.Count() > 1 || state->stack.Top()->todo.Count() > 1 || state->stack.Top()->todo.Top()->Tag() != Action::Kind::ValAction) { diff --git a/executable_semantics/interpreter/interpreter.h b/executable_semantics/interpreter/interpreter.h index 395bfcc73e04..73cfa1c4d688 100644 --- a/executable_semantics/interpreter/interpreter.h +++ b/executable_semantics/interpreter/interpreter.h @@ -23,7 +23,7 @@ namespace Carbon { using Env = Dictionary; struct State { - Stack stack; + Stack> stack; Heap heap; }; diff --git a/executable_semantics/interpreter/stack.h b/executable_semantics/interpreter/stack.h index 023aba4008b9..2ed05db79fa0 100644 --- a/executable_semantics/interpreter/stack.h +++ b/executable_semantics/interpreter/stack.h @@ -22,8 +22,6 @@ struct Stack { Stack() = default; // Creates an instance containing just `x`. - // TODO: consider removing this. It's somewhat unconventional, and the - // callsite readability is debatable. explicit Stack(T x) : Stack() { Push(std::move(x)); } // Pushes `x` onto the top of the stack. @@ -46,7 +44,7 @@ struct Stack { CHECK(n >= 0) << "Negative pop count disallowed."; CHECK(static_cast(n) <= elements.size()) << "Can only pop as many elements as stack has."; - elements.resize(elements.size() - n); + elements.erase(elements.end() - n, elements.end()); } // Returns the top element of the stack. @@ -71,11 +69,6 @@ struct Stack { std::vector elements; }; -// Explicitly enable CTAD to silence warnings. -// TODO: consider removing this (and perhaps the associated constructor). -template -Stack(T x) -> Stack; - } // namespace Carbon #endif // EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_ diff --git a/executable_semantics/interpreter/value.h b/executable_semantics/interpreter/value.h index b2c98392fe7c..553ce43775e3 100644 --- a/executable_semantics/interpreter/value.h +++ b/executable_semantics/interpreter/value.h @@ -14,6 +14,7 @@ #include "common/ostream.h" #include "executable_semantics/ast/function_definition.h" #include "executable_semantics/ast/statement.h" +#include "executable_semantics/common/ptr.h" #include "executable_semantics/interpreter/address.h" #include "executable_semantics/interpreter/field_path.h" #include "executable_semantics/interpreter/stack.h" @@ -431,17 +432,17 @@ class VariableType : public Value { // A first-class continuation representation of a fragment of the stack. class ContinuationValue : public Value { public: - explicit ContinuationValue(std::vector stack) + explicit ContinuationValue(std::vector> stack) : Value(Kind::ContinuationValue), stack(std::move(stack)) {} static auto classof(const Value* value) -> bool { return value->Tag() == Kind::ContinuationValue; } - auto Stack() const -> const std::vector& { return stack; } + auto Stack() const -> const std::vector>& { return stack; } private: - std::vector stack; + std::vector> stack; }; // The String type.