1/*
2 * Copyright (c) 2024 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_COMPILER_JIT_COMPILATION_ENV_H
17#define ECMASCRIPT_COMPILER_JIT_COMPILATION_ENV_H
18
19#include "ecmascript/compiler/compilation_env.h"
20#include "ecmascript/ic/profile_type_info.h"
21
22namespace panda::ecmascript {
23class JitCompilationEnv final : public CompilationEnv {
24public:
25    JitCompilationEnv(EcmaVM *vm, EcmaVM *hVm, JSHandle<JSFunction> &jsFunction);
26    ~JitCompilationEnv() = default;
27    bool IsJitCompiler() const override
28    {
29        return true;
30    }
31    JSRuntimeOptions &GetJSOptions() override;
32    // thread
33    const CMap<ElementsKind, std::pair<ConstantIndex, ConstantIndex>> &GetArrayHClassIndexMap() const override;
34    const BuiltinHClassEntries &GetBuiltinHClassEntries() const override;
35    JSHClass *GetBuiltinPrototypeHClass(BuiltinTypeId type) const override;
36    void SetTsManagerCompilationEnv();
37
38    std::shared_ptr<pgo::PGOProfiler> GetPGOProfiler() const override;
39
40    // context
41    JSTaggedValue FindConstpool(const JSPandaFile *jsPandaFile, panda_file::File::EntityId id) const override;
42    JSTaggedValue FindConstpool(const JSPandaFile *jsPandaFile, int32_t index) const override;
43    JSTaggedValue FindOrCreateUnsharedConstpool(const uint32_t methodOffset) const override;
44    JSTaggedValue FindOrCreateUnsharedConstpool(JSTaggedValue sharedConstpool) const override;
45    JSHandle<ConstantPool> FindOrCreateConstPool(const JSPandaFile *jsPandaFile,
46        panda_file::File::EntityId id) override;
47    JSTaggedValue GetConstantPoolByMethodOffset(const uint32_t methodOffset) const override;
48
49    // ConstantPool
50    JSTaggedValue GetArrayLiteralFromCache(JSTaggedValue constpool, uint32_t index, CString entry) const override;
51    JSTaggedValue GetObjectLiteralFromCache(JSTaggedValue constpool, uint32_t index, CString entry) const override;
52    JSTaggedValue GetMethodFromCache(JSTaggedValue constpool, uint32_t index) const override;
53    panda_file::File::EntityId GetIdFromCache(JSTaggedValue constpool, uint32_t index) const override;
54
55    // GlobalEnv
56    JSHandle<GlobalEnv> GetGlobalEnv() const override;
57
58    // GlobalConstants
59    const GlobalEnvConstants *GlobalConstants() const override;
60
61    JSTaggedValue GetStringFromConstantPool(const uint32_t methodOffset, const uint16_t cpIdx,
62        bool allowAlloc = true) const override;
63
64    JSThread *GetHostThread() const override
65    {
66        return hostThread_;
67    }
68
69    JSPandaFile *GetJSPandaFile() const override
70    {
71        return jsPandaFile_;
72    }
73
74    MethodLiteral *GetMethodLiteral() const override
75    {
76        return methodLiteral_;
77    }
78
79    const uint8_t *GetMethodPcStart() const override
80    {
81        return pcStart_;
82    }
83
84    pgo::ApEntityId GetMethodAbcId() const override
85    {
86        return abcId_;
87    }
88
89    void SetProfileTypeInfo(const JSHandle<ProfileTypeInfo> &info)
90    {
91        profileTypeInfo_ = info;
92    }
93
94    void UpdateFuncSlotIdMap(uint32_t calleeOffset, uint32_t callerOffset, uint32_t slotId)
95    {
96        if (functionSlotIdMap_.find(calleeOffset) != functionSlotIdMap_.end()) {
97            return;
98        }
99        if (callee2CallerMap_.find(calleeOffset) != callee2CallerMap_.end()) {
100            return;
101        }
102        functionSlotIdMap_[calleeOffset] = slotId;
103        callee2CallerMap_[calleeOffset] = callerOffset;
104    }
105
106    JSFunction *GetJsFunctionByMethodOffset(uint32_t methodOffset) const;
107private:
108    JSThread *hostThread_ {nullptr};
109    JSHandle<JSFunction> jsFunction_;
110    JSPandaFile *jsPandaFile_ {nullptr};
111    MethodLiteral *methodLiteral_ {nullptr};
112    const uint8_t* pcStart_ {nullptr};
113    pgo::ApEntityId abcId_ {0};
114    JSHandle<ProfileTypeInfo> profileTypeInfo_;
115    std::map<uint32_t, uint32_t> functionSlotIdMap_;
116    std::map<uint32_t, uint32_t> callee2CallerMap_;
117};
118} // namespace panda::ecmascript
119#endif  // ECMASCRIPT_COMPILER_JIT_COMPILATION_ENV_H
120