Commit 06359f74 authored by Benedikt Meurer's avatar Benedikt Meurer Committed by Commit Bot

[debug] Don't put a source position on internal `Return`s.

Be explicit about source positions for `Return`s in the
BytecodeGenerator, and only do self-healing explicitly in the
`ReturnStatement` translation, where an end position of
`kNoSourcePosition` is turned into the return position of the
function literal.

This allows us to reason more easily about which `Return`s actually
receive a meaningful source position, and in particular it allows us
to construct the internal `Return`s for `yield` and `yield*` with no
source position attached to them. Previously they'd get the source
position for the implicit (final) return attached to it, which confused
the debugger and led to breakpoints being set in the completely wrong
spot.

Considering the simplified example

```
function* foo(){
  var a = 1;
}
```

this would previously generate the following bytecode

```
        0 : SwitchOnGeneratorState r0, [0], [1] { 0: @20 }
        4 : Mov <closure>, r2
        7 : Mov <this>, r3
 13 E> 10 : InvokeIntrinsic [_CreateJSGeneratorObject], r2-r3
       14 : Star0
 13 E> 15 : SuspendGenerator r0, r0-r1, [0]
       20 : ResumeGenerator r0, r0-r1
       24 : Star2
       25 : InvokeIntrinsic [_GeneratorGetResumeMode], r0-r0
       29 : SwitchOnSmiNoFeedback [1], [2], [0] { 0: @39, 1: @36 }
       33 : Ldar r2
 13 E> 35 : Throw
       36 : Ldar r2
 30 S> 38 : Return    <=========================== internal Return
 27 S> 39 : LdaSmi [1]
       41 : Star1
       42 : LdaUndefined
 30 S> 43 : Return
```

