1 /** 2 * Copyright (c) 2021-2025 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 PANDA_RUNTIME_VM_CORE_CORE_VM_H_ 17 #define PANDA_RUNTIME_VM_CORE_CORE_VM_H_ 18 19 #include "libpandabase/macros.h" 20 #include "libpandabase/utils/expected.h" 21 #include "runtime/include/compiler_interface.h" 22 #include "runtime/include/mem/panda_smart_pointers.h" 23 #include "runtime/include/mem/panda_string.h" 24 #include "runtime/include/panda_vm.h" 25 #include "runtime/mem/gc/gc_phase.h" 26 #include "runtime/mem/refstorage/reference.h" 27 28 namespace ark { 29 30 class Method; 31 class Runtime; 32 class Compiler; 33 34 namespace core { 35 36 class PandaCoreVM final : public PandaVM { 37 public: 38 static Expected<PandaCoreVM *, PandaString> Create(Runtime *runtime, const RuntimeOptions &options); 39 ~PandaCoreVM() override; 40 41 static PandaCoreVM *GetCurrent(); 42 43 bool Initialize() override; 44 bool InitializeFinish() override; 45 void UninitializeThreads() override; 46 47 void PreStartup() override; 48 void PreZygoteFork() override; 49 void PostZygoteFork() override; 50 void InitializeGC() override; 51 void StartGC() override; 52 void StopGC() override; 53 54 void HandleReferences(const GCTask &task, const mem::GC::ReferenceClearPredicateT &pred) override; 55 void HandleEnqueueReferences() override; 56 void HandleGCFinished() override; 57 58 void VisitVmRoots(const GCRootVisitor & /* visitor */) override; 59 void UpdateVmRefs(const GCRootUpdater &gcRootUpdater) override; 60 GetHeapManager()61 mem::HeapManager *GetHeapManager() const override 62 { 63 return mm_->GetHeapManager(); 64 } 65 GetGC()66 mem::GC *GetGC() const override 67 { 68 return mm_->GetGC(); 69 } 70 GetGCTrigger()71 mem::GCTrigger *GetGCTrigger() const override 72 { 73 return mm_->GetGCTrigger(); 74 } 75 GetGCStats()76 mem::GCStats *GetGCStats() const override 77 { 78 return mm_->GetGCStats(); 79 } 80 GetAssociatedThread()81 ManagedThread *GetAssociatedThread() const override 82 { 83 return ManagedThread::GetCurrent(); 84 } 85 GetStringTable()86 StringTable *GetStringTable() const override 87 { 88 return stringTable_; 89 } 90 GetMemStats()91 mem::MemStatsType *GetMemStats() const override 92 { 93 return mm_->GetMemStats(); 94 } 95 GetOptions()96 const RuntimeOptions &GetOptions() const override 97 { 98 return Runtime::GetOptions(); 99 } 100 GetThreadManager()101 MTThreadManager *GetThreadManager() const override 102 { 103 return threadManager_; 104 } 105 GetMonitorPool()106 MonitorPool *GetMonitorPool() const override 107 { 108 return monitorPool_; 109 } 110 GetGlobalObjectStorage()111 mem::GlobalObjectStorage *GetGlobalObjectStorage() const override 112 { 113 return mm_->GetGlobalObjectStorage(); 114 } 115 GetReferenceProcessor()116 ark::mem::ReferenceProcessor *GetReferenceProcessor() const override 117 { 118 ASSERT(referenceProcessor_ != nullptr); 119 return referenceProcessor_; 120 } 121 GetLanguageContext()122 LanguageContext GetLanguageContext() const override 123 { 124 return Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY); 125 } 126 GetCompiler()127 CompilerInterface *GetCompiler() const override 128 { 129 return compiler_; 130 } 131 ResolveString(const panda_file::File & pf,panda_file::File::EntityId id)132 coretypes::String *ResolveString(const panda_file::File &pf, panda_file::File::EntityId id) override 133 { 134 coretypes::String *str = GetStringTable()->GetInternalStringFast(pf, id); 135 if (str != nullptr) { 136 return str; 137 } 138 str = GetStringTable()->GetOrInternInternalString(pf, id, GetLanguageContext()); 139 return str; 140 } 141 ResolveString(Frame * frame,panda_file::File::EntityId id)142 coretypes::String *ResolveString(Frame *frame, panda_file::File::EntityId id) override 143 { 144 return PandaCoreVM::ResolveString(*frame->GetMethod()->GetPandaFile(), id); 145 } 146 GetRendezvous()147 Rendezvous *GetRendezvous() const override 148 { 149 return rendezvous_; 150 } 151 GetCompilerRuntimeInterface()152 compiler::RuntimeInterface *GetCompilerRuntimeInterface() const override 153 { 154 return runtimeIface_; 155 } 156 IsStaticProfileEnabled()157 bool IsStaticProfileEnabled() const override 158 { 159 return true; 160 } 161 162 ObjectHeader *GetOOMErrorObject() override; 163 164 protected: 165 bool CheckEntrypointSignature(Method *entrypoint) override; 166 Expected<int, Runtime::Error> InvokeEntrypointImpl(Method *entrypoint, 167 const std::vector<std::string> &args) override; 168 [[noreturn]] void HandleUncaughtException() override; 169 170 private: 171 explicit PandaCoreVM(Runtime *runtime, const RuntimeOptions &options, mem::MemoryManager *mm); 172 173 void PreAllocOOMErrorObject(); 174 175 Runtime *runtime_ {nullptr}; 176 mem::MemoryManager *mm_ {nullptr}; 177 mem::ReferenceProcessor *referenceProcessor_ {nullptr}; 178 PandaVector<ObjectHeader *> gcRoots_; 179 Rendezvous *rendezvous_ {nullptr}; 180 CompilerInterface *compiler_ {nullptr}; 181 MTManagedThread *mainThread_ {nullptr}; 182 StringTable *stringTable_ {nullptr}; 183 MonitorPool *monitorPool_ {nullptr}; 184 MTThreadManager *threadManager_ {nullptr}; 185 ark::mem::Reference *oomObjRef_ {nullptr}; 186 compiler::RuntimeInterface *runtimeIface_ {nullptr}; 187 188 NO_MOVE_SEMANTIC(PandaCoreVM); 189 NO_COPY_SEMANTIC(PandaCoreVM); 190 191 friend class mem::Allocator; 192 }; 193 194 } // namespace core 195 } // namespace ark 196 197 #endif // PANDA_RUNTIME_VM_CORE_CORE_VM_H_ 198