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   CGBuilderTy &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 EmitNonConstInit(InitListExpr *E);
52 
53   //===--------------------------------------------------------------------===//
54   //                            Visitor Methods
55   //===--------------------------------------------------------------------===//
56 
57   void VisitStmt(Stmt *S) {
58     CGF.ErrorUnsupported(S, "aggregate expression");
59   }
60   void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
61   void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
62 
63   // l-values.
64   void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
65   void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
66   void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
67   void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
68   void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
69       { EmitAggLoadOfLValue(E); }
70 
71   void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
72     EmitAggLoadOfLValue(E);
73   }
74 
75   // Operators.
76   //  case Expr::UnaryOperatorClass:
77   //  case Expr::CastExprClass:
78   void VisitCStyleCastExpr(CStyleCastExpr *E);
79   void VisitImplicitCastExpr(ImplicitCastExpr *E);
80   void VisitCallExpr(const CallExpr *E);
81   void VisitStmtExpr(const StmtExpr *E);
82   void VisitBinaryOperator(const BinaryOperator *BO);
83   void VisitBinAssign(const BinaryOperator *E);
84   void VisitOverloadExpr(const OverloadExpr *E);
85   void VisitBinComma(const BinaryOperator *E);
86 
87   void VisitObjCMessageExpr(ObjCMessageExpr *E);
88   void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
89     EmitAggLoadOfLValue(E);
90   }
91   void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
92   void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
93 
94   void VisitConditionalOperator(const ConditionalOperator *CO);
95   void VisitInitListExpr(InitListExpr *E);
96   void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
97     Visit(DAE->getExpr());
98   }
99   void VisitVAArgExpr(VAArgExpr *E);
100 
101   void EmitInitializationToLValue(Expr *E, LValue Address);
102   void EmitNullInitializationToLValue(LValue Address, QualType T);
103   //  case Expr::ChooseExprClass:
104 
105 };
106 }  // end anonymous namespace.
107 
108 //===----------------------------------------------------------------------===//
109 //                                Utilities
110 //===----------------------------------------------------------------------===//
111 
112 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
113 /// represents a value lvalue, this method emits the address of the lvalue,
114 /// then loads the result into DestPtr.
115 void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
116   LValue LV = CGF.EmitLValue(E);
117   assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
118   llvm::Value *SrcPtr = LV.getAddress();
119 
120   // If the result is ignored, don't copy from the value.
121   if (DestPtr == 0)
122     // FIXME: If the source is volatile, we must read from it.
123     return;
124 
125   CGF.EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
126 }
127 
128 //===----------------------------------------------------------------------===//
129 //                            Visitor Methods
130 //===----------------------------------------------------------------------===//
131 
132 void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
133   // GCC union extension
134   if (E->getType()->isUnionType()) {
135     RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
136     LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *SD->field_begin(), true, 0);
137     EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
138     return;
139   }
140 
141   Visit(E->getSubExpr());
142 }
143 
144 void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
145   assert(CGF.getContext().typesAreCompatible(
146                           E->getSubExpr()->getType().getUnqualifiedType(),
147                           E->getType().getUnqualifiedType()) &&
148          "Implicit cast types must be compatible");
149   Visit(E->getSubExpr());
150 }
151 
152 void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
153   RValue RV = CGF.EmitCallExpr(E);
154   assert(RV.isAggregate() && "Return value must be aggregate value!");
155 
156   // If the result is ignored, don't copy from the value.
157   if (DestPtr == 0)
158     // FIXME: If the source is volatile, we must read from it.
159     return;
160 
161   CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
162 }
163 
164 void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
165   RValue RV = CGF.EmitObjCMessageExpr(E);
166   assert(RV.isAggregate() && "Return value must be aggregate value!");
167 
168   // If the result is ignored, don't copy from the value.
169   if (DestPtr == 0)
170     // FIXME: If the source is volatile, we must read from it.
171     return;
172 
173   CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
174 }
175 
176 void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
177   RValue RV = CGF.EmitObjCPropertyGet(E);
178   assert(RV.isAggregate() && "Return value must be aggregate value!");
179 
180   // If the result is ignored, don't copy from the value.
181   if (DestPtr == 0)
182     // FIXME: If the source is volatile, we must read from it.
183     return;
184 
185   CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
186 }
187 
188 void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
189   RValue RV = CGF.EmitObjCPropertyGet(E);
190   assert(RV.isAggregate() && "Return value must be aggregate value!");
191 
192   // If the result is ignored, don't copy from the value.
193   if (DestPtr == 0)
194     // FIXME: If the source is volatile, we must read from it.
195     return;
196 
197   CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
198 }
199 
200 void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E) {
201   RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
202                                E->arg_end(CGF.getContext()));
203 
204   assert(RV.isAggregate() && "Return value must be aggregate value!");
205 
206   // If the result is ignored, don't copy from the value.
207   if (DestPtr == 0)
208     // FIXME: If the source is volatile, we must read from it.
209     return;
210 
211   CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
212 }
213 
214 void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
215   CGF.EmitAnyExpr(E->getLHS());
216   CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
217 }
218 
219 void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
220   CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
221 }
222 
223 void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
224   CGF.ErrorUnsupported(E, "aggregate binary expression");
225 }
226 
227 void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
228   // For an assignment to work, the value on the right has
229   // to be compatible with the value on the left.
230   assert(CGF.getContext().typesAreCompatible(
231              E->getLHS()->getType().getUnqualifiedType(),
232              E->getRHS()->getType().getUnqualifiedType())
233          && "Invalid assignment");
234   LValue LHS = CGF.EmitLValue(E->getLHS());
235 
236   // We have to special case property setters, otherwise we must have
237   // a simple lvalue (no aggregates inside vectors, bitfields).
238   if (LHS.isPropertyRef()) {
239     // FIXME: Volatility?
240     llvm::Value *AggLoc = DestPtr;
241     if (!AggLoc)
242       AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
243     CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
244     CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
245                             RValue::getAggregate(AggLoc));
246   }
247   else if (LHS.isKVCRef()) {
248     // FIXME: Volatility?
249     llvm::Value *AggLoc = DestPtr;
250     if (!AggLoc)
251       AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
252     CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
253     CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
254                             RValue::getAggregate(AggLoc));
255   } else {
256     // Codegen the RHS so that it stores directly into the LHS.
257     CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
258 
259     if (DestPtr == 0)
260       return;
261 
262     // If the result of the assignment is used, copy the RHS there also.
263     CGF.EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
264   }
265 }
266 
267 void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
268   llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
269   llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
270   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
271 
272   llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
273   Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
274 
275   CGF.EmitBlock(LHSBlock);
276 
277   // Handle the GNU extension for missing LHS.
278   assert(E->getLHS() && "Must have LHS for aggregate value");
279 
280   Visit(E->getLHS());
281   CGF.EmitBranch(ContBlock);
282 
283   CGF.EmitBlock(RHSBlock);
284 
285   Visit(E->getRHS());
286   CGF.EmitBranch(ContBlock);
287 
288   CGF.EmitBlock(ContBlock);
289 }
290 
291 void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
292   llvm::Value *ArgValue = CGF.EmitLValue(VE->getSubExpr()).getAddress();
293   llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
294 
295   if (!ArgPtr) {
296     CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
297     return;
298   }
299 
300   if (DestPtr)
301     // FIXME: volatility
302     CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
303 }
304 
305 void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
306   const llvm::PointerType *APType =
307     cast<llvm::PointerType>(DestPtr->getType());
308   const llvm::Type *DestType = APType->getElementType();
309 
310   if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
311     unsigned NumInitElements = E->getNumInits();
312 
313     unsigned i;
314     for (i = 0; i != NumInitElements; ++i) {
315       llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
316       Expr *Init = E->getInit(i);
317       if (isa<InitListExpr>(Init))
318         CGF.EmitAggExpr(Init, NextVal, VolatileDest);
319       else
320         // FIXME: volatility
321         Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
322     }
323 
324     // Emit remaining default initializers
325     unsigned NumArrayElements = AType->getNumElements();
326     QualType QType = E->getInit(0)->getType();
327     const llvm::Type *EType = AType->getElementType();
328     for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
329       llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
330       if (EType->isSingleValueType())
331         // FIXME: volatility
332         Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
333       else
334         CGF.EmitAggregateClear(NextVal, QType);
335     }
336   } else
337     assert(false && "Invalid initializer");
338 }
339 
340 void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
341   // FIXME: Are initializers affected by volatile?
342   if (isa<ImplicitValueInitExpr>(E)) {
343     EmitNullInitializationToLValue(LV, E->getType());
344   } else if (E->getType()->isComplexType()) {
345     CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
346   } else if (CGF.hasAggregateLLVMType(E->getType())) {
347     CGF.EmitAnyExpr(E, LV.getAddress(), false);
348   } else {
349     CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
350   }
351 }
352 
353 void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
354   if (!CGF.hasAggregateLLVMType(T)) {
355     // For non-aggregates, we can store zero
356     llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
357     CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
358   } else {
359     // Otherwise, just memset the whole thing to zero.  This is legal
360     // because in LLVM, all default initializers are guaranteed to have a
361     // bit pattern of all zeros.
362     // There's a potential optimization opportunity in combining
363     // memsets; that would be easy for arrays, but relatively
364     // difficult for structures with the current code.
365     const llvm::Type *SizeTy = llvm::Type::Int64Ty;
366     llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset,
367                                                &SizeTy, 1);
368     uint64_t Size = CGF.getContext().getTypeSize(T);
369 
370     const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
371     llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
372     Builder.CreateCall4(MemSet, DestPtr,
373                         llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
374                         llvm::ConstantInt::get(SizeTy, Size/8),
375                         llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
376   }
377 }
378 
379 void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
380 #if 0
381   // FIXME: Disabled while we figure out what to do about
382   // test/CodeGen/bitfield.c
383   //
384   // If we can, prefer a copy from a global; this is a lot less
385   // code for long globals, and it's easier for the current optimizers
386   // to analyze.
387   // FIXME: Should we really be doing this? Should we try to avoid
388   // cases where we emit a global with a lot of zeros?  Should
389   // we try to avoid short globals?
390   if (E->isConstantInitializer(CGF.getContext(), 0)) {
391     llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
392     llvm::GlobalVariable* GV =
393     new llvm::GlobalVariable(C->getType(), true,
394                              llvm::GlobalValue::InternalLinkage,
395                              C, "", &CGF.CGM.getModule(), 0);
396     CGF.EmitAggregateCopy(DestPtr, GV, E->getType());
397     return;
398   }
399 #endif
400   // Handle initialization of an array.
401   if (E->getType()->isArrayType()) {
402     const llvm::PointerType *APType =
403       cast<llvm::PointerType>(DestPtr->getType());
404     const llvm::ArrayType *AType =
405       cast<llvm::ArrayType>(APType->getElementType());
406 
407     uint64_t NumInitElements = E->getNumInits();
408 
409     if (E->getNumInits() > 0) {
410       QualType T1 = E->getType();
411       QualType T2 = E->getInit(0)->getType();
412       if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
413           CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
414         EmitAggLoadOfLValue(E->getInit(0));
415         return;
416       }
417     }
418 
419     uint64_t NumArrayElements = AType->getNumElements();
420     QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
421     ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
422 
423     unsigned CVRqualifier = ElementType.getCVRQualifiers();
424 
425     for (uint64_t i = 0; i != NumArrayElements; ++i) {
426       llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
427       if (i < NumInitElements)
428         EmitInitializationToLValue(E->getInit(i),
429                                    LValue::MakeAddr(NextVal, CVRqualifier));
430       else
431         EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
432                                        ElementType);
433     }
434     return;
435   }
436 
437   assert(E->getType()->isRecordType() && "Only support structs/unions here!");
438 
439   // Do struct initialization; this code just sets each individual member
440   // to the approprate value.  This makes bitfield support automatic;
441   // the disadvantage is that the generated code is more difficult for
442   // the optimizer, especially with bitfields.
443   unsigned NumInitElements = E->getNumInits();
444   RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
445   unsigned CurInitVal = 0;
446 
447   if (E->getType()->isUnionType()) {
448     // Only initialize one field of a union. The field itself is
449     // specified by the initializer list.
450     if (!E->getInitializedFieldInUnion()) {
451       // Empty union; we have nothing to do.
452 
453 #ifndef NDEBUG
454       // Make sure that it's really an empty and not a failure of
455       // semantic analysis.
456       for (RecordDecl::field_iterator Field = SD->field_begin(),
457                                    FieldEnd = SD->field_end();
458            Field != FieldEnd; ++Field)
459         assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
460 #endif
461       return;
462     }
463 
464     // FIXME: volatility
465     FieldDecl *Field = E->getInitializedFieldInUnion();
466     LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
467 
468     if (NumInitElements) {
469       // Store the initializer into the field
470       EmitInitializationToLValue(E->getInit(0), FieldLoc);
471     } else {
472       // Default-initialize to null
473       EmitNullInitializationToLValue(FieldLoc, Field->getType());
474     }
475 
476     return;
477   }
478 
479   // Here we iterate over the fields; this makes it simpler to both
480   // default-initialize fields and skip over unnamed fields.
481   for (RecordDecl::field_iterator Field = SD->field_begin(),
482                                FieldEnd = SD->field_end();
483        Field != FieldEnd; ++Field) {
484     // We're done once we hit the flexible array member
485     if (Field->getType()->isIncompleteArrayType())
486       break;
487 
488     if (Field->isUnnamedBitfield())
489       continue;
490 
491     // FIXME: volatility
492     LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
493     if (CurInitVal < NumInitElements) {
494       // Store the initializer into the field
495       EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
496     } else {
497       // We're out of initalizers; default-initialize to null
498       EmitNullInitializationToLValue(FieldLoc, Field->getType());
499     }
500   }
501 }
502 
503 //===----------------------------------------------------------------------===//
504 //                        Entry Points into this File
505 //===----------------------------------------------------------------------===//
506 
507 /// EmitAggExpr - Emit the computation of the specified expression of
508 /// aggregate type.  The result is computed into DestPtr.  Note that if
509 /// DestPtr is null, the value of the aggregate expression is not needed.
510 void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
511                                   bool VolatileDest) {
512   assert(E && hasAggregateLLVMType(E->getType()) &&
513          "Invalid aggregate expression to emit");
514 
515   AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
516 }
517 
518 void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
519   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
520 
521   EmitMemSetToZero(DestPtr, Ty);
522 }
523 
524 void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
525                                         llvm::Value *SrcPtr, QualType Ty) {
526   assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
527 
528   // Aggregate assignment turns into llvm.memmove.
529   const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
530   if (DestPtr->getType() != BP)
531     DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
532   if (SrcPtr->getType() != BP)
533     SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
534 
535   // Get size and alignment info for this aggregate.
536   std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
537 
538   // FIXME: Handle variable sized types.
539   const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
540 
541   Builder.CreateCall4(CGM.getMemMoveFn(),
542                       DestPtr, SrcPtr,
543                       // TypeInfo.first describes size in bits.
544                       llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
545                       llvm::ConstantInt::get(llvm::Type::Int32Ty,
546                                              TypeInfo.second/8));
547 }
548