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) + 3; 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, JSHandle<JSTaggedValue> func, JSHandle<JSTaggedValue> thisObj, 46 JSHandle<JSTaggedValue> newTarget, uint32_t numArgs, bool needCheckStack = true); 47 static EcmaRuntimeCallInfo* ReBuildRuntimeCallInfo( 48 JSThread *thread, EcmaRuntimeCallInfo* info, uint32_t numArgs, bool needCheckStack = true); 49 static inline JSTaggedValue GeneratorReEnterInterpreter(JSThread *thread, JSHandle<GeneratorContext> context); 50 static inline JSTaggedValue GeneratorReEnterAot(JSThread *thread, JSHandle<GeneratorContext> context); 51 #ifndef EXCLUDE_C_INTERPRETER 52 static inline void RunInternal(JSThread *thread, const uint8_t *pc, JSTaggedType *sp); 53 #endif 54 static inline void InitStackFrame(JSThread *thread); 55 static inline void InitStackFrame(EcmaContext *context); 56 static inline size_t GetJumpSizeAfterCall(const uint8_t *prevPc); 57 58 static inline JSTaggedValue GetRuntimeProfileTypeInfo(JSTaggedType *sp); 59 static inline JSTaggedValue GetConstantPool(JSTaggedType *sp); 60 static inline JSTaggedValue GetEcmaModule(JSTaggedType *sp); 61 static inline bool UpdateHotnessCounter(JSThread* thread, JSTaggedType *sp, JSTaggedValue acc, int32_t offset); 62 static inline void NotifyBytecodePcChanged(JSThread *thread); 63 static inline void NotifyDebuggerStmt(JSThread *thread); 64 static inline void MethodEntry(JSThread *thread); 65 static inline void MethodExit(JSThread *thread); 66 static inline const JSPandaFile *GetNativeCallPandafile(JSThread *thread); 67 static inline JSTaggedValue GetCurrentEntryPoint(JSThread *thread); 68 static inline JSTaggedValue GetFunction(JSTaggedType *sp); 69 static inline JSTaggedValue GetNewTarget(JSTaggedType *sp); 70 static inline JSTaggedValue GetThis(JSTaggedType *sp); 71 static inline uint32_t GetNumArgs(JSTaggedType *sp, uint32_t restIdx, uint32_t &startIdx); 72 static inline bool IsFastNewFrameEnter(JSFunction *ctor, JSHandle<Method> method); 73 static inline bool IsFastNewFrameExit(JSTaggedType *sp); 74 static inline int16_t GetHotnessCounter(uint32_t codeSize); 75 static inline JSTaggedType *GetInterpreterFrameEnd(JSThread *thread, JSTaggedType *sp); 76 private: 77 static inline void InitStackFrameForSP(JSTaggedType *prevSp); 78 }; 79 } // namespace panda::ecmascript 80 #endif // ECMASCRIPT_INTERPRETER_INTERPRETER_H 81