blob: aa66bb6dfbf4eb5f037775b054d3c6fa74f890a2 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
16#include "clang/AST/AST.h"
17#include "llvm/Constants.h"
18#include "llvm/Function.h"
Chris Lattner6b5d0bf2007-08-26 22:47:40 +000019#include "llvm/ADT/SmallString.h"
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000020#include "llvm/Support/Compiler.h"
21using 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 {
31class VISIBILITY_HIDDEN ComplexExprEmitter
32 : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
33 CodeGenFunction &CGF;
Devang Patel50c90342007-10-09 19:49:58 +000034 llvm::LLVMFoldingBuilder &Builder;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000035public:
Chris Lattner5083a532007-08-21 17:39:38 +000036 ComplexExprEmitter(CodeGenFunction &cgf) : CGF(cgf), Builder(CGF.Builder) {
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000037 }
38
39
40 //===--------------------------------------------------------------------===//
41 // Utilities
42 //===--------------------------------------------------------------------===//
43
44 /// EmitLoadOfLValue - Given an expression with complex type that represents a
45 /// value l-value, this method emits the address of the l-value, then loads
46 /// and returns the result.
Chris Lattner46d7d9f2007-08-21 17:28:34 +000047 ComplexPairTy EmitLoadOfLValue(const Expr *E) {
Chris Lattner76e80332007-08-21 18:02:02 +000048 LValue LV = CGF.EmitLValue(E);
49 // FIXME: Volatile
50 return EmitLoadOfComplex(LV.getAddress(), false);
Chris Lattner46d7d9f2007-08-21 17:28:34 +000051 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000052
Chris Lattner46d7d9f2007-08-21 17:28:34 +000053 /// EmitLoadOfComplex - Given a pointer to a complex value, emit code to load
54 /// the real and imaginary pieces.
Chris Lattner76e80332007-08-21 18:02:02 +000055 ComplexPairTy EmitLoadOfComplex(llvm::Value *SrcPtr, bool isVolatile);
Chris Lattner46d7d9f2007-08-21 17:28:34 +000056
57 /// EmitStoreOfComplex - Store the specified real/imag parts into the
58 /// specified value pointer.
Chris Lattner76e80332007-08-21 18:02:02 +000059 void EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *ResPtr, bool isVol);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000060
Chris Lattnerab340c22007-08-26 22:09:01 +000061 /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
62 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
63 QualType DestType);
64
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000065 //===--------------------------------------------------------------------===//
66 // Visitor Methods
67 //===--------------------------------------------------------------------===//
Chris Lattner776c6492007-08-21 18:51:13 +000068
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000069 ComplexPairTy VisitStmt(Stmt *S) {
Chris Lattner419ea7e2007-09-13 01:17:29 +000070 S->dump(CGF.getContext().SourceMgr);
Chris Lattner776c6492007-08-21 18:51:13 +000071 assert(0 && "Stmt can't have complex result type!");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000072 return ComplexPairTy();
73 }
Chris Lattner776c6492007-08-21 18:51:13 +000074 ComplexPairTy VisitExpr(Expr *S);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000075 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
Chris Lattnerd272ff02007-08-26 05:57:57 +000076 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
Chris Lattnerdb68f1b2007-08-26 03:51:12 +000077
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000078 // l-values.
Chris Lattnerd272ff02007-08-26 05:57:57 +000079 ComplexPairTy VisitDeclRefExpr(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattner8bcd7232007-08-21 18:03:58 +000080 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerd272ff02007-08-26 05:57:57 +000081 ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000082
Chris Lattnere73e8e22007-08-21 22:33:41 +000083 // FIXME: CompoundLiteralExpr
Chris Lattnerd272ff02007-08-26 05:57:57 +000084
85 ComplexPairTy EmitCast(Expr *Op, QualType DestTy);
86 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
87 // Unlike for scalars, we don't have to worry about function->ptr demotion
88 // here.
89 return EmitCast(E->getSubExpr(), E->getType());
90 }
91 ComplexPairTy VisitCastExpr(CastExpr *E) {
92 return EmitCast(E->getSubExpr(), E->getType());
93 }
Chris Lattner419d25e2007-08-23 21:38:16 +000094 ComplexPairTy VisitCallExpr(const CallExpr *E);
Chris Lattner7e20d092007-08-31 22:51:38 +000095 ComplexPairTy VisitStmtExpr(const StmtExpr *E);
96
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000097 // Operators.
Chris Lattner3070f982007-08-21 22:25:29 +000098 ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
99 bool isInc, bool isPre);
100 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
101 return VisitPrePostIncDec(E, false, false);
102 }
103 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
104 return VisitPrePostIncDec(E, true, false);
105 }
106 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
107 return VisitPrePostIncDec(E, false, true);
108 }
109 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
110 return VisitPrePostIncDec(E, true, true);
111 }
112 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000113 ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
114 return Visit(E->getSubExpr());
115 }
116 ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
Chris Lattnere98a11c2007-08-21 20:41:44 +0000117 ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
118 // LNot,SizeOf,AlignOf,Real,Imag never return complex.
119 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
120 return Visit(E->getSubExpr());
121 }
122
Chris Lattnerab340c22007-08-26 22:09:01 +0000123 struct BinOpInfo {
124 ComplexPairTy LHS;
125 ComplexPairTy RHS;
126 QualType Ty; // Computation Type.
127 };
128
129 BinOpInfo EmitBinOps(const BinaryOperator *E);
130 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
131 ComplexPairTy (ComplexExprEmitter::*Func)
132 (const BinOpInfo &));
133
134 ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
135 ComplexPairTy EmitBinSub(const BinOpInfo &Op);
136 ComplexPairTy EmitBinMul(const BinOpInfo &Op);
137 ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
138
139 ComplexPairTy VisitBinMul(const BinaryOperator *E) {
140 return EmitBinMul(EmitBinOps(E));
141 }
142 ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
143 return EmitBinAdd(EmitBinOps(E));
144 }
145 ComplexPairTy VisitBinSub(const BinaryOperator *E) {
146 return EmitBinSub(EmitBinOps(E));
147 }
148 ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
149 return EmitBinDiv(EmitBinOps(E));
150 }
151
152 // Compound assignments.
153 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
154 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
155 }
156 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
157 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
158 }
159 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
160 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
161 }
162 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
163 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
164 }
165
Chris Lattner612c40c2007-08-26 21:27:07 +0000166 // GCC rejects rem/and/or/xor for integer complex.
Chris Lattner4034edb2007-08-21 17:12:50 +0000167 // Logical and/or always return int, never complex.
Chris Lattner58dee102007-08-21 16:57:55 +0000168
169 // No comparisons produce a complex result.
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000170 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
Chris Lattner756a4d82007-08-21 17:15:50 +0000171 ComplexPairTy VisitBinComma (const BinaryOperator *E);
172
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000173
174 ComplexPairTy VisitConditionalOperator(const ConditionalOperator *CO);
Chris Lattnerd0b12032007-08-24 02:18:47 +0000175 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000176};
177} // end anonymous namespace.
178
179//===----------------------------------------------------------------------===//
180// Utilities
181//===----------------------------------------------------------------------===//
182
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000183/// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to
184/// load the real and imaginary pieces, returning them as Real/Imag.
Chris Lattner76e80332007-08-21 18:02:02 +0000185ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr,
186 bool isVolatile) {
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000187 llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
188 llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000189
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000190 llvm::SmallString<64> Name(SrcPtr->getNameStart(),
191 SrcPtr->getNameStart()+SrcPtr->getNameLen());
192
193 Name += ".realp";
Ted Kremenekd6278892007-09-04 17:20:08 +0000194 llvm::Value *Ops[] = {Zero, Zero};
195 llvm::Value *RealPtr = Builder.CreateGEP(SrcPtr, Ops, Ops+2, Name.c_str());
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000196
197 Name.pop_back(); // .realp -> .real
198 llvm::Value *Real = Builder.CreateLoad(RealPtr, isVolatile, Name.c_str());
199
200 Name.resize(Name.size()-4); // .real -> .imagp
201 Name += "imagp";
Ted Kremenekd6278892007-09-04 17:20:08 +0000202
203 Ops[1] = One; // { Ops = { Zero, One }
204 llvm::Value *ImagPtr = Builder.CreateGEP(SrcPtr, Ops, Ops+2, Name.c_str());
205
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000206 Name.pop_back(); // .imagp -> .imag
207 llvm::Value *Imag = Builder.CreateLoad(ImagPtr, isVolatile, Name.c_str());
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000208 return ComplexPairTy(Real, Imag);
209}
210
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000211/// EmitStoreOfComplex - Store the specified real/imag parts into the
212/// specified value pointer.
Chris Lattner76e80332007-08-21 18:02:02 +0000213void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr,
214 bool isVolatile) {
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000215 llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
216 llvm::Constant *One = llvm::ConstantInt::get(llvm::Type::Int32Ty, 1);
Ted Kremenekd6278892007-09-04 17:20:08 +0000217
218 llvm::Value *Ops[] = {Zero, Zero};
219 llvm::Value *RealPtr = Builder.CreateGEP(Ptr, Ops, Ops+2, "real");
220
221 Ops[1] = One; // { Ops = { Zero, One }
222 llvm::Value *ImagPtr = Builder.CreateGEP(Ptr, Ops, Ops+2, "imag");
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000223
Chris Lattner76e80332007-08-21 18:02:02 +0000224 Builder.CreateStore(Val.first, RealPtr, isVolatile);
225 Builder.CreateStore(Val.second, ImagPtr, isVolatile);
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000226}
227
228
229
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000230//===----------------------------------------------------------------------===//
231// Visitor Methods
232//===----------------------------------------------------------------------===//
233
Chris Lattner776c6492007-08-21 18:51:13 +0000234ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
235 fprintf(stderr, "Unimplemented complex expr!\n");
Chris Lattner419ea7e2007-09-13 01:17:29 +0000236 E->dump(CGF.getContext().SourceMgr);
Chris Lattner776c6492007-08-21 18:51:13 +0000237 const llvm::Type *EltTy =
238 CGF.ConvertType(E->getType()->getAsComplexType()->getElementType());
239 llvm::Value *U = llvm::UndefValue::get(EltTy);
240 return ComplexPairTy(U, U);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000241}
242
Chris Lattnerd272ff02007-08-26 05:57:57 +0000243ComplexPairTy ComplexExprEmitter::
244VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000245 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
246 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
247}
248
249
Chris Lattner419d25e2007-08-23 21:38:16 +0000250ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
Chris Lattner9b655512007-08-31 22:49:20 +0000251 return CGF.EmitCallExpr(E).getComplexVal();
Chris Lattner419d25e2007-08-23 21:38:16 +0000252}
253
Chris Lattner7e20d092007-08-31 22:51:38 +0000254ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
255 return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getComplexVal();
256}
257
Chris Lattnerab340c22007-08-26 22:09:01 +0000258/// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
259ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
260 QualType SrcType,
261 QualType DestType) {
262 // Get the src/dest element type.
263 SrcType = cast<ComplexType>(SrcType.getCanonicalType())->getElementType();
264 DestType = cast<ComplexType>(DestType.getCanonicalType())->getElementType();
Chris Lattnerd272ff02007-08-26 05:57:57 +0000265
Chris Lattnerab340c22007-08-26 22:09:01 +0000266 // C99 6.3.1.6: When a value of complextype is converted to another
267 // complex type, both the real and imaginary parts followthe conversion
268 // rules for the corresponding real types.
269 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType);
270 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType);
271 return Val;
272}
273
274ComplexPairTy ComplexExprEmitter::EmitCast(Expr *Op, QualType DestTy) {
Chris Lattnerd272ff02007-08-26 05:57:57 +0000275 // Two cases here: cast from (complex to complex) and (scalar to complex).
Chris Lattnerab340c22007-08-26 22:09:01 +0000276 if (Op->getType()->isComplexType())
277 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
278
Chris Lattnerd272ff02007-08-26 05:57:57 +0000279 // C99 6.3.1.7: When a value of real type is converted to a complex type, the
280 // real part of the complex result value is determined by the rules of
281 // conversion to the corresponding real type and the imaginary part of the
282 // complex result value is a positive zero or an unsigned zero.
283 llvm::Value *Elt = CGF.EmitScalarExpr(Op);
284
285 // Convert the input element to the element type of the complex.
Chris Lattnerab340c22007-08-26 22:09:01 +0000286 DestTy = cast<ComplexType>(DestTy.getCanonicalType())->getElementType();
Chris Lattner3707b252007-08-26 06:48:56 +0000287 Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy);
Chris Lattnerd272ff02007-08-26 05:57:57 +0000288
289 // Return (realval, 0).
290 return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType()));
291}
292
Chris Lattner3070f982007-08-21 22:25:29 +0000293ComplexPairTy ComplexExprEmitter::VisitPrePostIncDec(const UnaryOperator *E,
294 bool isInc, bool isPre) {
295 LValue LV = CGF.EmitLValue(E->getSubExpr());
296 // FIXME: Handle volatile!
297 ComplexPairTy InVal = EmitLoadOfComplex(LV.getAddress(), false);
298
Hartmut Kaiser124dd7b2007-09-07 14:34:20 +0000299 uint64_t AmountVal = isInc ? 1 : -1;
Chris Lattner3070f982007-08-21 22:25:29 +0000300
301 llvm::Value *NextVal;
302 if (isa<llvm::IntegerType>(InVal.first->getType()))
303 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal);
Chris Lattnerca2617c2007-09-13 06:19:18 +0000304 else if (InVal.first->getType() == llvm::Type::FloatTy)
305 // FIXME: Handle long double.
Hartmut Kaiser124dd7b2007-09-07 14:34:20 +0000306 NextVal = llvm::ConstantFP::get(InVal.first->getType(),
Chris Lattnerca2617c2007-09-13 06:19:18 +0000307 llvm::APFloat(static_cast<float>(AmountVal)));
308 else {
309 // FIXME: Handle long double.
310 assert(InVal.first->getType() == llvm::Type::DoubleTy);
311 NextVal = llvm::ConstantFP::get(InVal.first->getType(),
312 llvm::APFloat(static_cast<double>(AmountVal)));
313 }
Chris Lattner3070f982007-08-21 22:25:29 +0000314
315 // Add the inc/dec to the real part.
316 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
317
318 ComplexPairTy IncVal(NextVal, InVal.second);
319
320 // Store the updated result through the lvalue.
321 EmitStoreOfComplex(IncVal, LV.getAddress(), false); /* FIXME: Volatile */
322
323 // If this is a postinc, return the value read from memory, otherwise use the
324 // updated value.
325 return isPre ? IncVal : InVal;
326}
327
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000328ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
329 ComplexPairTy Op = Visit(E->getSubExpr());
330 llvm::Value *ResR = Builder.CreateNeg(Op.first, "neg.r");
331 llvm::Value *ResI = Builder.CreateNeg(Op.second, "neg.i");
332 return ComplexPairTy(ResR, ResI);
333}
334
Chris Lattnere98a11c2007-08-21 20:41:44 +0000335ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
336 // ~(a+ib) = a + i*-b
337 ComplexPairTy Op = Visit(E->getSubExpr());
338 llvm::Value *ResI = Builder.CreateNeg(Op.second, "conj.i");
339 return ComplexPairTy(Op.first, ResI);
340}
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000341
Chris Lattnerab340c22007-08-26 22:09:01 +0000342ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
343 llvm::Value *ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
344 llvm::Value *ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000345 return ComplexPairTy(ResR, ResI);
346}
347
Chris Lattnerab340c22007-08-26 22:09:01 +0000348ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
349 llvm::Value *ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
350 llvm::Value *ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
Chris Lattner6bc1ade2007-08-23 23:46:33 +0000351 return ComplexPairTy(ResR, ResI);
352}
353
354
Chris Lattnerab340c22007-08-26 22:09:01 +0000355ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
356 llvm::Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
357 llvm::Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second,"mul.rr");
Chris Lattner5083a532007-08-21 17:39:38 +0000358 llvm::Value *ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
Chris Lattner2823c192007-08-21 16:34:16 +0000359
Chris Lattnerab340c22007-08-26 22:09:01 +0000360 llvm::Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
361 llvm::Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
Chris Lattner5083a532007-08-21 17:39:38 +0000362 llvm::Value *ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
Chris Lattner2823c192007-08-21 16:34:16 +0000363 return ComplexPairTy(ResR, ResI);
364}
365
Chris Lattnerab340c22007-08-26 22:09:01 +0000366ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
367 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
368 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
Chris Lattner3219c5d2007-08-26 21:24:19 +0000369
370 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
371 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr, "tmp"); // a*c
372 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi, "tmp"); // b*d
373 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2, "tmp"); // ac+bd
374
375 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr, "tmp"); // c*c
376 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi, "tmp"); // d*d
377 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5, "tmp"); // cc+dd
378
379 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr, "tmp"); // b*c
380 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi, "tmp"); // a*d
381 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8, "tmp"); // bc-ad
382
383 llvm::Value *DSTr, *DSTi;
384 if (Tmp3->getType()->isFloatingPoint()) {
385 DSTr = Builder.CreateFDiv(Tmp3, Tmp6, "tmp");
386 DSTi = Builder.CreateFDiv(Tmp9, Tmp6, "tmp");
387 } else {
Chris Lattnerab340c22007-08-26 22:09:01 +0000388 if (Op.Ty->getAsComplexType()->getElementType()->isUnsignedIntegerType()) {
Chris Lattner3219c5d2007-08-26 21:24:19 +0000389 DSTr = Builder.CreateUDiv(Tmp3, Tmp6, "tmp");
390 DSTi = Builder.CreateUDiv(Tmp9, Tmp6, "tmp");
391 } else {
392 DSTr = Builder.CreateSDiv(Tmp3, Tmp6, "tmp");
393 DSTi = Builder.CreateSDiv(Tmp9, Tmp6, "tmp");
394 }
395 }
396
397 return ComplexPairTy(DSTr, DSTi);
398}
399
Chris Lattnerab340c22007-08-26 22:09:01 +0000400ComplexExprEmitter::BinOpInfo
401ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
402 BinOpInfo Ops;
403 Ops.LHS = Visit(E->getLHS());
404 Ops.RHS = Visit(E->getRHS());
405 Ops.Ty = E->getType();
406 return Ops;
407}
408
409
410// Compound assignments.
411ComplexPairTy ComplexExprEmitter::
412EmitCompoundAssign(const CompoundAssignOperator *E,
413 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
414 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType();
415
416 // Load the LHS and RHS operands.
417 LValue LHSLV = CGF.EmitLValue(E->getLHS());
418
419 BinOpInfo OpInfo;
420 OpInfo.Ty = E->getComputationType();
421
422 // We know the LHS is a complex lvalue.
423 OpInfo.LHS = EmitLoadOfComplex(LHSLV.getAddress(), false);// FIXME: Volatile.
424 OpInfo.LHS = EmitComplexToComplexCast(OpInfo.LHS, LHSTy, OpInfo.Ty);
425
426 // It is possible for the RHS to be complex or scalar.
427 OpInfo.RHS = EmitCast(E->getRHS(), OpInfo.Ty);
428
429 // Expand the binary operator.
430 ComplexPairTy Result = (this->*Func)(OpInfo);
431
432 // Truncate the result back to the LHS type.
433 Result = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy);
434
435 // Store the result value into the LHS lvalue.
436 EmitStoreOfComplex(Result, LHSLV.getAddress(), false); // FIXME: VOLATILE
437 return Result;
438}
Chris Lattner3219c5d2007-08-26 21:24:19 +0000439
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000440ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
441 assert(E->getLHS()->getType().getCanonicalType() ==
442 E->getRHS()->getType().getCanonicalType() && "Invalid assignment");
443 // Emit the RHS.
444 ComplexPairTy Val = Visit(E->getRHS());
445
446 // Compute the address to store into.
447 LValue LHS = CGF.EmitLValue(E->getLHS());
448
449 // Store into it.
450 // FIXME: Volatility!
Chris Lattner76e80332007-08-21 18:02:02 +0000451 EmitStoreOfComplex(Val, LHS.getAddress(), false);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000452 return Val;
453}
454
Chris Lattner756a4d82007-08-21 17:15:50 +0000455ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
456 CGF.EmitStmt(E->getLHS());
457 return Visit(E->getRHS());
458}
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000459
460ComplexPairTy ComplexExprEmitter::
461VisitConditionalOperator(const ConditionalOperator *E) {
462 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
463 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
464 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
465
466 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner5083a532007-08-21 17:39:38 +0000467 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000468
469 CGF.EmitBlock(LHSBlock);
470
471 // Handle the GNU extension for missing LHS.
Chris Lattner05ba49c2007-08-21 05:54:53 +0000472 assert(E->getLHS() && "Must have LHS for complex value");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000473
474 ComplexPairTy LHS = Visit(E->getLHS());
Chris Lattner5083a532007-08-21 17:39:38 +0000475 Builder.CreateBr(ContBlock);
476 LHSBlock = Builder.GetInsertBlock();
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000477
478 CGF.EmitBlock(RHSBlock);
479
480 ComplexPairTy RHS = Visit(E->getRHS());
Chris Lattner5083a532007-08-21 17:39:38 +0000481 Builder.CreateBr(ContBlock);
482 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000483
484 CGF.EmitBlock(ContBlock);
485
486 // Create a PHI node for the real part.
Chris Lattner5083a532007-08-21 17:39:38 +0000487 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), "cond.r");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000488 RealPN->reserveOperandSpace(2);
489 RealPN->addIncoming(LHS.first, LHSBlock);
490 RealPN->addIncoming(RHS.first, RHSBlock);
491
492 // Create a PHI node for the imaginary part.
Chris Lattner5083a532007-08-21 17:39:38 +0000493 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), "cond.i");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000494 ImagPN->reserveOperandSpace(2);
495 ImagPN->addIncoming(LHS.second, LHSBlock);
496 ImagPN->addIncoming(RHS.second, RHSBlock);
497
498 return ComplexPairTy(RealPN, ImagPN);
499}
500
Chris Lattnerd0b12032007-08-24 02:18:47 +0000501ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Chris Lattnerd0b12032007-08-24 02:18:47 +0000502 // Emit the LHS or RHS as appropriate.
Chris Lattner27437ca2007-10-25 00:29:32 +0000503 return Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() :E->getRHS());
Chris Lattnerd0b12032007-08-24 02:18:47 +0000504}
505
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000506//===----------------------------------------------------------------------===//
507// Entry Point into this File
508//===----------------------------------------------------------------------===//
509
510/// EmitComplexExpr - Emit the computation of the specified expression of
511/// complex type, ignoring the result.
Chris Lattner58dee102007-08-21 16:57:55 +0000512ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E) {
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000513 assert(E && E->getType()->isComplexType() &&
514 "Invalid complex expression to emit");
515
Chris Lattner58dee102007-08-21 16:57:55 +0000516 return ComplexExprEmitter(*this).Visit(const_cast<Expr*>(E));
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000517}
518
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000519/// EmitComplexExprIntoAddr - Emit the computation of the specified expression
520/// of complex type, storing into the specified Value*.
521void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E,
Chris Lattner190dbe22007-08-26 16:22:13 +0000522 llvm::Value *DestAddr,
523 bool DestIsVolatile) {
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000524 assert(E && E->getType()->isComplexType() &&
525 "Invalid complex expression to emit");
526 ComplexExprEmitter Emitter(*this);
527 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
Chris Lattner190dbe22007-08-26 16:22:13 +0000528 Emitter.EmitStoreOfComplex(Val, DestAddr, DestIsVolatile);
Chris Lattner23b1cdb2007-08-23 23:43:33 +0000529}
Chris Lattner9b655512007-08-31 22:49:20 +0000530
531/// LoadComplexFromAddr - Load a complex number from the specified address.
532ComplexPairTy CodeGenFunction::LoadComplexFromAddr(llvm::Value *SrcAddr,
533 bool SrcIsVolatile) {
534 return ComplexExprEmitter(*this).EmitLoadOfComplex(SrcAddr, SrcIsVolatile);
535}