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