14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/method.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/jspandafile/program_object.h" 194514f5e3Sopenharmony_ci 204514f5e3Sopenharmony_cinamespace panda::ecmascript { 214514f5e3Sopenharmony_cistd::string Method::ParseFunctionName() const 224514f5e3Sopenharmony_ci{ 234514f5e3Sopenharmony_ci const JSPandaFile *jsPandaFile = GetJSPandaFile(); 244514f5e3Sopenharmony_ci return MethodLiteral::ParseFunctionName(jsPandaFile, GetMethodId()); 254514f5e3Sopenharmony_ci} 264514f5e3Sopenharmony_ci 274514f5e3Sopenharmony_cistd::pair<std::string_view, bool> Method::ParseFunctionNameView() const 284514f5e3Sopenharmony_ci{ 294514f5e3Sopenharmony_ci const JSPandaFile *jsPandaFile = GetJSPandaFile(); 304514f5e3Sopenharmony_ci return MethodLiteral::ParseFunctionNameView(jsPandaFile, GetMethodId()); 314514f5e3Sopenharmony_ci} 324514f5e3Sopenharmony_ci 334514f5e3Sopenharmony_ciconst char *Method::GetMethodName() const 344514f5e3Sopenharmony_ci{ 354514f5e3Sopenharmony_ci const JSPandaFile *jsPandaFile = GetJSPandaFile(); 364514f5e3Sopenharmony_ci return MethodLiteral::GetMethodName(jsPandaFile, GetMethodId()); 374514f5e3Sopenharmony_ci} 384514f5e3Sopenharmony_ci 394514f5e3Sopenharmony_ciconst char *Method::GetMethodName(const JSPandaFile *file) const 404514f5e3Sopenharmony_ci{ 414514f5e3Sopenharmony_ci return MethodLiteral::GetMethodName(file, GetMethodId()); 424514f5e3Sopenharmony_ci} 434514f5e3Sopenharmony_ci 444514f5e3Sopenharmony_ciconst CString Method::GetRecordNameStr() const 454514f5e3Sopenharmony_ci{ 464514f5e3Sopenharmony_ci const JSPandaFile *jsPandaFile = GetJSPandaFile(); 474514f5e3Sopenharmony_ci return MethodLiteral::GetRecordName(jsPandaFile, GetMethodId()); 484514f5e3Sopenharmony_ci} 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_ciuint32_t Method::GetCodeSize() const 514514f5e3Sopenharmony_ci{ 524514f5e3Sopenharmony_ci const JSPandaFile *jsPandaFile = GetJSPandaFile(); 534514f5e3Sopenharmony_ci return MethodLiteral::GetCodeSize(jsPandaFile, GetMethodId()); 544514f5e3Sopenharmony_ci} 554514f5e3Sopenharmony_ci 564514f5e3Sopenharmony_ciconst JSPandaFile *Method::GetJSPandaFile() const 574514f5e3Sopenharmony_ci{ 584514f5e3Sopenharmony_ci JSTaggedValue constpool = GetConstantPool(); 594514f5e3Sopenharmony_ci if (constpool.IsUndefined()) { 604514f5e3Sopenharmony_ci return nullptr; 614514f5e3Sopenharmony_ci } 624514f5e3Sopenharmony_ci 634514f5e3Sopenharmony_ci const ConstantPool *taggedPool = ConstantPool::Cast(constpool.GetTaggedObject()); 644514f5e3Sopenharmony_ci return taggedPool->GetJSPandaFile(); 654514f5e3Sopenharmony_ci} 664514f5e3Sopenharmony_ci 674514f5e3Sopenharmony_ciMethodLiteral *Method::GetMethodLiteral() const 684514f5e3Sopenharmony_ci{ 694514f5e3Sopenharmony_ci if (IsAotWithCallField() || IsDeoptimized()) { 704514f5e3Sopenharmony_ci ASSERT(!IsNativeWithCallField()); 714514f5e3Sopenharmony_ci const JSPandaFile *jsPandaFile = GetJSPandaFile(); 724514f5e3Sopenharmony_ci ASSERT(jsPandaFile != nullptr); 734514f5e3Sopenharmony_ci return jsPandaFile->FindMethodLiteral(GetMethodId().GetOffset()); 744514f5e3Sopenharmony_ci } 754514f5e3Sopenharmony_ci return reinterpret_cast<MethodLiteral *>(GetCodeEntryOrLiteral()); 764514f5e3Sopenharmony_ci} 774514f5e3Sopenharmony_ci 784514f5e3Sopenharmony_cibool Method::IsDeoptimized() const 794514f5e3Sopenharmony_ci{ 804514f5e3Sopenharmony_ci return GetDeoptType() != kungfu::DeoptType::NONE; 814514f5e3Sopenharmony_ci} 824514f5e3Sopenharmony_ci 834514f5e3Sopenharmony_ciuint32_t Method::FindCatchBlock(uint32_t pc) const 844514f5e3Sopenharmony_ci{ 854514f5e3Sopenharmony_ci ASSERT(!IsNativeWithCallField()); 864514f5e3Sopenharmony_ci ASSERT(GetJSPandaFile() != nullptr); 874514f5e3Sopenharmony_ci auto *pandaFile = GetJSPandaFile()->GetPandaFile(); 884514f5e3Sopenharmony_ci ASSERT(pandaFile != nullptr); 894514f5e3Sopenharmony_ci panda_file::MethodDataAccessor mda(*pandaFile, GetMethodId()); 904514f5e3Sopenharmony_ci panda_file::CodeDataAccessor cda(*pandaFile, mda.GetCodeId().value()); 914514f5e3Sopenharmony_ci 924514f5e3Sopenharmony_ci uint32_t pcOffset = INVALID_INDEX; 934514f5e3Sopenharmony_ci cda.EnumerateTryBlocks([&pcOffset, pc](panda_file::CodeDataAccessor::TryBlock &tryBlock) { 944514f5e3Sopenharmony_ci if ((tryBlock.GetStartPc() <= pc) && ((tryBlock.GetStartPc() + tryBlock.GetLength()) > pc)) { 954514f5e3Sopenharmony_ci tryBlock.EnumerateCatchBlocks([&](panda_file::CodeDataAccessor::CatchBlock &catchBlock) { 964514f5e3Sopenharmony_ci pcOffset = catchBlock.GetHandlerPc(); 974514f5e3Sopenharmony_ci return false; 984514f5e3Sopenharmony_ci }); 994514f5e3Sopenharmony_ci } 1004514f5e3Sopenharmony_ci return pcOffset == INVALID_INDEX; 1014514f5e3Sopenharmony_ci }); 1024514f5e3Sopenharmony_ci return pcOffset; 1034514f5e3Sopenharmony_ci} 1044514f5e3Sopenharmony_ci 1054514f5e3Sopenharmony_cibool Method::HasCatchBlock() const 1064514f5e3Sopenharmony_ci{ 1074514f5e3Sopenharmony_ci ASSERT(GetJSPandaFile() != nullptr); 1084514f5e3Sopenharmony_ci auto *pandaFile = GetJSPandaFile()->GetPandaFile(); 1094514f5e3Sopenharmony_ci ASSERT(pandaFile != nullptr); 1104514f5e3Sopenharmony_ci panda_file::MethodDataAccessor mda(*pandaFile, GetMethodId()); 1114514f5e3Sopenharmony_ci panda_file::CodeDataAccessor cda(*pandaFile, mda.GetCodeId().value()); 1124514f5e3Sopenharmony_ci return cda.GetTriesSize() != 0; 1134514f5e3Sopenharmony_ci} 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ciJSHandle<Method> Method::Create(JSThread *thread, const JSPandaFile *jsPandaFile, MethodLiteral *methodLiteral) 1164514f5e3Sopenharmony_ci{ 1174514f5e3Sopenharmony_ci EcmaVM *vm = thread->GetEcmaVM(); 1184514f5e3Sopenharmony_ci EntityId methodId = methodLiteral->GetMethodId(); 1194514f5e3Sopenharmony_ci JSTaggedValue patchVal = vm->GetQuickFixManager()->CheckAndGetPatch(thread, jsPandaFile, methodId); 1204514f5e3Sopenharmony_ci if (!patchVal.IsHole()) { 1214514f5e3Sopenharmony_ci return JSHandle<Method>(thread, patchVal); 1224514f5e3Sopenharmony_ci } 1234514f5e3Sopenharmony_ci 1244514f5e3Sopenharmony_ci JSHandle<Method> method; 1254514f5e3Sopenharmony_ci method = vm->GetFactory()->NewSMethod(methodLiteral); 1264514f5e3Sopenharmony_ci JSHandle<ConstantPool> newConstpool = thread->GetCurrentEcmaContext()->FindOrCreateConstPool(jsPandaFile, methodId); 1274514f5e3Sopenharmony_ci method->SetConstantPool(thread, newConstpool); 1284514f5e3Sopenharmony_ci return method; 1294514f5e3Sopenharmony_ci} 1304514f5e3Sopenharmony_ci 1314514f5e3Sopenharmony_civoid Method::SetCodeEntryAndMarkAOTWhenBinding(uintptr_t codeEntry) 1324514f5e3Sopenharmony_ci{ 1334514f5e3Sopenharmony_ci SetAotCodeBit(true); 1344514f5e3Sopenharmony_ci SetNativeBit(false); 1354514f5e3Sopenharmony_ci SetCodeEntryOrLiteral(codeEntry); 1364514f5e3Sopenharmony_ci} 1374514f5e3Sopenharmony_ci 1384514f5e3Sopenharmony_civoid Method::ClearAOTStatusWhenDeopt(uintptr_t entry) 1394514f5e3Sopenharmony_ci{ 1404514f5e3Sopenharmony_ci ClearAOTFlagsWhenInit(); 1414514f5e3Sopenharmony_ci // Do not clear deopt type, which records a method has deoptimized before 1424514f5e3Sopenharmony_ci SetCodeEntryOrLiteral(entry); 1434514f5e3Sopenharmony_ci} 1444514f5e3Sopenharmony_ci 1454514f5e3Sopenharmony_civoid Method::ClearAOTFlagsWhenInit() 1464514f5e3Sopenharmony_ci{ 1474514f5e3Sopenharmony_ci SetAotCodeBit(false); 1484514f5e3Sopenharmony_ci SetIsFastCall(false); 1494514f5e3Sopenharmony_ci} 1504514f5e3Sopenharmony_ci 1514514f5e3Sopenharmony_civoid Method::InitInterpreterStatusForCompiledMethod(const JSThread *thread) 1524514f5e3Sopenharmony_ci{ 1534514f5e3Sopenharmony_ci if (!IsAotWithCallField()) { 1544514f5e3Sopenharmony_ci return; 1554514f5e3Sopenharmony_ci } 1564514f5e3Sopenharmony_ci bool isFastCall = IsFastCall(); 1574514f5e3Sopenharmony_ci uintptr_t entry = 1584514f5e3Sopenharmony_ci isFastCall ? thread->GetRTInterface(kungfu::RuntimeStubCSigns::ID_FastCallToAsmInterBridge) 1594514f5e3Sopenharmony_ci : thread->GetRTInterface(kungfu::RuntimeStubCSigns::ID_AOTCallToAsmInterBridge); 1604514f5e3Sopenharmony_ci SetCodeEntryOrLiteral(entry); 1614514f5e3Sopenharmony_ci ClearAOTFlagsWhenInit(); 1624514f5e3Sopenharmony_ci SetDeoptType(kungfu::DeoptType::INIT_AOT_FAILED); 1634514f5e3Sopenharmony_ci} 1644514f5e3Sopenharmony_ci} // namespace panda::ecmascript 165