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