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_INTERPRETER_INTERPRETER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_INTERPRETER_INTERPRETER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h"
224514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h"
234514f5e3Sopenharmony_ci#include "ecmascript/frames.h"
244514f5e3Sopenharmony_ci#include "ecmascript/method.h"
254514f5e3Sopenharmony_ci#include "ecmascript/require/js_cjs_module.h"
264514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
274514f5e3Sopenharmony_ci#include "libpandafile/bytecode_instruction-inl.h"
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_cinamespace panda::ecmascript {
304514f5e3Sopenharmony_ciclass ConstantPool;
314514f5e3Sopenharmony_ciclass ECMAObject;
324514f5e3Sopenharmony_ciclass GeneratorContext;
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_ciusing EcmaOpcode = BytecodeInstruction::Opcode;
354514f5e3Sopenharmony_ciconst uint8_t EXCEPTION_OPCODE = static_cast<uint8_t>(EcmaOpcode::NOP) + 8;
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ciclass EcmaInterpreter {
384514f5e3Sopenharmony_cipublic:
394514f5e3Sopenharmony_ci    static const int16_t METHOD_HOTNESS_THRESHOLD = 0x700;
404514f5e3Sopenharmony_ci    static const int16_t METHOD_HOTNESS_THRESHOLD_FACTOR = 10;
414514f5e3Sopenharmony_ci    static const int16_t CANCEL_METHOD_HOTNESS_THRESHOLD = 0;
424514f5e3Sopenharmony_ci    enum ActualNumArgsOfCall : uint8_t { CALLARG0 = 0, CALLARG1, CALLARGS2, CALLARGS3 };
434514f5e3Sopenharmony_ci
444514f5e3Sopenharmony_ci    static JSTaggedValue Execute(EcmaRuntimeCallInfo *info);
454514f5e3Sopenharmony_ci    static JSTaggedValue ExecuteNative(EcmaRuntimeCallInfo *info);
464514f5e3Sopenharmony_ci    static EcmaRuntimeCallInfo* NewRuntimeCallInfo(
474514f5e3Sopenharmony_ci        JSThread *thread, JSTaggedValue func, JSTaggedValue thisObj, JSTaggedValue newTarget,
484514f5e3Sopenharmony_ci        uint32_t numArgs, StackCheck needCheckStack = StackCheck::YES);
494514f5e3Sopenharmony_ci    static EcmaRuntimeCallInfo* NewRuntimeCallInfo(
504514f5e3Sopenharmony_ci        JSThread *thread, JSHandle<JSTaggedValue> func, JSHandle<JSTaggedValue> thisObj,
514514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> newTarget, uint32_t numArgs, StackCheck needCheckStack = StackCheck::YES);
524514f5e3Sopenharmony_ci    static EcmaRuntimeCallInfo* ReBuildRuntimeCallInfo(
534514f5e3Sopenharmony_ci        JSThread *thread, EcmaRuntimeCallInfo* info, int numArgs, StackCheck needCheckStack = StackCheck::YES);
544514f5e3Sopenharmony_ci    static JSTaggedValue GeneratorReEnterInterpreter(JSThread *thread, JSHandle<GeneratorContext> context);
554514f5e3Sopenharmony_ci    static JSTaggedValue GeneratorReEnterAot(JSThread *thread, JSHandle<GeneratorContext> context);
564514f5e3Sopenharmony_ci#ifndef EXCLUDE_C_INTERPRETER
574514f5e3Sopenharmony_ci    static void RunInternal(JSThread *thread, const uint8_t *pc, JSTaggedType *sp);
584514f5e3Sopenharmony_ci#endif
594514f5e3Sopenharmony_ci    static void InitStackFrame(JSThread *thread);
604514f5e3Sopenharmony_ci    static void InitStackFrame(EcmaContext *context);
614514f5e3Sopenharmony_ci    static size_t GetJumpSizeAfterCall(const uint8_t *prevPc);
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_ci    static JSTaggedValue GetRuntimeProfileTypeInfo(JSTaggedType *sp);
644514f5e3Sopenharmony_ci    static JSTaggedValue GetConstantPool(JSTaggedType *sp);
654514f5e3Sopenharmony_ci    static JSTaggedValue GetUnsharedConstpool(JSThread* thread, JSTaggedType *sp);
664514f5e3Sopenharmony_ci    static JSTaggedValue GetEcmaModule(JSTaggedType *sp);
674514f5e3Sopenharmony_ci    static bool UpdateHotnessCounter(JSThread* thread, JSTaggedType *sp, JSTaggedValue acc, int32_t offset);
684514f5e3Sopenharmony_ci    static void NotifyBytecodePcChanged(JSThread *thread);
694514f5e3Sopenharmony_ci    static void NotifyDebuggerStmt(JSThread *thread);
704514f5e3Sopenharmony_ci    static void MethodEntry(JSThread *thread);
714514f5e3Sopenharmony_ci    static void MethodExit(JSThread *thread);
724514f5e3Sopenharmony_ci    static const JSPandaFile *GetNativeCallPandafile(JSThread *thread);
734514f5e3Sopenharmony_ci    static std::pair<CString, CString> GetCurrentEntryPoint(JSThread *thread);
744514f5e3Sopenharmony_ci    static JSTaggedValue GetFunction(JSTaggedType *sp);
754514f5e3Sopenharmony_ci    static JSTaggedValue GetNewTarget(JSTaggedType *sp);
764514f5e3Sopenharmony_ci    static JSTaggedValue GetThis(JSTaggedType *sp);
774514f5e3Sopenharmony_ci    static uint32_t GetNumArgs(JSTaggedType *sp, uint32_t restIdx, uint32_t &startIdx);
784514f5e3Sopenharmony_ci    static bool IsFastNewFrameEnter(JSFunction *ctor, JSHandle<Method> method);
794514f5e3Sopenharmony_ci    static bool IsFastNewFrameExit(JSTaggedType *sp);
804514f5e3Sopenharmony_ci    static int16_t GetHotnessCounter(uint32_t codeSize, bool cancelThreshold);
814514f5e3Sopenharmony_ci    static JSTaggedType *GetInterpreterFrameEnd(JSThread *thread, JSTaggedType *sp);
824514f5e3Sopenharmony_ci    static void UpdateProfileTypeInfoCellToFunction(JSThread *thread, JSHandle<JSFunction> &function,
834514f5e3Sopenharmony_ci                                                    JSTaggedValue profileTypeInfo, uint16_t slotId);
844514f5e3Sopenharmony_ciprivate:
854514f5e3Sopenharmony_ci    static void InitStackFrameForSP(JSTaggedType *prevSp);
864514f5e3Sopenharmony_ci    static EcmaRuntimeCallInfo* NewRuntimeCallInfoBase(
874514f5e3Sopenharmony_ci        JSThread *thread, JSTaggedType func, JSTaggedType thisObj, JSTaggedType newTarget,
884514f5e3Sopenharmony_ci        uint32_t numArgs, StackCheck needCheckStack = StackCheck::YES);
894514f5e3Sopenharmony_ci};
904514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
914514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_INTERPRETER_INTERPRETER_H
92