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