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