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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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 | af6f528 | 2007-08-10 20:13:28 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Aggregate Expression Emitter |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | namespace { |
| 29 | class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> { |
| 30 | CodeGenFunction &CGF; |
Devang Patel | 50c9034 | 2007-10-09 19:49:58 +0000 | [diff] [blame] | 31 | llvm::LLVMFoldingBuilder &Builder; |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 32 | llvm::Value *DestPtr; |
| 33 | bool VolatileDest; |
| 34 | public: |
| 35 | AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest) |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 36 | : CGF(cgf), Builder(CGF.Builder), |
| 37 | DestPtr(destPtr), VolatileDest(volatileDest) { |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 40 | //===--------------------------------------------------------------------===// |
| 41 | // Utilities |
| 42 | //===--------------------------------------------------------------------===// |
| 43 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 44 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 45 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 46 | /// then loads the result into DestPtr. |
| 47 | void EmitAggLoadOfLValue(const Expr *E); |
| 48 | |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 49 | void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
| 50 | QualType EltTy); |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 51 | |
| 52 | //===--------------------------------------------------------------------===// |
| 53 | // Visitor Methods |
| 54 | //===--------------------------------------------------------------------===// |
| 55 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 56 | void VisitStmt(Stmt *S) { |
| 57 | fprintf(stderr, "Unimplemented agg expr!\n"); |
Chris Lattner | 419ea7e | 2007-09-13 01:17:29 +0000 | [diff] [blame] | 58 | S->dump(CGF.getContext().SourceMgr); |
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. |
| 63 | void VisitDeclRefExpr(DeclRefExpr *DRE) { return EmitAggLoadOfLValue(DRE); } |
| 64 | // case Expr::ArraySubscriptExprClass: |
| 65 | |
| 66 | // Operators. |
| 67 | // case Expr::UnaryOperatorClass: |
| 68 | // case Expr::ImplicitCastExprClass: |
| 69 | // case Expr::CastExprClass: |
| 70 | // case Expr::CallExprClass: |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 71 | void VisitStmtExpr(const StmtExpr *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 72 | void VisitBinaryOperator(const BinaryOperator *BO); |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 73 | void VisitBinAssign(const BinaryOperator *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | void VisitConditionalOperator(const ConditionalOperator *CO); |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame^] | 77 | void VisitInitListExpr(InitListExpr *E); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 78 | // case Expr::ChooseExprClass: |
| 79 | }; |
| 80 | } // end anonymous namespace. |
| 81 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 82 | //===----------------------------------------------------------------------===// |
| 83 | // Utilities |
| 84 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 85 | |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 86 | void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr, |
| 87 | llvm::Value *SrcPtr, QualType Ty) { |
| 88 | assert(!Ty->isComplexType() && "Shouldn't happen for complex"); |
| 89 | |
| 90 | // Aggregate assignment turns into llvm.memcpy. |
| 91 | const llvm::Type *BP = llvm::PointerType::get(llvm::Type::Int8Ty); |
| 92 | if (DestPtr->getType() != BP) |
| 93 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 94 | if (SrcPtr->getType() != BP) |
| 95 | SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp"); |
| 96 | |
| 97 | // Get size and alignment info for this aggregate. |
| 98 | std::pair<uint64_t, unsigned> TypeInfo = |
| 99 | CGF.getContext().getTypeInfo(Ty, SourceLocation()); |
| 100 | |
| 101 | // FIXME: Handle variable sized types. |
| 102 | const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 103 | |
| 104 | llvm::Value *MemCpyOps[4] = { |
| 105 | DestPtr, SrcPtr, |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame^] | 106 | // TypeInfo.first describes size in bits. |
| 107 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 108 | llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second) |
| 109 | }; |
| 110 | |
| 111 | Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4); |
| 112 | } |
| 113 | |
| 114 | |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 115 | /// EmitAggLoadOfLValue - Given an expression with aggregate type that |
| 116 | /// represents a value lvalue, this method emits the address of the lvalue, |
| 117 | /// then loads the result into DestPtr. |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 118 | void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) { |
| 119 | LValue LV = CGF.EmitLValue(E); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 120 | assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc"); |
| 121 | llvm::Value *SrcPtr = LV.getAddress(); |
| 122 | |
| 123 | // If the result is ignored, don't copy from the value. |
| 124 | if (DestPtr == 0) |
| 125 | // FIXME: If the source is volatile, we must read from it. |
| 126 | return; |
| 127 | |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 128 | EmitAggregateCopy(DestPtr, SrcPtr, E->getType()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 131 | //===----------------------------------------------------------------------===// |
| 132 | // Visitor Methods |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | |
Chris Lattner | b2d963f | 2007-08-31 22:54:14 +0000 | [diff] [blame] | 135 | void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
| 136 | CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest); |
| 137 | } |
| 138 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 139 | void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 140 | fprintf(stderr, "Unimplemented aggregate binary expr!\n"); |
Chris Lattner | 419ea7e | 2007-09-13 01:17:29 +0000 | [diff] [blame] | 141 | E->dump(CGF.getContext().SourceMgr); |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | 03d6fb9 | 2007-08-21 04:43:17 +0000 | [diff] [blame] | 144 | void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 145 | assert(E->getLHS()->getType().getCanonicalType() == |
| 146 | E->getRHS()->getType().getCanonicalType() && "Invalid assignment"); |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 147 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 148 | |
| 149 | // Codegen the RHS so that it stores directly into the LHS. |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 150 | CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 151 | |
| 152 | // If the result of the assignment is used, copy the RHS there also. |
| 153 | if (DestPtr) { |
| 154 | assert(0 && "FIXME: Chained agg assignment not implemented yet"); |
| 155 | } |
| 156 | } |
| 157 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 158 | void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) { |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 159 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 160 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 161 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 162 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 163 | llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 164 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 166 | CGF.EmitBlock(LHSBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 167 | |
| 168 | // Handle the GNU extension for missing LHS. |
| 169 | assert(E->getLHS() && "Must have LHS for aggregate value"); |
| 170 | |
Chris Lattner | c748f27 | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 171 | Visit(E->getLHS()); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 172 | Builder.CreateBr(ContBlock); |
| 173 | LHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 175 | CGF.EmitBlock(RHSBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 176 | |
Chris Lattner | c748f27 | 2007-08-21 05:02:10 +0000 | [diff] [blame] | 177 | Visit(E->getRHS()); |
Chris Lattner | bfc0c1a | 2007-08-26 23:13:56 +0000 | [diff] [blame] | 178 | Builder.CreateBr(ContBlock); |
| 179 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 9c03356 | 2007-08-21 04:25:47 +0000 | [diff] [blame] | 181 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 182 | } |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 183 | |
Devang Patel | 636c3d0 | 2007-10-26 17:44:44 +0000 | [diff] [blame^] | 184 | void AggExprEmitter::VisitInitListExpr(InitListExpr *E) { |
| 185 | |
| 186 | unsigned NumInitElements = E->getNumInits(); |
| 187 | |
| 188 | assert ( E->getType()->isArrayType() |
| 189 | && "Only Array initializers are supported"); |
| 190 | |
| 191 | std::vector<llvm::Constant*> ArrayElts; |
| 192 | const llvm::PointerType *APType = cast<llvm::PointerType>(DestPtr->getType()); |
| 193 | const llvm::ArrayType *AType = cast<llvm::ArrayType>(APType->getElementType()); |
| 194 | |
| 195 | // Copy initializer elements. |
| 196 | bool AllConstElements = true; |
| 197 | unsigned i = 0; |
| 198 | for (i = 0; i < NumInitElements; ++i) { |
| 199 | if (llvm::Constant *C = dyn_cast<llvm::Constant>(CGF.EmitScalarExpr(E->getInit(i)))) |
| 200 | ArrayElts.push_back(C); |
| 201 | else { |
| 202 | AllConstElements = false; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | unsigned NumArrayElements = AType->getNumElements(); |
| 208 | const llvm::Type *ElementType = CGF.ConvertType(E->getInit(0)->getType()); |
| 209 | |
| 210 | if (AllConstElements) { |
| 211 | // Initialize remaining array elements. |
| 212 | for (/*Do not initialize i*/; i < NumArrayElements; ++i) |
| 213 | ArrayElts.push_back(llvm::Constant::getNullValue(ElementType)); |
| 214 | |
| 215 | // Create global value to hold this array. |
| 216 | llvm::Constant *V = llvm::ConstantArray::get(AType, ArrayElts); |
| 217 | V = new llvm::GlobalVariable(V->getType(), true, |
| 218 | llvm::GlobalValue::InternalLinkage, |
| 219 | V, ".array", |
| 220 | &CGF.CGM.getModule()); |
| 221 | |
| 222 | EmitAggregateCopy(DestPtr, V , E->getType()); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // Emit indiviudal array element stores. |
| 227 | unsigned index = 0; |
| 228 | llvm::Value *NextVal = NULL; |
| 229 | llvm::Value *Idxs[] = { |
| 230 | llvm::Constant::getNullValue(llvm::Type::Int32Ty), |
| 231 | NULL |
| 232 | }; |
| 233 | |
| 234 | // Emit already seen constants initializers. |
| 235 | for (i = 0; i < ArrayElts.size(); i++) { |
| 236 | Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, index++); |
| 237 | NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2, ".array"); |
| 238 | Builder.CreateStore(ArrayElts[i], NextVal); |
| 239 | } |
| 240 | |
| 241 | // Emit remaining initializers |
| 242 | for (/*Do not initizalize i*/; i < NumInitElements; ++i) { |
| 243 | Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, index++); |
| 244 | NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2, ".array"); |
| 245 | llvm::Value *V = CGF.EmitScalarExpr(E->getInit(i)); |
| 246 | Builder.CreateStore(V, NextVal); |
| 247 | } |
| 248 | |
| 249 | // Emit remaining default initializers |
| 250 | for (/*Do not initialize i*/; i < NumArrayElements; ++i) { |
| 251 | Idxs[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, index++); |
| 252 | NextVal = Builder.CreateGEP(DestPtr, Idxs, Idxs + 2, ".array"); |
| 253 | Builder.CreateStore(llvm::Constant::getNullValue(ElementType), NextVal); |
| 254 | } |
| 255 | } |
| 256 | |
Chris Lattner | ee755f9 | 2007-08-21 04:59:27 +0000 | [diff] [blame] | 257 | //===----------------------------------------------------------------------===// |
| 258 | // Entry Points into this File |
| 259 | //===----------------------------------------------------------------------===// |
| 260 | |
| 261 | /// EmitAggExpr - Emit the computation of the specified expression of |
| 262 | /// aggregate type. The result is computed into DestPtr. Note that if |
| 263 | /// DestPtr is null, the value of the aggregate expression is not needed. |
| 264 | void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr, |
| 265 | bool VolatileDest) { |
| 266 | assert(E && hasAggregateLLVMType(E->getType()) && |
| 267 | "Invalid aggregate expression to emit"); |
| 268 | |
| 269 | AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E)); |
| 270 | } |