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