• 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_X64_X64_CALL_CONV_H
17 #define MAPLEBE_INCLUDE_CG_X64_X64_CALL_CONV_H
18 
19 #include "types_def.h"
20 #include "becommon.h"
21 #include "call_conv.h"
22 #include "abi.h"
23 #include "x64_abi.h"
24 #include "x64_isa.h"
25 #include <vector>
26 
27 namespace maplebe {
28 using namespace maple;
29 using namespace x64;
30 constexpr const uint32 kMaxStructParamByReg = 4;
31 
32 class CallConventionInfo {
33 public:
34     virtual const std::vector<X64reg> &GetIntParamRegs() const = 0;
35     virtual size_t GetIntParamRegsNum() const = 0;
36     virtual const std::vector<X64reg> &GetIntReturnRegs() const = 0;
37     virtual size_t GetIntReturnRegsNum() const = 0;
38     virtual const std::vector<X64reg> &GetFloatParamRegs() const = 0;
39     virtual size_t GetFloatParamRegsNum() const = 0;
40     virtual const std::vector<X64reg> &GetFloatReturnRegs() const = 0;
41     virtual size_t GetFloatReturnRegsNum() const = 0;
42     virtual int32 Classification(const BECommon &be, MIRType &mirType, std::vector<ArgumentClass> &classes) const = 0;
43 };
44 
45 #define CALL_CONVENTION_INFO_SUBCLASS_DECLARE_BEGIN(CLASSNAME)                    \
46     class CLASSNAME : public CallConventionInfo {                                 \
47     public:                                                                       \
48         const std::vector<X64reg> &GetIntParamRegs() const override               \
49         {                                                                         \
50             return intParmRegs;                                                   \
51         }                                                                         \
52         size_t GetIntParamRegsNum() const override                                \
53         {                                                                         \
54             return intParmRegs.size();                                            \
55         }                                                                         \
56         const std::vector<X64reg> &GetIntReturnRegs() const override              \
57         {                                                                         \
58             return intReturnRegs;                                                 \
59         }                                                                         \
60         size_t GetIntReturnRegsNum() const override                               \
61         {                                                                         \
62             return intReturnRegs.size();                                          \
63         }                                                                         \
64         const std::vector<X64reg> &GetFloatParamRegs() const override             \
65         {                                                                         \
66             return floatParmRegs;                                                 \
67         }                                                                         \
68         size_t GetFloatParamRegsNum() const override                              \
69         {                                                                         \
70             return floatParmRegs.size();                                          \
71         }                                                                         \
72         const std::vector<X64reg> &GetFloatReturnRegs() const override            \
73         {                                                                         \
74             return floatReturnRegs;                                               \
75         }                                                                         \
76         size_t GetFloatReturnRegsNum() const override                             \
77         {                                                                         \
78             return floatReturnRegs.size();                                        \
79         }                                                                         \
80         const static CLASSNAME &GetCallConvInfo()                                 \
81         {                                                                         \
82             static CLASSNAME callConvInfo;                                        \
83             return callConvInfo;                                                  \
84         }                                                                         \
85         int32 Classification(const BECommon &be, MIRType &mirType,                \
86                              std::vector<ArgumentClass> &classes) const override; \
87                                                                                   \
88     private:                                                                      \
89         CLASSNAME() {}                                                            \
90         ~CLASSNAME() {}                                                           \
91         CLASSNAME &operator=(const CLASSNAME &);                                  \
92         CLASSNAME(const CLASSNAME &);
93 
94 #define CALL_CONVENTION_INFO_SUBCLASS_DECLARE_END \
95     }                                             \
96     ;
97 
CALL_CONVENTION_INFO_SUBCLASS_DECLARE_BEGIN(WebKitJSCallConventionInfo)98 CALL_CONVENTION_INFO_SUBCLASS_DECLARE_BEGIN(WebKitJSCallConventionInfo)
99 
100 const std::vector<X64reg> intParmRegs {
101     RAX};
102 const std::vector<X64reg> intReturnRegs {
103     RAX};
104 const std::vector<X64reg> floatParmRegs{};
105 const std::vector<X64reg> floatReturnRegs{};
106 CALL_CONVENTION_INFO_SUBCLASS_DECLARE_END
107 
CALL_CONVENTION_INFO_SUBCLASS_DECLARE_BEGIN(CCallConventionInfo)108 CALL_CONVENTION_INFO_SUBCLASS_DECLARE_BEGIN(CCallConventionInfo)
109 const std::vector<X64reg> intParmRegs {
110     RDI, RSI, RDX, RCX, X64reg::R8, X64reg::R9};
111 const std::vector<X64reg> intReturnRegs {
112     RAX, RDX};
113 const std::vector<X64reg> floatParmRegs = {
114     X64reg::V0, X64reg::V1, X64reg::V2, X64reg::V3, X64reg::V4, X64reg::V5, X64reg::V6, X64reg::V7};
115 const std::vector<X64reg> floatReturnRegs = {
116     X64reg::V0, X64reg::V1 };
117 CALL_CONVENTION_INFO_SUBCLASS_DECLARE_END
118 
CALL_CONVENTION_INFO_SUBCLASS_DECLARE_BEGIN(GHCCallConventionInfo)119 CALL_CONVENTION_INFO_SUBCLASS_DECLARE_BEGIN(GHCCallConventionInfo)
120 const std::vector<X64reg> intParmRegs {
121     X64reg::R13, RBP, X64reg::R12, RBX, X64reg::R14, RSI, RDI, X64reg::R8, X64reg::R9, X64reg::R15};
122 const std::vector<X64reg> intReturnRegs{};
123 const std::vector<X64reg> floatParmRegs{};
124 const std::vector<X64reg> floatReturnRegs{};
125 CALL_CONVENTION_INFO_SUBCLASS_DECLARE_END
126 
127 class X64CallConvImpl {
128 public:
X64CallConvImpl(BECommon & be)129     X64CallConvImpl(BECommon &be) : beCommon(be)
130     {
131         convKind = GetCallConvKind(*(be.GetMIRModule().CurFunction()));
132     }
X64CallConvImpl(BECommon & be,CallConvKind convKind)133     X64CallConvImpl(BECommon &be, CallConvKind convKind) : beCommon(be), convKind(convKind) {}
134 
135     ~X64CallConvImpl() = default;
136 
GetCallConvInfo()137     const CallConventionInfo &GetCallConvInfo() const
138     {
139         return GetCallConvInfo(convKind);
140     }
141 
GetCallConvInfo(CallConvKind convKind_)142     static const CallConventionInfo &GetCallConvInfo(CallConvKind convKind_)
143     {
144         switch (convKind_) {
145             case kCCall:
146                 return CCallConventionInfo::GetCallConvInfo();
147             case kWebKitJS:
148                 return WebKitJSCallConventionInfo::GetCallConvInfo();
149             case kGHC:
150                 return GHCCallConventionInfo::GetCallConvInfo();
151             default:
152                 return CCallConventionInfo::GetCallConvInfo();
153         }
154     }
155 
GetCallConvKind(MIRFunction & mirFunction)156     static CallConvKind GetCallConvKind(MIRFunction &mirFunction)
157     {
158         if (mirFunction.GetAttr(FUNCATTR_ccall)) {
159             return kCCall;
160         } else if (mirFunction.GetAttr(FUNCATTR_webkitjscall)) {
161             return kWebKitJS;
162         } else if (mirFunction.GetAttr(FUNCATTR_ghcall)) {
163             return kGHC;
164         } else {
165             return kCCall;
166         }
167     }
168 
GetCallConvKind(StmtNode & node)169     static CallConvKind GetCallConvKind(StmtNode &node)
170     {
171         if (node.GetAttr(STMTATTR_ccall)) {
172             return kCCall;
173         } else if (node.GetAttr(STMTATTR_webkitjscall)) {
174             return kWebKitJS;
175         } else {
176             return kCCall;
177         }
178     }
179 
180     void InitCCLocInfo(CCLocInfo &pLoc) const;
181 
182     /* Passing  value related */
183     int32 LocateNextParm(MIRType &mirType, CCLocInfo &pLoc, bool isFirst = false, MIRFunction *func = nullptr);
184 
185     /*  return value related  */
186     int32 LocateRetVal(MIRType &retType, CCLocInfo &ploc);
187 
188 private:
AllocateGPParmRegister()189     X64reg AllocateGPParmRegister()
190     {
191         const std::vector<X64reg> &intParamRegs = GetCallConvInfo().GetIntParamRegs();
192         return (nextGeneralParmRegNO < intParamRegs.size()) ? intParamRegs[nextGeneralParmRegNO++] : X64reg::kRinvalid;
193     }
194 
AllocateSIMDFPRegister()195     X64reg AllocateSIMDFPRegister()
196     {
197         return (nextFloatRegNO < kNumFloatParmRegs) ? kFloatParmRegs[nextFloatRegNO++] : X64reg::kRinvalid;
198     }
199 
AllocateGPReturnRegister()200     X64reg AllocateGPReturnRegister()
201     {
202         const std::vector<X64reg> &intReturnRegs = GetCallConvInfo().GetIntReturnRegs();
203         return (nextGeneralReturnRegNO < intReturnRegs.size()) ?
204             intReturnRegs[nextGeneralReturnRegNO++] : X64reg::kRinvalid;
205     }
206 
AllocateTwoGPReturnRegisters(CCLocInfo & pLoc)207     void AllocateTwoGPReturnRegisters(CCLocInfo &pLoc)
208     {
209         const std::vector<X64reg> &intReturnRegs = GetCallConvInfo().GetIntReturnRegs();
210         if ((nextGeneralReturnRegNO + 1) < intReturnRegs.size()) {
211             pLoc.reg0 = intReturnRegs[nextGeneralReturnRegNO++];
212             pLoc.reg1 = intReturnRegs[nextGeneralReturnRegNO++];
213         } else {
214             pLoc.reg0 = X64reg::kRinvalid;
215         }
216     }
217 
AllocateSIMDFPReturnRegister()218     X64reg AllocateSIMDFPReturnRegister()
219     {
220         return (nextFloatRetRegNO < kNumFloatReturnRegs) ? kFloatReturnRegs[nextFloatRetRegNO++] : X64reg::kRinvalid;
221     }
222 
223     BECommon &beCommon;
224     CallConvKind convKind = kCCall;
225     uint64 paramNum = 0;               /* number of all types of parameters processed so far */
226     uint32 nextGeneralParmRegNO = 0;   /* number of integer parameters processed so far */
227     uint32 nextGeneralReturnRegNO = 0; /* number of integer return processed so far */
228     uint32 nextStackArgAdress = 0;
229     uint32 nextFloatRegNO = 0;
230     uint32 nextFloatRetRegNO = 0;
231 };
232 } /* namespace maplebe */
233 
234 #endif /* MAPLEBE_INCLUDE_CG_X64_X64_CALL_CONV_H */
235