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 | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 363 | public: |
Daniel Dunbar | 6bad265 | 2009-02-03 06:51:18 +0000 | [diff] [blame] | 364 | ABIArgInfo classifyReturnType(QualType RetTy, |
| 365 | ASTContext &Context) const; |
| 366 | |
| 367 | ABIArgInfo classifyArgumentType(QualType RetTy, |
| 368 | ASTContext &Context) const; |
| 369 | |
| 370 | virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const { |
| 371 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context); |
| 372 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 373 | it != ie; ++it) |
| 374 | it->info = classifyArgumentType(it->type, Context); |
| 375 | } |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 376 | }; |
| 377 | } |
| 378 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 379 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, |
| 380 | Class Field) const { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 381 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 382 | // classified recursively so that always two fields are |
| 383 | // considered. The resulting class is calculated according to |
| 384 | // the classes of the fields in the eightbyte: |
| 385 | // |
| 386 | // (a) If both classes are equal, this is the resulting class. |
| 387 | // |
| 388 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 389 | // the other class. |
| 390 | // |
| 391 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 392 | // class. |
| 393 | // |
| 394 | // (d) If one of the classes is INTEGER, the result is the |
| 395 | // INTEGER. |
| 396 | // |
| 397 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 398 | // MEMORY is used as class. |
| 399 | // |
| 400 | // (f) Otherwise class SSE is used. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 401 | assert((Accum == NoClass || Accum == Integer || |
| 402 | Accum == SSE || Accum == SSEUp) && |
| 403 | "Invalid accumulated classification during merge."); |
| 404 | if (Accum == Field || Field == NoClass) |
| 405 | return Accum; |
| 406 | else if (Field == Memory) |
| 407 | return Memory; |
| 408 | else if (Accum == NoClass) |
| 409 | return Field; |
| 410 | else if (Accum == Integer || Field == Integer) |
| 411 | return Integer; |
| 412 | else if (Field == X87 || Field == X87Up || Field == ComplexX87) |
| 413 | return Memory; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 414 | else |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 415 | return SSE; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 418 | void X86_64ABIInfo::classify(QualType Ty, |
| 419 | ASTContext &Context, |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 420 | uint64_t OffsetBase, |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 421 | Class &Lo, Class &Hi) const { |
Daniel Dunbar | 9a82b52 | 2009-02-02 18:06:39 +0000 | [diff] [blame] | 422 | // FIXME: This code can be simplified by introducing a simple value |
| 423 | // class for Class pairs with appropriate constructor methods for |
| 424 | // the various situations. |
| 425 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 426 | Lo = Hi = NoClass; |
| 427 | |
| 428 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 429 | Current = Memory; |
| 430 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 431 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) { |
| 432 | BuiltinType::Kind k = BT->getKind(); |
| 433 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 434 | if (k == BuiltinType::Void) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 435 | Current = NoClass; |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 436 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 437 | Current = Integer; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 438 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 439 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 440 | } else if (k == BuiltinType::LongDouble) { |
| 441 | Lo = X87; |
| 442 | Hi = X87Up; |
| 443 | } |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 444 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 445 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 446 | // FIXME: __int128 is (Integer, Integer). |
| 447 | } else if (Ty->isPointerLikeType() || Ty->isBlockPointerType() || |
| 448 | Ty->isObjCQualifiedInterfaceType()) { |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 449 | Current = Integer; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 450 | } else if (const VectorType *VT = Ty->getAsVectorType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 451 | uint64_t Size = Context.getTypeSize(VT); |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 452 | if (Size == 64) { |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 453 | // gcc passes <1 x double> in memory. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 454 | if (VT->getElementType() == Context.DoubleTy) |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 455 | return; |
Daniel Dunbar | d4cd1b0 | 2009-01-30 19:38:39 +0000 | [diff] [blame] | 456 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 457 | Current = SSE; |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 458 | |
| 459 | // If this type crosses an eightbyte boundary, it should be |
| 460 | // split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 461 | if (OffsetBase && OffsetBase != 64) |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 462 | Hi = Lo; |
Daniel Dunbar | 7a6605d | 2009-01-27 02:01:34 +0000 | [diff] [blame] | 463 | } else if (Size == 128) { |
| 464 | Lo = SSE; |
| 465 | Hi = SSEUp; |
| 466 | } |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 467 | } else if (const ComplexType *CT = Ty->getAsComplexType()) { |
| 468 | QualType ET = CT->getElementType(); |
| 469 | |
Daniel Dunbar | e33edf1 | 2009-01-30 18:40:10 +0000 | [diff] [blame] | 470 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 471 | if (ET->isIntegerType()) { |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 472 | if (Size <= 64) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 473 | Current = Integer; |
Daniel Dunbar | eac48dc | 2009-01-29 07:22:20 +0000 | [diff] [blame] | 474 | else if (Size <= 128) |
| 475 | Lo = Hi = Integer; |
| 476 | } else if (ET == Context.FloatTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 477 | Current = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 478 | else if (ET == Context.DoubleTy) |
| 479 | Lo = Hi = SSE; |
| 480 | else if (ET == Context.LongDoubleTy) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 481 | Current = ComplexX87; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 482 | |
| 483 | // If this complex type crosses an eightbyte boundary then it |
| 484 | // should be split. |
Daniel Dunbar | cdf920e | 2009-01-30 22:40:15 +0000 | [diff] [blame] | 485 | uint64_t EB_Real = (OffsetBase) / 64; |
| 486 | uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 487 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 488 | Hi = Lo; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 489 | } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 490 | // Arrays are treated like structures. |
| 491 | |
| 492 | uint64_t Size = Context.getTypeSize(Ty); |
| 493 | |
| 494 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 495 | // than two eightbytes, ..., it has class MEMORY. |
| 496 | if (Size > 128) |
| 497 | return; |
| 498 | |
| 499 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 500 | // fields, it has class MEMORY. |
| 501 | // |
| 502 | // Only need to check alignment of array base. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 503 | if (OffsetBase % Context.getTypeAlign(AT->getElementType())) |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 504 | return; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 505 | |
| 506 | // Otherwise implement simplified merge. We could be smarter about |
| 507 | // 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] | 508 | Current = NoClass; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 509 | uint64_t EltSize = Context.getTypeSize(AT->getElementType()); |
| 510 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
| 511 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 512 | Class FieldLo, FieldHi; |
| 513 | classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 514 | Lo = merge(Lo, FieldLo); |
| 515 | Hi = merge(Hi, FieldHi); |
| 516 | if (Lo == Memory || Hi == Memory) |
| 517 | break; |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 518 | } |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 519 | |
| 520 | // Do post merger cleanup (see below). Only case we worry about is Memory. |
| 521 | if (Hi == Memory) |
| 522 | Lo = Memory; |
| 523 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 524 | } else if (const RecordType *RT = Ty->getAsRecordType()) { |
Daniel Dunbar | e620ecd | 2009-01-30 00:47:38 +0000 | [diff] [blame] | 525 | uint64_t Size = Context.getTypeSize(Ty); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 526 | |
| 527 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
| 528 | // than two eightbytes, ..., it has class MEMORY. |
| 529 | if (Size > 128) |
| 530 | return; |
| 531 | |
| 532 | const RecordDecl *RD = RT->getDecl(); |
| 533 | |
| 534 | // Assume variable sized types are passed in memory. |
| 535 | if (RD->hasFlexibleArrayMember()) |
| 536 | return; |
| 537 | |
| 538 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 539 | |
| 540 | // Reset Lo class, this will be recomputed. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 541 | Current = NoClass; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 542 | unsigned idx = 0; |
| 543 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 544 | e = RD->field_end(); i != e; ++i, ++idx) { |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 545 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 546 | |
Daniel Dunbar | 8562ae7 | 2009-01-30 08:09:32 +0000 | [diff] [blame] | 547 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 548 | // fields, it has class MEMORY. |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 549 | if (Offset % Context.getTypeAlign(i->getType())) { |
| 550 | Lo = Memory; |
| 551 | return; |
| 552 | } |
| 553 | |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 554 | // Classify this field. |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 555 | // |
| 556 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 557 | // exceeds a single eightbyte, each is classified |
| 558 | // separately. Each eightbyte gets initialized to class |
| 559 | // NO_CLASS. |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 560 | Class FieldLo, FieldHi; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 561 | classify(i->getType(), Context, Offset, FieldLo, FieldHi); |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 562 | Lo = merge(Lo, FieldLo); |
| 563 | Hi = merge(Hi, FieldHi); |
| 564 | if (Lo == Memory || Hi == Memory) |
| 565 | break; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 569 | // |
| 570 | // (a) If one of the classes is MEMORY, the whole argument is |
| 571 | // passed in memory. |
| 572 | // |
| 573 | // (b) If SSEUP is not preceeded by SSE, it is converted to SSE. |
| 574 | |
| 575 | // The first of these conditions is guaranteed by how we implement |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 576 | // the merge (just bail). |
| 577 | // |
| 578 | // The second condition occurs in the case of unions; for example |
| 579 | // union { _Complex double; unsigned; }. |
| 580 | if (Hi == Memory) |
| 581 | Lo = Memory; |
Daniel Dunbar | 99037e5 | 2009-01-29 08:13:58 +0000 | [diff] [blame] | 582 | if (Hi == SSEUp && Lo != SSE) |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 583 | Hi = SSE; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 587 | |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 588 | ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy, |
| 589 | ASTContext &Context) const { |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 590 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 591 | // classification algorithm. |
| 592 | X86_64ABIInfo::Class Lo, Hi; |
Daniel Dunbar | f04d69b | 2009-01-29 09:42:07 +0000 | [diff] [blame] | 593 | classify(RetTy, Context, 0, Lo, Hi); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 594 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 595 | // Check some invariants. |
| 596 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
| 597 | assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification."); |
| 598 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 599 | |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 600 | const llvm::Type *ResType = 0; |
| 601 | switch (Lo) { |
| 602 | case NoClass: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 603 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 604 | |
| 605 | case SSEUp: |
| 606 | case X87Up: |
| 607 | assert(0 && "Invalid classification for lo word."); |
| 608 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 609 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 610 | // hidden argument, i.e. structret. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 611 | case Memory: |
| 612 | return ABIArgInfo::getStructRet(); |
| 613 | |
| 614 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 615 | // available register of the sequence %rax, %rdx is used. |
| 616 | case Integer: |
| 617 | ResType = llvm::Type::Int64Ty; break; |
| 618 | |
| 619 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 620 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 621 | case SSE: |
| 622 | ResType = llvm::Type::DoubleTy; break; |
| 623 | |
| 624 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 625 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 626 | case X87: |
| 627 | ResType = llvm::Type::X86_FP80Ty; break; |
| 628 | |
Daniel Dunbar | c450357 | 2009-01-31 00:06:58 +0000 | [diff] [blame] | 629 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 630 | // part of the value is returned in %st0 and the imaginary part in |
| 631 | // %st1. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 632 | case ComplexX87: |
| 633 | assert(Hi == NoClass && "Unexpected ComplexX87 classification."); |
| 634 | ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2); |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | switch (Hi) { |
| 639 | // Memory was handled previously, and ComplexX87 and X87 should |
| 640 | // never occur as hi classes. |
| 641 | case Memory: |
| 642 | case X87: |
| 643 | case ComplexX87: |
| 644 | assert(0 && "Invalid classification for hi word."); |
| 645 | |
| 646 | case NoClass: break; |
| 647 | case Integer: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 648 | ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL); |
| 649 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 650 | case SSE: |
Daniel Dunbar | b0e14f2 | 2009-01-29 07:36:07 +0000 | [diff] [blame] | 651 | ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL); |
| 652 | break; |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 653 | |
| 654 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
| 655 | // is passed in the upper half of the last used SSE register. |
| 656 | // |
| 657 | // SSEUP should always be preceeded by SSE, just widen. |
| 658 | case SSEUp: |
| 659 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
| 660 | ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2); |
| 661 | break; |
| 662 | |
| 663 | // 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] | 664 | // returned together with the previous X87 value in %st0. |
Daniel Dunbar | 6f3e7fa | 2009-01-24 08:32:22 +0000 | [diff] [blame] | 665 | // |
| 666 | // X87UP should always be preceeded by X87, so we don't need to do |
| 667 | // anything here. |
| 668 | case X87Up: |
| 669 | assert(Lo == X87 && "Unexpected X87Up classification."); |
| 670 | break; |
| 671 | } |
| 672 | |
| 673 | return ABIArgInfo::getCoerce(ResType); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, |
| 677 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 678 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
| 679 | return ABIArgInfo::getByVal(0); |
| 680 | } else { |
| 681 | return ABIArgInfo::getDirect(); |
| 682 | } |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 685 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy, |
| 686 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 687 | if (RetTy->isVoidType()) { |
| 688 | return ABIArgInfo::getIgnore(); |
| 689 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 690 | return ABIArgInfo::getStructRet(); |
| 691 | } else { |
| 692 | return ABIArgInfo::getDirect(); |
| 693 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty, |
| 697 | ASTContext &Context) const { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 698 | if (CodeGenFunction::hasAggregateLLVMType(Ty)) { |
| 699 | return ABIArgInfo::getByVal(0); |
| 700 | } else { |
| 701 | return ABIArgInfo::getDirect(); |
| 702 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | const ABIInfo &CodeGenTypes::getABIInfo() const { |
| 706 | if (TheABIInfo) |
| 707 | return *TheABIInfo; |
| 708 | |
| 709 | // For now we just cache this in the CodeGenTypes and don't bother |
| 710 | // to free it. |
| 711 | const char *TargetPrefix = getContext().Target.getTargetPrefix(); |
| 712 | if (strcmp(TargetPrefix, "x86") == 0) { |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 713 | switch (getContext().Target.getPointerWidth(0)) { |
| 714 | case 32: |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 715 | return *(TheABIInfo = new X86_32ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 716 | case 64: |
Daniel Dunbar | 11a76ed | 2009-01-30 18:47:53 +0000 | [diff] [blame] | 717 | return *(TheABIInfo = new X86_64ABIInfo()); |
Daniel Dunbar | d4edfe4 | 2009-01-15 18:18:40 +0000 | [diff] [blame] | 718 | } |
Daniel Dunbar | 6b1da0e | 2008-10-13 17:02:26 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | return *(TheABIInfo = new DefaultABIInfo); |
| 722 | } |
| 723 | |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 724 | /***/ |
| 725 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 726 | CGFunctionInfo::CGFunctionInfo(QualType ResTy, |
| 727 | const llvm::SmallVector<QualType, 16> &ArgTys) { |
| 728 | NumArgs = ArgTys.size(); |
| 729 | Args = new ArgInfo[1 + NumArgs]; |
| 730 | Args[0].type = ResTy; |
| 731 | for (unsigned i = 0; i < NumArgs; ++i) |
| 732 | Args[1 + i].type = ArgTys[i]; |
| 733 | } |
| 734 | |
| 735 | /***/ |
| 736 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 737 | void CodeGenTypes::GetExpandedTypes(QualType Ty, |
| 738 | std::vector<const llvm::Type*> &ArgTys) { |
| 739 | const RecordType *RT = Ty->getAsStructureType(); |
| 740 | assert(RT && "Can only expand structure types."); |
| 741 | const RecordDecl *RD = RT->getDecl(); |
| 742 | assert(!RD->hasFlexibleArrayMember() && |
| 743 | "Cannot expand structure with flexible array."); |
| 744 | |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 745 | for (RecordDecl::field_iterator i = RD->field_begin(), |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 746 | e = RD->field_end(); i != e; ++i) { |
| 747 | const FieldDecl *FD = *i; |
| 748 | assert(!FD->isBitField() && |
| 749 | "Cannot expand structure with bit-field members."); |
| 750 | |
| 751 | QualType FT = FD->getType(); |
| 752 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 753 | GetExpandedTypes(FT, ArgTys); |
| 754 | } else { |
| 755 | ArgTys.push_back(ConvertType(FT)); |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | llvm::Function::arg_iterator |
| 761 | CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, |
| 762 | llvm::Function::arg_iterator AI) { |
| 763 | const RecordType *RT = Ty->getAsStructureType(); |
| 764 | assert(RT && "Can only expand structure types."); |
| 765 | |
| 766 | RecordDecl *RD = RT->getDecl(); |
| 767 | assert(LV.isSimple() && |
| 768 | "Unexpected non-simple lvalue during struct expansion."); |
| 769 | llvm::Value *Addr = LV.getAddress(); |
| 770 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 771 | e = RD->field_end(); i != e; ++i) { |
| 772 | FieldDecl *FD = *i; |
| 773 | QualType FT = FD->getType(); |
| 774 | |
| 775 | // FIXME: What are the right qualifiers here? |
| 776 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 777 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 778 | AI = ExpandTypeFromArgs(FT, LV, AI); |
| 779 | } else { |
| 780 | EmitStoreThroughLValue(RValue::get(AI), LV, FT); |
| 781 | ++AI; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | return AI; |
| 786 | } |
| 787 | |
| 788 | void |
| 789 | CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, |
| 790 | llvm::SmallVector<llvm::Value*, 16> &Args) { |
| 791 | const RecordType *RT = Ty->getAsStructureType(); |
| 792 | assert(RT && "Can only expand structure types."); |
| 793 | |
| 794 | RecordDecl *RD = RT->getDecl(); |
| 795 | assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); |
| 796 | llvm::Value *Addr = RV.getAggregateAddr(); |
| 797 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 798 | e = RD->field_end(); i != e; ++i) { |
| 799 | FieldDecl *FD = *i; |
| 800 | QualType FT = FD->getType(); |
| 801 | |
| 802 | // FIXME: What are the right qualifiers here? |
| 803 | LValue LV = EmitLValueForField(Addr, FD, false, 0); |
| 804 | if (CodeGenFunction::hasAggregateLLVMType(FT)) { |
| 805 | ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); |
| 806 | } else { |
| 807 | RValue RV = EmitLoadOfLValue(LV, FT); |
| 808 | assert(RV.isScalar() && |
| 809 | "Unexpected non-scalar rvalue during struct expansion."); |
| 810 | Args.push_back(RV.getScalarVal()); |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 815 | /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as |
| 816 | /// a pointer to an object of type \arg Ty. |
| 817 | /// |
| 818 | /// This safely handles the case when the src type is smaller than the |
| 819 | /// destination type; in this situation the values of bits which not |
| 820 | /// present in the src are undefined. |
| 821 | static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, |
| 822 | const llvm::Type *Ty, |
| 823 | CodeGenFunction &CGF) { |
| 824 | const llvm::Type *SrcTy = |
| 825 | cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); |
| 826 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 827 | uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty); |
| 828 | |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 829 | // If load is legal, just bitcast the src pointer. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 830 | if (SrcSize == DstSize) { |
| 831 | llvm::Value *Casted = |
| 832 | CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); |
| 833 | return CGF.Builder.CreateLoad(Casted); |
| 834 | } else { |
| 835 | assert(SrcSize < DstSize && "Coercion is losing source bits!"); |
| 836 | |
| 837 | // Otherwise do coercion through memory. This is stupid, but |
| 838 | // simple. |
| 839 | llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); |
| 840 | llvm::Value *Casted = |
| 841 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); |
| 842 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); |
| 843 | return CGF.Builder.CreateLoad(Tmp); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, |
| 848 | /// where the source and destination may have different types. |
| 849 | /// |
| 850 | /// This safely handles the case when the src type is larger than the |
| 851 | /// destination type; the upper bits of the src will be lost. |
| 852 | static void CreateCoercedStore(llvm::Value *Src, |
| 853 | llvm::Value *DstPtr, |
| 854 | CodeGenFunction &CGF) { |
| 855 | const llvm::Type *SrcTy = Src->getType(); |
| 856 | const llvm::Type *DstTy = |
| 857 | cast<llvm::PointerType>(DstPtr->getType())->getElementType(); |
| 858 | |
| 859 | uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 860 | uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy); |
| 861 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 862 | // If store is legal, just bitcast the src pointer. |
Daniel Dunbar | 275e10d | 2009-02-02 19:06:38 +0000 | [diff] [blame] | 863 | if (SrcSize == DstSize) { |
| 864 | llvm::Value *Casted = |
| 865 | CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); |
| 866 | CGF.Builder.CreateStore(Src, Casted); |
| 867 | } else { |
| 868 | assert(SrcSize > DstSize && "Coercion is missing bits!"); |
| 869 | |
| 870 | // Otherwise do coercion through memory. This is stupid, but |
| 871 | // simple. |
| 872 | llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); |
| 873 | CGF.Builder.CreateStore(Src, Tmp); |
| 874 | llvm::Value *Casted = |
| 875 | CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); |
| 876 | CGF.Builder.CreateStore(CGF.Builder.CreateLoad(Casted), DstPtr); |
| 877 | } |
| 878 | } |
| 879 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 880 | /***/ |
| 881 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 882 | bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) { |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 883 | return FI.getReturnInfo().isStructRet(); |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 886 | const llvm::FunctionType * |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 887 | CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) { |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 888 | std::vector<const llvm::Type*> ArgTys; |
| 889 | |
| 890 | const llvm::Type *ResultType = 0; |
| 891 | |
Daniel Dunbar | a0a99e0 | 2009-02-02 23:43:58 +0000 | [diff] [blame] | 892 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 893 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 894 | switch (RetAI.getKind()) { |
| 895 | case ABIArgInfo::ByVal: |
| 896 | case ABIArgInfo::Expand: |
| 897 | assert(0 && "Invalid ABI kind for return argument"); |
| 898 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 899 | case ABIArgInfo::Direct: |
| 900 | ResultType = ConvertType(RetTy); |
| 901 | break; |
| 902 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 903 | case ABIArgInfo::StructRet: { |
| 904 | ResultType = llvm::Type::VoidTy; |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 905 | const llvm::Type *STy = ConvertType(RetTy); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 906 | ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace())); |
| 907 | break; |
| 908 | } |
| 909 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 910 | case ABIArgInfo::Ignore: |
| 911 | ResultType = llvm::Type::VoidTy; |
| 912 | break; |
| 913 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 914 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 915 | ResultType = RetAI.getCoerceToType(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 916 | break; |
| 917 | } |
| 918 | |
Daniel Dunbar | 88c2fa9 | 2009-02-03 05:31:23 +0000 | [diff] [blame] | 919 | for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), |
| 920 | ie = FI.arg_end(); it != ie; ++it) { |
| 921 | const ABIArgInfo &AI = it->info; |
| 922 | const llvm::Type *Ty = ConvertType(it->type); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 923 | |
| 924 | switch (AI.getKind()) { |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 925 | case ABIArgInfo::Ignore: |
| 926 | break; |
| 927 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 928 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame^] | 929 | ArgTys.push_back(AI.getCoerceToType()); |
| 930 | break; |
| 931 | |
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: |
| 1011 | assert(0 && "Invalid ABI kind for non-return argument"); |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame^] | 1012 | |
| 1013 | case ABIArgInfo::Coerce: |
| 1014 | break; |
| 1015 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1016 | case ABIArgInfo::ByVal: |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1017 | Attributes |= llvm::Attribute::ByVal; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1018 | assert(AI.getByValAlignment() == 0 && "FIXME: alignment unhandled"); |
| 1019 | break; |
| 1020 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1021 | case ABIArgInfo::Direct: |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1022 | if (ParamType->isPromotableIntegerType()) { |
| 1023 | if (ParamType->isSignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1024 | Attributes |= llvm::Attribute::SExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1025 | } else if (ParamType->isUnsignedIntegerType()) { |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1026 | Attributes |= llvm::Attribute::ZExt; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1027 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1028 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1029 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1030 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1031 | case ABIArgInfo::Ignore: |
| 1032 | // Skip increment, no matching LLVM parameter. |
| 1033 | continue; |
| 1034 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1035 | case ABIArgInfo::Expand: { |
| 1036 | std::vector<const llvm::Type*> Tys; |
| 1037 | // FIXME: This is rather inefficient. Do we ever actually need |
| 1038 | // to do anything here? The result should be just reconstructed |
| 1039 | // on the other side, so extension should be a non-issue. |
| 1040 | getTypes().GetExpandedTypes(ParamType, Tys); |
| 1041 | Index += Tys.size(); |
| 1042 | continue; |
| 1043 | } |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1044 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1045 | |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1046 | if (Attributes) |
| 1047 | PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1048 | ++Index; |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1049 | } |
Devang Patel | a2c6912 | 2008-09-26 22:53:57 +0000 | [diff] [blame] | 1050 | if (FuncAttrs) |
| 1051 | PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); |
| 1052 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1055 | void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, |
| 1056 | llvm::Function *Fn, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1057 | const FunctionArgList &Args) { |
Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 1058 | // FIXME: We no longer need the types from FunctionArgList; lift up |
| 1059 | // and simplify. |
| 1060 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1061 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 1062 | llvm::Function::arg_iterator AI = Fn->arg_begin(); |
| 1063 | |
| 1064 | // Name the struct return argument. |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1065 | if (CGM.ReturnTypeUsesSret(FI)) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1066 | AI->setName("agg.result"); |
| 1067 | ++AI; |
| 1068 | } |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1069 | |
| 1070 | CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1071 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1072 | i != e; ++i, ++info_it) { |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1073 | const VarDecl *Arg = i->first; |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1074 | QualType Ty = info_it->type; |
| 1075 | const ABIArgInfo &ArgI = info_it->info; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1076 | |
| 1077 | switch (ArgI.getKind()) { |
| 1078 | case ABIArgInfo::ByVal: |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1079 | case ABIArgInfo::Direct: { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1080 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1081 | llvm::Value* V = AI; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1082 | if (!getContext().typesAreCompatible(Ty, Arg->getType())) { |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1083 | // This must be a promotion, for something like |
| 1084 | // "void a(x) short x; {..." |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1085 | V = EmitScalarConversion(V, Ty, Arg->getType()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1086 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1087 | EmitParmDecl(*Arg, V); |
| 1088 | break; |
| 1089 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1090 | |
| 1091 | case ABIArgInfo::Expand: { |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1092 | // If this structure was expanded into multiple arguments then |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1093 | // we need to create a temporary and reconstruct it from the |
| 1094 | // arguments. |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 1095 | std::string Name = Arg->getNameAsString(); |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1096 | llvm::Value *Temp = CreateTempAlloca(ConvertType(Ty), |
| 1097 | (Name + ".addr").c_str()); |
| 1098 | // FIXME: What are the right qualifiers here? |
| 1099 | llvm::Function::arg_iterator End = |
| 1100 | ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI); |
| 1101 | EmitParmDecl(*Arg, Temp); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1102 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1103 | // Name the arguments used in expansion and increment AI. |
| 1104 | unsigned Index = 0; |
| 1105 | for (; AI != End; ++AI, ++Index) |
| 1106 | AI->setName(Name + "." + llvm::utostr(Index)); |
| 1107 | continue; |
| 1108 | } |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1109 | |
| 1110 | case ABIArgInfo::Ignore: |
| 1111 | break; |
| 1112 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame^] | 1113 | case ABIArgInfo::Coerce: { |
| 1114 | assert(AI != Fn->arg_end() && "Argument mismatch!"); |
| 1115 | // FIXME: This is very wasteful; EmitParmDecl is just going to |
| 1116 | // drop the result in a new alloca anyway, so we could just |
| 1117 | // store into that directly if we broke the abstraction down |
| 1118 | // more. |
| 1119 | llvm::Value *V = CreateTempAlloca(ConvertType(Ty), "coerce"); |
| 1120 | CreateCoercedStore(AI, V, *this); |
| 1121 | // Match to what EmitParmDecl is expecting for this type. |
| 1122 | if (!CodeGenFunction::hasAggregateLLVMType(Ty)) |
| 1123 | V = Builder.CreateLoad(V); |
| 1124 | EmitParmDecl(*Arg, V); |
| 1125 | break; |
| 1126 | } |
| 1127 | |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1128 | case ABIArgInfo::StructRet: |
| 1129 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1130 | } |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1131 | |
| 1132 | ++AI; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1133 | } |
| 1134 | assert(AI == Fn->arg_end() && "Argument mismatch!"); |
| 1135 | } |
| 1136 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1137 | void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1138 | llvm::Value *ReturnValue) { |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1139 | llvm::Value *RV = 0; |
| 1140 | |
| 1141 | // Functions with no result always return void. |
| 1142 | if (ReturnValue) { |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1143 | QualType RetTy = FI.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1144 | const ABIArgInfo &RetAI = FI.getReturnInfo(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1145 | |
| 1146 | switch (RetAI.getKind()) { |
| 1147 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1148 | if (RetTy->isAnyComplexType()) { |
| 1149 | // FIXME: Volatile |
| 1150 | ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); |
| 1151 | StoreComplexToAddr(RT, CurFn->arg_begin(), false); |
| 1152 | } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1153 | EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy); |
| 1154 | } else { |
| 1155 | Builder.CreateStore(Builder.CreateLoad(ReturnValue), |
| 1156 | CurFn->arg_begin()); |
| 1157 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1158 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1159 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1160 | case ABIArgInfo::Direct: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1161 | RV = Builder.CreateLoad(ReturnValue); |
| 1162 | break; |
| 1163 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1164 | case ABIArgInfo::Ignore: |
| 1165 | break; |
| 1166 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1167 | case ABIArgInfo::Coerce: { |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 1168 | RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1169 | break; |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1170 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1171 | |
| 1172 | case ABIArgInfo::ByVal: |
| 1173 | case ABIArgInfo::Expand: |
| 1174 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1175 | } |
| 1176 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1177 | |
| 1178 | if (RV) { |
| 1179 | Builder.CreateRet(RV); |
| 1180 | } else { |
| 1181 | Builder.CreateRetVoid(); |
| 1182 | } |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1185 | RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, |
| 1186 | llvm::Value *Callee, |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1187 | const CallArgList &CallArgs) { |
Daniel Dunbar | 5251afa | 2009-02-03 06:02:10 +0000 | [diff] [blame] | 1188 | // FIXME: We no longer need the types from CallArgs; lift up and |
| 1189 | // simplify. |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1190 | llvm::SmallVector<llvm::Value*, 16> Args; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1191 | |
| 1192 | // Handle struct-return functions by passing a pointer to the |
| 1193 | // location that we would like to return into. |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1194 | QualType RetTy = CallInfo.getReturnType(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1195 | const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1196 | switch (RetAI.getKind()) { |
| 1197 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1198 | // Create a temporary alloca to hold the result of the call. :( |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1199 | Args.push_back(CreateTempAlloca(ConvertType(RetTy))); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1200 | break; |
| 1201 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1202 | case ABIArgInfo::Direct: |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1203 | case ABIArgInfo::Ignore: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1204 | case ABIArgInfo::Coerce: |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1205 | break; |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1206 | |
| 1207 | case ABIArgInfo::ByVal: |
| 1208 | case ABIArgInfo::Expand: |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1209 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1212 | CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1213 | for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); |
Daniel Dunbar | b225be4 | 2009-02-03 05:59:18 +0000 | [diff] [blame] | 1214 | I != E; ++I, ++info_it) { |
| 1215 | const ABIArgInfo &ArgInfo = info_it->info; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1216 | RValue RV = I->first; |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1217 | |
| 1218 | switch (ArgInfo.getKind()) { |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1219 | case ABIArgInfo::ByVal: // Direct is byval |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1220 | case ABIArgInfo::Direct: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1221 | if (RV.isScalar()) { |
| 1222 | Args.push_back(RV.getScalarVal()); |
| 1223 | } else if (RV.isComplex()) { |
| 1224 | // Make a temporary alloca to pass the argument. |
| 1225 | Args.push_back(CreateTempAlloca(ConvertType(I->second))); |
| 1226 | StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); |
| 1227 | } else { |
| 1228 | Args.push_back(RV.getAggregateAddr()); |
| 1229 | } |
| 1230 | break; |
| 1231 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1232 | case ABIArgInfo::Ignore: |
| 1233 | break; |
| 1234 | |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame^] | 1235 | case ABIArgInfo::Coerce: { |
| 1236 | // FIXME: Avoid the conversion through memory if possible. |
| 1237 | llvm::Value *SrcPtr; |
| 1238 | if (RV.isScalar()) { |
| 1239 | SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce"); |
| 1240 | Builder.CreateStore(RV.getScalarVal(), SrcPtr); |
| 1241 | } else if (RV.isComplex()) { |
| 1242 | SrcPtr = CreateTempAlloca(ConvertType(I->second), "coerce"); |
| 1243 | StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); |
| 1244 | } else |
| 1245 | SrcPtr = RV.getAggregateAddr(); |
| 1246 | Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), |
| 1247 | *this)); |
| 1248 | break; |
| 1249 | } |
| 1250 | |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1251 | case ABIArgInfo::StructRet: |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1252 | assert(0 && "Invalid ABI kind for non-return argument"); |
| 1253 | break; |
| 1254 | |
| 1255 | case ABIArgInfo::Expand: |
| 1256 | ExpandTypeToArgs(I->second, RV, Args); |
| 1257 | break; |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size()); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1262 | |
Daniel Dunbar | 5323a4b | 2008-09-10 00:32:18 +0000 | [diff] [blame] | 1263 | // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set. |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1264 | CodeGen::AttributeListType AttributeList; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1265 | CGM.ConstructAttributeList(CallInfo, 0, AttributeList); |
Devang Patel | 761d7f7 | 2008-09-25 21:02:23 +0000 | [diff] [blame] | 1266 | CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), |
Daniel Dunbar | 725ad31 | 2009-01-31 02:19:00 +0000 | [diff] [blame] | 1267 | AttributeList.size())); |
| 1268 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1269 | if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
| 1270 | CI->setCallingConv(F->getCallingConv()); |
| 1271 | if (CI->getType() != llvm::Type::VoidTy) |
| 1272 | CI->setName("call"); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1273 | |
| 1274 | switch (RetAI.getKind()) { |
| 1275 | case ABIArgInfo::StructRet: |
| 1276 | if (RetTy->isAnyComplexType()) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1277 | return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1278 | else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Daniel Dunbar | 5627377 | 2008-09-17 00:51:38 +0000 | [diff] [blame] | 1279 | return RValue::getAggregate(Args[0]); |
Daniel Dunbar | 3aea8ca | 2008-12-18 04:52:14 +0000 | [diff] [blame] | 1280 | else |
| 1281 | return RValue::get(Builder.CreateLoad(Args[0])); |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1282 | |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1283 | case ABIArgInfo::Direct: |
| 1284 | assert((!RetTy->isAnyComplexType() && |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1285 | !CodeGenFunction::hasAggregateLLVMType(RetTy)) && |
| 1286 | "FIXME: Implement return for non-scalar direct types."); |
Daniel Dunbar | 46327aa | 2009-02-03 06:17:37 +0000 | [diff] [blame] | 1287 | return RValue::get(CI); |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1288 | |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1289 | case ABIArgInfo::Ignore: |
Daniel Dunbar | cc039fe | 2009-01-29 08:24:57 +0000 | [diff] [blame] | 1290 | if (RetTy->isVoidType()) |
| 1291 | return RValue::get(0); |
Daniel Dunbar | 0bcc521 | 2009-02-03 06:30:17 +0000 | [diff] [blame] | 1292 | |
| 1293 | // If we are ignoring an argument that had a result, make sure to |
| 1294 | // construct the appropriate return value for our caller. |
Daniel Dunbar | cc039fe | 2009-01-29 08:24:57 +0000 | [diff] [blame] | 1295 | if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { |
| 1296 | llvm::Value *Res = |
| 1297 | llvm::UndefValue::get(llvm::PointerType::getUnqual(ConvertType(RetTy))); |
| 1298 | return RValue::getAggregate(Res); |
| 1299 | } |
| 1300 | return RValue::get(llvm::UndefValue::get(ConvertType(RetTy))); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1301 | |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1302 | case ABIArgInfo::Coerce: { |
Daniel Dunbar | 89c9d8e | 2009-02-03 19:12:28 +0000 | [diff] [blame^] | 1303 | // FIXME: Avoid the conversion through memory if possible. |
Daniel Dunbar | 54d1ccb | 2009-01-27 01:36:03 +0000 | [diff] [blame] | 1304 | llvm::Value *V = CreateTempAlloca(ConvertType(RetTy), "coerce"); |
| 1305 | CreateCoercedStore(CI, V, *this); |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1306 | if (RetTy->isAnyComplexType()) |
| 1307 | return RValue::getComplex(LoadComplexFromAddr(V, false)); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1308 | else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) |
Anders Carlsson | ad3d691 | 2008-11-25 22:21:48 +0000 | [diff] [blame] | 1309 | return RValue::getAggregate(V); |
Daniel Dunbar | 1143492 | 2009-01-26 21:26:08 +0000 | [diff] [blame] | 1310 | else |
| 1311 | return RValue::get(Builder.CreateLoad(V)); |
Daniel Dunbar | 639ffe4 | 2008-09-10 07:04:09 +0000 | [diff] [blame] | 1312 | } |
Daniel Dunbar | 8951dbd | 2008-09-11 01:48:57 +0000 | [diff] [blame] | 1313 | |
| 1314 | case ABIArgInfo::ByVal: |
| 1315 | case ABIArgInfo::Expand: |
| 1316 | assert(0 && "Invalid ABI kind for return argument"); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1317 | } |
Daniel Dunbar | 2c8e0f3 | 2008-09-10 02:41:04 +0000 | [diff] [blame] | 1318 | |
| 1319 | assert(0 && "Unhandled ABIArgInfo::Kind"); |
| 1320 | return RValue::get(0); |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 1321 | } |