Commit 783e1fde authored by Lu Yahan's avatar Lu Yahan Committed by V8 LUCI CQ

[riscv64] Create Instance Type for Class Constructors

Port: 1cd7a582

Original Commit Message:

Class Constructors are special, because they are callable but [[Call]]
raises an exception. Instead of checking if a JS function is a class
constructor for every JS function call, this CL adds a new instance
type for class constructors.
This way we can use a fast instance type range check for the common
case, and only check for class constructors in the uncommon case were
a class constructor is called and when we need to raise an exception.

Change-Id: I8f11416124a1eefc4ba63423747686bc0b519bb9
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3218711
Commit-Queue: Yahan Lu <yahan@iscas.ac.cn>
Reviewed-by: 's avatarji qiu <qiuji@iscas.ac.cn>
Cr-Commit-Position: refs/heads/main@{#77395}
parent 32392279
...@@ -2351,16 +2351,8 @@ void Builtins::Generate_CallFunction(MacroAssembler* masm, ...@@ -2351,16 +2351,8 @@ void Builtins::Generate_CallFunction(MacroAssembler* masm,
// ----------------------------------- // -----------------------------------
__ AssertFunction(a1); __ AssertFunction(a1);
// See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList)
// Check that function is not a "classConstructor".
Label class_constructor;
__ LoadTaggedPointerField( __ LoadTaggedPointerField(
a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset)); a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
__ Lwu(a3, FieldMemOperand(a2, SharedFunctionInfo::kFlagsOffset));
__ And(kScratchReg, a3,
Operand(SharedFunctionInfo::IsClassConstructorBit::kMask));
__ Branch(&class_constructor, ne, kScratchReg, Operand(zero_reg));
// Enter the context of the function; ToObject has to run in the function // Enter the context of the function; ToObject has to run in the function
// context, and we also need to take the global proxy from the function // context, and we also need to take the global proxy from the function
// context in case of conversion. // context in case of conversion.
...@@ -2438,14 +2430,6 @@ void Builtins::Generate_CallFunction(MacroAssembler* masm, ...@@ -2438,14 +2430,6 @@ void Builtins::Generate_CallFunction(MacroAssembler* masm,
__ Lhu(a2, __ Lhu(a2,
FieldMemOperand(a2, SharedFunctionInfo::kFormalParameterCountOffset)); FieldMemOperand(a2, SharedFunctionInfo::kFormalParameterCountOffset));
__ InvokeFunctionCode(a1, no_reg, a2, a0, InvokeType::kJump); __ InvokeFunctionCode(a1, no_reg, a2, a0, InvokeType::kJump);
// The function is a "classConstructor", need to raise an exception.
__ bind(&class_constructor);
{
FrameScope frame(masm, StackFrame::INTERNAL);
__ Push(a1);
__ CallRuntime(Runtime::kThrowConstructorNonCallableError);
}
} }
namespace { namespace {
...@@ -2554,19 +2538,19 @@ void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) { ...@@ -2554,19 +2538,19 @@ void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) {
// -- a1 : the target to call (can be any Object). // -- a1 : the target to call (can be any Object).
// ----------------------------------- // -----------------------------------
Label non_callable, non_smi; Label non_callable, class_constructor;
UseScratchRegisterScope temps(masm); UseScratchRegisterScope temps(masm);
temps.Include(t1, t2); temps.Include(t1, t2);
temps.Include(t4); temps.Include(t4);
Register map = temps.Acquire(), type = temps.Acquire(), Register map = temps.Acquire(), type = temps.Acquire(),
range = temps.Acquire(); range = temps.Acquire();
__ JumpIfSmi(a1, &non_callable); __ JumpIfSmi(a1, &non_callable);
__ bind(&non_smi);
__ LoadMap(map, a1); __ LoadMap(map, a1);
__ GetInstanceTypeRange(map, type, FIRST_JS_FUNCTION_TYPE, range); __ GetInstanceTypeRange(map, type, FIRST_CALLABLE_JS_FUNCTION_TYPE, range);
__ Jump(masm->isolate()->builtins()->CallFunction(mode), __ Jump(masm->isolate()->builtins()->CallFunction(mode),
RelocInfo::CODE_TARGET, Uless_equal, range, RelocInfo::CODE_TARGET, Uless_equal, range,
Operand(LAST_JS_FUNCTION_TYPE - FIRST_JS_FUNCTION_TYPE)); Operand(LAST_CALLABLE_JS_FUNCTION_TYPE -
FIRST_CALLABLE_JS_FUNCTION_TYPE));
__ Jump(BUILTIN_CODE(masm->isolate(), CallBoundFunction), __ Jump(BUILTIN_CODE(masm->isolate(), CallBoundFunction),
RelocInfo::CODE_TARGET, eq, type, Operand(JS_BOUND_FUNCTION_TYPE)); RelocInfo::CODE_TARGET, eq, type, Operand(JS_BOUND_FUNCTION_TYPE));
Register scratch = map; Register scratch = map;
...@@ -2579,6 +2563,10 @@ void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) { ...@@ -2579,6 +2563,10 @@ void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) {
__ Jump(BUILTIN_CODE(masm->isolate(), CallProxy), RelocInfo::CODE_TARGET, eq, __ Jump(BUILTIN_CODE(masm->isolate(), CallProxy), RelocInfo::CODE_TARGET, eq,
type, Operand(JS_PROXY_TYPE)); type, Operand(JS_PROXY_TYPE));
// ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList)
// Check that the function is not a "classConstructor".
__ Branch(&class_constructor, eq, type, Operand(JS_CLASS_CONSTRUCTOR_TYPE));
// 2. Call to something else, which might have a [[Call]] internal method (if // 2. Call to something else, which might have a [[Call]] internal method (if
// not we raise an exception). // not we raise an exception).
// Overwrite the original receiver with the (original) target. // Overwrite the original receiver with the (original) target.
...@@ -2596,6 +2584,14 @@ void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) { ...@@ -2596,6 +2584,14 @@ void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) {
__ Push(a1); __ Push(a1);
__ CallRuntime(Runtime::kThrowCalledNonCallable); __ CallRuntime(Runtime::kThrowCalledNonCallable);
} }
// 4. The function is a "classConstructor", need to raise an exception.
__ bind(&class_constructor);
{
FrameScope frame(masm, StackFrame::INTERNAL);
__ Push(a1);
__ CallRuntime(Runtime::kThrowConstructorNonCallableError);
__ ebreak();
}
} }
void Builtins::Generate_ConstructFunction(MacroAssembler* masm) { void Builtins::Generate_ConstructFunction(MacroAssembler* masm) {
......
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