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