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