• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 LIBABCKIT_SRC_ADAPTER_STATIC_HELPERS_STATIC_H
17 #define LIBABCKIT_SRC_ADAPTER_STATIC_HELPERS_STATIC_H
18 
19 #include "libabckit/include/c/abckit.h"
20 #include "libabckit/include/c/ir_core.h"
21 #include "libabckit/include/c/isa/isa_dynamic.h"
22 #include "libabckit/src/include_v2/c/isa/isa_static.h"
23 #include "libabckit/src/ir_impl.h"
24 #include "libabckit/src/adapter_static/runtime_adapter_static.h"
25 
26 #include "static_core/compiler/optimizer/ir/inst.h"
27 #include "static_core/libpandabase/mem/arena_allocator.h"
28 #include "static_core/assembler/annotation.h"
29 
30 #include <string>
31 #include <tuple>
32 
33 namespace libabckit {
34 
35 struct CtxGInternal {
36     ark::ArenaAllocator *allocator;
37     ark::ArenaAllocator *localAllocator;
38     const AbckitIrInterface *irInterface;
39     AbckitRuntimeAdapterStatic *runtimeAdapter;
40 };
41 
42 std::tuple<std::string, std::string> ClassGetNames(const std::string &fullName);
43 std::tuple<std::string, std::string> FuncGetNames(const std::string &fullSig);
44 std::string FuncNameCropModule(const std::string &fullSig);
45 
46 void CheckInvalidOpcodes(ark::compiler::Graph *graph, bool isDynamic);
47 size_t GetIntrinicMaxInputsCount(AbckitInst *inst);
48 bool IsCallInst(AbckitInst *inst);
49 
50 AbckitIsaApiStaticOpcode GetStaticOpcode(ark::compiler::Inst *inst);
51 AbckitIsaApiDynamicOpcode GetDynamicOpcode(ark::compiler::Inst *inst);
52 AbckitTypeId TypeToTypeId(ark::compiler::DataType::Type type);
53 ark::compiler::DataType::Type TypeIdToType(AbckitTypeId typeId);
54 ark::compiler::ConditionCode CcToCc(AbckitIsaApiDynamicConditionCode cc);
55 ark::compiler::ConditionCode CcToCc(AbckitIsaApiStaticConditionCode cc);
56 AbckitIsaApiDynamicConditionCode CcToDynamicCc(ark::compiler::ConditionCode cc);
57 AbckitIsaApiStaticConditionCode CcToStaticCc(ark::compiler::ConditionCode cc);
58 void SetLastError(AbckitStatus err);
59 
60 std::string GetFuncName(AbckitCoreFunction *function);
61 std::string GetMangleFuncName(AbckitCoreFunction *function);
62 
63 uint32_t GetClassOffset(AbckitGraph *graph, AbckitCoreClass *klass);
64 uint32_t GetMethodOffset(AbckitGraph *graph, AbckitCoreFunction *function);
65 uint32_t GetStringOffset(AbckitGraph *graph, AbckitString *string);
66 uint32_t GetLiteralArrayOffset(AbckitGraph *graph, AbckitLiteralArray *arr);
67 
68 AbckitInst *CreateInstFromImpl(AbckitGraph *graph, ark::compiler::Inst *impl);
69 AbckitInst *FindOrCreateInstFromImpl(AbckitGraph *graph, ark::compiler::Inst *impl);
70 
71 AbckitInst *CreateDynInst(AbckitGraph *graph, ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
72 
73 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *input0, ark::compiler::IntrinsicInst::IntrinsicId intrinsicId,
74                           bool hasIC = true);
75 AbckitInst *CreateDynInst(AbckitGraph *graph, uint64_t imm0, ark::compiler::DataType::Type retType,
76                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
77 AbckitInst *CreateDynInst(AbckitGraph *graph, uint64_t imm0, ark::compiler::IntrinsicInst::IntrinsicId intrinsicId,
78                           bool hasIC = true);
79 
80 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *input0, AbckitInst *input1,
81                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
82 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *input0, uint64_t imm0,
83                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
84 AbckitInst *CreateDynInst(AbckitGraph *graph, uint64_t imm0, uint64_t imm1,
85                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
86 
87 // CC-OFFNXT(G.FUN.01) helper function
88 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *input0, AbckitInst *input1, AbckitInst *input2,
89                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
90 // CC-OFFNXT(G.FUN.01) helper function
91 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *input0, AbckitInst *input1, uint64_t imm0,
92                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
93 // CC-OFFNXT(G.FUN.01) helper function
94 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *input0, uint64_t imm0, uint64_t imm1,
95                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
96 
97 // CC-OFFNXT(G.FUN.01) helper function
98 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *input0, AbckitInst *input1, uint64_t imm0, uint64_t imm1,
99                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
100 // CC-OFFNXT(G.FUN.01) helper function
101 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *acc, AbckitInst *input0, AbckitInst *input1,
102                           AbckitInst *input2, ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
103 // CC-OFFNXT(G.FUN.01) helper function
104 AbckitInst *CreateDynInst(AbckitGraph *graph, uint64_t imm0, uint64_t imm1, uint64_t imm2, AbckitInst *input0,
105                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
106 
107 // CC-OFFNXT(G.FUN.01) helper function
108 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *acc, AbckitInst *input0, AbckitInst *input1,
109                           AbckitInst *input2, AbckitInst *input3, ark::compiler::IntrinsicInst::IntrinsicId intrinsicId,
110                           bool hasIC = true);
111 
112 // CC-OFFNXT(G.FUN.01) helper function
113 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *input0, AbckitInst *input1, uint64_t imm0, std::va_list args,
114                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
115 // CC-OFFNXT(G.FUN.01) helper function
116 AbckitInst *CreateDynInst(AbckitGraph *graph, AbckitInst *acc, size_t argCount, std::va_list args,
117                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
118 AbckitInst *CreateDynInst(AbckitGraph *graph, size_t argCount, std::va_list args,
119                           ark::compiler::IntrinsicInst::IntrinsicId intrinsicId, bool hasIC = true);
120 
121 // Forward declaration of functions which implemented in helpers_dynamic.cpp
122 panda::pandasm::Function *GetDynFunction(AbckitCoreFunction *function);
123 AbckitModulePayloadDyn *GetDynModulePayload(AbckitCoreModule *mod);
124 AbckitDynamicImportDescriptorPayload *GetDynImportDescriptorPayload(AbckitCoreImportDescriptor *id);
125 AbckitDynamicExportDescriptorPayload *GetDynExportDescriptorPayload(AbckitCoreExportDescriptor *ed);
126 void FixPreassignedRegisters(ark::compiler::Graph *graph);
127 bool AllocateRegisters(ark::compiler::Graph *graph, uint8_t reservedReg);
128 
129 AbckitLiteral *FindOrCreateLiteralBoolStaticImpl(AbckitFile *file, bool value);
130 AbckitLiteral *FindOrCreateLiteralU8StaticImpl(AbckitFile *file, uint8_t value);
131 AbckitLiteral *FindOrCreateLiteralU16StaticImpl(AbckitFile *file, uint16_t value);
132 AbckitLiteral *FindOrCreateLiteralMethodAffiliateStaticImpl(AbckitFile *file, uint16_t value);
133 AbckitLiteral *FindOrCreateLiteralU32StaticImpl(AbckitFile *file, uint32_t value);
134 AbckitLiteral *FindOrCreateLiteralU64StaticImpl(AbckitFile *file, uint64_t value);
135 AbckitLiteral *FindOrCreateLiteralFloatStaticImpl(AbckitFile *file, float value);
136 AbckitLiteral *FindOrCreateLiteralDoubleStaticImpl(AbckitFile *file, double value);
137 AbckitLiteral *FindOrCreateLiteralStringStaticImpl(AbckitFile *file, const std::string &value);
138 AbckitLiteral *FindOrCreateLiteralMethodStaticImpl(AbckitFile *file, const std::string &value);
139 
140 AbckitValue *FindOrCreateValueU1StaticImpl(AbckitFile *file, bool value);
141 AbckitValue *FindOrCreateValueDoubleStaticImpl(AbckitFile *file, double value);
142 AbckitValue *FindOrCreateValueStringStaticImpl(AbckitFile *file, const std::string &value);
143 AbckitValue *FindOrCreateLiteralArrayValueStaticImpl(AbckitFile *file, const std::string &value);
144 AbckitValue *FindOrCreateValueStatic(AbckitFile *file, const ark::pandasm::Value &value);
145 
146 void GraphInvalidateAnalyses(ark::compiler::Graph *graph);
147 bool GraphHasUnreachableBlocks(ark::compiler::Graph *graph);
148 bool GraphDominatorsTreeAnalysisIsValid(ark::compiler::Graph *graph);
149 
GetBitLengthUnsigned(uint64_t imm)150 constexpr AbckitBitImmSize GetBitLengthUnsigned(uint64_t imm)
151 {
152     // NOLINTNEXTLINE(readability-identifier-naming)
153     constexpr uint8_t bit4Max = 0xF;
154 
155     if (imm <= bit4Max) {
156         return AbckitBitImmSize::BITSIZE_4;
157     }
158     if (imm <= std::numeric_limits<uint8_t>::max()) {
159         return AbckitBitImmSize::BITSIZE_8;
160     }
161     if (imm <= std::numeric_limits<uint16_t>::max()) {
162         return AbckitBitImmSize::BITSIZE_16;
163     }
164     if (imm <= std::numeric_limits<uint32_t>::max()) {
165         return AbckitBitImmSize::BITSIZE_32;
166     }
167     return AbckitBitImmSize::BITSIZE_64;
168 }
169 
GetBinaryImmOperationSize(ark::compiler::Opcode opcode)170 constexpr AbckitBitImmSize GetBinaryImmOperationSize(ark::compiler::Opcode opcode)
171 {
172     switch (opcode) {
173         case ark::compiler::Opcode::AddI:
174         case ark::compiler::Opcode::SubI:
175         case ark::compiler::Opcode::MulI:
176         case ark::compiler::Opcode::DivI:
177         case ark::compiler::Opcode::ModI:
178         case ark::compiler::Opcode::ShlI:
179         case ark::compiler::Opcode::ShrI:
180         case ark::compiler::Opcode::AShrI:
181             return AbckitBitImmSize::BITSIZE_8;
182 
183         case ark::compiler::Opcode::AndI:
184         case ark::compiler::Opcode::OrI:
185         case ark::compiler::Opcode::XorI:
186             return AbckitBitImmSize::BITSIZE_32;
187         default:
188             LIBABCKIT_LOG(DEBUG) << "Not BinaryImmOperation" << '\n';
189     }
190     return AbckitBitImmSize::BITSIZE_0;
191 }
192 
193 }  // namespace libabckit
194 
195 #endif
196