Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 1 | //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These classes wrap the information about a call or function |
| 11 | // definition used to handle ABI compliancy. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CGCall.h" |
| 16 | #include "CodeGenFunction.h" |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/Decl.h" |
| 21 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Devang Patel | d0646bd | 2008-09-24 01:01:36 +0000 | [diff] [blame] | 24 | #include "llvm/Attributes.h" |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | using namespace CodeGen; |
| 29 | |
| 30 | /***/ |
| 31 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 32 | // FIXME: Use iterator and sidestep silly type array creation. |
| 33 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 34 | const |
| 35 | CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) { |
| 36 | return getFunctionInfo(FTNP->getResultType(), |
| 37 | llvm::SmallVector<QualType, 16>()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 40 | const |
| 41 | CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) { |
| 42 | llvm::SmallVector<QualType, 16> ArgTys; |
| 43 | // FIXME: Kill copy. |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 44 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 45 | ArgTys.push_back(FTP->getArgType(i)); |
| 46 | return getFunctionInfo(FTP->getResultType(), ArgTys); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 49 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) { |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 50 | const FunctionType *FTy = FD->getType()->getAsFunctionType(); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 51 | if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy)) |
| 52 | return getFunctionInfo(FTP); |
| 53 | return getFunctionInfo(cast<FunctionTypeNoProto>(FTy)); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 56 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) { |
| 57 | llvm::SmallVector<QualType, 16> ArgTys; |
| 58 | ArgTys.push_back(MD->getSelfDecl()->getType()); |
| 59 | ArgTys.push_back(Context.getObjCSelType()); |
| 60 | // FIXME: Kill copy? |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 61 | for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(), |
| 62 | e = MD->param_end(); i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 63 | ArgTys.push_back((*i)->getType()); |
| 64 | return getFunctionInfo(MD->getResultType(), ArgTys); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 67 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 68 | const CallArgList &Args) { |
| 69 | // FIXME: Kill copy. |
| 70 | llvm::SmallVector<QualType, 16> ArgTys; |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 71 | for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 72 | i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 73 | ArgTys.push_back(i->second); |
| 74 | return getFunctionInfo(ResTy, ArgTys); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 77 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 78 | const FunctionArgList &Args) { |
| 79 | // FIXME: Kill copy. |
| 80 | llvm::SmallVector<QualType, 16> ArgTys; |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 81 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 82 | i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 83 | ArgTys.push_back(i->second); |
| 84 | return getFunctionInfo(ResTy, ArgTys); |
| 85 | } |
| 86 | |
| 87 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 88 | const llvm::SmallVector<QualType, 16> &ArgTys) { |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame^] | 89 | // Lookup or create unique function info. |
| 90 | llvm::FoldingSetNodeID ID; |
| 91 | CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end()); |
| 92 | |
| 93 | void *InsertPos = 0; |
| 94 | CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos); |
| 95 | if (FI) |
| 96 | return *FI; |
| 97 | |
| 98 | FI = new CGFunctionInfo(ResTy, ArgTys); |
| 99 | FunctionInfos.InsertNode(FI, InsertPos); |
| 100 | return *FI; |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /***/ |
| 104 | |
| 105 | CGFunctionInfo::CGFunctionInfo(QualType ResTy, |
| 106 | const llvm::SmallVector<QualType, 16> &ArgTys) { |
| 107 | ArgTypes.push_back(ResTy); |
| 108 | ArgTypes.insert(ArgTypes.end(), ArgTys.begin(), ArgTys.end()); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 111 | CGFunctionInfo::arg_iterator CGFunctionInfo::arg_begin() const { |
| 112 | return ArgTypes.begin()+1; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 115 | CGFunctionInfo::arg_iterator CGFunctionInfo::arg_end() const { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 116 | return ArgTypes.end(); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /***/ |
| 120 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 121 | /// ABIArgInfo - Helper class to encapsulate information about how a |
| 122 | /// specific C type should be passed to or returned from a function. |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 123 | class ABIArgInfo { |
| 124 | public: |
| 125 | enum Kind { |
| 126 | Default, |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 127 | StructRet, /// Only valid for return values. The return value |
| 128 | /// should be passed through a pointer to a caller |
| 129 | /// allocated location passed as an implicit first |
| 130 | /// argument to the function. |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 131 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 132 | Ignore, /// Ignore the argument (treat as void). Useful for |
| 133 | /// void and empty structs. |
| 134 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 135 | Coerce, /// Only valid for aggregate return types, the argument |
| 136 | /// should be accessed by coercion to a provided type. |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 137 | |
| 138 | ByVal, /// Only valid for aggregate argument types. The |
| 139 | /// structure should be passed "byval" with the |
| 140 | /// specified alignment (0 indicates default |
| 141 | /// alignment). |
| 142 | |
| 143 | Expand, /// Only valid for aggregate argument types. The |
| 144 | /// structure should be expanded into consecutive |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 145 | /// arguments for its constituent fields. Currently |
| 146 | /// expand is only allowed on structures whose fields |
| 147 | /// are all scalar types or are themselves expandable |
| 148 | /// types. |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 149 | |
| 150 | KindFirst=Default, KindLast=Expand |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | private: |
| 154 | Kind TheKind; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 155 | const llvm::Type *TypeData; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 156 | unsigned UIntData; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 157 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 158 | ABIArgInfo(Kind K, const llvm::Type *TD=0, |
| 159 | unsigned UI=0) : TheKind(K), |
| 160 | TypeData(TD), |
| 161 | UIntData(0) {} |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 162 | public: |
| 163 | static ABIArgInfo getDefault() { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 164 | return ABIArgInfo(Default); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 165 | } |
| 166 | static ABIArgInfo getStructRet() { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 167 | return ABIArgInfo(StructRet); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 168 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 169 | static ABIArgInfo getIgnore() { |
| 170 | return ABIArgInfo(Ignore); |
| 171 | } |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 172 | static ABIArgInfo getCoerce(const llvm::Type *T) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 173 | return ABIArgInfo(Coerce, T); |
| 174 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 175 | static ABIArgInfo getByVal(unsigned Alignment) { |
| 176 | return ABIArgInfo(ByVal, 0, Alignment); |
| 177 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 178 | static ABIArgInfo getExpand() { |
| 179 | return ABIArgInfo(Expand); |
| 180 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 181 | |
| 182 | Kind getKind() const { return TheKind; } |
| 183 | bool isDefault() const { return TheKind == Default; } |
| 184 | bool isStructRet() const { return TheKind == StructRet; } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 185 | bool isIgnore() const { return TheKind == Ignore; } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 186 | bool isCoerce() const { return TheKind == Coerce; } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 187 | bool isByVal() const { return TheKind == ByVal; } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 188 | bool isExpand() const { return TheKind == Expand; } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 189 | |
| 190 | // Coerce accessors |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 191 | const llvm::Type *getCoerceToType() const { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 192 | assert(TheKind == Coerce && "Invalid kind!"); |
| 193 | return TypeData; |
| 194 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 195 | |
| 196 | // ByVal accessors |
| 197 | unsigned getByValAlignment() const { |
| 198 | assert(TheKind == ByVal && "Invalid kind!"); |
| 199 | return UIntData; |
| 200 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | /***/ |
| 204 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 205 | /* FIXME: All of this stuff should be part of the target interface |
| 206 | somehow. It is currently here because it is not clear how to factor |
| 207 | the targets to support this, since the Targets currently live in a |
| 208 | layer below types n'stuff. |
| 209 | */ |
| 210 | |
| 211 | /// ABIInfo - Target specific hooks for defining how a type should be |
| 212 | /// passed or returned from functions. |
| 213 | class clang::ABIInfo { |
| 214 | public: |
| 215 | virtual ~ABIInfo(); |
| 216 | |
| 217 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 218 | ASTContext &Context) const = 0; |
| 219 | |
| 220 | virtual ABIArgInfo classifyArgumentType(QualType Ty, |
| 221 | ASTContext &Context) const = 0; |
| 222 | }; |
| 223 | |
| 224 | ABIInfo::~ABIInfo() {} |
| 225 | |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 226 | /// isEmptyStruct - Return true iff a structure has no non-empty |
| 227 | /// members. Note that a structure with a flexible array member is not |
| 228 | /// considered empty. |
| 229 | static bool isEmptyStruct(QualType T) { |
| 230 | const RecordType *RT = T->getAsStructureType(); |
| 231 | if (!RT) |
| 232 | return 0; |
| 233 | const RecordDecl *RD = RT->getDecl(); |
| 234 | if (RD->hasFlexibleArrayMember()) |
| 235 | return false; |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 236 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 237 | e = RD->field_end(); i != e; ++i) { |
| 238 | const FieldDecl *FD = *i; |
| 239 | if (!isEmptyStruct(FD->getType())) |
| 240 | return false; |
| 241 | } |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | /// isSingleElementStruct - Determine if a structure is a "single |
| 246 | /// element struct", i.e. it has exactly one non-empty field or |
| 247 | /// exactly one field which is itself a single element |
| 248 | /// struct. Structures with flexible array members are never |
| 249 | /// considered single element structs. |
| 250 | /// |
| 251 | /// \return The field declaration for the single non-empty field, if |
| 252 | /// it exists. |
| 253 | static const FieldDecl *isSingleElementStruct(QualType T) { |
| 254 | const RecordType *RT = T->getAsStructureType(); |
| 255 | if (!RT) |
| 256 | return 0; |
| 257 | |
| 258 | const RecordDecl *RD = RT->getDecl(); |
| 259 | if (RD->hasFlexibleArrayMember()) |
| 260 | return 0; |
| 261 | |
| 262 | const FieldDecl *Found = 0; |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 263 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 264 | e = RD->field_end(); i != e; ++i) { |
| 265 | const FieldDecl *FD = *i; |
| 266 | QualType FT = FD->getType(); |
| 267 | |
| 268 | if (isEmptyStruct(FT)) { |
| 269 | // Ignore |
| 270 | } else if (Found) { |
| 271 | return 0; |
| 272 | } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 273 | Found = FD; |
| 274 | } else { |
| 275 | Found = isSingleElementStruct(FT); |
| 276 | if (!Found) |
| 277 | return 0; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return Found; |
| 282 | } |
| 283 | |
| 284 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 285 | if (!Ty->getAsBuiltinType() && !Ty->isPointerType()) |
| 286 | return false; |
| 287 | |
| 288 | uint64_t Size = Context.getTypeSize(Ty); |
| 289 | return Size == 32 || Size == 64; |
| 290 | } |
| 291 | |
| 292 | static bool areAllFields32Or64BitBasicType(const RecordDecl *RD, |
| 293 | ASTContext &Context) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 294 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 295 | e = RD->field_end(); i != e; ++i) { |
| 296 | const FieldDecl *FD = *i; |
| 297 | |
| 298 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 299 | return false; |
| 300 | |
| 301 | // If this is a bit-field we need to make sure it is still a |
| 302 | // 32-bit or 64-bit type. |
| 303 | if (Expr *BW = FD->getBitWidth()) { |
| 304 | unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue(); |
| 305 | if (Width <= 16) |
| 306 | return false; |
| 307 | } |
| 308 | } |
| 309 | return true; |
| 310 | } |
| 311 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 312 | namespace { |
| 313 | /// DefaultABIInfo - The default implementation for ABI specific |
| 314 | /// details. This implementation provides information which results in |
| 315 | /// sensible LLVM IR generation, but does not conform to any |
| 316 | /// particular ABI. |
| 317 | class DefaultABIInfo : public ABIInfo { |
| 318 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 319 | ASTContext &Context) const; |
| 320 | |
| 321 | virtual ABIArgInfo classifyArgumentType(QualType RetTy, |
| 322 | ASTContext &Context) const; |
| 323 | }; |
| 324 | |
| 325 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 326 | class X86_32ABIInfo : public ABIInfo { |
| 327 | public: |
| 328 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 329 | ASTContext &Context) const; |
| 330 | |
| 331 | virtual ABIArgInfo classifyArgumentType(QualType RetTy, |
| 332 | ASTContext &Context) const; |
| 333 | }; |
| 334 | } |
| 335 | |
| 336 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 337 | ASTContext &Context) const { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 338 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 339 | // Classify "single element" structs as their element type. |
| 340 | const FieldDecl *SeltFD = isSingleElementStruct(RetTy); |
| 341 | if (SeltFD) { |
| 342 | QualType SeltTy = SeltFD->getType()->getDesugaredType(); |
| 343 | if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) { |
| 344 | // FIXME: This is gross, it would be nice if we could just |
| 345 | // pass back SeltTy and have clients deal with it. Is it worth |
| 346 | // supporting coerce to both LLVM and clang Types? |
| 347 | if (BT->isIntegerType()) { |
| 348 | uint64_t Size = Context.getTypeSize(SeltTy); |
| 349 | return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size)); |
| 350 | } else if (BT->getKind() == BuiltinType::Float) { |
| 351 | return ABIArgInfo::getCoerce(llvm::Type::FloatTy); |
| 352 | } else if (BT->getKind() == BuiltinType::Double) { |
| 353 | return ABIArgInfo::getCoerce(llvm::Type::DoubleTy); |
| 354 | } |
| 355 | } else if (SeltTy->isPointerType()) { |
| 356 | // FIXME: It would be really nice if this could come out as |
| 357 | // the proper pointer type. |
| 358 | llvm::Type *PtrTy = |
| 359 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 360 | return ABIArgInfo::getCoerce(PtrTy); |
| 361 | } |
| 362 | } |
| 363 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 364 | uint64_t Size = Context.getTypeSize(RetTy); |
| 365 | if (Size == 8) { |
| 366 | return ABIArgInfo::getCoerce(llvm::Type::Int8Ty); |
| 367 | } else if (Size == 16) { |
| 368 | return ABIArgInfo::getCoerce(llvm::Type::Int16Ty); |
| 369 | } else if (Size == 32) { |
| 370 | return ABIArgInfo::getCoerce(llvm::Type::Int32Ty); |
| 371 | } else if (Size == 64) { |
| 372 | return ABIArgInfo::getCoerce(llvm::Type::Int64Ty); |
| 373 | } else { |
| 374 | return ABIArgInfo::getStructRet(); |
| 375 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 376 | } else { |
| 377 | return ABIArgInfo::getDefault(); |
| 378 | } |
| 379 | } |
| 380 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 381 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 382 | ASTContext &Context) const { |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 383 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 384 | // Structures with flexible arrays are always byval. |
| 385 | if (const RecordType *RT = Ty->getAsStructureType()) |
| 386 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 387 | return ABIArgInfo::getByVal(0); |
| 388 | |
| 389 | // Expand empty structs (i.e. ignore) |
| 390 | uint64_t Size = Context.getTypeSize(Ty); |
| 391 | if (Ty->isStructureType() && Size == 0) |
| 392 | return ABIArgInfo::getExpand(); |
| 393 | |
| 394 | // Expand structs with size <= 128-bits which consist only of |
| 395 | // basic types (int, long long, float, double, xxx*). This is |
| 396 | // non-recursive and does not ignore empty fields. |
| 397 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 398 | if (Context.getTypeSize(Ty) <= 4*32 && |
| 399 | areAllFields32Or64BitBasicType(RT->getDecl(), Context)) |
| 400 | return ABIArgInfo::getExpand(); |
| 401 | } |
| 402 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 403 | return ABIArgInfo::getByVal(0); |
| 404 | } else { |
| 405 | return ABIArgInfo::getDefault(); |
| 406 | } |
| 407 | } |
| 408 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 409 | namespace { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 410 | /// X86_64ABIInfo - The X86_64 ABI information. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 411 | class X86_64ABIInfo : public ABIInfo { |
| 412 | enum Class { |
| 413 | Integer = 0, |
| 414 | SSE, |
| 415 | SSEUp, |
| 416 | X87, |
| 417 | X87Up, |
| 418 | ComplexX87, |
| 419 | NoClass, |
| 420 | Memory |
| 421 | }; |
| 422 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 423 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 424 | /// |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 425 | /// Merge an accumulating classification \arg Accum with a field |
| 426 | /// classification \arg Field. |
| 427 | /// |
| 428 | /// \param Accum - The accumulating classification. This should |
| 429 | /// always be either NoClass or the result of a previous merge |
| 430 | /// call. In addition, this should never be Memory (the caller |
| 431 | /// should just return Memory for the aggregate). |
| 432 | Class merge(Class Accum, Class Field) const; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 433 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 434 | /// classify - Determine the x86_64 register classes in which the |
| 435 | /// given type T should be passed. |
| 436 | /// |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 437 | /// \param Lo - The classification for the parts of the type |
| 438 | /// residing in the low word of the containing object. |
| 439 | /// |
| 440 | /// \param Hi - The classification for the parts of the type |
| 441 | /// residing in the high word of the containing object. |
| 442 | /// |
| 443 | /// \param OffsetBase - The bit offset of this type in the |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 444 | /// containing object. Some parameters are classified different |
| 445 | /// depending on whether they straddle an eightbyte boundary. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 446 | /// |
| 447 | /// If a word is unused its result will be NoClass; if a type should |
| 448 | /// be passed in Memory then at least the classification of \arg Lo |
| 449 | /// will be Memory. |
| 450 | /// |
| 451 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
| 452 | /// |
| 453 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 454 | /// be NoClass. |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 455 | void classify(QualType T, ASTContext &Context, uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 456 | Class &Lo, Class &Hi) const; |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 457 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 458 | public: |
| 459 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 460 | ASTContext &Context) const; |
| 461 | |
| 462 | virtual ABIArgInfo classifyArgumentType(QualType RetTy, |
| 463 | ASTContext &Context) const; |
| 464 | }; |
| 465 | } |
| 466 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 467 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, |
| 468 | Class Field) const { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 469 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 470 | // classified recursively so that always two fields are |
| 471 | // considered. The resulting class is calculated according to |
| 472 | // the classes of the fields in the eightbyte: |
| 473 | // |
| 474 | // (a) If both classes are equal, this is the resulting class. |
| 475 | // |
| 476 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 477 | // the other class. |
| 478 | // |
| 479 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 480 | // class. |
| 481 | // |
| 482 | // (d) If one of the classes is INTEGER, the result is the |
| 483 | // INTEGER. |
| 484 | // |
| 485 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 486 | // MEMORY is used as class. |
| 487 | // |
| 488 | // (f) Otherwise class SSE is used. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 489 | assert((Accum == NoClass || Accum == Integer || |
| 490 | Accum == SSE || Accum == SSEUp) && |
| 491 | "Invalid accumulated classification during merge."); |
| 492 | if (Accum == Field || Field == NoClass) |
| 493 | return Accum; |
| 494 | else if (Field == Memory) |
| 495 | return Memory; |
| 496 | else if (Accum == NoClass) |
| 497 | return Field; |
| 498 | else if (Accum == Integer || Field == Integer) |
| 499 | return Integer; |
| 500 | else if (Field == X87 || Field == X87Up || Field == ComplexX87) |
| 501 | return Memory; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 502 | else |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 503 | return SSE; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 506 | void X86_64ABIInfo::classify(QualType Ty, |
| 507 | ASTContext &Context, |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 508 | uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 509 | Class &Lo, Class &Hi) const { |
Daniel Dunbar | 9a82b52 | 2009-02-02 18:06:39 +0000 | [diff] [blame] | 510 | // FIXME: This code can be simplified by introducing a simple value |
| 511 | // class for Class pairs with appropriate constructor methods for |
| 512 | // the various situations. |
| 513 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 514 | Lo = Hi = NoClass; |
| 515 | |
| 516 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 517 | Current = Memory; |
| 518 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 519 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) { |
| 520 | BuiltinType::Kind k = BT->getKind(); |
| 521 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 522 | if (k == BuiltinType::Void) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 523 | Current = NoClass; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 524 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 525 | Current = Integer; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 526 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 527 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 528 | } else if (k == BuiltinType::LongDouble) { |
| 529 | Lo = X87; |
| 530 | Hi = X87Up; |
| 531 | } |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 532 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 533 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 534 | // FIXME: __int128 is (Integer, Integer). |
| 535 | } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() || |
| 536 | Ty->isObjCQualifiedInterfaceType()) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 537 | Current = Integer; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 538 | } else if (const VectorType *VT = Ty->getAsVectorType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 539 | uint64_t Size = Context.getTypeSize(VT); |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 540 | if (Size == 64) { |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 541 | // gcc passes <1 x double> in memory. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 542 | if (VT->getElementType() == Context.DoubleTy) |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 543 | return; |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 544 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 545 | Current = SSE; |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 546 | |
| 547 | // If this type crosses an eightbyte boundary, it should be |
| 548 | // split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 549 | if (OffsetBase && OffsetBase != 64) |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 550 | Hi = Lo; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 551 | } else if (Size == 128) { |
| 552 | Lo = SSE; |
| 553 | Hi = SSEUp; |
| 554 | } |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 555 | } else if (const ComplexType *CT = Ty->getAsComplexType()) { |
| 556 | QualType ET = CT->getElementType(); |
| 557 | |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 558 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 559 | if (ET->isIntegerType()) { |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 560 | if (Size <= 64) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 561 | Current = Integer; |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 562 | else if (Size <= 128) |
| 563 | Lo = Hi = Integer; |
| 564 | } else if (ET == Context.FloatTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 565 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 566 | else if (ET == Context.DoubleTy) |
| 567 | Lo = Hi = SSE; |
| 568 | else if (ET == Context.LongDoubleTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 569 | Current = ComplexX87; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 570 | |
| 571 | // If this complex type crosses an eightbyte boundary then it |
| 572 | // should be split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 573 | uint64_t EB_Real = (OffsetBase) / 64; |
| 574 | uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 575 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 576 | Hi = Lo; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 577 | } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 578 | // Arrays are treated like structures. |
| 579 | |
| 580 | uint64_t Size = Context.getTypeSize(Ty); |
| 581 | |
| 582 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 583 | // than two eightbytes, ..., it has class MEMORY. |
| 584 | if (Size > 128) |
| 585 | return; |
| 586 | |
| 587 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 588 | // fields, it has class MEMORY. |
| 589 | // |
| 590 | // Only need to check alignment of array base. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 591 | if (OffsetBase % Context.getTypeAlign(AT->getElementType())) |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 592 | return; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 593 | |
| 594 | // Otherwise implement simplified merge. We could be smarter about |
| 595 | // this, but it isn't worth it and would be harder to verify. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 596 | Current = NoClass; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 597 | uint64_t EltSize = Context.getTypeSize(AT->getElementType()); |
| 598 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
| 599 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 600 | Class FieldLo, FieldHi; |
| 601 | classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 602 | Lo = merge(Lo, FieldLo); |
| 603 | Hi = merge(Hi, FieldHi); |
| 604 | if (Lo == Memory || Hi == Memory) |
| 605 | break; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 606 | } |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 607 | |
| 608 | // Do post merger cleanup (see below). Only case we worry about is Memory. |
| 609 | if (Hi == Memory) |
| 610 | Lo = Memory; |
| 611 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 612 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 613 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 614 | |
| 615 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 616 | // than two eightbytes, ..., it has class MEMORY. |
| 617 | if (Size > 128) |
| 618 | return; |
| 619 | |
| 620 | const RecordDecl *RD = RT->getDecl(); |
| 621 | |
| 622 | // Assume variable sized types are passed in memory. |
| 623 | if (RD->hasFlexibleArrayMember()) |
| 624 | return; |
| 625 | |
| 626 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 627 | |
| 628 | // Reset Lo class, this will be recomputed. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 629 | Current = NoClass; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 630 | unsigned idx = 0; |
| 631 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 632 | e = RD->field_end(); i != e; ++i, ++idx) { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 633 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 634 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 635 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 636 | // fields, it has class MEMORY. |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 637 | if (Offset % Context.getTypeAlign(i->getType())) { |
| 638 | Lo = Memory; |
| 639 | return; |
| 640 | } |
| 641 | |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 642 | // Classify this field. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 643 | // |
| 644 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 645 | // exceeds a single eightbyte, each is classified |
| 646 | // separately. Each eightbyte gets initialized to class |
| 647 | // NO_CLASS. |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 648 | Class FieldLo, FieldHi; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 649 | classify(i->getType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 650 | Lo = merge(Lo, FieldLo); |
| 651 | Hi = merge(Hi, FieldHi); |
| 652 | if (Lo == Memory || Hi == Memory) |
| 653 | break; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 657 | // |
| 658 | // (a) If one of the classes is MEMORY, the whole argument is |
| 659 | // passed in memory. |
| 660 | // |
| 661 | // (b) If SSEUP is not preceeded by SSE, it is converted to SSE. |
| 662 | |
| 663 | // The first of these conditions is guaranteed by how we implement |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 664 | // the merge (just bail). |
| 665 | // |
| 666 | // The second condition occurs in the case of unions; for example |
| 667 | // union { _Complex double; unsigned; }. |
| 668 | if (Hi == Memory) |
| 669 | Lo = Memory; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 670 | if (Hi == SSEUp && Lo != SSE) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 671 | Hi = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 675 | |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 676 | ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy, |
| 677 | ASTContext &Context) const { |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 678 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 679 | // classification algorithm. |
| 680 | X86_64ABIInfo::Class Lo, Hi; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 681 | classify(RetTy, Context, 0, Lo, Hi); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 682 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 683 | // Check some invariants. |
| 684 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| 685 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); |
| 686 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 687 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 688 | const llvm::Type *ResType = 0; |
| 689 | switch (Lo) { |
| 690 | case NoClass: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 691 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 692 | |
| 693 | case SSEUp: |
| 694 | case X87Up: |
| 695 | assert(0 && "Invalid classification for lo word."); |
| 696 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 697 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 698 | // hidden argument, i.e. structret. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 699 | case Memory: |
| 700 | return ABIArgInfo::getStructRet(); |
| 701 | |
| 702 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 703 | // available register of the sequence %rax, %rdx is used. |
| 704 | case Integer: |
| 705 | ResType = llvm::Type::Int64Ty; break; |
| 706 | |
| 707 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 708 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 709 | case SSE: |
| 710 | ResType = llvm::Type::DoubleTy; break; |
| 711 | |
| 712 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 713 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 714 | case X87: |
| 715 | ResType = llvm::Type::X86_FP80Ty; break; |
| 716 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 717 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 718 | // part of the value is returned in %st0 and the imaginary part in |
| 719 | // %st1. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 720 | case ComplexX87: |
| 721 | assert(Hi == NoClass && "Unexpected ComplexX87 classification."); |
| 722 | ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2); |
| 723 | break; |
| 724 | } |
| 725 | |
| 726 | switch (Hi) { |
| 727 | // Memory was handled previously, and ComplexX87 and X87 should |
| 728 | // never occur as hi classes. |
| 729 | case Memory: |
| 730 | case X87: |
| 731 | case ComplexX87: |
| 732 | assert(0 && "Invalid classification for hi word."); |
| 733 | |
| 734 | case NoClass: break; |
| 735 | case Integer: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 736 | ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL); |
| 737 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 738 | case SSE: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 739 | ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL); |
| 740 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 741 | |
| 742 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
| 743 | // is passed in the upper half of the last used SSE register. |
| 744 | // |
| 745 | // SSEUP should always be preceeded by SSE, just widen. |
| 746 | case SSEUp: |
| 747 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
| 748 | ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2); |
| 749 | break; |
| 750 | |
| 751 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 752 | // returned together with the previous X87 value in %st0. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 753 | // |
| 754 | // X87UP should always be preceeded by X87, so we don't need to do |
| 755 | // anything here. |
| 756 | case X87Up: |
| 757 | assert(Lo == X87 && "Unexpected X87Up classification."); |
| 758 | break; |
| 759 | } |
| 760 | |
| 761 | return ABIArgInfo::getCoerce(ResType); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, |
| 765 | ASTContext &Context) const { |
| 766 | return ABIArgInfo::getDefault(); |
| 767 | } |
| 768 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 769 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy, |
| 770 | ASTContext &Context) const { |
| 771 | return ABIArgInfo::getDefault(); |
| 772 | } |
| 773 | |
| 774 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty, |
| 775 | ASTContext &Context) const { |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 776 | return ABIArgInfo::getDefault(); |
| 777 | } |
| 778 | |
| 779 | const ABIInfo &CodeGenTypes::getABIInfo() const { |
| 780 | if (TheABIInfo) |
| 781 | return *TheABIInfo; |
| 782 | |
| 783 | // For now we just cache this in the CodeGenTypes and don't bother |
| 784 | // to free it. |
| 785 | const char *TargetPrefix = getContext().Target.getTargetPrefix(); |
| 786 | if (strcmp(TargetPrefix, "x86") == 0) { |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 787 | switch (getContext().Target.getPointerWidth(0)) { |
| 788 | case 32: |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 789 | return *(TheABIInfo = new X86_32ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 790 | case 64: |
Daniel Dunbar | 11a76ed | 2009-01-30 18:47:53 +0000 | [diff] [blame] | 791 | return *(TheABIInfo = new X86_64ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 792 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return *(TheABIInfo = new DefaultABIInfo); |
| 796 | } |
| 797 | |
| 798 | // getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering |
| 799 | // "default" types to StructRet when appropriate for simplicity. |
| 800 | static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) { |
| 801 | assert(!Ty->isArrayType() && |
| 802 | "Array types cannot be passed directly."); |
| 803 | ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext()); |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 804 | // Ensure default on aggregate types is StructRet. |
| 805 | if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 806 | return ABIArgInfo::getStructRet(); |
| 807 | return Info; |
| 808 | } |
| 809 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 810 | // getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering |
| 811 | // "default" types to ByVal when appropriate for simplicity. |
| 812 | static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) { |
| 813 | assert(!Ty->isArrayType() && |
| 814 | "Array types cannot be passed directly."); |
| 815 | ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext()); |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 816 | // Ensure default on aggregate types is ByVal. |
| 817 | if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 818 | return ABIArgInfo::getByVal(0); |
| 819 | return Info; |
| 820 | } |
| 821 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 822 | /***/ |
| 823 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 824 | void CodeGenTypes::GetExpandedTypes(QualType Ty, |
| 825 | std::vector<const llvm::Type*> &ArgTys) { |
| 826 | const RecordType *RT = Ty->getAsStructureType(); |
| 827 | assert(RT && "Can only expand structure types."); |
| 828 | const RecordDecl *RD = RT->getDecl(); |
| 829 | assert(!RD->hasFlexibleArrayMember() && |
| 830 | "Cannot expand structure with flexible array."); |
| 831 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 832 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 833 | e = RD->field_end(); i != e; ++i) { |
| 834 | const FieldDecl *FD = *i; |
| 835 | assert(!FD->isBitField() && |
| 836 | "Cannot expand structure with bit-field members."); |
| 837 | |
| 838 | QualType FT = FD->getType(); |
| 839 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 840 | GetExpandedTypes(FT, ArgTys); |
| 841 | } else { |
| 842 | ArgTys.push_back(ConvertType(FT)); |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | llvm::Function::arg_iterator |
| 848 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, |
| 849 | llvm::Function::arg_iterator AI) { |
| 850 | const RecordType *RT = Ty->getAsStructureType(); |
| 851 | assert(RT && "Can only expand structure types."); |
| 852 | |
| 853 | RecordDecl *RD = RT->getDecl(); |
| 854 | assert(LV.isSimple() && |
| 855 | "Unexpected non-simple lvalue during struct expansion."); |
| 856 | llvm::Value *Addr = LV.getAddress(); |
| 857 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 858 | e = RD->field_end(); i != e; ++i) { |
| 859 | FieldDecl *FD = *i; |
| 860 | QualType FT = FD->getType(); |
| 861 | |
| 862 | // FIXME: What are the right qualifiers here? |
| 863 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 864 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 865 | AI = ExpandTypeFromArgs(FT, LV, AI); |
| 866 | } else { |
| 867 | EmitStoreThroughLValue(RValue::get(AI), LV, FT); |
| 868 | ++AI; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | return AI; |
| 873 | } |
| 874 | |
| 875 | void |
| 876 | CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, |
| 877 | llvm::SmallVector<llvm::Value*, 16> &Args) { |
| 878 | const RecordType *RT = Ty->getAsStructureType(); |
| 879 | assert(RT && "Can only expand structure types."); |
| 880 | |
| 881 | RecordDecl *RD = RT->getDecl(); |
| 882 | assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); |
| 883 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 884 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 885 | e = RD->field_end(); i != e; ++i) { |
| 886 | FieldDecl *FD = *i; |
| 887 | QualType FT = FD->getType(); |
| 888 | |
| 889 | // FIXME: What are the right qualifiers here? |
| 890 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 891 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 892 | ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); |
| 893 | } else { |
| 894 | RValue RV = EmitLoadOfLValue(LV, FT); |
| 895 | assert(RV.isScalar() && |
| 896 | "Unexpected non-scalar rvalue during struct expansion."); |
| 897 | Args.push_back(RV.getScalarVal()); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 902 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
| 903 | /// a pointer to an object of type \arg Ty. |
| 904 | /// |
| 905 | /// This safely handles the case when the src type is smaller than the |
| 906 | /// destination type; in this situation the values of bits which not |
| 907 | /// present in the src are undefined. |
| 908 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, |
| 909 | const llvm::Type *Ty, |
| 910 | CodeGenFunction &CGF) { |
| 911 | const llvm::Type *SrcTy = |
| 912 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 913 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 914 | uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty); |
| 915 | |
| 916 | // If load is legal, just bitcase the src pointer. |
| 917 | if (SrcSize == DstSize) { |
| 918 | llvm::Value *Casted = |
| 919 | CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); |
| 920 | return CGF.Builder.CreateLoad(Casted); |
| 921 | } else { |
| 922 | assert(SrcSize < DstSize && "Coercion is losing source bits!"); |
| 923 | |
| 924 | // Otherwise do coercion through memory. This is stupid, but |
| 925 | // simple. |
| 926 | llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); |
| 927 | llvm::Value *Casted = |
| 928 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); |
| 929 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); |
| 930 | return CGF.Builder.CreateLoad(Tmp); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
| 935 | /// where the source and destination may have different types. |
| 936 | /// |
| 937 | /// This safely handles the case when the src type is larger than the |
| 938 | /// destination type; the upper bits of the src will be lost. |
| 939 | static void CreateCoercedStore(llvm::Value *Src, |
| 940 | llvm::Value *DstPtr, |
| 941 | CodeGenFunction &CGF) { |
| 942 | const llvm::Type *SrcTy = Src->getType(); |
| 943 | const llvm::Type *DstTy = |
| 944 | cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 945 | |
| 946 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 947 | uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy); |
| 948 | |
| 949 | // If store is legal, just bitcase the src pointer. |
| 950 | if (SrcSize == DstSize) { |
| 951 | llvm::Value *Casted = |
| 952 | CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); |
| 953 | CGF.Builder.CreateStore(Src, Casted); |
| 954 | } else { |
| 955 | assert(SrcSize > DstSize && "Coercion is missing bits!"); |
| 956 | |
| 957 | // Otherwise do coercion through memory. This is stupid, but |
| 958 | // simple. |
| 959 | llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); |
| 960 | CGF.Builder.CreateStore(Src, Tmp); |
| 961 | llvm::Value *Casted = |
| 962 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); |
| 963 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr); |
| 964 | } |
| 965 | } |
| 966 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 967 | /***/ |
| 968 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 969 | bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) { |
| 970 | return getABIReturnInfo(FI.getReturnType(), getTypes()).isStructRet(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 973 | const llvm::FunctionType * |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 974 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 975 | std::vector<const llvm::Type*> ArgTys; |
| 976 | |
| 977 | const llvm::Type *ResultType = 0; |
| 978 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 979 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 980 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 981 | switch (RetAI.getKind()) { |
| 982 | case ABIArgInfo::ByVal: |
| 983 | case ABIArgInfo::Expand: |
| 984 | assert(0 && "Invalid ABI kind for return argument"); |
| 985 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 986 | case ABIArgInfo::Default: |
| 987 | if (RetTy->isVoidType()) { |
| 988 | ResultType = llvm::Type::VoidTy; |
| 989 | } else { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 990 | ResultType = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 991 | } |
| 992 | break; |
| 993 | |
| 994 | case ABIArgInfo::StructRet: { |
| 995 | ResultType = llvm::Type::VoidTy; |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 996 | const llvm::Type *STy = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 997 | ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); |
| 998 | break; |
| 999 | } |
| 1000 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1001 | case ABIArgInfo::Ignore: |
| 1002 | ResultType = llvm::Type::VoidTy; |
| 1003 | break; |
| 1004 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1005 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1006 | ResultType = RetAI.getCoerceToType(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1007 | break; |
| 1008 | } |
| 1009 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1010 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 1011 | it != ie; ++it) { |
| 1012 | ABIArgInfo AI = getABIArgumentInfo(*it, *this); |
| 1013 | const llvm::Type *Ty = ConvertType(*it); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1014 | |
| 1015 | switch (AI.getKind()) { |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1016 | case ABIArgInfo::Ignore: |
| 1017 | break; |
| 1018 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1019 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1020 | case ABIArgInfo::StructRet: |
| 1021 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1022 | |
| 1023 | case ABIArgInfo::ByVal: |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1024 | // byval arguments are always on the stack, which is addr space #0. |
| 1025 | ArgTys.push_back(llvm::PointerType::getUnqual(Ty)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1026 | assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); |
| 1027 | break; |
| 1028 | |
| 1029 | case ABIArgInfo::Default: |
| 1030 | ArgTys.push_back(Ty); |
| 1031 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1032 | |
| 1033 | case ABIArgInfo::Expand: |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1034 | GetExpandedTypes(*it, ArgTys); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1039 | return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1042 | void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1043 | const Decl *TargetDecl, |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1044 | AttributeListType &PAL) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1045 | unsigned FuncAttrs = 0; |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1046 | unsigned RetAttrs = 0; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (TargetDecl) { |
| 1049 | if (TargetDecl->getAttr<NoThrowAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1050 | FuncAttrs |= llvm::Attribute::NoUnwind; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1051 | if (TargetDecl->getAttr<NoReturnAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1052 | FuncAttrs |= llvm::Attribute::NoReturn; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1053 | if (TargetDecl->getAttr<PureAttr>()) |
| 1054 | FuncAttrs |= llvm::Attribute::ReadOnly; |
| 1055 | if (TargetDecl->getAttr<ConstAttr>()) |
| 1056 | FuncAttrs |= llvm::Attribute::ReadNone; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1059 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1060 | unsigned Index = 1; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1061 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1062 | switch (RetAI.getKind()) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1063 | case ABIArgInfo::Default: |
| 1064 | if (RetTy->isPromotableIntegerType()) { |
| 1065 | if (RetTy->isSignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1066 | RetAttrs |= llvm::Attribute::SExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1067 | } else if (RetTy->isUnsignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1068 | RetAttrs |= llvm::Attribute::ZExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | break; |
| 1072 | |
| 1073 | case ABIArgInfo::StructRet: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1074 | PAL.push_back(llvm::AttributeWithIndex::get(Index, |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1075 | llvm::Attribute::StructRet | |
| 1076 | llvm::Attribute::NoAlias)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1077 | ++Index; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1078 | break; |
| 1079 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1080 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1081 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1082 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1083 | |
| 1084 | case ABIArgInfo::ByVal: |
| 1085 | case ABIArgInfo::Expand: |
| 1086 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1087 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1088 | |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1089 | if (RetAttrs) |
| 1090 | PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1091 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 1092 | it != ie; ++it) { |
| 1093 | QualType ParamType = *it; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1094 | unsigned Attributes = 0; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1095 | ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes()); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1096 | |
| 1097 | switch (AI.getKind()) { |
| 1098 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1099 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1100 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1101 | |
| 1102 | case ABIArgInfo::ByVal: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1103 | Attributes |= llvm::Attribute::ByVal; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1104 | assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); |
| 1105 | break; |
| 1106 | |
| 1107 | case ABIArgInfo::Default: |
| 1108 | if (ParamType->isPromotableIntegerType()) { |
| 1109 | if (ParamType->isSignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1110 | Attributes |= llvm::Attribute::SExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1111 | } else if (ParamType->isUnsignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1112 | Attributes |= llvm::Attribute::ZExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1113 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1114 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1115 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1116 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1117 | case ABIArgInfo::Ignore: |
| 1118 | // Skip increment, no matching LLVM parameter. |
| 1119 | continue; |
| 1120 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1121 | case ABIArgInfo::Expand: { |
| 1122 | std::vector<const llvm::Type*> Tys; |
| 1123 | // FIXME: This is rather inefficient. Do we ever actually need |
| 1124 | // to do anything here? The result should be just reconstructed |
| 1125 | // on the other side, so extension should be a non-issue. |
| 1126 | getTypes().GetExpandedTypes(ParamType, Tys); |
| 1127 | Index += Tys.size(); |
| 1128 | continue; |
| 1129 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1130 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1131 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1132 | if (Attributes) |
| 1133 | PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1134 | ++Index; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1135 | } |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1136 | if (FuncAttrs) |
| 1137 | PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); |
| 1138 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1141 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 1142 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1143 | const FunctionArgList &Args) { |
| 1144 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 1145 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 1146 | |
| 1147 | // Name the struct return argument. |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1148 | if (CGM.ReturnTypeUsesSret(FI)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1149 | AI->setName("agg.result"); |
| 1150 | ++AI; |
| 1151 | } |
| 1152 | |
| 1153 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1154 | i != e; ++i) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1155 | const VarDecl *Arg = i->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1156 | QualType Ty = i->second; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1157 | ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes()); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1158 | |
| 1159 | switch (ArgI.getKind()) { |
| 1160 | case ABIArgInfo::ByVal: |
| 1161 | case ABIArgInfo::Default: { |
| 1162 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1163 | llvm::Value* V = AI; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1164 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1165 | // This must be a promotion, for something like |
| 1166 | // "void a(x) short x; {..." |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1167 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1168 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1169 | EmitParmDecl(*Arg, V); |
| 1170 | break; |
| 1171 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1172 | |
| 1173 | case ABIArgInfo::Expand: { |
| 1174 | // If this was structure was expand into multiple arguments then |
| 1175 | // we need to create a temporary and reconstruct it from the |
| 1176 | // arguments. |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1177 | std::string Name = Arg->getNameAsString(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1178 | llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty), |
| 1179 | (Name + ".addr").c_str()); |
| 1180 | // FIXME: What are the right qualifiers here? |
| 1181 | llvm::Function::arg_iterator End = |
| 1182 | ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI); |
| 1183 | EmitParmDecl(*Arg, Temp); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1184 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1185 | // Name the arguments used in expansion and increment AI. |
| 1186 | unsigned Index = 0; |
| 1187 | for (; AI != End; ++AI, ++Index) |
| 1188 | AI->setName(Name + "." + llvm::utostr(Index)); |
| 1189 | continue; |
| 1190 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1191 | |
| 1192 | case ABIArgInfo::Ignore: |
| 1193 | break; |
| 1194 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1195 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1196 | case ABIArgInfo::StructRet: |
| 1197 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1198 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1199 | |
| 1200 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1201 | } |
| 1202 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 1203 | } |
| 1204 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1205 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1206 | llvm::Value *ReturnValue) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1207 | llvm::Value *RV = 0; |
| 1208 | |
| 1209 | // Functions with no result always return void. |
| 1210 | if (ReturnValue) { |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1211 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1212 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1213 | |
| 1214 | switch (RetAI.getKind()) { |
| 1215 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1216 | if (RetTy->isAnyComplexType()) { |
| 1217 | // FIXME: Volatile |
| 1218 | ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); |
| 1219 | StoreComplexToAddr(RT, CurFn->arg_begin(), false); |
| 1220 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1221 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
| 1222 | } else { |
| 1223 | Builder.CreateStore(Builder.CreateLoad(ReturnValue), |
| 1224 | CurFn->arg_begin()); |
| 1225 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1226 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1227 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1228 | case ABIArgInfo::Default: |
| 1229 | RV = Builder.CreateLoad(ReturnValue); |
| 1230 | break; |
| 1231 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1232 | case ABIArgInfo::Ignore: |
| 1233 | break; |
| 1234 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1235 | case ABIArgInfo::Coerce: { |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 1236 | RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1237 | break; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1238 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1239 | |
| 1240 | case ABIArgInfo::ByVal: |
| 1241 | case ABIArgInfo::Expand: |
| 1242 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1245 | |
| 1246 | if (RV) { |
| 1247 | Builder.CreateRet(RV); |
| 1248 | } else { |
| 1249 | Builder.CreateRetVoid(); |
| 1250 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1253 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
| 1254 | llvm::Value *Callee, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1255 | const CallArgList &CallArgs) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1256 | llvm::SmallVector<llvm::Value*, 16> Args; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1257 | |
| 1258 | // Handle struct-return functions by passing a pointer to the |
| 1259 | // location that we would like to return into. |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1260 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1261 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1262 | switch (RetAI.getKind()) { |
| 1263 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1264 | // Create a temporary alloca to hold the result of the call. :( |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1265 | Args.push_back(CreateTempAlloca(ConvertType(RetTy))); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1266 | break; |
| 1267 | |
| 1268 | case ABIArgInfo::Default: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1269 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1270 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1271 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1272 | |
| 1273 | case ABIArgInfo::ByVal: |
| 1274 | case ABIArgInfo::Expand: |
| 1275 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
| 1279 | I != E; ++I) { |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1280 | ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1281 | RValue RV = I->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1282 | |
| 1283 | switch (ArgInfo.getKind()) { |
| 1284 | case ABIArgInfo::ByVal: // Default is byval |
| 1285 | case ABIArgInfo::Default: |
| 1286 | if (RV.isScalar()) { |
| 1287 | Args.push_back(RV.getScalarVal()); |
| 1288 | } else if (RV.isComplex()) { |
| 1289 | // Make a temporary alloca to pass the argument. |
| 1290 | Args.push_back(CreateTempAlloca(ConvertType(I->second))); |
| 1291 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
| 1292 | } else { |
| 1293 | Args.push_back(RV.getAggregateAddr()); |
| 1294 | } |
| 1295 | break; |
| 1296 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1297 | case ABIArgInfo::Ignore: |
| 1298 | break; |
| 1299 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1300 | case ABIArgInfo::StructRet: |
| 1301 | case ABIArgInfo::Coerce: |
| 1302 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1303 | break; |
| 1304 | |
| 1305 | case ABIArgInfo::Expand: |
| 1306 | ExpandTypeToArgs(I->second, RV, Args); |
| 1307 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1312 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1313 | // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set. |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1314 | CodeGen::AttributeListType AttributeList; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1315 | CGM.ConstructAttributeList(CallInfo, 0, AttributeList); |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1316 | CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1317 | AttributeList.size())); |
| 1318 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1319 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 1320 | CI->setCallingConv(F->getCallingConv()); |
| 1321 | if (CI->getType() != llvm::Type::VoidTy) |
| 1322 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1323 | |
| 1324 | switch (RetAI.getKind()) { |
| 1325 | case ABIArgInfo::StructRet: |
| 1326 | if (RetTy->isAnyComplexType()) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1327 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1328 | else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1329 | return RValue::getAggregate(Args[0]); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1330 | else |
| 1331 | return RValue::get(Builder.CreateLoad(Args[0])); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1332 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1333 | case ABIArgInfo::Default: |
| 1334 | return RValue::get(RetTy->isVoidType() ? 0 : CI); |
| 1335 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1336 | case ABIArgInfo::Ignore: |
Daniel Dunbar | cc039fe | 2009-01-29 08:24:57 +0000 | [diff] [blame] | 1337 | if (RetTy->isVoidType()) |
| 1338 | return RValue::get(0); |
| 1339 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1340 | llvm::Value *Res = |
| 1341 | llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy))); |
| 1342 | return RValue::getAggregate(Res); |
| 1343 | } |
| 1344 | return RValue::get(llvm::UndefValue::get(ConvertType(RetTy))); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1345 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1346 | case ABIArgInfo::Coerce: { |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 1347 | llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce"); |
| 1348 | CreateCoercedStore(CI, V, *this); |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1349 | if (RetTy->isAnyComplexType()) |
| 1350 | return RValue::getComplex(LoadComplexFromAddr(V, false)); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1351 | else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1352 | return RValue::getAggregate(V); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1353 | else |
| 1354 | return RValue::get(Builder.CreateLoad(V)); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1355 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1356 | |
| 1357 | case ABIArgInfo::ByVal: |
| 1358 | case ABIArgInfo::Expand: |
| 1359 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1360 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1361 | |
| 1362 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 1363 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1364 | } |