blob: 21c298a96aea3854afc98fde07449b49bb379062 [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 +000021using namespace clang;
22using namespace CodeGen;
23
24//===----------------------------------------------------------------------===//
Chris Lattner05ba49c2007-08-21 05:54:53 +000025// Complex Expression Emitter
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000026//===----------------------------------------------------------------------===//
27
Chris Lattner58dee102007-08-21 16:57:55 +000028typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000029
30namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000031class ComplexExprEmitter
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000032 : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
33 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000034 CGBuilderTy &Builder;
Mike Stump7f79f9b2009-05-29 15:46:01 +000035 // True is we should ignore the value of a
36 bool IgnoreReal;
37 bool IgnoreImag;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000038public:
John McCallb418d742010-11-16 10:08:07 +000039 ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
40 : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000041 }
42
Mike Stumpdb52dcd2009-09-09 13:00:44 +000043
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000044 //===--------------------------------------------------------------------===//
45 // Utilities
46 //===--------------------------------------------------------------------===//
47
Mike Stump7f79f9b2009-05-29 15:46:01 +000048 bool TestAndClearIgnoreReal() {
49 bool I = IgnoreReal;
50 IgnoreReal = false;
51 return I;
52 }
53 bool TestAndClearIgnoreImag() {
54 bool I = IgnoreImag;
55 IgnoreImag = false;
56 return I;
57 }
Mike Stump7f79f9b2009-05-29 15:46:01 +000058
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000059 /// EmitLoadOfLValue - Given an expression with complex type that represents a
60 /// value l-value, this method emits the address of the l-value, then loads
61 /// and returns the result.
Chris Lattner46d7d9f2007-08-21 17:28:34 +000062 ComplexPairTy EmitLoadOfLValue(const Expr *E) {
John McCall0e800c92010-12-04 08:14:53 +000063 return EmitLoadOfLValue(CGF.EmitLValue(E));
64 }
65
66 ComplexPairTy EmitLoadOfLValue(LValue LV) {
Daniel Dunbar42f963d2009-06-10 04:38:50 +000067 if (LV.isSimple())
68 return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified());
69
John McCall119a1c62010-12-04 02:32:38 +000070 assert(LV.isPropertyRef() && "Unknown LValue type!");
71 return CGF.EmitLoadOfPropertyRefLValue(LV).getComplexVal();
Chris Lattner46d7d9f2007-08-21 17:28:34 +000072 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +000073
Chris Lattner46d7d9f2007-08-21 17:28:34 +000074 /// EmitLoadOfComplex - Given a pointer to a complex value, emit code to load
75 /// the real and imaginary pieces.
Chris Lattner76e80332007-08-21 18:02:02 +000076 ComplexPairTy EmitLoadOfComplex(llvm::Value *SrcPtr, bool isVolatile);
Mike Stumpdb52dcd2009-09-09 13:00:44 +000077
John McCall0e800c92010-12-04 08:14:53 +000078 /// EmitStoreThroughLValue - Given an l-value of complex type, store
79 /// a complex number into it.
80 void EmitStoreThroughLValue(ComplexPairTy Val, LValue LV) {
81 if (LV.isSimple())
82 return EmitStoreOfComplex(Val, LV.getAddress(), LV.isVolatileQualified());
83
84 assert(LV.isPropertyRef() && "Unknown LValue type!");
85 CGF.EmitStoreThroughPropertyRefLValue(RValue::getComplex(Val), LV);
86 }
87
Chris Lattner46d7d9f2007-08-21 17:28:34 +000088 /// EmitStoreOfComplex - Store the specified real/imag parts into the
89 /// specified value pointer.
Chris Lattner76e80332007-08-21 18:02:02 +000090 void EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *ResPtr, bool isVol);
Mike Stumpdb52dcd2009-09-09 13:00:44 +000091
Chris Lattnerab340c22007-08-26 22:09:01 +000092 /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
93 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
94 QualType DestType);
Mike Stumpdb52dcd2009-09-09 13:00:44 +000095
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000096 //===--------------------------------------------------------------------===//
97 // Visitor Methods
98 //===--------------------------------------------------------------------===//
Chris Lattner776c6492007-08-21 18:51:13 +000099
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +0000100 ComplexPairTy Visit(Expr *E) {
101 llvm::DenseMap<const Expr *, ComplexPairTy>::iterator I =
102 CGF.ConditionalSaveComplexExprs.find(E);
103 if (I != CGF.ConditionalSaveComplexExprs.end())
104 return I->second;
105
106 return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
107 }
108
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000109 ComplexPairTy VisitStmt(Stmt *S) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000110 S->dump(CGF.getContext().getSourceManager());
Chris Lattner776c6492007-08-21 18:51:13 +0000111 assert(0 && "Stmt can't have complex result type!");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000112 return ComplexPairTy();
113 }
Chris Lattner776c6492007-08-21 18:51:13 +0000114 ComplexPairTy VisitExpr(Expr *S);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000115 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
Chris Lattnerd272ff02007-08-26 05:57:57 +0000116 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000117
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000118 // l-values.
Chris Lattnerd272ff02007-08-26 05:57:57 +0000119 ComplexPairTy VisitDeclRefExpr(const Expr *E) { return EmitLoadOfLValue(E); }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000120 ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000121 return EmitLoadOfLValue(E);
122 }
123 ComplexPairTy VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
John McCallf6a16482010-12-04 03:47:34 +0000124 assert(E->getObjectKind() == OK_Ordinary);
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000125 return EmitLoadOfLValue(E);
126 }
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000127 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
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000134
Douglas Gregor77d439a2010-07-14 21:35:45 +0000135 ComplexPairTy EmitCast(CastExpr::CastKind CK, Expr *Op, QualType DestTy);
Chris Lattnerd272ff02007-08-26 05:57:57 +0000136 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
137 // Unlike for scalars, we don't have to worry about function->ptr demotion
138 // here.
Douglas Gregor77d439a2010-07-14 21:35:45 +0000139 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
Chris Lattnerd272ff02007-08-26 05:57:57 +0000140 }
141 ComplexPairTy VisitCastExpr(CastExpr *E) {
Douglas Gregor77d439a2010-07-14 21:35:45 +0000142 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
Chris Lattnerd272ff02007-08-26 05:57:57 +0000143 }
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,
Chris Lattnerdd36d322010-01-09 21:40:03 +0000149 bool isInc, bool isPre) {
150 LValue LV = CGF.EmitLValue(E->getSubExpr());
151 return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
152 }
Chris Lattner3070f982007-08-21 22:25:29 +0000153 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
154 return VisitPrePostIncDec(E, false, false);
155 }
156 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
157 return VisitPrePostIncDec(E, true, false);
158 }
159 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
160 return VisitPrePostIncDec(E, false, true);
161 }
162 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
163 return VisitPrePostIncDec(E, true, true);
164 }
165 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000166 ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000167 TestAndClearIgnoreReal();
168 TestAndClearIgnoreImag();
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000169 return Visit(E->getSubExpr());
170 }
171 ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
Chris Lattnere98a11c2007-08-21 20:41:44 +0000172 ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
Sebastian Redl05189992008-11-11 17:56:53 +0000173 // LNot,Real,Imag never return complex.
Chris Lattnere98a11c2007-08-21 20:41:44 +0000174 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
175 return Visit(E->getSubExpr());
176 }
Chris Lattner04421082008-04-08 04:40:51 +0000177 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
178 return Visit(DAE->getExpr());
179 }
John McCall4765fa02010-12-06 08:20:24 +0000180 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
181 return CGF.EmitExprWithCleanups(E).getComplexVal();
Anders Carlsson4b76b4f2009-05-31 00:12:05 +0000182 }
Douglas Gregored8abf12010-07-08 06:14:04 +0000183 ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000184 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
John McCall183700f2009-09-21 23:43:11 +0000185 QualType Elem = E->getType()->getAs<ComplexType>()->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000186 llvm::Constant *Null = 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!");
John McCall183700f2009-09-21 23:43:11 +0000191 QualType Elem = E->getType()->getAs<ComplexType>()->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +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 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000196
Chris Lattnerab340c22007-08-26 22:09:01 +0000197 struct BinOpInfo {
198 ComplexPairTy LHS;
199 ComplexPairTy RHS;
200 QualType Ty; // Computation Type.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000201 };
202
Chris Lattnerab340c22007-08-26 22:09:01 +0000203 BinOpInfo EmitBinOps(const BinaryOperator *E);
John McCallb418d742010-11-16 10:08:07 +0000204 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
205 ComplexPairTy (ComplexExprEmitter::*Func)
206 (const BinOpInfo &),
207 ComplexPairTy &Val);
Chris Lattnerab340c22007-08-26 22:09:01 +0000208 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
209 ComplexPairTy (ComplexExprEmitter::*Func)
210 (const BinOpInfo &));
211
212 ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
213 ComplexPairTy EmitBinSub(const BinOpInfo &Op);
214 ComplexPairTy EmitBinMul(const BinOpInfo &Op);
215 ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000216
Chris Lattnerab340c22007-08-26 22:09:01 +0000217 ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
218 return EmitBinAdd(EmitBinOps(E));
219 }
220 ComplexPairTy VisitBinSub(const BinaryOperator *E) {
221 return EmitBinSub(EmitBinOps(E));
222 }
John McCallb418d742010-11-16 10:08:07 +0000223 ComplexPairTy VisitBinMul(const BinaryOperator *E) {
224 return EmitBinMul(EmitBinOps(E));
225 }
Chris Lattnerab340c22007-08-26 22:09:01 +0000226 ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
227 return EmitBinDiv(EmitBinOps(E));
228 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000229
Chris Lattnerab340c22007-08-26 22:09:01 +0000230 // Compound assignments.
231 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
232 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
233 }
234 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
235 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
236 }
237 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
238 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
239 }
240 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
241 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
242 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000243
Chris Lattner612c40c2007-08-26 21:27:07 +0000244 // GCC rejects rem/and/or/xor for integer complex.
Chris Lattner4034edb2007-08-21 17:12:50 +0000245 // Logical and/or always return int, never complex.
Chris Lattner58dee102007-08-21 16:57:55 +0000246
247 // No comparisons produce a complex result.
John McCallb418d742010-11-16 10:08:07 +0000248
249 LValue EmitBinAssignLValue(const BinaryOperator *E,
250 ComplexPairTy &Val);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000251 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
Chris Lattner756a4d82007-08-21 17:15:50 +0000252 ComplexPairTy VisitBinComma (const BinaryOperator *E);
253
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000254
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000255 ComplexPairTy VisitConditionalOperator(const ConditionalOperator *CO);
Chris Lattnerd0b12032007-08-24 02:18:47 +0000256 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
Eli Friedmanc4777f12008-05-13 23:11:35 +0000257
258 ComplexPairTy VisitInitListExpr(InitListExpr *E);
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000259
260 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000261};
262} // end anonymous namespace.
263
264//===----------------------------------------------------------------------===//
265// Utilities
266//===----------------------------------------------------------------------===//
267
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000268/// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
269/// load the real and imaginary pieces, returning them as Real/Imag.
Chris Lattner76e80332007-08-21 18:02:02 +0000270ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr,
271 bool isVolatile) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000272 llvm::Value *Real=0, *Imag=0;
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000273
John McCallff624c12010-11-14 09:40:28 +0000274 if (!IgnoreReal || isVolatile) {
Benjamin Kramer56a44f72009-11-29 19:51:45 +0000275 llvm::Value *RealP = Builder.CreateStructGEP(SrcPtr, 0,
276 SrcPtr->getName() + ".realp");
277 Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr->getName() + ".real");
Mike Stump7f79f9b2009-05-29 15:46:01 +0000278 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000279
John McCallff624c12010-11-14 09:40:28 +0000280 if (!IgnoreImag || isVolatile) {
Benjamin Kramer56a44f72009-11-29 19:51:45 +0000281 llvm::Value *ImagP = Builder.CreateStructGEP(SrcPtr, 1,
282 SrcPtr->getName() + ".imagp");
283 Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr->getName() + ".imag");
Mike Stump7f79f9b2009-05-29 15:46:01 +0000284 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000285 return ComplexPairTy(Real, Imag);
286}
287
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000288/// EmitStoreOfComplex - Store the specified real/imag parts into the
289/// specified value pointer.
Chris Lattner76e80332007-08-21 18:02:02 +0000290void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr,
291 bool isVolatile) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000292 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real");
293 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000294
Chris Lattner76e80332007-08-21 18:02:02 +0000295 Builder.CreateStore(Val.first, RealPtr, isVolatile);
296 Builder.CreateStore(Val.second, ImagPtr, isVolatile);
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000297}
298
299
300
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000301//===----------------------------------------------------------------------===//
302// Visitor Methods
303//===----------------------------------------------------------------------===//
304
Chris Lattner776c6492007-08-21 18:51:13 +0000305ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000306 CGF.ErrorUnsupported(E, "complex expression");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000307 const llvm::Type *EltTy =
John McCall183700f2009-09-21 23:43:11 +0000308 CGF.ConvertType(E->getType()->getAs<ComplexType>()->getElementType());
Chris Lattner776c6492007-08-21 18:51:13 +0000309 llvm::Value *U = llvm::UndefValue::get(EltTy);
310 return ComplexPairTy(U, U);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000311}
312
Chris Lattnerd272ff02007-08-26 05:57:57 +0000313ComplexPairTy ComplexExprEmitter::
314VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000315 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
John McCall8d3d6c92011-01-13 02:03:06 +0000316 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000317}
318
319
Chris Lattner419d25e2007-08-23 21:38:16 +0000320ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone9f2f452009-05-27 03:37:57 +0000321 if (E->getCallReturnType()->isReferenceType())
322 return EmitLoadOfLValue(E);
323
Chris Lattner9b655512007-08-31 22:49:20 +0000324 return CGF.EmitCallExpr(E).getComplexVal();
Chris Lattner419d25e2007-08-23 21:38:16 +0000325}
326
Chris Lattner7e20d092007-08-31 22:51:38 +0000327ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
John McCall150b4622011-01-26 04:00:11 +0000328 CodeGenFunction::StmtExprEvaluation eval(CGF);
329 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getComplexVal();
Chris Lattner7e20d092007-08-31 22:51:38 +0000330}
331
Chris Lattnerab340c22007-08-26 22:09:01 +0000332/// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
333ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
334 QualType SrcType,
335 QualType DestType) {
336 // Get the src/dest element type.
John McCall183700f2009-09-21 23:43:11 +0000337 SrcType = SrcType->getAs<ComplexType>()->getElementType();
338 DestType = DestType->getAs<ComplexType>()->getElementType();
Chris Lattnerd272ff02007-08-26 05:57:57 +0000339
Daniel Dunbar56f0d162009-01-29 06:44:03 +0000340 // C99 6.3.1.6: When a value of complex type is converted to another
Daniel Dunbare12d8e32009-01-26 18:02:34 +0000341 // complex type, both the real and imaginary parts follow the conversion
Chris Lattnerab340c22007-08-26 22:09:01 +0000342 // rules for the corresponding real types.
343 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType);
344 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType);
345 return Val;
346}
347
Douglas Gregor77d439a2010-07-14 21:35:45 +0000348ComplexPairTy ComplexExprEmitter::EmitCast(CastExpr::CastKind CK, Expr *Op,
349 QualType DestTy) {
John McCallf6a16482010-12-04 03:47:34 +0000350 switch (CK) {
351 case CK_GetObjCProperty: {
352 LValue LV = CGF.EmitLValue(Op);
353 assert(LV.isPropertyRef() && "Unknown LValue type!");
354 return CGF.EmitLoadOfPropertyRefLValue(LV).getComplexVal();
355 }
356
357 case CK_NoOp:
358 case CK_LValueToRValue:
359 return Visit(Op);
360
361 // TODO: do all of these
362 default:
363 break;
364 }
John McCall2bb5d002010-11-13 09:02:35 +0000365
Chris Lattnerd272ff02007-08-26 05:57:57 +0000366 // Two cases here: cast from (complex to complex) and (scalar to complex).
Chris Lattner9b2dc282008-04-04 16:54:41 +0000367 if (Op->getType()->isAnyComplexType())
Chris Lattnerab340c22007-08-26 22:09:01 +0000368 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000369
Douglas Gregor77d439a2010-07-14 21:35:45 +0000370 // FIXME: We should be looking at all of the cast kinds here, not
371 // cherry-picking the ones we have test cases for.
John McCall2de56d12010-08-25 11:45:40 +0000372 if (CK == CK_LValueBitCast) {
Douglas Gregor77d439a2010-07-14 21:35:45 +0000373 llvm::Value *V = CGF.EmitLValue(Op).getAddress();
374 V = Builder.CreateBitCast(V,
375 CGF.ConvertType(CGF.getContext().getPointerType(DestTy)));
376 // FIXME: Are the qualifiers correct here?
377 return EmitLoadOfComplex(V, DestTy.isVolatileQualified());
378 }
379
Chris Lattnerd272ff02007-08-26 05:57:57 +0000380 // 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 +0000381 // real part of the complex result value is determined by the rules of
Chris Lattnerd272ff02007-08-26 05:57:57 +0000382 // conversion to the corresponding real type and the imaginary part of the
383 // complex result value is a positive zero or an unsigned zero.
384 llvm::Value *Elt = CGF.EmitScalarExpr(Op);
385
386 // Convert the input element to the element type of the complex.
John McCall183700f2009-09-21 23:43:11 +0000387 DestTy = DestTy->getAs<ComplexType>()->getElementType();
Chris Lattner3707b252007-08-26 06:48:56 +0000388 Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000389
Chris Lattnerd272ff02007-08-26 05:57:57 +0000390 // Return (realval, 0).
Owen Andersonc9c88b42009-07-31 20:28:54 +0000391 return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType()));
Chris Lattnerd272ff02007-08-26 05:57:57 +0000392}
393
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000394ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000395 TestAndClearIgnoreReal();
396 TestAndClearIgnoreImag();
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000397 ComplexPairTy Op = Visit(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000398
Chris Lattner87415d22009-06-17 06:36:24 +0000399 llvm::Value *ResR, *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000400 if (Op.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000401 ResR = Builder.CreateFNeg(Op.first, "neg.r");
402 ResI = Builder.CreateFNeg(Op.second, "neg.i");
403 } else {
404 ResR = Builder.CreateNeg(Op.first, "neg.r");
405 ResI = Builder.CreateNeg(Op.second, "neg.i");
406 }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000407 return ComplexPairTy(ResR, ResI);
408}
409
Chris Lattnere98a11c2007-08-21 20:41:44 +0000410ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000411 TestAndClearIgnoreReal();
412 TestAndClearIgnoreImag();
Chris Lattnere98a11c2007-08-21 20:41:44 +0000413 // ~(a+ib) = a + i*-b
414 ComplexPairTy Op = Visit(E->getSubExpr());
Chris Lattner87415d22009-06-17 06:36:24 +0000415 llvm::Value *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000416 if (Op.second->getType()->isFloatingPointTy())
Chris Lattner87415d22009-06-17 06:36:24 +0000417 ResI = Builder.CreateFNeg(Op.second, "conj.i");
418 else
419 ResI = Builder.CreateNeg(Op.second, "conj.i");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000420
Chris Lattnere98a11c2007-08-21 20:41:44 +0000421 return ComplexPairTy(Op.first, ResI);
422}
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000423
Chris Lattnerab340c22007-08-26 22:09:01 +0000424ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000425 llvm::Value *ResR, *ResI;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000426
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000427 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000428 ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
429 ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
430 } else {
431 ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
432 ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
433 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000434 return ComplexPairTy(ResR, ResI);
435}
436
Chris Lattnerab340c22007-08-26 22:09:01 +0000437ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000438 llvm::Value *ResR, *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000439 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000440 ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
441 ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
442 } else {
443 ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
444 ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
445 }
Chris Lattner6bc1ade2007-08-23 23:46:33 +0000446 return ComplexPairTy(ResR, ResI);
447}
448
449
Chris Lattnerab340c22007-08-26 22:09:01 +0000450ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000451 using llvm::Value;
452 Value *ResR, *ResI;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000453
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000454 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000455 Value *ResRl = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
456 Value *ResRr = Builder.CreateFMul(Op.LHS.second, Op.RHS.second,"mul.rr");
457 ResR = Builder.CreateFSub(ResRl, ResRr, "mul.r");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000458
Chris Lattner87415d22009-06-17 06:36:24 +0000459 Value *ResIl = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il");
460 Value *ResIr = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
461 ResI = Builder.CreateFAdd(ResIl, ResIr, "mul.i");
462 } else {
463 Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
464 Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second,"mul.rr");
465 ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000466
Chris Lattner87415d22009-06-17 06:36:24 +0000467 Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
468 Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
469 ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
470 }
Chris Lattner2823c192007-08-21 16:34:16 +0000471 return ComplexPairTy(ResR, ResI);
472}
473
Chris Lattnerab340c22007-08-26 22:09:01 +0000474ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
475 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
476 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000477
Chris Lattner3219c5d2007-08-26 21:24:19 +0000478
479 llvm::Value *DSTr, *DSTi;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000480 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000481 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
482 llvm::Value *Tmp1 = Builder.CreateFMul(LHSr, RHSr, "tmp"); // a*c
483 llvm::Value *Tmp2 = Builder.CreateFMul(LHSi, RHSi, "tmp"); // b*d
484 llvm::Value *Tmp3 = Builder.CreateFAdd(Tmp1, Tmp2, "tmp"); // ac+bd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000485
Chris Lattner87415d22009-06-17 06:36:24 +0000486 llvm::Value *Tmp4 = Builder.CreateFMul(RHSr, RHSr, "tmp"); // c*c
487 llvm::Value *Tmp5 = Builder.CreateFMul(RHSi, RHSi, "tmp"); // d*d
488 llvm::Value *Tmp6 = Builder.CreateFAdd(Tmp4, Tmp5, "tmp"); // cc+dd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000489
Chris Lattner87415d22009-06-17 06:36:24 +0000490 llvm::Value *Tmp7 = Builder.CreateFMul(LHSi, RHSr, "tmp"); // b*c
491 llvm::Value *Tmp8 = Builder.CreateFMul(LHSr, RHSi, "tmp"); // a*d
492 llvm::Value *Tmp9 = Builder.CreateFSub(Tmp7, Tmp8, "tmp"); // bc-ad
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000493
Chris Lattner3219c5d2007-08-26 21:24:19 +0000494 DSTr = Builder.CreateFDiv(Tmp3, Tmp6, "tmp");
495 DSTi = Builder.CreateFDiv(Tmp9, Tmp6, "tmp");
496 } else {
Chris Lattner87415d22009-06-17 06:36:24 +0000497 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
498 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr, "tmp"); // a*c
499 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi, "tmp"); // b*d
500 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2, "tmp"); // ac+bd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000501
Chris Lattner87415d22009-06-17 06:36:24 +0000502 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr, "tmp"); // c*c
503 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi, "tmp"); // d*d
504 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5, "tmp"); // cc+dd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000505
Chris Lattner87415d22009-06-17 06:36:24 +0000506 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr, "tmp"); // b*c
507 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi, "tmp"); // a*d
508 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8, "tmp"); // bc-ad
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000509
John McCall183700f2009-09-21 23:43:11 +0000510 if (Op.Ty->getAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
Chris Lattner3219c5d2007-08-26 21:24:19 +0000511 DSTr = Builder.CreateUDiv(Tmp3, Tmp6, "tmp");
512 DSTi = Builder.CreateUDiv(Tmp9, Tmp6, "tmp");
513 } else {
514 DSTr = Builder.CreateSDiv(Tmp3, Tmp6, "tmp");
515 DSTi = Builder.CreateSDiv(Tmp9, Tmp6, "tmp");
516 }
517 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000518
Chris Lattner3219c5d2007-08-26 21:24:19 +0000519 return ComplexPairTy(DSTr, DSTi);
520}
521
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000522ComplexExprEmitter::BinOpInfo
Chris Lattnerab340c22007-08-26 22:09:01 +0000523ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000524 TestAndClearIgnoreReal();
525 TestAndClearIgnoreImag();
Chris Lattnerab340c22007-08-26 22:09:01 +0000526 BinOpInfo Ops;
527 Ops.LHS = Visit(E->getLHS());
528 Ops.RHS = Visit(E->getRHS());
529 Ops.Ty = E->getType();
530 return Ops;
531}
532
533
John McCallb418d742010-11-16 10:08:07 +0000534LValue ComplexExprEmitter::
535EmitCompoundAssignLValue(const CompoundAssignOperator *E,
536 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
537 ComplexPairTy &Val) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000538 TestAndClearIgnoreReal();
539 TestAndClearIgnoreImag();
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000540 QualType LHSTy = E->getLHS()->getType();
Chris Lattnerab340c22007-08-26 22:09:01 +0000541
542 BinOpInfo OpInfo;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000543
Mike Stump7f79f9b2009-05-29 15:46:01 +0000544 // Load the RHS and LHS operands.
545 // __block variables need to have the rhs evaluated first, plus this should
John McCall641ca7d2010-11-16 05:45:35 +0000546 // improve codegen a little.
Eli Friedmanab3a8522009-03-28 01:22:36 +0000547 OpInfo.Ty = E->getComputationResultType();
John McCall641ca7d2010-11-16 05:45:35 +0000548
549 // The RHS should have been converted to the computation type.
550 assert(OpInfo.Ty->isAnyComplexType());
551 assert(CGF.getContext().hasSameUnqualifiedType(OpInfo.Ty,
552 E->getRHS()->getType()));
553 OpInfo.RHS = Visit(E->getRHS());
Fariborz Jahaniandfb1eb22010-03-23 18:08:50 +0000554
Daniel Dunbar120bc772010-06-29 22:44:21 +0000555 LValue LHS = CGF.EmitLValue(E->getLHS());
John McCall0e800c92010-12-04 08:14:53 +0000556
557 // Load from the l-value.
558 ComplexPairTy LHSComplexPair = EmitLoadOfLValue(LHS);
Fariborz Jahaniandfb1eb22010-03-23 18:08:50 +0000559
Daniel Dunbar120bc772010-06-29 22:44:21 +0000560 OpInfo.LHS = EmitComplexToComplexCast(LHSComplexPair, LHSTy, OpInfo.Ty);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000561
Chris Lattnerab340c22007-08-26 22:09:01 +0000562 // Expand the binary operator.
563 ComplexPairTy Result = (this->*Func)(OpInfo);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000564
Chris Lattnerab340c22007-08-26 22:09:01 +0000565 // Truncate the result back to the LHS type.
566 Result = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy);
John McCallb418d742010-11-16 10:08:07 +0000567 Val = Result;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000568
Chris Lattnerab340c22007-08-26 22:09:01 +0000569 // Store the result value into the LHS lvalue.
John McCall0e800c92010-12-04 08:14:53 +0000570 EmitStoreThroughLValue(Result, LHS);
Daniel Dunbar120bc772010-06-29 22:44:21 +0000571
John McCallb418d742010-11-16 10:08:07 +0000572 return LHS;
Chris Lattnerab340c22007-08-26 22:09:01 +0000573}
Chris Lattner3219c5d2007-08-26 21:24:19 +0000574
John McCallb418d742010-11-16 10:08:07 +0000575// Compound assignments.
576ComplexPairTy ComplexExprEmitter::
577EmitCompoundAssign(const CompoundAssignOperator *E,
578 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
579 ComplexPairTy Val;
580 LValue LV = EmitCompoundAssignLValue(E, Func, Val);
581
582 // The result of an assignment in C is the assigned r-value.
583 if (!CGF.getContext().getLangOptions().CPlusPlus)
584 return Val;
585
586 // Objective-C property assignment never reloads the value following a store.
John McCall119a1c62010-12-04 02:32:38 +0000587 if (LV.isPropertyRef())
John McCallb418d742010-11-16 10:08:07 +0000588 return Val;
589
590 // If the lvalue is non-volatile, return the computed value of the assignment.
591 if (!LV.isVolatileQualified())
592 return Val;
593
594 return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified());
595}
596
597LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
598 ComplexPairTy &Val) {
Douglas Gregor63982352010-07-13 18:40:04 +0000599 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
600 E->getRHS()->getType()) &&
Chris Lattner96196622008-07-26 22:37:01 +0000601 "Invalid assignment");
John McCallb418d742010-11-16 10:08:07 +0000602 TestAndClearIgnoreReal();
603 TestAndClearIgnoreImag();
604
John McCallcd940a12010-12-06 06:10:02 +0000605 // Emit the RHS. __block variables need the RHS evaluated first.
John McCallb418d742010-11-16 10:08:07 +0000606 Val = Visit(E->getRHS());
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000607
608 // Compute the address to store into.
609 LValue LHS = CGF.EmitLValue(E->getLHS());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000610
Daniel Dunbar120bc772010-06-29 22:44:21 +0000611 // Store the result value into the LHS lvalue.
John McCall0e800c92010-12-04 08:14:53 +0000612 EmitStoreThroughLValue(Val, LHS);
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000613
John McCallb418d742010-11-16 10:08:07 +0000614 return LHS;
615}
Daniel Dunbar120bc772010-06-29 22:44:21 +0000616
John McCallb418d742010-11-16 10:08:07 +0000617ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
618 ComplexPairTy Val;
619 LValue LV = EmitBinAssignLValue(E, Val);
620
621 // The result of an assignment in C is the assigned r-value.
622 if (!CGF.getContext().getLangOptions().CPlusPlus)
623 return Val;
624
625 // Objective-C property assignment never reloads the value following a store.
John McCall119a1c62010-12-04 02:32:38 +0000626 if (LV.isPropertyRef())
John McCallb418d742010-11-16 10:08:07 +0000627 return Val;
628
629 // If the lvalue is non-volatile, return the computed value of the assignment.
630 if (!LV.isVolatileQualified())
631 return Val;
632
633 return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified());
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000634}
635
Chris Lattner756a4d82007-08-21 17:15:50 +0000636ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
John McCall2a416372010-12-05 02:00:02 +0000637 CGF.EmitIgnoredExpr(E->getLHS());
Chris Lattner756a4d82007-08-21 17:15:50 +0000638 return Visit(E->getRHS());
639}
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000640
641ComplexPairTy ComplexExprEmitter::
642VisitConditionalOperator(const ConditionalOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000643 TestAndClearIgnoreReal();
644 TestAndClearIgnoreImag();
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000645 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
646 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
647 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000648
John McCall150b4622011-01-26 04:00:11 +0000649 CodeGenFunction::ConditionalEvaluation eval(CGF);
650
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +0000651 if (E->getLHS())
652 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
653 else {
654 Expr *save = E->getSAVE();
655 assert(save && "VisitConditionalOperator - save is null");
John McCall150b4622011-01-26 04:00:11 +0000656 // Intentionally not doing direct assignment to ConditionalSaveExprs[save] !!
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +0000657 ComplexPairTy SaveVal = Visit(save);
658 CGF.ConditionalSaveComplexExprs[save] = SaveVal;
659 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
660 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000661
John McCall150b4622011-01-26 04:00:11 +0000662 eval.begin(CGF);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000663 CGF.EmitBlock(LHSBlock);
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +0000664 ComplexPairTy LHS = Visit(E->getTrueExpr());
Chris Lattner5083a532007-08-21 17:39:38 +0000665 LHSBlock = Builder.GetInsertBlock();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000666 CGF.EmitBranch(ContBlock);
John McCall150b4622011-01-26 04:00:11 +0000667 eval.end(CGF);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000668
John McCall150b4622011-01-26 04:00:11 +0000669 eval.begin(CGF);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000670 CGF.EmitBlock(RHSBlock);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000671 ComplexPairTy RHS = Visit(E->getRHS());
Chris Lattner5083a532007-08-21 17:39:38 +0000672 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000673 CGF.EmitBlock(ContBlock);
John McCall150b4622011-01-26 04:00:11 +0000674 eval.end(CGF);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000675
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000676 // Create a PHI node for the real part.
Chris Lattner5083a532007-08-21 17:39:38 +0000677 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000678 RealPN->reserveOperandSpace(2);
679 RealPN->addIncoming(LHS.first, LHSBlock);
680 RealPN->addIncoming(RHS.first, RHSBlock);
681
682 // Create a PHI node for the imaginary part.
Chris Lattner5083a532007-08-21 17:39:38 +0000683 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000684 ImagPN->reserveOperandSpace(2);
685 ImagPN->addIncoming(LHS.second, LHSBlock);
686 ImagPN->addIncoming(RHS.second, RHSBlock);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000687
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000688 return ComplexPairTy(RealPN, ImagPN);
689}
690
Chris Lattnerd0b12032007-08-24 02:18:47 +0000691ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Eli Friedman79769322009-03-04 05:52:32 +0000692 return Visit(E->getChosenSubExpr(CGF.getContext()));
Chris Lattnerd0b12032007-08-24 02:18:47 +0000693}
694
Eli Friedmanc4777f12008-05-13 23:11:35 +0000695ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000696 bool Ignore = TestAndClearIgnoreReal();
697 (void)Ignore;
698 assert (Ignore == false && "init list ignored");
699 Ignore = TestAndClearIgnoreImag();
700 (void)Ignore;
701 assert (Ignore == false && "init list ignored");
Eli Friedmanc4777f12008-05-13 23:11:35 +0000702 if (E->getNumInits())
703 return Visit(E->getInit(0));
704
705 // Empty init list intializes to null
John McCall183700f2009-09-21 23:43:11 +0000706 QualType Ty = E->getType()->getAs<ComplexType>()->getElementType();
Eli Friedmanc4777f12008-05-13 23:11:35 +0000707 const llvm::Type* LTy = CGF.ConvertType(Ty);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000708 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
Eli Friedmanc4777f12008-05-13 23:11:35 +0000709 return ComplexPairTy(zeroConstant, zeroConstant);
710}
711
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000712ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000713 llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr());
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000714 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType());
715
716 if (!ArgPtr) {
717 CGF.ErrorUnsupported(E, "complex va_arg expression");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000718 const llvm::Type *EltTy =
John McCall183700f2009-09-21 23:43:11 +0000719 CGF.ConvertType(E->getType()->getAs<ComplexType>()->getElementType());
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000720 llvm::Value *U = llvm::UndefValue::get(EltTy);
721 return ComplexPairTy(U, U);
722 }
723
724 // FIXME Volatility.
725 return EmitLoadOfComplex(ArgPtr, false);
726}
727
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000728//===----------------------------------------------------------------------===//
729// Entry Point into this File
730//===----------------------------------------------------------------------===//
731
732/// EmitComplexExpr - Emit the computation of the specified expression of
733/// complex type, ignoring the result.
Mike Stump7f79f9b2009-05-29 15:46:01 +0000734ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
John McCallb418d742010-11-16 10:08:07 +0000735 bool IgnoreImag) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000736 assert(E && E->getType()->isAnyComplexType() &&
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000737 "Invalid complex expression to emit");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000738
John McCallb418d742010-11-16 10:08:07 +0000739 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
Mike Stump7f79f9b2009-05-29 15:46:01 +0000740 .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.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000763ComplexPairTy CodeGenFunction::LoadComplexFromAddr(llvm::Value *SrcAddr,
Chris Lattner9b655512007-08-31 22:49:20 +0000764 bool SrcIsVolatile) {
765 return ComplexExprEmitter(*this).EmitLoadOfComplex(SrcAddr, SrcIsVolatile);
766}
John McCall83ce9d42010-11-16 23:07:28 +0000767
768LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
John McCall2a416372010-12-05 02:00:02 +0000769 assert(E->getOpcode() == BO_Assign);
John McCall83ce9d42010-11-16 23:07:28 +0000770 ComplexPairTy Val; // ignored
John McCall2a416372010-12-05 02:00:02 +0000771 return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
772}
John McCall83ce9d42010-11-16 23:07:28 +0000773
John McCall2a416372010-12-05 02:00:02 +0000774LValue CodeGenFunction::
775EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
John McCall83ce9d42010-11-16 23:07:28 +0000776 ComplexPairTy(ComplexExprEmitter::*Op)(const ComplexExprEmitter::BinOpInfo &);
John McCall83ce9d42010-11-16 23:07:28 +0000777 switch (E->getOpcode()) {
John McCall83ce9d42010-11-16 23:07:28 +0000778 case BO_MulAssign: Op = &ComplexExprEmitter::EmitBinMul; break;
779 case BO_DivAssign: Op = &ComplexExprEmitter::EmitBinDiv; break;
780 case BO_SubAssign: Op = &ComplexExprEmitter::EmitBinSub; break;
781 case BO_AddAssign: Op = &ComplexExprEmitter::EmitBinAdd; break;
782
783 default:
784 llvm_unreachable("unexpected complex compound assignment");
785 Op = 0;
786 }
787
John McCall2a416372010-12-05 02:00:02 +0000788 ComplexPairTy Val; // ignored
789 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
John McCall83ce9d42010-11-16 23:07:28 +0000790}