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