where everything between offset 4 and 42 corresponds to the implicit
yield at the beginning of every generator function, in particular the
code between 20 and 42 corresponds to that initial yields resumption
logic. Notice how the internal Return at offset 38 gets assigned the
source position of the function literal (the same as the implicit
return at the end). This confuses the debugger quite a bit when trying
to set a breakpoint on the closing brace, since it's going in bytecode
order and will thus discover the `Return` at offset 38 first (matching
the source position 30 it's currently looking for) and setting the
breakpoint there. This `Return` bytecode however is only executed when
the generator is resumed via `GeneratorPrototype.return()`, and it'll
not hit when the developer uses the generator normally, which is not
the desired behavior and extremely confusing (especially since stepping
on the other hand works as expected).

With this patch, we no longer slap a source position (and in particular
not the function literal's return position) onto these internal
`Return`s as you can see from the generated bytecode below:

```
       0 : SwitchOnGeneratorState r0, [0], [1] { 0: @20 }
       4 : Mov <closure>, r2
       7 : Mov <this>, r3
13 E> 10 : InvokeIntrinsic [_CreateJSGeneratorObject], r2-r3
      14 : Star0
13 E> 15 : SuspendGenerator r0, r0-r1, [0]
      20 : ResumeGenerator r0, r0-r1
      24 : Star2
      25 : InvokeIntrinsic [_GeneratorGetResumeMode], r0-r0
      29 : SwitchOnSmiNoFeedback [1], [2], [0] { 0: @39, 1: @36 }
      33 : Ldar r2
13 E> 35 : Throw
      36 : Ldar r2
      38 : Return
27 S> 39 : LdaSmi [1]
      41 : Star1
      42 : LdaUndefined
30 S> 43 : Return
```

This also allows us to remove the break position finding hack that was
kept in BreakIterator::BreakIndexFromPosition() for generators and
modules.

Fixed: chromium:901819
Change-Id: If19a6b26e2622d49b6b5e54bf7a162747543f970
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2727820Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73119}
parent be5ccde2
......@@ -216,29 +216,6 @@ BreakIterator::BreakIterator(Handle<DebugInfo> debug_info)
}
int BreakIterator::BreakIndexFromPosition(int source_position) {
// TODO(crbug.com/901819): When there's no exact match, we
// should always pick the first match (in execution order)
// to ensure that when setting a breakpoint on a line, we
// really break as early as possible in that line. With
// generators that's currently broken because of the way
// the implicit yield is handled, this will be fixed in
// a follow up CL.
if (IsGeneratorFunction(debug_info_->shared().kind()) ||
IsModule(debug_info_->shared().kind())) {
int distance = kMaxInt;
int closest_break = break_index();
while (!Done()) {
int next_position = position();
if (source_position <= next_position &&
next_position - source_position < distance) {
closest_break = break_index();
distance = next_position - source_position;
if (distance == 0) break;
}
Next();
}
return closest_break;
}
int first_break = break_index();
bool first = true;
while (!Done()) {
......
......@@ -508,8 +508,12 @@ class V8_EXPORT_PRIVATE BytecodeArrayBuilder final {
void InitializeReturnPosition(FunctionLiteral* literal);
void SetStatementPosition(Statement* stmt) {
if (stmt->position() == kNoSourcePosition) return;
latest_source_info_.MakeStatementPosition(stmt->position());
SetStatementPosition(stmt->position());
}
void SetStatementPosition(int position) {
if (position == kNoSourcePosition) return;
latest_source_info_.MakeStatementPosition(position);
}
void SetExpressionPosition(Expression* expr) {
......@@ -526,16 +530,7 @@ class V8_EXPORT_PRIVATE BytecodeArrayBuilder final {
}
void SetExpressionAsStatementPosition(Expression* expr) {
if (expr->position() == kNoSourcePosition) return;
latest_source_info_.MakeStatementPosition(expr->position());
}
void SetReturnPosition(int source_position, FunctionLiteral* literal) {
if (source_position != kNoSourcePosition) {
latest_source_info_.MakeStatementPosition(source_position);
} else if (literal->return_position() != kNoSourcePosition) {
latest_source_info_.MakeStatementPosition(literal->return_position());
}
SetStatementPosition(expr->position());
}
bool RemainderOfBlockIsDead() const {
......
......@@ -10,6 +10,7 @@
#include "src/builtins/builtins-constructor.h"
#include "src/codegen/compiler.h"
#include "src/codegen/unoptimized-compilation-info.h"
#include "src/common/globals.h"
#include "src/interpreter/bytecode-flags.h"
#include "src/interpreter/bytecode-jump-table.h"
#include "src/interpreter/bytecode-label.h"
......@@ -122,10 +123,10 @@ class V8_NODISCARD BytecodeGenerator::ControlScope {
void Continue(Statement* stmt) {
PerformCommand(CMD_CONTINUE, stmt, kNoSourcePosition);
}
void ReturnAccumulator(int source_position = kNoSourcePosition) {
void ReturnAccumulator(int source_position) {
PerformCommand(CMD_RETURN, nullptr, source_position);
}
void AsyncReturnAccumulator(int source_position = kNoSourcePosition) {
void AsyncReturnAccumulator(int source_position) {
PerformCommand(CMD_ASYNC_RETURN, nullptr, source_position);
}
......@@ -1458,7 +1459,7 @@ void BytecodeGenerator::GenerateBytecodeBody() {
// end of the function without an explicit return being present on all paths.
if (!builder()->RemainderOfBlockIsDead()) {
builder()->LoadUndefined();
BuildReturn();
BuildReturn(literal->return_position());
}
}
......@@ -1772,10 +1773,14 @@ void BytecodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
AllocateBlockCoverageSlotIfEnabled(stmt, SourceRangeKind::kContinuation);
builder()->SetStatementPosition(stmt);
VisitForAccumulatorValue(stmt->expression());
int return_position = stmt->end_position();
if (return_position == kNoSourcePosition) {
return_position = info()->literal()->return_position();
}
if (stmt->is_async_return()) {
execution_control()->AsyncReturnAccumulator(stmt->end_position());
execution_control()->AsyncReturnAccumulator(return_position);
} else {
execution_control()->ReturnAccumulator(stmt->end_position());
execution_control()->ReturnAccumulator(return_position);
}
}
......@@ -3307,7 +3312,7 @@ void BytecodeGenerator::BuildReturn(int source_position) {
if (info()->flags().collect_type_profile()) {
builder()->CollectTypeProfile(info()->literal()->return_position());
}
builder()->SetReturnPosition(source_position, info()->literal());
builder()->SetStatementPosition(source_position);
builder()->Return();
}
......@@ -4394,9 +4399,9 @@ void BytecodeGenerator::VisitYield(Yield* expr) {
builder()->Bind(jump_table, JSGeneratorObject::kReturn);
builder()->LoadAccumulatorWithRegister(input);
if (IsAsyncGeneratorFunction(function_kind())) {
execution_control()->AsyncReturnAccumulator();
execution_control()->AsyncReturnAccumulator(kNoSourcePosition);
} else {
execution_control()->ReturnAccumulator();
execution_control()->ReturnAccumulator(kNoSourcePosition);
}
}
......@@ -4546,9 +4551,9 @@ void BytecodeGenerator::VisitYieldStar(YieldStar* expr) {
if (iterator_type == IteratorType::kAsync) {
// Await input.
BuildAwait(expr->position());
execution_control()->AsyncReturnAccumulator();
execution_control()->AsyncReturnAccumulator(kNoSourcePosition);
} else {
execution_control()->ReturnAccumulator();
execution_control()->ReturnAccumulator(kNoSourcePosition);
}
}
......@@ -4638,9 +4643,9 @@ void BytecodeGenerator::VisitYieldStar(YieldStar* expr) {
.JumpIfFalse(ToBooleanMode::kAlreadyBoolean, &completion_is_output_value)
.LoadAccumulatorWithRegister(output_value);
if (iterator_type == IteratorType::kAsync) {
execution_control()->AsyncReturnAccumulator();
execution_control()->AsyncReturnAccumulator(kNoSourcePosition);
} else {
execution_control()->ReturnAccumulator();
execution_control()->ReturnAccumulator(kNoSourcePosition);
}
builder()->Bind(&completion_is_output_value);
......
......@@ -252,8 +252,8 @@ class BytecodeGenerator final : public AstVisitor<BytecodeGenerator> {
LookupHoistingMode lookup_hoisting_mode = LookupHoistingMode::kNormal);
void BuildLiteralCompareNil(Token::Value compare_op,
BytecodeArrayBuilder::NilValue nil);
void BuildReturn(int source_position = kNoSourcePosition);
void BuildAsyncReturn(int source_position = kNoSourcePosition);
void BuildReturn(int source_position);
void BuildAsyncReturn(int source_position);
void BuildAsyncGeneratorReturn();
void BuildReThrow();
void BuildHoleCheckForVariableAssignment(Variable* variable, Token::Value op);
......
......@@ -75,9 +75,9 @@ bytecodes: [
B(Mov), R(0), R(4),
B(Mov), R(2), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncGeneratorResolve), R(4), U8(3),
/* 22 S> */ B(Return),
B(Return),
B(Ldar), R(2),
/* 22 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 22 S> */ B(Return),
]
......@@ -180,9 +180,9 @@ bytecodes: [
B(Mov), R(0), R(4),
B(Mov), R(2), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncGeneratorResolve), R(4), U8(3),
/* 31 S> */ B(Return),
B(Return),
B(Ldar), R(2),
/* 31 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 31 S> */ B(Return),
]
......@@ -352,9 +352,9 @@ bytecodes: [
B(Mov), R(0), R(7),
B(Mov), R(5), R(8),
B(InvokeIntrinsic), U8(Runtime::k_AsyncGeneratorResolve), R(7), U8(3),
/* 50 S> */ B(Return),
B(Return),
B(Ldar), R(5),
/* 50 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 50 S> */ B(Return),
]
......@@ -569,9 +569,9 @@ bytecodes: [
B(Mov), R(0), R(4),
B(Mov), R(2), R(5),
B(InvokeIntrinsic), U8(Runtime::k_AsyncGeneratorResolve), R(4), U8(3),
/* 60 S> */ B(Return),
B(Return),
B(Ldar), R(2),
/* 60 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 60 S> */ B(Return),
]
......
......@@ -29,7 +29,7 @@ bytecodes: [
B(Ldar), R(2),
/* 0 E> */ B(Throw),
B(Ldar), R(2),
/* 10 S> */ B(Return),
B(Return),
B(Mov), R(2), R(1),
B(Ldar), R(1),
B(Mov), R(context), R(2),
......@@ -101,7 +101,7 @@ bytecodes: [
B(Ldar), R(2),
/* 0 E> */ B(Throw),
B(Ldar), R(2),
/* 21 S> */ B(Return),
B(Return),
B(Mov), R(2), R(1),
B(Ldar), R(1),
B(Mov), R(context), R(2),
......@@ -184,7 +184,7 @@ bytecodes: [
B(Ldar), R(3),
/* 0 E> */ B(Throw),
B(Ldar), R(3),
/* 54 S> */ B(Return),
B(Return),
B(Mov), R(3), R(2),
B(Ldar), R(2),
B(Mov), R(context), R(3),
......@@ -264,7 +264,7 @@ bytecodes: [
B(Ldar), R(3),
/* 0 E> */ B(Throw),
B(Ldar), R(3),
/* 49 S> */ B(Return),
B(Return),
B(Mov), R(3), R(2),
B(Ldar), R(2),
B(Mov), R(context), R(3),
......
......@@ -282,7 +282,7 @@ bytecodes: [
B(Mov), R(0), R(14),
B(Mov), R(9), R(15),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionResolve), R(14), U8(3),
/* 68 S> */ B(Return),
B(Return),
B(LdaUndefined),
B(Star6),
B(LdaTrue),
......@@ -578,7 +578,7 @@ bytecodes: [
B(Mov), R(0), R(11),
B(Mov), R(7), R(12),
B(InvokeIntrinsic), U8(Runtime::k_AsyncFunctionResolve), R(11), U8(3),
/* 96 S> */ B(Return),
B(Return),
B(LdaUndefined),
B(Star4),
B(LdaFalse),
......
......@@ -162,7 +162,7 @@ bytecodes: [
B(Ldar), R(7),
B(ReThrow),
B(Ldar), R(7),
/* 85 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 85 S> */ B(Return),
]
......@@ -351,7 +351,7 @@ bytecodes: [
B(Ldar), R(5),
B(ReThrow),
B(Ldar), R(5),
/* 105 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 105 S> */ B(Return),
]
......
......@@ -429,7 +429,7 @@ bytecodes: [
B(Ldar), R(5),
/* 11 E> */ B(Throw),
B(Ldar), R(5),
/* 55 S> */ B(Return),
B(Return),
/* 35 S> */ B(GetIterator), R(arg0), U8(0), U8(2),
B(JumpIfJSReceiver), U8(7),
B(CallRuntime), U16(Runtime::kThrowSymbolIteratorInvalid), R(0), U8(0),
......@@ -531,7 +531,7 @@ bytecodes: [
B(Ldar), R(4),
/* 11 E> */ B(Throw),
B(Ldar), R(4),
/* 49 S> */ B(Return),
B(Return),
/* 35 S> */ B(GetIterator), R(arg0), U8(0), U8(2),
B(JumpIfJSReceiver), U8(7),
B(CallRuntime), U16(Runtime::kThrowSymbolIteratorInvalid), R(0), U8(0),
......@@ -607,7 +607,7 @@ bytecodes: [
B(Ldar), R(8),
B(ReThrow),
B(Ldar), R(8),
/* 49 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 49 S> */ B(Return),
]
......
......@@ -28,7 +28,7 @@ bytecodes: [
B(Ldar), R(1),
/* 11 E> */ B(Throw),
B(Ldar), R(1),
/* 16 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 16 S> */ B(Return),
]
......@@ -62,7 +62,7 @@ bytecodes: [
B(Ldar), R(1),
/* 11 E> */ B(Throw),
B(Ldar), R(1),
/* 25 S> */ B(Return),
B(Return),
/* 16 S> */ B(LdaSmi), I8(42),
B(Star1),
B(LdaFalse),
......@@ -76,7 +76,7 @@ bytecodes: [
B(Ldar), R(1),
/* 16 E> */ B(Throw),
B(Ldar), R(1),
/* 25 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 25 S> */ B(Return),
]
......@@ -113,7 +113,7 @@ bytecodes: [
B(Ldar), R(4),
/* 11 E> */ B(Throw),
B(Ldar), R(4),
/* 44 S> */ B(Return),
B(Return),
/* 30 S> */ B(CreateArrayLiteral), U8(4), U8(0), U8(37),
B(Star6),
B(GetIterator), R(6), U8(1), U8(3),
......@@ -191,7 +191,7 @@ bytecodes: [
B(Ldar), R(8),
B(ReThrow),
B(Ldar), R(8),
/* 44 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 44 S> */ B(Return),
]
......@@ -238,7 +238,7 @@ bytecodes: [
B(Ldar), R(1),
/* 38 E> */ B(Throw),
B(Ldar), R(1),
/* 54 S> */ B(Return),
B(Return),
/* 43 S> */ B(LdaGlobal), U8(4), U8(0),
B(Star5),
/* 50 E> */ B(CallUndefinedReceiver0), R(5), U8(2),
......@@ -263,7 +263,7 @@ bytecodes: [
B(CallProperty1), R(6), R(3), R(4), U8(14),
B(Jump), U8(45),
B(Ldar), R(4),
/* 54 S> */ B(Return),
B(Return),
B(LdaNamedProperty), R(3), U8(9), U8(16),
B(JumpIfUndefinedOrNull), U8(10),
B(Star6),
......@@ -296,7 +296,7 @@ bytecodes: [
B(TestReferenceEqual), R(2),
B(JumpIfFalse), U8(5),
B(Ldar), R(3),
/* 54 S> */ B(Return),
B(Return),
B(LdaUndefined),
/* 54 S> */ B(Return),
]
......
......@@ -28,7 +28,7 @@ bytecodes: [
B(Ldar), R(2),
/* 0 E> */ B(Throw),
B(Ldar), R(2),
/* 14 S> */ B(Return),
B(Return),
B(Mov), R(2), R(1),
B(Ldar), R(1),
/* 14 S> */ B(Return),
......@@ -62,7 +62,7 @@ bytecodes: [
B(Ldar), R(2),
/* 0 E> */ B(Throw),
B(Ldar), R(2),
/* 25 S> */ B(Return),
B(Return),
B(Mov), R(2), R(1),
B(Ldar), R(1),
/* 25 S> */ B(Return),
......@@ -98,7 +98,7 @@ bytecodes: [
B(Ldar), R(3),
/* 0 E> */ B(Throw),
B(Ldar), R(3),
/* 65 S> */ B(Return),
B(Return),
/* 32 S> */ B(LdaModuleVariable), I8(-1), U8(0),
B(ThrowReferenceErrorIfHole), U8(3),
B(Star3),
......@@ -148,7 +148,7 @@ bytecodes: [
B(Ldar), R(3),
/* 0 E> */ B(Throw),
B(Ldar), R(3),
/* 50 S> */ B(Return),
B(Return),
/* 17 S> */ B(LdaSmi), I8(42),
/* 17 E> */ B(StaModuleVariable), I8(1), U8(0),
/* 21 S> */ B(LdaModuleVariable), I8(1), U8(0),
......@@ -201,7 +201,7 @@ bytecodes: [
B(Ldar), R(3),
/* 0 E> */ B(Throw),
B(Ldar), R(3),
/* 50 S> */ B(Return),
B(Return),
/* 17 S> */ B(LdaSmi), I8(42),
/* 17 E> */ B(StaModuleVariable), I8(1), U8(0),
/* 21 S> */ B(LdaModuleVariable), I8(1), U8(0),
......@@ -255,7 +255,7 @@ bytecodes: [
B(Ldar), R(3),
/* 0 E> */ B(Throw),
B(Ldar), R(3),
/* 52 S> */ B(Return),
B(Return),
/* 19 S> */ B(LdaSmi), I8(42),
/* 19 E> */ B(StaModuleVariable), I8(1), U8(0),
/* 23 S> */ B(LdaModuleVariable), I8(1), U8(0),
......@@ -307,7 +307,7 @@ bytecodes: [
B(Ldar), R(2),
/* 0 E> */ B(Throw),
B(Ldar), R(2),
/* 33 S> */ B(Return),
B(Return),
B(Mov), R(2), R(1),
B(CreateClosure), U8(4), U8(0), U8(0),
B(StaModuleVariable), I8(1), U8(0),
......@@ -350,7 +350,7 @@ bytecodes: [
B(Ldar), R(2),
/* 0 E> */ B(Throw),
B(Ldar), R(2),
/* 27 S> */ B(Return),
B(Return),
B(Mov), R(2), R(1),
B(LdaTheHole),
B(Star5),
......@@ -398,7 +398,7 @@ bytecodes: [
B(Ldar), R(2),
/* 0 E> */ B(Throw),
B(Ldar), R(2),
/* 31 S> */ B(Return),
B(Return),
B(Mov), R(2), R(1),
B(Ldar), R(1),
/* 31 S> */ B(Return),
......@@ -432,7 +432,7 @@ bytecodes: [
B(Ldar), R(2),
/* 0 E> */ B(Throw),
B(Ldar), R(2),
/* 20 S> */ B(Return),
B(Return),
B(Mov), R(2), R(1),
B(Ldar), R(1),
/* 20 S> */ B(Return),
......@@ -472,7 +472,7 @@ bytecodes: [
B(Ldar), R(3),
/* 0 E> */ B(Throw),
B(Ldar), R(3),
/* 46 S> */ B(Return),
B(Return),
/* 31 S> */ B(LdaNamedProperty), R(1), U8(3), U8(0),
B(Star3),
/* 42 E> */ B(LdaNamedProperty), R(1), U8(4), U8(2),
......
......@@ -265,7 +265,7 @@ bytecodes: [
B(Ldar), R(3),
/* 11 E> */ B(Throw),
B(Ldar), R(3),
/* 62 S> */ B(Return),
B(Return),
/* 31 S> */ B(LdaZero),
B(Star1),
/* 36 S> */ B(LdaSmi), I8(10),
......@@ -311,7 +311,7 @@ bytecodes: [
B(Ldar), R(2),
/* 11 E> */ B(Throw),
B(Ldar), R(2),
/* 56 S> */ B(Return),
B(Return),
/* 31 S> */ B(LdaZero),
B(Star1),
/* 36 S> */ B(LdaSmi), I8(10),
......@@ -329,7 +329,7 @@ bytecodes: [
B(Ldar), R(2),
/* 47 E> */ B(Throw),
B(Ldar), R(2),
/* 56 S> */ B(Return),
B(Return),
/* 44 S> */ B(Ldar), R(1),
B(Inc), U8(1),
B(Star1),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment