blob: f9cb607611de94022420df84587c3e0f1303c868 [file] [log] [blame]
Chris Lattner566b6ce2007-08-24 02:22:53 +00001//===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===//
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes with complex types as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/StmtVisitor.h"
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Chris Lattner6b5d0bf2007-08-26 22:47:40 +000020#include "llvm/ADT/SmallString.h"
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000021#include "llvm/Support/Compiler.h"
22using namespace clang;
23using namespace CodeGen;
24
25//===----------------------------------------------------------------------===//
Chris Lattner05ba49c2007-08-21 05:54:53 +000026// Complex Expression Emitter
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000027//===----------------------------------------------------------------------===//
28
Chris Lattner58dee102007-08-21 16:57:55 +000029typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000030
31namespace {
32class VISIBILITY_HIDDEN ComplexExprEmitter
33 : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
34 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000035 CGBuilderTy &Builder;
Mike Stump7f79f9b2009-05-29 15:46:01 +000036 // True is we should ignore the value of a
37 bool IgnoreReal;
38 bool IgnoreImag;
39 // True if we should ignore the value of a=b
40 bool IgnoreRealAssign;
41 bool IgnoreImagAssign;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000042public:
Mike Stump7f79f9b2009-05-29 15:46:01 +000043 ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false,
44 bool irn=false, bool iin=false)
45 : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii),
46 IgnoreRealAssign(irn), IgnoreImagAssign(iin) {
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000047 }
48
49
50 //===--------------------------------------------------------------------===//
51 // Utilities
52 //===--------------------------------------------------------------------===//
53
Mike Stump7f79f9b2009-05-29 15:46:01 +000054 bool TestAndClearIgnoreReal() {
55 bool I = IgnoreReal;
56 IgnoreReal = false;
57 return I;
58 }
59 bool TestAndClearIgnoreImag() {
60 bool I = IgnoreImag;
61 IgnoreImag = false;
62 return I;
63 }
64 bool TestAndClearIgnoreRealAssign() {
65 bool I = IgnoreRealAssign;
66 IgnoreRealAssign = false;
67 return I;
68 }
69 bool TestAndClearIgnoreImagAssign() {
70 bool I = IgnoreImagAssign;
71 IgnoreImagAssign = false;
72 return I;
73 }
74
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000075 /// EmitLoadOfLValue - Given an expression with complex type that represents a
76 /// value l-value, this method emits the address of the l-value, then loads
77 /// and returns the result.
Chris Lattner46d7d9f2007-08-21 17:28:34 +000078 ComplexPairTy EmitLoadOfLValue(const Expr *E) {
Chris Lattner76e80332007-08-21 18:02:02 +000079 LValue LV = CGF.EmitLValue(E);
Daniel Dunbar42f963d2009-06-10 04:38:50 +000080 if (LV.isSimple())
81 return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified());
82
83 if (LV.isPropertyRef())
84 return CGF.EmitObjCPropertyGet(LV.getPropertyRefExpr()).getComplexVal();
85
86 assert(LV.isKVCRef() && "Unknown LValue type!");
87 return CGF.EmitObjCPropertyGet(LV.getKVCRefExpr()).getComplexVal();
Chris Lattner46d7d9f2007-08-21 17:28:34 +000088 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000089
Chris Lattner46d7d9f2007-08-21 17:28:34 +000090 /// EmitLoadOfComplex - Given a pointer to a complex value, emit code to load
91 /// the real and imaginary pieces.
Chris Lattner76e80332007-08-21 18:02:02 +000092 ComplexPairTy EmitLoadOfComplex(llvm::Value *SrcPtr, bool isVolatile);
Chris Lattner46d7d9f2007-08-21 17:28:34 +000093
94 /// EmitStoreOfComplex - Store the specified real/imag parts into the
95 /// specified value pointer.
Chris Lattner76e80332007-08-21 18:02:02 +000096 void EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *ResPtr, bool isVol);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000097
Chris Lattnerab340c22007-08-26 22:09:01 +000098 /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
99 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
100 QualType DestType);
101
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000102 //===--------------------------------------------------------------------===//
103 // Visitor Methods
104 //===--------------------------------------------------------------------===//
Chris Lattner776c6492007-08-21 18:51:13 +0000105
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000106 ComplexPairTy VisitStmt(Stmt *S) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000107 S->dump(CGF.getContext().getSourceManager());
Chris Lattner776c6492007-08-21 18:51:13 +0000108 assert(0 && "Stmt can't have complex result type!");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000109 return ComplexPairTy();
110 }
Chris Lattner776c6492007-08-21 18:51:13 +0000111 ComplexPairTy VisitExpr(Expr *S);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000112 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
Chris Lattnerd272ff02007-08-26 05:57:57 +0000113 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000114
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000115 // l-values.
Chris Lattnerd272ff02007-08-26 05:57:57 +0000116 ComplexPairTy VisitDeclRefExpr(const Expr *E) { return EmitLoadOfLValue(E); }
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000117 ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
118 return EmitLoadOfLValue(E);
119 }
120 ComplexPairTy VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
121 return EmitLoadOfLValue(E);
122 }
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000123 ComplexPairTy VisitObjCImplicitSetterGetterRefExpr(
124 ObjCImplicitSetterGetterRefExpr *E) {
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000125 return EmitLoadOfLValue(E);
126 }
127 ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
128 return CGF.EmitObjCMessageExpr(E).getComplexVal();
129 }
Chris Lattner8bcd7232007-08-21 18:03:58 +0000130 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerd272ff02007-08-26 05:57:57 +0000131 ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000132
Chris Lattnere73e8e22007-08-21 22:33:41 +0000133 // FIXME: CompoundLiteralExpr
Chris Lattnerd272ff02007-08-26 05:57:57 +0000134
135 ComplexPairTy EmitCast(Expr *Op, QualType DestTy);
136 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
137 // Unlike for scalars, we don't have to worry about function->ptr demotion
138 // here.
139 return EmitCast(E->getSubExpr(), E->getType());
140 }
141 ComplexPairTy VisitCastExpr(CastExpr *E) {
142 return EmitCast(E->getSubExpr(), E->getType());
143 }
Chris Lattner419d25e2007-08-23 21:38:16 +0000144 ComplexPairTy VisitCallExpr(const CallExpr *E);
Chris Lattner7e20d092007-08-31 22:51:38 +0000145 ComplexPairTy VisitStmtExpr(const StmtExpr *E);
146
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000147 // Operators.
Chris Lattner3070f982007-08-21 22:25:29 +0000148 ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
149 bool isInc, bool isPre);
150 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
151 return VisitPrePostIncDec(E, false, false);
152 }
153 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
154 return VisitPrePostIncDec(E, true, false);
155 }
156 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
157 return VisitPrePostIncDec(E, false, true);
158 }
159 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
160 return VisitPrePostIncDec(E, true, true);
161 }
162 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000163 ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000164 TestAndClearIgnoreReal();
165 TestAndClearIgnoreImag();
166 TestAndClearIgnoreRealAssign();
167 TestAndClearIgnoreImagAssign();
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000168 return Visit(E->getSubExpr());
169 }
170 ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
Chris Lattnere98a11c2007-08-21 20:41:44 +0000171 ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
Sebastian Redl05189992008-11-11 17:56:53 +0000172 // LNot,Real,Imag never return complex.
Chris Lattnere98a11c2007-08-21 20:41:44 +0000173 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
174 return Visit(E->getSubExpr());
175 }
Chris Lattner04421082008-04-08 04:40:51 +0000176 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
177 return Visit(DAE->getExpr());
178 }
Anders Carlsson4b76b4f2009-05-31 00:12:05 +0000179 ComplexPairTy VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
180 return CGF.EmitCXXExprWithTemporaries(E).getComplexVal();
181 }
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000182 ComplexPairTy VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
183 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
184 QualType Elem = E->getType()->getAsComplexType()->getElementType();
Owen Anderson69243822009-07-13 04:10:07 +0000185 llvm::Constant *Null =
Owen Andersonc9c88b42009-07-31 20:28:54 +0000186 llvm::Constant::getNullValue(CGF.ConvertType(Elem));
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000187 return ComplexPairTy(Null, Null);
188 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000189 ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
190 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
191 QualType Elem = E->getType()->getAsComplexType()->getElementType();
Owen Anderson69243822009-07-13 04:10:07 +0000192 llvm::Constant *Null =
Owen Andersonc9c88b42009-07-31 20:28:54 +0000193 llvm::Constant::getNullValue(CGF.ConvertType(Elem));
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000194 return ComplexPairTy(Null, Null);
195 }
Chris Lattnere98a11c2007-08-21 20:41:44 +0000196
Chris Lattnerab340c22007-08-26 22:09:01 +0000197 struct BinOpInfo {
198 ComplexPairTy LHS;
199 ComplexPairTy RHS;
200 QualType Ty; // Computation Type.
201 };
202
203 BinOpInfo EmitBinOps(const BinaryOperator *E);
204 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
205 ComplexPairTy (ComplexExprEmitter::*Func)
206 (const BinOpInfo &));
207
208 ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
209 ComplexPairTy EmitBinSub(const BinOpInfo &Op);
210 ComplexPairTy EmitBinMul(const BinOpInfo &Op);
211 ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
212
213 ComplexPairTy VisitBinMul(const BinaryOperator *E) {
214 return EmitBinMul(EmitBinOps(E));
215 }
216 ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
217 return EmitBinAdd(EmitBinOps(E));
218 }
219 ComplexPairTy VisitBinSub(const BinaryOperator *E) {
220 return EmitBinSub(EmitBinOps(E));
221 }
222 ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
223 return EmitBinDiv(EmitBinOps(E));
224 }
225
226 // Compound assignments.
227 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
228 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
229 }
230 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
231 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
232 }
233 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
234 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
235 }
236 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
237 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
238 }
239
Chris Lattner612c40c2007-08-26 21:27:07 +0000240 // GCC rejects rem/and/or/xor for integer complex.
Chris Lattner4034edb2007-08-21 17:12:50 +0000241 // Logical and/or always return int, never complex.
Chris Lattner58dee102007-08-21 16:57:55 +0000242
243 // No comparisons produce a complex result.
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000244 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
Chris Lattner756a4d82007-08-21 17:15:50 +0000245 ComplexPairTy VisitBinComma (const BinaryOperator *E);
246
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000247
248 ComplexPairTy VisitConditionalOperator(const ConditionalOperator *CO);
Chris Lattnerd0b12032007-08-24 02:18:47 +0000249 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
Eli Friedmanc4777f12008-05-13 23:11:35 +0000250
251 ComplexPairTy VisitInitListExpr(InitListExpr *E);
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000252
253 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000254};
255} // end anonymous namespace.
256
257//===----------------------------------------------------------------------===//
258// Utilities
259//===----------------------------------------------------------------------===//
260
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000261/// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
262/// load the real and imaginary pieces, returning them as Real/Imag.
Chris Lattner76e80332007-08-21 18:02:02 +0000263ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr,
264 bool isVolatile) {
Daniel Dunbar0d9c54c2009-07-26 08:32:30 +0000265 llvm::SmallString<64> Name(SrcPtr->getName().begin(),
266 SrcPtr->getName().end());
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000267
Mike Stump7f79f9b2009-05-29 15:46:01 +0000268 llvm::Value *Real=0, *Imag=0;
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000269
Mike Stump7f79f9b2009-05-29 15:46:01 +0000270 if (!IgnoreReal) {
Daniel Dunbar77659342009-08-19 20:04:03 +0000271 // FIXME: Clean this up once builder takes Twine/StringRef.
Mike Stump7f79f9b2009-05-29 15:46:01 +0000272 Name += ".realp";
Daniel Dunbar77659342009-08-19 20:04:03 +0000273 llvm::Value *RealPtr = Builder.CreateStructGEP(SrcPtr, 0,
274 Name.str().str().c_str());
Ted Kremenekd6278892007-09-04 17:20:08 +0000275
Mike Stump7f79f9b2009-05-29 15:46:01 +0000276 Name.pop_back(); // .realp -> .real
Daniel Dunbar77659342009-08-19 20:04:03 +0000277 // FIXME: Clean this up once builder takes Twine/StringRef.
278 Real = Builder.CreateLoad(RealPtr, isVolatile,
279 Name.str().str().c_str());
Mike Stump7f79f9b2009-05-29 15:46:01 +0000280 Name.resize(Name.size()-4); // .real -> .imagp
281 }
282
283 if (!IgnoreImag) {
284 Name += "imagp";
285
Daniel Dunbar77659342009-08-19 20:04:03 +0000286 // FIXME: Clean this up once builder takes Twine/StringRef.
287 llvm::Value *ImagPtr = Builder.CreateStructGEP(SrcPtr, 1,
288 Name.str().str().c_str());
Mike Stump7f79f9b2009-05-29 15:46:01 +0000289
290 Name.pop_back(); // .imagp -> .imag
Daniel Dunbar77659342009-08-19 20:04:03 +0000291 // FIXME: Clean this up once builder takes Twine/StringRef.
292 Imag = Builder.CreateLoad(ImagPtr, isVolatile, Name.str().str().c_str());
Mike Stump7f79f9b2009-05-29 15:46:01 +0000293 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000294 return ComplexPairTy(Real, Imag);
295}
296
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000297/// EmitStoreOfComplex - Store the specified real/imag parts into the
298/// specified value pointer.
Chris Lattner76e80332007-08-21 18:02:02 +0000299void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr,
300 bool isVolatile) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000301 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real");
302 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag");
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000303
Chris Lattner76e80332007-08-21 18:02:02 +0000304 Builder.CreateStore(Val.first, RealPtr, isVolatile);
305 Builder.CreateStore(Val.second, ImagPtr, isVolatile);
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000306}
307
308
309
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000310//===----------------------------------------------------------------------===//
311// Visitor Methods
312//===----------------------------------------------------------------------===//
313
Chris Lattner776c6492007-08-21 18:51:13 +0000314ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000315 CGF.ErrorUnsupported(E, "complex expression");
Chris Lattner776c6492007-08-21 18:51:13 +0000316 const llvm::Type *EltTy =
317 CGF.ConvertType(E->getType()->getAsComplexType()->getElementType());
318 llvm::Value *U = llvm::UndefValue::get(EltTy);
319 return ComplexPairTy(U, U);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000320}
321
Chris Lattnerd272ff02007-08-26 05:57:57 +0000322ComplexPairTy ComplexExprEmitter::
323VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000324 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
Owen Anderson69243822009-07-13 04:10:07 +0000325 return
Owen Andersonc9c88b42009-07-31 20:28:54 +0000326 ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000327}
328
329
Chris Lattner419d25e2007-08-23 21:38:16 +0000330ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone9f2f452009-05-27 03:37:57 +0000331 if (E->getCallReturnType()->isReferenceType())
332 return EmitLoadOfLValue(E);
333
Chris Lattner9b655512007-08-31 22:49:20 +0000334 return CGF.EmitCallExpr(E).getComplexVal();
Chris Lattner419d25e2007-08-23 21:38:16 +0000335}
336
Chris Lattner7e20d092007-08-31 22:51:38 +0000337ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
338 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getComplexVal();
339}
340
Chris Lattnerab340c22007-08-26 22:09:01 +0000341/// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
342ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
343 QualType SrcType,
344 QualType DestType) {
345 // Get the src/dest element type.
Chris Lattner96196622008-07-26 22:37:01 +0000346 SrcType = SrcType->getAsComplexType()->getElementType();
347 DestType = DestType->getAsComplexType()->getElementType();
Chris Lattnerd272ff02007-08-26 05:57:57 +0000348
Daniel Dunbar56f0d162009-01-29 06:44:03 +0000349 // C99 6.3.1.6: When a value of complex type is converted to another
Daniel Dunbare12d8e32009-01-26 18:02:34 +0000350 // complex type, both the real and imaginary parts follow the conversion
Chris Lattnerab340c22007-08-26 22:09:01 +0000351 // rules for the corresponding real types.
352 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType);
353 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType);
354 return Val;
355}
356
357ComplexPairTy ComplexExprEmitter::EmitCast(Expr *Op, QualType DestTy) {
Chris Lattnerd272ff02007-08-26 05:57:57 +0000358 // Two cases here: cast from (complex to complex) and (scalar to complex).
Chris Lattner9b2dc282008-04-04 16:54:41 +0000359 if (Op->getType()->isAnyComplexType())
Chris Lattnerab340c22007-08-26 22:09:01 +0000360 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
361
Chris Lattnerd272ff02007-08-26 05:57:57 +0000362 // C99 6.3.1.7: When a value of real type is converted to a complex type, the
Eli Friedmanc110c542009-03-23 04:08:46 +0000363 // real part of the complex result value is determined by the rules of
Chris Lattnerd272ff02007-08-26 05:57:57 +0000364 // conversion to the corresponding real type and the imaginary part of the
365 // complex result value is a positive zero or an unsigned zero.
366 llvm::Value *Elt = CGF.EmitScalarExpr(Op);
367
368 // Convert the input element to the element type of the complex.
Chris Lattner96196622008-07-26 22:37:01 +0000369 DestTy = DestTy->getAsComplexType()->getElementType();
Chris Lattner3707b252007-08-26 06:48:56 +0000370 Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy);
Chris Lattnerd272ff02007-08-26 05:57:57 +0000371
372 // Return (realval, 0).
Owen Andersonc9c88b42009-07-31 20:28:54 +0000373 return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType()));
Chris Lattnerd272ff02007-08-26 05:57:57 +0000374}
375
Chris Lattner3070f982007-08-21 22:25:29 +0000376ComplexPairTy ComplexExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
377 bool isInc, bool isPre) {
378 LValue LV = CGF.EmitLValue(E->getSubExpr());
Chris Lattner87415d22009-06-17 06:36:24 +0000379 ComplexPairTy InVal = EmitLoadOfComplex(LV.getAddress(),
380 LV.isVolatileQualified());
Chris Lattner3070f982007-08-21 22:25:29 +0000381
382 llvm::Value *NextVal;
Eli Friedmanc110c542009-03-23 04:08:46 +0000383 if (isa<llvm::IntegerType>(InVal.first->getType())) {
384 uint64_t AmountVal = isInc ? 1 : -1;
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000385 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
Chris Lattner87415d22009-06-17 06:36:24 +0000386
387 // Add the inc/dec to the real part.
388 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
Eli Friedmanc110c542009-03-23 04:08:46 +0000389 } else {
390 QualType ElemTy = E->getType()->getAsComplexType()->getElementType();
391 llvm::APFloat FVal(CGF.getContext().getFloatTypeSemantics(ElemTy), 1);
392 if (!isInc)
393 FVal.changeSign();
Owen Andersonbc0a2222009-07-27 21:00:51 +0000394 NextVal = llvm::ConstantFP::get(CGF.getLLVMContext(), FVal);
Chris Lattner87415d22009-06-17 06:36:24 +0000395
396 // Add the inc/dec to the real part.
397 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
Chris Lattnerca2617c2007-09-13 06:19:18 +0000398 }
Chris Lattner3070f982007-08-21 22:25:29 +0000399
Chris Lattner3070f982007-08-21 22:25:29 +0000400 ComplexPairTy IncVal(NextVal, InVal.second);
401
402 // Store the updated result through the lvalue.
Eli Friedmanc110c542009-03-23 04:08:46 +0000403 EmitStoreOfComplex(IncVal, LV.getAddress(), LV.isVolatileQualified());
Chris Lattner3070f982007-08-21 22:25:29 +0000404
405 // If this is a postinc, return the value read from memory, otherwise use the
406 // updated value.
407 return isPre ? IncVal : InVal;
408}
409
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000410ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000411 TestAndClearIgnoreReal();
412 TestAndClearIgnoreImag();
413 TestAndClearIgnoreRealAssign();
414 TestAndClearIgnoreImagAssign();
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000415 ComplexPairTy Op = Visit(E->getSubExpr());
Chris Lattner87415d22009-06-17 06:36:24 +0000416
417 llvm::Value *ResR, *ResI;
418 if (Op.first->getType()->isFloatingPoint()) {
419 ResR = Builder.CreateFNeg(Op.first, "neg.r");
420 ResI = Builder.CreateFNeg(Op.second, "neg.i");
421 } else {
422 ResR = Builder.CreateNeg(Op.first, "neg.r");
423 ResI = Builder.CreateNeg(Op.second, "neg.i");
424 }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000425 return ComplexPairTy(ResR, ResI);
426}
427
Chris Lattnere98a11c2007-08-21 20:41:44 +0000428ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000429 TestAndClearIgnoreReal();
430 TestAndClearIgnoreImag();
431 TestAndClearIgnoreRealAssign();
432 TestAndClearIgnoreImagAssign();
Chris Lattnere98a11c2007-08-21 20:41:44 +0000433 // ~(a+ib) = a + i*-b
434 ComplexPairTy Op = Visit(E->getSubExpr());
Chris Lattner87415d22009-06-17 06:36:24 +0000435 llvm::Value *ResI;
436 if (Op.second->getType()->isFloatingPoint())
437 ResI = Builder.CreateFNeg(Op.second, "conj.i");
438 else
439 ResI = Builder.CreateNeg(Op.second, "conj.i");
440
Chris Lattnere98a11c2007-08-21 20:41:44 +0000441 return ComplexPairTy(Op.first, ResI);
442}
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000443
Chris Lattnerab340c22007-08-26 22:09:01 +0000444ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000445 llvm::Value *ResR, *ResI;
446
447 if (Op.LHS.first->getType()->isFloatingPoint()) {
448 ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
449 ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
450 } else {
451 ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
452 ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
453 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000454 return ComplexPairTy(ResR, ResI);
455}
456
Chris Lattnerab340c22007-08-26 22:09:01 +0000457ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000458 llvm::Value *ResR, *ResI;
459 if (Op.LHS.first->getType()->isFloatingPoint()) {
460 ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
461 ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
462 } else {
463 ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
464 ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
465 }
Chris Lattner6bc1ade2007-08-23 23:46:33 +0000466 return ComplexPairTy(ResR, ResI);
467}
468
469
Chris Lattnerab340c22007-08-26 22:09:01 +0000470ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000471 using llvm::Value;
472 Value *ResR, *ResI;
Chris Lattner2823c192007-08-21 16:34:16 +0000473
Chris Lattner87415d22009-06-17 06:36:24 +0000474 if (Op.LHS.first->getType()->isFloatingPoint()) {
475 Value *ResRl = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
476 Value *ResRr = Builder.CreateFMul(Op.LHS.second, Op.RHS.second,"mul.rr");
477 ResR = Builder.CreateFSub(ResRl, ResRr, "mul.r");
478
479 Value *ResIl = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il");
480 Value *ResIr = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
481 ResI = Builder.CreateFAdd(ResIl, ResIr, "mul.i");
482 } else {
483 Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
484 Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second,"mul.rr");
485 ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
486
487 Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
488 Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
489 ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
490 }
Chris Lattner2823c192007-08-21 16:34:16 +0000491 return ComplexPairTy(ResR, ResI);
492}
493
Chris Lattnerab340c22007-08-26 22:09:01 +0000494ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
495 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
496 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
Chris Lattner3219c5d2007-08-26 21:24:19 +0000497
Chris Lattner3219c5d2007-08-26 21:24:19 +0000498
499 llvm::Value *DSTr, *DSTi;
Chris Lattner87415d22009-06-17 06:36:24 +0000500 if (Op.LHS.first->getType()->isFloatingPoint()) {
501 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
502 llvm::Value *Tmp1 = Builder.CreateFMul(LHSr, RHSr, "tmp"); // a*c
503 llvm::Value *Tmp2 = Builder.CreateFMul(LHSi, RHSi, "tmp"); // b*d
504 llvm::Value *Tmp3 = Builder.CreateFAdd(Tmp1, Tmp2, "tmp"); // ac+bd
505
506 llvm::Value *Tmp4 = Builder.CreateFMul(RHSr, RHSr, "tmp"); // c*c
507 llvm::Value *Tmp5 = Builder.CreateFMul(RHSi, RHSi, "tmp"); // d*d
508 llvm::Value *Tmp6 = Builder.CreateFAdd(Tmp4, Tmp5, "tmp"); // cc+dd
509
510 llvm::Value *Tmp7 = Builder.CreateFMul(LHSi, RHSr, "tmp"); // b*c
511 llvm::Value *Tmp8 = Builder.CreateFMul(LHSr, RHSi, "tmp"); // a*d
512 llvm::Value *Tmp9 = Builder.CreateFSub(Tmp7, Tmp8, "tmp"); // bc-ad
513
Chris Lattner3219c5d2007-08-26 21:24:19 +0000514 DSTr = Builder.CreateFDiv(Tmp3, Tmp6, "tmp");
515 DSTi = Builder.CreateFDiv(Tmp9, Tmp6, "tmp");
516 } else {
Chris Lattner87415d22009-06-17 06:36:24 +0000517 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
518 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr, "tmp"); // a*c
519 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi, "tmp"); // b*d
520 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2, "tmp"); // ac+bd
521
522 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr, "tmp"); // c*c
523 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi, "tmp"); // d*d
524 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5, "tmp"); // cc+dd
525
526 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr, "tmp"); // b*c
527 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi, "tmp"); // a*d
528 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8, "tmp"); // bc-ad
529
Chris Lattnerab340c22007-08-26 22:09:01 +0000530 if (Op.Ty->getAsComplexType()->getElementType()->isUnsignedIntegerType()) {
Chris Lattner3219c5d2007-08-26 21:24:19 +0000531 DSTr = Builder.CreateUDiv(Tmp3, Tmp6, "tmp");
532 DSTi = Builder.CreateUDiv(Tmp9, Tmp6, "tmp");
533 } else {
534 DSTr = Builder.CreateSDiv(Tmp3, Tmp6, "tmp");
535 DSTi = Builder.CreateSDiv(Tmp9, Tmp6, "tmp");
536 }
537 }
538
539 return ComplexPairTy(DSTr, DSTi);
540}
541
Chris Lattnerab340c22007-08-26 22:09:01 +0000542ComplexExprEmitter::BinOpInfo
543ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000544 TestAndClearIgnoreReal();
545 TestAndClearIgnoreImag();
546 TestAndClearIgnoreRealAssign();
547 TestAndClearIgnoreImagAssign();
Chris Lattnerab340c22007-08-26 22:09:01 +0000548 BinOpInfo Ops;
549 Ops.LHS = Visit(E->getLHS());
550 Ops.RHS = Visit(E->getRHS());
551 Ops.Ty = E->getType();
552 return Ops;
553}
554
555
556// Compound assignments.
557ComplexPairTy ComplexExprEmitter::
558EmitCompoundAssign(const CompoundAssignOperator *E,
559 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
Mike Stump7f79f9b2009-05-29 15:46:01 +0000560 TestAndClearIgnoreReal();
561 TestAndClearIgnoreImag();
562 bool ignreal = TestAndClearIgnoreRealAssign();
563 bool ignimag = TestAndClearIgnoreImagAssign();
Chris Lattnerab340c22007-08-26 22:09:01 +0000564 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
Chris Lattnerab340c22007-08-26 22:09:01 +0000565
566 BinOpInfo OpInfo;
Mike Stump7f79f9b2009-05-29 15:46:01 +0000567
568 // Load the RHS and LHS operands.
569 // __block variables need to have the rhs evaluated first, plus this should
570 // improve codegen a little. It is possible for the RHS to be complex or
571 // scalar.
Eli Friedmanab3a8522009-03-28 01:22:36 +0000572 OpInfo.Ty = E->getComputationResultType();
Chris Lattnerab340c22007-08-26 22:09:01 +0000573 OpInfo.RHS = EmitCast(E->getRHS(), OpInfo.Ty);
574
Mike Stump7f79f9b2009-05-29 15:46:01 +0000575 LValue LHSLV = CGF.EmitLValue(E->getLHS());
576
577
578 // We know the LHS is a complex lvalue.
579 OpInfo.LHS=EmitLoadOfComplex(LHSLV.getAddress(),LHSLV.isVolatileQualified());
580 OpInfo.LHS=EmitComplexToComplexCast(OpInfo.LHS, LHSTy, OpInfo.Ty);
581
Chris Lattnerab340c22007-08-26 22:09:01 +0000582 // Expand the binary operator.
583 ComplexPairTy Result = (this->*Func)(OpInfo);
584
585 // Truncate the result back to the LHS type.
586 Result = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy);
587
588 // Store the result value into the LHS lvalue.
Eli Friedmanc110c542009-03-23 04:08:46 +0000589 EmitStoreOfComplex(Result, LHSLV.getAddress(), LHSLV.isVolatileQualified());
Mike Stump7f79f9b2009-05-29 15:46:01 +0000590 // And now return the LHS
591 IgnoreReal = ignreal;
592 IgnoreImag = ignimag;
593 IgnoreRealAssign = ignreal;
594 IgnoreImagAssign = ignimag;
595 return EmitLoadOfComplex(LHSLV.getAddress(), LHSLV.isVolatileQualified());
Chris Lattnerab340c22007-08-26 22:09:01 +0000596}
Chris Lattner3219c5d2007-08-26 21:24:19 +0000597
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000598ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000599 TestAndClearIgnoreReal();
600 TestAndClearIgnoreImag();
601 bool ignreal = TestAndClearIgnoreRealAssign();
602 bool ignimag = TestAndClearIgnoreImagAssign();
Chris Lattner96196622008-07-26 22:37:01 +0000603 assert(CGF.getContext().getCanonicalType(E->getLHS()->getType()) ==
604 CGF.getContext().getCanonicalType(E->getRHS()->getType()) &&
605 "Invalid assignment");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000606 // Emit the RHS.
607 ComplexPairTy Val = Visit(E->getRHS());
608
609 // Compute the address to store into.
610 LValue LHS = CGF.EmitLValue(E->getLHS());
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000611
612 // Store into it, if simple.
613 if (LHS.isSimple()) {
614 EmitStoreOfComplex(Val, LHS.getAddress(), LHS.isVolatileQualified());
615
616 // And now return the LHS
617 IgnoreReal = ignreal;
618 IgnoreImag = ignimag;
619 IgnoreRealAssign = ignreal;
620 IgnoreImagAssign = ignimag;
621 return EmitLoadOfComplex(LHS.getAddress(), LHS.isVolatileQualified());
622 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000623
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000624 // Otherwise we must have a property setter (no complex vector/bitfields).
625 if (LHS.isPropertyRef())
626 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), RValue::getComplex(Val));
627 else
628 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), RValue::getComplex(Val));
629
630 // There is no reload after a store through a method, but we need to restore
631 // the Ignore* flags.
Mike Stump7f79f9b2009-05-29 15:46:01 +0000632 IgnoreReal = ignreal;
633 IgnoreImag = ignimag;
634 IgnoreRealAssign = ignreal;
635 IgnoreImagAssign = ignimag;
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000636 return Val;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000637}
638
Chris Lattner756a4d82007-08-21 17:15:50 +0000639ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
640 CGF.EmitStmt(E->getLHS());
Daniel Dunbara448fb22008-11-11 23:11:34 +0000641 CGF.EnsureInsertPoint();
Chris Lattner756a4d82007-08-21 17:15:50 +0000642 return Visit(E->getRHS());
643}
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000644
645ComplexPairTy ComplexExprEmitter::
646VisitConditionalOperator(const ConditionalOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000647 TestAndClearIgnoreReal();
648 TestAndClearIgnoreImag();
649 TestAndClearIgnoreRealAssign();
650 TestAndClearIgnoreImagAssign();
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000651 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
652 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
653 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000654
655 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner5083a532007-08-21 17:39:38 +0000656 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000657
658 CGF.EmitBlock(LHSBlock);
659
660 // Handle the GNU extension for missing LHS.
Chris Lattner05ba49c2007-08-21 05:54:53 +0000661 assert(E->getLHS() && "Must have LHS for complex value");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000662
663 ComplexPairTy LHS = Visit(E->getLHS());
Chris Lattner5083a532007-08-21 17:39:38 +0000664 LHSBlock = Builder.GetInsertBlock();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000665 CGF.EmitBranch(ContBlock);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000666
667 CGF.EmitBlock(RHSBlock);
668
669 ComplexPairTy RHS = Visit(E->getRHS());
Chris Lattner5083a532007-08-21 17:39:38 +0000670 RHSBlock = Builder.GetInsertBlock();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000671 CGF.EmitBranch(ContBlock);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000672
673 CGF.EmitBlock(ContBlock);
674
675 // Create a PHI node for the real part.
Chris Lattner5083a532007-08-21 17:39:38 +0000676 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000677 RealPN->reserveOperandSpace(2);
678 RealPN->addIncoming(LHS.first, LHSBlock);
679 RealPN->addIncoming(RHS.first, RHSBlock);
680
681 // Create a PHI node for the imaginary part.
Chris Lattner5083a532007-08-21 17:39:38 +0000682 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000683 ImagPN->reserveOperandSpace(2);
684 ImagPN->addIncoming(LHS.second, LHSBlock);
685 ImagPN->addIncoming(RHS.second, RHSBlock);
686
687 return ComplexPairTy(RealPN, ImagPN);
688}
689
Chris Lattnerd0b12032007-08-24 02:18:47 +0000690ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Eli Friedman79769322009-03-04 05:52:32 +0000691 return Visit(E->getChosenSubExpr(CGF.getContext()));
Chris Lattnerd0b12032007-08-24 02:18:47 +0000692}
693
Eli Friedmanc4777f12008-05-13 23:11:35 +0000694ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000695 bool Ignore = TestAndClearIgnoreReal();
696 (void)Ignore;
697 assert (Ignore == false && "init list ignored");
698 Ignore = TestAndClearIgnoreImag();
699 (void)Ignore;
700 assert (Ignore == false && "init list ignored");
Eli Friedmanc4777f12008-05-13 23:11:35 +0000701 if (E->getNumInits())
702 return Visit(E->getInit(0));
703
704 // Empty init list intializes to null
705 QualType Ty = E->getType()->getAsComplexType()->getElementType();
706 const llvm::Type* LTy = CGF.ConvertType(Ty);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000707 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
Eli Friedmanc4777f12008-05-13 23:11:35 +0000708 return ComplexPairTy(zeroConstant, zeroConstant);
709}
710
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000711ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000712 llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr());
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000713 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType());
714
715 if (!ArgPtr) {
716 CGF.ErrorUnsupported(E, "complex va_arg expression");
717 const llvm::Type *EltTy =
718 CGF.ConvertType(E->getType()->getAsComplexType()->getElementType());
719 llvm::Value *U = llvm::UndefValue::get(EltTy);
720 return ComplexPairTy(U, U);
721 }
722
723 // FIXME Volatility.
724 return EmitLoadOfComplex(ArgPtr, false);
725}
726
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000727//===----------------------------------------------------------------------===//
728// Entry Point into this File
729//===----------------------------------------------------------------------===//
730
731/// EmitComplexExpr - Emit the computation of the specified expression of
732/// complex type, ignoring the result.
Mike Stump7f79f9b2009-05-29 15:46:01 +0000733ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
734 bool IgnoreImag, bool IgnoreRealAssign, bool IgnoreImagAssign) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000735 assert(E && E->getType()->isAnyComplexType() &&
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000736 "Invalid complex expression to emit");
737
Mike Stump7f79f9b2009-05-29 15:46:01 +0000738 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag, IgnoreRealAssign,
739 IgnoreImagAssign)
740 .Visit(const_cast<Expr*>(E));
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000741}
742
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000743/// EmitComplexExprIntoAddr - Emit the computation of the specified expression
744/// of complex type, storing into the specified Value*.
745void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E,
Chris Lattner190dbe22007-08-26 16:22:13 +0000746 llvm::Value *DestAddr,
747 bool DestIsVolatile) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000748 assert(E && E->getType()->isAnyComplexType() &&
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000749 "Invalid complex expression to emit");
750 ComplexExprEmitter Emitter(*this);
751 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
Chris Lattner190dbe22007-08-26 16:22:13 +0000752 Emitter.EmitStoreOfComplex(Val, DestAddr, DestIsVolatile);
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000753}
Chris Lattner9b655512007-08-31 22:49:20 +0000754
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000755/// StoreComplexToAddr - Store a complex number into the specified address.
756void CodeGenFunction::StoreComplexToAddr(ComplexPairTy V,
757 llvm::Value *DestAddr,
758 bool DestIsVolatile) {
759 ComplexExprEmitter(*this).EmitStoreOfComplex(V, DestAddr, DestIsVolatile);
760}
761
Chris Lattner9b655512007-08-31 22:49:20 +0000762/// LoadComplexFromAddr - Load a complex number from the specified address.
763ComplexPairTy CodeGenFunction::LoadComplexFromAddr(llvm::Value *SrcAddr,
764 bool SrcIsVolatile) {
765 return ComplexExprEmitter(*this).EmitLoadOfComplex(SrcAddr, SrcIsVolatile);
766}