1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_INTERPRETER_INTERPRETER_H 17 #define ECMASCRIPT_INTERPRETER_INTERPRETER_H 18 19 #include "ecmascript/ecma_runtime_call_info.h" 20 #include "ecmascript/js_tagged_value.h" 21 #include "ecmascript/js_handle.h" 22 #include "ecmascript/js_thread.h" 23 #include "ecmascript/frames.h" 24 #include "ecmascript/method.h" 25 #include "ecmascript/require/js_cjs_module.h" 26 #include "libpandafile/bytecode_instruction-inl.h" 27 28 namespace panda::ecmascript { 29 class ConstantPool; 30 class ECMAObject; 31 class GeneratorContext; 32 33 using EcmaOpcode = BytecodeInstruction::Opcode; 34 const uint8_t EXCEPTION_OPCODE = static_cast<uint8_t>(EcmaOpcode::NOP) + 7; 35 36 class EcmaInterpreter { 37 public: 38 static const int16_t METHOD_HOTNESS_THRESHOLD = 0x700; 39 static const int16_t METHOD_HOTNESS_THRESHOLD_FACTOR = 10; 40 enum ActualNumArgsOfCall : uint8_t { CALLARG0 = 0, CALLARG1, CALLARGS2, CALLARGS3 }; 41 42 static inline JSTaggedValue Execute(EcmaRuntimeCallInfo *info); 43 static inline JSTaggedValue ExecuteNative(EcmaRuntimeCallInfo *info); 44 static EcmaRuntimeCallInfo* NewRuntimeCallInfo( 45 JSThread *thread, JSTaggedValue func, JSTaggedValue thisObj, JSTaggedValue newTarget, 46 uint32_t numArgs, bool needCheckStack = true); 47 static EcmaRuntimeCallInfo* NewRuntimeCallInfo( 48 JSThread *thread, JSHandle<JSTaggedValue> func, JSHandle<JSTaggedValue> thisObj, 49 JSHandle<JSTaggedValue> newTarget, uint32_t numArgs, bool needCheckStack = true); 50 static EcmaRuntimeCallInfo* ReBuildRuntimeCallInfo( 51 JSThread *thread, EcmaRuntimeCallInfo* info, uint32_t numArgs, bool needCheckStack = true); 52 static inline JSTaggedValue GeneratorReEnterInterpreter(JSThread *thread, JSHandle<GeneratorContext> context); 53 static inline JSTaggedValue GeneratorReEnterAot(JSThread *thread, JSHandle<GeneratorContext> context); 54 #ifndef EXCLUDE_C_INTERPRETER 55 static inline void RunInternal(JSThread *thread, const uint8_t *pc, JSTaggedType *sp); 56 #endif 57 static inline void InitStackFrame(JSThread *thread); 58 static inline void InitStackFrame(EcmaContext *context); 59 static inline size_t GetJumpSizeAfterCall(const uint8_t *prevPc); 60 61 static inline JSTaggedValue GetRuntimeProfileTypeInfo(JSTaggedType *sp); 62 static inline JSTaggedValue GetConstantPool(JSTaggedType *sp); 63 static inline JSTaggedValue GetEcmaModule(JSTaggedType *sp); 64 static inline bool UpdateHotnessCounter(JSThread* thread, JSTaggedType *sp, JSTaggedValue acc, int32_t offset); 65 static inline void NotifyBytecodePcChanged(JSThread *thread); 66 static inline void NotifyDebuggerStmt(JSThread *thread); 67 static inline void MethodEntry(JSThread *thread); 68 static inline void MethodExit(JSThread *thread); 69 static inline const JSPandaFile *GetNativeCallPandafile(JSThread *thread); 70 static inline std::pair<JSTaggedValue, JSTaggedValue> GetCurrentEntryPoint(JSThread *thread); 71 static inline JSTaggedValue GetFunction(JSTaggedType *sp); 72 static inline JSTaggedValue GetNewTarget(JSTaggedType *sp); 73 static inline JSTaggedValue GetThis(JSTaggedType *sp); 74 static inline uint32_t GetNumArgs(JSTaggedType *sp, uint32_t restIdx, uint32_t &startIdx); 75 static inline bool IsFastNewFrameEnter(JSFunction *ctor, JSHandle<Method> method); 76 static inline bool IsFastNewFrameExit(JSTaggedType *sp); 77 static inline int16_t GetHotnessCounter(uint32_t codeSize); 78 static inline JSTaggedType *GetInterpreterFrameEnd(JSThread *thread, JSTaggedType *sp); 79 private: 80 static inline void InitStackFrameForSP(JSTaggedType *prevSp); 81 static inline EcmaRuntimeCallInfo* NewRuntimeCallInfoBase( 82 JSThread *thread, JSTaggedType func, JSTaggedType thisObj, JSTaggedType newTarget, 83 uint32_t numArgs, bool needCheckStack = true); 84 }; 85 } // namespace panda::ecmascript 86 #endif // ECMASCRIPT_INTERPRETER_INTERPRETER_H 87