• 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 CPP_ABCKIT_INSTRUCTION_IMPL_H
17 #define CPP_ABCKIT_INSTRUCTION_IMPL_H
18 
19 #include <cstdint>
20 #include "./instruction.h"
21 #include "./graph.h"
22 #include "./file.h"
23 #include "./core/function.h"
24 #include "./core/import_descriptor.h"
25 #include "basic_block.h"
26 #include "core/export_descriptor.h"
27 #include "literal_array.h"
28 
29 // NOLINTBEGIN(performance-unnecessary-value-param)
30 namespace abckit {
31 
InsertAfter(Instruction inst)32 inline Instruction Instruction::InsertAfter(Instruction inst) const
33 {
34     const ApiConfig *conf = GetApiConfig();
35     conf->cGapi_->iInsertAfter(GetView(), inst.GetView());
36     CheckError(conf);
37     return Instruction(GetView(), GetApiConfig(), GetResource());
38 }
39 
InsertBefore(Instruction inst)40 inline Instruction Instruction::InsertBefore(Instruction inst) const
41 {
42     const ApiConfig *conf = GetApiConfig();
43     conf->cGapi_->iInsertBefore(GetView(), inst.GetView());
44     CheckError(conf);
45     return Instruction(GetView(), GetApiConfig(), GetResource());
46 }
47 
Remove()48 inline void Instruction::Remove() const
49 {
50     GetApiConfig()->cGapi_->iRemove(GetView());
51     CheckError(GetApiConfig());
52 }
53 
GetConstantValueI64()54 inline int64_t Instruction::GetConstantValueI64() const
55 {
56     int64_t ret = GetApiConfig()->cGapi_->iGetConstantValueI64(GetView());
57     CheckError(GetApiConfig());
58     return ret;
59 }
60 
GetString()61 inline std::string Instruction::GetString() const
62 {
63     const ApiConfig *conf = GetApiConfig();
64     AbckitString *cString = conf->cGapi_->iGetString(GetView());
65     CheckError(conf);
66     std::string str = conf->cIapi_->abckitStringToString(cString);
67     CheckError(conf);
68     return str;
69 }
70 
GetNext()71 inline Instruction Instruction::GetNext() const
72 {
73     const ApiConfig *conf = GetApiConfig();
74     AbckitInst *inst = conf->cGapi_->iGetNext(GetView());
75     CheckError(conf);
76     return Instruction(inst, conf, GetResource());
77 }
78 
GetPrev()79 inline Instruction Instruction::GetPrev() const
80 {
81     const ApiConfig *conf = GetApiConfig();
82     AbckitInst *inst = conf->cGapi_->iGetPrev(GetView());
83     CheckError(conf);
84     return Instruction(inst, conf, GetResource());
85 }
86 
GetFunction()87 inline core::Function Instruction::GetFunction() const
88 {
89     const ApiConfig *conf = GetApiConfig();
90     AbckitCoreFunction *func = conf->cGapi_->iGetFunction(GetView());
91     CheckError(conf);
92     return core::Function(func, conf, GetResource()->GetFile());
93 }
94 
GetBasicBlock()95 inline BasicBlock Instruction::GetBasicBlock() const
96 {
97     const ApiConfig *conf = GetApiConfig();
98     AbckitBasicBlock *bb = conf->cGapi_->iGetBasicBlock(GetView());
99     CheckError(conf);
100     return BasicBlock(bb, conf, GetResource());
101 }
102 
GetInputCount()103 inline uint32_t Instruction::GetInputCount() const
104 {
105     const ApiConfig *conf = GetApiConfig();
106     uint32_t count = conf->cGapi_->iGetInputCount(GetView());
107     CheckError(conf);
108     return count;
109 }
110 
GetInput(uint32_t index)111 inline Instruction Instruction::GetInput(uint32_t index) const
112 {
113     const ApiConfig *conf = GetApiConfig();
114     AbckitInst *inInst = conf->cGapi_->iGetInput(GetView(), index);
115     CheckError(conf);
116     return Instruction(inInst, conf, GetResource());
117 }
118 
SetInput(uint32_t index,Instruction input)119 inline Instruction Instruction::SetInput(uint32_t index, Instruction input) const
120 {
121     const ApiConfig *conf = GetApiConfig();
122     conf->cGapi_->iSetInput(GetView(), input.GetView(), index);
123     CheckError(conf);
124     return Instruction(GetView(), GetApiConfig(), GetResource());
125 }
126 
127 template <typename... Args>
SetInputs(Args...instrs)128 inline Instruction Instruction::SetInputs(Args... instrs)
129 {
130     const ApiConfig *conf = GetApiConfig();
131     conf->cGapi_->iSetInputs(GetView(), sizeof...(instrs), (instrs.GetView())...);
132     CheckError(conf);
133     return Instruction(GetView(), GetApiConfig(), GetResource());
134 }
135 
VisitUsers(const std::function<bool (Instruction)> & cb)136 inline bool Instruction::VisitUsers(const std::function<bool(Instruction)> &cb) const
137 {
138     Payload<const std::function<bool(Instruction)> &> payload {cb, GetApiConfig(), GetResource()};
139 
140     auto isNormalExit = GetApiConfig()->cGapi_->iVisitUsers(GetView(), &payload, [](AbckitInst *user, void *data) {
141         const auto &payload = *static_cast<Payload<const std::function<bool(Instruction)> &> *>(data);
142         if (!payload.data(Instruction(user, payload.config, payload.resource))) {
143             return false;
144         };
145         return true;
146     });
147     CheckError(GetApiConfig());
148     return isNormalExit;
149 }
150 
GetUserCount()151 inline uint32_t Instruction::GetUserCount() const
152 {
153     uint32_t count = GetApiConfig()->cGapi_->iGetUserCount(GetView());
154     CheckError(GetApiConfig());
155     return count;
156 }
157 
GetLiteralArray()158 inline LiteralArray Instruction::GetLiteralArray() const
159 {
160     const ApiConfig *conf = GetApiConfig();
161     AbckitLiteralArray *litarr = conf->cGapi_->iGetLiteralArray(GetView());
162     CheckError(conf);
163     return LiteralArray(litarr, conf, GetResource()->GetFile());
164 }
165 
GetGraph()166 inline const Graph *Instruction::GetGraph() const
167 {
168     return GetResource();
169 }
170 
GetType()171 inline Type Instruction::GetType() const
172 {
173     const ApiConfig *conf = GetApiConfig();
174     auto *type = conf->cGapi_->iGetType(GetView());
175     CheckError(conf);
176     return Type(type, conf, GetResource()->GetFile());
177 }
178 
GetImmediateSize(size_t index)179 inline enum AbckitBitImmSize Instruction::GetImmediateSize(size_t index) const
180 {
181     const ApiConfig *conf = GetApiConfig();
182     auto immBitSize = conf->cGapi_->iGetImmediateSize(GetView(), index);
183     CheckError(conf);
184     return immBitSize;
185 }
186 
GetId()187 inline uint32_t Instruction::GetId() const
188 {
189     const ApiConfig *conf = GetApiConfig();
190     auto id = conf->cGapi_->iGetId(GetView());
191     CheckError(conf);
192     return id;
193 }
194 
CheckDominance(Instruction dominator)195 inline bool Instruction::CheckDominance(Instruction dominator) const
196 {
197     const ApiConfig *conf = GetApiConfig();
198     auto isDom = conf->cGapi_->iCheckDominance(GetView(), dominator.GetView());
199     CheckError(conf);
200     return isDom;
201 }
202 
CheckIsCall()203 inline bool Instruction::CheckIsCall() const
204 {
205     const ApiConfig *conf = GetApiConfig();
206     auto isCall = conf->cGapi_->iCheckIsCall(GetView());
207     CheckError(conf);
208     return isCall;
209 }
210 
AppendInput(Instruction input)211 inline Instruction Instruction::AppendInput(Instruction input) const
212 {
213     const ApiConfig *conf = GetApiConfig();
214     conf->cGapi_->iAppendInput(GetView(), input.GetView());
215     CheckError(conf);
216     return Instruction(GetView(), GetApiConfig(), GetResource());
217 }
218 
Dump(int32_t fd)219 inline Instruction Instruction::Dump(int32_t fd) const
220 {
221     const ApiConfig *conf = GetApiConfig();
222     conf->cGapi_->iDump(GetView(), fd);
223     CheckError(conf);
224     return Instruction(GetView(), GetApiConfig(), GetResource());
225 }
226 
SetFunction(core::Function function)227 inline Instruction Instruction::SetFunction(core::Function function) const
228 {
229     const ApiConfig *conf = GetApiConfig();
230     conf->cGapi_->iSetFunction(GetView(), function.GetView());
231     CheckError(conf);
232     return Instruction(GetView(), GetApiConfig(), GetResource());
233 }
234 
GetImmediate(size_t index)235 inline uint64_t Instruction::GetImmediate(size_t index) const
236 {
237     const ApiConfig *conf = GetApiConfig();
238     auto imm = conf->cGapi_->iGetImmediate(GetView(), index);
239     CheckError(conf);
240     return imm;
241 }
242 
SetImmediate(size_t index,uint64_t imm)243 inline Instruction Instruction::SetImmediate(size_t index, uint64_t imm) const
244 {
245     const ApiConfig *conf = GetApiConfig();
246     conf->cGapi_->iSetImmediate(GetView(), index, imm);
247     CheckError(conf);
248     return Instruction(GetView(), GetApiConfig(), GetResource());
249 }
250 
GetImmediateCount()251 inline uint64_t Instruction::GetImmediateCount() const
252 {
253     const ApiConfig *conf = GetApiConfig();
254     auto count = conf->cGapi_->iGetImmediateCount(GetView());
255     CheckError(conf);
256     return count;
257 }
258 
SetLiteralArray(LiteralArray la)259 inline Instruction Instruction::SetLiteralArray(LiteralArray la) const
260 {
261     const ApiConfig *conf = GetApiConfig();
262     conf->cGapi_->iSetLiteralArray(GetView(), la.GetView());
263     CheckError(conf);
264     return Instruction(GetView(), GetApiConfig(), GetResource());
265 }
266 
SetString(std::string_view s)267 inline Instruction Instruction::SetString(std::string_view s) const
268 {
269     const ApiConfig *conf = GetApiConfig();
270     auto *str = conf->cMapi_->createString(GetResource()->GetFile()->GetResource(), s.data(), s.size());
271     CheckError(GetApiConfig());
272     conf->cGapi_->iSetString(GetView(), str);
273     CheckError(conf);
274     return Instruction(GetView(), GetApiConfig(), GetResource());
275 }
276 
GetConstantValueI32()277 inline int32_t Instruction::GetConstantValueI32() const
278 {
279     const ApiConfig *conf = GetApiConfig();
280     auto val = conf->cGapi_->iGetConstantValueI32(GetView());
281     CheckError(conf);
282     return val;
283 }
284 
GetConstantValueU64()285 inline uint64_t Instruction::GetConstantValueU64() const
286 {
287     const ApiConfig *conf = GetApiConfig();
288     auto val = conf->cGapi_->iGetConstantValueU64(GetView());
289     CheckError(conf);
290     return val;
291 }
292 
GetConstantValueF64()293 inline double Instruction::GetConstantValueF64() const
294 {
295     const ApiConfig *conf = GetApiConfig();
296     auto val = conf->cGapi_->iGetConstantValueF64(GetView());
297     CheckError(conf);
298     return val;
299 }
300 
301 }  // namespace abckit
302 // NOLINTEND(performance-unnecessary-value-param)
303 
304 #endif  // CPP_ABCKIT_INSTRUCTION_IMPL_H
305