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