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