2021-09-07 14:24:16 +00:00
|
|
|
/*
|
2024-07-11 13:21:48 +00:00
|
|
|
* Copyright (c) 2022-2024 Huawei Device Co., Ltd.
|
2021-09-07 14:24:16 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-08-23 01:56:32 +00:00
|
|
|
#include "ecmascript/method.h"
|
2022-03-21 13:19:10 +00:00
|
|
|
|
2022-08-16 13:05:02 +00:00
|
|
|
#include "ecmascript/jspandafile/program_object.h"
|
2022-07-23 10:54:34 +00:00
|
|
|
|
2021-09-07 14:24:16 +00:00
|
|
|
namespace panda::ecmascript {
|
2022-08-23 01:56:32 +00:00
|
|
|
std::string Method::ParseFunctionName() const
|
2021-09-07 14:24:16 +00:00
|
|
|
{
|
2022-08-16 13:05:02 +00:00
|
|
|
const JSPandaFile *jsPandaFile = GetJSPandaFile();
|
|
|
|
return MethodLiteral::ParseFunctionName(jsPandaFile, GetMethodId());
|
2021-12-17 09:18:10 +00:00
|
|
|
}
|
2022-04-14 08:14:34 +00:00
|
|
|
|
2024-09-25 13:21:21 +00:00
|
|
|
std::pair<std::string_view, bool> Method::ParseFunctionNameView() const
|
|
|
|
{
|
|
|
|
const JSPandaFile *jsPandaFile = GetJSPandaFile();
|
|
|
|
return MethodLiteral::ParseFunctionNameView(jsPandaFile, GetMethodId());
|
|
|
|
}
|
|
|
|
|
2022-08-23 01:56:32 +00:00
|
|
|
const char *Method::GetMethodName() const
|
2022-04-14 08:14:34 +00:00
|
|
|
{
|
2022-08-16 13:05:02 +00:00
|
|
|
const JSPandaFile *jsPandaFile = GetJSPandaFile();
|
|
|
|
return MethodLiteral::GetMethodName(jsPandaFile, GetMethodId());
|
2022-04-14 08:14:34 +00:00
|
|
|
}
|
2022-04-21 09:17:00 +00:00
|
|
|
|
2023-03-15 08:25:25 +00:00
|
|
|
const char *Method::GetMethodName(const JSPandaFile *file) const
|
2022-09-22 15:53:58 +00:00
|
|
|
{
|
|
|
|
return MethodLiteral::GetMethodName(file, GetMethodId());
|
|
|
|
}
|
|
|
|
|
2023-09-20 07:24:34 +00:00
|
|
|
const CString Method::GetRecordNameStr() const
|
2022-12-16 02:54:52 +00:00
|
|
|
{
|
|
|
|
const JSPandaFile *jsPandaFile = GetJSPandaFile();
|
|
|
|
return MethodLiteral::GetRecordName(jsPandaFile, GetMethodId());
|
|
|
|
}
|
|
|
|
|
2022-08-23 01:56:32 +00:00
|
|
|
uint32_t Method::GetCodeSize() const
|
2022-04-21 09:17:00 +00:00
|
|
|
{
|
2022-08-16 13:05:02 +00:00
|
|
|
const JSPandaFile *jsPandaFile = GetJSPandaFile();
|
|
|
|
return MethodLiteral::GetCodeSize(jsPandaFile, GetMethodId());
|
2022-04-27 12:43:40 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 01:56:32 +00:00
|
|
|
const JSPandaFile *Method::GetJSPandaFile() const
|
2022-04-27 12:43:40 +00:00
|
|
|
{
|
2022-08-16 13:05:02 +00:00
|
|
|
JSTaggedValue constpool = GetConstantPool();
|
|
|
|
if (constpool.IsUndefined()) {
|
|
|
|
return nullptr;
|
2022-04-27 12:43:40 +00:00
|
|
|
}
|
2022-08-16 13:05:02 +00:00
|
|
|
|
2022-08-26 12:40:41 +00:00
|
|
|
const ConstantPool *taggedPool = ConstantPool::Cast(constpool.GetTaggedObject());
|
|
|
|
return taggedPool->GetJSPandaFile();
|
2022-04-21 09:17:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 01:42:27 +00:00
|
|
|
MethodLiteral *Method::GetMethodLiteral() const
|
|
|
|
{
|
2024-06-04 07:18:15 +00:00
|
|
|
if (IsAotWithCallField() || IsDeoptimized()) {
|
2022-09-26 01:42:27 +00:00
|
|
|
ASSERT(!IsNativeWithCallField());
|
|
|
|
const JSPandaFile *jsPandaFile = GetJSPandaFile();
|
2024-08-05 08:12:28 +00:00
|
|
|
ASSERT(jsPandaFile != nullptr);
|
2022-09-26 01:42:27 +00:00
|
|
|
return jsPandaFile->FindMethodLiteral(GetMethodId().GetOffset());
|
|
|
|
}
|
|
|
|
return reinterpret_cast<MethodLiteral *>(GetCodeEntryOrLiteral());
|
|
|
|
}
|
2022-12-03 14:03:40 +00:00
|
|
|
|
2024-06-04 07:18:15 +00:00
|
|
|
bool Method::IsDeoptimized() const
|
|
|
|
{
|
|
|
|
return GetDeoptType() != kungfu::DeoptType::NONE;
|
|
|
|
}
|
|
|
|
|
2022-12-03 14:03:40 +00:00
|
|
|
uint32_t Method::FindCatchBlock(uint32_t pc) const
|
|
|
|
{
|
|
|
|
ASSERT(!IsNativeWithCallField());
|
2024-08-10 03:36:57 +00:00
|
|
|
ASSERT(GetJSPandaFile() != nullptr);
|
2022-12-03 14:03:40 +00:00
|
|
|
auto *pandaFile = GetJSPandaFile()->GetPandaFile();
|
2024-08-05 08:12:28 +00:00
|
|
|
ASSERT(pandaFile != nullptr);
|
2022-12-03 14:03:40 +00:00
|
|
|
panda_file::MethodDataAccessor mda(*pandaFile, GetMethodId());
|
|
|
|
panda_file::CodeDataAccessor cda(*pandaFile, mda.GetCodeId().value());
|
|
|
|
|
|
|
|
uint32_t pcOffset = INVALID_INDEX;
|
|
|
|
cda.EnumerateTryBlocks([&pcOffset, pc](panda_file::CodeDataAccessor::TryBlock &tryBlock) {
|
|
|
|
if ((tryBlock.GetStartPc() <= pc) && ((tryBlock.GetStartPc() + tryBlock.GetLength()) > pc)) {
|
|
|
|
tryBlock.EnumerateCatchBlocks([&](panda_file::CodeDataAccessor::CatchBlock &catchBlock) {
|
|
|
|
pcOffset = catchBlock.GetHandlerPc();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return pcOffset == INVALID_INDEX;
|
|
|
|
});
|
|
|
|
return pcOffset;
|
|
|
|
}
|
2023-02-16 12:28:51 +00:00
|
|
|
|
2024-05-27 08:41:56 +00:00
|
|
|
bool Method::HasCatchBlock() const
|
|
|
|
{
|
2024-08-10 03:36:57 +00:00
|
|
|
ASSERT(GetJSPandaFile() != nullptr);
|
2024-05-27 08:41:56 +00:00
|
|
|
auto *pandaFile = GetJSPandaFile()->GetPandaFile();
|
2024-08-05 08:12:28 +00:00
|
|
|
ASSERT(pandaFile != nullptr);
|
2024-05-27 08:41:56 +00:00
|
|
|
panda_file::MethodDataAccessor mda(*pandaFile, GetMethodId());
|
|
|
|
panda_file::CodeDataAccessor cda(*pandaFile, mda.GetCodeId().value());
|
|
|
|
return cda.GetTriesSize() != 0;
|
|
|
|
}
|
|
|
|
|
2024-02-07 13:36:41 +00:00
|
|
|
JSHandle<Method> Method::Create(JSThread *thread, const JSPandaFile *jsPandaFile, MethodLiteral *methodLiteral)
|
2023-02-16 12:28:51 +00:00
|
|
|
{
|
|
|
|
EcmaVM *vm = thread->GetEcmaVM();
|
|
|
|
EntityId methodId = methodLiteral->GetMethodId();
|
2024-02-07 13:36:41 +00:00
|
|
|
JSTaggedValue patchVal = vm->GetQuickFixManager()->CheckAndGetPatch(thread, jsPandaFile, methodId);
|
2023-02-16 12:28:51 +00:00
|
|
|
if (!patchVal.IsHole()) {
|
|
|
|
return JSHandle<Method>(thread, patchVal);
|
|
|
|
}
|
|
|
|
|
2024-01-23 13:45:43 +00:00
|
|
|
JSHandle<Method> method;
|
2024-02-07 13:36:41 +00:00
|
|
|
method = vm->GetFactory()->NewSMethod(methodLiteral);
|
2023-05-23 07:51:10 +00:00
|
|
|
JSHandle<ConstantPool> newConstpool = thread->GetCurrentEcmaContext()->FindOrCreateConstPool(jsPandaFile, methodId);
|
2023-02-16 12:28:51 +00:00
|
|
|
method->SetConstantPool(thread, newConstpool);
|
|
|
|
return method;
|
|
|
|
}
|
2023-09-20 07:24:34 +00:00
|
|
|
|
2024-03-08 10:34:02 +00:00
|
|
|
void Method::SetCodeEntryAndMarkAOTWhenBinding(uintptr_t codeEntry)
|
|
|
|
{
|
|
|
|
SetAotCodeBit(true);
|
|
|
|
SetNativeBit(false);
|
|
|
|
SetCodeEntryOrLiteral(codeEntry);
|
|
|
|
}
|
|
|
|
|
2024-06-01 02:33:53 +00:00
|
|
|
void Method::ClearAOTStatusWhenDeopt(uintptr_t entry)
|
2024-03-08 10:34:02 +00:00
|
|
|
{
|
|
|
|
ClearAOTFlagsWhenInit();
|
2024-06-04 07:18:15 +00:00
|
|
|
// Do not clear deopt type, which records a method has deoptimized before
|
2024-06-01 02:33:53 +00:00
|
|
|
SetCodeEntryOrLiteral(entry);
|
2024-03-08 10:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Method::ClearAOTFlagsWhenInit()
|
|
|
|
{
|
|
|
|
SetAotCodeBit(false);
|
|
|
|
SetIsFastCall(false);
|
|
|
|
}
|
2024-03-27 14:46:50 +00:00
|
|
|
|
2024-06-04 07:18:15 +00:00
|
|
|
void Method::InitInterpreterStatusForCompiledMethod(const JSThread *thread)
|
|
|
|
{
|
|
|
|
if (!IsAotWithCallField()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool isFastCall = IsFastCall();
|
|
|
|
uintptr_t entry =
|
|
|
|
isFastCall ? thread->GetRTInterface(kungfu::RuntimeStubCSigns::ID_FastCallToAsmInterBridge)
|
|
|
|
: thread->GetRTInterface(kungfu::RuntimeStubCSigns::ID_AOTCallToAsmInterBridge);
|
|
|
|
SetCodeEntryOrLiteral(entry);
|
|
|
|
ClearAOTFlagsWhenInit();
|
|
|
|
SetDeoptType(kungfu::DeoptType::INIT_AOT_FAILED);
|
|
|
|
}
|
2022-04-22 03:03:38 +00:00
|
|
|
} // namespace panda::ecmascript
|