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