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