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_COMPILATION_ENV_H
17#define ECMASCRIPT_COMPILER_COMPILATION_ENV_H
18
19#include "ecmascript/global_env.h"
20#include "ecmascript/js_handle.h"
21#include "ecmascript/jspandafile/method_literal.h"
22#include "ecmascript/pgo_profiler/pgo_utils.h"
23
24namespace panda::ecmascript {
25namespace kungfu {
26class PGOTypeManager;
27};
28class ConstantPool;
29namespace pgo {
30class PGOProfiler;
31};
32class JSThread;
33
34class CompilationEnv {
35public:
36    CompilationEnv(EcmaVM *vm);
37    virtual ~CompilationEnv() = default;
38    virtual bool IsJitCompiler() const
39    {
40        return false;
41    }
42    virtual bool IsAotCompiler() const
43    {
44        return false;
45    }
46    EcmaVM *GetEcmaVM() const
47    {
48        return vm_;
49    }
50
51    JSThread *GetJSThread() const
52    {
53        return thread_;
54    }
55
56    kungfu::PGOTypeManager *GetPTManager() const
57    {
58        return ptManager_;
59    }
60
61    NativeAreaAllocator *GetNativeAreaAllocator() const;
62    virtual JSRuntimeOptions &GetJSOptions() = 0;
63    virtual std::shared_ptr<pgo::PGOProfiler> GetPGOProfiler() const;
64
65    // thread
66    virtual const CMap<ElementsKind, std::pair<ConstantIndex, ConstantIndex>> &GetArrayHClassIndexMap() const = 0;
67    virtual const BuiltinHClassEntries &GetBuiltinHClassEntries() const = 0;
68    virtual JSHClass *GetBuiltinPrototypeHClass(BuiltinTypeId type) const = 0;
69
70    // context
71    virtual JSTaggedValue FindConstpool(const JSPandaFile *jsPandaFile, panda_file::File::EntityId id) const = 0;
72    virtual JSTaggedValue FindConstpool(const JSPandaFile *jsPandaFile, int32_t index) const = 0;
73    virtual JSTaggedValue FindOrCreateUnsharedConstpool(const uint32_t methodOffset) const = 0;
74    virtual JSTaggedValue FindOrCreateUnsharedConstpool(JSTaggedValue sharedConstpool) const = 0;
75    virtual JSHandle<ConstantPool> FindOrCreateConstPool(const JSPandaFile *jsPandaFile,
76        panda_file::File::EntityId id) = 0;
77    virtual JSTaggedValue GetConstantPoolByMethodOffset(const uint32_t methodOffset) const = 0;
78
79    // ConstantPool
80    virtual JSTaggedValue GetArrayLiteralFromCache(JSTaggedValue constpool, uint32_t index, CString entry) const = 0;
81    virtual JSTaggedValue GetObjectLiteralFromCache(JSTaggedValue constpool, uint32_t index, CString entry) const = 0;
82    virtual JSTaggedValue GetMethodFromCache(JSTaggedValue constpool, uint32_t index) const = 0;
83    virtual panda_file::File::EntityId GetIdFromCache(JSTaggedValue constpool, uint32_t index) const = 0;
84    virtual JSTaggedValue GetStringFromConstantPool(const uint32_t methodOffset, const uint16_t cpIdx,
85        bool allowAlloc = true) const = 0;
86
87    // GlobalEnv
88    virtual JSHandle<GlobalEnv> GetGlobalEnv() const = 0;
89
90    // GlobalConstants
91    virtual const GlobalEnvConstants *GlobalConstants() const = 0;
92
93    virtual JSThread *GetHostThread() const
94    {
95        ASSERT(0);
96        return nullptr;
97    }
98
99    virtual JSPandaFile *GetJSPandaFile() const
100    {
101        ASSERT(0);
102        return nullptr;
103    }
104
105    virtual MethodLiteral *GetMethodLiteral() const
106    {
107        ASSERT(0);
108        return nullptr;
109    }
110
111    virtual const uint8_t *GetMethodPcStart() const
112    {
113        ASSERT(0);
114        return nullptr;
115    }
116
117    virtual pgo::ApEntityId GetMethodAbcId() const
118    {
119        ASSERT(0);
120        return 0;
121    }
122
123protected:
124    EcmaVM *vm_ {nullptr};
125    JSThread *thread_ {nullptr};
126    kungfu::PGOTypeManager *ptManager_ {nullptr};
127};
128} // namespace panda::ecmascript
129#endif  // ECMASCRIPT_COMPILER_COMPILATION_ENV_H
130