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" |
| 17 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Function.h" |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 20 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 22 | #include "llvm/Intrinsics.h" |
Chris Lattner | af6f528 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // Aggregate Expression Emitter |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | namespace { |
| 31 | class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> { |
| 32 | CodeGenFunction &CGF; |
Chris Lattner | 85e3568 | 2008-08-08 19:57:58 +0000 | [diff] [blame] | 33 | llvm::IRBuilder<> &Builder; |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 34 | llvm::Value *DestPtr; |
| 35 | bool VolatileDest; |
| 36 | public: |
| 37 | AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest) |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 38 | : CGF(cgf), Builder(CGF.Builder), |
| 39 | DestPtr(destPtr), VolatileDest(volatileDest) { |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 42 | //===--------------------------------------------------------------------===// |
| 43 | // Utilities |
| 44 | //===--------------------------------------------------------------------===// |
| 45 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 46 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 47 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 48 | /// then loads the result into DestPtr. |
| 49 | void EmitAggLoadOfLValue(const Expr *E); |
| 50 | |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 51 | void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
| 52 | QualType EltTy); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 53 | |
| 54 | void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty); |
| 55 | |
| 56 | void EmitNonConstInit(InitListExpr *E); |
Eli Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 57 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 58 | //===--------------------------------------------------------------------===// |
| 59 | // Visitor Methods |
| 60 | //===--------------------------------------------------------------------===// |
| 61 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 62 | void VisitStmt(Stmt *S) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 63 | CGF.ErrorUnsupported(S, "aggregate expression"); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 64 | } |
| 65 | void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); } |
| 66 | |
| 67 | // l-values. |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 68 | void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); } |
| 69 | void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } |
| 70 | void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } |
Seo Sanghyeon | ad6ebd6 | 2007-12-23 03:11:58 +0000 | [diff] [blame] | 71 | void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 72 | void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) |
| 73 | { EmitAggLoadOfLValue(E); } |
Seo Sanghyeon | 9b73b39 | 2007-12-14 02:04:12 +0000 | [diff] [blame] | 74 | |
| 75 | void VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 76 | EmitAggLoadOfLValue(E); |
| 77 | } |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 78 | |
| 79 | // Operators. |
| 80 | // case Expr::UnaryOperatorClass: |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 81 | // case Expr::CastExprClass: |
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); |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 87 | void VisitOverloadExpr(const OverloadExpr *E); |
Eli Friedman | 07fa52a | 2008-05-20 07:56:31 +0000 | [diff] [blame] | 88 | void VisitBinComma(const BinaryOperator *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 90 | void VisitObjCMessageExpr(ObjCMessageExpr *E); |
| 91 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 92 | |
| 93 | void VisitConditionalOperator(const ConditionalOperator *CO); |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 94 | void VisitInitListExpr(InitListExpr *E); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 95 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 96 | Visit(DAE->getExpr()); |
| 97 | } |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 98 | void VisitVAArgExpr(VAArgExpr *E); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 99 | |
| 100 | void EmitInitializationToLValue(Expr *E, LValue Address); |
| 101 | void EmitNullInitializationToLValue(LValue Address, QualType T); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 102 | // case Expr::ChooseExprClass: |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 104 | }; |
| 105 | } // end anonymous namespace. |
| 106 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 107 | //===----------------------------------------------------------------------===// |
| 108 | // Utilities |
| 109 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 110 | |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 111 | void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 112 | assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 113 | |
| 114 | // Aggregate assignment turns into llvm.memset. |
| 115 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 116 | if (DestPtr->getType() != BP) |
| 117 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 118 | |
| 119 | // Get size and alignment info for this aggregate. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 120 | std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 121 | |
| 122 | // FIXME: Handle variable sized types. |
| 123 | const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 124 | |
| 125 | llvm::Value *MemSetOps[4] = { |
| 126 | DestPtr, |
| 127 | llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty), |
| 128 | // TypeInfo.first describes size in bits. |
| 129 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
| 130 | llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8) |
| 131 | }; |
| 132 | |
| 133 | Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4); |
| 134 | } |
| 135 | |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 136 | void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr, |
| 137 | llvm::Value *SrcPtr, QualType Ty) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 138 | assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex"); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 139 | |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 140 | // Aggregate assignment turns into llvm.memmove. |
Christopher Lamb | ddc23f3 | 2007-12-17 01:11:20 +0000 | [diff] [blame] | 141 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 142 | if (DestPtr->getType() != BP) |
| 143 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 144 | if (SrcPtr->getType() != BP) |
| 145 | SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); |
| 146 | |
| 147 | // Get size and alignment info for this aggregate. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 148 | std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 149 | |
| 150 | // FIXME: Handle variable sized types. |
| 151 | const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 152 | |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 153 | llvm::Value *MemMoveOps[4] = { |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 154 | DestPtr, SrcPtr, |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 155 | // TypeInfo.first describes size in bits. |
| 156 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 157 | llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8) |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 160 | Builder.CreateCall(CGF.CGM.getMemMoveFn(), MemMoveOps, MemMoveOps+4); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 164 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 165 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 166 | /// then loads the result into DestPtr. |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 167 | void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { |
| 168 | LValue LV = CGF.EmitLValue(E); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 169 | assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc"); |
| 170 | llvm::Value *SrcPtr = LV.getAddress(); |
| 171 | |
| 172 | // If the result is ignored, don't copy from the value. |
| 173 | if (DestPtr == 0) |
| 174 | // FIXME: If the source is volatile, we must read from it. |
| 175 | return; |
| 176 | |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 177 | EmitAggregateCopy(DestPtr, SrcPtr, E->getType()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 180 | //===----------------------------------------------------------------------===// |
| 181 | // Visitor Methods |
| 182 | //===----------------------------------------------------------------------===// |
| 183 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 184 | void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 185 | assert(CGF.getContext().typesAreCompatible( |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 186 | E->getSubExpr()->getType().getUnqualifiedType(), |
| 187 | E->getType().getUnqualifiedType()) && |
| 188 | "Implicit cast types must be compatible"); |
Anders Carlsson | e4707ff | 2008-01-14 06:28:57 +0000 | [diff] [blame] | 189 | Visit(E->getSubExpr()); |
| 190 | } |
| 191 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 192 | void AggExprEmitter::VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 193 | RValue RV = CGF.EmitCallExpr(E); |
| 194 | assert(RV.isAggregate() && "Return value must be aggregate value!"); |
| 195 | |
| 196 | // If the result is ignored, don't copy from the value. |
| 197 | if (DestPtr == 0) |
| 198 | // FIXME: If the source is volatile, we must read from it. |
| 199 | return; |
| 200 | |
| 201 | EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType()); |
| 202 | } |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 203 | |
| 204 | void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame^] | 205 | RValue RV = CGF.EmitObjCMessageExpr(E); |
| 206 | assert(RV.isAggregate() && "Return value must be aggregate value!"); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 207 | |
| 208 | // If the result is ignored, don't copy from the value. |
| 209 | if (DestPtr == 0) |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame^] | 210 | // FIXME: If the source is volatile, we must read from it. |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 211 | return; |
| 212 | |
| 213 | EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType()); |
| 214 | } |
Anders Carlsson | 148fe67 | 2007-10-31 22:04:46 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 216 | void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E) { |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 217 | RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(), |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 218 | E->arg_end(CGF.getContext())); |
| 219 | |
Nate Begeman | 796ef3d | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 220 | assert(RV.isAggregate() && "Return value must be aggregate value!"); |
| 221 | |
| 222 | // If the result is ignored, don't copy from the value. |
| 223 | if (DestPtr == 0) |
| 224 | // FIXME: If the source is volatile, we must read from it. |
| 225 | return; |
| 226 | |
| 227 | EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType()); |
| 228 | } |
| 229 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 230 | void AggExprEmitter::VisitBinComma(const BinaryOperator *E) { |
Eli Friedman | 07fa52a | 2008-05-20 07:56:31 +0000 | [diff] [blame] | 231 | CGF.EmitAnyExpr(E->getLHS()); |
| 232 | CGF.EmitAggExpr(E->getRHS(), DestPtr, false); |
| 233 | } |
| 234 | |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 235 | void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
| 236 | CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest); |
| 237 | } |
| 238 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 239 | void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 240 | CGF.ErrorUnsupported(E, "aggregate binary expression"); |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 243 | void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 244 | // For an assignment to work, the value on the right has |
| 245 | // to be compatible with the value on the left. |
| 246 | assert(CGF.getContext().typesAreCompatible( |
| 247 | E->getLHS()->getType().getUnqualifiedType(), |
| 248 | E->getRHS()->getType().getUnqualifiedType()) |
| 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 | |
| 252 | // Codegen the RHS so that it stores directly into the LHS. |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 253 | CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 254 | |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 255 | if (DestPtr == 0) |
| 256 | return; |
| 257 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 258 | // If the result of the assignment is used, copy the RHS there also. |
Eli Friedman | ff6e2b7 | 2008-02-11 01:09:17 +0000 | [diff] [blame] | 259 | EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 262 | void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 263 | llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?"); |
| 264 | llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:"); |
| 265 | llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont"); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 266 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 267 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 268 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 270 | CGF.EmitBlock(LHSBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 271 | |
| 272 | // Handle the GNU extension for missing LHS. |
| 273 | assert(E->getLHS() && "Must have LHS for aggregate value"); |
| 274 | |
Chris Lattner | c748f27 | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 275 | Visit(E->getLHS()); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 276 | Builder.CreateBr(ContBlock); |
| 277 | LHSBlock = Builder.GetInsertBlock(); |
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(RHSBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 280 | |
Chris Lattner | c748f27 | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 281 | Visit(E->getRHS()); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 282 | Builder.CreateBr(ContBlock); |
| 283 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 285 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 286 | } |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 287 | |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 288 | void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
| 289 | llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress(); |
| 290 | llvm::Value *V = Builder.CreateVAArg(ArgValue, CGF.ConvertType(VE->getType())); |
| 291 | if (DestPtr) |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 292 | // FIXME: volatility |
Eli Friedman | b185124 | 2008-05-27 15:51:49 +0000 | [diff] [blame] | 293 | Builder.CreateStore(V, DestPtr); |
| 294 | } |
| 295 | |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 296 | void AggExprEmitter::EmitNonConstInit(InitListExpr *E) { |
| 297 | |
| 298 | const llvm::PointerType *APType = |
| 299 | cast<llvm::PointerType>(DestPtr->getType()); |
| 300 | const llvm::Type *DestType = APType->getElementType(); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 301 | |
| 302 | if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) { |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 303 | unsigned NumInitElements = E->getNumInits(); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 304 | |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 305 | unsigned i; |
| 306 | for (i = 0; i != NumInitElements; ++i) { |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 307 | llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 308 | Expr *Init = E->getInit(i); |
| 309 | if (isa<InitListExpr>(Init)) |
| 310 | CGF.EmitAggExpr(Init, NextVal, VolatileDest); |
| 311 | else |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 312 | // FIXME: volatility |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 313 | Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // Emit remaining default initializers |
| 317 | unsigned NumArrayElements = AType->getNumElements(); |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 318 | QualType QType = E->getInit(0)->getType(); |
| 319 | const llvm::Type *EType = AType->getElementType(); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 320 | for (/*Do not initialize i*/; i < NumArrayElements; ++i) { |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 321 | llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); |
Dan Gohman | d79a726 | 2008-05-22 22:12:56 +0000 | [diff] [blame] | 322 | if (EType->isSingleValueType()) |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 323 | // FIXME: volatility |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 324 | Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal); |
| 325 | else |
| 326 | EmitAggregateClear(NextVal, QType); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 327 | } |
Lauro Ramos Venancio | 13e22cf | 2008-02-19 22:04:22 +0000 | [diff] [blame] | 328 | } else |
| 329 | assert(false && "Invalid initializer"); |
Lauro Ramos Venancio | 305762c | 2008-02-18 22:44:02 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 332 | void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) { |
| 333 | // FIXME: Are initializers affected by volatile? |
| 334 | if (E->getType()->isComplexType()) { |
| 335 | CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false); |
Eli Friedman | c8ba961 | 2008-05-12 15:06:05 +0000 | [diff] [blame] | 336 | } else if (CGF.hasAggregateLLVMType(E->getType())) { |
| 337 | CGF.EmitAnyExpr(E, LV.getAddress(), false); |
| 338 | } else { |
| 339 | CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType()); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 340 | } |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) { |
| 344 | if (!CGF.hasAggregateLLVMType(T)) { |
| 345 | // For non-aggregates, we can store zero |
Daniel Dunbar | 8239713 | 2008-08-06 05:32:55 +0000 | [diff] [blame] | 346 | llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T)); |
| 347 | CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T); |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 348 | } else { |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 349 | // Otherwise, just memset the whole thing to zero. This is legal |
| 350 | // because in LLVM, all default initializers are guaranteed to have a |
| 351 | // bit pattern of all zeros. |
| 352 | // There's a potential optimization opportunity in combining |
| 353 | // memsets; that would be easy for arrays, but relatively |
| 354 | // difficult for structures with the current code. |
| 355 | llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64); |
| 356 | uint64_t Size = CGF.getContext().getTypeSize(T); |
| 357 | |
| 358 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 359 | llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp"); |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 360 | Builder.CreateCall4(MemSet, DestPtr, |
| 361 | llvm::ConstantInt::get(llvm::Type::Int8Ty, 0), |
| 362 | llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8), |
| 363 | llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 367 | void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { |
| 368 | if (E->isConstantExpr(CGF.getContext(), 0)) { |
| 369 | // FIXME: call into const expr emitter so that we can emit |
| 370 | // a memcpy instead of storing the individual members. |
| 371 | // This is purely for perf; both codepaths lead to equivalent |
| 372 | // (although not necessarily identical) code. |
| 373 | // It's worth noting that LLVM keeps on getting smarter, though, |
| 374 | // so it might not be worth bothering. |
| 375 | } |
| 376 | |
| 377 | // Handle initialization of an array. |
| 378 | if (E->getType()->isArrayType()) { |
| 379 | const llvm::PointerType *APType = |
| 380 | cast<llvm::PointerType>(DestPtr->getType()); |
| 381 | const llvm::ArrayType *AType = |
| 382 | cast<llvm::ArrayType>(APType->getElementType()); |
| 383 | |
| 384 | uint64_t NumInitElements = E->getNumInits(); |
Eli Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 386 | if (E->getNumInits() > 0) { |
| 387 | QualType T1 = E->getType(); |
| 388 | QualType T2 = E->getInit(0)->getType(); |
| 389 | if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() == |
| 390 | CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) { |
| 391 | EmitAggLoadOfLValue(E->getInit(0)); |
| 392 | return; |
| 393 | } |
Eli Friedman | 922696f | 2008-05-19 17:51:16 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 396 | uint64_t NumArrayElements = AType->getNumElements(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 397 | QualType ElementType = CGF.getContext().getCanonicalType(E->getType()); |
| 398 | ElementType =CGF.getContext().getAsArrayType(ElementType)->getElementType(); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 399 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 400 | unsigned CVRqualifier = ElementType.getCVRQualifiers(); |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 401 | |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 402 | for (uint64_t i = 0; i != NumArrayElements; ++i) { |
| 403 | llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array"); |
| 404 | if (i < NumInitElements) |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 405 | EmitInitializationToLValue(E->getInit(i), |
| 406 | LValue::MakeAddr(NextVal, CVRqualifier)); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 407 | else |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 408 | EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier), |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 409 | ElementType); |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 410 | } |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 411 | return; |
| 412 | } |
| 413 | |
| 414 | assert(E->getType()->isRecordType() && "Only support structs/unions here!"); |
| 415 | |
| 416 | // Do struct initialization; this code just sets each individual member |
| 417 | // to the approprate value. This makes bitfield support automatic; |
| 418 | // the disadvantage is that the generated code is more difficult for |
| 419 | // the optimizer, especially with bitfields. |
| 420 | unsigned NumInitElements = E->getNumInits(); |
| 421 | RecordDecl *SD = E->getType()->getAsRecordType()->getDecl(); |
| 422 | unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember(); |
| 423 | unsigned CurInitVal = 0; |
| 424 | bool isUnion = E->getType()->isUnionType(); |
| 425 | |
| 426 | // Here we iterate over the fields; this makes it simpler to both |
| 427 | // default-initialize fields and skip over unnamed fields. |
| 428 | for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) { |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 429 | FieldDecl *CurField = SD->getMember(CurFieldNo); |
| 430 | if (CurField->getIdentifier() == 0) { |
| 431 | // Initializers can't initialize unnamed fields, e.g. "int : 20;" |
| 432 | continue; |
| 433 | } |
Eli Friedman | 1e692ac | 2008-06-13 23:01:12 +0000 | [diff] [blame] | 434 | // FIXME: volatility |
| 435 | LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion,0); |
Chris Lattner | f81557c | 2008-04-04 18:42:16 +0000 | [diff] [blame] | 436 | if (CurInitVal < NumInitElements) { |
| 437 | // Store the initializer into the field |
| 438 | // This will probably have to get a bit smarter when we support |
| 439 | // designators in initializers |
| 440 | EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc); |
| 441 | } else { |
| 442 | // We're out of initalizers; default-initialize to null |
| 443 | EmitNullInitializationToLValue(FieldLoc, CurField->getType()); |
| 444 | } |
| 445 | |
| 446 | // Unions only initialize one field. |
| 447 | // (things can get weird with designators, but they aren't |
| 448 | // supported yet.) |
| 449 | if (E->getType()->isUnionType()) |
| 450 | break; |
Lauro Ramos Venancio | 145cd89 | 2008-02-19 19:27:31 +0000 | [diff] [blame] | 451 | } |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 454 | //===----------------------------------------------------------------------===// |
| 455 | // Entry Points into this File |
| 456 | //===----------------------------------------------------------------------===// |
| 457 | |
| 458 | /// EmitAggExpr - Emit the computation of the specified expression of |
| 459 | /// aggregate type. The result is computed into DestPtr. Note that if |
| 460 | /// DestPtr is null, the value of the aggregate expression is not needed. |
| 461 | void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr, |
| 462 | bool VolatileDest) { |
| 463 | assert(E && hasAggregateLLVMType(E->getType()) && |
| 464 | "Invalid aggregate expression to emit"); |
| 465 | |
| 466 | AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E)); |
| 467 | } |