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_JIT_COMPILE_DECISION_H 17 #define ECMASCRIPT_JIT_COMPILE_DECISION_H 18 19 #include "ecmascript/ecma_vm.h" 20 #include "ecmascript/js_handle.h" 21 #include "ecmascript/js_function.h" 22 23 namespace panda::ecmascript { 24 class JitCompileMode { 25 public: 26 enum class Mode : uint8_t { 27 SYNC = 0, 28 ASYNC 29 }; 30 JitCompileMode(Mode mode)31 JitCompileMode(Mode mode) : mode_(mode) {} 32 IsAsync()33 bool IsAsync() const 34 { 35 return mode_ == Mode::ASYNC; 36 } 37 IsSync()38 bool IsSync() const 39 { 40 return mode_ == Mode::SYNC; 41 } 42 43 private: 44 Mode mode_; 45 }; 46 47 class CompilerTier { 48 public: 49 enum class Tier : uint8_t { 50 BASELINE, 51 FAST, 52 }; 53 CompilerTier(Tier tier)54 CompilerTier(Tier tier) : tier_(tier) {} IsFast()55 bool IsFast() const 56 { 57 return tier_ == Tier::FAST; 58 } 59 IsBaseLine()60 bool IsBaseLine() const 61 { 62 return tier_ == Tier::BASELINE; 63 } 64 65 friend std::ostream &operator<<(std::ostream &os, const CompilerTier &tier) 66 { 67 os << (tier.IsFast() ? "[fastjit] " : " [baselinejit] "); 68 return os; 69 } 70 71 private: 72 Tier tier_; 73 }; 74 75 class MethodNameCollector { 76 public: 77 void Init(EcmaVM *vm); 78 inline void Collect(const std::string& methodFullName) const; 79 ~MethodNameCollector(); 80 private: 81 mutable std::ofstream file_; 82 bool enable_ {false}; 83 bool isInit_ {false}; 84 }; 85 86 class MethodNameFilter { 87 public: 88 void Init(EcmaVM *vm); 89 inline bool NeedCompiledByJit(const std::string& methodFullName) const; 90 ~MethodNameFilter(); 91 private: 92 std::set<std::string> methodFullNames; 93 bool enable_ {false}; 94 bool isInit_ {false}; 95 }; 96 97 class CompileDecision { 98 public: 99 CompileDecision(EcmaVM *vm, JSHandle<JSFunction> &jsFunction, CompilerTier tier, 100 int32_t osrOffset, JitCompileMode mode); 101 bool Decision(); 102 GetTier()103 CompilerTier GetTier() const 104 { 105 return tier_; 106 } 107 GetJsFunction()108 JSHandle<JSFunction> GetJsFunction() const 109 { 110 return jsFunction_; 111 } 112 113 CString GetMethodInfo() const; 114 CString GetMethodName() const; 115 uint32_t GetCodeSize() const; GetOsrOffset()116 int32_t GetOsrOffset() const 117 { 118 return osrOffset_; 119 } 120 GetCompileMode()121 JitCompileMode GetCompileMode() const 122 { 123 return compileMode_; 124 } 125 GetMethodNameCollector()126 static inline MethodNameCollector &GetMethodNameCollector() 127 { 128 return methodNameCollector; 129 } 130 GetMethodNameFilter()131 static inline MethodNameFilter &GetMethodNameFilter() 132 { 133 return methodNameFilter; 134 } 135 136 private: 137 bool IsGoodCompilationRequest() const; 138 bool IsSupportFunctionKind() const; 139 bool CheckJsFunctionStatus() const; 140 bool IsJsFunctionSupportCompile() const; 141 void DisableJitCompile() const; 142 bool CheckVmState() const; 143 144 EcmaVM *vm_; 145 JSHandle<JSFunction> jsFunction_; 146 CompilerTier tier_; 147 int32_t osrOffset_; 148 JitCompileMode compileMode_; 149 static MethodNameCollector methodNameCollector; 150 static MethodNameFilter methodNameFilter; 151 }; 152 } // namespace panda::ecmascript 153 #endif // ECMASCRIPT_JIT_JIT_COMPILE_DECISION_H 154