1 /** 2 * Copyright (c) 2021-2022 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 panda { 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() 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 string_table_; 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 ThreadManager *GetThreadManager() const override 102 { 103 return thread_manager_; 104 } 105 GetMonitorPool()106 MonitorPool *GetMonitorPool() const override 107 { 108 return monitor_pool_; 109 } 110 GetGlobalObjectStorage()111 mem::GlobalObjectStorage *GetGlobalObjectStorage() const override 112 { 113 return mm_->GetGlobalObjectStorage(); 114 } 115 GetReferenceProcessor()116 panda::mem::ReferenceProcessor *GetReferenceProcessor() const override 117 { 118 ASSERT(reference_processor_ != nullptr); 119 return reference_processor_; 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([[maybe_unused]] const panda_file::File &pf, 133 [[maybe_unused]] panda_file::File::EntityId id) override 134 { 135 coretypes::String *str = GetStringTable()->GetInternalStringFast(pf, id); 136 if (str != nullptr) { 137 return str; 138 } 139 str = GetStringTable()->GetOrInternInternalString(pf, id, GetLanguageContext()); 140 return str; 141 } 142 GetRendezvous()143 Rendezvous *GetRendezvous() const override 144 { 145 return rendezvous_; 146 } 147 GetCompilerRuntimeInterface()148 compiler::RuntimeInterface *GetCompilerRuntimeInterface() const override 149 { 150 return runtime_iface_; 151 } 152 153 ObjectHeader *GetOOMErrorObject() override; 154 155 protected: 156 bool CheckEntrypointSignature(Method *entrypoint) override; 157 Expected<int, Runtime::Error> InvokeEntrypointImpl(Method *entrypoint, 158 const std::vector<std::string> &args) override; 159 void HandleUncaughtException() override; 160 161 private: 162 explicit PandaCoreVM(Runtime *runtime, const RuntimeOptions &options, mem::MemoryManager *mm); 163 164 void PreAllocOOMErrorObject(); 165 166 Runtime *runtime_ {nullptr}; 167 mem::MemoryManager *mm_ {nullptr}; 168 mem::ReferenceProcessor *reference_processor_ {nullptr}; 169 PandaVector<ObjectHeader *> gc_roots_; 170 Rendezvous *rendezvous_ {nullptr}; 171 CompilerInterface *compiler_ {nullptr}; 172 MTManagedThread *main_thread_ {nullptr}; 173 StringTable *string_table_ {nullptr}; 174 MonitorPool *monitor_pool_ {nullptr}; 175 ThreadManager *thread_manager_ {nullptr}; 176 panda::mem::Reference *oom_obj_ref_ {nullptr}; 177 compiler::RuntimeInterface *runtime_iface_ {nullptr}; 178 179 NO_MOVE_SEMANTIC(PandaCoreVM); 180 NO_COPY_SEMANTIC(PandaCoreVM); 181 182 friend class mem::Allocator; 183 }; 184 185 } // namespace core 186 } // namespace panda 187 188 #endif // PANDA_RUNTIME_VM_CORE_CORE_VM_H_ 189