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