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 | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | 9eb5c6d | 2009-02-03 01:05:53 +0000 | [diff] [blame] | 27 | |
| 28 | #include "ABIInfo.h" |
| 29 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
| 33 | /***/ |
| 34 | |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 35 | // FIXME: Use iterator and sidestep silly type array creation. |
| 36 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 37 | const |
| 38 | CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeNoProto *FTNP) { |
| 39 | return getFunctionInfo(FTNP->getResultType(), |
| 40 | llvm::SmallVector<QualType, 16>()); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 43 | const |
| 44 | CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionTypeProto *FTP) { |
| 45 | llvm::SmallVector<QualType, 16> ArgTys; |
| 46 | // FIXME: Kill copy. |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 47 | for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 48 | ArgTys.push_back(FTP->getArgType(i)); |
| 49 | return getFunctionInfo(FTP->getResultType(), ArgTys); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 52 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) { |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 53 | const FunctionType *FTy = FD->getType()->getAsFunctionType(); |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 54 | if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(FTy)) |
| 55 | return getFunctionInfo(FTP); |
| 56 | return getFunctionInfo(cast<FunctionTypeNoProto>(FTy)); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 59 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) { |
| 60 | llvm::SmallVector<QualType, 16> ArgTys; |
| 61 | ArgTys.push_back(MD->getSelfDecl()->getType()); |
| 62 | ArgTys.push_back(Context.getObjCSelType()); |
| 63 | // FIXME: Kill copy? |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 64 | for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(), |
| 65 | e = MD->param_end(); i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 66 | ArgTys.push_back((*i)->getType()); |
| 67 | return getFunctionInfo(MD->getResultType(), ArgTys); |
Daniel Dunbar | 0dbe227 | 2008-09-08 21:33:45 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 70 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 71 | const CallArgList &Args) { |
| 72 | // FIXME: Kill copy. |
| 73 | llvm::SmallVector<QualType, 16> ArgTys; |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 74 | for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 75 | i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 76 | ArgTys.push_back(i->second); |
| 77 | return getFunctionInfo(ResTy, ArgTys); |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 80 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 81 | const FunctionArgList &Args) { |
| 82 | // FIXME: Kill copy. |
| 83 | llvm::SmallVector<QualType, 16> ArgTys; |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 84 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 85 | i != e; ++i) |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 86 | ArgTys.push_back(i->second); |
| 87 | return getFunctionInfo(ResTy, ArgTys); |
| 88 | } |
| 89 | |
| 90 | const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, |
| 91 | const llvm::SmallVector<QualType, 16> &ArgTys) { |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 92 | // Lookup or create unique function info. |
| 93 | llvm::FoldingSetNodeID ID; |
| 94 | CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end()); |
| 95 | |
| 96 | void *InsertPos = 0; |
| 97 | CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos); |
| 98 | if (FI) |
| 99 | return *FI; |
| 100 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 101 | // Construct the function info. |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 102 | FI = new CGFunctionInfo(ResTy, ArgTys); |
| 103 | FunctionInfos.InsertNode(FI, InsertPos); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 104 | |
| 105 | // Compute ABI information. |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 106 | getABIInfo().computeInfo(*FI, getContext()); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 107 | |
Daniel Dunbar | 40a6be6 | 2009-02-03 00:07:12 +0000 | [diff] [blame] | 108 | return *FI; |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /***/ |
| 112 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 113 | ABIInfo::~ABIInfo() {} |
| 114 | |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 115 | /// isEmptyStruct - Return true iff a structure has no non-empty |
| 116 | /// members. Note that a structure with a flexible array member is not |
| 117 | /// considered empty. |
| 118 | static bool isEmptyStruct(QualType T) { |
| 119 | const RecordType *RT = T->getAsStructureType(); |
| 120 | if (!RT) |
| 121 | return 0; |
| 122 | const RecordDecl *RD = RT->getDecl(); |
| 123 | if (RD->hasFlexibleArrayMember()) |
| 124 | return false; |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 125 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 126 | e = RD->field_end(); i != e; ++i) { |
| 127 | const FieldDecl *FD = *i; |
| 128 | if (!isEmptyStruct(FD->getType())) |
| 129 | return false; |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | /// isSingleElementStruct - Determine if a structure is a "single |
| 135 | /// element struct", i.e. it has exactly one non-empty field or |
| 136 | /// exactly one field which is itself a single element |
| 137 | /// struct. Structures with flexible array members are never |
| 138 | /// considered single element structs. |
| 139 | /// |
| 140 | /// \return The field declaration for the single non-empty field, if |
| 141 | /// it exists. |
| 142 | static const FieldDecl *isSingleElementStruct(QualType T) { |
| 143 | const RecordType *RT = T->getAsStructureType(); |
| 144 | if (!RT) |
| 145 | return 0; |
| 146 | |
| 147 | const RecordDecl *RD = RT->getDecl(); |
| 148 | if (RD->hasFlexibleArrayMember()) |
| 149 | return 0; |
| 150 | |
| 151 | const FieldDecl *Found = 0; |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 152 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 153 | e = RD->field_end(); i != e; ++i) { |
| 154 | const FieldDecl *FD = *i; |
| 155 | QualType FT = FD->getType(); |
| 156 | |
| 157 | if (isEmptyStruct(FT)) { |
| 158 | // Ignore |
| 159 | } else if (Found) { |
| 160 | return 0; |
| 161 | } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 162 | Found = FD; |
| 163 | } else { |
| 164 | Found = isSingleElementStruct(FT); |
| 165 | if (!Found) |
| 166 | return 0; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return Found; |
| 171 | } |
| 172 | |
| 173 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 174 | if (!Ty->getAsBuiltinType() && !Ty->isPointerType()) |
| 175 | return false; |
| 176 | |
| 177 | uint64_t Size = Context.getTypeSize(Ty); |
| 178 | return Size == 32 || Size == 64; |
| 179 | } |
| 180 | |
| 181 | static bool areAllFields32Or64BitBasicType(const RecordDecl *RD, |
| 182 | ASTContext &Context) { |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 183 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 184 | e = RD->field_end(); i != e; ++i) { |
| 185 | const FieldDecl *FD = *i; |
| 186 | |
| 187 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 188 | return false; |
| 189 | |
| 190 | // If this is a bit-field we need to make sure it is still a |
| 191 | // 32-bit or 64-bit type. |
| 192 | if (Expr *BW = FD->getBitWidth()) { |
| 193 | unsigned Width = BW->getIntegerConstantExprValue(Context).getZExtValue(); |
| 194 | if (Width <= 16) |
| 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 201 | namespace { |
| 202 | /// DefaultABIInfo - The default implementation for ABI specific |
| 203 | /// details. This implementation provides information which results in |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 204 | /// self-consistent and sensible LLVM IR generation, but does not |
| 205 | /// conform to any particular ABI. |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 206 | class DefaultABIInfo : public ABIInfo { |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 207 | ABIArgInfo classifyReturnType(QualType RetTy, |
| 208 | ASTContext &Context) const; |
| 209 | |
| 210 | ABIArgInfo classifyArgumentType(QualType RetTy, |
| 211 | ASTContext &Context) const; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 212 | |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 213 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 214 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 215 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 216 | it != ie; ++it) |
| 217 | it->info = classifyArgumentType(it->type, Context); |
| 218 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
| 221 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 222 | class X86_32ABIInfo : public ABIInfo { |
| 223 | public: |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 224 | ABIArgInfo classifyReturnType(QualType RetTy, |
| 225 | ASTContext &Context) const; |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 226 | |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 227 | ABIArgInfo classifyArgumentType(QualType RetTy, |
| 228 | ASTContext &Context) const; |
| 229 | |
| 230 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 231 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 232 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 233 | it != ie; ++it) |
| 234 | it->info = classifyArgumentType(it->type, Context); |
| 235 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 236 | }; |
| 237 | } |
| 238 | |
| 239 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 240 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 241 | if (RetTy->isVoidType()) { |
| 242 | return ABIArgInfo::getIgnore(); |
| 243 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 244 | // Classify "single element" structs as their element type. |
| 245 | const FieldDecl *SeltFD = isSingleElementStruct(RetTy); |
| 246 | if (SeltFD) { |
| 247 | QualType SeltTy = SeltFD->getType()->getDesugaredType(); |
| 248 | if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) { |
| 249 | // FIXME: This is gross, it would be nice if we could just |
| 250 | // pass back SeltTy and have clients deal with it. Is it worth |
| 251 | // supporting coerce to both LLVM and clang Types? |
| 252 | if (BT->isIntegerType()) { |
| 253 | uint64_t Size = Context.getTypeSize(SeltTy); |
| 254 | return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size)); |
| 255 | } else if (BT->getKind() == BuiltinType::Float) { |
| 256 | return ABIArgInfo::getCoerce(llvm::Type::FloatTy); |
| 257 | } else if (BT->getKind() == BuiltinType::Double) { |
| 258 | return ABIArgInfo::getCoerce(llvm::Type::DoubleTy); |
| 259 | } |
| 260 | } else if (SeltTy->isPointerType()) { |
| 261 | // FIXME: It would be really nice if this could come out as |
| 262 | // the proper pointer type. |
| 263 | llvm::Type *PtrTy = |
| 264 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 265 | return ABIArgInfo::getCoerce(PtrTy); |
| 266 | } |
| 267 | } |
| 268 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 269 | uint64_t Size = Context.getTypeSize(RetTy); |
| 270 | if (Size == 8) { |
| 271 | return ABIArgInfo::getCoerce(llvm::Type::Int8Ty); |
| 272 | } else if (Size == 16) { |
| 273 | return ABIArgInfo::getCoerce(llvm::Type::Int16Ty); |
| 274 | } else if (Size == 32) { |
| 275 | return ABIArgInfo::getCoerce(llvm::Type::Int32Ty); |
| 276 | } else if (Size == 64) { |
| 277 | return ABIArgInfo::getCoerce(llvm::Type::Int64Ty); |
| 278 | } else { |
| 279 | return ABIArgInfo::getStructRet(); |
| 280 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 281 | } else { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 282 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 286 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 287 | ASTContext &Context) const { |
Daniel Dunbar | f035738 | 2008-09-17 20:11:04 +0000 | [diff] [blame] | 288 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
Daniel Dunbar | 834af45 | 2008-09-17 21:22:33 +0000 | [diff] [blame] | 289 | // Structures with flexible arrays are always byval. |
| 290 | if (const RecordType *RT = Ty->getAsStructureType()) |
| 291 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 292 | return ABIArgInfo::getByVal(0); |
| 293 | |
| 294 | // Expand empty structs (i.e. ignore) |
| 295 | uint64_t Size = Context.getTypeSize(Ty); |
| 296 | if (Ty->isStructureType() && Size == 0) |
| 297 | return ABIArgInfo::getExpand(); |
| 298 | |
| 299 | // Expand structs with size <= 128-bits which consist only of |
| 300 | // basic types (int, long long, float, double, xxx*). This is |
| 301 | // non-recursive and does not ignore empty fields. |
| 302 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 303 | if (Context.getTypeSize(Ty) <= 4*32 && |
| 304 | areAllFields32Or64BitBasicType(RT->getDecl(), Context)) |
| 305 | return ABIArgInfo::getExpand(); |
| 306 | } |
| 307 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 308 | return ABIArgInfo::getByVal(0); |
| 309 | } else { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 310 | return ABIArgInfo::getDirect(); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 314 | namespace { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 315 | /// X86_64ABIInfo - The X86_64 ABI information. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 316 | class X86_64ABIInfo : public ABIInfo { |
| 317 | enum Class { |
| 318 | Integer = 0, |
| 319 | SSE, |
| 320 | SSEUp, |
| 321 | X87, |
| 322 | X87Up, |
| 323 | ComplexX87, |
| 324 | NoClass, |
| 325 | Memory |
| 326 | }; |
| 327 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 328 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 329 | /// |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 330 | /// Merge an accumulating classification \arg Accum with a field |
| 331 | /// classification \arg Field. |
| 332 | /// |
| 333 | /// \param Accum - The accumulating classification. This should |
| 334 | /// always be either NoClass or the result of a previous merge |
| 335 | /// call. In addition, this should never be Memory (the caller |
| 336 | /// should just return Memory for the aggregate). |
| 337 | Class merge(Class Accum, Class Field) const; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 338 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 339 | /// classify - Determine the x86_64 register classes in which the |
| 340 | /// given type T should be passed. |
| 341 | /// |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 342 | /// \param Lo - The classification for the parts of the type |
| 343 | /// residing in the low word of the containing object. |
| 344 | /// |
| 345 | /// \param Hi - The classification for the parts of the type |
| 346 | /// residing in the high word of the containing object. |
| 347 | /// |
| 348 | /// \param OffsetBase - The bit offset of this type in the |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 349 | /// containing object. Some parameters are classified different |
| 350 | /// depending on whether they straddle an eightbyte boundary. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 351 | /// |
| 352 | /// If a word is unused its result will be NoClass; if a type should |
| 353 | /// be passed in Memory then at least the classification of \arg Lo |
| 354 | /// will be Memory. |
| 355 | /// |
| 356 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
| 357 | /// |
| 358 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 359 | /// be NoClass. |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 360 | void classify(QualType T, ASTContext &Context, uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 361 | Class &Lo, Class &Hi) const; |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 362 | |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 363 | ABIArgInfo classifyReturnType(QualType RetTy, |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame^] | 364 | ASTContext &Context) const; |
| 365 | |
| 366 | ABIArgInfo classifyArgumentType(QualType Ty, |
| 367 | ASTContext &Context, |
| 368 | unsigned &freeIntRegs, |
| 369 | unsigned &freeSSERegs) const; |
| 370 | |
| 371 | public: |
| 372 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 373 | }; |
| 374 | } |
| 375 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 376 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, |
| 377 | Class Field) const { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 378 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 379 | // classified recursively so that always two fields are |
| 380 | // considered. The resulting class is calculated according to |
| 381 | // the classes of the fields in the eightbyte: |
| 382 | // |
| 383 | // (a) If both classes are equal, this is the resulting class. |
| 384 | // |
| 385 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 386 | // the other class. |
| 387 | // |
| 388 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 389 | // class. |
| 390 | // |
| 391 | // (d) If one of the classes is INTEGER, the result is the |
| 392 | // INTEGER. |
| 393 | // |
| 394 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 395 | // MEMORY is used as class. |
| 396 | // |
| 397 | // (f) Otherwise class SSE is used. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 398 | assert((Accum == NoClass || Accum == Integer || |
| 399 | Accum == SSE || Accum == SSEUp) && |
| 400 | "Invalid accumulated classification during merge."); |
| 401 | if (Accum == Field || Field == NoClass) |
| 402 | return Accum; |
| 403 | else if (Field == Memory) |
| 404 | return Memory; |
| 405 | else if (Accum == NoClass) |
| 406 | return Field; |
| 407 | else if (Accum == Integer || Field == Integer) |
| 408 | return Integer; |
| 409 | else if (Field == X87 || Field == X87Up || Field == ComplexX87) |
| 410 | return Memory; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 411 | else |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 412 | return SSE; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 415 | void X86_64ABIInfo::classify(QualType Ty, |
| 416 | ASTContext &Context, |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 417 | uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 418 | Class &Lo, Class &Hi) const { |
Daniel Dunbar | 9a82b52 | 2009-02-02 18:06:39 +0000 | [diff] [blame] | 419 | // FIXME: This code can be simplified by introducing a simple value |
| 420 | // class for Class pairs with appropriate constructor methods for |
| 421 | // the various situations. |
| 422 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 423 | Lo = Hi = NoClass; |
| 424 | |
| 425 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 426 | Current = Memory; |
| 427 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 428 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) { |
| 429 | BuiltinType::Kind k = BT->getKind(); |
| 430 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 431 | if (k == BuiltinType::Void) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 432 | Current = NoClass; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 433 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 434 | Current = Integer; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 435 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 436 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 437 | } else if (k == BuiltinType::LongDouble) { |
| 438 | Lo = X87; |
| 439 | Hi = X87Up; |
| 440 | } |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 441 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 442 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 443 | // FIXME: __int128 is (Integer, Integer). |
| 444 | } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() || |
| 445 | Ty->isObjCQualifiedInterfaceType()) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 446 | Current = Integer; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 447 | } else if (const VectorType *VT = Ty->getAsVectorType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 448 | uint64_t Size = Context.getTypeSize(VT); |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 449 | if (Size == 64) { |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 450 | // gcc passes <1 x double> in memory. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 451 | if (VT->getElementType() == Context.DoubleTy) |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 452 | return; |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 453 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 454 | Current = SSE; |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 455 | |
| 456 | // If this type crosses an eightbyte boundary, it should be |
| 457 | // split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 458 | if (OffsetBase && OffsetBase != 64) |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 459 | Hi = Lo; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 460 | } else if (Size == 128) { |
| 461 | Lo = SSE; |
| 462 | Hi = SSEUp; |
| 463 | } |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 464 | } else if (const ComplexType *CT = Ty->getAsComplexType()) { |
| 465 | QualType ET = CT->getElementType(); |
| 466 | |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 467 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 468 | if (ET->isIntegerType()) { |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 469 | if (Size <= 64) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 470 | Current = Integer; |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 471 | else if (Size <= 128) |
| 472 | Lo = Hi = Integer; |
| 473 | } else if (ET == Context.FloatTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 474 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 475 | else if (ET == Context.DoubleTy) |
| 476 | Lo = Hi = SSE; |
| 477 | else if (ET == Context.LongDoubleTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 478 | Current = ComplexX87; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 479 | |
| 480 | // If this complex type crosses an eightbyte boundary then it |
| 481 | // should be split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 482 | uint64_t EB_Real = (OffsetBase) / 64; |
| 483 | uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 484 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 485 | Hi = Lo; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 486 | } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 487 | // Arrays are treated like structures. |
| 488 | |
| 489 | uint64_t Size = Context.getTypeSize(Ty); |
| 490 | |
| 491 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 492 | // than two eightbytes, ..., it has class MEMORY. |
| 493 | if (Size > 128) |
| 494 | return; |
| 495 | |
| 496 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 497 | // fields, it has class MEMORY. |
| 498 | // |
| 499 | // Only need to check alignment of array base. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 500 | if (OffsetBase % Context.getTypeAlign(AT->getElementType())) |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 501 | return; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 502 | |
| 503 | // Otherwise implement simplified merge. We could be smarter about |
| 504 | // 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] | 505 | Current = NoClass; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 506 | uint64_t EltSize = Context.getTypeSize(AT->getElementType()); |
| 507 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
| 508 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 509 | Class FieldLo, FieldHi; |
| 510 | classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 511 | Lo = merge(Lo, FieldLo); |
| 512 | Hi = merge(Hi, FieldHi); |
| 513 | if (Lo == Memory || Hi == Memory) |
| 514 | break; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 515 | } |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 516 | |
| 517 | // Do post merger cleanup (see below). Only case we worry about is Memory. |
| 518 | if (Hi == Memory) |
| 519 | Lo = Memory; |
| 520 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 521 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 522 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 523 | |
| 524 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 525 | // than two eightbytes, ..., it has class MEMORY. |
| 526 | if (Size > 128) |
| 527 | return; |
| 528 | |
| 529 | const RecordDecl *RD = RT->getDecl(); |
| 530 | |
| 531 | // Assume variable sized types are passed in memory. |
| 532 | if (RD->hasFlexibleArrayMember()) |
| 533 | return; |
| 534 | |
| 535 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 536 | |
| 537 | // Reset Lo class, this will be recomputed. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 538 | Current = NoClass; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 539 | unsigned idx = 0; |
| 540 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 541 | e = RD->field_end(); i != e; ++i, ++idx) { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 542 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 543 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 544 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 545 | // fields, it has class MEMORY. |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 546 | if (Offset % Context.getTypeAlign(i->getType())) { |
| 547 | Lo = Memory; |
| 548 | return; |
| 549 | } |
| 550 | |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 551 | // Classify this field. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 552 | // |
| 553 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 554 | // exceeds a single eightbyte, each is classified |
| 555 | // separately. Each eightbyte gets initialized to class |
| 556 | // NO_CLASS. |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 557 | Class FieldLo, FieldHi; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 558 | classify(i->getType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 559 | Lo = merge(Lo, FieldLo); |
| 560 | Hi = merge(Hi, FieldHi); |
| 561 | if (Lo == Memory || Hi == Memory) |
| 562 | break; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 566 | // |
| 567 | // (a) If one of the classes is MEMORY, the whole argument is |
| 568 | // passed in memory. |
| 569 | // |
| 570 | // (b) If SSEUP is not preceeded by SSE, it is converted to SSE. |
| 571 | |
| 572 | // The first of these conditions is guaranteed by how we implement |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 573 | // the merge (just bail). |
| 574 | // |
| 575 | // The second condition occurs in the case of unions; for example |
| 576 | // union { _Complex double; unsigned; }. |
| 577 | if (Hi == Memory) |
| 578 | Lo = Memory; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 579 | if (Hi == SSEUp && Lo != SSE) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 580 | Hi = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 584 | |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 585 | ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy, |
| 586 | ASTContext &Context) const { |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 587 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 588 | // classification algorithm. |
| 589 | X86_64ABIInfo::Class Lo, Hi; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 590 | classify(RetTy, Context, 0, Lo, Hi); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 591 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 592 | // Check some invariants. |
| 593 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| 594 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); |
| 595 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 596 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 597 | const llvm::Type *ResType = 0; |
| 598 | switch (Lo) { |
| 599 | case NoClass: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 600 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 601 | |
| 602 | case SSEUp: |
| 603 | case X87Up: |
| 604 | assert(0 && "Invalid classification for lo word."); |
| 605 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 606 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 607 | // hidden argument, i.e. structret. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 608 | case Memory: |
| 609 | return ABIArgInfo::getStructRet(); |
| 610 | |
| 611 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 612 | // available register of the sequence %rax, %rdx is used. |
| 613 | case Integer: |
| 614 | ResType = llvm::Type::Int64Ty; break; |
| 615 | |
| 616 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 617 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 618 | case SSE: |
| 619 | ResType = llvm::Type::DoubleTy; break; |
| 620 | |
| 621 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 622 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 623 | case X87: |
| 624 | ResType = llvm::Type::X86_FP80Ty; break; |
| 625 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 626 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 627 | // part of the value is returned in %st0 and the imaginary part in |
| 628 | // %st1. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 629 | case ComplexX87: |
| 630 | assert(Hi == NoClass && "Unexpected ComplexX87 classification."); |
| 631 | ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2); |
| 632 | break; |
| 633 | } |
| 634 | |
| 635 | switch (Hi) { |
| 636 | // Memory was handled previously, and ComplexX87 and X87 should |
| 637 | // never occur as hi classes. |
| 638 | case Memory: |
| 639 | case X87: |
| 640 | case ComplexX87: |
| 641 | assert(0 && "Invalid classification for hi word."); |
| 642 | |
| 643 | case NoClass: break; |
| 644 | case Integer: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 645 | ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL); |
| 646 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 647 | case SSE: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 648 | ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL); |
| 649 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 650 | |
| 651 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
| 652 | // is passed in the upper half of the last used SSE register. |
| 653 | // |
| 654 | // SSEUP should always be preceeded by SSE, just widen. |
| 655 | case SSEUp: |
| 656 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
| 657 | ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2); |
| 658 | break; |
| 659 | |
| 660 | // 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] | 661 | // returned together with the previous X87 value in %st0. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 662 | // |
| 663 | // X87UP should always be preceeded by X87, so we don't need to do |
| 664 | // anything here. |
| 665 | case X87Up: |
| 666 | assert(Lo == X87 && "Unexpected X87Up classification."); |
| 667 | break; |
| 668 | } |
| 669 | |
| 670 | return ABIArgInfo::getCoerce(ResType); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame^] | 673 | ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context, |
| 674 | unsigned &freeIntRegs, |
| 675 | unsigned &freeSSERegs) const { |
| 676 | X86_64ABIInfo::Class Lo, Hi; |
| 677 | classify(Ty, Context, 0, Lo, Hi); |
| 678 | |
| 679 | // Check some invariants. |
| 680 | // FIXME: Enforce these by construction. |
| 681 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| 682 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); |
| 683 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 684 | |
| 685 | unsigned neededInt = 0, neededSSE = 0; |
| 686 | const llvm::Type *ResType = 0; |
| 687 | switch (Lo) { |
| 688 | case NoClass: |
| 689 | return ABIArgInfo::getIgnore(); |
| 690 | |
| 691 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 692 | // on the stack. |
| 693 | case Memory: |
| 694 | |
| 695 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 696 | // COMPLEX_X87, it is passed in memory. |
| 697 | case X87: |
| 698 | case ComplexX87: |
| 699 | // Choose appropriate in memory type. |
| 700 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 701 | return ABIArgInfo::getByVal(0); |
| 702 | else |
| 703 | return ABIArgInfo::getDirect(); |
| 704 | |
| 705 | case SSEUp: |
| 706 | case X87Up: |
| 707 | assert(0 && "Invalid classification for lo word."); |
| 708 | |
| 709 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 710 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 711 | // and %r9 is used. |
| 712 | case Integer: |
| 713 | ++neededInt; |
| 714 | ResType = llvm::Type::Int64Ty; |
| 715 | break; |
| 716 | |
| 717 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 718 | // available SSE register is used, the registers are taken in the |
| 719 | // order from %xmm0 to %xmm7. |
| 720 | case SSE: |
| 721 | ++neededSSE; |
| 722 | ResType = llvm::Type::DoubleTy; |
| 723 | break; |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 724 | } |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame^] | 725 | |
| 726 | switch (Hi) { |
| 727 | // Memory was handled previously, ComplexX87 and X87 should |
| 728 | // never occur as hi classes, and X87Up must be preceed by X87, |
| 729 | // which is passed in memory. |
| 730 | case Memory: |
| 731 | case X87: |
| 732 | case X87Up: |
| 733 | case ComplexX87: |
| 734 | assert(0 && "Invalid classification for hi word."); |
| 735 | |
| 736 | case NoClass: break; |
| 737 | case Integer: |
| 738 | ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL); |
| 739 | ++neededInt; |
| 740 | break; |
| 741 | case SSE: |
| 742 | ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL); |
| 743 | ++neededSSE; |
| 744 | break; |
| 745 | |
| 746 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 747 | // eightbyte is passed in the upper half of the last used SSE |
| 748 | // register. |
| 749 | case SSEUp: |
| 750 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
| 751 | ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2); |
| 752 | break; |
| 753 | } |
| 754 | |
| 755 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 756 | // eightbyte of an argument, the whole argument is passed on the |
| 757 | // stack. If registers have already been assigned for some |
| 758 | // eightbytes of such an argument, the assignments get reverted. |
| 759 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
| 760 | freeIntRegs -= neededInt; |
| 761 | freeSSERegs -= neededSSE; |
| 762 | return ABIArgInfo::getCoerce(ResType); |
| 763 | } else { |
| 764 | // Choose appropriate in memory type. |
| 765 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 766 | return ABIArgInfo::getByVal(0); |
| 767 | else |
| 768 | return ABIArgInfo::getDirect(); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 773 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 774 | |
| 775 | // Keep track of the number of assigned registers. |
| 776 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
| 777 | |
| 778 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 779 | // get assigned (in left-to-right order) for passing as follows... |
| 780 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 781 | it != ie; ++it) |
| 782 | it->info = classifyArgumentType(it->type, Context, freeIntRegs, freeSSERegs); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 785 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy, |
| 786 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 787 | if (RetTy->isVoidType()) { |
| 788 | return ABIArgInfo::getIgnore(); |
| 789 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 790 | return ABIArgInfo::getStructRet(); |
| 791 | } else { |
| 792 | return ABIArgInfo::getDirect(); |
| 793 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty, |
| 797 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 798 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
| 799 | return ABIArgInfo::getByVal(0); |
| 800 | } else { |
| 801 | return ABIArgInfo::getDirect(); |
| 802 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | const ABIInfo &CodeGenTypes::getABIInfo() const { |
| 806 | if (TheABIInfo) |
| 807 | return *TheABIInfo; |
| 808 | |
| 809 | // For now we just cache this in the CodeGenTypes and don't bother |
| 810 | // to free it. |
| 811 | const char *TargetPrefix = getContext().Target.getTargetPrefix(); |
| 812 | if (strcmp(TargetPrefix, "x86") == 0) { |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 813 | switch (getContext().Target.getPointerWidth(0)) { |
| 814 | case 32: |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 815 | return *(TheABIInfo = new X86_32ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 816 | case 64: |
Daniel Dunbar | 11a76ed | 2009-01-30 18:47:53 +0000 | [diff] [blame] | 817 | return *(TheABIInfo = new X86_64ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 818 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | return *(TheABIInfo = new DefaultABIInfo); |
| 822 | } |
| 823 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 824 | /***/ |
| 825 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 826 | CGFunctionInfo::CGFunctionInfo(QualType ResTy, |
| 827 | const llvm::SmallVector<QualType, 16> &ArgTys) { |
| 828 | NumArgs = ArgTys.size(); |
| 829 | Args = new ArgInfo[1 + NumArgs]; |
| 830 | Args[0].type = ResTy; |
| 831 | for (unsigned i = 0; i < NumArgs; ++i) |
| 832 | Args[1 + i].type = ArgTys[i]; |
| 833 | } |
| 834 | |
| 835 | /***/ |
| 836 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 837 | void CodeGenTypes::GetExpandedTypes(QualType Ty, |
| 838 | std::vector<const llvm::Type*> &ArgTys) { |
| 839 | const RecordType *RT = Ty->getAsStructureType(); |
| 840 | assert(RT && "Can only expand structure types."); |
| 841 | const RecordDecl *RD = RT->getDecl(); |
| 842 | assert(!RD->hasFlexibleArrayMember() && |
| 843 | "Cannot expand structure with flexible array."); |
| 844 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 845 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 846 | e = RD->field_end(); i != e; ++i) { |
| 847 | const FieldDecl *FD = *i; |
| 848 | assert(!FD->isBitField() && |
| 849 | "Cannot expand structure with bit-field members."); |
| 850 | |
| 851 | QualType FT = FD->getType(); |
| 852 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 853 | GetExpandedTypes(FT, ArgTys); |
| 854 | } else { |
| 855 | ArgTys.push_back(ConvertType(FT)); |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | llvm::Function::arg_iterator |
| 861 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, |
| 862 | llvm::Function::arg_iterator AI) { |
| 863 | const RecordType *RT = Ty->getAsStructureType(); |
| 864 | assert(RT && "Can only expand structure types."); |
| 865 | |
| 866 | RecordDecl *RD = RT->getDecl(); |
| 867 | assert(LV.isSimple() && |
| 868 | "Unexpected non-simple lvalue during struct expansion."); |
| 869 | llvm::Value *Addr = LV.getAddress(); |
| 870 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 871 | e = RD->field_end(); i != e; ++i) { |
| 872 | FieldDecl *FD = *i; |
| 873 | QualType FT = FD->getType(); |
| 874 | |
| 875 | // FIXME: What are the right qualifiers here? |
| 876 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 877 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 878 | AI = ExpandTypeFromArgs(FT, LV, AI); |
| 879 | } else { |
| 880 | EmitStoreThroughLValue(RValue::get(AI), LV, FT); |
| 881 | ++AI; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | return AI; |
| 886 | } |
| 887 | |
| 888 | void |
| 889 | CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, |
| 890 | llvm::SmallVector<llvm::Value*, 16> &Args) { |
| 891 | const RecordType *RT = Ty->getAsStructureType(); |
| 892 | assert(RT && "Can only expand structure types."); |
| 893 | |
| 894 | RecordDecl *RD = RT->getDecl(); |
| 895 | assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); |
| 896 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 897 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 898 | e = RD->field_end(); i != e; ++i) { |
| 899 | FieldDecl *FD = *i; |
| 900 | QualType FT = FD->getType(); |
| 901 | |
| 902 | // FIXME: What are the right qualifiers here? |
| 903 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 904 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 905 | ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); |
| 906 | } else { |
| 907 | RValue RV = EmitLoadOfLValue(LV, FT); |
| 908 | assert(RV.isScalar() && |
| 909 | "Unexpected non-scalar rvalue during struct expansion."); |
| 910 | Args.push_back(RV.getScalarVal()); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 915 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
| 916 | /// a pointer to an object of type \arg Ty. |
| 917 | /// |
| 918 | /// This safely handles the case when the src type is smaller than the |
| 919 | /// destination type; in this situation the values of bits which not |
| 920 | /// present in the src are undefined. |
| 921 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, |
| 922 | const llvm::Type *Ty, |
| 923 | CodeGenFunction &CGF) { |
| 924 | const llvm::Type *SrcTy = |
| 925 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 926 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 927 | uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty); |
| 928 | |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 929 | // If load is legal, just bitcast the src pointer. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 930 | if (SrcSize == DstSize) { |
| 931 | llvm::Value *Casted = |
| 932 | CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); |
| 933 | return CGF.Builder.CreateLoad(Casted); |
| 934 | } else { |
| 935 | assert(SrcSize < DstSize && "Coercion is losing source bits!"); |
| 936 | |
| 937 | // Otherwise do coercion through memory. This is stupid, but |
| 938 | // simple. |
| 939 | llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); |
| 940 | llvm::Value *Casted = |
| 941 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); |
| 942 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); |
| 943 | return CGF.Builder.CreateLoad(Tmp); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
| 948 | /// where the source and destination may have different types. |
| 949 | /// |
| 950 | /// This safely handles the case when the src type is larger than the |
| 951 | /// destination type; the upper bits of the src will be lost. |
| 952 | static void CreateCoercedStore(llvm::Value *Src, |
| 953 | llvm::Value *DstPtr, |
| 954 | CodeGenFunction &CGF) { |
| 955 | const llvm::Type *SrcTy = Src->getType(); |
| 956 | const llvm::Type *DstTy = |
| 957 | cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 958 | |
| 959 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 960 | uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy); |
| 961 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 962 | // If store is legal, just bitcast the src pointer. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 963 | if (SrcSize == DstSize) { |
| 964 | llvm::Value *Casted = |
| 965 | CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); |
| 966 | CGF.Builder.CreateStore(Src, Casted); |
| 967 | } else { |
| 968 | assert(SrcSize > DstSize && "Coercion is missing bits!"); |
| 969 | |
| 970 | // Otherwise do coercion through memory. This is stupid, but |
| 971 | // simple. |
| 972 | llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); |
| 973 | CGF.Builder.CreateStore(Src, Tmp); |
| 974 | llvm::Value *Casted = |
| 975 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); |
| 976 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr); |
| 977 | } |
| 978 | } |
| 979 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 980 | /***/ |
| 981 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 982 | bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) { |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 983 | return FI.getReturnInfo().isStructRet(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 986 | const llvm::FunctionType * |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 987 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 988 | std::vector<const llvm::Type*> ArgTys; |
| 989 | |
| 990 | const llvm::Type *ResultType = 0; |
| 991 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 992 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 993 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 994 | switch (RetAI.getKind()) { |
| 995 | case ABIArgInfo::ByVal: |
| 996 | case ABIArgInfo::Expand: |
| 997 | assert(0 && "Invalid ABI kind for return argument"); |
| 998 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 999 | case ABIArgInfo::Direct: |
| 1000 | ResultType = ConvertType(RetTy); |
| 1001 | break; |
| 1002 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1003 | case ABIArgInfo::StructRet: { |
| 1004 | ResultType = llvm::Type::VoidTy; |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1005 | const llvm::Type *STy = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1006 | ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); |
| 1007 | break; |
| 1008 | } |
| 1009 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1010 | case ABIArgInfo::Ignore: |
| 1011 | ResultType = llvm::Type::VoidTy; |
| 1012 | break; |
| 1013 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1014 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1015 | ResultType = RetAI.getCoerceToType(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1016 | break; |
| 1017 | } |
| 1018 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1019 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
| 1020 | ie = FI.arg_end(); it != ie; ++it) { |
| 1021 | const ABIArgInfo &AI = it->info; |
| 1022 | const llvm::Type *Ty = ConvertType(it->type); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1023 | |
| 1024 | switch (AI.getKind()) { |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1025 | case ABIArgInfo::Ignore: |
| 1026 | break; |
| 1027 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1028 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1029 | ArgTys.push_back(AI.getCoerceToType()); |
| 1030 | break; |
| 1031 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1032 | case ABIArgInfo::StructRet: |
| 1033 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1034 | |
| 1035 | case ABIArgInfo::ByVal: |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1036 | // byval arguments are always on the stack, which is addr space #0. |
| 1037 | ArgTys.push_back(llvm::PointerType::getUnqual(Ty)); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1038 | assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); |
| 1039 | break; |
| 1040 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1041 | case ABIArgInfo::Direct: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1042 | ArgTys.push_back(Ty); |
| 1043 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1044 | |
| 1045 | case ABIArgInfo::Expand: |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1046 | GetExpandedTypes(it->type, ArgTys); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1047 | break; |
| 1048 | } |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1051 | return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1054 | void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1055 | const Decl *TargetDecl, |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1056 | AttributeListType &PAL) { |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1057 | unsigned FuncAttrs = 0; |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1058 | unsigned RetAttrs = 0; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1059 | |
| 1060 | if (TargetDecl) { |
| 1061 | if (TargetDecl->getAttr<NoThrowAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1062 | FuncAttrs |= llvm::Attribute::NoUnwind; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1063 | if (TargetDecl->getAttr<NoReturnAttr>()) |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1064 | FuncAttrs |= llvm::Attribute::NoReturn; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1065 | if (TargetDecl->getAttr<PureAttr>()) |
| 1066 | FuncAttrs |= llvm::Attribute::ReadOnly; |
| 1067 | if (TargetDecl->getAttr<ConstAttr>()) |
| 1068 | FuncAttrs |= llvm::Attribute::ReadNone; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 1071 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1072 | unsigned Index = 1; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1073 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1074 | switch (RetAI.getKind()) { |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1075 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1076 | if (RetTy->isPromotableIntegerType()) { |
| 1077 | if (RetTy->isSignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1078 | RetAttrs |= llvm::Attribute::SExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1079 | } else if (RetTy->isUnsignedIntegerType()) { |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1080 | RetAttrs |= llvm::Attribute::ZExt; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | break; |
| 1084 | |
| 1085 | case ABIArgInfo::StructRet: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1086 | PAL.push_back(llvm::AttributeWithIndex::get(Index, |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1087 | llvm::Attribute::StructRet | |
| 1088 | llvm::Attribute::NoAlias)); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1089 | ++Index; |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1090 | break; |
| 1091 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1092 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1093 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1094 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1095 | |
| 1096 | case ABIArgInfo::ByVal: |
| 1097 | case ABIArgInfo::Expand: |
| 1098 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1099 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1100 | |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1101 | if (RetAttrs) |
| 1102 | PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 1103 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
| 1104 | ie = FI.arg_end(); it != ie; ++it) { |
| 1105 | QualType ParamType = it->type; |
| 1106 | const ABIArgInfo &AI = it->info; |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1107 | unsigned Attributes = 0; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1108 | |
| 1109 | switch (AI.getKind()) { |
| 1110 | case ABIArgInfo::StructRet: |
| 1111 | assert(0 && "Invalid ABI kind for non-return argument"); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1112 | |
| 1113 | case ABIArgInfo::Coerce: |
| 1114 | break; |
| 1115 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1116 | case ABIArgInfo::ByVal: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1117 | Attributes |= llvm::Attribute::ByVal; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1118 | assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); |
| 1119 | break; |
| 1120 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1121 | case ABIArgInfo::Direct: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1122 | if (ParamType->isPromotableIntegerType()) { |
| 1123 | if (ParamType->isSignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1124 | Attributes |= llvm::Attribute::SExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1125 | } else if (ParamType->isUnsignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1126 | Attributes |= llvm::Attribute::ZExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1127 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1128 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1129 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1130 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1131 | case ABIArgInfo::Ignore: |
| 1132 | // Skip increment, no matching LLVM parameter. |
| 1133 | continue; |
| 1134 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1135 | case ABIArgInfo::Expand: { |
| 1136 | std::vector<const llvm::Type*> Tys; |
| 1137 | // FIXME: This is rather inefficient. Do we ever actually need |
| 1138 | // to do anything here? The result should be just reconstructed |
| 1139 | // on the other side, so extension should be a non-issue. |
| 1140 | getTypes().GetExpandedTypes(ParamType, Tys); |
| 1141 | Index += Tys.size(); |
| 1142 | continue; |
| 1143 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1144 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1145 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1146 | if (Attributes) |
| 1147 | PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1148 | ++Index; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1149 | } |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1150 | if (FuncAttrs) |
| 1151 | PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); |
| 1152 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1155 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 1156 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1157 | const FunctionArgList &Args) { |
Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 1158 | // FIXME: We no longer need the types from FunctionArgList; lift up |
| 1159 | // and simplify. |
| 1160 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1161 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 1162 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 1163 | |
| 1164 | // Name the struct return argument. |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1165 | if (CGM.ReturnTypeUsesSret(FI)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1166 | AI->setName("agg.result"); |
| 1167 | ++AI; |
| 1168 | } |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1169 | |
| 1170 | CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1171 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1172 | i != e; ++i, ++info_it) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1173 | const VarDecl *Arg = i->first; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1174 | QualType Ty = info_it->type; |
| 1175 | const ABIArgInfo &ArgI = info_it->info; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1176 | |
| 1177 | switch (ArgI.getKind()) { |
| 1178 | case ABIArgInfo::ByVal: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1179 | case ABIArgInfo::Direct: { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1180 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1181 | llvm::Value* V = AI; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1182 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1183 | // This must be a promotion, for something like |
| 1184 | // "void a(x) short x; {..." |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1185 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1186 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1187 | EmitParmDecl(*Arg, V); |
| 1188 | break; |
| 1189 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1190 | |
| 1191 | case ABIArgInfo::Expand: { |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1192 | // If this structure was expanded into multiple arguments then |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1193 | // we need to create a temporary and reconstruct it from the |
| 1194 | // arguments. |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1195 | std::string Name = Arg->getNameAsString(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1196 | llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty), |
| 1197 | (Name + ".addr").c_str()); |
| 1198 | // FIXME: What are the right qualifiers here? |
| 1199 | llvm::Function::arg_iterator End = |
| 1200 | ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI); |
| 1201 | EmitParmDecl(*Arg, Temp); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1202 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1203 | // Name the arguments used in expansion and increment AI. |
| 1204 | unsigned Index = 0; |
| 1205 | for (; AI != End; ++AI, ++Index) |
| 1206 | AI->setName(Name + "." + llvm::utostr(Index)); |
| 1207 | continue; |
| 1208 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1209 | |
| 1210 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 59e5a0e | 2009-02-03 20:00:13 +0000 | [diff] [blame^] | 1211 | // Skip increment, no matching LLVM parameter. |
| 1212 | continue; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1213 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1214 | case ABIArgInfo::Coerce: { |
| 1215 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1216 | // FIXME: This is very wasteful; EmitParmDecl is just going to |
| 1217 | // drop the result in a new alloca anyway, so we could just |
| 1218 | // store into that directly if we broke the abstraction down |
| 1219 | // more. |
| 1220 | llvm::Value *V = CreateTempAlloca(ConvertType(Ty), "coerce"); |
| 1221 | CreateCoercedStore(AI, V, *this); |
| 1222 | // Match to what EmitParmDecl is expecting for this type. |
| 1223 | if (!CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 1224 | V = Builder.CreateLoad(V); |
| 1225 | EmitParmDecl(*Arg, V); |
| 1226 | break; |
| 1227 | } |
| 1228 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1229 | case ABIArgInfo::StructRet: |
| 1230 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1231 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1232 | |
| 1233 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1234 | } |
| 1235 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 1236 | } |
| 1237 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1238 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1239 | llvm::Value *ReturnValue) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1240 | llvm::Value *RV = 0; |
| 1241 | |
| 1242 | // Functions with no result always return void. |
| 1243 | if (ReturnValue) { |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1244 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1245 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1246 | |
| 1247 | switch (RetAI.getKind()) { |
| 1248 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1249 | if (RetTy->isAnyComplexType()) { |
| 1250 | // FIXME: Volatile |
| 1251 | ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); |
| 1252 | StoreComplexToAddr(RT, CurFn->arg_begin(), false); |
| 1253 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1254 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
| 1255 | } else { |
| 1256 | Builder.CreateStore(Builder.CreateLoad(ReturnValue), |
| 1257 | CurFn->arg_begin()); |
| 1258 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1259 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1260 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1261 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1262 | RV = Builder.CreateLoad(ReturnValue); |
| 1263 | break; |
| 1264 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1265 | case ABIArgInfo::Ignore: |
| 1266 | break; |
| 1267 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1268 | case ABIArgInfo::Coerce: { |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 1269 | RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1270 | break; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1271 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1272 | |
| 1273 | case ABIArgInfo::ByVal: |
| 1274 | case ABIArgInfo::Expand: |
| 1275 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1276 | } |
| 1277 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1278 | |
| 1279 | if (RV) { |
| 1280 | Builder.CreateRet(RV); |
| 1281 | } else { |
| 1282 | Builder.CreateRetVoid(); |
| 1283 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1286 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
| 1287 | llvm::Value *Callee, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1288 | const CallArgList &CallArgs) { |
Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 1289 | // FIXME: We no longer need the types from CallArgs; lift up and |
| 1290 | // simplify. |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1291 | llvm::SmallVector<llvm::Value*, 16> Args; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Handle struct-return functions by passing a pointer to the |
| 1294 | // location that we would like to return into. |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1295 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1296 | const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1297 | switch (RetAI.getKind()) { |
| 1298 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1299 | // Create a temporary alloca to hold the result of the call. :( |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1300 | Args.push_back(CreateTempAlloca(ConvertType(RetTy))); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1301 | break; |
| 1302 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1303 | case ABIArgInfo::Direct: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1304 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1305 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1306 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1307 | |
| 1308 | case ABIArgInfo::ByVal: |
| 1309 | case ABIArgInfo::Expand: |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1310 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1313 | CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1314 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1315 | I != E; ++I, ++info_it) { |
| 1316 | const ABIArgInfo &ArgInfo = info_it->info; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1317 | RValue RV = I->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1318 | |
| 1319 | switch (ArgInfo.getKind()) { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1320 | case ABIArgInfo::ByVal: // Direct is byval |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1321 | case ABIArgInfo::Direct: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1322 | if (RV.isScalar()) { |
| 1323 | Args.push_back(RV.getScalarVal()); |
| 1324 | } else if (RV.isComplex()) { |
| 1325 | // Make a temporary alloca to pass the argument. |
| 1326 | Args.push_back(CreateTempAlloca(ConvertType(I->second))); |
| 1327 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
| 1328 | } else { |
| 1329 | Args.push_back(RV.getAggregateAddr()); |
| 1330 | } |
| 1331 | break; |
| 1332 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1333 | case ABIArgInfo::Ignore: |
| 1334 | break; |
| 1335 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1336 | case ABIArgInfo::Coerce: { |
| 1337 | // FIXME: Avoid the conversion through memory if possible. |
| 1338 | llvm::Value *SrcPtr; |
| 1339 | if (RV.isScalar()) { |
| 1340 | SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce"); |
| 1341 | Builder.CreateStore(RV.getScalarVal(), SrcPtr); |
| 1342 | } else if (RV.isComplex()) { |
| 1343 | SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce"); |
| 1344 | StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); |
| 1345 | } else |
| 1346 | SrcPtr = RV.getAggregateAddr(); |
| 1347 | Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), |
| 1348 | *this)); |
| 1349 | break; |
| 1350 | } |
| 1351 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1352 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1353 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1354 | break; |
| 1355 | |
| 1356 | case ABIArgInfo::Expand: |
| 1357 | ExpandTypeToArgs(I->second, RV, Args); |
| 1358 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1363 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1364 | // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set. |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1365 | CodeGen::AttributeListType AttributeList; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1366 | CGM.ConstructAttributeList(CallInfo, 0, AttributeList); |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1367 | CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1368 | AttributeList.size())); |
| 1369 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1370 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 1371 | CI->setCallingConv(F->getCallingConv()); |
| 1372 | if (CI->getType() != llvm::Type::VoidTy) |
| 1373 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1374 | |
| 1375 | switch (RetAI.getKind()) { |
| 1376 | case ABIArgInfo::StructRet: |
| 1377 | if (RetTy->isAnyComplexType()) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1378 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1379 | else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1380 | return RValue::getAggregate(Args[0]); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1381 | else |
| 1382 | return RValue::get(Builder.CreateLoad(Args[0])); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1383 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1384 | case ABIArgInfo::Direct: |
| 1385 | assert((!RetTy->isAnyComplexType() && |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1386 | !CodeGenFunction::hasAggregateLLVMType(RetTy)) && |
| 1387 | "FIXME: Implement return for non-scalar direct types."); |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1388 | return RValue::get(CI); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1389 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1390 | case ABIArgInfo::Ignore: |
Daniel Dunbar | cc039fe | 2009-01-29 08:24:57 +0000 | [diff] [blame] | 1391 | if (RetTy->isVoidType()) |
| 1392 | return RValue::get(0); |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1393 | |
| 1394 | // If we are ignoring an argument that had a result, make sure to |
| 1395 | // construct the appropriate return value for our caller. |
Daniel Dunbar | cc039fe | 2009-01-29 08:24:57 +0000 | [diff] [blame] | 1396 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1397 | llvm::Value *Res = |
| 1398 | llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy))); |
| 1399 | return RValue::getAggregate(Res); |
| 1400 | } |
| 1401 | return RValue::get(llvm::UndefValue::get(ConvertType(RetTy))); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1402 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1403 | case ABIArgInfo::Coerce: { |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame] | 1404 | // FIXME: Avoid the conversion through memory if possible. |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 1405 | llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce"); |
| 1406 | CreateCoercedStore(CI, V, *this); |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1407 | if (RetTy->isAnyComplexType()) |
| 1408 | return RValue::getComplex(LoadComplexFromAddr(V, false)); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1409 | else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1410 | return RValue::getAggregate(V); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1411 | else |
| 1412 | return RValue::get(Builder.CreateLoad(V)); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1413 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1414 | |
| 1415 | case ABIArgInfo::ByVal: |
| 1416 | case ABIArgInfo::Expand: |
| 1417 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1418 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1419 | |
| 1420 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 1421 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1422 | } |