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 "ecmascript/object_factory.h"
27 #include "libpandafile/bytecode_instruction-inl.h"
28 
29 namespace panda::ecmascript {
30 class ConstantPool;
31 class ECMAObject;
32 class GeneratorContext;
33 
34 using EcmaOpcode = BytecodeInstruction::Opcode;
35 const uint8_t EXCEPTION_OPCODE = static_cast<uint8_t>(EcmaOpcode::NOP) + 8;
36 
37 class EcmaInterpreter {
38 public:
39     static const int16_t METHOD_HOTNESS_THRESHOLD = 0x700;
40     static const int16_t METHOD_HOTNESS_THRESHOLD_FACTOR = 10;
41     static const int16_t CANCEL_METHOD_HOTNESS_THRESHOLD = 0;
42     enum ActualNumArgsOfCall : uint8_t { CALLARG0 = 0, CALLARG1, CALLARGS2, CALLARGS3 };
43 
44     static JSTaggedValue Execute(EcmaRuntimeCallInfo *info);
45     static JSTaggedValue ExecuteNative(EcmaRuntimeCallInfo *info);
46     static EcmaRuntimeCallInfo* NewRuntimeCallInfo(
47         JSThread *thread, JSTaggedValue func, JSTaggedValue thisObj, JSTaggedValue newTarget,
48         uint32_t numArgs, StackCheck needCheckStack = StackCheck::YES);
49     static EcmaRuntimeCallInfo* NewRuntimeCallInfo(
50         JSThread *thread, JSHandle<JSTaggedValue> func, JSHandle<JSTaggedValue> thisObj,
51         JSHandle<JSTaggedValue> newTarget, uint32_t numArgs, StackCheck needCheckStack = StackCheck::YES);
52     static EcmaRuntimeCallInfo* ReBuildRuntimeCallInfo(
53         JSThread *thread, EcmaRuntimeCallInfo* info, int numArgs, StackCheck needCheckStack = StackCheck::YES);
54     static JSTaggedValue GeneratorReEnterInterpreter(JSThread *thread, JSHandle<GeneratorContext> context);
55     static JSTaggedValue GeneratorReEnterAot(JSThread *thread, JSHandle<GeneratorContext> context);
56 #ifndef EXCLUDE_C_INTERPRETER
57     static void RunInternal(JSThread *thread, const uint8_t *pc, JSTaggedType *sp);
58 #endif
59     static void InitStackFrame(JSThread *thread);
60     static void InitStackFrame(EcmaContext *context);
61     static size_t GetJumpSizeAfterCall(const uint8_t *prevPc);
62 
63     static JSTaggedValue GetRuntimeProfileTypeInfo(JSTaggedType *sp);
64     static JSTaggedValue GetConstantPool(JSTaggedType *sp);
65     static JSTaggedValue GetUnsharedConstpool(JSThread* thread, JSTaggedType *sp);
66     static JSTaggedValue GetEcmaModule(JSTaggedType *sp);
67     static bool UpdateHotnessCounter(JSThread* thread, JSTaggedType *sp, JSTaggedValue acc, int32_t offset);
68     static void NotifyBytecodePcChanged(JSThread *thread);
69     static void NotifyDebuggerStmt(JSThread *thread);
70     static void MethodEntry(JSThread *thread);
71     static void MethodExit(JSThread *thread);
72     static const JSPandaFile *GetNativeCallPandafile(JSThread *thread);
73     static std::pair<CString, CString> GetCurrentEntryPoint(JSThread *thread);
74     static JSTaggedValue GetFunction(JSTaggedType *sp);
75     static JSTaggedValue GetNewTarget(JSTaggedType *sp);
76     static JSTaggedValue GetThis(JSTaggedType *sp);
77     static uint32_t GetNumArgs(JSTaggedType *sp, uint32_t restIdx, uint32_t &startIdx);
78     static bool IsFastNewFrameEnter(JSFunction *ctor, JSHandle<Method> method);
79     static bool IsFastNewFrameExit(JSTaggedType *sp);
80     static int16_t GetHotnessCounter(uint32_t codeSize, bool cancelThreshold);
81     static JSTaggedType *GetInterpreterFrameEnd(JSThread *thread, JSTaggedType *sp);
82     static void UpdateProfileTypeInfoCellToFunction(JSThread *thread, JSHandle<JSFunction> &function,
83                                                     JSTaggedValue profileTypeInfo, uint16_t slotId);
84 private:
85     static void InitStackFrameForSP(JSTaggedType *prevSp);
86     static EcmaRuntimeCallInfo* NewRuntimeCallInfoBase(
87         JSThread *thread, JSTaggedType func, JSTaggedType thisObj, JSTaggedType newTarget,
88         uint32_t numArgs, StackCheck needCheckStack = StackCheck::YES);
89 };
90 }  // namespace panda::ecmascript
91 #endif  // ECMASCRIPT_INTERPRETER_INTERPRETER_H
92