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