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