blob: abd0ca8527e0a93565b07da3ece854f73a3c7d8c [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;
Chris Lattner85e35682008-08-08 19:57:58 +000035 llvm::IRBuilder<> &Builder;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000036public:
Chris Lattner5083a532007-08-21 17:39:38 +000037 ComplexExprEmitter(CodeGenFunction &cgf) : CGF(cgf), Builder(CGF.Builder) {
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000038 }
39
40
41 //===--------------------------------------------------------------------===//
42 // Utilities
43 //===--------------------------------------------------------------------===//
44
45 /// EmitLoadOfLValue - Given an expression with complex type that represents a
46 /// value l-value, this method emits the address of the l-value, then loads
47 /// and returns the result.
Chris Lattner46d7d9f2007-08-21 17:28:34 +000048 ComplexPairTy EmitLoadOfLValue(const Expr *E) {
Chris Lattner76e80332007-08-21 18:02:02 +000049 LValue LV = CGF.EmitLValue(E);
50 // FIXME: Volatile
51 return EmitLoadOfComplex(LV.getAddress(), false);
Chris Lattner46d7d9f2007-08-21 17:28:34 +000052 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000053
Chris Lattner46d7d9f2007-08-21 17:28:34 +000054 /// EmitLoadOfComplex - Given a pointer to a complex value, emit code to load
55 /// the real and imaginary pieces.
Chris Lattner76e80332007-08-21 18:02:02 +000056 ComplexPairTy EmitLoadOfComplex(llvm::Value *SrcPtr, bool isVolatile);
Chris Lattner46d7d9f2007-08-21 17:28:34 +000057
58 /// EmitStoreOfComplex - Store the specified real/imag parts into the
59 /// specified value pointer.
Chris Lattner76e80332007-08-21 18:02:02 +000060 void EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *ResPtr, bool isVol);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000061
Chris Lattnerab340c22007-08-26 22:09:01 +000062 /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
63 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
64 QualType DestType);
65
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000066 //===--------------------------------------------------------------------===//
67 // Visitor Methods
68 //===--------------------------------------------------------------------===//
Chris Lattner776c6492007-08-21 18:51:13 +000069
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000070 ComplexPairTy VisitStmt(Stmt *S) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000071 S->dump(CGF.getContext().getSourceManager());
Chris Lattner776c6492007-08-21 18:51:13 +000072 assert(0 && "Stmt can't have complex result type!");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000073 return ComplexPairTy();
74 }
Chris Lattner776c6492007-08-21 18:51:13 +000075 ComplexPairTy VisitExpr(Expr *S);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000076 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
Chris Lattnerd272ff02007-08-26 05:57:57 +000077 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
Chris Lattnerdb68f1b2007-08-26 03:51:12 +000078
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000079 // l-values.
Chris Lattnerd272ff02007-08-26 05:57:57 +000080 ComplexPairTy VisitDeclRefExpr(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattner8bcd7232007-08-21 18:03:58 +000081 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerd272ff02007-08-26 05:57:57 +000082 ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000083
Chris Lattnere73e8e22007-08-21 22:33:41 +000084 // FIXME: CompoundLiteralExpr
Chris Lattnerd272ff02007-08-26 05:57:57 +000085
86 ComplexPairTy EmitCast(Expr *Op, QualType DestTy);
87 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
88 // Unlike for scalars, we don't have to worry about function->ptr demotion
89 // here.
90 return EmitCast(E->getSubExpr(), E->getType());
91 }
92 ComplexPairTy VisitCastExpr(CastExpr *E) {
93 return EmitCast(E->getSubExpr(), E->getType());
94 }
Chris Lattner419d25e2007-08-23 21:38:16 +000095 ComplexPairTy VisitCallExpr(const CallExpr *E);
Chris Lattner7e20d092007-08-31 22:51:38 +000096 ComplexPairTy VisitStmtExpr(const StmtExpr *E);
Nate Begeman67295d02008-01-30 20:50:20 +000097 ComplexPairTy VisitOverloadExpr(const OverloadExpr *OE);
Chris Lattner7e20d092007-08-31 22:51:38 +000098
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000099 // Operators.
Chris Lattner3070f982007-08-21 22:25:29 +0000100 ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
101 bool isInc, bool isPre);
102 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
103 return VisitPrePostIncDec(E, false, false);
104 }
105 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
106 return VisitPrePostIncDec(E, true, false);
107 }
108 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
109 return VisitPrePostIncDec(E, false, true);
110 }
111 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
112 return VisitPrePostIncDec(E, true, true);
113 }
114 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000115 ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
116 return Visit(E->getSubExpr());
117 }
118 ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
Chris Lattnere98a11c2007-08-21 20:41:44 +0000119 ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
120 // LNot,SizeOf,AlignOf,Real,Imag never return complex.
121 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
122 return Visit(E->getSubExpr());
123 }
Chris Lattner04421082008-04-08 04:40:51 +0000124 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
125 return Visit(DAE->getExpr());
126 }
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000127 ComplexPairTy VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
128 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
129 QualType Elem = E->getType()->getAsComplexType()->getElementType();
130 llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
131 return ComplexPairTy(Null, Null);
132 }
Chris Lattnere98a11c2007-08-21 20:41:44 +0000133
Chris Lattnerab340c22007-08-26 22:09:01 +0000134 struct BinOpInfo {
135 ComplexPairTy LHS;
136 ComplexPairTy RHS;
137 QualType Ty; // Computation Type.
138 };
139
140 BinOpInfo EmitBinOps(const BinaryOperator *E);
141 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
142 ComplexPairTy (ComplexExprEmitter::*Func)
143 (const BinOpInfo &));
144
145 ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
146 ComplexPairTy EmitBinSub(const BinOpInfo &Op);
147 ComplexPairTy EmitBinMul(const BinOpInfo &Op);
148 ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
149
150 ComplexPairTy VisitBinMul(const BinaryOperator *E) {
151 return EmitBinMul(EmitBinOps(E));
152 }
153 ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
154 return EmitBinAdd(EmitBinOps(E));
155 }
156 ComplexPairTy VisitBinSub(const BinaryOperator *E) {
157 return EmitBinSub(EmitBinOps(E));
158 }
159 ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
160 return EmitBinDiv(EmitBinOps(E));
161 }
162
163 // Compound assignments.
164 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
165 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
166 }
167 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
168 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
169 }
170 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
171 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
172 }
173 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
174 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
175 }
176
Chris Lattner612c40c2007-08-26 21:27:07 +0000177 // GCC rejects rem/and/or/xor for integer complex.
Chris Lattner4034edb2007-08-21 17:12:50 +0000178 // Logical and/or always return int, never complex.
Chris Lattner58dee102007-08-21 16:57:55 +0000179
180 // No comparisons produce a complex result.
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000181 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
Chris Lattner756a4d82007-08-21 17:15:50 +0000182 ComplexPairTy VisitBinComma (const BinaryOperator *E);
183
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000184
185 ComplexPairTy VisitConditionalOperator(const ConditionalOperator *CO);
Chris Lattnerd0b12032007-08-24 02:18:47 +0000186 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
Eli Friedmanc4777f12008-05-13 23:11:35 +0000187
188 ComplexPairTy VisitInitListExpr(InitListExpr *E);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000189};
190} // end anonymous namespace.
191
192//===----------------------------------------------------------------------===//
193// Utilities
194//===----------------------------------------------------------------------===//
195
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000196/// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
197/// load the real and imaginary pieces, returning them as Real/Imag.
Chris Lattner76e80332007-08-21 18:02:02 +0000198ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr,
199 bool isVolatile) {
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000200 llvm::SmallString<64> Name(SrcPtr->getNameStart(),
201 SrcPtr->getNameStart()+SrcPtr->getNameLen());
202
203 Name += ".realp";
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000204 llvm::Value *RealPtr = Builder.CreateStructGEP(SrcPtr, 0, Name.c_str());
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000205
206 Name.pop_back(); // .realp -> .real
207 llvm::Value *Real = Builder.CreateLoad(RealPtr, isVolatile, Name.c_str());
208
209 Name.resize(Name.size()-4); // .real -> .imagp
210 Name += "imagp";
Ted Kremenekd6278892007-09-04 17:20:08 +0000211
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000212 llvm::Value *ImagPtr = Builder.CreateStructGEP(SrcPtr, 1, Name.c_str());
Ted Kremenekd6278892007-09-04 17:20:08 +0000213
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000214 Name.pop_back(); // .imagp -> .imag
215 llvm::Value *Imag = Builder.CreateLoad(ImagPtr, isVolatile, Name.c_str());
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000216 return ComplexPairTy(Real, Imag);
217}
218
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000219/// EmitStoreOfComplex - Store the specified real/imag parts into the
220/// specified value pointer.
Chris Lattner76e80332007-08-21 18:02:02 +0000221void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr,
222 bool isVolatile) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000223 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real");
224 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag");
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000225
Chris Lattner76e80332007-08-21 18:02:02 +0000226 Builder.CreateStore(Val.first, RealPtr, isVolatile);
227 Builder.CreateStore(Val.second, ImagPtr, isVolatile);
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000228}
229
230
231
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000232//===----------------------------------------------------------------------===//
233// Visitor Methods
234//===----------------------------------------------------------------------===//
235
Chris Lattner776c6492007-08-21 18:51:13 +0000236ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000237 CGF.ErrorUnsupported(E, "complex expression");
Chris Lattner776c6492007-08-21 18:51:13 +0000238 const llvm::Type *EltTy =
239 CGF.ConvertType(E->getType()->getAsComplexType()->getElementType());
240 llvm::Value *U = llvm::UndefValue::get(EltTy);
241 return ComplexPairTy(U, U);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000242}
243
Chris Lattnerd272ff02007-08-26 05:57:57 +0000244ComplexPairTy ComplexExprEmitter::
245VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000246 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
247 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
248}
249
250
Chris Lattner419d25e2007-08-23 21:38:16 +0000251ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
Chris Lattner9b655512007-08-31 22:49:20 +0000252 return CGF.EmitCallExpr(E).getComplexVal();
Chris Lattner419d25e2007-08-23 21:38:16 +0000253}
254
Nate Begeman67295d02008-01-30 20:50:20 +0000255ComplexPairTy ComplexExprEmitter::VisitOverloadExpr(const OverloadExpr *E) {
Ted Kremenek55499762008-06-17 02:43:46 +0000256 return CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
257 E->arg_end(CGF.getContext())).getComplexVal();
Nate Begeman67295d02008-01-30 20:50:20 +0000258}
259
Chris Lattner7e20d092007-08-31 22:51:38 +0000260ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
261 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getComplexVal();
262}
263
Chris Lattnerab340c22007-08-26 22:09:01 +0000264/// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
265ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
266 QualType SrcType,
267 QualType DestType) {
268 // Get the src/dest element type.
Chris Lattner96196622008-07-26 22:37:01 +0000269 SrcType = SrcType->getAsComplexType()->getElementType();
270 DestType = DestType->getAsComplexType()->getElementType();
Chris Lattnerd272ff02007-08-26 05:57:57 +0000271
Chris Lattnerab340c22007-08-26 22:09:01 +0000272 // C99 6.3.1.6: When a value of complextype is converted to another
273 // complex type, both the real and imaginary parts followthe conversion
274 // rules for the corresponding real types.
275 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType);
276 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType);
277 return Val;
278}
279
280ComplexPairTy ComplexExprEmitter::EmitCast(Expr *Op, QualType DestTy) {
Chris Lattnerd272ff02007-08-26 05:57:57 +0000281 // Two cases here: cast from (complex to complex) and (scalar to complex).
Chris Lattner9b2dc282008-04-04 16:54:41 +0000282 if (Op->getType()->isAnyComplexType())
Chris Lattnerab340c22007-08-26 22:09:01 +0000283 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
284
Chris Lattnerd272ff02007-08-26 05:57:57 +0000285 // C99 6.3.1.7: When a value of real type is converted to a complex type, the
286 // real part of the complex result value is determined by the rules of
287 // conversion to the corresponding real type and the imaginary part of the
288 // complex result value is a positive zero or an unsigned zero.
289 llvm::Value *Elt = CGF.EmitScalarExpr(Op);
290
291 // Convert the input element to the element type of the complex.
Chris Lattner96196622008-07-26 22:37:01 +0000292 DestTy = DestTy->getAsComplexType()->getElementType();
Chris Lattner3707b252007-08-26 06:48:56 +0000293 Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy);
Chris Lattnerd272ff02007-08-26 05:57:57 +0000294
295 // Return (realval, 0).
296 return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType()));
297}
298
Chris Lattner3070f982007-08-21 22:25:29 +0000299ComplexPairTy ComplexExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
300 bool isInc, bool isPre) {
301 LValue LV = CGF.EmitLValue(E->getSubExpr());
302 // FIXME: Handle volatile!
303 ComplexPairTy InVal = EmitLoadOfComplex(LV.getAddress(), false);
304
Hartmut Kaiser124dd7b2007-09-07 14:34:20 +0000305 uint64_t AmountVal = isInc ? 1 : -1;
Chris Lattner3070f982007-08-21 22:25:29 +0000306
307 llvm::Value *NextVal;
308 if (isa<llvm::IntegerType>(InVal.first->getType()))
309 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal);
Chris Lattnerca2617c2007-09-13 06:19:18 +0000310 else if (InVal.first->getType() == llvm::Type::FloatTy)
311 // FIXME: Handle long double.
Devang Patele9b8c0a2007-10-30 20:59:40 +0000312 NextVal =
Chris Lattner2c62a1a2008-05-04 18:23:51 +0000313 llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal)));
Chris Lattnerca2617c2007-09-13 06:19:18 +0000314 else {
315 // FIXME: Handle long double.
316 assert(InVal.first->getType() == llvm::Type::DoubleTy);
Devang Patele9b8c0a2007-10-30 20:59:40 +0000317 NextVal =
Chris Lattner2c62a1a2008-05-04 18:23:51 +0000318 llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal)));
Chris Lattnerca2617c2007-09-13 06:19:18 +0000319 }
Chris Lattner3070f982007-08-21 22:25:29 +0000320
321 // Add the inc/dec to the real part.
322 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
323
324 ComplexPairTy IncVal(NextVal, InVal.second);
325
326 // Store the updated result through the lvalue.
327 EmitStoreOfComplex(IncVal, LV.getAddress(), false); /* FIXME: Volatile */
328
329 // If this is a postinc, return the value read from memory, otherwise use the
330 // updated value.
331 return isPre ? IncVal : InVal;
332}
333
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000334ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
335 ComplexPairTy Op = Visit(E->getSubExpr());
336 llvm::Value *ResR = Builder.CreateNeg(Op.first, "neg.r");
337 llvm::Value *ResI = Builder.CreateNeg(Op.second, "neg.i");
338 return ComplexPairTy(ResR, ResI);
339}
340
Chris Lattnere98a11c2007-08-21 20:41:44 +0000341ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
342 // ~(a+ib) = a + i*-b
343 ComplexPairTy Op = Visit(E->getSubExpr());
344 llvm::Value *ResI = Builder.CreateNeg(Op.second, "conj.i");
345 return ComplexPairTy(Op.first, ResI);
346}
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000347
Chris Lattnerab340c22007-08-26 22:09:01 +0000348ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
349 llvm::Value *ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
350 llvm::Value *ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000351 return ComplexPairTy(ResR, ResI);
352}
353
Chris Lattnerab340c22007-08-26 22:09:01 +0000354ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
355 llvm::Value *ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
356 llvm::Value *ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
Chris Lattner6bc1ade2007-08-23 23:46:33 +0000357 return ComplexPairTy(ResR, ResI);
358}
359
360
Chris Lattnerab340c22007-08-26 22:09:01 +0000361ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
362 llvm::Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
363 llvm::Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second,"mul.rr");
Chris Lattner5083a532007-08-21 17:39:38 +0000364 llvm::Value *ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
Chris Lattner2823c192007-08-21 16:34:16 +0000365
Chris Lattnerab340c22007-08-26 22:09:01 +0000366 llvm::Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
367 llvm::Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
Chris Lattner5083a532007-08-21 17:39:38 +0000368 llvm::Value *ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
Chris Lattner2823c192007-08-21 16:34:16 +0000369 return ComplexPairTy(ResR, ResI);
370}
371
Chris Lattnerab340c22007-08-26 22:09:01 +0000372ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
373 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
374 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
Chris Lattner3219c5d2007-08-26 21:24:19 +0000375
376 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
377 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr, "tmp"); // a*c
378 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi, "tmp"); // b*d
379 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2, "tmp"); // ac+bd
380
381 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr, "tmp"); // c*c
382 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi, "tmp"); // d*d
383 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5, "tmp"); // cc+dd
384
385 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr, "tmp"); // b*c
386 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi, "tmp"); // a*d
387 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8, "tmp"); // bc-ad
388
389 llvm::Value *DSTr, *DSTi;
390 if (Tmp3->getType()->isFloatingPoint()) {
391 DSTr = Builder.CreateFDiv(Tmp3, Tmp6, "tmp");
392 DSTi = Builder.CreateFDiv(Tmp9, Tmp6, "tmp");
393 } else {
Chris Lattnerab340c22007-08-26 22:09:01 +0000394 if (Op.Ty->getAsComplexType()->getElementType()->isUnsignedIntegerType()) {
Chris Lattner3219c5d2007-08-26 21:24:19 +0000395 DSTr = Builder.CreateUDiv(Tmp3, Tmp6, "tmp");
396 DSTi = Builder.CreateUDiv(Tmp9, Tmp6, "tmp");
397 } else {
398 DSTr = Builder.CreateSDiv(Tmp3, Tmp6, "tmp");
399 DSTi = Builder.CreateSDiv(Tmp9, Tmp6, "tmp");
400 }
401 }
402
403 return ComplexPairTy(DSTr, DSTi);
404}
405
Chris Lattnerab340c22007-08-26 22:09:01 +0000406ComplexExprEmitter::BinOpInfo
407ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
408 BinOpInfo Ops;
409 Ops.LHS = Visit(E->getLHS());
410 Ops.RHS = Visit(E->getRHS());
411 Ops.Ty = E->getType();
412 return Ops;
413}
414
415
416// Compound assignments.
417ComplexPairTy ComplexExprEmitter::
418EmitCompoundAssign(const CompoundAssignOperator *E,
419 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
420 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
421
422 // Load the LHS and RHS operands.
423 LValue LHSLV = CGF.EmitLValue(E->getLHS());
424
425 BinOpInfo OpInfo;
426 OpInfo.Ty = E->getComputationType();
427
428 // We know the LHS is a complex lvalue.
429 OpInfo.LHS = EmitLoadOfComplex(LHSLV.getAddress(), false);// FIXME: Volatile.
430 OpInfo.LHS = EmitComplexToComplexCast(OpInfo.LHS, LHSTy, OpInfo.Ty);
431
432 // It is possible for the RHS to be complex or scalar.
433 OpInfo.RHS = EmitCast(E->getRHS(), OpInfo.Ty);
434
435 // Expand the binary operator.
436 ComplexPairTy Result = (this->*Func)(OpInfo);
437
438 // Truncate the result back to the LHS type.
439 Result = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy);
440
441 // Store the result value into the LHS lvalue.
442 EmitStoreOfComplex(Result, LHSLV.getAddress(), false); // FIXME: VOLATILE
443 return Result;
444}
Chris Lattner3219c5d2007-08-26 21:24:19 +0000445
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000446ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Chris Lattner96196622008-07-26 22:37:01 +0000447 assert(CGF.getContext().getCanonicalType(E->getLHS()->getType()) ==
448 CGF.getContext().getCanonicalType(E->getRHS()->getType()) &&
449 "Invalid assignment");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000450 // Emit the RHS.
451 ComplexPairTy Val = Visit(E->getRHS());
452
453 // Compute the address to store into.
454 LValue LHS = CGF.EmitLValue(E->getLHS());
455
456 // Store into it.
457 // FIXME: Volatility!
Chris Lattner76e80332007-08-21 18:02:02 +0000458 EmitStoreOfComplex(Val, LHS.getAddress(), false);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000459 return Val;
460}
461
Chris Lattner756a4d82007-08-21 17:15:50 +0000462ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
463 CGF.EmitStmt(E->getLHS());
464 return Visit(E->getRHS());
465}
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000466
467ComplexPairTy ComplexExprEmitter::
468VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif984d0b42008-04-06 20:42:52 +0000469 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
470 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
471 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000472
473 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner5083a532007-08-21 17:39:38 +0000474 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000475
476 CGF.EmitBlock(LHSBlock);
477
478 // Handle the GNU extension for missing LHS.
Chris Lattner05ba49c2007-08-21 05:54:53 +0000479 assert(E->getLHS() && "Must have LHS for complex value");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000480
481 ComplexPairTy LHS = Visit(E->getLHS());
Chris Lattner5083a532007-08-21 17:39:38 +0000482 Builder.CreateBr(ContBlock);
483 LHSBlock = Builder.GetInsertBlock();
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000484
485 CGF.EmitBlock(RHSBlock);
486
487 ComplexPairTy RHS = Visit(E->getRHS());
Chris Lattner5083a532007-08-21 17:39:38 +0000488 Builder.CreateBr(ContBlock);
489 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000490
491 CGF.EmitBlock(ContBlock);
492
493 // Create a PHI node for the real part.
Chris Lattner5083a532007-08-21 17:39:38 +0000494 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000495 RealPN->reserveOperandSpace(2);
496 RealPN->addIncoming(LHS.first, LHSBlock);
497 RealPN->addIncoming(RHS.first, RHSBlock);
498
499 // Create a PHI node for the imaginary part.
Chris Lattner5083a532007-08-21 17:39:38 +0000500 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000501 ImagPN->reserveOperandSpace(2);
502 ImagPN->addIncoming(LHS.second, LHSBlock);
503 ImagPN->addIncoming(RHS.second, RHSBlock);
504
505 return ComplexPairTy(RealPN, ImagPN);
506}
507
Chris Lattnerd0b12032007-08-24 02:18:47 +0000508ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Chris Lattnerd0b12032007-08-24 02:18:47 +0000509 // Emit the LHS or RHS as appropriate.
Chris Lattner27437ca2007-10-25 00:29:32 +0000510 return Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() :E->getRHS());
Chris Lattnerd0b12032007-08-24 02:18:47 +0000511}
512
Eli Friedmanc4777f12008-05-13 23:11:35 +0000513ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
514 if (E->getNumInits())
515 return Visit(E->getInit(0));
516
517 // Empty init list intializes to null
518 QualType Ty = E->getType()->getAsComplexType()->getElementType();
519 const llvm::Type* LTy = CGF.ConvertType(Ty);
520 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
521 return ComplexPairTy(zeroConstant, zeroConstant);
522}
523
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000524//===----------------------------------------------------------------------===//
525// Entry Point into this File
526//===----------------------------------------------------------------------===//
527
528/// EmitComplexExpr - Emit the computation of the specified expression of
529/// complex type, ignoring the result.
Chris Lattner58dee102007-08-21 16:57:55 +0000530ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000531 assert(E && E->getType()->isAnyComplexType() &&
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000532 "Invalid complex expression to emit");
533
Chris Lattner58dee102007-08-21 16:57:55 +0000534 return ComplexExprEmitter(*this).Visit(const_cast<Expr*>(E));
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000535}
536
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000537/// EmitComplexExprIntoAddr - Emit the computation of the specified expression
538/// of complex type, storing into the specified Value*.
539void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E,
Chris Lattner190dbe22007-08-26 16:22:13 +0000540 llvm::Value *DestAddr,
541 bool DestIsVolatile) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000542 assert(E && E->getType()->isAnyComplexType() &&
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000543 "Invalid complex expression to emit");
544 ComplexExprEmitter Emitter(*this);
545 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
Chris Lattner190dbe22007-08-26 16:22:13 +0000546 Emitter.EmitStoreOfComplex(Val, DestAddr, DestIsVolatile);
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000547}
Chris Lattner9b655512007-08-31 22:49:20 +0000548
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000549/// StoreComplexToAddr - Store a complex number into the specified address.
550void CodeGenFunction::StoreComplexToAddr(ComplexPairTy V,
551 llvm::Value *DestAddr,
552 bool DestIsVolatile) {
553 ComplexExprEmitter(*this).EmitStoreOfComplex(V, DestAddr, DestIsVolatile);
554}
555
Chris Lattner9b655512007-08-31 22:49:20 +0000556/// LoadComplexFromAddr - Load a complex number from the specified address.
557ComplexPairTy CodeGenFunction::LoadComplexFromAddr(llvm::Value *SrcAddr,
558 bool SrcIsVolatile) {
559 return ComplexExprEmitter(*this).EmitLoadOfComplex(SrcAddr, SrcIsVolatile);
560}