Chris Lattner | 566b6ce | 2007-08-24 02:22:53 +0000 | [diff] [blame] | 1 | //===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===// |
Chris Lattner | af6f528 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | af6f528 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Aggregate Expr nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 15 | #include "CodeGenModule.h" |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 16 | #include "CGObjCRuntime.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/Function.h" |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 22 | #include "llvm/GlobalVariable.h" |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
Chris Lattner | af6f528 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | // Aggregate Expression Emitter |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 32 | class AggExprEmitter : public StmtVisitor<AggExprEmitter> { |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 33 | CodeGenFunction &CGF; |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 34 | CGBuilderTy &Builder; |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 35 | llvm::Value *DestPtr; |
| 36 | bool VolatileDest; |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 37 | bool IgnoreResult; |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 38 | bool IsInitializer; |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 39 | bool RequiresGCollection; |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 40 | |
| 41 | ReturnValueSlot getReturnValueSlot() const { |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 42 | // If the destination slot requires garbage collection, we can't |
| 43 | // use the real return value slot, because we have to use the GC |
| 44 | // API. |
| 45 | if (RequiresGCollection) return ReturnValueSlot(); |
| 46 | |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 47 | return ReturnValueSlot(DestPtr, VolatileDest); |
| 48 | } |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 50 | public: |
Mike Stump | ff4bf3b | 2009-05-27 01:42:21 +0000 | [diff] [blame] | 51 | AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 52 | bool ignore, bool isinit, bool requiresGCollection) |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 53 | : CGF(cgf), Builder(CGF.Builder), |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 54 | DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore), |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 55 | IsInitializer(isinit), RequiresGCollection(requiresGCollection) { |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 58 | //===--------------------------------------------------------------------===// |
| 59 | // Utilities |
| 60 | //===--------------------------------------------------------------------===// |
| 61 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 62 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 63 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 64 | /// then loads the result into DestPtr. |
| 65 | void EmitAggLoadOfLValue(const Expr *E); |
Eli Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 66 | |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 67 | /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 68 | void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false); |
| 69 | void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 70 | |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 71 | void EmitGCMove(const Expr *E, RValue Src); |
| 72 | |
| 73 | bool TypeRequiresGCollection(QualType T); |
| 74 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 75 | //===--------------------------------------------------------------------===// |
| 76 | // Visitor Methods |
| 77 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 79 | void VisitStmt(Stmt *S) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 80 | CGF.ErrorUnsupported(S, "aggregate expression"); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 81 | } |
| 82 | void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } |
Eli Friedman | 12444a2 | 2009-01-27 09:03:41 +0000 | [diff] [blame] | 83 | void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 84 | |
| 85 | // l-values. |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 86 | void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } |
| 87 | void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } |
| 88 | void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } |
Daniel Dunbar | 5be028f | 2010-01-04 18:47:06 +0000 | [diff] [blame] | 89 | void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 90 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | EmitAggLoadOfLValue(E); |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 92 | } |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 93 | void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 94 | EmitAggLoadOfLValue(E); |
| 95 | } |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 96 | void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | EmitAggLoadOfLValue(E); |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 98 | } |
| 99 | void VisitPredefinedExpr(const PredefinedExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | EmitAggLoadOfLValue(E); |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 101 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 103 | // Operators. |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 104 | void VisitCastExpr(CastExpr *E); |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 105 | void VisitCallExpr(const CallExpr *E); |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 106 | void VisitStmtExpr(const StmtExpr *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 107 | void VisitBinaryOperator(const BinaryOperator *BO); |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 108 | void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO); |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 109 | void VisitBinAssign(const BinaryOperator *E); |
Eli Friedman | 07fa52a | 2008-05-20 07:56:31 +0000 | [diff] [blame] | 110 | void VisitBinComma(const BinaryOperator *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 112 | void VisitObjCMessageExpr(ObjCMessageExpr *E); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 113 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
| 114 | EmitAggLoadOfLValue(E); |
| 115 | } |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 116 | void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 117 | void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 119 | void VisitConditionalOperator(const ConditionalOperator *CO); |
Anders Carlsson | a294ca8 | 2009-07-08 18:33:14 +0000 | [diff] [blame] | 120 | void VisitChooseExpr(const ChooseExpr *CE); |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 121 | void VisitInitListExpr(InitListExpr *E); |
Anders Carlsson | 30311fa | 2009-12-16 06:57:54 +0000 | [diff] [blame] | 122 | void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 123 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 124 | Visit(DAE->getExpr()); |
| 125 | } |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 126 | void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 127 | void VisitCXXConstructExpr(const CXXConstructExpr *E); |
Anders Carlsson | 7f6ad15 | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 128 | void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E); |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 129 | void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); |
Mike Stump | 2710c41 | 2009-11-18 00:40:12 +0000 | [diff] [blame] | 130 | void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); } |
Anders Carlsson | 7f6ad15 | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 131 | |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 132 | void VisitVAArgExpr(VAArgExpr *E); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 133 | |
Anders Carlsson | 78e83f8 | 2010-02-03 17:33:16 +0000 | [diff] [blame] | 134 | void EmitInitializationToLValue(Expr *E, LValue Address, QualType T); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 135 | void EmitNullInitializationToLValue(LValue Address, QualType T); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 136 | // case Expr::ChooseExprClass: |
Mike Stump | 39406b1 | 2009-12-09 19:24:08 +0000 | [diff] [blame] | 137 | void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); } |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 138 | }; |
| 139 | } // end anonymous namespace. |
| 140 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 141 | //===----------------------------------------------------------------------===// |
| 142 | // Utilities |
| 143 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 145 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 146 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 147 | /// then loads the result into DestPtr. |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 148 | void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { |
| 149 | LValue LV = CGF.EmitLValue(E); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 150 | EmitFinalDestCopy(E, LV); |
| 151 | } |
| 152 | |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 153 | /// \brief True if the given aggregate type requires special GC API calls. |
| 154 | bool AggExprEmitter::TypeRequiresGCollection(QualType T) { |
| 155 | // Only record types have members that might require garbage collection. |
| 156 | const RecordType *RecordTy = T->getAs<RecordType>(); |
| 157 | if (!RecordTy) return false; |
| 158 | |
| 159 | // Don't mess with non-trivial C++ types. |
| 160 | RecordDecl *Record = RecordTy->getDecl(); |
| 161 | if (isa<CXXRecordDecl>(Record) && |
| 162 | (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() || |
| 163 | !cast<CXXRecordDecl>(Record)->hasTrivialDestructor())) |
| 164 | return false; |
| 165 | |
| 166 | // Check whether the type has an object member. |
| 167 | return Record->hasObjectMember(); |
| 168 | } |
| 169 | |
| 170 | /// \brief Perform the final move to DestPtr if RequiresGCollection is set. |
| 171 | /// |
| 172 | /// The idea is that you do something like this: |
| 173 | /// RValue Result = EmitSomething(..., getReturnValueSlot()); |
| 174 | /// EmitGCMove(E, Result); |
| 175 | /// If GC doesn't interfere, this will cause the result to be emitted |
| 176 | /// directly into the return value slot. If GC does interfere, a final |
| 177 | /// move will be performed. |
| 178 | void AggExprEmitter::EmitGCMove(const Expr *E, RValue Src) { |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 179 | if (RequiresGCollection) { |
| 180 | std::pair<uint64_t, unsigned> TypeInfo = |
| 181 | CGF.getContext().getTypeInfo(E->getType()); |
| 182 | unsigned long size = TypeInfo.first/8; |
| 183 | const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); |
| 184 | llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); |
| 185 | CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, DestPtr, |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 186 | Src.getAggregateAddr(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 187 | SizeVal); |
| 188 | } |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 191 | /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 192 | void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) { |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 193 | assert(Src.isAggregate() && "value must be aggregate value!"); |
| 194 | |
John McCall | a8f28da | 2010-08-25 02:50:31 +0000 | [diff] [blame^] | 195 | // If DestPtr is null, then we're evaluating an aggregate expression |
| 196 | // in a context (like an expression statement) that doesn't care |
| 197 | // about the result. C says that an lvalue-to-rvalue conversion is |
| 198 | // performed in these cases; C++ says that it is not. In either |
| 199 | // case, we don't actually need to do anything unless the value is |
| 200 | // volatile. |
Mike Stump | 9ccb103 | 2009-05-23 22:01:27 +0000 | [diff] [blame] | 201 | if (DestPtr == 0) { |
John McCall | a8f28da | 2010-08-25 02:50:31 +0000 | [diff] [blame^] | 202 | if (!Src.isVolatileQualified() || |
| 203 | CGF.CGM.getLangOptions().CPlusPlus || |
| 204 | (IgnoreResult && Ignore)) |
Mike Stump | 9ccb103 | 2009-05-23 22:01:27 +0000 | [diff] [blame] | 205 | return; |
John McCall | a8f28da | 2010-08-25 02:50:31 +0000 | [diff] [blame^] | 206 | |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 207 | // If the source is volatile, we must read from it; to do that, we need |
| 208 | // some place to put it. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 209 | DestPtr = CGF.CreateMemTemp(E->getType(), "agg.tmp"); |
Mike Stump | 9ccb103 | 2009-05-23 22:01:27 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 211 | |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 212 | if (RequiresGCollection) { |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 213 | std::pair<uint64_t, unsigned> TypeInfo = |
| 214 | CGF.getContext().getTypeInfo(E->getType()); |
| 215 | unsigned long size = TypeInfo.first/8; |
| 216 | const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType()); |
| 217 | llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 218 | CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, |
| 219 | DestPtr, Src.getAggregateAddr(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 220 | SizeVal); |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 221 | return; |
| 222 | } |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 223 | // If the result of the assignment is used, copy the LHS there also. |
| 224 | // FIXME: Pass VolatileDest as well. I think we also need to merge volatile |
| 225 | // from the source as well, as we can't eliminate it if either operand |
| 226 | // is volatile, unless copy has volatile for both source and destination.. |
Mike Stump | 27fe2e6 | 2009-05-23 22:29:41 +0000 | [diff] [blame] | 227 | CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(), |
| 228 | VolatileDest|Src.isVolatileQualified()); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 232 | void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 233 | assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc"); |
| 234 | |
| 235 | EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(), |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 236 | Src.isVolatileQualified()), |
| 237 | Ignore); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 240 | //===----------------------------------------------------------------------===// |
| 241 | // Visitor Methods |
| 242 | //===----------------------------------------------------------------------===// |
| 243 | |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 244 | void AggExprEmitter::VisitCastExpr(CastExpr *E) { |
Douglas Gregor | 69cfeb1 | 2010-05-14 21:31:02 +0000 | [diff] [blame] | 245 | if (!DestPtr && E->getCastKind() != CastExpr::CK_Dynamic) { |
Douglas Gregor | 4ce46c2 | 2010-03-07 23:24:59 +0000 | [diff] [blame] | 246 | Visit(E->getSubExpr()); |
| 247 | return; |
| 248 | } |
| 249 | |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame] | 250 | switch (E->getCastKind()) { |
| 251 | default: assert(0 && "Unhandled cast kind!"); |
| 252 | |
Douglas Gregor | 69cfeb1 | 2010-05-14 21:31:02 +0000 | [diff] [blame] | 253 | case CastExpr::CK_Dynamic: { |
| 254 | assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?"); |
| 255 | LValue LV = CGF.EmitCheckedLValue(E->getSubExpr()); |
| 256 | // FIXME: Do we also need to handle property references here? |
| 257 | if (LV.isSimple()) |
| 258 | CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E)); |
| 259 | else |
| 260 | CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast"); |
| 261 | |
| 262 | if (DestPtr) |
| 263 | CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination"); |
| 264 | break; |
| 265 | } |
| 266 | |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame] | 267 | case CastExpr::CK_ToUnion: { |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 268 | // GCC union extension |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 269 | QualType Ty = E->getSubExpr()->getType(); |
| 270 | QualType PtrTy = CGF.getContext().getPointerType(Ty); |
Eli Friedman | 34ebf4d | 2009-06-03 20:45:06 +0000 | [diff] [blame] | 271 | llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr, |
| 272 | CGF.ConvertType(PtrTy)); |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 273 | EmitInitializationToLValue(E->getSubExpr(), CGF.MakeAddrLValue(CastPtr, Ty), |
| 274 | Ty); |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame] | 275 | break; |
Nuno Lopes | 7e91627 | 2009-01-15 20:14:33 +0000 | [diff] [blame] | 276 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 278 | case CastExpr::CK_DerivedToBase: |
| 279 | case CastExpr::CK_BaseToDerived: |
| 280 | case CastExpr::CK_UncheckedDerivedToBase: { |
| 281 | assert(0 && "cannot perform hierarchy conversion in EmitAggExpr: " |
| 282 | "should have been unpacked before we got here"); |
| 283 | break; |
| 284 | } |
| 285 | |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 286 | // FIXME: Remove the CK_Unknown check here. |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame] | 287 | case CastExpr::CK_Unknown: |
| 288 | case CastExpr::CK_NoOp: |
| 289 | case CastExpr::CK_UserDefinedConversion: |
| 290 | case CastExpr::CK_ConstructorConversion: |
| 291 | assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), |
| 292 | E->getType()) && |
| 293 | "Implicit cast types must be compatible"); |
| 294 | Visit(E->getSubExpr()); |
| 295 | break; |
Anders Carlsson | e9d34dc | 2009-09-29 02:09:01 +0000 | [diff] [blame] | 296 | |
Douglas Gregor | e39a389 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 297 | case CastExpr::CK_LValueBitCast: |
| 298 | llvm_unreachable("there are no lvalue bit-casts on aggregates"); |
| 299 | break; |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame] | 300 | } |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 303 | void AggExprEmitter::VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | e70e8f7 | 2009-05-27 16:45:02 +0000 | [diff] [blame] | 304 | if (E->getCallReturnType()->isReferenceType()) { |
| 305 | EmitAggLoadOfLValue(E); |
| 306 | return; |
| 307 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 309 | RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot()); |
| 310 | EmitGCMove(E, RV); |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 312 | |
| 313 | void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 314 | RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot()); |
| 315 | EmitGCMove(E, RV); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 316 | } |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 317 | |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 318 | void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 319 | RValue RV = CGF.EmitObjCPropertyGet(E, getReturnValueSlot()); |
| 320 | EmitGCMove(E, RV); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 323 | void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr( |
| 324 | ObjCImplicitSetterGetterRefExpr *E) { |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 325 | RValue RV = CGF.EmitObjCPropertyGet(E, getReturnValueSlot()); |
| 326 | EmitGCMove(E, RV); |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 329 | void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 330 | CGF.EmitAnyExpr(E->getLHS(), 0, false, true); |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 331 | CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest, |
| 332 | /*IgnoreResult=*/false, IsInitializer); |
Eli Friedman | 07fa52a | 2008-05-20 07:56:31 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 335 | void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
| 336 | CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest); |
| 337 | } |
| 338 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 339 | void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { |
Fariborz Jahanian | 52f08bc | 2009-10-26 21:58:25 +0000 | [diff] [blame] | 340 | if (E->getOpcode() == BinaryOperator::PtrMemD || |
| 341 | E->getOpcode() == BinaryOperator::PtrMemI) |
Fariborz Jahanian | 8bfd31f | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 342 | VisitPointerToDataMemberBinaryOperator(E); |
| 343 | else |
| 344 | CGF.ErrorUnsupported(E, "aggregate binary expression"); |
| 345 | } |
| 346 | |
| 347 | void AggExprEmitter::VisitPointerToDataMemberBinaryOperator( |
| 348 | const BinaryOperator *E) { |
| 349 | LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E); |
| 350 | EmitFinalDestCopy(E, LV); |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 353 | void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 354 | // For an assignment to work, the value on the right has |
| 355 | // to be compatible with the value on the left. |
Eli Friedman | 2dce5f8 | 2009-05-28 23:04:00 +0000 | [diff] [blame] | 356 | assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), |
| 357 | E->getRHS()->getType()) |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 358 | && "Invalid assignment"); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 359 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 360 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 361 | // We have to special case property setters, otherwise we must have |
| 362 | // a simple lvalue (no aggregates inside vectors, bitfields). |
| 363 | if (LHS.isPropertyRef()) { |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 364 | llvm::Value *AggLoc = DestPtr; |
| 365 | if (!AggLoc) |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 366 | AggLoc = CGF.CreateMemTemp(E->getRHS()->getType()); |
Mike Stump | 240993d | 2009-05-23 23:48:13 +0000 | [diff] [blame] | 367 | CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), |
Mike Stump | 240993d | 2009-05-23 23:48:13 +0000 | [diff] [blame] | 369 | RValue::getAggregate(AggLoc, VolatileDest)); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 370 | } else if (LHS.isKVCRef()) { |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 371 | llvm::Value *AggLoc = DestPtr; |
| 372 | if (!AggLoc) |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 373 | AggLoc = CGF.CreateMemTemp(E->getRHS()->getType()); |
Mike Stump | a49af1a | 2009-05-23 23:52:31 +0000 | [diff] [blame] | 374 | CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), |
Mike Stump | a49af1a | 2009-05-23 23:52:31 +0000 | [diff] [blame] | 376 | RValue::getAggregate(AggLoc, VolatileDest)); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 377 | } else { |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 378 | bool RequiresGCollection = false; |
John McCall | fa037bd | 2010-05-22 22:13:32 +0000 | [diff] [blame] | 379 | if (CGF.getContext().getLangOptions().getGCMode()) |
| 380 | RequiresGCollection = TypeRequiresGCollection(E->getLHS()->getType()); |
| 381 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 382 | // Codegen the RHS so that it stores directly into the LHS. |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 383 | CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(), |
| 384 | false, false, RequiresGCollection); |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 385 | EmitFinalDestCopy(E, LHS, true); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 386 | } |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 389 | void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { |
Eli Friedman | 8e274bd | 2009-12-25 06:17:05 +0000 | [diff] [blame] | 390 | if (!E->getLHS()) { |
| 391 | CGF.ErrorUnsupported(E, "conditional operator with missing LHS"); |
| 392 | return; |
| 393 | } |
| 394 | |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 395 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 396 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
| 397 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Eli Friedman | 8e274bd | 2009-12-25 06:17:05 +0000 | [diff] [blame] | 399 | CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 401 | CGF.BeginConditionalBranch(); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 402 | CGF.EmitBlock(LHSBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 404 | // Handle the GNU extension for missing LHS. |
| 405 | assert(E->getLHS() && "Must have LHS for aggregate value"); |
| 406 | |
Chris Lattner | c748f27 | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 407 | Visit(E->getLHS()); |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 408 | CGF.EndConditionalBranch(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 409 | CGF.EmitBranch(ContBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 411 | CGF.BeginConditionalBranch(); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 412 | CGF.EmitBlock(RHSBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Chris Lattner | c748f27 | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 414 | Visit(E->getRHS()); |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 415 | CGF.EndConditionalBranch(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 416 | CGF.EmitBranch(ContBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 418 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 419 | } |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 420 | |
Anders Carlsson | a294ca8 | 2009-07-08 18:33:14 +0000 | [diff] [blame] | 421 | void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { |
| 422 | Visit(CE->getChosenSubExpr(CGF.getContext())); |
| 423 | } |
| 424 | |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 425 | void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 426 | llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 427 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); |
| 428 | |
Sebastian Redl | 0262f02 | 2009-01-09 21:09:38 +0000 | [diff] [blame] | 429 | if (!ArgPtr) { |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 430 | CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); |
Sebastian Redl | 0262f02 | 2009-01-09 21:09:38 +0000 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 434 | EmitFinalDestCopy(VE, CGF.MakeAddrLValue(ArgPtr, VE->getType())); |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 437 | void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
| 438 | llvm::Value *Val = DestPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 440 | if (!Val) { |
| 441 | // Create a temporary variable. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 442 | Val = CGF.CreateMemTemp(E->getType(), "tmp"); |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 443 | |
| 444 | // FIXME: volatile |
| 445 | CGF.EmitAggExpr(E->getSubExpr(), Val, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | } else |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 447 | Visit(E->getSubExpr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 449 | // Don't make this a live temporary if we're emitting an initializer expr. |
| 450 | if (!IsInitializer) |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 451 | CGF.EmitCXXTemporary(E->getTemporary(), Val); |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 454 | void |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 455 | AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 456 | llvm::Value *Val = DestPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Chris Lattner | fbe02ff | 2010-06-27 07:40:06 +0000 | [diff] [blame] | 458 | if (!Val) // Create a temporary variable. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 459 | Val = CGF.CreateMemTemp(E->getType(), "tmp"); |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 460 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 461 | CGF.EmitCXXConstructExpr(Val, E); |
Anders Carlsson | 7f6ad15 | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { |
Mike Stump | dd5614b | 2009-12-18 02:14:27 +0000 | [diff] [blame] | 465 | llvm::Value *Val = DestPtr; |
| 466 | |
Mike Stump | dd5614b | 2009-12-18 02:14:27 +0000 | [diff] [blame] | 467 | CGF.EmitCXXExprWithTemporaries(E, Val, VolatileDest, IsInitializer); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 470 | void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
Mike Stump | dd5614b | 2009-12-18 02:14:27 +0000 | [diff] [blame] | 471 | llvm::Value *Val = DestPtr; |
| 472 | |
| 473 | if (!Val) { |
| 474 | // Create a temporary variable. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 475 | Val = CGF.CreateMemTemp(E->getType(), "tmp"); |
Mike Stump | dd5614b | 2009-12-18 02:14:27 +0000 | [diff] [blame] | 476 | } |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 477 | EmitNullInitializationToLValue(CGF.MakeAddrLValue(Val, E->getType()), |
| 478 | E->getType()); |
Anders Carlsson | 30311fa | 2009-12-16 06:57:54 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
Mike Stump | dd5614b | 2009-12-18 02:14:27 +0000 | [diff] [blame] | 482 | llvm::Value *Val = DestPtr; |
| 483 | |
| 484 | if (!Val) { |
| 485 | // Create a temporary variable. |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 486 | Val = CGF.CreateMemTemp(E->getType(), "tmp"); |
Mike Stump | dd5614b | 2009-12-18 02:14:27 +0000 | [diff] [blame] | 487 | } |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 488 | EmitNullInitializationToLValue(CGF.MakeAddrLValue(Val, E->getType()), |
| 489 | E->getType()); |
Nuno Lopes | 329763b | 2009-10-18 15:18:11 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Anders Carlsson | 78e83f8 | 2010-02-03 17:33:16 +0000 | [diff] [blame] | 492 | void |
| 493 | AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV, QualType T) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 494 | // FIXME: Ignore result? |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 495 | // FIXME: Are initializers affected by volatile? |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 496 | if (isa<ImplicitValueInitExpr>(E)) { |
Anders Carlsson | 78e83f8 | 2010-02-03 17:33:16 +0000 | [diff] [blame] | 497 | EmitNullInitializationToLValue(LV, T); |
Anders Carlsson | e78ccb4 | 2010-02-03 19:13:55 +0000 | [diff] [blame] | 498 | } else if (T->isReferenceType()) { |
Anders Carlsson | 32f36ba | 2010-06-26 16:35:32 +0000 | [diff] [blame] | 499 | RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0); |
Anders Carlsson | e78ccb4 | 2010-02-03 19:13:55 +0000 | [diff] [blame] | 500 | CGF.EmitStoreThroughLValue(RV, LV, T); |
Anders Carlsson | 78e83f8 | 2010-02-03 17:33:16 +0000 | [diff] [blame] | 501 | } else if (T->isAnyComplexType()) { |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 502 | CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); |
Anders Carlsson | 78e83f8 | 2010-02-03 17:33:16 +0000 | [diff] [blame] | 503 | } else if (CGF.hasAggregateLLVMType(T)) { |
Eli Friedman | c8ba961 | 2008-05-12 15:06:05 +0000 | [diff] [blame] | 504 | CGF.EmitAnyExpr(E, LV.getAddress(), false); |
| 505 | } else { |
Anders Carlsson | 78e83f8 | 2010-02-03 17:33:16 +0000 | [diff] [blame] | 506 | CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, T); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 507 | } |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) { |
| 511 | if (!CGF.hasAggregateLLVMType(T)) { |
| 512 | // For non-aggregates, we can store zero |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 513 | llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T)); |
Daniel Dunbar | 8239713 | 2008-08-06 05:32:55 +0000 | [diff] [blame] | 514 | CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T); |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 515 | } else { |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 516 | // There's a potential optimization opportunity in combining |
| 517 | // memsets; that would be easy for arrays, but relatively |
| 518 | // difficult for structures with the current code. |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 519 | CGF.EmitNullInitialization(LV.getAddress(), T); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 523 | void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { |
Eli Friedman | a385b3c | 2008-12-02 01:17:45 +0000 | [diff] [blame] | 524 | #if 0 |
Eli Friedman | 13a5be1 | 2009-12-04 01:30:56 +0000 | [diff] [blame] | 525 | // FIXME: Assess perf here? Figure out what cases are worth optimizing here |
| 526 | // (Length of globals? Chunks of zeroed-out space?). |
Eli Friedman | a385b3c | 2008-12-02 01:17:45 +0000 | [diff] [blame] | 527 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 528 | // If we can, prefer a copy from a global; this is a lot less code for long |
| 529 | // globals, and it's easier for the current optimizers to analyze. |
Eli Friedman | 13a5be1 | 2009-12-04 01:30:56 +0000 | [diff] [blame] | 530 | if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) { |
Eli Friedman | 994ffef | 2008-11-30 02:11:09 +0000 | [diff] [blame] | 531 | llvm::GlobalVariable* GV = |
Eli Friedman | 13a5be1 | 2009-12-04 01:30:56 +0000 | [diff] [blame] | 532 | new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true, |
| 533 | llvm::GlobalValue::InternalLinkage, C, ""); |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 534 | EmitFinalDestCopy(E, CGF.MakeAddrLValue(GV, E->getType())); |
Eli Friedman | 994ffef | 2008-11-30 02:11:09 +0000 | [diff] [blame] | 535 | return; |
| 536 | } |
Eli Friedman | a385b3c | 2008-12-02 01:17:45 +0000 | [diff] [blame] | 537 | #endif |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 538 | if (E->hadArrayRangeDesignator()) { |
| 539 | CGF.ErrorUnsupported(E, "GNU array range designator extension"); |
| 540 | } |
| 541 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 542 | // Handle initialization of an array. |
| 543 | if (E->getType()->isArrayType()) { |
| 544 | const llvm::PointerType *APType = |
| 545 | cast<llvm::PointerType>(DestPtr->getType()); |
| 546 | const llvm::ArrayType *AType = |
| 547 | cast<llvm::ArrayType>(APType->getElementType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 549 | uint64_t NumInitElements = E->getNumInits(); |
Eli Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 551 | if (E->getNumInits() > 0) { |
| 552 | QualType T1 = E->getType(); |
| 553 | QualType T2 = E->getInit(0)->getType(); |
Eli Friedman | 2dce5f8 | 2009-05-28 23:04:00 +0000 | [diff] [blame] | 554 | if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 555 | EmitAggLoadOfLValue(E->getInit(0)); |
| 556 | return; |
| 557 | } |
Eli Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 560 | uint64_t NumArrayElements = AType->getNumElements(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 561 | QualType ElementType = CGF.getContext().getCanonicalType(E->getType()); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 562 | ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 564 | // FIXME: were we intentionally ignoring address spaces and GC attributes? |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 565 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 566 | for (uint64_t i = 0; i != NumArrayElements; ++i) { |
| 567 | llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 568 | LValue LV = CGF.MakeAddrLValue(NextVal, ElementType); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 569 | if (i < NumInitElements) |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 570 | EmitInitializationToLValue(E->getInit(i), LV, ElementType); |
| 571 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 572 | else |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 573 | EmitNullInitializationToLValue(LV, ElementType); |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 574 | } |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 575 | return; |
| 576 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 578 | assert(E->getType()->isRecordType() && "Only support structs/unions here!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 580 | // Do struct initialization; this code just sets each individual member |
| 581 | // to the approprate value. This makes bitfield support automatic; |
| 582 | // the disadvantage is that the generated code is more difficult for |
| 583 | // the optimizer, especially with bitfields. |
| 584 | unsigned NumInitElements = E->getNumInits(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 585 | RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 586 | unsigned CurInitVal = 0; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 587 | |
| 588 | if (E->getType()->isUnionType()) { |
| 589 | // Only initialize one field of a union. The field itself is |
| 590 | // specified by the initializer list. |
| 591 | if (!E->getInitializedFieldInUnion()) { |
| 592 | // Empty union; we have nothing to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 593 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 594 | #ifndef NDEBUG |
| 595 | // Make sure that it's really an empty and not a failure of |
| 596 | // semantic analysis. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 597 | for (RecordDecl::field_iterator Field = SD->field_begin(), |
| 598 | FieldEnd = SD->field_end(); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 599 | Field != FieldEnd; ++Field) |
| 600 | assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); |
| 601 | #endif |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | // FIXME: volatility |
| 606 | FieldDecl *Field = E->getInitializedFieldInUnion(); |
Anders Carlsson | e78ccb4 | 2010-02-03 19:13:55 +0000 | [diff] [blame] | 607 | LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 608 | |
| 609 | if (NumInitElements) { |
| 610 | // Store the initializer into the field |
Anders Carlsson | 78e83f8 | 2010-02-03 17:33:16 +0000 | [diff] [blame] | 611 | EmitInitializationToLValue(E->getInit(0), FieldLoc, Field->getType()); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 612 | } else { |
| 613 | // Default-initialize to null |
| 614 | EmitNullInitializationToLValue(FieldLoc, Field->getType()); |
| 615 | } |
| 616 | |
| 617 | return; |
| 618 | } |
Chris Lattner | b35baae | 2010-03-08 21:08:07 +0000 | [diff] [blame] | 619 | |
| 620 | // If we're initializing the whole aggregate, just do it in place. |
| 621 | // FIXME: This is a hack around an AST bug (PR6537). |
| 622 | if (NumInitElements == 1 && E->getType() == E->getInit(0)->getType()) { |
| 623 | EmitInitializationToLValue(E->getInit(0), |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 624 | CGF.MakeAddrLValue(DestPtr, E->getType()), |
Chris Lattner | b35baae | 2010-03-08 21:08:07 +0000 | [diff] [blame] | 625 | E->getType()); |
| 626 | return; |
| 627 | } |
| 628 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 630 | // Here we iterate over the fields; this makes it simpler to both |
| 631 | // default-initialize fields and skip over unnamed fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 632 | for (RecordDecl::field_iterator Field = SD->field_begin(), |
| 633 | FieldEnd = SD->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 634 | Field != FieldEnd; ++Field) { |
| 635 | // We're done once we hit the flexible array member |
| 636 | if (Field->getType()->isIncompleteArrayType()) |
| 637 | break; |
| 638 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 639 | if (Field->isUnnamedBitfield()) |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 640 | continue; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 641 | |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 642 | // FIXME: volatility |
Anders Carlsson | e78ccb4 | 2010-02-03 19:13:55 +0000 | [diff] [blame] | 643 | LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, *Field, 0); |
Fariborz Jahanian | 14674ff | 2009-05-27 19:54:11 +0000 | [diff] [blame] | 644 | // We never generate write-barries for initialized fields. |
Daniel Dunbar | ea61917 | 2010-08-21 03:22:38 +0000 | [diff] [blame] | 645 | FieldLoc.setNonGC(true); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 646 | if (CurInitVal < NumInitElements) { |
Chris Lattner | b35baae | 2010-03-08 21:08:07 +0000 | [diff] [blame] | 647 | // Store the initializer into the field. |
| 648 | EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc, |
Anders Carlsson | 78e83f8 | 2010-02-03 17:33:16 +0000 | [diff] [blame] | 649 | Field->getType()); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 650 | } else { |
| 651 | // We're out of initalizers; default-initialize to null |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 652 | EmitNullInitializationToLValue(FieldLoc, Field->getType()); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 653 | } |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 654 | } |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 657 | //===----------------------------------------------------------------------===// |
| 658 | // Entry Points into this File |
| 659 | //===----------------------------------------------------------------------===// |
| 660 | |
Mike Stump | e1129a9 | 2009-05-26 18:57:45 +0000 | [diff] [blame] | 661 | /// EmitAggExpr - Emit the computation of the specified expression of aggregate |
| 662 | /// type. The result is computed into DestPtr. Note that if DestPtr is null, |
| 663 | /// the value of the aggregate expression is not needed. If VolatileDest is |
| 664 | /// true, DestPtr cannot be 0. |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 665 | // |
| 666 | // FIXME: Take Qualifiers object. |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 667 | void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr, |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 668 | bool VolatileDest, bool IgnoreResult, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | bool IsInitializer, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 670 | bool RequiresGCollection) { |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 671 | assert(E && hasAggregateLLVMType(E->getType()) && |
| 672 | "Invalid aggregate expression to emit"); |
Mike Stump | e1129a9 | 2009-05-26 18:57:45 +0000 | [diff] [blame] | 673 | assert ((DestPtr != 0 || VolatileDest == false) |
| 674 | && "volatile aggregate can't be 0"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 676 | AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer, |
| 677 | RequiresGCollection) |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 678 | .Visit(const_cast<Expr*>(E)); |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 679 | } |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 680 | |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 681 | LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) { |
| 682 | assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!"); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 683 | llvm::Value *Temp = CreateMemTemp(E->getType()); |
Daniel Dunbar | 79c3928 | 2010-08-21 03:15:20 +0000 | [diff] [blame] | 684 | LValue LV = MakeAddrLValue(Temp, E->getType()); |
| 685 | EmitAggExpr(E, Temp, LV.isVolatileQualified()); |
| 686 | return LV; |
Daniel Dunbar | 18aba0d | 2010-02-05 19:38:31 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 689 | void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, |
Mike Stump | 27fe2e6 | 2009-05-23 22:29:41 +0000 | [diff] [blame] | 690 | llvm::Value *SrcPtr, QualType Ty, |
| 691 | bool isVolatile) { |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 692 | assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | |
Anders Carlsson | 0d7c583 | 2010-05-03 01:20:20 +0000 | [diff] [blame] | 694 | if (getContext().getLangOptions().CPlusPlus) { |
| 695 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Douglas Gregor | e997948 | 2010-05-20 15:39:01 +0000 | [diff] [blame] | 696 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); |
| 697 | assert((Record->hasTrivialCopyConstructor() || |
Fariborz Jahanian | 1d49f21 | 2010-05-20 16:46:55 +0000 | [diff] [blame] | 698 | Record->hasTrivialCopyAssignment()) && |
Douglas Gregor | e997948 | 2010-05-20 15:39:01 +0000 | [diff] [blame] | 699 | "Trying to aggregate-copy a type without a trivial copy " |
| 700 | "constructor or assignment operator"); |
Douglas Gregor | 419aa96 | 2010-05-20 15:48:29 +0000 | [diff] [blame] | 701 | // Ignore empty classes in C++. |
Douglas Gregor | e997948 | 2010-05-20 15:39:01 +0000 | [diff] [blame] | 702 | if (Record->isEmpty()) |
Anders Carlsson | 0d7c583 | 2010-05-03 01:20:20 +0000 | [diff] [blame] | 703 | return; |
| 704 | } |
| 705 | } |
| 706 | |
Chris Lattner | 83c9629 | 2009-02-28 18:31:01 +0000 | [diff] [blame] | 707 | // Aggregate assignment turns into llvm.memcpy. This is almost valid per |
Chris Lattner | ca4fc2c | 2009-02-28 18:18:58 +0000 | [diff] [blame] | 708 | // C99 6.5.16.1p3, which states "If the value being stored in an object is |
| 709 | // read from another object that overlaps in anyway the storage of the first |
| 710 | // object, then the overlap shall be exact and the two objects shall have |
| 711 | // qualified or unqualified versions of a compatible type." |
| 712 | // |
Chris Lattner | 83c9629 | 2009-02-28 18:31:01 +0000 | [diff] [blame] | 713 | // memcpy is not defined if the source and destination pointers are exactly |
Chris Lattner | ca4fc2c | 2009-02-28 18:18:58 +0000 | [diff] [blame] | 714 | // equal, but other compilers do this optimization, and almost every memcpy |
| 715 | // implementation handles this case safely. If there is a libc that does not |
| 716 | // safely handle this, we can add a target hook. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 718 | // Get size and alignment info for this aggregate. |
| 719 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 721 | // FIXME: Handle variable sized types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | |
Mike Stump | fde6420 | 2009-05-23 04:13:59 +0000 | [diff] [blame] | 723 | // FIXME: If we have a volatile struct, the optimizer can remove what might |
| 724 | // appear to be `extra' memory ops: |
| 725 | // |
| 726 | // volatile struct { int i; } a, b; |
| 727 | // |
| 728 | // int main() { |
| 729 | // a = b; |
| 730 | // a = b; |
| 731 | // } |
| 732 | // |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 733 | // we need to use a different call here. We use isVolatile to indicate when |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 734 | // either the source or the destination is volatile. |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 735 | |
| 736 | const llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); |
Chris Lattner | 098432c | 2010-07-08 00:07:45 +0000 | [diff] [blame] | 737 | const llvm::Type *DBP = |
| 738 | llvm::Type::getInt8PtrTy(VMContext, DPT->getAddressSpace()); |
| 739 | DestPtr = Builder.CreateBitCast(DestPtr, DBP, "tmp"); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 740 | |
| 741 | const llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); |
Chris Lattner | 098432c | 2010-07-08 00:07:45 +0000 | [diff] [blame] | 742 | const llvm::Type *SBP = |
| 743 | llvm::Type::getInt8PtrTy(VMContext, SPT->getAddressSpace()); |
| 744 | SrcPtr = Builder.CreateBitCast(SrcPtr, SBP, "tmp"); |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 745 | |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 746 | if (const RecordType *RecordTy = Ty->getAs<RecordType>()) { |
| 747 | RecordDecl *Record = RecordTy->getDecl(); |
| 748 | if (Record->hasObjectMember()) { |
| 749 | unsigned long size = TypeInfo.first/8; |
| 750 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 751 | llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); |
| 752 | CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, |
| 753 | SizeVal); |
| 754 | return; |
| 755 | } |
| 756 | } else if (getContext().getAsArrayType(Ty)) { |
| 757 | QualType BaseType = getContext().getBaseElementType(Ty); |
| 758 | if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) { |
| 759 | if (RecordTy->getDecl()->hasObjectMember()) { |
| 760 | unsigned long size = TypeInfo.first/8; |
| 761 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 762 | llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size); |
| 763 | CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr, |
| 764 | SizeVal); |
| 765 | return; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | |
Mon P Wang | 3ecd785 | 2010-04-04 03:10:52 +0000 | [diff] [blame] | 770 | Builder.CreateCall5(CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(), |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 771 | IntPtrTy), |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 772 | DestPtr, SrcPtr, |
| 773 | // TypeInfo.first describes size in bits. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 774 | llvm::ConstantInt::get(IntPtrTy, TypeInfo.first/8), |
Chris Lattner | 098432c | 2010-07-08 00:07:45 +0000 | [diff] [blame] | 775 | Builder.getInt32(TypeInfo.second/8), |
| 776 | Builder.getInt1(isVolatile)); |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 777 | } |