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 | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 24 | #include "llvm/Intrinsics.h" |
Chris Lattner | af6f528 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // Aggregate Expression Emitter |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | namespace { |
| 33 | class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> { |
| 34 | CodeGenFunction &CGF; |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 35 | CGBuilderTy &Builder; |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 36 | llvm::Value *DestPtr; |
| 37 | bool VolatileDest; |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 38 | bool IgnoreResult; |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 39 | bool IsInitializer; |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 40 | bool RequiresGCollection; |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 41 | public: |
Mike Stump | ff4bf3b | 2009-05-27 01:42:21 +0000 | [diff] [blame] | 42 | AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 43 | bool ignore, bool isinit, bool requiresGCollection) |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 44 | : CGF(cgf), Builder(CGF.Builder), |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 45 | DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore), |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 46 | IsInitializer(isinit), RequiresGCollection(requiresGCollection) { |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 49 | //===--------------------------------------------------------------------===// |
| 50 | // Utilities |
| 51 | //===--------------------------------------------------------------------===// |
| 52 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 53 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 54 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 55 | /// then loads the result into DestPtr. |
| 56 | void EmitAggLoadOfLValue(const Expr *E); |
Eli Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 57 | |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 58 | /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 59 | void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false); |
| 60 | void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 61 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 62 | //===--------------------------------------------------------------------===// |
| 63 | // Visitor Methods |
| 64 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 66 | void VisitStmt(Stmt *S) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 67 | CGF.ErrorUnsupported(S, "aggregate expression"); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 68 | } |
| 69 | void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } |
Eli Friedman | 12444a2 | 2009-01-27 09:03:41 +0000 | [diff] [blame] | 70 | void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); } |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 71 | |
| 72 | // l-values. |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 73 | void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } |
| 74 | void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } |
| 75 | void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } |
Seo Sanghyeon | ad6ebd6 | 2007-12-23 03:11:58 +0000 | [diff] [blame] | 76 | void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 77 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | EmitAggLoadOfLValue(E); |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 79 | } |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 80 | void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 81 | EmitAggLoadOfLValue(E); |
| 82 | } |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 83 | void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | EmitAggLoadOfLValue(E); |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 85 | } |
| 86 | void VisitPredefinedExpr(const PredefinedExpr *E) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | EmitAggLoadOfLValue(E); |
Chris Lattner | f0a990c | 2009-04-21 23:00:09 +0000 | [diff] [blame] | 88 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 90 | // Operators. |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 91 | void VisitCastExpr(CastExpr *E); |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 92 | void VisitCallExpr(const CallExpr *E); |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 93 | void VisitStmtExpr(const StmtExpr *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 94 | void VisitBinaryOperator(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); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 98 | void VisitObjCMessageExpr(ObjCMessageExpr *E); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 99 | void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
| 100 | EmitAggLoadOfLValue(E); |
| 101 | } |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 102 | void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 103 | void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 105 | void VisitConditionalOperator(const ConditionalOperator *CO); |
Anders Carlsson | a294ca8 | 2009-07-08 18:33:14 +0000 | [diff] [blame] | 106 | void VisitChooseExpr(const ChooseExpr *CE); |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 107 | void VisitInitListExpr(InitListExpr *E); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 108 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 109 | Visit(DAE->getExpr()); |
| 110 | } |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 111 | void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 112 | void VisitCXXConstructExpr(const CXXConstructExpr *E); |
Anders Carlsson | 7f6ad15 | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 113 | void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E); |
| 114 | |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 115 | void VisitVAArgExpr(VAArgExpr *E); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 116 | |
| 117 | void EmitInitializationToLValue(Expr *E, LValue Address); |
| 118 | void EmitNullInitializationToLValue(LValue Address, QualType T); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 119 | // case Expr::ChooseExprClass: |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 121 | }; |
| 122 | } // end anonymous namespace. |
| 123 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 124 | //===----------------------------------------------------------------------===// |
| 125 | // Utilities |
| 126 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 128 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 129 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 130 | /// then loads the result into DestPtr. |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 131 | void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { |
| 132 | LValue LV = CGF.EmitLValue(E); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 133 | EmitFinalDestCopy(E, LV); |
| 134 | } |
| 135 | |
| 136 | /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 137 | void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) { |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 138 | assert(Src.isAggregate() && "value must be aggregate value!"); |
| 139 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 140 | // If the result is ignored, don't copy from the value. |
Mike Stump | 9ccb103 | 2009-05-23 22:01:27 +0000 | [diff] [blame] | 141 | if (DestPtr == 0) { |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 142 | if (!Src.isVolatileQualified() || (IgnoreResult && Ignore)) |
Mike Stump | 9ccb103 | 2009-05-23 22:01:27 +0000 | [diff] [blame] | 143 | return; |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 144 | // If the source is volatile, we must read from it; to do that, we need |
| 145 | // some place to put it. |
| 146 | DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp"); |
Mike Stump | 9ccb103 | 2009-05-23 22:01:27 +0000 | [diff] [blame] | 147 | } |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 148 | |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 149 | if (RequiresGCollection) { |
| 150 | CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, |
| 151 | DestPtr, Src.getAggregateAddr(), |
| 152 | E->getType()); |
| 153 | return; |
| 154 | } |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 155 | // If the result of the assignment is used, copy the LHS there also. |
| 156 | // FIXME: Pass VolatileDest as well. I think we also need to merge volatile |
| 157 | // from the source as well, as we can't eliminate it if either operand |
| 158 | // is volatile, unless copy has volatile for both source and destination.. |
Mike Stump | 27fe2e6 | 2009-05-23 22:29:41 +0000 | [diff] [blame] | 159 | CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(), |
| 160 | VolatileDest|Src.isVolatileQualified()); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired. |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 164 | void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) { |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 165 | assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc"); |
| 166 | |
| 167 | EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(), |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 168 | Src.isVolatileQualified()), |
| 169 | Ignore); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 172 | //===----------------------------------------------------------------------===// |
| 173 | // Visitor Methods |
| 174 | //===----------------------------------------------------------------------===// |
| 175 | |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 176 | void AggExprEmitter::VisitCastExpr(CastExpr *E) { |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame^] | 177 | switch (E->getCastKind()) { |
| 178 | default: assert(0 && "Unhandled cast kind!"); |
| 179 | |
| 180 | case CastExpr::CK_ToUnion: { |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 181 | // GCC union extension |
Eli Friedman | 34ebf4d | 2009-06-03 20:45:06 +0000 | [diff] [blame] | 182 | QualType PtrTy = |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame^] | 183 | CGF.getContext().getPointerType(E->getSubExpr()->getType()); |
Eli Friedman | 34ebf4d | 2009-06-03 20:45:06 +0000 | [diff] [blame] | 184 | llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr, |
| 185 | CGF.ConvertType(PtrTy)); |
Mon P Wang | c6a38a4 | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 186 | EmitInitializationToLValue(E->getSubExpr(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 187 | LValue::MakeAddr(CastPtr, Qualifiers())); |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame^] | 188 | break; |
Nuno Lopes | 7e91627 | 2009-01-15 20:14:33 +0000 | [diff] [blame] | 189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 191 | // FIXME: Remove the CK_Unknown check here. |
Anders Carlsson | 3016842 | 2009-09-29 01:23:39 +0000 | [diff] [blame^] | 192 | case CastExpr::CK_Unknown: |
| 193 | case CastExpr::CK_NoOp: |
| 194 | case CastExpr::CK_UserDefinedConversion: |
| 195 | case CastExpr::CK_ConstructorConversion: |
| 196 | assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(), |
| 197 | E->getType()) && |
| 198 | "Implicit cast types must be compatible"); |
| 199 | Visit(E->getSubExpr()); |
| 200 | break; |
| 201 | } |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 204 | void AggExprEmitter::VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | e70e8f7 | 2009-05-27 16:45:02 +0000 | [diff] [blame] | 205 | if (E->getCallReturnType()->isReferenceType()) { |
| 206 | EmitAggLoadOfLValue(E); |
| 207 | return; |
| 208 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 210 | RValue RV = CGF.EmitCallExpr(E); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 211 | EmitFinalDestCopy(E, RV); |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 212 | } |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 213 | |
| 214 | void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 215 | RValue RV = CGF.EmitObjCMessageExpr(E); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 216 | EmitFinalDestCopy(E, RV); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 217 | } |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 218 | |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 219 | void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { |
| 220 | RValue RV = CGF.EmitObjCPropertyGet(E); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 221 | EmitFinalDestCopy(E, RV); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 224 | void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr( |
| 225 | ObjCImplicitSetterGetterRefExpr *E) { |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 226 | RValue RV = CGF.EmitObjCPropertyGet(E); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 227 | EmitFinalDestCopy(E, RV); |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 230 | void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 231 | CGF.EmitAnyExpr(E->getLHS(), 0, false, true); |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 232 | CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest, |
| 233 | /*IgnoreResult=*/false, IsInitializer); |
Eli Friedman | 07fa52a | 2008-05-20 07:56:31 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 236 | void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
| 237 | CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest); |
| 238 | } |
| 239 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 240 | void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 241 | CGF.ErrorUnsupported(E, "aggregate binary expression"); |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 244 | void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 245 | // For an assignment to work, the value on the right has |
| 246 | // to be compatible with the value on the left. |
Eli Friedman | 2dce5f8 | 2009-05-28 23:04:00 +0000 | [diff] [blame] | 247 | assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), |
| 248 | E->getRHS()->getType()) |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 249 | && "Invalid assignment"); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 250 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 251 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 252 | // We have to special case property setters, otherwise we must have |
| 253 | // a simple lvalue (no aggregates inside vectors, bitfields). |
| 254 | if (LHS.isPropertyRef()) { |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 255 | llvm::Value *AggLoc = DestPtr; |
| 256 | if (!AggLoc) |
| 257 | AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType())); |
Mike Stump | 240993d | 2009-05-23 23:48:13 +0000 | [diff] [blame] | 258 | CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), |
Mike Stump | 240993d | 2009-05-23 23:48:13 +0000 | [diff] [blame] | 260 | RValue::getAggregate(AggLoc, VolatileDest)); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 261 | } else if (LHS.isKVCRef()) { |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 262 | llvm::Value *AggLoc = DestPtr; |
| 263 | if (!AggLoc) |
| 264 | AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType())); |
Mike Stump | a49af1a | 2009-05-23 23:52:31 +0000 | [diff] [blame] | 265 | CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), |
Mike Stump | a49af1a | 2009-05-23 23:52:31 +0000 | [diff] [blame] | 267 | RValue::getAggregate(AggLoc, VolatileDest)); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 268 | } else { |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 269 | bool RequiresGCollection = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 270 | if (CGF.getContext().getLangOptions().NeXTRuntime) { |
| 271 | QualType LHSTy = E->getLHS()->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 272 | if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | RequiresGCollection = FDTTy->getDecl()->hasObjectMember(); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 274 | } |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 275 | // Codegen the RHS so that it stores directly into the LHS. |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 276 | CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(), |
| 277 | false, false, RequiresGCollection); |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 278 | EmitFinalDestCopy(E, LHS, true); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 279 | } |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 282 | void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 283 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 284 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
| 285 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 287 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 288 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Anders Carlsson | fb6fa30 | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 290 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 291 | CGF.EmitBlock(LHSBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 293 | // Handle the GNU extension for missing LHS. |
| 294 | assert(E->getLHS() && "Must have LHS for aggregate value"); |
| 295 | |
Chris Lattner | c748f27 | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 296 | Visit(E->getLHS()); |
Anders Carlsson | fb6fa30 | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 297 | CGF.PopConditionalTempDestruction(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 298 | CGF.EmitBranch(ContBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Anders Carlsson | fb6fa30 | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 300 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 301 | CGF.EmitBlock(RHSBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
Chris Lattner | c748f27 | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 303 | Visit(E->getRHS()); |
Anders Carlsson | fb6fa30 | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 304 | CGF.PopConditionalTempDestruction(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 305 | CGF.EmitBranch(ContBlock); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 307 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 308 | } |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 309 | |
Anders Carlsson | a294ca8 | 2009-07-08 18:33:14 +0000 | [diff] [blame] | 310 | void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) { |
| 311 | Visit(CE->getChosenSubExpr(CGF.getContext())); |
| 312 | } |
| 313 | |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 314 | void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 315 | llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 316 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); |
| 317 | |
Sebastian Redl | 0262f02 | 2009-01-09 21:09:38 +0000 | [diff] [blame] | 318 | if (!ArgPtr) { |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 319 | CGF.ErrorUnsupported(VE, "aggregate va_arg expression"); |
Sebastian Redl | 0262f02 | 2009-01-09 21:09:38 +0000 | [diff] [blame] | 320 | return; |
| 321 | } |
| 322 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 323 | EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, Qualifiers())); |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 326 | void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
| 327 | llvm::Value *Val = DestPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 329 | if (!Val) { |
| 330 | // Create a temporary variable. |
| 331 | Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp"); |
| 332 | |
| 333 | // FIXME: volatile |
| 334 | CGF.EmitAggExpr(E->getSubExpr(), Val, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | } else |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 336 | Visit(E->getSubExpr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 338 | // Don't make this a live temporary if we're emitting an initializer expr. |
| 339 | if (!IsInitializer) |
| 340 | CGF.PushCXXTemporary(E->getTemporary(), Val); |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 343 | void |
Anders Carlsson | 31ccf37 | 2009-05-03 17:47:16 +0000 | [diff] [blame] | 344 | AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 345 | llvm::Value *Val = DestPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 347 | if (!Val) { |
| 348 | // Create a temporary variable. |
| 349 | Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp"); |
| 350 | } |
Anders Carlsson | 8e587a1 | 2009-05-30 20:56:46 +0000 | [diff] [blame] | 351 | |
Anders Carlsson | b58d017 | 2009-05-30 23:23:33 +0000 | [diff] [blame] | 352 | CGF.EmitCXXConstructExpr(Val, E); |
Anders Carlsson | 7f6ad15 | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 356 | CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest, IsInitializer); |
Anders Carlsson | b14095a | 2009-04-17 00:06:03 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 359 | void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 360 | // FIXME: Ignore result? |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 361 | // FIXME: Are initializers affected by volatile? |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 362 | if (isa<ImplicitValueInitExpr>(E)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 363 | EmitNullInitializationToLValue(LV, E->getType()); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 364 | } else if (E->getType()->isComplexType()) { |
| 365 | CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); |
Eli Friedman | c8ba961 | 2008-05-12 15:06:05 +0000 | [diff] [blame] | 366 | } else if (CGF.hasAggregateLLVMType(E->getType())) { |
| 367 | CGF.EmitAnyExpr(E, LV.getAddress(), false); |
| 368 | } else { |
| 369 | CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType()); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 370 | } |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) { |
| 374 | if (!CGF.hasAggregateLLVMType(T)) { |
| 375 | // For non-aggregates, we can store zero |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 376 | llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T)); |
Daniel Dunbar | 8239713 | 2008-08-06 05:32:55 +0000 | [diff] [blame] | 377 | CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T); |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 378 | } else { |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 379 | // Otherwise, just memset the whole thing to zero. This is legal |
| 380 | // because in LLVM, all default initializers are guaranteed to have a |
| 381 | // bit pattern of all zeros. |
Eli Friedman | 0f59312 | 2009-04-13 21:47:26 +0000 | [diff] [blame] | 382 | // FIXME: That isn't true for member pointers! |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 383 | // There's a potential optimization opportunity in combining |
| 384 | // memsets; that would be easy for arrays, but relatively |
| 385 | // difficult for structures with the current code. |
Eli Friedman | ccf0ed8 | 2009-03-28 03:10:45 +0000 | [diff] [blame] | 386 | CGF.EmitMemSetToZero(LV.getAddress(), T); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 390 | void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { |
Eli Friedman | a385b3c | 2008-12-02 01:17:45 +0000 | [diff] [blame] | 391 | #if 0 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | // FIXME: Disabled while we figure out what to do about |
Eli Friedman | a385b3c | 2008-12-02 01:17:45 +0000 | [diff] [blame] | 393 | // test/CodeGen/bitfield.c |
| 394 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 395 | // If we can, prefer a copy from a global; this is a lot less code for long |
| 396 | // globals, and it's easier for the current optimizers to analyze. |
| 397 | // FIXME: Should we really be doing this? Should we try to avoid cases where |
| 398 | // we emit a global with a lot of zeros? Should we try to avoid short |
| 399 | // globals? |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 400 | if (E->isConstantInitializer(CGF.getContext(), 0)) { |
Eli Friedman | 994ffef | 2008-11-30 02:11:09 +0000 | [diff] [blame] | 401 | llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF); |
| 402 | llvm::GlobalVariable* GV = |
| 403 | new llvm::GlobalVariable(C->getType(), true, |
| 404 | llvm::GlobalValue::InternalLinkage, |
| 405 | C, "", &CGF.CGM.getModule(), 0); |
Mike Stump | 4ac20dd | 2009-05-23 20:28:01 +0000 | [diff] [blame] | 406 | EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0)); |
Eli Friedman | 994ffef | 2008-11-30 02:11:09 +0000 | [diff] [blame] | 407 | return; |
| 408 | } |
Eli Friedman | a385b3c | 2008-12-02 01:17:45 +0000 | [diff] [blame] | 409 | #endif |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 410 | if (E->hadArrayRangeDesignator()) { |
| 411 | CGF.ErrorUnsupported(E, "GNU array range designator extension"); |
| 412 | } |
| 413 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 414 | // Handle initialization of an array. |
| 415 | if (E->getType()->isArrayType()) { |
| 416 | const llvm::PointerType *APType = |
| 417 | cast<llvm::PointerType>(DestPtr->getType()); |
| 418 | const llvm::ArrayType *AType = |
| 419 | cast<llvm::ArrayType>(APType->getElementType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 421 | uint64_t NumInitElements = E->getNumInits(); |
Eli Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 423 | if (E->getNumInits() > 0) { |
| 424 | QualType T1 = E->getType(); |
| 425 | QualType T2 = E->getInit(0)->getType(); |
Eli Friedman | 2dce5f8 | 2009-05-28 23:04:00 +0000 | [diff] [blame] | 426 | if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) { |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 427 | EmitAggLoadOfLValue(E->getInit(0)); |
| 428 | return; |
| 429 | } |
Eli Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 432 | uint64_t NumArrayElements = AType->getNumElements(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 433 | QualType ElementType = CGF.getContext().getCanonicalType(E->getType()); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 434 | ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 436 | // FIXME: were we intentionally ignoring address spaces and GC attributes? |
| 437 | Qualifiers Quals = CGF.MakeQualifiers(ElementType); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 438 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 439 | for (uint64_t i = 0; i != NumArrayElements; ++i) { |
| 440 | llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); |
| 441 | if (i < NumInitElements) |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 442 | EmitInitializationToLValue(E->getInit(i), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 443 | LValue::MakeAddr(NextVal, Quals)); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 444 | else |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 445 | EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, Quals), |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 446 | ElementType); |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 447 | } |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 448 | return; |
| 449 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 451 | assert(E->getType()->isRecordType() && "Only support structs/unions here!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 453 | // Do struct initialization; this code just sets each individual member |
| 454 | // to the approprate value. This makes bitfield support automatic; |
| 455 | // the disadvantage is that the generated code is more difficult for |
| 456 | // the optimizer, especially with bitfields. |
| 457 | unsigned NumInitElements = E->getNumInits(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 458 | RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl(); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 459 | unsigned CurInitVal = 0; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 460 | |
| 461 | if (E->getType()->isUnionType()) { |
| 462 | // Only initialize one field of a union. The field itself is |
| 463 | // specified by the initializer list. |
| 464 | if (!E->getInitializedFieldInUnion()) { |
| 465 | // Empty union; we have nothing to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 467 | #ifndef NDEBUG |
| 468 | // Make sure that it's really an empty and not a failure of |
| 469 | // semantic analysis. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 470 | for (RecordDecl::field_iterator Field = SD->field_begin(), |
| 471 | FieldEnd = SD->field_end(); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 472 | Field != FieldEnd; ++Field) |
| 473 | assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed"); |
| 474 | #endif |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | // FIXME: volatility |
| 479 | FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 480 | LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0); |
| 481 | |
| 482 | if (NumInitElements) { |
| 483 | // Store the initializer into the field |
| 484 | EmitInitializationToLValue(E->getInit(0), FieldLoc); |
| 485 | } else { |
| 486 | // Default-initialize to null |
| 487 | EmitNullInitializationToLValue(FieldLoc, Field->getType()); |
| 488 | } |
| 489 | |
| 490 | return; |
| 491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 493 | // Here we iterate over the fields; this makes it simpler to both |
| 494 | // default-initialize fields and skip over unnamed fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 495 | for (RecordDecl::field_iterator Field = SD->field_begin(), |
| 496 | FieldEnd = SD->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 497 | Field != FieldEnd; ++Field) { |
| 498 | // We're done once we hit the flexible array member |
| 499 | if (Field->getType()->isIncompleteArrayType()) |
| 500 | break; |
| 501 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 502 | if (Field->isUnnamedBitfield()) |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 503 | continue; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 504 | |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 505 | // FIXME: volatility |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 506 | LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0); |
Fariborz Jahanian | 14674ff | 2009-05-27 19:54:11 +0000 | [diff] [blame] | 507 | // We never generate write-barries for initialized fields. |
| 508 | LValue::SetObjCNonGC(FieldLoc, true); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 509 | if (CurInitVal < NumInitElements) { |
| 510 | // Store the initializer into the field |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 511 | EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc); |
| 512 | } else { |
| 513 | // We're out of initalizers; default-initialize to null |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 514 | EmitNullInitializationToLValue(FieldLoc, Field->getType()); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 515 | } |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 516 | } |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 519 | //===----------------------------------------------------------------------===// |
| 520 | // Entry Points into this File |
| 521 | //===----------------------------------------------------------------------===// |
| 522 | |
Mike Stump | e1129a9 | 2009-05-26 18:57:45 +0000 | [diff] [blame] | 523 | /// EmitAggExpr - Emit the computation of the specified expression of aggregate |
| 524 | /// type. The result is computed into DestPtr. Note that if DestPtr is null, |
| 525 | /// the value of the aggregate expression is not needed. If VolatileDest is |
| 526 | /// true, DestPtr cannot be 0. |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 527 | void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr, |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 528 | bool VolatileDest, bool IgnoreResult, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | bool IsInitializer, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 530 | bool RequiresGCollection) { |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 531 | assert(E && hasAggregateLLVMType(E->getType()) && |
| 532 | "Invalid aggregate expression to emit"); |
Mike Stump | e1129a9 | 2009-05-26 18:57:45 +0000 | [diff] [blame] | 533 | assert ((DestPtr != 0 || VolatileDest == false) |
| 534 | && "volatile aggregate can't be 0"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 536 | AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer, |
| 537 | RequiresGCollection) |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 538 | .Visit(const_cast<Expr*>(E)); |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 539 | } |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 540 | |
| 541 | void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) { |
| 542 | assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); |
| 543 | |
| 544 | EmitMemSetToZero(DestPtr, Ty); |
| 545 | } |
| 546 | |
| 547 | void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr, |
Mike Stump | 27fe2e6 | 2009-05-23 22:29:41 +0000 | [diff] [blame] | 548 | llvm::Value *SrcPtr, QualType Ty, |
| 549 | bool isVolatile) { |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 550 | assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | |
Chris Lattner | 83c9629 | 2009-02-28 18:31:01 +0000 | [diff] [blame] | 552 | // Aggregate assignment turns into llvm.memcpy. This is almost valid per |
Chris Lattner | ca4fc2c | 2009-02-28 18:18:58 +0000 | [diff] [blame] | 553 | // C99 6.5.16.1p3, which states "If the value being stored in an object is |
| 554 | // read from another object that overlaps in anyway the storage of the first |
| 555 | // object, then the overlap shall be exact and the two objects shall have |
| 556 | // qualified or unqualified versions of a compatible type." |
| 557 | // |
Chris Lattner | 83c9629 | 2009-02-28 18:31:01 +0000 | [diff] [blame] | 558 | // memcpy is not defined if the source and destination pointers are exactly |
Chris Lattner | ca4fc2c | 2009-02-28 18:18:58 +0000 | [diff] [blame] | 559 | // equal, but other compilers do this optimization, and almost every memcpy |
| 560 | // implementation handles this case safely. If there is a libc that does not |
| 561 | // safely handle this, we can add a target hook. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 562 | const llvm::Type *BP = |
| 563 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 564 | if (DestPtr->getType() != BP) |
| 565 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 566 | if (SrcPtr->getType() != BP) |
| 567 | SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 569 | // Get size and alignment info for this aggregate. |
| 570 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 572 | // FIXME: Handle variable sized types. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 573 | const llvm::Type *IntPtr = |
| 574 | llvm::IntegerType::get(VMContext, LLVMPointerWidth); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | |
Mike Stump | fde6420 | 2009-05-23 04:13:59 +0000 | [diff] [blame] | 576 | // FIXME: If we have a volatile struct, the optimizer can remove what might |
| 577 | // appear to be `extra' memory ops: |
| 578 | // |
| 579 | // volatile struct { int i; } a, b; |
| 580 | // |
| 581 | // int main() { |
| 582 | // a = b; |
| 583 | // a = b; |
| 584 | // } |
| 585 | // |
Mike Stump | 49d1cd5 | 2009-05-26 22:03:21 +0000 | [diff] [blame] | 586 | // we need to use a differnt call here. We use isVolatile to indicate when |
| 587 | // either the source or the destination is volatile. |
Chris Lattner | ca4fc2c | 2009-02-28 18:18:58 +0000 | [diff] [blame] | 588 | Builder.CreateCall4(CGM.getMemCpyFn(), |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 589 | DestPtr, SrcPtr, |
| 590 | // TypeInfo.first describes size in bits. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 591 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Daniel Dunbar | 7482d12 | 2008-09-09 20:49:46 +0000 | [diff] [blame] | 593 | TypeInfo.second/8)); |
| 594 | } |