Commit 66e2a786 authored by zhengxing.li's avatar zhengxing.li Committed by Commit bot

X87: [es6] Tail calls support.

  port 6131ab1e (r33509)

  original commit message:
  This CL implements PrepareForTailCall() mentioned in ES6 spec for full codegen, Crankshaft and Turbofan.
  When debugger is active tail calls are disabled.

  Tail calling can be enabled by --harmony-tailcalls flag.

BUG=

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

Cr-Commit-Position: refs/heads/master@{#33549}
parent d984b3b0
......@@ -4004,31 +4004,35 @@ void LCodeGen::DoInvokeFunction(LInvokeFunction* instr) {
void LCodeGen::DoCallFunction(LCallFunction* instr) {
HCallFunction* hinstr = instr->hydrogen();
DCHECK(ToRegister(instr->context()).is(esi));
DCHECK(ToRegister(instr->function()).is(edi));
DCHECK(ToRegister(instr->result()).is(eax));
int arity = instr->arity();
ConvertReceiverMode mode = instr->hydrogen()->convert_mode();
if (instr->hydrogen()->HasVectorAndSlot()) {
ConvertReceiverMode mode = hinstr->convert_mode();
TailCallMode tail_call_mode = hinstr->tail_call_mode();
if (hinstr->HasVectorAndSlot()) {
Register slot_register = ToRegister(instr->temp_slot());
Register vector_register = ToRegister(instr->temp_vector());
DCHECK(slot_register.is(edx));
DCHECK(vector_register.is(ebx));
AllowDeferredHandleDereference vector_structure_check;
Handle<TypeFeedbackVector> vector = instr->hydrogen()->feedback_vector();
int index = vector->GetIndex(instr->hydrogen()->slot());
Handle<TypeFeedbackVector> vector = hinstr->feedback_vector();
int index = vector->GetIndex(hinstr->slot());
__ mov(vector_register, vector);
__ mov(slot_register, Immediate(Smi::FromInt(index)));
Handle<Code> ic =
CodeFactory::CallICInOptimizedCode(isolate(), arity, mode).code();
Handle<Code> ic = CodeFactory::CallICInOptimizedCode(isolate(), arity, mode,
tail_call_mode)
.code();
CallCode(ic, RelocInfo::CODE_TARGET, instr);
} else {
__ Set(eax, arity);
CallCode(isolate()->builtins()->Call(mode), RelocInfo::CODE_TARGET, instr);
CallCode(isolate()->builtins()->Call(mode, tail_call_mode),
RelocInfo::CODE_TARGET, instr);
}
}
......
......@@ -2740,7 +2740,9 @@ void FullCodeGenerator::EmitCall(Call* expr, ConvertReceiverMode mode) {
PrepareForBailoutForId(expr->CallId(), NO_REGISTERS);
SetCallPosition(expr);
Handle<Code> ic = CodeFactory::CallIC(isolate(), arg_count, mode).code();
Handle<Code> ic =
CodeFactory::CallIC(isolate(), arg_count, mode, expr->tail_call_mode())
.code();
__ Move(edx, Immediate(SmiFromSlot(expr->CallFeedbackICSlot())));
__ mov(edi, Operand(esp, (arg_count + 1) * kPointerSize));
// Don't assign a type feedback id to the IC, since type feedback is provided
......
......@@ -1857,10 +1857,128 @@ void Builtins::Generate_Apply(MacroAssembler* masm) {
}
}
namespace {
// Drops top JavaScript frame and an arguments adaptor frame below it (if
// present) preserving all the arguments prepared for current call.
// Does nothing if debugger is currently active.
// ES6 14.6.3. PrepareForTailCall
//
// Stack structure for the function g() tail calling f():
//
// ------- Caller frame: -------
// | ...
// | g()'s arg M
// | ...
// | g()'s arg 1
// | g()'s receiver arg
// | g()'s caller pc
// ------- g()'s frame: -------
// | g()'s caller fp <- fp
// | g()'s context
// | function pointer: g
// | -------------------------
// | ...
// | ...
// | f()'s arg N
// | ...
// | f()'s arg 1
// | f()'s receiver arg
// | f()'s caller pc <- sp
// ----------------------
//
void PrepareForTailCall(MacroAssembler* masm, Register args_reg,
Register scratch1, Register scratch2,
Register scratch3) {
DCHECK(!AreAliased(args_reg, scratch1, scratch2, scratch3));
Comment cmnt(masm, "[ PrepareForTailCall");
// Prepare for tail call only if the debugger is not active.
Label done;
ExternalReference debug_is_active =
ExternalReference::debug_is_active_address(masm->isolate());
__ movzx_b(scratch1, Operand::StaticVariable(debug_is_active));
__ cmp(scratch1, Immediate(0));
__ j(not_equal, &done, Label::kNear);
// Check if next frame is an arguments adaptor frame.
Label no_arguments_adaptor, formal_parameter_count_loaded;
__ mov(scratch2, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
__ cmp(Operand(scratch2, StandardFrameConstants::kContextOffset),
Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
__ j(not_equal, &no_arguments_adaptor, Label::kNear);
// Drop arguments adaptor frame and load arguments count.
__ mov(ebp, scratch2);
__ mov(scratch1, Operand(ebp, ArgumentsAdaptorFrameConstants::kLengthOffset));
__ SmiUntag(scratch1);
__ jmp(&formal_parameter_count_loaded, Label::kNear);
__ bind(&no_arguments_adaptor);
// Load caller's formal parameter count
__ mov(scratch1, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
__ mov(scratch1,
FieldOperand(scratch1, JSFunction::kSharedFunctionInfoOffset));
__ mov(
scratch1,
FieldOperand(scratch1, SharedFunctionInfo::kFormalParameterCountOffset));
__ SmiUntag(scratch1);
__ bind(&formal_parameter_count_loaded);
// Calculate the destination address where we will put the return address
// after we drop current frame.
Register new_sp_reg = scratch2;
__ sub(scratch1, args_reg);
__ lea(new_sp_reg, Operand(ebp, scratch1, times_pointer_size,
StandardFrameConstants::kCallerPCOffset));
if (FLAG_debug_code) {
__ cmp(esp, new_sp_reg);
__ Check(below, kStackAccessBelowStackPointer);
}
// Copy receiver and return address as well.
Register count_reg = scratch1;
__ lea(count_reg, Operand(args_reg, 2));
// Copy return address from caller's frame to current frame's return address
// to avoid its trashing and let the following loop copy it to the right
// place.
Register tmp_reg = scratch3;
__ mov(tmp_reg, Operand(ebp, StandardFrameConstants::kCallerPCOffset));
__ mov(Operand(esp, 0), tmp_reg);
// Restore caller's frame pointer now as it could be overwritten by
// the copying loop.
__ mov(ebp, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
Operand src(esp, count_reg, times_pointer_size, 0);
Operand dst(new_sp_reg, count_reg, times_pointer_size, 0);
// Now copy callee arguments to the caller frame going backwards to avoid
// callee arguments corruption (source and destination areas could overlap).
Label loop, entry;
__ jmp(&entry, Label::kNear);
__ bind(&loop);
__ dec(count_reg);
__ mov(tmp_reg, src);
__ mov(dst, tmp_reg);
__ bind(&entry);
__ cmp(count_reg, Immediate(0));
__ j(not_equal, &loop, Label::kNear);
// Leave current frame.
__ mov(esp, new_sp_reg);
__ bind(&done);
}
} // namespace
// static
void Builtins::Generate_CallFunction(MacroAssembler* masm,
ConvertReceiverMode mode) {
ConvertReceiverMode mode,
TailCallMode tail_call_mode) {
// ----------- S t a t e -------------
// -- eax : the number of arguments (not including the receiver)
// -- edi : the function to call (checked to be a JSFunction)
......@@ -1949,6 +2067,12 @@ void Builtins::Generate_CallFunction(MacroAssembler* masm,
// -- esi : the function context.
// -----------------------------------
if (tail_call_mode == TailCallMode::kAllow) {
PrepareForTailCall(masm, eax, ebx, ecx, edx);
// Reload shared function info.
__ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
}
__ mov(ebx,
FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
__ SmiUntag(ebx);
......@@ -2054,13 +2178,18 @@ void Generate_PushBoundArguments(MacroAssembler* masm) {
// static
void Builtins::Generate_CallBoundFunction(MacroAssembler* masm) {
void Builtins::Generate_CallBoundFunctionImpl(MacroAssembler* masm,
TailCallMode tail_call_mode) {
// ----------- S t a t e -------------
// -- eax : the number of arguments (not including the receiver)
// -- edi : the function to call (checked to be a JSBoundFunction)
// -----------------------------------
__ AssertBoundFunction(edi);
if (tail_call_mode == TailCallMode::kAllow) {
PrepareForTailCall(masm, eax, ebx, ecx, edx);
}
// Patch the receiver to [[BoundThis]].
__ mov(ebx, FieldOperand(edi, JSBoundFunction::kBoundThisOffset));
__ mov(Operand(esp, eax, times_pointer_size, kPointerSize), ebx);
......@@ -2078,7 +2207,8 @@ void Builtins::Generate_CallBoundFunction(MacroAssembler* masm) {
// static
void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) {
void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode,
TailCallMode tail_call_mode) {
// ----------- S t a t e -------------
// -- eax : the number of arguments (not including the receiver)
// -- edi : the target to call (can be any Object).
......@@ -2088,14 +2218,19 @@ void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) {
__ JumpIfSmi(edi, &non_callable);
__ bind(&non_smi);
__ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
__ j(equal, masm->isolate()->builtins()->CallFunction(mode),
__ j(equal, masm->isolate()->builtins()->CallFunction(mode, tail_call_mode),
RelocInfo::CODE_TARGET);
__ CmpInstanceType(ecx, JS_BOUND_FUNCTION_TYPE);
__ j(equal, masm->isolate()->builtins()->CallBoundFunction(),
__ j(equal, masm->isolate()->builtins()->CallBoundFunction(tail_call_mode),
RelocInfo::CODE_TARGET);
__ CmpInstanceType(ecx, JS_PROXY_TYPE);
__ j(not_equal, &non_function);
// 0. Prepare for tail call if necessary.
if (tail_call_mode == TailCallMode::kAllow) {
PrepareForTailCall(masm, eax, ebx, ecx, edx);
}
// 1. Runtime fallback for Proxy [[Call]].
__ PopReturnAddressTo(ecx);
__ Push(edi);
......@@ -2118,7 +2253,7 @@ void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) {
// Let the "call_as_function_delegate" take care of the rest.
__ LoadGlobalFunction(Context::CALL_AS_FUNCTION_DELEGATE_INDEX, edi);
__ Jump(masm->isolate()->builtins()->CallFunction(
ConvertReceiverMode::kNotNullOrUndefined),
ConvertReceiverMode::kNotNullOrUndefined, tail_call_mode),
RelocInfo::CODE_TARGET);
// 3. Call to something that is not callable.
......
......@@ -1854,7 +1854,8 @@ void CallICStub::Generate(MacroAssembler* masm) {
__ bind(&call_function);
__ Set(eax, argc);
__ Jump(masm->isolate()->builtins()->CallFunction(convert_mode()),
__ Jump(masm->isolate()->builtins()->CallFunction(convert_mode(),
tail_call_mode()),
RelocInfo::CODE_TARGET);
__ bind(&extra_checks_or_miss);
......@@ -1893,7 +1894,7 @@ void CallICStub::Generate(MacroAssembler* masm) {
__ bind(&call);
__ Set(eax, argc);
__ Jump(masm->isolate()->builtins()->Call(convert_mode()),
__ Jump(masm->isolate()->builtins()->Call(convert_mode(), tail_call_mode()),
RelocInfo::CODE_TARGET);
__ bind(&uninitialized);
......
......@@ -1734,13 +1734,13 @@ void MacroAssembler::InitializeFieldsWithFiller(Register current_address,
Register end_address,
Register filler) {
Label loop, entry;
jmp(&entry);
jmp(&entry, Label::kNear);
bind(&loop);
mov(Operand(current_address, 0), filler);
add(current_address, Immediate(kPointerSize));
bind(&entry);
cmp(current_address, end_address);
j(below, &loop);
j(below, &loop, Label::kNear);
}
......@@ -1762,9 +1762,9 @@ void MacroAssembler::NegativeZeroTest(Register result,
Label* then_label) {
Label ok;
test(result, result);
j(not_zero, &ok);
j(not_zero, &ok, Label::kNear);
test(op, op);
j(sign, then_label);
j(sign, then_label, Label::kNear);
bind(&ok);
}
......@@ -1776,10 +1776,10 @@ void MacroAssembler::NegativeZeroTest(Register result,
Label* then_label) {
Label ok;
test(result, result);
j(not_zero, &ok);
j(not_zero, &ok, Label::kNear);
mov(scratch, op1);
or_(scratch, op2);
j(sign, then_label);
j(sign, then_label, Label::kNear);
bind(&ok);
}
......
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