• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 MAPLE_IR_INCLUDE_MIR_PRAGMA_H
17 #define MAPLE_IR_INCLUDE_MIR_PRAGMA_H
18 #include "types_def.h"
19 #include "prim_types.h"
20 #include "mir_module.h"
21 #include "mpl_logging.h"
22 #include "mempool_allocator.h"
23 
24 namespace maple {
25 class MIRModule;         // circular dependency exists, no other choice
26 class MIRType;           // circular dependency exists, no other choice
27 class MIRFunction;       // circular dependency exists, no other choice
28 class MIRSymbol;         // circular dependency exists, no other choice
29 class MIRSymbolTable;    // circular dependency exists, no other choice
30 class MIRTypeNameTable;  // circular dependency exists, no other choice
31 enum PragmaKind {
32     kPragmaUnknown,
33     kPragmaClass,
34     kPragmaFunc,
35     kPragmaField,
36     kPragmaParam,
37     kPragmaPkg,
38     kPragmaVar,
39     kPragmaGlbvar,
40     kPragmaFuncExecptioni,
41     kPragmaFuncVar
42 };
43 
44 enum PragmaVisibility { kVisBuild, kVisRuntime, kVisSystem, kVisMaple };
45 
46 enum PragmaValueType {
47     kValueByte = 0x00,          // (none; must be 0)  ubyte[1]
48     kValueShort = 0x02,         // size - 1 (0…1)  ubyte[size]
49     kValueChar = 0x03,          // size - 1 (0…1)  ubyte[size]
50     kValueInt = 0x04,           // size - 1 (0…3)  ubyte[size]
51     kValueLong = 0x06,          // size - 1 (0…7)  ubyte[size]
52     kValueFloat = 0x10,         // size - 1 (0…3)  ubyte[size]
53     kValueDouble = 0x11,        // size - 1 (0…7)  ubyte[size]
54     kValueMethodType = 0x15,    // size - 1 (0…3)  ubyte[size]
55     kValueMethodHandle = 0x16,  // size - 1 (0…3)  ubyte[size]
56     kValueString = 0x17,        // size - 1 (0…3)  ubyte[size]
57     kValueType = 0x18,          // size - 1 (0…3)  ubyte[size]
58     kValueField = 0x19,         // size - 1 (0…3)  ubyte[size]
59     kValueMethod = 0x1a,        // size - 1 (0…3)  ubyte[size]
60     kValueEnum = 0x1b,          // size - 1 (0…3)  ubyte[size]
61     kValueArray = 0x1c,         // (none; must be 0) encoded_array
62     kValueAnnotation = 0x1d,    // (none; must be 0) encoded_annotation
63     kValueNull = 0x1e,          // (none; must be 0) (none)
64     kValueBoolean = 0x1f        // boolean (0…1)   (none)
65 };
66 
67 class MIRPragmaElement {
68 public:
MIRPragmaElement(MIRModule & m)69     explicit MIRPragmaElement(MIRModule &m) : MIRPragmaElement(m.GetPragmaMPAllocator())
70     {
71         val.d = 0;
72     }
73 
MIRPragmaElement(MapleAllocator & subElemAllocator)74     explicit MIRPragmaElement(MapleAllocator &subElemAllocator) : subElemVec(subElemAllocator.Adapter())
75     {
76         subElemVec.clear();
77         val.d = 0;
78     }
79 
80     ~MIRPragmaElement() = default;
81     void Dump(int indent) const;
SubElemVecPushBack(MIRPragmaElement * elem)82     void SubElemVecPushBack(MIRPragmaElement *elem)
83     {
84         subElemVec.push_back(elem);
85     }
86 
GetSubElemVec()87     const MapleVector<MIRPragmaElement *> &GetSubElemVec() const
88     {
89         return subElemVec;
90     }
91 
GetSubElement(uint64 i)92     const MIRPragmaElement *GetSubElement(uint64 i) const
93     {
94         return subElemVec[i];
95     }
96 
GetSubElemVec()97     MapleVector<MIRPragmaElement *> &GetSubElemVec()
98     {
99         return subElemVec;
100     }
101 
GetNameStrIdx()102     const GStrIdx GetNameStrIdx() const
103     {
104         return nameStrIdx;
105     }
106 
GetTypeStrIdx()107     const GStrIdx GetTypeStrIdx() const
108     {
109         return typeStrIdx;
110     }
111 
GetType()112     PragmaValueType GetType() const
113     {
114         return valueType;
115     }
116 
GetI32Val()117     int32 GetI32Val() const
118     {
119         return val.i;
120     }
121 
GetI64Val()122     int64 GetI64Val() const
123     {
124         return val.j;
125     }
126 
GetU64Val()127     uint64 GetU64Val() const
128     {
129         return val.u;
130     }
131 
GetFloatVal()132     float GetFloatVal() const
133     {
134         return val.f;
135     }
136 
GetDoubleVal()137     double GetDoubleVal() const
138     {
139         return val.d;
140     }
141 
SetTypeStrIdx(GStrIdx strIdx)142     void SetTypeStrIdx(GStrIdx strIdx)
143     {
144         typeStrIdx = strIdx;
145     }
146 
SetNameStrIdx(GStrIdx strIdx)147     void SetNameStrIdx(GStrIdx strIdx)
148     {
149         nameStrIdx = strIdx;
150     }
151 
SetType(PragmaValueType type)152     void SetType(PragmaValueType type)
153     {
154         valueType = type;
155     }
156 
SetI32Val(int32 val)157     void SetI32Val(int32 val)
158     {
159         this->val.i = val;
160     }
161 
SetI64Val(int64 val)162     void SetI64Val(int64 val)
163     {
164         this->val.j = val;
165     }
166 
SetU64Val(uint64 val)167     void SetU64Val(uint64 val)
168     {
169         this->val.u = val;
170     }
171 
SetFloatVal(float val)172     void SetFloatVal(float val)
173     {
174         this->val.f = val;
175     }
176 
SetDoubleVal(double val)177     void SetDoubleVal(double val)
178     {
179         this->val.d = val;
180     }
181 
182 private:
183     GStrIdx nameStrIdx {0};
184     GStrIdx typeStrIdx {0};
185     PragmaValueType valueType = kValueNull;
186     union {
187         int32 i;
188         int64 j;
189         uint64 u;
190         float f;
191         double d;
192     } val;
193     MapleVector<MIRPragmaElement *> subElemVec;
194 };
195 
196 class MIRPragma {
197 public:
MIRPragma(MIRModule & m)198     explicit MIRPragma(MIRModule &m) : MIRPragma(m, m.GetPragmaMPAllocator()) {}
199 
MIRPragma(MIRModule & m,MapleAllocator & elemAllocator)200     MIRPragma(MIRModule &m, MapleAllocator &elemAllocator) : mod(&m), elementVec(elemAllocator.Adapter()) {}
201 
202     ~MIRPragma() = default;
203     MIRPragmaElement *GetPragmaElemFromSignature(const std::string &signature);
204     void Dump(int indent) const;
PushElementVector(MIRPragmaElement * elem)205     void PushElementVector(MIRPragmaElement *elem)
206     {
207         elementVec.push_back(elem);
208     }
209 
ClearElementVector()210     void ClearElementVector()
211     {
212         elementVec.clear();
213     }
214 
GetKind()215     PragmaKind GetKind() const
216     {
217         return pragmaKind;
218     }
219 
GetVisibility()220     uint8 GetVisibility() const
221     {
222         return visibility;
223     }
224 
GetStrIdx()225     const GStrIdx GetStrIdx() const
226     {
227         return strIdx;
228     }
229 
GetTyIdx()230     const TyIdx GetTyIdx() const
231     {
232         return tyIdx;
233     }
234 
GetTyIdxEx()235     const TyIdx GetTyIdxEx() const
236     {
237         return tyIdxEx;
238     }
239 
GetParamNum()240     int32 GetParamNum() const
241     {
242         return paramNum;
243     }
244 
GetElementVector()245     const MapleVector<MIRPragmaElement *> &GetElementVector() const
246     {
247         return elementVec;
248     }
249 
GetNthElement(uint32 i)250     const MIRPragmaElement *GetNthElement(uint32 i) const
251     {
252         return elementVec[i];
253     }
254 
ElementVecPushBack(MIRPragmaElement * elem)255     void ElementVecPushBack(MIRPragmaElement *elem)
256     {
257         elementVec.push_back(elem);
258     }
259 
SetKind(PragmaKind kind)260     void SetKind(PragmaKind kind)
261     {
262         pragmaKind = kind;
263     }
264 
SetVisibility(uint8 visValue)265     void SetVisibility(uint8 visValue)
266     {
267         visibility = visValue;
268     }
269 
SetStrIdx(GStrIdx idx)270     void SetStrIdx(GStrIdx idx)
271     {
272         strIdx = idx;
273     }
274 
SetTyIdx(TyIdx idx)275     void SetTyIdx(TyIdx idx)
276     {
277         tyIdx = idx;
278     }
279 
SetTyIdxEx(TyIdx idx)280     void SetTyIdxEx(TyIdx idx)
281     {
282         tyIdxEx = idx;
283     }
284 
SetParamNum(int32 num)285     void SetParamNum(int32 num)
286     {
287         paramNum = num;
288     }
289 
290 private:
291     MIRModule *mod;
292     PragmaKind pragmaKind = kPragmaUnknown;
293     uint8 visibility = 0;
294     GStrIdx strIdx {0};
295     TyIdx tyIdx {0};
296     TyIdx tyIdxEx {0};
297     int32 paramNum = -1;  // paramNum th param in function, -1 not for param annotation
298     MapleVector<MIRPragmaElement *> elementVec;
299 };
300 }  // namespace maple
301 #endif  // MAPLE_IR_INCLUDE_MIR_PRAGMA_H
302