1 /* 2 * Copyright (c) 2021 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_INCLUDE_PANDA_VM_H_ 17 #define PANDA_RUNTIME_INCLUDE_PANDA_VM_H_ 18 19 #include "include/runtime_options.h" 20 #include "runtime/include/method.h" 21 #include "runtime/include/runtime.h" 22 #include "runtime/mem/gc/gc_phase.h" 23 24 #include "libpandabase/utils/expected.h" 25 26 namespace panda { 27 28 class ManagedThread; 29 class StringTable; 30 class ThreadManager; 31 32 namespace mem { 33 class HeapManager; 34 class GC; 35 class GCTrigger; 36 class ReferenceProcessor; 37 } // namespace mem 38 39 enum class PandaVMType : size_t { CORE_VM, JAVA_VM, ECMA_VM }; 40 41 class PandaVM { 42 public: 43 static PandaVM *Create(Runtime *runtime, const RuntimeOptions &options, std::string_view runtime_type); 44 45 PandaVM() = default; 46 virtual ~PandaVM() = default; 47 GetCurrent()48 static PandaVM *GetCurrent() 49 { 50 return Thread::GetCurrent()->GetVM(); 51 } 52 53 virtual bool Initialize() = 0; 54 virtual bool InitializeFinish() = 0; 55 virtual void PreStartup() = 0; 56 virtual void PreZygoteFork() = 0; 57 virtual void PostZygoteFork() = 0; 58 virtual void InitializeGC() = 0; 59 virtual void StartGC() = 0; 60 virtual void StopGC() = 0; 61 virtual void VisitVmRoots(const GCRootVisitor &visitor) = 0; 62 virtual void UpdateVmRefs() = 0; 63 virtual void UninitializeThreads() = 0; 64 65 virtual Expected<int, Runtime::Error> InvokeEntrypoint(Method *entrypoint, const std::vector<std::string> &args); 66 67 // NOLINTNEXTLINE(performance-unnecessary-value-param) HandleReferences(const GCTask & task)68 virtual void HandleReferences([[maybe_unused]] const GCTask &task) {} HandleEnqueueReferences()69 virtual void HandleEnqueueReferences() {} HandleBufferData(bool reverse)70 virtual void HandleBufferData([[maybe_unused]] bool reverse) {} HandleGCFinished()71 virtual void HandleGCFinished() {} 72 73 virtual mem::GCStats *GetGCStats() const = 0; 74 virtual mem::HeapManager *GetHeapManager() const = 0; 75 virtual mem::GC *GetGC() const = 0; 76 virtual mem::GCTrigger *GetGCTrigger() const = 0; 77 virtual const RuntimeOptions &GetOptions() const = 0; 78 virtual ManagedThread *GetAssociatedThread() const = 0; 79 virtual StringTable *GetStringTable() const = 0; 80 virtual mem::MemStatsType *GetMemStats() const = 0; 81 virtual Rendezvous *GetRendezvous() const = 0; 82 virtual mem::GlobalObjectStorage *GetGlobalObjectStorage() const = 0; 83 virtual MonitorPool *GetMonitorPool() const = 0; 84 virtual ThreadManager *GetThreadManager() const = 0; 85 86 // remove this method after fixing interpreter performance 87 virtual PandaVMType GetPandaVMType() const = 0; 88 virtual LanguageContext GetLanguageContext() const = 0; 89 virtual CompilerInterface *GetCompiler() const = 0; 90 91 virtual panda::mem::ReferenceProcessor *GetReferenceProcessor() const = 0; 92 93 virtual ObjectHeader *GetOOMErrorObject() = 0; 94 95 NO_MOVE_SEMANTIC(PandaVM); 96 NO_COPY_SEMANTIC(PandaVM); 97 98 protected: 99 virtual bool CheckEntrypointSignature(Method *entrypoint) = 0; 100 virtual Expected<int, Runtime::Error> InvokeEntrypointImpl(Method *entrypoint, 101 const std::vector<std::string> &args) = 0; 102 virtual void HandleUncaughtException(ObjectHeader *exception) = 0; 103 }; 104 105 } // namespace panda 106 107 #endif // PANDA_RUNTIME_INCLUDE_PANDA_VM_H_ 108