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 | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MathExtras.h" |
Daniel Dunbar | 6f7279b | 2009-02-04 23:24:38 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 29 | |
| 30 | #include "ABIInfo.h" |
| 31 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | using namespace CodeGen; |
| 34 | |
| 35 | /***/ |
| 36 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 37 | // FIXME: Use iterator and sidestep silly type array creation. |
| 38 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 39 | const |
| 40 | CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) { |
| 41 | return getFunctionInfo(FTNP->getResultType(), |
| 42 | llvm::SmallVector<QualType, 16>()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 45 | const |
| 46 | CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) { |
| 47 | llvm::SmallVector<QualType, 16> ArgTys; |
| 48 | // FIXME: Kill copy. |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 49 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 50 | ArgTys.push_back(FTP->getArgType(i)); |
| 51 | return getFunctionInfo(FTP->getResultType(), ArgTys); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 54 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) { |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 55 | const FunctionType *FTy = FD->getType()->getAsFunctionType(); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 56 | if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy)) |
| 57 | return getFunctionInfo(FTP); |
| 58 | return getFunctionInfo(cast<FunctionTypeNoProto>(FTy)); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 61 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) { |
| 62 | llvm::SmallVector<QualType, 16> ArgTys; |
| 63 | ArgTys.push_back(MD->getSelfDecl()->getType()); |
| 64 | ArgTys.push_back(Context.getObjCSelType()); |
| 65 | // FIXME: Kill copy? |
Chris Lattner | 2073216 | 2009-02-20 06:23:21 +0000 | [diff] [blame] | 66 | for (ObjCMethodDecl::param_iterator i = MD->param_begin(), |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 67 | e = MD->param_end(); i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 68 | ArgTys.push_back((*i)->getType()); |
| 69 | return getFunctionInfo(MD->getResultType(), ArgTys); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 72 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 73 | const CallArgList &Args) { |
| 74 | // FIXME: Kill copy. |
| 75 | llvm::SmallVector<QualType, 16> ArgTys; |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 76 | for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 77 | i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 78 | ArgTys.push_back(i->second); |
| 79 | return getFunctionInfo(ResTy, ArgTys); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 82 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 83 | const FunctionArgList &Args) { |
| 84 | // FIXME: Kill copy. |
| 85 | llvm::SmallVector<QualType, 16> ArgTys; |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 86 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 87 | i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 88 | ArgTys.push_back(i->second); |
| 89 | return getFunctionInfo(ResTy, ArgTys); |
| 90 | } |
| 91 | |
| 92 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 93 | const llvm::SmallVector<QualType, 16> &ArgTys) { |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 94 | // Lookup or create unique function info. |
| 95 | llvm::FoldingSetNodeID ID; |
| 96 | CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end()); |
| 97 | |
| 98 | void *InsertPos = 0; |
| 99 | CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos); |
| 100 | if (FI) |
| 101 | return *FI; |
| 102 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 103 | // Construct the function info. |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 104 | FI = new CGFunctionInfo(ResTy, ArgTys); |
Daniel Dunbar | 35e67d4 | 2009-02-05 00:00:23 +0000 | [diff] [blame] | 105 | FunctionInfos.InsertNode(FI, InsertPos); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 106 | |
| 107 | // Compute ABI information. |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 108 | getABIInfo().computeInfo(*FI, getContext()); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 109 | |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 110 | return *FI; |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /***/ |
| 114 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 115 | ABIInfo::~ABIInfo() {} |
| 116 | |
Daniel Dunbar | 6f7279b | 2009-02-04 23:24:38 +0000 | [diff] [blame] | 117 | void ABIArgInfo::dump() const { |
| 118 | fprintf(stderr, "(ABIArgInfo Kind="); |
| 119 | switch (TheKind) { |
| 120 | case Direct: |
| 121 | fprintf(stderr, "Direct"); |
| 122 | break; |
Daniel Dunbar | 6f7279b | 2009-02-04 23:24:38 +0000 | [diff] [blame] | 123 | case Ignore: |
| 124 | fprintf(stderr, "Ignore"); |
| 125 | break; |
| 126 | case Coerce: |
| 127 | fprintf(stderr, "Coerce Type="); |
| 128 | getCoerceToType()->print(llvm::errs()); |
| 129 | // FIXME: This is ridiculous. |
| 130 | llvm::errs().flush(); |
| 131 | break; |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 132 | case Indirect: |
| 133 | fprintf(stderr, "Indirect Align=%d", getIndirectAlign()); |
Daniel Dunbar | 6f7279b | 2009-02-04 23:24:38 +0000 | [diff] [blame] | 134 | break; |
| 135 | case Expand: |
| 136 | fprintf(stderr, "Expand"); |
| 137 | break; |
| 138 | } |
| 139 | fprintf(stderr, ")\n"); |
| 140 | } |
| 141 | |
| 142 | /***/ |
| 143 | |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 144 | /// isEmptyStruct - Return true iff a structure has no non-empty |
| 145 | /// members. Note that a structure with a flexible array member is not |
| 146 | /// considered empty. |
| 147 | static bool isEmptyStruct(QualType T) { |
| 148 | const RecordType *RT = T->getAsStructureType(); |
| 149 | if (!RT) |
| 150 | return 0; |
| 151 | const RecordDecl *RD = RT->getDecl(); |
| 152 | if (RD->hasFlexibleArrayMember()) |
| 153 | return false; |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 154 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 155 | e = RD->field_end(); i != e; ++i) { |
| 156 | const FieldDecl *FD = *i; |
| 157 | if (!isEmptyStruct(FD->getType())) |
| 158 | return false; |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | /// isSingleElementStruct - Determine if a structure is a "single |
| 164 | /// element struct", i.e. it has exactly one non-empty field or |
| 165 | /// exactly one field which is itself a single element |
| 166 | /// struct. Structures with flexible array members are never |
| 167 | /// considered single element structs. |
| 168 | /// |
| 169 | /// \return The field declaration for the single non-empty field, if |
| 170 | /// it exists. |
| 171 | static const FieldDecl *isSingleElementStruct(QualType T) { |
| 172 | const RecordType *RT = T->getAsStructureType(); |
| 173 | if (!RT) |
| 174 | return 0; |
| 175 | |
| 176 | const RecordDecl *RD = RT->getDecl(); |
| 177 | if (RD->hasFlexibleArrayMember()) |
| 178 | return 0; |
| 179 | |
| 180 | const FieldDecl *Found = 0; |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 181 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 182 | e = RD->field_end(); i != e; ++i) { |
| 183 | const FieldDecl *FD = *i; |
| 184 | QualType FT = FD->getType(); |
| 185 | |
| 186 | if (isEmptyStruct(FT)) { |
| 187 | // Ignore |
| 188 | } else if (Found) { |
| 189 | return 0; |
| 190 | } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 191 | Found = FD; |
| 192 | } else { |
| 193 | Found = isSingleElementStruct(FT); |
| 194 | if (!Found) |
| 195 | return 0; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return Found; |
| 200 | } |
| 201 | |
| 202 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 203 | if (!Ty->getAsBuiltinType() && !Ty->isPointerType()) |
| 204 | return false; |
| 205 | |
| 206 | uint64_t Size = Context.getTypeSize(Ty); |
| 207 | return Size == 32 || Size == 64; |
| 208 | } |
| 209 | |
| 210 | static bool areAllFields32Or64BitBasicType(const RecordDecl *RD, |
| 211 | ASTContext &Context) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 212 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 213 | e = RD->field_end(); i != e; ++i) { |
| 214 | const FieldDecl *FD = *i; |
| 215 | |
| 216 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 217 | return false; |
| 218 | |
| 219 | // If this is a bit-field we need to make sure it is still a |
| 220 | // 32-bit or 64-bit type. |
| 221 | if (Expr *BW = FD->getBitWidth()) { |
| 222 | unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue(); |
| 223 | if (Width <= 16) |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | return true; |
| 228 | } |
| 229 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 230 | namespace { |
| 231 | /// DefaultABIInfo - The default implementation for ABI specific |
| 232 | /// details. This implementation provides information which results in |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 233 | /// self-consistent and sensible LLVM IR generation, but does not |
| 234 | /// conform to any particular ABI. |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 235 | class DefaultABIInfo : public ABIInfo { |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 236 | ABIArgInfo classifyReturnType(QualType RetTy, |
| 237 | ASTContext &Context) const; |
| 238 | |
| 239 | ABIArgInfo classifyArgumentType(QualType RetTy, |
| 240 | ASTContext &Context) const; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 241 | |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 242 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 243 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 244 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 245 | it != ie; ++it) |
| 246 | it->info = classifyArgumentType(it->type, Context); |
| 247 | } |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 248 | |
| 249 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 250 | CodeGenFunction &CGF) const; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 254 | class X86_32ABIInfo : public ABIInfo { |
| 255 | public: |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 256 | ABIArgInfo classifyReturnType(QualType RetTy, |
| 257 | ASTContext &Context) const; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 258 | |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 259 | ABIArgInfo classifyArgumentType(QualType RetTy, |
| 260 | ASTContext &Context) const; |
| 261 | |
| 262 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 263 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 264 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 265 | it != ie; ++it) |
| 266 | it->info = classifyArgumentType(it->type, Context); |
| 267 | } |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 268 | |
| 269 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 270 | CodeGenFunction &CGF) const; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 271 | }; |
| 272 | } |
| 273 | |
| 274 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 275 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 276 | if (RetTy->isVoidType()) { |
| 277 | return ABIArgInfo::getIgnore(); |
| 278 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 279 | // Classify "single element" structs as their element type. |
| 280 | const FieldDecl *SeltFD = isSingleElementStruct(RetTy); |
| 281 | if (SeltFD) { |
| 282 | QualType SeltTy = SeltFD->getType()->getDesugaredType(); |
| 283 | if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) { |
| 284 | // FIXME: This is gross, it would be nice if we could just |
| 285 | // pass back SeltTy and have clients deal with it. Is it worth |
| 286 | // supporting coerce to both LLVM and clang Types? |
| 287 | if (BT->isIntegerType()) { |
| 288 | uint64_t Size = Context.getTypeSize(SeltTy); |
| 289 | return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size)); |
| 290 | } else if (BT->getKind() == BuiltinType::Float) { |
| 291 | return ABIArgInfo::getCoerce(llvm::Type::FloatTy); |
| 292 | } else if (BT->getKind() == BuiltinType::Double) { |
| 293 | return ABIArgInfo::getCoerce(llvm::Type::DoubleTy); |
| 294 | } |
| 295 | } else if (SeltTy->isPointerType()) { |
| 296 | // FIXME: It would be really nice if this could come out as |
| 297 | // the proper pointer type. |
| 298 | llvm::Type *PtrTy = |
| 299 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 300 | return ABIArgInfo::getCoerce(PtrTy); |
| 301 | } |
| 302 | } |
| 303 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 304 | uint64_t Size = Context.getTypeSize(RetTy); |
| 305 | if (Size == 8) { |
| 306 | return ABIArgInfo::getCoerce(llvm::Type::Int8Ty); |
| 307 | } else if (Size == 16) { |
| 308 | return ABIArgInfo::getCoerce(llvm::Type::Int16Ty); |
| 309 | } else if (Size == 32) { |
| 310 | return ABIArgInfo::getCoerce(llvm::Type::Int32Ty); |
| 311 | } else if (Size == 64) { |
| 312 | return ABIArgInfo::getCoerce(llvm::Type::Int64Ty); |
| 313 | } else { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 314 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 315 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 316 | } else { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 317 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 321 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 322 | ASTContext &Context) const { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 323 | // FIXME: Set alignment on indirect arguments. |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 324 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 325 | // Structures with flexible arrays are always indirect. |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 326 | if (const RecordType *RT = Ty->getAsStructureType()) |
| 327 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 328 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 329 | |
Daniel Dunbar | 3170c93 | 2009-02-05 01:50:07 +0000 | [diff] [blame] | 330 | // Ignore empty structs. |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 331 | uint64_t Size = Context.getTypeSize(Ty); |
| 332 | if (Ty->isStructureType() && Size == 0) |
Daniel Dunbar | 3170c93 | 2009-02-05 01:50:07 +0000 | [diff] [blame] | 333 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 334 | |
| 335 | // Expand structs with size <= 128-bits which consist only of |
| 336 | // basic types (int, long long, float, double, xxx*). This is |
| 337 | // non-recursive and does not ignore empty fields. |
| 338 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 339 | if (Context.getTypeSize(Ty) <= 4*32 && |
| 340 | areAllFields32Or64BitBasicType(RT->getDecl(), Context)) |
| 341 | return ABIArgInfo::getExpand(); |
| 342 | } |
| 343 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 344 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 345 | } else { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 346 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 350 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 351 | CodeGenFunction &CGF) const { |
| 352 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 353 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 354 | |
| 355 | CGBuilderTy &Builder = CGF.Builder; |
| 356 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 357 | "ap"); |
| 358 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 359 | llvm::Type *PTy = |
| 360 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 361 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 362 | |
Daniel Dunbar | 570f0cf | 2009-02-18 22:28:45 +0000 | [diff] [blame] | 363 | uint64_t Offset = |
| 364 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 365 | llvm::Value *NextAddr = |
| 366 | Builder.CreateGEP(Addr, |
Daniel Dunbar | 570f0cf | 2009-02-18 22:28:45 +0000 | [diff] [blame] | 367 | llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset), |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 368 | "ap.next"); |
| 369 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 370 | |
| 371 | return AddrTyped; |
| 372 | } |
| 373 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 374 | namespace { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 375 | /// X86_64ABIInfo - The X86_64 ABI information. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 376 | class X86_64ABIInfo : public ABIInfo { |
| 377 | enum Class { |
| 378 | Integer = 0, |
| 379 | SSE, |
| 380 | SSEUp, |
| 381 | X87, |
| 382 | X87Up, |
| 383 | ComplexX87, |
| 384 | NoClass, |
| 385 | Memory |
| 386 | }; |
| 387 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 388 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 389 | /// |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 390 | /// Merge an accumulating classification \arg Accum with a field |
| 391 | /// classification \arg Field. |
| 392 | /// |
| 393 | /// \param Accum - The accumulating classification. This should |
| 394 | /// always be either NoClass or the result of a previous merge |
| 395 | /// call. In addition, this should never be Memory (the caller |
| 396 | /// should just return Memory for the aggregate). |
| 397 | Class merge(Class Accum, Class Field) const; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 398 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 399 | /// classify - Determine the x86_64 register classes in which the |
| 400 | /// given type T should be passed. |
| 401 | /// |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 402 | /// \param Lo - The classification for the parts of the type |
| 403 | /// residing in the low word of the containing object. |
| 404 | /// |
| 405 | /// \param Hi - The classification for the parts of the type |
| 406 | /// residing in the high word of the containing object. |
| 407 | /// |
| 408 | /// \param OffsetBase - The bit offset of this type in the |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 409 | /// containing object. Some parameters are classified different |
| 410 | /// depending on whether they straddle an eightbyte boundary. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 411 | /// |
| 412 | /// If a word is unused its result will be NoClass; if a type should |
| 413 | /// be passed in Memory then at least the classification of \arg Lo |
| 414 | /// will be Memory. |
| 415 | /// |
| 416 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
| 417 | /// |
| 418 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
Daniel Dunbar | 6e53e9b | 2009-02-17 07:55:55 +0000 | [diff] [blame] | 419 | /// also be ComplexX87. |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 420 | void classify(QualType T, ASTContext &Context, uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 421 | Class &Lo, Class &Hi) const; |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 422 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 423 | /// getCoerceResult - Given a source type \arg Ty and an LLVM type |
| 424 | /// to coerce to, chose the best way to pass Ty in the same place |
| 425 | /// that \arg CoerceTo would be passed, but while keeping the |
| 426 | /// emitted code as simple as possible. |
| 427 | /// |
| 428 | /// FIXME: Note, this should be cleaned up to just take an |
| 429 | /// enumeration of all the ways we might want to pass things, |
| 430 | /// instead of constructing an LLVM type. This makes this code more |
| 431 | /// explicit, and it makes it clearer that we are also doing this |
| 432 | /// for correctness in the case of passing scalar types. |
| 433 | ABIArgInfo getCoerceResult(QualType Ty, |
| 434 | const llvm::Type *CoerceTo, |
| 435 | ASTContext &Context) const; |
| 436 | |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 437 | ABIArgInfo classifyReturnType(QualType RetTy, |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 438 | ASTContext &Context) const; |
| 439 | |
| 440 | ABIArgInfo classifyArgumentType(QualType Ty, |
| 441 | ASTContext &Context, |
Daniel Dunbar | 3b4e9cd | 2009-02-10 17:06:09 +0000 | [diff] [blame] | 442 | unsigned &neededInt, |
| 443 | unsigned &neededSSE) const; |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 444 | |
| 445 | public: |
| 446 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const; |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 447 | |
| 448 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 449 | CodeGenFunction &CGF) const; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 450 | }; |
| 451 | } |
| 452 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 453 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, |
| 454 | Class Field) const { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 455 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 456 | // classified recursively so that always two fields are |
| 457 | // considered. The resulting class is calculated according to |
| 458 | // the classes of the fields in the eightbyte: |
| 459 | // |
| 460 | // (a) If both classes are equal, this is the resulting class. |
| 461 | // |
| 462 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 463 | // the other class. |
| 464 | // |
| 465 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 466 | // class. |
| 467 | // |
| 468 | // (d) If one of the classes is INTEGER, the result is the |
| 469 | // INTEGER. |
| 470 | // |
| 471 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 472 | // MEMORY is used as class. |
| 473 | // |
| 474 | // (f) Otherwise class SSE is used. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 475 | assert((Accum == NoClass || Accum == Integer || |
| 476 | Accum == SSE || Accum == SSEUp) && |
| 477 | "Invalid accumulated classification during merge."); |
| 478 | if (Accum == Field || Field == NoClass) |
| 479 | return Accum; |
| 480 | else if (Field == Memory) |
| 481 | return Memory; |
| 482 | else if (Accum == NoClass) |
| 483 | return Field; |
| 484 | else if (Accum == Integer || Field == Integer) |
| 485 | return Integer; |
| 486 | else if (Field == X87 || Field == X87Up || Field == ComplexX87) |
| 487 | return Memory; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 488 | else |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 489 | return SSE; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 492 | void X86_64ABIInfo::classify(QualType Ty, |
| 493 | ASTContext &Context, |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 494 | uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 495 | Class &Lo, Class &Hi) const { |
Daniel Dunbar | 9a82b52 | 2009-02-02 18:06:39 +0000 | [diff] [blame] | 496 | // FIXME: This code can be simplified by introducing a simple value |
| 497 | // class for Class pairs with appropriate constructor methods for |
| 498 | // the various situations. |
| 499 | |
Daniel Dunbar | e28099b | 2009-02-22 04:48:22 +0000 | [diff] [blame] | 500 | // FIXME: Some of the split computations are wrong; unaligned |
| 501 | // vectors shouldn't be passed in registers for example, so there is |
| 502 | // no chance they can straddle an eightbyte. Verify & simplify. |
| 503 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 504 | Lo = Hi = NoClass; |
| 505 | |
| 506 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 507 | Current = Memory; |
| 508 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 509 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) { |
| 510 | BuiltinType::Kind k = BT->getKind(); |
| 511 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 512 | if (k == BuiltinType::Void) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 513 | Current = NoClass; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 514 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 515 | Current = Integer; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 516 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 517 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 518 | } else if (k == BuiltinType::LongDouble) { |
| 519 | Lo = X87; |
| 520 | Hi = X87Up; |
| 521 | } |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 522 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 523 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 524 | // FIXME: __int128 is (Integer, Integer). |
| 525 | } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() || |
| 526 | Ty->isObjCQualifiedInterfaceType()) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 527 | Current = Integer; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 528 | } else if (const VectorType *VT = Ty->getAsVectorType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 529 | uint64_t Size = Context.getTypeSize(VT); |
Daniel Dunbar | e28099b | 2009-02-22 04:48:22 +0000 | [diff] [blame] | 530 | if (Size == 32) { |
| 531 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 532 | // float> as integer. |
| 533 | Current = Integer; |
| 534 | |
| 535 | // If this type crosses an eightbyte boundary, it should be |
| 536 | // split. |
| 537 | uint64_t EB_Real = (OffsetBase) / 64; |
| 538 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 539 | if (EB_Real != EB_Imag) |
| 540 | Hi = Lo; |
| 541 | } else if (Size == 64) { |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 542 | // gcc passes <1 x double> in memory. :( |
| 543 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 544 | return; |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 545 | |
| 546 | // gcc passes <1 x long long> as INTEGER. |
| 547 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 548 | Current = Integer; |
| 549 | else |
| 550 | Current = SSE; |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 551 | |
| 552 | // If this type crosses an eightbyte boundary, it should be |
| 553 | // split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 554 | if (OffsetBase && OffsetBase != 64) |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 555 | Hi = Lo; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 556 | } else if (Size == 128) { |
| 557 | Lo = SSE; |
| 558 | Hi = SSEUp; |
| 559 | } |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 560 | } else if (const ComplexType *CT = Ty->getAsComplexType()) { |
Daniel Dunbar | 3327f6e | 2009-02-14 02:45:45 +0000 | [diff] [blame] | 561 | QualType ET = Context.getCanonicalType(CT->getElementType()); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 562 | |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 563 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 564 | if (ET->isIntegralType()) { |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 565 | if (Size <= 64) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 566 | Current = Integer; |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 567 | else if (Size <= 128) |
| 568 | Lo = Hi = Integer; |
| 569 | } else if (ET == Context.FloatTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 570 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 571 | else if (ET == Context.DoubleTy) |
| 572 | Lo = Hi = SSE; |
| 573 | else if (ET == Context.LongDoubleTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 574 | Current = ComplexX87; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 575 | |
| 576 | // If this complex type crosses an eightbyte boundary then it |
| 577 | // should be split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 578 | uint64_t EB_Real = (OffsetBase) / 64; |
| 579 | uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 580 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 581 | Hi = Lo; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 582 | } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 583 | // Arrays are treated like structures. |
| 584 | |
| 585 | uint64_t Size = Context.getTypeSize(Ty); |
| 586 | |
| 587 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 588 | // than two eightbytes, ..., it has class MEMORY. |
| 589 | if (Size > 128) |
| 590 | return; |
| 591 | |
| 592 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 593 | // fields, it has class MEMORY. |
| 594 | // |
| 595 | // Only need to check alignment of array base. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 596 | if (OffsetBase % Context.getTypeAlign(AT->getElementType())) |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 597 | return; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 598 | |
| 599 | // Otherwise implement simplified merge. We could be smarter about |
| 600 | // 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] | 601 | Current = NoClass; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 602 | uint64_t EltSize = Context.getTypeSize(AT->getElementType()); |
| 603 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
| 604 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 605 | Class FieldLo, FieldHi; |
| 606 | classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 607 | Lo = merge(Lo, FieldLo); |
| 608 | Hi = merge(Hi, FieldHi); |
| 609 | if (Lo == Memory || Hi == Memory) |
| 610 | break; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 611 | } |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 612 | |
| 613 | // Do post merger cleanup (see below). Only case we worry about is Memory. |
| 614 | if (Hi == Memory) |
| 615 | Lo = Memory; |
| 616 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 617 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 618 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 619 | |
| 620 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 621 | // than two eightbytes, ..., it has class MEMORY. |
| 622 | if (Size > 128) |
| 623 | return; |
| 624 | |
| 625 | const RecordDecl *RD = RT->getDecl(); |
| 626 | |
| 627 | // Assume variable sized types are passed in memory. |
| 628 | if (RD->hasFlexibleArrayMember()) |
| 629 | return; |
| 630 | |
| 631 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 632 | |
| 633 | // Reset Lo class, this will be recomputed. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 634 | Current = NoClass; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 635 | unsigned idx = 0; |
| 636 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 637 | e = RD->field_end(); i != e; ++i, ++idx) { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 638 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Daniel Dunbar | dd81d44 | 2009-02-17 02:45:44 +0000 | [diff] [blame] | 639 | bool BitField = i->isBitField(); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 640 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 641 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 642 | // fields, it has class MEMORY. |
Daniel Dunbar | dd81d44 | 2009-02-17 02:45:44 +0000 | [diff] [blame] | 643 | // |
| 644 | // Note, skip this test for bitfields, see below. |
| 645 | if (!BitField && Offset % Context.getTypeAlign(i->getType())) { |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 646 | Lo = Memory; |
| 647 | return; |
| 648 | } |
| 649 | |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 650 | // Classify this field. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 651 | // |
| 652 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 653 | // exceeds a single eightbyte, each is classified |
| 654 | // separately. Each eightbyte gets initialized to class |
| 655 | // NO_CLASS. |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 656 | Class FieldLo, FieldHi; |
Daniel Dunbar | dd81d44 | 2009-02-17 02:45:44 +0000 | [diff] [blame] | 657 | |
| 658 | // Bitfields require special handling, they do not force the |
| 659 | // structure to be passed in memory even if unaligned, and |
| 660 | // therefore they can straddle an eightbyte. |
| 661 | if (BitField) { |
| 662 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 663 | uint64_t Size = |
| 664 | i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue(); |
| 665 | |
| 666 | uint64_t EB_Lo = Offset / 64; |
| 667 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
| 668 | FieldLo = FieldHi = NoClass; |
| 669 | if (EB_Lo) { |
| 670 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 671 | FieldLo = NoClass; |
| 672 | FieldHi = Integer; |
| 673 | } else { |
| 674 | FieldLo = Integer; |
| 675 | FieldHi = EB_Hi ? Integer : NoClass; |
| 676 | } |
| 677 | } else |
| 678 | classify(i->getType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 679 | Lo = merge(Lo, FieldLo); |
| 680 | Hi = merge(Hi, FieldHi); |
| 681 | if (Lo == Memory || Hi == Memory) |
| 682 | break; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 686 | // |
| 687 | // (a) If one of the classes is MEMORY, the whole argument is |
| 688 | // passed in memory. |
| 689 | // |
| 690 | // (b) If SSEUP is not preceeded by SSE, it is converted to SSE. |
| 691 | |
| 692 | // The first of these conditions is guaranteed by how we implement |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 693 | // the merge (just bail). |
| 694 | // |
| 695 | // The second condition occurs in the case of unions; for example |
| 696 | // union { _Complex double; unsigned; }. |
| 697 | if (Hi == Memory) |
| 698 | Lo = Memory; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 699 | if (Hi == SSEUp && Lo != SSE) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 700 | Hi = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 704 | ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty, |
| 705 | const llvm::Type *CoerceTo, |
| 706 | ASTContext &Context) const { |
| 707 | if (CoerceTo == llvm::Type::Int64Ty) { |
| 708 | // Integer and pointer types will end up in a general purpose |
| 709 | // register. |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 710 | if (Ty->isIntegralType() || Ty->isPointerType()) |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 711 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 712 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 713 | } else if (CoerceTo == llvm::Type::DoubleTy) { |
Daniel Dunbar | 3327f6e | 2009-02-14 02:45:45 +0000 | [diff] [blame] | 714 | // FIXME: It would probably be better to make CGFunctionInfo only |
| 715 | // map using canonical types than to canonize here. |
| 716 | QualType CTy = Context.getCanonicalType(Ty); |
| 717 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 718 | // Float and double end up in a single SSE reg. |
Daniel Dunbar | 3327f6e | 2009-02-14 02:45:45 +0000 | [diff] [blame] | 719 | if (CTy == Context.FloatTy || CTy == Context.DoubleTy) |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 720 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 0af9929 | 2009-02-22 04:16:10 +0000 | [diff] [blame] | 721 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | return ABIArgInfo::getCoerce(CoerceTo); |
| 725 | } |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 726 | |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 727 | ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy, |
| 728 | ASTContext &Context) const { |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 729 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 730 | // classification algorithm. |
| 731 | X86_64ABIInfo::Class Lo, Hi; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 732 | classify(RetTy, Context, 0, Lo, Hi); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 733 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 734 | // Check some invariants. |
| 735 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| 736 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); |
| 737 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 738 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 739 | const llvm::Type *ResType = 0; |
| 740 | switch (Lo) { |
| 741 | case NoClass: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 742 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 743 | |
| 744 | case SSEUp: |
| 745 | case X87Up: |
| 746 | assert(0 && "Invalid classification for lo word."); |
| 747 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 748 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 749 | // hidden argument. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 750 | case Memory: |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 751 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 752 | |
| 753 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 754 | // available register of the sequence %rax, %rdx is used. |
| 755 | case Integer: |
| 756 | ResType = llvm::Type::Int64Ty; break; |
| 757 | |
| 758 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 759 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 760 | case SSE: |
| 761 | ResType = llvm::Type::DoubleTy; break; |
| 762 | |
| 763 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 764 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 765 | case X87: |
| 766 | ResType = llvm::Type::X86_FP80Ty; break; |
| 767 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 768 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 769 | // part of the value is returned in %st0 and the imaginary part in |
| 770 | // %st1. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 771 | case ComplexX87: |
Daniel Dunbar | 6e53e9b | 2009-02-17 07:55:55 +0000 | [diff] [blame] | 772 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Daniel Dunbar | 3e030b4 | 2009-02-18 03:44:19 +0000 | [diff] [blame] | 773 | ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty, |
| 774 | llvm::Type::X86_FP80Ty, |
| 775 | NULL); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 776 | break; |
| 777 | } |
| 778 | |
| 779 | switch (Hi) { |
Daniel Dunbar | 6e53e9b | 2009-02-17 07:55:55 +0000 | [diff] [blame] | 780 | // Memory was handled previously and X87 should |
| 781 | // never occur as a hi class. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 782 | case Memory: |
| 783 | case X87: |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 784 | assert(0 && "Invalid classification for hi word."); |
| 785 | |
Daniel Dunbar | 6e53e9b | 2009-02-17 07:55:55 +0000 | [diff] [blame] | 786 | case ComplexX87: // Previously handled. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 787 | case NoClass: break; |
Daniel Dunbar | 6e53e9b | 2009-02-17 07:55:55 +0000 | [diff] [blame] | 788 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 789 | case Integer: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 790 | ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL); |
| 791 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 792 | case SSE: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 793 | ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL); |
| 794 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 795 | |
| 796 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
| 797 | // is passed in the upper half of the last used SSE register. |
| 798 | // |
| 799 | // SSEUP should always be preceeded by SSE, just widen. |
| 800 | case SSEUp: |
| 801 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
| 802 | ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2); |
| 803 | break; |
| 804 | |
| 805 | // 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] | 806 | // returned together with the previous X87 value in %st0. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 807 | // |
| 808 | // X87UP should always be preceeded by X87, so we don't need to do |
| 809 | // anything here. |
| 810 | case X87Up: |
| 811 | assert(Lo == X87 && "Unexpected X87Up classification."); |
| 812 | break; |
| 813 | } |
| 814 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 815 | return getCoerceResult(RetTy, ResType, Context); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 818 | ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 3b4e9cd | 2009-02-10 17:06:09 +0000 | [diff] [blame] | 819 | unsigned &neededInt, |
| 820 | unsigned &neededSSE) const { |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 821 | X86_64ABIInfo::Class Lo, Hi; |
| 822 | classify(Ty, Context, 0, Lo, Hi); |
| 823 | |
| 824 | // Check some invariants. |
| 825 | // FIXME: Enforce these by construction. |
| 826 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| 827 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); |
| 828 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 829 | |
Daniel Dunbar | 3b4e9cd | 2009-02-10 17:06:09 +0000 | [diff] [blame] | 830 | neededInt = 0; |
| 831 | neededSSE = 0; |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 832 | const llvm::Type *ResType = 0; |
| 833 | switch (Lo) { |
| 834 | case NoClass: |
| 835 | return ABIArgInfo::getIgnore(); |
| 836 | |
| 837 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 838 | // on the stack. |
| 839 | case Memory: |
| 840 | |
| 841 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 842 | // COMPLEX_X87, it is passed in memory. |
| 843 | case X87: |
| 844 | case ComplexX87: |
Daniel Dunbar | 245f553 | 2009-02-22 08:17:51 +0000 | [diff] [blame] | 845 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 846 | |
| 847 | case SSEUp: |
| 848 | case X87Up: |
| 849 | assert(0 && "Invalid classification for lo word."); |
| 850 | |
| 851 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 852 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 853 | // and %r9 is used. |
| 854 | case Integer: |
| 855 | ++neededInt; |
| 856 | ResType = llvm::Type::Int64Ty; |
| 857 | break; |
| 858 | |
| 859 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 860 | // available SSE register is used, the registers are taken in the |
| 861 | // order from %xmm0 to %xmm7. |
| 862 | case SSE: |
| 863 | ++neededSSE; |
| 864 | ResType = llvm::Type::DoubleTy; |
| 865 | break; |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 866 | } |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 867 | |
| 868 | switch (Hi) { |
| 869 | // Memory was handled previously, ComplexX87 and X87 should |
| 870 | // never occur as hi classes, and X87Up must be preceed by X87, |
| 871 | // which is passed in memory. |
| 872 | case Memory: |
| 873 | case X87: |
| 874 | case X87Up: |
| 875 | case ComplexX87: |
| 876 | assert(0 && "Invalid classification for hi word."); |
| 877 | |
| 878 | case NoClass: break; |
| 879 | case Integer: |
| 880 | ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL); |
| 881 | ++neededInt; |
| 882 | break; |
| 883 | case SSE: |
| 884 | ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL); |
| 885 | ++neededSSE; |
| 886 | break; |
| 887 | |
| 888 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 889 | // eightbyte is passed in the upper half of the last used SSE |
| 890 | // register. |
| 891 | case SSEUp: |
| 892 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
| 893 | ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2); |
| 894 | break; |
| 895 | } |
| 896 | |
Daniel Dunbar | 644f4c3 | 2009-02-14 02:09:24 +0000 | [diff] [blame] | 897 | return getCoerceResult(Ty, ResType, Context); |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 901 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 902 | |
| 903 | // Keep track of the number of assigned registers. |
| 904 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
| 905 | |
| 906 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 907 | // get assigned (in left-to-right order) for passing as follows... |
| 908 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Daniel Dunbar | 3b4e9cd | 2009-02-10 17:06:09 +0000 | [diff] [blame] | 909 | it != ie; ++it) { |
| 910 | unsigned neededInt, neededSSE; |
| 911 | it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE); |
| 912 | |
| 913 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 914 | // eightbyte of an argument, the whole argument is passed on the |
| 915 | // stack. If registers have already been assigned for some |
| 916 | // eightbytes of such an argument, the assignments get reverted. |
| 917 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
| 918 | freeIntRegs -= neededInt; |
| 919 | freeSSERegs -= neededSSE; |
| 920 | } else { |
Daniel Dunbar | 245f553 | 2009-02-22 08:17:51 +0000 | [diff] [blame] | 921 | it->info = ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 3b4e9cd | 2009-02-10 17:06:09 +0000 | [diff] [blame] | 922 | } |
| 923 | } |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 926 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 927 | QualType Ty, |
| 928 | CodeGenFunction &CGF) { |
| 929 | llvm::Value *overflow_arg_area_p = |
| 930 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 931 | llvm::Value *overflow_arg_area = |
| 932 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 933 | |
| 934 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 935 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Daniel Dunbar | c5bcee4 | 2009-02-16 23:38:56 +0000 | [diff] [blame] | 936 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 937 | if (Align > 8) { |
Daniel Dunbar | c5bcee4 | 2009-02-16 23:38:56 +0000 | [diff] [blame] | 938 | // Note that we follow the ABI & gcc here, even though the type |
| 939 | // could in theory have an alignment greater than 16. This case |
| 940 | // shouldn't ever matter in practice. |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 941 | |
Daniel Dunbar | c5bcee4 | 2009-02-16 23:38:56 +0000 | [diff] [blame] | 942 | // overflow_arg_area = (overflow_arg_area + 15) & ~15; |
| 943 | llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15); |
| 944 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 945 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
| 946 | llvm::Type::Int64Ty); |
| 947 | llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL); |
| 948 | overflow_arg_area = |
| 949 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 950 | overflow_arg_area->getType(), |
| 951 | "overflow_arg_area.align"); |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
| 955 | const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
| 956 | llvm::Value *Res = |
| 957 | CGF.Builder.CreateBitCast(overflow_arg_area, |
| 958 | llvm::PointerType::getUnqual(LTy)); |
| 959 | |
| 960 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 961 | // l->overflow_arg_area + sizeof(type). |
| 962 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 963 | // an 8 byte boundary. |
| 964 | |
| 965 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
| 966 | llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 967 | (SizeInBytes + 7) & ~7); |
| 968 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 969 | "overflow_arg_area.next"); |
| 970 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 971 | |
| 972 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 973 | return Res; |
| 974 | } |
| 975 | |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 976 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 977 | CodeGenFunction &CGF) const { |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 978 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 979 | // struct { |
| 980 | // i32 gp_offset; |
| 981 | // i32 fp_offset; |
| 982 | // i8* overflow_arg_area; |
| 983 | // i8* reg_save_area; |
| 984 | // }; |
| 985 | unsigned neededInt, neededSSE; |
| 986 | ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(), |
| 987 | neededInt, neededSSE); |
| 988 | |
| 989 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 990 | // in the registers. If not go to step 7. |
| 991 | if (!neededInt && !neededSSE) |
| 992 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 993 | |
| 994 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 995 | // general purpose registers needed to pass type and num_fp to hold |
| 996 | // the number of floating point registers needed. |
| 997 | |
| 998 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 999 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 1000 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 1001 | // |
| 1002 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 1003 | // register save space). |
| 1004 | |
| 1005 | llvm::Value *InRegs = 0; |
| 1006 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 1007 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 1008 | if (neededInt) { |
| 1009 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 1010 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
| 1011 | InRegs = |
| 1012 | CGF.Builder.CreateICmpULE(gp_offset, |
| 1013 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 1014 | 48 - neededInt * 8), |
| 1015 | "fits_in_gp"); |
| 1016 | } |
| 1017 | |
| 1018 | if (neededSSE) { |
| 1019 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 1020 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 1021 | llvm::Value *FitsInFP = |
| 1022 | CGF.Builder.CreateICmpULE(fp_offset, |
| 1023 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
Daniel Dunbar | 90dafa1 | 2009-02-18 22:19:44 +0000 | [diff] [blame] | 1024 | 176 - neededSSE * 16), |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1025 | "fits_in_fp"); |
Daniel Dunbar | f231346 | 2009-02-18 22:05:01 +0000 | [diff] [blame] | 1026 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 1030 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 1031 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 1032 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 1033 | |
| 1034 | // Emit code to load the value if it was passed in registers. |
| 1035 | |
| 1036 | CGF.EmitBlock(InRegBlock); |
| 1037 | |
| 1038 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 1039 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 1040 | // copying to a temporary location in case the parameter is passed |
| 1041 | // in different register classes or requires an alignment greater |
| 1042 | // than 8 for general purpose registers and 16 for XMM registers. |
Daniel Dunbar | 3e030b4 | 2009-02-18 03:44:19 +0000 | [diff] [blame] | 1043 | // |
| 1044 | // FIXME: This really results in shameful code when we end up |
| 1045 | // needing to collect arguments from different places; often what |
| 1046 | // should result in a simple assembling of a structure from |
| 1047 | // scattered addresses has many more loads than necessary. Can we |
| 1048 | // clean this up? |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1049 | const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
| 1050 | llvm::Value *RegAddr = |
| 1051 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 1052 | "reg_save_area"); |
| 1053 | if (neededInt && neededSSE) { |
Daniel Dunbar | 55e5d89 | 2009-02-13 17:46:31 +0000 | [diff] [blame] | 1054 | // FIXME: Cleanup. |
| 1055 | assert(AI.isCoerce() && "Unexpected ABI info for mixed regs"); |
| 1056 | const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
| 1057 | llvm::Value *Tmp = CGF.CreateTempAlloca(ST); |
| 1058 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
| 1059 | const llvm::Type *TyLo = ST->getElementType(0); |
| 1060 | const llvm::Type *TyHi = ST->getElementType(1); |
| 1061 | assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) && |
| 1062 | "Unexpected ABI info for mixed regs"); |
| 1063 | const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 1064 | const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
| 1065 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 1066 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 1067 | llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr; |
| 1068 | llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr; |
| 1069 | llvm::Value *V = |
| 1070 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 1071 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 1072 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 1073 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 1074 | |
| 1075 | RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1076 | } else if (neededInt) { |
| 1077 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 1078 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 1079 | llvm::PointerType::getUnqual(LTy)); |
| 1080 | } else { |
Daniel Dunbar | 3e030b4 | 2009-02-18 03:44:19 +0000 | [diff] [blame] | 1081 | if (neededSSE == 1) { |
| 1082 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 1083 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 1084 | llvm::PointerType::getUnqual(LTy)); |
| 1085 | } else { |
| 1086 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 1087 | // SSE registers are spaced 16 bytes apart in the register save |
| 1088 | // area, we need to collect the two eightbytes together. |
| 1089 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 1090 | llvm::Value *RegAddrHi = |
| 1091 | CGF.Builder.CreateGEP(RegAddrLo, |
| 1092 | llvm::ConstantInt::get(llvm::Type::Int32Ty, 16)); |
| 1093 | const llvm::Type *DblPtrTy = |
| 1094 | llvm::PointerType::getUnqual(llvm::Type::DoubleTy); |
| 1095 | const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy, |
| 1096 | llvm::Type::DoubleTy, |
| 1097 | NULL); |
| 1098 | llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST); |
| 1099 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 1100 | DblPtrTy)); |
| 1101 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 1102 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 1103 | DblPtrTy)); |
| 1104 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 1105 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 1106 | llvm::PointerType::getUnqual(LTy)); |
| 1107 | } |
Daniel Dunbar | be9eb09 | 2009-02-12 09:04:14 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 1111 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 1112 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 1113 | if (neededInt) { |
| 1114 | llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 1115 | neededInt * 8); |
| 1116 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 1117 | gp_offset_p); |
| 1118 | } |
| 1119 | if (neededSSE) { |
| 1120 | llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 1121 | neededSSE * 16); |
| 1122 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 1123 | fp_offset_p); |
| 1124 | } |
| 1125 | CGF.EmitBranch(ContBlock); |
| 1126 | |
| 1127 | // Emit code to load the value if it was passed in memory. |
| 1128 | |
| 1129 | CGF.EmitBlock(InMemBlock); |
| 1130 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 1131 | |
| 1132 | // Return the appropriate result. |
| 1133 | |
| 1134 | CGF.EmitBlock(ContBlock); |
| 1135 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), |
| 1136 | "vaarg.addr"); |
| 1137 | ResAddr->reserveOperandSpace(2); |
| 1138 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 1139 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 1140 | |
| 1141 | return ResAddr; |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1144 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy, |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1145 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1146 | if (RetTy->isVoidType()) { |
| 1147 | return ABIArgInfo::getIgnore(); |
| 1148 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1149 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1150 | } else { |
| 1151 | return ABIArgInfo::getDirect(); |
| 1152 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty, |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1156 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1157 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1158 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1159 | } else { |
| 1160 | return ABIArgInfo::getDirect(); |
| 1161 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1164 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1165 | CodeGenFunction &CGF) const { |
| 1166 | return 0; |
| 1167 | } |
| 1168 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1169 | const ABIInfo &CodeGenTypes::getABIInfo() const { |
| 1170 | if (TheABIInfo) |
| 1171 | return *TheABIInfo; |
| 1172 | |
| 1173 | // For now we just cache this in the CodeGenTypes and don't bother |
| 1174 | // to free it. |
| 1175 | const char *TargetPrefix = getContext().Target.getTargetPrefix(); |
| 1176 | if (strcmp(TargetPrefix, "x86") == 0) { |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 1177 | switch (getContext().Target.getPointerWidth(0)) { |
| 1178 | case 32: |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1179 | return *(TheABIInfo = new X86_32ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 1180 | case 64: |
Daniel Dunbar | 11a76ed | 2009-01-30 18:47:53 +0000 | [diff] [blame] | 1181 | return *(TheABIInfo = new X86_64ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 1182 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | return *(TheABIInfo = new DefaultABIInfo); |
| 1186 | } |
| 1187 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1188 | /***/ |
| 1189 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1190 | CGFunctionInfo::CGFunctionInfo(QualType ResTy, |
| 1191 | const llvm::SmallVector<QualType, 16> &ArgTys) { |
| 1192 | NumArgs = ArgTys.size(); |
| 1193 | Args = new ArgInfo[1 + NumArgs]; |
| 1194 | Args[0].type = ResTy; |
| 1195 | for (unsigned i = 0; i < NumArgs; ++i) |
| 1196 | Args[1 + i].type = ArgTys[i]; |
| 1197 | } |
| 1198 | |
| 1199 | /***/ |
| 1200 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1201 | void CodeGenTypes::GetExpandedTypes(QualType Ty, |
| 1202 | std::vector<const llvm::Type*> &ArgTys) { |
| 1203 | const RecordType *RT = Ty->getAsStructureType(); |
| 1204 | assert(RT && "Can only expand structure types."); |
| 1205 | const RecordDecl *RD = RT->getDecl(); |
| 1206 | assert(!RD->hasFlexibleArrayMember() && |
| 1207 | "Cannot expand structure with flexible array."); |
| 1208 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1209 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1210 | e = RD->field_end(); i != e; ++i) { |
| 1211 | const FieldDecl *FD = *i; |
| 1212 | assert(!FD->isBitField() && |
| 1213 | "Cannot expand structure with bit-field members."); |
| 1214 | |
| 1215 | QualType FT = FD->getType(); |
| 1216 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 1217 | GetExpandedTypes(FT, ArgTys); |
| 1218 | } else { |
| 1219 | ArgTys.push_back(ConvertType(FT)); |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | llvm::Function::arg_iterator |
| 1225 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, |
| 1226 | llvm::Function::arg_iterator AI) { |
| 1227 | const RecordType *RT = Ty->getAsStructureType(); |
| 1228 | assert(RT && "Can only expand structure types."); |
| 1229 | |
| 1230 | RecordDecl *RD = RT->getDecl(); |
| 1231 | assert(LV.isSimple() && |
| 1232 | "Unexpected non-simple lvalue during struct expansion."); |
| 1233 | llvm::Value *Addr = LV.getAddress(); |
| 1234 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 1235 | e = RD->field_end(); i != e; ++i) { |
| 1236 | FieldDecl *FD = *i; |
| 1237 | QualType FT = FD->getType(); |
| 1238 | |
| 1239 | // FIXME: What are the right qualifiers here? |
| 1240 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 1241 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 1242 | AI = ExpandTypeFromArgs(FT, LV, AI); |
| 1243 | } else { |
| 1244 | EmitStoreThroughLValue(RValue::get(AI), LV, FT); |
| 1245 | ++AI; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | return AI; |
| 1250 | } |
| 1251 | |
| 1252 | void |
| 1253 | CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, |
| 1254 | llvm::SmallVector<llvm::Value*, 16> &Args) { |
| 1255 | const RecordType *RT = Ty->getAsStructureType(); |
| 1256 | assert(RT && "Can only expand structure types."); |
| 1257 | |
| 1258 | RecordDecl *RD = RT->getDecl(); |
| 1259 | assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); |
| 1260 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 1261 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 1262 | e = RD->field_end(); i != e; ++i) { |
| 1263 | FieldDecl *FD = *i; |
| 1264 | QualType FT = FD->getType(); |
| 1265 | |
| 1266 | // FIXME: What are the right qualifiers here? |
| 1267 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 1268 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 1269 | ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); |
| 1270 | } else { |
| 1271 | RValue RV = EmitLoadOfLValue(LV, FT); |
| 1272 | assert(RV.isScalar() && |
| 1273 | "Unexpected non-scalar rvalue during struct expansion."); |
| 1274 | Args.push_back(RV.getScalarVal()); |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1279 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
| 1280 | /// a pointer to an object of type \arg Ty. |
| 1281 | /// |
| 1282 | /// This safely handles the case when the src type is smaller than the |
| 1283 | /// destination type; in this situation the values of bits which not |
| 1284 | /// present in the src are undefined. |
| 1285 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, |
| 1286 | const llvm::Type *Ty, |
| 1287 | CodeGenFunction &CGF) { |
| 1288 | const llvm::Type *SrcTy = |
| 1289 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 1290 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 1291 | uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty); |
| 1292 | |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1293 | // If load is legal, just bitcast the src pointer. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1294 | if (SrcSize == DstSize) { |
| 1295 | llvm::Value *Casted = |
| 1296 | CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 1297 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); |
| 1298 | // FIXME: Use better alignment / avoid requiring aligned load. |
| 1299 | Load->setAlignment(1); |
| 1300 | return Load; |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1301 | } else { |
| 1302 | assert(SrcSize < DstSize && "Coercion is losing source bits!"); |
| 1303 | |
| 1304 | // Otherwise do coercion through memory. This is stupid, but |
| 1305 | // simple. |
| 1306 | llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); |
| 1307 | llvm::Value *Casted = |
| 1308 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 1309 | llvm::StoreInst *Store = |
| 1310 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); |
| 1311 | // FIXME: Use better alignment / avoid requiring aligned store. |
| 1312 | Store->setAlignment(1); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1313 | return CGF.Builder.CreateLoad(Tmp); |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
| 1318 | /// where the source and destination may have different types. |
| 1319 | /// |
| 1320 | /// This safely handles the case when the src type is larger than the |
| 1321 | /// destination type; the upper bits of the src will be lost. |
| 1322 | static void CreateCoercedStore(llvm::Value *Src, |
| 1323 | llvm::Value *DstPtr, |
| 1324 | CodeGenFunction &CGF) { |
| 1325 | const llvm::Type *SrcTy = Src->getType(); |
| 1326 | const llvm::Type *DstTy = |
| 1327 | cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 1328 | |
| 1329 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 1330 | uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy); |
| 1331 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1332 | // If store is legal, just bitcast the src pointer. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1333 | if (SrcSize == DstSize) { |
| 1334 | llvm::Value *Casted = |
| 1335 | CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 1336 | // FIXME: Use better alignment / avoid requiring aligned store. |
| 1337 | CGF.Builder.CreateStore(Src, Casted)->setAlignment(1); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1338 | } else { |
| 1339 | assert(SrcSize > DstSize && "Coercion is missing bits!"); |
| 1340 | |
| 1341 | // Otherwise do coercion through memory. This is stupid, but |
| 1342 | // simple. |
| 1343 | llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); |
| 1344 | CGF.Builder.CreateStore(Src, Tmp); |
| 1345 | llvm::Value *Casted = |
| 1346 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); |
Daniel Dunbar | 386621f | 2009-02-07 02:46:03 +0000 | [diff] [blame] | 1347 | llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); |
| 1348 | // FIXME: Use better alignment / avoid requiring aligned load. |
| 1349 | Load->setAlignment(1); |
| 1350 | CGF.Builder.CreateStore(Load, DstPtr); |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1354 | /***/ |
| 1355 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1356 | bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1357 | return FI.getReturnInfo().isIndirect(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1360 | const llvm::FunctionType * |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1361 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1362 | std::vector<const llvm::Type*> ArgTys; |
| 1363 | |
| 1364 | const llvm::Type *ResultType = 0; |
| 1365 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1366 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1367 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1368 | switch (RetAI.getKind()) { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1369 | case ABIArgInfo::Expand: |
| 1370 | assert(0 && "Invalid ABI kind for return argument"); |
| 1371 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1372 | case ABIArgInfo::Direct: |
| 1373 | ResultType = ConvertType(RetTy); |
| 1374 | break; |
| 1375 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1376 | case ABIArgInfo::Indirect: { |
| 1377 | assert(!RetAI.getIndirectAlign() && "Align unused on indirect return."); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1378 | ResultType = llvm::Type::VoidTy; |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1379 | const llvm::Type *STy = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1380 | ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); |
| 1381 | break; |
| 1382 | } |
| 1383 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1384 | case ABIArgInfo::Ignore: |
| 1385 | ResultType = llvm::Type::VoidTy; |
| 1386 | break; |
| 1387 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1388 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1389 | ResultType = RetAI.getCoerceToType(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1390 | break; |
| 1391 | } |
| 1392 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1393 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
| 1394 | ie = FI.arg_end(); it != ie; ++it) { |
| 1395 | const ABIArgInfo &AI = it->info; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1396 | |
| 1397 | switch (AI.getKind()) { |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1398 | case ABIArgInfo::Ignore: |
| 1399 | break; |
| 1400 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1401 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1402 | ArgTys.push_back(AI.getCoerceToType()); |
| 1403 | break; |
| 1404 | |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1405 | case ABIArgInfo::Indirect: { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1406 | // indirect arguments are always on the stack, which is addr space #0. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1407 | const llvm::Type *LTy = ConvertTypeForMem(it->type); |
| 1408 | ArgTys.push_back(llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1409 | break; |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1410 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1411 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1412 | case ABIArgInfo::Direct: |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1413 | ArgTys.push_back(ConvertType(it->type)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1414 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1415 | |
| 1416 | case ABIArgInfo::Expand: |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1417 | GetExpandedTypes(it->type, ArgTys); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1418 | break; |
| 1419 | } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1422 | return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1425 | void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1426 | const Decl *TargetDecl, |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1427 | AttributeListType &PAL) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1428 | unsigned FuncAttrs = 0; |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1429 | unsigned RetAttrs = 0; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1430 | |
| 1431 | if (TargetDecl) { |
| 1432 | if (TargetDecl->getAttr<NoThrowAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1433 | FuncAttrs |= llvm::Attribute::NoUnwind; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1434 | if (TargetDecl->getAttr<NoReturnAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1435 | FuncAttrs |= llvm::Attribute::NoReturn; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1436 | if (TargetDecl->getAttr<PureAttr>()) |
| 1437 | FuncAttrs |= llvm::Attribute::ReadOnly; |
| 1438 | if (TargetDecl->getAttr<ConstAttr>()) |
| 1439 | FuncAttrs |= llvm::Attribute::ReadNone; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1442 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1443 | unsigned Index = 1; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1444 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1445 | switch (RetAI.getKind()) { |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1446 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1447 | if (RetTy->isPromotableIntegerType()) { |
| 1448 | if (RetTy->isSignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1449 | RetAttrs |= llvm::Attribute::SExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1450 | } else if (RetTy->isUnsignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1451 | RetAttrs |= llvm::Attribute::ZExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | break; |
| 1455 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1456 | case ABIArgInfo::Indirect: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1457 | PAL.push_back(llvm::AttributeWithIndex::get(Index, |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1458 | llvm::Attribute::StructRet | |
| 1459 | llvm::Attribute::NoAlias)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1460 | ++Index; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1461 | break; |
| 1462 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1463 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1464 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1465 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1466 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1467 | case ABIArgInfo::Expand: |
| 1468 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1469 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1470 | |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1471 | if (RetAttrs) |
| 1472 | PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1473 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
| 1474 | ie = FI.arg_end(); it != ie; ++it) { |
| 1475 | QualType ParamType = it->type; |
| 1476 | const ABIArgInfo &AI = it->info; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1477 | unsigned Attributes = 0; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1478 | |
| 1479 | switch (AI.getKind()) { |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1480 | case ABIArgInfo::Coerce: |
| 1481 | break; |
| 1482 | |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1483 | case ABIArgInfo::Indirect: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1484 | Attributes |= llvm::Attribute::ByVal; |
Daniel Dunbar | ca00882 | 2009-02-05 01:31:19 +0000 | [diff] [blame] | 1485 | Attributes |= |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1486 | llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign()); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1487 | break; |
| 1488 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1489 | case ABIArgInfo::Direct: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1490 | if (ParamType->isPromotableIntegerType()) { |
| 1491 | if (ParamType->isSignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1492 | Attributes |= llvm::Attribute::SExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1493 | } else if (ParamType->isUnsignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1494 | Attributes |= llvm::Attribute::ZExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1495 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1496 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1497 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1498 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1499 | case ABIArgInfo::Ignore: |
| 1500 | // Skip increment, no matching LLVM parameter. |
| 1501 | continue; |
| 1502 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1503 | case ABIArgInfo::Expand: { |
| 1504 | std::vector<const llvm::Type*> Tys; |
| 1505 | // FIXME: This is rather inefficient. Do we ever actually need |
| 1506 | // to do anything here? The result should be just reconstructed |
| 1507 | // on the other side, so extension should be a non-issue. |
| 1508 | getTypes().GetExpandedTypes(ParamType, Tys); |
| 1509 | Index += Tys.size(); |
| 1510 | continue; |
| 1511 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1512 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1513 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1514 | if (Attributes) |
| 1515 | PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1516 | ++Index; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1517 | } |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1518 | if (FuncAttrs) |
| 1519 | PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1522 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 1523 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1524 | const FunctionArgList &Args) { |
Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 1525 | // FIXME: We no longer need the types from FunctionArgList; lift up |
| 1526 | // and simplify. |
| 1527 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1528 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 1529 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 1530 | |
| 1531 | // Name the struct return argument. |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1532 | if (CGM.ReturnTypeUsesSret(FI)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1533 | AI->setName("agg.result"); |
| 1534 | ++AI; |
| 1535 | } |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1536 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 1537 | assert(FI.arg_size() == Args.size() && |
| 1538 | "Mismatch between function signature & arguments."); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1539 | CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1540 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1541 | i != e; ++i, ++info_it) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1542 | const VarDecl *Arg = i->first; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1543 | QualType Ty = info_it->type; |
| 1544 | const ABIArgInfo &ArgI = info_it->info; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1545 | |
| 1546 | switch (ArgI.getKind()) { |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1547 | case ABIArgInfo::Indirect: { |
| 1548 | llvm::Value* V = AI; |
| 1549 | if (hasAggregateLLVMType(Ty)) { |
| 1550 | // Do nothing, aggregates and complex variables are accessed by |
| 1551 | // reference. |
| 1552 | } else { |
| 1553 | // Load scalar value from indirect argument. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1554 | V = EmitLoadOfScalar(V, false, Ty); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1555 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
| 1556 | // This must be a promotion, for something like |
| 1557 | // "void a(x) short x; {..." |
| 1558 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
| 1559 | } |
| 1560 | } |
| 1561 | EmitParmDecl(*Arg, V); |
| 1562 | break; |
| 1563 | } |
| 1564 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1565 | case ABIArgInfo::Direct: { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1566 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1567 | llvm::Value* V = AI; |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1568 | if (hasAggregateLLVMType(Ty)) { |
| 1569 | // Create a temporary alloca to hold the argument; the rest of |
| 1570 | // codegen expects to access aggregates & complex values by |
| 1571 | // reference. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1572 | V = CreateTempAlloca(ConvertTypeForMem(Ty)); |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1573 | Builder.CreateStore(AI, V); |
| 1574 | } else { |
| 1575 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
| 1576 | // This must be a promotion, for something like |
| 1577 | // "void a(x) short x; {..." |
| 1578 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
| 1579 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1580 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1581 | EmitParmDecl(*Arg, V); |
| 1582 | break; |
| 1583 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1584 | |
| 1585 | case ABIArgInfo::Expand: { |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1586 | // If this structure was expanded into multiple arguments then |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1587 | // we need to create a temporary and reconstruct it from the |
| 1588 | // arguments. |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1589 | std::string Name = Arg->getNameAsString(); |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1590 | llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty), |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1591 | (Name + ".addr").c_str()); |
| 1592 | // FIXME: What are the right qualifiers here? |
| 1593 | llvm::Function::arg_iterator End = |
| 1594 | ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI); |
| 1595 | EmitParmDecl(*Arg, Temp); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1596 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1597 | // Name the arguments used in expansion and increment AI. |
| 1598 | unsigned Index = 0; |
| 1599 | for (; AI != End; ++AI, ++Index) |
| 1600 | AI->setName(Name + "." + llvm::utostr(Index)); |
| 1601 | continue; |
| 1602 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1603 | |
| 1604 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 8b979d9 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 1605 | // Initialize the local variable appropriately. |
| 1606 | if (hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1607 | EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty))); |
Daniel Dunbar | 8b979d9 | 2009-02-10 00:06:49 +0000 | [diff] [blame] | 1608 | } else { |
| 1609 | EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType()))); |
| 1610 | } |
| 1611 | |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame] | 1612 | // Skip increment, no matching LLVM parameter. |
| 1613 | continue; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1614 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1615 | case ABIArgInfo::Coerce: { |
| 1616 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1617 | // FIXME: This is very wasteful; EmitParmDecl is just going to |
| 1618 | // drop the result in a new alloca anyway, so we could just |
| 1619 | // store into that directly if we broke the abstraction down |
| 1620 | // more. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1621 | llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce"); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1622 | CreateCoercedStore(AI, V, *this); |
| 1623 | // Match to what EmitParmDecl is expecting for this type. |
Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 1624 | if (!CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1625 | V = EmitLoadOfScalar(V, false, Ty); |
Daniel Dunbar | 8b29a38 | 2009-02-04 07:22:24 +0000 | [diff] [blame] | 1626 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
| 1627 | // This must be a promotion, for something like |
| 1628 | // "void a(x) short x; {..." |
| 1629 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
| 1630 | } |
| 1631 | } |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1632 | EmitParmDecl(*Arg, V); |
| 1633 | break; |
| 1634 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1635 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1636 | |
| 1637 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1638 | } |
| 1639 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 1640 | } |
| 1641 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1642 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1643 | llvm::Value *ReturnValue) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1644 | llvm::Value *RV = 0; |
| 1645 | |
| 1646 | // Functions with no result always return void. |
| 1647 | if (ReturnValue) { |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1648 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1649 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1650 | |
| 1651 | switch (RetAI.getKind()) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1652 | case ABIArgInfo::Indirect: |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1653 | if (RetTy->isAnyComplexType()) { |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1654 | ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); |
| 1655 | StoreComplexToAddr(RT, CurFn->arg_begin(), false); |
| 1656 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1657 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
| 1658 | } else { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1659 | EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(), |
| 1660 | false); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1661 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1662 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1663 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1664 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1665 | // The internal return value temp always will have |
| 1666 | // pointer-to-return-type type. |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1667 | RV = Builder.CreateLoad(ReturnValue); |
| 1668 | break; |
| 1669 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1670 | case ABIArgInfo::Ignore: |
| 1671 | break; |
| 1672 | |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1673 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 1674 | RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1675 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1676 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1677 | case ABIArgInfo::Expand: |
| 1678 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1681 | |
| 1682 | if (RV) { |
| 1683 | Builder.CreateRet(RV); |
| 1684 | } else { |
| 1685 | Builder.CreateRetVoid(); |
| 1686 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1689 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
| 1690 | llvm::Value *Callee, |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 1691 | const CallArgList &CallArgs, |
| 1692 | const Decl *TargetDecl) { |
Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 1693 | // FIXME: We no longer need the types from CallArgs; lift up and |
| 1694 | // simplify. |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1695 | llvm::SmallVector<llvm::Value*, 16> Args; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1696 | |
| 1697 | // Handle struct-return functions by passing a pointer to the |
| 1698 | // location that we would like to return into. |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1699 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1700 | const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); |
Daniel Dunbar | 2969a02 | 2009-02-05 09:24:53 +0000 | [diff] [blame] | 1701 | if (CGM.ReturnTypeUsesSret(CallInfo)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1702 | // Create a temporary alloca to hold the result of the call. :( |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1703 | Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy))); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Daniel Dunbar | 4b5f0a4 | 2009-02-04 21:17:21 +0000 | [diff] [blame] | 1706 | assert(CallInfo.arg_size() == CallArgs.size() && |
| 1707 | "Mismatch between function signature & arguments."); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1708 | CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1709 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1710 | I != E; ++I, ++info_it) { |
| 1711 | const ABIArgInfo &ArgInfo = info_it->info; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1712 | RValue RV = I->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1713 | |
| 1714 | switch (ArgInfo.getKind()) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1715 | case ABIArgInfo::Indirect: |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1716 | if (RV.isScalar() || RV.isComplex()) { |
| 1717 | // Make a temporary alloca to pass the argument. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1718 | Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second))); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1719 | if (RV.isScalar()) |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1720 | EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false); |
Daniel Dunbar | 1f74598 | 2009-02-05 09:16:39 +0000 | [diff] [blame] | 1721 | else |
| 1722 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
| 1723 | } else { |
| 1724 | Args.push_back(RV.getAggregateAddr()); |
| 1725 | } |
| 1726 | break; |
| 1727 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1728 | case ABIArgInfo::Direct: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1729 | if (RV.isScalar()) { |
| 1730 | Args.push_back(RV.getScalarVal()); |
| 1731 | } else if (RV.isComplex()) { |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1732 | llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second)); |
| 1733 | Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0); |
| 1734 | Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1); |
| 1735 | Args.push_back(Tmp); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1736 | } else { |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1737 | Args.push_back(Builder.CreateLoad(RV.getAggregateAddr())); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1738 | } |
| 1739 | break; |
| 1740 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1741 | case ABIArgInfo::Ignore: |
| 1742 | break; |
| 1743 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1744 | case ABIArgInfo::Coerce: { |
| 1745 | // FIXME: Avoid the conversion through memory if possible. |
| 1746 | llvm::Value *SrcPtr; |
| 1747 | if (RV.isScalar()) { |
Daniel Dunbar | 5a1be6e | 2009-02-03 23:04:57 +0000 | [diff] [blame] | 1748 | SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce"); |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1749 | EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1750 | } else if (RV.isComplex()) { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1751 | SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce"); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1752 | StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); |
| 1753 | } else |
| 1754 | SrcPtr = RV.getAggregateAddr(); |
| 1755 | Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), |
| 1756 | *this)); |
| 1757 | break; |
| 1758 | } |
| 1759 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1760 | case ABIArgInfo::Expand: |
| 1761 | ExpandTypeToArgs(I->second, RV, Args); |
| 1762 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1763 | } |
| 1764 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1765 | |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1766 | llvm::BasicBlock *InvokeDest = getInvokeDest(); |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1767 | CodeGen::AttributeListType AttributeList; |
Daniel Dunbar | c0ef9f5 | 2009-02-20 18:06:48 +0000 | [diff] [blame] | 1768 | CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList); |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1769 | llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(), |
| 1770 | AttributeList.end()); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1771 | |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1772 | llvm::Instruction *CI; |
| 1773 | if (!InvokeDest || Attrs.getFnAttributes() & (llvm::Attribute::NoUnwind || |
| 1774 | llvm::Attribute::NoReturn)) { |
| 1775 | llvm::CallInst *CallInstr = |
| 1776 | Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size()); |
| 1777 | CI = CallInstr; |
Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 1778 | |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1779 | CallInstr->setAttributes(Attrs); |
| 1780 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 1781 | CallInstr->setCallingConv(F->getCallingConv()); |
Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 1782 | |
Daniel Dunbar | 9834ffb | 2009-02-23 17:26:39 +0000 | [diff] [blame] | 1783 | // If the call doesn't return, finish the basic block and clear the |
| 1784 | // insertion point; this allows the rest of IRgen to discard |
| 1785 | // unreachable code. |
| 1786 | if (CallInstr->doesNotReturn()) { |
| 1787 | Builder.CreateUnreachable(); |
| 1788 | Builder.ClearInsertionPoint(); |
| 1789 | |
| 1790 | // Return a reasonable RValue. |
| 1791 | return GetUndefRValue(RetTy); |
| 1792 | } |
| 1793 | } else { |
| 1794 | llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); |
| 1795 | llvm::InvokeInst *InvokeInstr = |
| 1796 | Builder.CreateInvoke(Callee, Cont, InvokeDest, |
| 1797 | &Args[0], &Args[0]+Args.size()); |
| 1798 | CI = InvokeInstr; |
| 1799 | |
| 1800 | InvokeInstr->setAttributes(Attrs); |
| 1801 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 1802 | InvokeInstr->setCallingConv(F->getCallingConv()); |
| 1803 | |
| 1804 | EmitBlock(Cont); |
Daniel Dunbar | f4fe0f0 | 2009-02-20 18:54:31 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1807 | if (CI->getType() != llvm::Type::VoidTy) |
| 1808 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1809 | |
| 1810 | switch (RetAI.getKind()) { |
Daniel Dunbar | 11e383a | 2009-02-05 08:00:50 +0000 | [diff] [blame] | 1811 | case ABIArgInfo::Indirect: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1812 | if (RetTy->isAnyComplexType()) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1813 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1814 | else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1815 | return RValue::getAggregate(Args[0]); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1816 | else |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1817 | return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1818 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1819 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1820 | if (RetTy->isAnyComplexType()) { |
| 1821 | llvm::Value *Real = Builder.CreateExtractValue(CI, 0); |
| 1822 | llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); |
| 1823 | return RValue::getComplex(std::make_pair(Real, Imag)); |
| 1824 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1825 | llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp"); |
Daniel Dunbar | 2fbf2f5 | 2009-02-05 11:13:54 +0000 | [diff] [blame] | 1826 | Builder.CreateStore(CI, V); |
| 1827 | return RValue::getAggregate(V); |
| 1828 | } else |
| 1829 | return RValue::get(CI); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1830 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1831 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1832 | // If we are ignoring an argument that had a result, make sure to |
| 1833 | // construct the appropriate return value for our caller. |
Daniel Dunbar | 13e8173 | 2009-02-05 07:09:07 +0000 | [diff] [blame] | 1834 | return GetUndefRValue(RetTy); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1835 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1836 | case ABIArgInfo::Coerce: { |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1837 | // FIXME: Avoid the conversion through memory if possible. |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1838 | llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce"); |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 1839 | CreateCoercedStore(CI, V, *this); |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1840 | if (RetTy->isAnyComplexType()) |
| 1841 | return RValue::getComplex(LoadComplexFromAddr(V, false)); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1842 | else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1843 | return RValue::getAggregate(V); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1844 | else |
Daniel Dunbar | adc8bdd | 2009-02-10 01:51:39 +0000 | [diff] [blame] | 1845 | return RValue::get(EmitLoadOfScalar(V, false, RetTy)); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1846 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1847 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1848 | case ABIArgInfo::Expand: |
| 1849 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1850 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1851 | |
| 1852 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 1853 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1854 | } |
Daniel Dunbar | b4094ea | 2009-02-10 20:44:09 +0000 | [diff] [blame] | 1855 | |
| 1856 | /* VarArg handling */ |
| 1857 | |
| 1858 | llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { |
| 1859 | return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); |
| 1860 | } |