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 | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
| 27 | /***/ |
| 28 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 29 | // FIXME: Use iterator and sidestep silly type array creation. |
| 30 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 31 | CGFunctionInfo::CGFunctionInfo(const FunctionTypeNoProto *FTNP) |
| 32 | : IsVariadic(true) |
| 33 | { |
| 34 | ArgTypes.push_back(FTNP->getResultType()); |
| 35 | } |
| 36 | |
| 37 | CGFunctionInfo::CGFunctionInfo(const FunctionTypeProto *FTP) |
| 38 | : IsVariadic(FTP->isVariadic()) |
| 39 | { |
| 40 | ArgTypes.push_back(FTP->getResultType()); |
| 41 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 42 | ArgTypes.push_back(FTP->getArgType(i)); |
| 43 | } |
| 44 | |
| 45 | // FIXME: Is there really any reason to have this still? |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 46 | CGFunctionInfo::CGFunctionInfo(const FunctionDecl *FD) |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 47 | { |
| 48 | const FunctionType *FTy = FD->getType()->getAsFunctionType(); |
| 49 | const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 51 | ArgTypes.push_back(FTy->getResultType()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 52 | if (FTP) { |
| 53 | IsVariadic = FTP->isVariadic(); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 54 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
| 55 | ArgTypes.push_back(FTP->getArgType(i)); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 56 | } else { |
| 57 | IsVariadic = true; |
| 58 | } |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | CGFunctionInfo::CGFunctionInfo(const ObjCMethodDecl *MD, |
| 62 | const ASTContext &Context) |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 63 | : IsVariadic(MD->isVariadic()) |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 64 | { |
| 65 | ArgTypes.push_back(MD->getResultType()); |
| 66 | ArgTypes.push_back(MD->getSelfDecl()->getType()); |
| 67 | ArgTypes.push_back(Context.getObjCSelType()); |
| 68 | for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(), |
| 69 | e = MD->param_end(); i != e; ++i) |
| 70 | ArgTypes.push_back((*i)->getType()); |
| 71 | } |
| 72 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 73 | ArgTypeIterator CGFunctionInfo::argtypes_begin() const { |
| 74 | return ArgTypes.begin(); |
| 75 | } |
| 76 | |
| 77 | ArgTypeIterator CGFunctionInfo::argtypes_end() const { |
| 78 | return ArgTypes.end(); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /***/ |
| 82 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 83 | CGCallInfo::CGCallInfo(QualType _ResultType, const CallArgList &_Args) { |
| 84 | ArgTypes.push_back(_ResultType); |
| 85 | 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] | 86 | ArgTypes.push_back(i->second); |
| 87 | } |
| 88 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 89 | ArgTypeIterator CGCallInfo::argtypes_begin() const { |
| 90 | return ArgTypes.begin(); |
| 91 | } |
| 92 | |
| 93 | ArgTypeIterator CGCallInfo::argtypes_end() const { |
| 94 | return ArgTypes.end(); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 95 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 96 | |
| 97 | /***/ |
| 98 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 99 | /// ABIArgInfo - Helper class to encapsulate information about how a |
| 100 | /// specific C type should be passed to or returned from a function. |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 101 | class ABIArgInfo { |
| 102 | public: |
| 103 | enum Kind { |
| 104 | Default, |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 105 | StructRet, /// Only valid for aggregate return types. |
| 106 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 107 | Coerce, /// Only valid for aggregate return types, the argument |
| 108 | /// should be accessed by coercion to a provided type. |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 109 | |
| 110 | ByVal, /// Only valid for aggregate argument types. The |
| 111 | /// structure should be passed "byval" with the |
| 112 | /// specified alignment (0 indicates default |
| 113 | /// alignment). |
| 114 | |
| 115 | Expand, /// Only valid for aggregate argument types. The |
| 116 | /// structure should be expanded into consecutive |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 117 | /// arguments for its constituent fields. Currently |
| 118 | /// expand is only allowed on structures whose fields |
| 119 | /// are all scalar types or are themselves expandable |
| 120 | /// types. |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 121 | |
| 122 | KindFirst=Default, KindLast=Expand |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | private: |
| 126 | Kind TheKind; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 127 | const llvm::Type *TypeData; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 128 | unsigned UIntData; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 129 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 130 | ABIArgInfo(Kind K, const llvm::Type *TD=0, |
| 131 | unsigned UI=0) : TheKind(K), |
| 132 | TypeData(TD), |
| 133 | UIntData(0) {} |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 134 | public: |
| 135 | static ABIArgInfo getDefault() { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 136 | return ABIArgInfo(Default); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 137 | } |
| 138 | static ABIArgInfo getStructRet() { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 139 | return ABIArgInfo(StructRet); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 140 | } |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 141 | static ABIArgInfo getCoerce(const llvm::Type *T) { |
| 142 | assert(T->isSingleValueType() && "Can only coerce to simple types"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 143 | return ABIArgInfo(Coerce, T); |
| 144 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 145 | static ABIArgInfo getByVal(unsigned Alignment) { |
| 146 | return ABIArgInfo(ByVal, 0, Alignment); |
| 147 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 148 | static ABIArgInfo getExpand() { |
| 149 | return ABIArgInfo(Expand); |
| 150 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 151 | |
| 152 | Kind getKind() const { return TheKind; } |
| 153 | bool isDefault() const { return TheKind == Default; } |
| 154 | bool isStructRet() const { return TheKind == StructRet; } |
| 155 | bool isCoerce() const { return TheKind == Coerce; } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 156 | bool isByVal() const { return TheKind == ByVal; } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 157 | bool isExpand() const { return TheKind == Expand; } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 158 | |
| 159 | // Coerce accessors |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 160 | const llvm::Type *getCoerceToType() const { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 161 | assert(TheKind == Coerce && "Invalid kind!"); |
| 162 | return TypeData; |
| 163 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 164 | |
| 165 | // ByVal accessors |
| 166 | unsigned getByValAlignment() const { |
| 167 | assert(TheKind == ByVal && "Invalid kind!"); |
| 168 | return UIntData; |
| 169 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /***/ |
| 173 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 174 | /* FIXME: All of this stuff should be part of the target interface |
| 175 | somehow. It is currently here because it is not clear how to factor |
| 176 | the targets to support this, since the Targets currently live in a |
| 177 | layer below types n'stuff. |
| 178 | */ |
| 179 | |
| 180 | /// ABIInfo - Target specific hooks for defining how a type should be |
| 181 | /// passed or returned from functions. |
| 182 | class clang::ABIInfo { |
| 183 | public: |
| 184 | virtual ~ABIInfo(); |
| 185 | |
| 186 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 187 | ASTContext &Context) const = 0; |
| 188 | |
| 189 | virtual ABIArgInfo classifyArgumentType(QualType Ty, |
| 190 | ASTContext &Context) const = 0; |
| 191 | }; |
| 192 | |
| 193 | ABIInfo::~ABIInfo() {} |
| 194 | |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 195 | /// isEmptyStruct - Return true iff a structure has no non-empty |
| 196 | /// members. Note that a structure with a flexible array member is not |
| 197 | /// considered empty. |
| 198 | static bool isEmptyStruct(QualType T) { |
| 199 | const RecordType *RT = T->getAsStructureType(); |
| 200 | if (!RT) |
| 201 | return 0; |
| 202 | const RecordDecl *RD = RT->getDecl(); |
| 203 | if (RD->hasFlexibleArrayMember()) |
| 204 | return false; |
| 205 | for (RecordDecl::field_const_iterator i = RD->field_begin(), |
| 206 | e = RD->field_end(); i != e; ++i) { |
| 207 | const FieldDecl *FD = *i; |
| 208 | if (!isEmptyStruct(FD->getType())) |
| 209 | return false; |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | /// isSingleElementStruct - Determine if a structure is a "single |
| 215 | /// element struct", i.e. it has exactly one non-empty field or |
| 216 | /// exactly one field which is itself a single element |
| 217 | /// struct. Structures with flexible array members are never |
| 218 | /// considered single element structs. |
| 219 | /// |
| 220 | /// \return The field declaration for the single non-empty field, if |
| 221 | /// it exists. |
| 222 | static const FieldDecl *isSingleElementStruct(QualType T) { |
| 223 | const RecordType *RT = T->getAsStructureType(); |
| 224 | if (!RT) |
| 225 | return 0; |
| 226 | |
| 227 | const RecordDecl *RD = RT->getDecl(); |
| 228 | if (RD->hasFlexibleArrayMember()) |
| 229 | return 0; |
| 230 | |
| 231 | const FieldDecl *Found = 0; |
| 232 | for (RecordDecl::field_const_iterator i = RD->field_begin(), |
| 233 | e = RD->field_end(); i != e; ++i) { |
| 234 | const FieldDecl *FD = *i; |
| 235 | QualType FT = FD->getType(); |
| 236 | |
| 237 | if (isEmptyStruct(FT)) { |
| 238 | // Ignore |
| 239 | } else if (Found) { |
| 240 | return 0; |
| 241 | } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 242 | Found = FD; |
| 243 | } else { |
| 244 | Found = isSingleElementStruct(FT); |
| 245 | if (!Found) |
| 246 | return 0; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | return Found; |
| 251 | } |
| 252 | |
| 253 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 254 | if (!Ty->getAsBuiltinType() && !Ty->isPointerType()) |
| 255 | return false; |
| 256 | |
| 257 | uint64_t Size = Context.getTypeSize(Ty); |
| 258 | return Size == 32 || Size == 64; |
| 259 | } |
| 260 | |
| 261 | static bool areAllFields32Or64BitBasicType(const RecordDecl *RD, |
| 262 | ASTContext &Context) { |
| 263 | for (RecordDecl::field_const_iterator i = RD->field_begin(), |
| 264 | e = RD->field_end(); i != e; ++i) { |
| 265 | const FieldDecl *FD = *i; |
| 266 | |
| 267 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 268 | return false; |
| 269 | |
| 270 | // If this is a bit-field we need to make sure it is still a |
| 271 | // 32-bit or 64-bit type. |
| 272 | if (Expr *BW = FD->getBitWidth()) { |
| 273 | unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue(); |
| 274 | if (Width <= 16) |
| 275 | return false; |
| 276 | } |
| 277 | } |
| 278 | return true; |
| 279 | } |
| 280 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 281 | namespace { |
| 282 | /// DefaultABIInfo - The default implementation for ABI specific |
| 283 | /// details. This implementation provides information which results in |
| 284 | /// sensible LLVM IR generation, but does not conform to any |
| 285 | /// particular ABI. |
| 286 | class DefaultABIInfo : public ABIInfo { |
| 287 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 288 | ASTContext &Context) const; |
| 289 | |
| 290 | virtual ABIArgInfo classifyArgumentType(QualType RetTy, |
| 291 | ASTContext &Context) const; |
| 292 | }; |
| 293 | |
| 294 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 295 | class X86_32ABIInfo : public ABIInfo { |
| 296 | public: |
| 297 | virtual ABIArgInfo classifyReturnType(QualType RetTy, |
| 298 | ASTContext &Context) const; |
| 299 | |
| 300 | virtual ABIArgInfo classifyArgumentType(QualType RetTy, |
| 301 | ASTContext &Context) const; |
| 302 | }; |
| 303 | } |
| 304 | |
| 305 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 306 | ASTContext &Context) const { |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 307 | assert(!RetTy->isArrayType() && |
| 308 | "Array types cannot be passed directly."); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 309 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 310 | // Classify "single element" structs as their element type. |
| 311 | const FieldDecl *SeltFD = isSingleElementStruct(RetTy); |
| 312 | if (SeltFD) { |
| 313 | QualType SeltTy = SeltFD->getType()->getDesugaredType(); |
| 314 | if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) { |
| 315 | // FIXME: This is gross, it would be nice if we could just |
| 316 | // pass back SeltTy and have clients deal with it. Is it worth |
| 317 | // supporting coerce to both LLVM and clang Types? |
| 318 | if (BT->isIntegerType()) { |
| 319 | uint64_t Size = Context.getTypeSize(SeltTy); |
| 320 | return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size)); |
| 321 | } else if (BT->getKind() == BuiltinType::Float) { |
| 322 | return ABIArgInfo::getCoerce(llvm::Type::FloatTy); |
| 323 | } else if (BT->getKind() == BuiltinType::Double) { |
| 324 | return ABIArgInfo::getCoerce(llvm::Type::DoubleTy); |
| 325 | } |
| 326 | } else if (SeltTy->isPointerType()) { |
| 327 | // FIXME: It would be really nice if this could come out as |
| 328 | // the proper pointer type. |
| 329 | llvm::Type *PtrTy = |
| 330 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 331 | return ABIArgInfo::getCoerce(PtrTy); |
| 332 | } |
| 333 | } |
| 334 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 335 | uint64_t Size = Context.getTypeSize(RetTy); |
| 336 | if (Size == 8) { |
| 337 | return ABIArgInfo::getCoerce(llvm::Type::Int8Ty); |
| 338 | } else if (Size == 16) { |
| 339 | return ABIArgInfo::getCoerce(llvm::Type::Int16Ty); |
| 340 | } else if (Size == 32) { |
| 341 | return ABIArgInfo::getCoerce(llvm::Type::Int32Ty); |
| 342 | } else if (Size == 64) { |
| 343 | return ABIArgInfo::getCoerce(llvm::Type::Int64Ty); |
| 344 | } else { |
| 345 | return ABIArgInfo::getStructRet(); |
| 346 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 347 | } else { |
| 348 | return ABIArgInfo::getDefault(); |
| 349 | } |
| 350 | } |
| 351 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 352 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 353 | ASTContext &Context) const { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 354 | assert(!Ty->isArrayType() && "Array types cannot be passed directly."); |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 355 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 356 | // Structures with flexible arrays are always byval. |
| 357 | if (const RecordType *RT = Ty->getAsStructureType()) |
| 358 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 359 | return ABIArgInfo::getByVal(0); |
| 360 | |
| 361 | // Expand empty structs (i.e. ignore) |
| 362 | uint64_t Size = Context.getTypeSize(Ty); |
| 363 | if (Ty->isStructureType() && Size == 0) |
| 364 | return ABIArgInfo::getExpand(); |
| 365 | |
| 366 | // Expand structs with size <= 128-bits which consist only of |
| 367 | // basic types (int, long long, float, double, xxx*). This is |
| 368 | // non-recursive and does not ignore empty fields. |
| 369 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 370 | if (Context.getTypeSize(Ty) <= 4*32 && |
| 371 | areAllFields32Or64BitBasicType(RT->getDecl(), Context)) |
| 372 | return ABIArgInfo::getExpand(); |
| 373 | } |
| 374 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 375 | return ABIArgInfo::getByVal(0); |
| 376 | } else { |
| 377 | return ABIArgInfo::getDefault(); |
| 378 | } |
| 379 | } |
| 380 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 381 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy, |
| 382 | ASTContext &Context) const { |
| 383 | return ABIArgInfo::getDefault(); |
| 384 | } |
| 385 | |
| 386 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty, |
| 387 | ASTContext &Context) const { |
| 388 | assert(!Ty->isArrayType() && "Array types cannot be passed directly."); |
| 389 | return ABIArgInfo::getDefault(); |
| 390 | } |
| 391 | |
| 392 | const ABIInfo &CodeGenTypes::getABIInfo() const { |
| 393 | if (TheABIInfo) |
| 394 | return *TheABIInfo; |
| 395 | |
| 396 | // For now we just cache this in the CodeGenTypes and don't bother |
| 397 | // to free it. |
| 398 | const char *TargetPrefix = getContext().Target.getTargetPrefix(); |
| 399 | if (strcmp(TargetPrefix, "x86") == 0) { |
| 400 | if (getContext().Target.getPointerWidth(0) == 32) |
| 401 | return *(TheABIInfo = new X86_32ABIInfo()); |
| 402 | } |
| 403 | |
| 404 | return *(TheABIInfo = new DefaultABIInfo); |
| 405 | } |
| 406 | |
| 407 | // getABIReturnInfo - Wrap the ABIInfo getABIReturnInfo, altering |
| 408 | // "default" types to StructRet when appropriate for simplicity. |
| 409 | static ABIArgInfo getABIReturnInfo(QualType Ty, CodeGenTypes &CGT) { |
| 410 | assert(!Ty->isArrayType() && |
| 411 | "Array types cannot be passed directly."); |
| 412 | ABIArgInfo Info = CGT.getABIInfo().classifyReturnType(Ty, CGT.getContext()); |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 413 | // Ensure default on aggregate types is StructRet. |
| 414 | if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 415 | return ABIArgInfo::getStructRet(); |
| 416 | return Info; |
| 417 | } |
| 418 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 419 | // getABIArgumentInfo - Wrap the ABIInfo getABIReturnInfo, altering |
| 420 | // "default" types to ByVal when appropriate for simplicity. |
| 421 | static ABIArgInfo getABIArgumentInfo(QualType Ty, CodeGenTypes &CGT) { |
| 422 | assert(!Ty->isArrayType() && |
| 423 | "Array types cannot be passed directly."); |
| 424 | ABIArgInfo Info = CGT.getABIInfo().classifyArgumentType(Ty, CGT.getContext()); |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 425 | // Ensure default on aggregate types is ByVal. |
| 426 | if (Info.isDefault() && CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 427 | return ABIArgInfo::getByVal(0); |
| 428 | return Info; |
| 429 | } |
| 430 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 431 | /***/ |
| 432 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 433 | void CodeGenTypes::GetExpandedTypes(QualType Ty, |
| 434 | std::vector<const llvm::Type*> &ArgTys) { |
| 435 | const RecordType *RT = Ty->getAsStructureType(); |
| 436 | assert(RT && "Can only expand structure types."); |
| 437 | const RecordDecl *RD = RT->getDecl(); |
| 438 | assert(!RD->hasFlexibleArrayMember() && |
| 439 | "Cannot expand structure with flexible array."); |
| 440 | |
| 441 | for (RecordDecl::field_const_iterator i = RD->field_begin(), |
| 442 | e = RD->field_end(); i != e; ++i) { |
| 443 | const FieldDecl *FD = *i; |
| 444 | assert(!FD->isBitField() && |
| 445 | "Cannot expand structure with bit-field members."); |
| 446 | |
| 447 | QualType FT = FD->getType(); |
| 448 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 449 | GetExpandedTypes(FT, ArgTys); |
| 450 | } else { |
| 451 | ArgTys.push_back(ConvertType(FT)); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | llvm::Function::arg_iterator |
| 457 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, |
| 458 | llvm::Function::arg_iterator AI) { |
| 459 | const RecordType *RT = Ty->getAsStructureType(); |
| 460 | assert(RT && "Can only expand structure types."); |
| 461 | |
| 462 | RecordDecl *RD = RT->getDecl(); |
| 463 | assert(LV.isSimple() && |
| 464 | "Unexpected non-simple lvalue during struct expansion."); |
| 465 | llvm::Value *Addr = LV.getAddress(); |
| 466 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 467 | e = RD->field_end(); i != e; ++i) { |
| 468 | FieldDecl *FD = *i; |
| 469 | QualType FT = FD->getType(); |
| 470 | |
| 471 | // FIXME: What are the right qualifiers here? |
| 472 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 473 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 474 | AI = ExpandTypeFromArgs(FT, LV, AI); |
| 475 | } else { |
| 476 | EmitStoreThroughLValue(RValue::get(AI), LV, FT); |
| 477 | ++AI; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return AI; |
| 482 | } |
| 483 | |
| 484 | void |
| 485 | CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, |
| 486 | llvm::SmallVector<llvm::Value*, 16> &Args) { |
| 487 | const RecordType *RT = Ty->getAsStructureType(); |
| 488 | assert(RT && "Can only expand structure types."); |
| 489 | |
| 490 | RecordDecl *RD = RT->getDecl(); |
| 491 | assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); |
| 492 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 493 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 494 | e = RD->field_end(); i != e; ++i) { |
| 495 | FieldDecl *FD = *i; |
| 496 | QualType FT = FD->getType(); |
| 497 | |
| 498 | // FIXME: What are the right qualifiers here? |
| 499 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 500 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 501 | ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); |
| 502 | } else { |
| 503 | RValue RV = EmitLoadOfLValue(LV, FT); |
| 504 | assert(RV.isScalar() && |
| 505 | "Unexpected non-scalar rvalue during struct expansion."); |
| 506 | Args.push_back(RV.getScalarVal()); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /***/ |
| 512 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 513 | const llvm::FunctionType * |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 514 | CodeGenTypes::GetFunctionType(const CGCallInfo &CI, bool IsVariadic) { |
| 515 | return GetFunctionType(CI.argtypes_begin(), CI.argtypes_end(), IsVariadic); |
| 516 | } |
| 517 | |
| 518 | const llvm::FunctionType * |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 519 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 520 | return GetFunctionType(FI.argtypes_begin(), FI.argtypes_end(), FI.isVariadic()); |
| 521 | } |
| 522 | |
| 523 | const llvm::FunctionType * |
| 524 | CodeGenTypes::GetFunctionType(ArgTypeIterator begin, ArgTypeIterator end, |
| 525 | bool IsVariadic) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 526 | std::vector<const llvm::Type*> ArgTys; |
| 527 | |
| 528 | const llvm::Type *ResultType = 0; |
| 529 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 530 | QualType RetTy = *begin; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 531 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, *this); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 532 | switch (RetAI.getKind()) { |
| 533 | case ABIArgInfo::ByVal: |
| 534 | case ABIArgInfo::Expand: |
| 535 | assert(0 && "Invalid ABI kind for return argument"); |
| 536 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 537 | case ABIArgInfo::Default: |
| 538 | if (RetTy->isVoidType()) { |
| 539 | ResultType = llvm::Type::VoidTy; |
| 540 | } else { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 541 | ResultType = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 542 | } |
| 543 | break; |
| 544 | |
| 545 | case ABIArgInfo::StructRet: { |
| 546 | ResultType = llvm::Type::VoidTy; |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 547 | const llvm::Type *STy = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 548 | ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 553 | ResultType = RetAI.getCoerceToType(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 554 | break; |
| 555 | } |
| 556 | |
| 557 | for (++begin; begin != end; ++begin) { |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 558 | ABIArgInfo AI = getABIArgumentInfo(*begin, *this); |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 559 | const llvm::Type *Ty = ConvertType(*begin); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 560 | |
| 561 | switch (AI.getKind()) { |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 562 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 563 | case ABIArgInfo::StructRet: |
| 564 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 565 | |
| 566 | case ABIArgInfo::ByVal: |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 567 | // byval arguments are always on the stack, which is addr space #0. |
| 568 | ArgTys.push_back(llvm::PointerType::getUnqual(Ty)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 569 | assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); |
| 570 | break; |
| 571 | |
| 572 | case ABIArgInfo::Default: |
| 573 | ArgTys.push_back(Ty); |
| 574 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 575 | |
| 576 | case ABIArgInfo::Expand: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 577 | GetExpandedTypes(*begin, ArgTys); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 578 | break; |
| 579 | } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 582 | return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 585 | bool CodeGenModule::ReturnTypeUsesSret(QualType RetTy) { |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 586 | return getABIReturnInfo(RetTy, getTypes()).isStructRet(); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 589 | void CodeGenModule::ConstructAttributeList(const Decl *TargetDecl, |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 590 | ArgTypeIterator begin, |
| 591 | ArgTypeIterator end, |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 592 | AttributeListType &PAL) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 593 | unsigned FuncAttrs = 0; |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 594 | unsigned RetAttrs = 0; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 595 | |
| 596 | if (TargetDecl) { |
| 597 | if (TargetDecl->getAttr<NoThrowAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 598 | FuncAttrs |= llvm::Attribute::NoUnwind; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 599 | if (TargetDecl->getAttr<NoReturnAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 600 | FuncAttrs |= llvm::Attribute::NoReturn; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 601 | if (TargetDecl->getAttr<PureAttr>()) |
| 602 | FuncAttrs |= llvm::Attribute::ReadOnly; |
| 603 | if (TargetDecl->getAttr<ConstAttr>()) |
| 604 | FuncAttrs |= llvm::Attribute::ReadNone; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 607 | QualType RetTy = *begin; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 608 | unsigned Index = 1; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 609 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, getTypes()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 610 | switch (RetAI.getKind()) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 611 | case ABIArgInfo::Default: |
| 612 | if (RetTy->isPromotableIntegerType()) { |
| 613 | if (RetTy->isSignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 614 | RetAttrs |= llvm::Attribute::SExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 615 | } else if (RetTy->isUnsignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 616 | RetAttrs |= llvm::Attribute::ZExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | break; |
| 620 | |
| 621 | case ABIArgInfo::StructRet: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 622 | PAL.push_back(llvm::AttributeWithIndex::get(Index, |
| 623 | llvm::Attribute::StructRet| |
| 624 | llvm::Attribute::NoAlias)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 625 | ++Index; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 626 | break; |
| 627 | |
| 628 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 629 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 630 | |
| 631 | case ABIArgInfo::ByVal: |
| 632 | case ABIArgInfo::Expand: |
| 633 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 634 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 635 | |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 636 | if (RetAttrs) |
| 637 | PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 638 | for (++begin; begin != end; ++begin) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 639 | QualType ParamType = *begin; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 640 | unsigned Attributes = 0; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 641 | ABIArgInfo AI = getABIArgumentInfo(ParamType, getTypes()); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 642 | |
| 643 | switch (AI.getKind()) { |
| 644 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 645 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 646 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 647 | |
| 648 | case ABIArgInfo::ByVal: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 649 | Attributes |= llvm::Attribute::ByVal; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 650 | assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); |
| 651 | break; |
| 652 | |
| 653 | case ABIArgInfo::Default: |
| 654 | if (ParamType->isPromotableIntegerType()) { |
| 655 | if (ParamType->isSignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 656 | Attributes |= llvm::Attribute::SExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 657 | } else if (ParamType->isUnsignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 658 | Attributes |= llvm::Attribute::ZExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 659 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 660 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 661 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 662 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 663 | case ABIArgInfo::Expand: { |
| 664 | std::vector<const llvm::Type*> Tys; |
| 665 | // FIXME: This is rather inefficient. Do we ever actually need |
| 666 | // to do anything here? The result should be just reconstructed |
| 667 | // on the other side, so extension should be a non-issue. |
| 668 | getTypes().GetExpandedTypes(ParamType, Tys); |
| 669 | Index += Tys.size(); |
| 670 | continue; |
| 671 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 672 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 673 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 674 | if (Attributes) |
| 675 | PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 676 | ++Index; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 677 | } |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 678 | if (FuncAttrs) |
| 679 | PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); |
| 680 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 683 | void CodeGenFunction::EmitFunctionProlog(llvm::Function *Fn, |
| 684 | QualType RetTy, |
| 685 | const FunctionArgList &Args) { |
| 686 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 687 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 688 | |
| 689 | // Name the struct return argument. |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 690 | if (CGM.ReturnTypeUsesSret(RetTy)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 691 | AI->setName("agg.result"); |
| 692 | ++AI; |
| 693 | } |
| 694 | |
| 695 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 696 | i != e; ++i) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 697 | const VarDecl *Arg = i->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 698 | QualType Ty = i->second; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 699 | ABIArgInfo ArgI = getABIArgumentInfo(Ty, CGM.getTypes()); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 700 | |
| 701 | switch (ArgI.getKind()) { |
| 702 | case ABIArgInfo::ByVal: |
| 703 | case ABIArgInfo::Default: { |
| 704 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 705 | llvm::Value* V = AI; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 706 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 707 | // This must be a promotion, for something like |
| 708 | // "void a(x) short x; {..." |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 709 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 710 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 711 | EmitParmDecl(*Arg, V); |
| 712 | break; |
| 713 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 714 | |
| 715 | case ABIArgInfo::Expand: { |
| 716 | // If this was structure was expand into multiple arguments then |
| 717 | // we need to create a temporary and reconstruct it from the |
| 718 | // arguments. |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 719 | std::string Name = Arg->getNameAsString(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 720 | llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty), |
| 721 | (Name + ".addr").c_str()); |
| 722 | // FIXME: What are the right qualifiers here? |
| 723 | llvm::Function::arg_iterator End = |
| 724 | ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI); |
| 725 | EmitParmDecl(*Arg, Temp); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 726 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 727 | // Name the arguments used in expansion and increment AI. |
| 728 | unsigned Index = 0; |
| 729 | for (; AI != End; ++AI, ++Index) |
| 730 | AI->setName(Name + "." + llvm::utostr(Index)); |
| 731 | continue; |
| 732 | } |
| 733 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 734 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 735 | case ABIArgInfo::StructRet: |
| 736 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 737 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 738 | |
| 739 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 740 | } |
| 741 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 742 | } |
| 743 | |
| 744 | void CodeGenFunction::EmitFunctionEpilog(QualType RetTy, |
| 745 | llvm::Value *ReturnValue) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 746 | llvm::Value *RV = 0; |
| 747 | |
| 748 | // Functions with no result always return void. |
| 749 | if (ReturnValue) { |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 750 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 751 | |
| 752 | switch (RetAI.getKind()) { |
| 753 | case ABIArgInfo::StructRet: |
Anders Carlsson | 0b685a5 | 2008-11-15 01:29:05 +0000 | [diff] [blame] | 754 | if (RetTy->isAnyComplexType()) { |
| 755 | // FIXME: Volatile |
| 756 | ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); |
Anders Carlsson | 1385f8e | 2008-11-15 06:35:36 +0000 | [diff] [blame] | 757 | StoreComplexToAddr(RT, CurFn->arg_begin(), false); |
Anders Carlsson | 0b685a5 | 2008-11-15 01:29:05 +0000 | [diff] [blame] | 758 | } else |
| 759 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 760 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 761 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 762 | case ABIArgInfo::Default: |
| 763 | RV = Builder.CreateLoad(ReturnValue); |
| 764 | break; |
| 765 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 766 | case ABIArgInfo::Coerce: { |
| 767 | const llvm::Type *CoerceToPTy = |
| 768 | llvm::PointerType::getUnqual(RetAI.getCoerceToType()); |
| 769 | RV = Builder.CreateLoad(Builder.CreateBitCast(ReturnValue, CoerceToPTy)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 770 | break; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 771 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 772 | |
| 773 | case ABIArgInfo::ByVal: |
| 774 | case ABIArgInfo::Expand: |
| 775 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 778 | |
| 779 | if (RV) { |
| 780 | Builder.CreateRet(RV); |
| 781 | } else { |
| 782 | Builder.CreateRetVoid(); |
| 783 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | RValue CodeGenFunction::EmitCall(llvm::Value *Callee, |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 787 | QualType RetTy, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 788 | const CallArgList &CallArgs) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 789 | llvm::SmallVector<llvm::Value*, 16> Args; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 790 | |
| 791 | // Handle struct-return functions by passing a pointer to the |
| 792 | // location that we would like to return into. |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 793 | ABIArgInfo RetAI = getABIReturnInfo(RetTy, CGM.getTypes()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 794 | switch (RetAI.getKind()) { |
| 795 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 796 | // Create a temporary alloca to hold the result of the call. :( |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 797 | Args.push_back(CreateTempAlloca(ConvertType(RetTy))); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 798 | break; |
| 799 | |
| 800 | case ABIArgInfo::Default: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 801 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 802 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 803 | |
| 804 | case ABIArgInfo::ByVal: |
| 805 | case ABIArgInfo::Expand: |
| 806 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
| 810 | I != E; ++I) { |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 811 | ABIArgInfo ArgInfo = getABIArgumentInfo(I->second, CGM.getTypes()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 812 | RValue RV = I->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 813 | |
| 814 | switch (ArgInfo.getKind()) { |
| 815 | case ABIArgInfo::ByVal: // Default is byval |
| 816 | case ABIArgInfo::Default: |
| 817 | if (RV.isScalar()) { |
| 818 | Args.push_back(RV.getScalarVal()); |
| 819 | } else if (RV.isComplex()) { |
| 820 | // Make a temporary alloca to pass the argument. |
| 821 | Args.push_back(CreateTempAlloca(ConvertType(I->second))); |
| 822 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
| 823 | } else { |
| 824 | Args.push_back(RV.getAggregateAddr()); |
| 825 | } |
| 826 | break; |
| 827 | |
| 828 | case ABIArgInfo::StructRet: |
| 829 | case ABIArgInfo::Coerce: |
| 830 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 831 | break; |
| 832 | |
| 833 | case ABIArgInfo::Expand: |
| 834 | ExpandTypeToArgs(I->second, RV, Args); |
| 835 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
| 839 | llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size()); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 840 | CGCallInfo CallInfo(RetTy, CallArgs); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 841 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 842 | // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set. |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 843 | CodeGen::AttributeListType AttributeList; |
| 844 | CGM.ConstructAttributeList(0, |
Daniel Dunbar | b768807 | 2008-09-10 00:41:16 +0000 | [diff] [blame] | 845 | CallInfo.argtypes_begin(), CallInfo.argtypes_end(), |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 846 | AttributeList); |
| 847 | CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
| 848 | AttributeList.size())); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 849 | |
| 850 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 851 | CI->setCallingConv(F->getCallingConv()); |
| 852 | if (CI->getType() != llvm::Type::VoidTy) |
| 853 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 854 | |
| 855 | switch (RetAI.getKind()) { |
| 856 | case ABIArgInfo::StructRet: |
| 857 | if (RetTy->isAnyComplexType()) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 858 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 859 | else |
| 860 | // Struct return. |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 861 | return RValue::getAggregate(Args[0]); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 862 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 863 | case ABIArgInfo::Default: |
| 864 | return RValue::get(RetTy->isVoidType() ? 0 : CI); |
| 865 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 866 | case ABIArgInfo::Coerce: { |
| 867 | const llvm::Type *CoerceToPTy = |
| 868 | llvm::PointerType::getUnqual(RetAI.getCoerceToType()); |
| 869 | llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "tmp"); |
| 870 | Builder.CreateStore(CI, Builder.CreateBitCast(V, CoerceToPTy)); |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 871 | if (RetTy->isAnyComplexType()) |
| 872 | return RValue::getComplex(LoadComplexFromAddr(V, false)); |
| 873 | else |
| 874 | return RValue::getAggregate(V); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 875 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 876 | |
| 877 | case ABIArgInfo::ByVal: |
| 878 | case ABIArgInfo::Expand: |
| 879 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 880 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 881 | |
| 882 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 883 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 884 | } |