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