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 CompileDecision { 76 public: 77 CompileDecision(EcmaVM *vm, JSHandle<JSFunction> &jsFunction, CompilerTier tier, 78 int32_t osrOffset, JitCompileMode mode); 79 bool Decision(); 80 GetTier()81 CompilerTier GetTier() const 82 { 83 return tier_; 84 } 85 GetJsFunction()86 JSHandle<JSFunction> GetJsFunction() const 87 { 88 return jsFunction_; 89 } 90 91 CString GetMethodInfo() const; 92 CString GetMethodName() const; 93 uint32_t GetCodeSize() const; GetOsrOffset()94 int32_t GetOsrOffset() const 95 { 96 return osrOffset_; 97 } 98 GetCompileMode()99 JitCompileMode GetCompileMode() const 100 { 101 return compileMode_; 102 } 103 104 private: 105 bool IsGoodCompilationRequest() const; 106 bool IsSupportFunctionKind() const; 107 bool CheckJsFunctionStatus() const; 108 bool IsJsFunctionSupportCompile() const; 109 void DisableJitCompile() const; 110 bool CheckVmState() const; 111 112 EcmaVM *vm_; 113 JSHandle<JSFunction> jsFunction_; 114 CompilerTier tier_; 115 int32_t osrOffset_; 116 JitCompileMode compileMode_; 117 }; 118 } // namespace panda::ecmascript 119 #endif // ECMASCRIPT_JIT_JIT_COMPILE_DECISION_H 120