• 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 MAPLEBE_INCLUDE_CG_CALL_CONV_H
17 #define MAPLEBE_INCLUDE_CG_CALL_CONV_H
18 
19 #include "types_def.h"
20 #include "becommon.h"
21 
22 namespace maplebe {
23 using namespace maple;
24 enum CallConvKind { kCCall, kWebKitJS, kGHC };
25 /* for specifying how a parameter is passed */
26 struct CCLocInfo {
27     regno_t reg0 = 0; /* 0 means parameter is stored on the stack */
28     regno_t reg1 = 0;
29     regno_t reg2 = 0; /* can have up to 4 single precision fp registers */
30     regno_t reg3 = 0; /* for small structure return. */
31     int32 memOffset = 0;
32     int32 memSize = 0;
33     uint32 fpSize = 0;
34     uint32 numFpPureRegs = 0;
35     uint8 regCount = 0;      /* number of registers <= 2 storing the return value */
36     PrimType primTypeOfReg0; /* the primitive type stored in reg0 */
37     PrimType primTypeOfReg1; /* the primitive type stored in reg1 */
38     PrimType primTypeOfReg2;
39     PrimType primTypeOfReg3;
40 
ClearCCLocInfo41     void Clear()
42     {
43         reg0 = kInvalidRegNO;
44         reg1 = kInvalidRegNO;
45         reg2 = kInvalidRegNO;
46         reg3 = kInvalidRegNO;
47         memOffset = 0;
48         memSize = 0;
49         fpSize = 0;
50         numFpPureRegs = 0;
51         regCount = 0;
52         primTypeOfReg0 = PTY_begin;
53         primTypeOfReg1 = PTY_begin;
54         primTypeOfReg2 = PTY_begin;
55         primTypeOfReg3 = PTY_begin;
56     }
57 
GetRegCountCCLocInfo58     uint8 GetRegCount() const
59     {
60         return regCount;
61     }
62 
GetPrimTypeOfReg0CCLocInfo63     PrimType GetPrimTypeOfReg0() const
64     {
65         return primTypeOfReg0;
66     }
67 
GetPrimTypeOfReg1CCLocInfo68     PrimType GetPrimTypeOfReg1() const
69     {
70         return primTypeOfReg1;
71     }
72 
GetPrimTypeOfReg2CCLocInfo73     PrimType GetPrimTypeOfReg2() const
74     {
75         return primTypeOfReg2;
76     }
77 
GetPrimTypeOfReg3CCLocInfo78     PrimType GetPrimTypeOfReg3() const
79     {
80         return primTypeOfReg3;
81     }
82 
GetReg0CCLocInfo83     regno_t GetReg0() const
84     {
85         return reg0;
86     }
87 
GetReg1CCLocInfo88     regno_t GetReg1() const
89     {
90         return reg1;
91     }
92 
GetReg2CCLocInfo93     regno_t GetReg2() const
94     {
95         return reg2;
96     }
97 
GetReg3CCLocInfo98     regno_t GetReg3() const
99     {
100         return reg3;
101     }
102 
DumpCCLocInfo103     void Dump()
104     {
105         std::cout << "reg: "
106                   << "[" << reg0 << "], "
107                   << "[" << reg1 << "]\n";
108         std::cout << "memBase: " << memOffset << " memSize: " << memSize << std::endl;
109     }
110 };
111 
112 class LmbcFormalParamInfo {
113 public:
LmbcFormalParamInfo(PrimType pType,uint32 ofst,uint32 sz)114     LmbcFormalParamInfo(PrimType pType, uint32 ofst, uint32 sz)
115         : primType(pType),
116           offset(ofst),
117           onStackOffset(0),
118           size(sz),
119           regNO(0),
120           vregNO(0),
121           numRegs(0),
122           fpSize(0),
123           isReturn(false),
124           isPureFloat(false),
125           isOnStack(false),
126           hasRegassign(false)
127     {
128     }
129 
130     ~LmbcFormalParamInfo() = default;
131 
GetPrimType()132     PrimType GetPrimType() const
133     {
134         return primType;
135     }
SetPrimType(PrimType pType)136     void SetPrimType(PrimType pType)
137     {
138         primType = pType;
139     }
GetOffset()140     uint32 GetOffset() const
141     {
142         return offset;
143     }
SetOffset(uint32 ofs)144     void SetOffset(uint32 ofs)
145     {
146         offset = ofs;
147     }
GetOnStackOffset()148     uint32 GetOnStackOffset() const
149     {
150         return onStackOffset;
151     }
SetOnStackOffset(uint32 ofs)152     void SetOnStackOffset(uint32 ofs)
153     {
154         onStackOffset = ofs;
155     }
GetSize()156     uint32 GetSize() const
157     {
158         return size;
159     }
SetSize(uint32 sz)160     void SetSize(uint32 sz)
161     {
162         size = sz;
163     }
GetRegNO()164     regno_t GetRegNO() const
165     {
166         return regNO;
167     }
SetRegNO(regno_t reg)168     void SetRegNO(regno_t reg)
169     {
170         regNO = reg;
171     }
GetVregNO()172     regno_t GetVregNO() const
173     {
174         return vregNO;
175     }
SetVregNO(regno_t reg)176     void SetVregNO(regno_t reg)
177     {
178         vregNO = reg;
179     }
GetNumRegs()180     uint32 GetNumRegs() const
181     {
182         return numRegs;
183     }
SetNumRegs(uint32 num)184     void SetNumRegs(uint32 num)
185     {
186         numRegs = num;
187     }
GetFpSize()188     uint32 GetFpSize() const
189     {
190         return fpSize;
191     }
SetFpSize(uint32 sz)192     void SetFpSize(uint32 sz)
193     {
194         fpSize = sz;
195     }
IsReturn()196     bool IsReturn() const
197     {
198         return isReturn;
199     }
SetIsReturn()200     void SetIsReturn()
201     {
202         isReturn = true;
203     }
IsPureFloat()204     bool IsPureFloat() const
205     {
206         return isPureFloat;
207     }
SetIsPureFloat()208     void SetIsPureFloat()
209     {
210         isPureFloat = true;
211     }
IsInReg()212     bool IsInReg() const
213     {
214         return !isOnStack;
215     }
IsOnStack()216     bool IsOnStack() const
217     {
218         return isOnStack;
219     }
SetIsOnStack()220     void SetIsOnStack()
221     {
222         isOnStack = true;
223     }
HasRegassign()224     bool HasRegassign() const
225     {
226         return hasRegassign;
227     }
SetHasRegassign()228     void SetHasRegassign()
229     {
230         hasRegassign = true;
231     }
232 
233 private:
234     PrimType primType;
235     uint32 offset;
236     uint32 onStackOffset; /* stack location if isOnStack */
237     uint32 size;          /* size primtype or struct */
238     regno_t regNO = 0;    /* param reg num or starting reg num if numRegs > 0 */
239     regno_t vregNO = 0;   /* if no explicit regassing from IR, create move from param reg */
240     uint32 numRegs = 0;   /* number of regs for struct param */
241     uint32 fpSize = 0;    /* size of fp param if isPureFloat */
242     bool isReturn;
243     bool isPureFloat = false;
244     bool isOnStack; /* large struct is passed by a copy on stack */
245     bool hasRegassign;
246 };
247 
248 class CCImpl {
249 public:
250     CCImpl() = default;
251 
252     virtual ~CCImpl() = default;
253 
254     virtual uint64 LocateNextParm(const MIRType &mirType, CCLocInfo &pLoc, bool isFirst = false,
255                                   MIRFuncType *tFunc = nullptr) = 0;
256 
257     virtual void LocateRetVal(const MIRType &retType, CCLocInfo &ploc) = 0;
258 
InitCCLocInfo(CCLocInfo & pLoc)259     void InitCCLocInfo(CCLocInfo &pLoc) const
260     {
261         pLoc.reg0 = kInvalidRegNO;
262         pLoc.reg1 = kInvalidRegNO;
263         pLoc.reg2 = kInvalidRegNO;
264         pLoc.reg3 = kInvalidRegNO;
265         pLoc.memOffset = nextStackArgAdress;
266         pLoc.fpSize = 0;
267         pLoc.numFpPureRegs = 0;
268         return;
269     };
270 
271     virtual void Init() = 0;
272 
GetCallConvKind(MIRFunction & mirFunction)273     static CallConvKind GetCallConvKind(MIRFunction &mirFunction)
274     {
275         if (mirFunction.GetAttr(FUNCATTR_ccall)) {
276             return kCCall;
277         } else if (mirFunction.GetAttr(FUNCATTR_webkitjscall)) {
278             return kWebKitJS;
279         } else if (mirFunction.GetAttr(FUNCATTR_ghcall)) {
280             return kGHC;
281         } else {
282             return kCCall;
283         }
284     }
285 
GetCallConvKind(StmtNode & node)286     static CallConvKind GetCallConvKind(StmtNode &node)
287     {
288         if (node.GetAttr(STMTATTR_ccall)) {
289             return kCCall;
290         } else if (node.GetAttr(STMTATTR_webkitjscall)) {
291             return kWebKitJS;
292         } else {
293             return kCCall;
294         }
295     }
296 
297 protected:
298     int32 nextStackArgAdress = 0;
299 };
300 } /* namespace maplebe */
301 
302 #endif /* MAPLEBE_INCLUDE_CG_CALL_CONV_H */
303