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