1 /** 2 * Copyright (c) 2021-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 PANDA_IRTOC_RUNTIME_H 17 #define PANDA_IRTOC_RUNTIME_H 18 19 #include <limits> 20 21 #include "compiler/optimizer/ir/runtime_interface.h" 22 #include "compilation.h" 23 #include "irtoc_interface_extensions_includes.inl.h" 24 #include "libpandabase/mem/gc_barrier.h" 25 #include "runtime/mem/gc/g1/g1-allocator_constants.h" 26 #include "runtime/include/hclass.h" 27 #include "libpandabase/utils/bit_utils.h" 28 29 namespace ark::irtoc { 30 31 class IrtocRuntimeInterface : public compiler::RuntimeInterface { 32 public: MethodCast(MethodPtr method)33 Function *MethodCast(MethodPtr method) const 34 { 35 return reinterpret_cast<Function *>(method); 36 } 37 GetMethodName(MethodPtr method)38 std::string GetMethodName(MethodPtr method) const override 39 { 40 return MethodCast(method)->GetName(); 41 } 42 GetExternalMethodName(MethodPtr method,uint32_t externalId)43 std::string GetExternalMethodName(MethodPtr method, uint32_t externalId) const override 44 { 45 return reinterpret_cast<Function *>(method)->GetExternalFunction(externalId); 46 } 47 GetMethodFullName(MethodPtr method,bool withSignature)48 std::string GetMethodFullName(MethodPtr method, [[maybe_unused]] bool withSignature) const override 49 { 50 return std::string("Irtoc::") + MethodCast(method)->GetName(); 51 } 52 GetClassName(ClassPtr klass)53 std::string GetClassName([[maybe_unused]] ClassPtr klass) const override 54 { 55 return "Irtoc"; 56 } 57 GetClassNameFromMethod(MethodPtr method)58 std::string GetClassNameFromMethod([[maybe_unused]] MethodPtr method) const override 59 { 60 return "Irtoc"; 61 } 62 GetMethodReturnType(MethodPtr method)63 compiler::DataType::Type GetMethodReturnType(MethodPtr method) const override 64 { 65 auto unit = reinterpret_cast<Function *>(method); 66 ASSERT(unit->GetGraph()->HasEndBlock()); 67 auto retType = compiler::DataType::NO_TYPE; 68 for (auto exit : unit->GetGraph()->GetEndBlock()->GetPredsBlocks()) { 69 if (exit->IsEmpty()) { 70 continue; 71 } 72 auto last = exit->GetLastInst(); 73 if (last->IsReturn()) { 74 ASSERT(retType == compiler::DataType::NO_TYPE || retType == last->GetType()); 75 retType = last->GetType(); 76 } 77 } 78 return retType; 79 } 80 GetMethodTotalArgumentsCount(MethodPtr method)81 size_t GetMethodTotalArgumentsCount(MethodPtr method) const override 82 { 83 return GetMethodArgumentsCount(method); 84 } 85 GetMethodArgumentsCount(MethodPtr method)86 size_t GetMethodArgumentsCount(MethodPtr method) const override 87 { 88 auto unit = reinterpret_cast<Function *>(method); 89 return unit->GetArgsCount(); 90 } 91 GetPreType()92 ::ark::mem::BarrierType GetPreType() const override 93 { 94 return ::ark::mem::BarrierType::PRE_SATB_BARRIER; 95 } 96 GetPostType()97 ::ark::mem::BarrierType GetPostType() const override 98 { 99 return ::ark::mem::BarrierType::POST_INTERREGION_BARRIER; 100 } 101 GetBarrierOperand(::ark::mem::BarrierPosition barrierPosition,std::string_view operandName)102 ::ark::mem::BarrierOperand GetBarrierOperand([[maybe_unused]] ::ark::mem::BarrierPosition barrierPosition, 103 [[maybe_unused]] std::string_view operandName) const override 104 { 105 ASSERT(operandName == "REGION_SIZE_BITS"); 106 uint8_t regionSizeBits = helpers::math::GetIntLog2(ark::mem::G1_REGION_SIZE); 107 return mem::BarrierOperand(mem::BarrierOperandType::UINT8_LITERAL, mem::BarrierOperandValue(regionSizeBits)); 108 } 109 GetFieldOffset(FieldPtr field)110 size_t GetFieldOffset(FieldPtr field) const override 111 { 112 ASSERT((reinterpret_cast<uintptr_t>(field) & 1U) == 1U); 113 return reinterpret_cast<uintptr_t>(field) >> 1U; 114 } 115 GetFieldByOffset(size_t offset)116 FieldPtr GetFieldByOffset(size_t offset) const override 117 { 118 ASSERT(MinimumBitsToStore(offset) < std::numeric_limits<uintptr_t>::digits); 119 return reinterpret_cast<FieldPtr>((offset << 1U) | 1U); 120 } 121 GetMethodSourceLanguage(MethodPtr method)122 SourceLanguage GetMethodSourceLanguage(MethodPtr method) const override 123 { 124 return MethodCast(method)->GetLanguage(); 125 } 126 GetMethodTotalArgumentType(MethodPtr method,size_t index)127 compiler::DataType::Type GetMethodTotalArgumentType(MethodPtr method, size_t index) const override 128 { 129 auto unit = reinterpret_cast<Function *>(method); 130 for (auto param : unit->GetGraph()->GetParameters()) { 131 if (param->CastToParameter()->GetArgNumber() == index) { 132 return param->GetType(); 133 } 134 } 135 return compiler::DataType::NO_TYPE; 136 } GetCallableMask()137 uint32_t GetCallableMask() const override 138 { 139 return HClass::GetCallableMask(); 140 } 141 #include "irtoc_interface_extensions.inl.h" 142 }; 143 144 } // namespace ark::irtoc 145 146 #endif // PANDA_IRTOC_RUNTIME_H 147