Commit 0e8d220e authored by zhengxing.li's avatar zhengxing.li Committed by Commit bot

X87: [generators] Decouple generator resume from fullcodegen.

  port 974721c6 (r35283)

  original commit message:
  Introduce a ResumeGeneratorTrampoline, which does the actual stack state
  reconstruction (currently always restores a fullcodegen frame), and
  introduce appropriate TurboFan builtins for %GeneratorPrototype%.next,
  %GeneratorPrototype%.return and %GeneratorPrototype%.throw based on
  this native builtin.

  Also unify the flooding in case of step-in to always work based on
  JSFunction and remove the special casing for JSGeneratorObject.

BUG=

Review URL: https://codereview.chromium.org/1889083002

Cr-Commit-Position: refs/heads/master@{#35510}
parent 20eff45d
......@@ -1749,7 +1749,7 @@ void FullCodeGenerator::VisitYield(Yield* expr) {
// this. It stays on the stack while we update the iterator.
VisitForStackValue(expr->expression());
Label suspend, continuation, post_runtime, resume;
Label suspend, continuation, post_runtime, resume, exception;
__ jmp(&suspend);
__ bind(&continuation);
......@@ -1758,12 +1758,18 @@ void FullCodeGenerator::VisitYield(Yield* expr) {
// respective resume operation).
__ RecordGeneratorContinuation();
__ pop(ebx);
__ cmp(ebx, Immediate(Smi::FromInt(JSGeneratorObject::RETURN)));
__ j(not_equal, &resume);
__ push(result_register());
STATIC_ASSERT(JSGeneratorObject::kNext < JSGeneratorObject::kReturn);
STATIC_ASSERT(JSGeneratorObject::kThrow > JSGeneratorObject::kReturn);
__ cmp(ebx, Immediate(Smi::FromInt(JSGeneratorObject::kReturn)));
__ j(less, &resume);
__ Push(result_register());
__ j(greater, &exception);
EmitCreateIteratorResult(true);
EmitUnwindAndReturn();
__ bind(&exception);
__ CallRuntime(Runtime::kThrow);
__ bind(&suspend);
OperandStackDepthIncrement(1); // Not popped on this path.
VisitForAccumulatorValue(expr->generator_object());
......@@ -1789,101 +1795,6 @@ void FullCodeGenerator::VisitYield(Yield* expr) {
context()->Plug(result_register());
}
void FullCodeGenerator::EmitGeneratorResume(Expression *generator,
Expression *value,
JSGeneratorObject::ResumeMode resume_mode) {
// The value stays in eax, and is ultimately read by the resumed generator, as
// if CallRuntime(Runtime::kSuspendJSGeneratorObject) returned it. Or it
// is read to throw the value when the resumed generator is already closed.
// ebx will hold the generator object until the activation has been resumed.
VisitForStackValue(generator);
VisitForAccumulatorValue(value);
PopOperand(ebx);
// Store input value into generator object.
__ mov(FieldOperand(ebx, JSGeneratorObject::kInputOffset), result_register());
__ mov(ecx, result_register());
__ RecordWriteField(ebx, JSGeneratorObject::kInputOffset, ecx, edx,
kDontSaveFPRegs);
// Load suspended function and context.
__ mov(esi, FieldOperand(ebx, JSGeneratorObject::kContextOffset));
__ mov(edi, FieldOperand(ebx, JSGeneratorObject::kFunctionOffset));
// Push receiver.
__ push(FieldOperand(ebx, JSGeneratorObject::kReceiverOffset));
// Push holes for arguments to generator function. Since the parser forced
// context allocation for any variables in generators, the actual argument
// values have already been copied into the context and these dummy values
// will never be used.
__ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
__ mov(edx,
FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
__ mov(ecx, isolate()->factory()->the_hole_value());
Label push_argument_holes, push_frame;
__ bind(&push_argument_holes);
__ sub(edx, Immediate(Smi::FromInt(1)));
__ j(carry, &push_frame);
__ push(ecx);
__ jmp(&push_argument_holes);
// Enter a new JavaScript frame, and initialize its slots as they were when
// the generator was suspended.
Label resume_frame, done;
__ bind(&push_frame);
__ call(&resume_frame);
__ jmp(&done);
__ bind(&resume_frame);
__ push(ebp); // Caller's frame pointer.
__ mov(ebp, esp);
__ push(esi); // Callee's context.
__ push(edi); // Callee's JS Function.
// Load the operand stack size.
__ mov(edx, FieldOperand(ebx, JSGeneratorObject::kOperandStackOffset));
__ mov(edx, FieldOperand(edx, FixedArray::kLengthOffset));
__ SmiUntag(edx);
// If we are sending a value and there is no operand stack, we can jump back
// in directly.
if (resume_mode == JSGeneratorObject::NEXT) {
Label slow_resume;
__ cmp(edx, Immediate(0));
__ j(not_zero, &slow_resume);
__ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
__ mov(ecx, FieldOperand(ebx, JSGeneratorObject::kContinuationOffset));
__ SmiUntag(ecx);
__ add(edx, ecx);
__ mov(FieldOperand(ebx, JSGeneratorObject::kContinuationOffset),
Immediate(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
__ Push(Smi::FromInt(resume_mode)); // Consumed in continuation.
__ jmp(edx);
__ bind(&slow_resume);
}
// Otherwise, we push holes for the operand stack and call the runtime to fix
// up the stack and the handlers.
Label push_operand_holes, call_resume;
__ bind(&push_operand_holes);
__ sub(edx, Immediate(1));
__ j(carry, &call_resume);
__ push(ecx);
__ jmp(&push_operand_holes);
__ bind(&call_resume);
__ Push(Smi::FromInt(resume_mode)); // Consumed in continuation.
__ push(ebx);
__ push(result_register());
__ Push(Smi::FromInt(resume_mode));
__ CallRuntime(Runtime::kResumeJSGeneratorObject);
// Not reached: the runtime call returns elsewhere.
__ Abort(kGeneratorFailedToResume);
__ bind(&done);
context()->Plug(result_register());
}
void FullCodeGenerator::PushOperand(MemOperand operand) {
OperandStackDepthIncrement(1);
__ Push(operand);
......
......@@ -392,6 +392,119 @@ void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
Generate_JSEntryTrampolineHelper(masm, true);
}
// static
void Builtins::Generate_ResumeGeneratorTrampoline(MacroAssembler* masm) {
// ----------- S t a t e -------------
// -- eax : the value to pass to the generator
// -- ebx : the JSGeneratorObject to resume
// -- edx : the resume mode (tagged)
// -- esp[0] : return address
// -----------------------------------
__ AssertGeneratorObject(ebx);
// Store input value into generator object.
__ mov(FieldOperand(ebx, JSGeneratorObject::kInputOffset), eax);
__ RecordWriteField(ebx, JSGeneratorObject::kInputOffset, eax, ecx,
kDontSaveFPRegs);
// Load suspended function and context.
__ mov(esi, FieldOperand(ebx, JSGeneratorObject::kContextOffset));
__ mov(edi, FieldOperand(ebx, JSGeneratorObject::kFunctionOffset));
// Flood function if we are stepping.
Label skip_flooding;
ExternalReference step_in_enabled =
ExternalReference::debug_step_in_enabled_address(masm->isolate());
__ cmpb(Operand::StaticVariable(step_in_enabled), Immediate(0));
__ j(equal, &skip_flooding);
{
FrameScope scope(masm, StackFrame::INTERNAL);
__ Push(ebx);
__ Push(edx);
__ Push(edi);
__ CallRuntime(Runtime::kDebugPrepareStepInIfStepping);
__ Pop(edx);
__ Pop(ebx);
__ mov(edi, FieldOperand(ebx, JSGeneratorObject::kFunctionOffset));
}
__ bind(&skip_flooding);
// Pop return address.
__ PopReturnAddressTo(eax);
// Push receiver.
__ Push(FieldOperand(ebx, JSGeneratorObject::kReceiverOffset));
// ----------- S t a t e -------------
// -- eax : return address
// -- ebx : the JSGeneratorObject to resume
// -- edx : the resume mode (tagged)
// -- edi : generator function
// -- esi : generator context
// -- esp[0] : generator receiver
// -----------------------------------
// Push holes for arguments to generator function. Since the parser forced
// context allocation for any variables in generators, the actual argument
// values have already been copied into the context and these dummy values
// will never be used.
__ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
__ mov(ecx,
FieldOperand(ecx, SharedFunctionInfo::kFormalParameterCountOffset));
{
Label done_loop, loop;
__ bind(&loop);
__ sub(ecx, Immediate(Smi::FromInt(1)));
__ j(carry, &done_loop, Label::kNear);
__ PushRoot(Heap::kTheHoleValueRootIndex);
__ jmp(&loop);
__ bind(&done_loop);
}
// Enter a new JavaScript frame, and initialize its slots as they were when
// the generator was suspended.
FrameScope scope(masm, StackFrame::MANUAL);
__ PushReturnAddressFrom(eax); // Return address.
__ Push(ebp); // Caller's frame pointer.
__ Move(ebp, esp);
__ Push(esi); // Callee's context.
__ Push(edi); // Callee's JS Function.
// Restore the operand stack.
__ mov(eax, FieldOperand(ebx, JSGeneratorObject::kOperandStackOffset));
{
Label done_loop, loop;
__ Move(ecx, Smi::FromInt(0));
__ bind(&loop);
__ cmp(ecx, FieldOperand(eax, FixedArray::kLengthOffset));
__ j(equal, &done_loop, Label::kNear);
__ Push(FieldOperand(eax, ecx, times_half_pointer_size,
FixedArray::kHeaderSize));
__ add(ecx, Immediate(Smi::FromInt(1)));
__ jmp(&loop);
__ bind(&done_loop);
}
// Push resume mode (consumed in continuation).
__ Push(edx);
// Reset operand stack so we don't leak.
__ mov(FieldOperand(ebx, JSGeneratorObject::kOperandStackOffset),
Immediate(masm->isolate()->factory()->empty_fixed_array()));
// Restore value.
__ mov(eax, FieldOperand(ebx, JSGeneratorObject::kInputOffset));
// Resume the generator function at the continuation.
__ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
__ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
__ mov(ecx, FieldOperand(ebx, JSGeneratorObject::kContinuationOffset));
__ SmiUntag(ecx);
__ lea(edx, FieldOperand(edx, ecx, times_1, Code::kHeaderSize));
__ mov(FieldOperand(ebx, JSGeneratorObject::kContinuationOffset),
Immediate(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
__ jmp(edx);
}
// Generate code for entering a JS function with the interpreter.
// On entry to the function the receiver and arguments have been pushed on the
......
......@@ -416,6 +416,16 @@ void InterpreterCEntryDescriptor::InitializePlatformSpecific(
data->InitializePlatformSpecific(arraysize(registers), registers);
}
void ResumeGeneratorDescriptor::InitializePlatformSpecific(
CallInterfaceDescriptorData* data) {
Register registers[] = {
eax, // the value to pass to the generator
ebx, // the JSGeneratorObject to resume
edx // the resume mode (tagged)
};
data->InitializePlatformSpecific(arraysize(registers), registers);
}
} // namespace internal
} // namespace v8
......
......@@ -973,6 +973,17 @@ void MacroAssembler::AssertBoundFunction(Register object) {
}
}
void MacroAssembler::AssertGeneratorObject(Register object) {
if (emit_debug_code()) {
test(object, Immediate(kSmiTagMask));
Check(not_equal, kOperandIsASmiAndNotAGeneratorObject);
Push(object);
CmpObjectType(object, JS_GENERATOR_OBJECT_TYPE, object);
Pop(object);
Check(equal, kOperandIsNotAGeneratorObject);
}
}
void MacroAssembler::AssertReceiver(Register object) {
if (emit_debug_code()) {
test(object, Immediate(kSmiTagMask));
......
......@@ -552,6 +552,10 @@ class MacroAssembler: public Assembler {
// enabled via --debug-code.
void AssertBoundFunction(Register object);
// Abort execution if argument is not a JSGeneratorObject,
// enabled via --debug-code.
void AssertGeneratorObject(Register object);
// Abort execution if argument is not a JSReceiver, enabled via --debug-code.
void AssertReceiver(Register object);
......
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