• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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_unit.h"
23 #include "libpandabase/mem/gc_barrier.h"
24 #include "runtime/mem/gc/g1/g1-allocator_constants.h"
25 
26 #include "libpandabase/utils/bit_utils.h"
27 
28 namespace panda::irtoc {
29 
30 class IrtocRuntimeInterface : public compiler::RuntimeInterface {
31 public:
MethodCast(MethodPtr method)32     CompilationUnit *MethodCast(MethodPtr method) const
33     {
34         return reinterpret_cast<CompilationUnit *>(method);
35     }
36 
GetMethodName(MethodPtr method)37     std::string GetMethodName(MethodPtr method) const override
38     {
39         return MethodCast(method)->GetName();
40     }
41 
GetMethodFullName(MethodPtr method,bool with_signature)42     std::string GetMethodFullName(MethodPtr method, [[maybe_unused]] bool with_signature) const override
43     {
44         return std::string("Irtoc::") + MethodCast(method)->GetName();
45     }
46 
GetClassName(ClassPtr klass)47     std::string GetClassName([[maybe_unused]] ClassPtr klass) const override
48     {
49         return "Irtoc";
50     }
51 
GetClassNameFromMethod(MethodPtr method)52     std::string GetClassNameFromMethod([[maybe_unused]] MethodPtr method) const override
53     {
54         return "Irtoc";
55     }
56 
GetMethodReturnType(MethodPtr method)57     virtual compiler::DataType::Type GetMethodReturnType(MethodPtr method) const override
58     {
59         auto end_block {reinterpret_cast<CompilationUnit *>(method)->GetGraph()->GetEndBlock()};
60         for (auto block : end_block->GetPredsBlocks()) {
61             for (auto inst : block->InstsReverse()) {
62                 if (inst->GetOpcode() == compiler::Opcode::ReturnVoid ||
63                     inst->GetOpcode() == compiler::Opcode::Return) {
64                     return inst->GetType();
65                 }
66             }
67         }
68         return compiler::DataType::VOID;
69     }
70 
GetMethodTotalArgumentsCount(MethodPtr method)71     size_t GetMethodTotalArgumentsCount(MethodPtr method) const override
72     {
73         return GetMethodArgumentsCount(method);
74     }
75 
GetMethodArgumentsCount(MethodPtr method)76     size_t GetMethodArgumentsCount(MethodPtr method) const override
77     {
78         auto unit = reinterpret_cast<CompilationUnit *>(method);
79         size_t res = 0;
80         for ([[maybe_unused]] auto param : unit->GetGraph()->GetParameters()) {
81             res++;
82         }
83         return res;
84     }
85 
GetPreType()86     ::panda::mem::BarrierType GetPreType() const override
87     {
88         return ::panda::mem::BarrierType::PRE_SATB_BARRIER;
89     }
90 
GetPostType()91     ::panda::mem::BarrierType GetPostType() const override
92     {
93         return ::panda::mem::BarrierType::POST_INTERREGION_BARRIER;
94     }
95 
GetBarrierOperand(::panda::mem::BarrierPosition barrier_position,std::string_view operand_name)96     ::panda::mem::BarrierOperand GetBarrierOperand([[maybe_unused]] ::panda::mem::BarrierPosition barrier_position,
97                                                    [[maybe_unused]] std::string_view operand_name) const override
98     {
99         ASSERT(operand_name == "REGION_SIZE_BITS");
100         uint8_t region_size_bits = helpers::math::GetIntLog2(panda::mem::G1_REGION_SIZE);
101         return mem::BarrierOperand(mem::BarrierOperandType::UINT8_LITERAL, mem::BarrierOperandValue(region_size_bits));
102     }
103 
GetFieldOffset(FieldPtr field)104     size_t GetFieldOffset(FieldPtr field) const override
105     {
106         ASSERT((reinterpret_cast<uintptr_t>(field) & 1U) == 1U);
107         return reinterpret_cast<uintptr_t>(field) >> 1U;
108     }
109 
GetFieldByOffset(size_t offset)110     virtual FieldPtr GetFieldByOffset(size_t offset) const override
111     {
112         ASSERT(MinimumBitsToStore(offset) < std::numeric_limits<uintptr_t>::digits);
113         return reinterpret_cast<FieldPtr>((offset << 1U) | 1U);
114     }
115 
GetMethodSourceLanguage(MethodPtr method)116     compiler::SourceLanguage GetMethodSourceLanguage(MethodPtr method) const override
117     {
118         return MethodCast(method)->GetLanguage();
119     }
120 };
121 
122 }  // namespace panda::irtoc
123 
124 #endif  // PANDA_IRTOC_RUNTIME_H
125