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