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