14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 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#ifndef ECMASCRIPT_COMPILER_INTERPRETER_STUB_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_INTERPRETER_STUB_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/base/config.h" 204514f5e3Sopenharmony_ci#include "ecmascript/compiler/bc_call_signature.h" 214514f5e3Sopenharmony_ci#include "ecmascript/compiler/profiler_operation.h" 224514f5e3Sopenharmony_ci#include "ecmascript/compiler/rt_call_signature.h" 234514f5e3Sopenharmony_ci#include "ecmascript/compiler/stub_builder.h" 244514f5e3Sopenharmony_ci#include "ecmascript/compiler/circuit_builder-inl.h" 254514f5e3Sopenharmony_ci 264514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu { 274514f5e3Sopenharmony_ciclass StringIdInfo { 284514f5e3Sopenharmony_cipublic: 294514f5e3Sopenharmony_ci enum class Offset : uint8_t { 304514f5e3Sopenharmony_ci BYTE_0, 314514f5e3Sopenharmony_ci BYTE_1, 324514f5e3Sopenharmony_ci BYTE_2, 334514f5e3Sopenharmony_ci INVALID, 344514f5e3Sopenharmony_ci }; 354514f5e3Sopenharmony_ci enum class Length : uint8_t { 364514f5e3Sopenharmony_ci BITS_16, 374514f5e3Sopenharmony_ci BITS_32, 384514f5e3Sopenharmony_ci INVALID, 394514f5e3Sopenharmony_ci }; 404514f5e3Sopenharmony_ci enum class StringIdType : uint8_t { 414514f5e3Sopenharmony_ci STRING_ID, 424514f5e3Sopenharmony_ci STRING_ID_INFO, 434514f5e3Sopenharmony_ci }; 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_ci StringIdInfo() : constpool(0), pc(0), offset(Offset::INVALID), 464514f5e3Sopenharmony_ci length(Length::INVALID), stringId(0), stringIdType(StringIdType::STRING_ID_INFO) {} 474514f5e3Sopenharmony_ci 484514f5e3Sopenharmony_ci StringIdInfo(GateRef inputConstpool, GateRef inputPc, Offset inputOffset, Length inputLength) 494514f5e3Sopenharmony_ci : constpool(inputConstpool), pc(inputPc), offset(inputOffset), 504514f5e3Sopenharmony_ci length(inputLength), stringId(0), stringIdType(StringIdType::STRING_ID_INFO) {} 514514f5e3Sopenharmony_ci 524514f5e3Sopenharmony_ci explicit StringIdInfo(GateRef inputConstpool, GateRef inputStringId) 534514f5e3Sopenharmony_ci : constpool(inputConstpool), pc(0), offset(Offset::INVALID), 544514f5e3Sopenharmony_ci length(Length::INVALID), stringId(inputStringId), stringIdType(StringIdType::STRING_ID) {} 554514f5e3Sopenharmony_ci 564514f5e3Sopenharmony_ci GateRef GetConstantPool() const 574514f5e3Sopenharmony_ci { 584514f5e3Sopenharmony_ci return constpool; 594514f5e3Sopenharmony_ci } 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci GateRef GetPc() const 624514f5e3Sopenharmony_ci { 634514f5e3Sopenharmony_ci return pc; 644514f5e3Sopenharmony_ci } 654514f5e3Sopenharmony_ci 664514f5e3Sopenharmony_ci Offset GetOffset() const 674514f5e3Sopenharmony_ci { 684514f5e3Sopenharmony_ci return offset; 694514f5e3Sopenharmony_ci } 704514f5e3Sopenharmony_ci 714514f5e3Sopenharmony_ci Length GetLength() const 724514f5e3Sopenharmony_ci { 734514f5e3Sopenharmony_ci return length; 744514f5e3Sopenharmony_ci } 754514f5e3Sopenharmony_ci 764514f5e3Sopenharmony_ci GateRef GetStringId() const 774514f5e3Sopenharmony_ci { 784514f5e3Sopenharmony_ci return stringId; 794514f5e3Sopenharmony_ci } 804514f5e3Sopenharmony_ci 814514f5e3Sopenharmony_ci StringIdType GetStringIdType() const 824514f5e3Sopenharmony_ci { 834514f5e3Sopenharmony_ci return stringIdType; 844514f5e3Sopenharmony_ci } 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_ci bool IsValid() const 874514f5e3Sopenharmony_ci { 884514f5e3Sopenharmony_ci if (stringIdType == StringIdType::STRING_ID_INFO) { 894514f5e3Sopenharmony_ci return (constpool != 0) && (pc != 0) && (offset != Offset::INVALID) && (length != Length::INVALID); 904514f5e3Sopenharmony_ci } 914514f5e3Sopenharmony_ci return stringId != 0; 924514f5e3Sopenharmony_ci } 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ciprivate: 954514f5e3Sopenharmony_ci GateRef constpool { 0 }; 964514f5e3Sopenharmony_ci GateRef pc { 0 }; 974514f5e3Sopenharmony_ci Offset offset { Offset::INVALID }; 984514f5e3Sopenharmony_ci Length length { Length::INVALID }; 994514f5e3Sopenharmony_ci GateRef stringId { 0 }; 1004514f5e3Sopenharmony_ci StringIdType stringIdType { StringIdType::STRING_ID_INFO }; 1014514f5e3Sopenharmony_ci}; 1024514f5e3Sopenharmony_ci 1034514f5e3Sopenharmony_ciclass InterpreterStubBuilder : public StubBuilder { 1044514f5e3Sopenharmony_cipublic: 1054514f5e3Sopenharmony_ci InterpreterStubBuilder(CallSignature *callSignature, Environment *env) 1064514f5e3Sopenharmony_ci : StubBuilder(callSignature, env) {} 1074514f5e3Sopenharmony_ci ~InterpreterStubBuilder() override = default; 1084514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(InterpreterStubBuilder); 1094514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(InterpreterStubBuilder); 1104514f5e3Sopenharmony_ci virtual void GenerateCircuit() override = 0; 1114514f5e3Sopenharmony_ci 1124514f5e3Sopenharmony_ci inline void SetVregValue(GateRef glue, GateRef sp, GateRef idx, GateRef val); 1134514f5e3Sopenharmony_ci inline GateRef GetVregValue(GateRef sp, GateRef idx); 1144514f5e3Sopenharmony_ci inline GateRef ReadInst4_0(GateRef pc); 1154514f5e3Sopenharmony_ci inline GateRef ReadInst4_1(GateRef pc); 1164514f5e3Sopenharmony_ci inline GateRef ReadInst4_2(GateRef pc); 1174514f5e3Sopenharmony_ci inline GateRef ReadInst4_3(GateRef pc); 1184514f5e3Sopenharmony_ci inline GateRef ReadInst8_0(GateRef pc); 1194514f5e3Sopenharmony_ci inline GateRef ReadInst8_1(GateRef pc); 1204514f5e3Sopenharmony_ci inline GateRef ReadInst8_2(GateRef pc); 1214514f5e3Sopenharmony_ci inline GateRef ReadInst8_3(GateRef pc); 1224514f5e3Sopenharmony_ci inline GateRef ReadInst8_4(GateRef pc); 1234514f5e3Sopenharmony_ci inline GateRef ReadInst8_5(GateRef pc); 1244514f5e3Sopenharmony_ci inline GateRef ReadInst8_6(GateRef pc); 1254514f5e3Sopenharmony_ci inline GateRef ReadInst8_7(GateRef pc); 1264514f5e3Sopenharmony_ci inline GateRef ReadInst8_8(GateRef pc); 1274514f5e3Sopenharmony_ci inline GateRef ReadInst8_9(GateRef pc); 1284514f5e3Sopenharmony_ci inline GateRef ReadInst16_0(GateRef pc); 1294514f5e3Sopenharmony_ci inline GateRef ReadInst16_1(GateRef pc); 1304514f5e3Sopenharmony_ci inline GateRef ReadInst16_2(GateRef pc); 1314514f5e3Sopenharmony_ci inline GateRef ReadInst16_3(GateRef pc); 1324514f5e3Sopenharmony_ci inline GateRef ReadInst16_4(GateRef pc); 1334514f5e3Sopenharmony_ci inline GateRef ReadInst16_5(GateRef pc); 1344514f5e3Sopenharmony_ci inline GateRef ReadInst16_6(GateRef pc); 1354514f5e3Sopenharmony_ci inline GateRef ReadInst16_7(GateRef pc); 1364514f5e3Sopenharmony_ci inline GateRef ReadInstSigned8_0(GateRef pc); 1374514f5e3Sopenharmony_ci inline GateRef ReadInstSigned16_0(GateRef pc); 1384514f5e3Sopenharmony_ci inline GateRef ReadInstSigned32_0(GateRef pc); 1394514f5e3Sopenharmony_ci inline GateRef ReadInst32_0(GateRef pc); 1404514f5e3Sopenharmony_ci inline GateRef ReadInst32_1(GateRef pc); 1414514f5e3Sopenharmony_ci inline GateRef ReadInst32_2(GateRef pc); 1424514f5e3Sopenharmony_ci inline GateRef ReadInst64_0(GateRef pc); 1434514f5e3Sopenharmony_ci 1444514f5e3Sopenharmony_ci inline GateRef GetFrame(GateRef frame); 1454514f5e3Sopenharmony_ci inline GateRef GetCurrentSpFrame(GateRef glue); 1464514f5e3Sopenharmony_ci inline GateRef GetLastLeaveFrame(GateRef glue); 1474514f5e3Sopenharmony_ci inline GateRef GetCurrentFrame(GateRef glue); 1484514f5e3Sopenharmony_ci inline GateRef GetPcFromFrame(GateRef frame); 1494514f5e3Sopenharmony_ci inline GateRef GetCallSizeFromFrame(GateRef frame); 1504514f5e3Sopenharmony_ci inline GateRef GetFunctionFromFrame(GateRef frame); 1514514f5e3Sopenharmony_ci inline GateRef GetNewTarget(GateRef sp); 1524514f5e3Sopenharmony_ci inline GateRef GetThisFromFrame(GateRef frame); 1534514f5e3Sopenharmony_ci inline GateRef GetAccFromFrame(GateRef frame); 1544514f5e3Sopenharmony_ci inline GateRef GetEnvFromFrame(GateRef frame); 1554514f5e3Sopenharmony_ci inline GateRef GetEnvFromFunction(GateRef frame); 1564514f5e3Sopenharmony_ci inline GateRef GetConstpoolFromMethod(GateRef function); 1574514f5e3Sopenharmony_ci inline GateRef GetModule(GateRef sp); 1584514f5e3Sopenharmony_ci inline GateRef GetProfileTypeInfoFromFunction(GateRef function); 1594514f5e3Sopenharmony_ci inline GateRef GetModuleFromFunction(GateRef function); 1604514f5e3Sopenharmony_ci inline GateRef GetSendableEnvFromModule(GateRef function); 1614514f5e3Sopenharmony_ci inline GateRef GetHomeObjectFromFunction(GateRef function); 1624514f5e3Sopenharmony_ci inline GateRef GetResumeModeFromGeneratorObject(GateRef obj); 1634514f5e3Sopenharmony_ci inline GateRef GetResumeModeFromAsyncGeneratorObject(GateRef obj); 1644514f5e3Sopenharmony_ci inline GateRef GetHotnessCounterFromMethod(GateRef method); 1654514f5e3Sopenharmony_ci 1664514f5e3Sopenharmony_ci inline void SetCurrentSpFrame(GateRef glue, GateRef sp); 1674514f5e3Sopenharmony_ci inline void SetLastLeaveFrame(GateRef glue, GateRef sp); 1684514f5e3Sopenharmony_ci inline void SetPcToFrame(GateRef glue, GateRef frame, GateRef value); 1694514f5e3Sopenharmony_ci inline void SetCallSizeToFrame(GateRef glue, GateRef frame, GateRef value); 1704514f5e3Sopenharmony_ci inline void SetFunctionToFrame(GateRef glue, GateRef frame, GateRef value); 1714514f5e3Sopenharmony_ci inline void SetAccToFrame(GateRef glue, GateRef frame, GateRef value); 1724514f5e3Sopenharmony_ci inline void SetEnvToFrame(GateRef glue, GateRef frame, GateRef value); 1734514f5e3Sopenharmony_ci inline void SetHomeObjectToFunction(GateRef glue, GateRef function, GateRef value); 1744514f5e3Sopenharmony_ci inline void SetFrameState(GateRef glue, GateRef sp, GateRef function, GateRef acc, 1754514f5e3Sopenharmony_ci GateRef env, GateRef pc, GateRef prev, GateRef type); 1764514f5e3Sopenharmony_ci 1774514f5e3Sopenharmony_ci inline void UpdateProfileTypeInfoCellToFunction(GateRef glue, GateRef function, 1784514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef slotId); 1794514f5e3Sopenharmony_ci 1804514f5e3Sopenharmony_ci inline void CheckException(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 1814514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, 1824514f5e3Sopenharmony_ci GateRef res, GateRef offset); 1834514f5e3Sopenharmony_ci inline void CheckPendingException(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 1844514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, 1854514f5e3Sopenharmony_ci GateRef res, GateRef offset); 1864514f5e3Sopenharmony_ci inline void CheckExceptionWithJump(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 1874514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, 1884514f5e3Sopenharmony_ci GateRef res, Label *jump); 1894514f5e3Sopenharmony_ci inline void CheckExceptionWithVar(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 1904514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, 1914514f5e3Sopenharmony_ci GateRef res, GateRef offset); 1924514f5e3Sopenharmony_ci 1934514f5e3Sopenharmony_ci inline GateRef CheckStackOverflow(GateRef glue, GateRef sp); 1944514f5e3Sopenharmony_ci inline GateRef PushArg(GateRef glue, GateRef sp, GateRef value); 1954514f5e3Sopenharmony_ci inline GateRef PushUndefined(GateRef glue, GateRef sp, GateRef num); 1964514f5e3Sopenharmony_ci inline GateRef PushRange(GateRef glue, GateRef sp, GateRef array, GateRef startIndex, GateRef endIndex); 1974514f5e3Sopenharmony_ci inline GateRef GetStartIdxAndNumArgs(GateRef sp, GateRef restIdx); 1984514f5e3Sopenharmony_ci inline void Dispatch(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 1994514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, GateRef format); 2004514f5e3Sopenharmony_ci inline void DispatchWithId(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 2014514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, GateRef index); 2024514f5e3Sopenharmony_ci inline void DispatchLast(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 2034514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter); 2044514f5e3Sopenharmony_ci inline void DispatchDebugger(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 2054514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter); 2064514f5e3Sopenharmony_ci inline void DispatchDebuggerLast(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, 2074514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter); 2084514f5e3Sopenharmony_ci 2094514f5e3Sopenharmony_ci template <bool needPrint> 2104514f5e3Sopenharmony_ci void DebugPrintInstruction(); 2114514f5e3Sopenharmony_ciprivate: 2124514f5e3Sopenharmony_ci template<typename... Args> 2134514f5e3Sopenharmony_ci void DispatchBase(GateRef target, GateRef glue, Args... args); 2144514f5e3Sopenharmony_ci}; 2154514f5e3Sopenharmony_ci 2164514f5e3Sopenharmony_ciclass InterpreterToolsStubBuilder : private InterpreterStubBuilder { 2174514f5e3Sopenharmony_cipublic: 2184514f5e3Sopenharmony_ci InterpreterToolsStubBuilder(CallSignature *callSignature, Environment *env) 2194514f5e3Sopenharmony_ci : InterpreterStubBuilder(callSignature, env) {} 2204514f5e3Sopenharmony_ci ~InterpreterToolsStubBuilder() override = default; 2214514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(InterpreterToolsStubBuilder); 2224514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(InterpreterToolsStubBuilder); 2234514f5e3Sopenharmony_ci void GenerateCircuit() override {} 2244514f5e3Sopenharmony_ci 2254514f5e3Sopenharmony_ci inline GateRef GetStringId(const StringIdInfo &info); 2264514f5e3Sopenharmony_ci}; 2274514f5e3Sopenharmony_ci 2284514f5e3Sopenharmony_ci#define DECLARE_HANDLE_STUB_CLASS(name) \ 2294514f5e3Sopenharmony_ci class name##StubBuilder : public InterpreterStubBuilder { \ 2304514f5e3Sopenharmony_ci public: \ 2314514f5e3Sopenharmony_ci explicit name##StubBuilder(CallSignature *callSignature, Environment *env) \ 2324514f5e3Sopenharmony_ci : InterpreterStubBuilder(callSignature, env) \ 2334514f5e3Sopenharmony_ci { \ 2344514f5e3Sopenharmony_ci env->GetCircuit()->SetFrameType(FrameType::ASM_INTERPRETER_FRAME); \ 2354514f5e3Sopenharmony_ci } \ 2364514f5e3Sopenharmony_ci ~name##StubBuilder() = default; \ 2374514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(name##StubBuilder); \ 2384514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(name##StubBuilder); \ 2394514f5e3Sopenharmony_ci void GenerateCircuit() override; \ 2404514f5e3Sopenharmony_ci \ 2414514f5e3Sopenharmony_ci protected: \ 2424514f5e3Sopenharmony_ci void GenerateCircuitImpl(GateRef glue, GateRef sp, GateRef pc, GateRef constpool, \ 2434514f5e3Sopenharmony_ci GateRef profileTypeInfo, GateRef acc, GateRef hotnessCounter, \ 2444514f5e3Sopenharmony_ci ProfileOperation callback); \ 2454514f5e3Sopenharmony_ci }; 2464514f5e3Sopenharmony_ci INTERPRETER_BC_STUB_LIST(DECLARE_HANDLE_STUB_CLASS) 2474514f5e3Sopenharmony_ci ASM_INTERPRETER_BC_HELPER_STUB_LIST(DECLARE_HANDLE_STUB_CLASS) 2484514f5e3Sopenharmony_ci 2494514f5e3Sopenharmony_ci#define DECLARE_HANDLE_PROFILE_STUB_CLASS(name, base, ...) \ 2504514f5e3Sopenharmony_ci class name##StubBuilder : public base##StubBuilder { \ 2514514f5e3Sopenharmony_ci public: \ 2524514f5e3Sopenharmony_ci explicit name##StubBuilder(CallSignature *callSignature, Environment *env) \ 2534514f5e3Sopenharmony_ci : base##StubBuilder(callSignature, env) \ 2544514f5e3Sopenharmony_ci { \ 2554514f5e3Sopenharmony_ci } \ 2564514f5e3Sopenharmony_ci ~name##StubBuilder() = default; \ 2574514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(name##StubBuilder); \ 2584514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(name##StubBuilder); \ 2594514f5e3Sopenharmony_ci void GenerateCircuit() override; \ 2604514f5e3Sopenharmony_ci }; 2614514f5e3Sopenharmony_ci ASM_INTERPRETER_BC_PROFILER_STUB_LIST(DECLARE_HANDLE_PROFILE_STUB_CLASS) 2624514f5e3Sopenharmony_ci#undef DECLARE_HANDLE_PROFILE_STUB_CLASS 2634514f5e3Sopenharmony_ci#define DECLARE_HANDLE_JIT_PROFILE_STUB_CLASS(name, base, ...) \ 2644514f5e3Sopenharmony_ci class name##StubBuilder : public base##StubBuilder { \ 2654514f5e3Sopenharmony_ci public: \ 2664514f5e3Sopenharmony_ci explicit name##StubBuilder(CallSignature *callSignature, Environment *env) \ 2674514f5e3Sopenharmony_ci : base##StubBuilder(callSignature, env) \ 2684514f5e3Sopenharmony_ci { \ 2694514f5e3Sopenharmony_ci } \ 2704514f5e3Sopenharmony_ci ~name##StubBuilder() = default; \ 2714514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(name##StubBuilder); \ 2724514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(name##StubBuilder); \ 2734514f5e3Sopenharmony_ci void GenerateCircuit() override; \ 2744514f5e3Sopenharmony_ci }; 2754514f5e3Sopenharmony_ci ASM_INTERPRETER_BC_JIT_PROFILER_STUB_LIST(DECLARE_HANDLE_JIT_PROFILE_STUB_CLASS) 2764514f5e3Sopenharmony_ci#undef DECLARE_HANDLE_PROFILE_STUB_CLASS 2774514f5e3Sopenharmony_ci#undef DECLARE_HANDLE_STUB_CLASS 2784514f5e3Sopenharmony_ci} // namespace panda::ecmascript::kungfu 2794514f5e3Sopenharmony_ci#endif // ECMASCRIPT_COMPILER_INTERPRETER_STUB_H 280