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