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 inline JSTaggedValue GeneratorReEnterInterpreter(JSThread *thread, JSHandle<GeneratorContext> context); 48 static inline JSTaggedValue GeneratorReEnterAot(JSThread *thread, JSHandle<GeneratorContext> context); 49 static inline void RunInternal(JSThread *thread, const uint8_t *pc, JSTaggedType *sp); 50 static inline void InitStackFrame(JSThread *thread); 51 static inline uint32_t FindCatchBlock(Method *caller, uint32_t pc); 52 static inline size_t GetJumpSizeAfterCall(const uint8_t *prevPc); 53 54 static inline JSTaggedValue GetRuntimeProfileTypeInfo(JSTaggedType *sp); 55 static inline JSTaggedValue GetConstantPool(JSTaggedType *sp); 56 static inline JSTaggedValue GetEcmaModule(JSTaggedType *sp); 57 static inline bool UpdateHotnessCounter(JSThread* thread, JSTaggedType *sp, JSTaggedValue acc, int32_t offset); 58 static inline void NotifyBytecodePcChanged(JSThread *thread); 59 static inline const JSPandaFile *GetNativeCallPandafile(JSThread *thread); 60 static inline JSTaggedValue GetCurrentEntryPoint(JSThread *thread); 61 static inline JSTaggedValue GetFunction(JSTaggedType *sp); 62 static inline JSTaggedValue GetNewTarget(JSTaggedType *sp); 63 static inline JSTaggedValue GetThis(JSTaggedType *sp); 64 static inline uint32_t GetNumArgs(JSTaggedType *sp, uint32_t restIdx, uint32_t &startIdx); 65 static inline bool IsFastNewFrameEnter(JSFunction *ctor, JSHandle<Method> method); 66 static inline bool IsFastNewFrameExit(JSTaggedType *sp); 67 static inline int16_t GetHotnessCounter(uint32_t codeSize); 68 static inline JSTaggedType *GetInterpreterFrameEnd(JSThread *thread, JSTaggedType *sp); 69 70 71 }; 72 } // namespace panda::ecmascript 73 #endif // ECMASCRIPT_INTERPRETER_INTERPRETER_H 74