• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             for (auto inst : exit->Insts()) {
73                 IntrinsicId id = inst->IsIntrinsic() ? inst->CastToIntrinsic()->GetIntrinsicId() : IntrinsicId::INVALID;
74                 auto tailCall = id == IntrinsicId::INTRINSIC_SLOW_PATH_ENTRY || id == IntrinsicId::INTRINSIC_TAIL_CALL;
75                 if (inst->IsReturn() || tailCall) {
76                     // Check for return type mismatches, allow mismatch for dynamic methods
77                     ASSERT(unit->GetGraph()->IsDynamicMethod() || retType == compiler::DataType::NO_TYPE ||
78                            retType == inst->GetType());
79                     retType = inst->GetType();
80                 }
81             }
82         }
83         return retType;
84     }
85 
GetMethodTotalArgumentsCount(MethodPtr method)86     size_t GetMethodTotalArgumentsCount(MethodPtr method) const override
87     {
88         return GetMethodArgumentsCount(method);
89     }
90 
GetMethodArgumentsCount(MethodPtr method)91     size_t GetMethodArgumentsCount(MethodPtr method) const override
92     {
93         auto unit = reinterpret_cast<Function *>(method);
94         return unit->GetArgsCount();
95     }
96 
GetPreType()97     ::ark::mem::BarrierType GetPreType() const override
98     {
99         return ::ark::mem::BarrierType::PRE_SATB_BARRIER;
100     }
101 
GetPostType()102     ::ark::mem::BarrierType GetPostType() const override
103     {
104         return ::ark::mem::BarrierType::POST_INTERREGION_BARRIER;
105     }
106 
GetBarrierOperand(::ark::mem::BarrierPosition barrierPosition,std::string_view operandName)107     ::ark::mem::BarrierOperand GetBarrierOperand([[maybe_unused]] ::ark::mem::BarrierPosition barrierPosition,
108                                                  [[maybe_unused]] std::string_view operandName) const override
109     {
110         ASSERT(operandName == "REGION_SIZE_BITS");
111         uint8_t regionSizeBits = helpers::math::GetIntLog2(ark::mem::G1_REGION_SIZE);
112         return mem::BarrierOperand(mem::BarrierOperandType::UINT8_LITERAL, mem::BarrierOperandValue(regionSizeBits));
113     }
114 
GetFieldOffset(FieldPtr field)115     size_t GetFieldOffset(FieldPtr field) const override
116     {
117         ASSERT((reinterpret_cast<uintptr_t>(field) & 1U) == 1U);
118         return reinterpret_cast<uintptr_t>(field) >> 1U;
119     }
120 
GetFieldByOffset(size_t offset)121     FieldPtr GetFieldByOffset(size_t offset) const override
122     {
123         ASSERT(MinimumBitsToStore(offset) < std::numeric_limits<uintptr_t>::digits);
124         return reinterpret_cast<FieldPtr>((offset << 1U) | 1U);
125     }
126 
GetMethodSourceLanguage(MethodPtr method)127     SourceLanguage GetMethodSourceLanguage(MethodPtr method) const override
128     {
129         return MethodCast(method)->GetLanguage();
130     }
131 
GetMethodTotalArgumentType(MethodPtr method,size_t index)132     compiler::DataType::Type GetMethodTotalArgumentType(MethodPtr method, size_t index) const override
133     {
134         auto unit = reinterpret_cast<Function *>(method);
135         for (auto param : unit->GetGraph()->GetParameters()) {
136             if (param->CastToParameter()->GetArgNumber() == index) {
137                 return param->GetType();
138             }
139         }
140         return compiler::DataType::NO_TYPE;
141     }
GetCallableMask()142     uint32_t GetCallableMask() const override
143     {
144         return HClass::GetCallableMask();
145     }
146 #include "irtoc_interface_extensions.inl.h"
147 };
148 
149 }  // namespace ark::irtoc
150 
151 #endif  // PANDA_IRTOC_RUNTIME_H
152