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